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