blob: 03216008e6a1e5be16a643b4363f0b1bab790be9 [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;
Bram Moolenaar3f6a16f2018-08-19 22:58:45 +0200947 int screen_x;
948 int screen_y;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000949 int x;
950 int y;
951 int x_offset = EVAL_OFFSET_X;
952 int y_offset = EVAL_OFFSET_Y;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000953 PangoLayout *layout;
Bram Moolenaara859f042016-11-17 19:11:55 +0100954
Bram Moolenaar7be9b502017-09-09 18:45:26 +0200955# if !GTK_CHECK_VERSION(3,22,2)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000956 GdkScreen *screen;
957
958 screen = gtk_widget_get_screen(beval->target);
959 gtk_window_set_screen(GTK_WINDOW(beval->balloonShell), screen);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000960# endif
Bram Moolenaar3f6a16f2018-08-19 22:58:45 +0200961 gui_gtk_get_screen_geom_of_win(beval->balloonShell,
962 &screen_x, &screen_y, &screen_w, &screen_h);
Bram Moolenaar98921892016-02-23 17:14:37 +0100963# if !GTK_CHECK_VERSION(3,0,0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000964 gtk_widget_ensure_style(beval->balloonShell);
965 gtk_widget_ensure_style(beval->balloonLabel);
Bram Moolenaar98921892016-02-23 17:14:37 +0100966# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000967
Bram Moolenaar071d4272004-06-13 20:20:40 +0000968 set_printable_label_text(GTK_LABEL(beval->balloonLabel), beval->msg);
969 /*
970 * Dirty trick: Enable wrapping mode on the label's layout behind its
971 * back. This way GtkLabel won't try to constrain the wrap width to a
972 * builtin maximum value of about 65 Latin characters.
973 */
974 layout = gtk_label_get_layout(GTK_LABEL(beval->balloonLabel));
Bram Moolenaar182c5be2010-06-25 05:37:59 +0200975# ifdef PANGO_WRAP_WORD_CHAR
Bram Moolenaar071d4272004-06-13 20:20:40 +0000976 pango_layout_set_wrap(layout, PANGO_WRAP_WORD_CHAR);
Bram Moolenaar182c5be2010-06-25 05:37:59 +0200977# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000978 pango_layout_set_wrap(layout, PANGO_WRAP_WORD);
Bram Moolenaar182c5be2010-06-25 05:37:59 +0200979# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000980 pango_layout_set_width(layout,
981 /* try to come up with some reasonable width */
982 PANGO_SCALE * CLAMP(gui.num_cols * gui.char_width,
983 screen_w / 2,
984 MAX(20, screen_w - 20)));
985
986 /* Calculate the balloon's width and height. */
Bram Moolenaar98921892016-02-23 17:14:37 +0100987# if GTK_CHECK_VERSION(3,0,0)
988 gtk_widget_get_preferred_size(beval->balloonShell, &requisition, NULL);
989# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000990 gtk_widget_size_request(beval->balloonShell, &requisition);
Bram Moolenaar98921892016-02-23 17:14:37 +0100991# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000992
993 /* Compute position of the balloon area */
Bram Moolenaar98921892016-02-23 17:14:37 +0100994# if GTK_CHECK_VERSION(3,0,0)
995 gdk_window_get_origin(gtk_widget_get_window(beval->target), &x, &y);
996# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000997 gdk_window_get_origin(beval->target->window, &x, &y);
Bram Moolenaar98921892016-02-23 17:14:37 +0100998# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000999 x += beval->x;
1000 y += beval->y;
1001
1002 /* Get out of the way of the mouse pointer */
Bram Moolenaar3f6a16f2018-08-19 22:58:45 +02001003 if (x + x_offset + requisition.width > screen_x + screen_w)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001004 y_offset += 15;
Bram Moolenaar3f6a16f2018-08-19 22:58:45 +02001005 if (y + y_offset + requisition.height > screen_y + screen_h)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001006 y_offset = -requisition.height - EVAL_OFFSET_Y;
1007
1008 /* Sanitize values */
Bram Moolenaar3f6a16f2018-08-19 22:58:45 +02001009 x = CLAMP(x + x_offset, 0,
1010 MAX(0, screen_x + screen_w - requisition.width));
1011 y = CLAMP(y + y_offset, 0,
1012 MAX(0, screen_y + screen_h - requisition.height));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001013
1014 /* Show the balloon */
Bram Moolenaar98921892016-02-23 17:14:37 +01001015# if GTK_CHECK_VERSION(3,0,0)
1016 gtk_window_move(GTK_WINDOW(beval->balloonShell), x, y);
1017# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001018 gtk_widget_set_uposition(beval->balloonShell, x, y);
Bram Moolenaar98921892016-02-23 17:14:37 +01001019# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001020 gtk_widget_show(beval->balloonShell);
1021
1022 beval->showState = ShS_SHOWING;
1023 }
1024}
1025
1026/*
1027 * Undraw a balloon.
1028 */
1029 static void
1030undrawBalloon(BalloonEval *beval)
1031{
1032 if (beval->balloonShell != NULL)
1033 gtk_widget_hide(beval->balloonShell);
1034 beval->showState = ShS_NEUTRAL;
1035}
1036
1037 static void
1038cancelBalloon(BalloonEval *beval)
1039{
1040 if (beval->showState == ShS_SHOWING
1041 || beval->showState == ShS_UPDATE_PENDING)
1042 undrawBalloon(beval);
1043
1044 if (beval->timerID != 0)
1045 {
Bram Moolenaar98921892016-02-23 17:14:37 +01001046# if GTK_CHECK_VERSION(3,0,0)
1047 g_source_remove(beval->timerID);
1048# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001049 gtk_timeout_remove(beval->timerID);
Bram Moolenaar98921892016-02-23 17:14:37 +01001050# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001051 beval->timerID = 0;
1052 }
1053 beval->showState = ShS_NEUTRAL;
1054}
1055
1056 static void
1057createBalloonEvalWindow(BalloonEval *beval)
1058{
1059 beval->balloonShell = gtk_window_new(GTK_WINDOW_POPUP);
1060
1061 gtk_widget_set_app_paintable(beval->balloonShell, TRUE);
Bram Moolenaar98921892016-02-23 17:14:37 +01001062# if GTK_CHECK_VERSION(3,0,0)
1063 gtk_window_set_resizable(GTK_WINDOW(beval->balloonShell), FALSE);
1064# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001065 gtk_window_set_policy(GTK_WINDOW(beval->balloonShell), FALSE, FALSE, TRUE);
Bram Moolenaar98921892016-02-23 17:14:37 +01001066# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001067 gtk_widget_set_name(beval->balloonShell, "gtk-tooltips");
Bram Moolenaar98921892016-02-23 17:14:37 +01001068# if GTK_CHECK_VERSION(3,0,0)
1069 gtk_container_set_border_width(GTK_CONTAINER(beval->balloonShell), 4);
1070# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001071 gtk_container_border_width(GTK_CONTAINER(beval->balloonShell), 4);
Bram Moolenaar98921892016-02-23 17:14:37 +01001072# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001073
Bram Moolenaar98921892016-02-23 17:14:37 +01001074# if GTK_CHECK_VERSION(3,0,0)
1075 g_signal_connect(G_OBJECT(beval->balloonShell), "draw",
1076 G_CALLBACK(balloon_draw_event_cb), NULL);
1077# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001078 gtk_signal_connect((GtkObject*)(beval->balloonShell), "expose_event",
1079 GTK_SIGNAL_FUNC(balloon_expose_event_cb), NULL);
Bram Moolenaar98921892016-02-23 17:14:37 +01001080# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001081 beval->balloonLabel = gtk_label_new(NULL);
1082
1083 gtk_label_set_line_wrap(GTK_LABEL(beval->balloonLabel), FALSE);
1084 gtk_label_set_justify(GTK_LABEL(beval->balloonLabel), GTK_JUSTIFY_LEFT);
Bram Moolenaar98921892016-02-23 17:14:37 +01001085# if GTK_CHECK_VERSION(3,16,0)
1086 gtk_label_set_xalign(GTK_LABEL(beval->balloonLabel), 0.5);
1087 gtk_label_set_yalign(GTK_LABEL(beval->balloonLabel), 0.5);
1088# elif GTK_CHECK_VERSION(3,14,0)
1089 GValue align_val = G_VALUE_INIT;
1090 g_value_init(&align_val, G_TYPE_FLOAT);
1091 g_value_set_float(&align_val, 0.5);
1092 g_object_set_property(G_OBJECT(beval->balloonLabel), "xalign", &align_val);
1093 g_object_set_property(G_OBJECT(beval->balloonLabel), "yalign", &align_val);
1094 g_value_unset(&align_val);
1095# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001096 gtk_misc_set_alignment(GTK_MISC(beval->balloonLabel), 0.5f, 0.5f);
Bram Moolenaar98921892016-02-23 17:14:37 +01001097# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001098 gtk_widget_set_name(beval->balloonLabel, "vim-balloon-label");
1099 gtk_widget_show(beval->balloonLabel);
1100
1101 gtk_container_add(GTK_CONTAINER(beval->balloonShell), beval->balloonLabel);
1102}
1103
1104#else /* !FEAT_GUI_GTK */
1105
1106/*
1107 * Draw a balloon.
1108 */
1109 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001110drawBalloon(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001111{
1112 Dimension w;
1113 Dimension h;
1114 Position tx;
1115 Position ty;
1116
1117 if (beval->msg != NULL)
1118 {
1119 /* Show the Balloon */
1120
1121 /* Calculate the label's width and height */
1122#ifdef FEAT_GUI_MOTIF
1123 XmString s;
1124
1125 /* For the callback function we parse NL characters to create a
1126 * multi-line label. This doesn't work for all languages, but
1127 * XmStringCreateLocalized() doesn't do multi-line labels... */
1128 if (beval->msgCB != NULL)
1129 s = XmStringCreateLtoR((char *)beval->msg, XmFONTLIST_DEFAULT_TAG);
1130 else
1131 s = XmStringCreateLocalized((char *)beval->msg);
1132 {
1133 XmFontList fl;
1134
1135 fl = gui_motif_fontset2fontlist(&gui.tooltip_fontset);
Bram Moolenaardb5ffaa2014-06-25 17:44:49 +02001136 if (fl == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001137 {
Bram Moolenaardb5ffaa2014-06-25 17:44:49 +02001138 XmStringFree(s);
1139 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001140 }
Bram Moolenaardb5ffaa2014-06-25 17:44:49 +02001141 XmStringExtent(fl, s, &w, &h);
1142 XmFontListFree(fl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001143 }
1144 w += gui.border_offset << 1;
1145 h += gui.border_offset << 1;
1146 XtVaSetValues(beval->balloonLabel, XmNlabelString, s, NULL);
1147 XmStringFree(s);
1148#else /* Athena */
1149 /* Assume XtNinternational == True */
1150 XFontSet fset;
1151 XFontSetExtents *ext;
1152
1153 XtVaGetValues(beval->balloonLabel, XtNfontSet, &fset, NULL);
1154 ext = XExtentsOfFontSet(fset);
1155 h = ext->max_ink_extent.height;
1156 w = XmbTextEscapement(fset,
1157 (char *)beval->msg,
1158 (int)STRLEN(beval->msg));
1159 w += gui.border_offset << 1;
1160 h += gui.border_offset << 1;
1161 XtVaSetValues(beval->balloonLabel, XtNlabel, beval->msg, NULL);
1162#endif
1163
1164 /* Compute position of the balloon area */
1165 tx = beval->x_root + EVAL_OFFSET_X;
1166 ty = beval->y_root + EVAL_OFFSET_Y;
1167 if ((tx + w) > beval->screen_width)
1168 tx = beval->screen_width - w;
1169 if ((ty + h) > beval->screen_height)
1170 ty = beval->screen_height - h;
1171#ifdef FEAT_GUI_MOTIF
1172 XtVaSetValues(beval->balloonShell,
1173 XmNx, tx,
1174 XmNy, ty,
1175 NULL);
1176#else
1177 /* Athena */
1178 XtVaSetValues(beval->balloonShell,
1179 XtNx, tx,
1180 XtNy, ty,
1181 NULL);
1182#endif
Bram Moolenaar8281f442009-03-18 11:22:25 +00001183 /* Set tooltip colors */
1184 {
1185 Arg args[2];
1186
1187#ifdef FEAT_GUI_MOTIF
1188 args[0].name = XmNbackground;
1189 args[0].value = gui.tooltip_bg_pixel;
1190 args[1].name = XmNforeground;
1191 args[1].value = gui.tooltip_fg_pixel;
1192#else /* Athena */
1193 args[0].name = XtNbackground;
1194 args[0].value = gui.tooltip_bg_pixel;
1195 args[1].name = XtNforeground;
1196 args[1].value = gui.tooltip_fg_pixel;
1197#endif
1198 XtSetValues(beval->balloonLabel, &args[0], XtNumber(args));
1199 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001200
1201 XtPopup(beval->balloonShell, XtGrabNone);
1202
1203 beval->showState = ShS_SHOWING;
1204
1205 current_beval = beval;
1206 }
1207}
1208
1209/*
1210 * Undraw a balloon.
1211 */
1212 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001213undrawBalloon(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001214{
1215 if (beval->balloonShell != (Widget)0)
1216 XtPopdown(beval->balloonShell);
1217 beval->showState = ShS_NEUTRAL;
1218
1219 current_beval = NULL;
1220}
1221
1222 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001223cancelBalloon(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001224{
1225 if (beval->showState == ShS_SHOWING
1226 || beval->showState == ShS_UPDATE_PENDING)
1227 undrawBalloon(beval);
1228
1229 if (beval->timerID != (XtIntervalId)NULL)
1230 {
1231 XtRemoveTimeOut(beval->timerID);
1232 beval->timerID = (XtIntervalId)NULL;
1233 }
1234 beval->showState = ShS_NEUTRAL;
1235}
1236
1237
1238 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001239createBalloonEvalWindow(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001240{
1241 Arg args[12];
1242 int n;
1243
1244 n = 0;
1245#ifdef FEAT_GUI_MOTIF
1246 XtSetArg(args[n], XmNallowShellResize, True); n++;
1247 beval->balloonShell = XtAppCreateShell("balloonEval", "BalloonEval",
1248 overrideShellWidgetClass, gui.dpy, args, n);
1249#else
1250 /* Athena */
1251 XtSetArg(args[n], XtNallowShellResize, True); n++;
1252 beval->balloonShell = XtAppCreateShell("balloonEval", "BalloonEval",
1253 overrideShellWidgetClass, gui.dpy, args, n);
1254#endif
1255
1256 n = 0;
1257#ifdef FEAT_GUI_MOTIF
1258 {
1259 XmFontList fl;
1260
1261 fl = gui_motif_fontset2fontlist(&gui.tooltip_fontset);
1262 XtSetArg(args[n], XmNforeground, gui.tooltip_fg_pixel); n++;
1263 XtSetArg(args[n], XmNbackground, gui.tooltip_bg_pixel); n++;
1264 XtSetArg(args[n], XmNfontList, fl); n++;
1265 XtSetArg(args[n], XmNalignment, XmALIGNMENT_BEGINNING); n++;
1266 beval->balloonLabel = XtCreateManagedWidget("balloonLabel",
1267 xmLabelWidgetClass, beval->balloonShell, args, n);
1268 }
1269#else /* FEAT_GUI_ATHENA */
1270 XtSetArg(args[n], XtNforeground, gui.tooltip_fg_pixel); n++;
1271 XtSetArg(args[n], XtNbackground, gui.tooltip_bg_pixel); n++;
1272 XtSetArg(args[n], XtNinternational, True); n++;
1273 XtSetArg(args[n], XtNfontSet, gui.tooltip_fontset); n++;
1274 beval->balloonLabel = XtCreateManagedWidget("balloonLabel",
1275 labelWidgetClass, beval->balloonShell, args, n);
1276#endif
1277}
1278
1279#endif /* !FEAT_GUI_GTK */
1280#endif /* !FEAT_GUI_W32 */
1281
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01001282#endif /* FEAT_BEVAL_GUI */