blob: ada048c62ce373bc4dce4610eccd0f0847357717 [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 Moolenaare2ac10d2005-03-07 23:26:06 +000032 win_T *cw;
Bram Moolenaar33aec762006-01-22 23:30:12 +000033#endif
Bram Moolenaar54a709e2006-05-04 21:57:11 +000034 static int recursive = FALSE;
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000035
36 /* Don't do anything when 'ballooneval' is off, messages scrolled the
37 * windows up or we have no beval area. */
Bram Moolenaar51b0f372017-11-18 18:52:04 +010038 if (!((gui.in_use && p_beval)
39# ifdef FEAT_BEVALTERM
40 || (!gui.in_use && p_bevalterm)
41# endif
42 ) || beval == NULL || msg_scrolled > 0)
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000043 return;
44
Bram Moolenaar54a709e2006-05-04 21:57:11 +000045 /* Don't do this recursively. Happens when the expression evaluation
46 * takes a long time and invokes something that checks for CTRL-C typed. */
47 if (recursive)
48 return;
49 recursive = TRUE;
50
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000051#ifdef FEAT_EVAL
Bram Moolenaar51b0f372017-11-18 18:52:04 +010052 if (get_beval_info(beval, TRUE, &wp, &lnum, &text, &col) == OK)
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000053 {
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000054 bexpr = (*wp->w_buffer->b_p_bexpr == NUL) ? p_bexpr
55 : wp->w_buffer->b_p_bexpr;
56 if (*bexpr != NUL)
57 {
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000058 /* Convert window pointer to number. */
59 for (cw = firstwin; cw != wp; cw = cw->w_next)
60 ++winnr;
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 {
Bram Moolenaar51b0f372017-11-18 18:52:04 +0100103 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 Moolenaar51b0f372017-11-18 18:52:04 +0100342# ifdef FEAT_BEVALTERM
343 if (!gui.in_use)
344 {
345 row = mouse_row;
346 col = mouse_col;
347 }
348 else
349# endif
350 {
351 row = Y_2_ROW(beval->y);
352 col = X_2_COL(beval->x);
353 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000354 wp = mouse_find_win(&row, &col);
Bram Moolenaar02631462017-09-22 15:20:32 +0200355 if (wp != NULL && row < wp->w_height && col < wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000356 {
357 /* Found a window and the cursor is in the text. Now find the line
358 * number. */
359 if (!mouse_comp_pos(wp, &row, &col, &lnum))
360 {
361 /* Not past end of the file. */
362 lbuf = ml_get_buf(wp->w_buffer, lnum, FALSE);
363 if (col <= win_linetabsize(wp, lbuf, (colnr_T)MAXCOL))
364 {
365 /* Not past end of line. */
Bram Moolenaare2ac10d2005-03-07 23:26:06 +0000366 if (getword)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000367 {
368 /* For Netbeans we get the relevant part of the line
369 * instead of the whole line. */
370 int len;
371 pos_T *spos = NULL, *epos = NULL;
372
373 if (VIsual_active)
374 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +0100375 if (LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000376 {
377 spos = &VIsual;
378 epos = &curwin->w_cursor;
379 }
380 else
381 {
382 spos = &curwin->w_cursor;
383 epos = &VIsual;
384 }
385 }
386
Bram Moolenaarb6101cf2012-10-21 00:58:39 +0200387 col = vcol2col(wp, lnum, col);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000388
389 if (VIsual_active
390 && wp->w_buffer == curwin->w_buffer
391 && (lnum == spos->lnum
392 ? col >= (int)spos->col
393 : lnum > spos->lnum)
394 && (lnum == epos->lnum
395 ? col <= (int)epos->col
396 : lnum < epos->lnum))
397 {
398 /* Visual mode and pointing to the line with the
399 * Visual selection: return selected text, with a
400 * maximum of one line. */
401 if (spos->lnum != epos->lnum || spos->col == epos->col)
402 return FAIL;
403
404 lbuf = ml_get_buf(curwin->w_buffer, VIsual.lnum, FALSE);
Bram Moolenaarb6101cf2012-10-21 00:58:39 +0200405 len = epos->col - spos->col;
406 if (*p_sel != 'e')
407 len += MB_PTR2LEN(lbuf + epos->col);
408 lbuf = vim_strnsave(lbuf + spos->col, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000409 lnum = spos->lnum;
410 col = spos->col;
411 }
412 else
413 {
414 /* Find the word under the cursor. */
415 ++emsg_off;
416 len = find_ident_at_pos(wp, lnum, (colnr_T)col, &lbuf,
417 FIND_IDENT + FIND_STRING + FIND_EVAL);
418 --emsg_off;
419 if (len == 0)
420 return FAIL;
421 lbuf = vim_strnsave(lbuf, len);
422 }
423 }
Bram Moolenaare2ac10d2005-03-07 23:26:06 +0000424
425 *winp = wp;
426 *lnump = lnum;
427 *textp = lbuf;
428 *colp = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000429 beval->ts = wp->w_buffer->b_p_ts;
430 return OK;
431 }
432 }
433 }
434
435 return FAIL;
436}
437
Bram Moolenaar51b0f372017-11-18 18:52:04 +0100438/*
439 * Show a balloon with "mesg".
440 */
441 void
442post_balloon(BalloonEval *beval, char_u *mesg)
443{
444# ifdef FEAT_BEVALTERM
445 if (!gui.in_use)
446 ui_post_balloon(mesg);
447 else
448# endif
449 gui_mch_post_balloon(beval, mesg);
450}
451
Bram Moolenaar071d4272004-06-13 20:20:40 +0000452# if !defined(FEAT_GUI_W32) || defined(PROTO)
453
454/*
455 * Show a balloon with "mesg".
456 */
457 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100458gui_mch_post_balloon(BalloonEval *beval, char_u *mesg)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000459{
460 beval->msg = mesg;
461 if (mesg != NULL)
462 drawBalloon(beval);
463 else
464 undrawBalloon(beval);
465}
466# endif /* FEAT_GUI_W32 */
467#endif /* FEAT_SUN_WORKSHOP || FEAT_NETBEANS_INTG || PROTO */
468
469#if !defined(FEAT_GUI_W32) || defined(PROTO)
470#if defined(FEAT_BEVAL_TIP) || defined(PROTO)
471/*
472 * Hide the given balloon.
473 */
474 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100475gui_mch_unpost_balloon(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000476{
477 undrawBalloon(beval);
478}
479#endif
480
481#ifdef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +0000482 static void
483addEventHandler(GtkWidget *target, BalloonEval *beval)
484{
485 /*
486 * Connect to the generic "event" signal instead of the individual
487 * signals for each event type, because the former is emitted earlier.
488 * This allows us to catch events independently of the signal handlers
489 * in gui_gtk_x11.c.
490 */
Bram Moolenaar98921892016-02-23 17:14:37 +0100491# if GTK_CHECK_VERSION(3,0,0)
492 g_signal_connect(G_OBJECT(target), "event",
493 G_CALLBACK(target_event_cb),
494 beval);
495# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000496 /* Should use GTK_OBJECT() here, but that causes a lint warning... */
497 gtk_signal_connect((GtkObject*)(target), "event",
498 GTK_SIGNAL_FUNC(target_event_cb),
499 beval);
Bram Moolenaar98921892016-02-23 17:14:37 +0100500# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000501 /*
502 * Nasty: Key press events go to the main window thus the drawing area
503 * will never see them. This means we have to connect to the main window
504 * as well in order to catch those events.
505 */
506 if (gtk_socket_id == 0 && gui.mainwin != NULL
507 && gtk_widget_is_ancestor(target, gui.mainwin))
508 {
Bram Moolenaar98921892016-02-23 17:14:37 +0100509# if GTK_CHECK_VERSION(3,0,0)
510 g_signal_connect(G_OBJECT(gui.mainwin), "event",
511 G_CALLBACK(mainwin_event_cb),
512 beval);
513# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000514 gtk_signal_connect((GtkObject*)(gui.mainwin), "event",
515 GTK_SIGNAL_FUNC(mainwin_event_cb),
516 beval);
Bram Moolenaar98921892016-02-23 17:14:37 +0100517# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000518 }
519}
520
521 static void
522removeEventHandler(BalloonEval *beval)
523{
Bram Moolenaar33570922005-01-25 22:26:29 +0000524 /* LINTED: avoid warning: dubious operation on enum */
Bram Moolenaar98921892016-02-23 17:14:37 +0100525# if GTK_CHECK_VERSION(3,0,0)
526 g_signal_handlers_disconnect_by_func(G_OBJECT(beval->target),
Bram Moolenaard47d8372016-09-09 22:13:24 +0200527 FUNC2GENERIC(target_event_cb),
Bram Moolenaar98921892016-02-23 17:14:37 +0100528 beval);
529# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000530 gtk_signal_disconnect_by_func((GtkObject*)(beval->target),
531 GTK_SIGNAL_FUNC(target_event_cb),
532 beval);
Bram Moolenaar98921892016-02-23 17:14:37 +0100533# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000534
535 if (gtk_socket_id == 0 && gui.mainwin != NULL
536 && gtk_widget_is_ancestor(beval->target, gui.mainwin))
537 {
Bram Moolenaar33570922005-01-25 22:26:29 +0000538 /* LINTED: avoid warning: dubious operation on enum */
Bram Moolenaar98921892016-02-23 17:14:37 +0100539# if GTK_CHECK_VERSION(3,0,0)
540 g_signal_handlers_disconnect_by_func(G_OBJECT(gui.mainwin),
Bram Moolenaard47d8372016-09-09 22:13:24 +0200541 FUNC2GENERIC(mainwin_event_cb),
Bram Moolenaar98921892016-02-23 17:14:37 +0100542 beval);
543# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000544 gtk_signal_disconnect_by_func((GtkObject*)(gui.mainwin),
545 GTK_SIGNAL_FUNC(mainwin_event_cb),
546 beval);
Bram Moolenaar98921892016-02-23 17:14:37 +0100547# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000548 }
549}
550
551 static gint
552target_event_cb(GtkWidget *widget, GdkEvent *event, gpointer data)
553{
554 BalloonEval *beval = (BalloonEval *)data;
555
556 switch (event->type)
557 {
558 case GDK_ENTER_NOTIFY:
559 pointer_event(beval, (int)event->crossing.x,
560 (int)event->crossing.y,
561 event->crossing.state);
562 break;
563 case GDK_MOTION_NOTIFY:
564 if (event->motion.is_hint)
565 {
566 int x;
567 int y;
568 GdkModifierType state;
569 /*
570 * GDK_POINTER_MOTION_HINT_MASK is set, thus we cannot obtain
571 * the coordinates from the GdkEventMotion struct directly.
572 */
Bram Moolenaar98921892016-02-23 17:14:37 +0100573# if GTK_CHECK_VERSION(3,0,0)
574 {
575 GdkWindow * const win = gtk_widget_get_window(widget);
576 GdkDisplay * const dpy = gdk_window_get_display(win);
Bram Moolenaar30e12d22016-04-17 20:49:53 +0200577# if GTK_CHECK_VERSION(3,20,0)
578 GdkSeat * const seat = gdk_display_get_default_seat(dpy);
579 GdkDevice * const dev = gdk_seat_get_pointer(seat);
580# else
Bram Moolenaar98921892016-02-23 17:14:37 +0100581 GdkDeviceManager * const mngr = gdk_display_get_device_manager(dpy);
582 GdkDevice * const dev = gdk_device_manager_get_client_pointer(mngr);
Bram Moolenaar30e12d22016-04-17 20:49:53 +0200583# endif
Bram Moolenaar98921892016-02-23 17:14:37 +0100584 gdk_window_get_device_position(win, dev , &x, &y, &state);
585 }
586# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000587 gdk_window_get_pointer(widget->window, &x, &y, &state);
Bram Moolenaar98921892016-02-23 17:14:37 +0100588# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000589 pointer_event(beval, x, y, (unsigned int)state);
590 }
591 else
592 {
593 pointer_event(beval, (int)event->motion.x,
594 (int)event->motion.y,
595 event->motion.state);
596 }
597 break;
598 case GDK_LEAVE_NOTIFY:
599 /*
600 * Ignore LeaveNotify events that are not "normal".
601 * Apparently we also get it when somebody else grabs focus.
602 */
603 if (event->crossing.mode == GDK_CROSSING_NORMAL)
604 cancelBalloon(beval);
605 break;
606 case GDK_BUTTON_PRESS:
Bram Moolenaar071d4272004-06-13 20:20:40 +0000607 case GDK_SCROLL:
Bram Moolenaar071d4272004-06-13 20:20:40 +0000608 cancelBalloon(beval);
609 break;
610 case GDK_KEY_PRESS:
611 key_event(beval, event->key.keyval, TRUE);
612 break;
613 case GDK_KEY_RELEASE:
614 key_event(beval, event->key.keyval, FALSE);
615 break;
616 default:
617 break;
618 }
619
620 return FALSE; /* continue emission */
621}
622
Bram Moolenaar071d4272004-06-13 20:20:40 +0000623 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +0000624mainwin_event_cb(GtkWidget *widget UNUSED, GdkEvent *event, gpointer data)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000625{
626 BalloonEval *beval = (BalloonEval *)data;
627
628 switch (event->type)
629 {
630 case GDK_KEY_PRESS:
631 key_event(beval, event->key.keyval, TRUE);
632 break;
633 case GDK_KEY_RELEASE:
634 key_event(beval, event->key.keyval, FALSE);
635 break;
636 default:
637 break;
638 }
639
640 return FALSE; /* continue emission */
641}
642
643 static void
644pointer_event(BalloonEval *beval, int x, int y, unsigned state)
645{
646 int distance;
647
648 distance = ABS(x - beval->x) + ABS(y - beval->y);
649
650 if (distance > 4)
651 {
652 /*
653 * Moved out of the balloon location: cancel it.
654 * Remember button state
655 */
656 beval->state = state;
657 cancelBalloon(beval);
658
659 /* Mouse buttons are pressed - no balloon now */
660 if (!(state & ((int)GDK_BUTTON1_MASK | (int)GDK_BUTTON2_MASK
661 | (int)GDK_BUTTON3_MASK)))
662 {
663 beval->x = x;
664 beval->y = y;
665
666 if (state & (int)GDK_MOD1_MASK)
667 {
668 /*
669 * Alt is pressed -- enter super-evaluate-mode,
670 * where there is no time delay
671 */
672 if (beval->msgCB != NULL)
673 {
674 beval->showState = ShS_PENDING;
675 (*beval->msgCB)(beval, state);
676 }
677 }
678 else
679 {
Bram Moolenaar98921892016-02-23 17:14:37 +0100680# if GTK_CHECK_VERSION(3,0,0)
681 beval->timerID = g_timeout_add((guint)p_bdlay,
682 &timeout_cb, beval);
683# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000684 beval->timerID = gtk_timeout_add((guint32)p_bdlay,
685 &timeout_cb, beval);
Bram Moolenaar98921892016-02-23 17:14:37 +0100686# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000687 }
688 }
689 }
690}
691
692 static void
693key_event(BalloonEval *beval, unsigned keyval, int is_keypress)
694{
695 if (beval->showState == ShS_SHOWING && beval->msgCB != NULL)
696 {
697 switch (keyval)
698 {
699 case GDK_Shift_L:
700 case GDK_Shift_R:
701 beval->showState = ShS_UPDATE_PENDING;
702 (*beval->msgCB)(beval, (is_keypress)
703 ? (int)GDK_SHIFT_MASK : 0);
704 break;
705 case GDK_Control_L:
706 case GDK_Control_R:
707 beval->showState = ShS_UPDATE_PENDING;
708 (*beval->msgCB)(beval, (is_keypress)
709 ? (int)GDK_CONTROL_MASK : 0);
710 break;
711 default:
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000712 /* Don't do this for key release, we apparently get these with
713 * focus changes in some GTK version. */
714 if (is_keypress)
715 cancelBalloon(beval);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000716 break;
717 }
718 }
719 else
720 cancelBalloon(beval);
721}
722
Bram Moolenaar98921892016-02-23 17:14:37 +0100723# if GTK_CHECK_VERSION(3,0,0)
724 static gboolean
725# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000726 static gint
Bram Moolenaar98921892016-02-23 17:14:37 +0100727# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000728timeout_cb(gpointer data)
729{
730 BalloonEval *beval = (BalloonEval *)data;
731
732 beval->timerID = 0;
733 /*
734 * If the timer event happens then the mouse has stopped long enough for
735 * a request to be started. The request will only send to the debugger if
736 * there the mouse is pointing at real data.
737 */
738 requestBalloon(beval);
739
740 return FALSE; /* don't call me again */
741}
742
Bram Moolenaar98921892016-02-23 17:14:37 +0100743# if GTK_CHECK_VERSION(3,0,0)
744 static gboolean
745balloon_draw_event_cb(GtkWidget *widget,
746 cairo_t *cr,
747 gpointer data UNUSED)
748{
749 GtkStyleContext *context = NULL;
750 gint width = -1, height = -1;
751
752 if (widget == NULL)
753 return TRUE;
754
755 context = gtk_widget_get_style_context(widget);
756 width = gtk_widget_get_allocated_width(widget);
757 height = gtk_widget_get_allocated_height(widget);
758
759 gtk_style_context_save(context);
760
761 gtk_style_context_add_class(context, "tooltip");
762 gtk_style_context_set_state(context, GTK_STATE_FLAG_NORMAL);
763
764 cairo_save(cr);
765 gtk_render_frame(context, cr, 0, 0, width, height);
766 gtk_render_background(context, cr, 0, 0, width, height);
767 cairo_restore(cr);
768
769 gtk_style_context_restore(context);
770
771 return FALSE;
772}
773# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000774 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +0000775balloon_expose_event_cb(GtkWidget *widget,
776 GdkEventExpose *event,
777 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000778{
779 gtk_paint_flat_box(widget->style, widget->window,
780 GTK_STATE_NORMAL, GTK_SHADOW_OUT,
781 &event->area, widget, "tooltip",
782 0, 0, -1, -1);
783
784 return FALSE; /* continue emission */
785}
Bram Moolenaar98921892016-02-23 17:14:37 +0100786# endif /* !GTK_CHECK_VERSION(3,0,0) */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000787
Bram Moolenaar071d4272004-06-13 20:20:40 +0000788#else /* !FEAT_GUI_GTK */
789
790 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100791addEventHandler(Widget target, BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000792{
793 XtAddEventHandler(target,
794 PointerMotionMask | EnterWindowMask |
795 LeaveWindowMask | ButtonPressMask | KeyPressMask |
796 KeyReleaseMask,
797 False,
798 pointerEventEH, (XtPointer)beval);
799}
800
801 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100802removeEventHandler(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000803{
804 XtRemoveEventHandler(beval->target,
805 PointerMotionMask | EnterWindowMask |
806 LeaveWindowMask | ButtonPressMask | KeyPressMask |
807 KeyReleaseMask,
808 False,
809 pointerEventEH, (XtPointer)beval);
810}
811
812
813/*
814 * The X event handler. All it does is call the real event handler.
815 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000816 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100817pointerEventEH(
818 Widget w UNUSED,
819 XtPointer client_data,
820 XEvent *event,
821 Boolean *unused UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000822{
823 BalloonEval *beval = (BalloonEval *)client_data;
824 pointerEvent(beval, event);
825}
826
827
828/*
829 * The real event handler. Called by pointerEventEH() whenever an event we are
Bram Moolenaarbae0c162007-05-10 19:30:25 +0000830 * interested in occurs.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000831 */
832
833 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100834pointerEvent(BalloonEval *beval, XEvent *event)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000835{
Bram Moolenaar84a05ac2013-05-06 04:24:17 +0200836 Position distance; /* a measure of how much the pointer moved */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000837 Position delta; /* used to compute distance */
838
839 switch (event->type)
840 {
841 case EnterNotify:
842 case MotionNotify:
843 delta = event->xmotion.x - beval->x;
844 if (delta < 0)
845 delta = -delta;
846 distance = delta;
847 delta = event->xmotion.y - beval->y;
848 if (delta < 0)
849 delta = -delta;
850 distance += delta;
851 if (distance > 4)
852 {
853 /*
854 * Moved out of the balloon location: cancel it.
855 * Remember button state
856 */
857 beval->state = event->xmotion.state;
858 if (beval->state & (Button1Mask|Button2Mask|Button3Mask))
859 {
860 /* Mouse buttons are pressed - no balloon now */
861 cancelBalloon(beval);
862 }
863 else if (beval->state & (Mod1Mask|Mod2Mask|Mod3Mask))
864 {
865 /*
866 * Alt is pressed -- enter super-evaluate-mode,
867 * where there is no time delay
868 */
869 beval->x = event->xmotion.x;
870 beval->y = event->xmotion.y;
871 beval->x_root = event->xmotion.x_root;
872 beval->y_root = event->xmotion.y_root;
873 cancelBalloon(beval);
874 if (beval->msgCB != NULL)
875 {
876 beval->showState = ShS_PENDING;
877 (*beval->msgCB)(beval, beval->state);
878 }
879 }
880 else
881 {
882 beval->x = event->xmotion.x;
883 beval->y = event->xmotion.y;
884 beval->x_root = event->xmotion.x_root;
885 beval->y_root = event->xmotion.y_root;
886 cancelBalloon(beval);
887 beval->timerID = XtAppAddTimeOut( beval->appContext,
888 (long_u)p_bdlay, timerRoutine, beval);
889 }
890 }
891 break;
892
893 /*
894 * Motif and Athena version: Keystrokes will be caught by the
895 * "textArea" widget, and handled in gui_x11_key_hit_cb().
896 */
897 case KeyPress:
898 if (beval->showState == ShS_SHOWING && beval->msgCB != NULL)
899 {
900 Modifiers modifier;
901 KeySym keysym;
902
903 XtTranslateKeycode(gui.dpy,
904 event->xkey.keycode, event->xkey.state,
905 &modifier, &keysym);
906 if (keysym == XK_Shift_L || keysym == XK_Shift_R)
907 {
908 beval->showState = ShS_UPDATE_PENDING;
909 (*beval->msgCB)(beval, ShiftMask);
910 }
911 else if (keysym == XK_Control_L || keysym == XK_Control_R)
912 {
913 beval->showState = ShS_UPDATE_PENDING;
914 (*beval->msgCB)(beval, ControlMask);
915 }
916 else
917 cancelBalloon(beval);
918 }
919 else
920 cancelBalloon(beval);
921 break;
922
923 case KeyRelease:
924 if (beval->showState == ShS_SHOWING && beval->msgCB != NULL)
925 {
926 Modifiers modifier;
927 KeySym keysym;
928
929 XtTranslateKeycode(gui.dpy, event->xkey.keycode,
930 event->xkey.state, &modifier, &keysym);
931 if ((keysym == XK_Shift_L) || (keysym == XK_Shift_R)) {
932 beval->showState = ShS_UPDATE_PENDING;
933 (*beval->msgCB)(beval, 0);
934 }
935 else if ((keysym == XK_Control_L) || (keysym == XK_Control_R))
936 {
937 beval->showState = ShS_UPDATE_PENDING;
938 (*beval->msgCB)(beval, 0);
939 }
940 else
941 cancelBalloon(beval);
942 }
943 else
944 cancelBalloon(beval);
945 break;
946
947 case LeaveNotify:
948 /* Ignore LeaveNotify events that are not "normal".
949 * Apparently we also get it when somebody else grabs focus.
950 * Happens for me every two seconds (some clipboard tool?) */
951 if (event->xcrossing.mode == NotifyNormal)
952 cancelBalloon(beval);
953 break;
954
955 case ButtonPress:
956 cancelBalloon(beval);
957 break;
958
959 default:
960 break;
961 }
962}
963
Bram Moolenaar071d4272004-06-13 20:20:40 +0000964 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100965timerRoutine(XtPointer dx, XtIntervalId *id UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000966{
967 BalloonEval *beval = (BalloonEval *)dx;
968
969 beval->timerID = (XtIntervalId)NULL;
970
971 /*
972 * If the timer event happens then the mouse has stopped long enough for
973 * a request to be started. The request will only send to the debugger if
974 * there the mouse is pointing at real data.
975 */
976 requestBalloon(beval);
977}
978
979#endif /* !FEAT_GUI_GTK */
980
981 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100982requestBalloon(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000983{
984 if (beval->showState != ShS_PENDING)
985 {
986 /* Determine the beval to display */
987 if (beval->msgCB != NULL)
988 {
989 beval->showState = ShS_PENDING;
990 (*beval->msgCB)(beval, beval->state);
991 }
992 else if (beval->msg != NULL)
993 drawBalloon(beval);
994 }
995}
996
997#ifdef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +0000998/*
999 * Convert the string to UTF-8 if 'encoding' is not "utf-8".
1000 * Replace any non-printable characters and invalid bytes sequences with
1001 * "^X" or "<xx>" escapes, and apply SpecialKey highlighting to them.
1002 * TAB and NL are passed through unscathed.
1003 */
Bram Moolenaar182c5be2010-06-25 05:37:59 +02001004# define IS_NONPRINTABLE(c) (((c) < 0x20 && (c) != TAB && (c) != NL) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00001005 || (c) == DEL)
1006 static void
Bram Moolenaar89d40322006-08-29 15:30:07 +00001007set_printable_label_text(GtkLabel *label, char_u *text)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001008{
1009 char_u *convbuf = NULL;
1010 char_u *buf;
1011 char_u *p;
1012 char_u *pdest;
1013 unsigned int len;
1014 int charlen;
1015 int uc;
1016 PangoAttrList *attr_list;
1017
1018 /* Convert to UTF-8 if it isn't already */
1019 if (output_conv.vc_type != CONV_NONE)
1020 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00001021 convbuf = string_convert(&output_conv, text, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001022 if (convbuf != NULL)
Bram Moolenaar89d40322006-08-29 15:30:07 +00001023 text = convbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001024 }
1025
1026 /* First let's see how much we need to allocate */
1027 len = 0;
Bram Moolenaar89d40322006-08-29 15:30:07 +00001028 for (p = text; *p != NUL; p += charlen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001029 {
1030 if ((*p & 0x80) == 0) /* be quick for ASCII */
1031 {
1032 charlen = 1;
1033 len += IS_NONPRINTABLE(*p) ? 2 : 1; /* nonprintable: ^X */
1034 }
1035 else
1036 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001037 charlen = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001038 uc = utf_ptr2char(p);
1039
1040 if (charlen != utf_char2len(uc))
1041 charlen = 1; /* reject overlong sequences */
1042
1043 if (charlen == 1 || uc < 0xa0) /* illegal byte or */
1044 len += 4; /* control char: <xx> */
1045 else if (!utf_printable(uc))
1046 /* Note: we assume here that utf_printable() doesn't
1047 * care about characters outside the BMP. */
1048 len += 6; /* nonprintable: <xxxx> */
1049 else
1050 len += charlen;
1051 }
1052 }
1053
1054 attr_list = pango_attr_list_new();
1055 buf = alloc(len + 1);
1056
1057 /* Now go for the real work */
1058 if (buf != NULL)
1059 {
1060 attrentry_T *aep;
1061 PangoAttribute *attr;
1062 guicolor_T pixel;
Bram Moolenaar36edf062016-07-21 22:10:12 +02001063#if GTK_CHECK_VERSION(3,0,0)
1064 GdkRGBA color = { 0.0, 0.0, 0.0, 1.0 };
Bram Moolenaar870b7492016-07-22 22:26:52 +02001065# if PANGO_VERSION_CHECK(1,38,0)
Bram Moolenaar36edf062016-07-21 22:10:12 +02001066 PangoAttribute *attr_alpha;
Bram Moolenaar870b7492016-07-22 22:26:52 +02001067# endif
Bram Moolenaar36edf062016-07-21 22:10:12 +02001068#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001069 GdkColor color = { 0, 0, 0, 0 };
Bram Moolenaar36edf062016-07-21 22:10:12 +02001070#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001071
1072 /* Look up the RGB values of the SpecialKey foreground color. */
Bram Moolenaar8820b482017-03-16 17:23:31 +01001073 aep = syn_gui_attr2entry(HL_ATTR(HLF_8));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001074 pixel = (aep != NULL) ? aep->ae_u.gui.fg_color : INVALCOLOR;
1075 if (pixel != INVALCOLOR)
Bram Moolenaar98921892016-02-23 17:14:37 +01001076# if GTK_CHECK_VERSION(3,0,0)
1077 {
Bram Moolenaar36edf062016-07-21 22:10:12 +02001078 color.red = ((pixel & 0xff0000) >> 16) / 255.0;
1079 color.green = ((pixel & 0xff00) >> 8) / 255.0;
1080 color.blue = (pixel & 0xff) / 255.0;
1081 color.alpha = 1.0;
Bram Moolenaar98921892016-02-23 17:14:37 +01001082 }
1083# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001084 gdk_colormap_query_color(gtk_widget_get_colormap(gui.drawarea),
1085 (unsigned long)pixel, &color);
Bram Moolenaar98921892016-02-23 17:14:37 +01001086# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001087
1088 pdest = buf;
Bram Moolenaar89d40322006-08-29 15:30:07 +00001089 p = text;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001090 while (*p != NUL)
1091 {
1092 /* Be quick for ASCII */
1093 if ((*p & 0x80) == 0 && !IS_NONPRINTABLE(*p))
1094 {
1095 *pdest++ = *p++;
1096 }
1097 else
1098 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001099 charlen = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001100 uc = utf_ptr2char(p);
1101
1102 if (charlen != utf_char2len(uc))
1103 charlen = 1; /* reject overlong sequences */
1104
1105 if (charlen == 1 || uc < 0xa0 || !utf_printable(uc))
1106 {
1107 int outlen;
1108
1109 /* Careful: we can't just use transchar_byte() here,
1110 * since 'encoding' is not necessarily set to "utf-8". */
1111 if (*p & 0x80 && charlen == 1)
1112 {
1113 transchar_hex(pdest, *p); /* <xx> */
1114 outlen = 4;
1115 }
1116 else if (uc >= 0x80)
1117 {
1118 /* Note: we assume here that utf_printable() doesn't
1119 * care about characters outside the BMP. */
1120 transchar_hex(pdest, uc); /* <xx> or <xxxx> */
1121 outlen = (uc < 0x100) ? 4 : 6;
1122 }
1123 else
1124 {
1125 transchar_nonprint(pdest, *p); /* ^X */
1126 outlen = 2;
1127 }
1128 if (pixel != INVALCOLOR)
1129 {
Bram Moolenaar36edf062016-07-21 22:10:12 +02001130#if GTK_CHECK_VERSION(3,0,0)
1131# define DOUBLE2UINT16(val) ((guint16)((val) * 65535 + 0.5))
1132 attr = pango_attr_foreground_new(
1133 DOUBLE2UINT16(color.red),
1134 DOUBLE2UINT16(color.green),
1135 DOUBLE2UINT16(color.blue));
Bram Moolenaar870b7492016-07-22 22:26:52 +02001136# if PANGO_VERSION_CHECK(1,38,0)
Bram Moolenaar36edf062016-07-21 22:10:12 +02001137 attr_alpha = pango_attr_foreground_alpha_new(
1138 DOUBLE2UINT16(color.alpha));
Bram Moolenaar870b7492016-07-22 22:26:52 +02001139# endif
Bram Moolenaar36edf062016-07-21 22:10:12 +02001140# undef DOUBLE2UINT16
1141#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001142 attr = pango_attr_foreground_new(
1143 color.red, color.green, color.blue);
Bram Moolenaar36edf062016-07-21 22:10:12 +02001144#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001145 attr->start_index = pdest - buf;
1146 attr->end_index = pdest - buf + outlen;
1147 pango_attr_list_insert(attr_list, attr);
Bram Moolenaar36edf062016-07-21 22:10:12 +02001148#if GTK_CHECK_VERSION(3,0,0)
Bram Moolenaar870b7492016-07-22 22:26:52 +02001149# if PANGO_VERSION_CHECK(1,38,0)
Bram Moolenaar36edf062016-07-21 22:10:12 +02001150 attr_alpha->start_index = pdest - buf;
1151 attr_alpha->end_index = pdest - buf + outlen;
1152 pango_attr_list_insert(attr_list, attr_alpha);
Bram Moolenaar870b7492016-07-22 22:26:52 +02001153# endif
Bram Moolenaar36edf062016-07-21 22:10:12 +02001154#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001155 }
1156 pdest += outlen;
1157 p += charlen;
1158 }
1159 else
1160 {
1161 do
1162 *pdest++ = *p++;
1163 while (--charlen != 0);
1164 }
1165 }
1166 }
1167 *pdest = NUL;
1168 }
1169
1170 vim_free(convbuf);
1171
1172 gtk_label_set_text(label, (const char *)buf);
1173 vim_free(buf);
1174
1175 gtk_label_set_attributes(label, attr_list);
1176 pango_attr_list_unref(attr_list);
1177}
Bram Moolenaar182c5be2010-06-25 05:37:59 +02001178# undef IS_NONPRINTABLE
Bram Moolenaar071d4272004-06-13 20:20:40 +00001179
1180/*
1181 * Draw a balloon.
1182 */
1183 static void
1184drawBalloon(BalloonEval *beval)
1185{
1186 if (beval->msg != NULL)
1187 {
1188 GtkRequisition requisition;
1189 int screen_w;
1190 int screen_h;
1191 int x;
1192 int y;
1193 int x_offset = EVAL_OFFSET_X;
1194 int y_offset = EVAL_OFFSET_Y;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001195 PangoLayout *layout;
Bram Moolenaara859f042016-11-17 19:11:55 +01001196
Bram Moolenaar7be9b502017-09-09 18:45:26 +02001197# if !GTK_CHECK_VERSION(3,22,2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001198 GdkScreen *screen;
1199
1200 screen = gtk_widget_get_screen(beval->target);
1201 gtk_window_set_screen(GTK_WINDOW(beval->balloonShell), screen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001202# endif
Bram Moolenaar7be9b502017-09-09 18:45:26 +02001203 gui_gtk_get_screen_size_of_win(beval->balloonShell,
1204 &screen_w, &screen_h);
Bram Moolenaar98921892016-02-23 17:14:37 +01001205# if !GTK_CHECK_VERSION(3,0,0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001206 gtk_widget_ensure_style(beval->balloonShell);
1207 gtk_widget_ensure_style(beval->balloonLabel);
Bram Moolenaar98921892016-02-23 17:14:37 +01001208# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001209
Bram Moolenaar071d4272004-06-13 20:20:40 +00001210 set_printable_label_text(GTK_LABEL(beval->balloonLabel), beval->msg);
1211 /*
1212 * Dirty trick: Enable wrapping mode on the label's layout behind its
1213 * back. This way GtkLabel won't try to constrain the wrap width to a
1214 * builtin maximum value of about 65 Latin characters.
1215 */
1216 layout = gtk_label_get_layout(GTK_LABEL(beval->balloonLabel));
Bram Moolenaar182c5be2010-06-25 05:37:59 +02001217# ifdef PANGO_WRAP_WORD_CHAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00001218 pango_layout_set_wrap(layout, PANGO_WRAP_WORD_CHAR);
Bram Moolenaar182c5be2010-06-25 05:37:59 +02001219# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001220 pango_layout_set_wrap(layout, PANGO_WRAP_WORD);
Bram Moolenaar182c5be2010-06-25 05:37:59 +02001221# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001222 pango_layout_set_width(layout,
1223 /* try to come up with some reasonable width */
1224 PANGO_SCALE * CLAMP(gui.num_cols * gui.char_width,
1225 screen_w / 2,
1226 MAX(20, screen_w - 20)));
1227
1228 /* Calculate the balloon's width and height. */
Bram Moolenaar98921892016-02-23 17:14:37 +01001229# if GTK_CHECK_VERSION(3,0,0)
1230 gtk_widget_get_preferred_size(beval->balloonShell, &requisition, NULL);
1231# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001232 gtk_widget_size_request(beval->balloonShell, &requisition);
Bram Moolenaar98921892016-02-23 17:14:37 +01001233# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001234
1235 /* Compute position of the balloon area */
Bram Moolenaar98921892016-02-23 17:14:37 +01001236# if GTK_CHECK_VERSION(3,0,0)
1237 gdk_window_get_origin(gtk_widget_get_window(beval->target), &x, &y);
1238# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001239 gdk_window_get_origin(beval->target->window, &x, &y);
Bram Moolenaar98921892016-02-23 17:14:37 +01001240# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001241 x += beval->x;
1242 y += beval->y;
1243
1244 /* Get out of the way of the mouse pointer */
1245 if (x + x_offset + requisition.width > screen_w)
1246 y_offset += 15;
1247 if (y + y_offset + requisition.height > screen_h)
1248 y_offset = -requisition.height - EVAL_OFFSET_Y;
1249
1250 /* Sanitize values */
1251 x = CLAMP(x + x_offset, 0, MAX(0, screen_w - requisition.width));
1252 y = CLAMP(y + y_offset, 0, MAX(0, screen_h - requisition.height));
1253
1254 /* Show the balloon */
Bram Moolenaar98921892016-02-23 17:14:37 +01001255# if GTK_CHECK_VERSION(3,0,0)
1256 gtk_window_move(GTK_WINDOW(beval->balloonShell), x, y);
1257# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001258 gtk_widget_set_uposition(beval->balloonShell, x, y);
Bram Moolenaar98921892016-02-23 17:14:37 +01001259# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001260 gtk_widget_show(beval->balloonShell);
1261
1262 beval->showState = ShS_SHOWING;
1263 }
1264}
1265
1266/*
1267 * Undraw a balloon.
1268 */
1269 static void
1270undrawBalloon(BalloonEval *beval)
1271{
1272 if (beval->balloonShell != NULL)
1273 gtk_widget_hide(beval->balloonShell);
1274 beval->showState = ShS_NEUTRAL;
1275}
1276
1277 static void
1278cancelBalloon(BalloonEval *beval)
1279{
1280 if (beval->showState == ShS_SHOWING
1281 || beval->showState == ShS_UPDATE_PENDING)
1282 undrawBalloon(beval);
1283
1284 if (beval->timerID != 0)
1285 {
Bram Moolenaar98921892016-02-23 17:14:37 +01001286# if GTK_CHECK_VERSION(3,0,0)
1287 g_source_remove(beval->timerID);
1288# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001289 gtk_timeout_remove(beval->timerID);
Bram Moolenaar98921892016-02-23 17:14:37 +01001290# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001291 beval->timerID = 0;
1292 }
1293 beval->showState = ShS_NEUTRAL;
1294}
1295
1296 static void
1297createBalloonEvalWindow(BalloonEval *beval)
1298{
1299 beval->balloonShell = gtk_window_new(GTK_WINDOW_POPUP);
1300
1301 gtk_widget_set_app_paintable(beval->balloonShell, TRUE);
Bram Moolenaar98921892016-02-23 17:14:37 +01001302# if GTK_CHECK_VERSION(3,0,0)
1303 gtk_window_set_resizable(GTK_WINDOW(beval->balloonShell), FALSE);
1304# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001305 gtk_window_set_policy(GTK_WINDOW(beval->balloonShell), FALSE, FALSE, TRUE);
Bram Moolenaar98921892016-02-23 17:14:37 +01001306# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001307 gtk_widget_set_name(beval->balloonShell, "gtk-tooltips");
Bram Moolenaar98921892016-02-23 17:14:37 +01001308# if GTK_CHECK_VERSION(3,0,0)
1309 gtk_container_set_border_width(GTK_CONTAINER(beval->balloonShell), 4);
1310# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001311 gtk_container_border_width(GTK_CONTAINER(beval->balloonShell), 4);
Bram Moolenaar98921892016-02-23 17:14:37 +01001312# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001313
Bram Moolenaar98921892016-02-23 17:14:37 +01001314# if GTK_CHECK_VERSION(3,0,0)
1315 g_signal_connect(G_OBJECT(beval->balloonShell), "draw",
1316 G_CALLBACK(balloon_draw_event_cb), NULL);
1317# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001318 gtk_signal_connect((GtkObject*)(beval->balloonShell), "expose_event",
1319 GTK_SIGNAL_FUNC(balloon_expose_event_cb), NULL);
Bram Moolenaar98921892016-02-23 17:14:37 +01001320# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001321 beval->balloonLabel = gtk_label_new(NULL);
1322
1323 gtk_label_set_line_wrap(GTK_LABEL(beval->balloonLabel), FALSE);
1324 gtk_label_set_justify(GTK_LABEL(beval->balloonLabel), GTK_JUSTIFY_LEFT);
Bram Moolenaar98921892016-02-23 17:14:37 +01001325# if GTK_CHECK_VERSION(3,16,0)
1326 gtk_label_set_xalign(GTK_LABEL(beval->balloonLabel), 0.5);
1327 gtk_label_set_yalign(GTK_LABEL(beval->balloonLabel), 0.5);
1328# elif GTK_CHECK_VERSION(3,14,0)
1329 GValue align_val = G_VALUE_INIT;
1330 g_value_init(&align_val, G_TYPE_FLOAT);
1331 g_value_set_float(&align_val, 0.5);
1332 g_object_set_property(G_OBJECT(beval->balloonLabel), "xalign", &align_val);
1333 g_object_set_property(G_OBJECT(beval->balloonLabel), "yalign", &align_val);
1334 g_value_unset(&align_val);
1335# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001336 gtk_misc_set_alignment(GTK_MISC(beval->balloonLabel), 0.5f, 0.5f);
Bram Moolenaar98921892016-02-23 17:14:37 +01001337# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001338 gtk_widget_set_name(beval->balloonLabel, "vim-balloon-label");
1339 gtk_widget_show(beval->balloonLabel);
1340
1341 gtk_container_add(GTK_CONTAINER(beval->balloonShell), beval->balloonLabel);
1342}
1343
1344#else /* !FEAT_GUI_GTK */
1345
1346/*
1347 * Draw a balloon.
1348 */
1349 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001350drawBalloon(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001351{
1352 Dimension w;
1353 Dimension h;
1354 Position tx;
1355 Position ty;
1356
1357 if (beval->msg != NULL)
1358 {
1359 /* Show the Balloon */
1360
1361 /* Calculate the label's width and height */
1362#ifdef FEAT_GUI_MOTIF
1363 XmString s;
1364
1365 /* For the callback function we parse NL characters to create a
1366 * multi-line label. This doesn't work for all languages, but
1367 * XmStringCreateLocalized() doesn't do multi-line labels... */
1368 if (beval->msgCB != NULL)
1369 s = XmStringCreateLtoR((char *)beval->msg, XmFONTLIST_DEFAULT_TAG);
1370 else
1371 s = XmStringCreateLocalized((char *)beval->msg);
1372 {
1373 XmFontList fl;
1374
1375 fl = gui_motif_fontset2fontlist(&gui.tooltip_fontset);
Bram Moolenaardb5ffaa2014-06-25 17:44:49 +02001376 if (fl == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001377 {
Bram Moolenaardb5ffaa2014-06-25 17:44:49 +02001378 XmStringFree(s);
1379 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001380 }
Bram Moolenaardb5ffaa2014-06-25 17:44:49 +02001381 XmStringExtent(fl, s, &w, &h);
1382 XmFontListFree(fl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001383 }
1384 w += gui.border_offset << 1;
1385 h += gui.border_offset << 1;
1386 XtVaSetValues(beval->balloonLabel, XmNlabelString, s, NULL);
1387 XmStringFree(s);
1388#else /* Athena */
1389 /* Assume XtNinternational == True */
1390 XFontSet fset;
1391 XFontSetExtents *ext;
1392
1393 XtVaGetValues(beval->balloonLabel, XtNfontSet, &fset, NULL);
1394 ext = XExtentsOfFontSet(fset);
1395 h = ext->max_ink_extent.height;
1396 w = XmbTextEscapement(fset,
1397 (char *)beval->msg,
1398 (int)STRLEN(beval->msg));
1399 w += gui.border_offset << 1;
1400 h += gui.border_offset << 1;
1401 XtVaSetValues(beval->balloonLabel, XtNlabel, beval->msg, NULL);
1402#endif
1403
1404 /* Compute position of the balloon area */
1405 tx = beval->x_root + EVAL_OFFSET_X;
1406 ty = beval->y_root + EVAL_OFFSET_Y;
1407 if ((tx + w) > beval->screen_width)
1408 tx = beval->screen_width - w;
1409 if ((ty + h) > beval->screen_height)
1410 ty = beval->screen_height - h;
1411#ifdef FEAT_GUI_MOTIF
1412 XtVaSetValues(beval->balloonShell,
1413 XmNx, tx,
1414 XmNy, ty,
1415 NULL);
1416#else
1417 /* Athena */
1418 XtVaSetValues(beval->balloonShell,
1419 XtNx, tx,
1420 XtNy, ty,
1421 NULL);
1422#endif
Bram Moolenaar8281f442009-03-18 11:22:25 +00001423 /* Set tooltip colors */
1424 {
1425 Arg args[2];
1426
1427#ifdef FEAT_GUI_MOTIF
1428 args[0].name = XmNbackground;
1429 args[0].value = gui.tooltip_bg_pixel;
1430 args[1].name = XmNforeground;
1431 args[1].value = gui.tooltip_fg_pixel;
1432#else /* Athena */
1433 args[0].name = XtNbackground;
1434 args[0].value = gui.tooltip_bg_pixel;
1435 args[1].name = XtNforeground;
1436 args[1].value = gui.tooltip_fg_pixel;
1437#endif
1438 XtSetValues(beval->balloonLabel, &args[0], XtNumber(args));
1439 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001440
1441 XtPopup(beval->balloonShell, XtGrabNone);
1442
1443 beval->showState = ShS_SHOWING;
1444
1445 current_beval = beval;
1446 }
1447}
1448
1449/*
1450 * Undraw a balloon.
1451 */
1452 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001453undrawBalloon(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001454{
1455 if (beval->balloonShell != (Widget)0)
1456 XtPopdown(beval->balloonShell);
1457 beval->showState = ShS_NEUTRAL;
1458
1459 current_beval = NULL;
1460}
1461
1462 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001463cancelBalloon(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001464{
1465 if (beval->showState == ShS_SHOWING
1466 || beval->showState == ShS_UPDATE_PENDING)
1467 undrawBalloon(beval);
1468
1469 if (beval->timerID != (XtIntervalId)NULL)
1470 {
1471 XtRemoveTimeOut(beval->timerID);
1472 beval->timerID = (XtIntervalId)NULL;
1473 }
1474 beval->showState = ShS_NEUTRAL;
1475}
1476
1477
1478 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001479createBalloonEvalWindow(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001480{
1481 Arg args[12];
1482 int n;
1483
1484 n = 0;
1485#ifdef FEAT_GUI_MOTIF
1486 XtSetArg(args[n], XmNallowShellResize, True); n++;
1487 beval->balloonShell = XtAppCreateShell("balloonEval", "BalloonEval",
1488 overrideShellWidgetClass, gui.dpy, args, n);
1489#else
1490 /* Athena */
1491 XtSetArg(args[n], XtNallowShellResize, True); n++;
1492 beval->balloonShell = XtAppCreateShell("balloonEval", "BalloonEval",
1493 overrideShellWidgetClass, gui.dpy, args, n);
1494#endif
1495
1496 n = 0;
1497#ifdef FEAT_GUI_MOTIF
1498 {
1499 XmFontList fl;
1500
1501 fl = gui_motif_fontset2fontlist(&gui.tooltip_fontset);
1502 XtSetArg(args[n], XmNforeground, gui.tooltip_fg_pixel); n++;
1503 XtSetArg(args[n], XmNbackground, gui.tooltip_bg_pixel); n++;
1504 XtSetArg(args[n], XmNfontList, fl); n++;
1505 XtSetArg(args[n], XmNalignment, XmALIGNMENT_BEGINNING); n++;
1506 beval->balloonLabel = XtCreateManagedWidget("balloonLabel",
1507 xmLabelWidgetClass, beval->balloonShell, args, n);
1508 }
1509#else /* FEAT_GUI_ATHENA */
1510 XtSetArg(args[n], XtNforeground, gui.tooltip_fg_pixel); n++;
1511 XtSetArg(args[n], XtNbackground, gui.tooltip_bg_pixel); n++;
1512 XtSetArg(args[n], XtNinternational, True); n++;
1513 XtSetArg(args[n], XtNfontSet, gui.tooltip_fontset); n++;
1514 beval->balloonLabel = XtCreateManagedWidget("balloonLabel",
1515 labelWidgetClass, beval->balloonShell, args, n);
1516#endif
1517}
1518
1519#endif /* !FEAT_GUI_GTK */
1520#endif /* !FEAT_GUI_W32 */
1521
1522#endif /* FEAT_BEVAL */