blob: 818a50b2dcb8ace1d59d0ad35f2c364dc871f4ae [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
13#if defined(FEAT_BEVAL) || defined(PROTO)
14
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000015/*
16 * Common code, invoked when the mouse is resting for a moment.
17 */
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000018 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +010019general_beval_cb(BalloonEval *beval, int state UNUSED)
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000020{
Bram Moolenaar56be9502010-06-06 14:20:26 +020021#ifdef FEAT_EVAL
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000022 win_T *wp;
23 int col;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000024 int use_sandbox;
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000025 linenr_T lnum;
26 char_u *text;
27 static char_u *result = NULL;
28 long winnr = 0;
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000029 char_u *bexpr;
30 buf_T *save_curbuf;
Bram Moolenaarf1b46222014-10-21 14:15:17 +020031 size_t len;
Bram Moolenaar56be9502010-06-06 14:20:26 +020032# ifdef FEAT_WINDOWS
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000033 win_T *cw;
Bram Moolenaar56be9502010-06-06 14:20:26 +020034# endif
Bram Moolenaar33aec762006-01-22 23:30:12 +000035#endif
Bram Moolenaar54a709e2006-05-04 21:57:11 +000036 static int recursive = FALSE;
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000037
38 /* Don't do anything when 'ballooneval' is off, messages scrolled the
39 * windows up or we have no beval area. */
40 if (!p_beval || balloonEval == NULL || msg_scrolled > 0)
41 return;
42
Bram Moolenaar54a709e2006-05-04 21:57:11 +000043 /* Don't do this recursively. Happens when the expression evaluation
44 * takes a long time and invokes something that checks for CTRL-C typed. */
45 if (recursive)
46 return;
47 recursive = TRUE;
48
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000049#ifdef FEAT_EVAL
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000050 if (get_beval_info(balloonEval, TRUE, &wp, &lnum, &text, &col) == OK)
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000051 {
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000052 bexpr = (*wp->w_buffer->b_p_bexpr == NUL) ? p_bexpr
53 : wp->w_buffer->b_p_bexpr;
54 if (*bexpr != NUL)
55 {
Bram Moolenaar33aec762006-01-22 23:30:12 +000056# ifdef FEAT_WINDOWS
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000057 /* Convert window pointer to number. */
58 for (cw = firstwin; cw != wp; cw = cw->w_next)
59 ++winnr;
Bram Moolenaar33aec762006-01-22 23:30:12 +000060# endif
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000061
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000062 set_vim_var_nr(VV_BEVAL_BUFNR, (long)wp->w_buffer->b_fnum);
63 set_vim_var_nr(VV_BEVAL_WINNR, winnr);
Bram Moolenaarc9721bd2016-06-04 17:41:03 +020064 set_vim_var_nr(VV_BEVAL_WINID, wp->w_id);
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000065 set_vim_var_nr(VV_BEVAL_LNUM, (long)lnum);
66 set_vim_var_nr(VV_BEVAL_COL, (long)(col + 1));
67 set_vim_var_string(VV_BEVAL_TEXT, text, -1);
68 vim_free(text);
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000069
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000070 /*
71 * Temporarily change the curbuf, so that we can determine whether
Bram Moolenaarbae0c162007-05-10 19:30:25 +000072 * the buffer-local balloonexpr option was set insecurely.
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000073 */
74 save_curbuf = curbuf;
75 curbuf = wp->w_buffer;
76 use_sandbox = was_set_insecurely((char_u *)"balloonexpr",
77 *curbuf->b_p_bexpr == NUL ? 0 : OPT_LOCAL);
78 curbuf = save_curbuf;
79 if (use_sandbox)
80 ++sandbox;
81 ++textlock;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000082
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000083 vim_free(result);
84 result = eval_to_string(bexpr, NULL, TRUE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000085
Bram Moolenaarf1b46222014-10-21 14:15:17 +020086 /* Remove one trailing newline, it is added when the result was a
Bram Moolenaarb5aedf32017-03-12 18:23:53 +010087 * list and it's hardly ever useful. If the user really wants a
Bram Moolenaarf1b46222014-10-21 14:15:17 +020088 * trailing newline he can add two and one remains. */
89 if (result != NULL)
90 {
91 len = STRLEN(result);
92 if (len > 0 && result[len - 1] == NL)
93 result[len - 1] = NUL;
94 }
95
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000096 if (use_sandbox)
97 --sandbox;
98 --textlock;
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000099
Bram Moolenaarb3656ed2006-03-20 21:59:49 +0000100 set_vim_var_string(VV_BEVAL_TEXT, NULL, -1);
101 if (result != NULL && result[0] != NUL)
102 {
103 gui_mch_post_balloon(beval, result);
Bram Moolenaar54a709e2006-05-04 21:57:11 +0000104 recursive = FALSE;
Bram Moolenaarb3656ed2006-03-20 21:59:49 +0000105 return;
106 }
Bram Moolenaare2ac10d2005-03-07 23:26:06 +0000107 }
108 }
109#endif
110#ifdef FEAT_NETBEANS_INTG
111 if (bevalServers & BEVAL_NETBEANS)
112 netbeans_beval_cb(beval, state);
113#endif
114#ifdef FEAT_SUN_WORKSHOP
115 if (bevalServers & BEVAL_WORKSHOP)
116 workshop_beval_cb(beval, state);
117#endif
Bram Moolenaar54a709e2006-05-04 21:57:11 +0000118
119 recursive = FALSE;
Bram Moolenaare2ac10d2005-03-07 23:26:06 +0000120}
121
122/* on Win32 only get_beval_info() is required */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000123#if !defined(FEAT_GUI_W32) || defined(PROTO)
124
125#ifdef FEAT_GUI_GTK
Bram Moolenaar98921892016-02-23 17:14:37 +0100126# if GTK_CHECK_VERSION(3,0,0)
127# include <gdk/gdkkeysyms-compat.h>
128# else
129# include <gdk/gdkkeysyms.h>
130# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000131# include <gtk/gtk.h>
132#else
133# include <X11/keysym.h>
134# ifdef FEAT_GUI_MOTIF
135# include <Xm/PushB.h>
136# include <Xm/Separator.h>
137# include <Xm/List.h>
138# include <Xm/Label.h>
139# include <Xm/AtomMgr.h>
140# include <Xm/Protocols.h>
141# else
142 /* Assume Athena */
143# include <X11/Shell.h>
Bram Moolenaar238a5642006-02-21 22:12:05 +0000144# ifdef FEAT_GUI_NEXTAW
145# include <X11/neXtaw/Label.h>
146# else
147# include <X11/Xaw/Label.h>
148# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000149# endif
150#endif
151
152#include "gui_beval.h"
153
154#ifndef FEAT_GUI_GTK
155extern Widget vimShell;
156
157/*
158 * Currently, we assume that there can be only one BalloonEval showing
159 * on-screen at any given moment. This variable will hold the currently
160 * showing BalloonEval or NULL if none is showing.
161 */
162static BalloonEval *current_beval = NULL;
163#endif
164
165#ifdef FEAT_GUI_GTK
Bram Moolenaard25c16e2016-01-29 22:13:30 +0100166static void addEventHandler(GtkWidget *, BalloonEval *);
167static void removeEventHandler(BalloonEval *);
168static gint target_event_cb(GtkWidget *, GdkEvent *, gpointer);
169static gint mainwin_event_cb(GtkWidget *, GdkEvent *, gpointer);
170static void pointer_event(BalloonEval *, int, int, unsigned);
171static void key_event(BalloonEval *, unsigned, int);
Bram Moolenaar98921892016-02-23 17:14:37 +0100172# if GTK_CHECK_VERSION(3,0,0)
173static gboolean timeout_cb(gpointer);
174# else
Bram Moolenaard25c16e2016-01-29 22:13:30 +0100175static gint timeout_cb(gpointer);
Bram Moolenaar98921892016-02-23 17:14:37 +0100176# endif
177# if GTK_CHECK_VERSION(3,0,0)
178static gboolean balloon_draw_event_cb (GtkWidget *, cairo_t *, gpointer);
179# else
180static gint balloon_expose_event_cb (GtkWidget *, GdkEventExpose *, gpointer);
181# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000182#else
Bram Moolenaard25c16e2016-01-29 22:13:30 +0100183static void addEventHandler(Widget, BalloonEval *);
184static void removeEventHandler(BalloonEval *);
185static void pointerEventEH(Widget, XtPointer, XEvent *, Boolean *);
186static void pointerEvent(BalloonEval *, XEvent *);
187static void timerRoutine(XtPointer, XtIntervalId *);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000188#endif
Bram Moolenaard25c16e2016-01-29 22:13:30 +0100189static void cancelBalloon(BalloonEval *);
190static void requestBalloon(BalloonEval *);
191static void drawBalloon(BalloonEval *);
192static void undrawBalloon(BalloonEval *beval);
193static void createBalloonEvalWindow(BalloonEval *);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000194
195
196
197/*
198 * Create a balloon-evaluation area for a Widget.
199 * There can be either a "mesg" for a fixed string or "mesgCB" to generate a
200 * message by calling this callback function.
201 * When "mesg" is not NULL it must remain valid for as long as the balloon is
202 * used. It is not freed here.
203 * Returns a pointer to the resulting object (NULL when out of memory).
204 */
205 BalloonEval *
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100206gui_mch_create_beval_area(
207 void *target,
208 char_u *mesg,
209 void (*mesgCB)(BalloonEval *, int),
210 void *clientData)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000211{
212#ifndef FEAT_GUI_GTK
213 char *display_name; /* get from gui.dpy */
214 int screen_num;
215 char *p;
216#endif
217 BalloonEval *beval;
218
219 if (mesg != NULL && mesgCB != NULL)
220 {
Bram Moolenaar95f09602016-11-10 20:01:45 +0100221 IEMSG(_("E232: Cannot create BalloonEval with both message and callback"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000222 return NULL;
223 }
224
225 beval = (BalloonEval *)alloc(sizeof(BalloonEval));
226 if (beval != NULL)
227 {
228#ifdef FEAT_GUI_GTK
229 beval->target = GTK_WIDGET(target);
230 beval->balloonShell = NULL;
231 beval->timerID = 0;
232#else
233 beval->target = (Widget)target;
234 beval->balloonShell = NULL;
235 beval->timerID = (XtIntervalId)NULL;
236 beval->appContext = XtWidgetToApplicationContext((Widget)target);
237#endif
238 beval->showState = ShS_NEUTRAL;
239 beval->x = 0;
240 beval->y = 0;
241 beval->msg = mesg;
242 beval->msgCB = mesgCB;
243 beval->clientData = clientData;
244
245 /*
246 * Set up event handler which will keep its eyes on the pointer,
247 * and when the pointer rests in a certain spot for a given time
248 * interval, show the beval.
249 */
250 addEventHandler(beval->target, beval);
251 createBalloonEvalWindow(beval);
252
253#ifndef FEAT_GUI_GTK
254 /*
255 * Now create and save the screen width and height. Used in drawing.
256 */
257 display_name = DisplayString(gui.dpy);
258 p = strrchr(display_name, '.');
259 if (p != NULL)
260 screen_num = atoi(++p);
261 else
262 screen_num = 0;
263 beval->screen_width = DisplayWidth(gui.dpy, screen_num);
264 beval->screen_height = DisplayHeight(gui.dpy, screen_num);
265#endif
266 }
267
268 return beval;
269}
270
271#if defined(FEAT_BEVAL_TIP) || defined(PROTO)
272/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +0000273 * Destroy a balloon-eval and free its associated memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000274 */
275 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100276gui_mch_destroy_beval_area(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000277{
278 cancelBalloon(beval);
279 removeEventHandler(beval);
280 /* Children will automatically be destroyed */
281# ifdef FEAT_GUI_GTK
282 gtk_widget_destroy(beval->balloonShell);
283# else
284 XtDestroyWidget(beval->balloonShell);
285# endif
286 vim_free(beval);
287}
288#endif
289
290 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100291gui_mch_enable_beval_area(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000292{
293 if (beval != NULL)
294 addEventHandler(beval->target, beval);
295}
296
297 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100298gui_mch_disable_beval_area(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000299{
300 if (beval != NULL)
301 removeEventHandler(beval);
302}
303
304#if defined(FEAT_BEVAL_TIP) || defined(PROTO)
305/*
306 * This function returns the BalloonEval * associated with the currently
307 * displayed tooltip. Returns NULL if there is no tooltip currently showing.
308 *
309 * Assumption: Only one tooltip can be shown at a time.
310 */
311 BalloonEval *
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100312gui_mch_currently_showing_beval(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000313{
314 return current_beval;
315}
316#endif
317#endif /* !FEAT_GUI_W32 */
318
Bram Moolenaare2ac10d2005-03-07 23:26:06 +0000319#if defined(FEAT_SUN_WORKSHOP) || defined(FEAT_NETBEANS_INTG) \
320 || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000321/*
322 * Get the text and position to be evaluated for "beval".
Bram Moolenaare2ac10d2005-03-07 23:26:06 +0000323 * If "getword" is true the returned text is not the whole line but the
324 * relevant word in allocated memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000325 * Returns OK or FAIL.
326 */
327 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100328get_beval_info(
329 BalloonEval *beval,
330 int getword,
331 win_T **winp,
332 linenr_T *lnump,
333 char_u **textp,
334 int *colp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000335{
336 win_T *wp;
337 int row, col;
338 char_u *lbuf;
339 linenr_T lnum;
340
Bram Moolenaare2ac10d2005-03-07 23:26:06 +0000341 *textp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000342 row = Y_2_ROW(beval->y);
343 col = X_2_COL(beval->x);
Bram Moolenaar33aec762006-01-22 23:30:12 +0000344#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +0000345 wp = mouse_find_win(&row, &col);
Bram Moolenaar33aec762006-01-22 23:30:12 +0000346#else
347 wp = firstwin;
348#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000349 if (wp != NULL && row < wp->w_height && col < W_WIDTH(wp))
350 {
351 /* Found a window and the cursor is in the text. Now find the line
352 * number. */
353 if (!mouse_comp_pos(wp, &row, &col, &lnum))
354 {
355 /* Not past end of the file. */
356 lbuf = ml_get_buf(wp->w_buffer, lnum, FALSE);
357 if (col <= win_linetabsize(wp, lbuf, (colnr_T)MAXCOL))
358 {
359 /* Not past end of line. */
Bram Moolenaare2ac10d2005-03-07 23:26:06 +0000360 if (getword)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000361 {
362 /* For Netbeans we get the relevant part of the line
363 * instead of the whole line. */
364 int len;
365 pos_T *spos = NULL, *epos = NULL;
366
367 if (VIsual_active)
368 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +0100369 if (LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000370 {
371 spos = &VIsual;
372 epos = &curwin->w_cursor;
373 }
374 else
375 {
376 spos = &curwin->w_cursor;
377 epos = &VIsual;
378 }
379 }
380
Bram Moolenaarb6101cf2012-10-21 00:58:39 +0200381 col = vcol2col(wp, lnum, col);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000382
383 if (VIsual_active
384 && wp->w_buffer == curwin->w_buffer
385 && (lnum == spos->lnum
386 ? col >= (int)spos->col
387 : lnum > spos->lnum)
388 && (lnum == epos->lnum
389 ? col <= (int)epos->col
390 : lnum < epos->lnum))
391 {
392 /* Visual mode and pointing to the line with the
393 * Visual selection: return selected text, with a
394 * maximum of one line. */
395 if (spos->lnum != epos->lnum || spos->col == epos->col)
396 return FAIL;
397
398 lbuf = ml_get_buf(curwin->w_buffer, VIsual.lnum, FALSE);
Bram Moolenaarb6101cf2012-10-21 00:58:39 +0200399 len = epos->col - spos->col;
400 if (*p_sel != 'e')
401 len += MB_PTR2LEN(lbuf + epos->col);
402 lbuf = vim_strnsave(lbuf + spos->col, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000403 lnum = spos->lnum;
404 col = spos->col;
405 }
406 else
407 {
408 /* Find the word under the cursor. */
409 ++emsg_off;
410 len = find_ident_at_pos(wp, lnum, (colnr_T)col, &lbuf,
411 FIND_IDENT + FIND_STRING + FIND_EVAL);
412 --emsg_off;
413 if (len == 0)
414 return FAIL;
415 lbuf = vim_strnsave(lbuf, len);
416 }
417 }
Bram Moolenaare2ac10d2005-03-07 23:26:06 +0000418
419 *winp = wp;
420 *lnump = lnum;
421 *textp = lbuf;
422 *colp = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000423 beval->ts = wp->w_buffer->b_p_ts;
424 return OK;
425 }
426 }
427 }
428
429 return FAIL;
430}
431
432# if !defined(FEAT_GUI_W32) || defined(PROTO)
433
434/*
435 * Show a balloon with "mesg".
436 */
437 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100438gui_mch_post_balloon(BalloonEval *beval, char_u *mesg)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000439{
440 beval->msg = mesg;
441 if (mesg != NULL)
442 drawBalloon(beval);
443 else
444 undrawBalloon(beval);
445}
446# endif /* FEAT_GUI_W32 */
447#endif /* FEAT_SUN_WORKSHOP || FEAT_NETBEANS_INTG || PROTO */
448
449#if !defined(FEAT_GUI_W32) || defined(PROTO)
450#if defined(FEAT_BEVAL_TIP) || defined(PROTO)
451/*
452 * Hide the given balloon.
453 */
454 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100455gui_mch_unpost_balloon(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000456{
457 undrawBalloon(beval);
458}
459#endif
460
461#ifdef FEAT_GUI_GTK
462/*
463 * We can unconditionally use ANSI-style prototypes here since
464 * GTK+ requires an ANSI C compiler anyway.
465 */
466 static void
467addEventHandler(GtkWidget *target, BalloonEval *beval)
468{
469 /*
470 * Connect to the generic "event" signal instead of the individual
471 * signals for each event type, because the former is emitted earlier.
472 * This allows us to catch events independently of the signal handlers
473 * in gui_gtk_x11.c.
474 */
Bram Moolenaar98921892016-02-23 17:14:37 +0100475# if GTK_CHECK_VERSION(3,0,0)
476 g_signal_connect(G_OBJECT(target), "event",
477 G_CALLBACK(target_event_cb),
478 beval);
479# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000480 /* Should use GTK_OBJECT() here, but that causes a lint warning... */
481 gtk_signal_connect((GtkObject*)(target), "event",
482 GTK_SIGNAL_FUNC(target_event_cb),
483 beval);
Bram Moolenaar98921892016-02-23 17:14:37 +0100484# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000485 /*
486 * Nasty: Key press events go to the main window thus the drawing area
487 * will never see them. This means we have to connect to the main window
488 * as well in order to catch those events.
489 */
490 if (gtk_socket_id == 0 && gui.mainwin != NULL
491 && gtk_widget_is_ancestor(target, gui.mainwin))
492 {
Bram Moolenaar98921892016-02-23 17:14:37 +0100493# if GTK_CHECK_VERSION(3,0,0)
494 g_signal_connect(G_OBJECT(gui.mainwin), "event",
495 G_CALLBACK(mainwin_event_cb),
496 beval);
497# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000498 gtk_signal_connect((GtkObject*)(gui.mainwin), "event",
499 GTK_SIGNAL_FUNC(mainwin_event_cb),
500 beval);
Bram Moolenaar98921892016-02-23 17:14:37 +0100501# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000502 }
503}
504
505 static void
506removeEventHandler(BalloonEval *beval)
507{
Bram Moolenaar33570922005-01-25 22:26:29 +0000508 /* LINTED: avoid warning: dubious operation on enum */
Bram Moolenaar98921892016-02-23 17:14:37 +0100509# if GTK_CHECK_VERSION(3,0,0)
510 g_signal_handlers_disconnect_by_func(G_OBJECT(beval->target),
Bram Moolenaard47d8372016-09-09 22:13:24 +0200511 FUNC2GENERIC(target_event_cb),
Bram Moolenaar98921892016-02-23 17:14:37 +0100512 beval);
513# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000514 gtk_signal_disconnect_by_func((GtkObject*)(beval->target),
515 GTK_SIGNAL_FUNC(target_event_cb),
516 beval);
Bram Moolenaar98921892016-02-23 17:14:37 +0100517# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000518
519 if (gtk_socket_id == 0 && gui.mainwin != NULL
520 && gtk_widget_is_ancestor(beval->target, gui.mainwin))
521 {
Bram Moolenaar33570922005-01-25 22:26:29 +0000522 /* LINTED: avoid warning: dubious operation on enum */
Bram Moolenaar98921892016-02-23 17:14:37 +0100523# if GTK_CHECK_VERSION(3,0,0)
524 g_signal_handlers_disconnect_by_func(G_OBJECT(gui.mainwin),
Bram Moolenaard47d8372016-09-09 22:13:24 +0200525 FUNC2GENERIC(mainwin_event_cb),
Bram Moolenaar98921892016-02-23 17:14:37 +0100526 beval);
527# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000528 gtk_signal_disconnect_by_func((GtkObject*)(gui.mainwin),
529 GTK_SIGNAL_FUNC(mainwin_event_cb),
530 beval);
Bram Moolenaar98921892016-02-23 17:14:37 +0100531# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000532 }
533}
534
535 static gint
536target_event_cb(GtkWidget *widget, GdkEvent *event, gpointer data)
537{
538 BalloonEval *beval = (BalloonEval *)data;
539
540 switch (event->type)
541 {
542 case GDK_ENTER_NOTIFY:
543 pointer_event(beval, (int)event->crossing.x,
544 (int)event->crossing.y,
545 event->crossing.state);
546 break;
547 case GDK_MOTION_NOTIFY:
548 if (event->motion.is_hint)
549 {
550 int x;
551 int y;
552 GdkModifierType state;
553 /*
554 * GDK_POINTER_MOTION_HINT_MASK is set, thus we cannot obtain
555 * the coordinates from the GdkEventMotion struct directly.
556 */
Bram Moolenaar98921892016-02-23 17:14:37 +0100557# if GTK_CHECK_VERSION(3,0,0)
558 {
559 GdkWindow * const win = gtk_widget_get_window(widget);
560 GdkDisplay * const dpy = gdk_window_get_display(win);
Bram Moolenaar30e12d22016-04-17 20:49:53 +0200561# if GTK_CHECK_VERSION(3,20,0)
562 GdkSeat * const seat = gdk_display_get_default_seat(dpy);
563 GdkDevice * const dev = gdk_seat_get_pointer(seat);
564# else
Bram Moolenaar98921892016-02-23 17:14:37 +0100565 GdkDeviceManager * const mngr = gdk_display_get_device_manager(dpy);
566 GdkDevice * const dev = gdk_device_manager_get_client_pointer(mngr);
Bram Moolenaar30e12d22016-04-17 20:49:53 +0200567# endif
Bram Moolenaar98921892016-02-23 17:14:37 +0100568 gdk_window_get_device_position(win, dev , &x, &y, &state);
569 }
570# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000571 gdk_window_get_pointer(widget->window, &x, &y, &state);
Bram Moolenaar98921892016-02-23 17:14:37 +0100572# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000573 pointer_event(beval, x, y, (unsigned int)state);
574 }
575 else
576 {
577 pointer_event(beval, (int)event->motion.x,
578 (int)event->motion.y,
579 event->motion.state);
580 }
581 break;
582 case GDK_LEAVE_NOTIFY:
583 /*
584 * Ignore LeaveNotify events that are not "normal".
585 * Apparently we also get it when somebody else grabs focus.
586 */
587 if (event->crossing.mode == GDK_CROSSING_NORMAL)
588 cancelBalloon(beval);
589 break;
590 case GDK_BUTTON_PRESS:
Bram Moolenaar071d4272004-06-13 20:20:40 +0000591 case GDK_SCROLL:
Bram Moolenaar071d4272004-06-13 20:20:40 +0000592 cancelBalloon(beval);
593 break;
594 case GDK_KEY_PRESS:
595 key_event(beval, event->key.keyval, TRUE);
596 break;
597 case GDK_KEY_RELEASE:
598 key_event(beval, event->key.keyval, FALSE);
599 break;
600 default:
601 break;
602 }
603
604 return FALSE; /* continue emission */
605}
606
Bram Moolenaar071d4272004-06-13 20:20:40 +0000607 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +0000608mainwin_event_cb(GtkWidget *widget UNUSED, GdkEvent *event, gpointer data)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000609{
610 BalloonEval *beval = (BalloonEval *)data;
611
612 switch (event->type)
613 {
614 case GDK_KEY_PRESS:
615 key_event(beval, event->key.keyval, TRUE);
616 break;
617 case GDK_KEY_RELEASE:
618 key_event(beval, event->key.keyval, FALSE);
619 break;
620 default:
621 break;
622 }
623
624 return FALSE; /* continue emission */
625}
626
627 static void
628pointer_event(BalloonEval *beval, int x, int y, unsigned state)
629{
630 int distance;
631
632 distance = ABS(x - beval->x) + ABS(y - beval->y);
633
634 if (distance > 4)
635 {
636 /*
637 * Moved out of the balloon location: cancel it.
638 * Remember button state
639 */
640 beval->state = state;
641 cancelBalloon(beval);
642
643 /* Mouse buttons are pressed - no balloon now */
644 if (!(state & ((int)GDK_BUTTON1_MASK | (int)GDK_BUTTON2_MASK
645 | (int)GDK_BUTTON3_MASK)))
646 {
647 beval->x = x;
648 beval->y = y;
649
650 if (state & (int)GDK_MOD1_MASK)
651 {
652 /*
653 * Alt is pressed -- enter super-evaluate-mode,
654 * where there is no time delay
655 */
656 if (beval->msgCB != NULL)
657 {
658 beval->showState = ShS_PENDING;
659 (*beval->msgCB)(beval, state);
660 }
661 }
662 else
663 {
Bram Moolenaar98921892016-02-23 17:14:37 +0100664# if GTK_CHECK_VERSION(3,0,0)
665 beval->timerID = g_timeout_add((guint)p_bdlay,
666 &timeout_cb, beval);
667# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000668 beval->timerID = gtk_timeout_add((guint32)p_bdlay,
669 &timeout_cb, beval);
Bram Moolenaar98921892016-02-23 17:14:37 +0100670# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000671 }
672 }
673 }
674}
675
676 static void
677key_event(BalloonEval *beval, unsigned keyval, int is_keypress)
678{
679 if (beval->showState == ShS_SHOWING && beval->msgCB != NULL)
680 {
681 switch (keyval)
682 {
683 case GDK_Shift_L:
684 case GDK_Shift_R:
685 beval->showState = ShS_UPDATE_PENDING;
686 (*beval->msgCB)(beval, (is_keypress)
687 ? (int)GDK_SHIFT_MASK : 0);
688 break;
689 case GDK_Control_L:
690 case GDK_Control_R:
691 beval->showState = ShS_UPDATE_PENDING;
692 (*beval->msgCB)(beval, (is_keypress)
693 ? (int)GDK_CONTROL_MASK : 0);
694 break;
695 default:
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000696 /* Don't do this for key release, we apparently get these with
697 * focus changes in some GTK version. */
698 if (is_keypress)
699 cancelBalloon(beval);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000700 break;
701 }
702 }
703 else
704 cancelBalloon(beval);
705}
706
Bram Moolenaar98921892016-02-23 17:14:37 +0100707# if GTK_CHECK_VERSION(3,0,0)
708 static gboolean
709# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000710 static gint
Bram Moolenaar98921892016-02-23 17:14:37 +0100711# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000712timeout_cb(gpointer data)
713{
714 BalloonEval *beval = (BalloonEval *)data;
715
716 beval->timerID = 0;
717 /*
718 * If the timer event happens then the mouse has stopped long enough for
719 * a request to be started. The request will only send to the debugger if
720 * there the mouse is pointing at real data.
721 */
722 requestBalloon(beval);
723
724 return FALSE; /* don't call me again */
725}
726
Bram Moolenaar98921892016-02-23 17:14:37 +0100727# if GTK_CHECK_VERSION(3,0,0)
728 static gboolean
729balloon_draw_event_cb(GtkWidget *widget,
730 cairo_t *cr,
731 gpointer data UNUSED)
732{
733 GtkStyleContext *context = NULL;
734 gint width = -1, height = -1;
735
736 if (widget == NULL)
737 return TRUE;
738
739 context = gtk_widget_get_style_context(widget);
740 width = gtk_widget_get_allocated_width(widget);
741 height = gtk_widget_get_allocated_height(widget);
742
743 gtk_style_context_save(context);
744
745 gtk_style_context_add_class(context, "tooltip");
746 gtk_style_context_set_state(context, GTK_STATE_FLAG_NORMAL);
747
748 cairo_save(cr);
749 gtk_render_frame(context, cr, 0, 0, width, height);
750 gtk_render_background(context, cr, 0, 0, width, height);
751 cairo_restore(cr);
752
753 gtk_style_context_restore(context);
754
755 return FALSE;
756}
757# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000758 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +0000759balloon_expose_event_cb(GtkWidget *widget,
760 GdkEventExpose *event,
761 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000762{
763 gtk_paint_flat_box(widget->style, widget->window,
764 GTK_STATE_NORMAL, GTK_SHADOW_OUT,
765 &event->area, widget, "tooltip",
766 0, 0, -1, -1);
767
768 return FALSE; /* continue emission */
769}
Bram Moolenaar98921892016-02-23 17:14:37 +0100770# endif /* !GTK_CHECK_VERSION(3,0,0) */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000771
Bram Moolenaar071d4272004-06-13 20:20:40 +0000772#else /* !FEAT_GUI_GTK */
773
774 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100775addEventHandler(Widget target, BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000776{
777 XtAddEventHandler(target,
778 PointerMotionMask | EnterWindowMask |
779 LeaveWindowMask | ButtonPressMask | KeyPressMask |
780 KeyReleaseMask,
781 False,
782 pointerEventEH, (XtPointer)beval);
783}
784
785 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100786removeEventHandler(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000787{
788 XtRemoveEventHandler(beval->target,
789 PointerMotionMask | EnterWindowMask |
790 LeaveWindowMask | ButtonPressMask | KeyPressMask |
791 KeyReleaseMask,
792 False,
793 pointerEventEH, (XtPointer)beval);
794}
795
796
797/*
798 * The X event handler. All it does is call the real event handler.
799 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000800 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100801pointerEventEH(
802 Widget w UNUSED,
803 XtPointer client_data,
804 XEvent *event,
805 Boolean *unused UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000806{
807 BalloonEval *beval = (BalloonEval *)client_data;
808 pointerEvent(beval, event);
809}
810
811
812/*
813 * The real event handler. Called by pointerEventEH() whenever an event we are
Bram Moolenaarbae0c162007-05-10 19:30:25 +0000814 * interested in occurs.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000815 */
816
817 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100818pointerEvent(BalloonEval *beval, XEvent *event)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000819{
Bram Moolenaar84a05ac2013-05-06 04:24:17 +0200820 Position distance; /* a measure of how much the pointer moved */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000821 Position delta; /* used to compute distance */
822
823 switch (event->type)
824 {
825 case EnterNotify:
826 case MotionNotify:
827 delta = event->xmotion.x - beval->x;
828 if (delta < 0)
829 delta = -delta;
830 distance = delta;
831 delta = event->xmotion.y - beval->y;
832 if (delta < 0)
833 delta = -delta;
834 distance += delta;
835 if (distance > 4)
836 {
837 /*
838 * Moved out of the balloon location: cancel it.
839 * Remember button state
840 */
841 beval->state = event->xmotion.state;
842 if (beval->state & (Button1Mask|Button2Mask|Button3Mask))
843 {
844 /* Mouse buttons are pressed - no balloon now */
845 cancelBalloon(beval);
846 }
847 else if (beval->state & (Mod1Mask|Mod2Mask|Mod3Mask))
848 {
849 /*
850 * Alt is pressed -- enter super-evaluate-mode,
851 * where there is no time delay
852 */
853 beval->x = event->xmotion.x;
854 beval->y = event->xmotion.y;
855 beval->x_root = event->xmotion.x_root;
856 beval->y_root = event->xmotion.y_root;
857 cancelBalloon(beval);
858 if (beval->msgCB != NULL)
859 {
860 beval->showState = ShS_PENDING;
861 (*beval->msgCB)(beval, beval->state);
862 }
863 }
864 else
865 {
866 beval->x = event->xmotion.x;
867 beval->y = event->xmotion.y;
868 beval->x_root = event->xmotion.x_root;
869 beval->y_root = event->xmotion.y_root;
870 cancelBalloon(beval);
871 beval->timerID = XtAppAddTimeOut( beval->appContext,
872 (long_u)p_bdlay, timerRoutine, beval);
873 }
874 }
875 break;
876
877 /*
878 * Motif and Athena version: Keystrokes will be caught by the
879 * "textArea" widget, and handled in gui_x11_key_hit_cb().
880 */
881 case KeyPress:
882 if (beval->showState == ShS_SHOWING && beval->msgCB != NULL)
883 {
884 Modifiers modifier;
885 KeySym keysym;
886
887 XtTranslateKeycode(gui.dpy,
888 event->xkey.keycode, event->xkey.state,
889 &modifier, &keysym);
890 if (keysym == XK_Shift_L || keysym == XK_Shift_R)
891 {
892 beval->showState = ShS_UPDATE_PENDING;
893 (*beval->msgCB)(beval, ShiftMask);
894 }
895 else if (keysym == XK_Control_L || keysym == XK_Control_R)
896 {
897 beval->showState = ShS_UPDATE_PENDING;
898 (*beval->msgCB)(beval, ControlMask);
899 }
900 else
901 cancelBalloon(beval);
902 }
903 else
904 cancelBalloon(beval);
905 break;
906
907 case KeyRelease:
908 if (beval->showState == ShS_SHOWING && beval->msgCB != NULL)
909 {
910 Modifiers modifier;
911 KeySym keysym;
912
913 XtTranslateKeycode(gui.dpy, event->xkey.keycode,
914 event->xkey.state, &modifier, &keysym);
915 if ((keysym == XK_Shift_L) || (keysym == XK_Shift_R)) {
916 beval->showState = ShS_UPDATE_PENDING;
917 (*beval->msgCB)(beval, 0);
918 }
919 else if ((keysym == XK_Control_L) || (keysym == XK_Control_R))
920 {
921 beval->showState = ShS_UPDATE_PENDING;
922 (*beval->msgCB)(beval, 0);
923 }
924 else
925 cancelBalloon(beval);
926 }
927 else
928 cancelBalloon(beval);
929 break;
930
931 case LeaveNotify:
932 /* Ignore LeaveNotify events that are not "normal".
933 * Apparently we also get it when somebody else grabs focus.
934 * Happens for me every two seconds (some clipboard tool?) */
935 if (event->xcrossing.mode == NotifyNormal)
936 cancelBalloon(beval);
937 break;
938
939 case ButtonPress:
940 cancelBalloon(beval);
941 break;
942
943 default:
944 break;
945 }
946}
947
Bram Moolenaar071d4272004-06-13 20:20:40 +0000948 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100949timerRoutine(XtPointer dx, XtIntervalId *id UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000950{
951 BalloonEval *beval = (BalloonEval *)dx;
952
953 beval->timerID = (XtIntervalId)NULL;
954
955 /*
956 * If the timer event happens then the mouse has stopped long enough for
957 * a request to be started. The request will only send to the debugger if
958 * there the mouse is pointing at real data.
959 */
960 requestBalloon(beval);
961}
962
963#endif /* !FEAT_GUI_GTK */
964
965 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100966requestBalloon(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000967{
968 if (beval->showState != ShS_PENDING)
969 {
970 /* Determine the beval to display */
971 if (beval->msgCB != NULL)
972 {
973 beval->showState = ShS_PENDING;
974 (*beval->msgCB)(beval, beval->state);
975 }
976 else if (beval->msg != NULL)
977 drawBalloon(beval);
978 }
979}
980
981#ifdef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +0000982/*
983 * Convert the string to UTF-8 if 'encoding' is not "utf-8".
984 * Replace any non-printable characters and invalid bytes sequences with
985 * "^X" or "<xx>" escapes, and apply SpecialKey highlighting to them.
986 * TAB and NL are passed through unscathed.
987 */
Bram Moolenaar182c5be2010-06-25 05:37:59 +0200988# define IS_NONPRINTABLE(c) (((c) < 0x20 && (c) != TAB && (c) != NL) \
Bram Moolenaar071d4272004-06-13 20:20:40 +0000989 || (c) == DEL)
990 static void
Bram Moolenaar89d40322006-08-29 15:30:07 +0000991set_printable_label_text(GtkLabel *label, char_u *text)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000992{
993 char_u *convbuf = NULL;
994 char_u *buf;
995 char_u *p;
996 char_u *pdest;
997 unsigned int len;
998 int charlen;
999 int uc;
1000 PangoAttrList *attr_list;
1001
1002 /* Convert to UTF-8 if it isn't already */
1003 if (output_conv.vc_type != CONV_NONE)
1004 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00001005 convbuf = string_convert(&output_conv, text, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001006 if (convbuf != NULL)
Bram Moolenaar89d40322006-08-29 15:30:07 +00001007 text = convbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001008 }
1009
1010 /* First let's see how much we need to allocate */
1011 len = 0;
Bram Moolenaar89d40322006-08-29 15:30:07 +00001012 for (p = text; *p != NUL; p += charlen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001013 {
1014 if ((*p & 0x80) == 0) /* be quick for ASCII */
1015 {
1016 charlen = 1;
1017 len += IS_NONPRINTABLE(*p) ? 2 : 1; /* nonprintable: ^X */
1018 }
1019 else
1020 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001021 charlen = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001022 uc = utf_ptr2char(p);
1023
1024 if (charlen != utf_char2len(uc))
1025 charlen = 1; /* reject overlong sequences */
1026
1027 if (charlen == 1 || uc < 0xa0) /* illegal byte or */
1028 len += 4; /* control char: <xx> */
1029 else if (!utf_printable(uc))
1030 /* Note: we assume here that utf_printable() doesn't
1031 * care about characters outside the BMP. */
1032 len += 6; /* nonprintable: <xxxx> */
1033 else
1034 len += charlen;
1035 }
1036 }
1037
1038 attr_list = pango_attr_list_new();
1039 buf = alloc(len + 1);
1040
1041 /* Now go for the real work */
1042 if (buf != NULL)
1043 {
1044 attrentry_T *aep;
1045 PangoAttribute *attr;
1046 guicolor_T pixel;
Bram Moolenaar36edf062016-07-21 22:10:12 +02001047#if GTK_CHECK_VERSION(3,0,0)
1048 GdkRGBA color = { 0.0, 0.0, 0.0, 1.0 };
Bram Moolenaar870b7492016-07-22 22:26:52 +02001049# if PANGO_VERSION_CHECK(1,38,0)
Bram Moolenaar36edf062016-07-21 22:10:12 +02001050 PangoAttribute *attr_alpha;
Bram Moolenaar870b7492016-07-22 22:26:52 +02001051# endif
Bram Moolenaar36edf062016-07-21 22:10:12 +02001052#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001053 GdkColor color = { 0, 0, 0, 0 };
Bram Moolenaar36edf062016-07-21 22:10:12 +02001054#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001055
1056 /* Look up the RGB values of the SpecialKey foreground color. */
Bram Moolenaar8820b482017-03-16 17:23:31 +01001057 aep = syn_gui_attr2entry(HL_ATTR(HLF_8));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001058 pixel = (aep != NULL) ? aep->ae_u.gui.fg_color : INVALCOLOR;
1059 if (pixel != INVALCOLOR)
Bram Moolenaar98921892016-02-23 17:14:37 +01001060# if GTK_CHECK_VERSION(3,0,0)
1061 {
Bram Moolenaar36edf062016-07-21 22:10:12 +02001062 color.red = ((pixel & 0xff0000) >> 16) / 255.0;
1063 color.green = ((pixel & 0xff00) >> 8) / 255.0;
1064 color.blue = (pixel & 0xff) / 255.0;
1065 color.alpha = 1.0;
Bram Moolenaar98921892016-02-23 17:14:37 +01001066 }
1067# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001068 gdk_colormap_query_color(gtk_widget_get_colormap(gui.drawarea),
1069 (unsigned long)pixel, &color);
Bram Moolenaar98921892016-02-23 17:14:37 +01001070# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001071
1072 pdest = buf;
Bram Moolenaar89d40322006-08-29 15:30:07 +00001073 p = text;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001074 while (*p != NUL)
1075 {
1076 /* Be quick for ASCII */
1077 if ((*p & 0x80) == 0 && !IS_NONPRINTABLE(*p))
1078 {
1079 *pdest++ = *p++;
1080 }
1081 else
1082 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001083 charlen = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001084 uc = utf_ptr2char(p);
1085
1086 if (charlen != utf_char2len(uc))
1087 charlen = 1; /* reject overlong sequences */
1088
1089 if (charlen == 1 || uc < 0xa0 || !utf_printable(uc))
1090 {
1091 int outlen;
1092
1093 /* Careful: we can't just use transchar_byte() here,
1094 * since 'encoding' is not necessarily set to "utf-8". */
1095 if (*p & 0x80 && charlen == 1)
1096 {
1097 transchar_hex(pdest, *p); /* <xx> */
1098 outlen = 4;
1099 }
1100 else if (uc >= 0x80)
1101 {
1102 /* Note: we assume here that utf_printable() doesn't
1103 * care about characters outside the BMP. */
1104 transchar_hex(pdest, uc); /* <xx> or <xxxx> */
1105 outlen = (uc < 0x100) ? 4 : 6;
1106 }
1107 else
1108 {
1109 transchar_nonprint(pdest, *p); /* ^X */
1110 outlen = 2;
1111 }
1112 if (pixel != INVALCOLOR)
1113 {
Bram Moolenaar36edf062016-07-21 22:10:12 +02001114#if GTK_CHECK_VERSION(3,0,0)
1115# define DOUBLE2UINT16(val) ((guint16)((val) * 65535 + 0.5))
1116 attr = pango_attr_foreground_new(
1117 DOUBLE2UINT16(color.red),
1118 DOUBLE2UINT16(color.green),
1119 DOUBLE2UINT16(color.blue));
Bram Moolenaar870b7492016-07-22 22:26:52 +02001120# if PANGO_VERSION_CHECK(1,38,0)
Bram Moolenaar36edf062016-07-21 22:10:12 +02001121 attr_alpha = pango_attr_foreground_alpha_new(
1122 DOUBLE2UINT16(color.alpha));
Bram Moolenaar870b7492016-07-22 22:26:52 +02001123# endif
Bram Moolenaar36edf062016-07-21 22:10:12 +02001124# undef DOUBLE2UINT16
1125#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001126 attr = pango_attr_foreground_new(
1127 color.red, color.green, color.blue);
Bram Moolenaar36edf062016-07-21 22:10:12 +02001128#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001129 attr->start_index = pdest - buf;
1130 attr->end_index = pdest - buf + outlen;
1131 pango_attr_list_insert(attr_list, attr);
Bram Moolenaar36edf062016-07-21 22:10:12 +02001132#if GTK_CHECK_VERSION(3,0,0)
Bram Moolenaar870b7492016-07-22 22:26:52 +02001133# if PANGO_VERSION_CHECK(1,38,0)
Bram Moolenaar36edf062016-07-21 22:10:12 +02001134 attr_alpha->start_index = pdest - buf;
1135 attr_alpha->end_index = pdest - buf + outlen;
1136 pango_attr_list_insert(attr_list, attr_alpha);
Bram Moolenaar870b7492016-07-22 22:26:52 +02001137# endif
Bram Moolenaar36edf062016-07-21 22:10:12 +02001138#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001139 }
1140 pdest += outlen;
1141 p += charlen;
1142 }
1143 else
1144 {
1145 do
1146 *pdest++ = *p++;
1147 while (--charlen != 0);
1148 }
1149 }
1150 }
1151 *pdest = NUL;
1152 }
1153
1154 vim_free(convbuf);
1155
1156 gtk_label_set_text(label, (const char *)buf);
1157 vim_free(buf);
1158
1159 gtk_label_set_attributes(label, attr_list);
1160 pango_attr_list_unref(attr_list);
1161}
Bram Moolenaar182c5be2010-06-25 05:37:59 +02001162# undef IS_NONPRINTABLE
Bram Moolenaar071d4272004-06-13 20:20:40 +00001163
1164/*
1165 * Draw a balloon.
1166 */
1167 static void
1168drawBalloon(BalloonEval *beval)
1169{
1170 if (beval->msg != NULL)
1171 {
1172 GtkRequisition requisition;
1173 int screen_w;
1174 int screen_h;
1175 int x;
1176 int y;
1177 int x_offset = EVAL_OFFSET_X;
1178 int y_offset = EVAL_OFFSET_Y;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001179 PangoLayout *layout;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001180# ifdef HAVE_GTK_MULTIHEAD
Bram Moolenaara859f042016-11-17 19:11:55 +01001181# if GTK_CHECK_VERSION(3,22,2)
1182 GdkRectangle rect;
1183 GdkMonitor * const mon = gdk_display_get_monitor_at_window(
1184 gtk_widget_get_display(beval->balloonShell),
1185 gtk_widget_get_window(beval->balloonShell));
1186 gdk_monitor_get_geometry(mon, &rect);
1187
1188 screen_w = rect.width;
1189 screen_h = rect.height;
1190# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001191 GdkScreen *screen;
1192
1193 screen = gtk_widget_get_screen(beval->target);
1194 gtk_window_set_screen(GTK_WINDOW(beval->balloonShell), screen);
1195 screen_w = gdk_screen_get_width(screen);
1196 screen_h = gdk_screen_get_height(screen);
Bram Moolenaara859f042016-11-17 19:11:55 +01001197# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001198# else
1199 screen_w = gdk_screen_width();
1200 screen_h = gdk_screen_height();
1201# endif
Bram Moolenaar98921892016-02-23 17:14:37 +01001202# if !GTK_CHECK_VERSION(3,0,0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001203 gtk_widget_ensure_style(beval->balloonShell);
1204 gtk_widget_ensure_style(beval->balloonLabel);
Bram Moolenaar98921892016-02-23 17:14:37 +01001205# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001206
Bram Moolenaar071d4272004-06-13 20:20:40 +00001207 set_printable_label_text(GTK_LABEL(beval->balloonLabel), beval->msg);
1208 /*
1209 * Dirty trick: Enable wrapping mode on the label's layout behind its
1210 * back. This way GtkLabel won't try to constrain the wrap width to a
1211 * builtin maximum value of about 65 Latin characters.
1212 */
1213 layout = gtk_label_get_layout(GTK_LABEL(beval->balloonLabel));
Bram Moolenaar182c5be2010-06-25 05:37:59 +02001214# ifdef PANGO_WRAP_WORD_CHAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00001215 pango_layout_set_wrap(layout, PANGO_WRAP_WORD_CHAR);
Bram Moolenaar182c5be2010-06-25 05:37:59 +02001216# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001217 pango_layout_set_wrap(layout, PANGO_WRAP_WORD);
Bram Moolenaar182c5be2010-06-25 05:37:59 +02001218# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001219 pango_layout_set_width(layout,
1220 /* try to come up with some reasonable width */
1221 PANGO_SCALE * CLAMP(gui.num_cols * gui.char_width,
1222 screen_w / 2,
1223 MAX(20, screen_w - 20)));
1224
1225 /* Calculate the balloon's width and height. */
Bram Moolenaar98921892016-02-23 17:14:37 +01001226# if GTK_CHECK_VERSION(3,0,0)
1227 gtk_widget_get_preferred_size(beval->balloonShell, &requisition, NULL);
1228# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001229 gtk_widget_size_request(beval->balloonShell, &requisition);
Bram Moolenaar98921892016-02-23 17:14:37 +01001230# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001231
1232 /* Compute position of the balloon area */
Bram Moolenaar98921892016-02-23 17:14:37 +01001233# if GTK_CHECK_VERSION(3,0,0)
1234 gdk_window_get_origin(gtk_widget_get_window(beval->target), &x, &y);
1235# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001236 gdk_window_get_origin(beval->target->window, &x, &y);
Bram Moolenaar98921892016-02-23 17:14:37 +01001237# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001238 x += beval->x;
1239 y += beval->y;
1240
1241 /* Get out of the way of the mouse pointer */
1242 if (x + x_offset + requisition.width > screen_w)
1243 y_offset += 15;
1244 if (y + y_offset + requisition.height > screen_h)
1245 y_offset = -requisition.height - EVAL_OFFSET_Y;
1246
1247 /* Sanitize values */
1248 x = CLAMP(x + x_offset, 0, MAX(0, screen_w - requisition.width));
1249 y = CLAMP(y + y_offset, 0, MAX(0, screen_h - requisition.height));
1250
1251 /* Show the balloon */
Bram Moolenaar98921892016-02-23 17:14:37 +01001252# if GTK_CHECK_VERSION(3,0,0)
1253 gtk_window_move(GTK_WINDOW(beval->balloonShell), x, y);
1254# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001255 gtk_widget_set_uposition(beval->balloonShell, x, y);
Bram Moolenaar98921892016-02-23 17:14:37 +01001256# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001257 gtk_widget_show(beval->balloonShell);
1258
1259 beval->showState = ShS_SHOWING;
1260 }
1261}
1262
1263/*
1264 * Undraw a balloon.
1265 */
1266 static void
1267undrawBalloon(BalloonEval *beval)
1268{
1269 if (beval->balloonShell != NULL)
1270 gtk_widget_hide(beval->balloonShell);
1271 beval->showState = ShS_NEUTRAL;
1272}
1273
1274 static void
1275cancelBalloon(BalloonEval *beval)
1276{
1277 if (beval->showState == ShS_SHOWING
1278 || beval->showState == ShS_UPDATE_PENDING)
1279 undrawBalloon(beval);
1280
1281 if (beval->timerID != 0)
1282 {
Bram Moolenaar98921892016-02-23 17:14:37 +01001283# if GTK_CHECK_VERSION(3,0,0)
1284 g_source_remove(beval->timerID);
1285# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001286 gtk_timeout_remove(beval->timerID);
Bram Moolenaar98921892016-02-23 17:14:37 +01001287# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001288 beval->timerID = 0;
1289 }
1290 beval->showState = ShS_NEUTRAL;
1291}
1292
1293 static void
1294createBalloonEvalWindow(BalloonEval *beval)
1295{
1296 beval->balloonShell = gtk_window_new(GTK_WINDOW_POPUP);
1297
1298 gtk_widget_set_app_paintable(beval->balloonShell, TRUE);
Bram Moolenaar98921892016-02-23 17:14:37 +01001299# if GTK_CHECK_VERSION(3,0,0)
1300 gtk_window_set_resizable(GTK_WINDOW(beval->balloonShell), FALSE);
1301# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001302 gtk_window_set_policy(GTK_WINDOW(beval->balloonShell), FALSE, FALSE, TRUE);
Bram Moolenaar98921892016-02-23 17:14:37 +01001303# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001304 gtk_widget_set_name(beval->balloonShell, "gtk-tooltips");
Bram Moolenaar98921892016-02-23 17:14:37 +01001305# if GTK_CHECK_VERSION(3,0,0)
1306 gtk_container_set_border_width(GTK_CONTAINER(beval->balloonShell), 4);
1307# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001308 gtk_container_border_width(GTK_CONTAINER(beval->balloonShell), 4);
Bram Moolenaar98921892016-02-23 17:14:37 +01001309# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001310
Bram Moolenaar98921892016-02-23 17:14:37 +01001311# if GTK_CHECK_VERSION(3,0,0)
1312 g_signal_connect(G_OBJECT(beval->balloonShell), "draw",
1313 G_CALLBACK(balloon_draw_event_cb), NULL);
1314# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001315 gtk_signal_connect((GtkObject*)(beval->balloonShell), "expose_event",
1316 GTK_SIGNAL_FUNC(balloon_expose_event_cb), NULL);
Bram Moolenaar98921892016-02-23 17:14:37 +01001317# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001318 beval->balloonLabel = gtk_label_new(NULL);
1319
1320 gtk_label_set_line_wrap(GTK_LABEL(beval->balloonLabel), FALSE);
1321 gtk_label_set_justify(GTK_LABEL(beval->balloonLabel), GTK_JUSTIFY_LEFT);
Bram Moolenaar98921892016-02-23 17:14:37 +01001322# if GTK_CHECK_VERSION(3,16,0)
1323 gtk_label_set_xalign(GTK_LABEL(beval->balloonLabel), 0.5);
1324 gtk_label_set_yalign(GTK_LABEL(beval->balloonLabel), 0.5);
1325# elif GTK_CHECK_VERSION(3,14,0)
1326 GValue align_val = G_VALUE_INIT;
1327 g_value_init(&align_val, G_TYPE_FLOAT);
1328 g_value_set_float(&align_val, 0.5);
1329 g_object_set_property(G_OBJECT(beval->balloonLabel), "xalign", &align_val);
1330 g_object_set_property(G_OBJECT(beval->balloonLabel), "yalign", &align_val);
1331 g_value_unset(&align_val);
1332# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001333 gtk_misc_set_alignment(GTK_MISC(beval->balloonLabel), 0.5f, 0.5f);
Bram Moolenaar98921892016-02-23 17:14:37 +01001334# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001335 gtk_widget_set_name(beval->balloonLabel, "vim-balloon-label");
1336 gtk_widget_show(beval->balloonLabel);
1337
1338 gtk_container_add(GTK_CONTAINER(beval->balloonShell), beval->balloonLabel);
1339}
1340
1341#else /* !FEAT_GUI_GTK */
1342
1343/*
1344 * Draw a balloon.
1345 */
1346 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001347drawBalloon(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001348{
1349 Dimension w;
1350 Dimension h;
1351 Position tx;
1352 Position ty;
1353
1354 if (beval->msg != NULL)
1355 {
1356 /* Show the Balloon */
1357
1358 /* Calculate the label's width and height */
1359#ifdef FEAT_GUI_MOTIF
1360 XmString s;
1361
1362 /* For the callback function we parse NL characters to create a
1363 * multi-line label. This doesn't work for all languages, but
1364 * XmStringCreateLocalized() doesn't do multi-line labels... */
1365 if (beval->msgCB != NULL)
1366 s = XmStringCreateLtoR((char *)beval->msg, XmFONTLIST_DEFAULT_TAG);
1367 else
1368 s = XmStringCreateLocalized((char *)beval->msg);
1369 {
1370 XmFontList fl;
1371
1372 fl = gui_motif_fontset2fontlist(&gui.tooltip_fontset);
Bram Moolenaardb5ffaa2014-06-25 17:44:49 +02001373 if (fl == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001374 {
Bram Moolenaardb5ffaa2014-06-25 17:44:49 +02001375 XmStringFree(s);
1376 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001377 }
Bram Moolenaardb5ffaa2014-06-25 17:44:49 +02001378 XmStringExtent(fl, s, &w, &h);
1379 XmFontListFree(fl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001380 }
1381 w += gui.border_offset << 1;
1382 h += gui.border_offset << 1;
1383 XtVaSetValues(beval->balloonLabel, XmNlabelString, s, NULL);
1384 XmStringFree(s);
1385#else /* Athena */
1386 /* Assume XtNinternational == True */
1387 XFontSet fset;
1388 XFontSetExtents *ext;
1389
1390 XtVaGetValues(beval->balloonLabel, XtNfontSet, &fset, NULL);
1391 ext = XExtentsOfFontSet(fset);
1392 h = ext->max_ink_extent.height;
1393 w = XmbTextEscapement(fset,
1394 (char *)beval->msg,
1395 (int)STRLEN(beval->msg));
1396 w += gui.border_offset << 1;
1397 h += gui.border_offset << 1;
1398 XtVaSetValues(beval->balloonLabel, XtNlabel, beval->msg, NULL);
1399#endif
1400
1401 /* Compute position of the balloon area */
1402 tx = beval->x_root + EVAL_OFFSET_X;
1403 ty = beval->y_root + EVAL_OFFSET_Y;
1404 if ((tx + w) > beval->screen_width)
1405 tx = beval->screen_width - w;
1406 if ((ty + h) > beval->screen_height)
1407 ty = beval->screen_height - h;
1408#ifdef FEAT_GUI_MOTIF
1409 XtVaSetValues(beval->balloonShell,
1410 XmNx, tx,
1411 XmNy, ty,
1412 NULL);
1413#else
1414 /* Athena */
1415 XtVaSetValues(beval->balloonShell,
1416 XtNx, tx,
1417 XtNy, ty,
1418 NULL);
1419#endif
Bram Moolenaar8281f442009-03-18 11:22:25 +00001420 /* Set tooltip colors */
1421 {
1422 Arg args[2];
1423
1424#ifdef FEAT_GUI_MOTIF
1425 args[0].name = XmNbackground;
1426 args[0].value = gui.tooltip_bg_pixel;
1427 args[1].name = XmNforeground;
1428 args[1].value = gui.tooltip_fg_pixel;
1429#else /* Athena */
1430 args[0].name = XtNbackground;
1431 args[0].value = gui.tooltip_bg_pixel;
1432 args[1].name = XtNforeground;
1433 args[1].value = gui.tooltip_fg_pixel;
1434#endif
1435 XtSetValues(beval->balloonLabel, &args[0], XtNumber(args));
1436 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001437
1438 XtPopup(beval->balloonShell, XtGrabNone);
1439
1440 beval->showState = ShS_SHOWING;
1441
1442 current_beval = beval;
1443 }
1444}
1445
1446/*
1447 * Undraw a balloon.
1448 */
1449 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001450undrawBalloon(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001451{
1452 if (beval->balloonShell != (Widget)0)
1453 XtPopdown(beval->balloonShell);
1454 beval->showState = ShS_NEUTRAL;
1455
1456 current_beval = NULL;
1457}
1458
1459 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001460cancelBalloon(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001461{
1462 if (beval->showState == ShS_SHOWING
1463 || beval->showState == ShS_UPDATE_PENDING)
1464 undrawBalloon(beval);
1465
1466 if (beval->timerID != (XtIntervalId)NULL)
1467 {
1468 XtRemoveTimeOut(beval->timerID);
1469 beval->timerID = (XtIntervalId)NULL;
1470 }
1471 beval->showState = ShS_NEUTRAL;
1472}
1473
1474
1475 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001476createBalloonEvalWindow(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001477{
1478 Arg args[12];
1479 int n;
1480
1481 n = 0;
1482#ifdef FEAT_GUI_MOTIF
1483 XtSetArg(args[n], XmNallowShellResize, True); n++;
1484 beval->balloonShell = XtAppCreateShell("balloonEval", "BalloonEval",
1485 overrideShellWidgetClass, gui.dpy, args, n);
1486#else
1487 /* Athena */
1488 XtSetArg(args[n], XtNallowShellResize, True); n++;
1489 beval->balloonShell = XtAppCreateShell("balloonEval", "BalloonEval",
1490 overrideShellWidgetClass, gui.dpy, args, n);
1491#endif
1492
1493 n = 0;
1494#ifdef FEAT_GUI_MOTIF
1495 {
1496 XmFontList fl;
1497
1498 fl = gui_motif_fontset2fontlist(&gui.tooltip_fontset);
1499 XtSetArg(args[n], XmNforeground, gui.tooltip_fg_pixel); n++;
1500 XtSetArg(args[n], XmNbackground, gui.tooltip_bg_pixel); n++;
1501 XtSetArg(args[n], XmNfontList, fl); n++;
1502 XtSetArg(args[n], XmNalignment, XmALIGNMENT_BEGINNING); n++;
1503 beval->balloonLabel = XtCreateManagedWidget("balloonLabel",
1504 xmLabelWidgetClass, beval->balloonShell, args, n);
1505 }
1506#else /* FEAT_GUI_ATHENA */
1507 XtSetArg(args[n], XtNforeground, gui.tooltip_fg_pixel); n++;
1508 XtSetArg(args[n], XtNbackground, gui.tooltip_bg_pixel); n++;
1509 XtSetArg(args[n], XtNinternational, True); n++;
1510 XtSetArg(args[n], XtNfontSet, gui.tooltip_fontset); n++;
1511 beval->balloonLabel = XtCreateManagedWidget("balloonLabel",
1512 labelWidgetClass, beval->balloonShell, args, n);
1513#endif
1514}
1515
1516#endif /* !FEAT_GUI_GTK */
1517#endif /* !FEAT_GUI_W32 */
1518
1519#endif /* FEAT_BEVAL */