blob: 9d85d90e0056388d44aa9525a0f5ce7e3d491e9a [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
Bram Moolenaar30613902019-12-01 22:11:18 +010015// 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>
Bram Moolenaar0b962e52022-04-03 18:02:37 +010027# include <Xm/PushB.h>
28# include <Xm/Separator.h>
29# include <Xm/List.h>
30# include <Xm/Label.h>
31# include <Xm/AtomMgr.h>
32# include <Xm/Protocols.h>
Bram Moolenaar071d4272004-06-13 20:20:40 +000033#endif
34
Bram Moolenaar071d4272004-06-13 20:20:40 +000035#ifndef FEAT_GUI_GTK
36extern Widget vimShell;
37
38/*
39 * Currently, we assume that there can be only one BalloonEval showing
40 * on-screen at any given moment. This variable will hold the currently
41 * showing BalloonEval or NULL if none is showing.
42 */
43static BalloonEval *current_beval = NULL;
44#endif
45
46#ifdef FEAT_GUI_GTK
Bram Moolenaard25c16e2016-01-29 22:13:30 +010047static void addEventHandler(GtkWidget *, BalloonEval *);
48static void removeEventHandler(BalloonEval *);
49static gint target_event_cb(GtkWidget *, GdkEvent *, gpointer);
50static gint mainwin_event_cb(GtkWidget *, GdkEvent *, gpointer);
51static void pointer_event(BalloonEval *, int, int, unsigned);
52static void key_event(BalloonEval *, unsigned, int);
Bram Moolenaar98921892016-02-23 17:14:37 +010053static gboolean timeout_cb(gpointer);
Bram Moolenaar98921892016-02-23 17:14:37 +010054# if GTK_CHECK_VERSION(3,0,0)
55static gboolean balloon_draw_event_cb (GtkWidget *, cairo_t *, gpointer);
56# else
57static gint balloon_expose_event_cb (GtkWidget *, GdkEventExpose *, gpointer);
58# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000059#else
Bram Moolenaard25c16e2016-01-29 22:13:30 +010060static void addEventHandler(Widget, BalloonEval *);
61static void removeEventHandler(BalloonEval *);
62static void pointerEventEH(Widget, XtPointer, XEvent *, Boolean *);
63static void pointerEvent(BalloonEval *, XEvent *);
64static void timerRoutine(XtPointer, XtIntervalId *);
Bram Moolenaar071d4272004-06-13 20:20:40 +000065#endif
Bram Moolenaard25c16e2016-01-29 22:13:30 +010066static void cancelBalloon(BalloonEval *);
67static void requestBalloon(BalloonEval *);
68static void drawBalloon(BalloonEval *);
69static void undrawBalloon(BalloonEval *beval);
70static void createBalloonEvalWindow(BalloonEval *);
Bram Moolenaar071d4272004-06-13 20:20:40 +000071
Bram Moolenaar071d4272004-06-13 20:20:40 +000072/*
73 * Create a balloon-evaluation area for a Widget.
74 * There can be either a "mesg" for a fixed string or "mesgCB" to generate a
75 * message by calling this callback function.
76 * When "mesg" is not NULL it must remain valid for as long as the balloon is
77 * used. It is not freed here.
78 * Returns a pointer to the resulting object (NULL when out of memory).
79 */
80 BalloonEval *
Bram Moolenaar66f948e2016-01-30 16:39:25 +010081gui_mch_create_beval_area(
82 void *target,
83 char_u *mesg,
84 void (*mesgCB)(BalloonEval *, int),
85 void *clientData)
Bram Moolenaar071d4272004-06-13 20:20:40 +000086{
87#ifndef FEAT_GUI_GTK
Bram Moolenaar30613902019-12-01 22:11:18 +010088 char *display_name; // get from gui.dpy
Bram Moolenaar071d4272004-06-13 20:20:40 +000089 int screen_num;
90 char *p;
91#endif
92 BalloonEval *beval;
93
94 if (mesg != NULL && mesgCB != NULL)
95 {
Bram Moolenaarcbadefe2022-01-01 19:33:50 +000096 iemsg(_(e_cannot_create_ballooneval_with_both_message_and_callback));
Bram Moolenaar071d4272004-06-13 20:20:40 +000097 return NULL;
98 }
99
Bram Moolenaare809a4e2019-07-04 17:35:05 +0200100 beval = ALLOC_CLEAR_ONE(BalloonEval);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000101 if (beval != NULL)
102 {
103#ifdef FEAT_GUI_GTK
104 beval->target = GTK_WIDGET(target);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000105#else
106 beval->target = (Widget)target;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000107 beval->appContext = XtWidgetToApplicationContext((Widget)target);
108#endif
109 beval->showState = ShS_NEUTRAL;
Bram Moolenaarbe0a2592019-05-09 13:50:16 +0200110 vim_free(beval->msg);
111 beval->msg = mesg == NULL ? NULL : vim_strsave(mesg);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000112 beval->msgCB = mesgCB;
113 beval->clientData = clientData;
114
115 /*
116 * Set up event handler which will keep its eyes on the pointer,
117 * and when the pointer rests in a certain spot for a given time
118 * interval, show the beval.
119 */
120 addEventHandler(beval->target, beval);
121 createBalloonEvalWindow(beval);
122
123#ifndef FEAT_GUI_GTK
124 /*
125 * Now create and save the screen width and height. Used in drawing.
126 */
127 display_name = DisplayString(gui.dpy);
128 p = strrchr(display_name, '.');
129 if (p != NULL)
130 screen_num = atoi(++p);
131 else
132 screen_num = 0;
133 beval->screen_width = DisplayWidth(gui.dpy, screen_num);
134 beval->screen_height = DisplayHeight(gui.dpy, screen_num);
135#endif
136 }
137
138 return beval;
139}
140
141#if defined(FEAT_BEVAL_TIP) || defined(PROTO)
142/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +0000143 * Destroy a balloon-eval and free its associated memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000144 */
145 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100146gui_mch_destroy_beval_area(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000147{
148 cancelBalloon(beval);
149 removeEventHandler(beval);
Bram Moolenaar30613902019-12-01 22:11:18 +0100150 // Children will automatically be destroyed
Bram Moolenaar071d4272004-06-13 20:20:40 +0000151# ifdef FEAT_GUI_GTK
152 gtk_widget_destroy(beval->balloonShell);
153# else
154 XtDestroyWidget(beval->balloonShell);
155# endif
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200156# ifdef FEAT_VARTABS
157 if (beval->vts)
158 vim_free(beval->vts);
159# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000160 vim_free(beval);
161}
162#endif
163
164 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100165gui_mch_enable_beval_area(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000166{
167 if (beval != NULL)
168 addEventHandler(beval->target, beval);
169}
170
171 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100172gui_mch_disable_beval_area(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000173{
174 if (beval != NULL)
175 removeEventHandler(beval);
176}
177
178#if defined(FEAT_BEVAL_TIP) || defined(PROTO)
179/*
180 * This function returns the BalloonEval * associated with the currently
181 * displayed tooltip. Returns NULL if there is no tooltip currently showing.
182 *
183 * Assumption: Only one tooltip can be shown at a time.
184 */
185 BalloonEval *
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100186gui_mch_currently_showing_beval(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000187{
188 return current_beval;
189}
190#endif
Bram Moolenaar30613902019-12-01 22:11:18 +0100191#endif // !FEAT_GUI_MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +0000192
Bram Moolenaarbb1969b2019-01-17 15:45:25 +0100193#if defined(FEAT_NETBEANS_INTG) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar4f974752019-02-17 17:44:42 +0100194# if !defined(FEAT_GUI_MSWIN) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000195
196/*
197 * Show a balloon with "mesg".
198 */
199 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100200gui_mch_post_balloon(BalloonEval *beval, char_u *mesg)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000201{
Bram Moolenaarbe0a2592019-05-09 13:50:16 +0200202 vim_free(beval->msg);
203 beval->msg = mesg == NULL ? NULL : vim_strsave(mesg);
204 if (beval->msg != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000205 drawBalloon(beval);
206 else
207 undrawBalloon(beval);
208}
Bram Moolenaar30613902019-12-01 22:11:18 +0100209# endif // !FEAT_GUI_MSWIN
210#endif // FEAT_NETBEANS_INTG || PROTO
Bram Moolenaar071d4272004-06-13 20:20:40 +0000211
Bram Moolenaar4f974752019-02-17 17:44:42 +0100212#if !defined(FEAT_GUI_MSWIN) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000213#if defined(FEAT_BEVAL_TIP) || defined(PROTO)
214/*
215 * Hide the given balloon.
216 */
217 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100218gui_mch_unpost_balloon(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000219{
Bram Moolenaarbe0a2592019-05-09 13:50:16 +0200220 VIM_CLEAR(beval->msg);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000221 undrawBalloon(beval);
222}
223#endif
224
225#ifdef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +0000226 static void
227addEventHandler(GtkWidget *target, BalloonEval *beval)
228{
229 /*
230 * Connect to the generic "event" signal instead of the individual
231 * signals for each event type, because the former is emitted earlier.
232 * This allows us to catch events independently of the signal handlers
233 * in gui_gtk_x11.c.
234 */
Bram Moolenaar98921892016-02-23 17:14:37 +0100235 g_signal_connect(G_OBJECT(target), "event",
236 G_CALLBACK(target_event_cb),
237 beval);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000238 /*
239 * Nasty: Key press events go to the main window thus the drawing area
240 * will never see them. This means we have to connect to the main window
241 * as well in order to catch those events.
242 */
243 if (gtk_socket_id == 0 && gui.mainwin != NULL
244 && gtk_widget_is_ancestor(target, gui.mainwin))
245 {
Bram Moolenaar47f6db92021-06-14 22:08:46 +0200246 gtk_widget_add_events(gui.mainwin,
247 GDK_LEAVE_NOTIFY_MASK);
248
Bram Moolenaar98921892016-02-23 17:14:37 +0100249 g_signal_connect(G_OBJECT(gui.mainwin), "event",
250 G_CALLBACK(mainwin_event_cb),
251 beval);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000252 }
253}
254
255 static void
256removeEventHandler(BalloonEval *beval)
257{
Bram Moolenaar98921892016-02-23 17:14:37 +0100258 g_signal_handlers_disconnect_by_func(G_OBJECT(beval->target),
Bram Moolenaard47d8372016-09-09 22:13:24 +0200259 FUNC2GENERIC(target_event_cb),
Bram Moolenaar98921892016-02-23 17:14:37 +0100260 beval);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000261
262 if (gtk_socket_id == 0 && gui.mainwin != NULL
263 && gtk_widget_is_ancestor(beval->target, gui.mainwin))
264 {
Bram Moolenaar98921892016-02-23 17:14:37 +0100265 g_signal_handlers_disconnect_by_func(G_OBJECT(gui.mainwin),
Bram Moolenaard47d8372016-09-09 22:13:24 +0200266 FUNC2GENERIC(mainwin_event_cb),
Bram Moolenaar98921892016-02-23 17:14:37 +0100267 beval);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000268 }
269}
270
271 static gint
272target_event_cb(GtkWidget *widget, GdkEvent *event, gpointer data)
273{
274 BalloonEval *beval = (BalloonEval *)data;
275
276 switch (event->type)
277 {
278 case GDK_ENTER_NOTIFY:
279 pointer_event(beval, (int)event->crossing.x,
280 (int)event->crossing.y,
281 event->crossing.state);
282 break;
283 case GDK_MOTION_NOTIFY:
284 if (event->motion.is_hint)
285 {
286 int x;
287 int y;
288 GdkModifierType state;
289 /*
290 * GDK_POINTER_MOTION_HINT_MASK is set, thus we cannot obtain
291 * the coordinates from the GdkEventMotion struct directly.
292 */
Bram Moolenaar98921892016-02-23 17:14:37 +0100293# if GTK_CHECK_VERSION(3,0,0)
294 {
295 GdkWindow * const win = gtk_widget_get_window(widget);
296 GdkDisplay * const dpy = gdk_window_get_display(win);
Bram Moolenaar30e12d22016-04-17 20:49:53 +0200297# if GTK_CHECK_VERSION(3,20,0)
298 GdkSeat * const seat = gdk_display_get_default_seat(dpy);
299 GdkDevice * const dev = gdk_seat_get_pointer(seat);
300# else
Bram Moolenaar98921892016-02-23 17:14:37 +0100301 GdkDeviceManager * const mngr = gdk_display_get_device_manager(dpy);
302 GdkDevice * const dev = gdk_device_manager_get_client_pointer(mngr);
Bram Moolenaar30e12d22016-04-17 20:49:53 +0200303# endif
Bram Moolenaar98921892016-02-23 17:14:37 +0100304 gdk_window_get_device_position(win, dev , &x, &y, &state);
305 }
306# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000307 gdk_window_get_pointer(widget->window, &x, &y, &state);
Bram Moolenaar98921892016-02-23 17:14:37 +0100308# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000309 pointer_event(beval, x, y, (unsigned int)state);
310 }
311 else
312 {
313 pointer_event(beval, (int)event->motion.x,
314 (int)event->motion.y,
315 event->motion.state);
316 }
317 break;
318 case GDK_LEAVE_NOTIFY:
319 /*
320 * Ignore LeaveNotify events that are not "normal".
321 * Apparently we also get it when somebody else grabs focus.
322 */
323 if (event->crossing.mode == GDK_CROSSING_NORMAL)
324 cancelBalloon(beval);
325 break;
326 case GDK_BUTTON_PRESS:
Bram Moolenaar071d4272004-06-13 20:20:40 +0000327 case GDK_SCROLL:
Bram Moolenaar071d4272004-06-13 20:20:40 +0000328 cancelBalloon(beval);
329 break;
330 case GDK_KEY_PRESS:
331 key_event(beval, event->key.keyval, TRUE);
332 break;
333 case GDK_KEY_RELEASE:
334 key_event(beval, event->key.keyval, FALSE);
335 break;
336 default:
337 break;
338 }
339
Bram Moolenaar30613902019-12-01 22:11:18 +0100340 return FALSE; // continue emission
Bram Moolenaar071d4272004-06-13 20:20:40 +0000341}
342
Bram Moolenaar071d4272004-06-13 20:20:40 +0000343 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +0000344mainwin_event_cb(GtkWidget *widget UNUSED, GdkEvent *event, gpointer data)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000345{
346 BalloonEval *beval = (BalloonEval *)data;
347
348 switch (event->type)
349 {
350 case GDK_KEY_PRESS:
351 key_event(beval, event->key.keyval, TRUE);
352 break;
353 case GDK_KEY_RELEASE:
354 key_event(beval, event->key.keyval, FALSE);
355 break;
Bram Moolenaar47f6db92021-06-14 22:08:46 +0200356 case GDK_LEAVE_NOTIFY:
357 // Ignore LeaveNotify events that are not "normal".
358 // Apparently we also get it when somebody else grabs focus.
359 if (event->crossing.mode == GDK_CROSSING_NORMAL)
360 cancelBalloon(beval);
361 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000362 default:
363 break;
364 }
365
Bram Moolenaar30613902019-12-01 22:11:18 +0100366 return FALSE; // continue emission
Bram Moolenaar071d4272004-06-13 20:20:40 +0000367}
368
369 static void
370pointer_event(BalloonEval *beval, int x, int y, unsigned state)
371{
372 int distance;
373
374 distance = ABS(x - beval->x) + ABS(y - beval->y);
375
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +0000376 if (distance <= 4)
377 return;
378
379 /*
380 * Moved out of the balloon location: cancel it.
381 * Remember button state
382 */
383 beval->state = state;
384 cancelBalloon(beval);
385
386 // Mouse buttons are pressed - no balloon now
387 if (!(state & ((int)GDK_BUTTON1_MASK | (int)GDK_BUTTON2_MASK
388 | (int)GDK_BUTTON3_MASK)))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000389 {
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +0000390 beval->x = x;
391 beval->y = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000392
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +0000393 if (state & (int)GDK_MOD1_MASK)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000394 {
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +0000395 /*
396 * Alt is pressed -- enter super-evaluate-mode,
397 * where there is no time delay
398 */
399 if (beval->msgCB != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000400 {
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +0000401 beval->showState = ShS_PENDING;
402 (*beval->msgCB)(beval, state);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000403 }
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +0000404 }
405 else
406 {
407 beval->timerID = g_timeout_add((guint)p_bdlay,
408 &timeout_cb, beval);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000409 }
410 }
411}
412
413 static void
414key_event(BalloonEval *beval, unsigned keyval, int is_keypress)
415{
416 if (beval->showState == ShS_SHOWING && beval->msgCB != NULL)
417 {
418 switch (keyval)
419 {
420 case GDK_Shift_L:
421 case GDK_Shift_R:
422 beval->showState = ShS_UPDATE_PENDING;
423 (*beval->msgCB)(beval, (is_keypress)
424 ? (int)GDK_SHIFT_MASK : 0);
425 break;
426 case GDK_Control_L:
427 case GDK_Control_R:
428 beval->showState = ShS_UPDATE_PENDING;
429 (*beval->msgCB)(beval, (is_keypress)
430 ? (int)GDK_CONTROL_MASK : 0);
431 break;
432 default:
Bram Moolenaar30613902019-12-01 22:11:18 +0100433 // Don't do this for key release, we apparently get these with
434 // focus changes in some GTK version.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000435 if (is_keypress)
436 cancelBalloon(beval);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000437 break;
438 }
439 }
440 else
441 cancelBalloon(beval);
442}
443
Bram Moolenaar98921892016-02-23 17:14:37 +0100444 static gboolean
Bram Moolenaar071d4272004-06-13 20:20:40 +0000445timeout_cb(gpointer data)
446{
447 BalloonEval *beval = (BalloonEval *)data;
448
449 beval->timerID = 0;
450 /*
451 * If the timer event happens then the mouse has stopped long enough for
452 * a request to be started. The request will only send to the debugger if
453 * there the mouse is pointing at real data.
454 */
455 requestBalloon(beval);
456
Bram Moolenaar30613902019-12-01 22:11:18 +0100457 return FALSE; // don't call me again
Bram Moolenaar071d4272004-06-13 20:20:40 +0000458}
459
Bram Moolenaar98921892016-02-23 17:14:37 +0100460# if GTK_CHECK_VERSION(3,0,0)
461 static gboolean
462balloon_draw_event_cb(GtkWidget *widget,
463 cairo_t *cr,
464 gpointer data UNUSED)
465{
466 GtkStyleContext *context = NULL;
467 gint width = -1, height = -1;
468
469 if (widget == NULL)
470 return TRUE;
471
472 context = gtk_widget_get_style_context(widget);
473 width = gtk_widget_get_allocated_width(widget);
474 height = gtk_widget_get_allocated_height(widget);
475
476 gtk_style_context_save(context);
477
478 gtk_style_context_add_class(context, "tooltip");
479 gtk_style_context_set_state(context, GTK_STATE_FLAG_NORMAL);
480
481 cairo_save(cr);
482 gtk_render_frame(context, cr, 0, 0, width, height);
483 gtk_render_background(context, cr, 0, 0, width, height);
484 cairo_restore(cr);
485
486 gtk_style_context_restore(context);
487
488 return FALSE;
489}
490# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000491 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +0000492balloon_expose_event_cb(GtkWidget *widget,
493 GdkEventExpose *event,
494 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000495{
496 gtk_paint_flat_box(widget->style, widget->window,
497 GTK_STATE_NORMAL, GTK_SHADOW_OUT,
498 &event->area, widget, "tooltip",
499 0, 0, -1, -1);
500
Bram Moolenaar30613902019-12-01 22:11:18 +0100501 return FALSE; // continue emission
Bram Moolenaar071d4272004-06-13 20:20:40 +0000502}
Bram Moolenaar30613902019-12-01 22:11:18 +0100503# endif // !GTK_CHECK_VERSION(3,0,0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000504
Bram Moolenaar30613902019-12-01 22:11:18 +0100505#else // !FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +0000506
507 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100508addEventHandler(Widget target, BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000509{
510 XtAddEventHandler(target,
511 PointerMotionMask | EnterWindowMask |
512 LeaveWindowMask | ButtonPressMask | KeyPressMask |
513 KeyReleaseMask,
514 False,
515 pointerEventEH, (XtPointer)beval);
516}
517
518 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100519removeEventHandler(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000520{
521 XtRemoveEventHandler(beval->target,
522 PointerMotionMask | EnterWindowMask |
523 LeaveWindowMask | ButtonPressMask | KeyPressMask |
524 KeyReleaseMask,
525 False,
526 pointerEventEH, (XtPointer)beval);
527}
528
529
530/*
531 * The X event handler. All it does is call the real event handler.
532 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000533 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100534pointerEventEH(
535 Widget w UNUSED,
536 XtPointer client_data,
537 XEvent *event,
538 Boolean *unused UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000539{
540 BalloonEval *beval = (BalloonEval *)client_data;
541 pointerEvent(beval, event);
542}
543
544
545/*
546 * The real event handler. Called by pointerEventEH() whenever an event we are
Bram Moolenaarbae0c162007-05-10 19:30:25 +0000547 * interested in occurs.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000548 */
549
550 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100551pointerEvent(BalloonEval *beval, XEvent *event)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000552{
Bram Moolenaar30613902019-12-01 22:11:18 +0100553 Position distance; // a measure of how much the pointer moved
554 Position delta; // used to compute distance
Bram Moolenaar071d4272004-06-13 20:20:40 +0000555
556 switch (event->type)
557 {
558 case EnterNotify:
559 case MotionNotify:
560 delta = event->xmotion.x - beval->x;
561 if (delta < 0)
562 delta = -delta;
563 distance = delta;
564 delta = event->xmotion.y - beval->y;
565 if (delta < 0)
566 delta = -delta;
567 distance += delta;
568 if (distance > 4)
569 {
570 /*
571 * Moved out of the balloon location: cancel it.
572 * Remember button state
573 */
574 beval->state = event->xmotion.state;
575 if (beval->state & (Button1Mask|Button2Mask|Button3Mask))
576 {
Bram Moolenaar30613902019-12-01 22:11:18 +0100577 // Mouse buttons are pressed - no balloon now
Bram Moolenaar071d4272004-06-13 20:20:40 +0000578 cancelBalloon(beval);
579 }
580 else if (beval->state & (Mod1Mask|Mod2Mask|Mod3Mask))
581 {
582 /*
583 * Alt is pressed -- enter super-evaluate-mode,
584 * where there is no time delay
585 */
586 beval->x = event->xmotion.x;
587 beval->y = event->xmotion.y;
588 beval->x_root = event->xmotion.x_root;
589 beval->y_root = event->xmotion.y_root;
590 cancelBalloon(beval);
591 if (beval->msgCB != NULL)
592 {
593 beval->showState = ShS_PENDING;
594 (*beval->msgCB)(beval, beval->state);
595 }
596 }
597 else
598 {
599 beval->x = event->xmotion.x;
600 beval->y = event->xmotion.y;
601 beval->x_root = event->xmotion.x_root;
602 beval->y_root = event->xmotion.y_root;
603 cancelBalloon(beval);
604 beval->timerID = XtAppAddTimeOut( beval->appContext,
605 (long_u)p_bdlay, timerRoutine, beval);
606 }
607 }
608 break;
609
610 /*
Bram Moolenaar0b962e52022-04-03 18:02:37 +0100611 * Motif version: Keystrokes will be caught by the
Bram Moolenaar071d4272004-06-13 20:20:40 +0000612 * "textArea" widget, and handled in gui_x11_key_hit_cb().
613 */
614 case KeyPress:
615 if (beval->showState == ShS_SHOWING && beval->msgCB != NULL)
616 {
617 Modifiers modifier;
618 KeySym keysym;
619
620 XtTranslateKeycode(gui.dpy,
621 event->xkey.keycode, event->xkey.state,
622 &modifier, &keysym);
623 if (keysym == XK_Shift_L || keysym == XK_Shift_R)
624 {
625 beval->showState = ShS_UPDATE_PENDING;
626 (*beval->msgCB)(beval, ShiftMask);
627 }
628 else if (keysym == XK_Control_L || keysym == XK_Control_R)
629 {
630 beval->showState = ShS_UPDATE_PENDING;
631 (*beval->msgCB)(beval, ControlMask);
632 }
633 else
634 cancelBalloon(beval);
635 }
636 else
637 cancelBalloon(beval);
638 break;
639
640 case KeyRelease:
641 if (beval->showState == ShS_SHOWING && beval->msgCB != NULL)
642 {
643 Modifiers modifier;
644 KeySym keysym;
645
646 XtTranslateKeycode(gui.dpy, event->xkey.keycode,
647 event->xkey.state, &modifier, &keysym);
Bram Moolenaarebfec1c2023-01-22 21:14:53 +0000648 if ((keysym == XK_Shift_L) || (keysym == XK_Shift_R))
649 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000650 beval->showState = ShS_UPDATE_PENDING;
651 (*beval->msgCB)(beval, 0);
652 }
653 else if ((keysym == XK_Control_L) || (keysym == XK_Control_R))
654 {
655 beval->showState = ShS_UPDATE_PENDING;
656 (*beval->msgCB)(beval, 0);
657 }
658 else
659 cancelBalloon(beval);
660 }
661 else
662 cancelBalloon(beval);
663 break;
664
665 case LeaveNotify:
Bram Moolenaar30613902019-12-01 22:11:18 +0100666 // Ignore LeaveNotify events that are not "normal".
667 // Apparently we also get it when somebody else grabs focus.
668 // Happens for me every two seconds (some clipboard tool?)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000669 if (event->xcrossing.mode == NotifyNormal)
670 cancelBalloon(beval);
671 break;
672
673 case ButtonPress:
674 cancelBalloon(beval);
675 break;
676
677 default:
678 break;
679 }
680}
681
Bram Moolenaar071d4272004-06-13 20:20:40 +0000682 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100683timerRoutine(XtPointer dx, XtIntervalId *id UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000684{
685 BalloonEval *beval = (BalloonEval *)dx;
686
687 beval->timerID = (XtIntervalId)NULL;
688
689 /*
690 * If the timer event happens then the mouse has stopped long enough for
691 * a request to be started. The request will only send to the debugger if
692 * there the mouse is pointing at real data.
693 */
694 requestBalloon(beval);
695}
696
Bram Moolenaar30613902019-12-01 22:11:18 +0100697#endif // !FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +0000698
699 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100700requestBalloon(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000701{
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +0000702 if (beval->showState == ShS_PENDING)
703 return;
704
705 // Determine the beval to display
706 if (beval->msgCB != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000707 {
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +0000708 beval->showState = ShS_PENDING;
709 (*beval->msgCB)(beval, beval->state);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000710 }
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +0000711 else if (beval->msg != NULL)
712 drawBalloon(beval);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000713}
714
715#ifdef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +0000716/*
717 * Convert the string to UTF-8 if 'encoding' is not "utf-8".
718 * Replace any non-printable characters and invalid bytes sequences with
719 * "^X" or "<xx>" escapes, and apply SpecialKey highlighting to them.
720 * TAB and NL are passed through unscathed.
721 */
Bram Moolenaar182c5be2010-06-25 05:37:59 +0200722# define IS_NONPRINTABLE(c) (((c) < 0x20 && (c) != TAB && (c) != NL) \
Bram Moolenaar071d4272004-06-13 20:20:40 +0000723 || (c) == DEL)
724 static void
Bram Moolenaar89d40322006-08-29 15:30:07 +0000725set_printable_label_text(GtkLabel *label, char_u *text)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000726{
727 char_u *convbuf = NULL;
728 char_u *buf;
729 char_u *p;
730 char_u *pdest;
731 unsigned int len;
732 int charlen;
733 int uc;
734 PangoAttrList *attr_list;
735
Bram Moolenaar30613902019-12-01 22:11:18 +0100736 // Convert to UTF-8 if it isn't already
Bram Moolenaar071d4272004-06-13 20:20:40 +0000737 if (output_conv.vc_type != CONV_NONE)
738 {
Bram Moolenaar89d40322006-08-29 15:30:07 +0000739 convbuf = string_convert(&output_conv, text, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000740 if (convbuf != NULL)
Bram Moolenaar89d40322006-08-29 15:30:07 +0000741 text = convbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000742 }
743
Bram Moolenaar30613902019-12-01 22:11:18 +0100744 // First let's see how much we need to allocate
Bram Moolenaar071d4272004-06-13 20:20:40 +0000745 len = 0;
Bram Moolenaar89d40322006-08-29 15:30:07 +0000746 for (p = text; *p != NUL; p += charlen)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000747 {
Bram Moolenaar30613902019-12-01 22:11:18 +0100748 if ((*p & 0x80) == 0) // be quick for ASCII
Bram Moolenaar071d4272004-06-13 20:20:40 +0000749 {
750 charlen = 1;
Bram Moolenaar30613902019-12-01 22:11:18 +0100751 len += IS_NONPRINTABLE(*p) ? 2 : 1; // nonprintable: ^X
Bram Moolenaar071d4272004-06-13 20:20:40 +0000752 }
753 else
754 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000755 charlen = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000756 uc = utf_ptr2char(p);
757
758 if (charlen != utf_char2len(uc))
Bram Moolenaar30613902019-12-01 22:11:18 +0100759 charlen = 1; // reject overlong sequences
Bram Moolenaar071d4272004-06-13 20:20:40 +0000760
Bram Moolenaar30613902019-12-01 22:11:18 +0100761 if (charlen == 1 || uc < 0xa0) // illegal byte or
762 len += 4; // control char: <xx>
Bram Moolenaar071d4272004-06-13 20:20:40 +0000763 else if (!utf_printable(uc))
Bram Moolenaar30613902019-12-01 22:11:18 +0100764 // Note: we assume here that utf_printable() doesn't
765 // care about characters outside the BMP.
766 len += 6; // nonprintable: <xxxx>
Bram Moolenaar071d4272004-06-13 20:20:40 +0000767 else
768 len += charlen;
769 }
770 }
771
772 attr_list = pango_attr_list_new();
773 buf = alloc(len + 1);
774
Bram Moolenaar30613902019-12-01 22:11:18 +0100775 // Now go for the real work
Bram Moolenaar071d4272004-06-13 20:20:40 +0000776 if (buf != NULL)
777 {
778 attrentry_T *aep;
779 PangoAttribute *attr;
780 guicolor_T pixel;
Bram Moolenaar36edf062016-07-21 22:10:12 +0200781#if GTK_CHECK_VERSION(3,0,0)
782 GdkRGBA color = { 0.0, 0.0, 0.0, 1.0 };
Bram Moolenaar870b7492016-07-22 22:26:52 +0200783# if PANGO_VERSION_CHECK(1,38,0)
Bram Moolenaar36edf062016-07-21 22:10:12 +0200784 PangoAttribute *attr_alpha;
Bram Moolenaar870b7492016-07-22 22:26:52 +0200785# endif
Bram Moolenaar36edf062016-07-21 22:10:12 +0200786#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000787 GdkColor color = { 0, 0, 0, 0 };
Bram Moolenaar36edf062016-07-21 22:10:12 +0200788#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000789
Bram Moolenaar30613902019-12-01 22:11:18 +0100790 // Look up the RGB values of the SpecialKey foreground color.
Bram Moolenaar8820b482017-03-16 17:23:31 +0100791 aep = syn_gui_attr2entry(HL_ATTR(HLF_8));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000792 pixel = (aep != NULL) ? aep->ae_u.gui.fg_color : INVALCOLOR;
793 if (pixel != INVALCOLOR)
Bram Moolenaar98921892016-02-23 17:14:37 +0100794# if GTK_CHECK_VERSION(3,0,0)
795 {
Bram Moolenaar36edf062016-07-21 22:10:12 +0200796 color.red = ((pixel & 0xff0000) >> 16) / 255.0;
797 color.green = ((pixel & 0xff00) >> 8) / 255.0;
798 color.blue = (pixel & 0xff) / 255.0;
799 color.alpha = 1.0;
Bram Moolenaar98921892016-02-23 17:14:37 +0100800 }
801# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000802 gdk_colormap_query_color(gtk_widget_get_colormap(gui.drawarea),
803 (unsigned long)pixel, &color);
Bram Moolenaar98921892016-02-23 17:14:37 +0100804# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000805
806 pdest = buf;
Bram Moolenaar89d40322006-08-29 15:30:07 +0000807 p = text;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000808 while (*p != NUL)
809 {
Bram Moolenaar30613902019-12-01 22:11:18 +0100810 // Be quick for ASCII
Bram Moolenaar071d4272004-06-13 20:20:40 +0000811 if ((*p & 0x80) == 0 && !IS_NONPRINTABLE(*p))
812 {
813 *pdest++ = *p++;
814 }
815 else
816 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000817 charlen = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000818 uc = utf_ptr2char(p);
819
820 if (charlen != utf_char2len(uc))
Bram Moolenaar30613902019-12-01 22:11:18 +0100821 charlen = 1; // reject overlong sequences
Bram Moolenaar071d4272004-06-13 20:20:40 +0000822
823 if (charlen == 1 || uc < 0xa0 || !utf_printable(uc))
824 {
825 int outlen;
826
Bram Moolenaar30613902019-12-01 22:11:18 +0100827 // Careful: we can't just use transchar_byte() here,
828 // since 'encoding' is not necessarily set to "utf-8".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000829 if (*p & 0x80 && charlen == 1)
830 {
Bram Moolenaar30613902019-12-01 22:11:18 +0100831 transchar_hex(pdest, *p); // <xx>
Bram Moolenaar071d4272004-06-13 20:20:40 +0000832 outlen = 4;
833 }
834 else if (uc >= 0x80)
835 {
Bram Moolenaar30613902019-12-01 22:11:18 +0100836 // Note: we assume here that utf_printable() doesn't
837 // care about characters outside the BMP.
838 transchar_hex(pdest, uc); // <xx> or <xxxx>
Bram Moolenaar071d4272004-06-13 20:20:40 +0000839 outlen = (uc < 0x100) ? 4 : 6;
840 }
841 else
842 {
Bram Moolenaar32ee6272020-06-10 14:16:49 +0200843 transchar_nonprint(curbuf, pdest, *p); // ^X
Bram Moolenaar071d4272004-06-13 20:20:40 +0000844 outlen = 2;
845 }
846 if (pixel != INVALCOLOR)
847 {
Bram Moolenaar36edf062016-07-21 22:10:12 +0200848#if GTK_CHECK_VERSION(3,0,0)
849# define DOUBLE2UINT16(val) ((guint16)((val) * 65535 + 0.5))
850 attr = pango_attr_foreground_new(
851 DOUBLE2UINT16(color.red),
852 DOUBLE2UINT16(color.green),
853 DOUBLE2UINT16(color.blue));
Bram Moolenaar870b7492016-07-22 22:26:52 +0200854# if PANGO_VERSION_CHECK(1,38,0)
Bram Moolenaar36edf062016-07-21 22:10:12 +0200855 attr_alpha = pango_attr_foreground_alpha_new(
856 DOUBLE2UINT16(color.alpha));
Bram Moolenaar870b7492016-07-22 22:26:52 +0200857# endif
Bram Moolenaar36edf062016-07-21 22:10:12 +0200858# undef DOUBLE2UINT16
859#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000860 attr = pango_attr_foreground_new(
861 color.red, color.green, color.blue);
Bram Moolenaar36edf062016-07-21 22:10:12 +0200862#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000863 attr->start_index = pdest - buf;
864 attr->end_index = pdest - buf + outlen;
865 pango_attr_list_insert(attr_list, attr);
Bram Moolenaar36edf062016-07-21 22:10:12 +0200866#if GTK_CHECK_VERSION(3,0,0)
Bram Moolenaar870b7492016-07-22 22:26:52 +0200867# if PANGO_VERSION_CHECK(1,38,0)
Bram Moolenaar36edf062016-07-21 22:10:12 +0200868 attr_alpha->start_index = pdest - buf;
869 attr_alpha->end_index = pdest - buf + outlen;
870 pango_attr_list_insert(attr_list, attr_alpha);
Bram Moolenaar870b7492016-07-22 22:26:52 +0200871# endif
Bram Moolenaar36edf062016-07-21 22:10:12 +0200872#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000873 }
874 pdest += outlen;
875 p += charlen;
876 }
877 else
878 {
879 do
880 *pdest++ = *p++;
881 while (--charlen != 0);
882 }
883 }
884 }
885 *pdest = NUL;
886 }
887
888 vim_free(convbuf);
889
890 gtk_label_set_text(label, (const char *)buf);
891 vim_free(buf);
892
893 gtk_label_set_attributes(label, attr_list);
894 pango_attr_list_unref(attr_list);
895}
Bram Moolenaar182c5be2010-06-25 05:37:59 +0200896# undef IS_NONPRINTABLE
Bram Moolenaar071d4272004-06-13 20:20:40 +0000897
898/*
899 * Draw a balloon.
900 */
901 static void
902drawBalloon(BalloonEval *beval)
903{
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +0000904 if (beval->msg == NULL)
905 return;
906
907 GtkRequisition requisition;
908 int screen_w;
909 int screen_h;
910 int screen_x;
911 int screen_y;
912 int x;
913 int y;
914 int x_offset = EVAL_OFFSET_X;
915 int y_offset = EVAL_OFFSET_Y;
916 PangoLayout *layout;
Bram Moolenaara859f042016-11-17 19:11:55 +0100917
Bram Moolenaar7be9b502017-09-09 18:45:26 +0200918# if !GTK_CHECK_VERSION(3,22,2)
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +0000919 GdkScreen *screen;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000920
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +0000921 screen = gtk_widget_get_screen(beval->target);
922 gtk_window_set_screen(GTK_WINDOW(beval->balloonShell), screen);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000923# endif
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +0000924 gui_gtk_get_screen_geom_of_win(beval->target, 0, 0,
925 &screen_x, &screen_y, &screen_w, &screen_h);
Bram Moolenaar98921892016-02-23 17:14:37 +0100926# if !GTK_CHECK_VERSION(3,0,0)
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +0000927 gtk_widget_ensure_style(beval->balloonShell);
928 gtk_widget_ensure_style(beval->balloonLabel);
Bram Moolenaar98921892016-02-23 17:14:37 +0100929# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000930
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +0000931 set_printable_label_text(GTK_LABEL(beval->balloonLabel), beval->msg);
932 /*
933 * Dirty trick: Enable wrapping mode on the label's layout behind its
934 * back. This way GtkLabel won't try to constrain the wrap width to a
935 * builtin maximum value of about 65 Latin characters.
936 */
937 layout = gtk_label_get_layout(GTK_LABEL(beval->balloonLabel));
Bram Moolenaar182c5be2010-06-25 05:37:59 +0200938# ifdef PANGO_WRAP_WORD_CHAR
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +0000939 pango_layout_set_wrap(layout, PANGO_WRAP_WORD_CHAR);
Bram Moolenaar182c5be2010-06-25 05:37:59 +0200940# else
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +0000941 pango_layout_set_wrap(layout, PANGO_WRAP_WORD);
Bram Moolenaar182c5be2010-06-25 05:37:59 +0200942# endif
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +0000943 pango_layout_set_width(layout,
944 // try to come up with some reasonable width
945 PANGO_SCALE * CLAMP(gui.num_cols * gui.char_width,
946 screen_w / 2,
947 MAX(20, screen_w - 20)));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000948
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +0000949 // Calculate the balloon's width and height.
Bram Moolenaar98921892016-02-23 17:14:37 +0100950# if GTK_CHECK_VERSION(3,0,0)
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +0000951 gtk_widget_get_preferred_size(beval->balloonShell, &requisition, NULL);
Bram Moolenaar98921892016-02-23 17:14:37 +0100952# else
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +0000953 gtk_widget_size_request(beval->balloonShell, &requisition);
Bram Moolenaar98921892016-02-23 17:14:37 +0100954# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000955
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +0000956 // Compute position of the balloon area
957 gdk_window_get_origin(gtk_widget_get_window(beval->target), &x, &y);
958 x += beval->x;
959 y += beval->y;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000960
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +0000961 // Get out of the way of the mouse pointer
962 if (x + x_offset + requisition.width > screen_x + screen_w)
963 y_offset += 15;
964 if (y + y_offset + requisition.height > screen_y + screen_h)
965 y_offset = -requisition.height - EVAL_OFFSET_Y;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000966
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +0000967 // Sanitize values
968 x = CLAMP(x + x_offset, 0,
969 MAX(0, screen_x + screen_w - requisition.width));
970 y = CLAMP(y + y_offset, 0,
971 MAX(0, screen_y + screen_h - requisition.height));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000972
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +0000973 // Show the balloon
Bram Moolenaar98921892016-02-23 17:14:37 +0100974# if GTK_CHECK_VERSION(3,0,0)
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +0000975 gtk_window_move(GTK_WINDOW(beval->balloonShell), x, y);
Bram Moolenaar98921892016-02-23 17:14:37 +0100976# else
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +0000977 gtk_widget_set_uposition(beval->balloonShell, x, y);
Bram Moolenaar98921892016-02-23 17:14:37 +0100978# endif
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +0000979 gtk_widget_show(beval->balloonShell);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000980
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +0000981 beval->showState = ShS_SHOWING;
982 gui_mch_update();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000983}
984
985/*
986 * Undraw a balloon.
987 */
988 static void
989undrawBalloon(BalloonEval *beval)
990{
991 if (beval->balloonShell != NULL)
992 gtk_widget_hide(beval->balloonShell);
993 beval->showState = ShS_NEUTRAL;
994}
995
996 static void
997cancelBalloon(BalloonEval *beval)
998{
999 if (beval->showState == ShS_SHOWING
1000 || beval->showState == ShS_UPDATE_PENDING)
1001 undrawBalloon(beval);
1002
1003 if (beval->timerID != 0)
1004 {
Bram Moolenaar98921892016-02-23 17:14:37 +01001005 g_source_remove(beval->timerID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001006 beval->timerID = 0;
1007 }
1008 beval->showState = ShS_NEUTRAL;
1009}
1010
1011 static void
1012createBalloonEvalWindow(BalloonEval *beval)
1013{
1014 beval->balloonShell = gtk_window_new(GTK_WINDOW_POPUP);
1015
1016 gtk_widget_set_app_paintable(beval->balloonShell, TRUE);
Bram Moolenaar98921892016-02-23 17:14:37 +01001017 gtk_window_set_resizable(GTK_WINDOW(beval->balloonShell), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001018 gtk_widget_set_name(beval->balloonShell, "gtk-tooltips");
Bram Moolenaar98921892016-02-23 17:14:37 +01001019 gtk_container_set_border_width(GTK_CONTAINER(beval->balloonShell), 4);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001020
Bram Moolenaar98921892016-02-23 17:14:37 +01001021# if GTK_CHECK_VERSION(3,0,0)
1022 g_signal_connect(G_OBJECT(beval->balloonShell), "draw",
1023 G_CALLBACK(balloon_draw_event_cb), NULL);
1024# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001025 gtk_signal_connect((GtkObject*)(beval->balloonShell), "expose_event",
1026 GTK_SIGNAL_FUNC(balloon_expose_event_cb), NULL);
Bram Moolenaar98921892016-02-23 17:14:37 +01001027# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001028 beval->balloonLabel = gtk_label_new(NULL);
1029
1030 gtk_label_set_line_wrap(GTK_LABEL(beval->balloonLabel), FALSE);
1031 gtk_label_set_justify(GTK_LABEL(beval->balloonLabel), GTK_JUSTIFY_LEFT);
Bram Moolenaar98921892016-02-23 17:14:37 +01001032# if GTK_CHECK_VERSION(3,16,0)
1033 gtk_label_set_xalign(GTK_LABEL(beval->balloonLabel), 0.5);
1034 gtk_label_set_yalign(GTK_LABEL(beval->balloonLabel), 0.5);
1035# elif GTK_CHECK_VERSION(3,14,0)
1036 GValue align_val = G_VALUE_INIT;
1037 g_value_init(&align_val, G_TYPE_FLOAT);
1038 g_value_set_float(&align_val, 0.5);
1039 g_object_set_property(G_OBJECT(beval->balloonLabel), "xalign", &align_val);
1040 g_object_set_property(G_OBJECT(beval->balloonLabel), "yalign", &align_val);
1041 g_value_unset(&align_val);
1042# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001043 gtk_misc_set_alignment(GTK_MISC(beval->balloonLabel), 0.5f, 0.5f);
Bram Moolenaar98921892016-02-23 17:14:37 +01001044# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001045 gtk_widget_set_name(beval->balloonLabel, "vim-balloon-label");
1046 gtk_widget_show(beval->balloonLabel);
1047
1048 gtk_container_add(GTK_CONTAINER(beval->balloonShell), beval->balloonLabel);
1049}
1050
Bram Moolenaar30613902019-12-01 22:11:18 +01001051#else // !FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +00001052
1053/*
1054 * Draw a balloon.
1055 */
1056 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001057drawBalloon(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001058{
1059 Dimension w;
1060 Dimension h;
1061 Position tx;
1062 Position ty;
1063
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00001064 if (beval->msg == NULL)
1065 return;
1066
1067 XmString s;
1068 // Show the Balloon
1069
1070 // Calculate the label's width and height
1071
1072 // For the callback function we parse NL characters to create a
1073 // multi-line label. This doesn't work for all languages, but
1074 // XmStringCreateLocalized() doesn't do multi-line labels...
1075 if (beval->msgCB != NULL)
1076 s = XmStringCreateLtoR((char *)beval->msg, XmFONTLIST_DEFAULT_TAG);
1077 else
1078 s = XmStringCreateLocalized((char *)beval->msg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001079 {
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00001080 XmFontList fl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001081
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00001082 fl = gui_motif_fontset2fontlist(&gui.tooltip_fontset);
1083 if (fl == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001084 {
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00001085 XmStringFree(s);
1086 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001087 }
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00001088 XmStringExtent(fl, s, &w, &h);
1089 XmFontListFree(fl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001090 }
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00001091 w += gui.border_offset << 1;
1092 h += gui.border_offset << 1;
1093 XtVaSetValues(beval->balloonLabel, XmNlabelString, s, NULL);
1094 XmStringFree(s);
1095
1096 // Compute position of the balloon area
1097 tx = beval->x_root + EVAL_OFFSET_X;
1098 ty = beval->y_root + EVAL_OFFSET_Y;
1099 if ((tx + w) > beval->screen_width)
1100 tx = beval->screen_width - w;
1101 if ((ty + h) > beval->screen_height)
1102 ty = beval->screen_height - h;
1103 XtVaSetValues(beval->balloonShell,
1104 XmNx, tx,
1105 XmNy, ty,
1106 NULL);
1107 // Set tooltip colors
1108 {
1109 Arg args[2];
1110
1111 args[0].name = XmNbackground;
1112 args[0].value = gui.tooltip_bg_pixel;
1113 args[1].name = XmNforeground;
1114 args[1].value = gui.tooltip_fg_pixel;
1115 XtSetValues(beval->balloonLabel, &args[0], XtNumber(args));
1116 }
1117
1118 XtPopup(beval->balloonShell, XtGrabNone);
1119
1120 beval->showState = ShS_SHOWING;
1121
1122 current_beval = beval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001123}
1124
1125/*
1126 * Undraw a balloon.
1127 */
1128 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001129undrawBalloon(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001130{
1131 if (beval->balloonShell != (Widget)0)
1132 XtPopdown(beval->balloonShell);
1133 beval->showState = ShS_NEUTRAL;
1134
1135 current_beval = NULL;
1136}
1137
1138 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001139cancelBalloon(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001140{
1141 if (beval->showState == ShS_SHOWING
1142 || beval->showState == ShS_UPDATE_PENDING)
1143 undrawBalloon(beval);
1144
1145 if (beval->timerID != (XtIntervalId)NULL)
1146 {
1147 XtRemoveTimeOut(beval->timerID);
1148 beval->timerID = (XtIntervalId)NULL;
1149 }
1150 beval->showState = ShS_NEUTRAL;
1151}
1152
1153
1154 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001155createBalloonEvalWindow(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001156{
1157 Arg args[12];
1158 int n;
1159
1160 n = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001161 XtSetArg(args[n], XmNallowShellResize, True); n++;
1162 beval->balloonShell = XtAppCreateShell("balloonEval", "BalloonEval",
1163 overrideShellWidgetClass, gui.dpy, args, n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001164
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00001165 XmFontList fl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001166
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00001167 n = 0;
1168 fl = gui_motif_fontset2fontlist(&gui.tooltip_fontset);
1169 XtSetArg(args[n], XmNforeground, gui.tooltip_fg_pixel); n++;
1170 XtSetArg(args[n], XmNbackground, gui.tooltip_bg_pixel); n++;
1171 XtSetArg(args[n], XmNfontList, fl); n++;
1172 XtSetArg(args[n], XmNalignment, XmALIGNMENT_BEGINNING); n++;
1173 beval->balloonLabel = XtCreateManagedWidget("balloonLabel",
1174 xmLabelWidgetClass, beval->balloonShell, args, n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001175}
1176
Bram Moolenaar30613902019-12-01 22:11:18 +01001177#endif // !FEAT_GUI_GTK
1178#endif // !FEAT_GUI_MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00001179
Bram Moolenaar30613902019-12-01 22:11:18 +01001180#endif // FEAT_BEVAL_GUI