blob: bb26497fc6c16e85a35fc73359b834254062938f [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:
612 cancelBalloon(beval);
613 break;
614 }
615 }
616 else
617 cancelBalloon(beval);
618}
619
620 static gint
621timeout_cb(gpointer data)
622{
623 BalloonEval *beval = (BalloonEval *)data;
624
625 beval->timerID = 0;
626 /*
627 * If the timer event happens then the mouse has stopped long enough for
628 * a request to be started. The request will only send to the debugger if
629 * there the mouse is pointing at real data.
630 */
631 requestBalloon(beval);
632
633 return FALSE; /* don't call me again */
634}
635
636/*ARGSUSED2*/
637 static gint
638balloon_expose_event_cb(GtkWidget *widget, GdkEventExpose *event, gpointer data)
639{
640 gtk_paint_flat_box(widget->style, widget->window,
641 GTK_STATE_NORMAL, GTK_SHADOW_OUT,
642 &event->area, widget, "tooltip",
643 0, 0, -1, -1);
644
645 return FALSE; /* continue emission */
646}
647
648# ifndef HAVE_GTK2
649/*ARGSUSED2*/
650 static void
651balloon_draw_cb(GtkWidget *widget, GdkRectangle *area, gpointer data)
652{
653 GtkWidget *child;
654 GdkRectangle child_area;
655
656 gtk_paint_flat_box(widget->style, widget->window,
657 GTK_STATE_NORMAL, GTK_SHADOW_OUT,
658 area, widget, "tooltip",
659 0, 0, -1, -1);
660
661 child = GTK_BIN(widget)->child;
662
663 if (gtk_widget_intersect(child, area, &child_area))
664 gtk_widget_draw(child, &child_area);
665}
666# endif
667
668#else /* !FEAT_GUI_GTK */
669
670 static void
671addEventHandler(target, beval)
672 Widget target;
673 BalloonEval *beval;
674{
675 XtAddEventHandler(target,
676 PointerMotionMask | EnterWindowMask |
677 LeaveWindowMask | ButtonPressMask | KeyPressMask |
678 KeyReleaseMask,
679 False,
680 pointerEventEH, (XtPointer)beval);
681}
682
683 static void
684removeEventHandler(beval)
685 BalloonEval *beval;
686{
687 XtRemoveEventHandler(beval->target,
688 PointerMotionMask | EnterWindowMask |
689 LeaveWindowMask | ButtonPressMask | KeyPressMask |
690 KeyReleaseMask,
691 False,
692 pointerEventEH, (XtPointer)beval);
693}
694
695
696/*
697 * The X event handler. All it does is call the real event handler.
698 */
699/*ARGSUSED*/
700 static void
701pointerEventEH(w, client_data, event, unused)
702 Widget w;
703 XtPointer client_data;
704 XEvent *event;
705 Boolean *unused;
706{
707 BalloonEval *beval = (BalloonEval *)client_data;
708 pointerEvent(beval, event);
709}
710
711
712/*
713 * The real event handler. Called by pointerEventEH() whenever an event we are
714 * interested in ocurrs.
715 */
716
717 static void
718pointerEvent(beval, event)
719 BalloonEval *beval;
720 XEvent *event;
721{
722 Position distance; /* a measure of how much the ponter moved */
723 Position delta; /* used to compute distance */
724
725 switch (event->type)
726 {
727 case EnterNotify:
728 case MotionNotify:
729 delta = event->xmotion.x - beval->x;
730 if (delta < 0)
731 delta = -delta;
732 distance = delta;
733 delta = event->xmotion.y - beval->y;
734 if (delta < 0)
735 delta = -delta;
736 distance += delta;
737 if (distance > 4)
738 {
739 /*
740 * Moved out of the balloon location: cancel it.
741 * Remember button state
742 */
743 beval->state = event->xmotion.state;
744 if (beval->state & (Button1Mask|Button2Mask|Button3Mask))
745 {
746 /* Mouse buttons are pressed - no balloon now */
747 cancelBalloon(beval);
748 }
749 else if (beval->state & (Mod1Mask|Mod2Mask|Mod3Mask))
750 {
751 /*
752 * Alt is pressed -- enter super-evaluate-mode,
753 * where there is no time delay
754 */
755 beval->x = event->xmotion.x;
756 beval->y = event->xmotion.y;
757 beval->x_root = event->xmotion.x_root;
758 beval->y_root = event->xmotion.y_root;
759 cancelBalloon(beval);
760 if (beval->msgCB != NULL)
761 {
762 beval->showState = ShS_PENDING;
763 (*beval->msgCB)(beval, beval->state);
764 }
765 }
766 else
767 {
768 beval->x = event->xmotion.x;
769 beval->y = event->xmotion.y;
770 beval->x_root = event->xmotion.x_root;
771 beval->y_root = event->xmotion.y_root;
772 cancelBalloon(beval);
773 beval->timerID = XtAppAddTimeOut( beval->appContext,
774 (long_u)p_bdlay, timerRoutine, beval);
775 }
776 }
777 break;
778
779 /*
780 * Motif and Athena version: Keystrokes will be caught by the
781 * "textArea" widget, and handled in gui_x11_key_hit_cb().
782 */
783 case KeyPress:
784 if (beval->showState == ShS_SHOWING && beval->msgCB != NULL)
785 {
786 Modifiers modifier;
787 KeySym keysym;
788
789 XtTranslateKeycode(gui.dpy,
790 event->xkey.keycode, event->xkey.state,
791 &modifier, &keysym);
792 if (keysym == XK_Shift_L || keysym == XK_Shift_R)
793 {
794 beval->showState = ShS_UPDATE_PENDING;
795 (*beval->msgCB)(beval, ShiftMask);
796 }
797 else if (keysym == XK_Control_L || keysym == XK_Control_R)
798 {
799 beval->showState = ShS_UPDATE_PENDING;
800 (*beval->msgCB)(beval, ControlMask);
801 }
802 else
803 cancelBalloon(beval);
804 }
805 else
806 cancelBalloon(beval);
807 break;
808
809 case KeyRelease:
810 if (beval->showState == ShS_SHOWING && beval->msgCB != NULL)
811 {
812 Modifiers modifier;
813 KeySym keysym;
814
815 XtTranslateKeycode(gui.dpy, event->xkey.keycode,
816 event->xkey.state, &modifier, &keysym);
817 if ((keysym == XK_Shift_L) || (keysym == XK_Shift_R)) {
818 beval->showState = ShS_UPDATE_PENDING;
819 (*beval->msgCB)(beval, 0);
820 }
821 else if ((keysym == XK_Control_L) || (keysym == XK_Control_R))
822 {
823 beval->showState = ShS_UPDATE_PENDING;
824 (*beval->msgCB)(beval, 0);
825 }
826 else
827 cancelBalloon(beval);
828 }
829 else
830 cancelBalloon(beval);
831 break;
832
833 case LeaveNotify:
834 /* Ignore LeaveNotify events that are not "normal".
835 * Apparently we also get it when somebody else grabs focus.
836 * Happens for me every two seconds (some clipboard tool?) */
837 if (event->xcrossing.mode == NotifyNormal)
838 cancelBalloon(beval);
839 break;
840
841 case ButtonPress:
842 cancelBalloon(beval);
843 break;
844
845 default:
846 break;
847 }
848}
849
850/*ARGSUSED*/
851 static void
852timerRoutine(dx, id)
853 XtPointer dx;
854 XtIntervalId *id;
855{
856 BalloonEval *beval = (BalloonEval *)dx;
857
858 beval->timerID = (XtIntervalId)NULL;
859
860 /*
861 * If the timer event happens then the mouse has stopped long enough for
862 * a request to be started. The request will only send to the debugger if
863 * there the mouse is pointing at real data.
864 */
865 requestBalloon(beval);
866}
867
868#endif /* !FEAT_GUI_GTK */
869
870 static void
871requestBalloon(beval)
872 BalloonEval *beval;
873{
874 if (beval->showState != ShS_PENDING)
875 {
876 /* Determine the beval to display */
877 if (beval->msgCB != NULL)
878 {
879 beval->showState = ShS_PENDING;
880 (*beval->msgCB)(beval, beval->state);
881 }
882 else if (beval->msg != NULL)
883 drawBalloon(beval);
884 }
885}
886
887#ifdef FEAT_GUI_GTK
888
889# ifdef HAVE_GTK2
890/*
891 * Convert the string to UTF-8 if 'encoding' is not "utf-8".
892 * Replace any non-printable characters and invalid bytes sequences with
893 * "^X" or "<xx>" escapes, and apply SpecialKey highlighting to them.
894 * TAB and NL are passed through unscathed.
895 */
896# define IS_NONPRINTABLE(c) (((c) < 0x20 && (c) != TAB && (c) != NL) \
897 || (c) == DEL)
898 static void
899set_printable_label_text(GtkLabel *label, char_u *msg)
900{
901 char_u *convbuf = NULL;
902 char_u *buf;
903 char_u *p;
904 char_u *pdest;
905 unsigned int len;
906 int charlen;
907 int uc;
908 PangoAttrList *attr_list;
909
910 /* Convert to UTF-8 if it isn't already */
911 if (output_conv.vc_type != CONV_NONE)
912 {
913 convbuf = string_convert(&output_conv, msg, NULL);
914 if (convbuf != NULL)
915 msg = convbuf;
916 }
917
918 /* First let's see how much we need to allocate */
919 len = 0;
920 for (p = msg; *p != NUL; p += charlen)
921 {
922 if ((*p & 0x80) == 0) /* be quick for ASCII */
923 {
924 charlen = 1;
925 len += IS_NONPRINTABLE(*p) ? 2 : 1; /* nonprintable: ^X */
926 }
927 else
928 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000929 charlen = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000930 uc = utf_ptr2char(p);
931
932 if (charlen != utf_char2len(uc))
933 charlen = 1; /* reject overlong sequences */
934
935 if (charlen == 1 || uc < 0xa0) /* illegal byte or */
936 len += 4; /* control char: <xx> */
937 else if (!utf_printable(uc))
938 /* Note: we assume here that utf_printable() doesn't
939 * care about characters outside the BMP. */
940 len += 6; /* nonprintable: <xxxx> */
941 else
942 len += charlen;
943 }
944 }
945
946 attr_list = pango_attr_list_new();
947 buf = alloc(len + 1);
948
949 /* Now go for the real work */
950 if (buf != NULL)
951 {
952 attrentry_T *aep;
953 PangoAttribute *attr;
954 guicolor_T pixel;
955 GdkColor color = { 0, 0, 0, 0 };
956
957 /* Look up the RGB values of the SpecialKey foreground color. */
958 aep = syn_gui_attr2entry(hl_attr(HLF_8));
959 pixel = (aep != NULL) ? aep->ae_u.gui.fg_color : INVALCOLOR;
960 if (pixel != INVALCOLOR)
961 gdk_colormap_query_color(gtk_widget_get_colormap(gui.drawarea),
962 (unsigned long)pixel, &color);
963
964 pdest = buf;
965 p = msg;
966 while (*p != NUL)
967 {
968 /* Be quick for ASCII */
969 if ((*p & 0x80) == 0 && !IS_NONPRINTABLE(*p))
970 {
971 *pdest++ = *p++;
972 }
973 else
974 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000975 charlen = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000976 uc = utf_ptr2char(p);
977
978 if (charlen != utf_char2len(uc))
979 charlen = 1; /* reject overlong sequences */
980
981 if (charlen == 1 || uc < 0xa0 || !utf_printable(uc))
982 {
983 int outlen;
984
985 /* Careful: we can't just use transchar_byte() here,
986 * since 'encoding' is not necessarily set to "utf-8". */
987 if (*p & 0x80 && charlen == 1)
988 {
989 transchar_hex(pdest, *p); /* <xx> */
990 outlen = 4;
991 }
992 else if (uc >= 0x80)
993 {
994 /* Note: we assume here that utf_printable() doesn't
995 * care about characters outside the BMP. */
996 transchar_hex(pdest, uc); /* <xx> or <xxxx> */
997 outlen = (uc < 0x100) ? 4 : 6;
998 }
999 else
1000 {
1001 transchar_nonprint(pdest, *p); /* ^X */
1002 outlen = 2;
1003 }
1004 if (pixel != INVALCOLOR)
1005 {
1006 attr = pango_attr_foreground_new(
1007 color.red, color.green, color.blue);
1008 attr->start_index = pdest - buf;
1009 attr->end_index = pdest - buf + outlen;
1010 pango_attr_list_insert(attr_list, attr);
1011 }
1012 pdest += outlen;
1013 p += charlen;
1014 }
1015 else
1016 {
1017 do
1018 *pdest++ = *p++;
1019 while (--charlen != 0);
1020 }
1021 }
1022 }
1023 *pdest = NUL;
1024 }
1025
1026 vim_free(convbuf);
1027
1028 gtk_label_set_text(label, (const char *)buf);
1029 vim_free(buf);
1030
1031 gtk_label_set_attributes(label, attr_list);
1032 pango_attr_list_unref(attr_list);
1033}
1034# undef IS_NONPRINTABLE
1035# endif /* HAVE_GTK2 */
1036
1037/*
1038 * Draw a balloon.
1039 */
1040 static void
1041drawBalloon(BalloonEval *beval)
1042{
1043 if (beval->msg != NULL)
1044 {
1045 GtkRequisition requisition;
1046 int screen_w;
1047 int screen_h;
1048 int x;
1049 int y;
1050 int x_offset = EVAL_OFFSET_X;
1051 int y_offset = EVAL_OFFSET_Y;
1052# ifdef HAVE_GTK2
1053 PangoLayout *layout;
1054# endif
1055# ifdef HAVE_GTK_MULTIHEAD
1056 GdkScreen *screen;
1057
1058 screen = gtk_widget_get_screen(beval->target);
1059 gtk_window_set_screen(GTK_WINDOW(beval->balloonShell), screen);
1060 screen_w = gdk_screen_get_width(screen);
1061 screen_h = gdk_screen_get_height(screen);
1062# else
1063 screen_w = gdk_screen_width();
1064 screen_h = gdk_screen_height();
1065# endif
1066 gtk_widget_ensure_style(beval->balloonShell);
1067 gtk_widget_ensure_style(beval->balloonLabel);
1068
1069# ifdef HAVE_GTK2
1070 set_printable_label_text(GTK_LABEL(beval->balloonLabel), beval->msg);
1071 /*
1072 * Dirty trick: Enable wrapping mode on the label's layout behind its
1073 * back. This way GtkLabel won't try to constrain the wrap width to a
1074 * builtin maximum value of about 65 Latin characters.
1075 */
1076 layout = gtk_label_get_layout(GTK_LABEL(beval->balloonLabel));
1077# ifdef PANGO_WRAP_WORD_CHAR
1078 pango_layout_set_wrap(layout, PANGO_WRAP_WORD_CHAR);
1079# else
1080 pango_layout_set_wrap(layout, PANGO_WRAP_WORD);
1081# endif
1082 pango_layout_set_width(layout,
1083 /* try to come up with some reasonable width */
1084 PANGO_SCALE * CLAMP(gui.num_cols * gui.char_width,
1085 screen_w / 2,
1086 MAX(20, screen_w - 20)));
1087
1088 /* Calculate the balloon's width and height. */
1089 gtk_widget_size_request(beval->balloonShell, &requisition);
1090# else
1091 gtk_label_set_line_wrap(GTK_LABEL(beval->balloonLabel), FALSE);
1092 gtk_label_set_text(GTK_LABEL(beval->balloonLabel),
1093 (const char *)beval->msg);
1094
1095 /* Calculate the balloon's width and height. */
1096 gtk_widget_size_request(beval->balloonShell, &requisition);
1097 /*
1098 * Unfortunately, the dirty trick used above to get around the builtin
1099 * maximum wrap width of GtkLabel doesn't work with GTK+ 1. Thus if
1100 * and only if it's absolutely necessary to avoid drawing off-screen,
1101 * do enable wrapping now and recalculate the size request.
1102 */
1103 if (requisition.width > screen_w)
1104 {
1105 gtk_label_set_line_wrap(GTK_LABEL(beval->balloonLabel), TRUE);
1106 gtk_widget_size_request(beval->balloonShell, &requisition);
1107 }
1108# endif
1109
1110 /* Compute position of the balloon area */
1111 gdk_window_get_origin(beval->target->window, &x, &y);
1112 x += beval->x;
1113 y += beval->y;
1114
1115 /* Get out of the way of the mouse pointer */
1116 if (x + x_offset + requisition.width > screen_w)
1117 y_offset += 15;
1118 if (y + y_offset + requisition.height > screen_h)
1119 y_offset = -requisition.height - EVAL_OFFSET_Y;
1120
1121 /* Sanitize values */
1122 x = CLAMP(x + x_offset, 0, MAX(0, screen_w - requisition.width));
1123 y = CLAMP(y + y_offset, 0, MAX(0, screen_h - requisition.height));
1124
1125 /* Show the balloon */
1126 gtk_widget_set_uposition(beval->balloonShell, x, y);
1127 gtk_widget_show(beval->balloonShell);
1128
1129 beval->showState = ShS_SHOWING;
1130 }
1131}
1132
1133/*
1134 * Undraw a balloon.
1135 */
1136 static void
1137undrawBalloon(BalloonEval *beval)
1138{
1139 if (beval->balloonShell != NULL)
1140 gtk_widget_hide(beval->balloonShell);
1141 beval->showState = ShS_NEUTRAL;
1142}
1143
1144 static void
1145cancelBalloon(BalloonEval *beval)
1146{
1147 if (beval->showState == ShS_SHOWING
1148 || beval->showState == ShS_UPDATE_PENDING)
1149 undrawBalloon(beval);
1150
1151 if (beval->timerID != 0)
1152 {
1153 gtk_timeout_remove(beval->timerID);
1154 beval->timerID = 0;
1155 }
1156 beval->showState = ShS_NEUTRAL;
1157}
1158
1159 static void
1160createBalloonEvalWindow(BalloonEval *beval)
1161{
1162 beval->balloonShell = gtk_window_new(GTK_WINDOW_POPUP);
1163
1164 gtk_widget_set_app_paintable(beval->balloonShell, TRUE);
1165 gtk_window_set_policy(GTK_WINDOW(beval->balloonShell), FALSE, FALSE, TRUE);
1166 gtk_widget_set_name(beval->balloonShell, "gtk-tooltips");
1167 gtk_container_border_width(GTK_CONTAINER(beval->balloonShell), 4);
1168
1169 gtk_signal_connect((GtkObject*)(beval->balloonShell), "expose_event",
1170 GTK_SIGNAL_FUNC(balloon_expose_event_cb), NULL);
1171# ifndef HAVE_GTK2
1172 gtk_signal_connect((GtkObject*)(beval->balloonShell), "draw",
1173 GTK_SIGNAL_FUNC(balloon_draw_cb), NULL);
1174# endif
1175 beval->balloonLabel = gtk_label_new(NULL);
1176
1177 gtk_label_set_line_wrap(GTK_LABEL(beval->balloonLabel), FALSE);
1178 gtk_label_set_justify(GTK_LABEL(beval->balloonLabel), GTK_JUSTIFY_LEFT);
1179 gtk_misc_set_alignment(GTK_MISC(beval->balloonLabel), 0.5f, 0.5f);
1180 gtk_widget_set_name(beval->balloonLabel, "vim-balloon-label");
1181 gtk_widget_show(beval->balloonLabel);
1182
1183 gtk_container_add(GTK_CONTAINER(beval->balloonShell), beval->balloonLabel);
1184}
1185
1186#else /* !FEAT_GUI_GTK */
1187
1188/*
1189 * Draw a balloon.
1190 */
1191 static void
1192drawBalloon(beval)
1193 BalloonEval *beval;
1194{
1195 Dimension w;
1196 Dimension h;
1197 Position tx;
1198 Position ty;
1199
1200 if (beval->msg != NULL)
1201 {
1202 /* Show the Balloon */
1203
1204 /* Calculate the label's width and height */
1205#ifdef FEAT_GUI_MOTIF
1206 XmString s;
1207
1208 /* For the callback function we parse NL characters to create a
1209 * multi-line label. This doesn't work for all languages, but
1210 * XmStringCreateLocalized() doesn't do multi-line labels... */
1211 if (beval->msgCB != NULL)
1212 s = XmStringCreateLtoR((char *)beval->msg, XmFONTLIST_DEFAULT_TAG);
1213 else
1214 s = XmStringCreateLocalized((char *)beval->msg);
1215 {
1216 XmFontList fl;
1217
1218 fl = gui_motif_fontset2fontlist(&gui.tooltip_fontset);
1219 if (fl != NULL)
1220 {
1221 XmStringExtent(fl, s, &w, &h);
1222 XmFontListFree(fl);
1223 }
1224 }
1225 w += gui.border_offset << 1;
1226 h += gui.border_offset << 1;
1227 XtVaSetValues(beval->balloonLabel, XmNlabelString, s, NULL);
1228 XmStringFree(s);
1229#else /* Athena */
1230 /* Assume XtNinternational == True */
1231 XFontSet fset;
1232 XFontSetExtents *ext;
1233
1234 XtVaGetValues(beval->balloonLabel, XtNfontSet, &fset, NULL);
1235 ext = XExtentsOfFontSet(fset);
1236 h = ext->max_ink_extent.height;
1237 w = XmbTextEscapement(fset,
1238 (char *)beval->msg,
1239 (int)STRLEN(beval->msg));
1240 w += gui.border_offset << 1;
1241 h += gui.border_offset << 1;
1242 XtVaSetValues(beval->balloonLabel, XtNlabel, beval->msg, NULL);
1243#endif
1244
1245 /* Compute position of the balloon area */
1246 tx = beval->x_root + EVAL_OFFSET_X;
1247 ty = beval->y_root + EVAL_OFFSET_Y;
1248 if ((tx + w) > beval->screen_width)
1249 tx = beval->screen_width - w;
1250 if ((ty + h) > beval->screen_height)
1251 ty = beval->screen_height - h;
1252#ifdef FEAT_GUI_MOTIF
1253 XtVaSetValues(beval->balloonShell,
1254 XmNx, tx,
1255 XmNy, ty,
1256 NULL);
1257#else
1258 /* Athena */
1259 XtVaSetValues(beval->balloonShell,
1260 XtNx, tx,
1261 XtNy, ty,
1262 NULL);
1263#endif
1264
1265 XtPopup(beval->balloonShell, XtGrabNone);
1266
1267 beval->showState = ShS_SHOWING;
1268
1269 current_beval = beval;
1270 }
1271}
1272
1273/*
1274 * Undraw a balloon.
1275 */
1276 static void
1277undrawBalloon(beval)
1278 BalloonEval *beval;
1279{
1280 if (beval->balloonShell != (Widget)0)
1281 XtPopdown(beval->balloonShell);
1282 beval->showState = ShS_NEUTRAL;
1283
1284 current_beval = NULL;
1285}
1286
1287 static void
1288cancelBalloon(beval)
1289 BalloonEval *beval;
1290{
1291 if (beval->showState == ShS_SHOWING
1292 || beval->showState == ShS_UPDATE_PENDING)
1293 undrawBalloon(beval);
1294
1295 if (beval->timerID != (XtIntervalId)NULL)
1296 {
1297 XtRemoveTimeOut(beval->timerID);
1298 beval->timerID = (XtIntervalId)NULL;
1299 }
1300 beval->showState = ShS_NEUTRAL;
1301}
1302
1303
1304 static void
1305createBalloonEvalWindow(beval)
1306 BalloonEval *beval;
1307{
1308 Arg args[12];
1309 int n;
1310
1311 n = 0;
1312#ifdef FEAT_GUI_MOTIF
1313 XtSetArg(args[n], XmNallowShellResize, True); n++;
1314 beval->balloonShell = XtAppCreateShell("balloonEval", "BalloonEval",
1315 overrideShellWidgetClass, gui.dpy, args, n);
1316#else
1317 /* Athena */
1318 XtSetArg(args[n], XtNallowShellResize, True); n++;
1319 beval->balloonShell = XtAppCreateShell("balloonEval", "BalloonEval",
1320 overrideShellWidgetClass, gui.dpy, args, n);
1321#endif
1322
1323 n = 0;
1324#ifdef FEAT_GUI_MOTIF
1325 {
1326 XmFontList fl;
1327
1328 fl = gui_motif_fontset2fontlist(&gui.tooltip_fontset);
1329 XtSetArg(args[n], XmNforeground, gui.tooltip_fg_pixel); n++;
1330 XtSetArg(args[n], XmNbackground, gui.tooltip_bg_pixel); n++;
1331 XtSetArg(args[n], XmNfontList, fl); n++;
1332 XtSetArg(args[n], XmNalignment, XmALIGNMENT_BEGINNING); n++;
1333 beval->balloonLabel = XtCreateManagedWidget("balloonLabel",
1334 xmLabelWidgetClass, beval->balloonShell, args, n);
1335 }
1336#else /* FEAT_GUI_ATHENA */
1337 XtSetArg(args[n], XtNforeground, gui.tooltip_fg_pixel); n++;
1338 XtSetArg(args[n], XtNbackground, gui.tooltip_bg_pixel); n++;
1339 XtSetArg(args[n], XtNinternational, True); n++;
1340 XtSetArg(args[n], XtNfontSet, gui.tooltip_fontset); n++;
1341 beval->balloonLabel = XtCreateManagedWidget("balloonLabel",
1342 labelWidgetClass, beval->balloonShell, args, n);
1343#endif
1344}
1345
1346#endif /* !FEAT_GUI_GTK */
1347#endif /* !FEAT_GUI_W32 */
1348
1349#endif /* FEAT_BEVAL */