1 /* vi:set ts=8 sts=4 sw=4:
3 * VIM - Vi IMproved by Bram Moolenaar
4 * Visual Workshop integration by Gordon Prieur
6 * Do ":help uganda" in Vim to read copying and usage conditions.
7 * Do ":help credits" in Vim to see a list of people who contributed.
8 * See README.txt for an overview of the Vim source code.
13 #if defined(FEAT_BEVAL) || defined(PROTO)
16 * Common code, invoked when the mouse is resting for a moment.
19 general_beval_cb(beval
, state
)
28 static char_u
*result
= NULL
;
35 static int recursive
= FALSE
;
37 /* Don't do anything when 'ballooneval' is off, messages scrolled the
38 * windows up or we have no beval area. */
39 if (!p_beval
|| balloonEval
== NULL
|| msg_scrolled
> 0)
42 /* Don't do this recursively. Happens when the expression evaluation
43 * takes a long time and invokes something that checks for CTRL-C typed. */
49 if (get_beval_info(balloonEval
, TRUE
, &wp
, &lnum
, &text
, &col
) == OK
)
51 bexpr
= (*wp
->w_buffer
->b_p_bexpr
== NUL
) ? p_bexpr
52 : wp
->w_buffer
->b_p_bexpr
;
56 /* Convert window pointer to number. */
57 for (cw
= firstwin
; cw
!= wp
; cw
= cw
->w_next
)
61 set_vim_var_nr(VV_BEVAL_BUFNR
, (long)wp
->w_buffer
->b_fnum
);
62 set_vim_var_nr(VV_BEVAL_WINNR
, winnr
);
63 set_vim_var_nr(VV_BEVAL_LNUM
, (long)lnum
);
64 set_vim_var_nr(VV_BEVAL_COL
, (long)(col
+ 1));
65 set_vim_var_string(VV_BEVAL_TEXT
, text
, -1);
69 * Temporarily change the curbuf, so that we can determine whether
70 * the buffer-local balloonexpr option was set insecurely.
73 curbuf
= wp
->w_buffer
;
74 use_sandbox
= was_set_insecurely((char_u
*)"balloonexpr",
75 *curbuf
->b_p_bexpr
== NUL
? 0 : OPT_LOCAL
);
82 result
= eval_to_string(bexpr
, NULL
, TRUE
);
88 set_vim_var_string(VV_BEVAL_TEXT
, NULL
, -1);
89 if (result
!= NULL
&& result
[0] != NUL
)
91 gui_mch_post_balloon(beval
, result
);
98 #ifdef FEAT_NETBEANS_INTG
99 if (bevalServers
& BEVAL_NETBEANS
)
100 netbeans_beval_cb(beval
, state
);
102 #ifdef FEAT_SUN_WORKSHOP
103 if (bevalServers
& BEVAL_WORKSHOP
)
104 workshop_beval_cb(beval
, state
);
110 /* on Win32 only get_beval_info() is required */
111 #if !defined(FEAT_GUI_W32) || defined(PROTO)
114 # include <gdk/gdkkeysyms.h>
115 # include <gtk/gtk.h>
117 # include <X11/keysym.h>
118 # ifdef FEAT_GUI_MOTIF
119 # include <Xm/PushB.h>
120 # include <Xm/Separator.h>
121 # include <Xm/List.h>
122 # include <Xm/Label.h>
123 # include <Xm/AtomMgr.h>
124 # include <Xm/Protocols.h>
127 # include <X11/Shell.h>
128 # ifdef FEAT_GUI_NEXTAW
129 # include <X11/neXtaw/Label.h>
131 # include <X11/Xaw/Label.h>
136 #include "gui_beval.h"
139 extern Widget vimShell
;
142 * Currently, we assume that there can be only one BalloonEval showing
143 * on-screen at any given moment. This variable will hold the currently
144 * showing BalloonEval or NULL if none is showing.
146 static BalloonEval
*current_beval
= NULL
;
150 static void addEventHandler
__ARGS((GtkWidget
*, BalloonEval
*));
151 static void removeEventHandler
__ARGS((BalloonEval
*));
152 static gint target_event_cb
__ARGS((GtkWidget
*, GdkEvent
*, gpointer
));
153 static gint mainwin_event_cb
__ARGS((GtkWidget
*, GdkEvent
*, gpointer
));
154 static void pointer_event
__ARGS((BalloonEval
*, int, int, unsigned));
155 static void key_event
__ARGS((BalloonEval
*, unsigned, int));
156 static gint timeout_cb
__ARGS((gpointer
));
157 static gint balloon_expose_event_cb
__ARGS((GtkWidget
*, GdkEventExpose
*, gpointer
));
159 static void balloon_draw_cb
__ARGS((GtkWidget
*, GdkRectangle
*, gpointer
));
162 static void addEventHandler
__ARGS((Widget
, BalloonEval
*));
163 static void removeEventHandler
__ARGS((BalloonEval
*));
164 static void pointerEventEH
__ARGS((Widget
, XtPointer
, XEvent
*, Boolean
*));
165 static void pointerEvent
__ARGS((BalloonEval
*, XEvent
*));
166 static void timerRoutine
__ARGS((XtPointer
, XtIntervalId
*));
168 static void cancelBalloon
__ARGS((BalloonEval
*));
169 static void requestBalloon
__ARGS((BalloonEval
*));
170 static void drawBalloon
__ARGS((BalloonEval
*));
171 static void undrawBalloon
__ARGS((BalloonEval
*beval
));
172 static void createBalloonEvalWindow
__ARGS((BalloonEval
*));
177 * Create a balloon-evaluation area for a Widget.
178 * There can be either a "mesg" for a fixed string or "mesgCB" to generate a
179 * message by calling this callback function.
180 * When "mesg" is not NULL it must remain valid for as long as the balloon is
181 * used. It is not freed here.
182 * Returns a pointer to the resulting object (NULL when out of memory).
185 gui_mch_create_beval_area(target
, mesg
, mesgCB
, clientData
)
188 void (*mesgCB
)__ARGS((BalloonEval
*, int));
192 char *display_name
; /* get from gui.dpy */
198 if (mesg
!= NULL
&& mesgCB
!= NULL
)
200 EMSG(_("E232: Cannot create BalloonEval with both message and callback"));
204 beval
= (BalloonEval
*)alloc(sizeof(BalloonEval
));
208 beval
->target
= GTK_WIDGET(target
);
209 beval
->balloonShell
= NULL
;
212 beval
->target
= (Widget
)target
;
213 beval
->balloonShell
= NULL
;
214 beval
->timerID
= (XtIntervalId
)NULL
;
215 beval
->appContext
= XtWidgetToApplicationContext((Widget
)target
);
217 beval
->showState
= ShS_NEUTRAL
;
221 beval
->msgCB
= mesgCB
;
222 beval
->clientData
= clientData
;
228 * Set up event handler which will keep its eyes on the pointer,
229 * and when the pointer rests in a certain spot for a given time
230 * interval, show the beval.
232 addEventHandler(beval
->target
, beval
);
233 createBalloonEvalWindow(beval
);
237 * Now create and save the screen width and height. Used in drawing.
239 display_name
= DisplayString(gui
.dpy
);
240 p
= strrchr(display_name
, '.');
242 screen_num
= atoi(++p
);
245 beval
->screen_width
= DisplayWidth(gui
.dpy
, screen_num
);
246 beval
->screen_height
= DisplayHeight(gui
.dpy
, screen_num
);
253 #if defined(FEAT_BEVAL_TIP) || defined(PROTO)
255 * Destroy a balloon-eval and free its associated memory.
258 gui_mch_destroy_beval_area(beval
)
261 cancelBalloon(beval
);
262 removeEventHandler(beval
);
263 /* Children will automatically be destroyed */
265 gtk_widget_destroy(beval
->balloonShell
);
267 XtDestroyWidget(beval
->balloonShell
);
271 vim_free(beval
->vts
);
278 gui_mch_enable_beval_area(beval
)
282 addEventHandler(beval
->target
, beval
);
286 gui_mch_disable_beval_area(beval
)
290 removeEventHandler(beval
);
293 #if defined(FEAT_BEVAL_TIP) || defined(PROTO)
295 * This function returns the BalloonEval * associated with the currently
296 * displayed tooltip. Returns NULL if there is no tooltip currently showing.
298 * Assumption: Only one tooltip can be shown at a time.
301 gui_mch_currently_showing_beval()
303 return current_beval
;
306 #endif /* !FEAT_GUI_W32 */
308 #if defined(FEAT_SUN_WORKSHOP) || defined(FEAT_NETBEANS_INTG) \
309 || defined(FEAT_EVAL) || defined(PROTO)
311 * Get the text and position to be evaluated for "beval".
312 * If "getword" is true the returned text is not the whole line but the
313 * relevant word in allocated memory.
314 * Returns OK or FAIL.
317 get_beval_info(beval
, getword
, winp
, lnump
, textp
, colp
)
331 row
= Y_2_ROW(beval
->y
);
332 col
= X_2_COL(beval
->x
);
334 wp
= mouse_find_win(&row
, &col
);
338 if (wp
!= NULL
&& row
< wp
->w_height
&& col
< W_WIDTH(wp
))
340 /* Found a window and the cursor is in the text. Now find the line
342 if (!mouse_comp_pos(wp
, &row
, &col
, &lnum
))
344 /* Not past end of the file. */
345 lbuf
= ml_get_buf(wp
->w_buffer
, lnum
, FALSE
);
346 if (col
<= win_linetabsize(wp
, lbuf
, (colnr_T
)MAXCOL
))
348 /* Not past end of line. */
351 /* For Netbeans we get the relevant part of the line
352 * instead of the whole line. */
354 pos_T
*spos
= NULL
, *epos
= NULL
;
358 if (lt(VIsual
, curwin
->w_cursor
))
361 epos
= &curwin
->w_cursor
;
365 spos
= &curwin
->w_cursor
;
370 col
= vcol2col(wp
, lnum
, col
) - 1;
373 && wp
->w_buffer
== curwin
->w_buffer
374 && (lnum
== spos
->lnum
375 ? col
>= (int)spos
->col
377 && (lnum
== epos
->lnum
378 ? col
<= (int)epos
->col
379 : lnum
< epos
->lnum
))
381 /* Visual mode and pointing to the line with the
382 * Visual selection: return selected text, with a
383 * maximum of one line. */
384 if (spos
->lnum
!= epos
->lnum
|| spos
->col
== epos
->col
)
387 lbuf
= ml_get_buf(curwin
->w_buffer
, VIsual
.lnum
, FALSE
);
388 lbuf
= vim_strnsave(lbuf
+ spos
->col
,
389 epos
->col
- spos
->col
+ (*p_sel
!= 'e'));
395 /* Find the word under the cursor. */
397 len
= find_ident_at_pos(wp
, lnum
, (colnr_T
)col
, &lbuf
,
398 FIND_IDENT
+ FIND_STRING
+ FIND_EVAL
);
402 lbuf
= vim_strnsave(lbuf
, len
);
412 vim_free(beval
->vts
);
413 beval
->vts
= tabstop_copy(wp
->w_buffer
->b_p_vts_ary
);
415 beval
->ts
= wp
->w_buffer
->b_p_ts
;
424 # if !defined(FEAT_GUI_W32) || defined(PROTO)
427 * Show a balloon with "mesg".
430 gui_mch_post_balloon(beval
, mesg
)
438 undrawBalloon(beval
);
440 # endif /* FEAT_GUI_W32 */
441 #endif /* FEAT_SUN_WORKSHOP || FEAT_NETBEANS_INTG || PROTO */
443 #if !defined(FEAT_GUI_W32) || defined(PROTO)
444 #if defined(FEAT_BEVAL_TIP) || defined(PROTO)
446 * Hide the given balloon.
449 gui_mch_unpost_balloon(beval
)
452 undrawBalloon(beval
);
458 * We can unconditionally use ANSI-style prototypes here since
459 * GTK+ requires an ANSI C compiler anyway.
462 addEventHandler(GtkWidget
*target
, BalloonEval
*beval
)
465 * Connect to the generic "event" signal instead of the individual
466 * signals for each event type, because the former is emitted earlier.
467 * This allows us to catch events independently of the signal handlers
470 /* Should use GTK_OBJECT() here, but that causes a lint warning... */
471 gtk_signal_connect((GtkObject
*)(target
), "event",
472 GTK_SIGNAL_FUNC(target_event_cb
),
475 * Nasty: Key press events go to the main window thus the drawing area
476 * will never see them. This means we have to connect to the main window
477 * as well in order to catch those events.
479 if (gtk_socket_id
== 0 && gui
.mainwin
!= NULL
480 && gtk_widget_is_ancestor(target
, gui
.mainwin
))
482 gtk_signal_connect((GtkObject
*)(gui
.mainwin
), "event",
483 GTK_SIGNAL_FUNC(mainwin_event_cb
),
489 removeEventHandler(BalloonEval
*beval
)
491 /* LINTED: avoid warning: dubious operation on enum */
492 gtk_signal_disconnect_by_func((GtkObject
*)(beval
->target
),
493 GTK_SIGNAL_FUNC(target_event_cb
),
496 if (gtk_socket_id
== 0 && gui
.mainwin
!= NULL
497 && gtk_widget_is_ancestor(beval
->target
, gui
.mainwin
))
499 /* LINTED: avoid warning: dubious operation on enum */
500 gtk_signal_disconnect_by_func((GtkObject
*)(gui
.mainwin
),
501 GTK_SIGNAL_FUNC(mainwin_event_cb
),
507 target_event_cb(GtkWidget
*widget
, GdkEvent
*event
, gpointer data
)
509 BalloonEval
*beval
= (BalloonEval
*)data
;
513 case GDK_ENTER_NOTIFY
:
514 pointer_event(beval
, (int)event
->crossing
.x
,
515 (int)event
->crossing
.y
,
516 event
->crossing
.state
);
518 case GDK_MOTION_NOTIFY
:
519 if (event
->motion
.is_hint
)
523 GdkModifierType state
;
525 * GDK_POINTER_MOTION_HINT_MASK is set, thus we cannot obtain
526 * the coordinates from the GdkEventMotion struct directly.
528 gdk_window_get_pointer(widget
->window
, &x
, &y
, &state
);
529 pointer_event(beval
, x
, y
, (unsigned int)state
);
533 pointer_event(beval
, (int)event
->motion
.x
,
534 (int)event
->motion
.y
,
535 event
->motion
.state
);
538 case GDK_LEAVE_NOTIFY
:
540 * Ignore LeaveNotify events that are not "normal".
541 * Apparently we also get it when somebody else grabs focus.
543 if (event
->crossing
.mode
== GDK_CROSSING_NORMAL
)
544 cancelBalloon(beval
);
546 case GDK_BUTTON_PRESS
:
550 cancelBalloon(beval
);
553 key_event(beval
, event
->key
.keyval
, TRUE
);
555 case GDK_KEY_RELEASE
:
556 key_event(beval
, event
->key
.keyval
, FALSE
);
562 return FALSE
; /* continue emission */
566 mainwin_event_cb(GtkWidget
*widget UNUSED
, GdkEvent
*event
, gpointer data
)
568 BalloonEval
*beval
= (BalloonEval
*)data
;
573 key_event(beval
, event
->key
.keyval
, TRUE
);
575 case GDK_KEY_RELEASE
:
576 key_event(beval
, event
->key
.keyval
, FALSE
);
582 return FALSE
; /* continue emission */
586 pointer_event(BalloonEval
*beval
, int x
, int y
, unsigned state
)
590 distance
= ABS(x
- beval
->x
) + ABS(y
- beval
->y
);
595 * Moved out of the balloon location: cancel it.
596 * Remember button state
598 beval
->state
= state
;
599 cancelBalloon(beval
);
601 /* Mouse buttons are pressed - no balloon now */
602 if (!(state
& ((int)GDK_BUTTON1_MASK
| (int)GDK_BUTTON2_MASK
603 | (int)GDK_BUTTON3_MASK
)))
608 if (state
& (int)GDK_MOD1_MASK
)
611 * Alt is pressed -- enter super-evaluate-mode,
612 * where there is no time delay
614 if (beval
->msgCB
!= NULL
)
616 beval
->showState
= ShS_PENDING
;
617 (*beval
->msgCB
)(beval
, state
);
622 beval
->timerID
= gtk_timeout_add((guint32
)p_bdlay
,
630 key_event(BalloonEval
*beval
, unsigned keyval
, int is_keypress
)
632 if (beval
->showState
== ShS_SHOWING
&& beval
->msgCB
!= NULL
)
638 beval
->showState
= ShS_UPDATE_PENDING
;
639 (*beval
->msgCB
)(beval
, (is_keypress
)
640 ? (int)GDK_SHIFT_MASK
: 0);
644 beval
->showState
= ShS_UPDATE_PENDING
;
645 (*beval
->msgCB
)(beval
, (is_keypress
)
646 ? (int)GDK_CONTROL_MASK
: 0);
649 /* Don't do this for key release, we apparently get these with
650 * focus changes in some GTK version. */
652 cancelBalloon(beval
);
657 cancelBalloon(beval
);
661 timeout_cb(gpointer data
)
663 BalloonEval
*beval
= (BalloonEval
*)data
;
667 * If the timer event happens then the mouse has stopped long enough for
668 * a request to be started. The request will only send to the debugger if
669 * there the mouse is pointing at real data.
671 requestBalloon(beval
);
673 return FALSE
; /* don't call me again */
677 balloon_expose_event_cb(GtkWidget
*widget
,
678 GdkEventExpose
*event
,
679 gpointer data UNUSED
)
681 gtk_paint_flat_box(widget
->style
, widget
->window
,
682 GTK_STATE_NORMAL
, GTK_SHADOW_OUT
,
683 &event
->area
, widget
, "tooltip",
686 return FALSE
; /* continue emission */
691 balloon_draw_cb(GtkWidget
*widget
, GdkRectangle
*area
, gpointer data
)
694 GdkRectangle child_area
;
696 gtk_paint_flat_box(widget
->style
, widget
->window
,
697 GTK_STATE_NORMAL
, GTK_SHADOW_OUT
,
698 area
, widget
, "tooltip",
701 child
= GTK_BIN(widget
)->child
;
703 if (gtk_widget_intersect(child
, area
, &child_area
))
704 gtk_widget_draw(child
, &child_area
);
708 #else /* !FEAT_GUI_GTK */
711 addEventHandler(target
, beval
)
715 XtAddEventHandler(target
,
716 PointerMotionMask
| EnterWindowMask
|
717 LeaveWindowMask
| ButtonPressMask
| KeyPressMask
|
720 pointerEventEH
, (XtPointer
)beval
);
724 removeEventHandler(beval
)
727 XtRemoveEventHandler(beval
->target
,
728 PointerMotionMask
| EnterWindowMask
|
729 LeaveWindowMask
| ButtonPressMask
| KeyPressMask
|
732 pointerEventEH
, (XtPointer
)beval
);
737 * The X event handler. All it does is call the real event handler.
740 pointerEventEH(w
, client_data
, event
, unused
)
742 XtPointer client_data
;
744 Boolean
*unused UNUSED
;
746 BalloonEval
*beval
= (BalloonEval
*)client_data
;
747 pointerEvent(beval
, event
);
752 * The real event handler. Called by pointerEventEH() whenever an event we are
753 * interested in occurs.
757 pointerEvent(beval
, event
)
761 Position distance
; /* a measure of how much the ponter moved */
762 Position delta
; /* used to compute distance */
768 delta
= event
->xmotion
.x
- beval
->x
;
772 delta
= event
->xmotion
.y
- beval
->y
;
779 * Moved out of the balloon location: cancel it.
780 * Remember button state
782 beval
->state
= event
->xmotion
.state
;
783 if (beval
->state
& (Button1Mask
|Button2Mask
|Button3Mask
))
785 /* Mouse buttons are pressed - no balloon now */
786 cancelBalloon(beval
);
788 else if (beval
->state
& (Mod1Mask
|Mod2Mask
|Mod3Mask
))
791 * Alt is pressed -- enter super-evaluate-mode,
792 * where there is no time delay
794 beval
->x
= event
->xmotion
.x
;
795 beval
->y
= event
->xmotion
.y
;
796 beval
->x_root
= event
->xmotion
.x_root
;
797 beval
->y_root
= event
->xmotion
.y_root
;
798 cancelBalloon(beval
);
799 if (beval
->msgCB
!= NULL
)
801 beval
->showState
= ShS_PENDING
;
802 (*beval
->msgCB
)(beval
, beval
->state
);
807 beval
->x
= event
->xmotion
.x
;
808 beval
->y
= event
->xmotion
.y
;
809 beval
->x_root
= event
->xmotion
.x_root
;
810 beval
->y_root
= event
->xmotion
.y_root
;
811 cancelBalloon(beval
);
812 beval
->timerID
= XtAppAddTimeOut( beval
->appContext
,
813 (long_u
)p_bdlay
, timerRoutine
, beval
);
819 * Motif and Athena version: Keystrokes will be caught by the
820 * "textArea" widget, and handled in gui_x11_key_hit_cb().
823 if (beval
->showState
== ShS_SHOWING
&& beval
->msgCB
!= NULL
)
828 XtTranslateKeycode(gui
.dpy
,
829 event
->xkey
.keycode
, event
->xkey
.state
,
831 if (keysym
== XK_Shift_L
|| keysym
== XK_Shift_R
)
833 beval
->showState
= ShS_UPDATE_PENDING
;
834 (*beval
->msgCB
)(beval
, ShiftMask
);
836 else if (keysym
== XK_Control_L
|| keysym
== XK_Control_R
)
838 beval
->showState
= ShS_UPDATE_PENDING
;
839 (*beval
->msgCB
)(beval
, ControlMask
);
842 cancelBalloon(beval
);
845 cancelBalloon(beval
);
849 if (beval
->showState
== ShS_SHOWING
&& beval
->msgCB
!= NULL
)
854 XtTranslateKeycode(gui
.dpy
, event
->xkey
.keycode
,
855 event
->xkey
.state
, &modifier
, &keysym
);
856 if ((keysym
== XK_Shift_L
) || (keysym
== XK_Shift_R
)) {
857 beval
->showState
= ShS_UPDATE_PENDING
;
858 (*beval
->msgCB
)(beval
, 0);
860 else if ((keysym
== XK_Control_L
) || (keysym
== XK_Control_R
))
862 beval
->showState
= ShS_UPDATE_PENDING
;
863 (*beval
->msgCB
)(beval
, 0);
866 cancelBalloon(beval
);
869 cancelBalloon(beval
);
873 /* Ignore LeaveNotify events that are not "normal".
874 * Apparently we also get it when somebody else grabs focus.
875 * Happens for me every two seconds (some clipboard tool?) */
876 if (event
->xcrossing
.mode
== NotifyNormal
)
877 cancelBalloon(beval
);
881 cancelBalloon(beval
);
892 XtIntervalId
*id UNUSED
;
894 BalloonEval
*beval
= (BalloonEval
*)dx
;
896 beval
->timerID
= (XtIntervalId
)NULL
;
899 * If the timer event happens then the mouse has stopped long enough for
900 * a request to be started. The request will only send to the debugger if
901 * there the mouse is pointing at real data.
903 requestBalloon(beval
);
906 #endif /* !FEAT_GUI_GTK */
909 requestBalloon(beval
)
912 if (beval
->showState
!= ShS_PENDING
)
914 /* Determine the beval to display */
915 if (beval
->msgCB
!= NULL
)
917 beval
->showState
= ShS_PENDING
;
918 (*beval
->msgCB
)(beval
, beval
->state
);
920 else if (beval
->msg
!= NULL
)
929 * Convert the string to UTF-8 if 'encoding' is not "utf-8".
930 * Replace any non-printable characters and invalid bytes sequences with
931 * "^X" or "<xx>" escapes, and apply SpecialKey highlighting to them.
932 * TAB and NL are passed through unscathed.
934 # define IS_NONPRINTABLE(c) (((c) < 0x20 && (c) != TAB && (c) != NL) \
937 set_printable_label_text(GtkLabel
*label
, char_u
*text
)
939 char_u
*convbuf
= NULL
;
946 PangoAttrList
*attr_list
;
948 /* Convert to UTF-8 if it isn't already */
949 if (output_conv
.vc_type
!= CONV_NONE
)
951 convbuf
= string_convert(&output_conv
, text
, NULL
);
956 /* First let's see how much we need to allocate */
958 for (p
= text
; *p
!= NUL
; p
+= charlen
)
960 if ((*p
& 0x80) == 0) /* be quick for ASCII */
963 len
+= IS_NONPRINTABLE(*p
) ? 2 : 1; /* nonprintable: ^X */
967 charlen
= utf_ptr2len(p
);
968 uc
= utf_ptr2char(p
);
970 if (charlen
!= utf_char2len(uc
))
971 charlen
= 1; /* reject overlong sequences */
973 if (charlen
== 1 || uc
< 0xa0) /* illegal byte or */
974 len
+= 4; /* control char: <xx> */
975 else if (!utf_printable(uc
))
976 /* Note: we assume here that utf_printable() doesn't
977 * care about characters outside the BMP. */
978 len
+= 6; /* nonprintable: <xxxx> */
984 attr_list
= pango_attr_list_new();
985 buf
= alloc(len
+ 1);
987 /* Now go for the real work */
991 PangoAttribute
*attr
;
993 GdkColor color
= { 0, 0, 0, 0 };
995 /* Look up the RGB values of the SpecialKey foreground color. */
996 aep
= syn_gui_attr2entry(hl_attr(HLF_8
));
997 pixel
= (aep
!= NULL
) ? aep
->ae_u
.gui
.fg_color
: INVALCOLOR
;
998 if (pixel
!= INVALCOLOR
)
999 gdk_colormap_query_color(gtk_widget_get_colormap(gui
.drawarea
),
1000 (unsigned long)pixel
, &color
);
1006 /* Be quick for ASCII */
1007 if ((*p
& 0x80) == 0 && !IS_NONPRINTABLE(*p
))
1013 charlen
= utf_ptr2len(p
);
1014 uc
= utf_ptr2char(p
);
1016 if (charlen
!= utf_char2len(uc
))
1017 charlen
= 1; /* reject overlong sequences */
1019 if (charlen
== 1 || uc
< 0xa0 || !utf_printable(uc
))
1023 /* Careful: we can't just use transchar_byte() here,
1024 * since 'encoding' is not necessarily set to "utf-8". */
1025 if (*p
& 0x80 && charlen
== 1)
1027 transchar_hex(pdest
, *p
); /* <xx> */
1030 else if (uc
>= 0x80)
1032 /* Note: we assume here that utf_printable() doesn't
1033 * care about characters outside the BMP. */
1034 transchar_hex(pdest
, uc
); /* <xx> or <xxxx> */
1035 outlen
= (uc
< 0x100) ? 4 : 6;
1039 transchar_nonprint(pdest
, *p
); /* ^X */
1042 if (pixel
!= INVALCOLOR
)
1044 attr
= pango_attr_foreground_new(
1045 color
.red
, color
.green
, color
.blue
);
1046 attr
->start_index
= pdest
- buf
;
1047 attr
->end_index
= pdest
- buf
+ outlen
;
1048 pango_attr_list_insert(attr_list
, attr
);
1057 while (--charlen
!= 0);
1066 gtk_label_set_text(label
, (const char *)buf
);
1069 gtk_label_set_attributes(label
, attr_list
);
1070 pango_attr_list_unref(attr_list
);
1072 # undef IS_NONPRINTABLE
1073 # endif /* HAVE_GTK2 */
1079 drawBalloon(BalloonEval
*beval
)
1081 if (beval
->msg
!= NULL
)
1083 GtkRequisition requisition
;
1088 int x_offset
= EVAL_OFFSET_X
;
1089 int y_offset
= EVAL_OFFSET_Y
;
1091 PangoLayout
*layout
;
1093 # ifdef HAVE_GTK_MULTIHEAD
1096 screen
= gtk_widget_get_screen(beval
->target
);
1097 gtk_window_set_screen(GTK_WINDOW(beval
->balloonShell
), screen
);
1098 screen_w
= gdk_screen_get_width(screen
);
1099 screen_h
= gdk_screen_get_height(screen
);
1101 screen_w
= gdk_screen_width();
1102 screen_h
= gdk_screen_height();
1104 gtk_widget_ensure_style(beval
->balloonShell
);
1105 gtk_widget_ensure_style(beval
->balloonLabel
);
1108 set_printable_label_text(GTK_LABEL(beval
->balloonLabel
), beval
->msg
);
1110 * Dirty trick: Enable wrapping mode on the label's layout behind its
1111 * back. This way GtkLabel won't try to constrain the wrap width to a
1112 * builtin maximum value of about 65 Latin characters.
1114 layout
= gtk_label_get_layout(GTK_LABEL(beval
->balloonLabel
));
1115 # ifdef PANGO_WRAP_WORD_CHAR
1116 pango_layout_set_wrap(layout
, PANGO_WRAP_WORD_CHAR
);
1118 pango_layout_set_wrap(layout
, PANGO_WRAP_WORD
);
1120 pango_layout_set_width(layout
,
1121 /* try to come up with some reasonable width */
1122 PANGO_SCALE
* CLAMP(gui
.num_cols
* gui
.char_width
,
1124 MAX(20, screen_w
- 20)));
1126 /* Calculate the balloon's width and height. */
1127 gtk_widget_size_request(beval
->balloonShell
, &requisition
);
1129 gtk_label_set_line_wrap(GTK_LABEL(beval
->balloonLabel
), FALSE
);
1130 gtk_label_set_text(GTK_LABEL(beval
->balloonLabel
),
1131 (const char *)beval
->msg
);
1133 /* Calculate the balloon's width and height. */
1134 gtk_widget_size_request(beval
->balloonShell
, &requisition
);
1136 * Unfortunately, the dirty trick used above to get around the builtin
1137 * maximum wrap width of GtkLabel doesn't work with GTK+ 1. Thus if
1138 * and only if it's absolutely necessary to avoid drawing off-screen,
1139 * do enable wrapping now and recalculate the size request.
1141 if (requisition
.width
> screen_w
)
1143 gtk_label_set_line_wrap(GTK_LABEL(beval
->balloonLabel
), TRUE
);
1144 gtk_widget_size_request(beval
->balloonShell
, &requisition
);
1148 /* Compute position of the balloon area */
1149 gdk_window_get_origin(beval
->target
->window
, &x
, &y
);
1153 /* Get out of the way of the mouse pointer */
1154 if (x
+ x_offset
+ requisition
.width
> screen_w
)
1156 if (y
+ y_offset
+ requisition
.height
> screen_h
)
1157 y_offset
= -requisition
.height
- EVAL_OFFSET_Y
;
1159 /* Sanitize values */
1160 x
= CLAMP(x
+ x_offset
, 0, MAX(0, screen_w
- requisition
.width
));
1161 y
= CLAMP(y
+ y_offset
, 0, MAX(0, screen_h
- requisition
.height
));
1163 /* Show the balloon */
1164 gtk_widget_set_uposition(beval
->balloonShell
, x
, y
);
1165 gtk_widget_show(beval
->balloonShell
);
1167 beval
->showState
= ShS_SHOWING
;
1175 undrawBalloon(BalloonEval
*beval
)
1177 if (beval
->balloonShell
!= NULL
)
1178 gtk_widget_hide(beval
->balloonShell
);
1179 beval
->showState
= ShS_NEUTRAL
;
1183 cancelBalloon(BalloonEval
*beval
)
1185 if (beval
->showState
== ShS_SHOWING
1186 || beval
->showState
== ShS_UPDATE_PENDING
)
1187 undrawBalloon(beval
);
1189 if (beval
->timerID
!= 0)
1191 gtk_timeout_remove(beval
->timerID
);
1194 beval
->showState
= ShS_NEUTRAL
;
1198 createBalloonEvalWindow(BalloonEval
*beval
)
1200 beval
->balloonShell
= gtk_window_new(GTK_WINDOW_POPUP
);
1202 gtk_widget_set_app_paintable(beval
->balloonShell
, TRUE
);
1203 gtk_window_set_policy(GTK_WINDOW(beval
->balloonShell
), FALSE
, FALSE
, TRUE
);
1204 gtk_widget_set_name(beval
->balloonShell
, "gtk-tooltips");
1205 gtk_container_border_width(GTK_CONTAINER(beval
->balloonShell
), 4);
1207 gtk_signal_connect((GtkObject
*)(beval
->balloonShell
), "expose_event",
1208 GTK_SIGNAL_FUNC(balloon_expose_event_cb
), NULL
);
1210 gtk_signal_connect((GtkObject
*)(beval
->balloonShell
), "draw",
1211 GTK_SIGNAL_FUNC(balloon_draw_cb
), NULL
);
1213 beval
->balloonLabel
= gtk_label_new(NULL
);
1215 gtk_label_set_line_wrap(GTK_LABEL(beval
->balloonLabel
), FALSE
);
1216 gtk_label_set_justify(GTK_LABEL(beval
->balloonLabel
), GTK_JUSTIFY_LEFT
);
1217 gtk_misc_set_alignment(GTK_MISC(beval
->balloonLabel
), 0.5f
, 0.5f
);
1218 gtk_widget_set_name(beval
->balloonLabel
, "vim-balloon-label");
1219 gtk_widget_show(beval
->balloonLabel
);
1221 gtk_container_add(GTK_CONTAINER(beval
->balloonShell
), beval
->balloonLabel
);
1224 #else /* !FEAT_GUI_GTK */
1238 if (beval
->msg
!= NULL
)
1240 /* Show the Balloon */
1242 /* Calculate the label's width and height */
1243 #ifdef FEAT_GUI_MOTIF
1246 /* For the callback function we parse NL characters to create a
1247 * multi-line label. This doesn't work for all languages, but
1248 * XmStringCreateLocalized() doesn't do multi-line labels... */
1249 if (beval
->msgCB
!= NULL
)
1250 s
= XmStringCreateLtoR((char *)beval
->msg
, XmFONTLIST_DEFAULT_TAG
);
1252 s
= XmStringCreateLocalized((char *)beval
->msg
);
1256 fl
= gui_motif_fontset2fontlist(&gui
.tooltip_fontset
);
1259 XmStringExtent(fl
, s
, &w
, &h
);
1263 w
+= gui
.border_offset
<< 1;
1264 h
+= gui
.border_offset
<< 1;
1265 XtVaSetValues(beval
->balloonLabel
, XmNlabelString
, s
, NULL
);
1268 /* Assume XtNinternational == True */
1270 XFontSetExtents
*ext
;
1272 XtVaGetValues(beval
->balloonLabel
, XtNfontSet
, &fset
, NULL
);
1273 ext
= XExtentsOfFontSet(fset
);
1274 h
= ext
->max_ink_extent
.height
;
1275 w
= XmbTextEscapement(fset
,
1277 (int)STRLEN(beval
->msg
));
1278 w
+= gui
.border_offset
<< 1;
1279 h
+= gui
.border_offset
<< 1;
1280 XtVaSetValues(beval
->balloonLabel
, XtNlabel
, beval
->msg
, NULL
);
1283 /* Compute position of the balloon area */
1284 tx
= beval
->x_root
+ EVAL_OFFSET_X
;
1285 ty
= beval
->y_root
+ EVAL_OFFSET_Y
;
1286 if ((tx
+ w
) > beval
->screen_width
)
1287 tx
= beval
->screen_width
- w
;
1288 if ((ty
+ h
) > beval
->screen_height
)
1289 ty
= beval
->screen_height
- h
;
1290 #ifdef FEAT_GUI_MOTIF
1291 XtVaSetValues(beval
->balloonShell
,
1297 XtVaSetValues(beval
->balloonShell
,
1302 /* Set tooltip colors */
1306 #ifdef FEAT_GUI_MOTIF
1307 args
[0].name
= XmNbackground
;
1308 args
[0].value
= gui
.tooltip_bg_pixel
;
1309 args
[1].name
= XmNforeground
;
1310 args
[1].value
= gui
.tooltip_fg_pixel
;
1312 args
[0].name
= XtNbackground
;
1313 args
[0].value
= gui
.tooltip_bg_pixel
;
1314 args
[1].name
= XtNforeground
;
1315 args
[1].value
= gui
.tooltip_fg_pixel
;
1317 XtSetValues(beval
->balloonLabel
, &args
[0], XtNumber(args
));
1320 XtPopup(beval
->balloonShell
, XtGrabNone
);
1322 beval
->showState
= ShS_SHOWING
;
1324 current_beval
= beval
;
1332 undrawBalloon(beval
)
1335 if (beval
->balloonShell
!= (Widget
)0)
1336 XtPopdown(beval
->balloonShell
);
1337 beval
->showState
= ShS_NEUTRAL
;
1339 current_beval
= NULL
;
1343 cancelBalloon(beval
)
1346 if (beval
->showState
== ShS_SHOWING
1347 || beval
->showState
== ShS_UPDATE_PENDING
)
1348 undrawBalloon(beval
);
1350 if (beval
->timerID
!= (XtIntervalId
)NULL
)
1352 XtRemoveTimeOut(beval
->timerID
);
1353 beval
->timerID
= (XtIntervalId
)NULL
;
1355 beval
->showState
= ShS_NEUTRAL
;
1360 createBalloonEvalWindow(beval
)
1367 #ifdef FEAT_GUI_MOTIF
1368 XtSetArg(args
[n
], XmNallowShellResize
, True
); n
++;
1369 beval
->balloonShell
= XtAppCreateShell("balloonEval", "BalloonEval",
1370 overrideShellWidgetClass
, gui
.dpy
, args
, n
);
1373 XtSetArg(args
[n
], XtNallowShellResize
, True
); n
++;
1374 beval
->balloonShell
= XtAppCreateShell("balloonEval", "BalloonEval",
1375 overrideShellWidgetClass
, gui
.dpy
, args
, n
);
1379 #ifdef FEAT_GUI_MOTIF
1383 fl
= gui_motif_fontset2fontlist(&gui
.tooltip_fontset
);
1384 XtSetArg(args
[n
], XmNforeground
, gui
.tooltip_fg_pixel
); n
++;
1385 XtSetArg(args
[n
], XmNbackground
, gui
.tooltip_bg_pixel
); n
++;
1386 XtSetArg(args
[n
], XmNfontList
, fl
); n
++;
1387 XtSetArg(args
[n
], XmNalignment
, XmALIGNMENT_BEGINNING
); n
++;
1388 beval
->balloonLabel
= XtCreateManagedWidget("balloonLabel",
1389 xmLabelWidgetClass
, beval
->balloonShell
, args
, n
);
1391 #else /* FEAT_GUI_ATHENA */
1392 XtSetArg(args
[n
], XtNforeground
, gui
.tooltip_fg_pixel
); n
++;
1393 XtSetArg(args
[n
], XtNbackground
, gui
.tooltip_bg_pixel
); n
++;
1394 XtSetArg(args
[n
], XtNinternational
, True
); n
++;
1395 XtSetArg(args
[n
], XtNfontSet
, gui
.tooltip_fontset
); n
++;
1396 beval
->balloonLabel
= XtCreateManagedWidget("balloonLabel",
1397 labelWidgetClass
, beval
->balloonShell
, args
, n
);
1401 #endif /* !FEAT_GUI_GTK */
1402 #endif /* !FEAT_GUI_W32 */
1404 #endif /* FEAT_BEVAL */