blob: 4d2a6ae710316f4e2c4a42de9f194c3aa9441fa9 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * Porting to GTK+ was done by:
12 *
Bram Moolenaar0a56cb82005-01-04 21:45:14 +000013 * (C) 1998,1999,2000 by Marcin Dalecki <martin@dalecki.de>
Bram Moolenaar071d4272004-06-13 20:20:40 +000014 *
15 * With GREAT support and continuous encouragements by Andy Kahn and of
16 * course Bram Moolenaar!
17 *
18 * Support for GTK+ 2 was added by:
19 *
20 * (C) 2002,2003 Jason Hildebrand <jason@peaceworks.ca>
21 * Daniel Elstner <daniel.elstner@gmx.net>
Bram Moolenaar98921892016-02-23 17:14:37 +010022 *
23 * Support for GTK+ 3 was added by:
24 *
25 * 2016 Kazunobu Kuriyama <kazunobu.kuriyama@gmail.com>
Bram Moolenaar071d4272004-06-13 20:20:40 +000026 */
27
28#include "vim.h"
Bram Moolenaar36e294c2015-12-29 18:55:46 +010029#ifdef USE_GRESOURCE
30#include "auto/gui_gtk_gresources.h"
31#endif
Bram Moolenaarc93e7912008-07-08 10:46:08 +000032
Bram Moolenaar071d4272004-06-13 20:20:40 +000033#ifdef FEAT_GUI_GNOME
Bram Moolenaar30613902019-12-01 22:11:18 +010034// Gnome redefines _() and N_(). Grrr...
Bram Moolenaar071d4272004-06-13 20:20:40 +000035# ifdef _
36# undef _
37# endif
38# ifdef N_
39# undef N_
40# endif
41# ifdef textdomain
42# undef textdomain
43# endif
44# ifdef bindtextdomain
45# undef bindtextdomain
46# endif
Bram Moolenaara2dd9002007-05-14 17:38:30 +000047# ifdef bind_textdomain_codeset
48# undef bind_textdomain_codeset
Bram Moolenaar49325942007-05-10 19:19:59 +000049# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000050# if defined(FEAT_GETTEXT) && !defined(ENABLE_NLS)
Bram Moolenaar30613902019-12-01 22:11:18 +010051# define ENABLE_NLS // so the texts in the dialog boxes are translated
Bram Moolenaar071d4272004-06-13 20:20:40 +000052# endif
53# include <gnome.h>
54# include "version.h"
Bram Moolenaar30613902019-12-01 22:11:18 +010055// missing prototype in bonobo-dock-item.h
Bram Moolenaardb552d602006-03-23 22:59:57 +000056extern void bonobo_dock_item_set_behavior(BonoboDockItem *dock_item, BonoboDockItemBehavior beh);
Bram Moolenaar071d4272004-06-13 20:20:40 +000057#endif
58
59#if !defined(FEAT_GUI_GTK) && defined(PROTO)
Bram Moolenaar30613902019-12-01 22:11:18 +010060// When generating prototypes we don't want syntax errors.
Bram Moolenaar071d4272004-06-13 20:20:40 +000061# define GdkAtom int
62# define GdkEventExpose int
63# define GdkEventFocus int
64# define GdkEventVisibility int
65# define GdkEventProperty int
66# define GtkContainer int
67# define GtkTargetEntry int
68# define GtkType int
69# define GtkWidget int
70# define gint int
71# define gpointer int
72# define guint int
73# define GdkEventKey int
74# define GdkEventSelection int
75# define GtkSelectionData int
76# define GdkEventMotion int
77# define GdkEventButton int
78# define GdkDragContext int
79# define GdkEventConfigure int
80# define GdkEventClient int
81#else
Bram Moolenaar98921892016-02-23 17:14:37 +010082# if GTK_CHECK_VERSION(3,0,0)
83# include <gdk/gdkkeysyms-compat.h>
84# include <gtk/gtkx.h>
85# else
86# include <gdk/gdkkeysyms.h>
87# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000088# include <gdk/gdk.h>
Bram Moolenaar4f974752019-02-17 17:44:42 +010089# ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +000090# include <gdk/gdkwin32.h>
91# else
92# include <gdk/gdkx.h>
93# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000094# include <gtk/gtk.h>
95# include "gui_gtk_f.h"
96#endif
97
98#ifdef HAVE_X11_SUNKEYSYM_H
99# include <X11/Sunkeysym.h>
100#endif
101
102/*
103 * Easy-to-use macro for multihead support.
104 */
Bram Moolenaarb463e8d2017-06-05 15:07:09 +0200105#define GET_X_ATOM(atom) gdk_x11_atom_to_xatom_for_display( \
Bram Moolenaar071d4272004-06-13 20:20:40 +0000106 gtk_widget_get_display(gui.mainwin), atom)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000107
Bram Moolenaar30613902019-12-01 22:11:18 +0100108// Selection type distinguishers
Bram Moolenaar071d4272004-06-13 20:20:40 +0000109enum
110{
111 TARGET_TYPE_NONE,
112 TARGET_UTF8_STRING,
113 TARGET_STRING,
114 TARGET_COMPOUND_TEXT,
Bram Moolenaar3a6eaa52009-06-16 13:23:06 +0000115 TARGET_HTML,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000116 TARGET_TEXT,
117 TARGET_TEXT_URI_LIST,
118 TARGET_TEXT_PLAIN,
lilydjwg94ff09a2024-01-29 20:14:01 +0100119 TARGET_TEXT_PLAIN_UTF8,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000120 TARGET_VIM,
121 TARGET_VIMENC
122};
123
124/*
125 * Table of selection targets supported by Vim.
126 * Note: Order matters, preferred types should come first.
127 */
128static const GtkTargetEntry selection_targets[] =
129{
130 {VIMENC_ATOM_NAME, 0, TARGET_VIMENC},
131 {VIM_ATOM_NAME, 0, TARGET_VIM},
Bram Moolenaar3a6eaa52009-06-16 13:23:06 +0000132 {"text/html", 0, TARGET_HTML},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000133 {"UTF8_STRING", 0, TARGET_UTF8_STRING},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000134 {"COMPOUND_TEXT", 0, TARGET_COMPOUND_TEXT},
135 {"TEXT", 0, TARGET_TEXT},
lilydjwg94ff09a2024-01-29 20:14:01 +0100136 {"STRING", 0, TARGET_STRING},
137 {"text/plain;charset=utf-8", 0, TARGET_TEXT_PLAIN_UTF8},
138 {"text/plain", 0, TARGET_TEXT_PLAIN}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000139};
K.Takataeeec2542021-06-02 13:28:16 +0200140#define N_SELECTION_TARGETS ARRAY_LENGTH(selection_targets)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000141
142#ifdef FEAT_DND
143/*
144 * Table of DnD targets supported by Vim.
145 * Note: Order matters, preferred types should come first.
146 */
147static const GtkTargetEntry dnd_targets[] =
148{
149 {"text/uri-list", 0, TARGET_TEXT_URI_LIST},
Bram Moolenaar3a6eaa52009-06-16 13:23:06 +0000150 {"text/html", 0, TARGET_HTML},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000151 {"UTF8_STRING", 0, TARGET_UTF8_STRING},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000152 {"STRING", 0, TARGET_STRING},
153 {"text/plain", 0, TARGET_TEXT_PLAIN}
154};
K.Takataeeec2542021-06-02 13:28:16 +0200155# define N_DND_TARGETS ARRAY_LENGTH(dnd_targets)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000156#endif
157
158
Bram Moolenaar071d4272004-06-13 20:20:40 +0000159/*
160 * "Monospace" is a standard font alias that should be present
161 * on all proper Pango/fontconfig installations.
162 */
163# define DEFAULT_FONT "Monospace 10"
164
Bram Moolenaarf96ae0b2019-07-28 15:21:55 +0200165#if defined(FEAT_GUI_GNOME) && defined(FEAT_SESSION)
166# define USE_GNOME_SESSION
167#endif
168
169#if !defined(FEAT_GUI_GNOME)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000170/*
171 * Atoms used to communicate save-yourself from the X11 session manager. There
172 * is no need to move them into the GUI struct, since they should be constant.
173 */
174static GdkAtom wm_protocols_atom = GDK_NONE;
175static GdkAtom save_yourself_atom = GDK_NONE;
176#endif
177
178/*
179 * Atoms used to control/reference X11 selections.
180 */
Bram Moolenaar3a6eaa52009-06-16 13:23:06 +0000181static GdkAtom html_atom = GDK_NONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000182static GdkAtom utf8_string_atom = GDK_NONE;
Bram Moolenaar30613902019-12-01 22:11:18 +0100183static GdkAtom vim_atom = GDK_NONE; // Vim's own special selection format
184static GdkAtom vimenc_atom = GDK_NONE; // Vim's extended selection format
Bram Moolenaar071d4272004-06-13 20:20:40 +0000185
186/*
187 * Keycodes recognized by vim.
188 * NOTE: when changing this, the table in gui_x11.c probably needs the same
189 * change!
190 */
191static struct special_key
192{
193 guint key_sym;
194 char_u code0;
195 char_u code1;
196}
197const special_keys[] =
198{
199 {GDK_Up, 'k', 'u'},
200 {GDK_Down, 'k', 'd'},
201 {GDK_Left, 'k', 'l'},
202 {GDK_Right, 'k', 'r'},
203 {GDK_F1, 'k', '1'},
204 {GDK_F2, 'k', '2'},
205 {GDK_F3, 'k', '3'},
206 {GDK_F4, 'k', '4'},
207 {GDK_F5, 'k', '5'},
208 {GDK_F6, 'k', '6'},
209 {GDK_F7, 'k', '7'},
210 {GDK_F8, 'k', '8'},
211 {GDK_F9, 'k', '9'},
212 {GDK_F10, 'k', ';'},
213 {GDK_F11, 'F', '1'},
214 {GDK_F12, 'F', '2'},
215 {GDK_F13, 'F', '3'},
216 {GDK_F14, 'F', '4'},
217 {GDK_F15, 'F', '5'},
218 {GDK_F16, 'F', '6'},
219 {GDK_F17, 'F', '7'},
220 {GDK_F18, 'F', '8'},
221 {GDK_F19, 'F', '9'},
222 {GDK_F20, 'F', 'A'},
223 {GDK_F21, 'F', 'B'},
Bram Moolenaar30613902019-12-01 22:11:18 +0100224 {GDK_Pause, 'F', 'B'}, // Pause == F21 according to netbeans.txt
Bram Moolenaar071d4272004-06-13 20:20:40 +0000225 {GDK_F22, 'F', 'C'},
226 {GDK_F23, 'F', 'D'},
227 {GDK_F24, 'F', 'E'},
228 {GDK_F25, 'F', 'F'},
229 {GDK_F26, 'F', 'G'},
230 {GDK_F27, 'F', 'H'},
231 {GDK_F28, 'F', 'I'},
232 {GDK_F29, 'F', 'J'},
233 {GDK_F30, 'F', 'K'},
234 {GDK_F31, 'F', 'L'},
235 {GDK_F32, 'F', 'M'},
236 {GDK_F33, 'F', 'N'},
237 {GDK_F34, 'F', 'O'},
238 {GDK_F35, 'F', 'P'},
239#ifdef SunXK_F36
240 {SunXK_F36, 'F', 'Q'},
241 {SunXK_F37, 'F', 'R'},
242#endif
243 {GDK_Help, '%', '1'},
244 {GDK_Undo, '&', '8'},
245 {GDK_BackSpace, 'k', 'b'},
246 {GDK_Insert, 'k', 'I'},
247 {GDK_Delete, 'k', 'D'},
248 {GDK_3270_BackTab, 'k', 'B'},
249 {GDK_Clear, 'k', 'C'},
250 {GDK_Home, 'k', 'h'},
251 {GDK_End, '@', '7'},
252 {GDK_Prior, 'k', 'P'},
253 {GDK_Next, 'k', 'N'},
254 {GDK_Print, '%', '9'},
Bram Moolenaar30613902019-12-01 22:11:18 +0100255 // Keypad keys:
Bram Moolenaar071d4272004-06-13 20:20:40 +0000256 {GDK_KP_Left, 'k', 'l'},
257 {GDK_KP_Right, 'k', 'r'},
258 {GDK_KP_Up, 'k', 'u'},
259 {GDK_KP_Down, 'k', 'd'},
260 {GDK_KP_Insert, KS_EXTRA, (char_u)KE_KINS},
261 {GDK_KP_Delete, KS_EXTRA, (char_u)KE_KDEL},
262 {GDK_KP_Home, 'K', '1'},
263 {GDK_KP_End, 'K', '4'},
Bram Moolenaar30613902019-12-01 22:11:18 +0100264 {GDK_KP_Prior, 'K', '3'}, // page up
265 {GDK_KP_Next, 'K', '5'}, // page down
Bram Moolenaar071d4272004-06-13 20:20:40 +0000266
267 {GDK_KP_Add, 'K', '6'},
268 {GDK_KP_Subtract, 'K', '7'},
269 {GDK_KP_Divide, 'K', '8'},
270 {GDK_KP_Multiply, 'K', '9'},
271 {GDK_KP_Enter, 'K', 'A'},
272 {GDK_KP_Decimal, 'K', 'B'},
273
274 {GDK_KP_0, 'K', 'C'},
275 {GDK_KP_1, 'K', 'D'},
276 {GDK_KP_2, 'K', 'E'},
277 {GDK_KP_3, 'K', 'F'},
278 {GDK_KP_4, 'K', 'G'},
279 {GDK_KP_5, 'K', 'H'},
280 {GDK_KP_6, 'K', 'I'},
281 {GDK_KP_7, 'K', 'J'},
282 {GDK_KP_8, 'K', 'K'},
283 {GDK_KP_9, 'K', 'L'},
284
Bram Moolenaar30613902019-12-01 22:11:18 +0100285 // End of list marker:
Bram Moolenaar071d4272004-06-13 20:20:40 +0000286 {0, 0, 0}
287};
288
289/*
290 * Flags for command line options table below.
291 */
292#define ARG_FONT 1
293#define ARG_GEOMETRY 2
294#define ARG_REVERSE 3
295#define ARG_NOREVERSE 4
296#define ARG_BACKGROUND 5
297#define ARG_FOREGROUND 6
298#define ARG_ICONIC 7
299#define ARG_ROLE 8
300#define ARG_NETBEANS 9
Bram Moolenaar30613902019-12-01 22:11:18 +0100301#define ARG_XRM 10 // ignored
302#define ARG_MENUFONT 11 // ignored
Bram Moolenaar071d4272004-06-13 20:20:40 +0000303#define ARG_INDEX_MASK 0x00ff
Bram Moolenaar30613902019-12-01 22:11:18 +0100304#define ARG_HAS_VALUE 0x0100 // a value is expected after the argument
305#define ARG_NEEDS_GUI 0x0200 // need to initialize the GUI for this
306#define ARG_FOR_GTK 0x0400 // argument is handled by GTK+ or GNOME
307#define ARG_COMPAT_LONG 0x0800 // accept -foo but substitute with --foo
308#define ARG_KEEP 0x1000 // don't remove argument from argv[]
Bram Moolenaar071d4272004-06-13 20:20:40 +0000309
310/*
311 * This table holds all the X GUI command line options allowed. This includes
312 * the standard ones so that we can skip them when Vim is started without the
313 * GUI (but the GUI might start up later).
314 *
315 * When changing this, also update doc/gui_x11.txt and the usage message!!!
316 */
317typedef struct
318{
319 const char *name;
320 unsigned int flags;
321}
322cmdline_option_T;
323
324static const cmdline_option_T cmdline_options[] =
325{
Bram Moolenaar30613902019-12-01 22:11:18 +0100326 // We handle these options ourselves
Bram Moolenaar071d4272004-06-13 20:20:40 +0000327 {"-fn", ARG_FONT|ARG_HAS_VALUE},
328 {"-font", ARG_FONT|ARG_HAS_VALUE},
329 {"-geom", ARG_GEOMETRY|ARG_HAS_VALUE},
330 {"-geometry", ARG_GEOMETRY|ARG_HAS_VALUE},
331 {"-rv", ARG_REVERSE},
332 {"-reverse", ARG_REVERSE},
333 {"+rv", ARG_NOREVERSE},
334 {"+reverse", ARG_NOREVERSE},
335 {"-bg", ARG_BACKGROUND|ARG_HAS_VALUE},
336 {"-background", ARG_BACKGROUND|ARG_HAS_VALUE},
337 {"-fg", ARG_FOREGROUND|ARG_HAS_VALUE},
338 {"-foreground", ARG_FOREGROUND|ARG_HAS_VALUE},
339 {"-iconic", ARG_ICONIC},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000340 {"--role", ARG_ROLE|ARG_HAS_VALUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000341#ifdef FEAT_NETBEANS_INTG
Bram Moolenaar30613902019-12-01 22:11:18 +0100342 {"-nb", ARG_NETBEANS}, // non-standard value format
343 {"-xrm", ARG_XRM|ARG_HAS_VALUE}, // not implemented
344 {"-mf", ARG_MENUFONT|ARG_HAS_VALUE}, // not implemented
345 {"-menufont", ARG_MENUFONT|ARG_HAS_VALUE}, // not implemented
Bram Moolenaar071d4272004-06-13 20:20:40 +0000346#endif
Bram Moolenaar30613902019-12-01 22:11:18 +0100347 // Arguments handled by GTK (and GNOME) internally.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000348 {"--g-fatal-warnings", ARG_FOR_GTK},
349 {"--gdk-debug", ARG_FOR_GTK|ARG_HAS_VALUE},
350 {"--gdk-no-debug", ARG_FOR_GTK|ARG_HAS_VALUE},
351 {"--gtk-debug", ARG_FOR_GTK|ARG_HAS_VALUE},
352 {"--gtk-no-debug", ARG_FOR_GTK|ARG_HAS_VALUE},
353 {"--gtk-module", ARG_FOR_GTK|ARG_HAS_VALUE},
354 {"--sync", ARG_FOR_GTK},
355 {"--display", ARG_FOR_GTK|ARG_HAS_VALUE|ARG_COMPAT_LONG},
356 {"--name", ARG_FOR_GTK|ARG_HAS_VALUE|ARG_COMPAT_LONG},
357 {"--class", ARG_FOR_GTK|ARG_HAS_VALUE|ARG_COMPAT_LONG},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000358 {"--screen", ARG_FOR_GTK|ARG_HAS_VALUE},
359 {"--gxid-host", ARG_FOR_GTK|ARG_HAS_VALUE},
360 {"--gxid-port", ARG_FOR_GTK|ARG_HAS_VALUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000361#ifdef FEAT_GUI_GNOME
362 {"--load-modules", ARG_FOR_GTK|ARG_HAS_VALUE},
363 {"--sm-client-id", ARG_FOR_GTK|ARG_HAS_VALUE},
364 {"--sm-config-prefix", ARG_FOR_GTK|ARG_HAS_VALUE},
365 {"--sm-disable", ARG_FOR_GTK},
366 {"--oaf-ior-fd", ARG_FOR_GTK|ARG_HAS_VALUE},
367 {"--oaf-activate-iid", ARG_FOR_GTK|ARG_HAS_VALUE},
368 {"--oaf-private", ARG_FOR_GTK},
369 {"--enable-sound", ARG_FOR_GTK},
370 {"--disable-sound", ARG_FOR_GTK},
371 {"--espeaker", ARG_FOR_GTK|ARG_HAS_VALUE},
372 {"-?", ARG_FOR_GTK|ARG_NEEDS_GUI},
373 {"--help", ARG_FOR_GTK|ARG_NEEDS_GUI|ARG_KEEP},
374 {"--usage", ARG_FOR_GTK|ARG_NEEDS_GUI},
Bram Moolenaar30613902019-12-01 22:11:18 +0100375# if 0 // conflicts with Vim's own --version argument
Bram Moolenaar071d4272004-06-13 20:20:40 +0000376 {"--version", ARG_FOR_GTK|ARG_NEEDS_GUI},
377# endif
378 {"--disable-crash-dialog", ARG_FOR_GTK},
379#endif
380 {NULL, 0}
381};
382
383static int gui_argc = 0;
384static char **gui_argv = NULL;
385
Bram Moolenaar071d4272004-06-13 20:20:40 +0000386static const char *role_argument = NULL;
Bram Moolenaarf96ae0b2019-07-28 15:21:55 +0200387#if defined(USE_GNOME_SESSION)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000388static const char *restart_command = NULL;
Bram Moolenaar9085f802009-06-03 14:20:21 +0000389static char *abs_restart_command = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000390#endif
391static int found_iconic_arg = FALSE;
392
393#ifdef FEAT_GUI_GNOME
394/*
395 * Can't use Gnome if --socketid given
396 */
397static int using_gnome = 0;
398#else
399# define using_gnome 0
400#endif
401
Ernie Raeld42b8392022-04-23 19:52:23 +0100402// Comment out the following line to ignore code for resize history tracking.
403#define TRACK_RESIZE_HISTORY
404#ifdef TRACK_RESIZE_HISTORY
Bram Moolenaar071d4272004-06-13 20:20:40 +0000405/*
Ernie Rael9f53e7b2022-04-17 18:27:49 +0100406 * Keep a short term resize history so that stale gtk responses can be
407 * discarded.
408 * When a gtk_window_resize() request is sent to gtk, the width/height of
409 * the request is saved. Recent stale requests are kept around in a list.
410 * See https://github.com/vim/vim/issues/10123
411 */
Ernie Raeld42b8392022-04-23 19:52:23 +0100412# if 0 // Change to 1 to enable ch_log() calls for debugging.
Bram Moolenaar4c5678f2022-11-30 18:12:19 +0000413# ifdef FEAT_EVAL
Ernie Raeld42b8392022-04-23 19:52:23 +0100414# define ENABLE_RESIZE_HISTORY_LOG
415# endif
Ernie Rael9f53e7b2022-04-17 18:27:49 +0100416# endif
Ernie Rael9f53e7b2022-04-17 18:27:49 +0100417
418/*
419 * History item of a resize request.
420 * Width and height are of gui.mainwin.
421 */
422typedef struct resize_history {
423 int used; // If true, can't match for discard. Only matches once.
424 int width;
425 int height;
Ernie Raeld42b8392022-04-23 19:52:23 +0100426# ifdef ENABLE_RESIZE_HISTORY_LOG
Ernie Rael9f53e7b2022-04-17 18:27:49 +0100427 int seq; // for ch_log messages
Ernie Raeld42b8392022-04-23 19:52:23 +0100428# endif
Ernie Rael9f53e7b2022-04-17 18:27:49 +0100429 struct resize_history *next;
430} resize_hist_T;
431
432// never NULL during execution
433static resize_hist_T *latest_resize_hist;
434// list of stale resize requests
435static resize_hist_T *old_resize_hists;
436
437/*
438 * Used when calling gtk_window_resize().
439 * Create a resize request history item, put previous request on stale list.
440 * Width/height are the size of the request for the gui.mainwin.
441 */
442 static void
443alloc_resize_hist(int width, int height)
444{
445 // alloc a new resize hist, save current in list of old history
446 resize_hist_T *prev_hist = latest_resize_hist;
447 resize_hist_T *new_hist = ALLOC_CLEAR_ONE(resize_hist_T);
448
449 new_hist->width = width;
450 new_hist->height = height;
451 latest_resize_hist = new_hist;
452
453 // previous hist item becomes head of list
454 prev_hist->next = old_resize_hists;
455 old_resize_hists = prev_hist;
456
Ernie Raeld42b8392022-04-23 19:52:23 +0100457# ifdef ENABLE_RESIZE_HISTORY_LOG
Ernie Rael9f53e7b2022-04-17 18:27:49 +0100458 new_hist->seq = prev_hist->seq + 1;
459 ch_log(NULL, "gui_gtk: New resize seq %d (%d, %d) [%d, %d]",
460 new_hist->seq, width, height, (int)Columns, (int)Rows);
Ernie Raeld42b8392022-04-23 19:52:23 +0100461# endif
Ernie Rael9f53e7b2022-04-17 18:27:49 +0100462}
463
464/*
465 * Free everything on the stale resize history list.
466 * This list is empty when there are no outstanding resize requests.
467 */
468 static void
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +0000469clear_resize_hists(void)
Ernie Rael9f53e7b2022-04-17 18:27:49 +0100470{
Ernie Raeld42b8392022-04-23 19:52:23 +0100471# ifdef ENABLE_RESIZE_HISTORY_LOG
Ernie Rael9f53e7b2022-04-17 18:27:49 +0100472 int i = 0;
Ernie Raeld42b8392022-04-23 19:52:23 +0100473# endif
Ernie Rael9f53e7b2022-04-17 18:27:49 +0100474
475 if (latest_resize_hist)
476 latest_resize_hist->used = TRUE;
477 while (old_resize_hists != NULL)
478 {
479 resize_hist_T *next_hist = old_resize_hists->next;
480
481 vim_free(old_resize_hists);
482 old_resize_hists = next_hist;
Ernie Raeld42b8392022-04-23 19:52:23 +0100483# ifdef ENABLE_RESIZE_HISTORY_LOG
Ernie Rael9f53e7b2022-04-17 18:27:49 +0100484 i++;
Ernie Raeld42b8392022-04-23 19:52:23 +0100485# endif
Ernie Rael9f53e7b2022-04-17 18:27:49 +0100486 }
Ernie Raeld42b8392022-04-23 19:52:23 +0100487# ifdef ENABLE_RESIZE_HISTORY_LOG
Ernie Rael9f53e7b2022-04-17 18:27:49 +0100488 ch_log(NULL, "gui_gtk: free %d hists", i);
Ernie Raeld42b8392022-04-23 19:52:23 +0100489# endif
Ernie Rael9f53e7b2022-04-17 18:27:49 +0100490}
491
492// true if hist item is unused and matches w,h
Ernie Raeld42b8392022-04-23 19:52:23 +0100493# define MATCH_WIDTH_HEIGHT(hist, w, h) \
Ernie Rael9f53e7b2022-04-17 18:27:49 +0100494 (!hist->used && hist->width == w && hist->height == h)
495
496/*
497 * Search the resize hist list.
498 * Return true if the specified width,height match an item in the list that
499 * has never matched before. Mark the matching item as used so it will
500 * not match again.
501 */
502 static int
503match_stale_width_height(int width, int height)
504{
505 resize_hist_T *hist = old_resize_hists;
506
507 for (hist = old_resize_hists; hist != NULL; hist = hist->next)
508 if (MATCH_WIDTH_HEIGHT(hist, width, height))
509 {
Ernie Raeld42b8392022-04-23 19:52:23 +0100510# ifdef ENABLE_RESIZE_HISTORY_LOG
Ernie Rael9f53e7b2022-04-17 18:27:49 +0100511 ch_log(NULL, "gui_gtk: discard seq %d, cur seq %d",
512 hist->seq, latest_resize_hist->seq);
Ernie Raeld42b8392022-04-23 19:52:23 +0100513# endif
Ernie Rael9f53e7b2022-04-17 18:27:49 +0100514 hist->used = TRUE;
515 return TRUE;
516 }
517 return FALSE;
518}
519
Ernie Raeld42b8392022-04-23 19:52:23 +0100520# if defined(EXITFREE)
Ernie Rael9f53e7b2022-04-17 18:27:49 +0100521 static void
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +0000522free_all_resize_hist(void)
Ernie Rael9f53e7b2022-04-17 18:27:49 +0100523{
524 clear_resize_hists();
525 vim_free(latest_resize_hist);
526}
Ernie Raeld42b8392022-04-23 19:52:23 +0100527# endif
Ernie Rael9f53e7b2022-04-17 18:27:49 +0100528#endif
529
530/*
Chris Daltonee93e322021-11-23 12:27:48 +0000531 * GTK doesn't set the GDK_BUTTON1_MASK state when dragging a touch. Add this
532 * state when dragging.
533 */
534static guint dragging_button_state = 0;
535
536/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000537 * Parse the GUI related command-line arguments. Any arguments used are
538 * deleted from argv, and *argc is decremented accordingly. This is called
539 * when vim is started, whether or not the GUI has been started.
540 */
541 void
542gui_mch_prepare(int *argc, char **argv)
543{
544 const cmdline_option_T *option;
545 int i = 0;
546 int len = 0;
547
Bram Moolenaarf96ae0b2019-07-28 15:21:55 +0200548#if defined(USE_GNOME_SESSION)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000549 /*
550 * Determine the command used to invoke Vim, to be passed as restart
551 * command to the session manager. If argv[0] contains any directory
552 * components try building an absolute path, otherwise leave it as is.
553 */
554 restart_command = argv[0];
555
556 if (strchr(argv[0], G_DIR_SEPARATOR) != NULL)
557 {
558 char_u buf[MAXPATHL];
559
560 if (mch_FullName((char_u *)argv[0], buf, (int)sizeof(buf), TRUE) == OK)
Bram Moolenaar9085f802009-06-03 14:20:21 +0000561 {
562 abs_restart_command = (char *)vim_strsave(buf);
563 restart_command = abs_restart_command;
564 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000565 }
566#endif
567
568 /*
569 * Move all the entries in argv which are relevant to GTK+ and GNOME
570 * into gui_argv. Freed later in gui_mch_init().
571 */
572 gui_argc = 0;
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200573 gui_argv = ALLOC_MULT(char *, *argc + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000574
575 g_return_if_fail(gui_argv != NULL);
576
577 gui_argv[gui_argc++] = argv[i++];
578
579 while (i < *argc)
580 {
Bram Moolenaar30613902019-12-01 22:11:18 +0100581 // Don't waste CPU cycles on non-option arguments.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000582 if (argv[i][0] != '-' && argv[i][0] != '+')
583 {
584 ++i;
585 continue;
586 }
587
Bram Moolenaar30613902019-12-01 22:11:18 +0100588 // Look for argv[i] in cmdline_options[] table.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000589 for (option = &cmdline_options[0]; option->name != NULL; ++option)
590 {
591 len = strlen(option->name);
592
593 if (strncmp(argv[i], option->name, len) == 0)
594 {
595 if (argv[i][len] == '\0')
596 break;
Bram Moolenaar30613902019-12-01 22:11:18 +0100597 // allow --foo=bar style
Bram Moolenaar071d4272004-06-13 20:20:40 +0000598 if (argv[i][len] == '=' && (option->flags & ARG_HAS_VALUE))
599 break;
600#ifdef FEAT_NETBEANS_INTG
Bram Moolenaar30613902019-12-01 22:11:18 +0100601 // darn, -nb has non-standard syntax
Bram Moolenaar071d4272004-06-13 20:20:40 +0000602 if (vim_strchr((char_u *)":=", argv[i][len]) != NULL
603 && (option->flags & ARG_INDEX_MASK) == ARG_NETBEANS)
604 break;
605#endif
606 }
607 else if ((option->flags & ARG_COMPAT_LONG)
608 && strcmp(argv[i], option->name + 1) == 0)
609 {
Bram Moolenaar30613902019-12-01 22:11:18 +0100610 // Replace the standard X arguments "-name" and "-display"
611 // with their GNU-style long option counterparts.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000612 argv[i] = (char *)option->name;
613 break;
614 }
615 }
Bram Moolenaar30613902019-12-01 22:11:18 +0100616 if (option->name == NULL) // no match
Bram Moolenaar071d4272004-06-13 20:20:40 +0000617 {
618 ++i;
619 continue;
620 }
621
622 if (option->flags & ARG_FOR_GTK)
623 {
Bram Moolenaar30613902019-12-01 22:11:18 +0100624 // Move the argument into gui_argv, which
625 // will later be passed to gtk_init_check()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000626 gui_argv[gui_argc++] = argv[i];
627 }
628 else
629 {
630 char *value = NULL;
631
Bram Moolenaar30613902019-12-01 22:11:18 +0100632 // Extract the option's value if there is one.
633 // Accept both "--foo bar" and "--foo=bar" style.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000634 if (option->flags & ARG_HAS_VALUE)
635 {
636 if (argv[i][len] == '=')
637 value = &argv[i][len + 1];
638 else if (i + 1 < *argc && strcmp(argv[i + 1], "--") != 0)
639 value = argv[i + 1];
640 }
641
Bram Moolenaar30613902019-12-01 22:11:18 +0100642 // Check for options handled by Vim itself
Bram Moolenaar071d4272004-06-13 20:20:40 +0000643 switch (option->flags & ARG_INDEX_MASK)
644 {
645 case ARG_REVERSE:
646 found_reverse_arg = TRUE;
647 break;
648 case ARG_NOREVERSE:
649 found_reverse_arg = FALSE;
650 break;
651 case ARG_FONT:
652 font_argument = value;
653 break;
654 case ARG_GEOMETRY:
655 if (value != NULL)
656 gui.geom = vim_strsave((char_u *)value);
657 break;
658 case ARG_BACKGROUND:
659 background_argument = value;
660 break;
661 case ARG_FOREGROUND:
662 foreground_argument = value;
663 break;
664 case ARG_ICONIC:
665 found_iconic_arg = TRUE;
666 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000667 case ARG_ROLE:
Bram Moolenaar30613902019-12-01 22:11:18 +0100668 role_argument = value; // used later in gui_mch_open()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000669 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000670#ifdef FEAT_NETBEANS_INTG
671 case ARG_NETBEANS:
Bram Moolenaar30613902019-12-01 22:11:18 +0100672 gui.dofork = FALSE; // don't fork() when starting GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +0000673 netbeansArg = argv[i];
674 break;
675#endif
676 default:
677 break;
678 }
679 }
680
Bram Moolenaar30613902019-12-01 22:11:18 +0100681 // These arguments make gnome_program_init() print a message and exit.
682 // Must start the GUI for this, otherwise ":gui" will exit later!
683 // Only when the GUI can start.
Bram Moolenaar717e1962016-08-10 21:28:44 +0200684 if ((option->flags & ARG_NEEDS_GUI)
685 && gui_mch_early_init_check(FALSE) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000686 gui.starting = TRUE;
687
688 if (option->flags & ARG_KEEP)
689 ++i;
690 else
691 {
Bram Moolenaar30613902019-12-01 22:11:18 +0100692 // Remove the flag from the argument vector.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000693 if (--*argc > i)
694 {
695 int n_strip = 1;
696
Bram Moolenaar30613902019-12-01 22:11:18 +0100697 // Move the argument's value as well, if there is one.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000698 if ((option->flags & ARG_HAS_VALUE)
699 && argv[i][len] != '='
700 && strcmp(argv[i + 1], "--") != 0)
701 {
702 ++n_strip;
703 --*argc;
704 if (option->flags & ARG_FOR_GTK)
705 gui_argv[gui_argc++] = argv[i + 1];
706 }
707
708 if (*argc > i)
709 mch_memmove(&argv[i], &argv[i + n_strip],
710 (*argc - i) * sizeof(char *));
711 }
712 argv[*argc] = NULL;
713 }
714 }
715
716 gui_argv[gui_argc] = NULL;
717}
718
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000719#if defined(EXITFREE) || defined(PROTO)
720 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100721gui_mch_free_all(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000722{
723 vim_free(gui_argv);
Bram Moolenaarf96ae0b2019-07-28 15:21:55 +0200724#if defined(USE_GNOME_SESSION)
Bram Moolenaar9085f802009-06-03 14:20:21 +0000725 vim_free(abs_restart_command);
726#endif
Ernie Raeld42b8392022-04-23 19:52:23 +0100727#ifdef TRACK_RESIZE_HISTORY
Ernie Rael9f53e7b2022-04-17 18:27:49 +0100728 free_all_resize_hist();
Ernie Raeld42b8392022-04-23 19:52:23 +0100729#endif
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000730}
731#endif
732
Bram Moolenaar98921892016-02-23 17:14:37 +0100733#if !GTK_CHECK_VERSION(3,0,0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000734/*
735 * This should be maybe completely removed.
736 * Doesn't seem possible, since check_copy_area() relies on
737 * this information. --danielk
738 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000739 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +0000740visibility_event(GtkWidget *widget UNUSED,
741 GdkEventVisibility *event,
742 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000743{
744 gui.visibility = event->state;
745 /*
746 * When we do an gdk_window_copy_area(), and the window is partially
747 * obscured, we want to receive an event to tell us whether it worked
748 * or not.
749 */
750 if (gui.text_gc != NULL)
751 gdk_gc_set_exposures(gui.text_gc,
752 gui.visibility != GDK_VISIBILITY_UNOBSCURED);
753 return FALSE;
754}
Bram Moolenaar30613902019-12-01 22:11:18 +0100755#endif // !GTK_CHECK_VERSION(3,0,0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000756
757/*
758 * Redraw the corresponding portions of the screen.
759 */
Bram Moolenaar98921892016-02-23 17:14:37 +0100760#if GTK_CHECK_VERSION(3,0,0)
Bram Moolenaar98921892016-02-23 17:14:37 +0100761 static gboolean
Bram Moolenaar8e31fd52016-06-04 22:18:13 +0200762draw_event(GtkWidget *widget UNUSED,
Bram Moolenaar98921892016-02-23 17:14:37 +0100763 cairo_t *cr,
764 gpointer user_data UNUSED)
765{
Bram Moolenaar30613902019-12-01 22:11:18 +0100766 // Skip this when the GUI isn't set up yet, will redraw later.
Bram Moolenaar98921892016-02-23 17:14:37 +0100767 if (gui.starting)
768 return FALSE;
769
Bram Moolenaar30613902019-12-01 22:11:18 +0100770 out_flush(); // make sure all output has been processed
771 // for GTK+ 3, may induce other draw events.
Bram Moolenaar98921892016-02-23 17:14:37 +0100772
773 cairo_set_source_surface(cr, gui.surface, 0, 0);
774
Bram Moolenaar98921892016-02-23 17:14:37 +0100775 {
776 cairo_rectangle_list_t *list = NULL;
777
Bram Moolenaar98921892016-02-23 17:14:37 +0100778 list = cairo_copy_clip_rectangle_list(cr);
779 if (list->status != CAIRO_STATUS_CLIP_NOT_REPRESENTABLE)
780 {
781 int i;
Bram Moolenaarfe344a92017-02-23 12:20:35 +0100782
Bram Moolenaar98921892016-02-23 17:14:37 +0100783 for (i = 0; i < list->num_rectangles; i++)
784 {
presuku9459b8d2021-11-16 20:03:56 +0000785 const cairo_rectangle_t *rect = &list->rectangles[i];
Bram Moolenaar023930d2022-12-01 19:40:55 +0000786 cairo_rectangle(cr, rect->x, rect->y,
787 rect->width, rect->height);
presuku9459b8d2021-11-16 20:03:56 +0000788 cairo_fill(cr);
Bram Moolenaar98921892016-02-23 17:14:37 +0100789 }
790 }
791 cairo_rectangle_list_destroy(list);
Bram Moolenaar98921892016-02-23 17:14:37 +0100792 }
Bram Moolenaar98921892016-02-23 17:14:37 +0100793
794 return FALSE;
795}
Bram Moolenaar30613902019-12-01 22:11:18 +0100796#else // !GTK_CHECK_VERSION(3,0,0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000797 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +0000798expose_event(GtkWidget *widget UNUSED,
799 GdkEventExpose *event,
800 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000801{
Bram Moolenaar30613902019-12-01 22:11:18 +0100802 // Skip this when the GUI isn't set up yet, will redraw later.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000803 if (gui.starting)
804 return FALSE;
805
Bram Moolenaar30613902019-12-01 22:11:18 +0100806 out_flush(); // make sure all output has been processed
Bram Moolenaar071d4272004-06-13 20:20:40 +0000807 gui_redraw(event->area.x, event->area.y,
808 event->area.width, event->area.height);
809
Bram Moolenaar30613902019-12-01 22:11:18 +0100810 // Clear the border areas if needed
Bram Moolenaar071d4272004-06-13 20:20:40 +0000811 if (event->area.x < FILL_X(0))
812 gdk_window_clear_area(gui.drawarea->window, 0, 0, FILL_X(0), 0);
813 if (event->area.y < FILL_Y(0))
814 gdk_window_clear_area(gui.drawarea->window, 0, 0, 0, FILL_Y(0));
815 if (event->area.x > FILL_X(Columns))
816 gdk_window_clear_area(gui.drawarea->window,
817 FILL_X((int)Columns), 0, 0, 0);
818 if (event->area.y > FILL_Y(Rows))
819 gdk_window_clear_area(gui.drawarea->window, 0, FILL_Y((int)Rows), 0, 0);
820
821 return FALSE;
822}
Bram Moolenaar30613902019-12-01 22:11:18 +0100823#endif // !GTK_CHECK_VERSION(3,0,0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000824
825#ifdef FEAT_CLIENTSERVER
826/*
827 * Handle changes to the "Comm" property
828 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000829 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +0000830property_event(GtkWidget *widget,
831 GdkEventProperty *event,
832 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000833{
834 if (event->type == GDK_PROPERTY_NOTIFY
835 && event->state == (int)GDK_PROPERTY_NEW_VALUE
Bram Moolenaar98921892016-02-23 17:14:37 +0100836 && GDK_WINDOW_XID(event->window) == commWindow
Bram Moolenaar071d4272004-06-13 20:20:40 +0000837 && GET_X_ATOM(event->atom) == commProperty)
838 {
839 XEvent xev;
840
Bram Moolenaar30613902019-12-01 22:11:18 +0100841 // Translate to XLib
Bram Moolenaar071d4272004-06-13 20:20:40 +0000842 xev.xproperty.type = PropertyNotify;
843 xev.xproperty.atom = commProperty;
844 xev.xproperty.window = commWindow;
845 xev.xproperty.state = PropertyNewValue;
Bram Moolenaar98921892016-02-23 17:14:37 +0100846 serverEventProc(GDK_WINDOW_XDISPLAY(gtk_widget_get_window(widget)),
Ernie Rael9f53e7b2022-04-17 18:27:49 +0100847 &xev, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000848 }
849 return FALSE;
850}
Bram Moolenaar30613902019-12-01 22:11:18 +0100851#endif // defined(FEAT_CLIENTSERVER)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000852
Bram Moolenaar7ebf4e12018-08-07 20:01:40 +0200853/*
854 * Handle changes to the "Xft/DPI" setting
855 */
856 static void
857gtk_settings_xft_dpi_changed_cb(GtkSettings *gtk_settings UNUSED,
Ernie Rael9f53e7b2022-04-17 18:27:49 +0100858 GParamSpec *pspec UNUSED,
859 gpointer data UNUSED)
Bram Moolenaar7ebf4e12018-08-07 20:01:40 +0200860{
861 // Create a new PangoContext for this screen, and initialize it
862 // with the current font if necessary.
863 if (gui.text_context != NULL)
864 g_object_unref(gui.text_context);
865
866 gui.text_context = gtk_widget_create_pango_context(gui.mainwin);
867 pango_context_set_base_dir(gui.text_context, PANGO_DIRECTION_LTR);
868
869 if (gui.norm_font != NULL)
870 {
871 // force default font
872 gui_mch_init_font(*p_guifont == NUL ? NULL : p_guifont, FALSE);
873 gui_set_shellsize(TRUE, FALSE, RESIZE_BOTH);
874 }
875}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000876
Bram Moolenaar4ab79682017-08-27 14:50:47 +0200877typedef gboolean timeout_cb_type;
Bram Moolenaar4ab79682017-08-27 14:50:47 +0200878
879/*
880 * Start a timer that will invoke the specified callback.
881 * Returns the ID of the timer.
882 */
883 static guint
884timeout_add(int time, timeout_cb_type (*callback)(gpointer), int *flagp)
885{
Bram Moolenaar4ab79682017-08-27 14:50:47 +0200886 return g_timeout_add((guint)time, (GSourceFunc)callback, flagp);
Bram Moolenaar4ab79682017-08-27 14:50:47 +0200887}
888
889 static void
890timeout_remove(guint timer)
891{
Bram Moolenaar4ab79682017-08-27 14:50:47 +0200892 g_source_remove(timer);
Bram Moolenaar4ab79682017-08-27 14:50:47 +0200893}
894
895
Bram Moolenaar30613902019-12-01 22:11:18 +0100896/////////////////////////////////////////////////////////////////////////////
897// Focus handlers:
Bram Moolenaar071d4272004-06-13 20:20:40 +0000898
899
900/*
901 * This is a simple state machine:
902 * BLINK_NONE not blinking at all
903 * BLINK_OFF blinking, cursor is not shown
904 * BLINK_ON blinking, cursor is shown
905 */
906
907#define BLINK_NONE 0
908#define BLINK_OFF 1
909#define BLINK_ON 2
910
911static int blink_state = BLINK_NONE;
912static long_u blink_waittime = 700;
913static long_u blink_ontime = 400;
914static long_u blink_offtime = 250;
915static guint blink_timer = 0;
916
Bram Moolenaar703a8042016-06-04 16:24:32 +0200917 int
918gui_mch_is_blinking(void)
919{
920 return blink_state != BLINK_NONE;
921}
922
Bram Moolenaar9d5d3c92016-07-07 16:43:02 +0200923 int
924gui_mch_is_blink_off(void)
925{
926 return blink_state == BLINK_OFF;
927}
928
Bram Moolenaar071d4272004-06-13 20:20:40 +0000929 void
930gui_mch_set_blinking(long waittime, long on, long off)
931{
932 blink_waittime = waittime;
933 blink_ontime = on;
934 blink_offtime = off;
935}
936
937/*
938 * Stop the cursor blinking. Show the cursor if it wasn't shown.
939 */
940 void
Bram Moolenaar1dd45fb2018-01-31 21:10:01 +0100941gui_mch_stop_blink(int may_call_gui_update_cursor)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000942{
943 if (blink_timer)
944 {
Bram Moolenaar4ab79682017-08-27 14:50:47 +0200945 timeout_remove(blink_timer);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000946 blink_timer = 0;
947 }
Bram Moolenaar1dd45fb2018-01-31 21:10:01 +0100948 if (blink_state == BLINK_OFF && may_call_gui_update_cursor)
Bram Moolenaarda3a77d2016-07-10 23:16:09 +0200949 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000950 gui_update_cursor(TRUE, FALSE);
presuku9459b8d2021-11-16 20:03:56 +0000951#if !GTK_CHECK_VERSION(3,0,0)
Bram Moolenaarda3a77d2016-07-10 23:16:09 +0200952 gui_mch_flush();
presuku9459b8d2021-11-16 20:03:56 +0000953#endif
Bram Moolenaarda3a77d2016-07-10 23:16:09 +0200954 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000955 blink_state = BLINK_NONE;
956}
957
Bram Moolenaar4ab79682017-08-27 14:50:47 +0200958 static timeout_cb_type
Bram Moolenaarb85cb212009-05-17 14:24:23 +0000959blink_cb(gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000960{
961 if (blink_state == BLINK_ON)
962 {
963 gui_undraw_cursor();
964 blink_state = BLINK_OFF;
Bram Moolenaar4ab79682017-08-27 14:50:47 +0200965 blink_timer = timeout_add(blink_offtime, blink_cb, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000966 }
967 else
968 {
969 gui_update_cursor(TRUE, FALSE);
970 blink_state = BLINK_ON;
Bram Moolenaar4ab79682017-08-27 14:50:47 +0200971 blink_timer = timeout_add(blink_ontime, blink_cb, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000972 }
presuku9459b8d2021-11-16 20:03:56 +0000973#if !GTK_CHECK_VERSION(3,0,0)
Bram Moolenaarda3a77d2016-07-10 23:16:09 +0200974 gui_mch_flush();
presuku9459b8d2021-11-16 20:03:56 +0000975#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000976
Bram Moolenaar30613902019-12-01 22:11:18 +0100977 return FALSE; // don't happen again
Bram Moolenaar071d4272004-06-13 20:20:40 +0000978}
979
980/*
981 * Start the cursor blinking. If it was already blinking, this restarts the
982 * waiting time and shows the cursor.
983 */
984 void
985gui_mch_start_blink(void)
986{
987 if (blink_timer)
Bram Moolenaar7bcdb7d2014-04-06 21:08:45 +0200988 {
Bram Moolenaar4ab79682017-08-27 14:50:47 +0200989 timeout_remove(blink_timer);
Bram Moolenaar7bcdb7d2014-04-06 21:08:45 +0200990 blink_timer = 0;
991 }
Bram Moolenaar30613902019-12-01 22:11:18 +0100992 // Only switch blinking on if none of the times is zero
Bram Moolenaar071d4272004-06-13 20:20:40 +0000993 if (blink_waittime && blink_ontime && blink_offtime && gui.in_focus)
994 {
Bram Moolenaar4ab79682017-08-27 14:50:47 +0200995 blink_timer = timeout_add(blink_waittime, blink_cb, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000996 blink_state = BLINK_ON;
997 gui_update_cursor(TRUE, FALSE);
presuku9459b8d2021-11-16 20:03:56 +0000998#if !GTK_CHECK_VERSION(3,0,0)
Bram Moolenaarda3a77d2016-07-10 23:16:09 +0200999 gui_mch_flush();
presuku9459b8d2021-11-16 20:03:56 +00001000#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001001 }
1002}
1003
Bram Moolenaar071d4272004-06-13 20:20:40 +00001004 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001005enter_notify_event(GtkWidget *widget UNUSED,
1006 GdkEventCrossing *event UNUSED,
1007 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001008{
1009 if (blink_state == BLINK_NONE)
1010 gui_mch_start_blink();
1011
Bram Moolenaar30613902019-12-01 22:11:18 +01001012 // make sure keyboard input goes there
Bram Moolenaar98921892016-02-23 17:14:37 +01001013 if (gtk_socket_id == 0 || !gtk_widget_has_focus(gui.drawarea))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001014 gtk_widget_grab_focus(gui.drawarea);
1015
1016 return FALSE;
1017}
1018
Bram Moolenaar071d4272004-06-13 20:20:40 +00001019 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001020leave_notify_event(GtkWidget *widget UNUSED,
1021 GdkEventCrossing *event UNUSED,
1022 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001023{
1024 if (blink_state != BLINK_NONE)
Bram Moolenaar1dd45fb2018-01-31 21:10:01 +01001025 gui_mch_stop_blink(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001026
1027 return FALSE;
1028}
1029
Bram Moolenaar071d4272004-06-13 20:20:40 +00001030 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001031focus_in_event(GtkWidget *widget,
1032 GdkEventFocus *event UNUSED,
1033 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001034{
1035 gui_focus_change(TRUE);
1036
1037 if (blink_state == BLINK_NONE)
1038 gui_mch_start_blink();
1039
Bram Moolenaar30613902019-12-01 22:11:18 +01001040 // make sure keyboard input goes to the draw area (if this is focus for a
1041 // window)
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00001042 if (widget != gui.drawarea)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001043 gtk_widget_grab_focus(gui.drawarea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001044
1045 return TRUE;
1046}
1047
Bram Moolenaar071d4272004-06-13 20:20:40 +00001048 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001049focus_out_event(GtkWidget *widget UNUSED,
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001050 GdkEventFocus *event UNUSED,
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001051 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001052{
1053 gui_focus_change(FALSE);
1054
1055 if (blink_state != BLINK_NONE)
Bram Moolenaar1dd45fb2018-01-31 21:10:01 +01001056 gui_mch_stop_blink(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001057
1058 return TRUE;
1059}
1060
1061
Bram Moolenaar071d4272004-06-13 20:20:40 +00001062/*
1063 * Translate a GDK key value to UTF-8 independently of the current locale.
1064 * The output is written to string, which must have room for at least 6 bytes
1065 * plus the NUL terminator. Returns the length in bytes.
1066 *
Bram Moolenaarf4ae6b22020-05-30 19:52:46 +02001067 * event->string is evil; see here why:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001068 * http://developer.gnome.org/doc/API/2.0/gdk/gdk-Event-Structures.html#GdkEventKey
1069 */
1070 static int
Bram Moolenaarf4ae6b22020-05-30 19:52:46 +02001071keyval_to_string(unsigned int keyval, char_u *string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001072{
1073 int len;
1074 guint32 uc;
1075
1076 uc = gdk_keyval_to_unicode(keyval);
1077 if (uc != 0)
1078 {
Bram Moolenaarf4ae6b22020-05-30 19:52:46 +02001079 // Translate a normal key to UTF-8. This doesn't work for dead
1080 // keys of course, you _have_ to use an input method for that.
1081 len = utf_char2bytes((int)uc, string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001082 }
1083 else
1084 {
Bram Moolenaar30613902019-12-01 22:11:18 +01001085 // Translate keys which are represented by ASCII control codes in Vim.
1086 // There are only a few of those; most control keys are translated to
1087 // special terminal-like control sequences.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001088 len = 1;
1089 switch (keyval)
1090 {
1091 case GDK_Tab: case GDK_KP_Tab: case GDK_ISO_Left_Tab:
1092 string[0] = TAB;
1093 break;
1094 case GDK_Linefeed:
1095 string[0] = NL;
1096 break;
1097 case GDK_Return: case GDK_ISO_Enter: case GDK_3270_Enter:
1098 string[0] = CAR;
1099 break;
1100 case GDK_Escape:
1101 string[0] = ESC;
1102 break;
1103 default:
1104 len = 0;
1105 break;
1106 }
1107 }
1108 string[len] = NUL;
1109
1110 return len;
1111}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001112
Bram Moolenaar19a09a12005-03-04 23:39:37 +00001113 static int
1114modifiers_gdk2vim(guint state)
1115{
1116 int modifiers = 0;
1117
1118 if (state & GDK_SHIFT_MASK)
1119 modifiers |= MOD_MASK_SHIFT;
1120 if (state & GDK_CONTROL_MASK)
1121 modifiers |= MOD_MASK_CTRL;
1122 if (state & GDK_MOD1_MASK)
1123 modifiers |= MOD_MASK_ALT;
Bram Moolenaar580061a2010-08-13 13:57:13 +02001124#if GTK_CHECK_VERSION(2,10,0)
Casey Tucker92e90a12024-01-25 22:44:00 +01001125 if (state & GDK_META_MASK)
1126 modifiers |= MOD_MASK_META;
Bram Moolenaar30bb4142010-05-17 22:07:15 +02001127 if (state & GDK_SUPER_MASK)
Casey Tucker92e90a12024-01-25 22:44:00 +01001128 modifiers |= MOD_MASK_CMD;
1129#else
Bram Moolenaar19a09a12005-03-04 23:39:37 +00001130 if (state & GDK_MOD4_MASK)
Casey Tucker92e90a12024-01-25 22:44:00 +01001131 modifiers |= MOD_MASK_CMD;
1132#endif
Bram Moolenaar19a09a12005-03-04 23:39:37 +00001133
1134 return modifiers;
1135}
1136
1137 static int
1138modifiers_gdk2mouse(guint state)
1139{
1140 int modifiers = 0;
1141
1142 if (state & GDK_SHIFT_MASK)
1143 modifiers |= MOUSE_SHIFT;
1144 if (state & GDK_CONTROL_MASK)
1145 modifiers |= MOUSE_CTRL;
1146 if (state & GDK_MOD1_MASK)
1147 modifiers |= MOUSE_ALT;
1148
1149 return modifiers;
1150}
1151
Bram Moolenaar071d4272004-06-13 20:20:40 +00001152/*
1153 * Main keyboard handler:
1154 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001155 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001156key_press_event(GtkWidget *widget UNUSED,
1157 GdkEventKey *event,
1158 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001159{
Bram Moolenaar30613902019-12-01 22:11:18 +01001160 // For GTK+ 2 we know for sure how large the string might get.
1161 // (That is, up to 6 bytes + NUL + CSI escapes + safety measure.)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001162 char_u string[32], string2[32];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001163 guint key_sym;
1164 int len;
1165 int i;
1166 int modifiers;
1167 int key;
1168 guint state;
1169 char_u *s, *d;
Anton Sharonov4dd92522022-07-04 10:47:31 +01001170 int ctrl_prefix_added = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001171
Bram Moolenaar20892c12011-06-26 04:49:00 +02001172 gui.event_time = event->time;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001173 key_sym = event->keyval;
1174 state = event->state;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001175
1176#ifdef FEAT_XIM
1177 if (xim_queue_key_press_event(event, TRUE))
1178 return TRUE;
1179#endif
1180
Bram Moolenaar071d4272004-06-13 20:20:40 +00001181#ifdef SunXK_F36
1182 /*
1183 * These keys have bogus lookup strings, and trapping them here is
1184 * easier than trying to XRebindKeysym() on them with every possible
1185 * combination of modifiers.
1186 */
1187 if (key_sym == SunXK_F36 || key_sym == SunXK_F37)
1188 len = 0;
1189 else
1190#endif
1191 {
Bram Moolenaarf4ae6b22020-05-30 19:52:46 +02001192 len = keyval_to_string(key_sym, string2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001193
Bram Moolenaar30613902019-12-01 22:11:18 +01001194 // Careful: convert_input() doesn't handle the NUL character.
1195 // No need to convert pure ASCII anyway, thus the len > 1 check.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001196 if (len > 1 && input_conv.vc_type != CONV_NONE)
1197 len = convert_input(string2, len, sizeof(string2));
1198
1199 s = string2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001200 d = string;
1201 for (i = 0; i < len; ++i)
1202 {
1203 *d++ = s[i];
1204 if (d[-1] == CSI && d + 2 < string + sizeof(string))
1205 {
Bram Moolenaar30613902019-12-01 22:11:18 +01001206 // Turn CSI into K_CSI.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001207 *d++ = KS_EXTRA;
1208 *d++ = (int)KE_CSI;
1209 }
1210 }
1211 len = d - string;
1212 }
1213
Bram Moolenaar30613902019-12-01 22:11:18 +01001214 // Shift-Tab results in Left_Tab, but we want <S-Tab>
Bram Moolenaar071d4272004-06-13 20:20:40 +00001215 if (key_sym == GDK_ISO_Left_Tab)
1216 {
1217 key_sym = GDK_Tab;
1218 state |= GDK_SHIFT_MASK;
1219 }
1220
Bram Moolenaar071d4272004-06-13 20:20:40 +00001221#ifdef FEAT_MENU
Bram Moolenaar30613902019-12-01 22:11:18 +01001222 // If there is a menu and 'wak' is "yes", or 'wak' is "menu" and the key
1223 // is a menu shortcut, we ignore everything with the ALT modifier.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001224 if ((state & GDK_MOD1_MASK)
1225 && gui.menu_is_active
1226 && (*p_wak == 'y'
1227 || (*p_wak == 'm'
1228 && len == 1
1229 && gui_is_menu_shortcut(string[0]))))
Bram Moolenaar30613902019-12-01 22:11:18 +01001230 // For GTK2 we return false to signify that we haven't handled the
1231 // keypress, so that gtk will handle the mnemonic or accelerator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001232 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001233#endif
1234
Bram Moolenaarfd615a32020-05-16 14:01:51 +02001235 // We used to apply Alt/Meta to the key here (Mod1Mask), but that is now
1236 // done later, the same as it happens for the terminal. Hopefully that
1237 // works for everybody...
Bram Moolenaar071d4272004-06-13 20:20:40 +00001238
Bram Moolenaar30613902019-12-01 22:11:18 +01001239 // Check for special keys. Also do this when len == 1 (key has an ASCII
1240 // value) to detect backspace, delete and keypad keys.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001241 if (len == 0 || len == 1)
1242 {
1243 for (i = 0; special_keys[i].key_sym != 0; i++)
1244 {
1245 if (special_keys[i].key_sym == key_sym)
1246 {
1247 string[0] = CSI;
1248 string[1] = special_keys[i].code0;
1249 string[2] = special_keys[i].code1;
1250 len = -3;
1251 break;
1252 }
1253 }
1254 }
1255
Bram Moolenaar4c99e622022-07-04 19:58:17 +01001256#ifdef GDK_KEY_dead_circumflex
Anton Sharonov4dd92522022-07-04 10:47:31 +01001257 // Belgian Ctrl+[ workaround
1258 if (len == 0 && key_sym == GDK_KEY_dead_circumflex)
1259 {
1260 string[0] = CSI;
1261 string[1] = KS_MODIFIER;
1262 string[2] = MOD_MASK_CTRL;
1263 string[3] = '[';
1264 len = 4;
1265 add_to_input_buf(string, len);
1266 // workaround has to return here, otherwise our fake string[] entries
1267 // are confusing code downstream
1268 return TRUE;
1269 }
Bram Moolenaar4c99e622022-07-04 19:58:17 +01001270#endif
Anton Sharonov4dd92522022-07-04 10:47:31 +01001271
Bram Moolenaar30613902019-12-01 22:11:18 +01001272 if (len == 0) // Unrecognized key
Bram Moolenaar071d4272004-06-13 20:20:40 +00001273 return TRUE;
1274
Bram Moolenaarfd615a32020-05-16 14:01:51 +02001275 // For some keys a shift modifier is translated into another key code.
1276 if (len == -3)
1277 key = TO_SPECIAL(string[1], string[2]);
1278 else
Bram Moolenaar820ffa52020-06-21 15:09:14 +02001279 {
1280 string[len] = NUL;
1281 key = mb_ptr2char(string);
1282 }
Bram Moolenaarfd615a32020-05-16 14:01:51 +02001283
Bram Moolenaaref6746f2020-06-20 14:43:23 +02001284 // Handle modifiers.
1285 modifiers = modifiers_gdk2vim(state);
1286
1287 // Recognize special keys.
Bram Moolenaarfd615a32020-05-16 14:01:51 +02001288 key = simplify_key(key, &modifiers);
1289 if (key == CSI)
1290 key = K_CSI;
1291 if (IS_SPECIAL(key))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001292 {
Bram Moolenaarfd615a32020-05-16 14:01:51 +02001293 string[0] = CSI;
1294 string[1] = K_SECOND(key);
1295 string[2] = K_THIRD(key);
1296 len = 3;
1297 }
1298 else
1299 {
Bram Moolenaar4e2114e2020-10-07 16:12:37 +02001300 // Some keys need adjustment when the Ctrl modifier is used.
1301 key = may_adjust_key_for_ctrl(modifiers, key);
Bram Moolenaaref6746f2020-06-20 14:43:23 +02001302
Bram Moolenaar4e2114e2020-10-07 16:12:37 +02001303 // May remove the Shift modifier if it's included in the key.
Bram Moolenaaref6746f2020-06-20 14:43:23 +02001304 modifiers = may_remove_shift_modifier(modifiers, key);
1305
Bram Moolenaar820ffa52020-06-21 15:09:14 +02001306 len = mb_char2bytes(key, string);
Bram Moolenaarfd615a32020-05-16 14:01:51 +02001307 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001308
Bram Moolenaarfd615a32020-05-16 14:01:51 +02001309 if (modifiers != 0)
1310 {
1311 string2[0] = CSI;
1312 string2[1] = KS_MODIFIER;
1313 string2[2] = modifiers;
1314 add_to_input_buf(string2, 3);
Anton Sharonov4dd92522022-07-04 10:47:31 +01001315 if (modifiers == 0x4)
1316 ctrl_prefix_added = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001317 }
1318
Bram Moolenaar4e1d8bd2020-08-01 13:10:14 +02001319 // Check if the key interrupts.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001320 {
Bram Moolenaar4e1d8bd2020-08-01 13:10:14 +02001321 int int_ch = check_for_interrupt(key, modifiers);
1322
1323 if (int_ch != NUL)
1324 {
1325 trash_input_buf();
1326 string[0] = int_ch;
1327 len = 1;
1328 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001329 }
1330
Anton Sharonov4dd92522022-07-04 10:47:31 +01001331 // workaround for German keyboard, where instead of '[' char we have code
1332 // sequence of bytes 195, 188 (UTF-8 for "u-umlaut")
1333 if (ctrl_prefix_added && len == 2
1334 && ((int)string[0]) == 195
1335 && ((int)string[1]) == 188)
1336 {
1337 string[0] = 91; // ASCII('[')
1338 len = 1;
1339 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001340 add_to_input_buf(string, len);
1341
Bram Moolenaar30613902019-12-01 22:11:18 +01001342 // blank out the pointer if necessary
Bram Moolenaar071d4272004-06-13 20:20:40 +00001343 if (p_mh)
1344 gui_mch_mousehide(TRUE);
1345
Bram Moolenaar071d4272004-06-13 20:20:40 +00001346 return TRUE;
1347}
1348
Bram Moolenaar98921892016-02-23 17:14:37 +01001349#if defined(FEAT_XIM) || GTK_CHECK_VERSION(3,0,0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001350 static gboolean
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001351key_release_event(GtkWidget *widget UNUSED,
Bram Moolenaar0a748d92022-04-28 15:39:08 +01001352 GdkEventKey *event UNUSED,
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001353 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001354{
Bram Moolenaar98921892016-02-23 17:14:37 +01001355# if defined(FEAT_XIM)
Bram Moolenaar20892c12011-06-26 04:49:00 +02001356 gui.event_time = event->time;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001357 /*
1358 * GTK+ 2 input methods may do fancy stuff on key release events too.
1359 * With the default IM for instance, you can enter any UCS code point
1360 * by holding down CTRL-SHIFT and typing hexadecimal digits.
1361 */
1362 return xim_queue_key_press_event(event, FALSE);
Bram Moolenaar98921892016-02-23 17:14:37 +01001363# else
1364 return TRUE;
1365# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001366}
1367#endif
1368
1369
Bram Moolenaar30613902019-12-01 22:11:18 +01001370/////////////////////////////////////////////////////////////////////////////
1371// Selection handlers:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001372
Bram Moolenaar30613902019-12-01 22:11:18 +01001373// Remember when clip_lose_selection was called from here, we must not call
1374// gtk_selection_owner_set() then.
Bram Moolenaar29dfa5a2018-03-20 21:24:45 +01001375static int in_selection_clear_event = FALSE;
1376
Bram Moolenaar071d4272004-06-13 20:20:40 +00001377 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001378selection_clear_event(GtkWidget *widget UNUSED,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001379 GdkEventSelection *event,
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001380 gpointer user_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001381{
Bram Moolenaar29dfa5a2018-03-20 21:24:45 +01001382 in_selection_clear_event = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001383 if (event->selection == clip_plus.gtk_sel_atom)
1384 clip_lose_selection(&clip_plus);
1385 else
1386 clip_lose_selection(&clip_star);
Bram Moolenaar29dfa5a2018-03-20 21:24:45 +01001387 in_selection_clear_event = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001388
Bram Moolenaar071d4272004-06-13 20:20:40 +00001389 return TRUE;
1390}
1391
Bram Moolenaar30613902019-12-01 22:11:18 +01001392#define RS_NONE 0 // selection_received_cb() not called yet
1393#define RS_OK 1 // selection_received_cb() called and OK
1394#define RS_FAIL 2 // selection_received_cb() called and failed
Bram Moolenaar071d4272004-06-13 20:20:40 +00001395static int received_selection = RS_NONE;
1396
Bram Moolenaar071d4272004-06-13 20:20:40 +00001397 static void
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001398selection_received_cb(GtkWidget *widget UNUSED,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001399 GtkSelectionData *data,
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001400 guint time_ UNUSED,
1401 gpointer user_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001402{
Bram Moolenaar0554fa42019-06-14 21:36:54 +02001403 Clipboard_T *cbd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001404 char_u *text;
1405 char_u *tmpbuf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001406 guchar *tmpbuf_utf8 = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001407 int len;
Bram Moolenaard44347f2011-06-19 01:14:29 +02001408 int motion_type = MAUTO;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001409
Bram Moolenaar98921892016-02-23 17:14:37 +01001410 if (gtk_selection_data_get_selection(data) == clip_plus.gtk_sel_atom)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001411 cbd = &clip_plus;
1412 else
1413 cbd = &clip_star;
1414
Bram Moolenaar98921892016-02-23 17:14:37 +01001415 text = (char_u *)gtk_selection_data_get_data(data);
1416 len = gtk_selection_data_get_length(data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001417
1418 if (text == NULL || len <= 0)
1419 {
1420 received_selection = RS_FAIL;
Bram Moolenaar30613902019-12-01 22:11:18 +01001421 // clip_free_selection(cbd); ???
Bram Moolenaar071d4272004-06-13 20:20:40 +00001422
Bram Moolenaar071d4272004-06-13 20:20:40 +00001423 return;
1424 }
1425
Bram Moolenaar98921892016-02-23 17:14:37 +01001426 if (gtk_selection_data_get_data_type(data) == vim_atom)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001427 {
1428 motion_type = *text++;
1429 --len;
1430 }
Bram Moolenaar98921892016-02-23 17:14:37 +01001431 else if (gtk_selection_data_get_data_type(data) == vimenc_atom)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001432 {
1433 char_u *enc;
1434 vimconv_T conv;
1435
1436 motion_type = *text++;
1437 --len;
1438
1439 enc = text;
1440 text += STRLEN(text) + 1;
1441 len -= text - enc;
1442
Bram Moolenaar30613902019-12-01 22:11:18 +01001443 // If the encoding of the text is different from 'encoding', attempt
1444 // converting it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001445 conv.vc_type = CONV_NONE;
1446 convert_setup(&conv, enc, p_enc);
1447 if (conv.vc_type != CONV_NONE)
1448 {
1449 tmpbuf = string_convert(&conv, text, &len);
1450 if (tmpbuf != NULL)
1451 text = tmpbuf;
1452 convert_setup(&conv, NULL, NULL);
1453 }
1454 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001455
Bram Moolenaar30613902019-12-01 22:11:18 +01001456 // gtk_selection_data_get_text() handles all the nasty details
1457 // and targets and encodings etc. This rocks so hard.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001458 else
1459 {
1460 tmpbuf_utf8 = gtk_selection_data_get_text(data);
1461 if (tmpbuf_utf8 != NULL)
1462 {
1463 len = STRLEN(tmpbuf_utf8);
1464 if (input_conv.vc_type != CONV_NONE)
1465 {
1466 tmpbuf = string_convert(&input_conv, tmpbuf_utf8, &len);
1467 if (tmpbuf != NULL)
1468 text = tmpbuf;
1469 }
1470 else
1471 text = tmpbuf_utf8;
1472 }
Bram Moolenaar3a6eaa52009-06-16 13:23:06 +00001473 else if (len >= 2 && text[0] == 0xff && text[1] == 0xfe)
1474 {
1475 vimconv_T conv;
1476
Bram Moolenaar30613902019-12-01 22:11:18 +01001477 // UTF-16, we get this for HTML
Bram Moolenaar3a6eaa52009-06-16 13:23:06 +00001478 conv.vc_type = CONV_NONE;
1479 convert_setup_ext(&conv, (char_u *)"utf-16le", FALSE, p_enc, TRUE);
1480
1481 if (conv.vc_type != CONV_NONE)
1482 {
1483 text += 2;
1484 len -= 2;
1485 tmpbuf = string_convert(&conv, text, &len);
1486 convert_setup(&conv, NULL, NULL);
1487 }
1488 if (tmpbuf != NULL)
1489 text = tmpbuf;
1490 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001491 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001492
Bram Moolenaar30613902019-12-01 22:11:18 +01001493 // Chop off any trailing NUL bytes. OpenOffice sends these.
Bram Moolenaara76638f2010-06-05 12:49:46 +02001494 while (len > 0 && text[len - 1] == NUL)
1495 --len;
1496
Bram Moolenaar071d4272004-06-13 20:20:40 +00001497 clip_yank_selection(motion_type, text, (long)len, cbd);
1498 received_selection = RS_OK;
1499 vim_free(tmpbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001500 g_free(tmpbuf_utf8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001501}
1502
1503/*
1504 * Prepare our selection data for passing it to the external selection
1505 * client.
1506 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001507 static void
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001508selection_get_cb(GtkWidget *widget UNUSED,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001509 GtkSelectionData *selection_data,
1510 guint info,
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001511 guint time_ UNUSED,
1512 gpointer user_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001513{
1514 char_u *string;
1515 char_u *tmpbuf;
1516 long_u tmplen;
1517 int length;
1518 int motion_type;
1519 GdkAtom type;
Bram Moolenaar0554fa42019-06-14 21:36:54 +02001520 Clipboard_T *cbd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001521
Bram Moolenaar98921892016-02-23 17:14:37 +01001522 if (gtk_selection_data_get_selection(selection_data)
1523 == clip_plus.gtk_sel_atom)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001524 cbd = &clip_plus;
1525 else
1526 cbd = &clip_star;
1527
1528 if (!cbd->owned)
Bram Moolenaar30613902019-12-01 22:11:18 +01001529 return; // Shouldn't ever happen
Bram Moolenaar071d4272004-06-13 20:20:40 +00001530
1531 if (info != (guint)TARGET_STRING
Bram Moolenaar3a6eaa52009-06-16 13:23:06 +00001532 && (!clip_html || info != (guint)TARGET_HTML)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001533 && info != (guint)TARGET_UTF8_STRING
1534 && info != (guint)TARGET_VIMENC
Bram Moolenaar071d4272004-06-13 20:20:40 +00001535 && info != (guint)TARGET_VIM
1536 && info != (guint)TARGET_COMPOUND_TEXT
lilydjwg94ff09a2024-01-29 20:14:01 +01001537 && info != (guint)TARGET_TEXT_PLAIN
1538 && info != (guint)TARGET_TEXT_PLAIN_UTF8
Bram Moolenaar071d4272004-06-13 20:20:40 +00001539 && info != (guint)TARGET_TEXT)
1540 return;
1541
Bram Moolenaar30613902019-12-01 22:11:18 +01001542 // get the selection from the '*'/'+' register
Bram Moolenaar071d4272004-06-13 20:20:40 +00001543 clip_get_selection(cbd);
1544
1545 motion_type = clip_convert_selection(&string, &tmplen, cbd);
1546 if (motion_type < 0 || string == NULL)
1547 return;
Bram Moolenaar30613902019-12-01 22:11:18 +01001548 // Due to int arguments we can't handle more than G_MAXINT. Also
1549 // reserve one extra byte for NUL or the motion type; just in case.
1550 // (Not that pasting 2G of text is ever going to work, but... ;-)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001551 length = MIN(tmplen, (long_u)(G_MAXINT - 1));
1552
1553 if (info == (guint)TARGET_VIM)
1554 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02001555 tmpbuf = alloc(length + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001556 if (tmpbuf != NULL)
1557 {
1558 tmpbuf[0] = motion_type;
1559 mch_memmove(tmpbuf + 1, string, (size_t)length);
1560 }
Bram Moolenaar30613902019-12-01 22:11:18 +01001561 // For our own format, the first byte contains the motion type
Bram Moolenaar071d4272004-06-13 20:20:40 +00001562 ++length;
1563 vim_free(string);
1564 string = tmpbuf;
1565 type = vim_atom;
1566 }
1567
Bram Moolenaar3a6eaa52009-06-16 13:23:06 +00001568 else if (info == (guint)TARGET_HTML)
1569 {
1570 vimconv_T conv;
1571
Bram Moolenaar30613902019-12-01 22:11:18 +01001572 // Since we get utf-16, we probably should set it as well.
Bram Moolenaar3a6eaa52009-06-16 13:23:06 +00001573 conv.vc_type = CONV_NONE;
1574 convert_setup_ext(&conv, p_enc, TRUE, (char_u *)"utf-16le", FALSE);
1575 if (conv.vc_type != CONV_NONE)
1576 {
1577 tmpbuf = string_convert(&conv, string, &length);
1578 convert_setup(&conv, NULL, NULL);
1579 vim_free(string);
1580 string = tmpbuf;
1581 }
1582
Bram Moolenaar30613902019-12-01 22:11:18 +01001583 // Prepend the BOM: "fffe"
Bram Moolenaar3a6eaa52009-06-16 13:23:06 +00001584 if (string != NULL)
1585 {
1586 tmpbuf = alloc(length + 2);
Bram Moolenaar6ee96582019-04-27 22:06:37 +02001587 if (tmpbuf != NULL)
1588 {
1589 tmpbuf[0] = 0xff;
1590 tmpbuf[1] = 0xfe;
1591 mch_memmove(tmpbuf + 2, string, (size_t)length);
1592 vim_free(string);
1593 string = tmpbuf;
1594 length += 2;
1595 }
Bram Moolenaar3a6eaa52009-06-16 13:23:06 +00001596
Bram Moolenaar98921892016-02-23 17:14:37 +01001597#if !GTK_CHECK_VERSION(3,0,0)
Bram Moolenaar30613902019-12-01 22:11:18 +01001598 // Looks redundant even for GTK2 because these values are
1599 // overwritten by gtk_selection_data_set() that follows.
Bram Moolenaar3a6eaa52009-06-16 13:23:06 +00001600 selection_data->type = selection_data->target;
Bram Moolenaar30613902019-12-01 22:11:18 +01001601 selection_data->format = 16; // 16 bits per char
Bram Moolenaar98921892016-02-23 17:14:37 +01001602#endif
Bram Moolenaar3a6eaa52009-06-16 13:23:06 +00001603 gtk_selection_data_set(selection_data, html_atom, 16,
1604 string, length);
1605 vim_free(string);
1606 }
1607 return;
1608 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001609 else if (info == (guint)TARGET_VIMENC)
1610 {
1611 int l = STRLEN(p_enc);
1612
Bram Moolenaar30613902019-12-01 22:11:18 +01001613 // contents: motion_type 'encoding' NUL text
Bram Moolenaar964b3742019-05-24 18:54:09 +02001614 tmpbuf = alloc(length + l + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001615 if (tmpbuf != NULL)
1616 {
1617 tmpbuf[0] = motion_type;
1618 STRCPY(tmpbuf + 1, p_enc);
1619 mch_memmove(tmpbuf + l + 2, string, (size_t)length);
Bram Moolenaar6ee96582019-04-27 22:06:37 +02001620 length += l + 2;
1621 vim_free(string);
1622 string = tmpbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001623 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001624 type = vimenc_atom;
1625 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001626
Bram Moolenaar30613902019-12-01 22:11:18 +01001627 // gtk_selection_data_set_text() handles everything for us. This is
1628 // so easy and simple and cool, it'd be insane not to use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001629 else
1630 {
1631 if (output_conv.vc_type != CONV_NONE)
1632 {
1633 tmpbuf = string_convert(&output_conv, string, &length);
1634 vim_free(string);
1635 if (tmpbuf == NULL)
1636 return;
1637 string = tmpbuf;
1638 }
Bram Moolenaar30613902019-12-01 22:11:18 +01001639 // Validate the string to avoid runtime warnings
Bram Moolenaar071d4272004-06-13 20:20:40 +00001640 if (g_utf8_validate((const char *)string, (gssize)length, NULL))
1641 {
1642 gtk_selection_data_set_text(selection_data,
1643 (const char *)string, length);
1644 }
1645 vim_free(string);
1646 return;
1647 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001648
1649 if (string != NULL)
1650 {
Bram Moolenaar98921892016-02-23 17:14:37 +01001651#if !GTK_CHECK_VERSION(3,0,0)
Bram Moolenaar30613902019-12-01 22:11:18 +01001652 // Looks redundant even for GTK2 because these values are
1653 // overwritten by gtk_selection_data_set() that follows.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001654 selection_data->type = selection_data->target;
Bram Moolenaar30613902019-12-01 22:11:18 +01001655 selection_data->format = 8; // 8 bits per char
Bram Moolenaar98921892016-02-23 17:14:37 +01001656#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001657 gtk_selection_data_set(selection_data, type, 8, string, length);
1658 vim_free(string);
1659 }
1660}
1661
1662/*
Bram Moolenaar29695702012-05-18 17:03:18 +02001663 * Check if the GUI can be started. Called before gvimrc is sourced and
1664 * before fork().
1665 * Return OK or FAIL.
1666 */
1667 int
Bram Moolenaar717e1962016-08-10 21:28:44 +02001668gui_mch_early_init_check(int give_message)
Bram Moolenaar29695702012-05-18 17:03:18 +02001669{
1670 char_u *p;
1671
Bram Moolenaar30613902019-12-01 22:11:18 +01001672 // Guess that when $DISPLAY isn't set the GUI can't start.
Bram Moolenaar29695702012-05-18 17:03:18 +02001673 p = mch_getenv((char_u *)"DISPLAY");
1674 if (p == NULL || *p == NUL)
1675 {
1676 gui.dying = TRUE;
Bram Moolenaar717e1962016-08-10 21:28:44 +02001677 if (give_message)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001678 emsg(_((char *)e_cannot_open_display));
Bram Moolenaar29695702012-05-18 17:03:18 +02001679 return FAIL;
1680 }
1681 return OK;
1682}
1683
1684/*
1685 * Check if the GUI can be started. Called before gvimrc is sourced but after
1686 * fork().
Bram Moolenaar071d4272004-06-13 20:20:40 +00001687 * Return OK or FAIL.
1688 */
1689 int
1690gui_mch_init_check(void)
1691{
Bram Moolenaar3a466a82016-01-19 17:47:25 +01001692#ifdef USE_GRESOURCE
1693 static int res_registered = FALSE;
1694
1695 if (!res_registered)
1696 {
Bram Moolenaar30613902019-12-01 22:11:18 +01001697 // Call this function in the GUI process; otherwise, the resources
1698 // won't be available. Don't call it twice.
Bram Moolenaar3a466a82016-01-19 17:47:25 +01001699 res_registered = TRUE;
1700 gui_gtk_register_resource();
1701 }
1702#endif
1703
Bram Moolenaarf80663f2016-04-05 21:56:06 +02001704#if GTK_CHECK_VERSION(3,10,0)
Bram Moolenaar30613902019-12-01 22:11:18 +01001705 // Vim currently assumes that Gtk means X11, so it cannot use native Gtk
1706 // support for other backends such as Wayland.
Bram Moolenaarf80663f2016-04-05 21:56:06 +02001707 gdk_set_allowed_backends ("x11");
1708#endif
1709
Bram Moolenaar071d4272004-06-13 20:20:40 +00001710#ifdef FEAT_GUI_GNOME
1711 if (gtk_socket_id == 0)
1712 using_gnome = 1;
1713#endif
1714
Bram Moolenaar30613902019-12-01 22:11:18 +01001715 // This defaults to argv[0], but we want it to match the name of the
1716 // shipped gvim.desktop so that Vim's windows can be associated with this
1717 // file.
Bram Moolenaar8dd79012013-05-21 12:52:04 +02001718 g_set_prgname("gvim");
1719
Bram Moolenaar30613902019-12-01 22:11:18 +01001720 // Don't use gtk_init() or gnome_init(), it exits on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001721 if (!gtk_init_check(&gui_argc, &gui_argv))
1722 {
1723 gui.dying = TRUE;
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001724 emsg(_((char *)e_cannot_open_display));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001725 return FAIL;
1726 }
1727
1728 return OK;
1729}
1730
Bram Moolenaar30613902019-12-01 22:11:18 +01001731/////////////////////////////////////////////////////////////////////////////
1732// Mouse handling callbacks
Bram Moolenaar071d4272004-06-13 20:20:40 +00001733
1734
1735static guint mouse_click_timer = 0;
1736static int mouse_timed_out = TRUE;
1737
1738/*
1739 * Timer used to recognize multiple clicks of the mouse button
1740 */
Bram Moolenaar4ab79682017-08-27 14:50:47 +02001741 static timeout_cb_type
Bram Moolenaar071d4272004-06-13 20:20:40 +00001742mouse_click_timer_cb(gpointer data)
1743{
Bram Moolenaar30613902019-12-01 22:11:18 +01001744 // we don't use this information currently
Bram Moolenaar071d4272004-06-13 20:20:40 +00001745 int *timed_out = (int *) data;
1746
1747 *timed_out = TRUE;
Bram Moolenaar30613902019-12-01 22:11:18 +01001748 return FALSE; // don't happen again
Bram Moolenaar071d4272004-06-13 20:20:40 +00001749}
1750
Bram Moolenaar4ab79682017-08-27 14:50:47 +02001751static guint motion_repeat_timer = 0;
1752static int motion_repeat_offset = FALSE;
1753static timeout_cb_type motion_repeat_timer_cb(gpointer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001754
1755 static void
1756process_motion_notify(int x, int y, GdkModifierType state)
1757{
1758 int button;
1759 int_u vim_modifiers;
Bram Moolenaar98921892016-02-23 17:14:37 +01001760 GtkAllocation allocation;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001761
Chris Daltonee93e322021-11-23 12:27:48 +00001762 // Need to add GDK_BUTTON1_MASK state when dragging a touch.
1763 state |= dragging_button_state;
1764
Bram Moolenaar071d4272004-06-13 20:20:40 +00001765 button = (state & (GDK_BUTTON1_MASK | GDK_BUTTON2_MASK |
1766 GDK_BUTTON3_MASK | GDK_BUTTON4_MASK |
1767 GDK_BUTTON5_MASK))
1768 ? MOUSE_DRAG : ' ';
1769
Bram Moolenaar30613902019-12-01 22:11:18 +01001770 // If our pointer is currently hidden, then we should show it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001771 gui_mch_mousehide(FALSE);
1772
Bram Moolenaar30613902019-12-01 22:11:18 +01001773 // Just moving the rodent above the drawing area without any button
1774 // being pressed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001775 if (button != MOUSE_DRAG)
1776 {
1777 gui_mouse_moved(x, y);
1778 return;
1779 }
1780
Bram Moolenaar30613902019-12-01 22:11:18 +01001781 // translate modifier coding between the main engine and GTK
Bram Moolenaar19a09a12005-03-04 23:39:37 +00001782 vim_modifiers = modifiers_gdk2mouse(state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001783
Bram Moolenaar30613902019-12-01 22:11:18 +01001784 // inform the editor engine about the occurrence of this event
Bram Moolenaar071d4272004-06-13 20:20:40 +00001785 gui_send_mouse_event(button, x, y, FALSE, vim_modifiers);
1786
Bram Moolenaar071d4272004-06-13 20:20:40 +00001787 /*
1788 * Auto repeat timer handling.
1789 */
Bram Moolenaar98921892016-02-23 17:14:37 +01001790 gtk_widget_get_allocation(gui.drawarea, &allocation);
1791
1792 if (x < 0 || y < 0
1793 || x >= allocation.width
1794 || y >= allocation.height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001795 {
1796
1797 int dx;
1798 int dy;
1799 int offshoot;
1800 int delay = 10;
1801
Bram Moolenaar30613902019-12-01 22:11:18 +01001802 // Calculate the maximal distance of the cursor from the drawing area.
1803 // (offshoot can't become negative here!).
Bram Moolenaar98921892016-02-23 17:14:37 +01001804 dx = x < 0 ? -x : x - allocation.width;
1805 dy = y < 0 ? -y : y - allocation.height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001806
1807 offshoot = dx > dy ? dx : dy;
1808
Bram Moolenaar30613902019-12-01 22:11:18 +01001809 // Make a linearly decaying timer delay with a threshold of 5 at a
1810 // distance of 127 pixels from the main window.
1811 //
1812 // One could think endlessly about the most ergonomic variant here.
1813 // For example it could make sense to calculate the distance from the
1814 // drags start instead...
1815 //
1816 // Maybe a parabolic interpolation would suite us better here too...
Bram Moolenaar071d4272004-06-13 20:20:40 +00001817 if (offshoot > 127)
1818 {
Bram Moolenaar30613902019-12-01 22:11:18 +01001819 // 5 appears to be somehow near to my perceptual limits :-).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001820 delay = 5;
1821 }
1822 else
1823 {
1824 delay = (130 * (127 - offshoot)) / 127 + 5;
1825 }
1826
Bram Moolenaar30613902019-12-01 22:11:18 +01001827 // shoot again
Bram Moolenaar071d4272004-06-13 20:20:40 +00001828 if (!motion_repeat_timer)
Bram Moolenaar4ab79682017-08-27 14:50:47 +02001829 motion_repeat_timer = timeout_add(delay, motion_repeat_timer_cb,
1830 NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001831 }
1832}
1833
Bram Moolenaar98921892016-02-23 17:14:37 +01001834#if GTK_CHECK_VERSION(3,0,0)
1835 static GdkDevice *
1836gui_gtk_get_pointer_device(GtkWidget *widget)
1837{
1838 GdkWindow * const win = gtk_widget_get_window(widget);
1839 GdkDisplay * const dpy = gdk_window_get_display(win);
Bram Moolenaar30e12d22016-04-17 20:49:53 +02001840# if GTK_CHECK_VERSION(3,20,0)
1841 GdkSeat * const seat = gdk_display_get_default_seat(dpy);
1842 return gdk_seat_get_pointer(seat);
1843# else
Bram Moolenaar98921892016-02-23 17:14:37 +01001844 GdkDeviceManager * const mngr = gdk_display_get_device_manager(dpy);
1845 return gdk_device_manager_get_client_pointer(mngr);
Bram Moolenaar30e12d22016-04-17 20:49:53 +02001846# endif
Bram Moolenaar98921892016-02-23 17:14:37 +01001847}
1848
1849 static GdkWindow *
1850gui_gtk_get_pointer(GtkWidget *widget,
1851 gint *x,
1852 gint *y,
1853 GdkModifierType *state)
1854{
1855 GdkWindow * const win = gtk_widget_get_window(widget);
1856 GdkDevice * const dev = gui_gtk_get_pointer_device(widget);
1857 return gdk_window_get_device_position(win, dev , x, y, state);
1858}
1859
Bram Moolenaar2369c152016-03-04 23:08:25 +01001860# if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar98921892016-02-23 17:14:37 +01001861 static GdkWindow *
1862gui_gtk_window_at_position(GtkWidget *widget,
1863 gint *x,
1864 gint *y)
1865{
1866 GdkDevice * const dev = gui_gtk_get_pointer_device(widget);
1867 return gdk_device_get_window_at_position(dev, x, y);
1868}
Bram Moolenaar2369c152016-03-04 23:08:25 +01001869# endif
Bram Moolenaar30613902019-12-01 22:11:18 +01001870#else // !GTK_CHECK_VERSION(3,0,0)
Bram Moolenaar664323e2018-09-18 22:30:07 +02001871# define gui_gtk_get_pointer(wid, x, y, s) \
1872 gdk_window_get_pointer((wid)->window, x, y, s)
1873# define gui_gtk_window_at_position(wid, x, y) gdk_window_at_pointer(x, y)
Bram Moolenaar98921892016-02-23 17:14:37 +01001874#endif
1875
Bram Moolenaar071d4272004-06-13 20:20:40 +00001876/*
1877 * Timer used to recognize multiple clicks of the mouse button.
1878 */
Bram Moolenaar4ab79682017-08-27 14:50:47 +02001879 static timeout_cb_type
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001880motion_repeat_timer_cb(gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001881{
1882 int x;
1883 int y;
1884 GdkModifierType state;
1885
Bram Moolenaar98921892016-02-23 17:14:37 +01001886 gui_gtk_get_pointer(gui.drawarea, &x, &y, &state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001887
1888 if (!(state & (GDK_BUTTON1_MASK | GDK_BUTTON2_MASK |
1889 GDK_BUTTON3_MASK | GDK_BUTTON4_MASK |
1890 GDK_BUTTON5_MASK)))
1891 {
1892 motion_repeat_timer = 0;
1893 return FALSE;
1894 }
1895
Bram Moolenaar30613902019-12-01 22:11:18 +01001896 // If there already is a mouse click in the input buffer, wait another
1897 // time (otherwise we would create a backlog of clicks)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001898 if (vim_used_in_input_buf() > 10)
1899 return TRUE;
1900
1901 motion_repeat_timer = 0;
1902
1903 /*
1904 * Fake a motion event.
1905 * Trick: Pretend the mouse moved to the next character on every other
1906 * event, otherwise drag events will be discarded, because they are still
1907 * in the same character.
1908 */
1909 if (motion_repeat_offset)
1910 x += gui.char_width;
1911
1912 motion_repeat_offset = !motion_repeat_offset;
1913 process_motion_notify(x, y, state);
1914
Bram Moolenaar30613902019-12-01 22:11:18 +01001915 // Don't happen again. We will get reinstalled in the synthetic event
1916 // if needed -- thus repeating should still work.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001917 return FALSE;
1918}
1919
Bram Moolenaar071d4272004-06-13 20:20:40 +00001920 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001921motion_notify_event(GtkWidget *widget,
1922 GdkEventMotion *event,
1923 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001924{
1925 if (event->is_hint)
1926 {
1927 int x;
1928 int y;
1929 GdkModifierType state;
1930
Bram Moolenaar98921892016-02-23 17:14:37 +01001931 gui_gtk_get_pointer(widget, &x, &y, &state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001932 process_motion_notify(x, y, state);
1933 }
1934 else
1935 {
1936 process_motion_notify((int)event->x, (int)event->y,
1937 (GdkModifierType)event->state);
1938 }
1939
Bram Moolenaar30613902019-12-01 22:11:18 +01001940 return TRUE; // handled
Bram Moolenaar071d4272004-06-13 20:20:40 +00001941}
1942
1943
1944/*
1945 * Mouse button handling. Note please that we are capturing multiple click's
1946 * by our own timeout mechanism instead of the one provided by GTK+ itself.
1947 * This is due to the way the generic VIM code is recognizing multiple clicks.
1948 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001949 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001950button_press_event(GtkWidget *widget,
1951 GdkEventButton *event,
1952 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001953{
1954 int button;
1955 int repeated_click = FALSE;
1956 int x, y;
1957 int_u vim_modifiers;
1958
Bram Moolenaar20892c12011-06-26 04:49:00 +02001959 gui.event_time = event->time;
Bram Moolenaar7cfea752010-06-22 06:07:12 +02001960
Bram Moolenaar30613902019-12-01 22:11:18 +01001961 // Make sure we have focus now we've been selected
Bram Moolenaar98921892016-02-23 17:14:37 +01001962 if (gtk_socket_id != 0 && !gtk_widget_has_focus(widget))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001963 gtk_widget_grab_focus(widget);
1964
1965 /*
1966 * Don't let additional events about multiple clicks send by GTK to us
1967 * after the initial button press event confuse us.
1968 */
1969 if (event->type != GDK_BUTTON_PRESS)
1970 return FALSE;
1971
1972 x = event->x;
1973 y = event->y;
1974
Bram Moolenaar30613902019-12-01 22:11:18 +01001975 // Handle multiple clicks
Bram Moolenaar071d4272004-06-13 20:20:40 +00001976 if (!mouse_timed_out && mouse_click_timer)
1977 {
Bram Moolenaar4ab79682017-08-27 14:50:47 +02001978 timeout_remove(mouse_click_timer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001979 mouse_click_timer = 0;
1980 repeated_click = TRUE;
1981 }
1982
1983 mouse_timed_out = FALSE;
Bram Moolenaar4ab79682017-08-27 14:50:47 +02001984 mouse_click_timer = timeout_add(p_mouset, mouse_click_timer_cb,
1985 &mouse_timed_out);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001986
1987 switch (event->button)
1988 {
Bram Moolenaar30613902019-12-01 22:11:18 +01001989 // Keep in sync with gui_x11.c.
1990 // Buttons 4-7 are handled in scroll_event()
Chris Daltonee93e322021-11-23 12:27:48 +00001991 case 1:
1992 button = MOUSE_LEFT;
1993 // needed for touch-drag
1994 dragging_button_state |= GDK_BUTTON1_MASK;
1995 break;
Bram Moolenaar88e484b2015-11-24 15:38:44 +01001996 case 2: button = MOUSE_MIDDLE; break;
1997 case 3: button = MOUSE_RIGHT; break;
1998 case 8: button = MOUSE_X1; break;
1999 case 9: button = MOUSE_X2; break;
2000 default:
Bram Moolenaar30613902019-12-01 22:11:18 +01002001 return FALSE; // Unknown button
Bram Moolenaar071d4272004-06-13 20:20:40 +00002002 }
2003
2004#ifdef FEAT_XIM
Bram Moolenaar30613902019-12-01 22:11:18 +01002005 // cancel any preediting
Bram Moolenaar071d4272004-06-13 20:20:40 +00002006 if (im_is_preediting())
2007 xim_reset();
2008#endif
2009
Bram Moolenaar19a09a12005-03-04 23:39:37 +00002010 vim_modifiers = modifiers_gdk2mouse(event->state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002011
2012 gui_send_mouse_event(button, x, y, repeated_click, vim_modifiers);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002013
2014 return TRUE;
2015}
2016
Bram Moolenaar071d4272004-06-13 20:20:40 +00002017/*
Bram Moolenaar182c5be2010-06-25 05:37:59 +02002018 * GTK+ 2 abstracts scrolling via the GdkEventScroll.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002019 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002020 static gboolean
Bram Moolenaarb85cb212009-05-17 14:24:23 +00002021scroll_event(GtkWidget *widget,
2022 GdkEventScroll *event,
2023 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002024{
2025 int button;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00002026 int_u vim_modifiers;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002027
Bram Moolenaar98921892016-02-23 17:14:37 +01002028 if (gtk_socket_id != 0 && !gtk_widget_has_focus(widget))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002029 gtk_widget_grab_focus(widget);
2030
2031 switch (event->direction)
2032 {
2033 case GDK_SCROLL_UP:
2034 button = MOUSE_4;
2035 break;
2036 case GDK_SCROLL_DOWN:
2037 button = MOUSE_5;
2038 break;
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02002039 case GDK_SCROLL_LEFT:
2040 button = MOUSE_7;
2041 break;
2042 case GDK_SCROLL_RIGHT:
2043 button = MOUSE_6;
2044 break;
Bram Moolenaar30613902019-12-01 22:11:18 +01002045 default: // This shouldn't happen
Bram Moolenaar071d4272004-06-13 20:20:40 +00002046 return FALSE;
2047 }
2048
2049# ifdef FEAT_XIM
Bram Moolenaar30613902019-12-01 22:11:18 +01002050 // cancel any preediting
Bram Moolenaar071d4272004-06-13 20:20:40 +00002051 if (im_is_preediting())
2052 xim_reset();
2053# endif
2054
Bram Moolenaar19a09a12005-03-04 23:39:37 +00002055 vim_modifiers = modifiers_gdk2mouse(event->state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002056
2057 gui_send_mouse_event(button, (int)event->x, (int)event->y,
2058 FALSE, vim_modifiers);
2059
Bram Moolenaar071d4272004-06-13 20:20:40 +00002060 return TRUE;
2061}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002062
2063
Bram Moolenaar071d4272004-06-13 20:20:40 +00002064 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +00002065button_release_event(GtkWidget *widget UNUSED,
2066 GdkEventButton *event,
2067 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002068{
2069 int x, y;
2070 int_u vim_modifiers;
2071
Bram Moolenaar20892c12011-06-26 04:49:00 +02002072 gui.event_time = event->time;
Bram Moolenaar7cfea752010-06-22 06:07:12 +02002073
Bram Moolenaar30613902019-12-01 22:11:18 +01002074 // Remove any motion "machine gun" timers used for automatic further
2075 // extension of allocation areas if outside of the applications window
2076 // area .
Bram Moolenaar071d4272004-06-13 20:20:40 +00002077 if (motion_repeat_timer)
2078 {
Bram Moolenaar4ab79682017-08-27 14:50:47 +02002079 timeout_remove(motion_repeat_timer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002080 motion_repeat_timer = 0;
2081 }
2082
2083 x = event->x;
2084 y = event->y;
2085
Bram Moolenaar19a09a12005-03-04 23:39:37 +00002086 vim_modifiers = modifiers_gdk2mouse(event->state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002087
2088 gui_send_mouse_event(MOUSE_RELEASE, x, y, FALSE, vim_modifiers);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002089
Chris Daltonee93e322021-11-23 12:27:48 +00002090 switch (event->button)
2091 {
2092 case 1: // MOUSE_LEFT
2093 dragging_button_state = 0;
2094 break;
2095 }
2096
Bram Moolenaar071d4272004-06-13 20:20:40 +00002097 return TRUE;
2098}
2099
2100
2101#ifdef FEAT_DND
Bram Moolenaar30613902019-12-01 22:11:18 +01002102/////////////////////////////////////////////////////////////////////////////
2103// Drag aNd Drop support handlers.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002104
2105/*
2106 * Count how many items there may be and separate them with a NUL.
2107 * Apparently the items are separated with \r\n. This is not documented,
2108 * thus be careful not to go past the end. Also allow separation with
2109 * NUL characters.
2110 */
2111 static int
2112count_and_decode_uri_list(char_u *out, char_u *raw, int len)
2113{
2114 int i;
2115 char_u *p = out;
2116 int count = 0;
2117
2118 for (i = 0; i < len; ++i)
2119 {
2120 if (raw[i] == NUL || raw[i] == '\n' || raw[i] == '\r')
2121 {
2122 if (p > out && p[-1] != NUL)
2123 {
2124 ++count;
2125 *p++ = NUL;
2126 }
2127 }
2128 else if (raw[i] == '%' && i + 2 < len && hexhex2nr(raw + i + 1) > 0)
2129 {
2130 *p++ = hexhex2nr(raw + i + 1);
2131 i += 2;
2132 }
2133 else
2134 *p++ = raw[i];
2135 }
2136 if (p > out && p[-1] != NUL)
2137 {
Bram Moolenaar30613902019-12-01 22:11:18 +01002138 *p = NUL; // last item didn't have \r or \n
Bram Moolenaar071d4272004-06-13 20:20:40 +00002139 ++count;
2140 }
2141 return count;
2142}
2143
2144/*
2145 * Parse NUL separated "src" strings. Make it an array "outlist" form. On
2146 * this process, URI which protocol is not "file:" are removed. Return
2147 * length of array (less than "max").
2148 */
2149 static int
2150filter_uri_list(char_u **outlist, int max, char_u *src)
2151{
2152 int i, j;
2153
2154 for (i = j = 0; i < max; ++i)
2155 {
2156 outlist[i] = NULL;
2157 if (STRNCMP(src, "file:", 5) == 0)
2158 {
2159 src += 5;
2160 if (STRNCMP(src, "//localhost", 11) == 0)
2161 src += 11;
2162 while (src[0] == '/' && src[1] == '/')
2163 ++src;
2164 outlist[j++] = vim_strsave(src);
2165 }
2166 src += STRLEN(src) + 1;
2167 }
2168 return j;
2169}
2170
2171 static char_u **
2172parse_uri_list(int *count, char_u *data, int len)
2173{
2174 int n = 0;
2175 char_u *tmp = NULL;
Bram Moolenaar945ec092016-06-08 21:17:43 +02002176 char_u **array = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002177
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002178 if (data != NULL && len > 0 && (tmp = alloc(len + 1)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002179 {
2180 n = count_and_decode_uri_list(tmp, data, len);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002181 if (n > 0 && (array = ALLOC_MULT(char_u *, n)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002182 n = filter_uri_list(array, n, tmp);
2183 }
2184 vim_free(tmp);
2185 *count = n;
2186 return array;
2187}
2188
2189 static void
2190drag_handle_uri_list(GdkDragContext *context,
2191 GtkSelectionData *data,
2192 guint time_,
2193 GdkModifierType state,
2194 gint x,
2195 gint y)
2196{
2197 char_u **fnames;
2198 int nfiles = 0;
2199
Bram Moolenaar98921892016-02-23 17:14:37 +01002200 fnames = parse_uri_list(&nfiles,
2201 (char_u *)gtk_selection_data_get_data(data),
2202 gtk_selection_data_get_length(data));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002203
2204 if (fnames != NULL && nfiles > 0)
2205 {
Bram Moolenaar19a09a12005-03-04 23:39:37 +00002206 int_u modifiers;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002207
Bram Moolenaar30613902019-12-01 22:11:18 +01002208 gtk_drag_finish(context, TRUE, FALSE, time_); // accept
Bram Moolenaar071d4272004-06-13 20:20:40 +00002209
Bram Moolenaar19a09a12005-03-04 23:39:37 +00002210 modifiers = modifiers_gdk2mouse(state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002211
2212 gui_handle_drop(x, y, modifiers, fnames, nfiles);
2213 }
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002214 else
2215 vim_free(fnames);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002216}
2217
2218 static void
2219drag_handle_text(GdkDragContext *context,
2220 GtkSelectionData *data,
2221 guint time_,
2222 GdkModifierType state)
2223{
2224 char_u dropkey[6] = {CSI, KS_MODIFIER, 0, CSI, KS_EXTRA, (char_u)KE_DROP};
2225 char_u *text;
2226 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002227 char_u *tmpbuf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002228
Bram Moolenaar98921892016-02-23 17:14:37 +01002229 text = (char_u *)gtk_selection_data_get_data(data);
2230 len = gtk_selection_data_get_length(data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002231
Bram Moolenaar98921892016-02-23 17:14:37 +01002232 if (gtk_selection_data_get_data_type(data) == utf8_string_atom)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002233 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002234 if (input_conv.vc_type != CONV_NONE)
2235 tmpbuf = string_convert(&input_conv, text, &len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002236 if (tmpbuf != NULL)
2237 text = tmpbuf;
2238 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002239
2240 dnd_yank_drag_data(text, (long)len);
Bram Moolenaar30613902019-12-01 22:11:18 +01002241 gtk_drag_finish(context, TRUE, FALSE, time_); // accept
Bram Moolenaar071d4272004-06-13 20:20:40 +00002242 vim_free(tmpbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002243
Bram Moolenaar19a09a12005-03-04 23:39:37 +00002244 dropkey[2] = modifiers_gdk2vim(state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002245
2246 if (dropkey[2] != 0)
2247 add_to_input_buf(dropkey, (int)sizeof(dropkey));
2248 else
2249 add_to_input_buf(dropkey + 3, (int)(sizeof(dropkey) - 3));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002250}
2251
2252/*
2253 * DND receiver.
2254 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002255 static void
2256drag_data_received_cb(GtkWidget *widget,
2257 GdkDragContext *context,
2258 gint x,
2259 gint y,
2260 GtkSelectionData *data,
2261 guint info,
2262 guint time_,
Bram Moolenaarb85cb212009-05-17 14:24:23 +00002263 gpointer user_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002264{
2265 GdkModifierType state;
2266
Bram Moolenaar30613902019-12-01 22:11:18 +01002267 // Guard against trash
Bram Moolenaar98921892016-02-23 17:14:37 +01002268 const guchar * const data_data = gtk_selection_data_get_data(data);
2269 const gint data_length = gtk_selection_data_get_length(data);
2270 const gint data_format = gtk_selection_data_get_format(data);
2271
2272 if (data_data == NULL
2273 || data_length <= 0
2274 || data_format != 8
2275 || data_data[data_length] != '\0')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002276 {
2277 gtk_drag_finish(context, FALSE, FALSE, time_);
2278 return;
2279 }
2280
Bram Moolenaar30613902019-12-01 22:11:18 +01002281 // Get the current modifier state for proper distinguishment between
2282 // different operations later.
Bram Moolenaar98921892016-02-23 17:14:37 +01002283 gui_gtk_get_pointer(widget, NULL, NULL, &state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002284
Bram Moolenaar30613902019-12-01 22:11:18 +01002285 // Not sure about the role of "text/plain" here...
Bram Moolenaar071d4272004-06-13 20:20:40 +00002286 if (info == (guint)TARGET_TEXT_URI_LIST)
2287 drag_handle_uri_list(context, data, time_, state, x, y);
2288 else
2289 drag_handle_text(context, data, time_, state);
2290
2291}
Bram Moolenaar30613902019-12-01 22:11:18 +01002292#endif // FEAT_DND
Bram Moolenaar071d4272004-06-13 20:20:40 +00002293
2294
Bram Moolenaarf96ae0b2019-07-28 15:21:55 +02002295#if defined(USE_GNOME_SESSION)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002296/*
2297 * GnomeClient interact callback. Check for unsaved buffers that cannot
2298 * be abandoned and pop up a dialog asking the user for confirmation if
2299 * necessary.
2300 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002301 static void
Bram Moolenaar30bb4142010-05-17 22:07:15 +02002302sm_client_check_changed_any(GnomeClient *client UNUSED,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002303 gint key,
Bram Moolenaar30bb4142010-05-17 22:07:15 +02002304 GnomeDialogType type UNUSED,
2305 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002306{
2307 cmdmod_T save_cmdmod;
2308 gboolean shutdown_cancelled;
2309
2310 save_cmdmod = cmdmod;
2311
2312# ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +02002313 cmdmod.cmod_flags |= CMOD_BROWSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002314# endif
2315# if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaare1004402020-10-24 20:49:43 +02002316 cmdmod.cmod_flags |= CMOD_CONFIRM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002317# endif
2318 /*
2319 * If there are changed buffers, present the user with
2320 * a dialog if possible, otherwise give an error message.
2321 */
Bram Moolenaar027387f2016-01-02 22:25:52 +01002322 shutdown_cancelled = check_changed_any(FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002323
2324 exiting = FALSE;
2325 cmdmod = save_cmdmod;
Bram Moolenaar30613902019-12-01 22:11:18 +01002326 setcursor(); // position the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002327 out_flush();
2328 /*
2329 * If the user hit the [Cancel] button the whole shutdown
2330 * will be cancelled. Wow, quite powerful feature (:
2331 */
2332 gnome_interaction_key_return(key, shutdown_cancelled);
2333}
2334
2335/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002336 * "save_yourself" signal handler. Initiate an interaction to ask the user
2337 * for confirmation if necessary. Save the current editing session and tell
2338 * the session manager how to restart Vim.
2339 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002340 static gboolean
2341sm_client_save_yourself(GnomeClient *client,
Bram Moolenaar30bb4142010-05-17 22:07:15 +02002342 gint phase UNUSED,
2343 GnomeSaveStyle save_style UNUSED,
2344 gboolean shutdown UNUSED,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002345 GnomeInteractStyle interact_style,
Bram Moolenaar30bb4142010-05-17 22:07:15 +02002346 gboolean fast UNUSED,
2347 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002348{
2349 static const char suffix[] = "-session.vim";
2350 char *session_file;
2351 unsigned int len;
2352 gboolean success;
2353
Bram Moolenaar30613902019-12-01 22:11:18 +01002354 // Always request an interaction if possible. check_changed_any()
2355 // won't actually show a dialog unless any buffers have been modified.
2356 // There doesn't seem to be an obvious way to check that without
2357 // automatically firing the dialog. Anyway, it works just fine.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002358 if (interact_style == GNOME_INTERACT_ANY)
2359 gnome_client_request_interaction(client, GNOME_DIALOG_NORMAL,
2360 &sm_client_check_changed_any,
2361 NULL);
2362 out_flush();
Bram Moolenaar30613902019-12-01 22:11:18 +01002363 ml_sync_all(FALSE, FALSE); // preserve all swap files
Bram Moolenaar071d4272004-06-13 20:20:40 +00002364
Bram Moolenaar30613902019-12-01 22:11:18 +01002365 // The path is unique for each session save. We do neither know nor care
2366 // which session script will actually be used later. This decision is in
2367 // the domain of the session manager.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002368 session_file = gnome_config_get_real_path(
2369 gnome_client_get_config_prefix(client));
2370 len = strlen(session_file);
2371
2372 if (len > 0 && session_file[len-1] == G_DIR_SEPARATOR)
Bram Moolenaar30613902019-12-01 22:11:18 +01002373 --len; // get rid of the superfluous trailing '/'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002374
2375 session_file = g_renew(char, session_file, len + sizeof(suffix));
2376 memcpy(session_file + len, suffix, sizeof(suffix));
2377
2378 success = write_session_file((char_u *)session_file);
2379
2380 if (success)
2381 {
2382 const char *argv[8];
2383 int i;
2384
Bram Moolenaar30613902019-12-01 22:11:18 +01002385 // Tell the session manager how to wipe out the stored session data.
2386 // This isn't as dangerous as it looks, don't worry :) session_file
2387 // is a unique absolute filename. Usually it'll be something like
2388 // `/home/user/.gnome2/vim-XXXXXX-session.vim'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002389 i = 0;
2390 argv[i++] = "rm";
2391 argv[i++] = session_file;
2392 argv[i] = NULL;
2393
2394 gnome_client_set_discard_command(client, i, (char **)argv);
2395
Bram Moolenaar30613902019-12-01 22:11:18 +01002396 // Tell the session manager how to restore the just saved session.
2397 // This is easily done thanks to Vim's -S option. Pass the -f flag
2398 // since there's no need to fork -- it might even cause confusion.
2399 // Also pass the window role to give the WM something to match on.
2400 // The role is set in gui_mch_open(), thus should _never_ be NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002401 i = 0;
2402 argv[i++] = restart_command;
2403 argv[i++] = "-f";
2404 argv[i++] = "-g";
Bram Moolenaar071d4272004-06-13 20:20:40 +00002405 argv[i++] = "--role";
2406 argv[i++] = gtk_window_get_role(GTK_WINDOW(gui.mainwin));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002407 argv[i++] = "-S";
2408 argv[i++] = session_file;
2409 argv[i] = NULL;
2410
2411 gnome_client_set_restart_command(client, i, (char **)argv);
2412 gnome_client_set_clone_command(client, 0, NULL);
2413 }
2414
2415 g_free(session_file);
2416
2417 return success;
2418}
2419
2420/*
2421 * Called when the session manager wants us to die. There isn't much to save
2422 * here since "save_yourself" has been emitted before (unless serious trouble
2423 * is happening).
2424 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002425 static void
Bram Moolenaar30bb4142010-05-17 22:07:15 +02002426sm_client_die(GnomeClient *client UNUSED, gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002427{
Bram Moolenaar30613902019-12-01 22:11:18 +01002428 // Don't write messages to the GUI anymore
Bram Moolenaar071d4272004-06-13 20:20:40 +00002429 full_screen = FALSE;
2430
Bram Moolenaarc93e7912008-07-08 10:46:08 +00002431 vim_strncpy(IObuff, (char_u *)
Bram Moolenaarce0842a2005-07-18 21:58:11 +00002432 _("Vim: Received \"die\" request from session manager\n"),
Bram Moolenaarc93e7912008-07-08 10:46:08 +00002433 IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002434 preserve_exit();
2435}
2436
2437/*
2438 * Connect our signal handlers to be notified on session save and shutdown.
2439 */
2440 static void
2441setup_save_yourself(void)
2442{
2443 GnomeClient *client;
2444
2445 client = gnome_master_client();
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00002446 if (client == NULL)
2447 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002448
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00002449 // Must use the deprecated gtk_signal_connect() for compatibility
2450 // with GNOME 1. Arrgh, zombies!
2451 gtk_signal_connect(GTK_OBJECT(client), "save_yourself",
2452 GTK_SIGNAL_FUNC(&sm_client_save_yourself), NULL);
2453 gtk_signal_connect(GTK_OBJECT(client), "die",
2454 GTK_SIGNAL_FUNC(&sm_client_die), NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002455}
2456
Bram Moolenaarf96ae0b2019-07-28 15:21:55 +02002457#else // !USE_GNOME_SESSION
Bram Moolenaar071d4272004-06-13 20:20:40 +00002458
2459# ifdef USE_XSMP
2460/*
2461 * GTK tells us that XSMP needs attention
2462 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002463 static gboolean
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002464local_xsmp_handle_requests(
2465 GIOChannel *source UNUSED,
2466 GIOCondition condition,
2467 gpointer data)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002468{
2469 if (condition == G_IO_IN)
2470 {
Bram Moolenaar30613902019-12-01 22:11:18 +01002471 // Do stuff; maybe close connection
Bram Moolenaar071d4272004-06-13 20:20:40 +00002472 if (xsmp_handle_requests() == FAIL)
2473 g_io_channel_unref((GIOChannel *)data);
2474 return TRUE;
2475 }
Bram Moolenaar30613902019-12-01 22:11:18 +01002476 // Error
Bram Moolenaar071d4272004-06-13 20:20:40 +00002477 g_io_channel_unref((GIOChannel *)data);
2478 xsmp_close();
2479 return TRUE;
2480}
Bram Moolenaar30613902019-12-01 22:11:18 +01002481# endif // USE_XSMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00002482
2483/*
2484 * Setup the WM_PROTOCOLS to indicate we want the WM_SAVE_YOURSELF event.
2485 * This is an ugly use of X functions. GTK doesn't offer an alternative.
2486 */
2487 static void
2488setup_save_yourself(void)
2489{
2490 Atom *existing_atoms = NULL;
2491 int count = 0;
2492
Bram Moolenaar98921892016-02-23 17:14:37 +01002493# ifdef USE_XSMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00002494 if (xsmp_icefd != -1)
2495 {
2496 /*
2497 * Use XSMP is preference to legacy WM_SAVE_YOURSELF;
2498 * set up GTK IO monitor
2499 */
2500 GIOChannel *g_io = g_io_channel_unix_new(xsmp_icefd);
2501
2502 g_io_add_watch(g_io, G_IO_IN | G_IO_ERR | G_IO_HUP,
2503 local_xsmp_handle_requests, (gpointer)g_io);
Bram Moolenaar98921892016-02-23 17:14:37 +01002504 g_io_channel_unref(g_io);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002505 }
2506 else
Bram Moolenaar98921892016-02-23 17:14:37 +01002507# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002508 {
Bram Moolenaar30613902019-12-01 22:11:18 +01002509 // Fall back to old method
Bram Moolenaar071d4272004-06-13 20:20:40 +00002510
Bram Moolenaar30613902019-12-01 22:11:18 +01002511 // first get the existing value
Bram Moolenaar98921892016-02-23 17:14:37 +01002512 GdkWindow * const mainwin_win = gtk_widget_get_window(gui.mainwin);
2513
2514 if (XGetWMProtocols(GDK_WINDOW_XDISPLAY(mainwin_win),
2515 GDK_WINDOW_XID(mainwin_win),
2516 &existing_atoms, &count))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002517 {
2518 Atom *new_atoms;
2519 Atom save_yourself_xatom;
2520 int i;
2521
2522 save_yourself_xatom = GET_X_ATOM(save_yourself_atom);
2523
Bram Moolenaar30613902019-12-01 22:11:18 +01002524 // check if WM_SAVE_YOURSELF isn't there yet
Bram Moolenaar071d4272004-06-13 20:20:40 +00002525 for (i = 0; i < count; ++i)
2526 if (existing_atoms[i] == save_yourself_xatom)
2527 break;
2528
2529 if (i == count)
2530 {
Bram Moolenaar30613902019-12-01 22:11:18 +01002531 // allocate an Atoms array which is one item longer
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002532 new_atoms = ALLOC_MULT(Atom, count + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002533 if (new_atoms != NULL)
2534 {
2535 memcpy(new_atoms, existing_atoms, count * sizeof(Atom));
2536 new_atoms[count] = save_yourself_xatom;
Bram Moolenaar98921892016-02-23 17:14:37 +01002537 XSetWMProtocols(GDK_WINDOW_XDISPLAY(mainwin_win),
2538 GDK_WINDOW_XID(mainwin_win),
Bram Moolenaar071d4272004-06-13 20:20:40 +00002539 new_atoms, count + 1);
2540 vim_free(new_atoms);
2541 }
2542 }
2543 XFree(existing_atoms);
2544 }
2545 }
2546}
2547
Bram Moolenaar071d4272004-06-13 20:20:40 +00002548/*
2549 * Installing a global event filter seems to be the only way to catch
2550 * client messages of type WM_PROTOCOLS without overriding GDK's own
2551 * client message event filter. Well, that's still better than trying
2552 * to guess what the GDK filter had done if it had been invoked instead
Bram Moolenaar071d4272004-06-13 20:20:40 +00002553 *
2554 * GTK2_FIXME: This doesn't seem to work. For some reason we never
2555 * receive WM_SAVE_YOURSELF even though everything is set up correctly.
2556 * I have the nasty feeling modern session managers just don't send this
2557 * deprecated message anymore. Addition: confirmed by several people.
2558 *
2559 * The GNOME session support is much cooler anyway. Unlike this ugly
2560 * WM_SAVE_YOURSELF hack it actually stores the session... And yes,
2561 * it should work with KDE as well.
2562 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002563 static GdkFilterReturn
Bram Moolenaarb85cb212009-05-17 14:24:23 +00002564global_event_filter(GdkXEvent *xev,
2565 GdkEvent *event UNUSED,
2566 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002567{
2568 XEvent *xevent = (XEvent *)xev;
2569
2570 if (xevent != NULL
2571 && xevent->type == ClientMessage
2572 && xevent->xclient.message_type == GET_X_ATOM(wm_protocols_atom)
Bram Moolenaarb85cb212009-05-17 14:24:23 +00002573 && (long_u)xevent->xclient.data.l[0]
2574 == GET_X_ATOM(save_yourself_atom))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002575 {
2576 out_flush();
Bram Moolenaar30613902019-12-01 22:11:18 +01002577 ml_sync_all(FALSE, FALSE); // preserve all swap files
Bram Moolenaar071d4272004-06-13 20:20:40 +00002578 /*
2579 * Set the window's WM_COMMAND property, to let the window manager
2580 * know we are done saving ourselves. We don't want to be
2581 * restarted, thus set argv to NULL.
2582 */
Bram Moolenaar98921892016-02-23 17:14:37 +01002583 XSetCommand(GDK_WINDOW_XDISPLAY(gtk_widget_get_window(gui.mainwin)),
2584 GDK_WINDOW_XID(gtk_widget_get_window(gui.mainwin)),
Bram Moolenaar071d4272004-06-13 20:20:40 +00002585 NULL, 0);
2586 return GDK_FILTER_REMOVE;
2587 }
2588
2589 return GDK_FILTER_CONTINUE;
2590}
Bram Moolenaarf96ae0b2019-07-28 15:21:55 +02002591#endif // !USE_GNOME_SESSION
Bram Moolenaar071d4272004-06-13 20:20:40 +00002592
2593
2594/*
2595 * Setup the window icon & xcmdsrv comm after the main window has been realized.
2596 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002597 static void
Bram Moolenaarb85cb212009-05-17 14:24:23 +00002598mainwin_realize(GtkWidget *widget UNUSED, gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002599{
Bram Moolenaar30613902019-12-01 22:11:18 +01002600// If you get an error message here, you still need to unpack the runtime
2601// archive!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002602#ifdef magick
2603# undef magick
2604#endif
Bram Moolenaar30613902019-12-01 22:11:18 +01002605 // A bit hackish, but avoids casting later and allows optimization
Bram Moolenaar071d4272004-06-13 20:20:40 +00002606# define static static const
Bram Moolenaar071d4272004-06-13 20:20:40 +00002607#define magick vim32x32
2608#include "../runtime/vim32x32.xpm"
2609#undef magick
2610#define magick vim16x16
2611#include "../runtime/vim16x16.xpm"
2612#undef magick
2613#define magick vim48x48
2614#include "../runtime/vim48x48.xpm"
2615#undef magick
Bram Moolenaar071d4272004-06-13 20:20:40 +00002616# undef static
Bram Moolenaar071d4272004-06-13 20:20:40 +00002617
Bram Moolenaar98921892016-02-23 17:14:37 +01002618 GdkWindow * const mainwin_win = gtk_widget_get_window(gui.mainwin);
Bram Moolenaar98921892016-02-23 17:14:37 +01002619
Bram Moolenaar30613902019-12-01 22:11:18 +01002620 // When started with "--echo-wid" argument, write window ID on stdout.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002621 if (echo_wid_arg)
2622 {
Bram Moolenaar98921892016-02-23 17:14:37 +01002623 printf("WID: %ld\n", (long)GDK_WINDOW_XID(mainwin_win));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002624 fflush(stdout);
2625 }
2626
2627 if (vim_strchr(p_go, GO_ICON) != NULL)
2628 {
2629 /*
2630 * Add an icon to the main window. For fun and convenience of the user.
2631 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002632 GList *icons = NULL;
2633
2634 icons = g_list_prepend(icons, gdk_pixbuf_new_from_xpm_data(vim16x16));
2635 icons = g_list_prepend(icons, gdk_pixbuf_new_from_xpm_data(vim32x32));
2636 icons = g_list_prepend(icons, gdk_pixbuf_new_from_xpm_data(vim48x48));
2637
2638 gtk_window_set_icon_list(GTK_WINDOW(gui.mainwin), icons);
2639
Bram Moolenaar81a4cf42020-09-09 19:05:13 +02002640 // TODO: is this type cast OK?
2641 g_list_foreach(icons, (GFunc)(void *)&g_object_unref, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002642 g_list_free(icons);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002643 }
2644
Bram Moolenaarf96ae0b2019-07-28 15:21:55 +02002645#if !defined(USE_GNOME_SESSION)
Bram Moolenaar30613902019-12-01 22:11:18 +01002646 // Register a handler for WM_SAVE_YOURSELF with GDK's low-level X I/F
Bram Moolenaar071d4272004-06-13 20:20:40 +00002647 gdk_window_add_filter(NULL, &global_event_filter, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002648#endif
Bram Moolenaar30613902019-12-01 22:11:18 +01002649 // Setup to indicate to the window manager that we want to catch the
2650 // WM_SAVE_YOURSELF event. For GNOME, this connects to the session
2651 // manager instead.
Bram Moolenaarf96ae0b2019-07-28 15:21:55 +02002652#if defined(USE_GNOME_SESSION)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002653 if (using_gnome)
2654#endif
2655 setup_save_yourself();
2656
2657#ifdef FEAT_CLIENTSERVER
2658 if (serverName == NULL && serverDelayedStartName != NULL)
2659 {
Bram Moolenaar30613902019-12-01 22:11:18 +01002660 // This is a :gui command in a plain vim with no previous server
Bram Moolenaar98921892016-02-23 17:14:37 +01002661 commWindow = GDK_WINDOW_XID(mainwin_win);
2662
2663 (void)serverRegisterName(GDK_WINDOW_XDISPLAY(mainwin_win),
2664 serverDelayedStartName);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002665 }
2666 else
2667 {
2668 /*
2669 * Cannot handle "XLib-only" windows with gtk event routines, we'll
2670 * have to change the "server" registration to that of the main window
Bram Moolenaarb463e8d2017-06-05 15:07:09 +02002671 * If we have not registered a name yet, remember the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002672 */
Bram Moolenaar98921892016-02-23 17:14:37 +01002673 serverChangeRegisteredWindow(GDK_WINDOW_XDISPLAY(mainwin_win),
2674 GDK_WINDOW_XID(mainwin_win));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002675 }
2676 gtk_widget_add_events(gui.mainwin, GDK_PROPERTY_CHANGE_MASK);
Bram Moolenaar98921892016-02-23 17:14:37 +01002677 g_signal_connect(G_OBJECT(gui.mainwin), "property-notify-event",
2678 G_CALLBACK(property_event), NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002679#endif
2680}
2681
2682 static GdkCursor *
2683create_blank_pointer(void)
2684{
2685 GdkWindow *root_window = NULL;
Bram Moolenaar98921892016-02-23 17:14:37 +01002686#if GTK_CHECK_VERSION(3,0,0)
2687 GdkPixbuf *blank_mask;
2688#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002689 GdkPixmap *blank_mask;
Bram Moolenaar98921892016-02-23 17:14:37 +01002690#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002691 GdkCursor *cursor;
Bram Moolenaar36edf062016-07-21 22:10:12 +02002692#if GTK_CHECK_VERSION(3,0,0)
Kenny Stauffer660eb9f2023-04-28 16:36:51 +01002693 GdkRGBA color = { 0.0, 0.0, 0.0, 0.0 };
Bram Moolenaar36edf062016-07-21 22:10:12 +02002694#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002695 GdkColor color = { 0, 0, 0, 0 };
2696 char blank_data[] = { 0x0 };
Bram Moolenaar98921892016-02-23 17:14:37 +01002697#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002698
Bram Moolenaarb463e8d2017-06-05 15:07:09 +02002699#if GTK_CHECK_VERSION(3,12,0)
Bram Moolenaar98921892016-02-23 17:14:37 +01002700 {
2701 GdkWindow * const win = gtk_widget_get_window(gui.mainwin);
2702 GdkScreen * const scrn = gdk_window_get_screen(win);
2703 root_window = gdk_screen_get_root_window(scrn);
2704 }
Bram Moolenaarb463e8d2017-06-05 15:07:09 +02002705#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002706 root_window = gtk_widget_get_root_window(gui.mainwin);
2707#endif
2708
Bram Moolenaar30613902019-12-01 22:11:18 +01002709 // Create a pseudo blank pointer, which is in fact one pixel by one pixel
2710 // in size.
Bram Moolenaar98921892016-02-23 17:14:37 +01002711#if GTK_CHECK_VERSION(3,0,0)
2712 {
2713 cairo_surface_t *surf;
2714 cairo_t *cr;
2715
2716 surf = cairo_image_surface_create(CAIRO_FORMAT_A1, 1, 1);
2717 cr = cairo_create(surf);
2718
Bram Moolenaar36edf062016-07-21 22:10:12 +02002719 cairo_set_source_rgba(cr,
2720 color.red,
2721 color.green,
2722 color.blue,
2723 color.alpha);
Bram Moolenaar98921892016-02-23 17:14:37 +01002724 cairo_rectangle(cr, 0, 0, 1, 1);
2725 cairo_fill(cr);
2726 cairo_destroy(cr);
2727
2728 blank_mask = gdk_pixbuf_get_from_surface(surf, 0, 0, 1, 1);
2729 cairo_surface_destroy(surf);
2730
2731 cursor = gdk_cursor_new_from_pixbuf(gdk_window_get_display(root_window),
2732 blank_mask, 0, 0);
2733 g_object_unref(blank_mask);
2734 }
2735#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002736 blank_mask = gdk_bitmap_create_from_data(root_window, blank_data, 1, 1);
2737 cursor = gdk_cursor_new_from_pixmap(blank_mask, blank_mask,
2738 &color, &color, 0, 0);
2739 gdk_bitmap_unref(blank_mask);
Bram Moolenaar98921892016-02-23 17:14:37 +01002740#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002741
2742 return cursor;
2743}
2744
Bram Moolenaar071d4272004-06-13 20:20:40 +00002745 static void
2746mainwin_screen_changed_cb(GtkWidget *widget,
Bram Moolenaarb85cb212009-05-17 14:24:23 +00002747 GdkScreen *previous_screen UNUSED,
2748 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002749{
2750 if (!gtk_widget_has_screen(widget))
2751 return;
2752
2753 /*
Bram Moolenaar49325942007-05-10 19:19:59 +00002754 * Recreate the invisible mouse cursor.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002755 */
2756 if (gui.blank_pointer != NULL)
Bram Moolenaarb463e8d2017-06-05 15:07:09 +02002757#if GTK_CHECK_VERSION(3,0,0)
Bram Moolenaar98921892016-02-23 17:14:37 +01002758 g_object_unref(G_OBJECT(gui.blank_pointer));
Bram Moolenaarb463e8d2017-06-05 15:07:09 +02002759#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002760 gdk_cursor_unref(gui.blank_pointer);
Bram Moolenaarb463e8d2017-06-05 15:07:09 +02002761#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002762
2763 gui.blank_pointer = create_blank_pointer();
2764
Bram Moolenaar98921892016-02-23 17:14:37 +01002765 if (gui.pointer_hidden && gtk_widget_get_window(gui.drawarea) != NULL)
2766 gdk_window_set_cursor(gtk_widget_get_window(gui.drawarea),
2767 gui.blank_pointer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002768
2769 /*
2770 * Create a new PangoContext for this screen, and initialize it
2771 * with the current font if necessary.
2772 */
2773 if (gui.text_context != NULL)
2774 g_object_unref(gui.text_context);
2775
2776 gui.text_context = gtk_widget_create_pango_context(widget);
2777 pango_context_set_base_dir(gui.text_context, PANGO_DIRECTION_LTR);
2778
2779 if (gui.norm_font != NULL)
2780 {
Bram Moolenaar46c9c732004-12-12 11:37:09 +00002781 gui_mch_init_font(p_guifont, FALSE);
Bram Moolenaar8ac44152017-11-09 18:33:29 +01002782 gui_set_shellsize(TRUE, FALSE, RESIZE_BOTH);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002783 }
2784}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002785
2786/*
2787 * After the drawing area comes up, we calculate all colors and create the
2788 * dummy blank cursor.
2789 *
2790 * Don't try to set any VIM scrollbar sizes anywhere here. I'm relying on the
2791 * fact that the main VIM engine doesn't take them into account anywhere.
2792 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002793 static void
Bram Moolenaarb85cb212009-05-17 14:24:23 +00002794drawarea_realize_cb(GtkWidget *widget, gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002795{
2796 GtkWidget *sbar;
Bram Moolenaar98921892016-02-23 17:14:37 +01002797 GtkAllocation allocation;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002798
2799#ifdef FEAT_XIM
2800 xim_init();
2801#endif
2802 gui_mch_new_colors();
Bram Moolenaar98921892016-02-23 17:14:37 +01002803#if GTK_CHECK_VERSION(3,0,0)
2804 gui.surface = gdk_window_create_similar_surface(
2805 gtk_widget_get_window(widget),
2806 CAIRO_CONTENT_COLOR_ALPHA,
2807 gtk_widget_get_allocated_width(widget),
2808 gtk_widget_get_allocated_height(widget));
2809#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002810 gui.text_gc = gdk_gc_new(gui.drawarea->window);
Bram Moolenaar98921892016-02-23 17:14:37 +01002811#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002812
2813 gui.blank_pointer = create_blank_pointer();
2814 if (gui.pointer_hidden)
Bram Moolenaar98921892016-02-23 17:14:37 +01002815 gdk_window_set_cursor(gtk_widget_get_window(widget), gui.blank_pointer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002816
Bram Moolenaar30613902019-12-01 22:11:18 +01002817 // get the actual size of the scrollbars, if they are realized
Bram Moolenaar071d4272004-06-13 20:20:40 +00002818 sbar = firstwin->w_scrollbars[SBAR_LEFT].id;
2819 if (!sbar || (!gui.which_scrollbars[SBAR_LEFT]
2820 && firstwin->w_scrollbars[SBAR_RIGHT].id))
2821 sbar = firstwin->w_scrollbars[SBAR_RIGHT].id;
Bram Moolenaar98921892016-02-23 17:14:37 +01002822 gtk_widget_get_allocation(sbar, &allocation);
2823 if (sbar && gtk_widget_get_realized(sbar) && allocation.width)
2824 gui.scrollbar_width = allocation.width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002825
2826 sbar = gui.bottom_sbar.id;
Bram Moolenaar98921892016-02-23 17:14:37 +01002827 if (sbar && gtk_widget_get_realized(sbar) && allocation.height)
2828 gui.scrollbar_height = allocation.height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002829}
2830
2831/*
2832 * Properly clean up on shutdown.
2833 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002834 static void
Bram Moolenaarb85cb212009-05-17 14:24:23 +00002835drawarea_unrealize_cb(GtkWidget *widget UNUSED, gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002836{
Bram Moolenaar30613902019-12-01 22:11:18 +01002837 // Don't write messages to the GUI anymore
Bram Moolenaar071d4272004-06-13 20:20:40 +00002838 full_screen = FALSE;
2839
2840#ifdef FEAT_XIM
2841 im_shutdown();
2842#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002843 if (gui.ascii_glyphs != NULL)
2844 {
2845 pango_glyph_string_free(gui.ascii_glyphs);
2846 gui.ascii_glyphs = NULL;
2847 }
2848 if (gui.ascii_font != NULL)
2849 {
2850 g_object_unref(gui.ascii_font);
2851 gui.ascii_font = NULL;
2852 }
2853 g_object_unref(gui.text_context);
2854 gui.text_context = NULL;
2855
Bram Moolenaar98921892016-02-23 17:14:37 +01002856#if GTK_CHECK_VERSION(3,0,0)
2857 if (gui.surface != NULL)
2858 {
2859 cairo_surface_destroy(gui.surface);
2860 gui.surface = NULL;
2861 }
2862#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002863 g_object_unref(gui.text_gc);
2864 gui.text_gc = NULL;
Bram Moolenaar98921892016-02-23 17:14:37 +01002865#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002866
Bram Moolenaar98921892016-02-23 17:14:37 +01002867#if GTK_CHECK_VERSION(3,0,0)
2868 g_object_unref(G_OBJECT(gui.blank_pointer));
2869#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002870 gdk_cursor_unref(gui.blank_pointer);
Bram Moolenaar98921892016-02-23 17:14:37 +01002871#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002872 gui.blank_pointer = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002873}
2874
Bram Moolenaara859f042016-11-17 19:11:55 +01002875#if GTK_CHECK_VERSION(3,22,2)
2876 static void
2877drawarea_style_updated_cb(GtkWidget *widget UNUSED,
2878 gpointer data UNUSED)
2879#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002880 static void
Bram Moolenaarb85cb212009-05-17 14:24:23 +00002881drawarea_style_set_cb(GtkWidget *widget UNUSED,
2882 GtkStyle *previous_style UNUSED,
2883 gpointer data UNUSED)
Bram Moolenaara859f042016-11-17 19:11:55 +01002884#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002885{
2886 gui_mch_new_colors();
2887}
2888
Bram Moolenaar98921892016-02-23 17:14:37 +01002889#if GTK_CHECK_VERSION(3,0,0)
2890 static gboolean
2891drawarea_configure_event_cb(GtkWidget *widget,
2892 GdkEventConfigure *event,
2893 gpointer data UNUSED)
2894{
2895 static int cur_width = 0;
2896 static int cur_height = 0;
2897
2898 g_return_val_if_fail(event
2899 && event->width >= 1 && event->height >= 1, TRUE);
2900
Bram Moolenaar182707a2016-11-21 20:55:58 +01002901# if GTK_CHECK_VERSION(3,22,2) && !GTK_CHECK_VERSION(3,22,4)
Bram Moolenaar30613902019-12-01 22:11:18 +01002902 // As of 3.22.2, GdkWindows have started distributing configure events to
2903 // their "native" children (https://git.gnome.org/browse/gtk+/commit/?h=gtk-3-22&id=12579fe71b3b8f79eb9c1b80e429443bcc437dd0).
2904 //
2905 // As can be seen from the implementation of move_native_children() and
2906 // configure_native_child() in gdkwindow.c, those functions actually
2907 // propagate configure events to every child, failing to distinguish
2908 // "native" one from non-native one.
2909 //
2910 // Naturally, configure events propagated to here like that are fallacious
2911 // and, as a matter of fact, they trigger a geometric collapse of
2912 // gui.drawarea in fullscreen and maximized modes.
2913 //
2914 // To filter out such nuisance events, we are making use of the fact that
2915 // the field send_event of such GdkEventConfigures is set to FALSE in
2916 // configure_native_child().
2917 //
2918 // Obviously, this is a terrible hack making GVim depend on GTK's
2919 // implementation details. Therefore, watch out any relevant internal
2920 // changes happening in GTK in the feature (sigh).
2921 //
2922 // Follow-up
2923 // After a few weeks later, the GdkWindow change mentioned above was
2924 // reverted (https://git.gnome.org/browse/gtk+/commit/?h=gtk-3-22&id=f70039cb9603a02d2369fec4038abf40a1711155).
2925 // The corresponding official release is 3.22.4.
Bram Moolenaara859f042016-11-17 19:11:55 +01002926 if (event->send_event == FALSE)
2927 return TRUE;
2928# endif
2929
Bram Moolenaar98921892016-02-23 17:14:37 +01002930 if (event->width == cur_width && event->height == cur_height)
2931 return TRUE;
2932
2933 cur_width = event->width;
2934 cur_height = event->height;
2935
2936 if (gui.surface != NULL)
2937 cairo_surface_destroy(gui.surface);
2938
2939 gui.surface = gdk_window_create_similar_surface(
2940 gtk_widget_get_window(widget),
2941 CAIRO_CONTENT_COLOR_ALPHA,
2942 event->width, event->height);
2943
2944 gtk_widget_queue_draw(widget);
2945
2946 return TRUE;
2947}
2948#endif
2949
Bram Moolenaar071d4272004-06-13 20:20:40 +00002950/*
2951 * Callback routine for the "delete_event" signal on the toplevel window.
2952 * Tries to vim gracefully, or refuses to exit with changed buffers.
2953 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002954 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +00002955delete_event_cb(GtkWidget *widget UNUSED,
2956 GdkEventAny *event UNUSED,
2957 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002958{
2959 gui_shell_closed();
2960 return TRUE;
2961}
2962
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002963#if defined(FEAT_MENU) || defined(FEAT_TOOLBAR) || defined(FEAT_GUI_TABLINE)
2964 static int
2965get_item_dimensions(GtkWidget *widget, GtkOrientation orientation)
2966{
Bram Moolenaarc4a249a2017-01-30 22:56:48 +01002967# ifdef FEAT_GUI_GNOME
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002968 GtkOrientation item_orientation = GTK_ORIENTATION_HORIZONTAL;
2969
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002970 if (using_gnome && widget != NULL)
2971 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002972 GtkWidget *parent;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002973 BonoboDockItem *dockitem;
2974
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00002975 parent = gtk_widget_get_parent(widget);
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002976 if (G_TYPE_FROM_INSTANCE(parent) == BONOBO_TYPE_DOCK_ITEM)
2977 {
Bram Moolenaar30613902019-12-01 22:11:18 +01002978 // Only menu & toolbar are dock items. Could tabline be?
2979 // Seem to be only the 2 defined in GNOME
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002980 widget = parent;
2981 dockitem = BONOBO_DOCK_ITEM(widget);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002982
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002983 if (dockitem == NULL || dockitem->is_floating)
2984 return 0;
2985 item_orientation = bonobo_dock_item_get_orientation(dockitem);
2986 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002987 }
Bram Moolenaarc4a249a2017-01-30 22:56:48 +01002988# else
2989# define item_orientation GTK_ORIENTATION_HORIZONTAL
Bram Moolenaar98921892016-02-23 17:14:37 +01002990# endif
Bram Moolenaarc4a249a2017-01-30 22:56:48 +01002991
Bram Moolenaar98921892016-02-23 17:14:37 +01002992 if (widget != NULL
2993 && item_orientation == orientation
2994 && gtk_widget_get_realized(widget)
2995 && gtk_widget_get_visible(widget))
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002996 {
Bram Moolenaar664323e2018-09-18 22:30:07 +02002997# if GTK_CHECK_VERSION(3,0,0) || !defined(FEAT_GUI_GNOME)
Bram Moolenaar98921892016-02-23 17:14:37 +01002998 GtkAllocation allocation;
2999
3000 gtk_widget_get_allocation(widget, &allocation);
Bram Moolenaarc4a249a2017-01-30 22:56:48 +01003001 return allocation.height;
Bram Moolenaar98921892016-02-23 17:14:37 +01003002# else
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003003 if (orientation == GTK_ORIENTATION_HORIZONTAL)
3004 return widget->allocation.height;
3005 else
3006 return widget->allocation.width;
Bram Moolenaar98921892016-02-23 17:14:37 +01003007# endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003008 }
3009 return 0;
3010}
3011#endif
3012
3013 static int
3014get_menu_tool_width(void)
3015{
3016 int width = 0;
3017
Bram Moolenaar30613902019-12-01 22:11:18 +01003018#ifdef FEAT_GUI_GNOME // these are never vertical without GNOME
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003019# ifdef FEAT_MENU
3020 width += get_item_dimensions(gui.menubar, GTK_ORIENTATION_VERTICAL);
3021# endif
3022# ifdef FEAT_TOOLBAR
3023 width += get_item_dimensions(gui.toolbar, GTK_ORIENTATION_VERTICAL);
3024# endif
3025# ifdef FEAT_GUI_TABLINE
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00003026 if (gui.tabline != NULL)
3027 width += get_item_dimensions(gui.tabline, GTK_ORIENTATION_VERTICAL);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003028# endif
3029#endif
3030
3031 return width;
3032}
3033
3034 static int
3035get_menu_tool_height(void)
3036{
3037 int height = 0;
3038
3039#ifdef FEAT_MENU
3040 height += get_item_dimensions(gui.menubar, GTK_ORIENTATION_HORIZONTAL);
3041#endif
3042#ifdef FEAT_TOOLBAR
3043 height += get_item_dimensions(gui.toolbar, GTK_ORIENTATION_HORIZONTAL);
3044#endif
3045#ifdef FEAT_GUI_TABLINE
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00003046 if (gui.tabline != NULL)
3047 height += get_item_dimensions(gui.tabline, GTK_ORIENTATION_HORIZONTAL);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003048#endif
3049
3050 return height;
3051}
3052
Bram Moolenaar30613902019-12-01 22:11:18 +01003053// This controls whether we can set the real window hints at
3054// start-up when in a GtkPlug.
3055// 0 = normal processing (default)
3056// 1 = init. hints set, no-one's tried to reset since last check
3057// 2 = init. hints set, attempt made to change hints
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003058static int init_window_hints_state = 0;
3059
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003060 static void
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003061update_window_manager_hints(int force_width, int force_height)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003062{
3063 static int old_width = 0;
3064 static int old_height = 0;
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003065 static int old_min_width = 0;
3066 static int old_min_height = 0;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003067 static int old_char_width = 0;
3068 static int old_char_height = 0;
3069
3070 int width;
3071 int height;
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003072 int min_width;
3073 int min_height;
3074
Bram Moolenaar30613902019-12-01 22:11:18 +01003075 // At start-up, don't try to set the hints until the initial
3076 // values have been used (those that dictate our initial size)
3077 // Let forced (i.e., correct) values through always.
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003078 if (!(force_width && force_height) && init_window_hints_state > 0)
3079 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003080 // Don't do it!
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003081 init_window_hints_state = 2;
3082 return;
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003083 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003084
Bram Moolenaar30613902019-12-01 22:11:18 +01003085 // This also needs to be done when the main window isn't there yet,
3086 // otherwise the hints don't work.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003087 width = gui_get_base_width();
3088 height = gui_get_base_height();
3089# ifdef FEAT_MENU
3090 height += tabline_height() * gui.char_height;
3091# endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003092 width += get_menu_tool_width();
3093 height += get_menu_tool_height();
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003094
Bram Moolenaar30613902019-12-01 22:11:18 +01003095 // GtkSockets use GtkPlug's [gui,mainwin] min-size hints to determine
3096 // their actual widget size. When we set our size ourselves (e.g.,
3097 // 'set columns=' or init. -geom) we briefly set the min. to the size
3098 // we wish to be instead of the legitimate minimum so that we actually
3099 // resize correctly.
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003100 if (force_width && force_height)
3101 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003102 min_width = force_width;
3103 min_height = force_height;
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003104 }
3105 else
3106 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003107 min_width = width + MIN_COLUMNS * gui.char_width;
3108 min_height = height + MIN_LINES * gui.char_height;
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003109 }
3110
Bram Moolenaar30613902019-12-01 22:11:18 +01003111 // Avoid an expose event when the size didn't change.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003112 if (width != old_width
3113 || height != old_height
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003114 || min_width != old_min_width
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003115 || min_height != old_min_height
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003116 || gui.char_width != old_char_width
3117 || gui.char_height != old_char_height)
3118 {
3119 GdkGeometry geometry;
3120 GdkWindowHints geometry_mask;
3121
3122 geometry.width_inc = gui.char_width;
3123 geometry.height_inc = gui.char_height;
3124 geometry.base_width = width;
3125 geometry.base_height = height;
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003126 geometry.min_width = min_width;
3127 geometry.min_height = min_height;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003128 geometry_mask = GDK_HINT_BASE_SIZE|GDK_HINT_RESIZE_INC
3129 |GDK_HINT_MIN_SIZE;
Bram Moolenaar30613902019-12-01 22:11:18 +01003130 // Using gui.formwin as geometry widget doesn't work as expected
3131 // with GTK+ 2 -- dunno why. Presumably all the resizing hacks
Bram Moolenaarc0da5402023-05-16 22:15:51 +01003132 // in Vim confuse GTK+. For GTK 3 the second argument should be NULL
3133 // to make the width/height inc works, despite the docs saying
3134 // something else.
3135 gtk_window_set_geometry_hints(GTK_WINDOW(gui.mainwin), NULL,
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003136 &geometry, geometry_mask);
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003137 old_width = width;
3138 old_height = height;
3139 old_min_width = min_width;
3140 old_min_height = min_height;
3141 old_char_width = gui.char_width;
3142 old_char_height = gui.char_height;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003143 }
3144}
3145
Bram Moolenaar50bf7ce2019-09-15 13:17:00 +02003146#if defined(FEAT_GUI_DARKTHEME) || defined(PROTO)
3147 void
3148gui_mch_set_dark_theme(int dark)
3149{
3150# if GTK_CHECK_VERSION(3,0,0)
3151 GtkSettings *gtk_settings;
3152
3153 gtk_settings = gtk_settings_get_for_screen(gdk_screen_get_default());
3154 g_object_set(gtk_settings, "gtk-application-prefer-dark-theme", (gboolean)dark, NULL);
3155# endif
3156}
Bram Moolenaar30613902019-12-01 22:11:18 +01003157#endif // FEAT_GUI_DARKTHEME
Bram Moolenaar50bf7ce2019-09-15 13:17:00 +02003158
Bram Moolenaar071d4272004-06-13 20:20:40 +00003159#ifdef FEAT_TOOLBAR
3160
Bram Moolenaar071d4272004-06-13 20:20:40 +00003161/*
3162 * This extra effort wouldn't be necessary if we only used stock icons in the
3163 * toolbar, as we do for all builtin icons. But user-defined toolbar icons
3164 * shouldn't be treated differently, thus we do need this.
3165 */
3166 static void
3167icon_size_changed_foreach(GtkWidget *widget, gpointer user_data)
3168{
3169 if (GTK_IS_IMAGE(widget))
3170 {
3171 GtkImage *image = (GtkImage *)widget;
3172
Bram Moolenaar98921892016-02-23 17:14:37 +01003173# if GTK_CHECK_VERSION(3,10,0)
3174 if (gtk_image_get_storage_type(image) == GTK_IMAGE_ICON_NAME)
3175 {
3176 const GtkIconSize icon_size = GPOINTER_TO_INT(user_data);
3177 const gchar *icon_name;
3178
3179 gtk_image_get_icon_name(image, &icon_name, NULL);
Bram Moolenaar81a4cf42020-09-09 19:05:13 +02003180 image = (GtkImage *)gtk_image_new_from_icon_name(
3181 icon_name, icon_size);
Bram Moolenaar98921892016-02-23 17:14:37 +01003182 }
3183# else
Bram Moolenaar30613902019-12-01 22:11:18 +01003184 // User-defined icons are stored in a GtkIconSet
Bram Moolenaar071d4272004-06-13 20:20:40 +00003185 if (gtk_image_get_storage_type(image) == GTK_IMAGE_ICON_SET)
3186 {
3187 GtkIconSet *icon_set;
3188 GtkIconSize icon_size;
3189
3190 gtk_image_get_icon_set(image, &icon_set, &icon_size);
3191 icon_size = (GtkIconSize)(long)user_data;
3192
3193 gtk_icon_set_ref(icon_set);
3194 gtk_image_set_from_icon_set(image, icon_set, icon_size);
3195 gtk_icon_set_unref(icon_set);
3196 }
Bram Moolenaar98921892016-02-23 17:14:37 +01003197# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003198 }
3199 else if (GTK_IS_CONTAINER(widget))
3200 {
3201 gtk_container_foreach((GtkContainer *)widget,
3202 &icon_size_changed_foreach,
3203 user_data);
3204 }
3205}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003206
3207 static void
3208set_toolbar_style(GtkToolbar *toolbar)
3209{
3210 GtkToolbarStyle style;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003211 GtkIconSize size;
3212 GtkIconSize oldsize;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003213
Bram Moolenaar071d4272004-06-13 20:20:40 +00003214 if ((toolbar_flags & (TOOLBAR_TEXT | TOOLBAR_ICONS | TOOLBAR_HORIZ))
3215 == (TOOLBAR_TEXT | TOOLBAR_ICONS | TOOLBAR_HORIZ))
3216 style = GTK_TOOLBAR_BOTH_HORIZ;
Bram Moolenaar182c5be2010-06-25 05:37:59 +02003217 else if ((toolbar_flags & (TOOLBAR_TEXT | TOOLBAR_ICONS))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003218 == (TOOLBAR_TEXT | TOOLBAR_ICONS))
3219 style = GTK_TOOLBAR_BOTH;
3220 else if (toolbar_flags & TOOLBAR_TEXT)
3221 style = GTK_TOOLBAR_TEXT;
3222 else
3223 style = GTK_TOOLBAR_ICONS;
3224
3225 gtk_toolbar_set_style(toolbar, style);
Bram Moolenaar98921892016-02-23 17:14:37 +01003226# if !GTK_CHECK_VERSION(3,0,0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003227 gtk_toolbar_set_tooltips(toolbar, (toolbar_flags & TOOLBAR_TOOLTIPS) != 0);
Bram Moolenaar98921892016-02-23 17:14:37 +01003228# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003229
Bram Moolenaar071d4272004-06-13 20:20:40 +00003230 switch (tbis_flags)
3231 {
3232 case TBIS_TINY: size = GTK_ICON_SIZE_MENU; break;
3233 case TBIS_SMALL: size = GTK_ICON_SIZE_SMALL_TOOLBAR; break;
3234 case TBIS_MEDIUM: size = GTK_ICON_SIZE_BUTTON; break;
3235 case TBIS_LARGE: size = GTK_ICON_SIZE_LARGE_TOOLBAR; break;
Bram Moolenaarbeb003b2016-03-08 22:47:17 +01003236 case TBIS_HUGE: size = GTK_ICON_SIZE_DND; break;
3237 case TBIS_GIANT: size = GTK_ICON_SIZE_DIALOG; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003238 default: size = GTK_ICON_SIZE_INVALID; break;
3239 }
3240 oldsize = gtk_toolbar_get_icon_size(toolbar);
3241
3242 if (size == GTK_ICON_SIZE_INVALID)
3243 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003244 // Let global user preferences decide the icon size.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003245 gtk_toolbar_unset_icon_size(toolbar);
3246 size = gtk_toolbar_get_icon_size(toolbar);
3247 }
3248 if (size != oldsize)
3249 {
3250 gtk_container_foreach(GTK_CONTAINER(toolbar),
3251 &icon_size_changed_foreach,
3252 GINT_TO_POINTER((int)size));
3253 }
3254 gtk_toolbar_set_icon_size(toolbar, size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003255}
3256
Bram Moolenaar30613902019-12-01 22:11:18 +01003257#endif // FEAT_TOOLBAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00003258
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003259#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
3260static int ignore_tabline_evt = FALSE;
Bram Moolenaara5621492006-02-25 21:55:24 +00003261static GtkWidget *tabline_menu;
Bram Moolenaar98921892016-02-23 17:14:37 +01003262# if !GTK_CHECK_VERSION(3,0,0)
Bram Moolenaar437df8f2006-04-27 21:47:44 +00003263static GtkTooltips *tabline_tooltip;
Bram Moolenaar98921892016-02-23 17:14:37 +01003264# endif
Bram Moolenaar30613902019-12-01 22:11:18 +01003265static int clicked_page; // page clicked in tab line
Bram Moolenaara5621492006-02-25 21:55:24 +00003266
3267/*
3268 * Handle selecting an item in the tab line popup menu.
3269 */
Bram Moolenaara5621492006-02-25 21:55:24 +00003270 static void
Bram Moolenaarb85cb212009-05-17 14:24:23 +00003271tabline_menu_handler(GtkMenuItem *item UNUSED, gpointer user_data)
Bram Moolenaara5621492006-02-25 21:55:24 +00003272{
Bram Moolenaar30613902019-12-01 22:11:18 +01003273 // Add the string cmd into input buffer
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00003274 send_tabline_menu_event(clicked_page, (int)(long)user_data);
Bram Moolenaara5621492006-02-25 21:55:24 +00003275}
3276
Bram Moolenaar8ea91232006-04-28 22:41:43 +00003277 static void
3278add_tabline_menu_item(GtkWidget *menu, char_u *text, int resp)
3279{
3280 GtkWidget *item;
3281 char_u *utf_text;
3282
3283 utf_text = CONVERT_TO_UTF8(text);
3284 item = gtk_menu_item_new_with_label((const char *)utf_text);
3285 gtk_widget_show(item);
3286 CONVERT_TO_UTF8_FREE(utf_text);
3287
3288 gtk_container_add(GTK_CONTAINER(menu), item);
Bram Moolenaar98921892016-02-23 17:14:37 +01003289 g_signal_connect(G_OBJECT(item), "activate",
3290 G_CALLBACK(tabline_menu_handler),
3291 GINT_TO_POINTER(resp));
Bram Moolenaar8ea91232006-04-28 22:41:43 +00003292}
3293
Bram Moolenaara5621492006-02-25 21:55:24 +00003294/*
Bram Moolenaara5621492006-02-25 21:55:24 +00003295 * Create a menu for the tab line.
3296 */
3297 static GtkWidget *
3298create_tabline_menu(void)
3299{
Bram Moolenaar8ea91232006-04-28 22:41:43 +00003300 GtkWidget *menu;
Bram Moolenaara5621492006-02-25 21:55:24 +00003301
3302 menu = gtk_menu_new();
Bram Moolenaar29547192018-12-11 20:39:19 +01003303 add_tabline_menu_item(menu, (char_u *)_("Close tab"), TABLINE_MENU_CLOSE);
Bram Moolenaar8ea91232006-04-28 22:41:43 +00003304 add_tabline_menu_item(menu, (char_u *)_("New tab"), TABLINE_MENU_NEW);
3305 add_tabline_menu_item(menu, (char_u *)_("Open Tab..."), TABLINE_MENU_OPEN);
Bram Moolenaara5621492006-02-25 21:55:24 +00003306
3307 return menu;
3308}
3309
3310 static gboolean
3311on_tabline_menu(GtkWidget *widget, GdkEvent *event)
3312{
Bram Moolenaar30613902019-12-01 22:11:18 +01003313 // Was this button press event ?
Bram Moolenaara5621492006-02-25 21:55:24 +00003314 if (event->type == GDK_BUTTON_PRESS)
3315 {
3316 GdkEventButton *bevent = (GdkEventButton *)event;
3317 int x = bevent->x;
Bram Moolenaar8ea91232006-04-28 22:41:43 +00003318 int y = bevent->y;
3319 GtkWidget *tabwidget;
3320 GdkWindow *tabwin;
Bram Moolenaara5621492006-02-25 21:55:24 +00003321
Bram Moolenaar30613902019-12-01 22:11:18 +01003322 // When ignoring events return TRUE so that the selected page doesn't
3323 // change.
Martin Tournoij7904fa42022-10-04 16:28:45 +01003324 if (hold_gui_events || cmdwin_type != 0)
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003325 return TRUE;
3326
Bram Moolenaar98921892016-02-23 17:14:37 +01003327 tabwin = gui_gtk_window_at_position(gui.mainwin, &x, &y);
Bram Moolenaar98921892016-02-23 17:14:37 +01003328
Bram Moolenaar8ea91232006-04-28 22:41:43 +00003329 gdk_window_get_user_data(tabwin, (gpointer)&tabwidget);
Bram Moolenaar98921892016-02-23 17:14:37 +01003330 clicked_page = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(tabwidget),
3331 "tab_num"));
Bram Moolenaara5621492006-02-25 21:55:24 +00003332
Bram Moolenaar30613902019-12-01 22:11:18 +01003333 // If the event was generated for 3rd button popup the menu.
Bram Moolenaara5621492006-02-25 21:55:24 +00003334 if (bevent->button == 3)
3335 {
Bram Moolenaara859f042016-11-17 19:11:55 +01003336# if GTK_CHECK_VERSION(3,22,2)
3337 gtk_menu_popup_at_pointer(GTK_MENU(widget), event);
3338# else
Bram Moolenaara5621492006-02-25 21:55:24 +00003339 gtk_menu_popup(GTK_MENU(widget), NULL, NULL, NULL, NULL,
3340 bevent->button, bevent->time);
Bram Moolenaara859f042016-11-17 19:11:55 +01003341# endif
Bram Moolenaar30613902019-12-01 22:11:18 +01003342 // We handled the event.
Bram Moolenaara5621492006-02-25 21:55:24 +00003343 return TRUE;
3344 }
Bram Moolenaar96351572006-05-05 21:16:59 +00003345 else if (bevent->button == 1)
Bram Moolenaareddf53b2006-02-27 00:11:10 +00003346 {
Bram Moolenaar96351572006-05-05 21:16:59 +00003347 if (clicked_page == 0)
3348 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003349 // Click after all tabs moves to next tab page. When "x" is
3350 // small guess it's the left button.
Bram Moolenaara3f41662010-07-11 19:01:06 +02003351 send_tabline_event(x < 50 ? -1 : 0);
Bram Moolenaar96351572006-05-05 21:16:59 +00003352 }
Bram Moolenaareddf53b2006-02-27 00:11:10 +00003353 }
regomne3bdef102022-09-26 20:48:32 +01003354 else if (bevent->button == 2)
3355 {
3356 if (clicked_page != 0)
3357 // Middle mouse click on tabpage label closes that tab.
3358 send_tabline_menu_event(clicked_page, TABLINE_MENU_CLOSE);
3359 }
Bram Moolenaara5621492006-02-25 21:55:24 +00003360 }
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003361
Bram Moolenaar30613902019-12-01 22:11:18 +01003362 // We didn't handle the event.
Bram Moolenaara5621492006-02-25 21:55:24 +00003363 return FALSE;
3364}
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003365
3366/*
3367 * Handle selecting one of the tabs.
3368 */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003369 static void
3370on_select_tab(
Bram Moolenaarb85cb212009-05-17 14:24:23 +00003371 GtkNotebook *notebook UNUSED,
Bram Moolenaar98921892016-02-23 17:14:37 +01003372 gpointer *page UNUSED,
Bram Moolenaar89d40322006-08-29 15:30:07 +00003373 gint idx,
Bram Moolenaarb85cb212009-05-17 14:24:23 +00003374 gpointer data UNUSED)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003375{
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003376 if (!ignore_tabline_evt)
Bram Moolenaara3f41662010-07-11 19:01:06 +02003377 send_tabline_event(idx + 1);
Bram Moolenaarca05aa22017-10-22 15:36:14 +02003378}
3379
Bram Moolenaar92cbf622018-09-19 22:40:03 +02003380# if GTK_CHECK_VERSION(2,10,0)
Bram Moolenaarca05aa22017-10-22 15:36:14 +02003381/*
3382 * Handle reordering the tabs (using D&D).
3383 */
3384 static void
3385on_tab_reordered(
3386 GtkNotebook *notebook UNUSED,
Bram Moolenaarca05aa22017-10-22 15:36:14 +02003387 gpointer *page UNUSED,
Bram Moolenaarca05aa22017-10-22 15:36:14 +02003388 gint idx,
3389 gpointer data UNUSED)
3390{
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00003391 if (ignore_tabline_evt)
3392 return;
3393
3394 if ((tabpage_index(curtab) - 1) < idx)
3395 tabpage_move(idx + 1);
3396 else
3397 tabpage_move(idx);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003398}
Bram Moolenaar92cbf622018-09-19 22:40:03 +02003399# endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003400
3401/*
3402 * Show or hide the tabline.
3403 */
3404 void
3405gui_mch_show_tabline(int showit)
3406{
3407 if (gui.tabline == NULL)
3408 return;
3409
3410 if (!showit != !gtk_notebook_get_show_tabs(GTK_NOTEBOOK(gui.tabline)))
3411 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003412 // Note: this may cause a resize event
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003413 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(gui.tabline), showit);
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003414 update_window_manager_hints(0, 0);
Bram Moolenaar96351572006-05-05 21:16:59 +00003415 if (showit)
Bram Moolenaar98921892016-02-23 17:14:37 +01003416 gtk_widget_set_can_focus(GTK_WIDGET(gui.tabline), FALSE);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003417 }
Bram Moolenaar96351572006-05-05 21:16:59 +00003418
3419 gui_mch_update();
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003420}
3421
3422/*
Bram Moolenaar3517bb12006-03-03 22:58:45 +00003423 * Return TRUE when tabline is displayed.
3424 */
3425 int
3426gui_mch_showing_tabline(void)
3427{
3428 return gui.tabline != NULL
Bram Moolenaar182c5be2010-06-25 05:37:59 +02003429 && gtk_notebook_get_show_tabs(GTK_NOTEBOOK(gui.tabline));
Bram Moolenaar3517bb12006-03-03 22:58:45 +00003430}
3431
3432/*
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003433 * Update the labels of the tabline.
3434 */
3435 void
3436gui_mch_update_tabline(void)
3437{
3438 GtkWidget *page;
Bram Moolenaar437df8f2006-04-27 21:47:44 +00003439 GtkWidget *event_box;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003440 GtkWidget *label;
3441 tabpage_T *tp;
3442 int nr = 0;
Bram Moolenaar8ea91232006-04-28 22:41:43 +00003443 int tab_num;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003444 int curtabidx = 0;
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003445 char_u *labeltext;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003446
3447 if (gui.tabline == NULL)
3448 return;
3449
3450 ignore_tabline_evt = TRUE;
3451
Bram Moolenaar30613902019-12-01 22:11:18 +01003452 // Add a label for each tab page. They all contain the same text area.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003453 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next, ++nr)
3454 {
3455 if (tp == curtab)
3456 curtabidx = nr;
3457
Bram Moolenaar8ea91232006-04-28 22:41:43 +00003458 tab_num = nr + 1;
3459
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003460 page = gtk_notebook_get_nth_page(GTK_NOTEBOOK(gui.tabline), nr);
3461 if (page == NULL)
3462 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003463 // Add notebook page
Bram Moolenaar98921892016-02-23 17:14:37 +01003464# if GTK_CHECK_VERSION(3,2,0)
3465 page = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
3466 gtk_box_set_homogeneous(GTK_BOX(page), FALSE);
3467# else
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003468 page = gtk_vbox_new(FALSE, 0);
Bram Moolenaar98921892016-02-23 17:14:37 +01003469# endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003470 gtk_widget_show(page);
Bram Moolenaar437df8f2006-04-27 21:47:44 +00003471 event_box = gtk_event_box_new();
3472 gtk_widget_show(event_box);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003473 label = gtk_label_new("-Empty-");
Bram Moolenaar98921892016-02-23 17:14:37 +01003474# if !GTK_CHECK_VERSION(3,14,0)
Bram Moolenaar8ea91232006-04-28 22:41:43 +00003475 gtk_misc_set_padding(GTK_MISC(label), 2, 2);
Bram Moolenaar98921892016-02-23 17:14:37 +01003476# endif
Bram Moolenaar437df8f2006-04-27 21:47:44 +00003477 gtk_container_add(GTK_CONTAINER(event_box), label);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003478 gtk_widget_show(label);
3479 gtk_notebook_insert_page(GTK_NOTEBOOK(gui.tabline),
3480 page,
Bram Moolenaar437df8f2006-04-27 21:47:44 +00003481 event_box,
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003482 nr++);
Bram Moolenaar92cbf622018-09-19 22:40:03 +02003483# if GTK_CHECK_VERSION(2,10,0)
Bram Moolenaarca05aa22017-10-22 15:36:14 +02003484 gtk_notebook_set_tab_reorderable(GTK_NOTEBOOK(gui.tabline),
3485 page,
3486 TRUE);
Bram Moolenaar92cbf622018-09-19 22:40:03 +02003487# endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003488 }
3489
Bram Moolenaar437df8f2006-04-27 21:47:44 +00003490 event_box = gtk_notebook_get_tab_label(GTK_NOTEBOOK(gui.tabline), page);
Bram Moolenaar98921892016-02-23 17:14:37 +01003491 g_object_set_data(G_OBJECT(event_box), "tab_num",
3492 GINT_TO_POINTER(tab_num));
Bram Moolenaar98921892016-02-23 17:14:37 +01003493 label = gtk_bin_get_child(GTK_BIN(event_box));
Bram Moolenaar57657d82006-04-21 22:12:41 +00003494 get_tabline_label(tp, FALSE);
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003495 labeltext = CONVERT_TO_UTF8(NameBuff);
Bram Moolenaar437df8f2006-04-27 21:47:44 +00003496 gtk_label_set_text(GTK_LABEL(label), (const char *)labeltext);
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003497 CONVERT_TO_UTF8_FREE(labeltext);
Bram Moolenaar437df8f2006-04-27 21:47:44 +00003498
Bram Moolenaar437df8f2006-04-27 21:47:44 +00003499 get_tabline_label(tp, TRUE);
3500 labeltext = CONVERT_TO_UTF8(NameBuff);
Bram Moolenaar98921892016-02-23 17:14:37 +01003501# if GTK_CHECK_VERSION(3,0,0)
3502 gtk_widget_set_tooltip_text(event_box, (const gchar *)labeltext);
3503# else
Bram Moolenaar437df8f2006-04-27 21:47:44 +00003504 gtk_tooltips_set_tip(GTK_TOOLTIPS(tabline_tooltip), event_box,
3505 (const char *)labeltext, NULL);
Bram Moolenaar98921892016-02-23 17:14:37 +01003506# endif
Bram Moolenaar437df8f2006-04-27 21:47:44 +00003507 CONVERT_TO_UTF8_FREE(labeltext);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003508 }
3509
Bram Moolenaar30613902019-12-01 22:11:18 +01003510 // Remove any old labels.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003511 while (gtk_notebook_get_nth_page(GTK_NOTEBOOK(gui.tabline), nr) != NULL)
3512 gtk_notebook_remove_page(GTK_NOTEBOOK(gui.tabline), nr);
3513
Bram Moolenaar98921892016-02-23 17:14:37 +01003514 if (gtk_notebook_get_current_page(GTK_NOTEBOOK(gui.tabline)) != curtabidx)
3515 gtk_notebook_set_current_page(GTK_NOTEBOOK(gui.tabline), curtabidx);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003516
Bram Moolenaar30613902019-12-01 22:11:18 +01003517 // Make sure everything is in place before drawing text.
Bram Moolenaar8ea91232006-04-28 22:41:43 +00003518 gui_mch_update();
3519
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003520 ignore_tabline_evt = FALSE;
3521}
3522
3523/*
3524 * Set the current tab to "nr". First tab is 1.
3525 */
3526 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003527gui_mch_set_curtab(int nr)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003528{
3529 if (gui.tabline == NULL)
3530 return;
3531
3532 ignore_tabline_evt = TRUE;
Bram Moolenaar98921892016-02-23 17:14:37 +01003533 if (gtk_notebook_get_current_page(GTK_NOTEBOOK(gui.tabline)) != nr - 1)
3534 gtk_notebook_set_current_page(GTK_NOTEBOOK(gui.tabline), nr - 1);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003535 ignore_tabline_evt = FALSE;
3536}
3537
Bram Moolenaar30613902019-12-01 22:11:18 +01003538#endif // FEAT_GUI_TABLINE
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003539
Bram Moolenaar071d4272004-06-13 20:20:40 +00003540/*
Bram Moolenaara76638f2010-06-05 12:49:46 +02003541 * Add selection targets for PRIMARY and CLIPBOARD selections.
3542 */
3543 void
lilydjwg94ff09a2024-01-29 20:14:01 +01003544gui_gtk_set_selection_targets(GdkAtom selection)
Bram Moolenaara76638f2010-06-05 12:49:46 +02003545{
3546 int i, j = 0;
lilydjwg94ff09a2024-01-29 20:14:01 +01003547 static int n_targets = N_SELECTION_TARGETS;
3548 static GtkTargetEntry targets[N_SELECTION_TARGETS];
Bram Moolenaara76638f2010-06-05 12:49:46 +02003549
lilydjwg94ff09a2024-01-29 20:14:01 +01003550 if (targets[0].target == NULL)
Bram Moolenaara76638f2010-06-05 12:49:46 +02003551 {
lilydjwg94ff09a2024-01-29 20:14:01 +01003552 for (i = 0; i < (int)N_SELECTION_TARGETS; ++i)
3553 {
3554 // OpenOffice tries to use TARGET_HTML and fails when we don't
3555 // return something, instead of trying another target. Therefore only
3556 // offer TARGET_HTML when it works.
3557 if (!clip_html && selection_targets[i].info == TARGET_HTML)
3558 n_targets--;
3559 else
3560 targets[j++] = selection_targets[i];
3561 }
Bram Moolenaara76638f2010-06-05 12:49:46 +02003562 }
3563
lilydjwg94ff09a2024-01-29 20:14:01 +01003564 gtk_selection_clear_targets(gui.drawarea, selection);
3565 gtk_selection_add_targets(gui.drawarea, selection,
Bram Moolenaara76638f2010-06-05 12:49:46 +02003566 targets, n_targets);
3567}
3568
3569/*
3570 * Set up for receiving DND items.
3571 */
3572 void
3573gui_gtk_set_dnd_targets(void)
3574{
3575 int i, j = 0;
3576 int n_targets = N_DND_TARGETS;
3577 GtkTargetEntry targets[N_DND_TARGETS];
3578
3579 for (i = 0; i < (int)N_DND_TARGETS; ++i)
3580 {
Bram Moolenaarb931d742011-10-26 11:36:25 +02003581 if (!clip_html && dnd_targets[i].info == TARGET_HTML)
Bram Moolenaara76638f2010-06-05 12:49:46 +02003582 n_targets--;
3583 else
Bram Moolenaara76638f2010-06-05 12:49:46 +02003584 targets[j++] = dnd_targets[i];
3585 }
3586
3587 gtk_drag_dest_unset(gui.drawarea);
3588 gtk_drag_dest_set(gui.drawarea,
3589 GTK_DEST_DEFAULT_ALL,
3590 targets, n_targets,
Bram Moolenaarba7cc9f2011-02-25 17:10:27 +01003591 GDK_ACTION_COPY | GDK_ACTION_MOVE);
Bram Moolenaara76638f2010-06-05 12:49:46 +02003592}
3593
3594/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003595 * Initialize the GUI. Create all the windows, set up all the callbacks etc.
3596 * Returns OK for success, FAIL when the GUI can't be started.
3597 */
3598 int
3599gui_mch_init(void)
3600{
3601 GtkWidget *vbox;
3602
3603#ifdef FEAT_GUI_GNOME
Bram Moolenaar30613902019-12-01 22:11:18 +01003604 // Initialize the GNOME libraries. gnome_program_init()/gnome_init()
3605 // exits on failure, but that's a non-issue because we already called
3606 // gtk_init_check() in gui_mch_init_check().
Bram Moolenaar071d4272004-06-13 20:20:40 +00003607 if (using_gnome)
Bram Moolenaar3be71ce2013-01-23 16:00:11 +01003608 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003609 gnome_program_init(VIMPACKAGE, VIM_VERSION_SHORT,
3610 LIBGNOMEUI_MODULE, gui_argc, gui_argv, NULL);
Bram Moolenaar73e28dc2022-09-17 21:08:33 +01003611# if defined(LC_NUMERIC)
Bram Moolenaar1ff32c52014-04-29 15:11:43 +02003612 {
3613 char *p = setlocale(LC_NUMERIC, NULL);
3614
Bram Moolenaar30613902019-12-01 22:11:18 +01003615 // Make sure strtod() uses a decimal point, not a comma. Gnome
3616 // init may change it.
Bram Moolenaar1ff32c52014-04-29 15:11:43 +02003617 if (p == NULL || strcmp(p, "C") != 0)
3618 setlocale(LC_NUMERIC, "C");
3619 }
Bram Moolenaar3be71ce2013-01-23 16:00:11 +01003620# endif
3621 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003622#endif
Bram Moolenaard23a8232018-02-10 18:45:26 +01003623 VIM_CLEAR(gui_argv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003624
Bram Moolenaar182c5be2010-06-25 05:37:59 +02003625#if GLIB_CHECK_VERSION(2,1,3)
Bram Moolenaar30613902019-12-01 22:11:18 +01003626 // Set the human-readable application name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003627 g_set_application_name("Vim");
Bram Moolenaar182c5be2010-06-25 05:37:59 +02003628#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003629 /*
3630 * Force UTF-8 output no matter what the value of 'encoding' is.
3631 * did_set_string_option() in option.c prohibits changing 'termencoding'
3632 * to something else than UTF-8 if the GUI is in use.
3633 */
Bram Moolenaar31e5c602022-04-15 13:53:33 +01003634 set_option_value_give_err((char_u *)"termencoding",
3635 0L, (char_u *)"utf-8", 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003636
Bram Moolenaar182c5be2010-06-25 05:37:59 +02003637#ifdef FEAT_TOOLBAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00003638 gui_gtk_register_stock_icons();
Bram Moolenaar182c5be2010-06-25 05:37:59 +02003639#endif
Bram Moolenaar30613902019-12-01 22:11:18 +01003640 // FIXME: Need to install the classic icons and a gtkrc.classic file.
3641 // The hard part is deciding install locations and the Makefile magic.
Bram Moolenaar98921892016-02-23 17:14:37 +01003642#if !GTK_CHECK_VERSION(3,0,0)
3643# if 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003644 gtk_rc_parse("gtkrc");
Bram Moolenaar98921892016-02-23 17:14:37 +01003645# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003646#endif
3647
Bram Moolenaar30613902019-12-01 22:11:18 +01003648 // Initialize values
Bram Moolenaar071d4272004-06-13 20:20:40 +00003649 gui.border_width = 2;
3650 gui.scrollbar_width = SB_DEFAULT_WIDTH;
3651 gui.scrollbar_height = SB_DEFAULT_WIDTH;
Bram Moolenaar36edf062016-07-21 22:10:12 +02003652#if GTK_CHECK_VERSION(3,0,0)
3653 gui.fgcolor = g_new(GdkRGBA, 1);
3654 gui.bgcolor = g_new(GdkRGBA, 1);
3655 gui.spcolor = g_new(GdkRGBA, 1);
3656#else
Bram Moolenaar30613902019-12-01 22:11:18 +01003657 // LINTED: avoid warning: conversion to 'unsigned long'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003658 gui.fgcolor = g_new0(GdkColor, 1);
Bram Moolenaar30613902019-12-01 22:11:18 +01003659 // LINTED: avoid warning: conversion to 'unsigned long'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003660 gui.bgcolor = g_new0(GdkColor, 1);
Bram Moolenaar30613902019-12-01 22:11:18 +01003661 // LINTED: avoid warning: conversion to 'unsigned long'
Bram Moolenaarf36d3692005-03-15 22:48:14 +00003662 gui.spcolor = g_new0(GdkColor, 1);
Bram Moolenaar36edf062016-07-21 22:10:12 +02003663#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003664
Bram Moolenaar30613902019-12-01 22:11:18 +01003665 // Initialise atoms
Bram Moolenaar3a6eaa52009-06-16 13:23:06 +00003666 html_atom = gdk_atom_intern("text/html", FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003667 utf8_string_atom = gdk_atom_intern("UTF8_STRING", FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003668
Bram Moolenaar30613902019-12-01 22:11:18 +01003669 // Set default foreground and background colors.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003670 gui.norm_pixel = gui.def_norm_pixel;
3671 gui.back_pixel = gui.def_back_pixel;
3672
3673 if (gtk_socket_id != 0)
3674 {
3675 GtkWidget *plug;
3676
Bram Moolenaar30613902019-12-01 22:11:18 +01003677 // Use GtkSocket from another app.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003678 plug = gtk_plug_new_for_display(gdk_display_get_default(),
3679 gtk_socket_id);
Bram Moolenaar98921892016-02-23 17:14:37 +01003680 if (plug != NULL && gtk_plug_get_socket_window(GTK_PLUG(plug)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003681 {
3682 gui.mainwin = plug;
3683 }
3684 else
3685 {
3686 g_warning("Connection to GTK+ socket (ID %u) failed",
3687 (unsigned int)gtk_socket_id);
Bram Moolenaar30613902019-12-01 22:11:18 +01003688 // Pretend we never wanted it if it failed (get own window)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003689 gtk_socket_id = 0;
3690 }
3691 }
3692
3693 if (gtk_socket_id == 0)
3694 {
3695#ifdef FEAT_GUI_GNOME
3696 if (using_gnome)
3697 {
3698 gui.mainwin = gnome_app_new("Vim", NULL);
3699# ifdef USE_XSMP
Bram Moolenaar30613902019-12-01 22:11:18 +01003700 // Use the GNOME save-yourself functionality now.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003701 xsmp_close();
3702# endif
3703 }
3704 else
3705#endif
3706 gui.mainwin = gtk_window_new(GTK_WINDOW_TOPLEVEL);
3707 }
3708
3709 gtk_widget_set_name(gui.mainwin, "vim-main-window");
3710
Bram Moolenaar30613902019-12-01 22:11:18 +01003711 // Create the PangoContext used for drawing all text.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003712 gui.text_context = gtk_widget_create_pango_context(gui.mainwin);
3713 pango_context_set_base_dir(gui.text_context, PANGO_DIRECTION_LTR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003714
Bram Moolenaar98921892016-02-23 17:14:37 +01003715 gtk_container_set_border_width(GTK_CONTAINER(gui.mainwin), 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003716 gtk_widget_add_events(gui.mainwin, GDK_VISIBILITY_NOTIFY_MASK);
3717
Bram Moolenaar98921892016-02-23 17:14:37 +01003718 g_signal_connect(G_OBJECT(gui.mainwin), "delete-event",
3719 G_CALLBACK(&delete_event_cb), NULL);
3720
3721 g_signal_connect(G_OBJECT(gui.mainwin), "realize",
3722 G_CALLBACK(&mainwin_realize), NULL);
Bram Moolenaarb463e8d2017-06-05 15:07:09 +02003723
3724 g_signal_connect(G_OBJECT(gui.mainwin), "screen-changed",
Bram Moolenaar071d4272004-06-13 20:20:40 +00003725 G_CALLBACK(&mainwin_screen_changed_cb), NULL);
Bram Moolenaarb463e8d2017-06-05 15:07:09 +02003726
Bram Moolenaar071d4272004-06-13 20:20:40 +00003727 gui.accel_group = gtk_accel_group_new();
3728 gtk_window_add_accel_group(GTK_WINDOW(gui.mainwin), gui.accel_group);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003729
Bram Moolenaar30613902019-12-01 22:11:18 +01003730 // A vertical box holds the menubar, toolbar and main text window.
Bram Moolenaar98921892016-02-23 17:14:37 +01003731#if GTK_CHECK_VERSION(3,2,0)
3732 vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
3733 gtk_box_set_homogeneous(GTK_BOX(vbox), FALSE);
3734#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003735 vbox = gtk_vbox_new(FALSE, 0);
Bram Moolenaar98921892016-02-23 17:14:37 +01003736#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003737
3738#ifdef FEAT_GUI_GNOME
3739 if (using_gnome)
3740 {
Bram Moolenaar182c5be2010-06-25 05:37:59 +02003741# if defined(FEAT_MENU)
Bram Moolenaar30613902019-12-01 22:11:18 +01003742 // automagically restore menubar/toolbar placement
Bram Moolenaar071d4272004-06-13 20:20:40 +00003743 gnome_app_enable_layout_config(GNOME_APP(gui.mainwin), TRUE);
3744# endif
3745 gnome_app_set_contents(GNOME_APP(gui.mainwin), vbox);
3746 }
3747 else
3748#endif
3749 {
3750 gtk_container_add(GTK_CONTAINER(gui.mainwin), vbox);
3751 gtk_widget_show(vbox);
3752 }
3753
3754#ifdef FEAT_MENU
3755 /*
3756 * Create the menubar and handle
3757 */
3758 gui.menubar = gtk_menu_bar_new();
3759 gtk_widget_set_name(gui.menubar, "vim-menubar");
3760
Bram Moolenaar30613902019-12-01 22:11:18 +01003761 // Avoid that GTK takes <F10> away from us.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003762 {
3763 GtkSettings *gtk_settings;
3764
3765 gtk_settings = gtk_settings_get_for_screen(gdk_screen_get_default());
3766 g_object_set(gtk_settings, "gtk-menu-bar-accel", NULL, NULL);
3767 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003768
3769
Bram Moolenaar071d4272004-06-13 20:20:40 +00003770# ifdef FEAT_GUI_GNOME
3771 if (using_gnome)
3772 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003773 BonoboDockItem *dockitem;
3774
3775 gnome_app_set_menus(GNOME_APP(gui.mainwin), GTK_MENU_BAR(gui.menubar));
3776 dockitem = gnome_app_get_dock_item_by_name(GNOME_APP(gui.mainwin),
3777 GNOME_APP_MENUBAR_NAME);
Bram Moolenaar30613902019-12-01 22:11:18 +01003778 // We don't want the menu to float.
Bram Moolenaardb552d602006-03-23 22:59:57 +00003779 bonobo_dock_item_set_behavior(dockitem,
3780 bonobo_dock_item_get_behavior(dockitem)
3781 | BONOBO_DOCK_ITEM_BEH_NEVER_FLOATING);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003782 gui.menubar_h = GTK_WIDGET(dockitem);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003783 }
3784 else
Bram Moolenaar30613902019-12-01 22:11:18 +01003785# endif // FEAT_GUI_GNOME
Bram Moolenaar071d4272004-06-13 20:20:40 +00003786 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003787 // Always show the menubar, otherwise <F10> doesn't work. It may be
3788 // disabled in gui_init() later.
Bram Moolenaar18144c82006-04-12 21:52:12 +00003789 gtk_widget_show(gui.menubar);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003790 gtk_box_pack_start(GTK_BOX(vbox), gui.menubar, FALSE, FALSE, 0);
3791 }
Bram Moolenaar30613902019-12-01 22:11:18 +01003792#endif // FEAT_MENU
Bram Moolenaar071d4272004-06-13 20:20:40 +00003793
3794#ifdef FEAT_TOOLBAR
3795 /*
3796 * Create the toolbar and handle
3797 */
Bram Moolenaar30613902019-12-01 22:11:18 +01003798 // some aesthetics on the toolbar
Bram Moolenaar98921892016-02-23 17:14:37 +01003799# ifdef USE_GTK3
Bram Moolenaar30613902019-12-01 22:11:18 +01003800 // TODO: Add GTK+ 3 code here using GtkCssProvider if necessary.
3801 // N.B. Since the default value of GtkToolbar::button-relief is
3802 // GTK_RELIEF_NONE, there's no need to specify that, probably.
Bram Moolenaar98921892016-02-23 17:14:37 +01003803# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003804 gtk_rc_parse_string(
3805 "style \"vim-toolbar-style\" {\n"
3806 " GtkToolbar::button_relief = GTK_RELIEF_NONE\n"
3807 "}\n"
3808 "widget \"*.vim-toolbar\" style \"vim-toolbar-style\"\n");
Bram Moolenaar98921892016-02-23 17:14:37 +01003809# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003810 gui.toolbar = gtk_toolbar_new();
3811 gtk_widget_set_name(gui.toolbar, "vim-toolbar");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003812 set_toolbar_style(GTK_TOOLBAR(gui.toolbar));
3813
3814# ifdef FEAT_GUI_GNOME
3815 if (using_gnome)
3816 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003817 BonoboDockItem *dockitem;
3818
3819 gnome_app_set_toolbar(GNOME_APP(gui.mainwin), GTK_TOOLBAR(gui.toolbar));
3820 dockitem = gnome_app_get_dock_item_by_name(GNOME_APP(gui.mainwin),
3821 GNOME_APP_TOOLBAR_NAME);
3822 gui.toolbar_h = GTK_WIDGET(dockitem);
Bram Moolenaar30613902019-12-01 22:11:18 +01003823 // When the toolbar is floating it gets stuck. So long as that isn't
3824 // fixed let's disallow floating.
Bram Moolenaare580b0c2006-03-21 21:33:03 +00003825 bonobo_dock_item_set_behavior(dockitem,
Bram Moolenaardb552d602006-03-23 22:59:57 +00003826 bonobo_dock_item_get_behavior(dockitem)
3827 | BONOBO_DOCK_ITEM_BEH_NEVER_FLOATING);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003828 gtk_container_set_border_width(GTK_CONTAINER(gui.toolbar), 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003829 }
3830 else
Bram Moolenaar30613902019-12-01 22:11:18 +01003831# endif // FEAT_GUI_GNOME
Bram Moolenaar071d4272004-06-13 20:20:40 +00003832 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003833 if (vim_strchr(p_go, GO_TOOLBAR) != NULL
3834 && (toolbar_flags & (TOOLBAR_TEXT | TOOLBAR_ICONS)))
3835 gtk_widget_show(gui.toolbar);
3836 gtk_box_pack_start(GTK_BOX(vbox), gui.toolbar, FALSE, FALSE, 0);
3837 }
Bram Moolenaar30613902019-12-01 22:11:18 +01003838#endif // FEAT_TOOLBAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00003839
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003840#ifdef FEAT_GUI_TABLINE
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00003841 /*
3842 * Use a Notebook for the tab pages labels. The labels are hidden by
3843 * default.
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00003844 */
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003845 gui.tabline = gtk_notebook_new();
3846 gtk_widget_show(gui.tabline);
3847 gtk_box_pack_start(GTK_BOX(vbox), gui.tabline, FALSE, FALSE, 0);
3848 gtk_notebook_set_show_border(GTK_NOTEBOOK(gui.tabline), FALSE);
3849 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(gui.tabline), FALSE);
Bram Moolenaar4a85b412006-04-23 22:40:29 +00003850 gtk_notebook_set_scrollable(GTK_NOTEBOOK(gui.tabline), TRUE);
Bram Moolenaar98921892016-02-23 17:14:37 +01003851# if !GTK_CHECK_VERSION(3,0,0)
Bram Moolenaar96351572006-05-05 21:16:59 +00003852 gtk_notebook_set_tab_border(GTK_NOTEBOOK(gui.tabline), FALSE);
Bram Moolenaar98921892016-02-23 17:14:37 +01003853# endif
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003854
Bram Moolenaar98921892016-02-23 17:14:37 +01003855# if !GTK_CHECK_VERSION(3,0,0)
Bram Moolenaar437df8f2006-04-27 21:47:44 +00003856 tabline_tooltip = gtk_tooltips_new();
3857 gtk_tooltips_enable(GTK_TOOLTIPS(tabline_tooltip));
Bram Moolenaar98921892016-02-23 17:14:37 +01003858# endif
Bram Moolenaar437df8f2006-04-27 21:47:44 +00003859
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003860 {
Bram Moolenaar437df8f2006-04-27 21:47:44 +00003861 GtkWidget *page, *label, *event_box;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003862
Bram Moolenaar30613902019-12-01 22:11:18 +01003863 // Add the first tab.
Bram Moolenaar98921892016-02-23 17:14:37 +01003864# if GTK_CHECK_VERSION(3,2,0)
3865 page = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
3866 gtk_box_set_homogeneous(GTK_BOX(page), FALSE);
3867# else
Bram Moolenaar437df8f2006-04-27 21:47:44 +00003868 page = gtk_vbox_new(FALSE, 0);
Bram Moolenaar98921892016-02-23 17:14:37 +01003869# endif
Bram Moolenaar437df8f2006-04-27 21:47:44 +00003870 gtk_widget_show(page);
3871 gtk_container_add(GTK_CONTAINER(gui.tabline), page);
3872 label = gtk_label_new("-Empty-");
3873 gtk_widget_show(label);
Bram Moolenaar437df8f2006-04-27 21:47:44 +00003874 event_box = gtk_event_box_new();
3875 gtk_widget_show(event_box);
Bram Moolenaar98921892016-02-23 17:14:37 +01003876 g_object_set_data(G_OBJECT(event_box), "tab_num", GINT_TO_POINTER(1L));
Bram Moolenaar98921892016-02-23 17:14:37 +01003877# if !GTK_CHECK_VERSION(3,14,0)
Bram Moolenaar8ea91232006-04-28 22:41:43 +00003878 gtk_misc_set_padding(GTK_MISC(label), 2, 2);
Bram Moolenaar98921892016-02-23 17:14:37 +01003879# endif
Bram Moolenaar437df8f2006-04-27 21:47:44 +00003880 gtk_container_add(GTK_CONTAINER(event_box), label);
Bram Moolenaar437df8f2006-04-27 21:47:44 +00003881 gtk_notebook_set_tab_label(GTK_NOTEBOOK(gui.tabline), page, event_box);
Bram Moolenaar92cbf622018-09-19 22:40:03 +02003882# if GTK_CHECK_VERSION(2,10,0)
Bram Moolenaarca05aa22017-10-22 15:36:14 +02003883 gtk_notebook_set_tab_reorderable(GTK_NOTEBOOK(gui.tabline), page, TRUE);
Bram Moolenaar92cbf622018-09-19 22:40:03 +02003884# endif
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00003885 }
Bram Moolenaar54a709e2006-05-04 21:57:11 +00003886
Bram Moolenaar98921892016-02-23 17:14:37 +01003887 g_signal_connect(G_OBJECT(gui.tabline), "switch-page",
3888 G_CALLBACK(on_select_tab), NULL);
Bram Moolenaar92cbf622018-09-19 22:40:03 +02003889# if GTK_CHECK_VERSION(2,10,0)
Bram Moolenaarca05aa22017-10-22 15:36:14 +02003890 g_signal_connect(G_OBJECT(gui.tabline), "page-reordered",
3891 G_CALLBACK(on_tab_reordered), NULL);
Bram Moolenaar92cbf622018-09-19 22:40:03 +02003892# endif
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003893
Bram Moolenaar30613902019-12-01 22:11:18 +01003894 // Create a popup menu for the tab line and connect it.
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003895 tabline_menu = create_tabline_menu();
Bram Moolenaar98921892016-02-23 17:14:37 +01003896 g_signal_connect_swapped(G_OBJECT(gui.tabline), "button-press-event",
3897 G_CALLBACK(on_tabline_menu), G_OBJECT(tabline_menu));
Bram Moolenaar30613902019-12-01 22:11:18 +01003898#endif // FEAT_GUI_TABLINE
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003899
Bram Moolenaar8a99e662020-10-21 16:10:21 +02003900 gui.formwin = gui_gtk_form_new();
Bram Moolenaar98921892016-02-23 17:14:37 +01003901 gtk_container_set_border_width(GTK_CONTAINER(gui.formwin), 0);
Bram Moolenaar98921892016-02-23 17:14:37 +01003902#if !GTK_CHECK_VERSION(3,0,0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003903 gtk_widget_set_events(gui.formwin, GDK_EXPOSURE_MASK);
Bram Moolenaar98921892016-02-23 17:14:37 +01003904#endif
Bram Moolenaar023fd5d2020-12-08 20:31:16 +01003905#if GTK_CHECK_VERSION(3,22,2)
3906 gtk_widget_set_name(gui.formwin, "vim-gtk-form");
3907#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003908
3909 gui.drawarea = gtk_drawing_area_new();
Bram Moolenaar98921892016-02-23 17:14:37 +01003910#if GTK_CHECK_VERSION(3,0,0)
3911 gui.surface = NULL;
Bram Moolenaar98921892016-02-23 17:14:37 +01003912#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003913
Bram Moolenaar30613902019-12-01 22:11:18 +01003914 // Determine which events we will filter.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003915 gtk_widget_set_events(gui.drawarea,
3916 GDK_EXPOSURE_MASK |
3917 GDK_ENTER_NOTIFY_MASK |
3918 GDK_LEAVE_NOTIFY_MASK |
3919 GDK_BUTTON_PRESS_MASK |
3920 GDK_BUTTON_RELEASE_MASK |
Bram Moolenaar071d4272004-06-13 20:20:40 +00003921 GDK_SCROLL_MASK |
Bram Moolenaar071d4272004-06-13 20:20:40 +00003922 GDK_KEY_PRESS_MASK |
3923 GDK_KEY_RELEASE_MASK |
3924 GDK_POINTER_MOTION_MASK |
3925 GDK_POINTER_MOTION_HINT_MASK);
3926
3927 gtk_widget_show(gui.drawarea);
Bram Moolenaar8a99e662020-10-21 16:10:21 +02003928 gui_gtk_form_put(GTK_FORM(gui.formwin), gui.drawarea, 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003929 gtk_widget_show(gui.formwin);
3930 gtk_box_pack_start(GTK_BOX(vbox), gui.formwin, TRUE, TRUE, 0);
3931
Bram Moolenaar30613902019-12-01 22:11:18 +01003932 // For GtkSockets, key-presses must go to the focus widget (drawarea)
3933 // and not the window.
Bram Moolenaar98921892016-02-23 17:14:37 +01003934 g_signal_connect((gtk_socket_id == 0) ? G_OBJECT(gui.mainwin)
3935 : G_OBJECT(gui.drawarea),
3936 "key-press-event",
3937 G_CALLBACK(key_press_event), NULL);
Bram Moolenaar98921892016-02-23 17:14:37 +01003938#if defined(FEAT_XIM) || GTK_CHECK_VERSION(3,0,0)
Bram Moolenaar30613902019-12-01 22:11:18 +01003939 // Also forward key release events for the benefit of GTK+ 2 input
3940 // modules. Try CTRL-SHIFT-xdigits to enter a Unicode code point.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003941 g_signal_connect((gtk_socket_id == 0) ? G_OBJECT(gui.mainwin)
3942 : G_OBJECT(gui.drawarea),
Bram Moolenaar98921892016-02-23 17:14:37 +01003943 "key-release-event",
Bram Moolenaar071d4272004-06-13 20:20:40 +00003944 G_CALLBACK(&key_release_event), NULL);
3945#endif
Bram Moolenaar98921892016-02-23 17:14:37 +01003946 g_signal_connect(G_OBJECT(gui.drawarea), "realize",
3947 G_CALLBACK(drawarea_realize_cb), NULL);
3948 g_signal_connect(G_OBJECT(gui.drawarea), "unrealize",
3949 G_CALLBACK(drawarea_unrealize_cb), NULL);
Bram Moolenaar664323e2018-09-18 22:30:07 +02003950#if GTK_CHECK_VERSION(3,0,0)
Bram Moolenaar98921892016-02-23 17:14:37 +01003951 g_signal_connect(G_OBJECT(gui.drawarea), "configure-event",
3952 G_CALLBACK(drawarea_configure_event_cb), NULL);
Bram Moolenaar664323e2018-09-18 22:30:07 +02003953#endif
3954#if GTK_CHECK_VERSION(3,22,2)
Bram Moolenaara859f042016-11-17 19:11:55 +01003955 g_signal_connect_after(G_OBJECT(gui.drawarea), "style-updated",
3956 G_CALLBACK(&drawarea_style_updated_cb), NULL);
Bram Moolenaar664323e2018-09-18 22:30:07 +02003957#else
Bram Moolenaar98921892016-02-23 17:14:37 +01003958 g_signal_connect_after(G_OBJECT(gui.drawarea), "style-set",
3959 G_CALLBACK(&drawarea_style_set_cb), NULL);
Bram Moolenaar98921892016-02-23 17:14:37 +01003960#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003961
Bram Moolenaar98921892016-02-23 17:14:37 +01003962#if !GTK_CHECK_VERSION(3,0,0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003963 gui.visibility = GDK_VISIBILITY_UNOBSCURED;
Bram Moolenaar98921892016-02-23 17:14:37 +01003964#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003965
Bram Moolenaarf96ae0b2019-07-28 15:21:55 +02003966#if !defined(USE_GNOME_SESSION)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003967 wm_protocols_atom = gdk_atom_intern("WM_PROTOCOLS", FALSE);
3968 save_yourself_atom = gdk_atom_intern("WM_SAVE_YOURSELF", FALSE);
3969#endif
3970
3971 if (gtk_socket_id != 0)
Bram Moolenaar30613902019-12-01 22:11:18 +01003972 // make sure keyboard input can go to the drawarea
Bram Moolenaar98921892016-02-23 17:14:37 +01003973 gtk_widget_set_can_focus(gui.drawarea, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003974
3975 /*
3976 * Set clipboard specific atoms
3977 */
3978 vim_atom = gdk_atom_intern(VIM_ATOM_NAME, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003979 vimenc_atom = gdk_atom_intern(VIMENC_ATOM_NAME, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003980 clip_star.gtk_sel_atom = GDK_SELECTION_PRIMARY;
3981 clip_plus.gtk_sel_atom = gdk_atom_intern("CLIPBOARD", FALSE);
3982
3983 /*
3984 * Start out by adding the configured border width into the border offset.
3985 */
3986 gui.border_offset = gui.border_width;
3987
Bram Moolenaar98921892016-02-23 17:14:37 +01003988#if GTK_CHECK_VERSION(3,0,0)
3989 g_signal_connect(G_OBJECT(gui.drawarea), "draw",
3990 G_CALLBACK(draw_event), NULL);
3991#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003992 gtk_signal_connect(GTK_OBJECT(gui.mainwin), "visibility_notify_event",
3993 GTK_SIGNAL_FUNC(visibility_event), NULL);
3994 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "expose_event",
3995 GTK_SIGNAL_FUNC(expose_event), NULL);
Bram Moolenaar98921892016-02-23 17:14:37 +01003996#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003997
3998 /*
3999 * Only install these enter/leave callbacks when 'p' in 'guioptions'.
4000 * Only needed for some window managers.
4001 */
4002 if (vim_strchr(p_go, GO_POINTER) != NULL)
4003 {
Bram Moolenaar98921892016-02-23 17:14:37 +01004004 g_signal_connect(G_OBJECT(gui.drawarea), "leave-notify-event",
4005 G_CALLBACK(leave_notify_event), NULL);
4006 g_signal_connect(G_OBJECT(gui.drawarea), "enter-notify-event",
4007 G_CALLBACK(enter_notify_event), NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004008 }
4009
Bram Moolenaar30613902019-12-01 22:11:18 +01004010 // Real windows can get focus ... GtkPlug, being a mere container can't,
Christian Brabandtee17b6f2023-09-09 11:23:50 +02004011 // only its widgets. Arguably, this could be common code and we do not
4012 // use the window focus at all, but let's be safe.
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004013 if (gtk_socket_id == 0)
4014 {
Bram Moolenaar98921892016-02-23 17:14:37 +01004015 g_signal_connect(G_OBJECT(gui.mainwin), "focus-out-event",
4016 G_CALLBACK(focus_out_event), NULL);
4017 g_signal_connect(G_OBJECT(gui.mainwin), "focus-in-event",
4018 G_CALLBACK(focus_in_event), NULL);
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004019 }
4020 else
4021 {
Bram Moolenaar98921892016-02-23 17:14:37 +01004022 g_signal_connect(G_OBJECT(gui.drawarea), "focus-out-event",
4023 G_CALLBACK(focus_out_event), NULL);
4024 g_signal_connect(G_OBJECT(gui.drawarea), "focus-in-event",
4025 G_CALLBACK(focus_in_event), NULL);
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004026#ifdef FEAT_GUI_TABLINE
Bram Moolenaar98921892016-02-23 17:14:37 +01004027 g_signal_connect(G_OBJECT(gui.tabline), "focus-out-event",
4028 G_CALLBACK(focus_out_event), NULL);
4029 g_signal_connect(G_OBJECT(gui.tabline), "focus-in-event",
4030 G_CALLBACK(focus_in_event), NULL);
Bram Moolenaar30613902019-12-01 22:11:18 +01004031#endif // FEAT_GUI_TABLINE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004032 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004033
Bram Moolenaar98921892016-02-23 17:14:37 +01004034 g_signal_connect(G_OBJECT(gui.drawarea), "motion-notify-event",
4035 G_CALLBACK(motion_notify_event), NULL);
4036 g_signal_connect(G_OBJECT(gui.drawarea), "button-press-event",
4037 G_CALLBACK(button_press_event), NULL);
4038 g_signal_connect(G_OBJECT(gui.drawarea), "button-release-event",
4039 G_CALLBACK(button_release_event), NULL);
4040 g_signal_connect(G_OBJECT(gui.drawarea), "scroll-event",
lilydjwg94ff09a2024-01-29 20:14:01 +01004041 G_CALLBACK(scroll_event), NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004042
Bram Moolenaar30613902019-12-01 22:11:18 +01004043 // Pretend we don't have input focus, we will get an event if we do.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004044 gui.in_focus = FALSE;
4045
Bram Moolenaar7ebf4e12018-08-07 20:01:40 +02004046 // Handle changes to the "Xft/DPI" setting.
4047 {
4048 GtkSettings *gtk_settings =
4049 gtk_settings_get_for_screen(gdk_screen_get_default());
4050
4051 g_signal_connect(gtk_settings, "notify::gtk-xft-dpi",
4052 G_CALLBACK(gtk_settings_xft_dpi_changed_cb), NULL);
4053 }
4054
Bram Moolenaar071d4272004-06-13 20:20:40 +00004055 return OK;
4056}
4057
Bram Moolenaarf96ae0b2019-07-28 15:21:55 +02004058#if defined(USE_GNOME_SESSION) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004059/*
4060 * This is called from gui_start() after a fork() has been done.
4061 * We have to tell the session manager our new PID.
4062 */
4063 void
4064gui_mch_forked(void)
4065{
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00004066 if (!using_gnome)
4067 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004068
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00004069 GnomeClient *client;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004070
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00004071 client = gnome_master_client();
4072
4073 if (client != NULL)
4074 gnome_client_set_process_id(client, getpid());
Bram Moolenaar071d4272004-06-13 20:20:40 +00004075}
Bram Moolenaarf96ae0b2019-07-28 15:21:55 +02004076#endif // USE_GNOME_SESSION
Bram Moolenaar071d4272004-06-13 20:20:40 +00004077
Bram Moolenaar98921892016-02-23 17:14:37 +01004078#if GTK_CHECK_VERSION(3,0,0)
Bram Moolenaar36edf062016-07-21 22:10:12 +02004079 static GdkRGBA
4080color_to_rgba(guicolor_T color)
Bram Moolenaar98921892016-02-23 17:14:37 +01004081{
Bram Moolenaar36edf062016-07-21 22:10:12 +02004082 GdkRGBA rgba;
4083 rgba.red = ((color & 0xff0000) >> 16) / 255.0;
4084 rgba.green = ((color & 0xff00) >> 8) / 255.0;
4085 rgba.blue = ((color & 0xff)) / 255.0;
4086 rgba.alpha = 1.0;
4087 return rgba;
Bram Moolenaar98921892016-02-23 17:14:37 +01004088}
4089
4090 static void
Bram Moolenaar36edf062016-07-21 22:10:12 +02004091set_cairo_source_rgba_from_color(cairo_t *cr, guicolor_T color)
Bram Moolenaar98921892016-02-23 17:14:37 +01004092{
Bram Moolenaar36edf062016-07-21 22:10:12 +02004093 const GdkRGBA rgba = color_to_rgba(color);
4094 cairo_set_source_rgba(cr, rgba.red, rgba.green, rgba.blue, rgba.alpha);
Bram Moolenaar98921892016-02-23 17:14:37 +01004095}
Bram Moolenaar30613902019-12-01 22:11:18 +01004096#endif // GTK_CHECK_VERSION(3,0,0)
Bram Moolenaar98921892016-02-23 17:14:37 +01004097
Bram Moolenaar071d4272004-06-13 20:20:40 +00004098/*
4099 * Called when the foreground or background color has been changed.
4100 * This used to change the graphics contexts directly but we are
4101 * currently manipulating them where desired.
4102 */
4103 void
4104gui_mch_new_colors(void)
4105{
Bram Moolenaar7988a6f2020-12-09 15:53:27 +01004106 if (gui.drawarea != NULL
4107#if GTK_CHECK_VERSION(3,22,2)
4108 && gui.formwin != NULL
4109#endif
4110 && gtk_widget_get_window(gui.drawarea) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004111 {
Bram Moolenaar664323e2018-09-18 22:30:07 +02004112#if !GTK_CHECK_VERSION(3,22,2)
Bram Moolenaar7988a6f2020-12-09 15:53:27 +01004113 GdkWindow * const da_win = gtk_widget_get_window(gui.drawarea);
Bram Moolenaar664323e2018-09-18 22:30:07 +02004114#endif
Bram Moolenaara859f042016-11-17 19:11:55 +01004115#if GTK_CHECK_VERSION(3,22,2)
Bram Moolenaar7988a6f2020-12-09 15:53:27 +01004116 GtkStyleContext * const context =
4117 gtk_widget_get_style_context(gui.formwin);
Bram Moolenaara859f042016-11-17 19:11:55 +01004118 GtkCssProvider * const provider = gtk_css_provider_new();
4119 gchar * const css = g_strdup_printf(
Bram Moolenaar023fd5d2020-12-08 20:31:16 +01004120 "widget#vim-gtk-form {\n"
Bram Moolenaara859f042016-11-17 19:11:55 +01004121 " background-color: #%.2lx%.2lx%.2lx;\n"
4122 "}\n",
4123 (gui.back_pixel >> 16) & 0xff,
4124 (gui.back_pixel >> 8) & 0xff,
4125 gui.back_pixel & 0xff);
4126
4127 gtk_css_provider_load_from_data(provider, css, -1, NULL);
4128 gtk_style_context_add_provider(context,
4129 GTK_STYLE_PROVIDER(provider), G_MAXUINT);
4130
4131 g_free(css);
4132 g_object_unref(provider);
Bram Moolenaar30613902019-12-01 22:11:18 +01004133#elif GTK_CHECK_VERSION(3,4,0) // !GTK_CHECK_VERSION(3,22,2)
Bram Moolenaar36edf062016-07-21 22:10:12 +02004134 GdkRGBA rgba;
Bram Moolenaar98921892016-02-23 17:14:37 +01004135
Bram Moolenaar36edf062016-07-21 22:10:12 +02004136 rgba = color_to_rgba(gui.back_pixel);
Bram Moolenaar98921892016-02-23 17:14:37 +01004137 {
4138 cairo_pattern_t * const pat = cairo_pattern_create_rgba(
Bram Moolenaar36edf062016-07-21 22:10:12 +02004139 rgba.red, rgba.green, rgba.blue, rgba.alpha);
Bram Moolenaar98921892016-02-23 17:14:37 +01004140 if (pat != NULL)
4141 {
4142 gdk_window_set_background_pattern(da_win, pat);
4143 cairo_pattern_destroy(pat);
4144 }
4145 else
Bram Moolenaar36edf062016-07-21 22:10:12 +02004146 gdk_window_set_background_rgba(da_win, &rgba);
Bram Moolenaar98921892016-02-23 17:14:37 +01004147 }
Bram Moolenaar30613902019-12-01 22:11:18 +01004148#else // !GTK_CHECK_VERSION(3,4,0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004149 GdkColor color = { 0, 0, 0, 0 };
4150
4151 color.pixel = gui.back_pixel;
Bram Moolenaar98921892016-02-23 17:14:37 +01004152 gdk_window_set_background(da_win, &color);
Bram Moolenaar30613902019-12-01 22:11:18 +01004153#endif // !GTK_CHECK_VERSION(3,22,2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004154 }
4155}
4156
Bram Moolenaar071d4272004-06-13 20:20:40 +00004157/*
4158 * This signal informs us about the need to rearrange our sub-widgets.
4159 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004160 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +00004161form_configure_event(GtkWidget *widget UNUSED,
4162 GdkEventConfigure *event,
4163 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004164{
Ernie Raeld42b8392022-04-23 19:52:23 +01004165 int usable_height = event->height;
4166#ifdef TRACK_RESIZE_HISTORY
4167 // Resize requests are made for gui.mainwin;
4168 // get its dimensions for searching if this event
Ernie Rael9f53e7b2022-04-17 18:27:49 +01004169 // is a response to a vim request.
Ernie Raeld42b8392022-04-23 19:52:23 +01004170 int w, h;
4171 gtk_window_get_size(GTK_WINDOW(gui.mainwin), &w, &h);
Ernie Rael9f53e7b2022-04-17 18:27:49 +01004172
Ernie Raeld42b8392022-04-23 19:52:23 +01004173# ifdef ENABLE_RESIZE_HISTORY_LOG
Ernie Rael9f53e7b2022-04-17 18:27:49 +01004174 ch_log(NULL, "gui_gtk: form_configure_event: (%d, %d) [%d, %d]",
4175 w, h, (int)Columns, (int)Rows);
Ernie Raeld42b8392022-04-23 19:52:23 +01004176# endif
Ernie Rael9f53e7b2022-04-17 18:27:49 +01004177
Ernie Raeld42b8392022-04-23 19:52:23 +01004178 // Look through history of recent vim resize requests.
Ernie Rael9f53e7b2022-04-17 18:27:49 +01004179 // If this event matches:
4180 // - "latest resize hist" We're caught up;
4181 // clear the history and process this event.
4182 // If history is, old to new, 100, 99, 100, 99. If this event is
4183 // 99 for the stale, it is matched against the current. History
Ernie Raeld42b8392022-04-23 19:52:23 +01004184 // is cleared, we may bounce, but no worse than before.
Ernie Rael9f53e7b2022-04-17 18:27:49 +01004185 // - "older/stale hist" If match an unused event in history,
4186 // then discard this event, and mark the matching event as used.
4187 // - "no match" Figure it's a user resize event, clear history.
4188 // NOTE: clear history is default, then all incoming events are processed
4189
4190 if (!MATCH_WIDTH_HEIGHT(latest_resize_hist, w, h)
4191 && match_stale_width_height(w, h))
4192 // discard stale event
4193 return TRUE;
4194 clear_resize_hists();
Ernie Raeld42b8392022-04-23 19:52:23 +01004195#endif
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004196
Bram Moolenaar182707a2016-11-21 20:55:58 +01004197#if GTK_CHECK_VERSION(3,22,2) && !GTK_CHECK_VERSION(3,22,4)
Bram Moolenaar30613902019-12-01 22:11:18 +01004198 // As of 3.22.2, GdkWindows have started distributing configure events to
4199 // their "native" children (https://git.gnome.org/browse/gtk+/commit/?h=gtk-3-22&id=12579fe71b3b8f79eb9c1b80e429443bcc437dd0).
4200 //
4201 // As can be seen from the implementation of move_native_children() and
4202 // configure_native_child() in gdkwindow.c, those functions actually
4203 // propagate configure events to every child, failing to distinguish
4204 // "native" one from non-native one.
4205 //
4206 // Naturally, configure events propagated to here like that are fallacious
4207 // and, as a matter of fact, they trigger a geometric collapse of
4208 // gui.formwin.
4209 //
4210 // To filter out such fallacious events, check if the given event is the
4211 // one that was sent out to the right place. Ignore it if not.
4212 //
4213 // Follow-up
4214 // After a few weeks later, the GdkWindow change mentioned above was
4215 // reverted (https://git.gnome.org/browse/gtk+/commit/?h=gtk-3-22&id=f70039cb9603a02d2369fec4038abf40a1711155).
4216 // The corresponding official release is 3.22.4.
Bram Moolenaara859f042016-11-17 19:11:55 +01004217 if (event->window != gtk_widget_get_window(gui.formwin))
4218 return TRUE;
4219#endif
4220
Bram Moolenaar30613902019-12-01 22:11:18 +01004221 // When in a GtkPlug, we can't guarantee valid heights (as a round
4222 // no. of char-heights), so we have to manually sanitise them.
4223 // Widths seem to sort themselves out, don't ask me why.
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004224 if (gtk_socket_id != 0)
Bram Moolenaar30613902019-12-01 22:11:18 +01004225 usable_height -= (gui.char_height - (gui.char_height/2)); // sic.
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004226
Bram Moolenaar8a99e662020-10-21 16:10:21 +02004227 gui_gtk_form_freeze(GTK_FORM(gui.formwin));
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004228 gui_resize_shell(event->width, usable_height);
Bram Moolenaar8a99e662020-10-21 16:10:21 +02004229 gui_gtk_form_thaw(GTK_FORM(gui.formwin));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004230
4231 return TRUE;
4232}
4233
4234/*
4235 * Function called when window already closed.
4236 * We can't do much more here than to trying to preserve what had been done,
4237 * since the window is already inevitably going away.
4238 */
Bram Moolenaar98921892016-02-23 17:14:37 +01004239 static void
4240mainwin_destroy_cb(GObject *object UNUSED, gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004241{
Bram Moolenaar30613902019-12-01 22:11:18 +01004242 // Don't write messages to the GUI anymore
Bram Moolenaar071d4272004-06-13 20:20:40 +00004243 full_screen = FALSE;
4244
4245 gui.mainwin = NULL;
4246 gui.drawarea = NULL;
4247
Bram Moolenaar30613902019-12-01 22:11:18 +01004248 if (!exiting) // only do anything if the destroy was unexpected
Bram Moolenaar071d4272004-06-13 20:20:40 +00004249 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004250 vim_strncpy(IObuff,
4251 (char_u *)_("Vim: Main window unexpectedly destroyed\n"),
4252 IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004253 preserve_exit();
4254 }
Bram Moolenaar36e294c2015-12-29 18:55:46 +01004255#ifdef USE_GRESOURCE
4256 gui_gtk_unregister_resource();
4257#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004258}
4259
Bram Moolenaara555e6f2021-03-18 22:28:57 +01004260 void
4261gui_gtk_get_screen_geom_of_win(
4262 GtkWidget *wid,
4263 int point_x, // x position of window if not initialized
4264 int point_y, // y position of window if not initialized
4265 int *screen_x,
4266 int *screen_y,
4267 int *width,
4268 int *height)
4269{
4270 GdkRectangle geometry;
4271 GdkWindow *win = gtk_widget_get_window(wid);
4272#if GTK_CHECK_VERSION(3,22,0)
Bram Moolenaar24001432021-03-20 12:36:46 +01004273 GdkDisplay *dpy;
4274 GdkMonitor *monitor;
Bram Moolenaara555e6f2021-03-18 22:28:57 +01004275
Bram Moolenaar24001432021-03-20 12:36:46 +01004276 if (wid != NULL && gtk_widget_get_realized(wid))
4277 dpy = gtk_widget_get_display(wid);
4278 else
4279 dpy = gdk_display_get_default();
4280 if (win != NULL)
4281 monitor = gdk_display_get_monitor_at_window(dpy, win);
4282 else
4283 monitor = gdk_display_get_monitor_at_point(dpy, point_x, point_y);
Bram Moolenaara555e6f2021-03-18 22:28:57 +01004284 gdk_monitor_get_geometry(monitor, &geometry);
4285#else
4286 GdkScreen* screen;
4287 int monitor;
4288
4289 if (wid != NULL && gtk_widget_has_screen(wid))
4290 screen = gtk_widget_get_screen(wid);
4291 else
4292 screen = gdk_screen_get_default();
Bram Moolenaar24001432021-03-20 12:36:46 +01004293 if (win != NULL)
Bram Moolenaara555e6f2021-03-18 22:28:57 +01004294 monitor = gdk_screen_get_monitor_at_window(screen, win);
Bram Moolenaar24001432021-03-20 12:36:46 +01004295 else
4296 monitor = gdk_screen_get_monitor_at_point(screen, point_x, point_y);
Bram Moolenaara555e6f2021-03-18 22:28:57 +01004297 gdk_screen_get_monitor_geometry(screen, monitor, &geometry);
4298#endif
4299 *screen_x = geometry.x;
4300 *screen_y = geometry.y;
4301 *width = geometry.width;
4302 *height = geometry.height;
4303}
4304
4305/*
4306 * The screen size is used to make sure the initial window doesn't get bigger
4307 * than the screen. This subtracts some room for menubar, toolbar and window
4308 * decorations.
4309 */
4310 static void
4311gui_gtk_get_screen_dimensions(
4312 int point_x,
4313 int point_y,
4314 int *screen_w,
4315 int *screen_h)
4316{
4317 int x, y;
4318
4319 gui_gtk_get_screen_geom_of_win(gui.mainwin, point_x, point_y,
4320 &x, &y, screen_w, screen_h);
4321
4322 // Subtract 'guiheadroom' from the height to allow some room for the
4323 // window manager (task list and window title bar).
4324 *screen_h -= p_ghr;
4325
4326 /*
4327 * FIXME: dirty trick: Because the gui_get_base_height() doesn't include
4328 * the toolbar and menubar for GTK, we subtract them from the screen
4329 * height, so that the window size can be made to fit on the screen.
4330 * This should be completely changed later.
4331 */
4332 *screen_w -= get_menu_tool_width();
4333 *screen_h -= get_menu_tool_height();
4334}
4335
4336 void
4337gui_mch_get_screen_dimensions(int *screen_w, int *screen_h)
4338{
4339 gui_gtk_get_screen_dimensions(0, 0, screen_w, screen_h);
4340}
4341
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004342
4343/*
4344 * Bit of a hack to ensure we start GtkPlug windows with the correct window
4345 * hints (and thus the required size from -geom), but that after that we
4346 * put the hints back to normal (the actual minimum size) so we may
4347 * subsequently be resized smaller. GtkSocket (the parent end) uses the
Bram Moolenaarc4568ab2018-11-16 16:21:05 +01004348 * plug's window 'min hints to set *its* minimum size, but that's also the
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004349 * only way we have of making ourselves bigger (by set lines/columns).
4350 * Thus set hints at start-up to ensure correct init. size, then a
Bram Moolenaar3f2a5d82016-02-27 22:08:16 +01004351 * second after the final attempt to reset the real minimum hints (done by
4352 * scrollbar init.), actually do the standard hints and stop the timer.
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004353 * We'll not let the default hints be set while this timer's active.
4354 */
Bram Moolenaar4ab79682017-08-27 14:50:47 +02004355 static timeout_cb_type
Bram Moolenaarb85cb212009-05-17 14:24:23 +00004356check_startup_plug_hints(gpointer data UNUSED)
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004357{
4358 if (init_window_hints_state == 1)
4359 {
Bram Moolenaar30613902019-12-01 22:11:18 +01004360 // Safe to use normal hints now
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004361 init_window_hints_state = 0;
4362 update_window_manager_hints(0, 0);
Bram Moolenaar30613902019-12-01 22:11:18 +01004363 return FALSE; // stop timer
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004364 }
4365
Bram Moolenaar30613902019-12-01 22:11:18 +01004366 // Keep on trying
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004367 init_window_hints_state = 1;
4368 return TRUE;
4369}
4370
Bram Moolenaar071d4272004-06-13 20:20:40 +00004371/*
4372 * Open the GUI window which was created by a call to gui_mch_init().
4373 */
4374 int
4375gui_mch_open(void)
4376{
4377 guicolor_T fg_pixel = INVALCOLOR;
4378 guicolor_T bg_pixel = INVALCOLOR;
Bram Moolenaar79ef6d62009-09-23 15:35:48 +00004379 guint pixel_width;
4380 guint pixel_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004381
Bram Moolenaar071d4272004-06-13 20:20:40 +00004382 /*
4383 * Allow setting a window role on the command line, or invent one
4384 * if none was specified. This is mainly useful for GNOME session
4385 * support; allowing the WM to restore window placement.
4386 */
4387 if (role_argument != NULL)
4388 {
4389 gtk_window_set_role(GTK_WINDOW(gui.mainwin), role_argument);
4390 }
4391 else
4392 {
4393 char *role;
4394
Bram Moolenaar30613902019-12-01 22:11:18 +01004395 // Invent a unique-enough ID string for the role
Bram Moolenaar071d4272004-06-13 20:20:40 +00004396 role = g_strdup_printf("vim-%u-%u-%u",
4397 (unsigned)mch_get_pid(),
4398 (unsigned)g_random_int(),
4399 (unsigned)time(NULL));
4400
4401 gtk_window_set_role(GTK_WINDOW(gui.mainwin), role);
4402 g_free(role);
4403 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004404
4405 if (gui_win_x != -1 && gui_win_y != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004406 gtk_window_move(GTK_WINDOW(gui.mainwin), gui_win_x, gui_win_y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004407
Bram Moolenaar30613902019-12-01 22:11:18 +01004408 // Determine user specified geometry, if present.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004409 if (gui.geom != NULL)
4410 {
4411 int mask;
4412 unsigned int w, h;
4413 int x = 0;
4414 int y = 0;
4415
4416 mask = XParseGeometry((char *)gui.geom, &x, &y, &w, &h);
4417
4418 if (mask & WidthValue)
4419 Columns = w;
4420 if (mask & HeightValue)
Bram Moolenaard68071d2006-05-02 22:08:30 +00004421 {
Bram Moolenaarb85cb212009-05-17 14:24:23 +00004422 if (p_window > (long)h - 1 || !option_was_set((char_u *)"window"))
Bram Moolenaard68071d2006-05-02 22:08:30 +00004423 p_window = h - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004424 Rows = h;
Bram Moolenaard68071d2006-05-02 22:08:30 +00004425 }
Bram Moolenaare057d402013-06-30 17:51:51 +02004426 limit_screen_size();
Bram Moolenaar2dd8b522007-10-19 12:33:44 +00004427
4428 pixel_width = (guint)(gui_get_base_width() + Columns * gui.char_width);
4429 pixel_height = (guint)(gui_get_base_height() + Rows * gui.char_height);
4430
Bram Moolenaar2dd8b522007-10-19 12:33:44 +00004431 pixel_width += get_menu_tool_width();
4432 pixel_height += get_menu_tool_height();
Bram Moolenaar2dd8b522007-10-19 12:33:44 +00004433
Bram Moolenaar071d4272004-06-13 20:20:40 +00004434 if (mask & (XValue | YValue))
Bram Moolenaar2dd8b522007-10-19 12:33:44 +00004435 {
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004436 int ww, hh;
Bram Moolenaara555e6f2021-03-18 22:28:57 +01004437
4438#ifdef FEAT_GUI_GTK
4439 gui_gtk_get_screen_dimensions(x, y, &ww, &hh);
4440#else
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004441 gui_mch_get_screen_dimensions(&ww, &hh);
Bram Moolenaara555e6f2021-03-18 22:28:57 +01004442#endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004443 hh += p_ghr + get_menu_tool_height();
4444 ww += get_menu_tool_width();
Bram Moolenaar2dd8b522007-10-19 12:33:44 +00004445 if (mask & XNegative)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004446 x += ww - pixel_width;
Bram Moolenaar2dd8b522007-10-19 12:33:44 +00004447 if (mask & YNegative)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004448 y += hh - pixel_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004449 gtk_window_move(GTK_WINDOW(gui.mainwin), x, y);
Bram Moolenaar2dd8b522007-10-19 12:33:44 +00004450 }
Bram Moolenaard23a8232018-02-10 18:45:26 +01004451 VIM_CLEAR(gui.geom);
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004452
Bram Moolenaar30613902019-12-01 22:11:18 +01004453 // From now until everyone's stopped trying to set the window hints
4454 // to their correct minimum values, stop them being set as we need
4455 // them to remain at our required size for the parent GtkSocket to
4456 // give us the right initial size.
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004457 if (gtk_socket_id != 0 && (mask & WidthValue || mask & HeightValue))
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004458 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004459 update_window_manager_hints(pixel_width, pixel_height);
4460 init_window_hints_state = 1;
Bram Moolenaar4ab79682017-08-27 14:50:47 +02004461 timeout_add(1000, check_startup_plug_hints, NULL);
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004462 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004463 }
4464
Bram Moolenaar79ef6d62009-09-23 15:35:48 +00004465 pixel_width = (guint)(gui_get_base_width() + Columns * gui.char_width);
4466 pixel_height = (guint)(gui_get_base_height() + Rows * gui.char_height);
Bram Moolenaar30613902019-12-01 22:11:18 +01004467 // For GTK2 changing the size of the form widget doesn't cause window
4468 // resizing.
Bram Moolenaardebe25a2010-06-06 17:41:24 +02004469 if (gtk_socket_id == 0)
Bram Moolenaar79ef6d62009-09-23 15:35:48 +00004470 gtk_window_resize(GTK_WINDOW(gui.mainwin), pixel_width, pixel_height);
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004471 update_window_manager_hints(0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004472
4473 if (foreground_argument != NULL)
4474 fg_pixel = gui_get_color((char_u *)foreground_argument);
4475 if (fg_pixel == INVALCOLOR)
4476 fg_pixel = gui_get_color((char_u *)"Black");
4477
4478 if (background_argument != NULL)
4479 bg_pixel = gui_get_color((char_u *)background_argument);
4480 if (bg_pixel == INVALCOLOR)
4481 bg_pixel = gui_get_color((char_u *)"White");
4482
4483 if (found_reverse_arg)
4484 {
4485 gui.def_norm_pixel = bg_pixel;
4486 gui.def_back_pixel = fg_pixel;
4487 }
4488 else
4489 {
4490 gui.def_norm_pixel = fg_pixel;
4491 gui.def_back_pixel = bg_pixel;
4492 }
4493
Bram Moolenaar30613902019-12-01 22:11:18 +01004494 // Get the colors from the "Normal" and "Menu" group (set in syntax.c or
4495 // in a vimrc file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004496 set_normal_colors();
4497
Bram Moolenaar30613902019-12-01 22:11:18 +01004498 // Check that none of the colors are the same as the background color
Bram Moolenaar071d4272004-06-13 20:20:40 +00004499 gui_check_colors();
4500
Bram Moolenaar30613902019-12-01 22:11:18 +01004501 // Get the colors for the highlight groups (gui_check_colors() might have
4502 // changed them).
4503 highlight_gui_started(); // re-init colors and fonts
Bram Moolenaar071d4272004-06-13 20:20:40 +00004504
Bram Moolenaar98921892016-02-23 17:14:37 +01004505 g_signal_connect(G_OBJECT(gui.mainwin), "destroy",
4506 G_CALLBACK(mainwin_destroy_cb), NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004507
Bram Moolenaar071d4272004-06-13 20:20:40 +00004508 /*
4509 * Notify the fixed area about the need to resize the contents of the
4510 * gui.formwin, which we use for random positioning of the included
4511 * components.
4512 *
4513 * We connect this signal deferred finally after anything is in place,
4514 * since this is intended to handle resizements coming from the window
4515 * manager upon us and should not interfere with what VIM is requesting
4516 * upon startup.
4517 */
Ernie Raeld42b8392022-04-23 19:52:23 +01004518#ifdef TRACK_RESIZE_HISTORY
Ernie Rael9f53e7b2022-04-17 18:27:49 +01004519 latest_resize_hist = ALLOC_CLEAR_ONE(resize_hist_T);
Ernie Raeld42b8392022-04-23 19:52:23 +01004520#endif
Bram Moolenaar98921892016-02-23 17:14:37 +01004521 g_signal_connect(G_OBJECT(gui.formwin), "configure-event",
Ernie Rael9f53e7b2022-04-17 18:27:49 +01004522 G_CALLBACK(form_configure_event), NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004523
4524#ifdef FEAT_DND
Bram Moolenaar30613902019-12-01 22:11:18 +01004525 // Set up for receiving DND items.
Bram Moolenaara76638f2010-06-05 12:49:46 +02004526 gui_gtk_set_dnd_targets();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004527
Bram Moolenaar98921892016-02-23 17:14:37 +01004528 g_signal_connect(G_OBJECT(gui.drawarea), "drag-data-received",
Ernie Rael9f53e7b2022-04-17 18:27:49 +01004529 G_CALLBACK(drag_data_received_cb), NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004530#endif
4531
Bram Moolenaar30613902019-12-01 22:11:18 +01004532 // With GTK+ 2, we need to iconify the window before calling show()
4533 // to avoid mapping the window for a short time.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004534 if (found_iconic_arg && gtk_socket_id == 0)
4535 gui_mch_iconify();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004536
4537 {
Bram Moolenaar182c5be2010-06-25 05:37:59 +02004538#if defined(FEAT_GUI_GNOME) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004539 unsigned long menu_handler = 0;
4540# ifdef FEAT_TOOLBAR
4541 unsigned long tool_handler = 0;
4542# endif
4543 /*
4544 * Urgh hackish :/ For some reason BonoboDockLayout always forces a
4545 * show when restoring the saved layout configuration. We can't just
4546 * hide the widgets again after gtk_widget_show(gui.mainwin) since it's
4547 * a toplevel window and thus will be realized immediately. Instead,
4548 * connect signal handlers to hide the widgets just after they've been
4549 * marked visible, but before the main window is realized.
4550 */
4551 if (using_gnome && vim_strchr(p_go, GO_MENUS) == NULL)
4552 menu_handler = g_signal_connect_after(gui.menubar_h, "show",
4553 G_CALLBACK(&gtk_widget_hide),
4554 NULL);
4555# ifdef FEAT_TOOLBAR
4556 if (using_gnome && vim_strchr(p_go, GO_TOOLBAR) == NULL
4557 && (toolbar_flags & (TOOLBAR_TEXT | TOOLBAR_ICONS)))
4558 tool_handler = g_signal_connect_after(gui.toolbar_h, "show",
4559 G_CALLBACK(&gtk_widget_hide),
4560 NULL);
4561# endif
4562#endif
4563 gtk_widget_show(gui.mainwin);
4564
Bram Moolenaar182c5be2010-06-25 05:37:59 +02004565#if defined(FEAT_GUI_GNOME) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004566 if (menu_handler != 0)
4567 g_signal_handler_disconnect(gui.menubar_h, menu_handler);
4568# ifdef FEAT_TOOLBAR
4569 if (tool_handler != 0)
4570 g_signal_handler_disconnect(gui.toolbar_h, tool_handler);
4571# endif
4572#endif
4573 }
4574
lilydjwg94ff09a2024-01-29 20:14:01 +01004575 /*
4576 * Add selection handler functions.
4577 */
4578 g_signal_connect(G_OBJECT(gui.drawarea), "selection-clear-event",
4579 G_CALLBACK(selection_clear_event), NULL);
4580 g_signal_connect(G_OBJECT(gui.drawarea), "selection-received",
4581 G_CALLBACK(selection_received_cb), NULL);
4582
4583 g_signal_connect(G_OBJECT(gui.drawarea), "selection-get",
4584 G_CALLBACK(selection_get_cb), NULL);
4585
Bram Moolenaar071d4272004-06-13 20:20:40 +00004586 return OK;
4587}
4588
Bram Moolenaar32fbc4f2020-09-29 22:16:09 +02004589/*
4590 * Clean up for when exiting Vim.
4591 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004592 void
Bram Moolenaarb85cb212009-05-17 14:24:23 +00004593gui_mch_exit(int rc UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004594{
Bram Moolenaar32fbc4f2020-09-29 22:16:09 +02004595 // Clean up, unless we don't want to invoke free().
4596 if (gui.mainwin != NULL && !really_exiting)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004597 gtk_widget_destroy(gui.mainwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004598}
4599
4600/*
4601 * Get the position of the top left corner of the window.
4602 */
4603 int
4604gui_mch_get_winpos(int *x, int *y)
4605{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004606 gtk_window_get_position(GTK_WINDOW(gui.mainwin), x, y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004607 return OK;
4608}
4609
4610/*
4611 * Set the position of the top left corner of the window to the given
4612 * coordinates.
4613 */
4614 void
4615gui_mch_set_winpos(int x, int y)
4616{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004617 gtk_window_move(GTK_WINDOW(gui.mainwin), x, y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004618}
4619
Bram Moolenaar98921892016-02-23 17:14:37 +01004620#if !GTK_CHECK_VERSION(3,0,0)
4621# if 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004622static int resize_idle_installed = FALSE;
4623/*
4624 * Idle handler to force resize. Used by gui_mch_set_shellsize() to ensure
4625 * the shell size doesn't exceed the window size, i.e. if the window manager
4626 * ignored our size request. Usually this happens if the window is maximized.
4627 *
4628 * FIXME: It'd be nice if we could find a little more orthodox solution.
4629 * See also the remark below in gui_mch_set_shellsize().
4630 *
4631 * DISABLED: When doing ":set lines+=1" this function would first invoke
4632 * gui_resize_shell() with the old size, then the normal callback would
4633 * report the new size through form_configure_event(). That caused the window
4634 * layout to be messed up.
4635 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004636 static gboolean
4637force_shell_resize_idle(gpointer data)
4638{
4639 if (gui.mainwin != NULL
4640 && GTK_WIDGET_REALIZED(gui.mainwin)
4641 && GTK_WIDGET_VISIBLE(gui.mainwin))
4642 {
4643 int width;
4644 int height;
4645
4646 gtk_window_get_size(GTK_WINDOW(gui.mainwin), &width, &height);
4647
4648 width -= get_menu_tool_width();
4649 height -= get_menu_tool_height();
4650
4651 gui_resize_shell(width, height);
4652 }
4653
4654 resize_idle_installed = FALSE;
Bram Moolenaar30613902019-12-01 22:11:18 +01004655 return FALSE; // don't call me again
Bram Moolenaar071d4272004-06-13 20:20:40 +00004656}
Bram Moolenaar98921892016-02-23 17:14:37 +01004657# endif
Bram Moolenaar30613902019-12-01 22:11:18 +01004658#endif // !GTK_CHECK_VERSION(3,0,0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004659
Bram Moolenaar09736232009-09-23 16:14:49 +00004660/*
4661 * Return TRUE if the main window is maximized.
4662 */
4663 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004664gui_mch_maximized(void)
Bram Moolenaar09736232009-09-23 16:14:49 +00004665{
Bram Moolenaar98921892016-02-23 17:14:37 +01004666 return (gui.mainwin != NULL && gtk_widget_get_window(gui.mainwin) != NULL
4667 && (gdk_window_get_state(gtk_widget_get_window(gui.mainwin))
4668 & GDK_WINDOW_STATE_MAXIMIZED));
Bram Moolenaar09736232009-09-23 16:14:49 +00004669}
4670
4671/*
4672 * Unmaximize the main window
4673 */
4674 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004675gui_mch_unmaximize(void)
Bram Moolenaar09736232009-09-23 16:14:49 +00004676{
4677 if (gui.mainwin != NULL)
4678 gtk_window_unmaximize(GTK_WINDOW(gui.mainwin));
4679}
Bram Moolenaar09736232009-09-23 16:14:49 +00004680
Bram Moolenaar071d4272004-06-13 20:20:40 +00004681/*
Bram Moolenaar8ac44152017-11-09 18:33:29 +01004682 * Called when the font changed while the window is maximized or GO_KEEPWINSIZE
4683 * is set. Compute the new Rows and Columns. This is like resizing the
4684 * window.
Bram Moolenaar12bc1b52011-08-10 17:44:45 +02004685 */
4686 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004687gui_mch_newfont(void)
Bram Moolenaar12bc1b52011-08-10 17:44:45 +02004688{
4689 int w, h;
4690
4691 gtk_window_get_size(GTK_WINDOW(gui.mainwin), &w, &h);
4692 w -= get_menu_tool_width();
4693 h -= get_menu_tool_height();
4694 gui_resize_shell(w, h);
4695}
4696
4697/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004698 * Set the windows size.
4699 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004700 void
4701gui_mch_set_shellsize(int width, int height,
Bram Moolenaarb85cb212009-05-17 14:24:23 +00004702 int min_width UNUSED, int min_height UNUSED,
4703 int base_width UNUSED, int base_height UNUSED,
4704 int direction UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004705{
Bram Moolenaar30613902019-12-01 22:11:18 +01004706 // give GTK+ a chance to put all widget's into place
Bram Moolenaar071d4272004-06-13 20:20:40 +00004707 gui_mch_update();
4708
Bram Moolenaar30613902019-12-01 22:11:18 +01004709 // this will cause the proper resizement to happen too
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004710 if (gtk_socket_id == 0)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004711 update_window_manager_hints(0, 0);
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004712
Bram Moolenaar30613902019-12-01 22:11:18 +01004713 // With GTK+ 2, changing the size of the form widget doesn't resize
4714 // the window. So let's do it the other way around and resize the
4715 // main window instead.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004716 width += get_menu_tool_width();
4717 height += get_menu_tool_height();
4718
Ernie Raeld42b8392022-04-23 19:52:23 +01004719#ifdef TRACK_RESIZE_HISTORY
Ernie Rael9f53e7b2022-04-17 18:27:49 +01004720 alloc_resize_hist(width, height); // track the resize request
Ernie Raeld42b8392022-04-23 19:52:23 +01004721#endif
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004722 if (gtk_socket_id == 0)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004723 gtk_window_resize(GTK_WINDOW(gui.mainwin), width, height);
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004724 else
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004725 update_window_manager_hints(width, height);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004726
Bram Moolenaar98921892016-02-23 17:14:37 +01004727# if !GTK_CHECK_VERSION(3,0,0)
4728# if 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004729 if (!resize_idle_installed)
4730 {
4731 g_idle_add_full(GDK_PRIORITY_EVENTS + 10,
4732 &force_shell_resize_idle, NULL, NULL);
4733 resize_idle_installed = TRUE;
4734 }
Bram Moolenaar98921892016-02-23 17:14:37 +01004735# endif
Bram Moolenaar30613902019-12-01 22:11:18 +01004736# endif // !GTK_CHECK_VERSION(3,0,0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004737 /*
4738 * Wait until all events are processed to prevent a crash because the
4739 * real size of the drawing area doesn't reflect Vim's internal ideas.
4740 *
4741 * This is a bit of a hack, since Vim is a terminal application with a GUI
4742 * on top, while the GUI expects to be the boss.
4743 */
4744 gui_mch_update();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004745}
4746
Bram Moolenaar071d4272004-06-13 20:20:40 +00004747 void
Bram Moolenaarb85cb212009-05-17 14:24:23 +00004748gui_mch_settitle(char_u *title, char_u *icon UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004749{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004750 if (title != NULL && output_conv.vc_type != CONV_NONE)
4751 title = string_convert(&output_conv, title, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004752
4753 gtk_window_set_title(GTK_WINDOW(gui.mainwin), (const char *)title);
4754
Bram Moolenaar071d4272004-06-13 20:20:40 +00004755 if (output_conv.vc_type != CONV_NONE)
4756 vim_free(title);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004757}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004758
4759#if defined(FEAT_MENU) || defined(PROTO)
4760 void
4761gui_mch_enable_menu(int showit)
4762{
4763 GtkWidget *widget;
4764
4765# ifdef FEAT_GUI_GNOME
4766 if (using_gnome)
4767 widget = gui.menubar_h;
4768 else
4769# endif
4770 widget = gui.menubar;
4771
Bram Moolenaar30613902019-12-01 22:11:18 +01004772 // Do not disable the menu while starting up, otherwise F10 doesn't work.
Bram Moolenaar98921892016-02-23 17:14:37 +01004773 if (!showit != !gtk_widget_get_visible(widget) && !gui.starting)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004774 {
4775 if (showit)
4776 gtk_widget_show(widget);
4777 else
4778 gtk_widget_hide(widget);
4779
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004780 update_window_manager_hints(0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004781 }
4782}
Bram Moolenaar30613902019-12-01 22:11:18 +01004783#endif // FEAT_MENU
Bram Moolenaar071d4272004-06-13 20:20:40 +00004784
4785#if defined(FEAT_TOOLBAR) || defined(PROTO)
4786 void
4787gui_mch_show_toolbar(int showit)
4788{
4789 GtkWidget *widget;
4790
4791 if (gui.toolbar == NULL)
4792 return;
4793
4794# ifdef FEAT_GUI_GNOME
4795 if (using_gnome)
4796 widget = gui.toolbar_h;
4797 else
4798# endif
4799 widget = gui.toolbar;
4800
4801 if (showit)
4802 set_toolbar_style(GTK_TOOLBAR(gui.toolbar));
4803
Bram Moolenaar98921892016-02-23 17:14:37 +01004804 if (!showit != !gtk_widget_get_visible(widget))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004805 {
4806 if (showit)
4807 gtk_widget_show(widget);
4808 else
4809 gtk_widget_hide(widget);
4810
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004811 update_window_manager_hints(0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004812 }
4813}
Bram Moolenaar30613902019-12-01 22:11:18 +01004814#endif // FEAT_TOOLBAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00004815
Bram Moolenaar071d4272004-06-13 20:20:40 +00004816/*
4817 * Check if a given font is a CJK font. This is done in a very crude manner. It
4818 * just see if U+04E00 for zh and ja and U+AC00 for ko are covered in a given
4819 * font. Consequently, this function cannot be used as a general purpose check
4820 * for CJK-ness for which fontconfig APIs should be used. This is only used by
4821 * gui_mch_init_font() to deal with 'CJK fixed width fonts'.
4822 */
4823 static int
4824is_cjk_font(PangoFontDescription *font_desc)
4825{
4826 static const char * const cjk_langs[] =
4827 {"zh_CN", "zh_TW", "zh_HK", "ja", "ko"};
4828
4829 PangoFont *font;
4830 unsigned i;
4831 int is_cjk = FALSE;
4832
4833 font = pango_context_load_font(gui.text_context, font_desc);
4834
4835 if (font == NULL)
4836 return FALSE;
4837
4838 for (i = 0; !is_cjk && i < G_N_ELEMENTS(cjk_langs); ++i)
4839 {
4840 PangoCoverage *coverage;
4841 gunichar uc;
4842
Bram Moolenaar14285cb2020-03-27 20:58:37 +01004843 // Valgrind reports a leak for pango_language_from_string(), but the
4844 // documentation says "This is owned by Pango and should not be freed".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004845 coverage = pango_font_get_coverage(
4846 font, pango_language_from_string(cjk_langs[i]));
4847
4848 if (coverage != NULL)
4849 {
4850 uc = (cjk_langs[i][0] == 'k') ? 0xAC00 : 0x4E00;
4851 is_cjk = (pango_coverage_get(coverage, uc) == PANGO_COVERAGE_EXACT);
James McCoyfe146362023-08-28 21:29:13 +02004852#if PANGO_VERSION_CHECK(1, 52, 0)
4853 g_object_unref(coverage);
4854#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004855 pango_coverage_unref(coverage);
James McCoyfe146362023-08-28 21:29:13 +02004856#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004857 }
4858 }
4859
4860 g_object_unref(font);
4861
4862 return is_cjk;
4863}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004864
Bram Moolenaar231334e2005-07-25 20:46:57 +00004865/*
4866 * Adjust gui.char_height (after 'linespace' was changed).
4867 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004868 int
Bram Moolenaar231334e2005-07-25 20:46:57 +00004869gui_mch_adjust_charheight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004870{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004871 PangoFontMetrics *metrics;
4872 int ascent;
4873 int descent;
4874
4875 metrics = pango_context_get_metrics(gui.text_context, gui.norm_font,
4876 pango_context_get_language(gui.text_context));
4877 ascent = pango_font_metrics_get_ascent(metrics);
4878 descent = pango_font_metrics_get_descent(metrics);
4879
4880 pango_font_metrics_unref(metrics);
4881
Bram Moolenaar70cf4582020-10-27 20:43:26 +01004882 // Round up when the value is more than about 1/16 of a pixel above a whole
4883 // pixel (12.0624 becomes 12, 12.07 becomes 13). Then add 'linespace'.
4884 gui.char_height = (ascent + descent + (PANGO_SCALE * 15) / 16)
4885 / PANGO_SCALE + p_linespace;
Bram Moolenaar30613902019-12-01 22:11:18 +01004886 // LINTED: avoid warning: bitwise operation on signed value
Bram Moolenaar071d4272004-06-13 20:20:40 +00004887 gui.char_ascent = PANGO_PIXELS(ascent + p_linespace * PANGO_SCALE / 2);
4888
Bram Moolenaar30613902019-12-01 22:11:18 +01004889 // A not-positive value of char_height may crash Vim. Only happens
4890 // if 'linespace' is negative (which does make sense sometimes).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004891 gui.char_ascent = MAX(gui.char_ascent, 0);
4892 gui.char_height = MAX(gui.char_height, gui.char_ascent + 1);
4893
4894 return OK;
4895}
4896
Bram Moolenaar98921892016-02-23 17:14:37 +01004897#if GTK_CHECK_VERSION(3,0,0)
Bram Moolenaar30613902019-12-01 22:11:18 +01004898// Callback function used in gui_mch_font_dialog()
Bram Moolenaar98921892016-02-23 17:14:37 +01004899 static gboolean
4900font_filter(const PangoFontFamily *family,
4901 const PangoFontFace *face UNUSED,
4902 gpointer data UNUSED)
4903{
4904 return pango_font_family_is_monospace((PangoFontFamily *)family);
4905}
4906#endif
4907
Bram Moolenaar071d4272004-06-13 20:20:40 +00004908/*
4909 * Put up a font dialog and return the selected font name in allocated memory.
4910 * "oldval" is the previous value. Return NULL when cancelled.
4911 * This should probably go into gui_gtk.c. Hmm.
4912 * FIXME:
4913 * The GTK2 font selection dialog has no filtering API. So we could either
4914 * a) implement our own (possibly copying the code from somewhere else) or
4915 * b) just live with it.
4916 */
4917 char_u *
4918gui_mch_font_dialog(char_u *oldval)
4919{
4920 GtkWidget *dialog;
4921 int response;
4922 char_u *fontname = NULL;
4923 char_u *oldname;
4924
Bram Moolenaar98921892016-02-23 17:14:37 +01004925#if GTK_CHECK_VERSION(3,2,0)
4926 dialog = gtk_font_chooser_dialog_new(NULL, NULL);
4927 gtk_font_chooser_set_filter_func(GTK_FONT_CHOOSER(dialog), font_filter,
4928 NULL, NULL);
4929#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004930 dialog = gtk_font_selection_dialog_new(NULL);
Bram Moolenaar98921892016-02-23 17:14:37 +01004931#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004932
4933 gtk_window_set_transient_for(GTK_WINDOW(dialog), GTK_WINDOW(gui.mainwin));
4934 gtk_window_set_destroy_with_parent(GTK_WINDOW(dialog), TRUE);
4935
4936 if (oldval != NULL && oldval[0] != NUL)
4937 {
4938 if (output_conv.vc_type != CONV_NONE)
4939 oldname = string_convert(&output_conv, oldval, NULL);
4940 else
4941 oldname = oldval;
4942
Bram Moolenaar30613902019-12-01 22:11:18 +01004943 // Annoying bug in GTK (or Pango): if the font name does not include a
4944 // size, zero is used. Use default point size ten.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004945 if (!vim_isdigit(oldname[STRLEN(oldname) - 1]))
4946 {
4947 char_u *p = vim_strnsave(oldname, STRLEN(oldname) + 3);
4948
4949 if (p != NULL)
4950 {
4951 STRCPY(p + STRLEN(p), " 10");
4952 if (oldname != oldval)
4953 vim_free(oldname);
4954 oldname = p;
4955 }
4956 }
4957
Bram Moolenaar98921892016-02-23 17:14:37 +01004958#if GTK_CHECK_VERSION(3,2,0)
4959 gtk_font_chooser_set_font(
4960 GTK_FONT_CHOOSER(dialog), (const gchar *)oldname);
4961#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004962 gtk_font_selection_dialog_set_font_name(
4963 GTK_FONT_SELECTION_DIALOG(dialog), (const char *)oldname);
Bram Moolenaar98921892016-02-23 17:14:37 +01004964#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004965
4966 if (oldname != oldval)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004967 vim_free(oldname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004968 }
Bram Moolenaarbef9d832009-09-11 13:46:41 +00004969 else
Bram Moolenaar98921892016-02-23 17:14:37 +01004970#if GTK_CHECK_VERSION(3,2,0)
4971 gtk_font_chooser_set_font(
4972 GTK_FONT_CHOOSER(dialog), DEFAULT_FONT);
4973#else
Bram Moolenaarbef9d832009-09-11 13:46:41 +00004974 gtk_font_selection_dialog_set_font_name(
4975 GTK_FONT_SELECTION_DIALOG(dialog), DEFAULT_FONT);
Bram Moolenaar98921892016-02-23 17:14:37 +01004976#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004977
4978 response = gtk_dialog_run(GTK_DIALOG(dialog));
4979
4980 if (response == GTK_RESPONSE_OK)
4981 {
4982 char *name;
4983
Bram Moolenaar98921892016-02-23 17:14:37 +01004984#if GTK_CHECK_VERSION(3,2,0)
4985 name = gtk_font_chooser_get_font(GTK_FONT_CHOOSER(dialog));
4986#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004987 name = gtk_font_selection_dialog_get_font_name(
4988 GTK_FONT_SELECTION_DIALOG(dialog));
Bram Moolenaar98921892016-02-23 17:14:37 +01004989#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004990 if (name != NULL)
4991 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004992 char_u *p;
4993
Bram Moolenaar30613902019-12-01 22:11:18 +01004994 // Apparently some font names include a comma, need to escape
4995 // that, because in 'guifont' it separates names.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004996 p = vim_strsave_escaped((char_u *)name, (char_u *)",");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004997 g_free(name);
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004998 if (p != NULL && input_conv.vc_type != CONV_NONE)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004999 {
5000 fontname = string_convert(&input_conv, p, NULL);
5001 vim_free(p);
5002 }
5003 else
5004 fontname = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005005 }
5006 }
5007
5008 if (response != GTK_RESPONSE_NONE)
5009 gtk_widget_destroy(dialog);
5010
5011 return fontname;
5012}
5013
5014/*
5015 * Some monospace fonts don't support a bold weight, and fall back
5016 * silently to the regular weight. But this is no good since our text
5017 * drawing function can emulate bold by overstriking. So let's try
5018 * to detect whether bold weight is actually available and emulate it
5019 * otherwise.
5020 *
5021 * Note that we don't need to check for italic style since Xft can
5022 * emulate italic on its own, provided you have a proper fontconfig
5023 * setup. We wouldn't be able to emulate it in Vim anyway.
5024 */
5025 static void
5026get_styled_font_variants(void)
5027{
5028 PangoFontDescription *bold_font_desc;
5029 PangoFont *plain_font;
5030 PangoFont *bold_font;
5031
5032 gui.font_can_bold = FALSE;
5033
5034 plain_font = pango_context_load_font(gui.text_context, gui.norm_font);
5035
5036 if (plain_font == NULL)
5037 return;
5038
5039 bold_font_desc = pango_font_description_copy_static(gui.norm_font);
5040 pango_font_description_set_weight(bold_font_desc, PANGO_WEIGHT_BOLD);
5041
5042 bold_font = pango_context_load_font(gui.text_context, bold_font_desc);
5043 /*
5044 * The comparison relies on the unique handle nature of a PangoFont*,
5045 * i.e. it's assumed that a different PangoFont* won't refer to the
5046 * same font. Seems to work, and failing here isn't critical anyway.
5047 */
5048 if (bold_font != NULL)
5049 {
5050 gui.font_can_bold = (bold_font != plain_font);
5051 g_object_unref(bold_font);
5052 }
5053
5054 pango_font_description_free(bold_font_desc);
Christian Brabandta5218a72023-11-19 16:25:45 +01005055 if (bold_font != NULL && gui.font_can_bold)
5056 g_object_unref(plain_font);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005057}
5058
Bram Moolenaar071d4272004-06-13 20:20:40 +00005059static PangoEngineShape *default_shape_engine = NULL;
5060
5061/*
5062 * Create a map from ASCII characters in the range [32,126] to glyphs
5063 * of the current font. This is used by gui_gtk2_draw_string() to skip
5064 * the itemize and shaping process for the most common case.
5065 */
5066 static void
5067ascii_glyph_table_init(void)
5068{
Bram Moolenaar16350cb2016-08-14 20:27:34 +02005069 char_u ascii_chars[2 * 128];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005070 PangoAttrList *attr_list;
5071 GList *item_list;
5072 int i;
5073
5074 if (gui.ascii_glyphs != NULL)
5075 pango_glyph_string_free(gui.ascii_glyphs);
5076 if (gui.ascii_font != NULL)
5077 g_object_unref(gui.ascii_font);
5078
5079 gui.ascii_glyphs = NULL;
5080 gui.ascii_font = NULL;
5081
Bram Moolenaar30613902019-12-01 22:11:18 +01005082 // For safety, fill in question marks for the control characters.
5083 // Put a space between characters to avoid shaping.
Bram Moolenaar16350cb2016-08-14 20:27:34 +02005084 for (i = 0; i < 128; ++i)
5085 {
5086 if (i >= 32 && i < 127)
5087 ascii_chars[2 * i] = i;
5088 else
5089 ascii_chars[2 * i] = '?';
5090 ascii_chars[2 * i + 1] = ' ';
5091 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005092
5093 attr_list = pango_attr_list_new();
5094 item_list = pango_itemize(gui.text_context, (const char *)ascii_chars,
5095 0, sizeof(ascii_chars), attr_list, NULL);
5096
Bram Moolenaar30613902019-12-01 22:11:18 +01005097 if (item_list != NULL && item_list->next == NULL) // play safe
Bram Moolenaar071d4272004-06-13 20:20:40 +00005098 {
5099 PangoItem *item;
5100 int width;
5101
5102 item = (PangoItem *)item_list->data;
5103 width = gui.char_width * PANGO_SCALE;
5104
Bram Moolenaar30613902019-12-01 22:11:18 +01005105 // Remember the shape engine used for ASCII.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005106 default_shape_engine = item->analysis.shape_engine;
5107
5108 gui.ascii_font = item->analysis.font;
5109 g_object_ref(gui.ascii_font);
5110
5111 gui.ascii_glyphs = pango_glyph_string_new();
5112
5113 pango_shape((const char *)ascii_chars, sizeof(ascii_chars),
5114 &item->analysis, gui.ascii_glyphs);
5115
5116 g_return_if_fail(gui.ascii_glyphs->num_glyphs == sizeof(ascii_chars));
5117
5118 for (i = 0; i < gui.ascii_glyphs->num_glyphs; ++i)
5119 {
5120 PangoGlyphGeometry *geom;
5121
5122 geom = &gui.ascii_glyphs->glyphs[i].geometry;
5123 geom->x_offset += MAX(0, width - geom->width) / 2;
5124 geom->width = width;
5125 }
5126 }
5127
Bram Moolenaar81a4cf42020-09-09 19:05:13 +02005128 // TODO: is this type cast OK?
5129 g_list_foreach(item_list, (GFunc)(void *)&pango_item_free, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005130 g_list_free(item_list);
5131 pango_attr_list_unref(attr_list);
5132}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005133
5134/*
5135 * Initialize Vim to use the font or fontset with the given name.
5136 * Return FAIL if the font could not be loaded, OK otherwise.
5137 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005138 int
Bram Moolenaarb85cb212009-05-17 14:24:23 +00005139gui_mch_init_font(char_u *font_name, int fontset UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005140{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005141 PangoFontDescription *font_desc;
5142 PangoLayout *layout;
5143 int width;
5144
Bram Moolenaar30613902019-12-01 22:11:18 +01005145 // If font_name is NULL, this means to use the default, which should
5146 // be present on all proper Pango/fontconfig installations.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005147 if (font_name == NULL)
5148 font_name = (char_u *)DEFAULT_FONT;
5149
5150 font_desc = gui_mch_get_font(font_name, FALSE);
5151
5152 if (font_desc == NULL)
5153 return FAIL;
5154
5155 gui_mch_free_font(gui.norm_font);
5156 gui.norm_font = font_desc;
5157
5158 pango_context_set_font_description(gui.text_context, font_desc);
5159
5160 layout = pango_layout_new(gui.text_context);
5161 pango_layout_set_text(layout, "MW", 2);
5162 pango_layout_get_size(layout, &width, NULL);
5163 /*
5164 * Set char_width to half the width obtained from pango_layout_get_size()
5165 * for CJK fixed_width/bi-width fonts. An unpatched version of Xft leads
5166 * Pango to use the same width for both non-CJK characters (e.g. Latin
5167 * letters and numbers) and CJK characters. This results in 's p a c e d
5168 * o u t' rendering when a CJK 'fixed width' font is used. To work around
5169 * that, divide the width returned by Pango by 2 if cjk_width is equal to
5170 * width for CJK fonts.
5171 *
5172 * For related bugs, see:
5173 * http://bugzilla.gnome.org/show_bug.cgi?id=106618
5174 * http://bugzilla.gnome.org/show_bug.cgi?id=106624
5175 *
5176 * With this, for all four of the following cases, Vim works fine:
5177 * guifont=CJK_fixed_width_font
5178 * guifont=Non_CJK_fixed_font
5179 * guifont=Non_CJK_fixed_font,CJK_Fixed_font
5180 * guifont=Non_CJK_fixed_font guifontwide=CJK_fixed_font
5181 */
5182 if (is_cjk_font(gui.norm_font))
5183 {
5184 int cjk_width;
5185
Bram Moolenaar30613902019-12-01 22:11:18 +01005186 // Measure the text extent of U+4E00 and U+4E8C
Bram Moolenaar071d4272004-06-13 20:20:40 +00005187 pango_layout_set_text(layout, "\344\270\200\344\272\214", -1);
5188 pango_layout_get_size(layout, &cjk_width, NULL);
5189
Bram Moolenaar30613902019-12-01 22:11:18 +01005190 if (width == cjk_width) // Xft not patched
Bram Moolenaar071d4272004-06-13 20:20:40 +00005191 width /= 2;
5192 }
5193 g_object_unref(layout);
5194
5195 gui.char_width = (width / 2 + PANGO_SCALE - 1) / PANGO_SCALE;
5196
Bram Moolenaar30613902019-12-01 22:11:18 +01005197 // A zero width may cause a crash. Happens for semi-invalid fontsets.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005198 if (gui.char_width <= 0)
5199 gui.char_width = 8;
5200
Bram Moolenaar231334e2005-07-25 20:46:57 +00005201 gui_mch_adjust_charheight();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005202
Bram Moolenaar30613902019-12-01 22:11:18 +01005203 // Set the fontname, which will be used for information purposes
Bram Moolenaar071d4272004-06-13 20:20:40 +00005204 hl_set_font_name(font_name);
5205
5206 get_styled_font_variants();
5207 ascii_glyph_table_init();
5208
Bram Moolenaar30613902019-12-01 22:11:18 +01005209 // Avoid unnecessary overhead if 'guifontwide' is equal to 'guifont'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005210 if (gui.wide_font != NULL
5211 && pango_font_description_equal(gui.norm_font, gui.wide_font))
5212 {
5213 pango_font_description_free(gui.wide_font);
5214 gui.wide_font = NULL;
5215 }
5216
Bram Moolenaare161c792009-11-03 17:13:59 +00005217 if (gui_mch_maximized())
5218 {
Bram Moolenaar30613902019-12-01 22:11:18 +01005219 // Update lines and columns in accordance with the new font, keep the
5220 // window maximized.
Bram Moolenaar12bc1b52011-08-10 17:44:45 +02005221 gui_mch_newfont();
Bram Moolenaare161c792009-11-03 17:13:59 +00005222 }
5223 else
Bram Moolenaare161c792009-11-03 17:13:59 +00005224 {
Bram Moolenaar30613902019-12-01 22:11:18 +01005225 // Preserve the logical dimensions of the screen.
Bram Moolenaare161c792009-11-03 17:13:59 +00005226 update_window_manager_hints(0, 0);
5227 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005228
5229 return OK;
5230}
5231
5232/*
5233 * Get a reference to the font "name".
5234 * Return zero for failure.
5235 */
5236 GuiFont
5237gui_mch_get_font(char_u *name, int report_error)
5238{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005239 PangoFontDescription *font;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005240
Bram Moolenaar30613902019-12-01 22:11:18 +01005241 // can't do this when GUI is not running
Bram Moolenaar071d4272004-06-13 20:20:40 +00005242 if (!gui.in_use || name == NULL)
5243 return NULL;
5244
Bram Moolenaar071d4272004-06-13 20:20:40 +00005245 if (output_conv.vc_type != CONV_NONE)
5246 {
5247 char_u *buf;
5248
5249 buf = string_convert(&output_conv, name, NULL);
5250 if (buf != NULL)
5251 {
5252 font = pango_font_description_from_string((const char *)buf);
5253 vim_free(buf);
5254 }
5255 else
5256 font = NULL;
5257 }
5258 else
5259 font = pango_font_description_from_string((const char *)name);
5260
5261 if (font != NULL)
5262 {
5263 PangoFont *real_font;
5264
Bram Moolenaar30613902019-12-01 22:11:18 +01005265 // pango_context_load_font() bails out if no font size is set
Bram Moolenaar071d4272004-06-13 20:20:40 +00005266 if (pango_font_description_get_size(font) <= 0)
5267 pango_font_description_set_size(font, 10 * PANGO_SCALE);
5268
5269 real_font = pango_context_load_font(gui.text_context, font);
5270
5271 if (real_font == NULL)
5272 {
5273 pango_font_description_free(font);
5274 font = NULL;
5275 }
5276 else
5277 g_object_unref(real_font);
5278 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005279
5280 if (font == NULL)
5281 {
5282 if (report_error)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00005283 semsg(_((char *)e_unknown_font_str), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005284 return NULL;
5285 }
5286
Bram Moolenaar071d4272004-06-13 20:20:40 +00005287 return font;
5288}
5289
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005290#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar46c9c732004-12-12 11:37:09 +00005291/*
5292 * Return the name of font "font" in allocated memory.
5293 */
Bram Moolenaar46c9c732004-12-12 11:37:09 +00005294 char_u *
Bram Moolenaarb85cb212009-05-17 14:24:23 +00005295gui_mch_get_fontname(GuiFont font, char_u *name UNUSED)
Bram Moolenaar46c9c732004-12-12 11:37:09 +00005296{
Bram Moolenaar46c9c732004-12-12 11:37:09 +00005297 if (font != NOFONT)
5298 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00005299 char *pangoname = pango_font_description_to_string(font);
Bram Moolenaar46c9c732004-12-12 11:37:09 +00005300
Bram Moolenaar89d40322006-08-29 15:30:07 +00005301 if (pangoname != NULL)
Bram Moolenaar46c9c732004-12-12 11:37:09 +00005302 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00005303 char_u *s = vim_strsave((char_u *)pangoname);
Bram Moolenaar46c9c732004-12-12 11:37:09 +00005304
Bram Moolenaar89d40322006-08-29 15:30:07 +00005305 g_free(pangoname);
Bram Moolenaar46c9c732004-12-12 11:37:09 +00005306 return s;
5307 }
5308 }
Bram Moolenaar46c9c732004-12-12 11:37:09 +00005309 return NULL;
5310}
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005311#endif
Bram Moolenaar46c9c732004-12-12 11:37:09 +00005312
Bram Moolenaar071d4272004-06-13 20:20:40 +00005313/*
5314 * If a font is not going to be used, free its structure.
5315 */
5316 void
5317gui_mch_free_font(GuiFont font)
5318{
5319 if (font != NOFONT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005320 pango_font_description_free(font);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005321}
5322
Bram Moolenaar071d4272004-06-13 20:20:40 +00005323/*
Yee Cheng Chin290b8872023-10-05 20:54:21 +02005324 * Cmdline expansion for setting 'guifont' / 'guifontwide'. Will enumerate
5325 * through all fonts for completion. When setting 'guifont' it will only show
5326 * monospace fonts as it's unlikely other fonts would be useful.
5327 */
5328 void
5329gui_mch_expand_font(optexpand_T *args, void *param, int (*add_match)(char_u *val))
5330{
5331 PangoFontFamily **font_families = NULL;
5332 int n_families = 0;
5333 int wide = *(int *)param;
5334
5335 if (args->oe_include_orig_val && *args->oe_opt_value == NUL && !wide)
5336 {
5337 // If guifont is empty, and we want to fill in the orig value, suggest
5338 // the default so the user can modify it.
5339 if (add_match((char_u *)DEFAULT_FONT) != OK)
5340 return;
5341 }
5342
5343 pango_context_list_families(
5344 gui.text_context,
5345 &font_families,
5346 &n_families);
5347
5348 for (int i = 0; i < n_families; i++)
5349 {
5350 if (!wide && !pango_font_family_is_monospace(font_families[i]))
5351 continue;
5352
5353 const char* fam_name = pango_font_family_get_name(font_families[i]);
5354 if (input_conv.vc_type != CONV_NONE)
5355 {
5356 char_u *buf = string_convert(&input_conv, (char_u*)fam_name, NULL);
5357 if (buf != NULL)
5358 {
5359 if (add_match(buf) != OK)
5360 {
5361 vim_free(buf);
5362 break;
5363 }
5364 vim_free(buf);
5365 }
5366 else
5367 break;
5368 }
5369 else
5370 {
5371 if (add_match((char_u *)fam_name) != OK)
5372 break;
5373 }
5374 }
5375
5376 g_free(font_families);
5377}
5378
5379/*
Bram Moolenaar36edf062016-07-21 22:10:12 +02005380 * Return the Pixel value (color) for the given color name.
5381 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00005382 * Return INVALCOLOR for error.
5383 */
5384 guicolor_T
5385gui_mch_get_color(char_u *name)
5386{
Bram Moolenaar2e324952018-04-14 14:37:07 +02005387 guicolor_T color = INVALCOLOR;
5388
Bram Moolenaar30613902019-12-01 22:11:18 +01005389 if (!gui.in_use) // can't do this when GUI not running
Bram Moolenaar2e324952018-04-14 14:37:07 +02005390 return color;
5391
5392 if (name != NULL)
5393 color = gui_get_color_cmn(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005394
Bram Moolenaar98921892016-02-23 17:14:37 +01005395#if GTK_CHECK_VERSION(3,0,0)
Bram Moolenaar2e324952018-04-14 14:37:07 +02005396 return color;
Bram Moolenaar98921892016-02-23 17:14:37 +01005397#else
Bram Moolenaar36edf062016-07-21 22:10:12 +02005398 if (color == INVALCOLOR)
5399 return INVALCOLOR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005400
Bram Moolenaar26af85d2017-07-23 16:45:10 +02005401 return gui_mch_get_rgb_color(
5402 (color & 0xff0000) >> 16,
5403 (color & 0xff00) >> 8,
5404 color & 0xff);
5405#endif
5406}
5407
5408/*
5409 * Return the Pixel value (color) for the given RGB values.
5410 * Return INVALCOLOR for error.
5411 */
5412 guicolor_T
5413gui_mch_get_rgb_color(int r, int g, int b)
5414{
5415#if GTK_CHECK_VERSION(3,0,0)
5416 return gui_get_rgb_color_cmn(r, g, b);
5417#else
5418 GdkColor gcolor;
5419 int ret;
5420
5421 gcolor.red = (guint16)(r / 255.0 * 65535 + 0.5);
5422 gcolor.green = (guint16)(g / 255.0 * 65535 + 0.5);
5423 gcolor.blue = (guint16)(b / 255.0 * 65535 + 0.5);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005424
Bram Moolenaar36edf062016-07-21 22:10:12 +02005425 ret = gdk_colormap_alloc_color(gtk_widget_get_colormap(gui.drawarea),
5426 &gcolor, FALSE, TRUE);
5427
5428 return ret != 0 ? (guicolor_T)gcolor.pixel : INVALCOLOR;
5429#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005430}
5431
5432/*
5433 * Set the current text foreground color.
5434 */
5435 void
5436gui_mch_set_fg_color(guicolor_T color)
5437{
Bram Moolenaar36edf062016-07-21 22:10:12 +02005438#if GTK_CHECK_VERSION(3,0,0)
5439 *gui.fgcolor = color_to_rgba(color);
5440#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005441 gui.fgcolor->pixel = (unsigned long)color;
Bram Moolenaar36edf062016-07-21 22:10:12 +02005442#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005443}
5444
5445/*
5446 * Set the current text background color.
5447 */
5448 void
5449gui_mch_set_bg_color(guicolor_T color)
5450{
Bram Moolenaar36edf062016-07-21 22:10:12 +02005451#if GTK_CHECK_VERSION(3,0,0)
5452 *gui.bgcolor = color_to_rgba(color);
5453#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005454 gui.bgcolor->pixel = (unsigned long)color;
Bram Moolenaar36edf062016-07-21 22:10:12 +02005455#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005456}
5457
Bram Moolenaarf36d3692005-03-15 22:48:14 +00005458/*
5459 * Set the current text special color.
5460 */
5461 void
5462gui_mch_set_sp_color(guicolor_T color)
5463{
Bram Moolenaar36edf062016-07-21 22:10:12 +02005464#if GTK_CHECK_VERSION(3,0,0)
5465 *gui.spcolor = color_to_rgba(color);
5466#else
Bram Moolenaarf36d3692005-03-15 22:48:14 +00005467 gui.spcolor->pixel = (unsigned long)color;
Bram Moolenaar36edf062016-07-21 22:10:12 +02005468#endif
Bram Moolenaarf36d3692005-03-15 22:48:14 +00005469}
5470
Bram Moolenaar071d4272004-06-13 20:20:40 +00005471/*
5472 * Function-like convenience macro for the sake of efficiency.
5473 */
5474#define INSERT_PANGO_ATTR(Attribute, AttrList, Start, End) \
5475 G_STMT_START{ \
5476 PangoAttribute *tmp_attr_; \
5477 tmp_attr_ = (Attribute); \
5478 tmp_attr_->start_index = (Start); \
5479 tmp_attr_->end_index = (End); \
5480 pango_attr_list_insert((AttrList), tmp_attr_); \
5481 }G_STMT_END
5482
5483 static void
5484apply_wide_font_attr(char_u *s, int len, PangoAttrList *attr_list)
5485{
5486 char_u *start = NULL;
5487 char_u *p;
5488 int uc;
5489
5490 for (p = s; p < s + len; p += utf_byte2len(*p))
5491 {
5492 uc = utf_ptr2char(p);
5493
5494 if (start == NULL)
5495 {
5496 if (uc >= 0x80 && utf_char2cells(uc) == 2)
5497 start = p;
5498 }
Bram Moolenaar30613902019-12-01 22:11:18 +01005499 else if (uc < 0x80 // optimization shortcut
Bram Moolenaar071d4272004-06-13 20:20:40 +00005500 || (utf_char2cells(uc) != 2 && !utf_iscomposing(uc)))
5501 {
5502 INSERT_PANGO_ATTR(pango_attr_font_desc_new(gui.wide_font),
5503 attr_list, start - s, p - s);
5504 start = NULL;
5505 }
5506 }
5507
5508 if (start != NULL)
5509 INSERT_PANGO_ATTR(pango_attr_font_desc_new(gui.wide_font),
5510 attr_list, start - s, len);
5511}
5512
5513 static int
5514count_cluster_cells(char_u *s, PangoItem *item,
5515 PangoGlyphString* glyphs, int i,
5516 int *cluster_width,
5517 int *last_glyph_rbearing)
5518{
5519 char_u *p;
Bram Moolenaar30613902019-12-01 22:11:18 +01005520 int next; // glyph start index of next cluster
5521 int start, end; // string segment of current cluster
5522 int width; // real cluster width in Pango units
Bram Moolenaar071d4272004-06-13 20:20:40 +00005523 int uc;
5524 int cellcount = 0;
5525
5526 width = glyphs->glyphs[i].geometry.width;
5527
5528 for (next = i + 1; next < glyphs->num_glyphs; ++next)
5529 {
5530 if (glyphs->glyphs[next].attr.is_cluster_start)
5531 break;
5532 else if (glyphs->glyphs[next].geometry.width > width)
5533 width = glyphs->glyphs[next].geometry.width;
5534 }
5535
5536 start = item->offset + glyphs->log_clusters[i];
5537 end = item->offset + ((next < glyphs->num_glyphs) ?
5538 glyphs->log_clusters[next] : item->length);
5539
5540 for (p = s + start; p < s + end; p += utf_byte2len(*p))
5541 {
5542 uc = utf_ptr2char(p);
5543 if (uc < 0x80)
5544 ++cellcount;
5545 else if (!utf_iscomposing(uc))
5546 cellcount += utf_char2cells(uc);
5547 }
5548
5549 if (last_glyph_rbearing != NULL
5550 && cellcount > 0 && next == glyphs->num_glyphs)
5551 {
5552 PangoRectangle ink_rect;
5553 /*
5554 * If a certain combining mark had to be taken from a non-monospace
5555 * font, we have to compensate manually by adapting x_offset according
5556 * to the ink extents of the previous glyph.
5557 */
5558 pango_font_get_glyph_extents(item->analysis.font,
5559 glyphs->glyphs[i].glyph,
5560 &ink_rect, NULL);
5561
5562 if (PANGO_RBEARING(ink_rect) > 0)
5563 *last_glyph_rbearing = PANGO_RBEARING(ink_rect);
5564 }
5565
5566 if (cellcount > 0)
5567 *cluster_width = width;
5568
5569 return cellcount;
5570}
5571
5572/*
5573 * If there are only combining characters in the cluster, we cannot just
5574 * change the width of the previous glyph since there is none. Therefore
5575 * some guesswork is needed.
5576 *
5577 * If ink_rect.x is negative Pango apparently has taken care of the composing
5578 * by itself. Actually setting x_offset = 0 should be sufficient then, but due
5579 * to problems with composing from different fonts we still need to fine-tune
Bram Moolenaar64404472010-06-26 06:24:45 +02005580 * x_offset to avoid ugliness.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005581 *
5582 * If ink_rect.x is not negative, force overstriking by pointing x_offset to
5583 * the position of the previous glyph. Apparently this happens only with old
5584 * X fonts which don't provide the special combining information needed by
5585 * Pango.
5586 */
5587 static void
5588setup_zero_width_cluster(PangoItem *item, PangoGlyphInfo *glyph,
5589 int last_cellcount, int last_cluster_width,
5590 int last_glyph_rbearing)
5591{
5592 PangoRectangle ink_rect;
5593 PangoRectangle logical_rect;
5594 int width;
5595
5596 width = last_cellcount * gui.char_width * PANGO_SCALE;
5597 glyph->geometry.x_offset = -width + MAX(0, width - last_cluster_width) / 2;
5598 glyph->geometry.width = 0;
5599
5600 pango_font_get_glyph_extents(item->analysis.font,
5601 glyph->glyph,
5602 &ink_rect, &logical_rect);
5603 if (ink_rect.x < 0)
5604 {
5605 glyph->geometry.x_offset += last_glyph_rbearing;
5606 glyph->geometry.y_offset = logical_rect.height
5607 - (gui.char_height - p_linespace) * PANGO_SCALE;
5608 }
Bram Moolenaar706e84b2010-08-07 15:46:45 +02005609 else
Bram Moolenaar30613902019-12-01 22:11:18 +01005610 // If the accent width is smaller than the cluster width, position it
5611 // in the middle.
Bram Moolenaar706e84b2010-08-07 15:46:45 +02005612 glyph->geometry.x_offset = -width + MAX(0, width - ink_rect.width) / 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005613}
5614
Bram Moolenaar98921892016-02-23 17:14:37 +01005615#if GTK_CHECK_VERSION(3,0,0)
5616 static void
5617draw_glyph_string(int row, int col, int num_cells, int flags,
5618 PangoFont *font, PangoGlyphString *glyphs,
5619 cairo_t *cr)
5620#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005621 static void
5622draw_glyph_string(int row, int col, int num_cells, int flags,
5623 PangoFont *font, PangoGlyphString *glyphs)
Bram Moolenaar98921892016-02-23 17:14:37 +01005624#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005625{
5626 if (!(flags & DRAW_TRANSP))
5627 {
Bram Moolenaar98921892016-02-23 17:14:37 +01005628#if GTK_CHECK_VERSION(3,0,0)
Bram Moolenaar36edf062016-07-21 22:10:12 +02005629 cairo_set_source_rgba(cr,
5630 gui.bgcolor->red, gui.bgcolor->green, gui.bgcolor->blue,
5631 gui.bgcolor->alpha);
Bram Moolenaar98921892016-02-23 17:14:37 +01005632 cairo_rectangle(cr,
5633 FILL_X(col), FILL_Y(row),
5634 num_cells * gui.char_width, gui.char_height);
5635 cairo_fill(cr);
5636#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005637 gdk_gc_set_foreground(gui.text_gc, gui.bgcolor);
5638
5639 gdk_draw_rectangle(gui.drawarea->window,
5640 gui.text_gc,
5641 TRUE,
5642 FILL_X(col),
5643 FILL_Y(row),
5644 num_cells * gui.char_width,
5645 gui.char_height);
Bram Moolenaar98921892016-02-23 17:14:37 +01005646#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005647 }
5648
Bram Moolenaar98921892016-02-23 17:14:37 +01005649#if GTK_CHECK_VERSION(3,0,0)
Bram Moolenaar36edf062016-07-21 22:10:12 +02005650 cairo_set_source_rgba(cr,
5651 gui.fgcolor->red, gui.fgcolor->green, gui.fgcolor->blue,
5652 gui.fgcolor->alpha);
Bram Moolenaar98921892016-02-23 17:14:37 +01005653 cairo_move_to(cr, TEXT_X(col), TEXT_Y(row));
5654 pango_cairo_show_glyph_string(cr, font, glyphs);
5655#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005656 gdk_gc_set_foreground(gui.text_gc, gui.fgcolor);
5657
5658 gdk_draw_glyphs(gui.drawarea->window,
5659 gui.text_gc,
5660 font,
5661 TEXT_X(col),
5662 TEXT_Y(row),
5663 glyphs);
Bram Moolenaar98921892016-02-23 17:14:37 +01005664#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005665
Bram Moolenaar30613902019-12-01 22:11:18 +01005666 // redraw the contents with an offset of 1 to emulate bold
Bram Moolenaar071d4272004-06-13 20:20:40 +00005667 if ((flags & DRAW_BOLD) && !gui.font_can_bold)
Bram Moolenaar98921892016-02-23 17:14:37 +01005668#if GTK_CHECK_VERSION(3,0,0)
5669 {
Bram Moolenaar36edf062016-07-21 22:10:12 +02005670 cairo_set_source_rgba(cr,
5671 gui.fgcolor->red, gui.fgcolor->green, gui.fgcolor->blue,
5672 gui.fgcolor->alpha);
Bram Moolenaar98921892016-02-23 17:14:37 +01005673 cairo_move_to(cr, TEXT_X(col) + 1, TEXT_Y(row));
5674 pango_cairo_show_glyph_string(cr, font, glyphs);
5675 }
5676#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005677 gdk_draw_glyphs(gui.drawarea->window,
5678 gui.text_gc,
5679 font,
5680 TEXT_X(col) + 1,
5681 TEXT_Y(row),
5682 glyphs);
Bram Moolenaar98921892016-02-23 17:14:37 +01005683#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005684}
5685
Bram Moolenaarf36d3692005-03-15 22:48:14 +00005686/*
5687 * Draw underline and undercurl at the bottom of the character cell.
5688 */
Bram Moolenaar98921892016-02-23 17:14:37 +01005689#if GTK_CHECK_VERSION(3,0,0)
5690 static void
5691draw_under(int flags, int row, int col, int cells, cairo_t *cr)
5692#else
Bram Moolenaarf36d3692005-03-15 22:48:14 +00005693 static void
5694draw_under(int flags, int row, int col, int cells)
Bram Moolenaar98921892016-02-23 17:14:37 +01005695#endif
Bram Moolenaarf36d3692005-03-15 22:48:14 +00005696{
5697 int i;
5698 int offset;
Bram Moolenaarb85cb212009-05-17 14:24:23 +00005699 static const int val[8] = {1, 0, 0, 0, 1, 2, 2, 2 };
Bram Moolenaarf36d3692005-03-15 22:48:14 +00005700 int y = FILL_Y(row + 1) - 1;
5701
Bram Moolenaar30613902019-12-01 22:11:18 +01005702 // Undercurl: draw curl at the bottom of the character cell.
Bram Moolenaarf36d3692005-03-15 22:48:14 +00005703 if (flags & DRAW_UNDERC)
5704 {
Bram Moolenaar98921892016-02-23 17:14:37 +01005705#if GTK_CHECK_VERSION(3,0,0)
5706 cairo_set_line_width(cr, 1.0);
5707 cairo_set_line_cap(cr, CAIRO_LINE_CAP_BUTT);
Bram Moolenaar36edf062016-07-21 22:10:12 +02005708 cairo_set_source_rgba(cr,
5709 gui.spcolor->red, gui.spcolor->green, gui.spcolor->blue,
5710 gui.spcolor->alpha);
Yamagi9cd93852021-11-20 20:42:29 +00005711 cairo_move_to(cr, FILL_X(col) + 1, y - 2 + 0.5);
5712 for (i = FILL_X(col) + 1; i < FILL_X(col + cells); ++i)
Bram Moolenaar98921892016-02-23 17:14:37 +01005713 {
5714 offset = val[i % 8];
5715 cairo_line_to(cr, i, y - offset + 0.5);
5716 }
5717 cairo_stroke(cr);
5718#else
Bram Moolenaarf36d3692005-03-15 22:48:14 +00005719 gdk_gc_set_foreground(gui.text_gc, gui.spcolor);
5720 for (i = FILL_X(col); i < FILL_X(col + cells); ++i)
5721 {
5722 offset = val[i % 8];
5723 gdk_draw_point(gui.drawarea->window, gui.text_gc, i, y - offset);
5724 }
5725 gdk_gc_set_foreground(gui.text_gc, gui.fgcolor);
Bram Moolenaar98921892016-02-23 17:14:37 +01005726#endif
Bram Moolenaarf36d3692005-03-15 22:48:14 +00005727 }
5728
Bram Moolenaar30613902019-12-01 22:11:18 +01005729 // Draw a strikethrough line
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02005730 if (flags & DRAW_STRIKE)
5731 {
5732#if GTK_CHECK_VERSION(3,0,0)
5733 cairo_set_line_width(cr, 1.0);
5734 cairo_set_line_cap(cr, CAIRO_LINE_CAP_BUTT);
5735 cairo_set_source_rgba(cr,
5736 gui.spcolor->red, gui.spcolor->green, gui.spcolor->blue,
5737 gui.spcolor->alpha);
5738 cairo_move_to(cr, FILL_X(col), y + 1 - gui.char_height/2 + 0.5);
5739 cairo_line_to(cr, FILL_X(col + cells), y + 1 - gui.char_height/2 + 0.5);
5740 cairo_stroke(cr);
5741#else
5742 gdk_gc_set_foreground(gui.text_gc, gui.spcolor);
5743 gdk_draw_line(gui.drawarea->window, gui.text_gc,
5744 FILL_X(col), y + 1 - gui.char_height/2,
5745 FILL_X(col + cells), y + 1 - gui.char_height/2);
5746 gdk_gc_set_foreground(gui.text_gc, gui.fgcolor);
5747#endif
5748 }
5749
Bram Moolenaar30613902019-12-01 22:11:18 +01005750 // Underline: draw a line at the bottom of the character cell.
Bram Moolenaarf36d3692005-03-15 22:48:14 +00005751 if (flags & DRAW_UNDERL)
5752 {
Bram Moolenaar30613902019-12-01 22:11:18 +01005753 // When p_linespace is 0, overwrite the bottom row of pixels.
5754 // Otherwise put the line just below the character.
Bram Moolenaarf36d3692005-03-15 22:48:14 +00005755 if (p_linespace > 1)
5756 y -= p_linespace - 1;
Bram Moolenaar98921892016-02-23 17:14:37 +01005757#if GTK_CHECK_VERSION(3,0,0)
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02005758 cairo_set_line_width(cr, 1.0);
5759 cairo_set_line_cap(cr, CAIRO_LINE_CAP_BUTT);
5760 cairo_set_source_rgba(cr,
5761 gui.fgcolor->red, gui.fgcolor->green, gui.fgcolor->blue,
5762 gui.fgcolor->alpha);
5763 cairo_move_to(cr, FILL_X(col), y + 0.5);
5764 cairo_line_to(cr, FILL_X(col + cells), y + 0.5);
5765 cairo_stroke(cr);
Bram Moolenaar98921892016-02-23 17:14:37 +01005766#else
Bram Moolenaarf36d3692005-03-15 22:48:14 +00005767 gdk_draw_line(gui.drawarea->window, gui.text_gc,
5768 FILL_X(col), y,
5769 FILL_X(col + cells) - 1, y);
Bram Moolenaar98921892016-02-23 17:14:37 +01005770#endif
Bram Moolenaarf36d3692005-03-15 22:48:14 +00005771 }
5772}
5773
Bram Moolenaar071d4272004-06-13 20:20:40 +00005774 int
5775gui_gtk2_draw_string(int row, int col, char_u *s, int len, int flags)
5776{
Dusan Popovic4eeedc02021-10-16 20:52:05 +01005777 char_u *conv_buf = NULL; // result of UTF-8 conversion
5778 char_u *new_conv_buf;
5779 int convlen;
5780 char_u *sp, *bp;
5781 int plen;
5782 int len_sum; // return value needs to add up since we are
5783 // printing substrings
5784 int byte_sum; // byte position in string
5785 char_u *cs; // current *s pointer
5786 int needs_pango; // look ahead, 0=ascii 1=unicode/ligatures
Bram Moolenaar9d4b8ca2021-10-17 11:33:47 +01005787 int should_need_pango = FALSE;
Dusan Popovic4eeedc02021-10-16 20:52:05 +01005788 int slen;
5789 int is_ligature;
Dusan Popovic4eeedc02021-10-16 20:52:05 +01005790 int is_utf8;
5791 char_u backup_ch;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005792
Bram Moolenaar98921892016-02-23 17:14:37 +01005793 if (gui.text_context == NULL || gtk_widget_get_window(gui.drawarea) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005794 return len;
5795
5796 if (output_conv.vc_type != CONV_NONE)
5797 {
5798 /*
5799 * Convert characters from 'encoding' to 'termencoding', which is set
5800 * to UTF-8 by gui_mch_init(). did_set_string_option() in option.c
5801 * prohibits changing this to something else than UTF-8 if the GUI is
5802 * in use.
5803 */
5804 convlen = len;
5805 conv_buf = string_convert(&output_conv, s, &convlen);
5806 g_return_val_if_fail(conv_buf != NULL, len);
5807
Bram Moolenaar30613902019-12-01 22:11:18 +01005808 // Correct for differences in char width: some chars are
5809 // double-wide in 'encoding' but single-wide in utf-8. Add a space to
5810 // compensate for that.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005811 for (sp = s, bp = conv_buf; sp < s + len && bp < conv_buf + convlen; )
5812 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005813 plen = utf_ptr2len(bp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005814 if ((*mb_ptr2cells)(sp) == 2 && utf_ptr2cells(bp) == 1)
5815 {
5816 new_conv_buf = alloc(convlen + 2);
5817 if (new_conv_buf == NULL)
5818 return len;
5819 plen += bp - conv_buf;
5820 mch_memmove(new_conv_buf, conv_buf, plen);
5821 new_conv_buf[plen] = ' ';
5822 mch_memmove(new_conv_buf + plen + 1, conv_buf + plen,
5823 convlen - plen + 1);
5824 vim_free(conv_buf);
5825 conv_buf = new_conv_buf;
5826 ++convlen;
5827 bp = conv_buf + plen;
5828 plen = 1;
5829 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005830 sp += (*mb_ptr2len)(sp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005831 bp += plen;
5832 }
5833 s = conv_buf;
5834 len = convlen;
5835 }
5836
5837 /*
Dusan Popovic4eeedc02021-10-16 20:52:05 +01005838 * Ligature support and complex utf-8 char optimization:
5839 * String received to output to screen can print using pre-cached glyphs
5840 * (fast) or Pango (slow). Ligatures and multibype utf-8 must use Pango.
5841 * Since we receive mixed content string, split it into logical segments
dundargocc57b5bc2022-11-02 13:30:51 +00005842 * that are guaranteed to go through glyphs as much as possible. Since
Dusan Popovic4eeedc02021-10-16 20:52:05 +01005843 * single ligature char prints as ascii, print it that way.
5844 */
5845 len_sum = 0; // return value needs to add up since we are printing
5846 // substrings
5847 byte_sum = 0;
5848 cs = s;
Dusan Popovic3c19b502021-11-20 22:03:30 +00005849 // First char decides starting needs_pango mode, 0=ascii 1=utf8/ligatures.
5850 // Even if it is ligature char, two chars or more make ligature.
dundargocc57b5bc2022-11-02 13:30:51 +00005851 // Ascii followed by utf8 is also going through pango.
Dusan Popovic3c19b502021-11-20 22:03:30 +00005852 is_utf8 = (*cs & 0x80);
5853 is_ligature = gui.ligatures_map[*cs] && (len > 1);
5854 if (is_ligature)
5855 is_ligature = gui.ligatures_map[*(cs + 1)];
5856 if (!is_utf8 && len > 1)
5857 is_utf8 = (*(cs + 1) & 0x80) != 0;
5858 needs_pango = is_utf8 || is_ligature;
Dusan Popovic4eeedc02021-10-16 20:52:05 +01005859
5860 // split string into ascii and non-ascii (ligatures + utf-8) substrings,
5861 // print glyphs or use Pango
5862 while (cs < s + len)
5863 {
5864 slen = 0;
5865 while (slen < (len - byte_sum))
5866 {
5867 is_ligature = gui.ligatures_map[*(cs + slen)];
5868 // look ahead, single ligature char between ascii is ascii
5869 if (is_ligature && !needs_pango)
5870 {
5871 if ((slen + 1) < (len - byte_sum))
Dusan Popovic3c19b502021-11-20 22:03:30 +00005872 is_ligature = gui.ligatures_map[*(cs + slen + 1)];
Dusan Popovic4eeedc02021-10-16 20:52:05 +01005873 else
Dusan Popovic4eeedc02021-10-16 20:52:05 +01005874 is_ligature = 0;
Dusan Popovic4eeedc02021-10-16 20:52:05 +01005875 }
5876 is_utf8 = *(cs + slen) & 0x80;
Dusan Popovic3c19b502021-11-20 22:03:30 +00005877 // ascii followed by utf8 could be combining
dundargocc57b5bc2022-11-02 13:30:51 +00005878 // if so send it through pango
Dusan Popovic3c19b502021-11-20 22:03:30 +00005879 if ((!is_utf8) && ((slen + 1) < (len - byte_sum)))
5880 is_utf8 = (*(cs + slen + 1) & 0x80);
Dusan Popovic4eeedc02021-10-16 20:52:05 +01005881 should_need_pango = (is_ligature || is_utf8);
5882 if (needs_pango != should_need_pango) // mode switch
5883 break;
5884 if (needs_pango)
5885 {
5886 if (is_ligature)
5887 {
5888 slen++; // ligature char by char
5889 }
Bram Moolenaar2c236702021-11-21 11:15:49 +00005890 else // is_utf8
Dusan Popovic4eeedc02021-10-16 20:52:05 +01005891 {
5892 if ((*(cs + slen) & 0xC0) == 0x80)
5893 {
5894 // a continuation, find next 0xC0 != 0x80 but don't
5895 // include it
5896 while ((slen < (len - byte_sum))
5897 && ((*(cs + slen) & 0xC0) == 0x80))
5898 {
5899 slen++;
5900 }
5901 }
5902 else if ((*(cs + slen) & 0xE0) == 0xC0)
5903 {
5904 // + one byte utf8
5905 slen++;
5906 }
5907 else if ((*(cs + slen) & 0xF0) == 0xE0)
5908 {
5909 // + two bytes utf8
5910 slen += 2;
5911 }
5912 else if ((*(cs + slen) & 0xF8) == 0xF0)
5913 {
5914 // + three bytes utf8
5915 slen += 3;
5916 }
5917 else
5918 {
5919 // this should not happen, try moving forward, Pango
5920 // will catch it
5921 slen++;
5922 }
5923 }
5924 }
5925 else
5926 {
5927 slen++; // ascii
5928 }
5929 }
Bram Moolenaar9d4b8ca2021-10-17 11:33:47 +01005930
Bram Moolenaard68a0042021-10-20 23:08:11 +01005931 if (slen < len)
5932 {
5933 // temporarily zero terminate substring, print, restore char, wrap
5934 backup_ch = *(cs + slen);
5935 *(cs + slen) = NUL;
5936 }
Dusan Popovic4eeedc02021-10-16 20:52:05 +01005937 len_sum += gui_gtk2_draw_string_ext(row, col + len_sum,
5938 cs, slen, flags, needs_pango);
Bram Moolenaard68a0042021-10-20 23:08:11 +01005939 if (slen < len)
5940 *(cs + slen) = backup_ch;
Dusan Popovic4eeedc02021-10-16 20:52:05 +01005941 cs += slen;
5942 byte_sum += slen;
5943 needs_pango = should_need_pango;
5944 }
5945 vim_free(conv_buf);
5946 return len_sum;
5947}
5948
5949 int
5950gui_gtk2_draw_string_ext(
5951 int row,
5952 int col,
5953 char_u *s,
5954 int len,
5955 int flags,
5956 int force_pango)
5957{
5958 GdkRectangle area; // area for clip mask
5959 PangoGlyphString *glyphs; // glyphs of current item
5960 int column_offset = 0; // column offset in cells
5961 int i;
5962#if GTK_CHECK_VERSION(3,0,0)
5963 cairo_t *cr;
5964#endif
5965
5966 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005967 * Restrict all drawing to the current screen line in order to prevent
5968 * fuzzy font lookups from messing up the screen.
5969 */
5970 area.x = gui.border_offset;
5971 area.y = FILL_Y(row);
5972 area.width = gui.num_cols * gui.char_width;
5973 area.height = gui.char_height;
5974
Bram Moolenaar98921892016-02-23 17:14:37 +01005975#if GTK_CHECK_VERSION(3,0,0)
5976 cr = cairo_create(gui.surface);
5977 cairo_rectangle(cr, area.x, area.y, area.width, area.height);
5978 cairo_clip(cr);
5979#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005980 gdk_gc_set_clip_origin(gui.text_gc, 0, 0);
5981 gdk_gc_set_clip_rectangle(gui.text_gc, &area);
Bram Moolenaar98921892016-02-23 17:14:37 +01005982#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005983
5984 glyphs = pango_glyph_string_new();
5985
5986 /*
5987 * Optimization hack: If possible, skip the itemize and shaping process
5988 * for pure ASCII strings. This optimization is particularly effective
5989 * because Vim draws space characters to clear parts of the screen.
5990 */
5991 if (!(flags & DRAW_ITALIC)
5992 && !((flags & DRAW_BOLD) && gui.font_can_bold)
Dusan Popovic4eeedc02021-10-16 20:52:05 +01005993 && gui.ascii_glyphs != NULL
5994 && !force_pango)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005995 {
5996 char_u *p;
5997
5998 for (p = s; p < s + len; ++p)
5999 if (*p & 0x80)
6000 goto not_ascii;
6001
6002 pango_glyph_string_set_size(glyphs, len);
6003
6004 for (i = 0; i < len; ++i)
6005 {
Bram Moolenaar16350cb2016-08-14 20:27:34 +02006006 glyphs->glyphs[i] = gui.ascii_glyphs->glyphs[2 * s[i]];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006007 glyphs->log_clusters[i] = i;
6008 }
6009
Bram Moolenaar98921892016-02-23 17:14:37 +01006010#if GTK_CHECK_VERSION(3,0,0)
6011 draw_glyph_string(row, col, len, flags, gui.ascii_font, glyphs, cr);
6012#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006013 draw_glyph_string(row, col, len, flags, gui.ascii_font, glyphs);
Bram Moolenaar98921892016-02-23 17:14:37 +01006014#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006015
6016 column_offset = len;
6017 }
6018 else
6019not_ascii:
6020 {
6021 PangoAttrList *attr_list;
6022 GList *item_list;
6023 int cluster_width;
6024 int last_glyph_rbearing;
Bram Moolenaar30613902019-12-01 22:11:18 +01006025 int cells = 0; // cells occupied by current cluster
Bram Moolenaar071d4272004-06-13 20:20:40 +00006026
Bram Moolenaar30613902019-12-01 22:11:18 +01006027 // Safety check: pango crashes when invoked with invalid utf-8
6028 // characters.
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006029 if (!utf_valid_string(s, s + len))
6030 {
6031 column_offset = len;
6032 goto skipitall;
6033 }
6034
Bram Moolenaar30613902019-12-01 22:11:18 +01006035 // original width of the current cluster
Bram Moolenaar071d4272004-06-13 20:20:40 +00006036 cluster_width = PANGO_SCALE * gui.char_width;
6037
Bram Moolenaar30613902019-12-01 22:11:18 +01006038 // right bearing of the last non-composing glyph
Bram Moolenaar071d4272004-06-13 20:20:40 +00006039 last_glyph_rbearing = PANGO_SCALE * gui.char_width;
6040
6041 attr_list = pango_attr_list_new();
6042
Bram Moolenaar30613902019-12-01 22:11:18 +01006043 // If 'guifontwide' is set then use that for double-width characters.
6044 // Otherwise just go with 'guifont' and let Pango do its thing.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006045 if (gui.wide_font != NULL)
6046 apply_wide_font_attr(s, len, attr_list);
6047
6048 if ((flags & DRAW_BOLD) && gui.font_can_bold)
6049 INSERT_PANGO_ATTR(pango_attr_weight_new(PANGO_WEIGHT_BOLD),
6050 attr_list, 0, len);
6051 if (flags & DRAW_ITALIC)
6052 INSERT_PANGO_ATTR(pango_attr_style_new(PANGO_STYLE_ITALIC),
6053 attr_list, 0, len);
6054 /*
6055 * Break the text into segments with consistent directional level
6056 * and shaping engine. Pure Latin text needs only a single segment,
6057 * so there's no need to worry about the loop's efficiency. Better
6058 * try to optimize elsewhere, e.g. reducing exposes and stuff :)
6059 */
6060 item_list = pango_itemize(gui.text_context,
6061 (const char *)s, 0, len, attr_list, NULL);
6062
6063 while (item_list != NULL)
6064 {
6065 PangoItem *item;
Bram Moolenaar30613902019-12-01 22:11:18 +01006066 int item_cells = 0; // item length in cells
Bram Moolenaar071d4272004-06-13 20:20:40 +00006067
6068 item = (PangoItem *)item_list->data;
6069 item_list = g_list_delete_link(item_list, item_list);
6070 /*
6071 * Increment the bidirectional embedding level by 1 if it is not
6072 * even. An odd number means the output will be RTL, but we don't
6073 * want that since Vim handles right-to-left text on its own. It
6074 * would probably be sufficient to just set level = 0, but you can
6075 * never know :)
6076 *
6077 * Unfortunately we can't take advantage of Pango's ability to
6078 * render both LTR and RTL at the same time. In order to support
6079 * that, Vim's main screen engine would have to make use of Pango
6080 * functionality.
6081 */
6082 item->analysis.level = (item->analysis.level + 1) & (~1U);
6083
Bram Moolenaar30613902019-12-01 22:11:18 +01006084 // HACK: Overrule the shape engine, we don't want shaping to be
6085 // done, because drawing the cursor would change the display.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006086 item->analysis.shape_engine = default_shape_engine;
6087
Bram Moolenaar3cbe0c02015-09-08 20:00:22 +02006088#ifdef HAVE_PANGO_SHAPE_FULL
Bram Moolenaar7e2ec002015-09-08 16:31:06 +02006089 pango_shape_full((const char *)s + item->offset, item->length,
6090 (const char *)s, len, &item->analysis, glyphs);
Bram Moolenaar3cbe0c02015-09-08 20:00:22 +02006091#else
6092 pango_shape((const char *)s + item->offset, item->length,
6093 &item->analysis, glyphs);
6094#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006095 /*
6096 * Fixed-width hack: iterate over the array and assign a fixed
6097 * width to each glyph, thus overriding the choice made by the
6098 * shaping engine. We use utf_char2cells() to determine the
6099 * number of cells needed.
6100 *
6101 * Also perform all kind of dark magic to get composing
6102 * characters right (and pretty too of course).
6103 */
6104 for (i = 0; i < glyphs->num_glyphs; ++i)
6105 {
6106 PangoGlyphInfo *glyph;
6107
6108 glyph = &glyphs->glyphs[i];
6109
6110 if (glyph->attr.is_cluster_start)
6111 {
6112 int cellcount;
6113
6114 cellcount = count_cluster_cells(
6115 s, item, glyphs, i, &cluster_width,
6116 (item_list != NULL) ? &last_glyph_rbearing : NULL);
6117
6118 if (cellcount > 0)
6119 {
6120 int width;
6121
6122 width = cellcount * gui.char_width * PANGO_SCALE;
6123 glyph->geometry.x_offset +=
6124 MAX(0, width - cluster_width) / 2;
6125 glyph->geometry.width = width;
6126 }
6127 else
6128 {
Bram Moolenaar30613902019-12-01 22:11:18 +01006129 // If there are only combining characters in the
6130 // cluster, we cannot just change the width of the
6131 // previous glyph since there is none. Therefore
6132 // some guesswork is needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006133 setup_zero_width_cluster(item, glyph, cells,
6134 cluster_width,
6135 last_glyph_rbearing);
6136 }
6137
6138 item_cells += cellcount;
6139 cells = cellcount;
6140 }
6141 else if (i > 0)
6142 {
6143 int width;
6144
Bram Moolenaar30613902019-12-01 22:11:18 +01006145 // There is a previous glyph, so we deal with combining
6146 // characters the canonical way.
6147 // In some circumstances Pango uses a positive x_offset,
6148 // then use the width of the previous glyph for this one
6149 // and set the previous width to zero.
6150 // Otherwise we get a negative x_offset, Pango has already
6151 // positioned the combining char, keep the widths as they
6152 // are.
6153 // For both adjust the x_offset to position the glyph in
6154 // the middle.
Bram Moolenaar706e84b2010-08-07 15:46:45 +02006155 if (glyph->geometry.x_offset >= 0)
Bram Moolenaar96118f32010-08-08 14:40:37 +02006156 {
6157 glyphs->glyphs[i].geometry.width =
6158 glyphs->glyphs[i - 1].geometry.width;
Bram Moolenaar706e84b2010-08-07 15:46:45 +02006159 glyphs->glyphs[i - 1].geometry.width = 0;
Bram Moolenaar96118f32010-08-08 14:40:37 +02006160 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006161 width = cells * gui.char_width * PANGO_SCALE;
6162 glyph->geometry.x_offset +=
6163 MAX(0, width - cluster_width) / 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006164 }
Bram Moolenaar30613902019-12-01 22:11:18 +01006165 else // i == 0 "cannot happen"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006166 {
6167 glyph->geometry.width = 0;
6168 }
6169 }
6170
Bram Moolenaar30613902019-12-01 22:11:18 +01006171 //// Aaaaand action! **
Bram Moolenaar98921892016-02-23 17:14:37 +01006172#if GTK_CHECK_VERSION(3,0,0)
6173 draw_glyph_string(row, col + column_offset, item_cells,
6174 flags, item->analysis.font, glyphs,
6175 cr);
6176#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006177 draw_glyph_string(row, col + column_offset, item_cells,
6178 flags, item->analysis.font, glyphs);
Bram Moolenaar98921892016-02-23 17:14:37 +01006179#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006180
6181 pango_item_free(item);
6182
6183 column_offset += item_cells;
6184 }
6185
6186 pango_attr_list_unref(attr_list);
6187 }
6188
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006189skipitall:
Bram Moolenaar30613902019-12-01 22:11:18 +01006190 // Draw underline and undercurl.
Bram Moolenaar98921892016-02-23 17:14:37 +01006191#if GTK_CHECK_VERSION(3,0,0)
6192 draw_under(flags, row, col, column_offset, cr);
6193#else
Bram Moolenaarf36d3692005-03-15 22:48:14 +00006194 draw_under(flags, row, col, column_offset);
Bram Moolenaar98921892016-02-23 17:14:37 +01006195#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006196
6197 pango_glyph_string_free(glyphs);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006198
Bram Moolenaar98921892016-02-23 17:14:37 +01006199#if GTK_CHECK_VERSION(3,0,0)
6200 cairo_destroy(cr);
presuku9459b8d2021-11-16 20:03:56 +00006201 gtk_widget_queue_draw_area(gui.drawarea, area.x, area.y,
6202 area.width, area.height);
Bram Moolenaar98921892016-02-23 17:14:37 +01006203#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006204 gdk_gc_set_clip_rectangle(gui.text_gc, NULL);
Bram Moolenaar98921892016-02-23 17:14:37 +01006205#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006206
6207 return column_offset;
6208}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006209
6210/*
6211 * Return OK if the key with the termcap name "name" is supported.
6212 */
6213 int
6214gui_mch_haskey(char_u *name)
6215{
6216 int i;
6217
6218 for (i = 0; special_keys[i].key_sym != 0; i++)
6219 if (name[0] == special_keys[i].code0
6220 && name[1] == special_keys[i].code1)
6221 return OK;
6222 return FAIL;
6223}
6224
rbtnnb850c392022-10-01 15:47:04 +01006225#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006226/*
6227 * Return the text window-id and display. Only required for X-based GUI's
6228 */
6229 int
6230gui_get_x11_windis(Window *win, Display **dis)
6231{
Bram Moolenaar98921892016-02-23 17:14:37 +01006232 if (gui.mainwin != NULL && gtk_widget_get_window(gui.mainwin) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006233 {
Bram Moolenaar98921892016-02-23 17:14:37 +01006234 *dis = GDK_WINDOW_XDISPLAY(gtk_widget_get_window(gui.mainwin));
6235 *win = GDK_WINDOW_XID(gtk_widget_get_window(gui.mainwin));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006236 return OK;
6237 }
6238
6239 *dis = NULL;
6240 *win = 0;
6241 return FAIL;
6242}
6243#endif
6244
6245#if defined(FEAT_CLIENTSERVER) \
6246 || (defined(FEAT_X11) && defined(FEAT_CLIPBOARD)) || defined(PROTO)
6247
6248 Display *
6249gui_mch_get_display(void)
6250{
Bram Moolenaar98921892016-02-23 17:14:37 +01006251 if (gui.mainwin != NULL && gtk_widget_get_window(gui.mainwin) != NULL)
6252 return GDK_WINDOW_XDISPLAY(gtk_widget_get_window(gui.mainwin));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006253 else
6254 return NULL;
6255}
6256#endif
6257
6258 void
6259gui_mch_beep(void)
6260{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006261 GdkDisplay *display;
6262
Bram Moolenaar98921892016-02-23 17:14:37 +01006263 if (gui.mainwin != NULL && gtk_widget_get_realized(gui.mainwin))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006264 display = gtk_widget_get_display(gui.mainwin);
6265 else
6266 display = gdk_display_get_default();
6267
6268 if (display != NULL)
6269 gdk_display_beep(display);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006270}
6271
6272 void
6273gui_mch_flash(int msec)
6274{
Bram Moolenaar98921892016-02-23 17:14:37 +01006275#if GTK_CHECK_VERSION(3,0,0)
Bram Moolenaar30613902019-12-01 22:11:18 +01006276 // TODO Replace GdkGC with Cairo
Bram Moolenaar98921892016-02-23 17:14:37 +01006277 (void)msec;
6278#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006279 GdkGCValues values;
6280 GdkGC *invert_gc;
6281
6282 if (gui.drawarea->window == NULL)
6283 return;
6284
6285 values.foreground.pixel = gui.norm_pixel ^ gui.back_pixel;
6286 values.background.pixel = gui.norm_pixel ^ gui.back_pixel;
6287 values.function = GDK_XOR;
6288 invert_gc = gdk_gc_new_with_values(gui.drawarea->window,
6289 &values,
6290 GDK_GC_FOREGROUND |
6291 GDK_GC_BACKGROUND |
6292 GDK_GC_FUNCTION);
6293 gdk_gc_set_exposures(invert_gc,
6294 gui.visibility != GDK_VISIBILITY_UNOBSCURED);
6295 /*
6296 * Do a visual beep by changing back and forth in some undetermined way,
6297 * the foreground and background colors. This is due to the fact that
6298 * there can't be really any prediction about the effects of XOR on
6299 * arbitrary X11 servers. However this seems to be enough for what we
6300 * intend it to do.
6301 */
6302 gdk_draw_rectangle(gui.drawarea->window, invert_gc,
6303 TRUE,
6304 0, 0,
6305 FILL_X((int)Columns) + gui.border_offset,
6306 FILL_Y((int)Rows) + gui.border_offset);
6307
6308 gui_mch_flush();
Bram Moolenaar30613902019-12-01 22:11:18 +01006309 ui_delay((long)msec, TRUE); // wait so many msec
Bram Moolenaar071d4272004-06-13 20:20:40 +00006310
6311 gdk_draw_rectangle(gui.drawarea->window, invert_gc,
6312 TRUE,
6313 0, 0,
6314 FILL_X((int)Columns) + gui.border_offset,
6315 FILL_Y((int)Rows) + gui.border_offset);
6316
6317 gdk_gc_destroy(invert_gc);
Bram Moolenaar98921892016-02-23 17:14:37 +01006318#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006319}
6320
6321/*
6322 * Invert a rectangle from row r, column c, for nr rows and nc columns.
6323 */
6324 void
6325gui_mch_invert_rectangle(int r, int c, int nr, int nc)
6326{
Bram Moolenaar98921892016-02-23 17:14:37 +01006327#if GTK_CHECK_VERSION(3,0,0)
Bram Moolenaar4fc563b2016-03-12 12:40:58 +01006328 const GdkRectangle rect = {
6329 FILL_X(c), FILL_Y(r), nc * gui.char_width, nr * gui.char_height
6330 };
6331 cairo_t * const cr = cairo_create(gui.surface);
6332
Your Name810cb5a2022-09-13 11:25:54 +01006333 cairo_set_source_rgba(cr, 1.0, 1.0, 1.0, 1.0);
Bram Moolenaar4fc563b2016-03-12 12:40:58 +01006334# if CAIRO_VERSION >= CAIRO_VERSION_ENCODE(1,9,2)
6335 cairo_set_operator(cr, CAIRO_OPERATOR_DIFFERENCE);
6336# else
Bram Moolenaar30613902019-12-01 22:11:18 +01006337 // Give an implementation for older cairo versions if necessary.
Bram Moolenaar4fc563b2016-03-12 12:40:58 +01006338# endif
presuku9459b8d2021-11-16 20:03:56 +00006339 cairo_rectangle(cr, rect.x, rect.y, rect.width, rect.height);
Bram Moolenaar4fc563b2016-03-12 12:40:58 +01006340 cairo_fill(cr);
6341
6342 cairo_destroy(cr);
6343
presuku9459b8d2021-11-16 20:03:56 +00006344 gtk_widget_queue_draw_area(gui.drawarea, rect.x, rect.y,
6345 rect.width, rect.height);
Bram Moolenaar98921892016-02-23 17:14:37 +01006346#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006347 GdkGCValues values;
6348 GdkGC *invert_gc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006349
6350 if (gui.drawarea->window == NULL)
6351 return;
6352
Your Name810cb5a2022-09-13 11:25:54 +01006353 values.function = GDK_INVERT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006354 invert_gc = gdk_gc_new_with_values(gui.drawarea->window,
6355 &values,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006356 GDK_GC_FUNCTION);
Bram Moolenaar89d40322006-08-29 15:30:07 +00006357 gdk_gc_set_exposures(invert_gc, gui.visibility !=
6358 GDK_VISIBILITY_UNOBSCURED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006359 gdk_draw_rectangle(gui.drawarea->window, invert_gc,
6360 TRUE,
6361 FILL_X(c), FILL_Y(r),
6362 (nc) * gui.char_width, (nr) * gui.char_height);
6363 gdk_gc_destroy(invert_gc);
Bram Moolenaar98921892016-02-23 17:14:37 +01006364#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006365}
6366
6367/*
6368 * Iconify the GUI window.
6369 */
6370 void
6371gui_mch_iconify(void)
6372{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006373 gtk_window_iconify(GTK_WINDOW(gui.mainwin));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006374}
6375
6376#if defined(FEAT_EVAL) || defined(PROTO)
6377/*
6378 * Bring the Vim window to the foreground.
6379 */
6380 void
6381gui_mch_set_foreground(void)
6382{
Bram Moolenaar023930d2022-12-01 19:40:55 +00006383 // Just calling gtk_window_present() used to work in the past, but now this
6384 // sequence appears to be needed:
6385 // - Show the window on top of others.
6386 // - Present the window (also shows it above others).
6387 // - Do not the window on top of others (otherwise it would be stuck there).
6388 gtk_window_set_keep_above(GTK_WINDOW(gui.mainwin), TRUE);
6389 gui_may_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006390 gtk_window_present(GTK_WINDOW(gui.mainwin));
Bram Moolenaar023930d2022-12-01 19:40:55 +00006391 gui_may_flush();
6392 gtk_window_set_keep_above(GTK_WINDOW(gui.mainwin), FALSE);
6393 gui_may_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006394}
6395#endif
6396
6397/*
6398 * Draw a cursor without focus.
6399 */
6400 void
6401gui_mch_draw_hollow_cursor(guicolor_T color)
6402{
6403 int i = 1;
Bram Moolenaar98921892016-02-23 17:14:37 +01006404#if GTK_CHECK_VERSION(3,0,0)
6405 cairo_t *cr;
6406#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006407
Bram Moolenaar98921892016-02-23 17:14:37 +01006408 if (gtk_widget_get_window(gui.drawarea) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006409 return;
6410
Bram Moolenaar98921892016-02-23 17:14:37 +01006411#if GTK_CHECK_VERSION(3,0,0)
6412 cr = cairo_create(gui.surface);
6413#endif
6414
Bram Moolenaar071d4272004-06-13 20:20:40 +00006415 gui_mch_set_fg_color(color);
6416
Bram Moolenaar98921892016-02-23 17:14:37 +01006417#if GTK_CHECK_VERSION(3,0,0)
Bram Moolenaar36edf062016-07-21 22:10:12 +02006418 cairo_set_source_rgba(cr,
6419 gui.fgcolor->red, gui.fgcolor->green, gui.fgcolor->blue,
6420 gui.fgcolor->alpha);
Bram Moolenaar98921892016-02-23 17:14:37 +01006421#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006422 gdk_gc_set_foreground(gui.text_gc, gui.fgcolor);
Bram Moolenaar98921892016-02-23 17:14:37 +01006423#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006424 if (mb_lefthalve(gui.row, gui.col))
6425 i = 2;
Bram Moolenaar98921892016-02-23 17:14:37 +01006426#if GTK_CHECK_VERSION(3,0,0)
6427 cairo_set_line_width(cr, 1.0);
6428 cairo_set_line_cap(cr, CAIRO_LINE_CAP_BUTT);
6429 cairo_rectangle(cr,
6430 FILL_X(gui.col) + 0.5, FILL_Y(gui.row) + 0.5,
6431 i * gui.char_width - 1, gui.char_height - 1);
6432 cairo_stroke(cr);
6433 cairo_destroy(cr);
6434#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006435 gdk_draw_rectangle(gui.drawarea->window, gui.text_gc,
6436 FALSE,
6437 FILL_X(gui.col), FILL_Y(gui.row),
6438 i * gui.char_width - 1, gui.char_height - 1);
Bram Moolenaar98921892016-02-23 17:14:37 +01006439#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006440}
6441
6442/*
6443 * Draw part of a cursor, "w" pixels wide, and "h" pixels high, using
6444 * color "color".
6445 */
6446 void
6447gui_mch_draw_part_cursor(int w, int h, guicolor_T color)
6448{
Bram Moolenaar98921892016-02-23 17:14:37 +01006449 if (gtk_widget_get_window(gui.drawarea) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006450 return;
6451
6452 gui_mch_set_fg_color(color);
6453
Bram Moolenaar98921892016-02-23 17:14:37 +01006454#if GTK_CHECK_VERSION(3,0,0)
6455 {
6456 cairo_t *cr;
6457
6458 cr = cairo_create(gui.surface);
Bram Moolenaar36edf062016-07-21 22:10:12 +02006459 cairo_set_source_rgba(cr,
6460 gui.fgcolor->red, gui.fgcolor->green, gui.fgcolor->blue,
6461 gui.fgcolor->alpha);
Bram Moolenaar98921892016-02-23 17:14:37 +01006462 cairo_rectangle(cr,
6463# ifdef FEAT_RIGHTLEFT
Bram Moolenaar30613902019-12-01 22:11:18 +01006464 // vertical line should be on the right of current point
Bram Moolenaar98921892016-02-23 17:14:37 +01006465 CURSOR_BAR_RIGHT ? FILL_X(gui.col + 1) - w :
6466# endif
6467 FILL_X(gui.col), FILL_Y(gui.row) + gui.char_height - h,
6468 w, h);
6469 cairo_fill(cr);
6470 cairo_destroy(cr);
6471 }
Bram Moolenaar30613902019-12-01 22:11:18 +01006472#else // !GTK_CHECK_VERSION(3,0,0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006473 gdk_gc_set_foreground(gui.text_gc, gui.fgcolor);
6474 gdk_draw_rectangle(gui.drawarea->window, gui.text_gc,
6475 TRUE,
Bram Moolenaar98921892016-02-23 17:14:37 +01006476# ifdef FEAT_RIGHTLEFT
Bram Moolenaar30613902019-12-01 22:11:18 +01006477 // vertical line should be on the right of current point
Bram Moolenaar071d4272004-06-13 20:20:40 +00006478 CURSOR_BAR_RIGHT ? FILL_X(gui.col + 1) - w :
Bram Moolenaar98921892016-02-23 17:14:37 +01006479# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006480 FILL_X(gui.col),
6481 FILL_Y(gui.row) + gui.char_height - h,
6482 w, h);
Bram Moolenaar30613902019-12-01 22:11:18 +01006483#endif // !GTK_CHECK_VERSION(3,0,0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006484}
6485
6486
6487/*
6488 * Catch up with any queued X11 events. This may put keyboard input into the
6489 * input buffer, call resize call-backs, trigger timers etc. If there is
6490 * nothing in the X11 event queue (& no timers pending), then we return
6491 * immediately.
6492 */
6493 void
6494gui_mch_update(void)
6495{
Bram Moolenaara3f41662010-07-11 19:01:06 +02006496 while (g_main_context_pending(NULL) && !vim_is_input_buf_full())
6497 g_main_context_iteration(NULL, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006498}
6499
Bram Moolenaar4ab79682017-08-27 14:50:47 +02006500 static timeout_cb_type
Bram Moolenaar071d4272004-06-13 20:20:40 +00006501input_timer_cb(gpointer data)
6502{
6503 int *timed_out = (int *) data;
6504
Bram Moolenaar30613902019-12-01 22:11:18 +01006505 // Just inform the caller about the occurrence of it
Bram Moolenaar071d4272004-06-13 20:20:40 +00006506 *timed_out = TRUE;
6507
Bram Moolenaar30613902019-12-01 22:11:18 +01006508 return FALSE; // don't happen again
Bram Moolenaar071d4272004-06-13 20:20:40 +00006509}
6510
Bram Moolenaar4ab79682017-08-27 14:50:47 +02006511#ifdef FEAT_JOB_CHANNEL
6512 static timeout_cb_type
6513channel_poll_cb(gpointer data UNUSED)
6514{
Bram Moolenaar30613902019-12-01 22:11:18 +01006515 // Using an event handler for a channel that may be disconnected does
6516 // not work, it hangs. Instead poll for messages.
Bram Moolenaar4ab79682017-08-27 14:50:47 +02006517 channel_handle_events(TRUE);
6518 parse_queued_messages();
6519
Bram Moolenaar30613902019-12-01 22:11:18 +01006520 return TRUE; // repeat
Bram Moolenaar4ab79682017-08-27 14:50:47 +02006521}
6522#endif
6523
Bram Moolenaar071d4272004-06-13 20:20:40 +00006524/*
6525 * GUI input routine called by gui_wait_for_chars(). Waits for a character
6526 * from the keyboard.
6527 * wtime == -1 Wait forever.
6528 * wtime == 0 This should never happen.
6529 * wtime > 0 Wait wtime milliseconds for a character.
6530 * Returns OK if a character was found to be available within the given time,
6531 * or FAIL otherwise.
6532 */
6533 int
6534gui_mch_wait_for_chars(long wtime)
6535{
Bram Moolenaar4231da42016-06-02 14:30:04 +02006536 int focus;
6537 guint timer;
6538 static int timed_out;
6539 int retval = FAIL;
Bram Moolenaar4ab79682017-08-27 14:50:47 +02006540#ifdef FEAT_JOB_CHANNEL
6541 guint channel_timer = 0;
6542#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006543
6544 timed_out = FALSE;
6545
Bram Moolenaar12dfc9e2019-01-28 22:32:58 +01006546 // This timeout makes sure that we will return if no characters arrived in
6547 // time. If "wtime" is zero just use one.
6548 if (wtime >= 0)
Bram Moolenaar34a58742019-02-03 15:28:28 +01006549 timer = timeout_add(wtime == 0 ? 1L : wtime,
Bram Moolenaar12dfc9e2019-01-28 22:32:58 +01006550 input_timer_cb, &timed_out);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006551 else
6552 timer = 0;
6553
Bram Moolenaar4ab79682017-08-27 14:50:47 +02006554#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar30613902019-12-01 22:11:18 +01006555 // If there is a channel with the keep_open flag we need to poll for input
6556 // on them.
Bram Moolenaar4ab79682017-08-27 14:50:47 +02006557 if (channel_any_keep_open())
6558 channel_timer = timeout_add(20, channel_poll_cb, NULL);
6559#endif
6560
Bram Moolenaar071d4272004-06-13 20:20:40 +00006561 focus = gui.in_focus;
6562
6563 do
6564 {
Bram Moolenaar30613902019-12-01 22:11:18 +01006565 // Stop or start blinking when focus changes
Bram Moolenaar071d4272004-06-13 20:20:40 +00006566 if (gui.in_focus != focus)
6567 {
6568 if (gui.in_focus)
6569 gui_mch_start_blink();
6570 else
Bram Moolenaar1dd45fb2018-01-31 21:10:01 +01006571 gui_mch_stop_blink(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006572 focus = gui.in_focus;
6573 }
6574
Bram Moolenaar93c88e02015-09-15 14:12:05 +02006575#ifdef MESSAGE_QUEUE
Bram Moolenaar4231da42016-06-02 14:30:04 +02006576# ifdef FEAT_TIMERS
6577 did_add_timer = FALSE;
6578# endif
Bram Moolenaar93c88e02015-09-15 14:12:05 +02006579 parse_queued_messages();
Bram Moolenaar4231da42016-06-02 14:30:04 +02006580# ifdef FEAT_TIMERS
6581 if (did_add_timer)
Bram Moolenaar30613902019-12-01 22:11:18 +01006582 // Need to recompute the waiting time.
Bram Moolenaar4231da42016-06-02 14:30:04 +02006583 goto theend;
6584# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006585#endif
6586
Bram Moolenaar071d4272004-06-13 20:20:40 +00006587 /*
6588 * Loop in GTK+ processing until a timeout or input occurs.
Bram Moolenaard4755bb2004-09-02 19:12:26 +00006589 * Skip this if input is available anyway (can happen in rare
6590 * situations, sort of race condition).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006591 */
Bram Moolenaard4755bb2004-09-02 19:12:26 +00006592 if (!input_available())
Bram Moolenaara3f41662010-07-11 19:01:06 +02006593 g_main_context_iteration(NULL, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006594
Bram Moolenaar30613902019-12-01 22:11:18 +01006595 // Got char, return immediately
Bram Moolenaar071d4272004-06-13 20:20:40 +00006596 if (input_available())
6597 {
Bram Moolenaar4231da42016-06-02 14:30:04 +02006598 retval = OK;
6599 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006600 }
6601 } while (wtime < 0 || !timed_out);
6602
6603 /*
6604 * Flush all eventually pending (drawing) events.
6605 */
6606 gui_mch_update();
6607
Bram Moolenaar4231da42016-06-02 14:30:04 +02006608theend:
6609 if (timer != 0 && !timed_out)
Bram Moolenaar4ab79682017-08-27 14:50:47 +02006610 timeout_remove(timer);
6611#ifdef FEAT_JOB_CHANNEL
6612 if (channel_timer != 0)
6613 timeout_remove(channel_timer);
Bram Moolenaar4231da42016-06-02 14:30:04 +02006614#endif
6615
6616 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006617}
6618
6619
Bram Moolenaar30613902019-12-01 22:11:18 +01006620/////////////////////////////////////////////////////////////////////////////
6621// Output drawing routines.
6622//
Bram Moolenaar071d4272004-06-13 20:20:40 +00006623
6624
Bram Moolenaar30613902019-12-01 22:11:18 +01006625// Flush any output to the screen
Bram Moolenaar071d4272004-06-13 20:20:40 +00006626 void
6627gui_mch_flush(void)
6628{
Bram Moolenaar98921892016-02-23 17:14:37 +01006629 if (gui.mainwin != NULL && gtk_widget_get_realized(gui.mainwin))
Bram Moolenaar92cbf622018-09-19 22:40:03 +02006630#if GTK_CHECK_VERSION(2,4,0)
Bram Moolenaarb463e8d2017-06-05 15:07:09 +02006631 gdk_display_flush(gtk_widget_get_display(gui.mainwin));
Bram Moolenaar92cbf622018-09-19 22:40:03 +02006632#else
6633 gdk_display_sync(gtk_widget_get_display(gui.mainwin));
6634#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006635}
6636
6637/*
6638 * Clear a rectangular region of the screen from text pos (row1, col1) to
6639 * (row2, col2) inclusive.
6640 */
6641 void
Bram Moolenaarfe344a92017-02-23 12:20:35 +01006642gui_mch_clear_block(int row1arg, int col1arg, int row2arg, int col2arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006643{
Bram Moolenaarfe344a92017-02-23 12:20:35 +01006644
6645 int col1 = check_col(col1arg);
6646 int col2 = check_col(col2arg);
6647 int row1 = check_row(row1arg);
6648 int row2 = check_row(row2arg);
6649
Bram Moolenaar98921892016-02-23 17:14:37 +01006650#if GTK_CHECK_VERSION(3,0,0)
6651 if (gtk_widget_get_window(gui.drawarea) == NULL)
6652 return;
6653#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006654 GdkColor color;
6655
6656 if (gui.drawarea->window == NULL)
6657 return;
6658
6659 color.pixel = gui.back_pixel;
Bram Moolenaar98921892016-02-23 17:14:37 +01006660#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006661
Bram Moolenaar98921892016-02-23 17:14:37 +01006662#if GTK_CHECK_VERSION(3,0,0)
6663 {
Bram Moolenaar30613902019-12-01 22:11:18 +01006664 // Add one pixel to the far right column in case a double-stroked
6665 // bold glyph may sit there.
Bram Moolenaar98921892016-02-23 17:14:37 +01006666 const GdkRectangle rect = {
6667 FILL_X(col1), FILL_Y(row1),
6668 (col2 - col1 + 1) * gui.char_width + (col2 == Columns - 1),
6669 (row2 - row1 + 1) * gui.char_height
6670 };
Bram Moolenaar98921892016-02-23 17:14:37 +01006671 cairo_t * const cr = cairo_create(gui.surface);
Bram Moolenaara859f042016-11-17 19:11:55 +01006672# if GTK_CHECK_VERSION(3,22,2)
6673 set_cairo_source_rgba_from_color(cr, gui.back_pixel);
6674# else
presuku9459b8d2021-11-16 20:03:56 +00006675 GdkWindow * const win = gtk_widget_get_window(gui.drawarea);
Bram Moolenaar98921892016-02-23 17:14:37 +01006676 cairo_pattern_t * const pat = gdk_window_get_background_pattern(win);
6677 if (pat != NULL)
6678 cairo_set_source(cr, pat);
6679 else
Bram Moolenaar36edf062016-07-21 22:10:12 +02006680 set_cairo_source_rgba_from_color(cr, gui.back_pixel);
Bram Moolenaara859f042016-11-17 19:11:55 +01006681# endif
presuku9459b8d2021-11-16 20:03:56 +00006682 cairo_rectangle(cr, rect.x, rect.y, rect.width, rect.height);
Bram Moolenaar98921892016-02-23 17:14:37 +01006683 cairo_fill(cr);
6684 cairo_destroy(cr);
6685
presuku9459b8d2021-11-16 20:03:56 +00006686 gtk_widget_queue_draw_area(gui.drawarea,
6687 rect.x, rect.y, rect.width, rect.height);
Bram Moolenaar98921892016-02-23 17:14:37 +01006688 }
Bram Moolenaar30613902019-12-01 22:11:18 +01006689#else // !GTK_CHECK_VERSION(3,0,0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006690 gdk_gc_set_foreground(gui.text_gc, &color);
6691
Bram Moolenaar30613902019-12-01 22:11:18 +01006692 // Clear one extra pixel at the far right, for when bold characters have
6693 // spilled over to the window border.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006694 gdk_draw_rectangle(gui.drawarea->window, gui.text_gc, TRUE,
6695 FILL_X(col1), FILL_Y(row1),
6696 (col2 - col1 + 1) * gui.char_width
6697 + (col2 == Columns - 1),
6698 (row2 - row1 + 1) * gui.char_height);
Bram Moolenaar30613902019-12-01 22:11:18 +01006699#endif // !GTK_CHECK_VERSION(3,0,0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006700}
6701
Bram Moolenaar98921892016-02-23 17:14:37 +01006702#if GTK_CHECK_VERSION(3,0,0)
6703 static void
6704gui_gtk_window_clear(GdkWindow *win)
6705{
6706 const GdkRectangle rect = {
6707 0, 0, gdk_window_get_width(win), gdk_window_get_height(win)
6708 };
6709 cairo_t * const cr = cairo_create(gui.surface);
Bram Moolenaara859f042016-11-17 19:11:55 +01006710# if GTK_CHECK_VERSION(3,22,2)
6711 set_cairo_source_rgba_from_color(cr, gui.back_pixel);
6712# else
Bram Moolenaar98921892016-02-23 17:14:37 +01006713 cairo_pattern_t * const pat = gdk_window_get_background_pattern(win);
6714 if (pat != NULL)
6715 cairo_set_source(cr, pat);
6716 else
Bram Moolenaar36edf062016-07-21 22:10:12 +02006717 set_cairo_source_rgba_from_color(cr, gui.back_pixel);
Bram Moolenaara859f042016-11-17 19:11:55 +01006718# endif
presuku9459b8d2021-11-16 20:03:56 +00006719 cairo_rectangle(cr, rect.x, rect.y, rect.width, rect.height);
Bram Moolenaar98921892016-02-23 17:14:37 +01006720 cairo_fill(cr);
6721 cairo_destroy(cr);
6722
presuku9459b8d2021-11-16 20:03:56 +00006723 gtk_widget_queue_draw_area(gui.drawarea,
6724 rect.x, rect.y, rect.width, rect.height);
Bram Moolenaar98921892016-02-23 17:14:37 +01006725}
Bram Moolenaar25328e32018-09-11 21:30:09 +02006726#else
6727# define gui_gtk_window_clear(win) gdk_window_clear(win)
Bram Moolenaar98921892016-02-23 17:14:37 +01006728#endif
6729
Bram Moolenaar071d4272004-06-13 20:20:40 +00006730 void
6731gui_mch_clear_all(void)
6732{
Bram Moolenaar98921892016-02-23 17:14:37 +01006733 if (gtk_widget_get_window(gui.drawarea) != NULL)
6734 gui_gtk_window_clear(gtk_widget_get_window(gui.drawarea));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006735}
6736
Bram Moolenaar98921892016-02-23 17:14:37 +01006737#if !GTK_CHECK_VERSION(3,0,0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006738/*
6739 * Redraw any text revealed by scrolling up/down.
6740 */
6741 static void
6742check_copy_area(void)
6743{
6744 GdkEvent *event;
6745 int expose_count;
6746
6747 if (gui.visibility != GDK_VISIBILITY_PARTIAL)
6748 return;
6749
Bram Moolenaar30613902019-12-01 22:11:18 +01006750 // Avoid redrawing the cursor while scrolling or it'll end up where
6751 // we don't want it to be. I'm not sure if it's correct to call
6752 // gui_dont_update_cursor() at this point but it works as a quick
6753 // fix for now.
Bram Moolenaar107abd22016-08-12 14:08:25 +02006754 gui_dont_update_cursor(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006755
6756 do
6757 {
Bram Moolenaar30613902019-12-01 22:11:18 +01006758 // Wait to check whether the scroll worked or not.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006759 event = gdk_event_get_graphics_expose(gui.drawarea->window);
6760
6761 if (event == NULL)
Bram Moolenaar30613902019-12-01 22:11:18 +01006762 break; // received NoExpose event
Bram Moolenaar071d4272004-06-13 20:20:40 +00006763
6764 gui_redraw(event->expose.area.x, event->expose.area.y,
6765 event->expose.area.width, event->expose.area.height);
6766
6767 expose_count = event->expose.count;
6768 gdk_event_free(event);
6769 }
Bram Moolenaar30613902019-12-01 22:11:18 +01006770 while (expose_count > 0); // more events follow
Bram Moolenaar071d4272004-06-13 20:20:40 +00006771
6772 gui_can_update_cursor();
6773}
Bram Moolenaar30613902019-12-01 22:11:18 +01006774#endif // !GTK_CHECK_VERSION(3,0,0)
Bram Moolenaar98921892016-02-23 17:14:37 +01006775
6776#if GTK_CHECK_VERSION(3,0,0)
6777 static void
6778gui_gtk_surface_copy_rect(int dest_x, int dest_y,
6779 int src_x, int src_y,
6780 int width, int height)
6781{
6782 cairo_t * const cr = cairo_create(gui.surface);
6783
6784 cairo_rectangle(cr, dest_x, dest_y, width, height);
6785 cairo_clip(cr);
6786 cairo_push_group(cr);
6787 cairo_set_source_surface(cr, gui.surface, dest_x - src_x, dest_y - src_y);
6788 cairo_paint(cr);
6789 cairo_pop_group_to_source(cr);
6790 cairo_paint(cr);
6791
6792 cairo_destroy(cr);
6793}
6794#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006795
6796/*
6797 * Delete the given number of lines from the given row, scrolling up any
6798 * text further down within the scroll region.
6799 */
6800 void
6801gui_mch_delete_lines(int row, int num_lines)
6802{
Bram Moolenaar98921892016-02-23 17:14:37 +01006803#if GTK_CHECK_VERSION(3,0,0)
6804 const int ncols = gui.scroll_region_right - gui.scroll_region_left + 1;
6805 const int nrows = gui.scroll_region_bot - row + 1;
6806 const int src_nrows = nrows - num_lines;
6807
6808 gui_gtk_surface_copy_rect(
6809 FILL_X(gui.scroll_region_left), FILL_Y(row),
6810 FILL_X(gui.scroll_region_left), FILL_Y(row + num_lines),
6811 gui.char_width * ncols + 1, gui.char_height * src_nrows);
6812 gui_clear_block(
6813 gui.scroll_region_bot - num_lines + 1, gui.scroll_region_left,
6814 gui.scroll_region_bot, gui.scroll_region_right);
presuku9459b8d2021-11-16 20:03:56 +00006815 gtk_widget_queue_draw_area(gui.drawarea,
Bram Moolenaar98921892016-02-23 17:14:37 +01006816 FILL_X(gui.scroll_region_left), FILL_Y(row),
presuku9459b8d2021-11-16 20:03:56 +00006817 gui.char_width * ncols + 1, gui.char_height * nrows);
Bram Moolenaar98921892016-02-23 17:14:37 +01006818#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006819 if (gui.visibility == GDK_VISIBILITY_FULLY_OBSCURED)
Bram Moolenaar30613902019-12-01 22:11:18 +01006820 return; // Can't see the window
Bram Moolenaar071d4272004-06-13 20:20:40 +00006821
6822 gdk_gc_set_foreground(gui.text_gc, gui.fgcolor);
6823 gdk_gc_set_background(gui.text_gc, gui.bgcolor);
6824
Bram Moolenaar30613902019-12-01 22:11:18 +01006825 // copy one extra pixel, for when bold has spilled over
Bram Moolenaar071d4272004-06-13 20:20:40 +00006826 gdk_window_copy_area(gui.drawarea->window, gui.text_gc,
6827 FILL_X(gui.scroll_region_left), FILL_Y(row),
6828 gui.drawarea->window,
6829 FILL_X(gui.scroll_region_left),
6830 FILL_Y(row + num_lines),
6831 gui.char_width * (gui.scroll_region_right
6832 - gui.scroll_region_left + 1) + 1,
6833 gui.char_height * (gui.scroll_region_bot - row - num_lines + 1));
6834
6835 gui_clear_block(gui.scroll_region_bot - num_lines + 1,
6836 gui.scroll_region_left,
6837 gui.scroll_region_bot, gui.scroll_region_right);
6838 check_copy_area();
Bram Moolenaar30613902019-12-01 22:11:18 +01006839#endif // !GTK_CHECK_VERSION(3,0,0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006840}
6841
6842/*
6843 * Insert the given number of lines before the given row, scrolling down any
6844 * following text within the scroll region.
6845 */
6846 void
6847gui_mch_insert_lines(int row, int num_lines)
6848{
Bram Moolenaar98921892016-02-23 17:14:37 +01006849#if GTK_CHECK_VERSION(3,0,0)
6850 const int ncols = gui.scroll_region_right - gui.scroll_region_left + 1;
6851 const int nrows = gui.scroll_region_bot - row + 1;
6852 const int src_nrows = nrows - num_lines;
6853
6854 gui_gtk_surface_copy_rect(
6855 FILL_X(gui.scroll_region_left), FILL_Y(row + num_lines),
6856 FILL_X(gui.scroll_region_left), FILL_Y(row),
6857 gui.char_width * ncols + 1, gui.char_height * src_nrows);
presuku9459b8d2021-11-16 20:03:56 +00006858 gui_clear_block(
Bram Moolenaar98921892016-02-23 17:14:37 +01006859 row, gui.scroll_region_left,
6860 row + num_lines - 1, gui.scroll_region_right);
presuku9459b8d2021-11-16 20:03:56 +00006861 gtk_widget_queue_draw_area(gui.drawarea,
Bram Moolenaar98921892016-02-23 17:14:37 +01006862 FILL_X(gui.scroll_region_left), FILL_Y(row),
presuku9459b8d2021-11-16 20:03:56 +00006863 gui.char_width * ncols + 1, gui.char_height * nrows);
Bram Moolenaar98921892016-02-23 17:14:37 +01006864#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006865 if (gui.visibility == GDK_VISIBILITY_FULLY_OBSCURED)
Bram Moolenaar30613902019-12-01 22:11:18 +01006866 return; // Can't see the window
Bram Moolenaar071d4272004-06-13 20:20:40 +00006867
6868 gdk_gc_set_foreground(gui.text_gc, gui.fgcolor);
6869 gdk_gc_set_background(gui.text_gc, gui.bgcolor);
6870
Bram Moolenaar30613902019-12-01 22:11:18 +01006871 // copy one extra pixel, for when bold has spilled over
Bram Moolenaar071d4272004-06-13 20:20:40 +00006872 gdk_window_copy_area(gui.drawarea->window, gui.text_gc,
6873 FILL_X(gui.scroll_region_left), FILL_Y(row + num_lines),
6874 gui.drawarea->window,
6875 FILL_X(gui.scroll_region_left), FILL_Y(row),
6876 gui.char_width * (gui.scroll_region_right
6877 - gui.scroll_region_left + 1) + 1,
6878 gui.char_height * (gui.scroll_region_bot - row - num_lines + 1));
6879
6880 gui_clear_block(row, gui.scroll_region_left,
6881 row + num_lines - 1, gui.scroll_region_right);
6882 check_copy_area();
Bram Moolenaar30613902019-12-01 22:11:18 +01006883#endif // !GTK_CHECK_VERSION(3,0,0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006884}
6885
6886/*
6887 * X Selection stuff, for cutting and pasting text to other windows.
6888 */
6889 void
Bram Moolenaar0554fa42019-06-14 21:36:54 +02006890clip_mch_request_selection(Clipboard_T *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006891{
6892 GdkAtom target;
6893 unsigned i;
Bram Moolenaar51b5ab92008-01-06 14:17:07 +00006894 time_t start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006895
6896 for (i = 0; i < N_SELECTION_TARGETS; ++i)
6897 {
Bram Moolenaar3a6eaa52009-06-16 13:23:06 +00006898 if (!clip_html && selection_targets[i].info == TARGET_HTML)
6899 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006900 received_selection = RS_NONE;
6901 target = gdk_atom_intern(selection_targets[i].target, FALSE);
6902
6903 gtk_selection_convert(gui.drawarea,
6904 cbd->gtk_sel_atom, target,
6905 (guint32)GDK_CURRENT_TIME);
6906
Bram Moolenaar30613902019-12-01 22:11:18 +01006907 // Hack: Wait up to three seconds for the selection. A hang was
6908 // noticed here when using the netrw plugin combined with ":gui"
6909 // during the FocusGained event.
Bram Moolenaar51b5ab92008-01-06 14:17:07 +00006910 start = time(NULL);
6911 while (received_selection == RS_NONE && time(NULL) < start + 3)
Bram Moolenaar30613902019-12-01 22:11:18 +01006912 g_main_context_iteration(NULL, TRUE); // wait for selection_received_cb
Bram Moolenaar071d4272004-06-13 20:20:40 +00006913
6914 if (received_selection != RS_FAIL)
6915 return;
6916 }
6917
Bram Moolenaar30613902019-12-01 22:11:18 +01006918 // Final fallback position - use the X CUT_BUFFER0 store
Bram Moolenaar98921892016-02-23 17:14:37 +01006919 yank_cut_buffer0(GDK_WINDOW_XDISPLAY(gtk_widget_get_window(gui.mainwin)),
6920 cbd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006921}
6922
6923/*
6924 * Disown the selection.
6925 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006926 void
Bram Moolenaar0554fa42019-06-14 21:36:54 +02006927clip_mch_lose_selection(Clipboard_T *cbd UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006928{
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00006929 if (in_selection_clear_event)
6930 return;
6931
6932 gtk_selection_owner_set(NULL, cbd->gtk_sel_atom, gui.event_time);
6933 gui_mch_update();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006934}
6935
6936/*
6937 * Own the selection and return OK if it worked.
6938 */
6939 int
Bram Moolenaar0554fa42019-06-14 21:36:54 +02006940clip_mch_own_selection(Clipboard_T *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006941{
lilydjwg94ff09a2024-01-29 20:14:01 +01006942 // If we're blocking autocmds, we are filling the register to offer the
6943 // selection (inside selection-get)
6944 if (is_autocmd_blocked())
6945 return OK;
6946
Bram Moolenaar071d4272004-06-13 20:20:40 +00006947 int success;
6948
6949 success = gtk_selection_owner_set(gui.drawarea, cbd->gtk_sel_atom,
Bram Moolenaar20892c12011-06-26 04:49:00 +02006950 gui.event_time);
lilydjwg94ff09a2024-01-29 20:14:01 +01006951 // don't update on every visual selection change
6952 if (!(cbd->owned && VIsual_active))
6953 gui_gtk_set_selection_targets(cbd->gtk_sel_atom);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006954 gui_mch_update();
6955 return (success) ? OK : FAIL;
6956}
6957
6958/*
6959 * Send the current selection to the clipboard. Do nothing for X because we
6960 * will fill in the selection only when requested by another app.
6961 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006962 void
Bram Moolenaar0554fa42019-06-14 21:36:54 +02006963clip_mch_set_selection(Clipboard_T *cbd UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006964{
6965}
6966
Bram Moolenaar113e1072019-01-20 15:30:40 +01006967#if (defined(FEAT_XCLIPBOARD) && defined(USE_SYSTEM)) || defined(PROTO)
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01006968 int
Bram Moolenaar0554fa42019-06-14 21:36:54 +02006969clip_gtk_owner_exists(Clipboard_T *cbd)
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01006970{
6971 return gdk_selection_owner_get(cbd->gtk_sel_atom) != NULL;
6972}
Bram Moolenaar113e1072019-01-20 15:30:40 +01006973#endif
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01006974
Bram Moolenaar071d4272004-06-13 20:20:40 +00006975
6976#if defined(FEAT_MENU) || defined(PROTO)
6977/*
6978 * Make a menu item appear either active or not active (grey or not grey).
6979 */
6980 void
6981gui_mch_menu_grey(vimmenu_T *menu, int grey)
6982{
6983 if (menu->id == NULL)
6984 return;
6985
6986 if (menu_is_separator(menu->name))
6987 grey = TRUE;
6988
6989 gui_mch_menu_hidden(menu, FALSE);
Bram Moolenaar30613902019-12-01 22:11:18 +01006990 // Be clever about bitfields versus true booleans here!
Bram Moolenaar98921892016-02-23 17:14:37 +01006991 if (!gtk_widget_get_sensitive(menu->id) == !grey)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006992 {
6993 gtk_widget_set_sensitive(menu->id, !grey);
6994 gui_mch_update();
6995 }
6996}
6997
6998/*
6999 * Make menu item hidden or not hidden.
7000 */
7001 void
7002gui_mch_menu_hidden(vimmenu_T *menu, int hidden)
7003{
7004 if (menu->id == 0)
7005 return;
7006
7007 if (hidden)
7008 {
Bram Moolenaar98921892016-02-23 17:14:37 +01007009 if (gtk_widget_get_visible(menu->id))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007010 {
7011 gtk_widget_hide(menu->id);
7012 gui_mch_update();
7013 }
7014 }
7015 else
7016 {
Bram Moolenaar98921892016-02-23 17:14:37 +01007017 if (!gtk_widget_get_visible(menu->id))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007018 {
7019 gtk_widget_show(menu->id);
7020 gui_mch_update();
7021 }
7022 }
7023}
7024
7025/*
7026 * This is called after setting all the menus to grey/hidden or not.
7027 */
7028 void
7029gui_mch_draw_menubar(void)
7030{
Bram Moolenaar30613902019-12-01 22:11:18 +01007031 // just make sure that the visual changes get effect immediately
Bram Moolenaar071d4272004-06-13 20:20:40 +00007032 gui_mch_update();
7033}
Bram Moolenaar30613902019-12-01 22:11:18 +01007034#endif // FEAT_MENU
Bram Moolenaar071d4272004-06-13 20:20:40 +00007035
7036/*
7037 * Scrollbar stuff.
7038 */
7039 void
7040gui_mch_enable_scrollbar(scrollbar_T *sb, int flag)
7041{
7042 if (sb->id == NULL)
7043 return;
7044
Bram Moolenaar98921892016-02-23 17:14:37 +01007045 gtk_widget_set_visible(sb->id, flag);
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00007046 update_window_manager_hints(0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007047}
7048
7049
7050/*
7051 * Return the RGB value of a pixel as long.
7052 */
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02007053 guicolor_T
Bram Moolenaar071d4272004-06-13 20:20:40 +00007054gui_mch_get_rgb(guicolor_T pixel)
7055{
Bram Moolenaar98921892016-02-23 17:14:37 +01007056#if GTK_CHECK_VERSION(3,0,0)
Bram Moolenaar36edf062016-07-21 22:10:12 +02007057 return (long_u)pixel;
Bram Moolenaar98921892016-02-23 17:14:37 +01007058#else
Bram Moolenaar36edf062016-07-21 22:10:12 +02007059 GdkColor color;
7060
Bram Moolenaar071d4272004-06-13 20:20:40 +00007061 gdk_colormap_query_color(gtk_widget_get_colormap(gui.drawarea),
7062 (unsigned long)pixel, &color);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007063
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02007064 return (guicolor_T)(
7065 (((unsigned)color.red & 0xff00) << 8)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007066 | ((unsigned)color.green & 0xff00)
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02007067 | (((unsigned)color.blue & 0xff00) >> 8));
Bram Moolenaar36edf062016-07-21 22:10:12 +02007068#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007069}
7070
7071/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00007072 * Get current mouse coordinates in text window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007073 */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00007074 void
7075gui_mch_getmouse(int *x, int *y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007076{
Bram Moolenaar98921892016-02-23 17:14:37 +01007077 gui_gtk_get_pointer(gui.drawarea, x, y, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007078}
7079
7080 void
7081gui_mch_setmouse(int x, int y)
7082{
Bram Moolenaar30613902019-12-01 22:11:18 +01007083 // Sorry for the Xlib call, but we can't avoid it, since there is no
7084 // internal GDK mechanism present to accomplish this. (and for good
7085 // reason...)
Bram Moolenaar98921892016-02-23 17:14:37 +01007086 XWarpPointer(GDK_WINDOW_XDISPLAY(gtk_widget_get_window(gui.drawarea)),
7087 (Window)0, GDK_WINDOW_XID(gtk_widget_get_window(gui.drawarea)),
7088 0, 0, 0U, 0U, x, y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007089}
7090
7091
7092#ifdef FEAT_MOUSESHAPE
Bram Moolenaar30613902019-12-01 22:11:18 +01007093// The last set mouse pointer shape is remembered, to be used when it goes
7094// from hidden to not hidden.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007095static int last_shape = 0;
7096#endif
7097
7098/*
7099 * Use the blank mouse pointer or not.
7100 *
7101 * hide: TRUE = use blank ptr, FALSE = use parent ptr
7102 */
7103 void
7104gui_mch_mousehide(int hide)
7105{
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00007106 if (gui.pointer_hidden == hide)
7107 return;
7108
7109 gui.pointer_hidden = hide;
7110 if (gtk_widget_get_window(gui.drawarea) && gui.blank_pointer != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007111 {
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00007112 if (hide)
7113 gdk_window_set_cursor(gtk_widget_get_window(gui.drawarea),
7114 gui.blank_pointer);
7115 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007116#ifdef FEAT_MOUSESHAPE
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00007117 mch_set_mouse_shape(last_shape);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007118#else
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00007119 gdk_window_set_cursor(gtk_widget_get_window(gui.drawarea), NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007120#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007121 }
7122}
7123
7124#if defined(FEAT_MOUSESHAPE) || defined(PROTO)
7125
Bram Moolenaar30613902019-12-01 22:11:18 +01007126// Table for shape IDs. Keep in sync with the mshape_names[] table in
7127// misc2.c!
Bram Moolenaar071d4272004-06-13 20:20:40 +00007128static const int mshape_ids[] =
7129{
Bram Moolenaar30613902019-12-01 22:11:18 +01007130 GDK_LEFT_PTR, // arrow
7131 GDK_CURSOR_IS_PIXMAP, // blank
7132 GDK_XTERM, // beam
7133 GDK_SB_V_DOUBLE_ARROW, // updown
7134 GDK_SIZING, // udsizing
7135 GDK_SB_H_DOUBLE_ARROW, // leftright
7136 GDK_SIZING, // lrsizing
7137 GDK_WATCH, // busy
7138 GDK_X_CURSOR, // no
7139 GDK_CROSSHAIR, // crosshair
7140 GDK_HAND1, // hand1
7141 GDK_HAND2, // hand2
7142 GDK_PENCIL, // pencil
7143 GDK_QUESTION_ARROW, // question
7144 GDK_RIGHT_PTR, // right-arrow
7145 GDK_CENTER_PTR, // up-arrow
7146 GDK_LEFT_PTR // last one
Bram Moolenaar071d4272004-06-13 20:20:40 +00007147};
7148
7149 void
7150mch_set_mouse_shape(int shape)
7151{
7152 int id;
7153 GdkCursor *c;
7154
Bram Moolenaar98921892016-02-23 17:14:37 +01007155 if (gtk_widget_get_window(gui.drawarea) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007156 return;
7157
7158 if (shape == MSHAPE_HIDE || gui.pointer_hidden)
Bram Moolenaar98921892016-02-23 17:14:37 +01007159 gdk_window_set_cursor(gtk_widget_get_window(gui.drawarea),
7160 gui.blank_pointer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007161 else
7162 {
7163 if (shape >= MSHAPE_NUMBERED)
7164 {
7165 id = shape - MSHAPE_NUMBERED;
7166 if (id >= GDK_LAST_CURSOR)
7167 id = GDK_LEFT_PTR;
7168 else
Bram Moolenaar30613902019-12-01 22:11:18 +01007169 id &= ~1; // they are always even (why?)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007170 }
K.Takataeeec2542021-06-02 13:28:16 +02007171 else if (shape < (int)ARRAY_LENGTH(mshape_ids))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007172 id = mshape_ids[shape];
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00007173 else
7174 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007175 c = gdk_cursor_new_for_display(
Bram Moolenaarb71ec9f2005-01-25 22:22:02 +00007176 gtk_widget_get_display(gui.drawarea), (GdkCursorType)id);
Bram Moolenaar98921892016-02-23 17:14:37 +01007177 gdk_window_set_cursor(gtk_widget_get_window(gui.drawarea), c);
Bram Moolenaar98921892016-02-23 17:14:37 +01007178# if GTK_CHECK_VERSION(3,0,0)
7179 g_object_unref(G_OBJECT(c));
7180# else
Bram Moolenaar30613902019-12-01 22:11:18 +01007181 gdk_cursor_destroy(c); // Unref, actually. Bloody GTK+ 1.
Bram Moolenaar98921892016-02-23 17:14:37 +01007182# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007183 }
7184 if (shape != MSHAPE_HIDE)
7185 last_shape = shape;
7186}
Bram Moolenaar30613902019-12-01 22:11:18 +01007187#endif // FEAT_MOUSESHAPE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007188
7189
7190#if defined(FEAT_SIGN_ICONS) || defined(PROTO)
7191/*
7192 * Signs are currently always 2 chars wide. With GTK+ 2, the image will be
7193 * scaled down if the current font is not big enough, or scaled up if the image
7194 * size is less than 3/4 of the maximum sign size. With GTK+ 1, the pixmap
7195 * will be cut off if the current font is not big enough, or centered if it's
7196 * too small.
7197 */
7198# define SIGN_WIDTH (2 * gui.char_width)
7199# define SIGN_HEIGHT (gui.char_height)
7200# define SIGN_ASPECT ((double)SIGN_HEIGHT / (double)SIGN_WIDTH)
7201
Bram Moolenaar071d4272004-06-13 20:20:40 +00007202 void
7203gui_mch_drawsign(int row, int col, int typenr)
7204{
7205 GdkPixbuf *sign;
7206
7207 sign = (GdkPixbuf *)sign_get_image(typenr);
7208
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00007209 if (sign == NULL || gui.drawarea == NULL
7210 || gtk_widget_get_window(gui.drawarea) == NULL)
7211 return;
7212
7213 int width;
7214 int height;
7215 int xoffset;
7216 int yoffset;
7217 int need_scale;
7218
7219 width = gdk_pixbuf_get_width(sign);
7220 height = gdk_pixbuf_get_height(sign);
7221 /*
7222 * Decide whether we need to scale. Allow one pixel of border
7223 * width to be cut off, in order to avoid excessive scaling for
7224 * tiny differences in font size.
7225 * Do scale to fit the height to avoid gaps because of linespacing.
7226 */
7227 need_scale = (width > SIGN_WIDTH + 2
7228 || height != SIGN_HEIGHT
7229 || (width < 3 * SIGN_WIDTH / 4
7230 && height < 3 * SIGN_HEIGHT / 4));
7231 if (need_scale)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007232 {
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00007233 double aspect;
7234 int w = width;
7235 int h = height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007236
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00007237 // Keep the original aspect ratio
7238 aspect = (double)height / (double)width;
7239 width = (double)SIGN_WIDTH * SIGN_ASPECT / aspect;
7240 width = MIN(width, SIGN_WIDTH);
7241 if (((double)(MAX(height, SIGN_HEIGHT)) /
7242 (double)(MIN(height, SIGN_HEIGHT))) < 1.15)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007243 {
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00007244 // Change the aspect ratio by at most 15% to fill the
7245 // available space completely.
7246 height = (double)SIGN_HEIGHT * SIGN_ASPECT / aspect;
7247 height = MIN(height, SIGN_HEIGHT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007248 }
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00007249 else
7250 height = (double)width * aspect;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007251
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00007252 if (w == width && h == height)
7253 {
7254 // no change in dimensions; don't decrease reference counter
7255 // (below)
7256 need_scale = FALSE;
7257 }
7258 else
7259 {
7260 // This doesn't seem to be worth caching, and doing so would
7261 // complicate the code quite a bit.
7262 sign = gdk_pixbuf_scale_simple(sign, width, height,
7263 GDK_INTERP_BILINEAR);
7264 if (sign == NULL)
7265 return; // out of memory
7266 }
7267 }
7268
7269 // The origin is the upper-left corner of the pixmap. Therefore
7270 // these offset may become negative if the pixmap is smaller than
7271 // the 2x1 cells reserved for the sign icon.
7272 xoffset = (width - SIGN_WIDTH) / 2;
7273 yoffset = (height - SIGN_HEIGHT) / 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007274
Bram Moolenaar98921892016-02-23 17:14:37 +01007275# if GTK_CHECK_VERSION(3,0,0)
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00007276 {
7277 cairo_t *cr;
7278 cairo_surface_t *bg_surf;
7279 cairo_t *bg_cr;
7280 cairo_surface_t *sign_surf;
7281 cairo_t *sign_cr;
Bram Moolenaar98921892016-02-23 17:14:37 +01007282
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00007283 cr = cairo_create(gui.surface);
Bram Moolenaar98921892016-02-23 17:14:37 +01007284
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00007285 bg_surf = cairo_surface_create_similar(gui.surface,
7286 cairo_surface_get_content(gui.surface),
7287 SIGN_WIDTH, SIGN_HEIGHT);
7288 bg_cr = cairo_create(bg_surf);
7289 cairo_set_source_rgba(bg_cr,
7290 gui.bgcolor->red, gui.bgcolor->green, gui.bgcolor->blue,
7291 gui.bgcolor->alpha);
7292 cairo_paint(bg_cr);
Bram Moolenaar98921892016-02-23 17:14:37 +01007293
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00007294 sign_surf = cairo_surface_create_similar(gui.surface,
7295 cairo_surface_get_content(gui.surface),
7296 SIGN_WIDTH, SIGN_HEIGHT);
7297 sign_cr = cairo_create(sign_surf);
7298 gdk_cairo_set_source_pixbuf(sign_cr, sign, -xoffset, -yoffset);
7299 cairo_paint(sign_cr);
Bram Moolenaar98921892016-02-23 17:14:37 +01007300
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00007301 cairo_set_operator(sign_cr, CAIRO_OPERATOR_DEST_OVER);
7302 cairo_set_source_surface(sign_cr, bg_surf, 0, 0);
7303 cairo_paint(sign_cr);
Bram Moolenaar98921892016-02-23 17:14:37 +01007304
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00007305 cairo_set_source_surface(cr, sign_surf, FILL_X(col), FILL_Y(row));
7306 cairo_paint(cr);
Bram Moolenaar98921892016-02-23 17:14:37 +01007307
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00007308 cairo_destroy(sign_cr);
7309 cairo_surface_destroy(sign_surf);
7310 cairo_destroy(bg_cr);
7311 cairo_surface_destroy(bg_surf);
7312 cairo_destroy(cr);
Bram Moolenaar98921892016-02-23 17:14:37 +01007313
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00007314 gtk_widget_queue_draw_area(gui.drawarea,
7315 FILL_X(col), FILL_Y(col), width, height);
Bram Moolenaar98921892016-02-23 17:14:37 +01007316
Bram Moolenaar071d4272004-06-13 20:20:40 +00007317 }
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00007318# else // !GTK_CHECK_VERSION(3,0,0)
7319 gdk_gc_set_foreground(gui.text_gc, gui.bgcolor);
7320
7321 gdk_draw_rectangle(gui.drawarea->window,
7322 gui.text_gc,
7323 TRUE,
7324 FILL_X(col),
7325 FILL_Y(row),
7326 SIGN_WIDTH,
7327 SIGN_HEIGHT);
7328
7329 gdk_pixbuf_render_to_drawable_alpha(sign,
7330 gui.drawarea->window,
7331 MAX(0, xoffset),
7332 MAX(0, yoffset),
7333 FILL_X(col) - MIN(0, xoffset),
7334 FILL_Y(row) - MIN(0, yoffset),
7335 MIN(width, SIGN_WIDTH),
7336 MIN(height, SIGN_HEIGHT),
7337 GDK_PIXBUF_ALPHA_BILEVEL,
7338 127,
7339 GDK_RGB_DITHER_NORMAL,
7340 0, 0);
7341# endif // !GTK_CHECK_VERSION(3,0,0)
7342 if (need_scale)
7343 g_object_unref(sign);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007344}
7345
7346 void *
7347gui_mch_register_sign(char_u *signfile)
7348{
7349 if (signfile[0] != NUL && signfile[0] != '-' && gui.in_use)
7350 {
7351 GdkPixbuf *sign;
7352 GError *error = NULL;
7353 char_u *message;
7354
7355 sign = gdk_pixbuf_new_from_file((const char *)signfile, &error);
7356
7357 if (error == NULL)
7358 return sign;
7359
7360 message = (char_u *)error->message;
7361
7362 if (message != NULL && input_conv.vc_type != CONV_NONE)
7363 message = string_convert(&input_conv, message, NULL);
7364
7365 if (message != NULL)
7366 {
Bram Moolenaar30613902019-12-01 22:11:18 +01007367 // The error message is already translated and will be more
7368 // descriptive than anything we could possibly do ourselves.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007369 semsg("E255: %s", message);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007370
7371 if (input_conv.vc_type != CONV_NONE)
7372 vim_free(message);
7373 }
7374 g_error_free(error);
7375 }
7376
7377 return NULL;
7378}
7379
7380 void
7381gui_mch_destroy_sign(void *sign)
7382{
7383 if (sign != NULL)
7384 g_object_unref(sign);
7385}
7386
Bram Moolenaar30613902019-12-01 22:11:18 +01007387#endif // FEAT_SIGN_ICONS