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