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