blob: 9a945bc8ae0192b409e960a82f07628f36c6e1ff [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);
64 set_vim_var_nr(VV_BEVAL_LNUM, (long)lnum);
65 set_vim_var_nr(VV_BEVAL_COL, (long)(col + 1));
66 set_vim_var_string(VV_BEVAL_TEXT, text, -1);
67 vim_free(text);
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000068
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000069 /*
70 * Temporarily change the curbuf, so that we can determine whether
Bram Moolenaarbae0c162007-05-10 19:30:25 +000071 * the buffer-local balloonexpr option was set insecurely.
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000072 */
73 save_curbuf = curbuf;
74 curbuf = wp->w_buffer;
75 use_sandbox = was_set_insecurely((char_u *)"balloonexpr",
76 *curbuf->b_p_bexpr == NUL ? 0 : OPT_LOCAL);
77 curbuf = save_curbuf;
78 if (use_sandbox)
79 ++sandbox;
80 ++textlock;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000081
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000082 vim_free(result);
83 result = eval_to_string(bexpr, NULL, TRUE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000084
Bram Moolenaarf1b46222014-10-21 14:15:17 +020085 /* Remove one trailing newline, it is added when the result was a
86 * list and it's hardly every useful. If the user really wants a
87 * trailing newline he can add two and one remains. */
88 if (result != NULL)
89 {
90 len = STRLEN(result);
91 if (len > 0 && result[len - 1] == NL)
92 result[len - 1] = NUL;
93 }
94
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000095 if (use_sandbox)
96 --sandbox;
97 --textlock;
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000098
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000099 set_vim_var_string(VV_BEVAL_TEXT, NULL, -1);
100 if (result != NULL && result[0] != NUL)
101 {
102 gui_mch_post_balloon(beval, result);
Bram Moolenaar54a709e2006-05-04 21:57:11 +0000103 recursive = FALSE;
Bram Moolenaarb3656ed2006-03-20 21:59:49 +0000104 return;
105 }
Bram Moolenaare2ac10d2005-03-07 23:26:06 +0000106 }
107 }
108#endif
109#ifdef FEAT_NETBEANS_INTG
110 if (bevalServers & BEVAL_NETBEANS)
111 netbeans_beval_cb(beval, state);
112#endif
113#ifdef FEAT_SUN_WORKSHOP
114 if (bevalServers & BEVAL_WORKSHOP)
115 workshop_beval_cb(beval, state);
116#endif
Bram Moolenaar54a709e2006-05-04 21:57:11 +0000117
118 recursive = FALSE;
Bram Moolenaare2ac10d2005-03-07 23:26:06 +0000119}
120
121/* on Win32 only get_beval_info() is required */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000122#if !defined(FEAT_GUI_W32) || defined(PROTO)
123
124#ifdef FEAT_GUI_GTK
Bram Moolenaar98921892016-02-23 17:14:37 +0100125# if GTK_CHECK_VERSION(3,0,0)
126# include <gdk/gdkkeysyms-compat.h>
127# else
128# include <gdk/gdkkeysyms.h>
129# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000130# include <gtk/gtk.h>
131#else
132# include <X11/keysym.h>
133# ifdef FEAT_GUI_MOTIF
134# include <Xm/PushB.h>
135# include <Xm/Separator.h>
136# include <Xm/List.h>
137# include <Xm/Label.h>
138# include <Xm/AtomMgr.h>
139# include <Xm/Protocols.h>
140# else
141 /* Assume Athena */
142# include <X11/Shell.h>
Bram Moolenaar238a5642006-02-21 22:12:05 +0000143# ifdef FEAT_GUI_NEXTAW
144# include <X11/neXtaw/Label.h>
145# else
146# include <X11/Xaw/Label.h>
147# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000148# endif
149#endif
150
151#include "gui_beval.h"
152
153#ifndef FEAT_GUI_GTK
154extern Widget vimShell;
155
156/*
157 * Currently, we assume that there can be only one BalloonEval showing
158 * on-screen at any given moment. This variable will hold the currently
159 * showing BalloonEval or NULL if none is showing.
160 */
161static BalloonEval *current_beval = NULL;
162#endif
163
164#ifdef FEAT_GUI_GTK
Bram Moolenaard25c16e2016-01-29 22:13:30 +0100165static void addEventHandler(GtkWidget *, BalloonEval *);
166static void removeEventHandler(BalloonEval *);
167static gint target_event_cb(GtkWidget *, GdkEvent *, gpointer);
168static gint mainwin_event_cb(GtkWidget *, GdkEvent *, gpointer);
169static void pointer_event(BalloonEval *, int, int, unsigned);
170static void key_event(BalloonEval *, unsigned, int);
Bram Moolenaar98921892016-02-23 17:14:37 +0100171# if GTK_CHECK_VERSION(3,0,0)
172static gboolean timeout_cb(gpointer);
173# else
Bram Moolenaard25c16e2016-01-29 22:13:30 +0100174static gint timeout_cb(gpointer);
Bram Moolenaar98921892016-02-23 17:14:37 +0100175# endif
176# if GTK_CHECK_VERSION(3,0,0)
177static gboolean balloon_draw_event_cb (GtkWidget *, cairo_t *, gpointer);
178# else
179static gint balloon_expose_event_cb (GtkWidget *, GdkEventExpose *, gpointer);
180# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000181#else
Bram Moolenaard25c16e2016-01-29 22:13:30 +0100182static void addEventHandler(Widget, BalloonEval *);
183static void removeEventHandler(BalloonEval *);
184static void pointerEventEH(Widget, XtPointer, XEvent *, Boolean *);
185static void pointerEvent(BalloonEval *, XEvent *);
186static void timerRoutine(XtPointer, XtIntervalId *);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000187#endif
Bram Moolenaard25c16e2016-01-29 22:13:30 +0100188static void cancelBalloon(BalloonEval *);
189static void requestBalloon(BalloonEval *);
190static void drawBalloon(BalloonEval *);
191static void undrawBalloon(BalloonEval *beval);
192static void createBalloonEvalWindow(BalloonEval *);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000193
194
195
196/*
197 * Create a balloon-evaluation area for a Widget.
198 * There can be either a "mesg" for a fixed string or "mesgCB" to generate a
199 * message by calling this callback function.
200 * When "mesg" is not NULL it must remain valid for as long as the balloon is
201 * used. It is not freed here.
202 * Returns a pointer to the resulting object (NULL when out of memory).
203 */
204 BalloonEval *
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100205gui_mch_create_beval_area(
206 void *target,
207 char_u *mesg,
208 void (*mesgCB)(BalloonEval *, int),
209 void *clientData)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000210{
211#ifndef FEAT_GUI_GTK
212 char *display_name; /* get from gui.dpy */
213 int screen_num;
214 char *p;
215#endif
216 BalloonEval *beval;
217
218 if (mesg != NULL && mesgCB != NULL)
219 {
220 EMSG(_("E232: Cannot create BalloonEval with both message and callback"));
221 return NULL;
222 }
223
224 beval = (BalloonEval *)alloc(sizeof(BalloonEval));
225 if (beval != NULL)
226 {
227#ifdef FEAT_GUI_GTK
228 beval->target = GTK_WIDGET(target);
229 beval->balloonShell = NULL;
230 beval->timerID = 0;
231#else
232 beval->target = (Widget)target;
233 beval->balloonShell = NULL;
234 beval->timerID = (XtIntervalId)NULL;
235 beval->appContext = XtWidgetToApplicationContext((Widget)target);
236#endif
237 beval->showState = ShS_NEUTRAL;
238 beval->x = 0;
239 beval->y = 0;
240 beval->msg = mesg;
241 beval->msgCB = mesgCB;
242 beval->clientData = clientData;
243
244 /*
245 * Set up event handler which will keep its eyes on the pointer,
246 * and when the pointer rests in a certain spot for a given time
247 * interval, show the beval.
248 */
249 addEventHandler(beval->target, beval);
250 createBalloonEvalWindow(beval);
251
252#ifndef FEAT_GUI_GTK
253 /*
254 * Now create and save the screen width and height. Used in drawing.
255 */
256 display_name = DisplayString(gui.dpy);
257 p = strrchr(display_name, '.');
258 if (p != NULL)
259 screen_num = atoi(++p);
260 else
261 screen_num = 0;
262 beval->screen_width = DisplayWidth(gui.dpy, screen_num);
263 beval->screen_height = DisplayHeight(gui.dpy, screen_num);
264#endif
265 }
266
267 return beval;
268}
269
270#if defined(FEAT_BEVAL_TIP) || defined(PROTO)
271/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +0000272 * Destroy a balloon-eval and free its associated memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000273 */
274 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100275gui_mch_destroy_beval_area(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000276{
277 cancelBalloon(beval);
278 removeEventHandler(beval);
279 /* Children will automatically be destroyed */
280# ifdef FEAT_GUI_GTK
281 gtk_widget_destroy(beval->balloonShell);
282# else
283 XtDestroyWidget(beval->balloonShell);
284# endif
285 vim_free(beval);
286}
287#endif
288
289 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100290gui_mch_enable_beval_area(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000291{
292 if (beval != NULL)
293 addEventHandler(beval->target, beval);
294}
295
296 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100297gui_mch_disable_beval_area(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000298{
299 if (beval != NULL)
300 removeEventHandler(beval);
301}
302
303#if defined(FEAT_BEVAL_TIP) || defined(PROTO)
304/*
305 * This function returns the BalloonEval * associated with the currently
306 * displayed tooltip. Returns NULL if there is no tooltip currently showing.
307 *
308 * Assumption: Only one tooltip can be shown at a time.
309 */
310 BalloonEval *
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100311gui_mch_currently_showing_beval(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000312{
313 return current_beval;
314}
315#endif
316#endif /* !FEAT_GUI_W32 */
317
Bram Moolenaare2ac10d2005-03-07 23:26:06 +0000318#if defined(FEAT_SUN_WORKSHOP) || defined(FEAT_NETBEANS_INTG) \
319 || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000320/*
321 * Get the text and position to be evaluated for "beval".
Bram Moolenaare2ac10d2005-03-07 23:26:06 +0000322 * If "getword" is true the returned text is not the whole line but the
323 * relevant word in allocated memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000324 * Returns OK or FAIL.
325 */
326 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100327get_beval_info(
328 BalloonEval *beval,
329 int getword,
330 win_T **winp,
331 linenr_T *lnump,
332 char_u **textp,
333 int *colp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000334{
335 win_T *wp;
336 int row, col;
337 char_u *lbuf;
338 linenr_T lnum;
339
Bram Moolenaare2ac10d2005-03-07 23:26:06 +0000340 *textp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000341 row = Y_2_ROW(beval->y);
342 col = X_2_COL(beval->x);
Bram Moolenaar33aec762006-01-22 23:30:12 +0000343#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +0000344 wp = mouse_find_win(&row, &col);
Bram Moolenaar33aec762006-01-22 23:30:12 +0000345#else
346 wp = firstwin;
347#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000348 if (wp != NULL && row < wp->w_height && col < W_WIDTH(wp))
349 {
350 /* Found a window and the cursor is in the text. Now find the line
351 * number. */
352 if (!mouse_comp_pos(wp, &row, &col, &lnum))
353 {
354 /* Not past end of the file. */
355 lbuf = ml_get_buf(wp->w_buffer, lnum, FALSE);
356 if (col <= win_linetabsize(wp, lbuf, (colnr_T)MAXCOL))
357 {
358 /* Not past end of line. */
Bram Moolenaare2ac10d2005-03-07 23:26:06 +0000359 if (getword)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000360 {
361 /* For Netbeans we get the relevant part of the line
362 * instead of the whole line. */
363 int len;
364 pos_T *spos = NULL, *epos = NULL;
365
366 if (VIsual_active)
367 {
368 if (lt(VIsual, curwin->w_cursor))
369 {
370 spos = &VIsual;
371 epos = &curwin->w_cursor;
372 }
373 else
374 {
375 spos = &curwin->w_cursor;
376 epos = &VIsual;
377 }
378 }
379
Bram Moolenaarb6101cf2012-10-21 00:58:39 +0200380 col = vcol2col(wp, lnum, col);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000381
382 if (VIsual_active
383 && wp->w_buffer == curwin->w_buffer
384 && (lnum == spos->lnum
385 ? col >= (int)spos->col
386 : lnum > spos->lnum)
387 && (lnum == epos->lnum
388 ? col <= (int)epos->col
389 : lnum < epos->lnum))
390 {
391 /* Visual mode and pointing to the line with the
392 * Visual selection: return selected text, with a
393 * maximum of one line. */
394 if (spos->lnum != epos->lnum || spos->col == epos->col)
395 return FAIL;
396
397 lbuf = ml_get_buf(curwin->w_buffer, VIsual.lnum, FALSE);
Bram Moolenaarb6101cf2012-10-21 00:58:39 +0200398 len = epos->col - spos->col;
399 if (*p_sel != 'e')
400 len += MB_PTR2LEN(lbuf + epos->col);
401 lbuf = vim_strnsave(lbuf + spos->col, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000402 lnum = spos->lnum;
403 col = spos->col;
404 }
405 else
406 {
407 /* Find the word under the cursor. */
408 ++emsg_off;
409 len = find_ident_at_pos(wp, lnum, (colnr_T)col, &lbuf,
410 FIND_IDENT + FIND_STRING + FIND_EVAL);
411 --emsg_off;
412 if (len == 0)
413 return FAIL;
414 lbuf = vim_strnsave(lbuf, len);
415 }
416 }
Bram Moolenaare2ac10d2005-03-07 23:26:06 +0000417
418 *winp = wp;
419 *lnump = lnum;
420 *textp = lbuf;
421 *colp = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000422 beval->ts = wp->w_buffer->b_p_ts;
423 return OK;
424 }
425 }
426 }
427
428 return FAIL;
429}
430
431# if !defined(FEAT_GUI_W32) || defined(PROTO)
432
433/*
434 * Show a balloon with "mesg".
435 */
436 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100437gui_mch_post_balloon(BalloonEval *beval, char_u *mesg)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000438{
439 beval->msg = mesg;
440 if (mesg != NULL)
441 drawBalloon(beval);
442 else
443 undrawBalloon(beval);
444}
445# endif /* FEAT_GUI_W32 */
446#endif /* FEAT_SUN_WORKSHOP || FEAT_NETBEANS_INTG || PROTO */
447
448#if !defined(FEAT_GUI_W32) || defined(PROTO)
449#if defined(FEAT_BEVAL_TIP) || defined(PROTO)
450/*
451 * Hide the given balloon.
452 */
453 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100454gui_mch_unpost_balloon(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000455{
456 undrawBalloon(beval);
457}
458#endif
459
460#ifdef FEAT_GUI_GTK
461/*
462 * We can unconditionally use ANSI-style prototypes here since
463 * GTK+ requires an ANSI C compiler anyway.
464 */
465 static void
466addEventHandler(GtkWidget *target, BalloonEval *beval)
467{
468 /*
469 * Connect to the generic "event" signal instead of the individual
470 * signals for each event type, because the former is emitted earlier.
471 * This allows us to catch events independently of the signal handlers
472 * in gui_gtk_x11.c.
473 */
Bram Moolenaar98921892016-02-23 17:14:37 +0100474# if GTK_CHECK_VERSION(3,0,0)
475 g_signal_connect(G_OBJECT(target), "event",
476 G_CALLBACK(target_event_cb),
477 beval);
478# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000479 /* Should use GTK_OBJECT() here, but that causes a lint warning... */
480 gtk_signal_connect((GtkObject*)(target), "event",
481 GTK_SIGNAL_FUNC(target_event_cb),
482 beval);
Bram Moolenaar98921892016-02-23 17:14:37 +0100483# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000484 /*
485 * Nasty: Key press events go to the main window thus the drawing area
486 * will never see them. This means we have to connect to the main window
487 * as well in order to catch those events.
488 */
489 if (gtk_socket_id == 0 && gui.mainwin != NULL
490 && gtk_widget_is_ancestor(target, gui.mainwin))
491 {
Bram Moolenaar98921892016-02-23 17:14:37 +0100492# if GTK_CHECK_VERSION(3,0,0)
493 g_signal_connect(G_OBJECT(gui.mainwin), "event",
494 G_CALLBACK(mainwin_event_cb),
495 beval);
496# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000497 gtk_signal_connect((GtkObject*)(gui.mainwin), "event",
498 GTK_SIGNAL_FUNC(mainwin_event_cb),
499 beval);
Bram Moolenaar98921892016-02-23 17:14:37 +0100500# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000501 }
502}
503
504 static void
505removeEventHandler(BalloonEval *beval)
506{
Bram Moolenaar33570922005-01-25 22:26:29 +0000507 /* LINTED: avoid warning: dubious operation on enum */
Bram Moolenaar98921892016-02-23 17:14:37 +0100508# if GTK_CHECK_VERSION(3,0,0)
509 g_signal_handlers_disconnect_by_func(G_OBJECT(beval->target),
510 G_CALLBACK(target_event_cb),
511 beval);
512# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000513 gtk_signal_disconnect_by_func((GtkObject*)(beval->target),
514 GTK_SIGNAL_FUNC(target_event_cb),
515 beval);
Bram Moolenaar98921892016-02-23 17:14:37 +0100516# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000517
518 if (gtk_socket_id == 0 && gui.mainwin != NULL
519 && gtk_widget_is_ancestor(beval->target, gui.mainwin))
520 {
Bram Moolenaar33570922005-01-25 22:26:29 +0000521 /* LINTED: avoid warning: dubious operation on enum */
Bram Moolenaar98921892016-02-23 17:14:37 +0100522# if GTK_CHECK_VERSION(3,0,0)
523 g_signal_handlers_disconnect_by_func(G_OBJECT(gui.mainwin),
524 G_CALLBACK(mainwin_event_cb),
525 beval);
526# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000527 gtk_signal_disconnect_by_func((GtkObject*)(gui.mainwin),
528 GTK_SIGNAL_FUNC(mainwin_event_cb),
529 beval);
Bram Moolenaar98921892016-02-23 17:14:37 +0100530# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000531 }
532}
533
534 static gint
535target_event_cb(GtkWidget *widget, GdkEvent *event, gpointer data)
536{
537 BalloonEval *beval = (BalloonEval *)data;
538
539 switch (event->type)
540 {
541 case GDK_ENTER_NOTIFY:
542 pointer_event(beval, (int)event->crossing.x,
543 (int)event->crossing.y,
544 event->crossing.state);
545 break;
546 case GDK_MOTION_NOTIFY:
547 if (event->motion.is_hint)
548 {
549 int x;
550 int y;
551 GdkModifierType state;
552 /*
553 * GDK_POINTER_MOTION_HINT_MASK is set, thus we cannot obtain
554 * the coordinates from the GdkEventMotion struct directly.
555 */
Bram Moolenaar98921892016-02-23 17:14:37 +0100556# if GTK_CHECK_VERSION(3,0,0)
557 {
558 GdkWindow * const win = gtk_widget_get_window(widget);
559 GdkDisplay * const dpy = gdk_window_get_display(win);
Bram Moolenaar30e12d22016-04-17 20:49:53 +0200560# if GTK_CHECK_VERSION(3,20,0)
561 GdkSeat * const seat = gdk_display_get_default_seat(dpy);
562 GdkDevice * const dev = gdk_seat_get_pointer(seat);
563# else
Bram Moolenaar98921892016-02-23 17:14:37 +0100564 GdkDeviceManager * const mngr = gdk_display_get_device_manager(dpy);
565 GdkDevice * const dev = gdk_device_manager_get_client_pointer(mngr);
Bram Moolenaar30e12d22016-04-17 20:49:53 +0200566# endif
Bram Moolenaar98921892016-02-23 17:14:37 +0100567 gdk_window_get_device_position(win, dev , &x, &y, &state);
568 }
569# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000570 gdk_window_get_pointer(widget->window, &x, &y, &state);
Bram Moolenaar98921892016-02-23 17:14:37 +0100571# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000572 pointer_event(beval, x, y, (unsigned int)state);
573 }
574 else
575 {
576 pointer_event(beval, (int)event->motion.x,
577 (int)event->motion.y,
578 event->motion.state);
579 }
580 break;
581 case GDK_LEAVE_NOTIFY:
582 /*
583 * Ignore LeaveNotify events that are not "normal".
584 * Apparently we also get it when somebody else grabs focus.
585 */
586 if (event->crossing.mode == GDK_CROSSING_NORMAL)
587 cancelBalloon(beval);
588 break;
589 case GDK_BUTTON_PRESS:
Bram Moolenaar071d4272004-06-13 20:20:40 +0000590 case GDK_SCROLL:
Bram Moolenaar071d4272004-06-13 20:20:40 +0000591 cancelBalloon(beval);
592 break;
593 case GDK_KEY_PRESS:
594 key_event(beval, event->key.keyval, TRUE);
595 break;
596 case GDK_KEY_RELEASE:
597 key_event(beval, event->key.keyval, FALSE);
598 break;
599 default:
600 break;
601 }
602
603 return FALSE; /* continue emission */
604}
605
Bram Moolenaar071d4272004-06-13 20:20:40 +0000606 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +0000607mainwin_event_cb(GtkWidget *widget UNUSED, GdkEvent *event, gpointer data)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000608{
609 BalloonEval *beval = (BalloonEval *)data;
610
611 switch (event->type)
612 {
613 case GDK_KEY_PRESS:
614 key_event(beval, event->key.keyval, TRUE);
615 break;
616 case GDK_KEY_RELEASE:
617 key_event(beval, event->key.keyval, FALSE);
618 break;
619 default:
620 break;
621 }
622
623 return FALSE; /* continue emission */
624}
625
626 static void
627pointer_event(BalloonEval *beval, int x, int y, unsigned state)
628{
629 int distance;
630
631 distance = ABS(x - beval->x) + ABS(y - beval->y);
632
633 if (distance > 4)
634 {
635 /*
636 * Moved out of the balloon location: cancel it.
637 * Remember button state
638 */
639 beval->state = state;
640 cancelBalloon(beval);
641
642 /* Mouse buttons are pressed - no balloon now */
643 if (!(state & ((int)GDK_BUTTON1_MASK | (int)GDK_BUTTON2_MASK
644 | (int)GDK_BUTTON3_MASK)))
645 {
646 beval->x = x;
647 beval->y = y;
648
649 if (state & (int)GDK_MOD1_MASK)
650 {
651 /*
652 * Alt is pressed -- enter super-evaluate-mode,
653 * where there is no time delay
654 */
655 if (beval->msgCB != NULL)
656 {
657 beval->showState = ShS_PENDING;
658 (*beval->msgCB)(beval, state);
659 }
660 }
661 else
662 {
Bram Moolenaar98921892016-02-23 17:14:37 +0100663# if GTK_CHECK_VERSION(3,0,0)
664 beval->timerID = g_timeout_add((guint)p_bdlay,
665 &timeout_cb, beval);
666# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000667 beval->timerID = gtk_timeout_add((guint32)p_bdlay,
668 &timeout_cb, beval);
Bram Moolenaar98921892016-02-23 17:14:37 +0100669# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000670 }
671 }
672 }
673}
674
675 static void
676key_event(BalloonEval *beval, unsigned keyval, int is_keypress)
677{
678 if (beval->showState == ShS_SHOWING && beval->msgCB != NULL)
679 {
680 switch (keyval)
681 {
682 case GDK_Shift_L:
683 case GDK_Shift_R:
684 beval->showState = ShS_UPDATE_PENDING;
685 (*beval->msgCB)(beval, (is_keypress)
686 ? (int)GDK_SHIFT_MASK : 0);
687 break;
688 case GDK_Control_L:
689 case GDK_Control_R:
690 beval->showState = ShS_UPDATE_PENDING;
691 (*beval->msgCB)(beval, (is_keypress)
692 ? (int)GDK_CONTROL_MASK : 0);
693 break;
694 default:
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000695 /* Don't do this for key release, we apparently get these with
696 * focus changes in some GTK version. */
697 if (is_keypress)
698 cancelBalloon(beval);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000699 break;
700 }
701 }
702 else
703 cancelBalloon(beval);
704}
705
Bram Moolenaar98921892016-02-23 17:14:37 +0100706# if GTK_CHECK_VERSION(3,0,0)
707 static gboolean
708# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000709 static gint
Bram Moolenaar98921892016-02-23 17:14:37 +0100710# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000711timeout_cb(gpointer data)
712{
713 BalloonEval *beval = (BalloonEval *)data;
714
715 beval->timerID = 0;
716 /*
717 * If the timer event happens then the mouse has stopped long enough for
718 * a request to be started. The request will only send to the debugger if
719 * there the mouse is pointing at real data.
720 */
721 requestBalloon(beval);
722
723 return FALSE; /* don't call me again */
724}
725
Bram Moolenaar98921892016-02-23 17:14:37 +0100726# if GTK_CHECK_VERSION(3,0,0)
727 static gboolean
728balloon_draw_event_cb(GtkWidget *widget,
729 cairo_t *cr,
730 gpointer data UNUSED)
731{
732 GtkStyleContext *context = NULL;
733 gint width = -1, height = -1;
734
735 if (widget == NULL)
736 return TRUE;
737
738 context = gtk_widget_get_style_context(widget);
739 width = gtk_widget_get_allocated_width(widget);
740 height = gtk_widget_get_allocated_height(widget);
741
742 gtk_style_context_save(context);
743
744 gtk_style_context_add_class(context, "tooltip");
745 gtk_style_context_set_state(context, GTK_STATE_FLAG_NORMAL);
746
747 cairo_save(cr);
748 gtk_render_frame(context, cr, 0, 0, width, height);
749 gtk_render_background(context, cr, 0, 0, width, height);
750 cairo_restore(cr);
751
752 gtk_style_context_restore(context);
753
754 return FALSE;
755}
756# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000757 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +0000758balloon_expose_event_cb(GtkWidget *widget,
759 GdkEventExpose *event,
760 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000761{
762 gtk_paint_flat_box(widget->style, widget->window,
763 GTK_STATE_NORMAL, GTK_SHADOW_OUT,
764 &event->area, widget, "tooltip",
765 0, 0, -1, -1);
766
767 return FALSE; /* continue emission */
768}
Bram Moolenaar98921892016-02-23 17:14:37 +0100769# endif /* !GTK_CHECK_VERSION(3,0,0) */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000770
Bram Moolenaar071d4272004-06-13 20:20:40 +0000771#else /* !FEAT_GUI_GTK */
772
773 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100774addEventHandler(Widget target, BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000775{
776 XtAddEventHandler(target,
777 PointerMotionMask | EnterWindowMask |
778 LeaveWindowMask | ButtonPressMask | KeyPressMask |
779 KeyReleaseMask,
780 False,
781 pointerEventEH, (XtPointer)beval);
782}
783
784 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100785removeEventHandler(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000786{
787 XtRemoveEventHandler(beval->target,
788 PointerMotionMask | EnterWindowMask |
789 LeaveWindowMask | ButtonPressMask | KeyPressMask |
790 KeyReleaseMask,
791 False,
792 pointerEventEH, (XtPointer)beval);
793}
794
795
796/*
797 * The X event handler. All it does is call the real event handler.
798 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000799 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100800pointerEventEH(
801 Widget w UNUSED,
802 XtPointer client_data,
803 XEvent *event,
804 Boolean *unused UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000805{
806 BalloonEval *beval = (BalloonEval *)client_data;
807 pointerEvent(beval, event);
808}
809
810
811/*
812 * The real event handler. Called by pointerEventEH() whenever an event we are
Bram Moolenaarbae0c162007-05-10 19:30:25 +0000813 * interested in occurs.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000814 */
815
816 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100817pointerEvent(BalloonEval *beval, XEvent *event)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000818{
Bram Moolenaar84a05ac2013-05-06 04:24:17 +0200819 Position distance; /* a measure of how much the pointer moved */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000820 Position delta; /* used to compute distance */
821
822 switch (event->type)
823 {
824 case EnterNotify:
825 case MotionNotify:
826 delta = event->xmotion.x - beval->x;
827 if (delta < 0)
828 delta = -delta;
829 distance = delta;
830 delta = event->xmotion.y - beval->y;
831 if (delta < 0)
832 delta = -delta;
833 distance += delta;
834 if (distance > 4)
835 {
836 /*
837 * Moved out of the balloon location: cancel it.
838 * Remember button state
839 */
840 beval->state = event->xmotion.state;
841 if (beval->state & (Button1Mask|Button2Mask|Button3Mask))
842 {
843 /* Mouse buttons are pressed - no balloon now */
844 cancelBalloon(beval);
845 }
846 else if (beval->state & (Mod1Mask|Mod2Mask|Mod3Mask))
847 {
848 /*
849 * Alt is pressed -- enter super-evaluate-mode,
850 * where there is no time delay
851 */
852 beval->x = event->xmotion.x;
853 beval->y = event->xmotion.y;
854 beval->x_root = event->xmotion.x_root;
855 beval->y_root = event->xmotion.y_root;
856 cancelBalloon(beval);
857 if (beval->msgCB != NULL)
858 {
859 beval->showState = ShS_PENDING;
860 (*beval->msgCB)(beval, beval->state);
861 }
862 }
863 else
864 {
865 beval->x = event->xmotion.x;
866 beval->y = event->xmotion.y;
867 beval->x_root = event->xmotion.x_root;
868 beval->y_root = event->xmotion.y_root;
869 cancelBalloon(beval);
870 beval->timerID = XtAppAddTimeOut( beval->appContext,
871 (long_u)p_bdlay, timerRoutine, beval);
872 }
873 }
874 break;
875
876 /*
877 * Motif and Athena version: Keystrokes will be caught by the
878 * "textArea" widget, and handled in gui_x11_key_hit_cb().
879 */
880 case KeyPress:
881 if (beval->showState == ShS_SHOWING && beval->msgCB != NULL)
882 {
883 Modifiers modifier;
884 KeySym keysym;
885
886 XtTranslateKeycode(gui.dpy,
887 event->xkey.keycode, event->xkey.state,
888 &modifier, &keysym);
889 if (keysym == XK_Shift_L || keysym == XK_Shift_R)
890 {
891 beval->showState = ShS_UPDATE_PENDING;
892 (*beval->msgCB)(beval, ShiftMask);
893 }
894 else if (keysym == XK_Control_L || keysym == XK_Control_R)
895 {
896 beval->showState = ShS_UPDATE_PENDING;
897 (*beval->msgCB)(beval, ControlMask);
898 }
899 else
900 cancelBalloon(beval);
901 }
902 else
903 cancelBalloon(beval);
904 break;
905
906 case KeyRelease:
907 if (beval->showState == ShS_SHOWING && beval->msgCB != NULL)
908 {
909 Modifiers modifier;
910 KeySym keysym;
911
912 XtTranslateKeycode(gui.dpy, event->xkey.keycode,
913 event->xkey.state, &modifier, &keysym);
914 if ((keysym == XK_Shift_L) || (keysym == XK_Shift_R)) {
915 beval->showState = ShS_UPDATE_PENDING;
916 (*beval->msgCB)(beval, 0);
917 }
918 else if ((keysym == XK_Control_L) || (keysym == XK_Control_R))
919 {
920 beval->showState = ShS_UPDATE_PENDING;
921 (*beval->msgCB)(beval, 0);
922 }
923 else
924 cancelBalloon(beval);
925 }
926 else
927 cancelBalloon(beval);
928 break;
929
930 case LeaveNotify:
931 /* Ignore LeaveNotify events that are not "normal".
932 * Apparently we also get it when somebody else grabs focus.
933 * Happens for me every two seconds (some clipboard tool?) */
934 if (event->xcrossing.mode == NotifyNormal)
935 cancelBalloon(beval);
936 break;
937
938 case ButtonPress:
939 cancelBalloon(beval);
940 break;
941
942 default:
943 break;
944 }
945}
946
Bram Moolenaar071d4272004-06-13 20:20:40 +0000947 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100948timerRoutine(XtPointer dx, XtIntervalId *id UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000949{
950 BalloonEval *beval = (BalloonEval *)dx;
951
952 beval->timerID = (XtIntervalId)NULL;
953
954 /*
955 * If the timer event happens then the mouse has stopped long enough for
956 * a request to be started. The request will only send to the debugger if
957 * there the mouse is pointing at real data.
958 */
959 requestBalloon(beval);
960}
961
962#endif /* !FEAT_GUI_GTK */
963
964 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100965requestBalloon(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000966{
967 if (beval->showState != ShS_PENDING)
968 {
969 /* Determine the beval to display */
970 if (beval->msgCB != NULL)
971 {
972 beval->showState = ShS_PENDING;
973 (*beval->msgCB)(beval, beval->state);
974 }
975 else if (beval->msg != NULL)
976 drawBalloon(beval);
977 }
978}
979
980#ifdef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +0000981/*
982 * Convert the string to UTF-8 if 'encoding' is not "utf-8".
983 * Replace any non-printable characters and invalid bytes sequences with
984 * "^X" or "<xx>" escapes, and apply SpecialKey highlighting to them.
985 * TAB and NL are passed through unscathed.
986 */
Bram Moolenaar182c5be2010-06-25 05:37:59 +0200987# define IS_NONPRINTABLE(c) (((c) < 0x20 && (c) != TAB && (c) != NL) \
Bram Moolenaar071d4272004-06-13 20:20:40 +0000988 || (c) == DEL)
989 static void
Bram Moolenaar89d40322006-08-29 15:30:07 +0000990set_printable_label_text(GtkLabel *label, char_u *text)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000991{
992 char_u *convbuf = NULL;
993 char_u *buf;
994 char_u *p;
995 char_u *pdest;
996 unsigned int len;
997 int charlen;
998 int uc;
999 PangoAttrList *attr_list;
1000
1001 /* Convert to UTF-8 if it isn't already */
1002 if (output_conv.vc_type != CONV_NONE)
1003 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00001004 convbuf = string_convert(&output_conv, text, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001005 if (convbuf != NULL)
Bram Moolenaar89d40322006-08-29 15:30:07 +00001006 text = convbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001007 }
1008
1009 /* First let's see how much we need to allocate */
1010 len = 0;
Bram Moolenaar89d40322006-08-29 15:30:07 +00001011 for (p = text; *p != NUL; p += charlen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001012 {
1013 if ((*p & 0x80) == 0) /* be quick for ASCII */
1014 {
1015 charlen = 1;
1016 len += IS_NONPRINTABLE(*p) ? 2 : 1; /* nonprintable: ^X */
1017 }
1018 else
1019 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001020 charlen = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001021 uc = utf_ptr2char(p);
1022
1023 if (charlen != utf_char2len(uc))
1024 charlen = 1; /* reject overlong sequences */
1025
1026 if (charlen == 1 || uc < 0xa0) /* illegal byte or */
1027 len += 4; /* control char: <xx> */
1028 else if (!utf_printable(uc))
1029 /* Note: we assume here that utf_printable() doesn't
1030 * care about characters outside the BMP. */
1031 len += 6; /* nonprintable: <xxxx> */
1032 else
1033 len += charlen;
1034 }
1035 }
1036
1037 attr_list = pango_attr_list_new();
1038 buf = alloc(len + 1);
1039
1040 /* Now go for the real work */
1041 if (buf != NULL)
1042 {
1043 attrentry_T *aep;
1044 PangoAttribute *attr;
1045 guicolor_T pixel;
1046 GdkColor color = { 0, 0, 0, 0 };
1047
1048 /* Look up the RGB values of the SpecialKey foreground color. */
1049 aep = syn_gui_attr2entry(hl_attr(HLF_8));
1050 pixel = (aep != NULL) ? aep->ae_u.gui.fg_color : INVALCOLOR;
1051 if (pixel != INVALCOLOR)
Bram Moolenaar98921892016-02-23 17:14:37 +01001052# if GTK_CHECK_VERSION(3,0,0)
1053 {
1054 GdkVisual * const visual = gtk_widget_get_visual(gui.drawarea);
1055
1056 if (visual == NULL)
1057 {
1058 color.red = 0;
1059 color.green = 0;
1060 color.blue = 0;
1061 }
1062 else
1063 {
1064 guint32 r_mask, g_mask, b_mask;
1065 gint r_shift, g_shift, b_shift;
1066
1067 gdk_visual_get_red_pixel_details(visual, &r_mask, &r_shift,
1068 NULL);
1069 gdk_visual_get_green_pixel_details(visual, &g_mask, &g_shift,
1070 NULL);
1071 gdk_visual_get_blue_pixel_details(visual, &b_mask, &b_shift,
1072 NULL);
1073
1074 color.red = ((pixel & r_mask) >> r_shift) / 255.0 * 65535;
1075 color.green = ((pixel & g_mask) >> g_shift) / 255.0 * 65535;
1076 color.blue = ((pixel & b_mask) >> b_shift) / 255.0 * 65535;
1077 }
1078 }
1079# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001080 gdk_colormap_query_color(gtk_widget_get_colormap(gui.drawarea),
1081 (unsigned long)pixel, &color);
Bram Moolenaar98921892016-02-23 17:14:37 +01001082# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001083
1084 pdest = buf;
Bram Moolenaar89d40322006-08-29 15:30:07 +00001085 p = text;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001086 while (*p != NUL)
1087 {
1088 /* Be quick for ASCII */
1089 if ((*p & 0x80) == 0 && !IS_NONPRINTABLE(*p))
1090 {
1091 *pdest++ = *p++;
1092 }
1093 else
1094 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001095 charlen = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001096 uc = utf_ptr2char(p);
1097
1098 if (charlen != utf_char2len(uc))
1099 charlen = 1; /* reject overlong sequences */
1100
1101 if (charlen == 1 || uc < 0xa0 || !utf_printable(uc))
1102 {
1103 int outlen;
1104
1105 /* Careful: we can't just use transchar_byte() here,
1106 * since 'encoding' is not necessarily set to "utf-8". */
1107 if (*p & 0x80 && charlen == 1)
1108 {
1109 transchar_hex(pdest, *p); /* <xx> */
1110 outlen = 4;
1111 }
1112 else if (uc >= 0x80)
1113 {
1114 /* Note: we assume here that utf_printable() doesn't
1115 * care about characters outside the BMP. */
1116 transchar_hex(pdest, uc); /* <xx> or <xxxx> */
1117 outlen = (uc < 0x100) ? 4 : 6;
1118 }
1119 else
1120 {
1121 transchar_nonprint(pdest, *p); /* ^X */
1122 outlen = 2;
1123 }
1124 if (pixel != INVALCOLOR)
1125 {
1126 attr = pango_attr_foreground_new(
1127 color.red, color.green, color.blue);
1128 attr->start_index = pdest - buf;
1129 attr->end_index = pdest - buf + outlen;
1130 pango_attr_list_insert(attr_list, attr);
1131 }
1132 pdest += outlen;
1133 p += charlen;
1134 }
1135 else
1136 {
1137 do
1138 *pdest++ = *p++;
1139 while (--charlen != 0);
1140 }
1141 }
1142 }
1143 *pdest = NUL;
1144 }
1145
1146 vim_free(convbuf);
1147
1148 gtk_label_set_text(label, (const char *)buf);
1149 vim_free(buf);
1150
1151 gtk_label_set_attributes(label, attr_list);
1152 pango_attr_list_unref(attr_list);
1153}
Bram Moolenaar182c5be2010-06-25 05:37:59 +02001154# undef IS_NONPRINTABLE
Bram Moolenaar071d4272004-06-13 20:20:40 +00001155
1156/*
1157 * Draw a balloon.
1158 */
1159 static void
1160drawBalloon(BalloonEval *beval)
1161{
1162 if (beval->msg != NULL)
1163 {
1164 GtkRequisition requisition;
1165 int screen_w;
1166 int screen_h;
1167 int x;
1168 int y;
1169 int x_offset = EVAL_OFFSET_X;
1170 int y_offset = EVAL_OFFSET_Y;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001171 PangoLayout *layout;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001172# ifdef HAVE_GTK_MULTIHEAD
1173 GdkScreen *screen;
1174
1175 screen = gtk_widget_get_screen(beval->target);
1176 gtk_window_set_screen(GTK_WINDOW(beval->balloonShell), screen);
1177 screen_w = gdk_screen_get_width(screen);
1178 screen_h = gdk_screen_get_height(screen);
1179# else
1180 screen_w = gdk_screen_width();
1181 screen_h = gdk_screen_height();
1182# endif
Bram Moolenaar98921892016-02-23 17:14:37 +01001183# if !GTK_CHECK_VERSION(3,0,0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001184 gtk_widget_ensure_style(beval->balloonShell);
1185 gtk_widget_ensure_style(beval->balloonLabel);
Bram Moolenaar98921892016-02-23 17:14:37 +01001186# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001187
Bram Moolenaar071d4272004-06-13 20:20:40 +00001188 set_printable_label_text(GTK_LABEL(beval->balloonLabel), beval->msg);
1189 /*
1190 * Dirty trick: Enable wrapping mode on the label's layout behind its
1191 * back. This way GtkLabel won't try to constrain the wrap width to a
1192 * builtin maximum value of about 65 Latin characters.
1193 */
1194 layout = gtk_label_get_layout(GTK_LABEL(beval->balloonLabel));
Bram Moolenaar182c5be2010-06-25 05:37:59 +02001195# ifdef PANGO_WRAP_WORD_CHAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00001196 pango_layout_set_wrap(layout, PANGO_WRAP_WORD_CHAR);
Bram Moolenaar182c5be2010-06-25 05:37:59 +02001197# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001198 pango_layout_set_wrap(layout, PANGO_WRAP_WORD);
Bram Moolenaar182c5be2010-06-25 05:37:59 +02001199# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001200 pango_layout_set_width(layout,
1201 /* try to come up with some reasonable width */
1202 PANGO_SCALE * CLAMP(gui.num_cols * gui.char_width,
1203 screen_w / 2,
1204 MAX(20, screen_w - 20)));
1205
1206 /* Calculate the balloon's width and height. */
Bram Moolenaar98921892016-02-23 17:14:37 +01001207# if GTK_CHECK_VERSION(3,0,0)
1208 gtk_widget_get_preferred_size(beval->balloonShell, &requisition, NULL);
1209# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001210 gtk_widget_size_request(beval->balloonShell, &requisition);
Bram Moolenaar98921892016-02-23 17:14:37 +01001211# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001212
1213 /* Compute position of the balloon area */
Bram Moolenaar98921892016-02-23 17:14:37 +01001214# if GTK_CHECK_VERSION(3,0,0)
1215 gdk_window_get_origin(gtk_widget_get_window(beval->target), &x, &y);
1216# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001217 gdk_window_get_origin(beval->target->window, &x, &y);
Bram Moolenaar98921892016-02-23 17:14:37 +01001218# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001219 x += beval->x;
1220 y += beval->y;
1221
1222 /* Get out of the way of the mouse pointer */
1223 if (x + x_offset + requisition.width > screen_w)
1224 y_offset += 15;
1225 if (y + y_offset + requisition.height > screen_h)
1226 y_offset = -requisition.height - EVAL_OFFSET_Y;
1227
1228 /* Sanitize values */
1229 x = CLAMP(x + x_offset, 0, MAX(0, screen_w - requisition.width));
1230 y = CLAMP(y + y_offset, 0, MAX(0, screen_h - requisition.height));
1231
1232 /* Show the balloon */
Bram Moolenaar98921892016-02-23 17:14:37 +01001233# if GTK_CHECK_VERSION(3,0,0)
1234 gtk_window_move(GTK_WINDOW(beval->balloonShell), x, y);
1235# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001236 gtk_widget_set_uposition(beval->balloonShell, x, y);
Bram Moolenaar98921892016-02-23 17:14:37 +01001237# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001238 gtk_widget_show(beval->balloonShell);
1239
1240 beval->showState = ShS_SHOWING;
1241 }
1242}
1243
1244/*
1245 * Undraw a balloon.
1246 */
1247 static void
1248undrawBalloon(BalloonEval *beval)
1249{
1250 if (beval->balloonShell != NULL)
1251 gtk_widget_hide(beval->balloonShell);
1252 beval->showState = ShS_NEUTRAL;
1253}
1254
1255 static void
1256cancelBalloon(BalloonEval *beval)
1257{
1258 if (beval->showState == ShS_SHOWING
1259 || beval->showState == ShS_UPDATE_PENDING)
1260 undrawBalloon(beval);
1261
1262 if (beval->timerID != 0)
1263 {
Bram Moolenaar98921892016-02-23 17:14:37 +01001264# if GTK_CHECK_VERSION(3,0,0)
1265 g_source_remove(beval->timerID);
1266# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001267 gtk_timeout_remove(beval->timerID);
Bram Moolenaar98921892016-02-23 17:14:37 +01001268# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001269 beval->timerID = 0;
1270 }
1271 beval->showState = ShS_NEUTRAL;
1272}
1273
1274 static void
1275createBalloonEvalWindow(BalloonEval *beval)
1276{
1277 beval->balloonShell = gtk_window_new(GTK_WINDOW_POPUP);
1278
1279 gtk_widget_set_app_paintable(beval->balloonShell, TRUE);
Bram Moolenaar98921892016-02-23 17:14:37 +01001280# if GTK_CHECK_VERSION(3,0,0)
1281 gtk_window_set_resizable(GTK_WINDOW(beval->balloonShell), FALSE);
1282# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001283 gtk_window_set_policy(GTK_WINDOW(beval->balloonShell), FALSE, FALSE, TRUE);
Bram Moolenaar98921892016-02-23 17:14:37 +01001284# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001285 gtk_widget_set_name(beval->balloonShell, "gtk-tooltips");
Bram Moolenaar98921892016-02-23 17:14:37 +01001286# if GTK_CHECK_VERSION(3,0,0)
1287 gtk_container_set_border_width(GTK_CONTAINER(beval->balloonShell), 4);
1288# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001289 gtk_container_border_width(GTK_CONTAINER(beval->balloonShell), 4);
Bram Moolenaar98921892016-02-23 17:14:37 +01001290# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001291
Bram Moolenaar98921892016-02-23 17:14:37 +01001292# if GTK_CHECK_VERSION(3,0,0)
1293 g_signal_connect(G_OBJECT(beval->balloonShell), "draw",
1294 G_CALLBACK(balloon_draw_event_cb), NULL);
1295# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001296 gtk_signal_connect((GtkObject*)(beval->balloonShell), "expose_event",
1297 GTK_SIGNAL_FUNC(balloon_expose_event_cb), NULL);
Bram Moolenaar98921892016-02-23 17:14:37 +01001298# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001299 beval->balloonLabel = gtk_label_new(NULL);
1300
1301 gtk_label_set_line_wrap(GTK_LABEL(beval->balloonLabel), FALSE);
1302 gtk_label_set_justify(GTK_LABEL(beval->balloonLabel), GTK_JUSTIFY_LEFT);
Bram Moolenaar98921892016-02-23 17:14:37 +01001303# if GTK_CHECK_VERSION(3,16,0)
1304 gtk_label_set_xalign(GTK_LABEL(beval->balloonLabel), 0.5);
1305 gtk_label_set_yalign(GTK_LABEL(beval->balloonLabel), 0.5);
1306# elif GTK_CHECK_VERSION(3,14,0)
1307 GValue align_val = G_VALUE_INIT;
1308 g_value_init(&align_val, G_TYPE_FLOAT);
1309 g_value_set_float(&align_val, 0.5);
1310 g_object_set_property(G_OBJECT(beval->balloonLabel), "xalign", &align_val);
1311 g_object_set_property(G_OBJECT(beval->balloonLabel), "yalign", &align_val);
1312 g_value_unset(&align_val);
1313# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001314 gtk_misc_set_alignment(GTK_MISC(beval->balloonLabel), 0.5f, 0.5f);
Bram Moolenaar98921892016-02-23 17:14:37 +01001315# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001316 gtk_widget_set_name(beval->balloonLabel, "vim-balloon-label");
1317 gtk_widget_show(beval->balloonLabel);
1318
1319 gtk_container_add(GTK_CONTAINER(beval->balloonShell), beval->balloonLabel);
1320}
1321
1322#else /* !FEAT_GUI_GTK */
1323
1324/*
1325 * Draw a balloon.
1326 */
1327 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001328drawBalloon(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001329{
1330 Dimension w;
1331 Dimension h;
1332 Position tx;
1333 Position ty;
1334
1335 if (beval->msg != NULL)
1336 {
1337 /* Show the Balloon */
1338
1339 /* Calculate the label's width and height */
1340#ifdef FEAT_GUI_MOTIF
1341 XmString s;
1342
1343 /* For the callback function we parse NL characters to create a
1344 * multi-line label. This doesn't work for all languages, but
1345 * XmStringCreateLocalized() doesn't do multi-line labels... */
1346 if (beval->msgCB != NULL)
1347 s = XmStringCreateLtoR((char *)beval->msg, XmFONTLIST_DEFAULT_TAG);
1348 else
1349 s = XmStringCreateLocalized((char *)beval->msg);
1350 {
1351 XmFontList fl;
1352
1353 fl = gui_motif_fontset2fontlist(&gui.tooltip_fontset);
Bram Moolenaardb5ffaa2014-06-25 17:44:49 +02001354 if (fl == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001355 {
Bram Moolenaardb5ffaa2014-06-25 17:44:49 +02001356 XmStringFree(s);
1357 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001358 }
Bram Moolenaardb5ffaa2014-06-25 17:44:49 +02001359 XmStringExtent(fl, s, &w, &h);
1360 XmFontListFree(fl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001361 }
1362 w += gui.border_offset << 1;
1363 h += gui.border_offset << 1;
1364 XtVaSetValues(beval->balloonLabel, XmNlabelString, s, NULL);
1365 XmStringFree(s);
1366#else /* Athena */
1367 /* Assume XtNinternational == True */
1368 XFontSet fset;
1369 XFontSetExtents *ext;
1370
1371 XtVaGetValues(beval->balloonLabel, XtNfontSet, &fset, NULL);
1372 ext = XExtentsOfFontSet(fset);
1373 h = ext->max_ink_extent.height;
1374 w = XmbTextEscapement(fset,
1375 (char *)beval->msg,
1376 (int)STRLEN(beval->msg));
1377 w += gui.border_offset << 1;
1378 h += gui.border_offset << 1;
1379 XtVaSetValues(beval->balloonLabel, XtNlabel, beval->msg, NULL);
1380#endif
1381
1382 /* Compute position of the balloon area */
1383 tx = beval->x_root + EVAL_OFFSET_X;
1384 ty = beval->y_root + EVAL_OFFSET_Y;
1385 if ((tx + w) > beval->screen_width)
1386 tx = beval->screen_width - w;
1387 if ((ty + h) > beval->screen_height)
1388 ty = beval->screen_height - h;
1389#ifdef FEAT_GUI_MOTIF
1390 XtVaSetValues(beval->balloonShell,
1391 XmNx, tx,
1392 XmNy, ty,
1393 NULL);
1394#else
1395 /* Athena */
1396 XtVaSetValues(beval->balloonShell,
1397 XtNx, tx,
1398 XtNy, ty,
1399 NULL);
1400#endif
Bram Moolenaar8281f442009-03-18 11:22:25 +00001401 /* Set tooltip colors */
1402 {
1403 Arg args[2];
1404
1405#ifdef FEAT_GUI_MOTIF
1406 args[0].name = XmNbackground;
1407 args[0].value = gui.tooltip_bg_pixel;
1408 args[1].name = XmNforeground;
1409 args[1].value = gui.tooltip_fg_pixel;
1410#else /* Athena */
1411 args[0].name = XtNbackground;
1412 args[0].value = gui.tooltip_bg_pixel;
1413 args[1].name = XtNforeground;
1414 args[1].value = gui.tooltip_fg_pixel;
1415#endif
1416 XtSetValues(beval->balloonLabel, &args[0], XtNumber(args));
1417 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001418
1419 XtPopup(beval->balloonShell, XtGrabNone);
1420
1421 beval->showState = ShS_SHOWING;
1422
1423 current_beval = beval;
1424 }
1425}
1426
1427/*
1428 * Undraw a balloon.
1429 */
1430 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001431undrawBalloon(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001432{
1433 if (beval->balloonShell != (Widget)0)
1434 XtPopdown(beval->balloonShell);
1435 beval->showState = ShS_NEUTRAL;
1436
1437 current_beval = NULL;
1438}
1439
1440 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001441cancelBalloon(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001442{
1443 if (beval->showState == ShS_SHOWING
1444 || beval->showState == ShS_UPDATE_PENDING)
1445 undrawBalloon(beval);
1446
1447 if (beval->timerID != (XtIntervalId)NULL)
1448 {
1449 XtRemoveTimeOut(beval->timerID);
1450 beval->timerID = (XtIntervalId)NULL;
1451 }
1452 beval->showState = ShS_NEUTRAL;
1453}
1454
1455
1456 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001457createBalloonEvalWindow(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001458{
1459 Arg args[12];
1460 int n;
1461
1462 n = 0;
1463#ifdef FEAT_GUI_MOTIF
1464 XtSetArg(args[n], XmNallowShellResize, True); n++;
1465 beval->balloonShell = XtAppCreateShell("balloonEval", "BalloonEval",
1466 overrideShellWidgetClass, gui.dpy, args, n);
1467#else
1468 /* Athena */
1469 XtSetArg(args[n], XtNallowShellResize, True); n++;
1470 beval->balloonShell = XtAppCreateShell("balloonEval", "BalloonEval",
1471 overrideShellWidgetClass, gui.dpy, args, n);
1472#endif
1473
1474 n = 0;
1475#ifdef FEAT_GUI_MOTIF
1476 {
1477 XmFontList fl;
1478
1479 fl = gui_motif_fontset2fontlist(&gui.tooltip_fontset);
1480 XtSetArg(args[n], XmNforeground, gui.tooltip_fg_pixel); n++;
1481 XtSetArg(args[n], XmNbackground, gui.tooltip_bg_pixel); n++;
1482 XtSetArg(args[n], XmNfontList, fl); n++;
1483 XtSetArg(args[n], XmNalignment, XmALIGNMENT_BEGINNING); n++;
1484 beval->balloonLabel = XtCreateManagedWidget("balloonLabel",
1485 xmLabelWidgetClass, beval->balloonShell, args, n);
1486 }
1487#else /* FEAT_GUI_ATHENA */
1488 XtSetArg(args[n], XtNforeground, gui.tooltip_fg_pixel); n++;
1489 XtSetArg(args[n], XtNbackground, gui.tooltip_bg_pixel); n++;
1490 XtSetArg(args[n], XtNinternational, True); n++;
1491 XtSetArg(args[n], XtNfontSet, gui.tooltip_fontset); n++;
1492 beval->balloonLabel = XtCreateManagedWidget("balloonLabel",
1493 labelWidgetClass, beval->balloonShell, args, n);
1494#endif
1495}
1496
1497#endif /* !FEAT_GUI_GTK */
1498#endif /* !FEAT_GUI_W32 */
1499
1500#endif /* FEAT_BEVAL */