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
;
225 * Set up event handler which will keep its eyes on the pointer,
226 * and when the pointer rests in a certain spot for a given time
227 * interval, show the beval.
229 addEventHandler(beval
->target
, beval
);
230 createBalloonEvalWindow(beval
);
234 * Now create and save the screen width and height. Used in drawing.
236 display_name
= DisplayString(gui
.dpy
);
237 p
= strrchr(display_name
, '.');
239 screen_num
= atoi(++p
);
242 beval
->screen_width
= DisplayWidth(gui
.dpy
, screen_num
);
243 beval
->screen_height
= DisplayHeight(gui
.dpy
, screen_num
);
250 #if defined(FEAT_BEVAL_TIP) || defined(PROTO)
252 * Destroy a balloon-eval and free its associated memory.
255 gui_mch_destroy_beval_area(beval
)
258 cancelBalloon(beval
);
259 removeEventHandler(beval
);
260 /* Children will automatically be destroyed */
262 gtk_widget_destroy(beval
->balloonShell
);
264 XtDestroyWidget(beval
->balloonShell
);
271 gui_mch_enable_beval_area(beval
)
275 addEventHandler(beval
->target
, beval
);
279 gui_mch_disable_beval_area(beval
)
283 removeEventHandler(beval
);
286 #if defined(FEAT_BEVAL_TIP) || defined(PROTO)
288 * This function returns the BalloonEval * associated with the currently
289 * displayed tooltip. Returns NULL if there is no tooltip currently showing.
291 * Assumption: Only one tooltip can be shown at a time.
294 gui_mch_currently_showing_beval()
296 return current_beval
;
299 #endif /* !FEAT_GUI_W32 */
301 #if defined(FEAT_SUN_WORKSHOP) || defined(FEAT_NETBEANS_INTG) \
302 || defined(FEAT_EVAL) || defined(PROTO)
304 * Get the text and position to be evaluated for "beval".
305 * If "getword" is true the returned text is not the whole line but the
306 * relevant word in allocated memory.
307 * Returns OK or FAIL.
310 get_beval_info(beval
, getword
, winp
, lnump
, textp
, colp
)
324 row
= Y_2_ROW(beval
->y
);
325 col
= X_2_COL(beval
->x
);
327 wp
= mouse_find_win(&row
, &col
);
331 if (wp
!= NULL
&& row
< wp
->w_height
&& col
< W_WIDTH(wp
))
333 /* Found a window and the cursor is in the text. Now find the line
335 if (!mouse_comp_pos(wp
, &row
, &col
, &lnum
))
337 /* Not past end of the file. */
338 lbuf
= ml_get_buf(wp
->w_buffer
, lnum
, FALSE
);
339 if (col
<= win_linetabsize(wp
, lbuf
, (colnr_T
)MAXCOL
))
341 /* Not past end of line. */
344 /* For Netbeans we get the relevant part of the line
345 * instead of the whole line. */
347 pos_T
*spos
= NULL
, *epos
= NULL
;
351 if (lt(VIsual
, curwin
->w_cursor
))
354 epos
= &curwin
->w_cursor
;
358 spos
= &curwin
->w_cursor
;
363 col
= vcol2col(wp
, lnum
, col
) - 1;
366 && wp
->w_buffer
== curwin
->w_buffer
367 && (lnum
== spos
->lnum
368 ? col
>= (int)spos
->col
370 && (lnum
== epos
->lnum
371 ? col
<= (int)epos
->col
372 : lnum
< epos
->lnum
))
374 /* Visual mode and pointing to the line with the
375 * Visual selection: return selected text, with a
376 * maximum of one line. */
377 if (spos
->lnum
!= epos
->lnum
|| spos
->col
== epos
->col
)
380 lbuf
= ml_get_buf(curwin
->w_buffer
, VIsual
.lnum
, FALSE
);
381 lbuf
= vim_strnsave(lbuf
+ spos
->col
,
382 epos
->col
- spos
->col
+ (*p_sel
!= 'e'));
388 /* Find the word under the cursor. */
390 len
= find_ident_at_pos(wp
, lnum
, (colnr_T
)col
, &lbuf
,
391 FIND_IDENT
+ FIND_STRING
+ FIND_EVAL
);
395 lbuf
= vim_strnsave(lbuf
, len
);
403 beval
->ts
= wp
->w_buffer
->b_p_ts
;
412 # if !defined(FEAT_GUI_W32) || defined(PROTO)
415 * Show a balloon with "mesg".
418 gui_mch_post_balloon(beval
, mesg
)
426 undrawBalloon(beval
);
428 # endif /* FEAT_GUI_W32 */
429 #endif /* FEAT_SUN_WORKSHOP || FEAT_NETBEANS_INTG || PROTO */
431 #if !defined(FEAT_GUI_W32) || defined(PROTO)
432 #if defined(FEAT_BEVAL_TIP) || defined(PROTO)
434 * Hide the given balloon.
437 gui_mch_unpost_balloon(beval
)
440 undrawBalloon(beval
);
446 * We can unconditionally use ANSI-style prototypes here since
447 * GTK+ requires an ANSI C compiler anyway.
450 addEventHandler(GtkWidget
*target
, BalloonEval
*beval
)
453 * Connect to the generic "event" signal instead of the individual
454 * signals for each event type, because the former is emitted earlier.
455 * This allows us to catch events independently of the signal handlers
458 /* Should use GTK_OBJECT() here, but that causes a lint warning... */
459 gtk_signal_connect((GtkObject
*)(target
), "event",
460 GTK_SIGNAL_FUNC(target_event_cb
),
463 * Nasty: Key press events go to the main window thus the drawing area
464 * will never see them. This means we have to connect to the main window
465 * as well in order to catch those events.
467 if (gtk_socket_id
== 0 && gui
.mainwin
!= NULL
468 && gtk_widget_is_ancestor(target
, gui
.mainwin
))
470 gtk_signal_connect((GtkObject
*)(gui
.mainwin
), "event",
471 GTK_SIGNAL_FUNC(mainwin_event_cb
),
477 removeEventHandler(BalloonEval
*beval
)
479 /* LINTED: avoid warning: dubious operation on enum */
480 gtk_signal_disconnect_by_func((GtkObject
*)(beval
->target
),
481 GTK_SIGNAL_FUNC(target_event_cb
),
484 if (gtk_socket_id
== 0 && gui
.mainwin
!= NULL
485 && gtk_widget_is_ancestor(beval
->target
, gui
.mainwin
))
487 /* LINTED: avoid warning: dubious operation on enum */
488 gtk_signal_disconnect_by_func((GtkObject
*)(gui
.mainwin
),
489 GTK_SIGNAL_FUNC(mainwin_event_cb
),
495 target_event_cb(GtkWidget
*widget
, GdkEvent
*event
, gpointer data
)
497 BalloonEval
*beval
= (BalloonEval
*)data
;
501 case GDK_ENTER_NOTIFY
:
502 pointer_event(beval
, (int)event
->crossing
.x
,
503 (int)event
->crossing
.y
,
504 event
->crossing
.state
);
506 case GDK_MOTION_NOTIFY
:
507 if (event
->motion
.is_hint
)
511 GdkModifierType state
;
513 * GDK_POINTER_MOTION_HINT_MASK is set, thus we cannot obtain
514 * the coordinates from the GdkEventMotion struct directly.
516 gdk_window_get_pointer(widget
->window
, &x
, &y
, &state
);
517 pointer_event(beval
, x
, y
, (unsigned int)state
);
521 pointer_event(beval
, (int)event
->motion
.x
,
522 (int)event
->motion
.y
,
523 event
->motion
.state
);
526 case GDK_LEAVE_NOTIFY
:
528 * Ignore LeaveNotify events that are not "normal".
529 * Apparently we also get it when somebody else grabs focus.
531 if (event
->crossing
.mode
== GDK_CROSSING_NORMAL
)
532 cancelBalloon(beval
);
534 case GDK_BUTTON_PRESS
:
538 cancelBalloon(beval
);
541 key_event(beval
, event
->key
.keyval
, TRUE
);
543 case GDK_KEY_RELEASE
:
544 key_event(beval
, event
->key
.keyval
, FALSE
);
550 return FALSE
; /* continue emission */
554 mainwin_event_cb(GtkWidget
*widget UNUSED
, GdkEvent
*event
, gpointer data
)
556 BalloonEval
*beval
= (BalloonEval
*)data
;
561 key_event(beval
, event
->key
.keyval
, TRUE
);
563 case GDK_KEY_RELEASE
:
564 key_event(beval
, event
->key
.keyval
, FALSE
);
570 return FALSE
; /* continue emission */
574 pointer_event(BalloonEval
*beval
, int x
, int y
, unsigned state
)
578 distance
= ABS(x
- beval
->x
) + ABS(y
- beval
->y
);
583 * Moved out of the balloon location: cancel it.
584 * Remember button state
586 beval
->state
= state
;
587 cancelBalloon(beval
);
589 /* Mouse buttons are pressed - no balloon now */
590 if (!(state
& ((int)GDK_BUTTON1_MASK
| (int)GDK_BUTTON2_MASK
591 | (int)GDK_BUTTON3_MASK
)))
596 if (state
& (int)GDK_MOD1_MASK
)
599 * Alt is pressed -- enter super-evaluate-mode,
600 * where there is no time delay
602 if (beval
->msgCB
!= NULL
)
604 beval
->showState
= ShS_PENDING
;
605 (*beval
->msgCB
)(beval
, state
);
610 beval
->timerID
= gtk_timeout_add((guint32
)p_bdlay
,
618 key_event(BalloonEval
*beval
, unsigned keyval
, int is_keypress
)
620 if (beval
->showState
== ShS_SHOWING
&& beval
->msgCB
!= NULL
)
626 beval
->showState
= ShS_UPDATE_PENDING
;
627 (*beval
->msgCB
)(beval
, (is_keypress
)
628 ? (int)GDK_SHIFT_MASK
: 0);
632 beval
->showState
= ShS_UPDATE_PENDING
;
633 (*beval
->msgCB
)(beval
, (is_keypress
)
634 ? (int)GDK_CONTROL_MASK
: 0);
637 /* Don't do this for key release, we apparently get these with
638 * focus changes in some GTK version. */
640 cancelBalloon(beval
);
645 cancelBalloon(beval
);
649 timeout_cb(gpointer data
)
651 BalloonEval
*beval
= (BalloonEval
*)data
;
655 * If the timer event happens then the mouse has stopped long enough for
656 * a request to be started. The request will only send to the debugger if
657 * there the mouse is pointing at real data.
659 requestBalloon(beval
);
661 return FALSE
; /* don't call me again */
665 balloon_expose_event_cb(GtkWidget
*widget
,
666 GdkEventExpose
*event
,
667 gpointer data UNUSED
)
669 gtk_paint_flat_box(widget
->style
, widget
->window
,
670 GTK_STATE_NORMAL
, GTK_SHADOW_OUT
,
671 &event
->area
, widget
, "tooltip",
674 return FALSE
; /* continue emission */
679 balloon_draw_cb(GtkWidget
*widget
, GdkRectangle
*area
, gpointer data
)
682 GdkRectangle child_area
;
684 gtk_paint_flat_box(widget
->style
, widget
->window
,
685 GTK_STATE_NORMAL
, GTK_SHADOW_OUT
,
686 area
, widget
, "tooltip",
689 child
= GTK_BIN(widget
)->child
;
691 if (gtk_widget_intersect(child
, area
, &child_area
))
692 gtk_widget_draw(child
, &child_area
);
696 #else /* !FEAT_GUI_GTK */
699 addEventHandler(target
, beval
)
703 XtAddEventHandler(target
,
704 PointerMotionMask
| EnterWindowMask
|
705 LeaveWindowMask
| ButtonPressMask
| KeyPressMask
|
708 pointerEventEH
, (XtPointer
)beval
);
712 removeEventHandler(beval
)
715 XtRemoveEventHandler(beval
->target
,
716 PointerMotionMask
| EnterWindowMask
|
717 LeaveWindowMask
| ButtonPressMask
| KeyPressMask
|
720 pointerEventEH
, (XtPointer
)beval
);
725 * The X event handler. All it does is call the real event handler.
728 pointerEventEH(w
, client_data
, event
, unused
)
730 XtPointer client_data
;
732 Boolean
*unused UNUSED
;
734 BalloonEval
*beval
= (BalloonEval
*)client_data
;
735 pointerEvent(beval
, event
);
740 * The real event handler. Called by pointerEventEH() whenever an event we are
741 * interested in occurs.
745 pointerEvent(beval
, event
)
749 Position distance
; /* a measure of how much the ponter moved */
750 Position delta
; /* used to compute distance */
756 delta
= event
->xmotion
.x
- beval
->x
;
760 delta
= event
->xmotion
.y
- beval
->y
;
767 * Moved out of the balloon location: cancel it.
768 * Remember button state
770 beval
->state
= event
->xmotion
.state
;
771 if (beval
->state
& (Button1Mask
|Button2Mask
|Button3Mask
))
773 /* Mouse buttons are pressed - no balloon now */
774 cancelBalloon(beval
);
776 else if (beval
->state
& (Mod1Mask
|Mod2Mask
|Mod3Mask
))
779 * Alt is pressed -- enter super-evaluate-mode,
780 * where there is no time delay
782 beval
->x
= event
->xmotion
.x
;
783 beval
->y
= event
->xmotion
.y
;
784 beval
->x_root
= event
->xmotion
.x_root
;
785 beval
->y_root
= event
->xmotion
.y_root
;
786 cancelBalloon(beval
);
787 if (beval
->msgCB
!= NULL
)
789 beval
->showState
= ShS_PENDING
;
790 (*beval
->msgCB
)(beval
, beval
->state
);
795 beval
->x
= event
->xmotion
.x
;
796 beval
->y
= event
->xmotion
.y
;
797 beval
->x_root
= event
->xmotion
.x_root
;
798 beval
->y_root
= event
->xmotion
.y_root
;
799 cancelBalloon(beval
);
800 beval
->timerID
= XtAppAddTimeOut( beval
->appContext
,
801 (long_u
)p_bdlay
, timerRoutine
, beval
);
807 * Motif and Athena version: Keystrokes will be caught by the
808 * "textArea" widget, and handled in gui_x11_key_hit_cb().
811 if (beval
->showState
== ShS_SHOWING
&& beval
->msgCB
!= NULL
)
816 XtTranslateKeycode(gui
.dpy
,
817 event
->xkey
.keycode
, event
->xkey
.state
,
819 if (keysym
== XK_Shift_L
|| keysym
== XK_Shift_R
)
821 beval
->showState
= ShS_UPDATE_PENDING
;
822 (*beval
->msgCB
)(beval
, ShiftMask
);
824 else if (keysym
== XK_Control_L
|| keysym
== XK_Control_R
)
826 beval
->showState
= ShS_UPDATE_PENDING
;
827 (*beval
->msgCB
)(beval
, ControlMask
);
830 cancelBalloon(beval
);
833 cancelBalloon(beval
);
837 if (beval
->showState
== ShS_SHOWING
&& beval
->msgCB
!= NULL
)
842 XtTranslateKeycode(gui
.dpy
, event
->xkey
.keycode
,
843 event
->xkey
.state
, &modifier
, &keysym
);
844 if ((keysym
== XK_Shift_L
) || (keysym
== XK_Shift_R
)) {
845 beval
->showState
= ShS_UPDATE_PENDING
;
846 (*beval
->msgCB
)(beval
, 0);
848 else if ((keysym
== XK_Control_L
) || (keysym
== XK_Control_R
))
850 beval
->showState
= ShS_UPDATE_PENDING
;
851 (*beval
->msgCB
)(beval
, 0);
854 cancelBalloon(beval
);
857 cancelBalloon(beval
);
861 /* Ignore LeaveNotify events that are not "normal".
862 * Apparently we also get it when somebody else grabs focus.
863 * Happens for me every two seconds (some clipboard tool?) */
864 if (event
->xcrossing
.mode
== NotifyNormal
)
865 cancelBalloon(beval
);
869 cancelBalloon(beval
);
880 XtIntervalId
*id UNUSED
;
882 BalloonEval
*beval
= (BalloonEval
*)dx
;
884 beval
->timerID
= (XtIntervalId
)NULL
;
887 * If the timer event happens then the mouse has stopped long enough for
888 * a request to be started. The request will only send to the debugger if
889 * there the mouse is pointing at real data.
891 requestBalloon(beval
);
894 #endif /* !FEAT_GUI_GTK */
897 requestBalloon(beval
)
900 if (beval
->showState
!= ShS_PENDING
)
902 /* Determine the beval to display */
903 if (beval
->msgCB
!= NULL
)
905 beval
->showState
= ShS_PENDING
;
906 (*beval
->msgCB
)(beval
, beval
->state
);
908 else if (beval
->msg
!= NULL
)
917 * Convert the string to UTF-8 if 'encoding' is not "utf-8".
918 * Replace any non-printable characters and invalid bytes sequences with
919 * "^X" or "<xx>" escapes, and apply SpecialKey highlighting to them.
920 * TAB and NL are passed through unscathed.
922 # define IS_NONPRINTABLE(c) (((c) < 0x20 && (c) != TAB && (c) != NL) \
925 set_printable_label_text(GtkLabel
*label
, char_u
*text
)
927 char_u
*convbuf
= NULL
;
934 PangoAttrList
*attr_list
;
936 /* Convert to UTF-8 if it isn't already */
937 if (output_conv
.vc_type
!= CONV_NONE
)
939 convbuf
= string_convert(&output_conv
, text
, NULL
);
944 /* First let's see how much we need to allocate */
946 for (p
= text
; *p
!= NUL
; p
+= charlen
)
948 if ((*p
& 0x80) == 0) /* be quick for ASCII */
951 len
+= IS_NONPRINTABLE(*p
) ? 2 : 1; /* nonprintable: ^X */
955 charlen
= utf_ptr2len(p
);
956 uc
= utf_ptr2char(p
);
958 if (charlen
!= utf_char2len(uc
))
959 charlen
= 1; /* reject overlong sequences */
961 if (charlen
== 1 || uc
< 0xa0) /* illegal byte or */
962 len
+= 4; /* control char: <xx> */
963 else if (!utf_printable(uc
))
964 /* Note: we assume here that utf_printable() doesn't
965 * care about characters outside the BMP. */
966 len
+= 6; /* nonprintable: <xxxx> */
972 attr_list
= pango_attr_list_new();
973 buf
= alloc(len
+ 1);
975 /* Now go for the real work */
979 PangoAttribute
*attr
;
981 GdkColor color
= { 0, 0, 0, 0 };
983 /* Look up the RGB values of the SpecialKey foreground color. */
984 aep
= syn_gui_attr2entry(hl_attr(HLF_8
));
985 pixel
= (aep
!= NULL
) ? aep
->ae_u
.gui
.fg_color
: INVALCOLOR
;
986 if (pixel
!= INVALCOLOR
)
987 gdk_colormap_query_color(gtk_widget_get_colormap(gui
.drawarea
),
988 (unsigned long)pixel
, &color
);
994 /* Be quick for ASCII */
995 if ((*p
& 0x80) == 0 && !IS_NONPRINTABLE(*p
))
1001 charlen
= utf_ptr2len(p
);
1002 uc
= utf_ptr2char(p
);
1004 if (charlen
!= utf_char2len(uc
))
1005 charlen
= 1; /* reject overlong sequences */
1007 if (charlen
== 1 || uc
< 0xa0 || !utf_printable(uc
))
1011 /* Careful: we can't just use transchar_byte() here,
1012 * since 'encoding' is not necessarily set to "utf-8". */
1013 if (*p
& 0x80 && charlen
== 1)
1015 transchar_hex(pdest
, *p
); /* <xx> */
1018 else if (uc
>= 0x80)
1020 /* Note: we assume here that utf_printable() doesn't
1021 * care about characters outside the BMP. */
1022 transchar_hex(pdest
, uc
); /* <xx> or <xxxx> */
1023 outlen
= (uc
< 0x100) ? 4 : 6;
1027 transchar_nonprint(pdest
, *p
); /* ^X */
1030 if (pixel
!= INVALCOLOR
)
1032 attr
= pango_attr_foreground_new(
1033 color
.red
, color
.green
, color
.blue
);
1034 attr
->start_index
= pdest
- buf
;
1035 attr
->end_index
= pdest
- buf
+ outlen
;
1036 pango_attr_list_insert(attr_list
, attr
);
1045 while (--charlen
!= 0);
1054 gtk_label_set_text(label
, (const char *)buf
);
1057 gtk_label_set_attributes(label
, attr_list
);
1058 pango_attr_list_unref(attr_list
);
1060 # undef IS_NONPRINTABLE
1061 # endif /* HAVE_GTK2 */
1067 drawBalloon(BalloonEval
*beval
)
1069 if (beval
->msg
!= NULL
)
1071 GtkRequisition requisition
;
1076 int x_offset
= EVAL_OFFSET_X
;
1077 int y_offset
= EVAL_OFFSET_Y
;
1079 PangoLayout
*layout
;
1081 # ifdef HAVE_GTK_MULTIHEAD
1084 screen
= gtk_widget_get_screen(beval
->target
);
1085 gtk_window_set_screen(GTK_WINDOW(beval
->balloonShell
), screen
);
1086 screen_w
= gdk_screen_get_width(screen
);
1087 screen_h
= gdk_screen_get_height(screen
);
1089 screen_w
= gdk_screen_width();
1090 screen_h
= gdk_screen_height();
1092 gtk_widget_ensure_style(beval
->balloonShell
);
1093 gtk_widget_ensure_style(beval
->balloonLabel
);
1096 set_printable_label_text(GTK_LABEL(beval
->balloonLabel
), beval
->msg
);
1098 * Dirty trick: Enable wrapping mode on the label's layout behind its
1099 * back. This way GtkLabel won't try to constrain the wrap width to a
1100 * builtin maximum value of about 65 Latin characters.
1102 layout
= gtk_label_get_layout(GTK_LABEL(beval
->balloonLabel
));
1103 # ifdef PANGO_WRAP_WORD_CHAR
1104 pango_layout_set_wrap(layout
, PANGO_WRAP_WORD_CHAR
);
1106 pango_layout_set_wrap(layout
, PANGO_WRAP_WORD
);
1108 pango_layout_set_width(layout
,
1109 /* try to come up with some reasonable width */
1110 PANGO_SCALE
* CLAMP(gui
.num_cols
* gui
.char_width
,
1112 MAX(20, screen_w
- 20)));
1114 /* Calculate the balloon's width and height. */
1115 gtk_widget_size_request(beval
->balloonShell
, &requisition
);
1117 gtk_label_set_line_wrap(GTK_LABEL(beval
->balloonLabel
), FALSE
);
1118 gtk_label_set_text(GTK_LABEL(beval
->balloonLabel
),
1119 (const char *)beval
->msg
);
1121 /* Calculate the balloon's width and height. */
1122 gtk_widget_size_request(beval
->balloonShell
, &requisition
);
1124 * Unfortunately, the dirty trick used above to get around the builtin
1125 * maximum wrap width of GtkLabel doesn't work with GTK+ 1. Thus if
1126 * and only if it's absolutely necessary to avoid drawing off-screen,
1127 * do enable wrapping now and recalculate the size request.
1129 if (requisition
.width
> screen_w
)
1131 gtk_label_set_line_wrap(GTK_LABEL(beval
->balloonLabel
), TRUE
);
1132 gtk_widget_size_request(beval
->balloonShell
, &requisition
);
1136 /* Compute position of the balloon area */
1137 gdk_window_get_origin(beval
->target
->window
, &x
, &y
);
1141 /* Get out of the way of the mouse pointer */
1142 if (x
+ x_offset
+ requisition
.width
> screen_w
)
1144 if (y
+ y_offset
+ requisition
.height
> screen_h
)
1145 y_offset
= -requisition
.height
- EVAL_OFFSET_Y
;
1147 /* Sanitize values */
1148 x
= CLAMP(x
+ x_offset
, 0, MAX(0, screen_w
- requisition
.width
));
1149 y
= CLAMP(y
+ y_offset
, 0, MAX(0, screen_h
- requisition
.height
));
1151 /* Show the balloon */
1152 gtk_widget_set_uposition(beval
->balloonShell
, x
, y
);
1153 gtk_widget_show(beval
->balloonShell
);
1155 beval
->showState
= ShS_SHOWING
;
1163 undrawBalloon(BalloonEval
*beval
)
1165 if (beval
->balloonShell
!= NULL
)
1166 gtk_widget_hide(beval
->balloonShell
);
1167 beval
->showState
= ShS_NEUTRAL
;
1171 cancelBalloon(BalloonEval
*beval
)
1173 if (beval
->showState
== ShS_SHOWING
1174 || beval
->showState
== ShS_UPDATE_PENDING
)
1175 undrawBalloon(beval
);
1177 if (beval
->timerID
!= 0)
1179 gtk_timeout_remove(beval
->timerID
);
1182 beval
->showState
= ShS_NEUTRAL
;
1186 createBalloonEvalWindow(BalloonEval
*beval
)
1188 beval
->balloonShell
= gtk_window_new(GTK_WINDOW_POPUP
);
1190 gtk_widget_set_app_paintable(beval
->balloonShell
, TRUE
);
1191 gtk_window_set_policy(GTK_WINDOW(beval
->balloonShell
), FALSE
, FALSE
, TRUE
);
1192 gtk_widget_set_name(beval
->balloonShell
, "gtk-tooltips");
1193 gtk_container_border_width(GTK_CONTAINER(beval
->balloonShell
), 4);
1195 gtk_signal_connect((GtkObject
*)(beval
->balloonShell
), "expose_event",
1196 GTK_SIGNAL_FUNC(balloon_expose_event_cb
), NULL
);
1198 gtk_signal_connect((GtkObject
*)(beval
->balloonShell
), "draw",
1199 GTK_SIGNAL_FUNC(balloon_draw_cb
), NULL
);
1201 beval
->balloonLabel
= gtk_label_new(NULL
);
1203 gtk_label_set_line_wrap(GTK_LABEL(beval
->balloonLabel
), FALSE
);
1204 gtk_label_set_justify(GTK_LABEL(beval
->balloonLabel
), GTK_JUSTIFY_LEFT
);
1205 gtk_misc_set_alignment(GTK_MISC(beval
->balloonLabel
), 0.5f
, 0.5f
);
1206 gtk_widget_set_name(beval
->balloonLabel
, "vim-balloon-label");
1207 gtk_widget_show(beval
->balloonLabel
);
1209 gtk_container_add(GTK_CONTAINER(beval
->balloonShell
), beval
->balloonLabel
);
1212 #else /* !FEAT_GUI_GTK */
1226 if (beval
->msg
!= NULL
)
1228 /* Show the Balloon */
1230 /* Calculate the label's width and height */
1231 #ifdef FEAT_GUI_MOTIF
1234 /* For the callback function we parse NL characters to create a
1235 * multi-line label. This doesn't work for all languages, but
1236 * XmStringCreateLocalized() doesn't do multi-line labels... */
1237 if (beval
->msgCB
!= NULL
)
1238 s
= XmStringCreateLtoR((char *)beval
->msg
, XmFONTLIST_DEFAULT_TAG
);
1240 s
= XmStringCreateLocalized((char *)beval
->msg
);
1244 fl
= gui_motif_fontset2fontlist(&gui
.tooltip_fontset
);
1247 XmStringExtent(fl
, s
, &w
, &h
);
1251 w
+= gui
.border_offset
<< 1;
1252 h
+= gui
.border_offset
<< 1;
1253 XtVaSetValues(beval
->balloonLabel
, XmNlabelString
, s
, NULL
);
1256 /* Assume XtNinternational == True */
1258 XFontSetExtents
*ext
;
1260 XtVaGetValues(beval
->balloonLabel
, XtNfontSet
, &fset
, NULL
);
1261 ext
= XExtentsOfFontSet(fset
);
1262 h
= ext
->max_ink_extent
.height
;
1263 w
= XmbTextEscapement(fset
,
1265 (int)STRLEN(beval
->msg
));
1266 w
+= gui
.border_offset
<< 1;
1267 h
+= gui
.border_offset
<< 1;
1268 XtVaSetValues(beval
->balloonLabel
, XtNlabel
, beval
->msg
, NULL
);
1271 /* Compute position of the balloon area */
1272 tx
= beval
->x_root
+ EVAL_OFFSET_X
;
1273 ty
= beval
->y_root
+ EVAL_OFFSET_Y
;
1274 if ((tx
+ w
) > beval
->screen_width
)
1275 tx
= beval
->screen_width
- w
;
1276 if ((ty
+ h
) > beval
->screen_height
)
1277 ty
= beval
->screen_height
- h
;
1278 #ifdef FEAT_GUI_MOTIF
1279 XtVaSetValues(beval
->balloonShell
,
1285 XtVaSetValues(beval
->balloonShell
,
1290 /* Set tooltip colors */
1294 #ifdef FEAT_GUI_MOTIF
1295 args
[0].name
= XmNbackground
;
1296 args
[0].value
= gui
.tooltip_bg_pixel
;
1297 args
[1].name
= XmNforeground
;
1298 args
[1].value
= gui
.tooltip_fg_pixel
;
1300 args
[0].name
= XtNbackground
;
1301 args
[0].value
= gui
.tooltip_bg_pixel
;
1302 args
[1].name
= XtNforeground
;
1303 args
[1].value
= gui
.tooltip_fg_pixel
;
1305 XtSetValues(beval
->balloonLabel
, &args
[0], XtNumber(args
));
1308 XtPopup(beval
->balloonShell
, XtGrabNone
);
1310 beval
->showState
= ShS_SHOWING
;
1312 current_beval
= beval
;
1320 undrawBalloon(beval
)
1323 if (beval
->balloonShell
!= (Widget
)0)
1324 XtPopdown(beval
->balloonShell
);
1325 beval
->showState
= ShS_NEUTRAL
;
1327 current_beval
= NULL
;
1331 cancelBalloon(beval
)
1334 if (beval
->showState
== ShS_SHOWING
1335 || beval
->showState
== ShS_UPDATE_PENDING
)
1336 undrawBalloon(beval
);
1338 if (beval
->timerID
!= (XtIntervalId
)NULL
)
1340 XtRemoveTimeOut(beval
->timerID
);
1341 beval
->timerID
= (XtIntervalId
)NULL
;
1343 beval
->showState
= ShS_NEUTRAL
;
1348 createBalloonEvalWindow(beval
)
1355 #ifdef FEAT_GUI_MOTIF
1356 XtSetArg(args
[n
], XmNallowShellResize
, True
); n
++;
1357 beval
->balloonShell
= XtAppCreateShell("balloonEval", "BalloonEval",
1358 overrideShellWidgetClass
, gui
.dpy
, args
, n
);
1361 XtSetArg(args
[n
], XtNallowShellResize
, True
); n
++;
1362 beval
->balloonShell
= XtAppCreateShell("balloonEval", "BalloonEval",
1363 overrideShellWidgetClass
, gui
.dpy
, args
, n
);
1367 #ifdef FEAT_GUI_MOTIF
1371 fl
= gui_motif_fontset2fontlist(&gui
.tooltip_fontset
);
1372 XtSetArg(args
[n
], XmNforeground
, gui
.tooltip_fg_pixel
); n
++;
1373 XtSetArg(args
[n
], XmNbackground
, gui
.tooltip_bg_pixel
); n
++;
1374 XtSetArg(args
[n
], XmNfontList
, fl
); n
++;
1375 XtSetArg(args
[n
], XmNalignment
, XmALIGNMENT_BEGINNING
); n
++;
1376 beval
->balloonLabel
= XtCreateManagedWidget("balloonLabel",
1377 xmLabelWidgetClass
, beval
->balloonShell
, args
, n
);
1379 #else /* FEAT_GUI_ATHENA */
1380 XtSetArg(args
[n
], XtNforeground
, gui
.tooltip_fg_pixel
); n
++;
1381 XtSetArg(args
[n
], XtNbackground
, gui
.tooltip_bg_pixel
); n
++;
1382 XtSetArg(args
[n
], XtNinternational
, True
); n
++;
1383 XtSetArg(args
[n
], XtNfontSet
, gui
.tooltip_fontset
); n
++;
1384 beval
->balloonLabel
= XtCreateManagedWidget("balloonLabel",
1385 labelWidgetClass
, beval
->balloonShell
, args
, n
);
1389 #endif /* !FEAT_GUI_GTK */
1390 #endif /* !FEAT_GUI_W32 */
1392 #endif /* FEAT_BEVAL */