blob: 8b90362d790a544b14ae09610c9e16f1447df535 [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 g_signal_connect(G_OBJECT(target), "event",
248 G_CALLBACK(target_event_cb),
249 beval);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000250 /*
251 * Nasty: Key press events go to the main window thus the drawing area
252 * will never see them. This means we have to connect to the main window
253 * as well in order to catch those events.
254 */
255 if (gtk_socket_id == 0 && gui.mainwin != NULL
256 && gtk_widget_is_ancestor(target, gui.mainwin))
257 {
Bram Moolenaar98921892016-02-23 17:14:37 +0100258 g_signal_connect(G_OBJECT(gui.mainwin), "event",
259 G_CALLBACK(mainwin_event_cb),
260 beval);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000261 }
262}
263
264 static void
265removeEventHandler(BalloonEval *beval)
266{
Bram Moolenaar98921892016-02-23 17:14:37 +0100267 g_signal_handlers_disconnect_by_func(G_OBJECT(beval->target),
Bram Moolenaard47d8372016-09-09 22:13:24 +0200268 FUNC2GENERIC(target_event_cb),
Bram Moolenaar98921892016-02-23 17:14:37 +0100269 beval);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000270
271 if (gtk_socket_id == 0 && gui.mainwin != NULL
272 && gtk_widget_is_ancestor(beval->target, gui.mainwin))
273 {
Bram Moolenaar98921892016-02-23 17:14:37 +0100274 g_signal_handlers_disconnect_by_func(G_OBJECT(gui.mainwin),
Bram Moolenaard47d8372016-09-09 22:13:24 +0200275 FUNC2GENERIC(mainwin_event_cb),
Bram Moolenaar98921892016-02-23 17:14:37 +0100276 beval);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000277 }
278}
279
280 static gint
281target_event_cb(GtkWidget *widget, GdkEvent *event, gpointer data)
282{
283 BalloonEval *beval = (BalloonEval *)data;
284
285 switch (event->type)
286 {
287 case GDK_ENTER_NOTIFY:
288 pointer_event(beval, (int)event->crossing.x,
289 (int)event->crossing.y,
290 event->crossing.state);
291 break;
292 case GDK_MOTION_NOTIFY:
293 if (event->motion.is_hint)
294 {
295 int x;
296 int y;
297 GdkModifierType state;
298 /*
299 * GDK_POINTER_MOTION_HINT_MASK is set, thus we cannot obtain
300 * the coordinates from the GdkEventMotion struct directly.
301 */
Bram Moolenaar98921892016-02-23 17:14:37 +0100302# if GTK_CHECK_VERSION(3,0,0)
303 {
304 GdkWindow * const win = gtk_widget_get_window(widget);
305 GdkDisplay * const dpy = gdk_window_get_display(win);
Bram Moolenaar30e12d22016-04-17 20:49:53 +0200306# if GTK_CHECK_VERSION(3,20,0)
307 GdkSeat * const seat = gdk_display_get_default_seat(dpy);
308 GdkDevice * const dev = gdk_seat_get_pointer(seat);
309# else
Bram Moolenaar98921892016-02-23 17:14:37 +0100310 GdkDeviceManager * const mngr = gdk_display_get_device_manager(dpy);
311 GdkDevice * const dev = gdk_device_manager_get_client_pointer(mngr);
Bram Moolenaar30e12d22016-04-17 20:49:53 +0200312# endif
Bram Moolenaar98921892016-02-23 17:14:37 +0100313 gdk_window_get_device_position(win, dev , &x, &y, &state);
314 }
315# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000316 gdk_window_get_pointer(widget->window, &x, &y, &state);
Bram Moolenaar98921892016-02-23 17:14:37 +0100317# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000318 pointer_event(beval, x, y, (unsigned int)state);
319 }
320 else
321 {
322 pointer_event(beval, (int)event->motion.x,
323 (int)event->motion.y,
324 event->motion.state);
325 }
326 break;
327 case GDK_LEAVE_NOTIFY:
328 /*
329 * Ignore LeaveNotify events that are not "normal".
330 * Apparently we also get it when somebody else grabs focus.
331 */
332 if (event->crossing.mode == GDK_CROSSING_NORMAL)
333 cancelBalloon(beval);
334 break;
335 case GDK_BUTTON_PRESS:
Bram Moolenaar071d4272004-06-13 20:20:40 +0000336 case GDK_SCROLL:
Bram Moolenaar071d4272004-06-13 20:20:40 +0000337 cancelBalloon(beval);
338 break;
339 case GDK_KEY_PRESS:
340 key_event(beval, event->key.keyval, TRUE);
341 break;
342 case GDK_KEY_RELEASE:
343 key_event(beval, event->key.keyval, FALSE);
344 break;
345 default:
346 break;
347 }
348
349 return FALSE; /* continue emission */
350}
351
Bram Moolenaar071d4272004-06-13 20:20:40 +0000352 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +0000353mainwin_event_cb(GtkWidget *widget UNUSED, GdkEvent *event, gpointer data)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000354{
355 BalloonEval *beval = (BalloonEval *)data;
356
357 switch (event->type)
358 {
359 case GDK_KEY_PRESS:
360 key_event(beval, event->key.keyval, TRUE);
361 break;
362 case GDK_KEY_RELEASE:
363 key_event(beval, event->key.keyval, FALSE);
364 break;
365 default:
366 break;
367 }
368
369 return FALSE; /* continue emission */
370}
371
372 static void
373pointer_event(BalloonEval *beval, int x, int y, unsigned state)
374{
375 int distance;
376
377 distance = ABS(x - beval->x) + ABS(y - beval->y);
378
379 if (distance > 4)
380 {
381 /*
382 * Moved out of the balloon location: cancel it.
383 * Remember button state
384 */
385 beval->state = state;
386 cancelBalloon(beval);
387
388 /* Mouse buttons are pressed - no balloon now */
389 if (!(state & ((int)GDK_BUTTON1_MASK | (int)GDK_BUTTON2_MASK
390 | (int)GDK_BUTTON3_MASK)))
391 {
392 beval->x = x;
393 beval->y = y;
394
395 if (state & (int)GDK_MOD1_MASK)
396 {
397 /*
398 * Alt is pressed -- enter super-evaluate-mode,
399 * where there is no time delay
400 */
401 if (beval->msgCB != NULL)
402 {
403 beval->showState = ShS_PENDING;
404 (*beval->msgCB)(beval, state);
405 }
406 }
407 else
408 {
Bram Moolenaar98921892016-02-23 17:14:37 +0100409 beval->timerID = g_timeout_add((guint)p_bdlay,
410 &timeout_cb, beval);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000411 }
412 }
413 }
414}
415
416 static void
417key_event(BalloonEval *beval, unsigned keyval, int is_keypress)
418{
419 if (beval->showState == ShS_SHOWING && beval->msgCB != NULL)
420 {
421 switch (keyval)
422 {
423 case GDK_Shift_L:
424 case GDK_Shift_R:
425 beval->showState = ShS_UPDATE_PENDING;
426 (*beval->msgCB)(beval, (is_keypress)
427 ? (int)GDK_SHIFT_MASK : 0);
428 break;
429 case GDK_Control_L:
430 case GDK_Control_R:
431 beval->showState = ShS_UPDATE_PENDING;
432 (*beval->msgCB)(beval, (is_keypress)
433 ? (int)GDK_CONTROL_MASK : 0);
434 break;
435 default:
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000436 /* Don't do this for key release, we apparently get these with
437 * focus changes in some GTK version. */
438 if (is_keypress)
439 cancelBalloon(beval);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000440 break;
441 }
442 }
443 else
444 cancelBalloon(beval);
445}
446
Bram Moolenaar98921892016-02-23 17:14:37 +0100447# if GTK_CHECK_VERSION(3,0,0)
448 static gboolean
449# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000450 static gint
Bram Moolenaar98921892016-02-23 17:14:37 +0100451# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000452timeout_cb(gpointer data)
453{
454 BalloonEval *beval = (BalloonEval *)data;
455
456 beval->timerID = 0;
457 /*
458 * If the timer event happens then the mouse has stopped long enough for
459 * a request to be started. The request will only send to the debugger if
460 * there the mouse is pointing at real data.
461 */
462 requestBalloon(beval);
463
464 return FALSE; /* don't call me again */
465}
466
Bram Moolenaar98921892016-02-23 17:14:37 +0100467# if GTK_CHECK_VERSION(3,0,0)
468 static gboolean
469balloon_draw_event_cb(GtkWidget *widget,
470 cairo_t *cr,
471 gpointer data UNUSED)
472{
473 GtkStyleContext *context = NULL;
474 gint width = -1, height = -1;
475
476 if (widget == NULL)
477 return TRUE;
478
479 context = gtk_widget_get_style_context(widget);
480 width = gtk_widget_get_allocated_width(widget);
481 height = gtk_widget_get_allocated_height(widget);
482
483 gtk_style_context_save(context);
484
485 gtk_style_context_add_class(context, "tooltip");
486 gtk_style_context_set_state(context, GTK_STATE_FLAG_NORMAL);
487
488 cairo_save(cr);
489 gtk_render_frame(context, cr, 0, 0, width, height);
490 gtk_render_background(context, cr, 0, 0, width, height);
491 cairo_restore(cr);
492
493 gtk_style_context_restore(context);
494
495 return FALSE;
496}
497# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000498 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +0000499balloon_expose_event_cb(GtkWidget *widget,
500 GdkEventExpose *event,
501 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000502{
503 gtk_paint_flat_box(widget->style, widget->window,
504 GTK_STATE_NORMAL, GTK_SHADOW_OUT,
505 &event->area, widget, "tooltip",
506 0, 0, -1, -1);
507
508 return FALSE; /* continue emission */
509}
Bram Moolenaar98921892016-02-23 17:14:37 +0100510# endif /* !GTK_CHECK_VERSION(3,0,0) */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000511
Bram Moolenaar071d4272004-06-13 20:20:40 +0000512#else /* !FEAT_GUI_GTK */
513
514 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100515addEventHandler(Widget target, BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000516{
517 XtAddEventHandler(target,
518 PointerMotionMask | EnterWindowMask |
519 LeaveWindowMask | ButtonPressMask | KeyPressMask |
520 KeyReleaseMask,
521 False,
522 pointerEventEH, (XtPointer)beval);
523}
524
525 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100526removeEventHandler(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000527{
528 XtRemoveEventHandler(beval->target,
529 PointerMotionMask | EnterWindowMask |
530 LeaveWindowMask | ButtonPressMask | KeyPressMask |
531 KeyReleaseMask,
532 False,
533 pointerEventEH, (XtPointer)beval);
534}
535
536
537/*
538 * The X event handler. All it does is call the real event handler.
539 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000540 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100541pointerEventEH(
542 Widget w UNUSED,
543 XtPointer client_data,
544 XEvent *event,
545 Boolean *unused UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000546{
547 BalloonEval *beval = (BalloonEval *)client_data;
548 pointerEvent(beval, event);
549}
550
551
552/*
553 * The real event handler. Called by pointerEventEH() whenever an event we are
Bram Moolenaarbae0c162007-05-10 19:30:25 +0000554 * interested in occurs.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000555 */
556
557 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100558pointerEvent(BalloonEval *beval, XEvent *event)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000559{
Bram Moolenaar84a05ac2013-05-06 04:24:17 +0200560 Position distance; /* a measure of how much the pointer moved */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000561 Position delta; /* used to compute distance */
562
563 switch (event->type)
564 {
565 case EnterNotify:
566 case MotionNotify:
567 delta = event->xmotion.x - beval->x;
568 if (delta < 0)
569 delta = -delta;
570 distance = delta;
571 delta = event->xmotion.y - beval->y;
572 if (delta < 0)
573 delta = -delta;
574 distance += delta;
575 if (distance > 4)
576 {
577 /*
578 * Moved out of the balloon location: cancel it.
579 * Remember button state
580 */
581 beval->state = event->xmotion.state;
582 if (beval->state & (Button1Mask|Button2Mask|Button3Mask))
583 {
584 /* Mouse buttons are pressed - no balloon now */
585 cancelBalloon(beval);
586 }
587 else if (beval->state & (Mod1Mask|Mod2Mask|Mod3Mask))
588 {
589 /*
590 * Alt is pressed -- enter super-evaluate-mode,
591 * where there is no time delay
592 */
593 beval->x = event->xmotion.x;
594 beval->y = event->xmotion.y;
595 beval->x_root = event->xmotion.x_root;
596 beval->y_root = event->xmotion.y_root;
597 cancelBalloon(beval);
598 if (beval->msgCB != NULL)
599 {
600 beval->showState = ShS_PENDING;
601 (*beval->msgCB)(beval, beval->state);
602 }
603 }
604 else
605 {
606 beval->x = event->xmotion.x;
607 beval->y = event->xmotion.y;
608 beval->x_root = event->xmotion.x_root;
609 beval->y_root = event->xmotion.y_root;
610 cancelBalloon(beval);
611 beval->timerID = XtAppAddTimeOut( beval->appContext,
612 (long_u)p_bdlay, timerRoutine, beval);
613 }
614 }
615 break;
616
617 /*
618 * Motif and Athena version: Keystrokes will be caught by the
619 * "textArea" widget, and handled in gui_x11_key_hit_cb().
620 */
621 case KeyPress:
622 if (beval->showState == ShS_SHOWING && beval->msgCB != NULL)
623 {
624 Modifiers modifier;
625 KeySym keysym;
626
627 XtTranslateKeycode(gui.dpy,
628 event->xkey.keycode, event->xkey.state,
629 &modifier, &keysym);
630 if (keysym == XK_Shift_L || keysym == XK_Shift_R)
631 {
632 beval->showState = ShS_UPDATE_PENDING;
633 (*beval->msgCB)(beval, ShiftMask);
634 }
635 else if (keysym == XK_Control_L || keysym == XK_Control_R)
636 {
637 beval->showState = ShS_UPDATE_PENDING;
638 (*beval->msgCB)(beval, ControlMask);
639 }
640 else
641 cancelBalloon(beval);
642 }
643 else
644 cancelBalloon(beval);
645 break;
646
647 case KeyRelease:
648 if (beval->showState == ShS_SHOWING && beval->msgCB != NULL)
649 {
650 Modifiers modifier;
651 KeySym keysym;
652
653 XtTranslateKeycode(gui.dpy, event->xkey.keycode,
654 event->xkey.state, &modifier, &keysym);
655 if ((keysym == XK_Shift_L) || (keysym == XK_Shift_R)) {
656 beval->showState = ShS_UPDATE_PENDING;
657 (*beval->msgCB)(beval, 0);
658 }
659 else if ((keysym == XK_Control_L) || (keysym == XK_Control_R))
660 {
661 beval->showState = ShS_UPDATE_PENDING;
662 (*beval->msgCB)(beval, 0);
663 }
664 else
665 cancelBalloon(beval);
666 }
667 else
668 cancelBalloon(beval);
669 break;
670
671 case LeaveNotify:
672 /* Ignore LeaveNotify events that are not "normal".
673 * Apparently we also get it when somebody else grabs focus.
674 * Happens for me every two seconds (some clipboard tool?) */
675 if (event->xcrossing.mode == NotifyNormal)
676 cancelBalloon(beval);
677 break;
678
679 case ButtonPress:
680 cancelBalloon(beval);
681 break;
682
683 default:
684 break;
685 }
686}
687
Bram Moolenaar071d4272004-06-13 20:20:40 +0000688 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100689timerRoutine(XtPointer dx, XtIntervalId *id UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000690{
691 BalloonEval *beval = (BalloonEval *)dx;
692
693 beval->timerID = (XtIntervalId)NULL;
694
695 /*
696 * If the timer event happens then the mouse has stopped long enough for
697 * a request to be started. The request will only send to the debugger if
698 * there the mouse is pointing at real data.
699 */
700 requestBalloon(beval);
701}
702
703#endif /* !FEAT_GUI_GTK */
704
705 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100706requestBalloon(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000707{
708 if (beval->showState != ShS_PENDING)
709 {
710 /* Determine the beval to display */
711 if (beval->msgCB != NULL)
712 {
713 beval->showState = ShS_PENDING;
714 (*beval->msgCB)(beval, beval->state);
715 }
716 else if (beval->msg != NULL)
717 drawBalloon(beval);
718 }
719}
720
721#ifdef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +0000722/*
723 * Convert the string to UTF-8 if 'encoding' is not "utf-8".
724 * Replace any non-printable characters and invalid bytes sequences with
725 * "^X" or "<xx>" escapes, and apply SpecialKey highlighting to them.
726 * TAB and NL are passed through unscathed.
727 */
Bram Moolenaar182c5be2010-06-25 05:37:59 +0200728# define IS_NONPRINTABLE(c) (((c) < 0x20 && (c) != TAB && (c) != NL) \
Bram Moolenaar071d4272004-06-13 20:20:40 +0000729 || (c) == DEL)
730 static void
Bram Moolenaar89d40322006-08-29 15:30:07 +0000731set_printable_label_text(GtkLabel *label, char_u *text)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000732{
733 char_u *convbuf = NULL;
734 char_u *buf;
735 char_u *p;
736 char_u *pdest;
737 unsigned int len;
738 int charlen;
739 int uc;
740 PangoAttrList *attr_list;
741
742 /* Convert to UTF-8 if it isn't already */
743 if (output_conv.vc_type != CONV_NONE)
744 {
Bram Moolenaar89d40322006-08-29 15:30:07 +0000745 convbuf = string_convert(&output_conv, text, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000746 if (convbuf != NULL)
Bram Moolenaar89d40322006-08-29 15:30:07 +0000747 text = convbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000748 }
749
750 /* First let's see how much we need to allocate */
751 len = 0;
Bram Moolenaar89d40322006-08-29 15:30:07 +0000752 for (p = text; *p != NUL; p += charlen)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000753 {
754 if ((*p & 0x80) == 0) /* be quick for ASCII */
755 {
756 charlen = 1;
757 len += IS_NONPRINTABLE(*p) ? 2 : 1; /* nonprintable: ^X */
758 }
759 else
760 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000761 charlen = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000762 uc = utf_ptr2char(p);
763
764 if (charlen != utf_char2len(uc))
765 charlen = 1; /* reject overlong sequences */
766
767 if (charlen == 1 || uc < 0xa0) /* illegal byte or */
768 len += 4; /* control char: <xx> */
769 else if (!utf_printable(uc))
770 /* Note: we assume here that utf_printable() doesn't
771 * care about characters outside the BMP. */
772 len += 6; /* nonprintable: <xxxx> */
773 else
774 len += charlen;
775 }
776 }
777
778 attr_list = pango_attr_list_new();
779 buf = alloc(len + 1);
780
781 /* Now go for the real work */
782 if (buf != NULL)
783 {
784 attrentry_T *aep;
785 PangoAttribute *attr;
786 guicolor_T pixel;
Bram Moolenaar36edf062016-07-21 22:10:12 +0200787#if GTK_CHECK_VERSION(3,0,0)
788 GdkRGBA color = { 0.0, 0.0, 0.0, 1.0 };
Bram Moolenaar870b7492016-07-22 22:26:52 +0200789# if PANGO_VERSION_CHECK(1,38,0)
Bram Moolenaar36edf062016-07-21 22:10:12 +0200790 PangoAttribute *attr_alpha;
Bram Moolenaar870b7492016-07-22 22:26:52 +0200791# endif
Bram Moolenaar36edf062016-07-21 22:10:12 +0200792#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000793 GdkColor color = { 0, 0, 0, 0 };
Bram Moolenaar36edf062016-07-21 22:10:12 +0200794#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000795
796 /* Look up the RGB values of the SpecialKey foreground color. */
Bram Moolenaar8820b482017-03-16 17:23:31 +0100797 aep = syn_gui_attr2entry(HL_ATTR(HLF_8));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000798 pixel = (aep != NULL) ? aep->ae_u.gui.fg_color : INVALCOLOR;
799 if (pixel != INVALCOLOR)
Bram Moolenaar98921892016-02-23 17:14:37 +0100800# if GTK_CHECK_VERSION(3,0,0)
801 {
Bram Moolenaar36edf062016-07-21 22:10:12 +0200802 color.red = ((pixel & 0xff0000) >> 16) / 255.0;
803 color.green = ((pixel & 0xff00) >> 8) / 255.0;
804 color.blue = (pixel & 0xff) / 255.0;
805 color.alpha = 1.0;
Bram Moolenaar98921892016-02-23 17:14:37 +0100806 }
807# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000808 gdk_colormap_query_color(gtk_widget_get_colormap(gui.drawarea),
809 (unsigned long)pixel, &color);
Bram Moolenaar98921892016-02-23 17:14:37 +0100810# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000811
812 pdest = buf;
Bram Moolenaar89d40322006-08-29 15:30:07 +0000813 p = text;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000814 while (*p != NUL)
815 {
816 /* Be quick for ASCII */
817 if ((*p & 0x80) == 0 && !IS_NONPRINTABLE(*p))
818 {
819 *pdest++ = *p++;
820 }
821 else
822 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000823 charlen = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000824 uc = utf_ptr2char(p);
825
826 if (charlen != utf_char2len(uc))
827 charlen = 1; /* reject overlong sequences */
828
829 if (charlen == 1 || uc < 0xa0 || !utf_printable(uc))
830 {
831 int outlen;
832
833 /* Careful: we can't just use transchar_byte() here,
834 * since 'encoding' is not necessarily set to "utf-8". */
835 if (*p & 0x80 && charlen == 1)
836 {
837 transchar_hex(pdest, *p); /* <xx> */
838 outlen = 4;
839 }
840 else if (uc >= 0x80)
841 {
842 /* Note: we assume here that utf_printable() doesn't
843 * care about characters outside the BMP. */
844 transchar_hex(pdest, uc); /* <xx> or <xxxx> */
845 outlen = (uc < 0x100) ? 4 : 6;
846 }
847 else
848 {
849 transchar_nonprint(pdest, *p); /* ^X */
850 outlen = 2;
851 }
852 if (pixel != INVALCOLOR)
853 {
Bram Moolenaar36edf062016-07-21 22:10:12 +0200854#if GTK_CHECK_VERSION(3,0,0)
855# define DOUBLE2UINT16(val) ((guint16)((val) * 65535 + 0.5))
856 attr = pango_attr_foreground_new(
857 DOUBLE2UINT16(color.red),
858 DOUBLE2UINT16(color.green),
859 DOUBLE2UINT16(color.blue));
Bram Moolenaar870b7492016-07-22 22:26:52 +0200860# if PANGO_VERSION_CHECK(1,38,0)
Bram Moolenaar36edf062016-07-21 22:10:12 +0200861 attr_alpha = pango_attr_foreground_alpha_new(
862 DOUBLE2UINT16(color.alpha));
Bram Moolenaar870b7492016-07-22 22:26:52 +0200863# endif
Bram Moolenaar36edf062016-07-21 22:10:12 +0200864# undef DOUBLE2UINT16
865#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000866 attr = pango_attr_foreground_new(
867 color.red, color.green, color.blue);
Bram Moolenaar36edf062016-07-21 22:10:12 +0200868#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000869 attr->start_index = pdest - buf;
870 attr->end_index = pdest - buf + outlen;
871 pango_attr_list_insert(attr_list, attr);
Bram Moolenaar36edf062016-07-21 22:10:12 +0200872#if GTK_CHECK_VERSION(3,0,0)
Bram Moolenaar870b7492016-07-22 22:26:52 +0200873# if PANGO_VERSION_CHECK(1,38,0)
Bram Moolenaar36edf062016-07-21 22:10:12 +0200874 attr_alpha->start_index = pdest - buf;
875 attr_alpha->end_index = pdest - buf + outlen;
876 pango_attr_list_insert(attr_list, attr_alpha);
Bram Moolenaar870b7492016-07-22 22:26:52 +0200877# endif
Bram Moolenaar36edf062016-07-21 22:10:12 +0200878#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000879 }
880 pdest += outlen;
881 p += charlen;
882 }
883 else
884 {
885 do
886 *pdest++ = *p++;
887 while (--charlen != 0);
888 }
889 }
890 }
891 *pdest = NUL;
892 }
893
894 vim_free(convbuf);
895
896 gtk_label_set_text(label, (const char *)buf);
897 vim_free(buf);
898
899 gtk_label_set_attributes(label, attr_list);
900 pango_attr_list_unref(attr_list);
901}
Bram Moolenaar182c5be2010-06-25 05:37:59 +0200902# undef IS_NONPRINTABLE
Bram Moolenaar071d4272004-06-13 20:20:40 +0000903
904/*
905 * Draw a balloon.
906 */
907 static void
908drawBalloon(BalloonEval *beval)
909{
910 if (beval->msg != NULL)
911 {
912 GtkRequisition requisition;
913 int screen_w;
914 int screen_h;
Bram Moolenaar3f6a16f2018-08-19 22:58:45 +0200915 int screen_x;
916 int screen_y;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000917 int x;
918 int y;
919 int x_offset = EVAL_OFFSET_X;
920 int y_offset = EVAL_OFFSET_Y;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000921 PangoLayout *layout;
Bram Moolenaara859f042016-11-17 19:11:55 +0100922
Bram Moolenaar7be9b502017-09-09 18:45:26 +0200923# if !GTK_CHECK_VERSION(3,22,2)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000924 GdkScreen *screen;
925
926 screen = gtk_widget_get_screen(beval->target);
927 gtk_window_set_screen(GTK_WINDOW(beval->balloonShell), screen);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000928# endif
Bram Moolenaara449a7c2018-08-28 23:09:07 +0200929 gui_gtk_get_screen_geom_of_win(beval->target,
Bram Moolenaar3f6a16f2018-08-19 22:58:45 +0200930 &screen_x, &screen_y, &screen_w, &screen_h);
Bram Moolenaar98921892016-02-23 17:14:37 +0100931# if !GTK_CHECK_VERSION(3,0,0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000932 gtk_widget_ensure_style(beval->balloonShell);
933 gtk_widget_ensure_style(beval->balloonLabel);
Bram Moolenaar98921892016-02-23 17:14:37 +0100934# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000935
Bram Moolenaar071d4272004-06-13 20:20:40 +0000936 set_printable_label_text(GTK_LABEL(beval->balloonLabel), beval->msg);
937 /*
938 * Dirty trick: Enable wrapping mode on the label's layout behind its
939 * back. This way GtkLabel won't try to constrain the wrap width to a
940 * builtin maximum value of about 65 Latin characters.
941 */
942 layout = gtk_label_get_layout(GTK_LABEL(beval->balloonLabel));
Bram Moolenaar182c5be2010-06-25 05:37:59 +0200943# ifdef PANGO_WRAP_WORD_CHAR
Bram Moolenaar071d4272004-06-13 20:20:40 +0000944 pango_layout_set_wrap(layout, PANGO_WRAP_WORD_CHAR);
Bram Moolenaar182c5be2010-06-25 05:37:59 +0200945# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000946 pango_layout_set_wrap(layout, PANGO_WRAP_WORD);
Bram Moolenaar182c5be2010-06-25 05:37:59 +0200947# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000948 pango_layout_set_width(layout,
949 /* try to come up with some reasonable width */
950 PANGO_SCALE * CLAMP(gui.num_cols * gui.char_width,
951 screen_w / 2,
952 MAX(20, screen_w - 20)));
953
954 /* Calculate the balloon's width and height. */
Bram Moolenaar98921892016-02-23 17:14:37 +0100955# if GTK_CHECK_VERSION(3,0,0)
956 gtk_widget_get_preferred_size(beval->balloonShell, &requisition, NULL);
957# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000958 gtk_widget_size_request(beval->balloonShell, &requisition);
Bram Moolenaar98921892016-02-23 17:14:37 +0100959# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000960
961 /* Compute position of the balloon area */
Bram Moolenaar98921892016-02-23 17:14:37 +0100962 gdk_window_get_origin(gtk_widget_get_window(beval->target), &x, &y);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000963 x += beval->x;
964 y += beval->y;
965
966 /* Get out of the way of the mouse pointer */
Bram Moolenaar3f6a16f2018-08-19 22:58:45 +0200967 if (x + x_offset + requisition.width > screen_x + screen_w)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000968 y_offset += 15;
Bram Moolenaar3f6a16f2018-08-19 22:58:45 +0200969 if (y + y_offset + requisition.height > screen_y + screen_h)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000970 y_offset = -requisition.height - EVAL_OFFSET_Y;
971
972 /* Sanitize values */
Bram Moolenaar3f6a16f2018-08-19 22:58:45 +0200973 x = CLAMP(x + x_offset, 0,
974 MAX(0, screen_x + screen_w - requisition.width));
975 y = CLAMP(y + y_offset, 0,
976 MAX(0, screen_y + screen_h - requisition.height));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000977
978 /* Show the balloon */
Bram Moolenaar98921892016-02-23 17:14:37 +0100979# if GTK_CHECK_VERSION(3,0,0)
980 gtk_window_move(GTK_WINDOW(beval->balloonShell), x, y);
981# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000982 gtk_widget_set_uposition(beval->balloonShell, x, y);
Bram Moolenaar98921892016-02-23 17:14:37 +0100983# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000984 gtk_widget_show(beval->balloonShell);
985
986 beval->showState = ShS_SHOWING;
987 }
988}
989
990/*
991 * Undraw a balloon.
992 */
993 static void
994undrawBalloon(BalloonEval *beval)
995{
996 if (beval->balloonShell != NULL)
997 gtk_widget_hide(beval->balloonShell);
998 beval->showState = ShS_NEUTRAL;
999}
1000
1001 static void
1002cancelBalloon(BalloonEval *beval)
1003{
1004 if (beval->showState == ShS_SHOWING
1005 || beval->showState == ShS_UPDATE_PENDING)
1006 undrawBalloon(beval);
1007
1008 if (beval->timerID != 0)
1009 {
Bram Moolenaar98921892016-02-23 17:14:37 +01001010 g_source_remove(beval->timerID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001011 beval->timerID = 0;
1012 }
1013 beval->showState = ShS_NEUTRAL;
1014}
1015
1016 static void
1017createBalloonEvalWindow(BalloonEval *beval)
1018{
1019 beval->balloonShell = gtk_window_new(GTK_WINDOW_POPUP);
1020
1021 gtk_widget_set_app_paintable(beval->balloonShell, TRUE);
Bram Moolenaar98921892016-02-23 17:14:37 +01001022 gtk_window_set_resizable(GTK_WINDOW(beval->balloonShell), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001023 gtk_widget_set_name(beval->balloonShell, "gtk-tooltips");
Bram Moolenaar98921892016-02-23 17:14:37 +01001024 gtk_container_set_border_width(GTK_CONTAINER(beval->balloonShell), 4);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001025
Bram Moolenaar98921892016-02-23 17:14:37 +01001026# if GTK_CHECK_VERSION(3,0,0)
1027 g_signal_connect(G_OBJECT(beval->balloonShell), "draw",
1028 G_CALLBACK(balloon_draw_event_cb), NULL);
1029# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001030 gtk_signal_connect((GtkObject*)(beval->balloonShell), "expose_event",
1031 GTK_SIGNAL_FUNC(balloon_expose_event_cb), NULL);
Bram Moolenaar98921892016-02-23 17:14:37 +01001032# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001033 beval->balloonLabel = gtk_label_new(NULL);
1034
1035 gtk_label_set_line_wrap(GTK_LABEL(beval->balloonLabel), FALSE);
1036 gtk_label_set_justify(GTK_LABEL(beval->balloonLabel), GTK_JUSTIFY_LEFT);
Bram Moolenaar98921892016-02-23 17:14:37 +01001037# if GTK_CHECK_VERSION(3,16,0)
1038 gtk_label_set_xalign(GTK_LABEL(beval->balloonLabel), 0.5);
1039 gtk_label_set_yalign(GTK_LABEL(beval->balloonLabel), 0.5);
1040# elif GTK_CHECK_VERSION(3,14,0)
1041 GValue align_val = G_VALUE_INIT;
1042 g_value_init(&align_val, G_TYPE_FLOAT);
1043 g_value_set_float(&align_val, 0.5);
1044 g_object_set_property(G_OBJECT(beval->balloonLabel), "xalign", &align_val);
1045 g_object_set_property(G_OBJECT(beval->balloonLabel), "yalign", &align_val);
1046 g_value_unset(&align_val);
1047# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001048 gtk_misc_set_alignment(GTK_MISC(beval->balloonLabel), 0.5f, 0.5f);
Bram Moolenaar98921892016-02-23 17:14:37 +01001049# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001050 gtk_widget_set_name(beval->balloonLabel, "vim-balloon-label");
1051 gtk_widget_show(beval->balloonLabel);
1052
1053 gtk_container_add(GTK_CONTAINER(beval->balloonShell), beval->balloonLabel);
1054}
1055
1056#else /* !FEAT_GUI_GTK */
1057
1058/*
1059 * Draw a balloon.
1060 */
1061 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001062drawBalloon(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001063{
1064 Dimension w;
1065 Dimension h;
1066 Position tx;
1067 Position ty;
1068
1069 if (beval->msg != NULL)
1070 {
1071 /* Show the Balloon */
1072
1073 /* Calculate the label's width and height */
1074#ifdef FEAT_GUI_MOTIF
1075 XmString s;
1076
1077 /* For the callback function we parse NL characters to create a
1078 * multi-line label. This doesn't work for all languages, but
1079 * XmStringCreateLocalized() doesn't do multi-line labels... */
1080 if (beval->msgCB != NULL)
1081 s = XmStringCreateLtoR((char *)beval->msg, XmFONTLIST_DEFAULT_TAG);
1082 else
1083 s = XmStringCreateLocalized((char *)beval->msg);
1084 {
1085 XmFontList fl;
1086
1087 fl = gui_motif_fontset2fontlist(&gui.tooltip_fontset);
Bram Moolenaardb5ffaa2014-06-25 17:44:49 +02001088 if (fl == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001089 {
Bram Moolenaardb5ffaa2014-06-25 17:44:49 +02001090 XmStringFree(s);
1091 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001092 }
Bram Moolenaardb5ffaa2014-06-25 17:44:49 +02001093 XmStringExtent(fl, s, &w, &h);
1094 XmFontListFree(fl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001095 }
1096 w += gui.border_offset << 1;
1097 h += gui.border_offset << 1;
1098 XtVaSetValues(beval->balloonLabel, XmNlabelString, s, NULL);
1099 XmStringFree(s);
1100#else /* Athena */
1101 /* Assume XtNinternational == True */
1102 XFontSet fset;
1103 XFontSetExtents *ext;
1104
1105 XtVaGetValues(beval->balloonLabel, XtNfontSet, &fset, NULL);
1106 ext = XExtentsOfFontSet(fset);
1107 h = ext->max_ink_extent.height;
1108 w = XmbTextEscapement(fset,
1109 (char *)beval->msg,
1110 (int)STRLEN(beval->msg));
1111 w += gui.border_offset << 1;
1112 h += gui.border_offset << 1;
1113 XtVaSetValues(beval->balloonLabel, XtNlabel, beval->msg, NULL);
1114#endif
1115
1116 /* Compute position of the balloon area */
1117 tx = beval->x_root + EVAL_OFFSET_X;
1118 ty = beval->y_root + EVAL_OFFSET_Y;
1119 if ((tx + w) > beval->screen_width)
1120 tx = beval->screen_width - w;
1121 if ((ty + h) > beval->screen_height)
1122 ty = beval->screen_height - h;
1123#ifdef FEAT_GUI_MOTIF
1124 XtVaSetValues(beval->balloonShell,
1125 XmNx, tx,
1126 XmNy, ty,
1127 NULL);
1128#else
1129 /* Athena */
1130 XtVaSetValues(beval->balloonShell,
1131 XtNx, tx,
1132 XtNy, ty,
1133 NULL);
1134#endif
Bram Moolenaar8281f442009-03-18 11:22:25 +00001135 /* Set tooltip colors */
1136 {
1137 Arg args[2];
1138
1139#ifdef FEAT_GUI_MOTIF
1140 args[0].name = XmNbackground;
1141 args[0].value = gui.tooltip_bg_pixel;
1142 args[1].name = XmNforeground;
1143 args[1].value = gui.tooltip_fg_pixel;
1144#else /* Athena */
1145 args[0].name = XtNbackground;
1146 args[0].value = gui.tooltip_bg_pixel;
1147 args[1].name = XtNforeground;
1148 args[1].value = gui.tooltip_fg_pixel;
1149#endif
1150 XtSetValues(beval->balloonLabel, &args[0], XtNumber(args));
1151 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001152
1153 XtPopup(beval->balloonShell, XtGrabNone);
1154
1155 beval->showState = ShS_SHOWING;
1156
1157 current_beval = beval;
1158 }
1159}
1160
1161/*
1162 * Undraw a balloon.
1163 */
1164 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001165undrawBalloon(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001166{
1167 if (beval->balloonShell != (Widget)0)
1168 XtPopdown(beval->balloonShell);
1169 beval->showState = ShS_NEUTRAL;
1170
1171 current_beval = NULL;
1172}
1173
1174 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001175cancelBalloon(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001176{
1177 if (beval->showState == ShS_SHOWING
1178 || beval->showState == ShS_UPDATE_PENDING)
1179 undrawBalloon(beval);
1180
1181 if (beval->timerID != (XtIntervalId)NULL)
1182 {
1183 XtRemoveTimeOut(beval->timerID);
1184 beval->timerID = (XtIntervalId)NULL;
1185 }
1186 beval->showState = ShS_NEUTRAL;
1187}
1188
1189
1190 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001191createBalloonEvalWindow(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001192{
1193 Arg args[12];
1194 int n;
1195
1196 n = 0;
1197#ifdef FEAT_GUI_MOTIF
1198 XtSetArg(args[n], XmNallowShellResize, True); n++;
1199 beval->balloonShell = XtAppCreateShell("balloonEval", "BalloonEval",
1200 overrideShellWidgetClass, gui.dpy, args, n);
1201#else
1202 /* Athena */
1203 XtSetArg(args[n], XtNallowShellResize, True); n++;
1204 beval->balloonShell = XtAppCreateShell("balloonEval", "BalloonEval",
1205 overrideShellWidgetClass, gui.dpy, args, n);
1206#endif
1207
1208 n = 0;
1209#ifdef FEAT_GUI_MOTIF
1210 {
1211 XmFontList fl;
1212
1213 fl = gui_motif_fontset2fontlist(&gui.tooltip_fontset);
1214 XtSetArg(args[n], XmNforeground, gui.tooltip_fg_pixel); n++;
1215 XtSetArg(args[n], XmNbackground, gui.tooltip_bg_pixel); n++;
1216 XtSetArg(args[n], XmNfontList, fl); n++;
1217 XtSetArg(args[n], XmNalignment, XmALIGNMENT_BEGINNING); n++;
1218 beval->balloonLabel = XtCreateManagedWidget("balloonLabel",
1219 xmLabelWidgetClass, beval->balloonShell, args, n);
1220 }
1221#else /* FEAT_GUI_ATHENA */
1222 XtSetArg(args[n], XtNforeground, gui.tooltip_fg_pixel); n++;
1223 XtSetArg(args[n], XtNbackground, gui.tooltip_bg_pixel); n++;
1224 XtSetArg(args[n], XtNinternational, True); n++;
1225 XtSetArg(args[n], XtNfontSet, gui.tooltip_fontset); n++;
1226 beval->balloonLabel = XtCreateManagedWidget("balloonLabel",
1227 labelWidgetClass, beval->balloonShell, args, n);
1228#endif
1229}
1230
1231#endif /* !FEAT_GUI_GTK */
1232#endif /* !FEAT_GUI_W32 */
1233
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01001234#endif /* FEAT_BEVAL_GUI */