blob: 7e4625080f72ad36104344cd20c97c0bd312b4b5 [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 +010063# if GTK_CHECK_VERSION(3,0,0)
64static gboolean timeout_cb(gpointer);
65# else
Bram Moolenaard25c16e2016-01-29 22:13:30 +010066static gint timeout_cb(gpointer);
Bram Moolenaar98921892016-02-23 17:14:37 +010067# endif
68# if GTK_CHECK_VERSION(3,0,0)
69static gboolean balloon_draw_event_cb (GtkWidget *, cairo_t *, gpointer);
70# else
71static gint balloon_expose_event_cb (GtkWidget *, GdkEventExpose *, gpointer);
72# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000073#else
Bram Moolenaard25c16e2016-01-29 22:13:30 +010074static void addEventHandler(Widget, BalloonEval *);
75static void removeEventHandler(BalloonEval *);
76static void pointerEventEH(Widget, XtPointer, XEvent *, Boolean *);
77static void pointerEvent(BalloonEval *, XEvent *);
78static void timerRoutine(XtPointer, XtIntervalId *);
Bram Moolenaar071d4272004-06-13 20:20:40 +000079#endif
Bram Moolenaard25c16e2016-01-29 22:13:30 +010080static void cancelBalloon(BalloonEval *);
81static void requestBalloon(BalloonEval *);
82static void drawBalloon(BalloonEval *);
83static void undrawBalloon(BalloonEval *beval);
84static void createBalloonEvalWindow(BalloonEval *);
Bram Moolenaar071d4272004-06-13 20:20:40 +000085
Bram Moolenaar071d4272004-06-13 20:20:40 +000086/*
87 * Create a balloon-evaluation area for a Widget.
88 * There can be either a "mesg" for a fixed string or "mesgCB" to generate a
89 * message by calling this callback function.
90 * When "mesg" is not NULL it must remain valid for as long as the balloon is
91 * used. It is not freed here.
92 * Returns a pointer to the resulting object (NULL when out of memory).
93 */
94 BalloonEval *
Bram Moolenaar66f948e2016-01-30 16:39:25 +010095gui_mch_create_beval_area(
96 void *target,
97 char_u *mesg,
98 void (*mesgCB)(BalloonEval *, int),
99 void *clientData)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000100{
101#ifndef FEAT_GUI_GTK
102 char *display_name; /* get from gui.dpy */
103 int screen_num;
104 char *p;
105#endif
106 BalloonEval *beval;
107
108 if (mesg != NULL && mesgCB != NULL)
109 {
Bram Moolenaar95f09602016-11-10 20:01:45 +0100110 IEMSG(_("E232: Cannot create BalloonEval with both message and callback"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000111 return NULL;
112 }
113
114 beval = (BalloonEval *)alloc(sizeof(BalloonEval));
115 if (beval != NULL)
116 {
117#ifdef FEAT_GUI_GTK
118 beval->target = GTK_WIDGET(target);
119 beval->balloonShell = NULL;
120 beval->timerID = 0;
121#else
122 beval->target = (Widget)target;
123 beval->balloonShell = NULL;
124 beval->timerID = (XtIntervalId)NULL;
125 beval->appContext = XtWidgetToApplicationContext((Widget)target);
126#endif
127 beval->showState = ShS_NEUTRAL;
128 beval->x = 0;
129 beval->y = 0;
130 beval->msg = mesg;
131 beval->msgCB = mesgCB;
132 beval->clientData = clientData;
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200133#ifdef FEAT_VARTABS
134 beval->vts = NULL;
135#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000136
137 /*
138 * Set up event handler which will keep its eyes on the pointer,
139 * and when the pointer rests in a certain spot for a given time
140 * interval, show the beval.
141 */
142 addEventHandler(beval->target, beval);
143 createBalloonEvalWindow(beval);
144
145#ifndef FEAT_GUI_GTK
146 /*
147 * Now create and save the screen width and height. Used in drawing.
148 */
149 display_name = DisplayString(gui.dpy);
150 p = strrchr(display_name, '.');
151 if (p != NULL)
152 screen_num = atoi(++p);
153 else
154 screen_num = 0;
155 beval->screen_width = DisplayWidth(gui.dpy, screen_num);
156 beval->screen_height = DisplayHeight(gui.dpy, screen_num);
157#endif
158 }
159
160 return beval;
161}
162
163#if defined(FEAT_BEVAL_TIP) || defined(PROTO)
164/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +0000165 * Destroy a balloon-eval and free its associated memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000166 */
167 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100168gui_mch_destroy_beval_area(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000169{
170 cancelBalloon(beval);
171 removeEventHandler(beval);
172 /* Children will automatically be destroyed */
173# ifdef FEAT_GUI_GTK
174 gtk_widget_destroy(beval->balloonShell);
175# else
176 XtDestroyWidget(beval->balloonShell);
177# endif
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200178# ifdef FEAT_VARTABS
179 if (beval->vts)
180 vim_free(beval->vts);
181# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000182 vim_free(beval);
183}
184#endif
185
186 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100187gui_mch_enable_beval_area(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000188{
189 if (beval != NULL)
190 addEventHandler(beval->target, beval);
191}
192
193 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100194gui_mch_disable_beval_area(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000195{
196 if (beval != NULL)
197 removeEventHandler(beval);
198}
199
200#if defined(FEAT_BEVAL_TIP) || defined(PROTO)
201/*
202 * This function returns the BalloonEval * associated with the currently
203 * displayed tooltip. Returns NULL if there is no tooltip currently showing.
204 *
205 * Assumption: Only one tooltip can be shown at a time.
206 */
207 BalloonEval *
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100208gui_mch_currently_showing_beval(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000209{
210 return current_beval;
211}
212#endif
213#endif /* !FEAT_GUI_W32 */
214
Bram Moolenaare2ac10d2005-03-07 23:26:06 +0000215#if defined(FEAT_SUN_WORKSHOP) || defined(FEAT_NETBEANS_INTG) \
216 || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000217# if !defined(FEAT_GUI_W32) || defined(PROTO)
218
219/*
220 * Show a balloon with "mesg".
221 */
222 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100223gui_mch_post_balloon(BalloonEval *beval, char_u *mesg)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000224{
225 beval->msg = mesg;
226 if (mesg != NULL)
227 drawBalloon(beval);
228 else
229 undrawBalloon(beval);
230}
Bram Moolenaarc3719bd2017-11-18 22:13:31 +0100231# endif /* !FEAT_GUI_W32 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000232#endif /* FEAT_SUN_WORKSHOP || FEAT_NETBEANS_INTG || PROTO */
233
234#if !defined(FEAT_GUI_W32) || defined(PROTO)
235#if defined(FEAT_BEVAL_TIP) || defined(PROTO)
236/*
237 * Hide the given balloon.
238 */
239 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100240gui_mch_unpost_balloon(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000241{
242 undrawBalloon(beval);
243}
244#endif
245
246#ifdef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +0000247 static void
248addEventHandler(GtkWidget *target, BalloonEval *beval)
249{
250 /*
251 * Connect to the generic "event" signal instead of the individual
252 * signals for each event type, because the former is emitted earlier.
253 * This allows us to catch events independently of the signal handlers
254 * in gui_gtk_x11.c.
255 */
Bram Moolenaar98921892016-02-23 17:14:37 +0100256# if GTK_CHECK_VERSION(3,0,0)
257 g_signal_connect(G_OBJECT(target), "event",
258 G_CALLBACK(target_event_cb),
259 beval);
260# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000261 /* Should use GTK_OBJECT() here, but that causes a lint warning... */
262 gtk_signal_connect((GtkObject*)(target), "event",
263 GTK_SIGNAL_FUNC(target_event_cb),
264 beval);
Bram Moolenaar98921892016-02-23 17:14:37 +0100265# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000266 /*
267 * Nasty: Key press events go to the main window thus the drawing area
268 * will never see them. This means we have to connect to the main window
269 * as well in order to catch those events.
270 */
271 if (gtk_socket_id == 0 && gui.mainwin != NULL
272 && gtk_widget_is_ancestor(target, gui.mainwin))
273 {
Bram Moolenaar98921892016-02-23 17:14:37 +0100274# if GTK_CHECK_VERSION(3,0,0)
275 g_signal_connect(G_OBJECT(gui.mainwin), "event",
276 G_CALLBACK(mainwin_event_cb),
277 beval);
278# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000279 gtk_signal_connect((GtkObject*)(gui.mainwin), "event",
280 GTK_SIGNAL_FUNC(mainwin_event_cb),
281 beval);
Bram Moolenaar98921892016-02-23 17:14:37 +0100282# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000283 }
284}
285
286 static void
287removeEventHandler(BalloonEval *beval)
288{
Bram Moolenaar33570922005-01-25 22:26:29 +0000289 /* LINTED: avoid warning: dubious operation on enum */
Bram Moolenaar98921892016-02-23 17:14:37 +0100290# if GTK_CHECK_VERSION(3,0,0)
291 g_signal_handlers_disconnect_by_func(G_OBJECT(beval->target),
Bram Moolenaard47d8372016-09-09 22:13:24 +0200292 FUNC2GENERIC(target_event_cb),
Bram Moolenaar98921892016-02-23 17:14:37 +0100293 beval);
294# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000295 gtk_signal_disconnect_by_func((GtkObject*)(beval->target),
296 GTK_SIGNAL_FUNC(target_event_cb),
297 beval);
Bram Moolenaar98921892016-02-23 17:14:37 +0100298# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000299
300 if (gtk_socket_id == 0 && gui.mainwin != NULL
301 && gtk_widget_is_ancestor(beval->target, gui.mainwin))
302 {
Bram Moolenaar33570922005-01-25 22:26:29 +0000303 /* LINTED: avoid warning: dubious operation on enum */
Bram Moolenaar98921892016-02-23 17:14:37 +0100304# if GTK_CHECK_VERSION(3,0,0)
305 g_signal_handlers_disconnect_by_func(G_OBJECT(gui.mainwin),
Bram Moolenaard47d8372016-09-09 22:13:24 +0200306 FUNC2GENERIC(mainwin_event_cb),
Bram Moolenaar98921892016-02-23 17:14:37 +0100307 beval);
308# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000309 gtk_signal_disconnect_by_func((GtkObject*)(gui.mainwin),
310 GTK_SIGNAL_FUNC(mainwin_event_cb),
311 beval);
Bram Moolenaar98921892016-02-23 17:14:37 +0100312# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000313 }
314}
315
316 static gint
317target_event_cb(GtkWidget *widget, GdkEvent *event, gpointer data)
318{
319 BalloonEval *beval = (BalloonEval *)data;
320
321 switch (event->type)
322 {
323 case GDK_ENTER_NOTIFY:
324 pointer_event(beval, (int)event->crossing.x,
325 (int)event->crossing.y,
326 event->crossing.state);
327 break;
328 case GDK_MOTION_NOTIFY:
329 if (event->motion.is_hint)
330 {
331 int x;
332 int y;
333 GdkModifierType state;
334 /*
335 * GDK_POINTER_MOTION_HINT_MASK is set, thus we cannot obtain
336 * the coordinates from the GdkEventMotion struct directly.
337 */
Bram Moolenaar98921892016-02-23 17:14:37 +0100338# if GTK_CHECK_VERSION(3,0,0)
339 {
340 GdkWindow * const win = gtk_widget_get_window(widget);
341 GdkDisplay * const dpy = gdk_window_get_display(win);
Bram Moolenaar30e12d22016-04-17 20:49:53 +0200342# if GTK_CHECK_VERSION(3,20,0)
343 GdkSeat * const seat = gdk_display_get_default_seat(dpy);
344 GdkDevice * const dev = gdk_seat_get_pointer(seat);
345# else
Bram Moolenaar98921892016-02-23 17:14:37 +0100346 GdkDeviceManager * const mngr = gdk_display_get_device_manager(dpy);
347 GdkDevice * const dev = gdk_device_manager_get_client_pointer(mngr);
Bram Moolenaar30e12d22016-04-17 20:49:53 +0200348# endif
Bram Moolenaar98921892016-02-23 17:14:37 +0100349 gdk_window_get_device_position(win, dev , &x, &y, &state);
350 }
351# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000352 gdk_window_get_pointer(widget->window, &x, &y, &state);
Bram Moolenaar98921892016-02-23 17:14:37 +0100353# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000354 pointer_event(beval, x, y, (unsigned int)state);
355 }
356 else
357 {
358 pointer_event(beval, (int)event->motion.x,
359 (int)event->motion.y,
360 event->motion.state);
361 }
362 break;
363 case GDK_LEAVE_NOTIFY:
364 /*
365 * Ignore LeaveNotify events that are not "normal".
366 * Apparently we also get it when somebody else grabs focus.
367 */
368 if (event->crossing.mode == GDK_CROSSING_NORMAL)
369 cancelBalloon(beval);
370 break;
371 case GDK_BUTTON_PRESS:
Bram Moolenaar071d4272004-06-13 20:20:40 +0000372 case GDK_SCROLL:
Bram Moolenaar071d4272004-06-13 20:20:40 +0000373 cancelBalloon(beval);
374 break;
375 case GDK_KEY_PRESS:
376 key_event(beval, event->key.keyval, TRUE);
377 break;
378 case GDK_KEY_RELEASE:
379 key_event(beval, event->key.keyval, FALSE);
380 break;
381 default:
382 break;
383 }
384
385 return FALSE; /* continue emission */
386}
387
Bram Moolenaar071d4272004-06-13 20:20:40 +0000388 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +0000389mainwin_event_cb(GtkWidget *widget UNUSED, GdkEvent *event, gpointer data)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000390{
391 BalloonEval *beval = (BalloonEval *)data;
392
393 switch (event->type)
394 {
395 case GDK_KEY_PRESS:
396 key_event(beval, event->key.keyval, TRUE);
397 break;
398 case GDK_KEY_RELEASE:
399 key_event(beval, event->key.keyval, FALSE);
400 break;
401 default:
402 break;
403 }
404
405 return FALSE; /* continue emission */
406}
407
408 static void
409pointer_event(BalloonEval *beval, int x, int y, unsigned state)
410{
411 int distance;
412
413 distance = ABS(x - beval->x) + ABS(y - beval->y);
414
415 if (distance > 4)
416 {
417 /*
418 * Moved out of the balloon location: cancel it.
419 * Remember button state
420 */
421 beval->state = state;
422 cancelBalloon(beval);
423
424 /* Mouse buttons are pressed - no balloon now */
425 if (!(state & ((int)GDK_BUTTON1_MASK | (int)GDK_BUTTON2_MASK
426 | (int)GDK_BUTTON3_MASK)))
427 {
428 beval->x = x;
429 beval->y = y;
430
431 if (state & (int)GDK_MOD1_MASK)
432 {
433 /*
434 * Alt is pressed -- enter super-evaluate-mode,
435 * where there is no time delay
436 */
437 if (beval->msgCB != NULL)
438 {
439 beval->showState = ShS_PENDING;
440 (*beval->msgCB)(beval, state);
441 }
442 }
443 else
444 {
Bram Moolenaar98921892016-02-23 17:14:37 +0100445# if GTK_CHECK_VERSION(3,0,0)
446 beval->timerID = g_timeout_add((guint)p_bdlay,
447 &timeout_cb, beval);
448# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000449 beval->timerID = gtk_timeout_add((guint32)p_bdlay,
450 &timeout_cb, beval);
Bram Moolenaar98921892016-02-23 17:14:37 +0100451# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000452 }
453 }
454 }
455}
456
457 static void
458key_event(BalloonEval *beval, unsigned keyval, int is_keypress)
459{
460 if (beval->showState == ShS_SHOWING && beval->msgCB != NULL)
461 {
462 switch (keyval)
463 {
464 case GDK_Shift_L:
465 case GDK_Shift_R:
466 beval->showState = ShS_UPDATE_PENDING;
467 (*beval->msgCB)(beval, (is_keypress)
468 ? (int)GDK_SHIFT_MASK : 0);
469 break;
470 case GDK_Control_L:
471 case GDK_Control_R:
472 beval->showState = ShS_UPDATE_PENDING;
473 (*beval->msgCB)(beval, (is_keypress)
474 ? (int)GDK_CONTROL_MASK : 0);
475 break;
476 default:
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000477 /* Don't do this for key release, we apparently get these with
478 * focus changes in some GTK version. */
479 if (is_keypress)
480 cancelBalloon(beval);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000481 break;
482 }
483 }
484 else
485 cancelBalloon(beval);
486}
487
Bram Moolenaar98921892016-02-23 17:14:37 +0100488# if GTK_CHECK_VERSION(3,0,0)
489 static gboolean
490# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000491 static gint
Bram Moolenaar98921892016-02-23 17:14:37 +0100492# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000493timeout_cb(gpointer data)
494{
495 BalloonEval *beval = (BalloonEval *)data;
496
497 beval->timerID = 0;
498 /*
499 * If the timer event happens then the mouse has stopped long enough for
500 * a request to be started. The request will only send to the debugger if
501 * there the mouse is pointing at real data.
502 */
503 requestBalloon(beval);
504
505 return FALSE; /* don't call me again */
506}
507
Bram Moolenaar98921892016-02-23 17:14:37 +0100508# if GTK_CHECK_VERSION(3,0,0)
509 static gboolean
510balloon_draw_event_cb(GtkWidget *widget,
511 cairo_t *cr,
512 gpointer data UNUSED)
513{
514 GtkStyleContext *context = NULL;
515 gint width = -1, height = -1;
516
517 if (widget == NULL)
518 return TRUE;
519
520 context = gtk_widget_get_style_context(widget);
521 width = gtk_widget_get_allocated_width(widget);
522 height = gtk_widget_get_allocated_height(widget);
523
524 gtk_style_context_save(context);
525
526 gtk_style_context_add_class(context, "tooltip");
527 gtk_style_context_set_state(context, GTK_STATE_FLAG_NORMAL);
528
529 cairo_save(cr);
530 gtk_render_frame(context, cr, 0, 0, width, height);
531 gtk_render_background(context, cr, 0, 0, width, height);
532 cairo_restore(cr);
533
534 gtk_style_context_restore(context);
535
536 return FALSE;
537}
538# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000539 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +0000540balloon_expose_event_cb(GtkWidget *widget,
541 GdkEventExpose *event,
542 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000543{
544 gtk_paint_flat_box(widget->style, widget->window,
545 GTK_STATE_NORMAL, GTK_SHADOW_OUT,
546 &event->area, widget, "tooltip",
547 0, 0, -1, -1);
548
549 return FALSE; /* continue emission */
550}
Bram Moolenaar98921892016-02-23 17:14:37 +0100551# endif /* !GTK_CHECK_VERSION(3,0,0) */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000552
Bram Moolenaar071d4272004-06-13 20:20:40 +0000553#else /* !FEAT_GUI_GTK */
554
555 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100556addEventHandler(Widget target, BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000557{
558 XtAddEventHandler(target,
559 PointerMotionMask | EnterWindowMask |
560 LeaveWindowMask | ButtonPressMask | KeyPressMask |
561 KeyReleaseMask,
562 False,
563 pointerEventEH, (XtPointer)beval);
564}
565
566 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100567removeEventHandler(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000568{
569 XtRemoveEventHandler(beval->target,
570 PointerMotionMask | EnterWindowMask |
571 LeaveWindowMask | ButtonPressMask | KeyPressMask |
572 KeyReleaseMask,
573 False,
574 pointerEventEH, (XtPointer)beval);
575}
576
577
578/*
579 * The X event handler. All it does is call the real event handler.
580 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000581 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100582pointerEventEH(
583 Widget w UNUSED,
584 XtPointer client_data,
585 XEvent *event,
586 Boolean *unused UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000587{
588 BalloonEval *beval = (BalloonEval *)client_data;
589 pointerEvent(beval, event);
590}
591
592
593/*
594 * The real event handler. Called by pointerEventEH() whenever an event we are
Bram Moolenaarbae0c162007-05-10 19:30:25 +0000595 * interested in occurs.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000596 */
597
598 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100599pointerEvent(BalloonEval *beval, XEvent *event)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000600{
Bram Moolenaar84a05ac2013-05-06 04:24:17 +0200601 Position distance; /* a measure of how much the pointer moved */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000602 Position delta; /* used to compute distance */
603
604 switch (event->type)
605 {
606 case EnterNotify:
607 case MotionNotify:
608 delta = event->xmotion.x - beval->x;
609 if (delta < 0)
610 delta = -delta;
611 distance = delta;
612 delta = event->xmotion.y - beval->y;
613 if (delta < 0)
614 delta = -delta;
615 distance += delta;
616 if (distance > 4)
617 {
618 /*
619 * Moved out of the balloon location: cancel it.
620 * Remember button state
621 */
622 beval->state = event->xmotion.state;
623 if (beval->state & (Button1Mask|Button2Mask|Button3Mask))
624 {
625 /* Mouse buttons are pressed - no balloon now */
626 cancelBalloon(beval);
627 }
628 else if (beval->state & (Mod1Mask|Mod2Mask|Mod3Mask))
629 {
630 /*
631 * Alt is pressed -- enter super-evaluate-mode,
632 * where there is no time delay
633 */
634 beval->x = event->xmotion.x;
635 beval->y = event->xmotion.y;
636 beval->x_root = event->xmotion.x_root;
637 beval->y_root = event->xmotion.y_root;
638 cancelBalloon(beval);
639 if (beval->msgCB != NULL)
640 {
641 beval->showState = ShS_PENDING;
642 (*beval->msgCB)(beval, beval->state);
643 }
644 }
645 else
646 {
647 beval->x = event->xmotion.x;
648 beval->y = event->xmotion.y;
649 beval->x_root = event->xmotion.x_root;
650 beval->y_root = event->xmotion.y_root;
651 cancelBalloon(beval);
652 beval->timerID = XtAppAddTimeOut( beval->appContext,
653 (long_u)p_bdlay, timerRoutine, beval);
654 }
655 }
656 break;
657
658 /*
659 * Motif and Athena version: Keystrokes will be caught by the
660 * "textArea" widget, and handled in gui_x11_key_hit_cb().
661 */
662 case KeyPress:
663 if (beval->showState == ShS_SHOWING && beval->msgCB != NULL)
664 {
665 Modifiers modifier;
666 KeySym keysym;
667
668 XtTranslateKeycode(gui.dpy,
669 event->xkey.keycode, event->xkey.state,
670 &modifier, &keysym);
671 if (keysym == XK_Shift_L || keysym == XK_Shift_R)
672 {
673 beval->showState = ShS_UPDATE_PENDING;
674 (*beval->msgCB)(beval, ShiftMask);
675 }
676 else if (keysym == XK_Control_L || keysym == XK_Control_R)
677 {
678 beval->showState = ShS_UPDATE_PENDING;
679 (*beval->msgCB)(beval, ControlMask);
680 }
681 else
682 cancelBalloon(beval);
683 }
684 else
685 cancelBalloon(beval);
686 break;
687
688 case KeyRelease:
689 if (beval->showState == ShS_SHOWING && beval->msgCB != NULL)
690 {
691 Modifiers modifier;
692 KeySym keysym;
693
694 XtTranslateKeycode(gui.dpy, event->xkey.keycode,
695 event->xkey.state, &modifier, &keysym);
696 if ((keysym == XK_Shift_L) || (keysym == XK_Shift_R)) {
697 beval->showState = ShS_UPDATE_PENDING;
698 (*beval->msgCB)(beval, 0);
699 }
700 else if ((keysym == XK_Control_L) || (keysym == XK_Control_R))
701 {
702 beval->showState = ShS_UPDATE_PENDING;
703 (*beval->msgCB)(beval, 0);
704 }
705 else
706 cancelBalloon(beval);
707 }
708 else
709 cancelBalloon(beval);
710 break;
711
712 case LeaveNotify:
713 /* Ignore LeaveNotify events that are not "normal".
714 * Apparently we also get it when somebody else grabs focus.
715 * Happens for me every two seconds (some clipboard tool?) */
716 if (event->xcrossing.mode == NotifyNormal)
717 cancelBalloon(beval);
718 break;
719
720 case ButtonPress:
721 cancelBalloon(beval);
722 break;
723
724 default:
725 break;
726 }
727}
728
Bram Moolenaar071d4272004-06-13 20:20:40 +0000729 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100730timerRoutine(XtPointer dx, XtIntervalId *id UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000731{
732 BalloonEval *beval = (BalloonEval *)dx;
733
734 beval->timerID = (XtIntervalId)NULL;
735
736 /*
737 * If the timer event happens then the mouse has stopped long enough for
738 * a request to be started. The request will only send to the debugger if
739 * there the mouse is pointing at real data.
740 */
741 requestBalloon(beval);
742}
743
744#endif /* !FEAT_GUI_GTK */
745
746 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100747requestBalloon(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000748{
749 if (beval->showState != ShS_PENDING)
750 {
751 /* Determine the beval to display */
752 if (beval->msgCB != NULL)
753 {
754 beval->showState = ShS_PENDING;
755 (*beval->msgCB)(beval, beval->state);
756 }
757 else if (beval->msg != NULL)
758 drawBalloon(beval);
759 }
760}
761
762#ifdef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +0000763/*
764 * Convert the string to UTF-8 if 'encoding' is not "utf-8".
765 * Replace any non-printable characters and invalid bytes sequences with
766 * "^X" or "<xx>" escapes, and apply SpecialKey highlighting to them.
767 * TAB and NL are passed through unscathed.
768 */
Bram Moolenaar182c5be2010-06-25 05:37:59 +0200769# define IS_NONPRINTABLE(c) (((c) < 0x20 && (c) != TAB && (c) != NL) \
Bram Moolenaar071d4272004-06-13 20:20:40 +0000770 || (c) == DEL)
771 static void
Bram Moolenaar89d40322006-08-29 15:30:07 +0000772set_printable_label_text(GtkLabel *label, char_u *text)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000773{
774 char_u *convbuf = NULL;
775 char_u *buf;
776 char_u *p;
777 char_u *pdest;
778 unsigned int len;
779 int charlen;
780 int uc;
781 PangoAttrList *attr_list;
782
783 /* Convert to UTF-8 if it isn't already */
784 if (output_conv.vc_type != CONV_NONE)
785 {
Bram Moolenaar89d40322006-08-29 15:30:07 +0000786 convbuf = string_convert(&output_conv, text, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000787 if (convbuf != NULL)
Bram Moolenaar89d40322006-08-29 15:30:07 +0000788 text = convbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000789 }
790
791 /* First let's see how much we need to allocate */
792 len = 0;
Bram Moolenaar89d40322006-08-29 15:30:07 +0000793 for (p = text; *p != NUL; p += charlen)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000794 {
795 if ((*p & 0x80) == 0) /* be quick for ASCII */
796 {
797 charlen = 1;
798 len += IS_NONPRINTABLE(*p) ? 2 : 1; /* nonprintable: ^X */
799 }
800 else
801 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000802 charlen = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000803 uc = utf_ptr2char(p);
804
805 if (charlen != utf_char2len(uc))
806 charlen = 1; /* reject overlong sequences */
807
808 if (charlen == 1 || uc < 0xa0) /* illegal byte or */
809 len += 4; /* control char: <xx> */
810 else if (!utf_printable(uc))
811 /* Note: we assume here that utf_printable() doesn't
812 * care about characters outside the BMP. */
813 len += 6; /* nonprintable: <xxxx> */
814 else
815 len += charlen;
816 }
817 }
818
819 attr_list = pango_attr_list_new();
820 buf = alloc(len + 1);
821
822 /* Now go for the real work */
823 if (buf != NULL)
824 {
825 attrentry_T *aep;
826 PangoAttribute *attr;
827 guicolor_T pixel;
Bram Moolenaar36edf062016-07-21 22:10:12 +0200828#if GTK_CHECK_VERSION(3,0,0)
829 GdkRGBA color = { 0.0, 0.0, 0.0, 1.0 };
Bram Moolenaar870b7492016-07-22 22:26:52 +0200830# if PANGO_VERSION_CHECK(1,38,0)
Bram Moolenaar36edf062016-07-21 22:10:12 +0200831 PangoAttribute *attr_alpha;
Bram Moolenaar870b7492016-07-22 22:26:52 +0200832# endif
Bram Moolenaar36edf062016-07-21 22:10:12 +0200833#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000834 GdkColor color = { 0, 0, 0, 0 };
Bram Moolenaar36edf062016-07-21 22:10:12 +0200835#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000836
837 /* Look up the RGB values of the SpecialKey foreground color. */
Bram Moolenaar8820b482017-03-16 17:23:31 +0100838 aep = syn_gui_attr2entry(HL_ATTR(HLF_8));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000839 pixel = (aep != NULL) ? aep->ae_u.gui.fg_color : INVALCOLOR;
840 if (pixel != INVALCOLOR)
Bram Moolenaar98921892016-02-23 17:14:37 +0100841# if GTK_CHECK_VERSION(3,0,0)
842 {
Bram Moolenaar36edf062016-07-21 22:10:12 +0200843 color.red = ((pixel & 0xff0000) >> 16) / 255.0;
844 color.green = ((pixel & 0xff00) >> 8) / 255.0;
845 color.blue = (pixel & 0xff) / 255.0;
846 color.alpha = 1.0;
Bram Moolenaar98921892016-02-23 17:14:37 +0100847 }
848# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000849 gdk_colormap_query_color(gtk_widget_get_colormap(gui.drawarea),
850 (unsigned long)pixel, &color);
Bram Moolenaar98921892016-02-23 17:14:37 +0100851# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000852
853 pdest = buf;
Bram Moolenaar89d40322006-08-29 15:30:07 +0000854 p = text;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000855 while (*p != NUL)
856 {
857 /* Be quick for ASCII */
858 if ((*p & 0x80) == 0 && !IS_NONPRINTABLE(*p))
859 {
860 *pdest++ = *p++;
861 }
862 else
863 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000864 charlen = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000865 uc = utf_ptr2char(p);
866
867 if (charlen != utf_char2len(uc))
868 charlen = 1; /* reject overlong sequences */
869
870 if (charlen == 1 || uc < 0xa0 || !utf_printable(uc))
871 {
872 int outlen;
873
874 /* Careful: we can't just use transchar_byte() here,
875 * since 'encoding' is not necessarily set to "utf-8". */
876 if (*p & 0x80 && charlen == 1)
877 {
878 transchar_hex(pdest, *p); /* <xx> */
879 outlen = 4;
880 }
881 else if (uc >= 0x80)
882 {
883 /* Note: we assume here that utf_printable() doesn't
884 * care about characters outside the BMP. */
885 transchar_hex(pdest, uc); /* <xx> or <xxxx> */
886 outlen = (uc < 0x100) ? 4 : 6;
887 }
888 else
889 {
890 transchar_nonprint(pdest, *p); /* ^X */
891 outlen = 2;
892 }
893 if (pixel != INVALCOLOR)
894 {
Bram Moolenaar36edf062016-07-21 22:10:12 +0200895#if GTK_CHECK_VERSION(3,0,0)
896# define DOUBLE2UINT16(val) ((guint16)((val) * 65535 + 0.5))
897 attr = pango_attr_foreground_new(
898 DOUBLE2UINT16(color.red),
899 DOUBLE2UINT16(color.green),
900 DOUBLE2UINT16(color.blue));
Bram Moolenaar870b7492016-07-22 22:26:52 +0200901# if PANGO_VERSION_CHECK(1,38,0)
Bram Moolenaar36edf062016-07-21 22:10:12 +0200902 attr_alpha = pango_attr_foreground_alpha_new(
903 DOUBLE2UINT16(color.alpha));
Bram Moolenaar870b7492016-07-22 22:26:52 +0200904# endif
Bram Moolenaar36edf062016-07-21 22:10:12 +0200905# undef DOUBLE2UINT16
906#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000907 attr = pango_attr_foreground_new(
908 color.red, color.green, color.blue);
Bram Moolenaar36edf062016-07-21 22:10:12 +0200909#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000910 attr->start_index = pdest - buf;
911 attr->end_index = pdest - buf + outlen;
912 pango_attr_list_insert(attr_list, attr);
Bram Moolenaar36edf062016-07-21 22:10:12 +0200913#if GTK_CHECK_VERSION(3,0,0)
Bram Moolenaar870b7492016-07-22 22:26:52 +0200914# if PANGO_VERSION_CHECK(1,38,0)
Bram Moolenaar36edf062016-07-21 22:10:12 +0200915 attr_alpha->start_index = pdest - buf;
916 attr_alpha->end_index = pdest - buf + outlen;
917 pango_attr_list_insert(attr_list, attr_alpha);
Bram Moolenaar870b7492016-07-22 22:26:52 +0200918# endif
Bram Moolenaar36edf062016-07-21 22:10:12 +0200919#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000920 }
921 pdest += outlen;
922 p += charlen;
923 }
924 else
925 {
926 do
927 *pdest++ = *p++;
928 while (--charlen != 0);
929 }
930 }
931 }
932 *pdest = NUL;
933 }
934
935 vim_free(convbuf);
936
937 gtk_label_set_text(label, (const char *)buf);
938 vim_free(buf);
939
940 gtk_label_set_attributes(label, attr_list);
941 pango_attr_list_unref(attr_list);
942}
Bram Moolenaar182c5be2010-06-25 05:37:59 +0200943# undef IS_NONPRINTABLE
Bram Moolenaar071d4272004-06-13 20:20:40 +0000944
945/*
946 * Draw a balloon.
947 */
948 static void
949drawBalloon(BalloonEval *beval)
950{
951 if (beval->msg != NULL)
952 {
953 GtkRequisition requisition;
954 int screen_w;
955 int screen_h;
956 int x;
957 int y;
958 int x_offset = EVAL_OFFSET_X;
959 int y_offset = EVAL_OFFSET_Y;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000960 PangoLayout *layout;
Bram Moolenaara859f042016-11-17 19:11:55 +0100961
Bram Moolenaar7be9b502017-09-09 18:45:26 +0200962# if !GTK_CHECK_VERSION(3,22,2)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000963 GdkScreen *screen;
964
965 screen = gtk_widget_get_screen(beval->target);
966 gtk_window_set_screen(GTK_WINDOW(beval->balloonShell), screen);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000967# endif
Bram Moolenaar7be9b502017-09-09 18:45:26 +0200968 gui_gtk_get_screen_size_of_win(beval->balloonShell,
969 &screen_w, &screen_h);
Bram Moolenaar98921892016-02-23 17:14:37 +0100970# if !GTK_CHECK_VERSION(3,0,0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000971 gtk_widget_ensure_style(beval->balloonShell);
972 gtk_widget_ensure_style(beval->balloonLabel);
Bram Moolenaar98921892016-02-23 17:14:37 +0100973# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000974
Bram Moolenaar071d4272004-06-13 20:20:40 +0000975 set_printable_label_text(GTK_LABEL(beval->balloonLabel), beval->msg);
976 /*
977 * Dirty trick: Enable wrapping mode on the label's layout behind its
978 * back. This way GtkLabel won't try to constrain the wrap width to a
979 * builtin maximum value of about 65 Latin characters.
980 */
981 layout = gtk_label_get_layout(GTK_LABEL(beval->balloonLabel));
Bram Moolenaar182c5be2010-06-25 05:37:59 +0200982# ifdef PANGO_WRAP_WORD_CHAR
Bram Moolenaar071d4272004-06-13 20:20:40 +0000983 pango_layout_set_wrap(layout, PANGO_WRAP_WORD_CHAR);
Bram Moolenaar182c5be2010-06-25 05:37:59 +0200984# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000985 pango_layout_set_wrap(layout, PANGO_WRAP_WORD);
Bram Moolenaar182c5be2010-06-25 05:37:59 +0200986# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000987 pango_layout_set_width(layout,
988 /* try to come up with some reasonable width */
989 PANGO_SCALE * CLAMP(gui.num_cols * gui.char_width,
990 screen_w / 2,
991 MAX(20, screen_w - 20)));
992
993 /* Calculate the balloon's width and height. */
Bram Moolenaar98921892016-02-23 17:14:37 +0100994# if GTK_CHECK_VERSION(3,0,0)
995 gtk_widget_get_preferred_size(beval->balloonShell, &requisition, NULL);
996# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000997 gtk_widget_size_request(beval->balloonShell, &requisition);
Bram Moolenaar98921892016-02-23 17:14:37 +0100998# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000999
1000 /* Compute position of the balloon area */
Bram Moolenaar98921892016-02-23 17:14:37 +01001001# if GTK_CHECK_VERSION(3,0,0)
1002 gdk_window_get_origin(gtk_widget_get_window(beval->target), &x, &y);
1003# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001004 gdk_window_get_origin(beval->target->window, &x, &y);
Bram Moolenaar98921892016-02-23 17:14:37 +01001005# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001006 x += beval->x;
1007 y += beval->y;
1008
1009 /* Get out of the way of the mouse pointer */
1010 if (x + x_offset + requisition.width > screen_w)
1011 y_offset += 15;
1012 if (y + y_offset + requisition.height > screen_h)
1013 y_offset = -requisition.height - EVAL_OFFSET_Y;
1014
1015 /* Sanitize values */
1016 x = CLAMP(x + x_offset, 0, MAX(0, screen_w - requisition.width));
1017 y = CLAMP(y + y_offset, 0, MAX(0, screen_h - requisition.height));
1018
1019 /* Show the balloon */
Bram Moolenaar98921892016-02-23 17:14:37 +01001020# if GTK_CHECK_VERSION(3,0,0)
1021 gtk_window_move(GTK_WINDOW(beval->balloonShell), x, y);
1022# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001023 gtk_widget_set_uposition(beval->balloonShell, x, y);
Bram Moolenaar98921892016-02-23 17:14:37 +01001024# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001025 gtk_widget_show(beval->balloonShell);
1026
1027 beval->showState = ShS_SHOWING;
1028 }
1029}
1030
1031/*
1032 * Undraw a balloon.
1033 */
1034 static void
1035undrawBalloon(BalloonEval *beval)
1036{
1037 if (beval->balloonShell != NULL)
1038 gtk_widget_hide(beval->balloonShell);
1039 beval->showState = ShS_NEUTRAL;
1040}
1041
1042 static void
1043cancelBalloon(BalloonEval *beval)
1044{
1045 if (beval->showState == ShS_SHOWING
1046 || beval->showState == ShS_UPDATE_PENDING)
1047 undrawBalloon(beval);
1048
1049 if (beval->timerID != 0)
1050 {
Bram Moolenaar98921892016-02-23 17:14:37 +01001051# if GTK_CHECK_VERSION(3,0,0)
1052 g_source_remove(beval->timerID);
1053# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001054 gtk_timeout_remove(beval->timerID);
Bram Moolenaar98921892016-02-23 17:14:37 +01001055# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001056 beval->timerID = 0;
1057 }
1058 beval->showState = ShS_NEUTRAL;
1059}
1060
1061 static void
1062createBalloonEvalWindow(BalloonEval *beval)
1063{
1064 beval->balloonShell = gtk_window_new(GTK_WINDOW_POPUP);
1065
1066 gtk_widget_set_app_paintable(beval->balloonShell, TRUE);
Bram Moolenaar98921892016-02-23 17:14:37 +01001067# if GTK_CHECK_VERSION(3,0,0)
1068 gtk_window_set_resizable(GTK_WINDOW(beval->balloonShell), FALSE);
1069# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001070 gtk_window_set_policy(GTK_WINDOW(beval->balloonShell), FALSE, FALSE, TRUE);
Bram Moolenaar98921892016-02-23 17:14:37 +01001071# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001072 gtk_widget_set_name(beval->balloonShell, "gtk-tooltips");
Bram Moolenaar98921892016-02-23 17:14:37 +01001073# if GTK_CHECK_VERSION(3,0,0)
1074 gtk_container_set_border_width(GTK_CONTAINER(beval->balloonShell), 4);
1075# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001076 gtk_container_border_width(GTK_CONTAINER(beval->balloonShell), 4);
Bram Moolenaar98921892016-02-23 17:14:37 +01001077# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001078
Bram Moolenaar98921892016-02-23 17:14:37 +01001079# if GTK_CHECK_VERSION(3,0,0)
1080 g_signal_connect(G_OBJECT(beval->balloonShell), "draw",
1081 G_CALLBACK(balloon_draw_event_cb), NULL);
1082# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001083 gtk_signal_connect((GtkObject*)(beval->balloonShell), "expose_event",
1084 GTK_SIGNAL_FUNC(balloon_expose_event_cb), NULL);
Bram Moolenaar98921892016-02-23 17:14:37 +01001085# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001086 beval->balloonLabel = gtk_label_new(NULL);
1087
1088 gtk_label_set_line_wrap(GTK_LABEL(beval->balloonLabel), FALSE);
1089 gtk_label_set_justify(GTK_LABEL(beval->balloonLabel), GTK_JUSTIFY_LEFT);
Bram Moolenaar98921892016-02-23 17:14:37 +01001090# if GTK_CHECK_VERSION(3,16,0)
1091 gtk_label_set_xalign(GTK_LABEL(beval->balloonLabel), 0.5);
1092 gtk_label_set_yalign(GTK_LABEL(beval->balloonLabel), 0.5);
1093# elif GTK_CHECK_VERSION(3,14,0)
1094 GValue align_val = G_VALUE_INIT;
1095 g_value_init(&align_val, G_TYPE_FLOAT);
1096 g_value_set_float(&align_val, 0.5);
1097 g_object_set_property(G_OBJECT(beval->balloonLabel), "xalign", &align_val);
1098 g_object_set_property(G_OBJECT(beval->balloonLabel), "yalign", &align_val);
1099 g_value_unset(&align_val);
1100# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001101 gtk_misc_set_alignment(GTK_MISC(beval->balloonLabel), 0.5f, 0.5f);
Bram Moolenaar98921892016-02-23 17:14:37 +01001102# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001103 gtk_widget_set_name(beval->balloonLabel, "vim-balloon-label");
1104 gtk_widget_show(beval->balloonLabel);
1105
1106 gtk_container_add(GTK_CONTAINER(beval->balloonShell), beval->balloonLabel);
1107}
1108
1109#else /* !FEAT_GUI_GTK */
1110
1111/*
1112 * Draw a balloon.
1113 */
1114 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001115drawBalloon(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001116{
1117 Dimension w;
1118 Dimension h;
1119 Position tx;
1120 Position ty;
1121
1122 if (beval->msg != NULL)
1123 {
1124 /* Show the Balloon */
1125
1126 /* Calculate the label's width and height */
1127#ifdef FEAT_GUI_MOTIF
1128 XmString s;
1129
1130 /* For the callback function we parse NL characters to create a
1131 * multi-line label. This doesn't work for all languages, but
1132 * XmStringCreateLocalized() doesn't do multi-line labels... */
1133 if (beval->msgCB != NULL)
1134 s = XmStringCreateLtoR((char *)beval->msg, XmFONTLIST_DEFAULT_TAG);
1135 else
1136 s = XmStringCreateLocalized((char *)beval->msg);
1137 {
1138 XmFontList fl;
1139
1140 fl = gui_motif_fontset2fontlist(&gui.tooltip_fontset);
Bram Moolenaardb5ffaa2014-06-25 17:44:49 +02001141 if (fl == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001142 {
Bram Moolenaardb5ffaa2014-06-25 17:44:49 +02001143 XmStringFree(s);
1144 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001145 }
Bram Moolenaardb5ffaa2014-06-25 17:44:49 +02001146 XmStringExtent(fl, s, &w, &h);
1147 XmFontListFree(fl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001148 }
1149 w += gui.border_offset << 1;
1150 h += gui.border_offset << 1;
1151 XtVaSetValues(beval->balloonLabel, XmNlabelString, s, NULL);
1152 XmStringFree(s);
1153#else /* Athena */
1154 /* Assume XtNinternational == True */
1155 XFontSet fset;
1156 XFontSetExtents *ext;
1157
1158 XtVaGetValues(beval->balloonLabel, XtNfontSet, &fset, NULL);
1159 ext = XExtentsOfFontSet(fset);
1160 h = ext->max_ink_extent.height;
1161 w = XmbTextEscapement(fset,
1162 (char *)beval->msg,
1163 (int)STRLEN(beval->msg));
1164 w += gui.border_offset << 1;
1165 h += gui.border_offset << 1;
1166 XtVaSetValues(beval->balloonLabel, XtNlabel, beval->msg, NULL);
1167#endif
1168
1169 /* Compute position of the balloon area */
1170 tx = beval->x_root + EVAL_OFFSET_X;
1171 ty = beval->y_root + EVAL_OFFSET_Y;
1172 if ((tx + w) > beval->screen_width)
1173 tx = beval->screen_width - w;
1174 if ((ty + h) > beval->screen_height)
1175 ty = beval->screen_height - h;
1176#ifdef FEAT_GUI_MOTIF
1177 XtVaSetValues(beval->balloonShell,
1178 XmNx, tx,
1179 XmNy, ty,
1180 NULL);
1181#else
1182 /* Athena */
1183 XtVaSetValues(beval->balloonShell,
1184 XtNx, tx,
1185 XtNy, ty,
1186 NULL);
1187#endif
Bram Moolenaar8281f442009-03-18 11:22:25 +00001188 /* Set tooltip colors */
1189 {
1190 Arg args[2];
1191
1192#ifdef FEAT_GUI_MOTIF
1193 args[0].name = XmNbackground;
1194 args[0].value = gui.tooltip_bg_pixel;
1195 args[1].name = XmNforeground;
1196 args[1].value = gui.tooltip_fg_pixel;
1197#else /* Athena */
1198 args[0].name = XtNbackground;
1199 args[0].value = gui.tooltip_bg_pixel;
1200 args[1].name = XtNforeground;
1201 args[1].value = gui.tooltip_fg_pixel;
1202#endif
1203 XtSetValues(beval->balloonLabel, &args[0], XtNumber(args));
1204 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001205
1206 XtPopup(beval->balloonShell, XtGrabNone);
1207
1208 beval->showState = ShS_SHOWING;
1209
1210 current_beval = beval;
1211 }
1212}
1213
1214/*
1215 * Undraw a balloon.
1216 */
1217 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001218undrawBalloon(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001219{
1220 if (beval->balloonShell != (Widget)0)
1221 XtPopdown(beval->balloonShell);
1222 beval->showState = ShS_NEUTRAL;
1223
1224 current_beval = NULL;
1225}
1226
1227 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001228cancelBalloon(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001229{
1230 if (beval->showState == ShS_SHOWING
1231 || beval->showState == ShS_UPDATE_PENDING)
1232 undrawBalloon(beval);
1233
1234 if (beval->timerID != (XtIntervalId)NULL)
1235 {
1236 XtRemoveTimeOut(beval->timerID);
1237 beval->timerID = (XtIntervalId)NULL;
1238 }
1239 beval->showState = ShS_NEUTRAL;
1240}
1241
1242
1243 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001244createBalloonEvalWindow(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001245{
1246 Arg args[12];
1247 int n;
1248
1249 n = 0;
1250#ifdef FEAT_GUI_MOTIF
1251 XtSetArg(args[n], XmNallowShellResize, True); n++;
1252 beval->balloonShell = XtAppCreateShell("balloonEval", "BalloonEval",
1253 overrideShellWidgetClass, gui.dpy, args, n);
1254#else
1255 /* Athena */
1256 XtSetArg(args[n], XtNallowShellResize, True); n++;
1257 beval->balloonShell = XtAppCreateShell("balloonEval", "BalloonEval",
1258 overrideShellWidgetClass, gui.dpy, args, n);
1259#endif
1260
1261 n = 0;
1262#ifdef FEAT_GUI_MOTIF
1263 {
1264 XmFontList fl;
1265
1266 fl = gui_motif_fontset2fontlist(&gui.tooltip_fontset);
1267 XtSetArg(args[n], XmNforeground, gui.tooltip_fg_pixel); n++;
1268 XtSetArg(args[n], XmNbackground, gui.tooltip_bg_pixel); n++;
1269 XtSetArg(args[n], XmNfontList, fl); n++;
1270 XtSetArg(args[n], XmNalignment, XmALIGNMENT_BEGINNING); n++;
1271 beval->balloonLabel = XtCreateManagedWidget("balloonLabel",
1272 xmLabelWidgetClass, beval->balloonShell, args, n);
1273 }
1274#else /* FEAT_GUI_ATHENA */
1275 XtSetArg(args[n], XtNforeground, gui.tooltip_fg_pixel); n++;
1276 XtSetArg(args[n], XtNbackground, gui.tooltip_bg_pixel); n++;
1277 XtSetArg(args[n], XtNinternational, True); n++;
1278 XtSetArg(args[n], XtNfontSet, gui.tooltip_fontset); n++;
1279 beval->balloonLabel = XtCreateManagedWidget("balloonLabel",
1280 labelWidgetClass, beval->balloonShell, args, n);
1281#endif
1282}
1283
1284#endif /* !FEAT_GUI_GTK */
1285#endif /* !FEAT_GUI_W32 */
1286
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01001287#endif /* FEAT_BEVAL_GUI */