Bram Moolenaar | edf3f97 | 2016-08-29 22:49:24 +0200 | [diff] [blame] | 1 | /* vi:set ts=8 sts=4 sw=4 noet: |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 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 | |
Bram Moolenaar | c3719bd | 2017-11-18 22:13:31 +0100 | [diff] [blame] | 13 | #if defined(FEAT_BEVAL_GUI) || defined(PROTO) |
Bram Moolenaar | e2ac10d | 2005-03-07 23:26:06 +0000 | [diff] [blame] | 14 | |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 15 | // on Win32 only get_beval_info() is required |
Bram Moolenaar | 4f97475 | 2019-02-17 17:44:42 +0100 | [diff] [blame] | 16 | #if !defined(FEAT_GUI_MSWIN) || defined(PROTO) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 17 | |
| 18 | #ifdef FEAT_GUI_GTK |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 19 | # if GTK_CHECK_VERSION(3,0,0) |
| 20 | # include <gdk/gdkkeysyms-compat.h> |
| 21 | # else |
| 22 | # include <gdk/gdkkeysyms.h> |
| 23 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 24 | # include <gtk/gtk.h> |
| 25 | #else |
| 26 | # include <X11/keysym.h> |
Bram Moolenaar | 0b962e5 | 2022-04-03 18:02:37 +0100 | [diff] [blame] | 27 | # include <Xm/PushB.h> |
| 28 | # include <Xm/Separator.h> |
| 29 | # include <Xm/List.h> |
| 30 | # include <Xm/Label.h> |
| 31 | # include <Xm/AtomMgr.h> |
| 32 | # include <Xm/Protocols.h> |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 33 | #endif |
| 34 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 35 | #ifndef FEAT_GUI_GTK |
| 36 | extern Widget vimShell; |
| 37 | |
| 38 | /* |
| 39 | * Currently, we assume that there can be only one BalloonEval showing |
| 40 | * on-screen at any given moment. This variable will hold the currently |
| 41 | * showing BalloonEval or NULL if none is showing. |
| 42 | */ |
| 43 | static BalloonEval *current_beval = NULL; |
| 44 | #endif |
| 45 | |
| 46 | #ifdef FEAT_GUI_GTK |
Bram Moolenaar | d25c16e | 2016-01-29 22:13:30 +0100 | [diff] [blame] | 47 | static void addEventHandler(GtkWidget *, BalloonEval *); |
| 48 | static void removeEventHandler(BalloonEval *); |
| 49 | static gint target_event_cb(GtkWidget *, GdkEvent *, gpointer); |
| 50 | static gint mainwin_event_cb(GtkWidget *, GdkEvent *, gpointer); |
| 51 | static void pointer_event(BalloonEval *, int, int, unsigned); |
| 52 | static void key_event(BalloonEval *, unsigned, int); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 53 | static gboolean timeout_cb(gpointer); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 54 | # if GTK_CHECK_VERSION(3,0,0) |
| 55 | static gboolean balloon_draw_event_cb (GtkWidget *, cairo_t *, gpointer); |
| 56 | # else |
| 57 | static gint balloon_expose_event_cb (GtkWidget *, GdkEventExpose *, gpointer); |
| 58 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 59 | #else |
Bram Moolenaar | d25c16e | 2016-01-29 22:13:30 +0100 | [diff] [blame] | 60 | static void addEventHandler(Widget, BalloonEval *); |
| 61 | static void removeEventHandler(BalloonEval *); |
| 62 | static void pointerEventEH(Widget, XtPointer, XEvent *, Boolean *); |
| 63 | static void pointerEvent(BalloonEval *, XEvent *); |
| 64 | static void timerRoutine(XtPointer, XtIntervalId *); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 65 | #endif |
Bram Moolenaar | d25c16e | 2016-01-29 22:13:30 +0100 | [diff] [blame] | 66 | static void cancelBalloon(BalloonEval *); |
| 67 | static void requestBalloon(BalloonEval *); |
| 68 | static void drawBalloon(BalloonEval *); |
| 69 | static void undrawBalloon(BalloonEval *beval); |
| 70 | static void createBalloonEvalWindow(BalloonEval *); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 71 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 72 | /* |
| 73 | * Create a balloon-evaluation area for a Widget. |
| 74 | * There can be either a "mesg" for a fixed string or "mesgCB" to generate a |
| 75 | * message by calling this callback function. |
| 76 | * When "mesg" is not NULL it must remain valid for as long as the balloon is |
| 77 | * used. It is not freed here. |
| 78 | * Returns a pointer to the resulting object (NULL when out of memory). |
| 79 | */ |
| 80 | BalloonEval * |
Bram Moolenaar | 66f948e | 2016-01-30 16:39:25 +0100 | [diff] [blame] | 81 | gui_mch_create_beval_area( |
| 82 | void *target, |
| 83 | char_u *mesg, |
| 84 | void (*mesgCB)(BalloonEval *, int), |
| 85 | void *clientData) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 86 | { |
| 87 | #ifndef FEAT_GUI_GTK |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 88 | char *display_name; // get from gui.dpy |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 89 | int screen_num; |
| 90 | char *p; |
| 91 | #endif |
| 92 | BalloonEval *beval; |
| 93 | |
| 94 | if (mesg != NULL && mesgCB != NULL) |
| 95 | { |
RestorerZ | 68ebcee | 2023-05-31 17:12:14 +0100 | [diff] [blame] | 96 | iemsg(e_cannot_create_ballooneval_with_both_message_and_callback); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 97 | return NULL; |
| 98 | } |
| 99 | |
Bram Moolenaar | e809a4e | 2019-07-04 17:35:05 +0200 | [diff] [blame] | 100 | beval = ALLOC_CLEAR_ONE(BalloonEval); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 101 | if (beval != NULL) |
| 102 | { |
| 103 | #ifdef FEAT_GUI_GTK |
| 104 | beval->target = GTK_WIDGET(target); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 105 | #else |
| 106 | beval->target = (Widget)target; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 107 | beval->appContext = XtWidgetToApplicationContext((Widget)target); |
| 108 | #endif |
| 109 | beval->showState = ShS_NEUTRAL; |
Bram Moolenaar | be0a259 | 2019-05-09 13:50:16 +0200 | [diff] [blame] | 110 | vim_free(beval->msg); |
| 111 | beval->msg = mesg == NULL ? NULL : vim_strsave(mesg); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 112 | beval->msgCB = mesgCB; |
| 113 | beval->clientData = clientData; |
| 114 | |
| 115 | /* |
| 116 | * Set up event handler which will keep its eyes on the pointer, |
| 117 | * and when the pointer rests in a certain spot for a given time |
| 118 | * interval, show the beval. |
| 119 | */ |
| 120 | addEventHandler(beval->target, beval); |
| 121 | createBalloonEvalWindow(beval); |
| 122 | |
| 123 | #ifndef FEAT_GUI_GTK |
| 124 | /* |
| 125 | * Now create and save the screen width and height. Used in drawing. |
| 126 | */ |
| 127 | display_name = DisplayString(gui.dpy); |
| 128 | p = strrchr(display_name, '.'); |
| 129 | if (p != NULL) |
| 130 | screen_num = atoi(++p); |
| 131 | else |
| 132 | screen_num = 0; |
| 133 | beval->screen_width = DisplayWidth(gui.dpy, screen_num); |
| 134 | beval->screen_height = DisplayHeight(gui.dpy, screen_num); |
| 135 | #endif |
| 136 | } |
| 137 | |
| 138 | return beval; |
| 139 | } |
| 140 | |
| 141 | #if defined(FEAT_BEVAL_TIP) || defined(PROTO) |
| 142 | /* |
Bram Moolenaar | bae0c16 | 2007-05-10 19:30:25 +0000 | [diff] [blame] | 143 | * Destroy a balloon-eval and free its associated memory. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 144 | */ |
| 145 | void |
Bram Moolenaar | 66f948e | 2016-01-30 16:39:25 +0100 | [diff] [blame] | 146 | gui_mch_destroy_beval_area(BalloonEval *beval) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 147 | { |
| 148 | cancelBalloon(beval); |
| 149 | removeEventHandler(beval); |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 150 | // Children will automatically be destroyed |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 151 | # ifdef FEAT_GUI_GTK |
| 152 | gtk_widget_destroy(beval->balloonShell); |
| 153 | # else |
| 154 | XtDestroyWidget(beval->balloonShell); |
| 155 | # endif |
Bram Moolenaar | 04958cb | 2018-06-23 19:23:02 +0200 | [diff] [blame] | 156 | # ifdef FEAT_VARTABS |
| 157 | if (beval->vts) |
| 158 | vim_free(beval->vts); |
| 159 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 160 | vim_free(beval); |
| 161 | } |
| 162 | #endif |
| 163 | |
| 164 | void |
Bram Moolenaar | 66f948e | 2016-01-30 16:39:25 +0100 | [diff] [blame] | 165 | gui_mch_enable_beval_area(BalloonEval *beval) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 166 | { |
| 167 | if (beval != NULL) |
| 168 | addEventHandler(beval->target, beval); |
| 169 | } |
| 170 | |
| 171 | void |
Bram Moolenaar | 66f948e | 2016-01-30 16:39:25 +0100 | [diff] [blame] | 172 | gui_mch_disable_beval_area(BalloonEval *beval) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 173 | { |
| 174 | if (beval != NULL) |
| 175 | removeEventHandler(beval); |
| 176 | } |
| 177 | |
| 178 | #if defined(FEAT_BEVAL_TIP) || defined(PROTO) |
| 179 | /* |
| 180 | * This function returns the BalloonEval * associated with the currently |
| 181 | * displayed tooltip. Returns NULL if there is no tooltip currently showing. |
| 182 | * |
| 183 | * Assumption: Only one tooltip can be shown at a time. |
| 184 | */ |
| 185 | BalloonEval * |
Bram Moolenaar | 66f948e | 2016-01-30 16:39:25 +0100 | [diff] [blame] | 186 | gui_mch_currently_showing_beval(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 187 | { |
| 188 | return current_beval; |
| 189 | } |
| 190 | #endif |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 191 | #endif // !FEAT_GUI_MSWIN |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 192 | |
Bram Moolenaar | bb1969b | 2019-01-17 15:45:25 +0100 | [diff] [blame] | 193 | #if defined(FEAT_NETBEANS_INTG) || defined(FEAT_EVAL) || defined(PROTO) |
Bram Moolenaar | 4f97475 | 2019-02-17 17:44:42 +0100 | [diff] [blame] | 194 | # if !defined(FEAT_GUI_MSWIN) || defined(PROTO) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 195 | |
| 196 | /* |
| 197 | * Show a balloon with "mesg". |
| 198 | */ |
| 199 | void |
Bram Moolenaar | 66f948e | 2016-01-30 16:39:25 +0100 | [diff] [blame] | 200 | gui_mch_post_balloon(BalloonEval *beval, char_u *mesg) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 201 | { |
Bram Moolenaar | be0a259 | 2019-05-09 13:50:16 +0200 | [diff] [blame] | 202 | vim_free(beval->msg); |
| 203 | beval->msg = mesg == NULL ? NULL : vim_strsave(mesg); |
| 204 | if (beval->msg != NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 205 | drawBalloon(beval); |
| 206 | else |
| 207 | undrawBalloon(beval); |
| 208 | } |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 209 | # endif // !FEAT_GUI_MSWIN |
| 210 | #endif // FEAT_NETBEANS_INTG || PROTO |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 211 | |
Bram Moolenaar | 4f97475 | 2019-02-17 17:44:42 +0100 | [diff] [blame] | 212 | #if !defined(FEAT_GUI_MSWIN) || defined(PROTO) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 213 | #if defined(FEAT_BEVAL_TIP) || defined(PROTO) |
| 214 | /* |
| 215 | * Hide the given balloon. |
| 216 | */ |
| 217 | void |
Bram Moolenaar | 66f948e | 2016-01-30 16:39:25 +0100 | [diff] [blame] | 218 | gui_mch_unpost_balloon(BalloonEval *beval) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 219 | { |
Bram Moolenaar | be0a259 | 2019-05-09 13:50:16 +0200 | [diff] [blame] | 220 | VIM_CLEAR(beval->msg); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 221 | undrawBalloon(beval); |
| 222 | } |
| 223 | #endif |
| 224 | |
| 225 | #ifdef FEAT_GUI_GTK |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 226 | static void |
| 227 | addEventHandler(GtkWidget *target, BalloonEval *beval) |
| 228 | { |
| 229 | /* |
| 230 | * Connect to the generic "event" signal instead of the individual |
| 231 | * signals for each event type, because the former is emitted earlier. |
| 232 | * This allows us to catch events independently of the signal handlers |
| 233 | * in gui_gtk_x11.c. |
| 234 | */ |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 235 | g_signal_connect(G_OBJECT(target), "event", |
| 236 | G_CALLBACK(target_event_cb), |
| 237 | beval); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 238 | /* |
| 239 | * Nasty: Key press events go to the main window thus the drawing area |
| 240 | * will never see them. This means we have to connect to the main window |
| 241 | * as well in order to catch those events. |
| 242 | */ |
| 243 | if (gtk_socket_id == 0 && gui.mainwin != NULL |
| 244 | && gtk_widget_is_ancestor(target, gui.mainwin)) |
| 245 | { |
Bram Moolenaar | 47f6db9 | 2021-06-14 22:08:46 +0200 | [diff] [blame] | 246 | gtk_widget_add_events(gui.mainwin, |
| 247 | GDK_LEAVE_NOTIFY_MASK); |
| 248 | |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 249 | g_signal_connect(G_OBJECT(gui.mainwin), "event", |
| 250 | G_CALLBACK(mainwin_event_cb), |
| 251 | beval); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 252 | } |
| 253 | } |
| 254 | |
| 255 | static void |
| 256 | removeEventHandler(BalloonEval *beval) |
| 257 | { |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 258 | g_signal_handlers_disconnect_by_func(G_OBJECT(beval->target), |
Bram Moolenaar | d47d837 | 2016-09-09 22:13:24 +0200 | [diff] [blame] | 259 | FUNC2GENERIC(target_event_cb), |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 260 | beval); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 261 | |
| 262 | if (gtk_socket_id == 0 && gui.mainwin != NULL |
| 263 | && gtk_widget_is_ancestor(beval->target, gui.mainwin)) |
| 264 | { |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 265 | g_signal_handlers_disconnect_by_func(G_OBJECT(gui.mainwin), |
Bram Moolenaar | d47d837 | 2016-09-09 22:13:24 +0200 | [diff] [blame] | 266 | FUNC2GENERIC(mainwin_event_cb), |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 267 | beval); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 268 | } |
| 269 | } |
| 270 | |
| 271 | static gint |
| 272 | target_event_cb(GtkWidget *widget, GdkEvent *event, gpointer data) |
| 273 | { |
| 274 | BalloonEval *beval = (BalloonEval *)data; |
| 275 | |
| 276 | switch (event->type) |
| 277 | { |
| 278 | case GDK_ENTER_NOTIFY: |
| 279 | pointer_event(beval, (int)event->crossing.x, |
| 280 | (int)event->crossing.y, |
| 281 | event->crossing.state); |
| 282 | break; |
| 283 | case GDK_MOTION_NOTIFY: |
| 284 | if (event->motion.is_hint) |
| 285 | { |
| 286 | int x; |
| 287 | int y; |
| 288 | GdkModifierType state; |
| 289 | /* |
| 290 | * GDK_POINTER_MOTION_HINT_MASK is set, thus we cannot obtain |
| 291 | * the coordinates from the GdkEventMotion struct directly. |
| 292 | */ |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 293 | # if GTK_CHECK_VERSION(3,0,0) |
| 294 | { |
| 295 | GdkWindow * const win = gtk_widget_get_window(widget); |
| 296 | GdkDisplay * const dpy = gdk_window_get_display(win); |
Bram Moolenaar | 30e12d2 | 2016-04-17 20:49:53 +0200 | [diff] [blame] | 297 | # if GTK_CHECK_VERSION(3,20,0) |
| 298 | GdkSeat * const seat = gdk_display_get_default_seat(dpy); |
| 299 | GdkDevice * const dev = gdk_seat_get_pointer(seat); |
| 300 | # else |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 301 | GdkDeviceManager * const mngr = gdk_display_get_device_manager(dpy); |
| 302 | GdkDevice * const dev = gdk_device_manager_get_client_pointer(mngr); |
Bram Moolenaar | 30e12d2 | 2016-04-17 20:49:53 +0200 | [diff] [blame] | 303 | # endif |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 304 | gdk_window_get_device_position(win, dev , &x, &y, &state); |
| 305 | } |
| 306 | # else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 307 | gdk_window_get_pointer(widget->window, &x, &y, &state); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 308 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 309 | pointer_event(beval, x, y, (unsigned int)state); |
| 310 | } |
| 311 | else |
| 312 | { |
| 313 | pointer_event(beval, (int)event->motion.x, |
| 314 | (int)event->motion.y, |
| 315 | event->motion.state); |
| 316 | } |
| 317 | break; |
| 318 | case GDK_LEAVE_NOTIFY: |
| 319 | /* |
| 320 | * Ignore LeaveNotify events that are not "normal". |
| 321 | * Apparently we also get it when somebody else grabs focus. |
| 322 | */ |
| 323 | if (event->crossing.mode == GDK_CROSSING_NORMAL) |
| 324 | cancelBalloon(beval); |
| 325 | break; |
| 326 | case GDK_BUTTON_PRESS: |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 327 | case GDK_SCROLL: |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 328 | cancelBalloon(beval); |
| 329 | break; |
| 330 | case GDK_KEY_PRESS: |
| 331 | key_event(beval, event->key.keyval, TRUE); |
| 332 | break; |
| 333 | case GDK_KEY_RELEASE: |
| 334 | key_event(beval, event->key.keyval, FALSE); |
| 335 | break; |
| 336 | default: |
| 337 | break; |
| 338 | } |
| 339 | |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 340 | return FALSE; // continue emission |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 341 | } |
| 342 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 343 | static gint |
Bram Moolenaar | b85cb21 | 2009-05-17 14:24:23 +0000 | [diff] [blame] | 344 | mainwin_event_cb(GtkWidget *widget UNUSED, GdkEvent *event, gpointer data) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 345 | { |
| 346 | BalloonEval *beval = (BalloonEval *)data; |
| 347 | |
| 348 | switch (event->type) |
| 349 | { |
| 350 | case GDK_KEY_PRESS: |
| 351 | key_event(beval, event->key.keyval, TRUE); |
| 352 | break; |
| 353 | case GDK_KEY_RELEASE: |
| 354 | key_event(beval, event->key.keyval, FALSE); |
| 355 | break; |
Bram Moolenaar | 47f6db9 | 2021-06-14 22:08:46 +0200 | [diff] [blame] | 356 | case GDK_LEAVE_NOTIFY: |
| 357 | // Ignore LeaveNotify events that are not "normal". |
| 358 | // Apparently we also get it when somebody else grabs focus. |
| 359 | if (event->crossing.mode == GDK_CROSSING_NORMAL) |
| 360 | cancelBalloon(beval); |
| 361 | break; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 362 | default: |
| 363 | break; |
| 364 | } |
| 365 | |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 366 | return FALSE; // continue emission |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 367 | } |
| 368 | |
| 369 | static void |
| 370 | pointer_event(BalloonEval *beval, int x, int y, unsigned state) |
| 371 | { |
| 372 | int distance; |
| 373 | |
| 374 | distance = ABS(x - beval->x) + ABS(y - beval->y); |
| 375 | |
Yegappan Lakshmanan | 7f8b255 | 2023-01-08 13:44:24 +0000 | [diff] [blame] | 376 | if (distance <= 4) |
| 377 | return; |
| 378 | |
| 379 | /* |
| 380 | * Moved out of the balloon location: cancel it. |
| 381 | * Remember button state |
| 382 | */ |
| 383 | beval->state = state; |
| 384 | cancelBalloon(beval); |
| 385 | |
| 386 | // Mouse buttons are pressed - no balloon now |
| 387 | if (!(state & ((int)GDK_BUTTON1_MASK | (int)GDK_BUTTON2_MASK |
| 388 | | (int)GDK_BUTTON3_MASK))) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 389 | { |
Yegappan Lakshmanan | 7f8b255 | 2023-01-08 13:44:24 +0000 | [diff] [blame] | 390 | beval->x = x; |
| 391 | beval->y = y; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 392 | |
Yegappan Lakshmanan | 7f8b255 | 2023-01-08 13:44:24 +0000 | [diff] [blame] | 393 | if (state & (int)GDK_MOD1_MASK) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 394 | { |
Yegappan Lakshmanan | 7f8b255 | 2023-01-08 13:44:24 +0000 | [diff] [blame] | 395 | /* |
| 396 | * Alt is pressed -- enter super-evaluate-mode, |
| 397 | * where there is no time delay |
| 398 | */ |
| 399 | if (beval->msgCB != NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 400 | { |
Yegappan Lakshmanan | 7f8b255 | 2023-01-08 13:44:24 +0000 | [diff] [blame] | 401 | beval->showState = ShS_PENDING; |
| 402 | (*beval->msgCB)(beval, state); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 403 | } |
Yegappan Lakshmanan | 7f8b255 | 2023-01-08 13:44:24 +0000 | [diff] [blame] | 404 | } |
| 405 | else |
| 406 | { |
Bram Moolenaar | f39d9e9 | 2023-04-22 22:54:40 +0100 | [diff] [blame] | 407 | beval->timerID = g_timeout_add((guint)p_bdlay, &timeout_cb, beval); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 408 | } |
| 409 | } |
| 410 | } |
| 411 | |
| 412 | static void |
| 413 | key_event(BalloonEval *beval, unsigned keyval, int is_keypress) |
| 414 | { |
| 415 | if (beval->showState == ShS_SHOWING && beval->msgCB != NULL) |
| 416 | { |
| 417 | switch (keyval) |
| 418 | { |
| 419 | case GDK_Shift_L: |
| 420 | case GDK_Shift_R: |
| 421 | beval->showState = ShS_UPDATE_PENDING; |
| 422 | (*beval->msgCB)(beval, (is_keypress) |
| 423 | ? (int)GDK_SHIFT_MASK : 0); |
| 424 | break; |
| 425 | case GDK_Control_L: |
| 426 | case GDK_Control_R: |
| 427 | beval->showState = ShS_UPDATE_PENDING; |
| 428 | (*beval->msgCB)(beval, (is_keypress) |
| 429 | ? (int)GDK_CONTROL_MASK : 0); |
| 430 | break; |
| 431 | default: |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 432 | // Don't do this for key release, we apparently get these with |
| 433 | // focus changes in some GTK version. |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 434 | if (is_keypress) |
| 435 | cancelBalloon(beval); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 436 | break; |
| 437 | } |
| 438 | } |
| 439 | else |
| 440 | cancelBalloon(beval); |
| 441 | } |
| 442 | |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 443 | static gboolean |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 444 | timeout_cb(gpointer data) |
| 445 | { |
| 446 | BalloonEval *beval = (BalloonEval *)data; |
| 447 | |
| 448 | beval->timerID = 0; |
| 449 | /* |
| 450 | * If the timer event happens then the mouse has stopped long enough for |
| 451 | * a request to be started. The request will only send to the debugger if |
| 452 | * there the mouse is pointing at real data. |
| 453 | */ |
| 454 | requestBalloon(beval); |
| 455 | |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 456 | return FALSE; // don't call me again |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 457 | } |
| 458 | |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 459 | # if GTK_CHECK_VERSION(3,0,0) |
| 460 | static gboolean |
| 461 | balloon_draw_event_cb(GtkWidget *widget, |
| 462 | cairo_t *cr, |
| 463 | gpointer data UNUSED) |
| 464 | { |
| 465 | GtkStyleContext *context = NULL; |
| 466 | gint width = -1, height = -1; |
| 467 | |
| 468 | if (widget == NULL) |
| 469 | return TRUE; |
| 470 | |
| 471 | context = gtk_widget_get_style_context(widget); |
| 472 | width = gtk_widget_get_allocated_width(widget); |
| 473 | height = gtk_widget_get_allocated_height(widget); |
| 474 | |
| 475 | gtk_style_context_save(context); |
| 476 | |
| 477 | gtk_style_context_add_class(context, "tooltip"); |
| 478 | gtk_style_context_set_state(context, GTK_STATE_FLAG_NORMAL); |
| 479 | |
| 480 | cairo_save(cr); |
| 481 | gtk_render_frame(context, cr, 0, 0, width, height); |
| 482 | gtk_render_background(context, cr, 0, 0, width, height); |
| 483 | cairo_restore(cr); |
| 484 | |
| 485 | gtk_style_context_restore(context); |
| 486 | |
| 487 | return FALSE; |
| 488 | } |
| 489 | # else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 490 | static gint |
Bram Moolenaar | b85cb21 | 2009-05-17 14:24:23 +0000 | [diff] [blame] | 491 | balloon_expose_event_cb(GtkWidget *widget, |
| 492 | GdkEventExpose *event, |
| 493 | gpointer data UNUSED) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 494 | { |
| 495 | gtk_paint_flat_box(widget->style, widget->window, |
| 496 | GTK_STATE_NORMAL, GTK_SHADOW_OUT, |
| 497 | &event->area, widget, "tooltip", |
| 498 | 0, 0, -1, -1); |
| 499 | |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 500 | return FALSE; // continue emission |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 501 | } |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 502 | # endif // !GTK_CHECK_VERSION(3,0,0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 503 | |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 504 | #else // !FEAT_GUI_GTK |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 505 | |
| 506 | static void |
Bram Moolenaar | 66f948e | 2016-01-30 16:39:25 +0100 | [diff] [blame] | 507 | addEventHandler(Widget target, BalloonEval *beval) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 508 | { |
| 509 | XtAddEventHandler(target, |
| 510 | PointerMotionMask | EnterWindowMask | |
| 511 | LeaveWindowMask | ButtonPressMask | KeyPressMask | |
| 512 | KeyReleaseMask, |
| 513 | False, |
| 514 | pointerEventEH, (XtPointer)beval); |
| 515 | } |
| 516 | |
| 517 | static void |
Bram Moolenaar | 66f948e | 2016-01-30 16:39:25 +0100 | [diff] [blame] | 518 | removeEventHandler(BalloonEval *beval) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 519 | { |
| 520 | XtRemoveEventHandler(beval->target, |
| 521 | PointerMotionMask | EnterWindowMask | |
| 522 | LeaveWindowMask | ButtonPressMask | KeyPressMask | |
| 523 | KeyReleaseMask, |
| 524 | False, |
| 525 | pointerEventEH, (XtPointer)beval); |
| 526 | } |
| 527 | |
| 528 | |
| 529 | /* |
| 530 | * The X event handler. All it does is call the real event handler. |
| 531 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 532 | static void |
Bram Moolenaar | 66f948e | 2016-01-30 16:39:25 +0100 | [diff] [blame] | 533 | pointerEventEH( |
| 534 | Widget w UNUSED, |
| 535 | XtPointer client_data, |
| 536 | XEvent *event, |
| 537 | Boolean *unused UNUSED) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 538 | { |
| 539 | BalloonEval *beval = (BalloonEval *)client_data; |
| 540 | pointerEvent(beval, event); |
| 541 | } |
| 542 | |
| 543 | |
| 544 | /* |
| 545 | * The real event handler. Called by pointerEventEH() whenever an event we are |
Bram Moolenaar | bae0c16 | 2007-05-10 19:30:25 +0000 | [diff] [blame] | 546 | * interested in occurs. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 547 | */ |
| 548 | |
| 549 | static void |
Bram Moolenaar | 66f948e | 2016-01-30 16:39:25 +0100 | [diff] [blame] | 550 | pointerEvent(BalloonEval *beval, XEvent *event) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 551 | { |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 552 | Position distance; // a measure of how much the pointer moved |
| 553 | Position delta; // used to compute distance |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 554 | |
| 555 | switch (event->type) |
| 556 | { |
| 557 | case EnterNotify: |
| 558 | case MotionNotify: |
| 559 | delta = event->xmotion.x - beval->x; |
| 560 | if (delta < 0) |
| 561 | delta = -delta; |
| 562 | distance = delta; |
| 563 | delta = event->xmotion.y - beval->y; |
| 564 | if (delta < 0) |
| 565 | delta = -delta; |
| 566 | distance += delta; |
| 567 | if (distance > 4) |
| 568 | { |
| 569 | /* |
| 570 | * Moved out of the balloon location: cancel it. |
| 571 | * Remember button state |
| 572 | */ |
| 573 | beval->state = event->xmotion.state; |
| 574 | if (beval->state & (Button1Mask|Button2Mask|Button3Mask)) |
| 575 | { |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 576 | // Mouse buttons are pressed - no balloon now |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 577 | cancelBalloon(beval); |
| 578 | } |
| 579 | else if (beval->state & (Mod1Mask|Mod2Mask|Mod3Mask)) |
| 580 | { |
| 581 | /* |
| 582 | * Alt is pressed -- enter super-evaluate-mode, |
| 583 | * where there is no time delay |
| 584 | */ |
| 585 | beval->x = event->xmotion.x; |
| 586 | beval->y = event->xmotion.y; |
| 587 | beval->x_root = event->xmotion.x_root; |
| 588 | beval->y_root = event->xmotion.y_root; |
| 589 | cancelBalloon(beval); |
| 590 | if (beval->msgCB != NULL) |
| 591 | { |
| 592 | beval->showState = ShS_PENDING; |
| 593 | (*beval->msgCB)(beval, beval->state); |
| 594 | } |
| 595 | } |
| 596 | else |
| 597 | { |
| 598 | beval->x = event->xmotion.x; |
| 599 | beval->y = event->xmotion.y; |
| 600 | beval->x_root = event->xmotion.x_root; |
| 601 | beval->y_root = event->xmotion.y_root; |
| 602 | cancelBalloon(beval); |
| 603 | beval->timerID = XtAppAddTimeOut( beval->appContext, |
| 604 | (long_u)p_bdlay, timerRoutine, beval); |
| 605 | } |
| 606 | } |
| 607 | break; |
| 608 | |
| 609 | /* |
Bram Moolenaar | 0b962e5 | 2022-04-03 18:02:37 +0100 | [diff] [blame] | 610 | * Motif version: Keystrokes will be caught by the |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 611 | * "textArea" widget, and handled in gui_x11_key_hit_cb(). |
| 612 | */ |
| 613 | case KeyPress: |
| 614 | if (beval->showState == ShS_SHOWING && beval->msgCB != NULL) |
| 615 | { |
| 616 | Modifiers modifier; |
| 617 | KeySym keysym; |
| 618 | |
| 619 | XtTranslateKeycode(gui.dpy, |
| 620 | event->xkey.keycode, event->xkey.state, |
| 621 | &modifier, &keysym); |
| 622 | if (keysym == XK_Shift_L || keysym == XK_Shift_R) |
| 623 | { |
| 624 | beval->showState = ShS_UPDATE_PENDING; |
| 625 | (*beval->msgCB)(beval, ShiftMask); |
| 626 | } |
| 627 | else if (keysym == XK_Control_L || keysym == XK_Control_R) |
| 628 | { |
| 629 | beval->showState = ShS_UPDATE_PENDING; |
| 630 | (*beval->msgCB)(beval, ControlMask); |
| 631 | } |
| 632 | else |
| 633 | cancelBalloon(beval); |
| 634 | } |
| 635 | else |
| 636 | cancelBalloon(beval); |
| 637 | break; |
| 638 | |
| 639 | case KeyRelease: |
| 640 | if (beval->showState == ShS_SHOWING && beval->msgCB != NULL) |
| 641 | { |
| 642 | Modifiers modifier; |
| 643 | KeySym keysym; |
| 644 | |
| 645 | XtTranslateKeycode(gui.dpy, event->xkey.keycode, |
| 646 | event->xkey.state, &modifier, &keysym); |
Bram Moolenaar | ebfec1c | 2023-01-22 21:14:53 +0000 | [diff] [blame] | 647 | if ((keysym == XK_Shift_L) || (keysym == XK_Shift_R)) |
| 648 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 649 | beval->showState = ShS_UPDATE_PENDING; |
| 650 | (*beval->msgCB)(beval, 0); |
| 651 | } |
| 652 | else if ((keysym == XK_Control_L) || (keysym == XK_Control_R)) |
| 653 | { |
| 654 | beval->showState = ShS_UPDATE_PENDING; |
| 655 | (*beval->msgCB)(beval, 0); |
| 656 | } |
| 657 | else |
| 658 | cancelBalloon(beval); |
| 659 | } |
| 660 | else |
| 661 | cancelBalloon(beval); |
| 662 | break; |
| 663 | |
| 664 | case LeaveNotify: |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 665 | // Ignore LeaveNotify events that are not "normal". |
| 666 | // Apparently we also get it when somebody else grabs focus. |
| 667 | // Happens for me every two seconds (some clipboard tool?) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 668 | if (event->xcrossing.mode == NotifyNormal) |
| 669 | cancelBalloon(beval); |
| 670 | break; |
| 671 | |
| 672 | case ButtonPress: |
| 673 | cancelBalloon(beval); |
| 674 | break; |
| 675 | |
| 676 | default: |
| 677 | break; |
| 678 | } |
| 679 | } |
| 680 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 681 | static void |
Bram Moolenaar | 66f948e | 2016-01-30 16:39:25 +0100 | [diff] [blame] | 682 | timerRoutine(XtPointer dx, XtIntervalId *id UNUSED) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 683 | { |
| 684 | BalloonEval *beval = (BalloonEval *)dx; |
| 685 | |
| 686 | beval->timerID = (XtIntervalId)NULL; |
| 687 | |
| 688 | /* |
| 689 | * If the timer event happens then the mouse has stopped long enough for |
| 690 | * a request to be started. The request will only send to the debugger if |
| 691 | * there the mouse is pointing at real data. |
| 692 | */ |
| 693 | requestBalloon(beval); |
| 694 | } |
| 695 | |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 696 | #endif // !FEAT_GUI_GTK |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 697 | |
| 698 | static void |
Bram Moolenaar | 66f948e | 2016-01-30 16:39:25 +0100 | [diff] [blame] | 699 | requestBalloon(BalloonEval *beval) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 700 | { |
Yegappan Lakshmanan | 7f8b255 | 2023-01-08 13:44:24 +0000 | [diff] [blame] | 701 | if (beval->showState == ShS_PENDING) |
| 702 | return; |
| 703 | |
| 704 | // Determine the beval to display |
| 705 | if (beval->msgCB != NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 706 | { |
Yegappan Lakshmanan | 7f8b255 | 2023-01-08 13:44:24 +0000 | [diff] [blame] | 707 | beval->showState = ShS_PENDING; |
| 708 | (*beval->msgCB)(beval, beval->state); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 709 | } |
Yegappan Lakshmanan | 7f8b255 | 2023-01-08 13:44:24 +0000 | [diff] [blame] | 710 | else if (beval->msg != NULL) |
| 711 | drawBalloon(beval); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 712 | } |
| 713 | |
| 714 | #ifdef FEAT_GUI_GTK |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 715 | /* |
| 716 | * Convert the string to UTF-8 if 'encoding' is not "utf-8". |
| 717 | * Replace any non-printable characters and invalid bytes sequences with |
| 718 | * "^X" or "<xx>" escapes, and apply SpecialKey highlighting to them. |
| 719 | * TAB and NL are passed through unscathed. |
| 720 | */ |
Bram Moolenaar | 182c5be | 2010-06-25 05:37:59 +0200 | [diff] [blame] | 721 | # define IS_NONPRINTABLE(c) (((c) < 0x20 && (c) != TAB && (c) != NL) \ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 722 | || (c) == DEL) |
| 723 | static void |
Bram Moolenaar | 89d4032 | 2006-08-29 15:30:07 +0000 | [diff] [blame] | 724 | set_printable_label_text(GtkLabel *label, char_u *text) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 725 | { |
| 726 | char_u *convbuf = NULL; |
| 727 | char_u *buf; |
| 728 | char_u *p; |
| 729 | char_u *pdest; |
| 730 | unsigned int len; |
| 731 | int charlen; |
| 732 | int uc; |
| 733 | PangoAttrList *attr_list; |
| 734 | |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 735 | // Convert to UTF-8 if it isn't already |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 736 | if (output_conv.vc_type != CONV_NONE) |
| 737 | { |
Bram Moolenaar | 89d4032 | 2006-08-29 15:30:07 +0000 | [diff] [blame] | 738 | convbuf = string_convert(&output_conv, text, NULL); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 739 | if (convbuf != NULL) |
Bram Moolenaar | 89d4032 | 2006-08-29 15:30:07 +0000 | [diff] [blame] | 740 | text = convbuf; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 741 | } |
| 742 | |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 743 | // First let's see how much we need to allocate |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 744 | len = 0; |
Bram Moolenaar | 89d4032 | 2006-08-29 15:30:07 +0000 | [diff] [blame] | 745 | for (p = text; *p != NUL; p += charlen) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 746 | { |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 747 | if ((*p & 0x80) == 0) // be quick for ASCII |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 748 | { |
| 749 | charlen = 1; |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 750 | len += IS_NONPRINTABLE(*p) ? 2 : 1; // nonprintable: ^X |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 751 | } |
| 752 | else |
| 753 | { |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 754 | charlen = utf_ptr2len(p); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 755 | uc = utf_ptr2char(p); |
| 756 | |
| 757 | if (charlen != utf_char2len(uc)) |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 758 | charlen = 1; // reject overlong sequences |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 759 | |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 760 | if (charlen == 1 || uc < 0xa0) // illegal byte or |
| 761 | len += 4; // control char: <xx> |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 762 | else if (!utf_printable(uc)) |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 763 | // Note: we assume here that utf_printable() doesn't |
| 764 | // care about characters outside the BMP. |
| 765 | len += 6; // nonprintable: <xxxx> |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 766 | else |
| 767 | len += charlen; |
| 768 | } |
| 769 | } |
| 770 | |
| 771 | attr_list = pango_attr_list_new(); |
| 772 | buf = alloc(len + 1); |
| 773 | |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 774 | // Now go for the real work |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 775 | if (buf != NULL) |
| 776 | { |
| 777 | attrentry_T *aep; |
| 778 | PangoAttribute *attr; |
| 779 | guicolor_T pixel; |
Bram Moolenaar | 36edf06 | 2016-07-21 22:10:12 +0200 | [diff] [blame] | 780 | #if GTK_CHECK_VERSION(3,0,0) |
| 781 | GdkRGBA color = { 0.0, 0.0, 0.0, 1.0 }; |
Bram Moolenaar | 870b749 | 2016-07-22 22:26:52 +0200 | [diff] [blame] | 782 | # if PANGO_VERSION_CHECK(1,38,0) |
Bram Moolenaar | 36edf06 | 2016-07-21 22:10:12 +0200 | [diff] [blame] | 783 | PangoAttribute *attr_alpha; |
Bram Moolenaar | 870b749 | 2016-07-22 22:26:52 +0200 | [diff] [blame] | 784 | # endif |
Bram Moolenaar | 36edf06 | 2016-07-21 22:10:12 +0200 | [diff] [blame] | 785 | #else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 786 | GdkColor color = { 0, 0, 0, 0 }; |
Bram Moolenaar | 36edf06 | 2016-07-21 22:10:12 +0200 | [diff] [blame] | 787 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 788 | |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 789 | // Look up the RGB values of the SpecialKey foreground color. |
Bram Moolenaar | 8820b48 | 2017-03-16 17:23:31 +0100 | [diff] [blame] | 790 | aep = syn_gui_attr2entry(HL_ATTR(HLF_8)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 791 | pixel = (aep != NULL) ? aep->ae_u.gui.fg_color : INVALCOLOR; |
| 792 | if (pixel != INVALCOLOR) |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 793 | # if GTK_CHECK_VERSION(3,0,0) |
| 794 | { |
Bram Moolenaar | 36edf06 | 2016-07-21 22:10:12 +0200 | [diff] [blame] | 795 | color.red = ((pixel & 0xff0000) >> 16) / 255.0; |
| 796 | color.green = ((pixel & 0xff00) >> 8) / 255.0; |
| 797 | color.blue = (pixel & 0xff) / 255.0; |
| 798 | color.alpha = 1.0; |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 799 | } |
| 800 | # else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 801 | gdk_colormap_query_color(gtk_widget_get_colormap(gui.drawarea), |
| 802 | (unsigned long)pixel, &color); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 803 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 804 | |
| 805 | pdest = buf; |
Bram Moolenaar | 89d4032 | 2006-08-29 15:30:07 +0000 | [diff] [blame] | 806 | p = text; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 807 | while (*p != NUL) |
| 808 | { |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 809 | // Be quick for ASCII |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 810 | if ((*p & 0x80) == 0 && !IS_NONPRINTABLE(*p)) |
| 811 | { |
| 812 | *pdest++ = *p++; |
| 813 | } |
| 814 | else |
| 815 | { |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 816 | charlen = utf_ptr2len(p); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 817 | uc = utf_ptr2char(p); |
| 818 | |
| 819 | if (charlen != utf_char2len(uc)) |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 820 | charlen = 1; // reject overlong sequences |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 821 | |
| 822 | if (charlen == 1 || uc < 0xa0 || !utf_printable(uc)) |
| 823 | { |
| 824 | int outlen; |
| 825 | |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 826 | // Careful: we can't just use transchar_byte() here, |
| 827 | // since 'encoding' is not necessarily set to "utf-8". |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 828 | if (*p & 0x80 && charlen == 1) |
| 829 | { |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 830 | transchar_hex(pdest, *p); // <xx> |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 831 | outlen = 4; |
| 832 | } |
| 833 | else if (uc >= 0x80) |
| 834 | { |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 835 | // Note: we assume here that utf_printable() doesn't |
| 836 | // care about characters outside the BMP. |
| 837 | transchar_hex(pdest, uc); // <xx> or <xxxx> |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 838 | outlen = (uc < 0x100) ? 4 : 6; |
| 839 | } |
| 840 | else |
| 841 | { |
Bram Moolenaar | 32ee627 | 2020-06-10 14:16:49 +0200 | [diff] [blame] | 842 | transchar_nonprint(curbuf, pdest, *p); // ^X |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 843 | outlen = 2; |
| 844 | } |
| 845 | if (pixel != INVALCOLOR) |
| 846 | { |
Bram Moolenaar | 36edf06 | 2016-07-21 22:10:12 +0200 | [diff] [blame] | 847 | #if GTK_CHECK_VERSION(3,0,0) |
| 848 | # define DOUBLE2UINT16(val) ((guint16)((val) * 65535 + 0.5)) |
| 849 | attr = pango_attr_foreground_new( |
| 850 | DOUBLE2UINT16(color.red), |
| 851 | DOUBLE2UINT16(color.green), |
| 852 | DOUBLE2UINT16(color.blue)); |
Bram Moolenaar | 870b749 | 2016-07-22 22:26:52 +0200 | [diff] [blame] | 853 | # if PANGO_VERSION_CHECK(1,38,0) |
Bram Moolenaar | 36edf06 | 2016-07-21 22:10:12 +0200 | [diff] [blame] | 854 | attr_alpha = pango_attr_foreground_alpha_new( |
| 855 | DOUBLE2UINT16(color.alpha)); |
Bram Moolenaar | 870b749 | 2016-07-22 22:26:52 +0200 | [diff] [blame] | 856 | # endif |
Bram Moolenaar | 36edf06 | 2016-07-21 22:10:12 +0200 | [diff] [blame] | 857 | # undef DOUBLE2UINT16 |
| 858 | #else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 859 | attr = pango_attr_foreground_new( |
| 860 | color.red, color.green, color.blue); |
Bram Moolenaar | 36edf06 | 2016-07-21 22:10:12 +0200 | [diff] [blame] | 861 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 862 | attr->start_index = pdest - buf; |
| 863 | attr->end_index = pdest - buf + outlen; |
| 864 | pango_attr_list_insert(attr_list, attr); |
Bram Moolenaar | 36edf06 | 2016-07-21 22:10:12 +0200 | [diff] [blame] | 865 | #if GTK_CHECK_VERSION(3,0,0) |
Bram Moolenaar | 870b749 | 2016-07-22 22:26:52 +0200 | [diff] [blame] | 866 | # if PANGO_VERSION_CHECK(1,38,0) |
Bram Moolenaar | 36edf06 | 2016-07-21 22:10:12 +0200 | [diff] [blame] | 867 | attr_alpha->start_index = pdest - buf; |
| 868 | attr_alpha->end_index = pdest - buf + outlen; |
| 869 | pango_attr_list_insert(attr_list, attr_alpha); |
Bram Moolenaar | 870b749 | 2016-07-22 22:26:52 +0200 | [diff] [blame] | 870 | # endif |
Bram Moolenaar | 36edf06 | 2016-07-21 22:10:12 +0200 | [diff] [blame] | 871 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 872 | } |
| 873 | pdest += outlen; |
| 874 | p += charlen; |
| 875 | } |
| 876 | else |
| 877 | { |
| 878 | do |
| 879 | *pdest++ = *p++; |
| 880 | while (--charlen != 0); |
| 881 | } |
| 882 | } |
| 883 | } |
| 884 | *pdest = NUL; |
| 885 | } |
| 886 | |
| 887 | vim_free(convbuf); |
| 888 | |
| 889 | gtk_label_set_text(label, (const char *)buf); |
| 890 | vim_free(buf); |
| 891 | |
| 892 | gtk_label_set_attributes(label, attr_list); |
| 893 | pango_attr_list_unref(attr_list); |
| 894 | } |
Bram Moolenaar | 182c5be | 2010-06-25 05:37:59 +0200 | [diff] [blame] | 895 | # undef IS_NONPRINTABLE |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 896 | |
| 897 | /* |
| 898 | * Draw a balloon. |
| 899 | */ |
| 900 | static void |
| 901 | drawBalloon(BalloonEval *beval) |
| 902 | { |
Yegappan Lakshmanan | 7f8b255 | 2023-01-08 13:44:24 +0000 | [diff] [blame] | 903 | if (beval->msg == NULL) |
| 904 | return; |
| 905 | |
| 906 | GtkRequisition requisition; |
| 907 | int screen_w; |
| 908 | int screen_h; |
| 909 | int screen_x; |
| 910 | int screen_y; |
| 911 | int x; |
| 912 | int y; |
| 913 | int x_offset = EVAL_OFFSET_X; |
| 914 | int y_offset = EVAL_OFFSET_Y; |
| 915 | PangoLayout *layout; |
Bram Moolenaar | a859f04 | 2016-11-17 19:11:55 +0100 | [diff] [blame] | 916 | |
Bram Moolenaar | 7be9b50 | 2017-09-09 18:45:26 +0200 | [diff] [blame] | 917 | # if !GTK_CHECK_VERSION(3,22,2) |
Yegappan Lakshmanan | 7f8b255 | 2023-01-08 13:44:24 +0000 | [diff] [blame] | 918 | GdkScreen *screen; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 919 | |
Yegappan Lakshmanan | 7f8b255 | 2023-01-08 13:44:24 +0000 | [diff] [blame] | 920 | screen = gtk_widget_get_screen(beval->target); |
| 921 | gtk_window_set_screen(GTK_WINDOW(beval->balloonShell), screen); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 922 | # endif |
Yegappan Lakshmanan | 7f8b255 | 2023-01-08 13:44:24 +0000 | [diff] [blame] | 923 | gui_gtk_get_screen_geom_of_win(beval->target, 0, 0, |
| 924 | &screen_x, &screen_y, &screen_w, &screen_h); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 925 | # if !GTK_CHECK_VERSION(3,0,0) |
Yegappan Lakshmanan | 7f8b255 | 2023-01-08 13:44:24 +0000 | [diff] [blame] | 926 | gtk_widget_ensure_style(beval->balloonShell); |
| 927 | gtk_widget_ensure_style(beval->balloonLabel); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 928 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 929 | |
Yegappan Lakshmanan | 7f8b255 | 2023-01-08 13:44:24 +0000 | [diff] [blame] | 930 | set_printable_label_text(GTK_LABEL(beval->balloonLabel), beval->msg); |
| 931 | /* |
| 932 | * Dirty trick: Enable wrapping mode on the label's layout behind its |
| 933 | * back. This way GtkLabel won't try to constrain the wrap width to a |
| 934 | * builtin maximum value of about 65 Latin characters. |
| 935 | */ |
| 936 | layout = gtk_label_get_layout(GTK_LABEL(beval->balloonLabel)); |
Bram Moolenaar | 182c5be | 2010-06-25 05:37:59 +0200 | [diff] [blame] | 937 | # ifdef PANGO_WRAP_WORD_CHAR |
Yegappan Lakshmanan | 7f8b255 | 2023-01-08 13:44:24 +0000 | [diff] [blame] | 938 | pango_layout_set_wrap(layout, PANGO_WRAP_WORD_CHAR); |
Bram Moolenaar | 182c5be | 2010-06-25 05:37:59 +0200 | [diff] [blame] | 939 | # else |
Yegappan Lakshmanan | 7f8b255 | 2023-01-08 13:44:24 +0000 | [diff] [blame] | 940 | pango_layout_set_wrap(layout, PANGO_WRAP_WORD); |
Bram Moolenaar | 182c5be | 2010-06-25 05:37:59 +0200 | [diff] [blame] | 941 | # endif |
Yegappan Lakshmanan | 7f8b255 | 2023-01-08 13:44:24 +0000 | [diff] [blame] | 942 | pango_layout_set_width(layout, |
| 943 | // try to come up with some reasonable width |
| 944 | PANGO_SCALE * CLAMP(gui.num_cols * gui.char_width, |
| 945 | screen_w / 2, |
| 946 | MAX(20, screen_w - 20))); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 947 | |
Yegappan Lakshmanan | 7f8b255 | 2023-01-08 13:44:24 +0000 | [diff] [blame] | 948 | // Calculate the balloon's width and height. |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 949 | # if GTK_CHECK_VERSION(3,0,0) |
Yegappan Lakshmanan | 7f8b255 | 2023-01-08 13:44:24 +0000 | [diff] [blame] | 950 | gtk_widget_get_preferred_size(beval->balloonShell, &requisition, NULL); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 951 | # else |
Yegappan Lakshmanan | 7f8b255 | 2023-01-08 13:44:24 +0000 | [diff] [blame] | 952 | gtk_widget_size_request(beval->balloonShell, &requisition); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 953 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 954 | |
Yegappan Lakshmanan | 7f8b255 | 2023-01-08 13:44:24 +0000 | [diff] [blame] | 955 | // Compute position of the balloon area |
| 956 | gdk_window_get_origin(gtk_widget_get_window(beval->target), &x, &y); |
| 957 | x += beval->x; |
| 958 | y += beval->y; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 959 | |
Yegappan Lakshmanan | 7f8b255 | 2023-01-08 13:44:24 +0000 | [diff] [blame] | 960 | // Get out of the way of the mouse pointer |
| 961 | if (x + x_offset + requisition.width > screen_x + screen_w) |
| 962 | y_offset += 15; |
| 963 | if (y + y_offset + requisition.height > screen_y + screen_h) |
| 964 | y_offset = -requisition.height - EVAL_OFFSET_Y; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 965 | |
Yegappan Lakshmanan | 7f8b255 | 2023-01-08 13:44:24 +0000 | [diff] [blame] | 966 | // Sanitize values |
| 967 | x = CLAMP(x + x_offset, 0, |
| 968 | MAX(0, screen_x + screen_w - requisition.width)); |
| 969 | y = CLAMP(y + y_offset, 0, |
| 970 | MAX(0, screen_y + screen_h - requisition.height)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 971 | |
Yegappan Lakshmanan | 7f8b255 | 2023-01-08 13:44:24 +0000 | [diff] [blame] | 972 | // Show the balloon |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 973 | # if GTK_CHECK_VERSION(3,0,0) |
Yegappan Lakshmanan | 7f8b255 | 2023-01-08 13:44:24 +0000 | [diff] [blame] | 974 | gtk_window_move(GTK_WINDOW(beval->balloonShell), x, y); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 975 | # else |
Yegappan Lakshmanan | 7f8b255 | 2023-01-08 13:44:24 +0000 | [diff] [blame] | 976 | gtk_widget_set_uposition(beval->balloonShell, x, y); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 977 | # endif |
Yegappan Lakshmanan | 7f8b255 | 2023-01-08 13:44:24 +0000 | [diff] [blame] | 978 | gtk_widget_show(beval->balloonShell); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 979 | |
Yegappan Lakshmanan | 7f8b255 | 2023-01-08 13:44:24 +0000 | [diff] [blame] | 980 | beval->showState = ShS_SHOWING; |
| 981 | gui_mch_update(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 982 | } |
| 983 | |
| 984 | /* |
| 985 | * Undraw a balloon. |
| 986 | */ |
| 987 | static void |
| 988 | undrawBalloon(BalloonEval *beval) |
| 989 | { |
| 990 | if (beval->balloonShell != NULL) |
| 991 | gtk_widget_hide(beval->balloonShell); |
| 992 | beval->showState = ShS_NEUTRAL; |
| 993 | } |
| 994 | |
| 995 | static void |
| 996 | cancelBalloon(BalloonEval *beval) |
| 997 | { |
| 998 | if (beval->showState == ShS_SHOWING |
| 999 | || beval->showState == ShS_UPDATE_PENDING) |
| 1000 | undrawBalloon(beval); |
| 1001 | |
| 1002 | if (beval->timerID != 0) |
| 1003 | { |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 1004 | g_source_remove(beval->timerID); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1005 | beval->timerID = 0; |
| 1006 | } |
| 1007 | beval->showState = ShS_NEUTRAL; |
| 1008 | } |
| 1009 | |
| 1010 | static void |
| 1011 | createBalloonEvalWindow(BalloonEval *beval) |
| 1012 | { |
| 1013 | beval->balloonShell = gtk_window_new(GTK_WINDOW_POPUP); |
lilydjwg | 94ff09a | 2024-01-29 20:14:01 +0100 | [diff] [blame] | 1014 | gtk_window_set_transient_for(GTK_WINDOW(beval->balloonShell), GTK_WINDOW(gui.mainwin)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1015 | |
| 1016 | gtk_widget_set_app_paintable(beval->balloonShell, TRUE); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 1017 | gtk_window_set_resizable(GTK_WINDOW(beval->balloonShell), FALSE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1018 | gtk_widget_set_name(beval->balloonShell, "gtk-tooltips"); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 1019 | gtk_container_set_border_width(GTK_CONTAINER(beval->balloonShell), 4); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1020 | |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 1021 | # if GTK_CHECK_VERSION(3,0,0) |
| 1022 | g_signal_connect(G_OBJECT(beval->balloonShell), "draw", |
| 1023 | G_CALLBACK(balloon_draw_event_cb), NULL); |
| 1024 | # else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1025 | gtk_signal_connect((GtkObject*)(beval->balloonShell), "expose_event", |
| 1026 | GTK_SIGNAL_FUNC(balloon_expose_event_cb), NULL); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 1027 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1028 | beval->balloonLabel = gtk_label_new(NULL); |
| 1029 | |
| 1030 | gtk_label_set_line_wrap(GTK_LABEL(beval->balloonLabel), FALSE); |
| 1031 | gtk_label_set_justify(GTK_LABEL(beval->balloonLabel), GTK_JUSTIFY_LEFT); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 1032 | # if GTK_CHECK_VERSION(3,16,0) |
| 1033 | gtk_label_set_xalign(GTK_LABEL(beval->balloonLabel), 0.5); |
| 1034 | gtk_label_set_yalign(GTK_LABEL(beval->balloonLabel), 0.5); |
| 1035 | # elif GTK_CHECK_VERSION(3,14,0) |
| 1036 | GValue align_val = G_VALUE_INIT; |
| 1037 | g_value_init(&align_val, G_TYPE_FLOAT); |
| 1038 | g_value_set_float(&align_val, 0.5); |
| 1039 | g_object_set_property(G_OBJECT(beval->balloonLabel), "xalign", &align_val); |
| 1040 | g_object_set_property(G_OBJECT(beval->balloonLabel), "yalign", &align_val); |
| 1041 | g_value_unset(&align_val); |
| 1042 | # else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1043 | gtk_misc_set_alignment(GTK_MISC(beval->balloonLabel), 0.5f, 0.5f); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 1044 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1045 | gtk_widget_set_name(beval->balloonLabel, "vim-balloon-label"); |
| 1046 | gtk_widget_show(beval->balloonLabel); |
| 1047 | |
| 1048 | gtk_container_add(GTK_CONTAINER(beval->balloonShell), beval->balloonLabel); |
| 1049 | } |
| 1050 | |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 1051 | #else // !FEAT_GUI_GTK |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1052 | |
| 1053 | /* |
| 1054 | * Draw a balloon. |
| 1055 | */ |
| 1056 | static void |
Bram Moolenaar | 66f948e | 2016-01-30 16:39:25 +0100 | [diff] [blame] | 1057 | drawBalloon(BalloonEval *beval) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1058 | { |
| 1059 | Dimension w; |
| 1060 | Dimension h; |
| 1061 | Position tx; |
| 1062 | Position ty; |
| 1063 | |
Yegappan Lakshmanan | 7f8b255 | 2023-01-08 13:44:24 +0000 | [diff] [blame] | 1064 | if (beval->msg == NULL) |
| 1065 | return; |
| 1066 | |
| 1067 | XmString s; |
| 1068 | // Show the Balloon |
| 1069 | |
| 1070 | // Calculate the label's width and height |
| 1071 | |
| 1072 | // For the callback function we parse NL characters to create a |
| 1073 | // multi-line label. This doesn't work for all languages, but |
| 1074 | // XmStringCreateLocalized() doesn't do multi-line labels... |
| 1075 | if (beval->msgCB != NULL) |
| 1076 | s = XmStringCreateLtoR((char *)beval->msg, XmFONTLIST_DEFAULT_TAG); |
| 1077 | else |
| 1078 | s = XmStringCreateLocalized((char *)beval->msg); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1079 | { |
Yegappan Lakshmanan | 7f8b255 | 2023-01-08 13:44:24 +0000 | [diff] [blame] | 1080 | XmFontList fl; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1081 | |
Yegappan Lakshmanan | 7f8b255 | 2023-01-08 13:44:24 +0000 | [diff] [blame] | 1082 | fl = gui_motif_fontset2fontlist(&gui.tooltip_fontset); |
| 1083 | if (fl == NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1084 | { |
Yegappan Lakshmanan | 7f8b255 | 2023-01-08 13:44:24 +0000 | [diff] [blame] | 1085 | XmStringFree(s); |
| 1086 | return; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1087 | } |
Yegappan Lakshmanan | 7f8b255 | 2023-01-08 13:44:24 +0000 | [diff] [blame] | 1088 | XmStringExtent(fl, s, &w, &h); |
| 1089 | XmFontListFree(fl); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1090 | } |
Yegappan Lakshmanan | 7f8b255 | 2023-01-08 13:44:24 +0000 | [diff] [blame] | 1091 | w += gui.border_offset << 1; |
| 1092 | h += gui.border_offset << 1; |
| 1093 | XtVaSetValues(beval->balloonLabel, XmNlabelString, s, NULL); |
| 1094 | XmStringFree(s); |
| 1095 | |
| 1096 | // Compute position of the balloon area |
| 1097 | tx = beval->x_root + EVAL_OFFSET_X; |
| 1098 | ty = beval->y_root + EVAL_OFFSET_Y; |
| 1099 | if ((tx + w) > beval->screen_width) |
| 1100 | tx = beval->screen_width - w; |
| 1101 | if ((ty + h) > beval->screen_height) |
| 1102 | ty = beval->screen_height - h; |
| 1103 | XtVaSetValues(beval->balloonShell, |
| 1104 | XmNx, tx, |
| 1105 | XmNy, ty, |
| 1106 | NULL); |
| 1107 | // Set tooltip colors |
| 1108 | { |
| 1109 | Arg args[2]; |
| 1110 | |
| 1111 | args[0].name = XmNbackground; |
| 1112 | args[0].value = gui.tooltip_bg_pixel; |
| 1113 | args[1].name = XmNforeground; |
| 1114 | args[1].value = gui.tooltip_fg_pixel; |
| 1115 | XtSetValues(beval->balloonLabel, &args[0], XtNumber(args)); |
| 1116 | } |
| 1117 | |
| 1118 | XtPopup(beval->balloonShell, XtGrabNone); |
| 1119 | |
| 1120 | beval->showState = ShS_SHOWING; |
| 1121 | |
| 1122 | current_beval = beval; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1123 | } |
| 1124 | |
| 1125 | /* |
| 1126 | * Undraw a balloon. |
| 1127 | */ |
| 1128 | static void |
Bram Moolenaar | 66f948e | 2016-01-30 16:39:25 +0100 | [diff] [blame] | 1129 | undrawBalloon(BalloonEval *beval) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1130 | { |
| 1131 | if (beval->balloonShell != (Widget)0) |
| 1132 | XtPopdown(beval->balloonShell); |
| 1133 | beval->showState = ShS_NEUTRAL; |
| 1134 | |
| 1135 | current_beval = NULL; |
| 1136 | } |
| 1137 | |
| 1138 | static void |
Bram Moolenaar | 66f948e | 2016-01-30 16:39:25 +0100 | [diff] [blame] | 1139 | cancelBalloon(BalloonEval *beval) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1140 | { |
| 1141 | if (beval->showState == ShS_SHOWING |
| 1142 | || beval->showState == ShS_UPDATE_PENDING) |
| 1143 | undrawBalloon(beval); |
| 1144 | |
| 1145 | if (beval->timerID != (XtIntervalId)NULL) |
| 1146 | { |
| 1147 | XtRemoveTimeOut(beval->timerID); |
| 1148 | beval->timerID = (XtIntervalId)NULL; |
| 1149 | } |
| 1150 | beval->showState = ShS_NEUTRAL; |
| 1151 | } |
| 1152 | |
| 1153 | |
| 1154 | static void |
Bram Moolenaar | 66f948e | 2016-01-30 16:39:25 +0100 | [diff] [blame] | 1155 | createBalloonEvalWindow(BalloonEval *beval) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1156 | { |
| 1157 | Arg args[12]; |
| 1158 | int n; |
| 1159 | |
| 1160 | n = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1161 | XtSetArg(args[n], XmNallowShellResize, True); n++; |
| 1162 | beval->balloonShell = XtAppCreateShell("balloonEval", "BalloonEval", |
| 1163 | overrideShellWidgetClass, gui.dpy, args, n); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1164 | |
Yegappan Lakshmanan | 7f8b255 | 2023-01-08 13:44:24 +0000 | [diff] [blame] | 1165 | XmFontList fl; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1166 | |
Yegappan Lakshmanan | 7f8b255 | 2023-01-08 13:44:24 +0000 | [diff] [blame] | 1167 | n = 0; |
| 1168 | fl = gui_motif_fontset2fontlist(&gui.tooltip_fontset); |
| 1169 | XtSetArg(args[n], XmNforeground, gui.tooltip_fg_pixel); n++; |
| 1170 | XtSetArg(args[n], XmNbackground, gui.tooltip_bg_pixel); n++; |
| 1171 | XtSetArg(args[n], XmNfontList, fl); n++; |
| 1172 | XtSetArg(args[n], XmNalignment, XmALIGNMENT_BEGINNING); n++; |
| 1173 | beval->balloonLabel = XtCreateManagedWidget("balloonLabel", |
| 1174 | xmLabelWidgetClass, beval->balloonShell, args, n); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1175 | } |
| 1176 | |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 1177 | #endif // !FEAT_GUI_GTK |
| 1178 | #endif // !FEAT_GUI_MSWIN |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1179 | |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 1180 | #endif // FEAT_BEVAL_GUI |