blob: 49694cfdda5a49175dfe78d6a23cb595e53bed47 [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
Bram Moolenaarca4b6132018-06-28 12:05:11 +0200114 beval = (BalloonEval *)alloc_clear(sizeof(BalloonEval));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000115 if (beval != NULL)
116 {
117#ifdef FEAT_GUI_GTK
118 beval->target = GTK_WIDGET(target);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000119#else
120 beval->target = (Widget)target;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000121 beval->appContext = XtWidgetToApplicationContext((Widget)target);
122#endif
123 beval->showState = ShS_NEUTRAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000124 beval->msg = mesg;
125 beval->msgCB = mesgCB;
126 beval->clientData = clientData;
127
128 /*
129 * Set up event handler which will keep its eyes on the pointer,
130 * and when the pointer rests in a certain spot for a given time
131 * interval, show the beval.
132 */
133 addEventHandler(beval->target, beval);
134 createBalloonEvalWindow(beval);
135
136#ifndef FEAT_GUI_GTK
137 /*
138 * Now create and save the screen width and height. Used in drawing.
139 */
140 display_name = DisplayString(gui.dpy);
141 p = strrchr(display_name, '.');
142 if (p != NULL)
143 screen_num = atoi(++p);
144 else
145 screen_num = 0;
146 beval->screen_width = DisplayWidth(gui.dpy, screen_num);
147 beval->screen_height = DisplayHeight(gui.dpy, screen_num);
148#endif
149 }
150
151 return beval;
152}
153
154#if defined(FEAT_BEVAL_TIP) || defined(PROTO)
155/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +0000156 * Destroy a balloon-eval and free its associated memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000157 */
158 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100159gui_mch_destroy_beval_area(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000160{
161 cancelBalloon(beval);
162 removeEventHandler(beval);
163 /* Children will automatically be destroyed */
164# ifdef FEAT_GUI_GTK
165 gtk_widget_destroy(beval->balloonShell);
166# else
167 XtDestroyWidget(beval->balloonShell);
168# endif
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200169# ifdef FEAT_VARTABS
170 if (beval->vts)
171 vim_free(beval->vts);
172# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000173 vim_free(beval);
174}
175#endif
176
177 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100178gui_mch_enable_beval_area(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000179{
180 if (beval != NULL)
181 addEventHandler(beval->target, beval);
182}
183
184 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100185gui_mch_disable_beval_area(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000186{
187 if (beval != NULL)
188 removeEventHandler(beval);
189}
190
191#if defined(FEAT_BEVAL_TIP) || defined(PROTO)
192/*
193 * This function returns the BalloonEval * associated with the currently
194 * displayed tooltip. Returns NULL if there is no tooltip currently showing.
195 *
196 * Assumption: Only one tooltip can be shown at a time.
197 */
198 BalloonEval *
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100199gui_mch_currently_showing_beval(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000200{
201 return current_beval;
202}
203#endif
204#endif /* !FEAT_GUI_W32 */
205
Bram Moolenaare2ac10d2005-03-07 23:26:06 +0000206#if defined(FEAT_SUN_WORKSHOP) || defined(FEAT_NETBEANS_INTG) \
207 || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000208# if !defined(FEAT_GUI_W32) || defined(PROTO)
209
210/*
211 * Show a balloon with "mesg".
212 */
213 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100214gui_mch_post_balloon(BalloonEval *beval, char_u *mesg)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000215{
216 beval->msg = mesg;
217 if (mesg != NULL)
218 drawBalloon(beval);
219 else
220 undrawBalloon(beval);
221}
Bram Moolenaarc3719bd2017-11-18 22:13:31 +0100222# endif /* !FEAT_GUI_W32 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000223#endif /* FEAT_SUN_WORKSHOP || FEAT_NETBEANS_INTG || PROTO */
224
225#if !defined(FEAT_GUI_W32) || defined(PROTO)
226#if defined(FEAT_BEVAL_TIP) || defined(PROTO)
227/*
228 * Hide the given balloon.
229 */
230 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100231gui_mch_unpost_balloon(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000232{
233 undrawBalloon(beval);
234}
235#endif
236
237#ifdef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +0000238 static void
239addEventHandler(GtkWidget *target, BalloonEval *beval)
240{
241 /*
242 * Connect to the generic "event" signal instead of the individual
243 * signals for each event type, because the former is emitted earlier.
244 * This allows us to catch events independently of the signal handlers
245 * in gui_gtk_x11.c.
246 */
Bram Moolenaar98921892016-02-23 17:14:37 +0100247# if GTK_CHECK_VERSION(3,0,0)
248 g_signal_connect(G_OBJECT(target), "event",
249 G_CALLBACK(target_event_cb),
250 beval);
251# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000252 /* Should use GTK_OBJECT() here, but that causes a lint warning... */
253 gtk_signal_connect((GtkObject*)(target), "event",
254 GTK_SIGNAL_FUNC(target_event_cb),
255 beval);
Bram Moolenaar98921892016-02-23 17:14:37 +0100256# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000257 /*
258 * Nasty: Key press events go to the main window thus the drawing area
259 * will never see them. This means we have to connect to the main window
260 * as well in order to catch those events.
261 */
262 if (gtk_socket_id == 0 && gui.mainwin != NULL
263 && gtk_widget_is_ancestor(target, gui.mainwin))
264 {
Bram Moolenaar98921892016-02-23 17:14:37 +0100265# if GTK_CHECK_VERSION(3,0,0)
266 g_signal_connect(G_OBJECT(gui.mainwin), "event",
267 G_CALLBACK(mainwin_event_cb),
268 beval);
269# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000270 gtk_signal_connect((GtkObject*)(gui.mainwin), "event",
271 GTK_SIGNAL_FUNC(mainwin_event_cb),
272 beval);
Bram Moolenaar98921892016-02-23 17:14:37 +0100273# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000274 }
275}
276
277 static void
278removeEventHandler(BalloonEval *beval)
279{
Bram Moolenaar33570922005-01-25 22:26:29 +0000280 /* LINTED: avoid warning: dubious operation on enum */
Bram Moolenaar98921892016-02-23 17:14:37 +0100281# if GTK_CHECK_VERSION(3,0,0)
282 g_signal_handlers_disconnect_by_func(G_OBJECT(beval->target),
Bram Moolenaard47d8372016-09-09 22:13:24 +0200283 FUNC2GENERIC(target_event_cb),
Bram Moolenaar98921892016-02-23 17:14:37 +0100284 beval);
285# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000286 gtk_signal_disconnect_by_func((GtkObject*)(beval->target),
287 GTK_SIGNAL_FUNC(target_event_cb),
288 beval);
Bram Moolenaar98921892016-02-23 17:14:37 +0100289# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000290
291 if (gtk_socket_id == 0 && gui.mainwin != NULL
292 && gtk_widget_is_ancestor(beval->target, gui.mainwin))
293 {
Bram Moolenaar33570922005-01-25 22:26:29 +0000294 /* LINTED: avoid warning: dubious operation on enum */
Bram Moolenaar98921892016-02-23 17:14:37 +0100295# if GTK_CHECK_VERSION(3,0,0)
296 g_signal_handlers_disconnect_by_func(G_OBJECT(gui.mainwin),
Bram Moolenaard47d8372016-09-09 22:13:24 +0200297 FUNC2GENERIC(mainwin_event_cb),
Bram Moolenaar98921892016-02-23 17:14:37 +0100298 beval);
299# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000300 gtk_signal_disconnect_by_func((GtkObject*)(gui.mainwin),
301 GTK_SIGNAL_FUNC(mainwin_event_cb),
302 beval);
Bram Moolenaar98921892016-02-23 17:14:37 +0100303# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000304 }
305}
306
307 static gint
308target_event_cb(GtkWidget *widget, GdkEvent *event, gpointer data)
309{
310 BalloonEval *beval = (BalloonEval *)data;
311
312 switch (event->type)
313 {
314 case GDK_ENTER_NOTIFY:
315 pointer_event(beval, (int)event->crossing.x,
316 (int)event->crossing.y,
317 event->crossing.state);
318 break;
319 case GDK_MOTION_NOTIFY:
320 if (event->motion.is_hint)
321 {
322 int x;
323 int y;
324 GdkModifierType state;
325 /*
326 * GDK_POINTER_MOTION_HINT_MASK is set, thus we cannot obtain
327 * the coordinates from the GdkEventMotion struct directly.
328 */
Bram Moolenaar98921892016-02-23 17:14:37 +0100329# if GTK_CHECK_VERSION(3,0,0)
330 {
331 GdkWindow * const win = gtk_widget_get_window(widget);
332 GdkDisplay * const dpy = gdk_window_get_display(win);
Bram Moolenaar30e12d22016-04-17 20:49:53 +0200333# if GTK_CHECK_VERSION(3,20,0)
334 GdkSeat * const seat = gdk_display_get_default_seat(dpy);
335 GdkDevice * const dev = gdk_seat_get_pointer(seat);
336# else
Bram Moolenaar98921892016-02-23 17:14:37 +0100337 GdkDeviceManager * const mngr = gdk_display_get_device_manager(dpy);
338 GdkDevice * const dev = gdk_device_manager_get_client_pointer(mngr);
Bram Moolenaar30e12d22016-04-17 20:49:53 +0200339# endif
Bram Moolenaar98921892016-02-23 17:14:37 +0100340 gdk_window_get_device_position(win, dev , &x, &y, &state);
341 }
342# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000343 gdk_window_get_pointer(widget->window, &x, &y, &state);
Bram Moolenaar98921892016-02-23 17:14:37 +0100344# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000345 pointer_event(beval, x, y, (unsigned int)state);
346 }
347 else
348 {
349 pointer_event(beval, (int)event->motion.x,
350 (int)event->motion.y,
351 event->motion.state);
352 }
353 break;
354 case GDK_LEAVE_NOTIFY:
355 /*
356 * Ignore LeaveNotify events that are not "normal".
357 * Apparently we also get it when somebody else grabs focus.
358 */
359 if (event->crossing.mode == GDK_CROSSING_NORMAL)
360 cancelBalloon(beval);
361 break;
362 case GDK_BUTTON_PRESS:
Bram Moolenaar071d4272004-06-13 20:20:40 +0000363 case GDK_SCROLL:
Bram Moolenaar071d4272004-06-13 20:20:40 +0000364 cancelBalloon(beval);
365 break;
366 case GDK_KEY_PRESS:
367 key_event(beval, event->key.keyval, TRUE);
368 break;
369 case GDK_KEY_RELEASE:
370 key_event(beval, event->key.keyval, FALSE);
371 break;
372 default:
373 break;
374 }
375
376 return FALSE; /* continue emission */
377}
378
Bram Moolenaar071d4272004-06-13 20:20:40 +0000379 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +0000380mainwin_event_cb(GtkWidget *widget UNUSED, GdkEvent *event, gpointer data)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000381{
382 BalloonEval *beval = (BalloonEval *)data;
383
384 switch (event->type)
385 {
386 case GDK_KEY_PRESS:
387 key_event(beval, event->key.keyval, TRUE);
388 break;
389 case GDK_KEY_RELEASE:
390 key_event(beval, event->key.keyval, FALSE);
391 break;
392 default:
393 break;
394 }
395
396 return FALSE; /* continue emission */
397}
398
399 static void
400pointer_event(BalloonEval *beval, int x, int y, unsigned state)
401{
402 int distance;
403
404 distance = ABS(x - beval->x) + ABS(y - beval->y);
405
406 if (distance > 4)
407 {
408 /*
409 * Moved out of the balloon location: cancel it.
410 * Remember button state
411 */
412 beval->state = state;
413 cancelBalloon(beval);
414
415 /* Mouse buttons are pressed - no balloon now */
416 if (!(state & ((int)GDK_BUTTON1_MASK | (int)GDK_BUTTON2_MASK
417 | (int)GDK_BUTTON3_MASK)))
418 {
419 beval->x = x;
420 beval->y = y;
421
422 if (state & (int)GDK_MOD1_MASK)
423 {
424 /*
425 * Alt is pressed -- enter super-evaluate-mode,
426 * where there is no time delay
427 */
428 if (beval->msgCB != NULL)
429 {
430 beval->showState = ShS_PENDING;
431 (*beval->msgCB)(beval, state);
432 }
433 }
434 else
435 {
Bram Moolenaar98921892016-02-23 17:14:37 +0100436# if GTK_CHECK_VERSION(3,0,0)
437 beval->timerID = g_timeout_add((guint)p_bdlay,
438 &timeout_cb, beval);
439# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000440 beval->timerID = gtk_timeout_add((guint32)p_bdlay,
441 &timeout_cb, beval);
Bram Moolenaar98921892016-02-23 17:14:37 +0100442# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000443 }
444 }
445 }
446}
447
448 static void
449key_event(BalloonEval *beval, unsigned keyval, int is_keypress)
450{
451 if (beval->showState == ShS_SHOWING && beval->msgCB != NULL)
452 {
453 switch (keyval)
454 {
455 case GDK_Shift_L:
456 case GDK_Shift_R:
457 beval->showState = ShS_UPDATE_PENDING;
458 (*beval->msgCB)(beval, (is_keypress)
459 ? (int)GDK_SHIFT_MASK : 0);
460 break;
461 case GDK_Control_L:
462 case GDK_Control_R:
463 beval->showState = ShS_UPDATE_PENDING;
464 (*beval->msgCB)(beval, (is_keypress)
465 ? (int)GDK_CONTROL_MASK : 0);
466 break;
467 default:
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000468 /* Don't do this for key release, we apparently get these with
469 * focus changes in some GTK version. */
470 if (is_keypress)
471 cancelBalloon(beval);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000472 break;
473 }
474 }
475 else
476 cancelBalloon(beval);
477}
478
Bram Moolenaar98921892016-02-23 17:14:37 +0100479# if GTK_CHECK_VERSION(3,0,0)
480 static gboolean
481# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000482 static gint
Bram Moolenaar98921892016-02-23 17:14:37 +0100483# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000484timeout_cb(gpointer data)
485{
486 BalloonEval *beval = (BalloonEval *)data;
487
488 beval->timerID = 0;
489 /*
490 * If the timer event happens then the mouse has stopped long enough for
491 * a request to be started. The request will only send to the debugger if
492 * there the mouse is pointing at real data.
493 */
494 requestBalloon(beval);
495
496 return FALSE; /* don't call me again */
497}
498
Bram Moolenaar98921892016-02-23 17:14:37 +0100499# if GTK_CHECK_VERSION(3,0,0)
500 static gboolean
501balloon_draw_event_cb(GtkWidget *widget,
502 cairo_t *cr,
503 gpointer data UNUSED)
504{
505 GtkStyleContext *context = NULL;
506 gint width = -1, height = -1;
507
508 if (widget == NULL)
509 return TRUE;
510
511 context = gtk_widget_get_style_context(widget);
512 width = gtk_widget_get_allocated_width(widget);
513 height = gtk_widget_get_allocated_height(widget);
514
515 gtk_style_context_save(context);
516
517 gtk_style_context_add_class(context, "tooltip");
518 gtk_style_context_set_state(context, GTK_STATE_FLAG_NORMAL);
519
520 cairo_save(cr);
521 gtk_render_frame(context, cr, 0, 0, width, height);
522 gtk_render_background(context, cr, 0, 0, width, height);
523 cairo_restore(cr);
524
525 gtk_style_context_restore(context);
526
527 return FALSE;
528}
529# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000530 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +0000531balloon_expose_event_cb(GtkWidget *widget,
532 GdkEventExpose *event,
533 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000534{
535 gtk_paint_flat_box(widget->style, widget->window,
536 GTK_STATE_NORMAL, GTK_SHADOW_OUT,
537 &event->area, widget, "tooltip",
538 0, 0, -1, -1);
539
540 return FALSE; /* continue emission */
541}
Bram Moolenaar98921892016-02-23 17:14:37 +0100542# endif /* !GTK_CHECK_VERSION(3,0,0) */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000543
Bram Moolenaar071d4272004-06-13 20:20:40 +0000544#else /* !FEAT_GUI_GTK */
545
546 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100547addEventHandler(Widget target, BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000548{
549 XtAddEventHandler(target,
550 PointerMotionMask | EnterWindowMask |
551 LeaveWindowMask | ButtonPressMask | KeyPressMask |
552 KeyReleaseMask,
553 False,
554 pointerEventEH, (XtPointer)beval);
555}
556
557 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100558removeEventHandler(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000559{
560 XtRemoveEventHandler(beval->target,
561 PointerMotionMask | EnterWindowMask |
562 LeaveWindowMask | ButtonPressMask | KeyPressMask |
563 KeyReleaseMask,
564 False,
565 pointerEventEH, (XtPointer)beval);
566}
567
568
569/*
570 * The X event handler. All it does is call the real event handler.
571 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000572 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100573pointerEventEH(
574 Widget w UNUSED,
575 XtPointer client_data,
576 XEvent *event,
577 Boolean *unused UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000578{
579 BalloonEval *beval = (BalloonEval *)client_data;
580 pointerEvent(beval, event);
581}
582
583
584/*
585 * The real event handler. Called by pointerEventEH() whenever an event we are
Bram Moolenaarbae0c162007-05-10 19:30:25 +0000586 * interested in occurs.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000587 */
588
589 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100590pointerEvent(BalloonEval *beval, XEvent *event)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000591{
Bram Moolenaar84a05ac2013-05-06 04:24:17 +0200592 Position distance; /* a measure of how much the pointer moved */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000593 Position delta; /* used to compute distance */
594
595 switch (event->type)
596 {
597 case EnterNotify:
598 case MotionNotify:
599 delta = event->xmotion.x - beval->x;
600 if (delta < 0)
601 delta = -delta;
602 distance = delta;
603 delta = event->xmotion.y - beval->y;
604 if (delta < 0)
605 delta = -delta;
606 distance += delta;
607 if (distance > 4)
608 {
609 /*
610 * Moved out of the balloon location: cancel it.
611 * Remember button state
612 */
613 beval->state = event->xmotion.state;
614 if (beval->state & (Button1Mask|Button2Mask|Button3Mask))
615 {
616 /* Mouse buttons are pressed - no balloon now */
617 cancelBalloon(beval);
618 }
619 else if (beval->state & (Mod1Mask|Mod2Mask|Mod3Mask))
620 {
621 /*
622 * Alt is pressed -- enter super-evaluate-mode,
623 * where there is no time delay
624 */
625 beval->x = event->xmotion.x;
626 beval->y = event->xmotion.y;
627 beval->x_root = event->xmotion.x_root;
628 beval->y_root = event->xmotion.y_root;
629 cancelBalloon(beval);
630 if (beval->msgCB != NULL)
631 {
632 beval->showState = ShS_PENDING;
633 (*beval->msgCB)(beval, beval->state);
634 }
635 }
636 else
637 {
638 beval->x = event->xmotion.x;
639 beval->y = event->xmotion.y;
640 beval->x_root = event->xmotion.x_root;
641 beval->y_root = event->xmotion.y_root;
642 cancelBalloon(beval);
643 beval->timerID = XtAppAddTimeOut( beval->appContext,
644 (long_u)p_bdlay, timerRoutine, beval);
645 }
646 }
647 break;
648
649 /*
650 * Motif and Athena version: Keystrokes will be caught by the
651 * "textArea" widget, and handled in gui_x11_key_hit_cb().
652 */
653 case KeyPress:
654 if (beval->showState == ShS_SHOWING && beval->msgCB != NULL)
655 {
656 Modifiers modifier;
657 KeySym keysym;
658
659 XtTranslateKeycode(gui.dpy,
660 event->xkey.keycode, event->xkey.state,
661 &modifier, &keysym);
662 if (keysym == XK_Shift_L || keysym == XK_Shift_R)
663 {
664 beval->showState = ShS_UPDATE_PENDING;
665 (*beval->msgCB)(beval, ShiftMask);
666 }
667 else if (keysym == XK_Control_L || keysym == XK_Control_R)
668 {
669 beval->showState = ShS_UPDATE_PENDING;
670 (*beval->msgCB)(beval, ControlMask);
671 }
672 else
673 cancelBalloon(beval);
674 }
675 else
676 cancelBalloon(beval);
677 break;
678
679 case KeyRelease:
680 if (beval->showState == ShS_SHOWING && beval->msgCB != NULL)
681 {
682 Modifiers modifier;
683 KeySym keysym;
684
685 XtTranslateKeycode(gui.dpy, event->xkey.keycode,
686 event->xkey.state, &modifier, &keysym);
687 if ((keysym == XK_Shift_L) || (keysym == XK_Shift_R)) {
688 beval->showState = ShS_UPDATE_PENDING;
689 (*beval->msgCB)(beval, 0);
690 }
691 else if ((keysym == XK_Control_L) || (keysym == XK_Control_R))
692 {
693 beval->showState = ShS_UPDATE_PENDING;
694 (*beval->msgCB)(beval, 0);
695 }
696 else
697 cancelBalloon(beval);
698 }
699 else
700 cancelBalloon(beval);
701 break;
702
703 case LeaveNotify:
704 /* Ignore LeaveNotify events that are not "normal".
705 * Apparently we also get it when somebody else grabs focus.
706 * Happens for me every two seconds (some clipboard tool?) */
707 if (event->xcrossing.mode == NotifyNormal)
708 cancelBalloon(beval);
709 break;
710
711 case ButtonPress:
712 cancelBalloon(beval);
713 break;
714
715 default:
716 break;
717 }
718}
719
Bram Moolenaar071d4272004-06-13 20:20:40 +0000720 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100721timerRoutine(XtPointer dx, XtIntervalId *id UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000722{
723 BalloonEval *beval = (BalloonEval *)dx;
724
725 beval->timerID = (XtIntervalId)NULL;
726
727 /*
728 * If the timer event happens then the mouse has stopped long enough for
729 * a request to be started. The request will only send to the debugger if
730 * there the mouse is pointing at real data.
731 */
732 requestBalloon(beval);
733}
734
735#endif /* !FEAT_GUI_GTK */
736
737 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100738requestBalloon(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000739{
740 if (beval->showState != ShS_PENDING)
741 {
742 /* Determine the beval to display */
743 if (beval->msgCB != NULL)
744 {
745 beval->showState = ShS_PENDING;
746 (*beval->msgCB)(beval, beval->state);
747 }
748 else if (beval->msg != NULL)
749 drawBalloon(beval);
750 }
751}
752
753#ifdef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +0000754/*
755 * Convert the string to UTF-8 if 'encoding' is not "utf-8".
756 * Replace any non-printable characters and invalid bytes sequences with
757 * "^X" or "<xx>" escapes, and apply SpecialKey highlighting to them.
758 * TAB and NL are passed through unscathed.
759 */
Bram Moolenaar182c5be2010-06-25 05:37:59 +0200760# define IS_NONPRINTABLE(c) (((c) < 0x20 && (c) != TAB && (c) != NL) \
Bram Moolenaar071d4272004-06-13 20:20:40 +0000761 || (c) == DEL)
762 static void
Bram Moolenaar89d40322006-08-29 15:30:07 +0000763set_printable_label_text(GtkLabel *label, char_u *text)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000764{
765 char_u *convbuf = NULL;
766 char_u *buf;
767 char_u *p;
768 char_u *pdest;
769 unsigned int len;
770 int charlen;
771 int uc;
772 PangoAttrList *attr_list;
773
774 /* Convert to UTF-8 if it isn't already */
775 if (output_conv.vc_type != CONV_NONE)
776 {
Bram Moolenaar89d40322006-08-29 15:30:07 +0000777 convbuf = string_convert(&output_conv, text, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000778 if (convbuf != NULL)
Bram Moolenaar89d40322006-08-29 15:30:07 +0000779 text = convbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000780 }
781
782 /* First let's see how much we need to allocate */
783 len = 0;
Bram Moolenaar89d40322006-08-29 15:30:07 +0000784 for (p = text; *p != NUL; p += charlen)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000785 {
786 if ((*p & 0x80) == 0) /* be quick for ASCII */
787 {
788 charlen = 1;
789 len += IS_NONPRINTABLE(*p) ? 2 : 1; /* nonprintable: ^X */
790 }
791 else
792 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000793 charlen = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000794 uc = utf_ptr2char(p);
795
796 if (charlen != utf_char2len(uc))
797 charlen = 1; /* reject overlong sequences */
798
799 if (charlen == 1 || uc < 0xa0) /* illegal byte or */
800 len += 4; /* control char: <xx> */
801 else if (!utf_printable(uc))
802 /* Note: we assume here that utf_printable() doesn't
803 * care about characters outside the BMP. */
804 len += 6; /* nonprintable: <xxxx> */
805 else
806 len += charlen;
807 }
808 }
809
810 attr_list = pango_attr_list_new();
811 buf = alloc(len + 1);
812
813 /* Now go for the real work */
814 if (buf != NULL)
815 {
816 attrentry_T *aep;
817 PangoAttribute *attr;
818 guicolor_T pixel;
Bram Moolenaar36edf062016-07-21 22:10:12 +0200819#if GTK_CHECK_VERSION(3,0,0)
820 GdkRGBA color = { 0.0, 0.0, 0.0, 1.0 };
Bram Moolenaar870b7492016-07-22 22:26:52 +0200821# if PANGO_VERSION_CHECK(1,38,0)
Bram Moolenaar36edf062016-07-21 22:10:12 +0200822 PangoAttribute *attr_alpha;
Bram Moolenaar870b7492016-07-22 22:26:52 +0200823# endif
Bram Moolenaar36edf062016-07-21 22:10:12 +0200824#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000825 GdkColor color = { 0, 0, 0, 0 };
Bram Moolenaar36edf062016-07-21 22:10:12 +0200826#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000827
828 /* Look up the RGB values of the SpecialKey foreground color. */
Bram Moolenaar8820b482017-03-16 17:23:31 +0100829 aep = syn_gui_attr2entry(HL_ATTR(HLF_8));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000830 pixel = (aep != NULL) ? aep->ae_u.gui.fg_color : INVALCOLOR;
831 if (pixel != INVALCOLOR)
Bram Moolenaar98921892016-02-23 17:14:37 +0100832# if GTK_CHECK_VERSION(3,0,0)
833 {
Bram Moolenaar36edf062016-07-21 22:10:12 +0200834 color.red = ((pixel & 0xff0000) >> 16) / 255.0;
835 color.green = ((pixel & 0xff00) >> 8) / 255.0;
836 color.blue = (pixel & 0xff) / 255.0;
837 color.alpha = 1.0;
Bram Moolenaar98921892016-02-23 17:14:37 +0100838 }
839# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000840 gdk_colormap_query_color(gtk_widget_get_colormap(gui.drawarea),
841 (unsigned long)pixel, &color);
Bram Moolenaar98921892016-02-23 17:14:37 +0100842# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000843
844 pdest = buf;
Bram Moolenaar89d40322006-08-29 15:30:07 +0000845 p = text;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000846 while (*p != NUL)
847 {
848 /* Be quick for ASCII */
849 if ((*p & 0x80) == 0 && !IS_NONPRINTABLE(*p))
850 {
851 *pdest++ = *p++;
852 }
853 else
854 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000855 charlen = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000856 uc = utf_ptr2char(p);
857
858 if (charlen != utf_char2len(uc))
859 charlen = 1; /* reject overlong sequences */
860
861 if (charlen == 1 || uc < 0xa0 || !utf_printable(uc))
862 {
863 int outlen;
864
865 /* Careful: we can't just use transchar_byte() here,
866 * since 'encoding' is not necessarily set to "utf-8". */
867 if (*p & 0x80 && charlen == 1)
868 {
869 transchar_hex(pdest, *p); /* <xx> */
870 outlen = 4;
871 }
872 else if (uc >= 0x80)
873 {
874 /* Note: we assume here that utf_printable() doesn't
875 * care about characters outside the BMP. */
876 transchar_hex(pdest, uc); /* <xx> or <xxxx> */
877 outlen = (uc < 0x100) ? 4 : 6;
878 }
879 else
880 {
881 transchar_nonprint(pdest, *p); /* ^X */
882 outlen = 2;
883 }
884 if (pixel != INVALCOLOR)
885 {
Bram Moolenaar36edf062016-07-21 22:10:12 +0200886#if GTK_CHECK_VERSION(3,0,0)
887# define DOUBLE2UINT16(val) ((guint16)((val) * 65535 + 0.5))
888 attr = pango_attr_foreground_new(
889 DOUBLE2UINT16(color.red),
890 DOUBLE2UINT16(color.green),
891 DOUBLE2UINT16(color.blue));
Bram Moolenaar870b7492016-07-22 22:26:52 +0200892# if PANGO_VERSION_CHECK(1,38,0)
Bram Moolenaar36edf062016-07-21 22:10:12 +0200893 attr_alpha = pango_attr_foreground_alpha_new(
894 DOUBLE2UINT16(color.alpha));
Bram Moolenaar870b7492016-07-22 22:26:52 +0200895# endif
Bram Moolenaar36edf062016-07-21 22:10:12 +0200896# undef DOUBLE2UINT16
897#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000898 attr = pango_attr_foreground_new(
899 color.red, color.green, color.blue);
Bram Moolenaar36edf062016-07-21 22:10:12 +0200900#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000901 attr->start_index = pdest - buf;
902 attr->end_index = pdest - buf + outlen;
903 pango_attr_list_insert(attr_list, attr);
Bram Moolenaar36edf062016-07-21 22:10:12 +0200904#if GTK_CHECK_VERSION(3,0,0)
Bram Moolenaar870b7492016-07-22 22:26:52 +0200905# if PANGO_VERSION_CHECK(1,38,0)
Bram Moolenaar36edf062016-07-21 22:10:12 +0200906 attr_alpha->start_index = pdest - buf;
907 attr_alpha->end_index = pdest - buf + outlen;
908 pango_attr_list_insert(attr_list, attr_alpha);
Bram Moolenaar870b7492016-07-22 22:26:52 +0200909# endif
Bram Moolenaar36edf062016-07-21 22:10:12 +0200910#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000911 }
912 pdest += outlen;
913 p += charlen;
914 }
915 else
916 {
917 do
918 *pdest++ = *p++;
919 while (--charlen != 0);
920 }
921 }
922 }
923 *pdest = NUL;
924 }
925
926 vim_free(convbuf);
927
928 gtk_label_set_text(label, (const char *)buf);
929 vim_free(buf);
930
931 gtk_label_set_attributes(label, attr_list);
932 pango_attr_list_unref(attr_list);
933}
Bram Moolenaar182c5be2010-06-25 05:37:59 +0200934# undef IS_NONPRINTABLE
Bram Moolenaar071d4272004-06-13 20:20:40 +0000935
936/*
937 * Draw a balloon.
938 */
939 static void
940drawBalloon(BalloonEval *beval)
941{
942 if (beval->msg != NULL)
943 {
944 GtkRequisition requisition;
945 int screen_w;
946 int screen_h;
947 int x;
948 int y;
949 int x_offset = EVAL_OFFSET_X;
950 int y_offset = EVAL_OFFSET_Y;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000951 PangoLayout *layout;
Bram Moolenaara859f042016-11-17 19:11:55 +0100952
Bram Moolenaar7be9b502017-09-09 18:45:26 +0200953# if !GTK_CHECK_VERSION(3,22,2)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000954 GdkScreen *screen;
955
956 screen = gtk_widget_get_screen(beval->target);
957 gtk_window_set_screen(GTK_WINDOW(beval->balloonShell), screen);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000958# endif
Bram Moolenaar7be9b502017-09-09 18:45:26 +0200959 gui_gtk_get_screen_size_of_win(beval->balloonShell,
960 &screen_w, &screen_h);
Bram Moolenaar98921892016-02-23 17:14:37 +0100961# if !GTK_CHECK_VERSION(3,0,0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000962 gtk_widget_ensure_style(beval->balloonShell);
963 gtk_widget_ensure_style(beval->balloonLabel);
Bram Moolenaar98921892016-02-23 17:14:37 +0100964# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000965
Bram Moolenaar071d4272004-06-13 20:20:40 +0000966 set_printable_label_text(GTK_LABEL(beval->balloonLabel), beval->msg);
967 /*
968 * Dirty trick: Enable wrapping mode on the label's layout behind its
969 * back. This way GtkLabel won't try to constrain the wrap width to a
970 * builtin maximum value of about 65 Latin characters.
971 */
972 layout = gtk_label_get_layout(GTK_LABEL(beval->balloonLabel));
Bram Moolenaar182c5be2010-06-25 05:37:59 +0200973# ifdef PANGO_WRAP_WORD_CHAR
Bram Moolenaar071d4272004-06-13 20:20:40 +0000974 pango_layout_set_wrap(layout, PANGO_WRAP_WORD_CHAR);
Bram Moolenaar182c5be2010-06-25 05:37:59 +0200975# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000976 pango_layout_set_wrap(layout, PANGO_WRAP_WORD);
Bram Moolenaar182c5be2010-06-25 05:37:59 +0200977# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000978 pango_layout_set_width(layout,
979 /* try to come up with some reasonable width */
980 PANGO_SCALE * CLAMP(gui.num_cols * gui.char_width,
981 screen_w / 2,
982 MAX(20, screen_w - 20)));
983
984 /* Calculate the balloon's width and height. */
Bram Moolenaar98921892016-02-23 17:14:37 +0100985# if GTK_CHECK_VERSION(3,0,0)
986 gtk_widget_get_preferred_size(beval->balloonShell, &requisition, NULL);
987# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000988 gtk_widget_size_request(beval->balloonShell, &requisition);
Bram Moolenaar98921892016-02-23 17:14:37 +0100989# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000990
991 /* Compute position of the balloon area */
Bram Moolenaar98921892016-02-23 17:14:37 +0100992# if GTK_CHECK_VERSION(3,0,0)
993 gdk_window_get_origin(gtk_widget_get_window(beval->target), &x, &y);
994# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000995 gdk_window_get_origin(beval->target->window, &x, &y);
Bram Moolenaar98921892016-02-23 17:14:37 +0100996# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000997 x += beval->x;
998 y += beval->y;
999
1000 /* Get out of the way of the mouse pointer */
1001 if (x + x_offset + requisition.width > screen_w)
1002 y_offset += 15;
1003 if (y + y_offset + requisition.height > screen_h)
1004 y_offset = -requisition.height - EVAL_OFFSET_Y;
1005
1006 /* Sanitize values */
1007 x = CLAMP(x + x_offset, 0, MAX(0, screen_w - requisition.width));
1008 y = CLAMP(y + y_offset, 0, MAX(0, screen_h - requisition.height));
1009
1010 /* Show the balloon */
Bram Moolenaar98921892016-02-23 17:14:37 +01001011# if GTK_CHECK_VERSION(3,0,0)
1012 gtk_window_move(GTK_WINDOW(beval->balloonShell), x, y);
1013# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001014 gtk_widget_set_uposition(beval->balloonShell, x, y);
Bram Moolenaar98921892016-02-23 17:14:37 +01001015# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001016 gtk_widget_show(beval->balloonShell);
1017
1018 beval->showState = ShS_SHOWING;
1019 }
1020}
1021
1022/*
1023 * Undraw a balloon.
1024 */
1025 static void
1026undrawBalloon(BalloonEval *beval)
1027{
1028 if (beval->balloonShell != NULL)
1029 gtk_widget_hide(beval->balloonShell);
1030 beval->showState = ShS_NEUTRAL;
1031}
1032
1033 static void
1034cancelBalloon(BalloonEval *beval)
1035{
1036 if (beval->showState == ShS_SHOWING
1037 || beval->showState == ShS_UPDATE_PENDING)
1038 undrawBalloon(beval);
1039
1040 if (beval->timerID != 0)
1041 {
Bram Moolenaar98921892016-02-23 17:14:37 +01001042# if GTK_CHECK_VERSION(3,0,0)
1043 g_source_remove(beval->timerID);
1044# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001045 gtk_timeout_remove(beval->timerID);
Bram Moolenaar98921892016-02-23 17:14:37 +01001046# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001047 beval->timerID = 0;
1048 }
1049 beval->showState = ShS_NEUTRAL;
1050}
1051
1052 static void
1053createBalloonEvalWindow(BalloonEval *beval)
1054{
1055 beval->balloonShell = gtk_window_new(GTK_WINDOW_POPUP);
1056
1057 gtk_widget_set_app_paintable(beval->balloonShell, TRUE);
Bram Moolenaar98921892016-02-23 17:14:37 +01001058# if GTK_CHECK_VERSION(3,0,0)
1059 gtk_window_set_resizable(GTK_WINDOW(beval->balloonShell), FALSE);
1060# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001061 gtk_window_set_policy(GTK_WINDOW(beval->balloonShell), FALSE, FALSE, TRUE);
Bram Moolenaar98921892016-02-23 17:14:37 +01001062# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001063 gtk_widget_set_name(beval->balloonShell, "gtk-tooltips");
Bram Moolenaar98921892016-02-23 17:14:37 +01001064# if GTK_CHECK_VERSION(3,0,0)
1065 gtk_container_set_border_width(GTK_CONTAINER(beval->balloonShell), 4);
1066# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001067 gtk_container_border_width(GTK_CONTAINER(beval->balloonShell), 4);
Bram Moolenaar98921892016-02-23 17:14:37 +01001068# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001069
Bram Moolenaar98921892016-02-23 17:14:37 +01001070# if GTK_CHECK_VERSION(3,0,0)
1071 g_signal_connect(G_OBJECT(beval->balloonShell), "draw",
1072 G_CALLBACK(balloon_draw_event_cb), NULL);
1073# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001074 gtk_signal_connect((GtkObject*)(beval->balloonShell), "expose_event",
1075 GTK_SIGNAL_FUNC(balloon_expose_event_cb), NULL);
Bram Moolenaar98921892016-02-23 17:14:37 +01001076# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001077 beval->balloonLabel = gtk_label_new(NULL);
1078
1079 gtk_label_set_line_wrap(GTK_LABEL(beval->balloonLabel), FALSE);
1080 gtk_label_set_justify(GTK_LABEL(beval->balloonLabel), GTK_JUSTIFY_LEFT);
Bram Moolenaar98921892016-02-23 17:14:37 +01001081# if GTK_CHECK_VERSION(3,16,0)
1082 gtk_label_set_xalign(GTK_LABEL(beval->balloonLabel), 0.5);
1083 gtk_label_set_yalign(GTK_LABEL(beval->balloonLabel), 0.5);
1084# elif GTK_CHECK_VERSION(3,14,0)
1085 GValue align_val = G_VALUE_INIT;
1086 g_value_init(&align_val, G_TYPE_FLOAT);
1087 g_value_set_float(&align_val, 0.5);
1088 g_object_set_property(G_OBJECT(beval->balloonLabel), "xalign", &align_val);
1089 g_object_set_property(G_OBJECT(beval->balloonLabel), "yalign", &align_val);
1090 g_value_unset(&align_val);
1091# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001092 gtk_misc_set_alignment(GTK_MISC(beval->balloonLabel), 0.5f, 0.5f);
Bram Moolenaar98921892016-02-23 17:14:37 +01001093# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001094 gtk_widget_set_name(beval->balloonLabel, "vim-balloon-label");
1095 gtk_widget_show(beval->balloonLabel);
1096
1097 gtk_container_add(GTK_CONTAINER(beval->balloonShell), beval->balloonLabel);
1098}
1099
1100#else /* !FEAT_GUI_GTK */
1101
1102/*
1103 * Draw a balloon.
1104 */
1105 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001106drawBalloon(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001107{
1108 Dimension w;
1109 Dimension h;
1110 Position tx;
1111 Position ty;
1112
1113 if (beval->msg != NULL)
1114 {
1115 /* Show the Balloon */
1116
1117 /* Calculate the label's width and height */
1118#ifdef FEAT_GUI_MOTIF
1119 XmString s;
1120
1121 /* For the callback function we parse NL characters to create a
1122 * multi-line label. This doesn't work for all languages, but
1123 * XmStringCreateLocalized() doesn't do multi-line labels... */
1124 if (beval->msgCB != NULL)
1125 s = XmStringCreateLtoR((char *)beval->msg, XmFONTLIST_DEFAULT_TAG);
1126 else
1127 s = XmStringCreateLocalized((char *)beval->msg);
1128 {
1129 XmFontList fl;
1130
1131 fl = gui_motif_fontset2fontlist(&gui.tooltip_fontset);
Bram Moolenaardb5ffaa2014-06-25 17:44:49 +02001132 if (fl == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001133 {
Bram Moolenaardb5ffaa2014-06-25 17:44:49 +02001134 XmStringFree(s);
1135 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001136 }
Bram Moolenaardb5ffaa2014-06-25 17:44:49 +02001137 XmStringExtent(fl, s, &w, &h);
1138 XmFontListFree(fl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001139 }
1140 w += gui.border_offset << 1;
1141 h += gui.border_offset << 1;
1142 XtVaSetValues(beval->balloonLabel, XmNlabelString, s, NULL);
1143 XmStringFree(s);
1144#else /* Athena */
1145 /* Assume XtNinternational == True */
1146 XFontSet fset;
1147 XFontSetExtents *ext;
1148
1149 XtVaGetValues(beval->balloonLabel, XtNfontSet, &fset, NULL);
1150 ext = XExtentsOfFontSet(fset);
1151 h = ext->max_ink_extent.height;
1152 w = XmbTextEscapement(fset,
1153 (char *)beval->msg,
1154 (int)STRLEN(beval->msg));
1155 w += gui.border_offset << 1;
1156 h += gui.border_offset << 1;
1157 XtVaSetValues(beval->balloonLabel, XtNlabel, beval->msg, NULL);
1158#endif
1159
1160 /* Compute position of the balloon area */
1161 tx = beval->x_root + EVAL_OFFSET_X;
1162 ty = beval->y_root + EVAL_OFFSET_Y;
1163 if ((tx + w) > beval->screen_width)
1164 tx = beval->screen_width - w;
1165 if ((ty + h) > beval->screen_height)
1166 ty = beval->screen_height - h;
1167#ifdef FEAT_GUI_MOTIF
1168 XtVaSetValues(beval->balloonShell,
1169 XmNx, tx,
1170 XmNy, ty,
1171 NULL);
1172#else
1173 /* Athena */
1174 XtVaSetValues(beval->balloonShell,
1175 XtNx, tx,
1176 XtNy, ty,
1177 NULL);
1178#endif
Bram Moolenaar8281f442009-03-18 11:22:25 +00001179 /* Set tooltip colors */
1180 {
1181 Arg args[2];
1182
1183#ifdef FEAT_GUI_MOTIF
1184 args[0].name = XmNbackground;
1185 args[0].value = gui.tooltip_bg_pixel;
1186 args[1].name = XmNforeground;
1187 args[1].value = gui.tooltip_fg_pixel;
1188#else /* Athena */
1189 args[0].name = XtNbackground;
1190 args[0].value = gui.tooltip_bg_pixel;
1191 args[1].name = XtNforeground;
1192 args[1].value = gui.tooltip_fg_pixel;
1193#endif
1194 XtSetValues(beval->balloonLabel, &args[0], XtNumber(args));
1195 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001196
1197 XtPopup(beval->balloonShell, XtGrabNone);
1198
1199 beval->showState = ShS_SHOWING;
1200
1201 current_beval = beval;
1202 }
1203}
1204
1205/*
1206 * Undraw a balloon.
1207 */
1208 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001209undrawBalloon(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001210{
1211 if (beval->balloonShell != (Widget)0)
1212 XtPopdown(beval->balloonShell);
1213 beval->showState = ShS_NEUTRAL;
1214
1215 current_beval = NULL;
1216}
1217
1218 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001219cancelBalloon(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001220{
1221 if (beval->showState == ShS_SHOWING
1222 || beval->showState == ShS_UPDATE_PENDING)
1223 undrawBalloon(beval);
1224
1225 if (beval->timerID != (XtIntervalId)NULL)
1226 {
1227 XtRemoveTimeOut(beval->timerID);
1228 beval->timerID = (XtIntervalId)NULL;
1229 }
1230 beval->showState = ShS_NEUTRAL;
1231}
1232
1233
1234 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001235createBalloonEvalWindow(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001236{
1237 Arg args[12];
1238 int n;
1239
1240 n = 0;
1241#ifdef FEAT_GUI_MOTIF
1242 XtSetArg(args[n], XmNallowShellResize, True); n++;
1243 beval->balloonShell = XtAppCreateShell("balloonEval", "BalloonEval",
1244 overrideShellWidgetClass, gui.dpy, args, n);
1245#else
1246 /* Athena */
1247 XtSetArg(args[n], XtNallowShellResize, True); n++;
1248 beval->balloonShell = XtAppCreateShell("balloonEval", "BalloonEval",
1249 overrideShellWidgetClass, gui.dpy, args, n);
1250#endif
1251
1252 n = 0;
1253#ifdef FEAT_GUI_MOTIF
1254 {
1255 XmFontList fl;
1256
1257 fl = gui_motif_fontset2fontlist(&gui.tooltip_fontset);
1258 XtSetArg(args[n], XmNforeground, gui.tooltip_fg_pixel); n++;
1259 XtSetArg(args[n], XmNbackground, gui.tooltip_bg_pixel); n++;
1260 XtSetArg(args[n], XmNfontList, fl); n++;
1261 XtSetArg(args[n], XmNalignment, XmALIGNMENT_BEGINNING); n++;
1262 beval->balloonLabel = XtCreateManagedWidget("balloonLabel",
1263 xmLabelWidgetClass, beval->balloonShell, args, n);
1264 }
1265#else /* FEAT_GUI_ATHENA */
1266 XtSetArg(args[n], XtNforeground, gui.tooltip_fg_pixel); n++;
1267 XtSetArg(args[n], XtNbackground, gui.tooltip_bg_pixel); n++;
1268 XtSetArg(args[n], XtNinternational, True); n++;
1269 XtSetArg(args[n], XtNfontSet, gui.tooltip_fontset); n++;
1270 beval->balloonLabel = XtCreateManagedWidget("balloonLabel",
1271 labelWidgetClass, beval->balloonShell, args, n);
1272#endif
1273}
1274
1275#endif /* !FEAT_GUI_GTK */
1276#endif /* !FEAT_GUI_W32 */
1277
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01001278#endif /* FEAT_BEVAL_GUI */