Merge branch 'vim-with-runtime' into feat/var-tabstops
[vim_extended.git] / src / gui_beval.c
blob6139b9fcb289c147ff01c488da472c5e9f13b1fa
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.
9 */
11 #include "vim.h"
13 #if defined(FEAT_BEVAL) || defined(PROTO)
16 * Common code, invoked when the mouse is resting for a moment.
18 void
19 general_beval_cb(beval, state)
20 BalloonEval *beval;
21 int state UNUSED;
23 win_T *wp;
24 int col;
25 int use_sandbox;
26 linenr_T lnum;
27 char_u *text;
28 static char_u *result = NULL;
29 long winnr = 0;
30 char_u *bexpr;
31 buf_T *save_curbuf;
32 #ifdef FEAT_WINDOWS
33 win_T *cw;
34 #endif
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)
40 return;
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. */
44 if (recursive)
45 return;
46 recursive = TRUE;
48 #ifdef FEAT_EVAL
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;
53 if (*bexpr != NUL)
55 # ifdef FEAT_WINDOWS
56 /* Convert window pointer to number. */
57 for (cw = firstwin; cw != wp; cw = cw->w_next)
58 ++winnr;
59 # endif
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);
66 vim_free(text);
69 * Temporarily change the curbuf, so that we can determine whether
70 * the buffer-local balloonexpr option was set insecurely.
72 save_curbuf = curbuf;
73 curbuf = wp->w_buffer;
74 use_sandbox = was_set_insecurely((char_u *)"balloonexpr",
75 *curbuf->b_p_bexpr == NUL ? 0 : OPT_LOCAL);
76 curbuf = save_curbuf;
77 if (use_sandbox)
78 ++sandbox;
79 ++textlock;
81 vim_free(result);
82 result = eval_to_string(bexpr, NULL, TRUE);
84 if (use_sandbox)
85 --sandbox;
86 --textlock;
88 set_vim_var_string(VV_BEVAL_TEXT, NULL, -1);
89 if (result != NULL && result[0] != NUL)
91 gui_mch_post_balloon(beval, result);
92 recursive = FALSE;
93 return;
97 #endif
98 #ifdef FEAT_NETBEANS_INTG
99 if (bevalServers & BEVAL_NETBEANS)
100 netbeans_beval_cb(beval, state);
101 #endif
102 #ifdef FEAT_SUN_WORKSHOP
103 if (bevalServers & BEVAL_WORKSHOP)
104 workshop_beval_cb(beval, state);
105 #endif
107 recursive = FALSE;
110 /* on Win32 only get_beval_info() is required */
111 #if !defined(FEAT_GUI_W32) || defined(PROTO)
113 #ifdef FEAT_GUI_GTK
114 # include <gdk/gdkkeysyms.h>
115 # include <gtk/gtk.h>
116 #else
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>
125 # else
126 /* Assume Athena */
127 # include <X11/Shell.h>
128 # ifdef FEAT_GUI_NEXTAW
129 # include <X11/neXtaw/Label.h>
130 # else
131 # include <X11/Xaw/Label.h>
132 # endif
133 # endif
134 #endif
136 #include "gui_beval.h"
138 #ifndef FEAT_GUI_GTK
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;
147 #endif
149 #ifdef FEAT_GUI_GTK
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));
158 # ifndef HAVE_GTK2
159 static void balloon_draw_cb __ARGS((GtkWidget *, GdkRectangle *, gpointer));
160 # endif
161 #else
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 *));
167 #endif
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).
184 BalloonEval *
185 gui_mch_create_beval_area(target, mesg, mesgCB, clientData)
186 void *target;
187 char_u *mesg;
188 void (*mesgCB)__ARGS((BalloonEval *, int));
189 void *clientData;
191 #ifndef FEAT_GUI_GTK
192 char *display_name; /* get from gui.dpy */
193 int screen_num;
194 char *p;
195 #endif
196 BalloonEval *beval;
198 if (mesg != NULL && mesgCB != NULL)
200 EMSG(_("E232: Cannot create BalloonEval with both message and callback"));
201 return NULL;
204 beval = (BalloonEval *)alloc(sizeof(BalloonEval));
205 if (beval != NULL)
207 #ifdef FEAT_GUI_GTK
208 beval->target = GTK_WIDGET(target);
209 beval->balloonShell = NULL;
210 beval->timerID = 0;
211 #else
212 beval->target = (Widget)target;
213 beval->balloonShell = NULL;
214 beval->timerID = (XtIntervalId)NULL;
215 beval->appContext = XtWidgetToApplicationContext((Widget)target);
216 #endif
217 beval->showState = ShS_NEUTRAL;
218 beval->x = 0;
219 beval->y = 0;
220 beval->msg = mesg;
221 beval->msgCB = mesgCB;
222 beval->clientData = clientData;
223 #ifdef FEAT_VARTABS
224 beval->vts = 0;
225 #endif
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);
235 #ifndef FEAT_GUI_GTK
237 * Now create and save the screen width and height. Used in drawing.
239 display_name = DisplayString(gui.dpy);
240 p = strrchr(display_name, '.');
241 if (p != NULL)
242 screen_num = atoi(++p);
243 else
244 screen_num = 0;
245 beval->screen_width = DisplayWidth(gui.dpy, screen_num);
246 beval->screen_height = DisplayHeight(gui.dpy, screen_num);
247 #endif
250 return beval;
253 #if defined(FEAT_BEVAL_TIP) || defined(PROTO)
255 * Destroy a balloon-eval and free its associated memory.
257 void
258 gui_mch_destroy_beval_area(beval)
259 BalloonEval *beval;
261 cancelBalloon(beval);
262 removeEventHandler(beval);
263 /* Children will automatically be destroyed */
264 # ifdef FEAT_GUI_GTK
265 gtk_widget_destroy(beval->balloonShell);
266 # else
267 XtDestroyWidget(beval->balloonShell);
268 # endif
269 # ifdef FEAT_VARTABS
270 if (beval->vts)
271 vim_free(beval->vts);
272 # endif
273 vim_free(beval);
275 #endif
277 void
278 gui_mch_enable_beval_area(beval)
279 BalloonEval *beval;
281 if (beval != NULL)
282 addEventHandler(beval->target, beval);
285 void
286 gui_mch_disable_beval_area(beval)
287 BalloonEval *beval;
289 if (beval != NULL)
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.
300 BalloonEval *
301 gui_mch_currently_showing_beval()
303 return current_beval;
305 #endif
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)
318 BalloonEval *beval;
319 int getword;
320 win_T **winp;
321 linenr_T *lnump;
322 char_u **textp;
323 int *colp;
325 win_T *wp;
326 int row, col;
327 char_u *lbuf;
328 linenr_T lnum;
330 *textp = NULL;
331 row = Y_2_ROW(beval->y);
332 col = X_2_COL(beval->x);
333 #ifdef FEAT_WINDOWS
334 wp = mouse_find_win(&row, &col);
335 #else
336 wp = firstwin;
337 #endif
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
341 * number. */
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. */
349 if (getword)
351 /* For Netbeans we get the relevant part of the line
352 * instead of the whole line. */
353 int len;
354 pos_T *spos = NULL, *epos = NULL;
356 if (VIsual_active)
358 if (lt(VIsual, curwin->w_cursor))
360 spos = &VIsual;
361 epos = &curwin->w_cursor;
363 else
365 spos = &curwin->w_cursor;
366 epos = &VIsual;
370 col = vcol2col(wp, lnum, col) - 1;
372 if (VIsual_active
373 && wp->w_buffer == curwin->w_buffer
374 && (lnum == spos->lnum
375 ? col >= (int)spos->col
376 : lnum > spos->lnum)
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)
385 return FAIL;
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'));
390 lnum = spos->lnum;
391 col = spos->col;
393 else
395 /* Find the word under the cursor. */
396 ++emsg_off;
397 len = find_ident_at_pos(wp, lnum, (colnr_T)col, &lbuf,
398 FIND_IDENT + FIND_STRING + FIND_EVAL);
399 --emsg_off;
400 if (len == 0)
401 return FAIL;
402 lbuf = vim_strnsave(lbuf, len);
406 *winp = wp;
407 *lnump = lnum;
408 *textp = lbuf;
409 *colp = col;
410 #ifdef FEAT_VARTABS
411 if (beval->vts)
412 vim_free(beval->vts);
413 beval->vts = tabstop_copy(wp->w_buffer->b_p_vts_ary);
414 #endif
415 beval->ts = wp->w_buffer->b_p_ts;
416 return OK;
421 return FAIL;
424 # if !defined(FEAT_GUI_W32) || defined(PROTO)
427 * Show a balloon with "mesg".
429 void
430 gui_mch_post_balloon(beval, mesg)
431 BalloonEval *beval;
432 char_u *mesg;
434 beval->msg = mesg;
435 if (mesg != NULL)
436 drawBalloon(beval);
437 else
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.
448 void
449 gui_mch_unpost_balloon(beval)
450 BalloonEval *beval;
452 undrawBalloon(beval);
454 #endif
456 #ifdef FEAT_GUI_GTK
458 * We can unconditionally use ANSI-style prototypes here since
459 * GTK+ requires an ANSI C compiler anyway.
461 static void
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
468 * in gui_gtk_x11.c.
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),
473 beval);
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),
484 beval);
488 static void
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),
494 beval);
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),
502 beval);
506 static gint
507 target_event_cb(GtkWidget *widget, GdkEvent *event, gpointer data)
509 BalloonEval *beval = (BalloonEval *)data;
511 switch (event->type)
513 case GDK_ENTER_NOTIFY:
514 pointer_event(beval, (int)event->crossing.x,
515 (int)event->crossing.y,
516 event->crossing.state);
517 break;
518 case GDK_MOTION_NOTIFY:
519 if (event->motion.is_hint)
521 int x;
522 int y;
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);
531 else
533 pointer_event(beval, (int)event->motion.x,
534 (int)event->motion.y,
535 event->motion.state);
537 break;
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);
545 break;
546 case GDK_BUTTON_PRESS:
547 # ifdef HAVE_GTK2
548 case GDK_SCROLL:
549 # endif
550 cancelBalloon(beval);
551 break;
552 case GDK_KEY_PRESS:
553 key_event(beval, event->key.keyval, TRUE);
554 break;
555 case GDK_KEY_RELEASE:
556 key_event(beval, event->key.keyval, FALSE);
557 break;
558 default:
559 break;
562 return FALSE; /* continue emission */
565 static gint
566 mainwin_event_cb(GtkWidget *widget UNUSED, GdkEvent *event, gpointer data)
568 BalloonEval *beval = (BalloonEval *)data;
570 switch (event->type)
572 case GDK_KEY_PRESS:
573 key_event(beval, event->key.keyval, TRUE);
574 break;
575 case GDK_KEY_RELEASE:
576 key_event(beval, event->key.keyval, FALSE);
577 break;
578 default:
579 break;
582 return FALSE; /* continue emission */
585 static void
586 pointer_event(BalloonEval *beval, int x, int y, unsigned state)
588 int distance;
590 distance = ABS(x - beval->x) + ABS(y - beval->y);
592 if (distance > 4)
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)))
605 beval->x = x;
606 beval->y = y;
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);
620 else
622 beval->timerID = gtk_timeout_add((guint32)p_bdlay,
623 &timeout_cb, beval);
629 static void
630 key_event(BalloonEval *beval, unsigned keyval, int is_keypress)
632 if (beval->showState == ShS_SHOWING && beval->msgCB != NULL)
634 switch (keyval)
636 case GDK_Shift_L:
637 case GDK_Shift_R:
638 beval->showState = ShS_UPDATE_PENDING;
639 (*beval->msgCB)(beval, (is_keypress)
640 ? (int)GDK_SHIFT_MASK : 0);
641 break;
642 case GDK_Control_L:
643 case GDK_Control_R:
644 beval->showState = ShS_UPDATE_PENDING;
645 (*beval->msgCB)(beval, (is_keypress)
646 ? (int)GDK_CONTROL_MASK : 0);
647 break;
648 default:
649 /* Don't do this for key release, we apparently get these with
650 * focus changes in some GTK version. */
651 if (is_keypress)
652 cancelBalloon(beval);
653 break;
656 else
657 cancelBalloon(beval);
660 static gint
661 timeout_cb(gpointer data)
663 BalloonEval *beval = (BalloonEval *)data;
665 beval->timerID = 0;
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 */
676 static gint
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",
684 0, 0, -1, -1);
686 return FALSE; /* continue emission */
689 # ifndef HAVE_GTK2
690 static void
691 balloon_draw_cb(GtkWidget *widget, GdkRectangle *area, gpointer data)
693 GtkWidget *child;
694 GdkRectangle child_area;
696 gtk_paint_flat_box(widget->style, widget->window,
697 GTK_STATE_NORMAL, GTK_SHADOW_OUT,
698 area, widget, "tooltip",
699 0, 0, -1, -1);
701 child = GTK_BIN(widget)->child;
703 if (gtk_widget_intersect(child, area, &child_area))
704 gtk_widget_draw(child, &child_area);
706 # endif
708 #else /* !FEAT_GUI_GTK */
710 static void
711 addEventHandler(target, beval)
712 Widget target;
713 BalloonEval *beval;
715 XtAddEventHandler(target,
716 PointerMotionMask | EnterWindowMask |
717 LeaveWindowMask | ButtonPressMask | KeyPressMask |
718 KeyReleaseMask,
719 False,
720 pointerEventEH, (XtPointer)beval);
723 static void
724 removeEventHandler(beval)
725 BalloonEval *beval;
727 XtRemoveEventHandler(beval->target,
728 PointerMotionMask | EnterWindowMask |
729 LeaveWindowMask | ButtonPressMask | KeyPressMask |
730 KeyReleaseMask,
731 False,
732 pointerEventEH, (XtPointer)beval);
737 * The X event handler. All it does is call the real event handler.
739 static void
740 pointerEventEH(w, client_data, event, unused)
741 Widget w UNUSED;
742 XtPointer client_data;
743 XEvent *event;
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.
756 static void
757 pointerEvent(beval, event)
758 BalloonEval *beval;
759 XEvent *event;
761 Position distance; /* a measure of how much the ponter moved */
762 Position delta; /* used to compute distance */
764 switch (event->type)
766 case EnterNotify:
767 case MotionNotify:
768 delta = event->xmotion.x - beval->x;
769 if (delta < 0)
770 delta = -delta;
771 distance = delta;
772 delta = event->xmotion.y - beval->y;
773 if (delta < 0)
774 delta = -delta;
775 distance += delta;
776 if (distance > 4)
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);
805 else
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);
816 break;
819 * Motif and Athena version: Keystrokes will be caught by the
820 * "textArea" widget, and handled in gui_x11_key_hit_cb().
822 case KeyPress:
823 if (beval->showState == ShS_SHOWING && beval->msgCB != NULL)
825 Modifiers modifier;
826 KeySym keysym;
828 XtTranslateKeycode(gui.dpy,
829 event->xkey.keycode, event->xkey.state,
830 &modifier, &keysym);
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);
841 else
842 cancelBalloon(beval);
844 else
845 cancelBalloon(beval);
846 break;
848 case KeyRelease:
849 if (beval->showState == ShS_SHOWING && beval->msgCB != NULL)
851 Modifiers modifier;
852 KeySym keysym;
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);
865 else
866 cancelBalloon(beval);
868 else
869 cancelBalloon(beval);
870 break;
872 case LeaveNotify:
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);
878 break;
880 case ButtonPress:
881 cancelBalloon(beval);
882 break;
884 default:
885 break;
889 static void
890 timerRoutine(dx, id)
891 XtPointer dx;
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 */
908 static void
909 requestBalloon(beval)
910 BalloonEval *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)
921 drawBalloon(beval);
925 #ifdef FEAT_GUI_GTK
927 # ifdef HAVE_GTK2
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) \
935 || (c) == DEL)
936 static void
937 set_printable_label_text(GtkLabel *label, char_u *text)
939 char_u *convbuf = NULL;
940 char_u *buf;
941 char_u *p;
942 char_u *pdest;
943 unsigned int len;
944 int charlen;
945 int uc;
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);
952 if (convbuf != NULL)
953 text = convbuf;
956 /* First let's see how much we need to allocate */
957 len = 0;
958 for (p = text; *p != NUL; p += charlen)
960 if ((*p & 0x80) == 0) /* be quick for ASCII */
962 charlen = 1;
963 len += IS_NONPRINTABLE(*p) ? 2 : 1; /* nonprintable: ^X */
965 else
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> */
979 else
980 len += charlen;
984 attr_list = pango_attr_list_new();
985 buf = alloc(len + 1);
987 /* Now go for the real work */
988 if (buf != NULL)
990 attrentry_T *aep;
991 PangoAttribute *attr;
992 guicolor_T pixel;
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);
1002 pdest = buf;
1003 p = text;
1004 while (*p != NUL)
1006 /* Be quick for ASCII */
1007 if ((*p & 0x80) == 0 && !IS_NONPRINTABLE(*p))
1009 *pdest++ = *p++;
1011 else
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))
1021 int outlen;
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> */
1028 outlen = 4;
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;
1037 else
1039 transchar_nonprint(pdest, *p); /* ^X */
1040 outlen = 2;
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);
1050 pdest += outlen;
1051 p += charlen;
1053 else
1056 *pdest++ = *p++;
1057 while (--charlen != 0);
1061 *pdest = NUL;
1064 vim_free(convbuf);
1066 gtk_label_set_text(label, (const char *)buf);
1067 vim_free(buf);
1069 gtk_label_set_attributes(label, attr_list);
1070 pango_attr_list_unref(attr_list);
1072 # undef IS_NONPRINTABLE
1073 # endif /* HAVE_GTK2 */
1076 * Draw a balloon.
1078 static void
1079 drawBalloon(BalloonEval *beval)
1081 if (beval->msg != NULL)
1083 GtkRequisition requisition;
1084 int screen_w;
1085 int screen_h;
1086 int x;
1087 int y;
1088 int x_offset = EVAL_OFFSET_X;
1089 int y_offset = EVAL_OFFSET_Y;
1090 # ifdef HAVE_GTK2
1091 PangoLayout *layout;
1092 # endif
1093 # ifdef HAVE_GTK_MULTIHEAD
1094 GdkScreen *screen;
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);
1100 # else
1101 screen_w = gdk_screen_width();
1102 screen_h = gdk_screen_height();
1103 # endif
1104 gtk_widget_ensure_style(beval->balloonShell);
1105 gtk_widget_ensure_style(beval->balloonLabel);
1107 # ifdef HAVE_GTK2
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);
1117 # else
1118 pango_layout_set_wrap(layout, PANGO_WRAP_WORD);
1119 # endif
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,
1123 screen_w / 2,
1124 MAX(20, screen_w - 20)));
1126 /* Calculate the balloon's width and height. */
1127 gtk_widget_size_request(beval->balloonShell, &requisition);
1128 # else
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);
1146 # endif
1148 /* Compute position of the balloon area */
1149 gdk_window_get_origin(beval->target->window, &x, &y);
1150 x += beval->x;
1151 y += beval->y;
1153 /* Get out of the way of the mouse pointer */
1154 if (x + x_offset + requisition.width > screen_w)
1155 y_offset += 15;
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;
1172 * Undraw a balloon.
1174 static void
1175 undrawBalloon(BalloonEval *beval)
1177 if (beval->balloonShell != NULL)
1178 gtk_widget_hide(beval->balloonShell);
1179 beval->showState = ShS_NEUTRAL;
1182 static void
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);
1192 beval->timerID = 0;
1194 beval->showState = ShS_NEUTRAL;
1197 static void
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);
1209 # ifndef HAVE_GTK2
1210 gtk_signal_connect((GtkObject*)(beval->balloonShell), "draw",
1211 GTK_SIGNAL_FUNC(balloon_draw_cb), NULL);
1212 # endif
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 */
1227 * Draw a balloon.
1229 static void
1230 drawBalloon(beval)
1231 BalloonEval *beval;
1233 Dimension w;
1234 Dimension h;
1235 Position tx;
1236 Position ty;
1238 if (beval->msg != NULL)
1240 /* Show the Balloon */
1242 /* Calculate the label's width and height */
1243 #ifdef FEAT_GUI_MOTIF
1244 XmString s;
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);
1251 else
1252 s = XmStringCreateLocalized((char *)beval->msg);
1254 XmFontList fl;
1256 fl = gui_motif_fontset2fontlist(&gui.tooltip_fontset);
1257 if (fl != NULL)
1259 XmStringExtent(fl, s, &w, &h);
1260 XmFontListFree(fl);
1263 w += gui.border_offset << 1;
1264 h += gui.border_offset << 1;
1265 XtVaSetValues(beval->balloonLabel, XmNlabelString, s, NULL);
1266 XmStringFree(s);
1267 #else /* Athena */
1268 /* Assume XtNinternational == True */
1269 XFontSet fset;
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,
1276 (char *)beval->msg,
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);
1281 #endif
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,
1292 XmNx, tx,
1293 XmNy, ty,
1294 NULL);
1295 #else
1296 /* Athena */
1297 XtVaSetValues(beval->balloonShell,
1298 XtNx, tx,
1299 XtNy, ty,
1300 NULL);
1301 #endif
1302 /* Set tooltip colors */
1304 Arg args[2];
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;
1311 #else /* Athena */
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;
1316 #endif
1317 XtSetValues(beval->balloonLabel, &args[0], XtNumber(args));
1320 XtPopup(beval->balloonShell, XtGrabNone);
1322 beval->showState = ShS_SHOWING;
1324 current_beval = beval;
1329 * Undraw a balloon.
1331 static void
1332 undrawBalloon(beval)
1333 BalloonEval *beval;
1335 if (beval->balloonShell != (Widget)0)
1336 XtPopdown(beval->balloonShell);
1337 beval->showState = ShS_NEUTRAL;
1339 current_beval = NULL;
1342 static void
1343 cancelBalloon(beval)
1344 BalloonEval *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;
1359 static void
1360 createBalloonEvalWindow(beval)
1361 BalloonEval *beval;
1363 Arg args[12];
1364 int n;
1366 n = 0;
1367 #ifdef FEAT_GUI_MOTIF
1368 XtSetArg(args[n], XmNallowShellResize, True); n++;
1369 beval->balloonShell = XtAppCreateShell("balloonEval", "BalloonEval",
1370 overrideShellWidgetClass, gui.dpy, args, n);
1371 #else
1372 /* Athena */
1373 XtSetArg(args[n], XtNallowShellResize, True); n++;
1374 beval->balloonShell = XtAppCreateShell("balloonEval", "BalloonEval",
1375 overrideShellWidgetClass, gui.dpy, args, n);
1376 #endif
1378 n = 0;
1379 #ifdef FEAT_GUI_MOTIF
1381 XmFontList fl;
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);
1398 #endif
1401 #endif /* !FEAT_GUI_GTK */
1402 #endif /* !FEAT_GUI_W32 */
1404 #endif /* FEAT_BEVAL */