blob: e1e093f375ec9797b9a2be19248c8b8f71bf3a14 [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 Moolenaar4f974752019-02-17 17:44:42 +010016#if !defined(FEAT_GUI_MSWIN) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017
18#ifdef FEAT_GUI_GTK
Bram Moolenaar98921892016-02-23 17:14:37 +010019# if GTK_CHECK_VERSION(3,0,0)
20# include <gdk/gdkkeysyms-compat.h>
21# else
22# include <gdk/gdkkeysyms.h>
23# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024# include <gtk/gtk.h>
25#else
26# include <X11/keysym.h>
27# ifdef FEAT_GUI_MOTIF
28# include <Xm/PushB.h>
29# include <Xm/Separator.h>
30# include <Xm/List.h>
31# include <Xm/Label.h>
32# include <Xm/AtomMgr.h>
33# include <Xm/Protocols.h>
34# else
35 /* Assume Athena */
36# include <X11/Shell.h>
Bram Moolenaar238a5642006-02-21 22:12:05 +000037# ifdef FEAT_GUI_NEXTAW
38# include <X11/neXtaw/Label.h>
39# else
40# include <X11/Xaw/Label.h>
41# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000042# endif
43#endif
44
Bram Moolenaar071d4272004-06-13 20:20:40 +000045#ifndef FEAT_GUI_GTK
46extern Widget vimShell;
47
48/*
49 * Currently, we assume that there can be only one BalloonEval showing
50 * on-screen at any given moment. This variable will hold the currently
51 * showing BalloonEval or NULL if none is showing.
52 */
53static BalloonEval *current_beval = NULL;
54#endif
55
56#ifdef FEAT_GUI_GTK
Bram Moolenaard25c16e2016-01-29 22:13:30 +010057static void addEventHandler(GtkWidget *, BalloonEval *);
58static void removeEventHandler(BalloonEval *);
59static gint target_event_cb(GtkWidget *, GdkEvent *, gpointer);
60static gint mainwin_event_cb(GtkWidget *, GdkEvent *, gpointer);
61static void pointer_event(BalloonEval *, int, int, unsigned);
62static void key_event(BalloonEval *, unsigned, int);
Bram Moolenaar98921892016-02-23 17:14:37 +010063static gboolean timeout_cb(gpointer);
Bram Moolenaar98921892016-02-23 17:14:37 +010064# if GTK_CHECK_VERSION(3,0,0)
65static gboolean balloon_draw_event_cb (GtkWidget *, cairo_t *, gpointer);
66# else
67static gint balloon_expose_event_cb (GtkWidget *, GdkEventExpose *, gpointer);
68# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000069#else
Bram Moolenaard25c16e2016-01-29 22:13:30 +010070static void addEventHandler(Widget, BalloonEval *);
71static void removeEventHandler(BalloonEval *);
72static void pointerEventEH(Widget, XtPointer, XEvent *, Boolean *);
73static void pointerEvent(BalloonEval *, XEvent *);
74static void timerRoutine(XtPointer, XtIntervalId *);
Bram Moolenaar071d4272004-06-13 20:20:40 +000075#endif
Bram Moolenaard25c16e2016-01-29 22:13:30 +010076static void cancelBalloon(BalloonEval *);
77static void requestBalloon(BalloonEval *);
78static void drawBalloon(BalloonEval *);
79static void undrawBalloon(BalloonEval *beval);
80static void createBalloonEvalWindow(BalloonEval *);
Bram Moolenaar071d4272004-06-13 20:20:40 +000081
Bram Moolenaar071d4272004-06-13 20:20:40 +000082/*
83 * Create a balloon-evaluation area for a Widget.
84 * There can be either a "mesg" for a fixed string or "mesgCB" to generate a
85 * message by calling this callback function.
86 * When "mesg" is not NULL it must remain valid for as long as the balloon is
87 * used. It is not freed here.
88 * Returns a pointer to the resulting object (NULL when out of memory).
89 */
90 BalloonEval *
Bram Moolenaar66f948e2016-01-30 16:39:25 +010091gui_mch_create_beval_area(
92 void *target,
93 char_u *mesg,
94 void (*mesgCB)(BalloonEval *, int),
95 void *clientData)
Bram Moolenaar071d4272004-06-13 20:20:40 +000096{
97#ifndef FEAT_GUI_GTK
98 char *display_name; /* get from gui.dpy */
99 int screen_num;
100 char *p;
101#endif
102 BalloonEval *beval;
103
104 if (mesg != NULL && mesgCB != NULL)
105 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100106 iemsg(_("E232: Cannot create BalloonEval with both message and callback"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000107 return NULL;
108 }
109
Bram Moolenaarca4b6132018-06-28 12:05:11 +0200110 beval = (BalloonEval *)alloc_clear(sizeof(BalloonEval));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000111 if (beval != NULL)
112 {
113#ifdef FEAT_GUI_GTK
114 beval->target = GTK_WIDGET(target);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000115#else
116 beval->target = (Widget)target;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000117 beval->appContext = XtWidgetToApplicationContext((Widget)target);
118#endif
119 beval->showState = ShS_NEUTRAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000120 beval->msg = mesg;
121 beval->msgCB = mesgCB;
122 beval->clientData = clientData;
123
124 /*
125 * Set up event handler which will keep its eyes on the pointer,
126 * and when the pointer rests in a certain spot for a given time
127 * interval, show the beval.
128 */
129 addEventHandler(beval->target, beval);
130 createBalloonEvalWindow(beval);
131
132#ifndef FEAT_GUI_GTK
133 /*
134 * Now create and save the screen width and height. Used in drawing.
135 */
136 display_name = DisplayString(gui.dpy);
137 p = strrchr(display_name, '.');
138 if (p != NULL)
139 screen_num = atoi(++p);
140 else
141 screen_num = 0;
142 beval->screen_width = DisplayWidth(gui.dpy, screen_num);
143 beval->screen_height = DisplayHeight(gui.dpy, screen_num);
144#endif
145 }
146
147 return beval;
148}
149
150#if defined(FEAT_BEVAL_TIP) || defined(PROTO)
151/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +0000152 * Destroy a balloon-eval and free its associated memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000153 */
154 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100155gui_mch_destroy_beval_area(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000156{
157 cancelBalloon(beval);
158 removeEventHandler(beval);
159 /* Children will automatically be destroyed */
160# ifdef FEAT_GUI_GTK
161 gtk_widget_destroy(beval->balloonShell);
162# else
163 XtDestroyWidget(beval->balloonShell);
164# endif
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200165# ifdef FEAT_VARTABS
166 if (beval->vts)
167 vim_free(beval->vts);
168# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000169 vim_free(beval);
170}
171#endif
172
173 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100174gui_mch_enable_beval_area(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000175{
176 if (beval != NULL)
177 addEventHandler(beval->target, beval);
178}
179
180 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100181gui_mch_disable_beval_area(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000182{
183 if (beval != NULL)
184 removeEventHandler(beval);
185}
186
187#if defined(FEAT_BEVAL_TIP) || defined(PROTO)
188/*
189 * This function returns the BalloonEval * associated with the currently
190 * displayed tooltip. Returns NULL if there is no tooltip currently showing.
191 *
192 * Assumption: Only one tooltip can be shown at a time.
193 */
194 BalloonEval *
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100195gui_mch_currently_showing_beval(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000196{
197 return current_beval;
198}
199#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +0100200#endif /* !FEAT_GUI_MSWIN */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000201
Bram Moolenaarbb1969b2019-01-17 15:45:25 +0100202#if defined(FEAT_NETBEANS_INTG) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar4f974752019-02-17 17:44:42 +0100203# if !defined(FEAT_GUI_MSWIN) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000204
205/*
206 * Show a balloon with "mesg".
207 */
208 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100209gui_mch_post_balloon(BalloonEval *beval, char_u *mesg)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000210{
211 beval->msg = mesg;
212 if (mesg != NULL)
213 drawBalloon(beval);
214 else
215 undrawBalloon(beval);
216}
Bram Moolenaar4f974752019-02-17 17:44:42 +0100217# endif /* !FEAT_GUI_MSWIN */
Bram Moolenaarbb1969b2019-01-17 15:45:25 +0100218#endif /* FEAT_NETBEANS_INTG || PROTO */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000219
Bram Moolenaar4f974752019-02-17 17:44:42 +0100220#if !defined(FEAT_GUI_MSWIN) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000221#if defined(FEAT_BEVAL_TIP) || defined(PROTO)
222/*
223 * Hide the given balloon.
224 */
225 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100226gui_mch_unpost_balloon(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000227{
228 undrawBalloon(beval);
229}
230#endif
231
232#ifdef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +0000233 static void
234addEventHandler(GtkWidget *target, BalloonEval *beval)
235{
236 /*
237 * Connect to the generic "event" signal instead of the individual
238 * signals for each event type, because the former is emitted earlier.
239 * This allows us to catch events independently of the signal handlers
240 * in gui_gtk_x11.c.
241 */
Bram Moolenaar98921892016-02-23 17:14:37 +0100242 g_signal_connect(G_OBJECT(target), "event",
243 G_CALLBACK(target_event_cb),
244 beval);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000245 /*
246 * Nasty: Key press events go to the main window thus the drawing area
247 * will never see them. This means we have to connect to the main window
248 * as well in order to catch those events.
249 */
250 if (gtk_socket_id == 0 && gui.mainwin != NULL
251 && gtk_widget_is_ancestor(target, gui.mainwin))
252 {
Bram Moolenaar98921892016-02-23 17:14:37 +0100253 g_signal_connect(G_OBJECT(gui.mainwin), "event",
254 G_CALLBACK(mainwin_event_cb),
255 beval);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000256 }
257}
258
259 static void
260removeEventHandler(BalloonEval *beval)
261{
Bram Moolenaar98921892016-02-23 17:14:37 +0100262 g_signal_handlers_disconnect_by_func(G_OBJECT(beval->target),
Bram Moolenaard47d8372016-09-09 22:13:24 +0200263 FUNC2GENERIC(target_event_cb),
Bram Moolenaar98921892016-02-23 17:14:37 +0100264 beval);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000265
266 if (gtk_socket_id == 0 && gui.mainwin != NULL
267 && gtk_widget_is_ancestor(beval->target, gui.mainwin))
268 {
Bram Moolenaar98921892016-02-23 17:14:37 +0100269 g_signal_handlers_disconnect_by_func(G_OBJECT(gui.mainwin),
Bram Moolenaard47d8372016-09-09 22:13:24 +0200270 FUNC2GENERIC(mainwin_event_cb),
Bram Moolenaar98921892016-02-23 17:14:37 +0100271 beval);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000272 }
273}
274
275 static gint
276target_event_cb(GtkWidget *widget, GdkEvent *event, gpointer data)
277{
278 BalloonEval *beval = (BalloonEval *)data;
279
280 switch (event->type)
281 {
282 case GDK_ENTER_NOTIFY:
283 pointer_event(beval, (int)event->crossing.x,
284 (int)event->crossing.y,
285 event->crossing.state);
286 break;
287 case GDK_MOTION_NOTIFY:
288 if (event->motion.is_hint)
289 {
290 int x;
291 int y;
292 GdkModifierType state;
293 /*
294 * GDK_POINTER_MOTION_HINT_MASK is set, thus we cannot obtain
295 * the coordinates from the GdkEventMotion struct directly.
296 */
Bram Moolenaar98921892016-02-23 17:14:37 +0100297# if GTK_CHECK_VERSION(3,0,0)
298 {
299 GdkWindow * const win = gtk_widget_get_window(widget);
300 GdkDisplay * const dpy = gdk_window_get_display(win);
Bram Moolenaar30e12d22016-04-17 20:49:53 +0200301# if GTK_CHECK_VERSION(3,20,0)
302 GdkSeat * const seat = gdk_display_get_default_seat(dpy);
303 GdkDevice * const dev = gdk_seat_get_pointer(seat);
304# else
Bram Moolenaar98921892016-02-23 17:14:37 +0100305 GdkDeviceManager * const mngr = gdk_display_get_device_manager(dpy);
306 GdkDevice * const dev = gdk_device_manager_get_client_pointer(mngr);
Bram Moolenaar30e12d22016-04-17 20:49:53 +0200307# endif
Bram Moolenaar98921892016-02-23 17:14:37 +0100308 gdk_window_get_device_position(win, dev , &x, &y, &state);
309 }
310# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000311 gdk_window_get_pointer(widget->window, &x, &y, &state);
Bram Moolenaar98921892016-02-23 17:14:37 +0100312# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000313 pointer_event(beval, x, y, (unsigned int)state);
314 }
315 else
316 {
317 pointer_event(beval, (int)event->motion.x,
318 (int)event->motion.y,
319 event->motion.state);
320 }
321 break;
322 case GDK_LEAVE_NOTIFY:
323 /*
324 * Ignore LeaveNotify events that are not "normal".
325 * Apparently we also get it when somebody else grabs focus.
326 */
327 if (event->crossing.mode == GDK_CROSSING_NORMAL)
328 cancelBalloon(beval);
329 break;
330 case GDK_BUTTON_PRESS:
Bram Moolenaar071d4272004-06-13 20:20:40 +0000331 case GDK_SCROLL:
Bram Moolenaar071d4272004-06-13 20:20:40 +0000332 cancelBalloon(beval);
333 break;
334 case GDK_KEY_PRESS:
335 key_event(beval, event->key.keyval, TRUE);
336 break;
337 case GDK_KEY_RELEASE:
338 key_event(beval, event->key.keyval, FALSE);
339 break;
340 default:
341 break;
342 }
343
344 return FALSE; /* continue emission */
345}
346
Bram Moolenaar071d4272004-06-13 20:20:40 +0000347 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +0000348mainwin_event_cb(GtkWidget *widget UNUSED, GdkEvent *event, gpointer data)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000349{
350 BalloonEval *beval = (BalloonEval *)data;
351
352 switch (event->type)
353 {
354 case GDK_KEY_PRESS:
355 key_event(beval, event->key.keyval, TRUE);
356 break;
357 case GDK_KEY_RELEASE:
358 key_event(beval, event->key.keyval, FALSE);
359 break;
360 default:
361 break;
362 }
363
364 return FALSE; /* continue emission */
365}
366
367 static void
368pointer_event(BalloonEval *beval, int x, int y, unsigned state)
369{
370 int distance;
371
372 distance = ABS(x - beval->x) + ABS(y - beval->y);
373
374 if (distance > 4)
375 {
376 /*
377 * Moved out of the balloon location: cancel it.
378 * Remember button state
379 */
380 beval->state = state;
381 cancelBalloon(beval);
382
383 /* Mouse buttons are pressed - no balloon now */
384 if (!(state & ((int)GDK_BUTTON1_MASK | (int)GDK_BUTTON2_MASK
385 | (int)GDK_BUTTON3_MASK)))
386 {
387 beval->x = x;
388 beval->y = y;
389
390 if (state & (int)GDK_MOD1_MASK)
391 {
392 /*
393 * Alt is pressed -- enter super-evaluate-mode,
394 * where there is no time delay
395 */
396 if (beval->msgCB != NULL)
397 {
398 beval->showState = ShS_PENDING;
399 (*beval->msgCB)(beval, state);
400 }
401 }
402 else
403 {
Bram Moolenaar98921892016-02-23 17:14:37 +0100404 beval->timerID = g_timeout_add((guint)p_bdlay,
405 &timeout_cb, beval);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000406 }
407 }
408 }
409}
410
411 static void
412key_event(BalloonEval *beval, unsigned keyval, int is_keypress)
413{
414 if (beval->showState == ShS_SHOWING && beval->msgCB != NULL)
415 {
416 switch (keyval)
417 {
418 case GDK_Shift_L:
419 case GDK_Shift_R:
420 beval->showState = ShS_UPDATE_PENDING;
421 (*beval->msgCB)(beval, (is_keypress)
422 ? (int)GDK_SHIFT_MASK : 0);
423 break;
424 case GDK_Control_L:
425 case GDK_Control_R:
426 beval->showState = ShS_UPDATE_PENDING;
427 (*beval->msgCB)(beval, (is_keypress)
428 ? (int)GDK_CONTROL_MASK : 0);
429 break;
430 default:
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000431 /* Don't do this for key release, we apparently get these with
432 * focus changes in some GTK version. */
433 if (is_keypress)
434 cancelBalloon(beval);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000435 break;
436 }
437 }
438 else
439 cancelBalloon(beval);
440}
441
Bram Moolenaar98921892016-02-23 17:14:37 +0100442 static gboolean
Bram Moolenaar071d4272004-06-13 20:20:40 +0000443timeout_cb(gpointer data)
444{
445 BalloonEval *beval = (BalloonEval *)data;
446
447 beval->timerID = 0;
448 /*
449 * If the timer event happens then the mouse has stopped long enough for
450 * a request to be started. The request will only send to the debugger if
451 * there the mouse is pointing at real data.
452 */
453 requestBalloon(beval);
454
455 return FALSE; /* don't call me again */
456}
457
Bram Moolenaar98921892016-02-23 17:14:37 +0100458# if GTK_CHECK_VERSION(3,0,0)
459 static gboolean
460balloon_draw_event_cb(GtkWidget *widget,
461 cairo_t *cr,
462 gpointer data UNUSED)
463{
464 GtkStyleContext *context = NULL;
465 gint width = -1, height = -1;
466
467 if (widget == NULL)
468 return TRUE;
469
470 context = gtk_widget_get_style_context(widget);
471 width = gtk_widget_get_allocated_width(widget);
472 height = gtk_widget_get_allocated_height(widget);
473
474 gtk_style_context_save(context);
475
476 gtk_style_context_add_class(context, "tooltip");
477 gtk_style_context_set_state(context, GTK_STATE_FLAG_NORMAL);
478
479 cairo_save(cr);
480 gtk_render_frame(context, cr, 0, 0, width, height);
481 gtk_render_background(context, cr, 0, 0, width, height);
482 cairo_restore(cr);
483
484 gtk_style_context_restore(context);
485
486 return FALSE;
487}
488# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000489 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +0000490balloon_expose_event_cb(GtkWidget *widget,
491 GdkEventExpose *event,
492 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000493{
494 gtk_paint_flat_box(widget->style, widget->window,
495 GTK_STATE_NORMAL, GTK_SHADOW_OUT,
496 &event->area, widget, "tooltip",
497 0, 0, -1, -1);
498
499 return FALSE; /* continue emission */
500}
Bram Moolenaar98921892016-02-23 17:14:37 +0100501# endif /* !GTK_CHECK_VERSION(3,0,0) */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000502
Bram Moolenaar071d4272004-06-13 20:20:40 +0000503#else /* !FEAT_GUI_GTK */
504
505 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100506addEventHandler(Widget target, BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000507{
508 XtAddEventHandler(target,
509 PointerMotionMask | EnterWindowMask |
510 LeaveWindowMask | ButtonPressMask | KeyPressMask |
511 KeyReleaseMask,
512 False,
513 pointerEventEH, (XtPointer)beval);
514}
515
516 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100517removeEventHandler(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000518{
519 XtRemoveEventHandler(beval->target,
520 PointerMotionMask | EnterWindowMask |
521 LeaveWindowMask | ButtonPressMask | KeyPressMask |
522 KeyReleaseMask,
523 False,
524 pointerEventEH, (XtPointer)beval);
525}
526
527
528/*
529 * The X event handler. All it does is call the real event handler.
530 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000531 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100532pointerEventEH(
533 Widget w UNUSED,
534 XtPointer client_data,
535 XEvent *event,
536 Boolean *unused UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000537{
538 BalloonEval *beval = (BalloonEval *)client_data;
539 pointerEvent(beval, event);
540}
541
542
543/*
544 * The real event handler. Called by pointerEventEH() whenever an event we are
Bram Moolenaarbae0c162007-05-10 19:30:25 +0000545 * interested in occurs.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000546 */
547
548 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100549pointerEvent(BalloonEval *beval, XEvent *event)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000550{
Bram Moolenaar84a05ac2013-05-06 04:24:17 +0200551 Position distance; /* a measure of how much the pointer moved */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000552 Position delta; /* used to compute distance */
553
554 switch (event->type)
555 {
556 case EnterNotify:
557 case MotionNotify:
558 delta = event->xmotion.x - beval->x;
559 if (delta < 0)
560 delta = -delta;
561 distance = delta;
562 delta = event->xmotion.y - beval->y;
563 if (delta < 0)
564 delta = -delta;
565 distance += delta;
566 if (distance > 4)
567 {
568 /*
569 * Moved out of the balloon location: cancel it.
570 * Remember button state
571 */
572 beval->state = event->xmotion.state;
573 if (beval->state & (Button1Mask|Button2Mask|Button3Mask))
574 {
575 /* Mouse buttons are pressed - no balloon now */
576 cancelBalloon(beval);
577 }
578 else if (beval->state & (Mod1Mask|Mod2Mask|Mod3Mask))
579 {
580 /*
581 * Alt is pressed -- enter super-evaluate-mode,
582 * where there is no time delay
583 */
584 beval->x = event->xmotion.x;
585 beval->y = event->xmotion.y;
586 beval->x_root = event->xmotion.x_root;
587 beval->y_root = event->xmotion.y_root;
588 cancelBalloon(beval);
589 if (beval->msgCB != NULL)
590 {
591 beval->showState = ShS_PENDING;
592 (*beval->msgCB)(beval, beval->state);
593 }
594 }
595 else
596 {
597 beval->x = event->xmotion.x;
598 beval->y = event->xmotion.y;
599 beval->x_root = event->xmotion.x_root;
600 beval->y_root = event->xmotion.y_root;
601 cancelBalloon(beval);
602 beval->timerID = XtAppAddTimeOut( beval->appContext,
603 (long_u)p_bdlay, timerRoutine, beval);
604 }
605 }
606 break;
607
608 /*
609 * Motif and Athena version: Keystrokes will be caught by the
610 * "textArea" widget, and handled in gui_x11_key_hit_cb().
611 */
612 case KeyPress:
613 if (beval->showState == ShS_SHOWING && beval->msgCB != NULL)
614 {
615 Modifiers modifier;
616 KeySym keysym;
617
618 XtTranslateKeycode(gui.dpy,
619 event->xkey.keycode, event->xkey.state,
620 &modifier, &keysym);
621 if (keysym == XK_Shift_L || keysym == XK_Shift_R)
622 {
623 beval->showState = ShS_UPDATE_PENDING;
624 (*beval->msgCB)(beval, ShiftMask);
625 }
626 else if (keysym == XK_Control_L || keysym == XK_Control_R)
627 {
628 beval->showState = ShS_UPDATE_PENDING;
629 (*beval->msgCB)(beval, ControlMask);
630 }
631 else
632 cancelBalloon(beval);
633 }
634 else
635 cancelBalloon(beval);
636 break;
637
638 case KeyRelease:
639 if (beval->showState == ShS_SHOWING && beval->msgCB != NULL)
640 {
641 Modifiers modifier;
642 KeySym keysym;
643
644 XtTranslateKeycode(gui.dpy, event->xkey.keycode,
645 event->xkey.state, &modifier, &keysym);
646 if ((keysym == XK_Shift_L) || (keysym == XK_Shift_R)) {
647 beval->showState = ShS_UPDATE_PENDING;
648 (*beval->msgCB)(beval, 0);
649 }
650 else if ((keysym == XK_Control_L) || (keysym == XK_Control_R))
651 {
652 beval->showState = ShS_UPDATE_PENDING;
653 (*beval->msgCB)(beval, 0);
654 }
655 else
656 cancelBalloon(beval);
657 }
658 else
659 cancelBalloon(beval);
660 break;
661
662 case LeaveNotify:
663 /* Ignore LeaveNotify events that are not "normal".
664 * Apparently we also get it when somebody else grabs focus.
665 * Happens for me every two seconds (some clipboard tool?) */
666 if (event->xcrossing.mode == NotifyNormal)
667 cancelBalloon(beval);
668 break;
669
670 case ButtonPress:
671 cancelBalloon(beval);
672 break;
673
674 default:
675 break;
676 }
677}
678
Bram Moolenaar071d4272004-06-13 20:20:40 +0000679 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100680timerRoutine(XtPointer dx, XtIntervalId *id UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000681{
682 BalloonEval *beval = (BalloonEval *)dx;
683
684 beval->timerID = (XtIntervalId)NULL;
685
686 /*
687 * If the timer event happens then the mouse has stopped long enough for
688 * a request to be started. The request will only send to the debugger if
689 * there the mouse is pointing at real data.
690 */
691 requestBalloon(beval);
692}
693
694#endif /* !FEAT_GUI_GTK */
695
696 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100697requestBalloon(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000698{
699 if (beval->showState != ShS_PENDING)
700 {
701 /* Determine the beval to display */
702 if (beval->msgCB != NULL)
703 {
704 beval->showState = ShS_PENDING;
705 (*beval->msgCB)(beval, beval->state);
706 }
707 else if (beval->msg != NULL)
708 drawBalloon(beval);
709 }
710}
711
712#ifdef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +0000713/*
714 * Convert the string to UTF-8 if 'encoding' is not "utf-8".
715 * Replace any non-printable characters and invalid bytes sequences with
716 * "^X" or "<xx>" escapes, and apply SpecialKey highlighting to them.
717 * TAB and NL are passed through unscathed.
718 */
Bram Moolenaar182c5be2010-06-25 05:37:59 +0200719# define IS_NONPRINTABLE(c) (((c) < 0x20 && (c) != TAB && (c) != NL) \
Bram Moolenaar071d4272004-06-13 20:20:40 +0000720 || (c) == DEL)
721 static void
Bram Moolenaar89d40322006-08-29 15:30:07 +0000722set_printable_label_text(GtkLabel *label, char_u *text)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000723{
724 char_u *convbuf = NULL;
725 char_u *buf;
726 char_u *p;
727 char_u *pdest;
728 unsigned int len;
729 int charlen;
730 int uc;
731 PangoAttrList *attr_list;
732
733 /* Convert to UTF-8 if it isn't already */
734 if (output_conv.vc_type != CONV_NONE)
735 {
Bram Moolenaar89d40322006-08-29 15:30:07 +0000736 convbuf = string_convert(&output_conv, text, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000737 if (convbuf != NULL)
Bram Moolenaar89d40322006-08-29 15:30:07 +0000738 text = convbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000739 }
740
741 /* First let's see how much we need to allocate */
742 len = 0;
Bram Moolenaar89d40322006-08-29 15:30:07 +0000743 for (p = text; *p != NUL; p += charlen)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000744 {
745 if ((*p & 0x80) == 0) /* be quick for ASCII */
746 {
747 charlen = 1;
748 len += IS_NONPRINTABLE(*p) ? 2 : 1; /* nonprintable: ^X */
749 }
750 else
751 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000752 charlen = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000753 uc = utf_ptr2char(p);
754
755 if (charlen != utf_char2len(uc))
756 charlen = 1; /* reject overlong sequences */
757
758 if (charlen == 1 || uc < 0xa0) /* illegal byte or */
759 len += 4; /* control char: <xx> */
760 else if (!utf_printable(uc))
761 /* Note: we assume here that utf_printable() doesn't
762 * care about characters outside the BMP. */
763 len += 6; /* nonprintable: <xxxx> */
764 else
765 len += charlen;
766 }
767 }
768
769 attr_list = pango_attr_list_new();
770 buf = alloc(len + 1);
771
772 /* Now go for the real work */
773 if (buf != NULL)
774 {
775 attrentry_T *aep;
776 PangoAttribute *attr;
777 guicolor_T pixel;
Bram Moolenaar36edf062016-07-21 22:10:12 +0200778#if GTK_CHECK_VERSION(3,0,0)
779 GdkRGBA color = { 0.0, 0.0, 0.0, 1.0 };
Bram Moolenaar870b7492016-07-22 22:26:52 +0200780# if PANGO_VERSION_CHECK(1,38,0)
Bram Moolenaar36edf062016-07-21 22:10:12 +0200781 PangoAttribute *attr_alpha;
Bram Moolenaar870b7492016-07-22 22:26:52 +0200782# endif
Bram Moolenaar36edf062016-07-21 22:10:12 +0200783#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000784 GdkColor color = { 0, 0, 0, 0 };
Bram Moolenaar36edf062016-07-21 22:10:12 +0200785#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000786
787 /* Look up the RGB values of the SpecialKey foreground color. */
Bram Moolenaar8820b482017-03-16 17:23:31 +0100788 aep = syn_gui_attr2entry(HL_ATTR(HLF_8));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000789 pixel = (aep != NULL) ? aep->ae_u.gui.fg_color : INVALCOLOR;
790 if (pixel != INVALCOLOR)
Bram Moolenaar98921892016-02-23 17:14:37 +0100791# if GTK_CHECK_VERSION(3,0,0)
792 {
Bram Moolenaar36edf062016-07-21 22:10:12 +0200793 color.red = ((pixel & 0xff0000) >> 16) / 255.0;
794 color.green = ((pixel & 0xff00) >> 8) / 255.0;
795 color.blue = (pixel & 0xff) / 255.0;
796 color.alpha = 1.0;
Bram Moolenaar98921892016-02-23 17:14:37 +0100797 }
798# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000799 gdk_colormap_query_color(gtk_widget_get_colormap(gui.drawarea),
800 (unsigned long)pixel, &color);
Bram Moolenaar98921892016-02-23 17:14:37 +0100801# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000802
803 pdest = buf;
Bram Moolenaar89d40322006-08-29 15:30:07 +0000804 p = text;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000805 while (*p != NUL)
806 {
807 /* Be quick for ASCII */
808 if ((*p & 0x80) == 0 && !IS_NONPRINTABLE(*p))
809 {
810 *pdest++ = *p++;
811 }
812 else
813 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000814 charlen = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000815 uc = utf_ptr2char(p);
816
817 if (charlen != utf_char2len(uc))
818 charlen = 1; /* reject overlong sequences */
819
820 if (charlen == 1 || uc < 0xa0 || !utf_printable(uc))
821 {
822 int outlen;
823
824 /* Careful: we can't just use transchar_byte() here,
825 * since 'encoding' is not necessarily set to "utf-8". */
826 if (*p & 0x80 && charlen == 1)
827 {
828 transchar_hex(pdest, *p); /* <xx> */
829 outlen = 4;
830 }
831 else if (uc >= 0x80)
832 {
833 /* Note: we assume here that utf_printable() doesn't
834 * care about characters outside the BMP. */
835 transchar_hex(pdest, uc); /* <xx> or <xxxx> */
836 outlen = (uc < 0x100) ? 4 : 6;
837 }
838 else
839 {
840 transchar_nonprint(pdest, *p); /* ^X */
841 outlen = 2;
842 }
843 if (pixel != INVALCOLOR)
844 {
Bram Moolenaar36edf062016-07-21 22:10:12 +0200845#if GTK_CHECK_VERSION(3,0,0)
846# define DOUBLE2UINT16(val) ((guint16)((val) * 65535 + 0.5))
847 attr = pango_attr_foreground_new(
848 DOUBLE2UINT16(color.red),
849 DOUBLE2UINT16(color.green),
850 DOUBLE2UINT16(color.blue));
Bram Moolenaar870b7492016-07-22 22:26:52 +0200851# if PANGO_VERSION_CHECK(1,38,0)
Bram Moolenaar36edf062016-07-21 22:10:12 +0200852 attr_alpha = pango_attr_foreground_alpha_new(
853 DOUBLE2UINT16(color.alpha));
Bram Moolenaar870b7492016-07-22 22:26:52 +0200854# endif
Bram Moolenaar36edf062016-07-21 22:10:12 +0200855# undef DOUBLE2UINT16
856#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000857 attr = pango_attr_foreground_new(
858 color.red, color.green, color.blue);
Bram Moolenaar36edf062016-07-21 22:10:12 +0200859#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000860 attr->start_index = pdest - buf;
861 attr->end_index = pdest - buf + outlen;
862 pango_attr_list_insert(attr_list, attr);
Bram Moolenaar36edf062016-07-21 22:10:12 +0200863#if GTK_CHECK_VERSION(3,0,0)
Bram Moolenaar870b7492016-07-22 22:26:52 +0200864# if PANGO_VERSION_CHECK(1,38,0)
Bram Moolenaar36edf062016-07-21 22:10:12 +0200865 attr_alpha->start_index = pdest - buf;
866 attr_alpha->end_index = pdest - buf + outlen;
867 pango_attr_list_insert(attr_list, attr_alpha);
Bram Moolenaar870b7492016-07-22 22:26:52 +0200868# endif
Bram Moolenaar36edf062016-07-21 22:10:12 +0200869#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000870 }
871 pdest += outlen;
872 p += charlen;
873 }
874 else
875 {
876 do
877 *pdest++ = *p++;
878 while (--charlen != 0);
879 }
880 }
881 }
882 *pdest = NUL;
883 }
884
885 vim_free(convbuf);
886
887 gtk_label_set_text(label, (const char *)buf);
888 vim_free(buf);
889
890 gtk_label_set_attributes(label, attr_list);
891 pango_attr_list_unref(attr_list);
892}
Bram Moolenaar182c5be2010-06-25 05:37:59 +0200893# undef IS_NONPRINTABLE
Bram Moolenaar071d4272004-06-13 20:20:40 +0000894
895/*
896 * Draw a balloon.
897 */
898 static void
899drawBalloon(BalloonEval *beval)
900{
901 if (beval->msg != NULL)
902 {
903 GtkRequisition requisition;
904 int screen_w;
905 int screen_h;
Bram Moolenaar3f6a16f2018-08-19 22:58:45 +0200906 int screen_x;
907 int screen_y;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000908 int x;
909 int y;
910 int x_offset = EVAL_OFFSET_X;
911 int y_offset = EVAL_OFFSET_Y;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000912 PangoLayout *layout;
Bram Moolenaara859f042016-11-17 19:11:55 +0100913
Bram Moolenaar7be9b502017-09-09 18:45:26 +0200914# if !GTK_CHECK_VERSION(3,22,2)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000915 GdkScreen *screen;
916
917 screen = gtk_widget_get_screen(beval->target);
918 gtk_window_set_screen(GTK_WINDOW(beval->balloonShell), screen);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000919# endif
Bram Moolenaara449a7c2018-08-28 23:09:07 +0200920 gui_gtk_get_screen_geom_of_win(beval->target,
Bram Moolenaar3f6a16f2018-08-19 22:58:45 +0200921 &screen_x, &screen_y, &screen_w, &screen_h);
Bram Moolenaar98921892016-02-23 17:14:37 +0100922# if !GTK_CHECK_VERSION(3,0,0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000923 gtk_widget_ensure_style(beval->balloonShell);
924 gtk_widget_ensure_style(beval->balloonLabel);
Bram Moolenaar98921892016-02-23 17:14:37 +0100925# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000926
Bram Moolenaar071d4272004-06-13 20:20:40 +0000927 set_printable_label_text(GTK_LABEL(beval->balloonLabel), beval->msg);
928 /*
929 * Dirty trick: Enable wrapping mode on the label's layout behind its
930 * back. This way GtkLabel won't try to constrain the wrap width to a
931 * builtin maximum value of about 65 Latin characters.
932 */
933 layout = gtk_label_get_layout(GTK_LABEL(beval->balloonLabel));
Bram Moolenaar182c5be2010-06-25 05:37:59 +0200934# ifdef PANGO_WRAP_WORD_CHAR
Bram Moolenaar071d4272004-06-13 20:20:40 +0000935 pango_layout_set_wrap(layout, PANGO_WRAP_WORD_CHAR);
Bram Moolenaar182c5be2010-06-25 05:37:59 +0200936# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000937 pango_layout_set_wrap(layout, PANGO_WRAP_WORD);
Bram Moolenaar182c5be2010-06-25 05:37:59 +0200938# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000939 pango_layout_set_width(layout,
940 /* try to come up with some reasonable width */
941 PANGO_SCALE * CLAMP(gui.num_cols * gui.char_width,
942 screen_w / 2,
943 MAX(20, screen_w - 20)));
944
945 /* Calculate the balloon's width and height. */
Bram Moolenaar98921892016-02-23 17:14:37 +0100946# if GTK_CHECK_VERSION(3,0,0)
947 gtk_widget_get_preferred_size(beval->balloonShell, &requisition, NULL);
948# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000949 gtk_widget_size_request(beval->balloonShell, &requisition);
Bram Moolenaar98921892016-02-23 17:14:37 +0100950# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000951
952 /* Compute position of the balloon area */
Bram Moolenaar98921892016-02-23 17:14:37 +0100953 gdk_window_get_origin(gtk_widget_get_window(beval->target), &x, &y);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000954 x += beval->x;
955 y += beval->y;
956
957 /* Get out of the way of the mouse pointer */
Bram Moolenaar3f6a16f2018-08-19 22:58:45 +0200958 if (x + x_offset + requisition.width > screen_x + screen_w)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000959 y_offset += 15;
Bram Moolenaar3f6a16f2018-08-19 22:58:45 +0200960 if (y + y_offset + requisition.height > screen_y + screen_h)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000961 y_offset = -requisition.height - EVAL_OFFSET_Y;
962
963 /* Sanitize values */
Bram Moolenaar3f6a16f2018-08-19 22:58:45 +0200964 x = CLAMP(x + x_offset, 0,
965 MAX(0, screen_x + screen_w - requisition.width));
966 y = CLAMP(y + y_offset, 0,
967 MAX(0, screen_y + screen_h - requisition.height));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000968
969 /* Show the balloon */
Bram Moolenaar98921892016-02-23 17:14:37 +0100970# if GTK_CHECK_VERSION(3,0,0)
971 gtk_window_move(GTK_WINDOW(beval->balloonShell), x, y);
972# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000973 gtk_widget_set_uposition(beval->balloonShell, x, y);
Bram Moolenaar98921892016-02-23 17:14:37 +0100974# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000975 gtk_widget_show(beval->balloonShell);
976
977 beval->showState = ShS_SHOWING;
978 }
979}
980
981/*
982 * Undraw a balloon.
983 */
984 static void
985undrawBalloon(BalloonEval *beval)
986{
987 if (beval->balloonShell != NULL)
988 gtk_widget_hide(beval->balloonShell);
989 beval->showState = ShS_NEUTRAL;
990}
991
992 static void
993cancelBalloon(BalloonEval *beval)
994{
995 if (beval->showState == ShS_SHOWING
996 || beval->showState == ShS_UPDATE_PENDING)
997 undrawBalloon(beval);
998
999 if (beval->timerID != 0)
1000 {
Bram Moolenaar98921892016-02-23 17:14:37 +01001001 g_source_remove(beval->timerID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001002 beval->timerID = 0;
1003 }
1004 beval->showState = ShS_NEUTRAL;
1005}
1006
1007 static void
1008createBalloonEvalWindow(BalloonEval *beval)
1009{
1010 beval->balloonShell = gtk_window_new(GTK_WINDOW_POPUP);
1011
1012 gtk_widget_set_app_paintable(beval->balloonShell, TRUE);
Bram Moolenaar98921892016-02-23 17:14:37 +01001013 gtk_window_set_resizable(GTK_WINDOW(beval->balloonShell), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001014 gtk_widget_set_name(beval->balloonShell, "gtk-tooltips");
Bram Moolenaar98921892016-02-23 17:14:37 +01001015 gtk_container_set_border_width(GTK_CONTAINER(beval->balloonShell), 4);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001016
Bram Moolenaar98921892016-02-23 17:14:37 +01001017# if GTK_CHECK_VERSION(3,0,0)
1018 g_signal_connect(G_OBJECT(beval->balloonShell), "draw",
1019 G_CALLBACK(balloon_draw_event_cb), NULL);
1020# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001021 gtk_signal_connect((GtkObject*)(beval->balloonShell), "expose_event",
1022 GTK_SIGNAL_FUNC(balloon_expose_event_cb), NULL);
Bram Moolenaar98921892016-02-23 17:14:37 +01001023# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001024 beval->balloonLabel = gtk_label_new(NULL);
1025
1026 gtk_label_set_line_wrap(GTK_LABEL(beval->balloonLabel), FALSE);
1027 gtk_label_set_justify(GTK_LABEL(beval->balloonLabel), GTK_JUSTIFY_LEFT);
Bram Moolenaar98921892016-02-23 17:14:37 +01001028# if GTK_CHECK_VERSION(3,16,0)
1029 gtk_label_set_xalign(GTK_LABEL(beval->balloonLabel), 0.5);
1030 gtk_label_set_yalign(GTK_LABEL(beval->balloonLabel), 0.5);
1031# elif GTK_CHECK_VERSION(3,14,0)
1032 GValue align_val = G_VALUE_INIT;
1033 g_value_init(&align_val, G_TYPE_FLOAT);
1034 g_value_set_float(&align_val, 0.5);
1035 g_object_set_property(G_OBJECT(beval->balloonLabel), "xalign", &align_val);
1036 g_object_set_property(G_OBJECT(beval->balloonLabel), "yalign", &align_val);
1037 g_value_unset(&align_val);
1038# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001039 gtk_misc_set_alignment(GTK_MISC(beval->balloonLabel), 0.5f, 0.5f);
Bram Moolenaar98921892016-02-23 17:14:37 +01001040# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001041 gtk_widget_set_name(beval->balloonLabel, "vim-balloon-label");
1042 gtk_widget_show(beval->balloonLabel);
1043
1044 gtk_container_add(GTK_CONTAINER(beval->balloonShell), beval->balloonLabel);
1045}
1046
1047#else /* !FEAT_GUI_GTK */
1048
1049/*
1050 * Draw a balloon.
1051 */
1052 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001053drawBalloon(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001054{
1055 Dimension w;
1056 Dimension h;
1057 Position tx;
1058 Position ty;
1059
1060 if (beval->msg != NULL)
1061 {
1062 /* Show the Balloon */
1063
1064 /* Calculate the label's width and height */
1065#ifdef FEAT_GUI_MOTIF
1066 XmString s;
1067
1068 /* For the callback function we parse NL characters to create a
1069 * multi-line label. This doesn't work for all languages, but
1070 * XmStringCreateLocalized() doesn't do multi-line labels... */
1071 if (beval->msgCB != NULL)
1072 s = XmStringCreateLtoR((char *)beval->msg, XmFONTLIST_DEFAULT_TAG);
1073 else
1074 s = XmStringCreateLocalized((char *)beval->msg);
1075 {
1076 XmFontList fl;
1077
1078 fl = gui_motif_fontset2fontlist(&gui.tooltip_fontset);
Bram Moolenaardb5ffaa2014-06-25 17:44:49 +02001079 if (fl == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001080 {
Bram Moolenaardb5ffaa2014-06-25 17:44:49 +02001081 XmStringFree(s);
1082 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001083 }
Bram Moolenaardb5ffaa2014-06-25 17:44:49 +02001084 XmStringExtent(fl, s, &w, &h);
1085 XmFontListFree(fl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001086 }
1087 w += gui.border_offset << 1;
1088 h += gui.border_offset << 1;
1089 XtVaSetValues(beval->balloonLabel, XmNlabelString, s, NULL);
1090 XmStringFree(s);
1091#else /* Athena */
1092 /* Assume XtNinternational == True */
1093 XFontSet fset;
1094 XFontSetExtents *ext;
1095
1096 XtVaGetValues(beval->balloonLabel, XtNfontSet, &fset, NULL);
1097 ext = XExtentsOfFontSet(fset);
1098 h = ext->max_ink_extent.height;
1099 w = XmbTextEscapement(fset,
1100 (char *)beval->msg,
1101 (int)STRLEN(beval->msg));
1102 w += gui.border_offset << 1;
1103 h += gui.border_offset << 1;
1104 XtVaSetValues(beval->balloonLabel, XtNlabel, beval->msg, NULL);
1105#endif
1106
1107 /* Compute position of the balloon area */
1108 tx = beval->x_root + EVAL_OFFSET_X;
1109 ty = beval->y_root + EVAL_OFFSET_Y;
1110 if ((tx + w) > beval->screen_width)
1111 tx = beval->screen_width - w;
1112 if ((ty + h) > beval->screen_height)
1113 ty = beval->screen_height - h;
1114#ifdef FEAT_GUI_MOTIF
1115 XtVaSetValues(beval->balloonShell,
1116 XmNx, tx,
1117 XmNy, ty,
1118 NULL);
1119#else
1120 /* Athena */
1121 XtVaSetValues(beval->balloonShell,
1122 XtNx, tx,
1123 XtNy, ty,
1124 NULL);
1125#endif
Bram Moolenaar8281f442009-03-18 11:22:25 +00001126 /* Set tooltip colors */
1127 {
1128 Arg args[2];
1129
1130#ifdef FEAT_GUI_MOTIF
1131 args[0].name = XmNbackground;
1132 args[0].value = gui.tooltip_bg_pixel;
1133 args[1].name = XmNforeground;
1134 args[1].value = gui.tooltip_fg_pixel;
1135#else /* Athena */
1136 args[0].name = XtNbackground;
1137 args[0].value = gui.tooltip_bg_pixel;
1138 args[1].name = XtNforeground;
1139 args[1].value = gui.tooltip_fg_pixel;
1140#endif
1141 XtSetValues(beval->balloonLabel, &args[0], XtNumber(args));
1142 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001143
1144 XtPopup(beval->balloonShell, XtGrabNone);
1145
1146 beval->showState = ShS_SHOWING;
1147
1148 current_beval = beval;
1149 }
1150}
1151
1152/*
1153 * Undraw a balloon.
1154 */
1155 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001156undrawBalloon(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001157{
1158 if (beval->balloonShell != (Widget)0)
1159 XtPopdown(beval->balloonShell);
1160 beval->showState = ShS_NEUTRAL;
1161
1162 current_beval = NULL;
1163}
1164
1165 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001166cancelBalloon(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001167{
1168 if (beval->showState == ShS_SHOWING
1169 || beval->showState == ShS_UPDATE_PENDING)
1170 undrawBalloon(beval);
1171
1172 if (beval->timerID != (XtIntervalId)NULL)
1173 {
1174 XtRemoveTimeOut(beval->timerID);
1175 beval->timerID = (XtIntervalId)NULL;
1176 }
1177 beval->showState = ShS_NEUTRAL;
1178}
1179
1180
1181 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001182createBalloonEvalWindow(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001183{
1184 Arg args[12];
1185 int n;
1186
1187 n = 0;
1188#ifdef FEAT_GUI_MOTIF
1189 XtSetArg(args[n], XmNallowShellResize, True); n++;
1190 beval->balloonShell = XtAppCreateShell("balloonEval", "BalloonEval",
1191 overrideShellWidgetClass, gui.dpy, args, n);
1192#else
1193 /* Athena */
1194 XtSetArg(args[n], XtNallowShellResize, True); n++;
1195 beval->balloonShell = XtAppCreateShell("balloonEval", "BalloonEval",
1196 overrideShellWidgetClass, gui.dpy, args, n);
1197#endif
1198
1199 n = 0;
1200#ifdef FEAT_GUI_MOTIF
1201 {
1202 XmFontList fl;
1203
1204 fl = gui_motif_fontset2fontlist(&gui.tooltip_fontset);
1205 XtSetArg(args[n], XmNforeground, gui.tooltip_fg_pixel); n++;
1206 XtSetArg(args[n], XmNbackground, gui.tooltip_bg_pixel); n++;
1207 XtSetArg(args[n], XmNfontList, fl); n++;
1208 XtSetArg(args[n], XmNalignment, XmALIGNMENT_BEGINNING); n++;
1209 beval->balloonLabel = XtCreateManagedWidget("balloonLabel",
1210 xmLabelWidgetClass, beval->balloonShell, args, n);
1211 }
1212#else /* FEAT_GUI_ATHENA */
1213 XtSetArg(args[n], XtNforeground, gui.tooltip_fg_pixel); n++;
1214 XtSetArg(args[n], XtNbackground, gui.tooltip_bg_pixel); n++;
1215 XtSetArg(args[n], XtNinternational, True); n++;
1216 XtSetArg(args[n], XtNfontSet, gui.tooltip_fontset); n++;
1217 beval->balloonLabel = XtCreateManagedWidget("balloonLabel",
1218 labelWidgetClass, beval->balloonShell, args, n);
1219#endif
1220}
1221
1222#endif /* !FEAT_GUI_GTK */
Bram Moolenaar4f974752019-02-17 17:44:42 +01001223#endif /* !FEAT_GUI_MSWIN */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001224
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01001225#endif /* FEAT_BEVAL_GUI */