blob: 755f943100c1233f1f92e1e8b53eb60740c692da [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 * Visual Workshop integration by Gordon Prieur
5 *
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 */
10
11#include "vim.h"
12
13#if defined(FEAT_BEVAL) || defined(PROTO)
14
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000015/*
16 * Common code, invoked when the mouse is resting for a moment.
17 */
18/*ARGSUSED*/
19 void
20general_beval_cb(beval, state)
21 BalloonEval *beval;
22 int state;
23{
24 win_T *wp;
25 int col;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000026 int use_sandbox;
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000027 linenr_T lnum;
28 char_u *text;
29 static char_u *result = NULL;
30 long winnr = 0;
31 win_T *cw;
32
33
34 /* Don't do anything when 'ballooneval' is off, messages scrolled the
35 * windows up or we have no beval area. */
36 if (!p_beval || balloonEval == NULL || msg_scrolled > 0)
37 return;
38
39#ifdef FEAT_EVAL
40 if (*p_bexpr != NUL
41 && get_beval_info(balloonEval, TRUE, &wp, &lnum, &text, &col) == OK)
42 {
43 /* Convert window pointer to number. */
44 for (cw = firstwin; cw != wp; cw = cw->w_next)
45 ++winnr;
46
47 set_vim_var_nr(VV_BEVAL_BUFNR, (long)wp->w_buffer->b_fnum);
48 set_vim_var_nr(VV_BEVAL_WINNR, winnr);
49 set_vim_var_nr(VV_BEVAL_LNUM, (long)lnum);
50 set_vim_var_nr(VV_BEVAL_COL, (long)(col + 1));
51 set_vim_var_string(VV_BEVAL_TEXT, text, -1);
52 vim_free(text);
53
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000054 use_sandbox = was_set_insecurely((char_u *)"balloonexpr");
55 if (use_sandbox)
56 ++sandbox;
57 ++textlock;
58
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000059 vim_free(result);
60 result = eval_to_string(p_bexpr, NULL);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000061
62 if (use_sandbox)
63 --sandbox;
64 --textlock;
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000065
66 set_vim_var_string(VV_BEVAL_TEXT, NULL, -1);
67 if (result != NULL && result[0] != NUL)
68 {
69 gui_mch_post_balloon(beval, result);
70 return;
71 }
72 }
73#endif
74#ifdef FEAT_NETBEANS_INTG
75 if (bevalServers & BEVAL_NETBEANS)
76 netbeans_beval_cb(beval, state);
77#endif
78#ifdef FEAT_SUN_WORKSHOP
79 if (bevalServers & BEVAL_WORKSHOP)
80 workshop_beval_cb(beval, state);
81#endif
82}
83
84/* on Win32 only get_beval_info() is required */
Bram Moolenaar071d4272004-06-13 20:20:40 +000085#if !defined(FEAT_GUI_W32) || defined(PROTO)
86
87#ifdef FEAT_GUI_GTK
88# include <gdk/gdkkeysyms.h>
89# include <gtk/gtk.h>
90#else
91# include <X11/keysym.h>
92# ifdef FEAT_GUI_MOTIF
93# include <Xm/PushB.h>
94# include <Xm/Separator.h>
95# include <Xm/List.h>
96# include <Xm/Label.h>
97# include <Xm/AtomMgr.h>
98# include <Xm/Protocols.h>
99# else
100 /* Assume Athena */
101# include <X11/Shell.h>
102# include <X11/Xaw/Label.h>
103# endif
104#endif
105
106#include "gui_beval.h"
107
108#ifndef FEAT_GUI_GTK
109extern Widget vimShell;
110
111/*
112 * Currently, we assume that there can be only one BalloonEval showing
113 * on-screen at any given moment. This variable will hold the currently
114 * showing BalloonEval or NULL if none is showing.
115 */
116static BalloonEval *current_beval = NULL;
117#endif
118
119#ifdef FEAT_GUI_GTK
120static void addEventHandler __ARGS((GtkWidget *, BalloonEval *));
121static void removeEventHandler __ARGS((BalloonEval *));
122static gint target_event_cb __ARGS((GtkWidget *, GdkEvent *, gpointer));
123static gint mainwin_event_cb __ARGS((GtkWidget *, GdkEvent *, gpointer));
124static void pointer_event __ARGS((BalloonEval *, int, int, unsigned));
125static void key_event __ARGS((BalloonEval *, unsigned, int));
126static gint timeout_cb __ARGS((gpointer));
127static gint balloon_expose_event_cb __ARGS((GtkWidget *, GdkEventExpose *, gpointer));
128# ifndef HAVE_GTK2
129static void balloon_draw_cb __ARGS((GtkWidget *, GdkRectangle *, gpointer));
130# endif
131#else
132static void addEventHandler __ARGS((Widget, BalloonEval *));
133static void removeEventHandler __ARGS((BalloonEval *));
134static void pointerEventEH __ARGS((Widget, XtPointer, XEvent *, Boolean *));
135static void pointerEvent __ARGS((BalloonEval *, XEvent *));
136static void timerRoutine __ARGS((XtPointer, XtIntervalId *));
137#endif
138static void cancelBalloon __ARGS((BalloonEval *));
139static void requestBalloon __ARGS((BalloonEval *));
140static void drawBalloon __ARGS((BalloonEval *));
141static void undrawBalloon __ARGS((BalloonEval *beval));
142static void createBalloonEvalWindow __ARGS((BalloonEval *));
143
144
145
146/*
147 * Create a balloon-evaluation area for a Widget.
148 * There can be either a "mesg" for a fixed string or "mesgCB" to generate a
149 * message by calling this callback function.
150 * When "mesg" is not NULL it must remain valid for as long as the balloon is
151 * used. It is not freed here.
152 * Returns a pointer to the resulting object (NULL when out of memory).
153 */
154 BalloonEval *
155gui_mch_create_beval_area(target, mesg, mesgCB, clientData)
156 void *target;
157 char_u *mesg;
158 void (*mesgCB)__ARGS((BalloonEval *, int));
159 void *clientData;
160{
161#ifndef FEAT_GUI_GTK
162 char *display_name; /* get from gui.dpy */
163 int screen_num;
164 char *p;
165#endif
166 BalloonEval *beval;
167
168 if (mesg != NULL && mesgCB != NULL)
169 {
170 EMSG(_("E232: Cannot create BalloonEval with both message and callback"));
171 return NULL;
172 }
173
174 beval = (BalloonEval *)alloc(sizeof(BalloonEval));
175 if (beval != NULL)
176 {
177#ifdef FEAT_GUI_GTK
178 beval->target = GTK_WIDGET(target);
179 beval->balloonShell = NULL;
180 beval->timerID = 0;
181#else
182 beval->target = (Widget)target;
183 beval->balloonShell = NULL;
184 beval->timerID = (XtIntervalId)NULL;
185 beval->appContext = XtWidgetToApplicationContext((Widget)target);
186#endif
187 beval->showState = ShS_NEUTRAL;
188 beval->x = 0;
189 beval->y = 0;
190 beval->msg = mesg;
191 beval->msgCB = mesgCB;
192 beval->clientData = clientData;
193
194 /*
195 * Set up event handler which will keep its eyes on the pointer,
196 * and when the pointer rests in a certain spot for a given time
197 * interval, show the beval.
198 */
199 addEventHandler(beval->target, beval);
200 createBalloonEvalWindow(beval);
201
202#ifndef FEAT_GUI_GTK
203 /*
204 * Now create and save the screen width and height. Used in drawing.
205 */
206 display_name = DisplayString(gui.dpy);
207 p = strrchr(display_name, '.');
208 if (p != NULL)
209 screen_num = atoi(++p);
210 else
211 screen_num = 0;
212 beval->screen_width = DisplayWidth(gui.dpy, screen_num);
213 beval->screen_height = DisplayHeight(gui.dpy, screen_num);
214#endif
215 }
216
217 return beval;
218}
219
220#if defined(FEAT_BEVAL_TIP) || defined(PROTO)
221/*
222 * Destroy a ballon-eval and free its associated memory.
223 */
224 void
225gui_mch_destroy_beval_area(beval)
226 BalloonEval *beval;
227{
228 cancelBalloon(beval);
229 removeEventHandler(beval);
230 /* Children will automatically be destroyed */
231# ifdef FEAT_GUI_GTK
232 gtk_widget_destroy(beval->balloonShell);
233# else
234 XtDestroyWidget(beval->balloonShell);
235# endif
236 vim_free(beval);
237}
238#endif
239
240 void
241gui_mch_enable_beval_area(beval)
242 BalloonEval *beval;
243{
244 if (beval != NULL)
245 addEventHandler(beval->target, beval);
246}
247
248 void
249gui_mch_disable_beval_area(beval)
250 BalloonEval *beval;
251{
252 if (beval != NULL)
253 removeEventHandler(beval);
254}
255
256#if defined(FEAT_BEVAL_TIP) || defined(PROTO)
257/*
258 * This function returns the BalloonEval * associated with the currently
259 * displayed tooltip. Returns NULL if there is no tooltip currently showing.
260 *
261 * Assumption: Only one tooltip can be shown at a time.
262 */
263 BalloonEval *
264gui_mch_currently_showing_beval()
265{
266 return current_beval;
267}
268#endif
269#endif /* !FEAT_GUI_W32 */
270
Bram Moolenaare2ac10d2005-03-07 23:26:06 +0000271#if defined(FEAT_SUN_WORKSHOP) || defined(FEAT_NETBEANS_INTG) \
272 || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000273/*
274 * Get the text and position to be evaluated for "beval".
Bram Moolenaare2ac10d2005-03-07 23:26:06 +0000275 * If "getword" is true the returned text is not the whole line but the
276 * relevant word in allocated memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000277 * Returns OK or FAIL.
278 */
279 int
Bram Moolenaare2ac10d2005-03-07 23:26:06 +0000280get_beval_info(beval, getword, winp, lnump, textp, colp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000281 BalloonEval *beval;
Bram Moolenaare2ac10d2005-03-07 23:26:06 +0000282 int getword;
283 win_T **winp;
284 linenr_T *lnump;
285 char_u **textp;
286 int *colp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000287{
288 win_T *wp;
289 int row, col;
290 char_u *lbuf;
291 linenr_T lnum;
292
Bram Moolenaare2ac10d2005-03-07 23:26:06 +0000293 *textp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000294 row = Y_2_ROW(beval->y);
295 col = X_2_COL(beval->x);
296 wp = mouse_find_win(&row, &col);
297 if (wp != NULL && row < wp->w_height && col < W_WIDTH(wp))
298 {
299 /* Found a window and the cursor is in the text. Now find the line
300 * number. */
301 if (!mouse_comp_pos(wp, &row, &col, &lnum))
302 {
303 /* Not past end of the file. */
304 lbuf = ml_get_buf(wp->w_buffer, lnum, FALSE);
305 if (col <= win_linetabsize(wp, lbuf, (colnr_T)MAXCOL))
306 {
307 /* Not past end of line. */
Bram Moolenaare2ac10d2005-03-07 23:26:06 +0000308 if (getword)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000309 {
310 /* For Netbeans we get the relevant part of the line
311 * instead of the whole line. */
312 int len;
313 pos_T *spos = NULL, *epos = NULL;
314
315 if (VIsual_active)
316 {
317 if (lt(VIsual, curwin->w_cursor))
318 {
319 spos = &VIsual;
320 epos = &curwin->w_cursor;
321 }
322 else
323 {
324 spos = &curwin->w_cursor;
325 epos = &VIsual;
326 }
327 }
328
329 col = vcol2col(wp, lnum, col) - 1;
330
331 if (VIsual_active
332 && wp->w_buffer == curwin->w_buffer
333 && (lnum == spos->lnum
334 ? col >= (int)spos->col
335 : lnum > spos->lnum)
336 && (lnum == epos->lnum
337 ? col <= (int)epos->col
338 : lnum < epos->lnum))
339 {
340 /* Visual mode and pointing to the line with the
341 * Visual selection: return selected text, with a
342 * maximum of one line. */
343 if (spos->lnum != epos->lnum || spos->col == epos->col)
344 return FAIL;
345
346 lbuf = ml_get_buf(curwin->w_buffer, VIsual.lnum, FALSE);
347 lbuf = vim_strnsave(lbuf + spos->col,
348 epos->col - spos->col + (*p_sel != 'e'));
349 lnum = spos->lnum;
350 col = spos->col;
351 }
352 else
353 {
354 /* Find the word under the cursor. */
355 ++emsg_off;
356 len = find_ident_at_pos(wp, lnum, (colnr_T)col, &lbuf,
357 FIND_IDENT + FIND_STRING + FIND_EVAL);
358 --emsg_off;
359 if (len == 0)
360 return FAIL;
361 lbuf = vim_strnsave(lbuf, len);
362 }
363 }
Bram Moolenaare2ac10d2005-03-07 23:26:06 +0000364
365 *winp = wp;
366 *lnump = lnum;
367 *textp = lbuf;
368 *colp = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000369 beval->ts = wp->w_buffer->b_p_ts;
370 return OK;
371 }
372 }
373 }
374
375 return FAIL;
376}
377
378# if !defined(FEAT_GUI_W32) || defined(PROTO)
379
380/*
381 * Show a balloon with "mesg".
382 */
383 void
384gui_mch_post_balloon(beval, mesg)
385 BalloonEval *beval;
386 char_u *mesg;
387{
388 beval->msg = mesg;
389 if (mesg != NULL)
390 drawBalloon(beval);
391 else
392 undrawBalloon(beval);
393}
394# endif /* FEAT_GUI_W32 */
395#endif /* FEAT_SUN_WORKSHOP || FEAT_NETBEANS_INTG || PROTO */
396
397#if !defined(FEAT_GUI_W32) || defined(PROTO)
398#if defined(FEAT_BEVAL_TIP) || defined(PROTO)
399/*
400 * Hide the given balloon.
401 */
402 void
403gui_mch_unpost_balloon(beval)
404 BalloonEval *beval;
405{
406 undrawBalloon(beval);
407}
408#endif
409
410#ifdef FEAT_GUI_GTK
411/*
412 * We can unconditionally use ANSI-style prototypes here since
413 * GTK+ requires an ANSI C compiler anyway.
414 */
415 static void
416addEventHandler(GtkWidget *target, BalloonEval *beval)
417{
418 /*
419 * Connect to the generic "event" signal instead of the individual
420 * signals for each event type, because the former is emitted earlier.
421 * This allows us to catch events independently of the signal handlers
422 * in gui_gtk_x11.c.
423 */
424 /* Should use GTK_OBJECT() here, but that causes a lint warning... */
425 gtk_signal_connect((GtkObject*)(target), "event",
426 GTK_SIGNAL_FUNC(target_event_cb),
427 beval);
428 /*
429 * Nasty: Key press events go to the main window thus the drawing area
430 * will never see them. This means we have to connect to the main window
431 * as well in order to catch those events.
432 */
433 if (gtk_socket_id == 0 && gui.mainwin != NULL
434 && gtk_widget_is_ancestor(target, gui.mainwin))
435 {
436 gtk_signal_connect((GtkObject*)(gui.mainwin), "event",
437 GTK_SIGNAL_FUNC(mainwin_event_cb),
438 beval);
439 }
440}
441
442 static void
443removeEventHandler(BalloonEval *beval)
444{
Bram Moolenaar33570922005-01-25 22:26:29 +0000445 /* LINTED: avoid warning: dubious operation on enum */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000446 gtk_signal_disconnect_by_func((GtkObject*)(beval->target),
447 GTK_SIGNAL_FUNC(target_event_cb),
448 beval);
449
450 if (gtk_socket_id == 0 && gui.mainwin != NULL
451 && gtk_widget_is_ancestor(beval->target, gui.mainwin))
452 {
Bram Moolenaar33570922005-01-25 22:26:29 +0000453 /* LINTED: avoid warning: dubious operation on enum */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000454 gtk_signal_disconnect_by_func((GtkObject*)(gui.mainwin),
455 GTK_SIGNAL_FUNC(mainwin_event_cb),
456 beval);
457 }
458}
459
460 static gint
461target_event_cb(GtkWidget *widget, GdkEvent *event, gpointer data)
462{
463 BalloonEval *beval = (BalloonEval *)data;
464
465 switch (event->type)
466 {
467 case GDK_ENTER_NOTIFY:
468 pointer_event(beval, (int)event->crossing.x,
469 (int)event->crossing.y,
470 event->crossing.state);
471 break;
472 case GDK_MOTION_NOTIFY:
473 if (event->motion.is_hint)
474 {
475 int x;
476 int y;
477 GdkModifierType state;
478 /*
479 * GDK_POINTER_MOTION_HINT_MASK is set, thus we cannot obtain
480 * the coordinates from the GdkEventMotion struct directly.
481 */
482 gdk_window_get_pointer(widget->window, &x, &y, &state);
483 pointer_event(beval, x, y, (unsigned int)state);
484 }
485 else
486 {
487 pointer_event(beval, (int)event->motion.x,
488 (int)event->motion.y,
489 event->motion.state);
490 }
491 break;
492 case GDK_LEAVE_NOTIFY:
493 /*
494 * Ignore LeaveNotify events that are not "normal".
495 * Apparently we also get it when somebody else grabs focus.
496 */
497 if (event->crossing.mode == GDK_CROSSING_NORMAL)
498 cancelBalloon(beval);
499 break;
500 case GDK_BUTTON_PRESS:
501# ifdef HAVE_GTK2
502 case GDK_SCROLL:
503# endif
504 cancelBalloon(beval);
505 break;
506 case GDK_KEY_PRESS:
507 key_event(beval, event->key.keyval, TRUE);
508 break;
509 case GDK_KEY_RELEASE:
510 key_event(beval, event->key.keyval, FALSE);
511 break;
512 default:
513 break;
514 }
515
516 return FALSE; /* continue emission */
517}
518
519/*ARGSUSED*/
520 static gint
521mainwin_event_cb(GtkWidget *widget, GdkEvent *event, gpointer data)
522{
523 BalloonEval *beval = (BalloonEval *)data;
524
525 switch (event->type)
526 {
527 case GDK_KEY_PRESS:
528 key_event(beval, event->key.keyval, TRUE);
529 break;
530 case GDK_KEY_RELEASE:
531 key_event(beval, event->key.keyval, FALSE);
532 break;
533 default:
534 break;
535 }
536
537 return FALSE; /* continue emission */
538}
539
540 static void
541pointer_event(BalloonEval *beval, int x, int y, unsigned state)
542{
543 int distance;
544
545 distance = ABS(x - beval->x) + ABS(y - beval->y);
546
547 if (distance > 4)
548 {
549 /*
550 * Moved out of the balloon location: cancel it.
551 * Remember button state
552 */
553 beval->state = state;
554 cancelBalloon(beval);
555
556 /* Mouse buttons are pressed - no balloon now */
557 if (!(state & ((int)GDK_BUTTON1_MASK | (int)GDK_BUTTON2_MASK
558 | (int)GDK_BUTTON3_MASK)))
559 {
560 beval->x = x;
561 beval->y = y;
562
563 if (state & (int)GDK_MOD1_MASK)
564 {
565 /*
566 * Alt is pressed -- enter super-evaluate-mode,
567 * where there is no time delay
568 */
569 if (beval->msgCB != NULL)
570 {
571 beval->showState = ShS_PENDING;
572 (*beval->msgCB)(beval, state);
573 }
574 }
575 else
576 {
577 beval->timerID = gtk_timeout_add((guint32)p_bdlay,
578 &timeout_cb, beval);
579 }
580 }
581 }
582}
583
584 static void
585key_event(BalloonEval *beval, unsigned keyval, int is_keypress)
586{
587 if (beval->showState == ShS_SHOWING && beval->msgCB != NULL)
588 {
589 switch (keyval)
590 {
591 case GDK_Shift_L:
592 case GDK_Shift_R:
593 beval->showState = ShS_UPDATE_PENDING;
594 (*beval->msgCB)(beval, (is_keypress)
595 ? (int)GDK_SHIFT_MASK : 0);
596 break;
597 case GDK_Control_L:
598 case GDK_Control_R:
599 beval->showState = ShS_UPDATE_PENDING;
600 (*beval->msgCB)(beval, (is_keypress)
601 ? (int)GDK_CONTROL_MASK : 0);
602 break;
603 default:
604 cancelBalloon(beval);
605 break;
606 }
607 }
608 else
609 cancelBalloon(beval);
610}
611
612 static gint
613timeout_cb(gpointer data)
614{
615 BalloonEval *beval = (BalloonEval *)data;
616
617 beval->timerID = 0;
618 /*
619 * If the timer event happens then the mouse has stopped long enough for
620 * a request to be started. The request will only send to the debugger if
621 * there the mouse is pointing at real data.
622 */
623 requestBalloon(beval);
624
625 return FALSE; /* don't call me again */
626}
627
628/*ARGSUSED2*/
629 static gint
630balloon_expose_event_cb(GtkWidget *widget, GdkEventExpose *event, gpointer data)
631{
632 gtk_paint_flat_box(widget->style, widget->window,
633 GTK_STATE_NORMAL, GTK_SHADOW_OUT,
634 &event->area, widget, "tooltip",
635 0, 0, -1, -1);
636
637 return FALSE; /* continue emission */
638}
639
640# ifndef HAVE_GTK2
641/*ARGSUSED2*/
642 static void
643balloon_draw_cb(GtkWidget *widget, GdkRectangle *area, gpointer data)
644{
645 GtkWidget *child;
646 GdkRectangle child_area;
647
648 gtk_paint_flat_box(widget->style, widget->window,
649 GTK_STATE_NORMAL, GTK_SHADOW_OUT,
650 area, widget, "tooltip",
651 0, 0, -1, -1);
652
653 child = GTK_BIN(widget)->child;
654
655 if (gtk_widget_intersect(child, area, &child_area))
656 gtk_widget_draw(child, &child_area);
657}
658# endif
659
660#else /* !FEAT_GUI_GTK */
661
662 static void
663addEventHandler(target, beval)
664 Widget target;
665 BalloonEval *beval;
666{
667 XtAddEventHandler(target,
668 PointerMotionMask | EnterWindowMask |
669 LeaveWindowMask | ButtonPressMask | KeyPressMask |
670 KeyReleaseMask,
671 False,
672 pointerEventEH, (XtPointer)beval);
673}
674
675 static void
676removeEventHandler(beval)
677 BalloonEval *beval;
678{
679 XtRemoveEventHandler(beval->target,
680 PointerMotionMask | EnterWindowMask |
681 LeaveWindowMask | ButtonPressMask | KeyPressMask |
682 KeyReleaseMask,
683 False,
684 pointerEventEH, (XtPointer)beval);
685}
686
687
688/*
689 * The X event handler. All it does is call the real event handler.
690 */
691/*ARGSUSED*/
692 static void
693pointerEventEH(w, client_data, event, unused)
694 Widget w;
695 XtPointer client_data;
696 XEvent *event;
697 Boolean *unused;
698{
699 BalloonEval *beval = (BalloonEval *)client_data;
700 pointerEvent(beval, event);
701}
702
703
704/*
705 * The real event handler. Called by pointerEventEH() whenever an event we are
706 * interested in ocurrs.
707 */
708
709 static void
710pointerEvent(beval, event)
711 BalloonEval *beval;
712 XEvent *event;
713{
714 Position distance; /* a measure of how much the ponter moved */
715 Position delta; /* used to compute distance */
716
717 switch (event->type)
718 {
719 case EnterNotify:
720 case MotionNotify:
721 delta = event->xmotion.x - beval->x;
722 if (delta < 0)
723 delta = -delta;
724 distance = delta;
725 delta = event->xmotion.y - beval->y;
726 if (delta < 0)
727 delta = -delta;
728 distance += delta;
729 if (distance > 4)
730 {
731 /*
732 * Moved out of the balloon location: cancel it.
733 * Remember button state
734 */
735 beval->state = event->xmotion.state;
736 if (beval->state & (Button1Mask|Button2Mask|Button3Mask))
737 {
738 /* Mouse buttons are pressed - no balloon now */
739 cancelBalloon(beval);
740 }
741 else if (beval->state & (Mod1Mask|Mod2Mask|Mod3Mask))
742 {
743 /*
744 * Alt is pressed -- enter super-evaluate-mode,
745 * where there is no time delay
746 */
747 beval->x = event->xmotion.x;
748 beval->y = event->xmotion.y;
749 beval->x_root = event->xmotion.x_root;
750 beval->y_root = event->xmotion.y_root;
751 cancelBalloon(beval);
752 if (beval->msgCB != NULL)
753 {
754 beval->showState = ShS_PENDING;
755 (*beval->msgCB)(beval, beval->state);
756 }
757 }
758 else
759 {
760 beval->x = event->xmotion.x;
761 beval->y = event->xmotion.y;
762 beval->x_root = event->xmotion.x_root;
763 beval->y_root = event->xmotion.y_root;
764 cancelBalloon(beval);
765 beval->timerID = XtAppAddTimeOut( beval->appContext,
766 (long_u)p_bdlay, timerRoutine, beval);
767 }
768 }
769 break;
770
771 /*
772 * Motif and Athena version: Keystrokes will be caught by the
773 * "textArea" widget, and handled in gui_x11_key_hit_cb().
774 */
775 case KeyPress:
776 if (beval->showState == ShS_SHOWING && beval->msgCB != NULL)
777 {
778 Modifiers modifier;
779 KeySym keysym;
780
781 XtTranslateKeycode(gui.dpy,
782 event->xkey.keycode, event->xkey.state,
783 &modifier, &keysym);
784 if (keysym == XK_Shift_L || keysym == XK_Shift_R)
785 {
786 beval->showState = ShS_UPDATE_PENDING;
787 (*beval->msgCB)(beval, ShiftMask);
788 }
789 else if (keysym == XK_Control_L || keysym == XK_Control_R)
790 {
791 beval->showState = ShS_UPDATE_PENDING;
792 (*beval->msgCB)(beval, ControlMask);
793 }
794 else
795 cancelBalloon(beval);
796 }
797 else
798 cancelBalloon(beval);
799 break;
800
801 case KeyRelease:
802 if (beval->showState == ShS_SHOWING && beval->msgCB != NULL)
803 {
804 Modifiers modifier;
805 KeySym keysym;
806
807 XtTranslateKeycode(gui.dpy, event->xkey.keycode,
808 event->xkey.state, &modifier, &keysym);
809 if ((keysym == XK_Shift_L) || (keysym == XK_Shift_R)) {
810 beval->showState = ShS_UPDATE_PENDING;
811 (*beval->msgCB)(beval, 0);
812 }
813 else if ((keysym == XK_Control_L) || (keysym == XK_Control_R))
814 {
815 beval->showState = ShS_UPDATE_PENDING;
816 (*beval->msgCB)(beval, 0);
817 }
818 else
819 cancelBalloon(beval);
820 }
821 else
822 cancelBalloon(beval);
823 break;
824
825 case LeaveNotify:
826 /* Ignore LeaveNotify events that are not "normal".
827 * Apparently we also get it when somebody else grabs focus.
828 * Happens for me every two seconds (some clipboard tool?) */
829 if (event->xcrossing.mode == NotifyNormal)
830 cancelBalloon(beval);
831 break;
832
833 case ButtonPress:
834 cancelBalloon(beval);
835 break;
836
837 default:
838 break;
839 }
840}
841
842/*ARGSUSED*/
843 static void
844timerRoutine(dx, id)
845 XtPointer dx;
846 XtIntervalId *id;
847{
848 BalloonEval *beval = (BalloonEval *)dx;
849
850 beval->timerID = (XtIntervalId)NULL;
851
852 /*
853 * If the timer event happens then the mouse has stopped long enough for
854 * a request to be started. The request will only send to the debugger if
855 * there the mouse is pointing at real data.
856 */
857 requestBalloon(beval);
858}
859
860#endif /* !FEAT_GUI_GTK */
861
862 static void
863requestBalloon(beval)
864 BalloonEval *beval;
865{
866 if (beval->showState != ShS_PENDING)
867 {
868 /* Determine the beval to display */
869 if (beval->msgCB != NULL)
870 {
871 beval->showState = ShS_PENDING;
872 (*beval->msgCB)(beval, beval->state);
873 }
874 else if (beval->msg != NULL)
875 drawBalloon(beval);
876 }
877}
878
879#ifdef FEAT_GUI_GTK
880
881# ifdef HAVE_GTK2
882/*
883 * Convert the string to UTF-8 if 'encoding' is not "utf-8".
884 * Replace any non-printable characters and invalid bytes sequences with
885 * "^X" or "<xx>" escapes, and apply SpecialKey highlighting to them.
886 * TAB and NL are passed through unscathed.
887 */
888# define IS_NONPRINTABLE(c) (((c) < 0x20 && (c) != TAB && (c) != NL) \
889 || (c) == DEL)
890 static void
891set_printable_label_text(GtkLabel *label, char_u *msg)
892{
893 char_u *convbuf = NULL;
894 char_u *buf;
895 char_u *p;
896 char_u *pdest;
897 unsigned int len;
898 int charlen;
899 int uc;
900 PangoAttrList *attr_list;
901
902 /* Convert to UTF-8 if it isn't already */
903 if (output_conv.vc_type != CONV_NONE)
904 {
905 convbuf = string_convert(&output_conv, msg, NULL);
906 if (convbuf != NULL)
907 msg = convbuf;
908 }
909
910 /* First let's see how much we need to allocate */
911 len = 0;
912 for (p = msg; *p != NUL; p += charlen)
913 {
914 if ((*p & 0x80) == 0) /* be quick for ASCII */
915 {
916 charlen = 1;
917 len += IS_NONPRINTABLE(*p) ? 2 : 1; /* nonprintable: ^X */
918 }
919 else
920 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000921 charlen = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000922 uc = utf_ptr2char(p);
923
924 if (charlen != utf_char2len(uc))
925 charlen = 1; /* reject overlong sequences */
926
927 if (charlen == 1 || uc < 0xa0) /* illegal byte or */
928 len += 4; /* control char: <xx> */
929 else if (!utf_printable(uc))
930 /* Note: we assume here that utf_printable() doesn't
931 * care about characters outside the BMP. */
932 len += 6; /* nonprintable: <xxxx> */
933 else
934 len += charlen;
935 }
936 }
937
938 attr_list = pango_attr_list_new();
939 buf = alloc(len + 1);
940
941 /* Now go for the real work */
942 if (buf != NULL)
943 {
944 attrentry_T *aep;
945 PangoAttribute *attr;
946 guicolor_T pixel;
947 GdkColor color = { 0, 0, 0, 0 };
948
949 /* Look up the RGB values of the SpecialKey foreground color. */
950 aep = syn_gui_attr2entry(hl_attr(HLF_8));
951 pixel = (aep != NULL) ? aep->ae_u.gui.fg_color : INVALCOLOR;
952 if (pixel != INVALCOLOR)
953 gdk_colormap_query_color(gtk_widget_get_colormap(gui.drawarea),
954 (unsigned long)pixel, &color);
955
956 pdest = buf;
957 p = msg;
958 while (*p != NUL)
959 {
960 /* Be quick for ASCII */
961 if ((*p & 0x80) == 0 && !IS_NONPRINTABLE(*p))
962 {
963 *pdest++ = *p++;
964 }
965 else
966 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000967 charlen = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000968 uc = utf_ptr2char(p);
969
970 if (charlen != utf_char2len(uc))
971 charlen = 1; /* reject overlong sequences */
972
973 if (charlen == 1 || uc < 0xa0 || !utf_printable(uc))
974 {
975 int outlen;
976
977 /* Careful: we can't just use transchar_byte() here,
978 * since 'encoding' is not necessarily set to "utf-8". */
979 if (*p & 0x80 && charlen == 1)
980 {
981 transchar_hex(pdest, *p); /* <xx> */
982 outlen = 4;
983 }
984 else if (uc >= 0x80)
985 {
986 /* Note: we assume here that utf_printable() doesn't
987 * care about characters outside the BMP. */
988 transchar_hex(pdest, uc); /* <xx> or <xxxx> */
989 outlen = (uc < 0x100) ? 4 : 6;
990 }
991 else
992 {
993 transchar_nonprint(pdest, *p); /* ^X */
994 outlen = 2;
995 }
996 if (pixel != INVALCOLOR)
997 {
998 attr = pango_attr_foreground_new(
999 color.red, color.green, color.blue);
1000 attr->start_index = pdest - buf;
1001 attr->end_index = pdest - buf + outlen;
1002 pango_attr_list_insert(attr_list, attr);
1003 }
1004 pdest += outlen;
1005 p += charlen;
1006 }
1007 else
1008 {
1009 do
1010 *pdest++ = *p++;
1011 while (--charlen != 0);
1012 }
1013 }
1014 }
1015 *pdest = NUL;
1016 }
1017
1018 vim_free(convbuf);
1019
1020 gtk_label_set_text(label, (const char *)buf);
1021 vim_free(buf);
1022
1023 gtk_label_set_attributes(label, attr_list);
1024 pango_attr_list_unref(attr_list);
1025}
1026# undef IS_NONPRINTABLE
1027# endif /* HAVE_GTK2 */
1028
1029/*
1030 * Draw a balloon.
1031 */
1032 static void
1033drawBalloon(BalloonEval *beval)
1034{
1035 if (beval->msg != NULL)
1036 {
1037 GtkRequisition requisition;
1038 int screen_w;
1039 int screen_h;
1040 int x;
1041 int y;
1042 int x_offset = EVAL_OFFSET_X;
1043 int y_offset = EVAL_OFFSET_Y;
1044# ifdef HAVE_GTK2
1045 PangoLayout *layout;
1046# endif
1047# ifdef HAVE_GTK_MULTIHEAD
1048 GdkScreen *screen;
1049
1050 screen = gtk_widget_get_screen(beval->target);
1051 gtk_window_set_screen(GTK_WINDOW(beval->balloonShell), screen);
1052 screen_w = gdk_screen_get_width(screen);
1053 screen_h = gdk_screen_get_height(screen);
1054# else
1055 screen_w = gdk_screen_width();
1056 screen_h = gdk_screen_height();
1057# endif
1058 gtk_widget_ensure_style(beval->balloonShell);
1059 gtk_widget_ensure_style(beval->balloonLabel);
1060
1061# ifdef HAVE_GTK2
1062 set_printable_label_text(GTK_LABEL(beval->balloonLabel), beval->msg);
1063 /*
1064 * Dirty trick: Enable wrapping mode on the label's layout behind its
1065 * back. This way GtkLabel won't try to constrain the wrap width to a
1066 * builtin maximum value of about 65 Latin characters.
1067 */
1068 layout = gtk_label_get_layout(GTK_LABEL(beval->balloonLabel));
1069# ifdef PANGO_WRAP_WORD_CHAR
1070 pango_layout_set_wrap(layout, PANGO_WRAP_WORD_CHAR);
1071# else
1072 pango_layout_set_wrap(layout, PANGO_WRAP_WORD);
1073# endif
1074 pango_layout_set_width(layout,
1075 /* try to come up with some reasonable width */
1076 PANGO_SCALE * CLAMP(gui.num_cols * gui.char_width,
1077 screen_w / 2,
1078 MAX(20, screen_w - 20)));
1079
1080 /* Calculate the balloon's width and height. */
1081 gtk_widget_size_request(beval->balloonShell, &requisition);
1082# else
1083 gtk_label_set_line_wrap(GTK_LABEL(beval->balloonLabel), FALSE);
1084 gtk_label_set_text(GTK_LABEL(beval->balloonLabel),
1085 (const char *)beval->msg);
1086
1087 /* Calculate the balloon's width and height. */
1088 gtk_widget_size_request(beval->balloonShell, &requisition);
1089 /*
1090 * Unfortunately, the dirty trick used above to get around the builtin
1091 * maximum wrap width of GtkLabel doesn't work with GTK+ 1. Thus if
1092 * and only if it's absolutely necessary to avoid drawing off-screen,
1093 * do enable wrapping now and recalculate the size request.
1094 */
1095 if (requisition.width > screen_w)
1096 {
1097 gtk_label_set_line_wrap(GTK_LABEL(beval->balloonLabel), TRUE);
1098 gtk_widget_size_request(beval->balloonShell, &requisition);
1099 }
1100# endif
1101
1102 /* Compute position of the balloon area */
1103 gdk_window_get_origin(beval->target->window, &x, &y);
1104 x += beval->x;
1105 y += beval->y;
1106
1107 /* Get out of the way of the mouse pointer */
1108 if (x + x_offset + requisition.width > screen_w)
1109 y_offset += 15;
1110 if (y + y_offset + requisition.height > screen_h)
1111 y_offset = -requisition.height - EVAL_OFFSET_Y;
1112
1113 /* Sanitize values */
1114 x = CLAMP(x + x_offset, 0, MAX(0, screen_w - requisition.width));
1115 y = CLAMP(y + y_offset, 0, MAX(0, screen_h - requisition.height));
1116
1117 /* Show the balloon */
1118 gtk_widget_set_uposition(beval->balloonShell, x, y);
1119 gtk_widget_show(beval->balloonShell);
1120
1121 beval->showState = ShS_SHOWING;
1122 }
1123}
1124
1125/*
1126 * Undraw a balloon.
1127 */
1128 static void
1129undrawBalloon(BalloonEval *beval)
1130{
1131 if (beval->balloonShell != NULL)
1132 gtk_widget_hide(beval->balloonShell);
1133 beval->showState = ShS_NEUTRAL;
1134}
1135
1136 static void
1137cancelBalloon(BalloonEval *beval)
1138{
1139 if (beval->showState == ShS_SHOWING
1140 || beval->showState == ShS_UPDATE_PENDING)
1141 undrawBalloon(beval);
1142
1143 if (beval->timerID != 0)
1144 {
1145 gtk_timeout_remove(beval->timerID);
1146 beval->timerID = 0;
1147 }
1148 beval->showState = ShS_NEUTRAL;
1149}
1150
1151 static void
1152createBalloonEvalWindow(BalloonEval *beval)
1153{
1154 beval->balloonShell = gtk_window_new(GTK_WINDOW_POPUP);
1155
1156 gtk_widget_set_app_paintable(beval->balloonShell, TRUE);
1157 gtk_window_set_policy(GTK_WINDOW(beval->balloonShell), FALSE, FALSE, TRUE);
1158 gtk_widget_set_name(beval->balloonShell, "gtk-tooltips");
1159 gtk_container_border_width(GTK_CONTAINER(beval->balloonShell), 4);
1160
1161 gtk_signal_connect((GtkObject*)(beval->balloonShell), "expose_event",
1162 GTK_SIGNAL_FUNC(balloon_expose_event_cb), NULL);
1163# ifndef HAVE_GTK2
1164 gtk_signal_connect((GtkObject*)(beval->balloonShell), "draw",
1165 GTK_SIGNAL_FUNC(balloon_draw_cb), NULL);
1166# endif
1167 beval->balloonLabel = gtk_label_new(NULL);
1168
1169 gtk_label_set_line_wrap(GTK_LABEL(beval->balloonLabel), FALSE);
1170 gtk_label_set_justify(GTK_LABEL(beval->balloonLabel), GTK_JUSTIFY_LEFT);
1171 gtk_misc_set_alignment(GTK_MISC(beval->balloonLabel), 0.5f, 0.5f);
1172 gtk_widget_set_name(beval->balloonLabel, "vim-balloon-label");
1173 gtk_widget_show(beval->balloonLabel);
1174
1175 gtk_container_add(GTK_CONTAINER(beval->balloonShell), beval->balloonLabel);
1176}
1177
1178#else /* !FEAT_GUI_GTK */
1179
1180/*
1181 * Draw a balloon.
1182 */
1183 static void
1184drawBalloon(beval)
1185 BalloonEval *beval;
1186{
1187 Dimension w;
1188 Dimension h;
1189 Position tx;
1190 Position ty;
1191
1192 if (beval->msg != NULL)
1193 {
1194 /* Show the Balloon */
1195
1196 /* Calculate the label's width and height */
1197#ifdef FEAT_GUI_MOTIF
1198 XmString s;
1199
1200 /* For the callback function we parse NL characters to create a
1201 * multi-line label. This doesn't work for all languages, but
1202 * XmStringCreateLocalized() doesn't do multi-line labels... */
1203 if (beval->msgCB != NULL)
1204 s = XmStringCreateLtoR((char *)beval->msg, XmFONTLIST_DEFAULT_TAG);
1205 else
1206 s = XmStringCreateLocalized((char *)beval->msg);
1207 {
1208 XmFontList fl;
1209
1210 fl = gui_motif_fontset2fontlist(&gui.tooltip_fontset);
1211 if (fl != NULL)
1212 {
1213 XmStringExtent(fl, s, &w, &h);
1214 XmFontListFree(fl);
1215 }
1216 }
1217 w += gui.border_offset << 1;
1218 h += gui.border_offset << 1;
1219 XtVaSetValues(beval->balloonLabel, XmNlabelString, s, NULL);
1220 XmStringFree(s);
1221#else /* Athena */
1222 /* Assume XtNinternational == True */
1223 XFontSet fset;
1224 XFontSetExtents *ext;
1225
1226 XtVaGetValues(beval->balloonLabel, XtNfontSet, &fset, NULL);
1227 ext = XExtentsOfFontSet(fset);
1228 h = ext->max_ink_extent.height;
1229 w = XmbTextEscapement(fset,
1230 (char *)beval->msg,
1231 (int)STRLEN(beval->msg));
1232 w += gui.border_offset << 1;
1233 h += gui.border_offset << 1;
1234 XtVaSetValues(beval->balloonLabel, XtNlabel, beval->msg, NULL);
1235#endif
1236
1237 /* Compute position of the balloon area */
1238 tx = beval->x_root + EVAL_OFFSET_X;
1239 ty = beval->y_root + EVAL_OFFSET_Y;
1240 if ((tx + w) > beval->screen_width)
1241 tx = beval->screen_width - w;
1242 if ((ty + h) > beval->screen_height)
1243 ty = beval->screen_height - h;
1244#ifdef FEAT_GUI_MOTIF
1245 XtVaSetValues(beval->balloonShell,
1246 XmNx, tx,
1247 XmNy, ty,
1248 NULL);
1249#else
1250 /* Athena */
1251 XtVaSetValues(beval->balloonShell,
1252 XtNx, tx,
1253 XtNy, ty,
1254 NULL);
1255#endif
1256
1257 XtPopup(beval->balloonShell, XtGrabNone);
1258
1259 beval->showState = ShS_SHOWING;
1260
1261 current_beval = beval;
1262 }
1263}
1264
1265/*
1266 * Undraw a balloon.
1267 */
1268 static void
1269undrawBalloon(beval)
1270 BalloonEval *beval;
1271{
1272 if (beval->balloonShell != (Widget)0)
1273 XtPopdown(beval->balloonShell);
1274 beval->showState = ShS_NEUTRAL;
1275
1276 current_beval = NULL;
1277}
1278
1279 static void
1280cancelBalloon(beval)
1281 BalloonEval *beval;
1282{
1283 if (beval->showState == ShS_SHOWING
1284 || beval->showState == ShS_UPDATE_PENDING)
1285 undrawBalloon(beval);
1286
1287 if (beval->timerID != (XtIntervalId)NULL)
1288 {
1289 XtRemoveTimeOut(beval->timerID);
1290 beval->timerID = (XtIntervalId)NULL;
1291 }
1292 beval->showState = ShS_NEUTRAL;
1293}
1294
1295
1296 static void
1297createBalloonEvalWindow(beval)
1298 BalloonEval *beval;
1299{
1300 Arg args[12];
1301 int n;
1302
1303 n = 0;
1304#ifdef FEAT_GUI_MOTIF
1305 XtSetArg(args[n], XmNallowShellResize, True); n++;
1306 beval->balloonShell = XtAppCreateShell("balloonEval", "BalloonEval",
1307 overrideShellWidgetClass, gui.dpy, args, n);
1308#else
1309 /* Athena */
1310 XtSetArg(args[n], XtNallowShellResize, True); n++;
1311 beval->balloonShell = XtAppCreateShell("balloonEval", "BalloonEval",
1312 overrideShellWidgetClass, gui.dpy, args, n);
1313#endif
1314
1315 n = 0;
1316#ifdef FEAT_GUI_MOTIF
1317 {
1318 XmFontList fl;
1319
1320 fl = gui_motif_fontset2fontlist(&gui.tooltip_fontset);
1321 XtSetArg(args[n], XmNforeground, gui.tooltip_fg_pixel); n++;
1322 XtSetArg(args[n], XmNbackground, gui.tooltip_bg_pixel); n++;
1323 XtSetArg(args[n], XmNfontList, fl); n++;
1324 XtSetArg(args[n], XmNalignment, XmALIGNMENT_BEGINNING); n++;
1325 beval->balloonLabel = XtCreateManagedWidget("balloonLabel",
1326 xmLabelWidgetClass, beval->balloonShell, args, n);
1327 }
1328#else /* FEAT_GUI_ATHENA */
1329 XtSetArg(args[n], XtNforeground, gui.tooltip_fg_pixel); n++;
1330 XtSetArg(args[n], XtNbackground, gui.tooltip_bg_pixel); n++;
1331 XtSetArg(args[n], XtNinternational, True); n++;
1332 XtSetArg(args[n], XtNfontSet, gui.tooltip_fontset); n++;
1333 beval->balloonLabel = XtCreateManagedWidget("balloonLabel",
1334 labelWidgetClass, beval->balloonShell, args, n);
1335#endif
1336}
1337
1338#endif /* !FEAT_GUI_GTK */
1339#endif /* !FEAT_GUI_W32 */
1340
1341#endif /* FEAT_BEVAL */