blob: 9af17a9287e9f233fb0c5f64b5e7e17e6d6440a6 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
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
Bram Moolenaarc3719bd2017-11-18 22:13:31 +010013#if defined(FEAT_BEVAL_GUI) || defined(PROTO)
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000014
15/* on Win32 only get_beval_info() is required */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016#if !defined(FEAT_GUI_W32) || defined(PROTO)
17
18#ifdef FEAT_GUI_GTK
Bram Moolenaar98921892016-02-23 17:14:37 +010019# if GTK_CHECK_VERSION(3,0,0)
20# include <gdk/gdkkeysyms-compat.h>
21# else
22# include <gdk/gdkkeysyms.h>
23# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024# include <gtk/gtk.h>
25#else
26# include <X11/keysym.h>
27# ifdef FEAT_GUI_MOTIF
28# include <Xm/PushB.h>
29# include <Xm/Separator.h>
30# include <Xm/List.h>
31# include <Xm/Label.h>
32# include <Xm/AtomMgr.h>
33# include <Xm/Protocols.h>
34# else
35 /* Assume Athena */
36# include <X11/Shell.h>
Bram Moolenaar238a5642006-02-21 22:12:05 +000037# ifdef FEAT_GUI_NEXTAW
38# include <X11/neXtaw/Label.h>
39# else
40# include <X11/Xaw/Label.h>
41# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000042# endif
43#endif
44
Bram Moolenaar071d4272004-06-13 20:20:40 +000045#ifndef FEAT_GUI_GTK
46extern Widget vimShell;
47
48/*
49 * Currently, we assume that there can be only one BalloonEval showing
50 * on-screen at any given moment. This variable will hold the currently
51 * showing BalloonEval or NULL if none is showing.
52 */
53static BalloonEval *current_beval = NULL;
54#endif
55
56#ifdef FEAT_GUI_GTK
Bram Moolenaard25c16e2016-01-29 22:13:30 +010057static void addEventHandler(GtkWidget *, BalloonEval *);
58static void removeEventHandler(BalloonEval *);
59static gint target_event_cb(GtkWidget *, GdkEvent *, gpointer);
60static gint mainwin_event_cb(GtkWidget *, GdkEvent *, gpointer);
61static void pointer_event(BalloonEval *, int, int, unsigned);
62static void key_event(BalloonEval *, unsigned, int);
Bram Moolenaar98921892016-02-23 17:14:37 +010063static gboolean timeout_cb(gpointer);
Bram Moolenaar98921892016-02-23 17:14:37 +010064# if GTK_CHECK_VERSION(3,0,0)
65static gboolean balloon_draw_event_cb (GtkWidget *, cairo_t *, gpointer);
66# else
67static gint balloon_expose_event_cb (GtkWidget *, GdkEventExpose *, gpointer);
68# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000069#else
Bram Moolenaard25c16e2016-01-29 22:13:30 +010070static void addEventHandler(Widget, BalloonEval *);
71static void removeEventHandler(BalloonEval *);
72static void pointerEventEH(Widget, XtPointer, XEvent *, Boolean *);
73static void pointerEvent(BalloonEval *, XEvent *);
74static void timerRoutine(XtPointer, XtIntervalId *);
Bram Moolenaar071d4272004-06-13 20:20:40 +000075#endif
Bram Moolenaard25c16e2016-01-29 22:13:30 +010076static void cancelBalloon(BalloonEval *);
77static void requestBalloon(BalloonEval *);
78static void drawBalloon(BalloonEval *);
79static void undrawBalloon(BalloonEval *beval);
80static void createBalloonEvalWindow(BalloonEval *);
Bram Moolenaar071d4272004-06-13 20:20:40 +000081
Bram Moolenaar071d4272004-06-13 20:20:40 +000082/*
83 * Create a balloon-evaluation area for a Widget.
84 * There can be either a "mesg" for a fixed string or "mesgCB" to generate a
85 * message by calling this callback function.
86 * When "mesg" is not NULL it must remain valid for as long as the balloon is
87 * used. It is not freed here.
88 * Returns a pointer to the resulting object (NULL when out of memory).
89 */
90 BalloonEval *
Bram Moolenaar66f948e2016-01-30 16:39:25 +010091gui_mch_create_beval_area(
92 void *target,
93 char_u *mesg,
94 void (*mesgCB)(BalloonEval *, int),
95 void *clientData)
Bram Moolenaar071d4272004-06-13 20:20:40 +000096{
97#ifndef FEAT_GUI_GTK
98 char *display_name; /* get from gui.dpy */
99 int screen_num;
100 char *p;
101#endif
102 BalloonEval *beval;
103
104 if (mesg != NULL && mesgCB != NULL)
105 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100106 iemsg(_("E232: Cannot create BalloonEval with both message and callback"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000107 return NULL;
108 }
109
Bram Moolenaarca4b6132018-06-28 12:05:11 +0200110 beval = (BalloonEval *)alloc_clear(sizeof(BalloonEval));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000111 if (beval != NULL)
112 {
113#ifdef FEAT_GUI_GTK
114 beval->target = GTK_WIDGET(target);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000115#else
116 beval->target = (Widget)target;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000117 beval->appContext = XtWidgetToApplicationContext((Widget)target);
118#endif
119 beval->showState = ShS_NEUTRAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000120 beval->msg = mesg;
121 beval->msgCB = mesgCB;
122 beval->clientData = clientData;
123
124 /*
125 * Set up event handler which will keep its eyes on the pointer,
126 * and when the pointer rests in a certain spot for a given time
127 * interval, show the beval.
128 */
129 addEventHandler(beval->target, beval);
130 createBalloonEvalWindow(beval);
131
132#ifndef FEAT_GUI_GTK
133 /*
134 * Now create and save the screen width and height. Used in drawing.
135 */
136 display_name = DisplayString(gui.dpy);
137 p = strrchr(display_name, '.');
138 if (p != NULL)
139 screen_num = atoi(++p);
140 else
141 screen_num = 0;
142 beval->screen_width = DisplayWidth(gui.dpy, screen_num);
143 beval->screen_height = DisplayHeight(gui.dpy, screen_num);
144#endif
145 }
146
147 return beval;
148}
149
150#if defined(FEAT_BEVAL_TIP) || defined(PROTO)
151/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +0000152 * Destroy a balloon-eval and free its associated memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000153 */
154 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100155gui_mch_destroy_beval_area(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000156{
157 cancelBalloon(beval);
158 removeEventHandler(beval);
159 /* Children will automatically be destroyed */
160# ifdef FEAT_GUI_GTK
161 gtk_widget_destroy(beval->balloonShell);
162# else
163 XtDestroyWidget(beval->balloonShell);
164# endif
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200165# ifdef FEAT_VARTABS
166 if (beval->vts)
167 vim_free(beval->vts);
168# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000169 vim_free(beval);
170}
171#endif
172
173 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100174gui_mch_enable_beval_area(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000175{
176 if (beval != NULL)
177 addEventHandler(beval->target, beval);
178}
179
180 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100181gui_mch_disable_beval_area(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000182{
183 if (beval != NULL)
184 removeEventHandler(beval);
185}
186
187#if defined(FEAT_BEVAL_TIP) || defined(PROTO)
188/*
189 * This function returns the BalloonEval * associated with the currently
190 * displayed tooltip. Returns NULL if there is no tooltip currently showing.
191 *
192 * Assumption: Only one tooltip can be shown at a time.
193 */
194 BalloonEval *
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100195gui_mch_currently_showing_beval(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000196{
197 return current_beval;
198}
199#endif
200#endif /* !FEAT_GUI_W32 */
201
Bram Moolenaare2ac10d2005-03-07 23:26:06 +0000202#if defined(FEAT_SUN_WORKSHOP) || defined(FEAT_NETBEANS_INTG) \
203 || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000204# if !defined(FEAT_GUI_W32) || defined(PROTO)
205
206/*
207 * Show a balloon with "mesg".
208 */
209 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100210gui_mch_post_balloon(BalloonEval *beval, char_u *mesg)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000211{
212 beval->msg = mesg;
213 if (mesg != NULL)
214 drawBalloon(beval);
215 else
216 undrawBalloon(beval);
217}
Bram Moolenaarc3719bd2017-11-18 22:13:31 +0100218# endif /* !FEAT_GUI_W32 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000219#endif /* FEAT_SUN_WORKSHOP || FEAT_NETBEANS_INTG || PROTO */
220
221#if !defined(FEAT_GUI_W32) || defined(PROTO)
222#if defined(FEAT_BEVAL_TIP) || defined(PROTO)
223/*
224 * Hide the given balloon.
225 */
226 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100227gui_mch_unpost_balloon(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000228{
229 undrawBalloon(beval);
230}
231#endif
232
233#ifdef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +0000234 static void
235addEventHandler(GtkWidget *target, BalloonEval *beval)
236{
237 /*
238 * Connect to the generic "event" signal instead of the individual
239 * signals for each event type, because the former is emitted earlier.
240 * This allows us to catch events independently of the signal handlers
241 * in gui_gtk_x11.c.
242 */
Bram Moolenaar98921892016-02-23 17:14:37 +0100243 g_signal_connect(G_OBJECT(target), "event",
244 G_CALLBACK(target_event_cb),
245 beval);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000246 /*
247 * Nasty: Key press events go to the main window thus the drawing area
248 * will never see them. This means we have to connect to the main window
249 * as well in order to catch those events.
250 */
251 if (gtk_socket_id == 0 && gui.mainwin != NULL
252 && gtk_widget_is_ancestor(target, gui.mainwin))
253 {
Bram Moolenaar98921892016-02-23 17:14:37 +0100254 g_signal_connect(G_OBJECT(gui.mainwin), "event",
255 G_CALLBACK(mainwin_event_cb),
256 beval);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000257 }
258}
259
260 static void
261removeEventHandler(BalloonEval *beval)
262{
Bram Moolenaar98921892016-02-23 17:14:37 +0100263 g_signal_handlers_disconnect_by_func(G_OBJECT(beval->target),
Bram Moolenaard47d8372016-09-09 22:13:24 +0200264 FUNC2GENERIC(target_event_cb),
Bram Moolenaar98921892016-02-23 17:14:37 +0100265 beval);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000266
267 if (gtk_socket_id == 0 && gui.mainwin != NULL
268 && gtk_widget_is_ancestor(beval->target, gui.mainwin))
269 {
Bram Moolenaar98921892016-02-23 17:14:37 +0100270 g_signal_handlers_disconnect_by_func(G_OBJECT(gui.mainwin),
Bram Moolenaard47d8372016-09-09 22:13:24 +0200271 FUNC2GENERIC(mainwin_event_cb),
Bram Moolenaar98921892016-02-23 17:14:37 +0100272 beval);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000273 }
274}
275
276 static gint
277target_event_cb(GtkWidget *widget, GdkEvent *event, gpointer data)
278{
279 BalloonEval *beval = (BalloonEval *)data;
280
281 switch (event->type)
282 {
283 case GDK_ENTER_NOTIFY:
284 pointer_event(beval, (int)event->crossing.x,
285 (int)event->crossing.y,
286 event->crossing.state);
287 break;
288 case GDK_MOTION_NOTIFY:
289 if (event->motion.is_hint)
290 {
291 int x;
292 int y;
293 GdkModifierType state;
294 /*
295 * GDK_POINTER_MOTION_HINT_MASK is set, thus we cannot obtain
296 * the coordinates from the GdkEventMotion struct directly.
297 */
Bram Moolenaar98921892016-02-23 17:14:37 +0100298# if GTK_CHECK_VERSION(3,0,0)
299 {
300 GdkWindow * const win = gtk_widget_get_window(widget);
301 GdkDisplay * const dpy = gdk_window_get_display(win);
Bram Moolenaar30e12d22016-04-17 20:49:53 +0200302# if GTK_CHECK_VERSION(3,20,0)
303 GdkSeat * const seat = gdk_display_get_default_seat(dpy);
304 GdkDevice * const dev = gdk_seat_get_pointer(seat);
305# else
Bram Moolenaar98921892016-02-23 17:14:37 +0100306 GdkDeviceManager * const mngr = gdk_display_get_device_manager(dpy);
307 GdkDevice * const dev = gdk_device_manager_get_client_pointer(mngr);
Bram Moolenaar30e12d22016-04-17 20:49:53 +0200308# endif
Bram Moolenaar98921892016-02-23 17:14:37 +0100309 gdk_window_get_device_position(win, dev , &x, &y, &state);
310 }
311# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000312 gdk_window_get_pointer(widget->window, &x, &y, &state);
Bram Moolenaar98921892016-02-23 17:14:37 +0100313# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000314 pointer_event(beval, x, y, (unsigned int)state);
315 }
316 else
317 {
318 pointer_event(beval, (int)event->motion.x,
319 (int)event->motion.y,
320 event->motion.state);
321 }
322 break;
323 case GDK_LEAVE_NOTIFY:
324 /*
325 * Ignore LeaveNotify events that are not "normal".
326 * Apparently we also get it when somebody else grabs focus.
327 */
328 if (event->crossing.mode == GDK_CROSSING_NORMAL)
329 cancelBalloon(beval);
330 break;
331 case GDK_BUTTON_PRESS:
Bram Moolenaar071d4272004-06-13 20:20:40 +0000332 case GDK_SCROLL:
Bram Moolenaar071d4272004-06-13 20:20:40 +0000333 cancelBalloon(beval);
334 break;
335 case GDK_KEY_PRESS:
336 key_event(beval, event->key.keyval, TRUE);
337 break;
338 case GDK_KEY_RELEASE:
339 key_event(beval, event->key.keyval, FALSE);
340 break;
341 default:
342 break;
343 }
344
345 return FALSE; /* continue emission */
346}
347
Bram Moolenaar071d4272004-06-13 20:20:40 +0000348 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +0000349mainwin_event_cb(GtkWidget *widget UNUSED, GdkEvent *event, gpointer data)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000350{
351 BalloonEval *beval = (BalloonEval *)data;
352
353 switch (event->type)
354 {
355 case GDK_KEY_PRESS:
356 key_event(beval, event->key.keyval, TRUE);
357 break;
358 case GDK_KEY_RELEASE:
359 key_event(beval, event->key.keyval, FALSE);
360 break;
361 default:
362 break;
363 }
364
365 return FALSE; /* continue emission */
366}
367
368 static void
369pointer_event(BalloonEval *beval, int x, int y, unsigned state)
370{
371 int distance;
372
373 distance = ABS(x - beval->x) + ABS(y - beval->y);
374
375 if (distance > 4)
376 {
377 /*
378 * Moved out of the balloon location: cancel it.
379 * Remember button state
380 */
381 beval->state = state;
382 cancelBalloon(beval);
383
384 /* Mouse buttons are pressed - no balloon now */
385 if (!(state & ((int)GDK_BUTTON1_MASK | (int)GDK_BUTTON2_MASK
386 | (int)GDK_BUTTON3_MASK)))
387 {
388 beval->x = x;
389 beval->y = y;
390
391 if (state & (int)GDK_MOD1_MASK)
392 {
393 /*
394 * Alt is pressed -- enter super-evaluate-mode,
395 * where there is no time delay
396 */
397 if (beval->msgCB != NULL)
398 {
399 beval->showState = ShS_PENDING;
400 (*beval->msgCB)(beval, state);
401 }
402 }
403 else
404 {
Bram Moolenaar98921892016-02-23 17:14:37 +0100405 beval->timerID = g_timeout_add((guint)p_bdlay,
406 &timeout_cb, beval);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000407 }
408 }
409 }
410}
411
412 static void
413key_event(BalloonEval *beval, unsigned keyval, int is_keypress)
414{
415 if (beval->showState == ShS_SHOWING && beval->msgCB != NULL)
416 {
417 switch (keyval)
418 {
419 case GDK_Shift_L:
420 case GDK_Shift_R:
421 beval->showState = ShS_UPDATE_PENDING;
422 (*beval->msgCB)(beval, (is_keypress)
423 ? (int)GDK_SHIFT_MASK : 0);
424 break;
425 case GDK_Control_L:
426 case GDK_Control_R:
427 beval->showState = ShS_UPDATE_PENDING;
428 (*beval->msgCB)(beval, (is_keypress)
429 ? (int)GDK_CONTROL_MASK : 0);
430 break;
431 default:
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000432 /* Don't do this for key release, we apparently get these with
433 * focus changes in some GTK version. */
434 if (is_keypress)
435 cancelBalloon(beval);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000436 break;
437 }
438 }
439 else
440 cancelBalloon(beval);
441}
442
Bram Moolenaar98921892016-02-23 17:14:37 +0100443 static gboolean
Bram Moolenaar071d4272004-06-13 20:20:40 +0000444timeout_cb(gpointer data)
445{
446 BalloonEval *beval = (BalloonEval *)data;
447
448 beval->timerID = 0;
449 /*
450 * If the timer event happens then the mouse has stopped long enough for
451 * a request to be started. The request will only send to the debugger if
452 * there the mouse is pointing at real data.
453 */
454 requestBalloon(beval);
455
456 return FALSE; /* don't call me again */
457}
458
Bram Moolenaar98921892016-02-23 17:14:37 +0100459# if GTK_CHECK_VERSION(3,0,0)
460 static gboolean
461balloon_draw_event_cb(GtkWidget *widget,
462 cairo_t *cr,
463 gpointer data UNUSED)
464{
465 GtkStyleContext *context = NULL;
466 gint width = -1, height = -1;
467
468 if (widget == NULL)
469 return TRUE;
470
471 context = gtk_widget_get_style_context(widget);
472 width = gtk_widget_get_allocated_width(widget);
473 height = gtk_widget_get_allocated_height(widget);
474
475 gtk_style_context_save(context);
476
477 gtk_style_context_add_class(context, "tooltip");
478 gtk_style_context_set_state(context, GTK_STATE_FLAG_NORMAL);
479
480 cairo_save(cr);
481 gtk_render_frame(context, cr, 0, 0, width, height);
482 gtk_render_background(context, cr, 0, 0, width, height);
483 cairo_restore(cr);
484
485 gtk_style_context_restore(context);
486
487 return FALSE;
488}
489# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000490 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +0000491balloon_expose_event_cb(GtkWidget *widget,
492 GdkEventExpose *event,
493 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000494{
495 gtk_paint_flat_box(widget->style, widget->window,
496 GTK_STATE_NORMAL, GTK_SHADOW_OUT,
497 &event->area, widget, "tooltip",
498 0, 0, -1, -1);
499
500 return FALSE; /* continue emission */
501}
Bram Moolenaar98921892016-02-23 17:14:37 +0100502# endif /* !GTK_CHECK_VERSION(3,0,0) */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000503
Bram Moolenaar071d4272004-06-13 20:20:40 +0000504#else /* !FEAT_GUI_GTK */
505
506 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100507addEventHandler(Widget target, BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000508{
509 XtAddEventHandler(target,
510 PointerMotionMask | EnterWindowMask |
511 LeaveWindowMask | ButtonPressMask | KeyPressMask |
512 KeyReleaseMask,
513 False,
514 pointerEventEH, (XtPointer)beval);
515}
516
517 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100518removeEventHandler(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000519{
520 XtRemoveEventHandler(beval->target,
521 PointerMotionMask | EnterWindowMask |
522 LeaveWindowMask | ButtonPressMask | KeyPressMask |
523 KeyReleaseMask,
524 False,
525 pointerEventEH, (XtPointer)beval);
526}
527
528
529/*
530 * The X event handler. All it does is call the real event handler.
531 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000532 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100533pointerEventEH(
534 Widget w UNUSED,
535 XtPointer client_data,
536 XEvent *event,
537 Boolean *unused UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000538{
539 BalloonEval *beval = (BalloonEval *)client_data;
540 pointerEvent(beval, event);
541}
542
543
544/*
545 * The real event handler. Called by pointerEventEH() whenever an event we are
Bram Moolenaarbae0c162007-05-10 19:30:25 +0000546 * interested in occurs.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000547 */
548
549 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100550pointerEvent(BalloonEval *beval, XEvent *event)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000551{
Bram Moolenaar84a05ac2013-05-06 04:24:17 +0200552 Position distance; /* a measure of how much the pointer moved */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000553 Position delta; /* used to compute distance */
554
555 switch (event->type)
556 {
557 case EnterNotify:
558 case MotionNotify:
559 delta = event->xmotion.x - beval->x;
560 if (delta < 0)
561 delta = -delta;
562 distance = delta;
563 delta = event->xmotion.y - beval->y;
564 if (delta < 0)
565 delta = -delta;
566 distance += delta;
567 if (distance > 4)
568 {
569 /*
570 * Moved out of the balloon location: cancel it.
571 * Remember button state
572 */
573 beval->state = event->xmotion.state;
574 if (beval->state & (Button1Mask|Button2Mask|Button3Mask))
575 {
576 /* Mouse buttons are pressed - no balloon now */
577 cancelBalloon(beval);
578 }
579 else if (beval->state & (Mod1Mask|Mod2Mask|Mod3Mask))
580 {
581 /*
582 * Alt is pressed -- enter super-evaluate-mode,
583 * where there is no time delay
584 */
585 beval->x = event->xmotion.x;
586 beval->y = event->xmotion.y;
587 beval->x_root = event->xmotion.x_root;
588 beval->y_root = event->xmotion.y_root;
589 cancelBalloon(beval);
590 if (beval->msgCB != NULL)
591 {
592 beval->showState = ShS_PENDING;
593 (*beval->msgCB)(beval, beval->state);
594 }
595 }
596 else
597 {
598 beval->x = event->xmotion.x;
599 beval->y = event->xmotion.y;
600 beval->x_root = event->xmotion.x_root;
601 beval->y_root = event->xmotion.y_root;
602 cancelBalloon(beval);
603 beval->timerID = XtAppAddTimeOut( beval->appContext,
604 (long_u)p_bdlay, timerRoutine, beval);
605 }
606 }
607 break;
608
609 /*
610 * Motif and Athena version: Keystrokes will be caught by the
611 * "textArea" widget, and handled in gui_x11_key_hit_cb().
612 */
613 case KeyPress:
614 if (beval->showState == ShS_SHOWING && beval->msgCB != NULL)
615 {
616 Modifiers modifier;
617 KeySym keysym;
618
619 XtTranslateKeycode(gui.dpy,
620 event->xkey.keycode, event->xkey.state,
621 &modifier, &keysym);
622 if (keysym == XK_Shift_L || keysym == XK_Shift_R)
623 {
624 beval->showState = ShS_UPDATE_PENDING;
625 (*beval->msgCB)(beval, ShiftMask);
626 }
627 else if (keysym == XK_Control_L || keysym == XK_Control_R)
628 {
629 beval->showState = ShS_UPDATE_PENDING;
630 (*beval->msgCB)(beval, ControlMask);
631 }
632 else
633 cancelBalloon(beval);
634 }
635 else
636 cancelBalloon(beval);
637 break;
638
639 case KeyRelease:
640 if (beval->showState == ShS_SHOWING && beval->msgCB != NULL)
641 {
642 Modifiers modifier;
643 KeySym keysym;
644
645 XtTranslateKeycode(gui.dpy, event->xkey.keycode,
646 event->xkey.state, &modifier, &keysym);
647 if ((keysym == XK_Shift_L) || (keysym == XK_Shift_R)) {
648 beval->showState = ShS_UPDATE_PENDING;
649 (*beval->msgCB)(beval, 0);
650 }
651 else if ((keysym == XK_Control_L) || (keysym == XK_Control_R))
652 {
653 beval->showState = ShS_UPDATE_PENDING;
654 (*beval->msgCB)(beval, 0);
655 }
656 else
657 cancelBalloon(beval);
658 }
659 else
660 cancelBalloon(beval);
661 break;
662
663 case LeaveNotify:
664 /* Ignore LeaveNotify events that are not "normal".
665 * Apparently we also get it when somebody else grabs focus.
666 * Happens for me every two seconds (some clipboard tool?) */
667 if (event->xcrossing.mode == NotifyNormal)
668 cancelBalloon(beval);
669 break;
670
671 case ButtonPress:
672 cancelBalloon(beval);
673 break;
674
675 default:
676 break;
677 }
678}
679
Bram Moolenaar071d4272004-06-13 20:20:40 +0000680 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100681timerRoutine(XtPointer dx, XtIntervalId *id UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000682{
683 BalloonEval *beval = (BalloonEval *)dx;
684
685 beval->timerID = (XtIntervalId)NULL;
686
687 /*
688 * If the timer event happens then the mouse has stopped long enough for
689 * a request to be started. The request will only send to the debugger if
690 * there the mouse is pointing at real data.
691 */
692 requestBalloon(beval);
693}
694
695#endif /* !FEAT_GUI_GTK */
696
697 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100698requestBalloon(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000699{
700 if (beval->showState != ShS_PENDING)
701 {
702 /* Determine the beval to display */
703 if (beval->msgCB != NULL)
704 {
705 beval->showState = ShS_PENDING;
706 (*beval->msgCB)(beval, beval->state);
707 }
708 else if (beval->msg != NULL)
709 drawBalloon(beval);
710 }
711}
712
713#ifdef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +0000714/*
715 * Convert the string to UTF-8 if 'encoding' is not "utf-8".
716 * Replace any non-printable characters and invalid bytes sequences with
717 * "^X" or "<xx>" escapes, and apply SpecialKey highlighting to them.
718 * TAB and NL are passed through unscathed.
719 */
Bram Moolenaar182c5be2010-06-25 05:37:59 +0200720# define IS_NONPRINTABLE(c) (((c) < 0x20 && (c) != TAB && (c) != NL) \
Bram Moolenaar071d4272004-06-13 20:20:40 +0000721 || (c) == DEL)
722 static void
Bram Moolenaar89d40322006-08-29 15:30:07 +0000723set_printable_label_text(GtkLabel *label, char_u *text)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000724{
725 char_u *convbuf = NULL;
726 char_u *buf;
727 char_u *p;
728 char_u *pdest;
729 unsigned int len;
730 int charlen;
731 int uc;
732 PangoAttrList *attr_list;
733
734 /* Convert to UTF-8 if it isn't already */
735 if (output_conv.vc_type != CONV_NONE)
736 {
Bram Moolenaar89d40322006-08-29 15:30:07 +0000737 convbuf = string_convert(&output_conv, text, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000738 if (convbuf != NULL)
Bram Moolenaar89d40322006-08-29 15:30:07 +0000739 text = convbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000740 }
741
742 /* First let's see how much we need to allocate */
743 len = 0;
Bram Moolenaar89d40322006-08-29 15:30:07 +0000744 for (p = text; *p != NUL; p += charlen)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000745 {
746 if ((*p & 0x80) == 0) /* be quick for ASCII */
747 {
748 charlen = 1;
749 len += IS_NONPRINTABLE(*p) ? 2 : 1; /* nonprintable: ^X */
750 }
751 else
752 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000753 charlen = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000754 uc = utf_ptr2char(p);
755
756 if (charlen != utf_char2len(uc))
757 charlen = 1; /* reject overlong sequences */
758
759 if (charlen == 1 || uc < 0xa0) /* illegal byte or */
760 len += 4; /* control char: <xx> */
761 else if (!utf_printable(uc))
762 /* Note: we assume here that utf_printable() doesn't
763 * care about characters outside the BMP. */
764 len += 6; /* nonprintable: <xxxx> */
765 else
766 len += charlen;
767 }
768 }
769
770 attr_list = pango_attr_list_new();
771 buf = alloc(len + 1);
772
773 /* Now go for the real work */
774 if (buf != NULL)
775 {
776 attrentry_T *aep;
777 PangoAttribute *attr;
778 guicolor_T pixel;
Bram Moolenaar36edf062016-07-21 22:10:12 +0200779#if GTK_CHECK_VERSION(3,0,0)
780 GdkRGBA color = { 0.0, 0.0, 0.0, 1.0 };
Bram Moolenaar870b7492016-07-22 22:26:52 +0200781# if PANGO_VERSION_CHECK(1,38,0)
Bram Moolenaar36edf062016-07-21 22:10:12 +0200782 PangoAttribute *attr_alpha;
Bram Moolenaar870b7492016-07-22 22:26:52 +0200783# endif
Bram Moolenaar36edf062016-07-21 22:10:12 +0200784#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000785 GdkColor color = { 0, 0, 0, 0 };
Bram Moolenaar36edf062016-07-21 22:10:12 +0200786#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000787
788 /* Look up the RGB values of the SpecialKey foreground color. */
Bram Moolenaar8820b482017-03-16 17:23:31 +0100789 aep = syn_gui_attr2entry(HL_ATTR(HLF_8));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000790 pixel = (aep != NULL) ? aep->ae_u.gui.fg_color : INVALCOLOR;
791 if (pixel != INVALCOLOR)
Bram Moolenaar98921892016-02-23 17:14:37 +0100792# if GTK_CHECK_VERSION(3,0,0)
793 {
Bram Moolenaar36edf062016-07-21 22:10:12 +0200794 color.red = ((pixel & 0xff0000) >> 16) / 255.0;
795 color.green = ((pixel & 0xff00) >> 8) / 255.0;
796 color.blue = (pixel & 0xff) / 255.0;
797 color.alpha = 1.0;
Bram Moolenaar98921892016-02-23 17:14:37 +0100798 }
799# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000800 gdk_colormap_query_color(gtk_widget_get_colormap(gui.drawarea),
801 (unsigned long)pixel, &color);
Bram Moolenaar98921892016-02-23 17:14:37 +0100802# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000803
804 pdest = buf;
Bram Moolenaar89d40322006-08-29 15:30:07 +0000805 p = text;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000806 while (*p != NUL)
807 {
808 /* Be quick for ASCII */
809 if ((*p & 0x80) == 0 && !IS_NONPRINTABLE(*p))
810 {
811 *pdest++ = *p++;
812 }
813 else
814 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000815 charlen = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000816 uc = utf_ptr2char(p);
817
818 if (charlen != utf_char2len(uc))
819 charlen = 1; /* reject overlong sequences */
820
821 if (charlen == 1 || uc < 0xa0 || !utf_printable(uc))
822 {
823 int outlen;
824
825 /* Careful: we can't just use transchar_byte() here,
826 * since 'encoding' is not necessarily set to "utf-8". */
827 if (*p & 0x80 && charlen == 1)
828 {
829 transchar_hex(pdest, *p); /* <xx> */
830 outlen = 4;
831 }
832 else if (uc >= 0x80)
833 {
834 /* Note: we assume here that utf_printable() doesn't
835 * care about characters outside the BMP. */
836 transchar_hex(pdest, uc); /* <xx> or <xxxx> */
837 outlen = (uc < 0x100) ? 4 : 6;
838 }
839 else
840 {
841 transchar_nonprint(pdest, *p); /* ^X */
842 outlen = 2;
843 }
844 if (pixel != INVALCOLOR)
845 {
Bram Moolenaar36edf062016-07-21 22:10:12 +0200846#if GTK_CHECK_VERSION(3,0,0)
847# define DOUBLE2UINT16(val) ((guint16)((val) * 65535 + 0.5))
848 attr = pango_attr_foreground_new(
849 DOUBLE2UINT16(color.red),
850 DOUBLE2UINT16(color.green),
851 DOUBLE2UINT16(color.blue));
Bram Moolenaar870b7492016-07-22 22:26:52 +0200852# if PANGO_VERSION_CHECK(1,38,0)
Bram Moolenaar36edf062016-07-21 22:10:12 +0200853 attr_alpha = pango_attr_foreground_alpha_new(
854 DOUBLE2UINT16(color.alpha));
Bram Moolenaar870b7492016-07-22 22:26:52 +0200855# endif
Bram Moolenaar36edf062016-07-21 22:10:12 +0200856# undef DOUBLE2UINT16
857#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000858 attr = pango_attr_foreground_new(
859 color.red, color.green, color.blue);
Bram Moolenaar36edf062016-07-21 22:10:12 +0200860#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000861 attr->start_index = pdest - buf;
862 attr->end_index = pdest - buf + outlen;
863 pango_attr_list_insert(attr_list, attr);
Bram Moolenaar36edf062016-07-21 22:10:12 +0200864#if GTK_CHECK_VERSION(3,0,0)
Bram Moolenaar870b7492016-07-22 22:26:52 +0200865# if PANGO_VERSION_CHECK(1,38,0)
Bram Moolenaar36edf062016-07-21 22:10:12 +0200866 attr_alpha->start_index = pdest - buf;
867 attr_alpha->end_index = pdest - buf + outlen;
868 pango_attr_list_insert(attr_list, attr_alpha);
Bram Moolenaar870b7492016-07-22 22:26:52 +0200869# endif
Bram Moolenaar36edf062016-07-21 22:10:12 +0200870#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000871 }
872 pdest += outlen;
873 p += charlen;
874 }
875 else
876 {
877 do
878 *pdest++ = *p++;
879 while (--charlen != 0);
880 }
881 }
882 }
883 *pdest = NUL;
884 }
885
886 vim_free(convbuf);
887
888 gtk_label_set_text(label, (const char *)buf);
889 vim_free(buf);
890
891 gtk_label_set_attributes(label, attr_list);
892 pango_attr_list_unref(attr_list);
893}
Bram Moolenaar182c5be2010-06-25 05:37:59 +0200894# undef IS_NONPRINTABLE
Bram Moolenaar071d4272004-06-13 20:20:40 +0000895
896/*
897 * Draw a balloon.
898 */
899 static void
900drawBalloon(BalloonEval *beval)
901{
902 if (beval->msg != NULL)
903 {
904 GtkRequisition requisition;
905 int screen_w;
906 int screen_h;
Bram Moolenaar3f6a16f2018-08-19 22:58:45 +0200907 int screen_x;
908 int screen_y;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000909 int x;
910 int y;
911 int x_offset = EVAL_OFFSET_X;
912 int y_offset = EVAL_OFFSET_Y;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000913 PangoLayout *layout;
Bram Moolenaara859f042016-11-17 19:11:55 +0100914
Bram Moolenaar7be9b502017-09-09 18:45:26 +0200915# if !GTK_CHECK_VERSION(3,22,2)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000916 GdkScreen *screen;
917
918 screen = gtk_widget_get_screen(beval->target);
919 gtk_window_set_screen(GTK_WINDOW(beval->balloonShell), screen);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000920# endif
Bram Moolenaara449a7c2018-08-28 23:09:07 +0200921 gui_gtk_get_screen_geom_of_win(beval->target,
Bram Moolenaar3f6a16f2018-08-19 22:58:45 +0200922 &screen_x, &screen_y, &screen_w, &screen_h);
Bram Moolenaar98921892016-02-23 17:14:37 +0100923# if !GTK_CHECK_VERSION(3,0,0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000924 gtk_widget_ensure_style(beval->balloonShell);
925 gtk_widget_ensure_style(beval->balloonLabel);
Bram Moolenaar98921892016-02-23 17:14:37 +0100926# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000927
Bram Moolenaar071d4272004-06-13 20:20:40 +0000928 set_printable_label_text(GTK_LABEL(beval->balloonLabel), beval->msg);
929 /*
930 * Dirty trick: Enable wrapping mode on the label's layout behind its
931 * back. This way GtkLabel won't try to constrain the wrap width to a
932 * builtin maximum value of about 65 Latin characters.
933 */
934 layout = gtk_label_get_layout(GTK_LABEL(beval->balloonLabel));
Bram Moolenaar182c5be2010-06-25 05:37:59 +0200935# ifdef PANGO_WRAP_WORD_CHAR
Bram Moolenaar071d4272004-06-13 20:20:40 +0000936 pango_layout_set_wrap(layout, PANGO_WRAP_WORD_CHAR);
Bram Moolenaar182c5be2010-06-25 05:37:59 +0200937# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000938 pango_layout_set_wrap(layout, PANGO_WRAP_WORD);
Bram Moolenaar182c5be2010-06-25 05:37:59 +0200939# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000940 pango_layout_set_width(layout,
941 /* try to come up with some reasonable width */
942 PANGO_SCALE * CLAMP(gui.num_cols * gui.char_width,
943 screen_w / 2,
944 MAX(20, screen_w - 20)));
945
946 /* Calculate the balloon's width and height. */
Bram Moolenaar98921892016-02-23 17:14:37 +0100947# if GTK_CHECK_VERSION(3,0,0)
948 gtk_widget_get_preferred_size(beval->balloonShell, &requisition, NULL);
949# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000950 gtk_widget_size_request(beval->balloonShell, &requisition);
Bram Moolenaar98921892016-02-23 17:14:37 +0100951# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000952
953 /* Compute position of the balloon area */
Bram Moolenaar98921892016-02-23 17:14:37 +0100954 gdk_window_get_origin(gtk_widget_get_window(beval->target), &x, &y);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000955 x += beval->x;
956 y += beval->y;
957
958 /* Get out of the way of the mouse pointer */
Bram Moolenaar3f6a16f2018-08-19 22:58:45 +0200959 if (x + x_offset + requisition.width > screen_x + screen_w)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000960 y_offset += 15;
Bram Moolenaar3f6a16f2018-08-19 22:58:45 +0200961 if (y + y_offset + requisition.height > screen_y + screen_h)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000962 y_offset = -requisition.height - EVAL_OFFSET_Y;
963
964 /* Sanitize values */
Bram Moolenaar3f6a16f2018-08-19 22:58:45 +0200965 x = CLAMP(x + x_offset, 0,
966 MAX(0, screen_x + screen_w - requisition.width));
967 y = CLAMP(y + y_offset, 0,
968 MAX(0, screen_y + screen_h - requisition.height));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000969
970 /* Show the balloon */
Bram Moolenaar98921892016-02-23 17:14:37 +0100971# if GTK_CHECK_VERSION(3,0,0)
972 gtk_window_move(GTK_WINDOW(beval->balloonShell), x, y);
973# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000974 gtk_widget_set_uposition(beval->balloonShell, x, y);
Bram Moolenaar98921892016-02-23 17:14:37 +0100975# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000976 gtk_widget_show(beval->balloonShell);
977
978 beval->showState = ShS_SHOWING;
979 }
980}
981
982/*
983 * Undraw a balloon.
984 */
985 static void
986undrawBalloon(BalloonEval *beval)
987{
988 if (beval->balloonShell != NULL)
989 gtk_widget_hide(beval->balloonShell);
990 beval->showState = ShS_NEUTRAL;
991}
992
993 static void
994cancelBalloon(BalloonEval *beval)
995{
996 if (beval->showState == ShS_SHOWING
997 || beval->showState == ShS_UPDATE_PENDING)
998 undrawBalloon(beval);
999
1000 if (beval->timerID != 0)
1001 {
Bram Moolenaar98921892016-02-23 17:14:37 +01001002 g_source_remove(beval->timerID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001003 beval->timerID = 0;
1004 }
1005 beval->showState = ShS_NEUTRAL;
1006}
1007
1008 static void
1009createBalloonEvalWindow(BalloonEval *beval)
1010{
1011 beval->balloonShell = gtk_window_new(GTK_WINDOW_POPUP);
1012
1013 gtk_widget_set_app_paintable(beval->balloonShell, TRUE);
Bram Moolenaar98921892016-02-23 17:14:37 +01001014 gtk_window_set_resizable(GTK_WINDOW(beval->balloonShell), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001015 gtk_widget_set_name(beval->balloonShell, "gtk-tooltips");
Bram Moolenaar98921892016-02-23 17:14:37 +01001016 gtk_container_set_border_width(GTK_CONTAINER(beval->balloonShell), 4);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001017
Bram Moolenaar98921892016-02-23 17:14:37 +01001018# if GTK_CHECK_VERSION(3,0,0)
1019 g_signal_connect(G_OBJECT(beval->balloonShell), "draw",
1020 G_CALLBACK(balloon_draw_event_cb), NULL);
1021# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001022 gtk_signal_connect((GtkObject*)(beval->balloonShell), "expose_event",
1023 GTK_SIGNAL_FUNC(balloon_expose_event_cb), NULL);
Bram Moolenaar98921892016-02-23 17:14:37 +01001024# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001025 beval->balloonLabel = gtk_label_new(NULL);
1026
1027 gtk_label_set_line_wrap(GTK_LABEL(beval->balloonLabel), FALSE);
1028 gtk_label_set_justify(GTK_LABEL(beval->balloonLabel), GTK_JUSTIFY_LEFT);
Bram Moolenaar98921892016-02-23 17:14:37 +01001029# if GTK_CHECK_VERSION(3,16,0)
1030 gtk_label_set_xalign(GTK_LABEL(beval->balloonLabel), 0.5);
1031 gtk_label_set_yalign(GTK_LABEL(beval->balloonLabel), 0.5);
1032# elif GTK_CHECK_VERSION(3,14,0)
1033 GValue align_val = G_VALUE_INIT;
1034 g_value_init(&align_val, G_TYPE_FLOAT);
1035 g_value_set_float(&align_val, 0.5);
1036 g_object_set_property(G_OBJECT(beval->balloonLabel), "xalign", &align_val);
1037 g_object_set_property(G_OBJECT(beval->balloonLabel), "yalign", &align_val);
1038 g_value_unset(&align_val);
1039# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001040 gtk_misc_set_alignment(GTK_MISC(beval->balloonLabel), 0.5f, 0.5f);
Bram Moolenaar98921892016-02-23 17:14:37 +01001041# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001042 gtk_widget_set_name(beval->balloonLabel, "vim-balloon-label");
1043 gtk_widget_show(beval->balloonLabel);
1044
1045 gtk_container_add(GTK_CONTAINER(beval->balloonShell), beval->balloonLabel);
1046}
1047
1048#else /* !FEAT_GUI_GTK */
1049
1050/*
1051 * Draw a balloon.
1052 */
1053 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001054drawBalloon(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001055{
1056 Dimension w;
1057 Dimension h;
1058 Position tx;
1059 Position ty;
1060
1061 if (beval->msg != NULL)
1062 {
1063 /* Show the Balloon */
1064
1065 /* Calculate the label's width and height */
1066#ifdef FEAT_GUI_MOTIF
1067 XmString s;
1068
1069 /* For the callback function we parse NL characters to create a
1070 * multi-line label. This doesn't work for all languages, but
1071 * XmStringCreateLocalized() doesn't do multi-line labels... */
1072 if (beval->msgCB != NULL)
1073 s = XmStringCreateLtoR((char *)beval->msg, XmFONTLIST_DEFAULT_TAG);
1074 else
1075 s = XmStringCreateLocalized((char *)beval->msg);
1076 {
1077 XmFontList fl;
1078
1079 fl = gui_motif_fontset2fontlist(&gui.tooltip_fontset);
Bram Moolenaardb5ffaa2014-06-25 17:44:49 +02001080 if (fl == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001081 {
Bram Moolenaardb5ffaa2014-06-25 17:44:49 +02001082 XmStringFree(s);
1083 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001084 }
Bram Moolenaardb5ffaa2014-06-25 17:44:49 +02001085 XmStringExtent(fl, s, &w, &h);
1086 XmFontListFree(fl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001087 }
1088 w += gui.border_offset << 1;
1089 h += gui.border_offset << 1;
1090 XtVaSetValues(beval->balloonLabel, XmNlabelString, s, NULL);
1091 XmStringFree(s);
1092#else /* Athena */
1093 /* Assume XtNinternational == True */
1094 XFontSet fset;
1095 XFontSetExtents *ext;
1096
1097 XtVaGetValues(beval->balloonLabel, XtNfontSet, &fset, NULL);
1098 ext = XExtentsOfFontSet(fset);
1099 h = ext->max_ink_extent.height;
1100 w = XmbTextEscapement(fset,
1101 (char *)beval->msg,
1102 (int)STRLEN(beval->msg));
1103 w += gui.border_offset << 1;
1104 h += gui.border_offset << 1;
1105 XtVaSetValues(beval->balloonLabel, XtNlabel, beval->msg, NULL);
1106#endif
1107
1108 /* Compute position of the balloon area */
1109 tx = beval->x_root + EVAL_OFFSET_X;
1110 ty = beval->y_root + EVAL_OFFSET_Y;
1111 if ((tx + w) > beval->screen_width)
1112 tx = beval->screen_width - w;
1113 if ((ty + h) > beval->screen_height)
1114 ty = beval->screen_height - h;
1115#ifdef FEAT_GUI_MOTIF
1116 XtVaSetValues(beval->balloonShell,
1117 XmNx, tx,
1118 XmNy, ty,
1119 NULL);
1120#else
1121 /* Athena */
1122 XtVaSetValues(beval->balloonShell,
1123 XtNx, tx,
1124 XtNy, ty,
1125 NULL);
1126#endif
Bram Moolenaar8281f442009-03-18 11:22:25 +00001127 /* Set tooltip colors */
1128 {
1129 Arg args[2];
1130
1131#ifdef FEAT_GUI_MOTIF
1132 args[0].name = XmNbackground;
1133 args[0].value = gui.tooltip_bg_pixel;
1134 args[1].name = XmNforeground;
1135 args[1].value = gui.tooltip_fg_pixel;
1136#else /* Athena */
1137 args[0].name = XtNbackground;
1138 args[0].value = gui.tooltip_bg_pixel;
1139 args[1].name = XtNforeground;
1140 args[1].value = gui.tooltip_fg_pixel;
1141#endif
1142 XtSetValues(beval->balloonLabel, &args[0], XtNumber(args));
1143 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001144
1145 XtPopup(beval->balloonShell, XtGrabNone);
1146
1147 beval->showState = ShS_SHOWING;
1148
1149 current_beval = beval;
1150 }
1151}
1152
1153/*
1154 * Undraw a balloon.
1155 */
1156 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001157undrawBalloon(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001158{
1159 if (beval->balloonShell != (Widget)0)
1160 XtPopdown(beval->balloonShell);
1161 beval->showState = ShS_NEUTRAL;
1162
1163 current_beval = NULL;
1164}
1165
1166 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001167cancelBalloon(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001168{
1169 if (beval->showState == ShS_SHOWING
1170 || beval->showState == ShS_UPDATE_PENDING)
1171 undrawBalloon(beval);
1172
1173 if (beval->timerID != (XtIntervalId)NULL)
1174 {
1175 XtRemoveTimeOut(beval->timerID);
1176 beval->timerID = (XtIntervalId)NULL;
1177 }
1178 beval->showState = ShS_NEUTRAL;
1179}
1180
1181
1182 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001183createBalloonEvalWindow(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001184{
1185 Arg args[12];
1186 int n;
1187
1188 n = 0;
1189#ifdef FEAT_GUI_MOTIF
1190 XtSetArg(args[n], XmNallowShellResize, True); n++;
1191 beval->balloonShell = XtAppCreateShell("balloonEval", "BalloonEval",
1192 overrideShellWidgetClass, gui.dpy, args, n);
1193#else
1194 /* Athena */
1195 XtSetArg(args[n], XtNallowShellResize, True); n++;
1196 beval->balloonShell = XtAppCreateShell("balloonEval", "BalloonEval",
1197 overrideShellWidgetClass, gui.dpy, args, n);
1198#endif
1199
1200 n = 0;
1201#ifdef FEAT_GUI_MOTIF
1202 {
1203 XmFontList fl;
1204
1205 fl = gui_motif_fontset2fontlist(&gui.tooltip_fontset);
1206 XtSetArg(args[n], XmNforeground, gui.tooltip_fg_pixel); n++;
1207 XtSetArg(args[n], XmNbackground, gui.tooltip_bg_pixel); n++;
1208 XtSetArg(args[n], XmNfontList, fl); n++;
1209 XtSetArg(args[n], XmNalignment, XmALIGNMENT_BEGINNING); n++;
1210 beval->balloonLabel = XtCreateManagedWidget("balloonLabel",
1211 xmLabelWidgetClass, beval->balloonShell, args, n);
1212 }
1213#else /* FEAT_GUI_ATHENA */
1214 XtSetArg(args[n], XtNforeground, gui.tooltip_fg_pixel); n++;
1215 XtSetArg(args[n], XtNbackground, gui.tooltip_bg_pixel); n++;
1216 XtSetArg(args[n], XtNinternational, True); n++;
1217 XtSetArg(args[n], XtNfontSet, gui.tooltip_fontset); n++;
1218 beval->balloonLabel = XtCreateManagedWidget("balloonLabel",
1219 labelWidgetClass, beval->balloonShell, args, n);
1220#endif
1221}
1222
1223#endif /* !FEAT_GUI_GTK */
1224#endif /* !FEAT_GUI_W32 */
1225
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01001226#endif /* FEAT_BEVAL_GUI */