blob: 4525dde7a0c391506eec327ecc18e6fb62a37219 [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
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
87 * list and it's hardly every useful. If the user really wants a
88 * 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 {
221 EMSG(_("E232: Cannot create BalloonEval with both message and callback"));
222 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 {
369 if (lt(VIsual, curwin->w_cursor))
370 {
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),
511 G_CALLBACK(target_event_cb),
512 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),
525 G_CALLBACK(mainwin_event_cb),
526 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;
1047 GdkColor color = { 0, 0, 0, 0 };
1048
1049 /* Look up the RGB values of the SpecialKey foreground color. */
1050 aep = syn_gui_attr2entry(hl_attr(HLF_8));
1051 pixel = (aep != NULL) ? aep->ae_u.gui.fg_color : INVALCOLOR;
1052 if (pixel != INVALCOLOR)
Bram Moolenaar98921892016-02-23 17:14:37 +01001053# if GTK_CHECK_VERSION(3,0,0)
1054 {
1055 GdkVisual * const visual = gtk_widget_get_visual(gui.drawarea);
1056
1057 if (visual == NULL)
1058 {
1059 color.red = 0;
1060 color.green = 0;
1061 color.blue = 0;
1062 }
1063 else
1064 {
1065 guint32 r_mask, g_mask, b_mask;
1066 gint r_shift, g_shift, b_shift;
1067
1068 gdk_visual_get_red_pixel_details(visual, &r_mask, &r_shift,
1069 NULL);
1070 gdk_visual_get_green_pixel_details(visual, &g_mask, &g_shift,
1071 NULL);
1072 gdk_visual_get_blue_pixel_details(visual, &b_mask, &b_shift,
1073 NULL);
1074
1075 color.red = ((pixel & r_mask) >> r_shift) / 255.0 * 65535;
1076 color.green = ((pixel & g_mask) >> g_shift) / 255.0 * 65535;
1077 color.blue = ((pixel & b_mask) >> b_shift) / 255.0 * 65535;
1078 }
1079 }
1080# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001081 gdk_colormap_query_color(gtk_widget_get_colormap(gui.drawarea),
1082 (unsigned long)pixel, &color);
Bram Moolenaar98921892016-02-23 17:14:37 +01001083# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001084
1085 pdest = buf;
Bram Moolenaar89d40322006-08-29 15:30:07 +00001086 p = text;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001087 while (*p != NUL)
1088 {
1089 /* Be quick for ASCII */
1090 if ((*p & 0x80) == 0 && !IS_NONPRINTABLE(*p))
1091 {
1092 *pdest++ = *p++;
1093 }
1094 else
1095 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001096 charlen = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001097 uc = utf_ptr2char(p);
1098
1099 if (charlen != utf_char2len(uc))
1100 charlen = 1; /* reject overlong sequences */
1101
1102 if (charlen == 1 || uc < 0xa0 || !utf_printable(uc))
1103 {
1104 int outlen;
1105
1106 /* Careful: we can't just use transchar_byte() here,
1107 * since 'encoding' is not necessarily set to "utf-8". */
1108 if (*p & 0x80 && charlen == 1)
1109 {
1110 transchar_hex(pdest, *p); /* <xx> */
1111 outlen = 4;
1112 }
1113 else if (uc >= 0x80)
1114 {
1115 /* Note: we assume here that utf_printable() doesn't
1116 * care about characters outside the BMP. */
1117 transchar_hex(pdest, uc); /* <xx> or <xxxx> */
1118 outlen = (uc < 0x100) ? 4 : 6;
1119 }
1120 else
1121 {
1122 transchar_nonprint(pdest, *p); /* ^X */
1123 outlen = 2;
1124 }
1125 if (pixel != INVALCOLOR)
1126 {
1127 attr = pango_attr_foreground_new(
1128 color.red, color.green, color.blue);
1129 attr->start_index = pdest - buf;
1130 attr->end_index = pdest - buf + outlen;
1131 pango_attr_list_insert(attr_list, attr);
1132 }
1133 pdest += outlen;
1134 p += charlen;
1135 }
1136 else
1137 {
1138 do
1139 *pdest++ = *p++;
1140 while (--charlen != 0);
1141 }
1142 }
1143 }
1144 *pdest = NUL;
1145 }
1146
1147 vim_free(convbuf);
1148
1149 gtk_label_set_text(label, (const char *)buf);
1150 vim_free(buf);
1151
1152 gtk_label_set_attributes(label, attr_list);
1153 pango_attr_list_unref(attr_list);
1154}
Bram Moolenaar182c5be2010-06-25 05:37:59 +02001155# undef IS_NONPRINTABLE
Bram Moolenaar071d4272004-06-13 20:20:40 +00001156
1157/*
1158 * Draw a balloon.
1159 */
1160 static void
1161drawBalloon(BalloonEval *beval)
1162{
1163 if (beval->msg != NULL)
1164 {
1165 GtkRequisition requisition;
1166 int screen_w;
1167 int screen_h;
1168 int x;
1169 int y;
1170 int x_offset = EVAL_OFFSET_X;
1171 int y_offset = EVAL_OFFSET_Y;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001172 PangoLayout *layout;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001173# ifdef HAVE_GTK_MULTIHEAD
1174 GdkScreen *screen;
1175
1176 screen = gtk_widget_get_screen(beval->target);
1177 gtk_window_set_screen(GTK_WINDOW(beval->balloonShell), screen);
1178 screen_w = gdk_screen_get_width(screen);
1179 screen_h = gdk_screen_get_height(screen);
1180# else
1181 screen_w = gdk_screen_width();
1182 screen_h = gdk_screen_height();
1183# endif
Bram Moolenaar98921892016-02-23 17:14:37 +01001184# if !GTK_CHECK_VERSION(3,0,0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001185 gtk_widget_ensure_style(beval->balloonShell);
1186 gtk_widget_ensure_style(beval->balloonLabel);
Bram Moolenaar98921892016-02-23 17:14:37 +01001187# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001188
Bram Moolenaar071d4272004-06-13 20:20:40 +00001189 set_printable_label_text(GTK_LABEL(beval->balloonLabel), beval->msg);
1190 /*
1191 * Dirty trick: Enable wrapping mode on the label's layout behind its
1192 * back. This way GtkLabel won't try to constrain the wrap width to a
1193 * builtin maximum value of about 65 Latin characters.
1194 */
1195 layout = gtk_label_get_layout(GTK_LABEL(beval->balloonLabel));
Bram Moolenaar182c5be2010-06-25 05:37:59 +02001196# ifdef PANGO_WRAP_WORD_CHAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00001197 pango_layout_set_wrap(layout, PANGO_WRAP_WORD_CHAR);
Bram Moolenaar182c5be2010-06-25 05:37:59 +02001198# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001199 pango_layout_set_wrap(layout, PANGO_WRAP_WORD);
Bram Moolenaar182c5be2010-06-25 05:37:59 +02001200# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001201 pango_layout_set_width(layout,
1202 /* try to come up with some reasonable width */
1203 PANGO_SCALE * CLAMP(gui.num_cols * gui.char_width,
1204 screen_w / 2,
1205 MAX(20, screen_w - 20)));
1206
1207 /* Calculate the balloon's width and height. */
Bram Moolenaar98921892016-02-23 17:14:37 +01001208# if GTK_CHECK_VERSION(3,0,0)
1209 gtk_widget_get_preferred_size(beval->balloonShell, &requisition, NULL);
1210# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001211 gtk_widget_size_request(beval->balloonShell, &requisition);
Bram Moolenaar98921892016-02-23 17:14:37 +01001212# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001213
1214 /* Compute position of the balloon area */
Bram Moolenaar98921892016-02-23 17:14:37 +01001215# if GTK_CHECK_VERSION(3,0,0)
1216 gdk_window_get_origin(gtk_widget_get_window(beval->target), &x, &y);
1217# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001218 gdk_window_get_origin(beval->target->window, &x, &y);
Bram Moolenaar98921892016-02-23 17:14:37 +01001219# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001220 x += beval->x;
1221 y += beval->y;
1222
1223 /* Get out of the way of the mouse pointer */
1224 if (x + x_offset + requisition.width > screen_w)
1225 y_offset += 15;
1226 if (y + y_offset + requisition.height > screen_h)
1227 y_offset = -requisition.height - EVAL_OFFSET_Y;
1228
1229 /* Sanitize values */
1230 x = CLAMP(x + x_offset, 0, MAX(0, screen_w - requisition.width));
1231 y = CLAMP(y + y_offset, 0, MAX(0, screen_h - requisition.height));
1232
1233 /* Show the balloon */
Bram Moolenaar98921892016-02-23 17:14:37 +01001234# if GTK_CHECK_VERSION(3,0,0)
1235 gtk_window_move(GTK_WINDOW(beval->balloonShell), x, y);
1236# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001237 gtk_widget_set_uposition(beval->balloonShell, x, y);
Bram Moolenaar98921892016-02-23 17:14:37 +01001238# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001239 gtk_widget_show(beval->balloonShell);
1240
1241 beval->showState = ShS_SHOWING;
1242 }
1243}
1244
1245/*
1246 * Undraw a balloon.
1247 */
1248 static void
1249undrawBalloon(BalloonEval *beval)
1250{
1251 if (beval->balloonShell != NULL)
1252 gtk_widget_hide(beval->balloonShell);
1253 beval->showState = ShS_NEUTRAL;
1254}
1255
1256 static void
1257cancelBalloon(BalloonEval *beval)
1258{
1259 if (beval->showState == ShS_SHOWING
1260 || beval->showState == ShS_UPDATE_PENDING)
1261 undrawBalloon(beval);
1262
1263 if (beval->timerID != 0)
1264 {
Bram Moolenaar98921892016-02-23 17:14:37 +01001265# if GTK_CHECK_VERSION(3,0,0)
1266 g_source_remove(beval->timerID);
1267# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001268 gtk_timeout_remove(beval->timerID);
Bram Moolenaar98921892016-02-23 17:14:37 +01001269# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001270 beval->timerID = 0;
1271 }
1272 beval->showState = ShS_NEUTRAL;
1273}
1274
1275 static void
1276createBalloonEvalWindow(BalloonEval *beval)
1277{
1278 beval->balloonShell = gtk_window_new(GTK_WINDOW_POPUP);
1279
1280 gtk_widget_set_app_paintable(beval->balloonShell, TRUE);
Bram Moolenaar98921892016-02-23 17:14:37 +01001281# if GTK_CHECK_VERSION(3,0,0)
1282 gtk_window_set_resizable(GTK_WINDOW(beval->balloonShell), FALSE);
1283# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001284 gtk_window_set_policy(GTK_WINDOW(beval->balloonShell), FALSE, FALSE, TRUE);
Bram Moolenaar98921892016-02-23 17:14:37 +01001285# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001286 gtk_widget_set_name(beval->balloonShell, "gtk-tooltips");
Bram Moolenaar98921892016-02-23 17:14:37 +01001287# if GTK_CHECK_VERSION(3,0,0)
1288 gtk_container_set_border_width(GTK_CONTAINER(beval->balloonShell), 4);
1289# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001290 gtk_container_border_width(GTK_CONTAINER(beval->balloonShell), 4);
Bram Moolenaar98921892016-02-23 17:14:37 +01001291# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001292
Bram Moolenaar98921892016-02-23 17:14:37 +01001293# if GTK_CHECK_VERSION(3,0,0)
1294 g_signal_connect(G_OBJECT(beval->balloonShell), "draw",
1295 G_CALLBACK(balloon_draw_event_cb), NULL);
1296# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001297 gtk_signal_connect((GtkObject*)(beval->balloonShell), "expose_event",
1298 GTK_SIGNAL_FUNC(balloon_expose_event_cb), NULL);
Bram Moolenaar98921892016-02-23 17:14:37 +01001299# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001300 beval->balloonLabel = gtk_label_new(NULL);
1301
1302 gtk_label_set_line_wrap(GTK_LABEL(beval->balloonLabel), FALSE);
1303 gtk_label_set_justify(GTK_LABEL(beval->balloonLabel), GTK_JUSTIFY_LEFT);
Bram Moolenaar98921892016-02-23 17:14:37 +01001304# if GTK_CHECK_VERSION(3,16,0)
1305 gtk_label_set_xalign(GTK_LABEL(beval->balloonLabel), 0.5);
1306 gtk_label_set_yalign(GTK_LABEL(beval->balloonLabel), 0.5);
1307# elif GTK_CHECK_VERSION(3,14,0)
1308 GValue align_val = G_VALUE_INIT;
1309 g_value_init(&align_val, G_TYPE_FLOAT);
1310 g_value_set_float(&align_val, 0.5);
1311 g_object_set_property(G_OBJECT(beval->balloonLabel), "xalign", &align_val);
1312 g_object_set_property(G_OBJECT(beval->balloonLabel), "yalign", &align_val);
1313 g_value_unset(&align_val);
1314# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001315 gtk_misc_set_alignment(GTK_MISC(beval->balloonLabel), 0.5f, 0.5f);
Bram Moolenaar98921892016-02-23 17:14:37 +01001316# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001317 gtk_widget_set_name(beval->balloonLabel, "vim-balloon-label");
1318 gtk_widget_show(beval->balloonLabel);
1319
1320 gtk_container_add(GTK_CONTAINER(beval->balloonShell), beval->balloonLabel);
1321}
1322
1323#else /* !FEAT_GUI_GTK */
1324
1325/*
1326 * Draw a balloon.
1327 */
1328 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001329drawBalloon(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001330{
1331 Dimension w;
1332 Dimension h;
1333 Position tx;
1334 Position ty;
1335
1336 if (beval->msg != NULL)
1337 {
1338 /* Show the Balloon */
1339
1340 /* Calculate the label's width and height */
1341#ifdef FEAT_GUI_MOTIF
1342 XmString s;
1343
1344 /* For the callback function we parse NL characters to create a
1345 * multi-line label. This doesn't work for all languages, but
1346 * XmStringCreateLocalized() doesn't do multi-line labels... */
1347 if (beval->msgCB != NULL)
1348 s = XmStringCreateLtoR((char *)beval->msg, XmFONTLIST_DEFAULT_TAG);
1349 else
1350 s = XmStringCreateLocalized((char *)beval->msg);
1351 {
1352 XmFontList fl;
1353
1354 fl = gui_motif_fontset2fontlist(&gui.tooltip_fontset);
Bram Moolenaardb5ffaa2014-06-25 17:44:49 +02001355 if (fl == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001356 {
Bram Moolenaardb5ffaa2014-06-25 17:44:49 +02001357 XmStringFree(s);
1358 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001359 }
Bram Moolenaardb5ffaa2014-06-25 17:44:49 +02001360 XmStringExtent(fl, s, &w, &h);
1361 XmFontListFree(fl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001362 }
1363 w += gui.border_offset << 1;
1364 h += gui.border_offset << 1;
1365 XtVaSetValues(beval->balloonLabel, XmNlabelString, s, NULL);
1366 XmStringFree(s);
1367#else /* Athena */
1368 /* Assume XtNinternational == True */
1369 XFontSet fset;
1370 XFontSetExtents *ext;
1371
1372 XtVaGetValues(beval->balloonLabel, XtNfontSet, &fset, NULL);
1373 ext = XExtentsOfFontSet(fset);
1374 h = ext->max_ink_extent.height;
1375 w = XmbTextEscapement(fset,
1376 (char *)beval->msg,
1377 (int)STRLEN(beval->msg));
1378 w += gui.border_offset << 1;
1379 h += gui.border_offset << 1;
1380 XtVaSetValues(beval->balloonLabel, XtNlabel, beval->msg, NULL);
1381#endif
1382
1383 /* Compute position of the balloon area */
1384 tx = beval->x_root + EVAL_OFFSET_X;
1385 ty = beval->y_root + EVAL_OFFSET_Y;
1386 if ((tx + w) > beval->screen_width)
1387 tx = beval->screen_width - w;
1388 if ((ty + h) > beval->screen_height)
1389 ty = beval->screen_height - h;
1390#ifdef FEAT_GUI_MOTIF
1391 XtVaSetValues(beval->balloonShell,
1392 XmNx, tx,
1393 XmNy, ty,
1394 NULL);
1395#else
1396 /* Athena */
1397 XtVaSetValues(beval->balloonShell,
1398 XtNx, tx,
1399 XtNy, ty,
1400 NULL);
1401#endif
Bram Moolenaar8281f442009-03-18 11:22:25 +00001402 /* Set tooltip colors */
1403 {
1404 Arg args[2];
1405
1406#ifdef FEAT_GUI_MOTIF
1407 args[0].name = XmNbackground;
1408 args[0].value = gui.tooltip_bg_pixel;
1409 args[1].name = XmNforeground;
1410 args[1].value = gui.tooltip_fg_pixel;
1411#else /* Athena */
1412 args[0].name = XtNbackground;
1413 args[0].value = gui.tooltip_bg_pixel;
1414 args[1].name = XtNforeground;
1415 args[1].value = gui.tooltip_fg_pixel;
1416#endif
1417 XtSetValues(beval->balloonLabel, &args[0], XtNumber(args));
1418 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001419
1420 XtPopup(beval->balloonShell, XtGrabNone);
1421
1422 beval->showState = ShS_SHOWING;
1423
1424 current_beval = beval;
1425 }
1426}
1427
1428/*
1429 * Undraw a balloon.
1430 */
1431 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001432undrawBalloon(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001433{
1434 if (beval->balloonShell != (Widget)0)
1435 XtPopdown(beval->balloonShell);
1436 beval->showState = ShS_NEUTRAL;
1437
1438 current_beval = NULL;
1439}
1440
1441 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001442cancelBalloon(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001443{
1444 if (beval->showState == ShS_SHOWING
1445 || beval->showState == ShS_UPDATE_PENDING)
1446 undrawBalloon(beval);
1447
1448 if (beval->timerID != (XtIntervalId)NULL)
1449 {
1450 XtRemoveTimeOut(beval->timerID);
1451 beval->timerID = (XtIntervalId)NULL;
1452 }
1453 beval->showState = ShS_NEUTRAL;
1454}
1455
1456
1457 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001458createBalloonEvalWindow(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001459{
1460 Arg args[12];
1461 int n;
1462
1463 n = 0;
1464#ifdef FEAT_GUI_MOTIF
1465 XtSetArg(args[n], XmNallowShellResize, True); n++;
1466 beval->balloonShell = XtAppCreateShell("balloonEval", "BalloonEval",
1467 overrideShellWidgetClass, gui.dpy, args, n);
1468#else
1469 /* Athena */
1470 XtSetArg(args[n], XtNallowShellResize, True); n++;
1471 beval->balloonShell = XtAppCreateShell("balloonEval", "BalloonEval",
1472 overrideShellWidgetClass, gui.dpy, args, n);
1473#endif
1474
1475 n = 0;
1476#ifdef FEAT_GUI_MOTIF
1477 {
1478 XmFontList fl;
1479
1480 fl = gui_motif_fontset2fontlist(&gui.tooltip_fontset);
1481 XtSetArg(args[n], XmNforeground, gui.tooltip_fg_pixel); n++;
1482 XtSetArg(args[n], XmNbackground, gui.tooltip_bg_pixel); n++;
1483 XtSetArg(args[n], XmNfontList, fl); n++;
1484 XtSetArg(args[n], XmNalignment, XmALIGNMENT_BEGINNING); n++;
1485 beval->balloonLabel = XtCreateManagedWidget("balloonLabel",
1486 xmLabelWidgetClass, beval->balloonShell, args, n);
1487 }
1488#else /* FEAT_GUI_ATHENA */
1489 XtSetArg(args[n], XtNforeground, gui.tooltip_fg_pixel); n++;
1490 XtSetArg(args[n], XtNbackground, gui.tooltip_bg_pixel); n++;
1491 XtSetArg(args[n], XtNinternational, True); n++;
1492 XtSetArg(args[n], XtNfontSet, gui.tooltip_fontset); n++;
1493 beval->balloonLabel = XtCreateManagedWidget("balloonLabel",
1494 labelWidgetClass, beval->balloonShell, args, n);
1495#endif
1496}
1497
1498#endif /* !FEAT_GUI_GTK */
1499#endif /* !FEAT_GUI_W32 */
1500
1501#endif /* FEAT_BEVAL */