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.
20 general_beval_cb(beval
, state
)
29 static char_u
*result
= NULL
;
36 static int recursive
= FALSE
;
38 /* Don't do anything when 'ballooneval' is off, messages scrolled the
39 * windows up or we have no beval area. */
40 if (!p_beval
|| balloonEval
== NULL
|| msg_scrolled
> 0)
43 /* Don't do this recursively. Happens when the expression evaluation
44 * takes a long time and invokes something that checks for CTRL-C typed. */
50 if (get_beval_info(balloonEval
, TRUE
, &wp
, &lnum
, &text
, &col
) == OK
)
52 bexpr
= (*wp
->w_buffer
->b_p_bexpr
== NUL
) ? p_bexpr
53 : wp
->w_buffer
->b_p_bexpr
;
57 /* Convert window pointer to number. */
58 for (cw
= firstwin
; cw
!= wp
; cw
= cw
->w_next
)
62 set_vim_var_nr(VV_BEVAL_BUFNR
, (long)wp
->w_buffer
->b_fnum
);
63 set_vim_var_nr(VV_BEVAL_WINNR
, winnr
);
64 set_vim_var_nr(VV_BEVAL_LNUM
, (long)lnum
);
65 set_vim_var_nr(VV_BEVAL_COL
, (long)(col
+ 1));
66 set_vim_var_string(VV_BEVAL_TEXT
, text
, -1);
70 * Temporarily change the curbuf, so that we can determine whether
71 * the buffer-local balloonexpr option was set insecurely.
74 curbuf
= wp
->w_buffer
;
75 use_sandbox
= was_set_insecurely((char_u
*)"balloonexpr",
76 *curbuf
->b_p_bexpr
== NUL
? 0 : OPT_LOCAL
);
83 result
= eval_to_string(bexpr
, NULL
, TRUE
);
89 set_vim_var_string(VV_BEVAL_TEXT
, NULL
, -1);
90 if (result
!= NULL
&& result
[0] != NUL
)
92 gui_mch_post_balloon(beval
, result
);
99 #ifdef FEAT_NETBEANS_INTG
100 if (bevalServers
& BEVAL_NETBEANS
)
101 netbeans_beval_cb(beval
, state
);
103 #ifdef FEAT_SUN_WORKSHOP
104 if (bevalServers
& BEVAL_WORKSHOP
)
105 workshop_beval_cb(beval
, state
);
111 /* on Win32 only get_beval_info() is required */
112 #if !defined(FEAT_GUI_W32) || defined(PROTO)
115 # include <gdk/gdkkeysyms.h>
116 # include <gtk/gtk.h>
118 # include <X11/keysym.h>
119 # ifdef FEAT_GUI_MOTIF
120 # include <Xm/PushB.h>
121 # include <Xm/Separator.h>
122 # include <Xm/List.h>
123 # include <Xm/Label.h>
124 # include <Xm/AtomMgr.h>
125 # include <Xm/Protocols.h>
128 # include <X11/Shell.h>
129 # ifdef FEAT_GUI_NEXTAW
130 # include <X11/neXtaw/Label.h>
132 # include <X11/Xaw/Label.h>
137 #include "gui_beval.h"
140 extern Widget vimShell
;
143 * Currently, we assume that there can be only one BalloonEval showing
144 * on-screen at any given moment. This variable will hold the currently
145 * showing BalloonEval or NULL if none is showing.
147 static BalloonEval
*current_beval
= NULL
;
151 static void addEventHandler
__ARGS((GtkWidget
*, BalloonEval
*));
152 static void removeEventHandler
__ARGS((BalloonEval
*));
153 static gint target_event_cb
__ARGS((GtkWidget
*, GdkEvent
*, gpointer
));
154 static gint mainwin_event_cb
__ARGS((GtkWidget
*, GdkEvent
*, gpointer
));
155 static void pointer_event
__ARGS((BalloonEval
*, int, int, unsigned));
156 static void key_event
__ARGS((BalloonEval
*, unsigned, int));
157 static gint timeout_cb
__ARGS((gpointer
));
158 static gint balloon_expose_event_cb
__ARGS((GtkWidget
*, GdkEventExpose
*, gpointer
));
160 static void balloon_draw_cb
__ARGS((GtkWidget
*, GdkRectangle
*, gpointer
));
163 static void addEventHandler
__ARGS((Widget
, BalloonEval
*));
164 static void removeEventHandler
__ARGS((BalloonEval
*));
165 static void pointerEventEH
__ARGS((Widget
, XtPointer
, XEvent
*, Boolean
*));
166 static void pointerEvent
__ARGS((BalloonEval
*, XEvent
*));
167 static void timerRoutine
__ARGS((XtPointer
, XtIntervalId
*));
169 static void cancelBalloon
__ARGS((BalloonEval
*));
170 static void requestBalloon
__ARGS((BalloonEval
*));
171 static void drawBalloon
__ARGS((BalloonEval
*));
172 static void undrawBalloon
__ARGS((BalloonEval
*beval
));
173 static void createBalloonEvalWindow
__ARGS((BalloonEval
*));
178 * Create a balloon-evaluation area for a Widget.
179 * There can be either a "mesg" for a fixed string or "mesgCB" to generate a
180 * message by calling this callback function.
181 * When "mesg" is not NULL it must remain valid for as long as the balloon is
182 * used. It is not freed here.
183 * Returns a pointer to the resulting object (NULL when out of memory).
186 gui_mch_create_beval_area(target
, mesg
, mesgCB
, clientData
)
189 void (*mesgCB
)__ARGS((BalloonEval
*, int));
193 char *display_name
; /* get from gui.dpy */
199 if (mesg
!= NULL
&& mesgCB
!= NULL
)
201 EMSG(_("E232: Cannot create BalloonEval with both message and callback"));
205 beval
= (BalloonEval
*)alloc(sizeof(BalloonEval
));
209 beval
->target
= GTK_WIDGET(target
);
210 beval
->balloonShell
= NULL
;
213 beval
->target
= (Widget
)target
;
214 beval
->balloonShell
= NULL
;
215 beval
->timerID
= (XtIntervalId
)NULL
;
216 beval
->appContext
= XtWidgetToApplicationContext((Widget
)target
);
218 beval
->showState
= ShS_NEUTRAL
;
222 beval
->msgCB
= mesgCB
;
223 beval
->clientData
= clientData
;
226 * Set up event handler which will keep its eyes on the pointer,
227 * and when the pointer rests in a certain spot for a given time
228 * interval, show the beval.
230 addEventHandler(beval
->target
, beval
);
231 createBalloonEvalWindow(beval
);
235 * Now create and save the screen width and height. Used in drawing.
237 display_name
= DisplayString(gui
.dpy
);
238 p
= strrchr(display_name
, '.');
240 screen_num
= atoi(++p
);
243 beval
->screen_width
= DisplayWidth(gui
.dpy
, screen_num
);
244 beval
->screen_height
= DisplayHeight(gui
.dpy
, screen_num
);
251 #if defined(FEAT_BEVAL_TIP) || defined(PROTO)
253 * Destroy a balloon-eval and free its associated memory.
256 gui_mch_destroy_beval_area(beval
)
259 cancelBalloon(beval
);
260 removeEventHandler(beval
);
261 /* Children will automatically be destroyed */
263 gtk_widget_destroy(beval
->balloonShell
);
265 XtDestroyWidget(beval
->balloonShell
);
272 gui_mch_enable_beval_area(beval
)
276 addEventHandler(beval
->target
, beval
);
280 gui_mch_disable_beval_area(beval
)
284 removeEventHandler(beval
);
287 #if defined(FEAT_BEVAL_TIP) || defined(PROTO)
289 * This function returns the BalloonEval * associated with the currently
290 * displayed tooltip. Returns NULL if there is no tooltip currently showing.
292 * Assumption: Only one tooltip can be shown at a time.
295 gui_mch_currently_showing_beval()
297 return current_beval
;
300 #endif /* !FEAT_GUI_W32 */
302 #if defined(FEAT_SUN_WORKSHOP) || defined(FEAT_NETBEANS_INTG) \
303 || defined(FEAT_EVAL) || defined(PROTO)
305 * Get the text and position to be evaluated for "beval".
306 * If "getword" is true the returned text is not the whole line but the
307 * relevant word in allocated memory.
308 * Returns OK or FAIL.
311 get_beval_info(beval
, getword
, winp
, lnump
, textp
, colp
)
325 row
= Y_2_ROW(beval
->y
);
326 col
= X_2_COL(beval
->x
);
328 wp
= mouse_find_win(&row
, &col
);
332 if (wp
!= NULL
&& row
< wp
->w_height
&& col
< W_WIDTH(wp
))
334 /* Found a window and the cursor is in the text. Now find the line
336 if (!mouse_comp_pos(wp
, &row
, &col
, &lnum
))
338 /* Not past end of the file. */
339 lbuf
= ml_get_buf(wp
->w_buffer
, lnum
, FALSE
);
340 if (col
<= win_linetabsize(wp
, lbuf
, (colnr_T
)MAXCOL
))
342 /* Not past end of line. */
345 /* For Netbeans we get the relevant part of the line
346 * instead of the whole line. */
348 pos_T
*spos
= NULL
, *epos
= NULL
;
352 if (lt(VIsual
, curwin
->w_cursor
))
355 epos
= &curwin
->w_cursor
;
359 spos
= &curwin
->w_cursor
;
364 col
= vcol2col(wp
, lnum
, col
) - 1;
367 && wp
->w_buffer
== curwin
->w_buffer
368 && (lnum
== spos
->lnum
369 ? col
>= (int)spos
->col
371 && (lnum
== epos
->lnum
372 ? col
<= (int)epos
->col
373 : lnum
< epos
->lnum
))
375 /* Visual mode and pointing to the line with the
376 * Visual selection: return selected text, with a
377 * maximum of one line. */
378 if (spos
->lnum
!= epos
->lnum
|| spos
->col
== epos
->col
)
381 lbuf
= ml_get_buf(curwin
->w_buffer
, VIsual
.lnum
, FALSE
);
382 lbuf
= vim_strnsave(lbuf
+ spos
->col
,
383 epos
->col
- spos
->col
+ (*p_sel
!= 'e'));
389 /* Find the word under the cursor. */
391 len
= find_ident_at_pos(wp
, lnum
, (colnr_T
)col
, &lbuf
,
392 FIND_IDENT
+ FIND_STRING
+ FIND_EVAL
);
396 lbuf
= vim_strnsave(lbuf
, len
);
404 beval
->ts
= wp
->w_buffer
->b_p_ts
;
413 # if !defined(FEAT_GUI_W32) || defined(PROTO)
416 * Show a balloon with "mesg".
419 gui_mch_post_balloon(beval
, mesg
)
427 undrawBalloon(beval
);
429 # endif /* FEAT_GUI_W32 */
430 #endif /* FEAT_SUN_WORKSHOP || FEAT_NETBEANS_INTG || PROTO */
432 #if !defined(FEAT_GUI_W32) || defined(PROTO)
433 #if defined(FEAT_BEVAL_TIP) || defined(PROTO)
435 * Hide the given balloon.
438 gui_mch_unpost_balloon(beval
)
441 undrawBalloon(beval
);
447 * We can unconditionally use ANSI-style prototypes here since
448 * GTK+ requires an ANSI C compiler anyway.
451 addEventHandler(GtkWidget
*target
, BalloonEval
*beval
)
454 * Connect to the generic "event" signal instead of the individual
455 * signals for each event type, because the former is emitted earlier.
456 * This allows us to catch events independently of the signal handlers
459 /* Should use GTK_OBJECT() here, but that causes a lint warning... */
460 gtk_signal_connect((GtkObject
*)(target
), "event",
461 GTK_SIGNAL_FUNC(target_event_cb
),
464 * Nasty: Key press events go to the main window thus the drawing area
465 * will never see them. This means we have to connect to the main window
466 * as well in order to catch those events.
468 if (gtk_socket_id
== 0 && gui
.mainwin
!= NULL
469 && gtk_widget_is_ancestor(target
, gui
.mainwin
))
471 gtk_signal_connect((GtkObject
*)(gui
.mainwin
), "event",
472 GTK_SIGNAL_FUNC(mainwin_event_cb
),
478 removeEventHandler(BalloonEval
*beval
)
480 /* LINTED: avoid warning: dubious operation on enum */
481 gtk_signal_disconnect_by_func((GtkObject
*)(beval
->target
),
482 GTK_SIGNAL_FUNC(target_event_cb
),
485 if (gtk_socket_id
== 0 && gui
.mainwin
!= NULL
486 && gtk_widget_is_ancestor(beval
->target
, gui
.mainwin
))
488 /* LINTED: avoid warning: dubious operation on enum */
489 gtk_signal_disconnect_by_func((GtkObject
*)(gui
.mainwin
),
490 GTK_SIGNAL_FUNC(mainwin_event_cb
),
496 target_event_cb(GtkWidget
*widget
, GdkEvent
*event
, gpointer data
)
498 BalloonEval
*beval
= (BalloonEval
*)data
;
502 case GDK_ENTER_NOTIFY
:
503 pointer_event(beval
, (int)event
->crossing
.x
,
504 (int)event
->crossing
.y
,
505 event
->crossing
.state
);
507 case GDK_MOTION_NOTIFY
:
508 if (event
->motion
.is_hint
)
512 GdkModifierType state
;
514 * GDK_POINTER_MOTION_HINT_MASK is set, thus we cannot obtain
515 * the coordinates from the GdkEventMotion struct directly.
517 gdk_window_get_pointer(widget
->window
, &x
, &y
, &state
);
518 pointer_event(beval
, x
, y
, (unsigned int)state
);
522 pointer_event(beval
, (int)event
->motion
.x
,
523 (int)event
->motion
.y
,
524 event
->motion
.state
);
527 case GDK_LEAVE_NOTIFY
:
529 * Ignore LeaveNotify events that are not "normal".
530 * Apparently we also get it when somebody else grabs focus.
532 if (event
->crossing
.mode
== GDK_CROSSING_NORMAL
)
533 cancelBalloon(beval
);
535 case GDK_BUTTON_PRESS
:
539 cancelBalloon(beval
);
542 key_event(beval
, event
->key
.keyval
, TRUE
);
544 case GDK_KEY_RELEASE
:
545 key_event(beval
, event
->key
.keyval
, FALSE
);
551 return FALSE
; /* continue emission */
556 mainwin_event_cb(GtkWidget
*widget
, GdkEvent
*event
, gpointer data
)
558 BalloonEval
*beval
= (BalloonEval
*)data
;
563 key_event(beval
, event
->key
.keyval
, TRUE
);
565 case GDK_KEY_RELEASE
:
566 key_event(beval
, event
->key
.keyval
, FALSE
);
572 return FALSE
; /* continue emission */
576 pointer_event(BalloonEval
*beval
, int x
, int y
, unsigned state
)
580 distance
= ABS(x
- beval
->x
) + ABS(y
- beval
->y
);
585 * Moved out of the balloon location: cancel it.
586 * Remember button state
588 beval
->state
= state
;
589 cancelBalloon(beval
);
591 /* Mouse buttons are pressed - no balloon now */
592 if (!(state
& ((int)GDK_BUTTON1_MASK
| (int)GDK_BUTTON2_MASK
593 | (int)GDK_BUTTON3_MASK
)))
598 if (state
& (int)GDK_MOD1_MASK
)
601 * Alt is pressed -- enter super-evaluate-mode,
602 * where there is no time delay
604 if (beval
->msgCB
!= NULL
)
606 beval
->showState
= ShS_PENDING
;
607 (*beval
->msgCB
)(beval
, state
);
612 beval
->timerID
= gtk_timeout_add((guint32
)p_bdlay
,
620 key_event(BalloonEval
*beval
, unsigned keyval
, int is_keypress
)
622 if (beval
->showState
== ShS_SHOWING
&& beval
->msgCB
!= NULL
)
628 beval
->showState
= ShS_UPDATE_PENDING
;
629 (*beval
->msgCB
)(beval
, (is_keypress
)
630 ? (int)GDK_SHIFT_MASK
: 0);
634 beval
->showState
= ShS_UPDATE_PENDING
;
635 (*beval
->msgCB
)(beval
, (is_keypress
)
636 ? (int)GDK_CONTROL_MASK
: 0);
639 /* Don't do this for key release, we apparently get these with
640 * focus changes in some GTK version. */
642 cancelBalloon(beval
);
647 cancelBalloon(beval
);
651 timeout_cb(gpointer data
)
653 BalloonEval
*beval
= (BalloonEval
*)data
;
657 * If the timer event happens then the mouse has stopped long enough for
658 * a request to be started. The request will only send to the debugger if
659 * there the mouse is pointing at real data.
661 requestBalloon(beval
);
663 return FALSE
; /* don't call me again */
668 balloon_expose_event_cb(GtkWidget
*widget
, GdkEventExpose
*event
, gpointer data
)
670 gtk_paint_flat_box(widget
->style
, widget
->window
,
671 GTK_STATE_NORMAL
, GTK_SHADOW_OUT
,
672 &event
->area
, widget
, "tooltip",
675 return FALSE
; /* continue emission */
681 balloon_draw_cb(GtkWidget
*widget
, GdkRectangle
*area
, gpointer data
)
684 GdkRectangle child_area
;
686 gtk_paint_flat_box(widget
->style
, widget
->window
,
687 GTK_STATE_NORMAL
, GTK_SHADOW_OUT
,
688 area
, widget
, "tooltip",
691 child
= GTK_BIN(widget
)->child
;
693 if (gtk_widget_intersect(child
, area
, &child_area
))
694 gtk_widget_draw(child
, &child_area
);
698 #else /* !FEAT_GUI_GTK */
701 addEventHandler(target
, beval
)
705 XtAddEventHandler(target
,
706 PointerMotionMask
| EnterWindowMask
|
707 LeaveWindowMask
| ButtonPressMask
| KeyPressMask
|
710 pointerEventEH
, (XtPointer
)beval
);
714 removeEventHandler(beval
)
717 XtRemoveEventHandler(beval
->target
,
718 PointerMotionMask
| EnterWindowMask
|
719 LeaveWindowMask
| ButtonPressMask
| KeyPressMask
|
722 pointerEventEH
, (XtPointer
)beval
);
727 * The X event handler. All it does is call the real event handler.
731 pointerEventEH(w
, client_data
, event
, unused
)
733 XtPointer client_data
;
737 BalloonEval
*beval
= (BalloonEval
*)client_data
;
738 pointerEvent(beval
, event
);
743 * The real event handler. Called by pointerEventEH() whenever an event we are
744 * interested in occurs.
748 pointerEvent(beval
, event
)
752 Position distance
; /* a measure of how much the ponter moved */
753 Position delta
; /* used to compute distance */
759 delta
= event
->xmotion
.x
- beval
->x
;
763 delta
= event
->xmotion
.y
- beval
->y
;
770 * Moved out of the balloon location: cancel it.
771 * Remember button state
773 beval
->state
= event
->xmotion
.state
;
774 if (beval
->state
& (Button1Mask
|Button2Mask
|Button3Mask
))
776 /* Mouse buttons are pressed - no balloon now */
777 cancelBalloon(beval
);
779 else if (beval
->state
& (Mod1Mask
|Mod2Mask
|Mod3Mask
))
782 * Alt is pressed -- enter super-evaluate-mode,
783 * where there is no time delay
785 beval
->x
= event
->xmotion
.x
;
786 beval
->y
= event
->xmotion
.y
;
787 beval
->x_root
= event
->xmotion
.x_root
;
788 beval
->y_root
= event
->xmotion
.y_root
;
789 cancelBalloon(beval
);
790 if (beval
->msgCB
!= NULL
)
792 beval
->showState
= ShS_PENDING
;
793 (*beval
->msgCB
)(beval
, beval
->state
);
798 beval
->x
= event
->xmotion
.x
;
799 beval
->y
= event
->xmotion
.y
;
800 beval
->x_root
= event
->xmotion
.x_root
;
801 beval
->y_root
= event
->xmotion
.y_root
;
802 cancelBalloon(beval
);
803 beval
->timerID
= XtAppAddTimeOut( beval
->appContext
,
804 (long_u
)p_bdlay
, timerRoutine
, beval
);
810 * Motif and Athena version: Keystrokes will be caught by the
811 * "textArea" widget, and handled in gui_x11_key_hit_cb().
814 if (beval
->showState
== ShS_SHOWING
&& beval
->msgCB
!= NULL
)
819 XtTranslateKeycode(gui
.dpy
,
820 event
->xkey
.keycode
, event
->xkey
.state
,
822 if (keysym
== XK_Shift_L
|| keysym
== XK_Shift_R
)
824 beval
->showState
= ShS_UPDATE_PENDING
;
825 (*beval
->msgCB
)(beval
, ShiftMask
);
827 else if (keysym
== XK_Control_L
|| keysym
== XK_Control_R
)
829 beval
->showState
= ShS_UPDATE_PENDING
;
830 (*beval
->msgCB
)(beval
, ControlMask
);
833 cancelBalloon(beval
);
836 cancelBalloon(beval
);
840 if (beval
->showState
== ShS_SHOWING
&& beval
->msgCB
!= NULL
)
845 XtTranslateKeycode(gui
.dpy
, event
->xkey
.keycode
,
846 event
->xkey
.state
, &modifier
, &keysym
);
847 if ((keysym
== XK_Shift_L
) || (keysym
== XK_Shift_R
)) {
848 beval
->showState
= ShS_UPDATE_PENDING
;
849 (*beval
->msgCB
)(beval
, 0);
851 else if ((keysym
== XK_Control_L
) || (keysym
== XK_Control_R
))
853 beval
->showState
= ShS_UPDATE_PENDING
;
854 (*beval
->msgCB
)(beval
, 0);
857 cancelBalloon(beval
);
860 cancelBalloon(beval
);
864 /* Ignore LeaveNotify events that are not "normal".
865 * Apparently we also get it when somebody else grabs focus.
866 * Happens for me every two seconds (some clipboard tool?) */
867 if (event
->xcrossing
.mode
== NotifyNormal
)
868 cancelBalloon(beval
);
872 cancelBalloon(beval
);
886 BalloonEval
*beval
= (BalloonEval
*)dx
;
888 beval
->timerID
= (XtIntervalId
)NULL
;
891 * If the timer event happens then the mouse has stopped long enough for
892 * a request to be started. The request will only send to the debugger if
893 * there the mouse is pointing at real data.
895 requestBalloon(beval
);
898 #endif /* !FEAT_GUI_GTK */
901 requestBalloon(beval
)
904 if (beval
->showState
!= ShS_PENDING
)
906 /* Determine the beval to display */
907 if (beval
->msgCB
!= NULL
)
909 beval
->showState
= ShS_PENDING
;
910 (*beval
->msgCB
)(beval
, beval
->state
);
912 else if (beval
->msg
!= NULL
)
921 * Convert the string to UTF-8 if 'encoding' is not "utf-8".
922 * Replace any non-printable characters and invalid bytes sequences with
923 * "^X" or "<xx>" escapes, and apply SpecialKey highlighting to them.
924 * TAB and NL are passed through unscathed.
926 # define IS_NONPRINTABLE(c) (((c) < 0x20 && (c) != TAB && (c) != NL) \
929 set_printable_label_text(GtkLabel
*label
, char_u
*text
)
931 char_u
*convbuf
= NULL
;
938 PangoAttrList
*attr_list
;
940 /* Convert to UTF-8 if it isn't already */
941 if (output_conv
.vc_type
!= CONV_NONE
)
943 convbuf
= string_convert(&output_conv
, text
, NULL
);
948 /* First let's see how much we need to allocate */
950 for (p
= text
; *p
!= NUL
; p
+= charlen
)
952 if ((*p
& 0x80) == 0) /* be quick for ASCII */
955 len
+= IS_NONPRINTABLE(*p
) ? 2 : 1; /* nonprintable: ^X */
959 charlen
= utf_ptr2len(p
);
960 uc
= utf_ptr2char(p
);
962 if (charlen
!= utf_char2len(uc
))
963 charlen
= 1; /* reject overlong sequences */
965 if (charlen
== 1 || uc
< 0xa0) /* illegal byte or */
966 len
+= 4; /* control char: <xx> */
967 else if (!utf_printable(uc
))
968 /* Note: we assume here that utf_printable() doesn't
969 * care about characters outside the BMP. */
970 len
+= 6; /* nonprintable: <xxxx> */
976 attr_list
= pango_attr_list_new();
977 buf
= alloc(len
+ 1);
979 /* Now go for the real work */
983 PangoAttribute
*attr
;
985 GdkColor color
= { 0, 0, 0, 0 };
987 /* Look up the RGB values of the SpecialKey foreground color. */
988 aep
= syn_gui_attr2entry(hl_attr(HLF_8
));
989 pixel
= (aep
!= NULL
) ? aep
->ae_u
.gui
.fg_color
: INVALCOLOR
;
990 if (pixel
!= INVALCOLOR
)
991 gdk_colormap_query_color(gtk_widget_get_colormap(gui
.drawarea
),
992 (unsigned long)pixel
, &color
);
998 /* Be quick for ASCII */
999 if ((*p
& 0x80) == 0 && !IS_NONPRINTABLE(*p
))
1005 charlen
= utf_ptr2len(p
);
1006 uc
= utf_ptr2char(p
);
1008 if (charlen
!= utf_char2len(uc
))
1009 charlen
= 1; /* reject overlong sequences */
1011 if (charlen
== 1 || uc
< 0xa0 || !utf_printable(uc
))
1015 /* Careful: we can't just use transchar_byte() here,
1016 * since 'encoding' is not necessarily set to "utf-8". */
1017 if (*p
& 0x80 && charlen
== 1)
1019 transchar_hex(pdest
, *p
); /* <xx> */
1022 else if (uc
>= 0x80)
1024 /* Note: we assume here that utf_printable() doesn't
1025 * care about characters outside the BMP. */
1026 transchar_hex(pdest
, uc
); /* <xx> or <xxxx> */
1027 outlen
= (uc
< 0x100) ? 4 : 6;
1031 transchar_nonprint(pdest
, *p
); /* ^X */
1034 if (pixel
!= INVALCOLOR
)
1036 attr
= pango_attr_foreground_new(
1037 color
.red
, color
.green
, color
.blue
);
1038 attr
->start_index
= pdest
- buf
;
1039 attr
->end_index
= pdest
- buf
+ outlen
;
1040 pango_attr_list_insert(attr_list
, attr
);
1049 while (--charlen
!= 0);
1058 gtk_label_set_text(label
, (const char *)buf
);
1061 gtk_label_set_attributes(label
, attr_list
);
1062 pango_attr_list_unref(attr_list
);
1064 # undef IS_NONPRINTABLE
1065 # endif /* HAVE_GTK2 */
1071 drawBalloon(BalloonEval
*beval
)
1073 if (beval
->msg
!= NULL
)
1075 GtkRequisition requisition
;
1080 int x_offset
= EVAL_OFFSET_X
;
1081 int y_offset
= EVAL_OFFSET_Y
;
1083 PangoLayout
*layout
;
1085 # ifdef HAVE_GTK_MULTIHEAD
1088 screen
= gtk_widget_get_screen(beval
->target
);
1089 gtk_window_set_screen(GTK_WINDOW(beval
->balloonShell
), screen
);
1090 screen_w
= gdk_screen_get_width(screen
);
1091 screen_h
= gdk_screen_get_height(screen
);
1093 screen_w
= gdk_screen_width();
1094 screen_h
= gdk_screen_height();
1096 gtk_widget_ensure_style(beval
->balloonShell
);
1097 gtk_widget_ensure_style(beval
->balloonLabel
);
1100 set_printable_label_text(GTK_LABEL(beval
->balloonLabel
), beval
->msg
);
1102 * Dirty trick: Enable wrapping mode on the label's layout behind its
1103 * back. This way GtkLabel won't try to constrain the wrap width to a
1104 * builtin maximum value of about 65 Latin characters.
1106 layout
= gtk_label_get_layout(GTK_LABEL(beval
->balloonLabel
));
1107 # ifdef PANGO_WRAP_WORD_CHAR
1108 pango_layout_set_wrap(layout
, PANGO_WRAP_WORD_CHAR
);
1110 pango_layout_set_wrap(layout
, PANGO_WRAP_WORD
);
1112 pango_layout_set_width(layout
,
1113 /* try to come up with some reasonable width */
1114 PANGO_SCALE
* CLAMP(gui
.num_cols
* gui
.char_width
,
1116 MAX(20, screen_w
- 20)));
1118 /* Calculate the balloon's width and height. */
1119 gtk_widget_size_request(beval
->balloonShell
, &requisition
);
1121 gtk_label_set_line_wrap(GTK_LABEL(beval
->balloonLabel
), FALSE
);
1122 gtk_label_set_text(GTK_LABEL(beval
->balloonLabel
),
1123 (const char *)beval
->msg
);
1125 /* Calculate the balloon's width and height. */
1126 gtk_widget_size_request(beval
->balloonShell
, &requisition
);
1128 * Unfortunately, the dirty trick used above to get around the builtin
1129 * maximum wrap width of GtkLabel doesn't work with GTK+ 1. Thus if
1130 * and only if it's absolutely necessary to avoid drawing off-screen,
1131 * do enable wrapping now and recalculate the size request.
1133 if (requisition
.width
> screen_w
)
1135 gtk_label_set_line_wrap(GTK_LABEL(beval
->balloonLabel
), TRUE
);
1136 gtk_widget_size_request(beval
->balloonShell
, &requisition
);
1140 /* Compute position of the balloon area */
1141 gdk_window_get_origin(beval
->target
->window
, &x
, &y
);
1145 /* Get out of the way of the mouse pointer */
1146 if (x
+ x_offset
+ requisition
.width
> screen_w
)
1148 if (y
+ y_offset
+ requisition
.height
> screen_h
)
1149 y_offset
= -requisition
.height
- EVAL_OFFSET_Y
;
1151 /* Sanitize values */
1152 x
= CLAMP(x
+ x_offset
, 0, MAX(0, screen_w
- requisition
.width
));
1153 y
= CLAMP(y
+ y_offset
, 0, MAX(0, screen_h
- requisition
.height
));
1155 /* Show the balloon */
1156 gtk_widget_set_uposition(beval
->balloonShell
, x
, y
);
1157 gtk_widget_show(beval
->balloonShell
);
1159 beval
->showState
= ShS_SHOWING
;
1167 undrawBalloon(BalloonEval
*beval
)
1169 if (beval
->balloonShell
!= NULL
)
1170 gtk_widget_hide(beval
->balloonShell
);
1171 beval
->showState
= ShS_NEUTRAL
;
1175 cancelBalloon(BalloonEval
*beval
)
1177 if (beval
->showState
== ShS_SHOWING
1178 || beval
->showState
== ShS_UPDATE_PENDING
)
1179 undrawBalloon(beval
);
1181 if (beval
->timerID
!= 0)
1183 gtk_timeout_remove(beval
->timerID
);
1186 beval
->showState
= ShS_NEUTRAL
;
1190 createBalloonEvalWindow(BalloonEval
*beval
)
1192 beval
->balloonShell
= gtk_window_new(GTK_WINDOW_POPUP
);
1194 gtk_widget_set_app_paintable(beval
->balloonShell
, TRUE
);
1195 gtk_window_set_policy(GTK_WINDOW(beval
->balloonShell
), FALSE
, FALSE
, TRUE
);
1196 gtk_widget_set_name(beval
->balloonShell
, "gtk-tooltips");
1197 gtk_container_border_width(GTK_CONTAINER(beval
->balloonShell
), 4);
1199 gtk_signal_connect((GtkObject
*)(beval
->balloonShell
), "expose_event",
1200 GTK_SIGNAL_FUNC(balloon_expose_event_cb
), NULL
);
1202 gtk_signal_connect((GtkObject
*)(beval
->balloonShell
), "draw",
1203 GTK_SIGNAL_FUNC(balloon_draw_cb
), NULL
);
1205 beval
->balloonLabel
= gtk_label_new(NULL
);
1207 gtk_label_set_line_wrap(GTK_LABEL(beval
->balloonLabel
), FALSE
);
1208 gtk_label_set_justify(GTK_LABEL(beval
->balloonLabel
), GTK_JUSTIFY_LEFT
);
1209 gtk_misc_set_alignment(GTK_MISC(beval
->balloonLabel
), 0.5f
, 0.5f
);
1210 gtk_widget_set_name(beval
->balloonLabel
, "vim-balloon-label");
1211 gtk_widget_show(beval
->balloonLabel
);
1213 gtk_container_add(GTK_CONTAINER(beval
->balloonShell
), beval
->balloonLabel
);
1216 #else /* !FEAT_GUI_GTK */
1230 if (beval
->msg
!= NULL
)
1232 /* Show the Balloon */
1234 /* Calculate the label's width and height */
1235 #ifdef FEAT_GUI_MOTIF
1238 /* For the callback function we parse NL characters to create a
1239 * multi-line label. This doesn't work for all languages, but
1240 * XmStringCreateLocalized() doesn't do multi-line labels... */
1241 if (beval
->msgCB
!= NULL
)
1242 s
= XmStringCreateLtoR((char *)beval
->msg
, XmFONTLIST_DEFAULT_TAG
);
1244 s
= XmStringCreateLocalized((char *)beval
->msg
);
1248 fl
= gui_motif_fontset2fontlist(&gui
.tooltip_fontset
);
1251 XmStringExtent(fl
, s
, &w
, &h
);
1255 w
+= gui
.border_offset
<< 1;
1256 h
+= gui
.border_offset
<< 1;
1257 XtVaSetValues(beval
->balloonLabel
, XmNlabelString
, s
, NULL
);
1260 /* Assume XtNinternational == True */
1262 XFontSetExtents
*ext
;
1264 XtVaGetValues(beval
->balloonLabel
, XtNfontSet
, &fset
, NULL
);
1265 ext
= XExtentsOfFontSet(fset
);
1266 h
= ext
->max_ink_extent
.height
;
1267 w
= XmbTextEscapement(fset
,
1269 (int)STRLEN(beval
->msg
));
1270 w
+= gui
.border_offset
<< 1;
1271 h
+= gui
.border_offset
<< 1;
1272 XtVaSetValues(beval
->balloonLabel
, XtNlabel
, beval
->msg
, NULL
);
1275 /* Compute position of the balloon area */
1276 tx
= beval
->x_root
+ EVAL_OFFSET_X
;
1277 ty
= beval
->y_root
+ EVAL_OFFSET_Y
;
1278 if ((tx
+ w
) > beval
->screen_width
)
1279 tx
= beval
->screen_width
- w
;
1280 if ((ty
+ h
) > beval
->screen_height
)
1281 ty
= beval
->screen_height
- h
;
1282 #ifdef FEAT_GUI_MOTIF
1283 XtVaSetValues(beval
->balloonShell
,
1289 XtVaSetValues(beval
->balloonShell
,
1295 XtPopup(beval
->balloonShell
, XtGrabNone
);
1297 beval
->showState
= ShS_SHOWING
;
1299 current_beval
= beval
;
1307 undrawBalloon(beval
)
1310 if (beval
->balloonShell
!= (Widget
)0)
1311 XtPopdown(beval
->balloonShell
);
1312 beval
->showState
= ShS_NEUTRAL
;
1314 current_beval
= NULL
;
1318 cancelBalloon(beval
)
1321 if (beval
->showState
== ShS_SHOWING
1322 || beval
->showState
== ShS_UPDATE_PENDING
)
1323 undrawBalloon(beval
);
1325 if (beval
->timerID
!= (XtIntervalId
)NULL
)
1327 XtRemoveTimeOut(beval
->timerID
);
1328 beval
->timerID
= (XtIntervalId
)NULL
;
1330 beval
->showState
= ShS_NEUTRAL
;
1335 createBalloonEvalWindow(beval
)
1342 #ifdef FEAT_GUI_MOTIF
1343 XtSetArg(args
[n
], XmNallowShellResize
, True
); n
++;
1344 beval
->balloonShell
= XtAppCreateShell("balloonEval", "BalloonEval",
1345 overrideShellWidgetClass
, gui
.dpy
, args
, n
);
1348 XtSetArg(args
[n
], XtNallowShellResize
, True
); n
++;
1349 beval
->balloonShell
= XtAppCreateShell("balloonEval", "BalloonEval",
1350 overrideShellWidgetClass
, gui
.dpy
, args
, n
);
1354 #ifdef FEAT_GUI_MOTIF
1358 fl
= gui_motif_fontset2fontlist(&gui
.tooltip_fontset
);
1359 XtSetArg(args
[n
], XmNforeground
, gui
.tooltip_fg_pixel
); n
++;
1360 XtSetArg(args
[n
], XmNbackground
, gui
.tooltip_bg_pixel
); n
++;
1361 XtSetArg(args
[n
], XmNfontList
, fl
); n
++;
1362 XtSetArg(args
[n
], XmNalignment
, XmALIGNMENT_BEGINNING
); n
++;
1363 beval
->balloonLabel
= XtCreateManagedWidget("balloonLabel",
1364 xmLabelWidgetClass
, beval
->balloonShell
, args
, n
);
1366 #else /* FEAT_GUI_ATHENA */
1367 XtSetArg(args
[n
], XtNforeground
, gui
.tooltip_fg_pixel
); n
++;
1368 XtSetArg(args
[n
], XtNbackground
, gui
.tooltip_bg_pixel
); n
++;
1369 XtSetArg(args
[n
], XtNinternational
, True
); n
++;
1370 XtSetArg(args
[n
], XtNfontSet
, gui
.tooltip_fontset
); n
++;
1371 beval
->balloonLabel
= XtCreateManagedWidget("balloonLabel",
1372 labelWidgetClass
, beval
->balloonShell
, args
, n
);
1376 #endif /* !FEAT_GUI_GTK */
1377 #endif /* !FEAT_GUI_W32 */
1379 #endif /* FEAT_BEVAL */