blob: 4d201fcaede6655921905f838662aafc78709181 [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}
lilydjwg6e0a18f2024-01-29 20:54:28 +0100796
797# if GTK_CHECK_VERSION(3,10,0)
798 static gboolean
799scale_factor_event(GtkWidget *widget,
800 GParamSpec* pspec UNUSED,
801 gpointer user_data UNUSED)
802{
803 if (gui.surface != NULL)
804 cairo_surface_destroy(gui.surface);
805
806 int w, h;
807 gtk_window_get_size(GTK_WINDOW(gui.mainwin), &w, &h);
808 gui.surface = gdk_window_create_similar_surface(
809 gtk_widget_get_window(widget),
810 CAIRO_CONTENT_COLOR_ALPHA,
811 w, h);
812
813 int usable_height = h;
814 if (gtk_socket_id != 0)
815 usable_height -= (gui.char_height - (gui.char_height/2)); // sic.
816
817 gui_gtk_form_freeze(GTK_FORM(gui.formwin));
818 gui.force_redraw = 1;
819 gui_resize_shell(w, usable_height);
820 gui_gtk_form_thaw(GTK_FORM(gui.formwin));
821
822 return TRUE;
823}
824# endif // GTK_CHECK_VERSION(3,10,0)
825
Bram Moolenaar30613902019-12-01 22:11:18 +0100826#else // !GTK_CHECK_VERSION(3,0,0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000827 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +0000828expose_event(GtkWidget *widget UNUSED,
829 GdkEventExpose *event,
830 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000831{
Bram Moolenaar30613902019-12-01 22:11:18 +0100832 // Skip this when the GUI isn't set up yet, will redraw later.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000833 if (gui.starting)
834 return FALSE;
835
Bram Moolenaar30613902019-12-01 22:11:18 +0100836 out_flush(); // make sure all output has been processed
Bram Moolenaar071d4272004-06-13 20:20:40 +0000837 gui_redraw(event->area.x, event->area.y,
838 event->area.width, event->area.height);
839
Bram Moolenaar30613902019-12-01 22:11:18 +0100840 // Clear the border areas if needed
Bram Moolenaar071d4272004-06-13 20:20:40 +0000841 if (event->area.x < FILL_X(0))
842 gdk_window_clear_area(gui.drawarea->window, 0, 0, FILL_X(0), 0);
843 if (event->area.y < FILL_Y(0))
844 gdk_window_clear_area(gui.drawarea->window, 0, 0, 0, FILL_Y(0));
845 if (event->area.x > FILL_X(Columns))
846 gdk_window_clear_area(gui.drawarea->window,
847 FILL_X((int)Columns), 0, 0, 0);
848 if (event->area.y > FILL_Y(Rows))
849 gdk_window_clear_area(gui.drawarea->window, 0, FILL_Y((int)Rows), 0, 0);
850
851 return FALSE;
852}
Bram Moolenaar30613902019-12-01 22:11:18 +0100853#endif // !GTK_CHECK_VERSION(3,0,0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000854
855#ifdef FEAT_CLIENTSERVER
856/*
857 * Handle changes to the "Comm" property
858 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000859 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +0000860property_event(GtkWidget *widget,
861 GdkEventProperty *event,
862 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000863{
864 if (event->type == GDK_PROPERTY_NOTIFY
865 && event->state == (int)GDK_PROPERTY_NEW_VALUE
Bram Moolenaar98921892016-02-23 17:14:37 +0100866 && GDK_WINDOW_XID(event->window) == commWindow
Bram Moolenaar071d4272004-06-13 20:20:40 +0000867 && GET_X_ATOM(event->atom) == commProperty)
868 {
869 XEvent xev;
870
Bram Moolenaar30613902019-12-01 22:11:18 +0100871 // Translate to XLib
Bram Moolenaar071d4272004-06-13 20:20:40 +0000872 xev.xproperty.type = PropertyNotify;
873 xev.xproperty.atom = commProperty;
874 xev.xproperty.window = commWindow;
875 xev.xproperty.state = PropertyNewValue;
Bram Moolenaar98921892016-02-23 17:14:37 +0100876 serverEventProc(GDK_WINDOW_XDISPLAY(gtk_widget_get_window(widget)),
Ernie Rael9f53e7b2022-04-17 18:27:49 +0100877 &xev, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000878 }
879 return FALSE;
880}
Bram Moolenaar30613902019-12-01 22:11:18 +0100881#endif // defined(FEAT_CLIENTSERVER)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000882
Bram Moolenaar7ebf4e12018-08-07 20:01:40 +0200883/*
884 * Handle changes to the "Xft/DPI" setting
885 */
886 static void
887gtk_settings_xft_dpi_changed_cb(GtkSettings *gtk_settings UNUSED,
Ernie Rael9f53e7b2022-04-17 18:27:49 +0100888 GParamSpec *pspec UNUSED,
889 gpointer data UNUSED)
Bram Moolenaar7ebf4e12018-08-07 20:01:40 +0200890{
891 // Create a new PangoContext for this screen, and initialize it
892 // with the current font if necessary.
893 if (gui.text_context != NULL)
894 g_object_unref(gui.text_context);
895
896 gui.text_context = gtk_widget_create_pango_context(gui.mainwin);
897 pango_context_set_base_dir(gui.text_context, PANGO_DIRECTION_LTR);
898
899 if (gui.norm_font != NULL)
900 {
901 // force default font
902 gui_mch_init_font(*p_guifont == NUL ? NULL : p_guifont, FALSE);
903 gui_set_shellsize(TRUE, FALSE, RESIZE_BOTH);
904 }
905}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000906
Bram Moolenaar4ab79682017-08-27 14:50:47 +0200907typedef gboolean timeout_cb_type;
Bram Moolenaar4ab79682017-08-27 14:50:47 +0200908
909/*
910 * Start a timer that will invoke the specified callback.
911 * Returns the ID of the timer.
912 */
913 static guint
914timeout_add(int time, timeout_cb_type (*callback)(gpointer), int *flagp)
915{
Bram Moolenaar4ab79682017-08-27 14:50:47 +0200916 return g_timeout_add((guint)time, (GSourceFunc)callback, flagp);
Bram Moolenaar4ab79682017-08-27 14:50:47 +0200917}
918
919 static void
920timeout_remove(guint timer)
921{
Bram Moolenaar4ab79682017-08-27 14:50:47 +0200922 g_source_remove(timer);
Bram Moolenaar4ab79682017-08-27 14:50:47 +0200923}
924
925
Bram Moolenaar30613902019-12-01 22:11:18 +0100926/////////////////////////////////////////////////////////////////////////////
927// Focus handlers:
Bram Moolenaar071d4272004-06-13 20:20:40 +0000928
929
930/*
931 * This is a simple state machine:
932 * BLINK_NONE not blinking at all
933 * BLINK_OFF blinking, cursor is not shown
934 * BLINK_ON blinking, cursor is shown
935 */
936
937#define BLINK_NONE 0
938#define BLINK_OFF 1
939#define BLINK_ON 2
940
941static int blink_state = BLINK_NONE;
942static long_u blink_waittime = 700;
943static long_u blink_ontime = 400;
944static long_u blink_offtime = 250;
945static guint blink_timer = 0;
946
Bram Moolenaar703a8042016-06-04 16:24:32 +0200947 int
948gui_mch_is_blinking(void)
949{
950 return blink_state != BLINK_NONE;
951}
952
Bram Moolenaar9d5d3c92016-07-07 16:43:02 +0200953 int
954gui_mch_is_blink_off(void)
955{
956 return blink_state == BLINK_OFF;
957}
958
Bram Moolenaar071d4272004-06-13 20:20:40 +0000959 void
960gui_mch_set_blinking(long waittime, long on, long off)
961{
962 blink_waittime = waittime;
963 blink_ontime = on;
964 blink_offtime = off;
965}
966
967/*
968 * Stop the cursor blinking. Show the cursor if it wasn't shown.
969 */
970 void
Bram Moolenaar1dd45fb2018-01-31 21:10:01 +0100971gui_mch_stop_blink(int may_call_gui_update_cursor)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000972{
973 if (blink_timer)
974 {
Bram Moolenaar4ab79682017-08-27 14:50:47 +0200975 timeout_remove(blink_timer);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000976 blink_timer = 0;
977 }
Bram Moolenaar1dd45fb2018-01-31 21:10:01 +0100978 if (blink_state == BLINK_OFF && may_call_gui_update_cursor)
Bram Moolenaarda3a77d2016-07-10 23:16:09 +0200979 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000980 gui_update_cursor(TRUE, FALSE);
presuku9459b8d2021-11-16 20:03:56 +0000981#if !GTK_CHECK_VERSION(3,0,0)
Bram Moolenaarda3a77d2016-07-10 23:16:09 +0200982 gui_mch_flush();
presuku9459b8d2021-11-16 20:03:56 +0000983#endif
Bram Moolenaarda3a77d2016-07-10 23:16:09 +0200984 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000985 blink_state = BLINK_NONE;
986}
987
Bram Moolenaar4ab79682017-08-27 14:50:47 +0200988 static timeout_cb_type
Bram Moolenaarb85cb212009-05-17 14:24:23 +0000989blink_cb(gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000990{
991 if (blink_state == BLINK_ON)
992 {
993 gui_undraw_cursor();
994 blink_state = BLINK_OFF;
Bram Moolenaar4ab79682017-08-27 14:50:47 +0200995 blink_timer = timeout_add(blink_offtime, blink_cb, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000996 }
997 else
998 {
999 gui_update_cursor(TRUE, FALSE);
1000 blink_state = BLINK_ON;
Bram Moolenaar4ab79682017-08-27 14:50:47 +02001001 blink_timer = timeout_add(blink_ontime, blink_cb, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001002 }
presuku9459b8d2021-11-16 20:03:56 +00001003#if !GTK_CHECK_VERSION(3,0,0)
Bram Moolenaarda3a77d2016-07-10 23:16:09 +02001004 gui_mch_flush();
presuku9459b8d2021-11-16 20:03:56 +00001005#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001006
Bram Moolenaar30613902019-12-01 22:11:18 +01001007 return FALSE; // don't happen again
Bram Moolenaar071d4272004-06-13 20:20:40 +00001008}
1009
1010/*
1011 * Start the cursor blinking. If it was already blinking, this restarts the
1012 * waiting time and shows the cursor.
1013 */
1014 void
1015gui_mch_start_blink(void)
1016{
1017 if (blink_timer)
Bram Moolenaar7bcdb7d2014-04-06 21:08:45 +02001018 {
Bram Moolenaar4ab79682017-08-27 14:50:47 +02001019 timeout_remove(blink_timer);
Bram Moolenaar7bcdb7d2014-04-06 21:08:45 +02001020 blink_timer = 0;
1021 }
Bram Moolenaar30613902019-12-01 22:11:18 +01001022 // Only switch blinking on if none of the times is zero
Bram Moolenaar071d4272004-06-13 20:20:40 +00001023 if (blink_waittime && blink_ontime && blink_offtime && gui.in_focus)
1024 {
Bram Moolenaar4ab79682017-08-27 14:50:47 +02001025 blink_timer = timeout_add(blink_waittime, blink_cb, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001026 blink_state = BLINK_ON;
1027 gui_update_cursor(TRUE, FALSE);
presuku9459b8d2021-11-16 20:03:56 +00001028#if !GTK_CHECK_VERSION(3,0,0)
Bram Moolenaarda3a77d2016-07-10 23:16:09 +02001029 gui_mch_flush();
presuku9459b8d2021-11-16 20:03:56 +00001030#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001031 }
1032}
1033
Bram Moolenaar071d4272004-06-13 20:20:40 +00001034 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001035enter_notify_event(GtkWidget *widget UNUSED,
1036 GdkEventCrossing *event UNUSED,
1037 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001038{
1039 if (blink_state == BLINK_NONE)
1040 gui_mch_start_blink();
1041
Bram Moolenaar30613902019-12-01 22:11:18 +01001042 // make sure keyboard input goes there
Bram Moolenaar98921892016-02-23 17:14:37 +01001043 if (gtk_socket_id == 0 || !gtk_widget_has_focus(gui.drawarea))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001044 gtk_widget_grab_focus(gui.drawarea);
1045
1046 return FALSE;
1047}
1048
Bram Moolenaar071d4272004-06-13 20:20:40 +00001049 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001050leave_notify_event(GtkWidget *widget UNUSED,
1051 GdkEventCrossing *event UNUSED,
1052 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001053{
1054 if (blink_state != BLINK_NONE)
Bram Moolenaar1dd45fb2018-01-31 21:10:01 +01001055 gui_mch_stop_blink(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001056
1057 return FALSE;
1058}
1059
Bram Moolenaar071d4272004-06-13 20:20:40 +00001060 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001061focus_in_event(GtkWidget *widget,
1062 GdkEventFocus *event UNUSED,
1063 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001064{
1065 gui_focus_change(TRUE);
1066
1067 if (blink_state == BLINK_NONE)
1068 gui_mch_start_blink();
1069
Bram Moolenaar30613902019-12-01 22:11:18 +01001070 // make sure keyboard input goes to the draw area (if this is focus for a
1071 // window)
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00001072 if (widget != gui.drawarea)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001073 gtk_widget_grab_focus(gui.drawarea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001074
1075 return TRUE;
1076}
1077
Bram Moolenaar071d4272004-06-13 20:20:40 +00001078 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001079focus_out_event(GtkWidget *widget UNUSED,
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001080 GdkEventFocus *event UNUSED,
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001081 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001082{
1083 gui_focus_change(FALSE);
1084
1085 if (blink_state != BLINK_NONE)
Bram Moolenaar1dd45fb2018-01-31 21:10:01 +01001086 gui_mch_stop_blink(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001087
1088 return TRUE;
1089}
1090
1091
Bram Moolenaar071d4272004-06-13 20:20:40 +00001092/*
1093 * Translate a GDK key value to UTF-8 independently of the current locale.
1094 * The output is written to string, which must have room for at least 6 bytes
1095 * plus the NUL terminator. Returns the length in bytes.
1096 *
Bram Moolenaarf4ae6b22020-05-30 19:52:46 +02001097 * event->string is evil; see here why:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001098 * http://developer.gnome.org/doc/API/2.0/gdk/gdk-Event-Structures.html#GdkEventKey
1099 */
1100 static int
Bram Moolenaarf4ae6b22020-05-30 19:52:46 +02001101keyval_to_string(unsigned int keyval, char_u *string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001102{
1103 int len;
1104 guint32 uc;
1105
1106 uc = gdk_keyval_to_unicode(keyval);
1107 if (uc != 0)
1108 {
Bram Moolenaarf4ae6b22020-05-30 19:52:46 +02001109 // Translate a normal key to UTF-8. This doesn't work for dead
1110 // keys of course, you _have_ to use an input method for that.
1111 len = utf_char2bytes((int)uc, string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001112 }
1113 else
1114 {
Bram Moolenaar30613902019-12-01 22:11:18 +01001115 // Translate keys which are represented by ASCII control codes in Vim.
1116 // There are only a few of those; most control keys are translated to
1117 // special terminal-like control sequences.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001118 len = 1;
1119 switch (keyval)
1120 {
1121 case GDK_Tab: case GDK_KP_Tab: case GDK_ISO_Left_Tab:
1122 string[0] = TAB;
1123 break;
1124 case GDK_Linefeed:
1125 string[0] = NL;
1126 break;
1127 case GDK_Return: case GDK_ISO_Enter: case GDK_3270_Enter:
1128 string[0] = CAR;
1129 break;
1130 case GDK_Escape:
1131 string[0] = ESC;
1132 break;
1133 default:
1134 len = 0;
1135 break;
1136 }
1137 }
1138 string[len] = NUL;
1139
1140 return len;
1141}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001142
Bram Moolenaar19a09a12005-03-04 23:39:37 +00001143 static int
1144modifiers_gdk2vim(guint state)
1145{
1146 int modifiers = 0;
1147
1148 if (state & GDK_SHIFT_MASK)
1149 modifiers |= MOD_MASK_SHIFT;
1150 if (state & GDK_CONTROL_MASK)
1151 modifiers |= MOD_MASK_CTRL;
1152 if (state & GDK_MOD1_MASK)
1153 modifiers |= MOD_MASK_ALT;
Bram Moolenaar580061a2010-08-13 13:57:13 +02001154#if GTK_CHECK_VERSION(2,10,0)
Casey Tucker92e90a12024-01-25 22:44:00 +01001155 if (state & GDK_META_MASK)
1156 modifiers |= MOD_MASK_META;
Bram Moolenaar30bb4142010-05-17 22:07:15 +02001157 if (state & GDK_SUPER_MASK)
Casey Tucker92e90a12024-01-25 22:44:00 +01001158 modifiers |= MOD_MASK_CMD;
1159#else
Bram Moolenaar19a09a12005-03-04 23:39:37 +00001160 if (state & GDK_MOD4_MASK)
Casey Tucker92e90a12024-01-25 22:44:00 +01001161 modifiers |= MOD_MASK_CMD;
1162#endif
Bram Moolenaar19a09a12005-03-04 23:39:37 +00001163
1164 return modifiers;
1165}
1166
1167 static int
1168modifiers_gdk2mouse(guint state)
1169{
1170 int modifiers = 0;
1171
1172 if (state & GDK_SHIFT_MASK)
1173 modifiers |= MOUSE_SHIFT;
1174 if (state & GDK_CONTROL_MASK)
1175 modifiers |= MOUSE_CTRL;
1176 if (state & GDK_MOD1_MASK)
1177 modifiers |= MOUSE_ALT;
1178
1179 return modifiers;
1180}
1181
Bram Moolenaar071d4272004-06-13 20:20:40 +00001182/*
1183 * Main keyboard handler:
1184 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001185 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001186key_press_event(GtkWidget *widget UNUSED,
1187 GdkEventKey *event,
1188 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001189{
Bram Moolenaar30613902019-12-01 22:11:18 +01001190 // For GTK+ 2 we know for sure how large the string might get.
1191 // (That is, up to 6 bytes + NUL + CSI escapes + safety measure.)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001192 char_u string[32], string2[32];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001193 guint key_sym;
1194 int len;
1195 int i;
1196 int modifiers;
1197 int key;
1198 guint state;
1199 char_u *s, *d;
Anton Sharonov4dd92522022-07-04 10:47:31 +01001200 int ctrl_prefix_added = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001201
Bram Moolenaar20892c12011-06-26 04:49:00 +02001202 gui.event_time = event->time;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001203 key_sym = event->keyval;
1204 state = event->state;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001205
1206#ifdef FEAT_XIM
1207 if (xim_queue_key_press_event(event, TRUE))
1208 return TRUE;
1209#endif
1210
Bram Moolenaar071d4272004-06-13 20:20:40 +00001211#ifdef SunXK_F36
1212 /*
1213 * These keys have bogus lookup strings, and trapping them here is
1214 * easier than trying to XRebindKeysym() on them with every possible
1215 * combination of modifiers.
1216 */
1217 if (key_sym == SunXK_F36 || key_sym == SunXK_F37)
1218 len = 0;
1219 else
1220#endif
1221 {
Bram Moolenaarf4ae6b22020-05-30 19:52:46 +02001222 len = keyval_to_string(key_sym, string2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001223
Bram Moolenaar30613902019-12-01 22:11:18 +01001224 // Careful: convert_input() doesn't handle the NUL character.
1225 // No need to convert pure ASCII anyway, thus the len > 1 check.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001226 if (len > 1 && input_conv.vc_type != CONV_NONE)
1227 len = convert_input(string2, len, sizeof(string2));
1228
1229 s = string2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001230 d = string;
1231 for (i = 0; i < len; ++i)
1232 {
1233 *d++ = s[i];
1234 if (d[-1] == CSI && d + 2 < string + sizeof(string))
1235 {
Bram Moolenaar30613902019-12-01 22:11:18 +01001236 // Turn CSI into K_CSI.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001237 *d++ = KS_EXTRA;
1238 *d++ = (int)KE_CSI;
1239 }
1240 }
1241 len = d - string;
1242 }
1243
Bram Moolenaar30613902019-12-01 22:11:18 +01001244 // Shift-Tab results in Left_Tab, but we want <S-Tab>
Bram Moolenaar071d4272004-06-13 20:20:40 +00001245 if (key_sym == GDK_ISO_Left_Tab)
1246 {
1247 key_sym = GDK_Tab;
1248 state |= GDK_SHIFT_MASK;
1249 }
1250
Bram Moolenaar071d4272004-06-13 20:20:40 +00001251#ifdef FEAT_MENU
Bram Moolenaar30613902019-12-01 22:11:18 +01001252 // If there is a menu and 'wak' is "yes", or 'wak' is "menu" and the key
1253 // is a menu shortcut, we ignore everything with the ALT modifier.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001254 if ((state & GDK_MOD1_MASK)
1255 && gui.menu_is_active
1256 && (*p_wak == 'y'
1257 || (*p_wak == 'm'
1258 && len == 1
1259 && gui_is_menu_shortcut(string[0]))))
Bram Moolenaar30613902019-12-01 22:11:18 +01001260 // For GTK2 we return false to signify that we haven't handled the
1261 // keypress, so that gtk will handle the mnemonic or accelerator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001262 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001263#endif
1264
Bram Moolenaarfd615a32020-05-16 14:01:51 +02001265 // We used to apply Alt/Meta to the key here (Mod1Mask), but that is now
1266 // done later, the same as it happens for the terminal. Hopefully that
1267 // works for everybody...
Bram Moolenaar071d4272004-06-13 20:20:40 +00001268
Bram Moolenaar30613902019-12-01 22:11:18 +01001269 // Check for special keys. Also do this when len == 1 (key has an ASCII
1270 // value) to detect backspace, delete and keypad keys.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001271 if (len == 0 || len == 1)
1272 {
1273 for (i = 0; special_keys[i].key_sym != 0; i++)
1274 {
1275 if (special_keys[i].key_sym == key_sym)
1276 {
1277 string[0] = CSI;
1278 string[1] = special_keys[i].code0;
1279 string[2] = special_keys[i].code1;
1280 len = -3;
1281 break;
1282 }
1283 }
1284 }
1285
Bram Moolenaar4c99e622022-07-04 19:58:17 +01001286#ifdef GDK_KEY_dead_circumflex
Anton Sharonov4dd92522022-07-04 10:47:31 +01001287 // Belgian Ctrl+[ workaround
1288 if (len == 0 && key_sym == GDK_KEY_dead_circumflex)
1289 {
1290 string[0] = CSI;
1291 string[1] = KS_MODIFIER;
1292 string[2] = MOD_MASK_CTRL;
1293 string[3] = '[';
1294 len = 4;
1295 add_to_input_buf(string, len);
1296 // workaround has to return here, otherwise our fake string[] entries
1297 // are confusing code downstream
1298 return TRUE;
1299 }
Bram Moolenaar4c99e622022-07-04 19:58:17 +01001300#endif
Anton Sharonov4dd92522022-07-04 10:47:31 +01001301
Bram Moolenaar30613902019-12-01 22:11:18 +01001302 if (len == 0) // Unrecognized key
Bram Moolenaar071d4272004-06-13 20:20:40 +00001303 return TRUE;
1304
Bram Moolenaarfd615a32020-05-16 14:01:51 +02001305 // For some keys a shift modifier is translated into another key code.
1306 if (len == -3)
1307 key = TO_SPECIAL(string[1], string[2]);
1308 else
Bram Moolenaar820ffa52020-06-21 15:09:14 +02001309 {
1310 string[len] = NUL;
1311 key = mb_ptr2char(string);
1312 }
Bram Moolenaarfd615a32020-05-16 14:01:51 +02001313
Bram Moolenaaref6746f2020-06-20 14:43:23 +02001314 // Handle modifiers.
1315 modifiers = modifiers_gdk2vim(state);
1316
1317 // Recognize special keys.
Bram Moolenaarfd615a32020-05-16 14:01:51 +02001318 key = simplify_key(key, &modifiers);
1319 if (key == CSI)
1320 key = K_CSI;
1321 if (IS_SPECIAL(key))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001322 {
Bram Moolenaarfd615a32020-05-16 14:01:51 +02001323 string[0] = CSI;
1324 string[1] = K_SECOND(key);
1325 string[2] = K_THIRD(key);
1326 len = 3;
1327 }
1328 else
1329 {
Bram Moolenaar4e2114e2020-10-07 16:12:37 +02001330 // Some keys need adjustment when the Ctrl modifier is used.
1331 key = may_adjust_key_for_ctrl(modifiers, key);
Bram Moolenaaref6746f2020-06-20 14:43:23 +02001332
Bram Moolenaar4e2114e2020-10-07 16:12:37 +02001333 // May remove the Shift modifier if it's included in the key.
Bram Moolenaaref6746f2020-06-20 14:43:23 +02001334 modifiers = may_remove_shift_modifier(modifiers, key);
1335
Bram Moolenaar820ffa52020-06-21 15:09:14 +02001336 len = mb_char2bytes(key, string);
Bram Moolenaarfd615a32020-05-16 14:01:51 +02001337 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001338
Bram Moolenaarfd615a32020-05-16 14:01:51 +02001339 if (modifiers != 0)
1340 {
1341 string2[0] = CSI;
1342 string2[1] = KS_MODIFIER;
1343 string2[2] = modifiers;
1344 add_to_input_buf(string2, 3);
Anton Sharonov4dd92522022-07-04 10:47:31 +01001345 if (modifiers == 0x4)
1346 ctrl_prefix_added = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001347 }
1348
Bram Moolenaar4e1d8bd2020-08-01 13:10:14 +02001349 // Check if the key interrupts.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001350 {
Bram Moolenaar4e1d8bd2020-08-01 13:10:14 +02001351 int int_ch = check_for_interrupt(key, modifiers);
1352
1353 if (int_ch != NUL)
1354 {
1355 trash_input_buf();
1356 string[0] = int_ch;
1357 len = 1;
1358 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001359 }
1360
Anton Sharonov4dd92522022-07-04 10:47:31 +01001361 // workaround for German keyboard, where instead of '[' char we have code
1362 // sequence of bytes 195, 188 (UTF-8 for "u-umlaut")
1363 if (ctrl_prefix_added && len == 2
1364 && ((int)string[0]) == 195
1365 && ((int)string[1]) == 188)
1366 {
1367 string[0] = 91; // ASCII('[')
1368 len = 1;
1369 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001370 add_to_input_buf(string, len);
1371
Bram Moolenaar30613902019-12-01 22:11:18 +01001372 // blank out the pointer if necessary
Bram Moolenaar071d4272004-06-13 20:20:40 +00001373 if (p_mh)
1374 gui_mch_mousehide(TRUE);
1375
Bram Moolenaar071d4272004-06-13 20:20:40 +00001376 return TRUE;
1377}
1378
Bram Moolenaar98921892016-02-23 17:14:37 +01001379#if defined(FEAT_XIM) || GTK_CHECK_VERSION(3,0,0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001380 static gboolean
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001381key_release_event(GtkWidget *widget UNUSED,
Bram Moolenaar0a748d92022-04-28 15:39:08 +01001382 GdkEventKey *event UNUSED,
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001383 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001384{
Bram Moolenaar98921892016-02-23 17:14:37 +01001385# if defined(FEAT_XIM)
Bram Moolenaar20892c12011-06-26 04:49:00 +02001386 gui.event_time = event->time;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001387 /*
1388 * GTK+ 2 input methods may do fancy stuff on key release events too.
1389 * With the default IM for instance, you can enter any UCS code point
1390 * by holding down CTRL-SHIFT and typing hexadecimal digits.
1391 */
1392 return xim_queue_key_press_event(event, FALSE);
Bram Moolenaar98921892016-02-23 17:14:37 +01001393# else
1394 return TRUE;
1395# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001396}
1397#endif
1398
1399
Bram Moolenaar30613902019-12-01 22:11:18 +01001400/////////////////////////////////////////////////////////////////////////////
1401// Selection handlers:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001402
Bram Moolenaar30613902019-12-01 22:11:18 +01001403// Remember when clip_lose_selection was called from here, we must not call
1404// gtk_selection_owner_set() then.
Bram Moolenaar29dfa5a2018-03-20 21:24:45 +01001405static int in_selection_clear_event = FALSE;
1406
Bram Moolenaar071d4272004-06-13 20:20:40 +00001407 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001408selection_clear_event(GtkWidget *widget UNUSED,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001409 GdkEventSelection *event,
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001410 gpointer user_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001411{
Bram Moolenaar29dfa5a2018-03-20 21:24:45 +01001412 in_selection_clear_event = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001413 if (event->selection == clip_plus.gtk_sel_atom)
1414 clip_lose_selection(&clip_plus);
1415 else
1416 clip_lose_selection(&clip_star);
Bram Moolenaar29dfa5a2018-03-20 21:24:45 +01001417 in_selection_clear_event = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001418
Bram Moolenaar071d4272004-06-13 20:20:40 +00001419 return TRUE;
1420}
1421
Bram Moolenaar30613902019-12-01 22:11:18 +01001422#define RS_NONE 0 // selection_received_cb() not called yet
1423#define RS_OK 1 // selection_received_cb() called and OK
1424#define RS_FAIL 2 // selection_received_cb() called and failed
Bram Moolenaar071d4272004-06-13 20:20:40 +00001425static int received_selection = RS_NONE;
1426
Bram Moolenaar071d4272004-06-13 20:20:40 +00001427 static void
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001428selection_received_cb(GtkWidget *widget UNUSED,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001429 GtkSelectionData *data,
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001430 guint time_ UNUSED,
1431 gpointer user_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001432{
Bram Moolenaar0554fa42019-06-14 21:36:54 +02001433 Clipboard_T *cbd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001434 char_u *text;
1435 char_u *tmpbuf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001436 guchar *tmpbuf_utf8 = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001437 int len;
Bram Moolenaard44347f2011-06-19 01:14:29 +02001438 int motion_type = MAUTO;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001439
Bram Moolenaar98921892016-02-23 17:14:37 +01001440 if (gtk_selection_data_get_selection(data) == clip_plus.gtk_sel_atom)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001441 cbd = &clip_plus;
1442 else
1443 cbd = &clip_star;
1444
Bram Moolenaar98921892016-02-23 17:14:37 +01001445 text = (char_u *)gtk_selection_data_get_data(data);
1446 len = gtk_selection_data_get_length(data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001447
1448 if (text == NULL || len <= 0)
1449 {
1450 received_selection = RS_FAIL;
Bram Moolenaar30613902019-12-01 22:11:18 +01001451 // clip_free_selection(cbd); ???
Bram Moolenaar071d4272004-06-13 20:20:40 +00001452
Bram Moolenaar071d4272004-06-13 20:20:40 +00001453 return;
1454 }
1455
Bram Moolenaar98921892016-02-23 17:14:37 +01001456 if (gtk_selection_data_get_data_type(data) == vim_atom)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001457 {
1458 motion_type = *text++;
1459 --len;
1460 }
Bram Moolenaar98921892016-02-23 17:14:37 +01001461 else if (gtk_selection_data_get_data_type(data) == vimenc_atom)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001462 {
1463 char_u *enc;
1464 vimconv_T conv;
1465
1466 motion_type = *text++;
1467 --len;
1468
1469 enc = text;
1470 text += STRLEN(text) + 1;
1471 len -= text - enc;
1472
Bram Moolenaar30613902019-12-01 22:11:18 +01001473 // If the encoding of the text is different from 'encoding', attempt
1474 // converting it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001475 conv.vc_type = CONV_NONE;
1476 convert_setup(&conv, enc, p_enc);
1477 if (conv.vc_type != CONV_NONE)
1478 {
1479 tmpbuf = string_convert(&conv, text, &len);
1480 if (tmpbuf != NULL)
1481 text = tmpbuf;
1482 convert_setup(&conv, NULL, NULL);
1483 }
1484 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001485
Bram Moolenaar30613902019-12-01 22:11:18 +01001486 // gtk_selection_data_get_text() handles all the nasty details
1487 // and targets and encodings etc. This rocks so hard.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001488 else
1489 {
1490 tmpbuf_utf8 = gtk_selection_data_get_text(data);
1491 if (tmpbuf_utf8 != NULL)
1492 {
1493 len = STRLEN(tmpbuf_utf8);
1494 if (input_conv.vc_type != CONV_NONE)
1495 {
1496 tmpbuf = string_convert(&input_conv, tmpbuf_utf8, &len);
1497 if (tmpbuf != NULL)
1498 text = tmpbuf;
1499 }
1500 else
1501 text = tmpbuf_utf8;
1502 }
Bram Moolenaar3a6eaa52009-06-16 13:23:06 +00001503 else if (len >= 2 && text[0] == 0xff && text[1] == 0xfe)
1504 {
1505 vimconv_T conv;
1506
Bram Moolenaar30613902019-12-01 22:11:18 +01001507 // UTF-16, we get this for HTML
Bram Moolenaar3a6eaa52009-06-16 13:23:06 +00001508 conv.vc_type = CONV_NONE;
1509 convert_setup_ext(&conv, (char_u *)"utf-16le", FALSE, p_enc, TRUE);
1510
1511 if (conv.vc_type != CONV_NONE)
1512 {
1513 text += 2;
1514 len -= 2;
1515 tmpbuf = string_convert(&conv, text, &len);
1516 convert_setup(&conv, NULL, NULL);
1517 }
1518 if (tmpbuf != NULL)
1519 text = tmpbuf;
1520 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001521 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001522
Bram Moolenaar30613902019-12-01 22:11:18 +01001523 // Chop off any trailing NUL bytes. OpenOffice sends these.
Bram Moolenaara76638f2010-06-05 12:49:46 +02001524 while (len > 0 && text[len - 1] == NUL)
1525 --len;
1526
Bram Moolenaar071d4272004-06-13 20:20:40 +00001527 clip_yank_selection(motion_type, text, (long)len, cbd);
1528 received_selection = RS_OK;
1529 vim_free(tmpbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001530 g_free(tmpbuf_utf8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001531}
1532
1533/*
1534 * Prepare our selection data for passing it to the external selection
1535 * client.
1536 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001537 static void
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001538selection_get_cb(GtkWidget *widget UNUSED,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001539 GtkSelectionData *selection_data,
1540 guint info,
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001541 guint time_ UNUSED,
1542 gpointer user_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001543{
1544 char_u *string;
1545 char_u *tmpbuf;
1546 long_u tmplen;
1547 int length;
1548 int motion_type;
1549 GdkAtom type;
Bram Moolenaar0554fa42019-06-14 21:36:54 +02001550 Clipboard_T *cbd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001551
Bram Moolenaar98921892016-02-23 17:14:37 +01001552 if (gtk_selection_data_get_selection(selection_data)
1553 == clip_plus.gtk_sel_atom)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001554 cbd = &clip_plus;
1555 else
1556 cbd = &clip_star;
1557
1558 if (!cbd->owned)
Bram Moolenaar30613902019-12-01 22:11:18 +01001559 return; // Shouldn't ever happen
Bram Moolenaar071d4272004-06-13 20:20:40 +00001560
1561 if (info != (guint)TARGET_STRING
Bram Moolenaar3a6eaa52009-06-16 13:23:06 +00001562 && (!clip_html || info != (guint)TARGET_HTML)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001563 && info != (guint)TARGET_UTF8_STRING
1564 && info != (guint)TARGET_VIMENC
Bram Moolenaar071d4272004-06-13 20:20:40 +00001565 && info != (guint)TARGET_VIM
1566 && info != (guint)TARGET_COMPOUND_TEXT
lilydjwg94ff09a2024-01-29 20:14:01 +01001567 && info != (guint)TARGET_TEXT_PLAIN
1568 && info != (guint)TARGET_TEXT_PLAIN_UTF8
Bram Moolenaar071d4272004-06-13 20:20:40 +00001569 && info != (guint)TARGET_TEXT)
1570 return;
1571
Bram Moolenaar30613902019-12-01 22:11:18 +01001572 // get the selection from the '*'/'+' register
Bram Moolenaar071d4272004-06-13 20:20:40 +00001573 clip_get_selection(cbd);
1574
1575 motion_type = clip_convert_selection(&string, &tmplen, cbd);
1576 if (motion_type < 0 || string == NULL)
1577 return;
Bram Moolenaar30613902019-12-01 22:11:18 +01001578 // Due to int arguments we can't handle more than G_MAXINT. Also
1579 // reserve one extra byte for NUL or the motion type; just in case.
1580 // (Not that pasting 2G of text is ever going to work, but... ;-)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001581 length = MIN(tmplen, (long_u)(G_MAXINT - 1));
1582
1583 if (info == (guint)TARGET_VIM)
1584 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02001585 tmpbuf = alloc(length + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001586 if (tmpbuf != NULL)
1587 {
1588 tmpbuf[0] = motion_type;
1589 mch_memmove(tmpbuf + 1, string, (size_t)length);
1590 }
Bram Moolenaar30613902019-12-01 22:11:18 +01001591 // For our own format, the first byte contains the motion type
Bram Moolenaar071d4272004-06-13 20:20:40 +00001592 ++length;
1593 vim_free(string);
1594 string = tmpbuf;
1595 type = vim_atom;
1596 }
1597
Bram Moolenaar3a6eaa52009-06-16 13:23:06 +00001598 else if (info == (guint)TARGET_HTML)
1599 {
1600 vimconv_T conv;
1601
Bram Moolenaar30613902019-12-01 22:11:18 +01001602 // Since we get utf-16, we probably should set it as well.
Bram Moolenaar3a6eaa52009-06-16 13:23:06 +00001603 conv.vc_type = CONV_NONE;
1604 convert_setup_ext(&conv, p_enc, TRUE, (char_u *)"utf-16le", FALSE);
1605 if (conv.vc_type != CONV_NONE)
1606 {
1607 tmpbuf = string_convert(&conv, string, &length);
1608 convert_setup(&conv, NULL, NULL);
1609 vim_free(string);
1610 string = tmpbuf;
1611 }
1612
Bram Moolenaar30613902019-12-01 22:11:18 +01001613 // Prepend the BOM: "fffe"
Bram Moolenaar3a6eaa52009-06-16 13:23:06 +00001614 if (string != NULL)
1615 {
1616 tmpbuf = alloc(length + 2);
Bram Moolenaar6ee96582019-04-27 22:06:37 +02001617 if (tmpbuf != NULL)
1618 {
1619 tmpbuf[0] = 0xff;
1620 tmpbuf[1] = 0xfe;
1621 mch_memmove(tmpbuf + 2, string, (size_t)length);
1622 vim_free(string);
1623 string = tmpbuf;
1624 length += 2;
1625 }
Bram Moolenaar3a6eaa52009-06-16 13:23:06 +00001626
Bram Moolenaar98921892016-02-23 17:14:37 +01001627#if !GTK_CHECK_VERSION(3,0,0)
Bram Moolenaar30613902019-12-01 22:11:18 +01001628 // Looks redundant even for GTK2 because these values are
1629 // overwritten by gtk_selection_data_set() that follows.
Bram Moolenaar3a6eaa52009-06-16 13:23:06 +00001630 selection_data->type = selection_data->target;
Bram Moolenaar30613902019-12-01 22:11:18 +01001631 selection_data->format = 16; // 16 bits per char
Bram Moolenaar98921892016-02-23 17:14:37 +01001632#endif
Bram Moolenaar3a6eaa52009-06-16 13:23:06 +00001633 gtk_selection_data_set(selection_data, html_atom, 16,
1634 string, length);
1635 vim_free(string);
1636 }
1637 return;
1638 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001639 else if (info == (guint)TARGET_VIMENC)
1640 {
1641 int l = STRLEN(p_enc);
1642
Bram Moolenaar30613902019-12-01 22:11:18 +01001643 // contents: motion_type 'encoding' NUL text
Bram Moolenaar964b3742019-05-24 18:54:09 +02001644 tmpbuf = alloc(length + l + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001645 if (tmpbuf != NULL)
1646 {
1647 tmpbuf[0] = motion_type;
1648 STRCPY(tmpbuf + 1, p_enc);
1649 mch_memmove(tmpbuf + l + 2, string, (size_t)length);
Bram Moolenaar6ee96582019-04-27 22:06:37 +02001650 length += l + 2;
1651 vim_free(string);
1652 string = tmpbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001653 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001654 type = vimenc_atom;
1655 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001656
Bram Moolenaar30613902019-12-01 22:11:18 +01001657 // gtk_selection_data_set_text() handles everything for us. This is
1658 // so easy and simple and cool, it'd be insane not to use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001659 else
1660 {
1661 if (output_conv.vc_type != CONV_NONE)
1662 {
1663 tmpbuf = string_convert(&output_conv, string, &length);
1664 vim_free(string);
1665 if (tmpbuf == NULL)
1666 return;
1667 string = tmpbuf;
1668 }
Bram Moolenaar30613902019-12-01 22:11:18 +01001669 // Validate the string to avoid runtime warnings
Bram Moolenaar071d4272004-06-13 20:20:40 +00001670 if (g_utf8_validate((const char *)string, (gssize)length, NULL))
1671 {
1672 gtk_selection_data_set_text(selection_data,
1673 (const char *)string, length);
1674 }
1675 vim_free(string);
1676 return;
1677 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001678
1679 if (string != NULL)
1680 {
Bram Moolenaar98921892016-02-23 17:14:37 +01001681#if !GTK_CHECK_VERSION(3,0,0)
Bram Moolenaar30613902019-12-01 22:11:18 +01001682 // Looks redundant even for GTK2 because these values are
1683 // overwritten by gtk_selection_data_set() that follows.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001684 selection_data->type = selection_data->target;
Bram Moolenaar30613902019-12-01 22:11:18 +01001685 selection_data->format = 8; // 8 bits per char
Bram Moolenaar98921892016-02-23 17:14:37 +01001686#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001687 gtk_selection_data_set(selection_data, type, 8, string, length);
1688 vim_free(string);
1689 }
1690}
1691
1692/*
Bram Moolenaar29695702012-05-18 17:03:18 +02001693 * Check if the GUI can be started. Called before gvimrc is sourced and
1694 * before fork().
1695 * Return OK or FAIL.
1696 */
1697 int
Bram Moolenaar717e1962016-08-10 21:28:44 +02001698gui_mch_early_init_check(int give_message)
Bram Moolenaar29695702012-05-18 17:03:18 +02001699{
lilydjwg6e0a18f2024-01-29 20:54:28 +01001700 char_u *p, *q;
Bram Moolenaar29695702012-05-18 17:03:18 +02001701
Bram Moolenaar30613902019-12-01 22:11:18 +01001702 // Guess that when $DISPLAY isn't set the GUI can't start.
Bram Moolenaar29695702012-05-18 17:03:18 +02001703 p = mch_getenv((char_u *)"DISPLAY");
lilydjwg6e0a18f2024-01-29 20:54:28 +01001704 q = mch_getenv((char_u *)"WAYLAND_DISPLAY");
1705 if ((p == NULL || *p == NUL) && (q == NULL || *q == NUL))
Bram Moolenaar29695702012-05-18 17:03:18 +02001706 {
1707 gui.dying = TRUE;
Bram Moolenaar717e1962016-08-10 21:28:44 +02001708 if (give_message)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001709 emsg(_((char *)e_cannot_open_display));
Bram Moolenaar29695702012-05-18 17:03:18 +02001710 return FAIL;
1711 }
1712 return OK;
1713}
1714
1715/*
1716 * Check if the GUI can be started. Called before gvimrc is sourced but after
1717 * fork().
Bram Moolenaar071d4272004-06-13 20:20:40 +00001718 * Return OK or FAIL.
1719 */
1720 int
1721gui_mch_init_check(void)
1722{
Bram Moolenaar3a466a82016-01-19 17:47:25 +01001723#ifdef USE_GRESOURCE
1724 static int res_registered = FALSE;
1725
1726 if (!res_registered)
1727 {
Bram Moolenaar30613902019-12-01 22:11:18 +01001728 // Call this function in the GUI process; otherwise, the resources
1729 // won't be available. Don't call it twice.
Bram Moolenaar3a466a82016-01-19 17:47:25 +01001730 res_registered = TRUE;
1731 gui_gtk_register_resource();
1732 }
1733#endif
1734
Bram Moolenaarf80663f2016-04-05 21:56:06 +02001735#if GTK_CHECK_VERSION(3,10,0)
Bram Moolenaar30613902019-12-01 22:11:18 +01001736 // Vim currently assumes that Gtk means X11, so it cannot use native Gtk
1737 // support for other backends such as Wayland.
lilydjwg6e0a18f2024-01-29 20:54:28 +01001738 //
1739 // Use an environment variable to enable unfinished Wayland support.
1740 if (getenv("GVIM_ENABLE_WAYLAND") == NULL)
1741 gdk_set_allowed_backends ("x11");
Bram Moolenaarf80663f2016-04-05 21:56:06 +02001742#endif
1743
Bram Moolenaar071d4272004-06-13 20:20:40 +00001744#ifdef FEAT_GUI_GNOME
1745 if (gtk_socket_id == 0)
1746 using_gnome = 1;
1747#endif
1748
Bram Moolenaar30613902019-12-01 22:11:18 +01001749 // This defaults to argv[0], but we want it to match the name of the
1750 // shipped gvim.desktop so that Vim's windows can be associated with this
1751 // file.
Bram Moolenaar8dd79012013-05-21 12:52:04 +02001752 g_set_prgname("gvim");
1753
Bram Moolenaar30613902019-12-01 22:11:18 +01001754 // Don't use gtk_init() or gnome_init(), it exits on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001755 if (!gtk_init_check(&gui_argc, &gui_argv))
1756 {
1757 gui.dying = TRUE;
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001758 emsg(_((char *)e_cannot_open_display));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001759 return FAIL;
1760 }
1761
1762 return OK;
1763}
1764
Bram Moolenaar30613902019-12-01 22:11:18 +01001765/////////////////////////////////////////////////////////////////////////////
1766// Mouse handling callbacks
Bram Moolenaar071d4272004-06-13 20:20:40 +00001767
1768
1769static guint mouse_click_timer = 0;
1770static int mouse_timed_out = TRUE;
1771
1772/*
1773 * Timer used to recognize multiple clicks of the mouse button
1774 */
Bram Moolenaar4ab79682017-08-27 14:50:47 +02001775 static timeout_cb_type
Bram Moolenaar071d4272004-06-13 20:20:40 +00001776mouse_click_timer_cb(gpointer data)
1777{
Bram Moolenaar30613902019-12-01 22:11:18 +01001778 // we don't use this information currently
Bram Moolenaar071d4272004-06-13 20:20:40 +00001779 int *timed_out = (int *) data;
1780
1781 *timed_out = TRUE;
Bram Moolenaar30613902019-12-01 22:11:18 +01001782 return FALSE; // don't happen again
Bram Moolenaar071d4272004-06-13 20:20:40 +00001783}
1784
Bram Moolenaar4ab79682017-08-27 14:50:47 +02001785static guint motion_repeat_timer = 0;
1786static int motion_repeat_offset = FALSE;
1787static timeout_cb_type motion_repeat_timer_cb(gpointer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001788
1789 static void
1790process_motion_notify(int x, int y, GdkModifierType state)
1791{
1792 int button;
1793 int_u vim_modifiers;
Bram Moolenaar98921892016-02-23 17:14:37 +01001794 GtkAllocation allocation;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001795
Chris Daltonee93e322021-11-23 12:27:48 +00001796 // Need to add GDK_BUTTON1_MASK state when dragging a touch.
1797 state |= dragging_button_state;
1798
Bram Moolenaar071d4272004-06-13 20:20:40 +00001799 button = (state & (GDK_BUTTON1_MASK | GDK_BUTTON2_MASK |
1800 GDK_BUTTON3_MASK | GDK_BUTTON4_MASK |
1801 GDK_BUTTON5_MASK))
1802 ? MOUSE_DRAG : ' ';
1803
Bram Moolenaar30613902019-12-01 22:11:18 +01001804 // If our pointer is currently hidden, then we should show it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001805 gui_mch_mousehide(FALSE);
1806
Bram Moolenaar30613902019-12-01 22:11:18 +01001807 // Just moving the rodent above the drawing area without any button
1808 // being pressed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001809 if (button != MOUSE_DRAG)
1810 {
1811 gui_mouse_moved(x, y);
1812 return;
1813 }
1814
Bram Moolenaar30613902019-12-01 22:11:18 +01001815 // translate modifier coding between the main engine and GTK
Bram Moolenaar19a09a12005-03-04 23:39:37 +00001816 vim_modifiers = modifiers_gdk2mouse(state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001817
Bram Moolenaar30613902019-12-01 22:11:18 +01001818 // inform the editor engine about the occurrence of this event
Bram Moolenaar071d4272004-06-13 20:20:40 +00001819 gui_send_mouse_event(button, x, y, FALSE, vim_modifiers);
1820
Bram Moolenaar071d4272004-06-13 20:20:40 +00001821 /*
1822 * Auto repeat timer handling.
1823 */
Bram Moolenaar98921892016-02-23 17:14:37 +01001824 gtk_widget_get_allocation(gui.drawarea, &allocation);
1825
1826 if (x < 0 || y < 0
1827 || x >= allocation.width
1828 || y >= allocation.height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001829 {
1830
1831 int dx;
1832 int dy;
1833 int offshoot;
1834 int delay = 10;
1835
Bram Moolenaar30613902019-12-01 22:11:18 +01001836 // Calculate the maximal distance of the cursor from the drawing area.
1837 // (offshoot can't become negative here!).
Bram Moolenaar98921892016-02-23 17:14:37 +01001838 dx = x < 0 ? -x : x - allocation.width;
1839 dy = y < 0 ? -y : y - allocation.height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001840
1841 offshoot = dx > dy ? dx : dy;
1842
Bram Moolenaar30613902019-12-01 22:11:18 +01001843 // Make a linearly decaying timer delay with a threshold of 5 at a
1844 // distance of 127 pixels from the main window.
1845 //
1846 // One could think endlessly about the most ergonomic variant here.
1847 // For example it could make sense to calculate the distance from the
1848 // drags start instead...
1849 //
1850 // Maybe a parabolic interpolation would suite us better here too...
Bram Moolenaar071d4272004-06-13 20:20:40 +00001851 if (offshoot > 127)
1852 {
Bram Moolenaar30613902019-12-01 22:11:18 +01001853 // 5 appears to be somehow near to my perceptual limits :-).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001854 delay = 5;
1855 }
1856 else
1857 {
1858 delay = (130 * (127 - offshoot)) / 127 + 5;
1859 }
1860
Bram Moolenaar30613902019-12-01 22:11:18 +01001861 // shoot again
Bram Moolenaar071d4272004-06-13 20:20:40 +00001862 if (!motion_repeat_timer)
Bram Moolenaar4ab79682017-08-27 14:50:47 +02001863 motion_repeat_timer = timeout_add(delay, motion_repeat_timer_cb,
1864 NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001865 }
1866}
1867
Bram Moolenaar98921892016-02-23 17:14:37 +01001868#if GTK_CHECK_VERSION(3,0,0)
1869 static GdkDevice *
1870gui_gtk_get_pointer_device(GtkWidget *widget)
1871{
1872 GdkWindow * const win = gtk_widget_get_window(widget);
1873 GdkDisplay * const dpy = gdk_window_get_display(win);
Bram Moolenaar30e12d22016-04-17 20:49:53 +02001874# if GTK_CHECK_VERSION(3,20,0)
1875 GdkSeat * const seat = gdk_display_get_default_seat(dpy);
1876 return gdk_seat_get_pointer(seat);
1877# else
Bram Moolenaar98921892016-02-23 17:14:37 +01001878 GdkDeviceManager * const mngr = gdk_display_get_device_manager(dpy);
1879 return gdk_device_manager_get_client_pointer(mngr);
Bram Moolenaar30e12d22016-04-17 20:49:53 +02001880# endif
Bram Moolenaar98921892016-02-23 17:14:37 +01001881}
1882
1883 static GdkWindow *
1884gui_gtk_get_pointer(GtkWidget *widget,
1885 gint *x,
1886 gint *y,
1887 GdkModifierType *state)
1888{
1889 GdkWindow * const win = gtk_widget_get_window(widget);
1890 GdkDevice * const dev = gui_gtk_get_pointer_device(widget);
1891 return gdk_window_get_device_position(win, dev , x, y, state);
1892}
1893
Bram Moolenaar2369c152016-03-04 23:08:25 +01001894# if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar98921892016-02-23 17:14:37 +01001895 static GdkWindow *
1896gui_gtk_window_at_position(GtkWidget *widget,
1897 gint *x,
1898 gint *y)
1899{
1900 GdkDevice * const dev = gui_gtk_get_pointer_device(widget);
1901 return gdk_device_get_window_at_position(dev, x, y);
1902}
Bram Moolenaar2369c152016-03-04 23:08:25 +01001903# endif
Bram Moolenaar30613902019-12-01 22:11:18 +01001904#else // !GTK_CHECK_VERSION(3,0,0)
Bram Moolenaar664323e2018-09-18 22:30:07 +02001905# define gui_gtk_get_pointer(wid, x, y, s) \
1906 gdk_window_get_pointer((wid)->window, x, y, s)
1907# define gui_gtk_window_at_position(wid, x, y) gdk_window_at_pointer(x, y)
Bram Moolenaar98921892016-02-23 17:14:37 +01001908#endif
1909
Bram Moolenaar071d4272004-06-13 20:20:40 +00001910/*
1911 * Timer used to recognize multiple clicks of the mouse button.
1912 */
Bram Moolenaar4ab79682017-08-27 14:50:47 +02001913 static timeout_cb_type
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001914motion_repeat_timer_cb(gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001915{
1916 int x;
1917 int y;
1918 GdkModifierType state;
1919
Bram Moolenaar98921892016-02-23 17:14:37 +01001920 gui_gtk_get_pointer(gui.drawarea, &x, &y, &state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001921
1922 if (!(state & (GDK_BUTTON1_MASK | GDK_BUTTON2_MASK |
1923 GDK_BUTTON3_MASK | GDK_BUTTON4_MASK |
1924 GDK_BUTTON5_MASK)))
1925 {
1926 motion_repeat_timer = 0;
1927 return FALSE;
1928 }
1929
Bram Moolenaar30613902019-12-01 22:11:18 +01001930 // If there already is a mouse click in the input buffer, wait another
1931 // time (otherwise we would create a backlog of clicks)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001932 if (vim_used_in_input_buf() > 10)
1933 return TRUE;
1934
1935 motion_repeat_timer = 0;
1936
1937 /*
1938 * Fake a motion event.
1939 * Trick: Pretend the mouse moved to the next character on every other
1940 * event, otherwise drag events will be discarded, because they are still
1941 * in the same character.
1942 */
1943 if (motion_repeat_offset)
1944 x += gui.char_width;
1945
1946 motion_repeat_offset = !motion_repeat_offset;
1947 process_motion_notify(x, y, state);
1948
Bram Moolenaar30613902019-12-01 22:11:18 +01001949 // Don't happen again. We will get reinstalled in the synthetic event
1950 // if needed -- thus repeating should still work.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001951 return FALSE;
1952}
1953
Bram Moolenaar071d4272004-06-13 20:20:40 +00001954 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001955motion_notify_event(GtkWidget *widget,
1956 GdkEventMotion *event,
1957 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001958{
1959 if (event->is_hint)
1960 {
1961 int x;
1962 int y;
1963 GdkModifierType state;
1964
Bram Moolenaar98921892016-02-23 17:14:37 +01001965 gui_gtk_get_pointer(widget, &x, &y, &state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001966 process_motion_notify(x, y, state);
1967 }
1968 else
1969 {
1970 process_motion_notify((int)event->x, (int)event->y,
1971 (GdkModifierType)event->state);
1972 }
1973
Bram Moolenaar30613902019-12-01 22:11:18 +01001974 return TRUE; // handled
Bram Moolenaar071d4272004-06-13 20:20:40 +00001975}
1976
1977
1978/*
1979 * Mouse button handling. Note please that we are capturing multiple click's
1980 * by our own timeout mechanism instead of the one provided by GTK+ itself.
1981 * This is due to the way the generic VIM code is recognizing multiple clicks.
1982 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001983 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001984button_press_event(GtkWidget *widget,
1985 GdkEventButton *event,
1986 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001987{
Christian Brabandte1cd1fd2024-01-31 22:10:50 +01001988 int button;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001989 int repeated_click = FALSE;
1990 int x, y;
1991 int_u vim_modifiers;
1992
Bram Moolenaar20892c12011-06-26 04:49:00 +02001993 gui.event_time = event->time;
Bram Moolenaar7cfea752010-06-22 06:07:12 +02001994
Bram Moolenaar30613902019-12-01 22:11:18 +01001995 // Make sure we have focus now we've been selected
Bram Moolenaar98921892016-02-23 17:14:37 +01001996 if (gtk_socket_id != 0 && !gtk_widget_has_focus(widget))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001997 gtk_widget_grab_focus(widget);
1998
1999 /*
2000 * Don't let additional events about multiple clicks send by GTK to us
2001 * after the initial button press event confuse us.
2002 */
2003 if (event->type != GDK_BUTTON_PRESS)
2004 return FALSE;
2005
2006 x = event->x;
2007 y = event->y;
2008
Bram Moolenaar30613902019-12-01 22:11:18 +01002009 // Handle multiple clicks
Bram Moolenaar071d4272004-06-13 20:20:40 +00002010 if (!mouse_timed_out && mouse_click_timer)
2011 {
Bram Moolenaar4ab79682017-08-27 14:50:47 +02002012 timeout_remove(mouse_click_timer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002013 mouse_click_timer = 0;
2014 repeated_click = TRUE;
2015 }
2016
2017 mouse_timed_out = FALSE;
Bram Moolenaar4ab79682017-08-27 14:50:47 +02002018 mouse_click_timer = timeout_add(p_mouset, mouse_click_timer_cb,
2019 &mouse_timed_out);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002020
2021 switch (event->button)
2022 {
Bram Moolenaar30613902019-12-01 22:11:18 +01002023 // Keep in sync with gui_x11.c.
2024 // Buttons 4-7 are handled in scroll_event()
Chris Daltonee93e322021-11-23 12:27:48 +00002025 case 1:
2026 button = MOUSE_LEFT;
2027 // needed for touch-drag
2028 dragging_button_state |= GDK_BUTTON1_MASK;
2029 break;
Bram Moolenaar88e484b2015-11-24 15:38:44 +01002030 case 2: button = MOUSE_MIDDLE; break;
2031 case 3: button = MOUSE_RIGHT; break;
2032 case 8: button = MOUSE_X1; break;
2033 case 9: button = MOUSE_X2; break;
2034 default:
Bram Moolenaar30613902019-12-01 22:11:18 +01002035 return FALSE; // Unknown button
Bram Moolenaar071d4272004-06-13 20:20:40 +00002036 }
2037
2038#ifdef FEAT_XIM
Bram Moolenaar30613902019-12-01 22:11:18 +01002039 // cancel any preediting
Bram Moolenaar071d4272004-06-13 20:20:40 +00002040 if (im_is_preediting())
2041 xim_reset();
2042#endif
2043
Bram Moolenaar19a09a12005-03-04 23:39:37 +00002044 vim_modifiers = modifiers_gdk2mouse(event->state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002045
2046 gui_send_mouse_event(button, x, y, repeated_click, vim_modifiers);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002047
2048 return TRUE;
2049}
2050
Bram Moolenaar071d4272004-06-13 20:20:40 +00002051/*
lilydjwg725c7c32024-02-09 19:24:23 +01002052 * GTK+ abstracts scrolling via the GdkEventScroll.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002053 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002054 static gboolean
Bram Moolenaarb85cb212009-05-17 14:24:23 +00002055scroll_event(GtkWidget *widget,
2056 GdkEventScroll *event,
2057 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002058{
Christian Brabandte1cd1fd2024-01-31 22:10:50 +01002059 int button = 0; // silence gcc
Bram Moolenaar19a09a12005-03-04 23:39:37 +00002060 int_u vim_modifiers;
lilydjwg6e0a18f2024-01-29 20:54:28 +01002061#if GTK_CHECK_VERSION(3,4,0)
2062 static double acc_x, acc_y;
lilydjwg725c7c32024-02-09 19:24:23 +01002063#if !GTK_CHECK_VERSION(3,22,0)
lilydjwg6e0a18f2024-01-29 20:54:28 +01002064 static guint32 last_smooth_event_time;
lilydjwg725c7c32024-02-09 19:24:23 +01002065#endif
lilydjwgc4d4a1e2024-02-09 19:13:12 +01002066#define DT_X11 1
2067#define DT_WAYLAND 2
Christian Brabandtc9083712024-02-10 13:17:16 +01002068 static int display_type;
lilydjwgc4d4a1e2024-02-09 19:13:12 +01002069 if (!display_type)
2070 display_type = gui_mch_get_display() ? DT_X11 : DT_WAYLAND;
lilydjwg6e0a18f2024-01-29 20:54:28 +01002071#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002072
Bram Moolenaar98921892016-02-23 17:14:37 +01002073 if (gtk_socket_id != 0 && !gtk_widget_has_focus(widget))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002074 gtk_widget_grab_focus(widget);
2075
2076 switch (event->direction)
2077 {
2078 case GDK_SCROLL_UP:
2079 button = MOUSE_4;
2080 break;
2081 case GDK_SCROLL_DOWN:
2082 button = MOUSE_5;
2083 break;
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02002084 case GDK_SCROLL_LEFT:
2085 button = MOUSE_7;
2086 break;
2087 case GDK_SCROLL_RIGHT:
2088 button = MOUSE_6;
2089 break;
lilydjwg6e0a18f2024-01-29 20:54:28 +01002090#if GTK_CHECK_VERSION(3,4,0)
2091 case GDK_SCROLL_SMOOTH:
lilydjwg725c7c32024-02-09 19:24:23 +01002092 if (event->is_stop)
2093 {
2094 acc_x = acc_y = 0;
2095 // this event tells us to stop, without an actual moving
2096 return FALSE;
2097 }
2098#if GTK_CHECK_VERSION(3,22,0)
2099 if (gdk_device_get_axes(event->device) & GDK_AXIS_FLAG_WHEEL)
2100 // this is from a wheel (as oppose to a touchpad / trackpoint)
2101#else
lilydjwg6e0a18f2024-01-29 20:54:28 +01002102 if (event->time - last_smooth_event_time > 50)
2103 // reset our accumulations after 50ms of silence
lilydjwg725c7c32024-02-09 19:24:23 +01002104#endif
lilydjwg6e0a18f2024-01-29 20:54:28 +01002105 acc_x = acc_y = 0;
2106 acc_x += event->delta_x;
2107 acc_y += event->delta_y;
lilydjwg725c7c32024-02-09 19:24:23 +01002108#if !GTK_CHECK_VERSION(3,22,0)
lilydjwg6e0a18f2024-01-29 20:54:28 +01002109 last_smooth_event_time = event->time;
lilydjwg725c7c32024-02-09 19:24:23 +01002110#endif
lilydjwg6e0a18f2024-01-29 20:54:28 +01002111 break;
2112#endif
Bram Moolenaar30613902019-12-01 22:11:18 +01002113 default: // This shouldn't happen
Bram Moolenaar071d4272004-06-13 20:20:40 +00002114 return FALSE;
2115 }
2116
2117# ifdef FEAT_XIM
Bram Moolenaar30613902019-12-01 22:11:18 +01002118 // cancel any preediting
Bram Moolenaar071d4272004-06-13 20:20:40 +00002119 if (im_is_preediting())
2120 xim_reset();
2121# endif
2122
Bram Moolenaar19a09a12005-03-04 23:39:37 +00002123 vim_modifiers = modifiers_gdk2mouse(event->state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002124
lilydjwg6e0a18f2024-01-29 20:54:28 +01002125#if GTK_CHECK_VERSION(3,4,0)
lilydjwgc4d4a1e2024-02-09 19:13:12 +01002126 // on x11, despite not requested, when we copy into primary clipboard,
2127 // we'll get smooth events. Unsmooth ones will also come along.
2128 if (event->direction == GDK_SCROLL_SMOOTH && display_type == DT_WAYLAND)
lilydjwg6e0a18f2024-01-29 20:54:28 +01002129 {
lilydjwg1efb1b02024-02-08 11:04:21 +01002130 while (acc_x >= 1.0)
lilydjwg6e0a18f2024-01-29 20:54:28 +01002131 { // right
2132 acc_x = MAX(0.0, acc_x - 1.0);
2133 gui_send_mouse_event(MOUSE_6, (int)event->x, (int)event->y,
2134 FALSE, vim_modifiers);
2135 }
lilydjwg1efb1b02024-02-08 11:04:21 +01002136 while (acc_x <= -1.0)
lilydjwg6e0a18f2024-01-29 20:54:28 +01002137 { // left
2138 acc_x = MIN(0.0, acc_x + 1.0);
2139 gui_send_mouse_event(MOUSE_7, (int)event->x, (int)event->y,
2140 FALSE, vim_modifiers);
2141 }
lilydjwg1efb1b02024-02-08 11:04:21 +01002142 while (acc_y >= 1.0)
lilydjwg6e0a18f2024-01-29 20:54:28 +01002143 { // down
2144 acc_y = MAX(0.0, acc_y - 1.0);
2145 gui_send_mouse_event(MOUSE_5, (int)event->x, (int)event->y,
2146 FALSE, vim_modifiers);
2147 }
lilydjwg1efb1b02024-02-08 11:04:21 +01002148 while (acc_y <= -1.0)
lilydjwg6e0a18f2024-01-29 20:54:28 +01002149 { // up
2150 acc_y = MIN(0.0, acc_y + 1.0);
2151 gui_send_mouse_event(MOUSE_4, (int)event->x, (int)event->y,
2152 FALSE, vim_modifiers);
2153 }
2154 }
lilydjwgbaedc992024-04-18 21:22:57 +02002155 else if (event->direction == GDK_SCROLL_SMOOTH && display_type == DT_X11)
2156 // for X11 we deal with unsmooth events, and so ignore the smooth ones
2157 ;
lilydjwg6e0a18f2024-01-29 20:54:28 +01002158 else
lilydjwgc4d4a1e2024-02-09 19:13:12 +01002159#undef DT_X11
2160#undef DT_WAYLAND
lilydjwg6e0a18f2024-01-29 20:54:28 +01002161#endif
2162 gui_send_mouse_event(button, (int)event->x, (int)event->y,
2163 FALSE, vim_modifiers);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002164
Bram Moolenaar071d4272004-06-13 20:20:40 +00002165 return TRUE;
2166}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002167
2168
Bram Moolenaar071d4272004-06-13 20:20:40 +00002169 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +00002170button_release_event(GtkWidget *widget UNUSED,
2171 GdkEventButton *event,
2172 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002173{
2174 int x, y;
2175 int_u vim_modifiers;
2176
Bram Moolenaar20892c12011-06-26 04:49:00 +02002177 gui.event_time = event->time;
Bram Moolenaar7cfea752010-06-22 06:07:12 +02002178
Bram Moolenaar30613902019-12-01 22:11:18 +01002179 // Remove any motion "machine gun" timers used for automatic further
2180 // extension of allocation areas if outside of the applications window
2181 // area .
Bram Moolenaar071d4272004-06-13 20:20:40 +00002182 if (motion_repeat_timer)
2183 {
Bram Moolenaar4ab79682017-08-27 14:50:47 +02002184 timeout_remove(motion_repeat_timer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002185 motion_repeat_timer = 0;
2186 }
2187
2188 x = event->x;
2189 y = event->y;
2190
Bram Moolenaar19a09a12005-03-04 23:39:37 +00002191 vim_modifiers = modifiers_gdk2mouse(event->state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002192
2193 gui_send_mouse_event(MOUSE_RELEASE, x, y, FALSE, vim_modifiers);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002194
Chris Daltonee93e322021-11-23 12:27:48 +00002195 switch (event->button)
2196 {
2197 case 1: // MOUSE_LEFT
2198 dragging_button_state = 0;
2199 break;
2200 }
2201
Bram Moolenaar071d4272004-06-13 20:20:40 +00002202 return TRUE;
2203}
2204
2205
2206#ifdef FEAT_DND
Bram Moolenaar30613902019-12-01 22:11:18 +01002207/////////////////////////////////////////////////////////////////////////////
2208// Drag aNd Drop support handlers.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002209
2210/*
2211 * Count how many items there may be and separate them with a NUL.
2212 * Apparently the items are separated with \r\n. This is not documented,
2213 * thus be careful not to go past the end. Also allow separation with
2214 * NUL characters.
2215 */
2216 static int
2217count_and_decode_uri_list(char_u *out, char_u *raw, int len)
2218{
2219 int i;
2220 char_u *p = out;
2221 int count = 0;
2222
2223 for (i = 0; i < len; ++i)
2224 {
2225 if (raw[i] == NUL || raw[i] == '\n' || raw[i] == '\r')
2226 {
2227 if (p > out && p[-1] != NUL)
2228 {
2229 ++count;
2230 *p++ = NUL;
2231 }
2232 }
2233 else if (raw[i] == '%' && i + 2 < len && hexhex2nr(raw + i + 1) > 0)
2234 {
2235 *p++ = hexhex2nr(raw + i + 1);
2236 i += 2;
2237 }
2238 else
2239 *p++ = raw[i];
2240 }
2241 if (p > out && p[-1] != NUL)
2242 {
Bram Moolenaar30613902019-12-01 22:11:18 +01002243 *p = NUL; // last item didn't have \r or \n
Bram Moolenaar071d4272004-06-13 20:20:40 +00002244 ++count;
2245 }
2246 return count;
2247}
2248
2249/*
2250 * Parse NUL separated "src" strings. Make it an array "outlist" form. On
2251 * this process, URI which protocol is not "file:" are removed. Return
2252 * length of array (less than "max").
2253 */
2254 static int
2255filter_uri_list(char_u **outlist, int max, char_u *src)
2256{
2257 int i, j;
2258
2259 for (i = j = 0; i < max; ++i)
2260 {
2261 outlist[i] = NULL;
2262 if (STRNCMP(src, "file:", 5) == 0)
2263 {
2264 src += 5;
2265 if (STRNCMP(src, "//localhost", 11) == 0)
2266 src += 11;
2267 while (src[0] == '/' && src[1] == '/')
2268 ++src;
2269 outlist[j++] = vim_strsave(src);
2270 }
2271 src += STRLEN(src) + 1;
2272 }
2273 return j;
2274}
2275
2276 static char_u **
2277parse_uri_list(int *count, char_u *data, int len)
2278{
2279 int n = 0;
2280 char_u *tmp = NULL;
Bram Moolenaar945ec092016-06-08 21:17:43 +02002281 char_u **array = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002282
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002283 if (data != NULL && len > 0 && (tmp = alloc(len + 1)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002284 {
2285 n = count_and_decode_uri_list(tmp, data, len);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002286 if (n > 0 && (array = ALLOC_MULT(char_u *, n)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002287 n = filter_uri_list(array, n, tmp);
2288 }
2289 vim_free(tmp);
2290 *count = n;
2291 return array;
2292}
2293
2294 static void
2295drag_handle_uri_list(GdkDragContext *context,
2296 GtkSelectionData *data,
2297 guint time_,
2298 GdkModifierType state,
2299 gint x,
2300 gint y)
2301{
2302 char_u **fnames;
2303 int nfiles = 0;
2304
Bram Moolenaar98921892016-02-23 17:14:37 +01002305 fnames = parse_uri_list(&nfiles,
2306 (char_u *)gtk_selection_data_get_data(data),
2307 gtk_selection_data_get_length(data));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002308
2309 if (fnames != NULL && nfiles > 0)
2310 {
Bram Moolenaar19a09a12005-03-04 23:39:37 +00002311 int_u modifiers;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002312
Bram Moolenaar30613902019-12-01 22:11:18 +01002313 gtk_drag_finish(context, TRUE, FALSE, time_); // accept
Bram Moolenaar071d4272004-06-13 20:20:40 +00002314
Bram Moolenaar19a09a12005-03-04 23:39:37 +00002315 modifiers = modifiers_gdk2mouse(state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002316
2317 gui_handle_drop(x, y, modifiers, fnames, nfiles);
2318 }
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002319 else
2320 vim_free(fnames);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002321}
2322
2323 static void
2324drag_handle_text(GdkDragContext *context,
2325 GtkSelectionData *data,
2326 guint time_,
2327 GdkModifierType state)
2328{
2329 char_u dropkey[6] = {CSI, KS_MODIFIER, 0, CSI, KS_EXTRA, (char_u)KE_DROP};
2330 char_u *text;
2331 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002332 char_u *tmpbuf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002333
Bram Moolenaar98921892016-02-23 17:14:37 +01002334 text = (char_u *)gtk_selection_data_get_data(data);
2335 len = gtk_selection_data_get_length(data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002336
Bram Moolenaar98921892016-02-23 17:14:37 +01002337 if (gtk_selection_data_get_data_type(data) == utf8_string_atom)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002338 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002339 if (input_conv.vc_type != CONV_NONE)
2340 tmpbuf = string_convert(&input_conv, text, &len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002341 if (tmpbuf != NULL)
2342 text = tmpbuf;
2343 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002344
2345 dnd_yank_drag_data(text, (long)len);
Bram Moolenaar30613902019-12-01 22:11:18 +01002346 gtk_drag_finish(context, TRUE, FALSE, time_); // accept
Bram Moolenaar071d4272004-06-13 20:20:40 +00002347 vim_free(tmpbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002348
Bram Moolenaar19a09a12005-03-04 23:39:37 +00002349 dropkey[2] = modifiers_gdk2vim(state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002350
2351 if (dropkey[2] != 0)
2352 add_to_input_buf(dropkey, (int)sizeof(dropkey));
2353 else
2354 add_to_input_buf(dropkey + 3, (int)(sizeof(dropkey) - 3));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002355}
2356
2357/*
2358 * DND receiver.
2359 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002360 static void
2361drag_data_received_cb(GtkWidget *widget,
2362 GdkDragContext *context,
2363 gint x,
2364 gint y,
2365 GtkSelectionData *data,
2366 guint info,
2367 guint time_,
Bram Moolenaarb85cb212009-05-17 14:24:23 +00002368 gpointer user_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002369{
2370 GdkModifierType state;
2371
Bram Moolenaar30613902019-12-01 22:11:18 +01002372 // Guard against trash
Bram Moolenaar98921892016-02-23 17:14:37 +01002373 const guchar * const data_data = gtk_selection_data_get_data(data);
2374 const gint data_length = gtk_selection_data_get_length(data);
2375 const gint data_format = gtk_selection_data_get_format(data);
2376
2377 if (data_data == NULL
2378 || data_length <= 0
2379 || data_format != 8
2380 || data_data[data_length] != '\0')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002381 {
2382 gtk_drag_finish(context, FALSE, FALSE, time_);
2383 return;
2384 }
2385
Bram Moolenaar30613902019-12-01 22:11:18 +01002386 // Get the current modifier state for proper distinguishment between
2387 // different operations later.
Bram Moolenaar98921892016-02-23 17:14:37 +01002388 gui_gtk_get_pointer(widget, NULL, NULL, &state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002389
Bram Moolenaar30613902019-12-01 22:11:18 +01002390 // Not sure about the role of "text/plain" here...
Bram Moolenaar071d4272004-06-13 20:20:40 +00002391 if (info == (guint)TARGET_TEXT_URI_LIST)
2392 drag_handle_uri_list(context, data, time_, state, x, y);
2393 else
2394 drag_handle_text(context, data, time_, state);
2395
2396}
Bram Moolenaar30613902019-12-01 22:11:18 +01002397#endif // FEAT_DND
Bram Moolenaar071d4272004-06-13 20:20:40 +00002398
2399
Bram Moolenaarf96ae0b2019-07-28 15:21:55 +02002400#if defined(USE_GNOME_SESSION)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002401/*
2402 * GnomeClient interact callback. Check for unsaved buffers that cannot
2403 * be abandoned and pop up a dialog asking the user for confirmation if
2404 * necessary.
2405 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002406 static void
Bram Moolenaar30bb4142010-05-17 22:07:15 +02002407sm_client_check_changed_any(GnomeClient *client UNUSED,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002408 gint key,
Bram Moolenaar30bb4142010-05-17 22:07:15 +02002409 GnomeDialogType type UNUSED,
2410 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002411{
2412 cmdmod_T save_cmdmod;
2413 gboolean shutdown_cancelled;
2414
2415 save_cmdmod = cmdmod;
2416
2417# ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +02002418 cmdmod.cmod_flags |= CMOD_BROWSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002419# endif
2420# if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaare1004402020-10-24 20:49:43 +02002421 cmdmod.cmod_flags |= CMOD_CONFIRM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002422# endif
2423 /*
2424 * If there are changed buffers, present the user with
2425 * a dialog if possible, otherwise give an error message.
2426 */
Bram Moolenaar027387f2016-01-02 22:25:52 +01002427 shutdown_cancelled = check_changed_any(FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002428
2429 exiting = FALSE;
2430 cmdmod = save_cmdmod;
Bram Moolenaar30613902019-12-01 22:11:18 +01002431 setcursor(); // position the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002432 out_flush();
2433 /*
2434 * If the user hit the [Cancel] button the whole shutdown
2435 * will be cancelled. Wow, quite powerful feature (:
2436 */
2437 gnome_interaction_key_return(key, shutdown_cancelled);
2438}
2439
2440/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002441 * "save_yourself" signal handler. Initiate an interaction to ask the user
2442 * for confirmation if necessary. Save the current editing session and tell
2443 * the session manager how to restart Vim.
2444 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002445 static gboolean
2446sm_client_save_yourself(GnomeClient *client,
Bram Moolenaar30bb4142010-05-17 22:07:15 +02002447 gint phase UNUSED,
2448 GnomeSaveStyle save_style UNUSED,
2449 gboolean shutdown UNUSED,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002450 GnomeInteractStyle interact_style,
Bram Moolenaar30bb4142010-05-17 22:07:15 +02002451 gboolean fast UNUSED,
2452 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002453{
2454 static const char suffix[] = "-session.vim";
2455 char *session_file;
2456 unsigned int len;
2457 gboolean success;
2458
Bram Moolenaar30613902019-12-01 22:11:18 +01002459 // Always request an interaction if possible. check_changed_any()
2460 // won't actually show a dialog unless any buffers have been modified.
2461 // There doesn't seem to be an obvious way to check that without
2462 // automatically firing the dialog. Anyway, it works just fine.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002463 if (interact_style == GNOME_INTERACT_ANY)
2464 gnome_client_request_interaction(client, GNOME_DIALOG_NORMAL,
2465 &sm_client_check_changed_any,
2466 NULL);
2467 out_flush();
Bram Moolenaar30613902019-12-01 22:11:18 +01002468 ml_sync_all(FALSE, FALSE); // preserve all swap files
Bram Moolenaar071d4272004-06-13 20:20:40 +00002469
Bram Moolenaar30613902019-12-01 22:11:18 +01002470 // The path is unique for each session save. We do neither know nor care
2471 // which session script will actually be used later. This decision is in
2472 // the domain of the session manager.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002473 session_file = gnome_config_get_real_path(
2474 gnome_client_get_config_prefix(client));
2475 len = strlen(session_file);
2476
2477 if (len > 0 && session_file[len-1] == G_DIR_SEPARATOR)
Bram Moolenaar30613902019-12-01 22:11:18 +01002478 --len; // get rid of the superfluous trailing '/'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002479
2480 session_file = g_renew(char, session_file, len + sizeof(suffix));
2481 memcpy(session_file + len, suffix, sizeof(suffix));
2482
2483 success = write_session_file((char_u *)session_file);
2484
2485 if (success)
2486 {
2487 const char *argv[8];
2488 int i;
2489
Bram Moolenaar30613902019-12-01 22:11:18 +01002490 // Tell the session manager how to wipe out the stored session data.
2491 // This isn't as dangerous as it looks, don't worry :) session_file
2492 // is a unique absolute filename. Usually it'll be something like
2493 // `/home/user/.gnome2/vim-XXXXXX-session.vim'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002494 i = 0;
2495 argv[i++] = "rm";
2496 argv[i++] = session_file;
2497 argv[i] = NULL;
2498
2499 gnome_client_set_discard_command(client, i, (char **)argv);
2500
Bram Moolenaar30613902019-12-01 22:11:18 +01002501 // Tell the session manager how to restore the just saved session.
2502 // This is easily done thanks to Vim's -S option. Pass the -f flag
2503 // since there's no need to fork -- it might even cause confusion.
2504 // Also pass the window role to give the WM something to match on.
2505 // The role is set in gui_mch_open(), thus should _never_ be NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002506 i = 0;
2507 argv[i++] = restart_command;
2508 argv[i++] = "-f";
2509 argv[i++] = "-g";
Bram Moolenaar071d4272004-06-13 20:20:40 +00002510 argv[i++] = "--role";
2511 argv[i++] = gtk_window_get_role(GTK_WINDOW(gui.mainwin));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002512 argv[i++] = "-S";
2513 argv[i++] = session_file;
2514 argv[i] = NULL;
2515
2516 gnome_client_set_restart_command(client, i, (char **)argv);
2517 gnome_client_set_clone_command(client, 0, NULL);
2518 }
2519
2520 g_free(session_file);
2521
2522 return success;
2523}
2524
2525/*
2526 * Called when the session manager wants us to die. There isn't much to save
2527 * here since "save_yourself" has been emitted before (unless serious trouble
2528 * is happening).
2529 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002530 static void
Bram Moolenaar30bb4142010-05-17 22:07:15 +02002531sm_client_die(GnomeClient *client UNUSED, gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002532{
Bram Moolenaar30613902019-12-01 22:11:18 +01002533 // Don't write messages to the GUI anymore
Bram Moolenaar071d4272004-06-13 20:20:40 +00002534 full_screen = FALSE;
2535
Bram Moolenaarc93e7912008-07-08 10:46:08 +00002536 vim_strncpy(IObuff, (char_u *)
Bram Moolenaarce0842a2005-07-18 21:58:11 +00002537 _("Vim: Received \"die\" request from session manager\n"),
Bram Moolenaarc93e7912008-07-08 10:46:08 +00002538 IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002539 preserve_exit();
2540}
2541
2542/*
2543 * Connect our signal handlers to be notified on session save and shutdown.
2544 */
2545 static void
2546setup_save_yourself(void)
2547{
2548 GnomeClient *client;
2549
2550 client = gnome_master_client();
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00002551 if (client == NULL)
2552 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002553
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00002554 // Must use the deprecated gtk_signal_connect() for compatibility
2555 // with GNOME 1. Arrgh, zombies!
2556 gtk_signal_connect(GTK_OBJECT(client), "save_yourself",
2557 GTK_SIGNAL_FUNC(&sm_client_save_yourself), NULL);
2558 gtk_signal_connect(GTK_OBJECT(client), "die",
2559 GTK_SIGNAL_FUNC(&sm_client_die), NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002560}
2561
Bram Moolenaarf96ae0b2019-07-28 15:21:55 +02002562#else // !USE_GNOME_SESSION
Bram Moolenaar071d4272004-06-13 20:20:40 +00002563
2564# ifdef USE_XSMP
2565/*
2566 * GTK tells us that XSMP needs attention
2567 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002568 static gboolean
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002569local_xsmp_handle_requests(
2570 GIOChannel *source UNUSED,
2571 GIOCondition condition,
2572 gpointer data)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002573{
2574 if (condition == G_IO_IN)
2575 {
Bram Moolenaar30613902019-12-01 22:11:18 +01002576 // Do stuff; maybe close connection
Bram Moolenaar071d4272004-06-13 20:20:40 +00002577 if (xsmp_handle_requests() == FAIL)
2578 g_io_channel_unref((GIOChannel *)data);
2579 return TRUE;
2580 }
Bram Moolenaar30613902019-12-01 22:11:18 +01002581 // Error
Bram Moolenaar071d4272004-06-13 20:20:40 +00002582 g_io_channel_unref((GIOChannel *)data);
2583 xsmp_close();
2584 return TRUE;
2585}
Bram Moolenaar30613902019-12-01 22:11:18 +01002586# endif // USE_XSMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00002587
2588/*
2589 * Setup the WM_PROTOCOLS to indicate we want the WM_SAVE_YOURSELF event.
2590 * This is an ugly use of X functions. GTK doesn't offer an alternative.
2591 */
2592 static void
2593setup_save_yourself(void)
2594{
2595 Atom *existing_atoms = NULL;
2596 int count = 0;
2597
Bram Moolenaar98921892016-02-23 17:14:37 +01002598# ifdef USE_XSMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00002599 if (xsmp_icefd != -1)
2600 {
2601 /*
2602 * Use XSMP is preference to legacy WM_SAVE_YOURSELF;
2603 * set up GTK IO monitor
2604 */
2605 GIOChannel *g_io = g_io_channel_unix_new(xsmp_icefd);
2606
2607 g_io_add_watch(g_io, G_IO_IN | G_IO_ERR | G_IO_HUP,
2608 local_xsmp_handle_requests, (gpointer)g_io);
Bram Moolenaar98921892016-02-23 17:14:37 +01002609 g_io_channel_unref(g_io);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002610 }
2611 else
Bram Moolenaar98921892016-02-23 17:14:37 +01002612# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002613 {
Bram Moolenaar30613902019-12-01 22:11:18 +01002614 // Fall back to old method
Bram Moolenaar071d4272004-06-13 20:20:40 +00002615
Bram Moolenaar30613902019-12-01 22:11:18 +01002616 // first get the existing value
lilydjwg6e0a18f2024-01-29 20:54:28 +01002617 Display * dpy = gui_mch_get_display();
2618 if (!dpy)
2619 return;
Bram Moolenaar98921892016-02-23 17:14:37 +01002620
lilydjwg6e0a18f2024-01-29 20:54:28 +01002621 GdkWindow * const mainwin_win = gtk_widget_get_window(gui.mainwin);
2622 if (XGetWMProtocols(dpy, GDK_WINDOW_XID(mainwin_win),
Bram Moolenaar98921892016-02-23 17:14:37 +01002623 &existing_atoms, &count))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002624 {
2625 Atom *new_atoms;
2626 Atom save_yourself_xatom;
2627 int i;
2628
2629 save_yourself_xatom = GET_X_ATOM(save_yourself_atom);
2630
Bram Moolenaar30613902019-12-01 22:11:18 +01002631 // check if WM_SAVE_YOURSELF isn't there yet
Bram Moolenaar071d4272004-06-13 20:20:40 +00002632 for (i = 0; i < count; ++i)
2633 if (existing_atoms[i] == save_yourself_xatom)
2634 break;
2635
2636 if (i == count)
2637 {
Bram Moolenaar30613902019-12-01 22:11:18 +01002638 // allocate an Atoms array which is one item longer
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002639 new_atoms = ALLOC_MULT(Atom, count + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002640 if (new_atoms != NULL)
2641 {
2642 memcpy(new_atoms, existing_atoms, count * sizeof(Atom));
2643 new_atoms[count] = save_yourself_xatom;
Bram Moolenaar98921892016-02-23 17:14:37 +01002644 XSetWMProtocols(GDK_WINDOW_XDISPLAY(mainwin_win),
2645 GDK_WINDOW_XID(mainwin_win),
Bram Moolenaar071d4272004-06-13 20:20:40 +00002646 new_atoms, count + 1);
2647 vim_free(new_atoms);
2648 }
2649 }
2650 XFree(existing_atoms);
2651 }
2652 }
2653}
2654
Bram Moolenaar071d4272004-06-13 20:20:40 +00002655/*
2656 * Installing a global event filter seems to be the only way to catch
2657 * client messages of type WM_PROTOCOLS without overriding GDK's own
2658 * client message event filter. Well, that's still better than trying
2659 * to guess what the GDK filter had done if it had been invoked instead
Bram Moolenaar071d4272004-06-13 20:20:40 +00002660 *
2661 * GTK2_FIXME: This doesn't seem to work. For some reason we never
2662 * receive WM_SAVE_YOURSELF even though everything is set up correctly.
2663 * I have the nasty feeling modern session managers just don't send this
2664 * deprecated message anymore. Addition: confirmed by several people.
2665 *
2666 * The GNOME session support is much cooler anyway. Unlike this ugly
2667 * WM_SAVE_YOURSELF hack it actually stores the session... And yes,
2668 * it should work with KDE as well.
2669 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002670 static GdkFilterReturn
Bram Moolenaarb85cb212009-05-17 14:24:23 +00002671global_event_filter(GdkXEvent *xev,
2672 GdkEvent *event UNUSED,
2673 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002674{
2675 XEvent *xevent = (XEvent *)xev;
2676
2677 if (xevent != NULL
2678 && xevent->type == ClientMessage
2679 && xevent->xclient.message_type == GET_X_ATOM(wm_protocols_atom)
Bram Moolenaarb85cb212009-05-17 14:24:23 +00002680 && (long_u)xevent->xclient.data.l[0]
2681 == GET_X_ATOM(save_yourself_atom))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002682 {
2683 out_flush();
Bram Moolenaar30613902019-12-01 22:11:18 +01002684 ml_sync_all(FALSE, FALSE); // preserve all swap files
Bram Moolenaar071d4272004-06-13 20:20:40 +00002685 /*
2686 * Set the window's WM_COMMAND property, to let the window manager
2687 * know we are done saving ourselves. We don't want to be
2688 * restarted, thus set argv to NULL.
2689 */
Bram Moolenaar98921892016-02-23 17:14:37 +01002690 XSetCommand(GDK_WINDOW_XDISPLAY(gtk_widget_get_window(gui.mainwin)),
2691 GDK_WINDOW_XID(gtk_widget_get_window(gui.mainwin)),
Bram Moolenaar071d4272004-06-13 20:20:40 +00002692 NULL, 0);
2693 return GDK_FILTER_REMOVE;
2694 }
2695
2696 return GDK_FILTER_CONTINUE;
2697}
Bram Moolenaarf96ae0b2019-07-28 15:21:55 +02002698#endif // !USE_GNOME_SESSION
Bram Moolenaar071d4272004-06-13 20:20:40 +00002699
2700
2701/*
2702 * Setup the window icon & xcmdsrv comm after the main window has been realized.
2703 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002704 static void
Bram Moolenaarb85cb212009-05-17 14:24:23 +00002705mainwin_realize(GtkWidget *widget UNUSED, gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002706{
Bram Moolenaar30613902019-12-01 22:11:18 +01002707// If you get an error message here, you still need to unpack the runtime
2708// archive!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002709#ifdef magick
2710# undef magick
2711#endif
Bram Moolenaar30613902019-12-01 22:11:18 +01002712 // A bit hackish, but avoids casting later and allows optimization
Bram Moolenaar071d4272004-06-13 20:20:40 +00002713# define static static const
Bram Moolenaar071d4272004-06-13 20:20:40 +00002714#define magick vim32x32
2715#include "../runtime/vim32x32.xpm"
2716#undef magick
2717#define magick vim16x16
2718#include "../runtime/vim16x16.xpm"
2719#undef magick
2720#define magick vim48x48
2721#include "../runtime/vim48x48.xpm"
2722#undef magick
Bram Moolenaar071d4272004-06-13 20:20:40 +00002723# undef static
Bram Moolenaar071d4272004-06-13 20:20:40 +00002724
Bram Moolenaar98921892016-02-23 17:14:37 +01002725 GdkWindow * const mainwin_win = gtk_widget_get_window(gui.mainwin);
Bram Moolenaar98921892016-02-23 17:14:37 +01002726
Bram Moolenaar30613902019-12-01 22:11:18 +01002727 // When started with "--echo-wid" argument, write window ID on stdout.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002728 if (echo_wid_arg)
2729 {
lilydjwg6e0a18f2024-01-29 20:54:28 +01002730 if (gui_mch_get_display())
2731 printf("WID: %ld\n", (long)GDK_WINDOW_XID(mainwin_win));
2732 else
2733 printf("WID: 0\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002734 fflush(stdout);
2735 }
2736
2737 if (vim_strchr(p_go, GO_ICON) != NULL)
2738 {
2739 /*
2740 * Add an icon to the main window. For fun and convenience of the user.
2741 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002742 GList *icons = NULL;
2743
2744 icons = g_list_prepend(icons, gdk_pixbuf_new_from_xpm_data(vim16x16));
2745 icons = g_list_prepend(icons, gdk_pixbuf_new_from_xpm_data(vim32x32));
2746 icons = g_list_prepend(icons, gdk_pixbuf_new_from_xpm_data(vim48x48));
2747
2748 gtk_window_set_icon_list(GTK_WINDOW(gui.mainwin), icons);
2749
Bram Moolenaar81a4cf42020-09-09 19:05:13 +02002750 // TODO: is this type cast OK?
2751 g_list_foreach(icons, (GFunc)(void *)&g_object_unref, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002752 g_list_free(icons);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002753 }
2754
Bram Moolenaarf96ae0b2019-07-28 15:21:55 +02002755#if !defined(USE_GNOME_SESSION)
Bram Moolenaar30613902019-12-01 22:11:18 +01002756 // Register a handler for WM_SAVE_YOURSELF with GDK's low-level X I/F
Bram Moolenaar071d4272004-06-13 20:20:40 +00002757 gdk_window_add_filter(NULL, &global_event_filter, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002758#endif
Bram Moolenaar30613902019-12-01 22:11:18 +01002759 // Setup to indicate to the window manager that we want to catch the
2760 // WM_SAVE_YOURSELF event. For GNOME, this connects to the session
2761 // manager instead.
Bram Moolenaarf96ae0b2019-07-28 15:21:55 +02002762#if defined(USE_GNOME_SESSION)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002763 if (using_gnome)
2764#endif
2765 setup_save_yourself();
2766
2767#ifdef FEAT_CLIENTSERVER
lilydjwg6e0a18f2024-01-29 20:54:28 +01002768 if (gui_mch_get_display())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002769 {
lilydjwg6e0a18f2024-01-29 20:54:28 +01002770 if (serverName == NULL && serverDelayedStartName != NULL)
2771 {
2772 // This is a :gui command in a plain vim with no previous server
2773 commWindow = GDK_WINDOW_XID(mainwin_win);
Bram Moolenaar98921892016-02-23 17:14:37 +01002774
lilydjwg6e0a18f2024-01-29 20:54:28 +01002775 (void)serverRegisterName(GDK_WINDOW_XDISPLAY(mainwin_win),
2776 serverDelayedStartName);
2777 }
2778 else
2779 {
2780 /*
2781 * Cannot handle "XLib-only" windows with gtk event routines, we'll
2782 * have to change the "server" registration to that of the main window
2783 * If we have not registered a name yet, remember the window.
2784 */
2785 serverChangeRegisteredWindow(GDK_WINDOW_XDISPLAY(mainwin_win),
2786 GDK_WINDOW_XID(mainwin_win));
2787 }
2788 gtk_widget_add_events(gui.mainwin, GDK_PROPERTY_CHANGE_MASK);
2789 g_signal_connect(G_OBJECT(gui.mainwin), "property-notify-event",
2790 G_CALLBACK(property_event), NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002791 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002792#endif
2793}
2794
2795 static GdkCursor *
2796create_blank_pointer(void)
2797{
2798 GdkWindow *root_window = NULL;
Bram Moolenaar98921892016-02-23 17:14:37 +01002799#if GTK_CHECK_VERSION(3,0,0)
2800 GdkPixbuf *blank_mask;
2801#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002802 GdkPixmap *blank_mask;
Bram Moolenaar98921892016-02-23 17:14:37 +01002803#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002804 GdkCursor *cursor;
Bram Moolenaar36edf062016-07-21 22:10:12 +02002805#if GTK_CHECK_VERSION(3,0,0)
Kenny Stauffer660eb9f2023-04-28 16:36:51 +01002806 GdkRGBA color = { 0.0, 0.0, 0.0, 0.0 };
Bram Moolenaar36edf062016-07-21 22:10:12 +02002807#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002808 GdkColor color = { 0, 0, 0, 0 };
2809 char blank_data[] = { 0x0 };
Bram Moolenaar98921892016-02-23 17:14:37 +01002810#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002811
Bram Moolenaarb463e8d2017-06-05 15:07:09 +02002812#if GTK_CHECK_VERSION(3,12,0)
Bram Moolenaar98921892016-02-23 17:14:37 +01002813 {
2814 GdkWindow * const win = gtk_widget_get_window(gui.mainwin);
2815 GdkScreen * const scrn = gdk_window_get_screen(win);
2816 root_window = gdk_screen_get_root_window(scrn);
2817 }
Bram Moolenaarb463e8d2017-06-05 15:07:09 +02002818#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002819 root_window = gtk_widget_get_root_window(gui.mainwin);
2820#endif
2821
Bram Moolenaar30613902019-12-01 22:11:18 +01002822 // Create a pseudo blank pointer, which is in fact one pixel by one pixel
2823 // in size.
Bram Moolenaar98921892016-02-23 17:14:37 +01002824#if GTK_CHECK_VERSION(3,0,0)
2825 {
2826 cairo_surface_t *surf;
2827 cairo_t *cr;
2828
2829 surf = cairo_image_surface_create(CAIRO_FORMAT_A1, 1, 1);
2830 cr = cairo_create(surf);
2831
Bram Moolenaar36edf062016-07-21 22:10:12 +02002832 cairo_set_source_rgba(cr,
2833 color.red,
2834 color.green,
2835 color.blue,
2836 color.alpha);
Bram Moolenaar98921892016-02-23 17:14:37 +01002837 cairo_rectangle(cr, 0, 0, 1, 1);
2838 cairo_fill(cr);
2839 cairo_destroy(cr);
2840
2841 blank_mask = gdk_pixbuf_get_from_surface(surf, 0, 0, 1, 1);
2842 cairo_surface_destroy(surf);
2843
2844 cursor = gdk_cursor_new_from_pixbuf(gdk_window_get_display(root_window),
2845 blank_mask, 0, 0);
2846 g_object_unref(blank_mask);
2847 }
2848#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002849 blank_mask = gdk_bitmap_create_from_data(root_window, blank_data, 1, 1);
2850 cursor = gdk_cursor_new_from_pixmap(blank_mask, blank_mask,
2851 &color, &color, 0, 0);
2852 gdk_bitmap_unref(blank_mask);
Bram Moolenaar98921892016-02-23 17:14:37 +01002853#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002854
2855 return cursor;
2856}
2857
Bram Moolenaar071d4272004-06-13 20:20:40 +00002858 static void
2859mainwin_screen_changed_cb(GtkWidget *widget,
Bram Moolenaarb85cb212009-05-17 14:24:23 +00002860 GdkScreen *previous_screen UNUSED,
2861 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002862{
2863 if (!gtk_widget_has_screen(widget))
2864 return;
2865
2866 /*
Bram Moolenaar49325942007-05-10 19:19:59 +00002867 * Recreate the invisible mouse cursor.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002868 */
2869 if (gui.blank_pointer != NULL)
Bram Moolenaarb463e8d2017-06-05 15:07:09 +02002870#if GTK_CHECK_VERSION(3,0,0)
Bram Moolenaar98921892016-02-23 17:14:37 +01002871 g_object_unref(G_OBJECT(gui.blank_pointer));
Bram Moolenaarb463e8d2017-06-05 15:07:09 +02002872#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002873 gdk_cursor_unref(gui.blank_pointer);
Bram Moolenaarb463e8d2017-06-05 15:07:09 +02002874#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002875
2876 gui.blank_pointer = create_blank_pointer();
2877
Bram Moolenaar98921892016-02-23 17:14:37 +01002878 if (gui.pointer_hidden && gtk_widget_get_window(gui.drawarea) != NULL)
2879 gdk_window_set_cursor(gtk_widget_get_window(gui.drawarea),
2880 gui.blank_pointer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002881
2882 /*
2883 * Create a new PangoContext for this screen, and initialize it
2884 * with the current font if necessary.
2885 */
2886 if (gui.text_context != NULL)
2887 g_object_unref(gui.text_context);
2888
2889 gui.text_context = gtk_widget_create_pango_context(widget);
2890 pango_context_set_base_dir(gui.text_context, PANGO_DIRECTION_LTR);
2891
2892 if (gui.norm_font != NULL)
2893 {
Bram Moolenaar46c9c732004-12-12 11:37:09 +00002894 gui_mch_init_font(p_guifont, FALSE);
Bram Moolenaar8ac44152017-11-09 18:33:29 +01002895 gui_set_shellsize(TRUE, FALSE, RESIZE_BOTH);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002896 }
2897}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002898
2899/*
2900 * After the drawing area comes up, we calculate all colors and create the
2901 * dummy blank cursor.
2902 *
2903 * Don't try to set any VIM scrollbar sizes anywhere here. I'm relying on the
2904 * fact that the main VIM engine doesn't take them into account anywhere.
2905 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002906 static void
Bram Moolenaarb85cb212009-05-17 14:24:23 +00002907drawarea_realize_cb(GtkWidget *widget, gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002908{
2909 GtkWidget *sbar;
Bram Moolenaar98921892016-02-23 17:14:37 +01002910 GtkAllocation allocation;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002911
2912#ifdef FEAT_XIM
2913 xim_init();
2914#endif
2915 gui_mch_new_colors();
Bram Moolenaar98921892016-02-23 17:14:37 +01002916#if GTK_CHECK_VERSION(3,0,0)
2917 gui.surface = gdk_window_create_similar_surface(
2918 gtk_widget_get_window(widget),
2919 CAIRO_CONTENT_COLOR_ALPHA,
2920 gtk_widget_get_allocated_width(widget),
2921 gtk_widget_get_allocated_height(widget));
2922#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002923 gui.text_gc = gdk_gc_new(gui.drawarea->window);
Bram Moolenaar98921892016-02-23 17:14:37 +01002924#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002925
2926 gui.blank_pointer = create_blank_pointer();
2927 if (gui.pointer_hidden)
Bram Moolenaar98921892016-02-23 17:14:37 +01002928 gdk_window_set_cursor(gtk_widget_get_window(widget), gui.blank_pointer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002929
Bram Moolenaar30613902019-12-01 22:11:18 +01002930 // get the actual size of the scrollbars, if they are realized
Bram Moolenaar071d4272004-06-13 20:20:40 +00002931 sbar = firstwin->w_scrollbars[SBAR_LEFT].id;
2932 if (!sbar || (!gui.which_scrollbars[SBAR_LEFT]
2933 && firstwin->w_scrollbars[SBAR_RIGHT].id))
2934 sbar = firstwin->w_scrollbars[SBAR_RIGHT].id;
Bram Moolenaar98921892016-02-23 17:14:37 +01002935 gtk_widget_get_allocation(sbar, &allocation);
2936 if (sbar && gtk_widget_get_realized(sbar) && allocation.width)
2937 gui.scrollbar_width = allocation.width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002938
2939 sbar = gui.bottom_sbar.id;
Bram Moolenaar98921892016-02-23 17:14:37 +01002940 if (sbar && gtk_widget_get_realized(sbar) && allocation.height)
2941 gui.scrollbar_height = allocation.height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002942}
2943
2944/*
2945 * Properly clean up on shutdown.
2946 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002947 static void
Bram Moolenaarb85cb212009-05-17 14:24:23 +00002948drawarea_unrealize_cb(GtkWidget *widget UNUSED, gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002949{
Bram Moolenaar30613902019-12-01 22:11:18 +01002950 // Don't write messages to the GUI anymore
Bram Moolenaar071d4272004-06-13 20:20:40 +00002951 full_screen = FALSE;
2952
2953#ifdef FEAT_XIM
2954 im_shutdown();
2955#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002956 if (gui.ascii_glyphs != NULL)
2957 {
2958 pango_glyph_string_free(gui.ascii_glyphs);
2959 gui.ascii_glyphs = NULL;
2960 }
2961 if (gui.ascii_font != NULL)
2962 {
2963 g_object_unref(gui.ascii_font);
2964 gui.ascii_font = NULL;
2965 }
2966 g_object_unref(gui.text_context);
2967 gui.text_context = NULL;
2968
Bram Moolenaar98921892016-02-23 17:14:37 +01002969#if GTK_CHECK_VERSION(3,0,0)
2970 if (gui.surface != NULL)
2971 {
2972 cairo_surface_destroy(gui.surface);
2973 gui.surface = NULL;
2974 }
2975#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002976 g_object_unref(gui.text_gc);
2977 gui.text_gc = NULL;
Bram Moolenaar98921892016-02-23 17:14:37 +01002978#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002979
Bram Moolenaar98921892016-02-23 17:14:37 +01002980#if GTK_CHECK_VERSION(3,0,0)
2981 g_object_unref(G_OBJECT(gui.blank_pointer));
2982#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002983 gdk_cursor_unref(gui.blank_pointer);
Bram Moolenaar98921892016-02-23 17:14:37 +01002984#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002985 gui.blank_pointer = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002986}
2987
Bram Moolenaara859f042016-11-17 19:11:55 +01002988#if GTK_CHECK_VERSION(3,22,2)
2989 static void
2990drawarea_style_updated_cb(GtkWidget *widget UNUSED,
2991 gpointer data UNUSED)
2992#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002993 static void
Bram Moolenaarb85cb212009-05-17 14:24:23 +00002994drawarea_style_set_cb(GtkWidget *widget UNUSED,
2995 GtkStyle *previous_style UNUSED,
2996 gpointer data UNUSED)
Bram Moolenaara859f042016-11-17 19:11:55 +01002997#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002998{
2999 gui_mch_new_colors();
3000}
3001
Bram Moolenaar98921892016-02-23 17:14:37 +01003002#if GTK_CHECK_VERSION(3,0,0)
3003 static gboolean
3004drawarea_configure_event_cb(GtkWidget *widget,
3005 GdkEventConfigure *event,
3006 gpointer data UNUSED)
3007{
3008 static int cur_width = 0;
3009 static int cur_height = 0;
3010
3011 g_return_val_if_fail(event
3012 && event->width >= 1 && event->height >= 1, TRUE);
3013
Bram Moolenaar182707a2016-11-21 20:55:58 +01003014# if GTK_CHECK_VERSION(3,22,2) && !GTK_CHECK_VERSION(3,22,4)
Bram Moolenaar30613902019-12-01 22:11:18 +01003015 // As of 3.22.2, GdkWindows have started distributing configure events to
3016 // their "native" children (https://git.gnome.org/browse/gtk+/commit/?h=gtk-3-22&id=12579fe71b3b8f79eb9c1b80e429443bcc437dd0).
3017 //
3018 // As can be seen from the implementation of move_native_children() and
3019 // configure_native_child() in gdkwindow.c, those functions actually
3020 // propagate configure events to every child, failing to distinguish
3021 // "native" one from non-native one.
3022 //
3023 // Naturally, configure events propagated to here like that are fallacious
3024 // and, as a matter of fact, they trigger a geometric collapse of
3025 // gui.drawarea in fullscreen and maximized modes.
3026 //
3027 // To filter out such nuisance events, we are making use of the fact that
3028 // the field send_event of such GdkEventConfigures is set to FALSE in
3029 // configure_native_child().
3030 //
3031 // Obviously, this is a terrible hack making GVim depend on GTK's
3032 // implementation details. Therefore, watch out any relevant internal
3033 // changes happening in GTK in the feature (sigh).
3034 //
3035 // Follow-up
3036 // After a few weeks later, the GdkWindow change mentioned above was
3037 // reverted (https://git.gnome.org/browse/gtk+/commit/?h=gtk-3-22&id=f70039cb9603a02d2369fec4038abf40a1711155).
3038 // The corresponding official release is 3.22.4.
Bram Moolenaara859f042016-11-17 19:11:55 +01003039 if (event->send_event == FALSE)
3040 return TRUE;
3041# endif
3042
Bram Moolenaar98921892016-02-23 17:14:37 +01003043 if (event->width == cur_width && event->height == cur_height)
3044 return TRUE;
3045
3046 cur_width = event->width;
3047 cur_height = event->height;
3048
3049 if (gui.surface != NULL)
3050 cairo_surface_destroy(gui.surface);
3051
3052 gui.surface = gdk_window_create_similar_surface(
3053 gtk_widget_get_window(widget),
3054 CAIRO_CONTENT_COLOR_ALPHA,
3055 event->width, event->height);
3056
3057 gtk_widget_queue_draw(widget);
3058
3059 return TRUE;
3060}
3061#endif
3062
Bram Moolenaar071d4272004-06-13 20:20:40 +00003063/*
3064 * Callback routine for the "delete_event" signal on the toplevel window.
3065 * Tries to vim gracefully, or refuses to exit with changed buffers.
3066 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003067 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +00003068delete_event_cb(GtkWidget *widget UNUSED,
3069 GdkEventAny *event UNUSED,
3070 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003071{
3072 gui_shell_closed();
3073 return TRUE;
3074}
3075
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003076#if defined(FEAT_MENU) || defined(FEAT_TOOLBAR) || defined(FEAT_GUI_TABLINE)
3077 static int
3078get_item_dimensions(GtkWidget *widget, GtkOrientation orientation)
3079{
Bram Moolenaarc4a249a2017-01-30 22:56:48 +01003080# ifdef FEAT_GUI_GNOME
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003081 GtkOrientation item_orientation = GTK_ORIENTATION_HORIZONTAL;
3082
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003083 if (using_gnome && widget != NULL)
3084 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003085 GtkWidget *parent;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003086 BonoboDockItem *dockitem;
3087
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003088 parent = gtk_widget_get_parent(widget);
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003089 if (G_TYPE_FROM_INSTANCE(parent) == BONOBO_TYPE_DOCK_ITEM)
3090 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003091 // Only menu & toolbar are dock items. Could tabline be?
3092 // Seem to be only the 2 defined in GNOME
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003093 widget = parent;
3094 dockitem = BONOBO_DOCK_ITEM(widget);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003095
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003096 if (dockitem == NULL || dockitem->is_floating)
3097 return 0;
3098 item_orientation = bonobo_dock_item_get_orientation(dockitem);
3099 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003100 }
Bram Moolenaarc4a249a2017-01-30 22:56:48 +01003101# else
3102# define item_orientation GTK_ORIENTATION_HORIZONTAL
Bram Moolenaar98921892016-02-23 17:14:37 +01003103# endif
Bram Moolenaarc4a249a2017-01-30 22:56:48 +01003104
Bram Moolenaar98921892016-02-23 17:14:37 +01003105 if (widget != NULL
3106 && item_orientation == orientation
3107 && gtk_widget_get_realized(widget)
3108 && gtk_widget_get_visible(widget))
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003109 {
Bram Moolenaar664323e2018-09-18 22:30:07 +02003110# if GTK_CHECK_VERSION(3,0,0) || !defined(FEAT_GUI_GNOME)
Bram Moolenaar98921892016-02-23 17:14:37 +01003111 GtkAllocation allocation;
3112
3113 gtk_widget_get_allocation(widget, &allocation);
Bram Moolenaarc4a249a2017-01-30 22:56:48 +01003114 return allocation.height;
Bram Moolenaar98921892016-02-23 17:14:37 +01003115# else
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003116 if (orientation == GTK_ORIENTATION_HORIZONTAL)
3117 return widget->allocation.height;
3118 else
3119 return widget->allocation.width;
Bram Moolenaar98921892016-02-23 17:14:37 +01003120# endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003121 }
3122 return 0;
3123}
3124#endif
3125
3126 static int
3127get_menu_tool_width(void)
3128{
3129 int width = 0;
3130
Bram Moolenaar30613902019-12-01 22:11:18 +01003131#ifdef FEAT_GUI_GNOME // these are never vertical without GNOME
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003132# ifdef FEAT_MENU
3133 width += get_item_dimensions(gui.menubar, GTK_ORIENTATION_VERTICAL);
3134# endif
3135# ifdef FEAT_TOOLBAR
3136 width += get_item_dimensions(gui.toolbar, GTK_ORIENTATION_VERTICAL);
3137# endif
3138# ifdef FEAT_GUI_TABLINE
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00003139 if (gui.tabline != NULL)
3140 width += get_item_dimensions(gui.tabline, GTK_ORIENTATION_VERTICAL);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003141# endif
3142#endif
3143
3144 return width;
3145}
3146
3147 static int
3148get_menu_tool_height(void)
3149{
3150 int height = 0;
3151
3152#ifdef FEAT_MENU
3153 height += get_item_dimensions(gui.menubar, GTK_ORIENTATION_HORIZONTAL);
3154#endif
3155#ifdef FEAT_TOOLBAR
3156 height += get_item_dimensions(gui.toolbar, GTK_ORIENTATION_HORIZONTAL);
3157#endif
3158#ifdef FEAT_GUI_TABLINE
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00003159 if (gui.tabline != NULL)
3160 height += get_item_dimensions(gui.tabline, GTK_ORIENTATION_HORIZONTAL);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003161#endif
3162
3163 return height;
3164}
3165
Bram Moolenaar30613902019-12-01 22:11:18 +01003166// This controls whether we can set the real window hints at
3167// start-up when in a GtkPlug.
3168// 0 = normal processing (default)
3169// 1 = init. hints set, no-one's tried to reset since last check
3170// 2 = init. hints set, attempt made to change hints
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003171static int init_window_hints_state = 0;
3172
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003173 static void
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003174update_window_manager_hints(int force_width, int force_height)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003175{
3176 static int old_width = 0;
3177 static int old_height = 0;
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003178 static int old_min_width = 0;
3179 static int old_min_height = 0;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003180 static int old_char_width = 0;
3181 static int old_char_height = 0;
3182
3183 int width;
3184 int height;
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003185 int min_width;
3186 int min_height;
3187
Bram Moolenaar30613902019-12-01 22:11:18 +01003188 // At start-up, don't try to set the hints until the initial
3189 // values have been used (those that dictate our initial size)
3190 // Let forced (i.e., correct) values through always.
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003191 if (!(force_width && force_height) && init_window_hints_state > 0)
3192 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003193 // Don't do it!
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003194 init_window_hints_state = 2;
3195 return;
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003196 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003197
Bram Moolenaar30613902019-12-01 22:11:18 +01003198 // This also needs to be done when the main window isn't there yet,
3199 // otherwise the hints don't work.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003200 width = gui_get_base_width();
3201 height = gui_get_base_height();
3202# ifdef FEAT_MENU
3203 height += tabline_height() * gui.char_height;
3204# endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003205 width += get_menu_tool_width();
3206 height += get_menu_tool_height();
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003207
Bram Moolenaar30613902019-12-01 22:11:18 +01003208 // GtkSockets use GtkPlug's [gui,mainwin] min-size hints to determine
3209 // their actual widget size. When we set our size ourselves (e.g.,
3210 // 'set columns=' or init. -geom) we briefly set the min. to the size
3211 // we wish to be instead of the legitimate minimum so that we actually
3212 // resize correctly.
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003213 if (force_width && force_height)
3214 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003215 min_width = force_width;
3216 min_height = force_height;
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003217 }
3218 else
3219 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003220 min_width = width + MIN_COLUMNS * gui.char_width;
3221 min_height = height + MIN_LINES * gui.char_height;
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003222 }
3223
Bram Moolenaar30613902019-12-01 22:11:18 +01003224 // Avoid an expose event when the size didn't change.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003225 if (width != old_width
3226 || height != old_height
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003227 || min_width != old_min_width
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003228 || min_height != old_min_height
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003229 || gui.char_width != old_char_width
3230 || gui.char_height != old_char_height)
3231 {
3232 GdkGeometry geometry;
3233 GdkWindowHints geometry_mask;
3234
3235 geometry.width_inc = gui.char_width;
3236 geometry.height_inc = gui.char_height;
3237 geometry.base_width = width;
3238 geometry.base_height = height;
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003239 geometry.min_width = min_width;
3240 geometry.min_height = min_height;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003241 geometry_mask = GDK_HINT_BASE_SIZE|GDK_HINT_RESIZE_INC
3242 |GDK_HINT_MIN_SIZE;
Bram Moolenaar30613902019-12-01 22:11:18 +01003243 // Using gui.formwin as geometry widget doesn't work as expected
3244 // with GTK+ 2 -- dunno why. Presumably all the resizing hacks
Bram Moolenaarc0da5402023-05-16 22:15:51 +01003245 // in Vim confuse GTK+. For GTK 3 the second argument should be NULL
3246 // to make the width/height inc works, despite the docs saying
3247 // something else.
3248 gtk_window_set_geometry_hints(GTK_WINDOW(gui.mainwin), NULL,
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003249 &geometry, geometry_mask);
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003250 old_width = width;
3251 old_height = height;
3252 old_min_width = min_width;
3253 old_min_height = min_height;
3254 old_char_width = gui.char_width;
3255 old_char_height = gui.char_height;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003256 }
3257}
3258
Bram Moolenaar50bf7ce2019-09-15 13:17:00 +02003259#if defined(FEAT_GUI_DARKTHEME) || defined(PROTO)
3260 void
3261gui_mch_set_dark_theme(int dark)
3262{
3263# if GTK_CHECK_VERSION(3,0,0)
3264 GtkSettings *gtk_settings;
3265
3266 gtk_settings = gtk_settings_get_for_screen(gdk_screen_get_default());
3267 g_object_set(gtk_settings, "gtk-application-prefer-dark-theme", (gboolean)dark, NULL);
3268# endif
3269}
Bram Moolenaar30613902019-12-01 22:11:18 +01003270#endif // FEAT_GUI_DARKTHEME
Bram Moolenaar50bf7ce2019-09-15 13:17:00 +02003271
Bram Moolenaar071d4272004-06-13 20:20:40 +00003272#ifdef FEAT_TOOLBAR
3273
Bram Moolenaar071d4272004-06-13 20:20:40 +00003274/*
3275 * This extra effort wouldn't be necessary if we only used stock icons in the
3276 * toolbar, as we do for all builtin icons. But user-defined toolbar icons
3277 * shouldn't be treated differently, thus we do need this.
3278 */
3279 static void
3280icon_size_changed_foreach(GtkWidget *widget, gpointer user_data)
3281{
3282 if (GTK_IS_IMAGE(widget))
3283 {
3284 GtkImage *image = (GtkImage *)widget;
3285
Bram Moolenaar98921892016-02-23 17:14:37 +01003286# if GTK_CHECK_VERSION(3,10,0)
3287 if (gtk_image_get_storage_type(image) == GTK_IMAGE_ICON_NAME)
3288 {
3289 const GtkIconSize icon_size = GPOINTER_TO_INT(user_data);
3290 const gchar *icon_name;
3291
3292 gtk_image_get_icon_name(image, &icon_name, NULL);
Bram Moolenaar81a4cf42020-09-09 19:05:13 +02003293 image = (GtkImage *)gtk_image_new_from_icon_name(
3294 icon_name, icon_size);
Bram Moolenaar98921892016-02-23 17:14:37 +01003295 }
3296# else
Bram Moolenaar30613902019-12-01 22:11:18 +01003297 // User-defined icons are stored in a GtkIconSet
Bram Moolenaar071d4272004-06-13 20:20:40 +00003298 if (gtk_image_get_storage_type(image) == GTK_IMAGE_ICON_SET)
3299 {
3300 GtkIconSet *icon_set;
3301 GtkIconSize icon_size;
3302
3303 gtk_image_get_icon_set(image, &icon_set, &icon_size);
3304 icon_size = (GtkIconSize)(long)user_data;
3305
3306 gtk_icon_set_ref(icon_set);
3307 gtk_image_set_from_icon_set(image, icon_set, icon_size);
3308 gtk_icon_set_unref(icon_set);
3309 }
Bram Moolenaar98921892016-02-23 17:14:37 +01003310# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003311 }
3312 else if (GTK_IS_CONTAINER(widget))
3313 {
3314 gtk_container_foreach((GtkContainer *)widget,
3315 &icon_size_changed_foreach,
3316 user_data);
3317 }
3318}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003319
3320 static void
3321set_toolbar_style(GtkToolbar *toolbar)
3322{
3323 GtkToolbarStyle style;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003324 GtkIconSize size;
3325 GtkIconSize oldsize;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003326
Bram Moolenaar071d4272004-06-13 20:20:40 +00003327 if ((toolbar_flags & (TOOLBAR_TEXT | TOOLBAR_ICONS | TOOLBAR_HORIZ))
3328 == (TOOLBAR_TEXT | TOOLBAR_ICONS | TOOLBAR_HORIZ))
3329 style = GTK_TOOLBAR_BOTH_HORIZ;
Bram Moolenaar182c5be2010-06-25 05:37:59 +02003330 else if ((toolbar_flags & (TOOLBAR_TEXT | TOOLBAR_ICONS))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003331 == (TOOLBAR_TEXT | TOOLBAR_ICONS))
3332 style = GTK_TOOLBAR_BOTH;
3333 else if (toolbar_flags & TOOLBAR_TEXT)
3334 style = GTK_TOOLBAR_TEXT;
3335 else
3336 style = GTK_TOOLBAR_ICONS;
3337
3338 gtk_toolbar_set_style(toolbar, style);
Bram Moolenaar98921892016-02-23 17:14:37 +01003339# if !GTK_CHECK_VERSION(3,0,0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003340 gtk_toolbar_set_tooltips(toolbar, (toolbar_flags & TOOLBAR_TOOLTIPS) != 0);
Bram Moolenaar98921892016-02-23 17:14:37 +01003341# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003342
Bram Moolenaar071d4272004-06-13 20:20:40 +00003343 switch (tbis_flags)
3344 {
3345 case TBIS_TINY: size = GTK_ICON_SIZE_MENU; break;
3346 case TBIS_SMALL: size = GTK_ICON_SIZE_SMALL_TOOLBAR; break;
3347 case TBIS_MEDIUM: size = GTK_ICON_SIZE_BUTTON; break;
3348 case TBIS_LARGE: size = GTK_ICON_SIZE_LARGE_TOOLBAR; break;
Bram Moolenaarbeb003b2016-03-08 22:47:17 +01003349 case TBIS_HUGE: size = GTK_ICON_SIZE_DND; break;
3350 case TBIS_GIANT: size = GTK_ICON_SIZE_DIALOG; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003351 default: size = GTK_ICON_SIZE_INVALID; break;
3352 }
3353 oldsize = gtk_toolbar_get_icon_size(toolbar);
3354
3355 if (size == GTK_ICON_SIZE_INVALID)
3356 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003357 // Let global user preferences decide the icon size.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003358 gtk_toolbar_unset_icon_size(toolbar);
3359 size = gtk_toolbar_get_icon_size(toolbar);
3360 }
3361 if (size != oldsize)
3362 {
3363 gtk_container_foreach(GTK_CONTAINER(toolbar),
3364 &icon_size_changed_foreach,
3365 GINT_TO_POINTER((int)size));
3366 }
3367 gtk_toolbar_set_icon_size(toolbar, size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003368}
3369
Bram Moolenaar30613902019-12-01 22:11:18 +01003370#endif // FEAT_TOOLBAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00003371
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003372#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
3373static int ignore_tabline_evt = FALSE;
Bram Moolenaara5621492006-02-25 21:55:24 +00003374static GtkWidget *tabline_menu;
Bram Moolenaar98921892016-02-23 17:14:37 +01003375# if !GTK_CHECK_VERSION(3,0,0)
Bram Moolenaar437df8f2006-04-27 21:47:44 +00003376static GtkTooltips *tabline_tooltip;
Bram Moolenaar98921892016-02-23 17:14:37 +01003377# endif
Bram Moolenaar30613902019-12-01 22:11:18 +01003378static int clicked_page; // page clicked in tab line
Bram Moolenaara5621492006-02-25 21:55:24 +00003379
3380/*
3381 * Handle selecting an item in the tab line popup menu.
3382 */
Bram Moolenaara5621492006-02-25 21:55:24 +00003383 static void
Bram Moolenaarb85cb212009-05-17 14:24:23 +00003384tabline_menu_handler(GtkMenuItem *item UNUSED, gpointer user_data)
Bram Moolenaara5621492006-02-25 21:55:24 +00003385{
Bram Moolenaar30613902019-12-01 22:11:18 +01003386 // Add the string cmd into input buffer
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00003387 send_tabline_menu_event(clicked_page, (int)(long)user_data);
Bram Moolenaara5621492006-02-25 21:55:24 +00003388}
3389
Bram Moolenaar8ea91232006-04-28 22:41:43 +00003390 static void
3391add_tabline_menu_item(GtkWidget *menu, char_u *text, int resp)
3392{
3393 GtkWidget *item;
3394 char_u *utf_text;
3395
3396 utf_text = CONVERT_TO_UTF8(text);
3397 item = gtk_menu_item_new_with_label((const char *)utf_text);
3398 gtk_widget_show(item);
3399 CONVERT_TO_UTF8_FREE(utf_text);
3400
3401 gtk_container_add(GTK_CONTAINER(menu), item);
Bram Moolenaar98921892016-02-23 17:14:37 +01003402 g_signal_connect(G_OBJECT(item), "activate",
3403 G_CALLBACK(tabline_menu_handler),
3404 GINT_TO_POINTER(resp));
Bram Moolenaar8ea91232006-04-28 22:41:43 +00003405}
3406
Bram Moolenaara5621492006-02-25 21:55:24 +00003407/*
Bram Moolenaara5621492006-02-25 21:55:24 +00003408 * Create a menu for the tab line.
3409 */
3410 static GtkWidget *
3411create_tabline_menu(void)
3412{
Bram Moolenaar8ea91232006-04-28 22:41:43 +00003413 GtkWidget *menu;
Bram Moolenaara5621492006-02-25 21:55:24 +00003414
3415 menu = gtk_menu_new();
Bram Moolenaar29547192018-12-11 20:39:19 +01003416 add_tabline_menu_item(menu, (char_u *)_("Close tab"), TABLINE_MENU_CLOSE);
Bram Moolenaar8ea91232006-04-28 22:41:43 +00003417 add_tabline_menu_item(menu, (char_u *)_("New tab"), TABLINE_MENU_NEW);
3418 add_tabline_menu_item(menu, (char_u *)_("Open Tab..."), TABLINE_MENU_OPEN);
Bram Moolenaara5621492006-02-25 21:55:24 +00003419
3420 return menu;
3421}
3422
3423 static gboolean
3424on_tabline_menu(GtkWidget *widget, GdkEvent *event)
3425{
Bram Moolenaar30613902019-12-01 22:11:18 +01003426 // Was this button press event ?
Bram Moolenaara5621492006-02-25 21:55:24 +00003427 if (event->type == GDK_BUTTON_PRESS)
3428 {
3429 GdkEventButton *bevent = (GdkEventButton *)event;
3430 int x = bevent->x;
Bram Moolenaar8ea91232006-04-28 22:41:43 +00003431 int y = bevent->y;
3432 GtkWidget *tabwidget;
3433 GdkWindow *tabwin;
Bram Moolenaara5621492006-02-25 21:55:24 +00003434
Bram Moolenaar30613902019-12-01 22:11:18 +01003435 // When ignoring events return TRUE so that the selected page doesn't
3436 // change.
Martin Tournoij7904fa42022-10-04 16:28:45 +01003437 if (hold_gui_events || cmdwin_type != 0)
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003438 return TRUE;
3439
Bram Moolenaar98921892016-02-23 17:14:37 +01003440 tabwin = gui_gtk_window_at_position(gui.mainwin, &x, &y);
Bram Moolenaar98921892016-02-23 17:14:37 +01003441
Bram Moolenaar8ea91232006-04-28 22:41:43 +00003442 gdk_window_get_user_data(tabwin, (gpointer)&tabwidget);
Bram Moolenaar98921892016-02-23 17:14:37 +01003443 clicked_page = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(tabwidget),
3444 "tab_num"));
Bram Moolenaara5621492006-02-25 21:55:24 +00003445
Bram Moolenaar30613902019-12-01 22:11:18 +01003446 // If the event was generated for 3rd button popup the menu.
Bram Moolenaara5621492006-02-25 21:55:24 +00003447 if (bevent->button == 3)
3448 {
Bram Moolenaara859f042016-11-17 19:11:55 +01003449# if GTK_CHECK_VERSION(3,22,2)
3450 gtk_menu_popup_at_pointer(GTK_MENU(widget), event);
3451# else
Bram Moolenaara5621492006-02-25 21:55:24 +00003452 gtk_menu_popup(GTK_MENU(widget), NULL, NULL, NULL, NULL,
3453 bevent->button, bevent->time);
Bram Moolenaara859f042016-11-17 19:11:55 +01003454# endif
Bram Moolenaar30613902019-12-01 22:11:18 +01003455 // We handled the event.
Bram Moolenaara5621492006-02-25 21:55:24 +00003456 return TRUE;
3457 }
Bram Moolenaar96351572006-05-05 21:16:59 +00003458 else if (bevent->button == 1)
Bram Moolenaareddf53b2006-02-27 00:11:10 +00003459 {
Bram Moolenaar96351572006-05-05 21:16:59 +00003460 if (clicked_page == 0)
3461 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003462 // Click after all tabs moves to next tab page. When "x" is
3463 // small guess it's the left button.
Bram Moolenaara3f41662010-07-11 19:01:06 +02003464 send_tabline_event(x < 50 ? -1 : 0);
Bram Moolenaar96351572006-05-05 21:16:59 +00003465 }
Bram Moolenaareddf53b2006-02-27 00:11:10 +00003466 }
regomne3bdef102022-09-26 20:48:32 +01003467 else if (bevent->button == 2)
3468 {
3469 if (clicked_page != 0)
3470 // Middle mouse click on tabpage label closes that tab.
3471 send_tabline_menu_event(clicked_page, TABLINE_MENU_CLOSE);
3472 }
Bram Moolenaara5621492006-02-25 21:55:24 +00003473 }
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003474
Bram Moolenaar30613902019-12-01 22:11:18 +01003475 // We didn't handle the event.
Bram Moolenaara5621492006-02-25 21:55:24 +00003476 return FALSE;
3477}
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003478
3479/*
3480 * Handle selecting one of the tabs.
3481 */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003482 static void
3483on_select_tab(
Bram Moolenaarb85cb212009-05-17 14:24:23 +00003484 GtkNotebook *notebook UNUSED,
Bram Moolenaar98921892016-02-23 17:14:37 +01003485 gpointer *page UNUSED,
Bram Moolenaar89d40322006-08-29 15:30:07 +00003486 gint idx,
Bram Moolenaarb85cb212009-05-17 14:24:23 +00003487 gpointer data UNUSED)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003488{
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003489 if (!ignore_tabline_evt)
Bram Moolenaara3f41662010-07-11 19:01:06 +02003490 send_tabline_event(idx + 1);
Bram Moolenaarca05aa22017-10-22 15:36:14 +02003491}
3492
Bram Moolenaar92cbf622018-09-19 22:40:03 +02003493# if GTK_CHECK_VERSION(2,10,0)
Bram Moolenaarca05aa22017-10-22 15:36:14 +02003494/*
3495 * Handle reordering the tabs (using D&D).
3496 */
3497 static void
3498on_tab_reordered(
3499 GtkNotebook *notebook UNUSED,
Bram Moolenaarca05aa22017-10-22 15:36:14 +02003500 gpointer *page UNUSED,
Bram Moolenaarca05aa22017-10-22 15:36:14 +02003501 gint idx,
3502 gpointer data UNUSED)
3503{
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00003504 if (ignore_tabline_evt)
3505 return;
3506
3507 if ((tabpage_index(curtab) - 1) < idx)
3508 tabpage_move(idx + 1);
3509 else
3510 tabpage_move(idx);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003511}
Bram Moolenaar92cbf622018-09-19 22:40:03 +02003512# endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003513
3514/*
3515 * Show or hide the tabline.
3516 */
3517 void
3518gui_mch_show_tabline(int showit)
3519{
3520 if (gui.tabline == NULL)
3521 return;
3522
3523 if (!showit != !gtk_notebook_get_show_tabs(GTK_NOTEBOOK(gui.tabline)))
3524 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003525 // Note: this may cause a resize event
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003526 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(gui.tabline), showit);
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003527 update_window_manager_hints(0, 0);
Bram Moolenaar96351572006-05-05 21:16:59 +00003528 if (showit)
Bram Moolenaar98921892016-02-23 17:14:37 +01003529 gtk_widget_set_can_focus(GTK_WIDGET(gui.tabline), FALSE);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003530 }
Bram Moolenaar96351572006-05-05 21:16:59 +00003531
3532 gui_mch_update();
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003533}
3534
3535/*
Bram Moolenaar3517bb12006-03-03 22:58:45 +00003536 * Return TRUE when tabline is displayed.
3537 */
3538 int
3539gui_mch_showing_tabline(void)
3540{
3541 return gui.tabline != NULL
Bram Moolenaar182c5be2010-06-25 05:37:59 +02003542 && gtk_notebook_get_show_tabs(GTK_NOTEBOOK(gui.tabline));
Bram Moolenaar3517bb12006-03-03 22:58:45 +00003543}
3544
3545/*
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003546 * Update the labels of the tabline.
3547 */
3548 void
3549gui_mch_update_tabline(void)
3550{
3551 GtkWidget *page;
Bram Moolenaar437df8f2006-04-27 21:47:44 +00003552 GtkWidget *event_box;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003553 GtkWidget *label;
3554 tabpage_T *tp;
3555 int nr = 0;
Bram Moolenaar8ea91232006-04-28 22:41:43 +00003556 int tab_num;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003557 int curtabidx = 0;
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003558 char_u *labeltext;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003559
3560 if (gui.tabline == NULL)
3561 return;
3562
3563 ignore_tabline_evt = TRUE;
3564
Bram Moolenaar30613902019-12-01 22:11:18 +01003565 // Add a label for each tab page. They all contain the same text area.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003566 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next, ++nr)
3567 {
3568 if (tp == curtab)
3569 curtabidx = nr;
3570
Bram Moolenaar8ea91232006-04-28 22:41:43 +00003571 tab_num = nr + 1;
3572
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003573 page = gtk_notebook_get_nth_page(GTK_NOTEBOOK(gui.tabline), nr);
3574 if (page == NULL)
3575 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003576 // Add notebook page
Bram Moolenaar98921892016-02-23 17:14:37 +01003577# if GTK_CHECK_VERSION(3,2,0)
3578 page = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
3579 gtk_box_set_homogeneous(GTK_BOX(page), FALSE);
3580# else
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003581 page = gtk_vbox_new(FALSE, 0);
Bram Moolenaar98921892016-02-23 17:14:37 +01003582# endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003583 gtk_widget_show(page);
Bram Moolenaar437df8f2006-04-27 21:47:44 +00003584 event_box = gtk_event_box_new();
3585 gtk_widget_show(event_box);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003586 label = gtk_label_new("-Empty-");
Bram Moolenaar98921892016-02-23 17:14:37 +01003587# if !GTK_CHECK_VERSION(3,14,0)
Bram Moolenaar8ea91232006-04-28 22:41:43 +00003588 gtk_misc_set_padding(GTK_MISC(label), 2, 2);
Bram Moolenaar98921892016-02-23 17:14:37 +01003589# endif
Bram Moolenaar437df8f2006-04-27 21:47:44 +00003590 gtk_container_add(GTK_CONTAINER(event_box), label);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003591 gtk_widget_show(label);
3592 gtk_notebook_insert_page(GTK_NOTEBOOK(gui.tabline),
3593 page,
Bram Moolenaar437df8f2006-04-27 21:47:44 +00003594 event_box,
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003595 nr++);
Bram Moolenaar92cbf622018-09-19 22:40:03 +02003596# if GTK_CHECK_VERSION(2,10,0)
Bram Moolenaarca05aa22017-10-22 15:36:14 +02003597 gtk_notebook_set_tab_reorderable(GTK_NOTEBOOK(gui.tabline),
3598 page,
3599 TRUE);
Bram Moolenaar92cbf622018-09-19 22:40:03 +02003600# endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003601 }
3602
Bram Moolenaar437df8f2006-04-27 21:47:44 +00003603 event_box = gtk_notebook_get_tab_label(GTK_NOTEBOOK(gui.tabline), page);
Bram Moolenaar98921892016-02-23 17:14:37 +01003604 g_object_set_data(G_OBJECT(event_box), "tab_num",
3605 GINT_TO_POINTER(tab_num));
Bram Moolenaar98921892016-02-23 17:14:37 +01003606 label = gtk_bin_get_child(GTK_BIN(event_box));
Bram Moolenaar57657d82006-04-21 22:12:41 +00003607 get_tabline_label(tp, FALSE);
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003608 labeltext = CONVERT_TO_UTF8(NameBuff);
Bram Moolenaar437df8f2006-04-27 21:47:44 +00003609 gtk_label_set_text(GTK_LABEL(label), (const char *)labeltext);
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003610 CONVERT_TO_UTF8_FREE(labeltext);
Bram Moolenaar437df8f2006-04-27 21:47:44 +00003611
Bram Moolenaar437df8f2006-04-27 21:47:44 +00003612 get_tabline_label(tp, TRUE);
3613 labeltext = CONVERT_TO_UTF8(NameBuff);
Bram Moolenaar98921892016-02-23 17:14:37 +01003614# if GTK_CHECK_VERSION(3,0,0)
3615 gtk_widget_set_tooltip_text(event_box, (const gchar *)labeltext);
3616# else
Bram Moolenaar437df8f2006-04-27 21:47:44 +00003617 gtk_tooltips_set_tip(GTK_TOOLTIPS(tabline_tooltip), event_box,
3618 (const char *)labeltext, NULL);
Bram Moolenaar98921892016-02-23 17:14:37 +01003619# endif
Bram Moolenaar437df8f2006-04-27 21:47:44 +00003620 CONVERT_TO_UTF8_FREE(labeltext);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003621 }
3622
Bram Moolenaar30613902019-12-01 22:11:18 +01003623 // Remove any old labels.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003624 while (gtk_notebook_get_nth_page(GTK_NOTEBOOK(gui.tabline), nr) != NULL)
3625 gtk_notebook_remove_page(GTK_NOTEBOOK(gui.tabline), nr);
3626
Bram Moolenaar98921892016-02-23 17:14:37 +01003627 if (gtk_notebook_get_current_page(GTK_NOTEBOOK(gui.tabline)) != curtabidx)
3628 gtk_notebook_set_current_page(GTK_NOTEBOOK(gui.tabline), curtabidx);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003629
Bram Moolenaar30613902019-12-01 22:11:18 +01003630 // Make sure everything is in place before drawing text.
Bram Moolenaar8ea91232006-04-28 22:41:43 +00003631 gui_mch_update();
3632
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003633 ignore_tabline_evt = FALSE;
3634}
3635
3636/*
3637 * Set the current tab to "nr". First tab is 1.
3638 */
3639 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003640gui_mch_set_curtab(int nr)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003641{
3642 if (gui.tabline == NULL)
3643 return;
3644
3645 ignore_tabline_evt = TRUE;
Bram Moolenaar98921892016-02-23 17:14:37 +01003646 if (gtk_notebook_get_current_page(GTK_NOTEBOOK(gui.tabline)) != nr - 1)
3647 gtk_notebook_set_current_page(GTK_NOTEBOOK(gui.tabline), nr - 1);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003648 ignore_tabline_evt = FALSE;
3649}
3650
Bram Moolenaar30613902019-12-01 22:11:18 +01003651#endif // FEAT_GUI_TABLINE
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003652
Bram Moolenaar071d4272004-06-13 20:20:40 +00003653/*
Bram Moolenaara76638f2010-06-05 12:49:46 +02003654 * Add selection targets for PRIMARY and CLIPBOARD selections.
3655 */
3656 void
lilydjwg94ff09a2024-01-29 20:14:01 +01003657gui_gtk_set_selection_targets(GdkAtom selection)
Bram Moolenaara76638f2010-06-05 12:49:46 +02003658{
3659 int i, j = 0;
lilydjwg94ff09a2024-01-29 20:14:01 +01003660 static int n_targets = N_SELECTION_TARGETS;
3661 static GtkTargetEntry targets[N_SELECTION_TARGETS];
Bram Moolenaara76638f2010-06-05 12:49:46 +02003662
lilydjwg94ff09a2024-01-29 20:14:01 +01003663 if (targets[0].target == NULL)
Bram Moolenaara76638f2010-06-05 12:49:46 +02003664 {
lilydjwg94ff09a2024-01-29 20:14:01 +01003665 for (i = 0; i < (int)N_SELECTION_TARGETS; ++i)
3666 {
3667 // OpenOffice tries to use TARGET_HTML and fails when we don't
3668 // return something, instead of trying another target. Therefore only
3669 // offer TARGET_HTML when it works.
3670 if (!clip_html && selection_targets[i].info == TARGET_HTML)
3671 n_targets--;
3672 else
3673 targets[j++] = selection_targets[i];
3674 }
Bram Moolenaara76638f2010-06-05 12:49:46 +02003675 }
3676
lilydjwg94ff09a2024-01-29 20:14:01 +01003677 gtk_selection_clear_targets(gui.drawarea, selection);
3678 gtk_selection_add_targets(gui.drawarea, selection,
Bram Moolenaara76638f2010-06-05 12:49:46 +02003679 targets, n_targets);
3680}
3681
3682/*
3683 * Set up for receiving DND items.
3684 */
3685 void
3686gui_gtk_set_dnd_targets(void)
3687{
3688 int i, j = 0;
3689 int n_targets = N_DND_TARGETS;
3690 GtkTargetEntry targets[N_DND_TARGETS];
3691
3692 for (i = 0; i < (int)N_DND_TARGETS; ++i)
3693 {
Bram Moolenaarb931d742011-10-26 11:36:25 +02003694 if (!clip_html && dnd_targets[i].info == TARGET_HTML)
Bram Moolenaara76638f2010-06-05 12:49:46 +02003695 n_targets--;
3696 else
Bram Moolenaara76638f2010-06-05 12:49:46 +02003697 targets[j++] = dnd_targets[i];
3698 }
3699
3700 gtk_drag_dest_unset(gui.drawarea);
3701 gtk_drag_dest_set(gui.drawarea,
3702 GTK_DEST_DEFAULT_ALL,
3703 targets, n_targets,
Bram Moolenaarba7cc9f2011-02-25 17:10:27 +01003704 GDK_ACTION_COPY | GDK_ACTION_MOVE);
Bram Moolenaara76638f2010-06-05 12:49:46 +02003705}
3706
3707/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003708 * Initialize the GUI. Create all the windows, set up all the callbacks etc.
3709 * Returns OK for success, FAIL when the GUI can't be started.
3710 */
3711 int
3712gui_mch_init(void)
3713{
3714 GtkWidget *vbox;
3715
3716#ifdef FEAT_GUI_GNOME
Bram Moolenaar30613902019-12-01 22:11:18 +01003717 // Initialize the GNOME libraries. gnome_program_init()/gnome_init()
3718 // exits on failure, but that's a non-issue because we already called
3719 // gtk_init_check() in gui_mch_init_check().
Bram Moolenaar071d4272004-06-13 20:20:40 +00003720 if (using_gnome)
Bram Moolenaar3be71ce2013-01-23 16:00:11 +01003721 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003722 gnome_program_init(VIMPACKAGE, VIM_VERSION_SHORT,
3723 LIBGNOMEUI_MODULE, gui_argc, gui_argv, NULL);
Bram Moolenaar73e28dc2022-09-17 21:08:33 +01003724# if defined(LC_NUMERIC)
Bram Moolenaar1ff32c52014-04-29 15:11:43 +02003725 {
3726 char *p = setlocale(LC_NUMERIC, NULL);
3727
Bram Moolenaar30613902019-12-01 22:11:18 +01003728 // Make sure strtod() uses a decimal point, not a comma. Gnome
3729 // init may change it.
Bram Moolenaar1ff32c52014-04-29 15:11:43 +02003730 if (p == NULL || strcmp(p, "C") != 0)
3731 setlocale(LC_NUMERIC, "C");
3732 }
Bram Moolenaar3be71ce2013-01-23 16:00:11 +01003733# endif
3734 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003735#endif
Bram Moolenaard23a8232018-02-10 18:45:26 +01003736 VIM_CLEAR(gui_argv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003737
Bram Moolenaar182c5be2010-06-25 05:37:59 +02003738#if GLIB_CHECK_VERSION(2,1,3)
Bram Moolenaar30613902019-12-01 22:11:18 +01003739 // Set the human-readable application name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003740 g_set_application_name("Vim");
Bram Moolenaar182c5be2010-06-25 05:37:59 +02003741#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003742 /*
3743 * Force UTF-8 output no matter what the value of 'encoding' is.
3744 * did_set_string_option() in option.c prohibits changing 'termencoding'
3745 * to something else than UTF-8 if the GUI is in use.
3746 */
Bram Moolenaar31e5c602022-04-15 13:53:33 +01003747 set_option_value_give_err((char_u *)"termencoding",
3748 0L, (char_u *)"utf-8", 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003749
Bram Moolenaar182c5be2010-06-25 05:37:59 +02003750#ifdef FEAT_TOOLBAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00003751 gui_gtk_register_stock_icons();
Bram Moolenaar182c5be2010-06-25 05:37:59 +02003752#endif
Bram Moolenaar30613902019-12-01 22:11:18 +01003753 // FIXME: Need to install the classic icons and a gtkrc.classic file.
3754 // The hard part is deciding install locations and the Makefile magic.
Bram Moolenaar98921892016-02-23 17:14:37 +01003755#if !GTK_CHECK_VERSION(3,0,0)
3756# if 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003757 gtk_rc_parse("gtkrc");
Bram Moolenaar98921892016-02-23 17:14:37 +01003758# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003759#endif
3760
Bram Moolenaar30613902019-12-01 22:11:18 +01003761 // Initialize values
Bram Moolenaar071d4272004-06-13 20:20:40 +00003762 gui.border_width = 2;
3763 gui.scrollbar_width = SB_DEFAULT_WIDTH;
3764 gui.scrollbar_height = SB_DEFAULT_WIDTH;
Bram Moolenaar36edf062016-07-21 22:10:12 +02003765#if GTK_CHECK_VERSION(3,0,0)
3766 gui.fgcolor = g_new(GdkRGBA, 1);
3767 gui.bgcolor = g_new(GdkRGBA, 1);
3768 gui.spcolor = g_new(GdkRGBA, 1);
3769#else
Bram Moolenaar30613902019-12-01 22:11:18 +01003770 // LINTED: avoid warning: conversion to 'unsigned long'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003771 gui.fgcolor = g_new0(GdkColor, 1);
Bram Moolenaar30613902019-12-01 22:11:18 +01003772 // LINTED: avoid warning: conversion to 'unsigned long'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003773 gui.bgcolor = g_new0(GdkColor, 1);
Bram Moolenaar30613902019-12-01 22:11:18 +01003774 // LINTED: avoid warning: conversion to 'unsigned long'
Bram Moolenaarf36d3692005-03-15 22:48:14 +00003775 gui.spcolor = g_new0(GdkColor, 1);
Bram Moolenaar36edf062016-07-21 22:10:12 +02003776#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003777
Bram Moolenaar30613902019-12-01 22:11:18 +01003778 // Initialise atoms
Bram Moolenaar3a6eaa52009-06-16 13:23:06 +00003779 html_atom = gdk_atom_intern("text/html", FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003780 utf8_string_atom = gdk_atom_intern("UTF8_STRING", FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003781
Bram Moolenaar30613902019-12-01 22:11:18 +01003782 // Set default foreground and background colors.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003783 gui.norm_pixel = gui.def_norm_pixel;
3784 gui.back_pixel = gui.def_back_pixel;
3785
3786 if (gtk_socket_id != 0)
3787 {
3788 GtkWidget *plug;
3789
Bram Moolenaar30613902019-12-01 22:11:18 +01003790 // Use GtkSocket from another app.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003791 plug = gtk_plug_new_for_display(gdk_display_get_default(),
3792 gtk_socket_id);
Bram Moolenaar98921892016-02-23 17:14:37 +01003793 if (plug != NULL && gtk_plug_get_socket_window(GTK_PLUG(plug)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003794 {
3795 gui.mainwin = plug;
3796 }
3797 else
3798 {
3799 g_warning("Connection to GTK+ socket (ID %u) failed",
3800 (unsigned int)gtk_socket_id);
Bram Moolenaar30613902019-12-01 22:11:18 +01003801 // Pretend we never wanted it if it failed (get own window)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003802 gtk_socket_id = 0;
3803 }
3804 }
3805
3806 if (gtk_socket_id == 0)
3807 {
3808#ifdef FEAT_GUI_GNOME
3809 if (using_gnome)
3810 {
3811 gui.mainwin = gnome_app_new("Vim", NULL);
3812# ifdef USE_XSMP
Bram Moolenaar30613902019-12-01 22:11:18 +01003813 // Use the GNOME save-yourself functionality now.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003814 xsmp_close();
3815# endif
3816 }
3817 else
3818#endif
3819 gui.mainwin = gtk_window_new(GTK_WINDOW_TOPLEVEL);
3820 }
3821
3822 gtk_widget_set_name(gui.mainwin, "vim-main-window");
3823
Bram Moolenaar30613902019-12-01 22:11:18 +01003824 // Create the PangoContext used for drawing all text.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003825 gui.text_context = gtk_widget_create_pango_context(gui.mainwin);
3826 pango_context_set_base_dir(gui.text_context, PANGO_DIRECTION_LTR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003827
Bram Moolenaar98921892016-02-23 17:14:37 +01003828 gtk_container_set_border_width(GTK_CONTAINER(gui.mainwin), 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003829 gtk_widget_add_events(gui.mainwin, GDK_VISIBILITY_NOTIFY_MASK);
3830
Bram Moolenaar98921892016-02-23 17:14:37 +01003831 g_signal_connect(G_OBJECT(gui.mainwin), "delete-event",
3832 G_CALLBACK(&delete_event_cb), NULL);
3833
3834 g_signal_connect(G_OBJECT(gui.mainwin), "realize",
3835 G_CALLBACK(&mainwin_realize), NULL);
Bram Moolenaarb463e8d2017-06-05 15:07:09 +02003836
3837 g_signal_connect(G_OBJECT(gui.mainwin), "screen-changed",
Bram Moolenaar071d4272004-06-13 20:20:40 +00003838 G_CALLBACK(&mainwin_screen_changed_cb), NULL);
Bram Moolenaarb463e8d2017-06-05 15:07:09 +02003839
Bram Moolenaar071d4272004-06-13 20:20:40 +00003840 gui.accel_group = gtk_accel_group_new();
3841 gtk_window_add_accel_group(GTK_WINDOW(gui.mainwin), gui.accel_group);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003842
Bram Moolenaar30613902019-12-01 22:11:18 +01003843 // A vertical box holds the menubar, toolbar and main text window.
Bram Moolenaar98921892016-02-23 17:14:37 +01003844#if GTK_CHECK_VERSION(3,2,0)
3845 vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
3846 gtk_box_set_homogeneous(GTK_BOX(vbox), FALSE);
3847#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003848 vbox = gtk_vbox_new(FALSE, 0);
Bram Moolenaar98921892016-02-23 17:14:37 +01003849#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003850
3851#ifdef FEAT_GUI_GNOME
3852 if (using_gnome)
3853 {
Bram Moolenaar182c5be2010-06-25 05:37:59 +02003854# if defined(FEAT_MENU)
Bram Moolenaar30613902019-12-01 22:11:18 +01003855 // automagically restore menubar/toolbar placement
Bram Moolenaar071d4272004-06-13 20:20:40 +00003856 gnome_app_enable_layout_config(GNOME_APP(gui.mainwin), TRUE);
3857# endif
3858 gnome_app_set_contents(GNOME_APP(gui.mainwin), vbox);
3859 }
3860 else
3861#endif
3862 {
3863 gtk_container_add(GTK_CONTAINER(gui.mainwin), vbox);
3864 gtk_widget_show(vbox);
3865 }
3866
3867#ifdef FEAT_MENU
3868 /*
3869 * Create the menubar and handle
3870 */
3871 gui.menubar = gtk_menu_bar_new();
3872 gtk_widget_set_name(gui.menubar, "vim-menubar");
3873
Bram Moolenaar30613902019-12-01 22:11:18 +01003874 // Avoid that GTK takes <F10> away from us.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003875 {
3876 GtkSettings *gtk_settings;
3877
3878 gtk_settings = gtk_settings_get_for_screen(gdk_screen_get_default());
3879 g_object_set(gtk_settings, "gtk-menu-bar-accel", NULL, NULL);
3880 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003881
3882
Bram Moolenaar071d4272004-06-13 20:20:40 +00003883# ifdef FEAT_GUI_GNOME
3884 if (using_gnome)
3885 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003886 BonoboDockItem *dockitem;
3887
3888 gnome_app_set_menus(GNOME_APP(gui.mainwin), GTK_MENU_BAR(gui.menubar));
3889 dockitem = gnome_app_get_dock_item_by_name(GNOME_APP(gui.mainwin),
3890 GNOME_APP_MENUBAR_NAME);
Bram Moolenaar30613902019-12-01 22:11:18 +01003891 // We don't want the menu to float.
Bram Moolenaardb552d602006-03-23 22:59:57 +00003892 bonobo_dock_item_set_behavior(dockitem,
3893 bonobo_dock_item_get_behavior(dockitem)
3894 | BONOBO_DOCK_ITEM_BEH_NEVER_FLOATING);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003895 gui.menubar_h = GTK_WIDGET(dockitem);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003896 }
3897 else
Bram Moolenaar30613902019-12-01 22:11:18 +01003898# endif // FEAT_GUI_GNOME
Bram Moolenaar071d4272004-06-13 20:20:40 +00003899 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003900 // Always show the menubar, otherwise <F10> doesn't work. It may be
3901 // disabled in gui_init() later.
Bram Moolenaar18144c82006-04-12 21:52:12 +00003902 gtk_widget_show(gui.menubar);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003903 gtk_box_pack_start(GTK_BOX(vbox), gui.menubar, FALSE, FALSE, 0);
3904 }
Bram Moolenaar30613902019-12-01 22:11:18 +01003905#endif // FEAT_MENU
Bram Moolenaar071d4272004-06-13 20:20:40 +00003906
3907#ifdef FEAT_TOOLBAR
3908 /*
3909 * Create the toolbar and handle
3910 */
Bram Moolenaar30613902019-12-01 22:11:18 +01003911 // some aesthetics on the toolbar
Bram Moolenaar98921892016-02-23 17:14:37 +01003912# ifdef USE_GTK3
Bram Moolenaar30613902019-12-01 22:11:18 +01003913 // TODO: Add GTK+ 3 code here using GtkCssProvider if necessary.
3914 // N.B. Since the default value of GtkToolbar::button-relief is
3915 // GTK_RELIEF_NONE, there's no need to specify that, probably.
Bram Moolenaar98921892016-02-23 17:14:37 +01003916# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003917 gtk_rc_parse_string(
3918 "style \"vim-toolbar-style\" {\n"
3919 " GtkToolbar::button_relief = GTK_RELIEF_NONE\n"
3920 "}\n"
3921 "widget \"*.vim-toolbar\" style \"vim-toolbar-style\"\n");
Bram Moolenaar98921892016-02-23 17:14:37 +01003922# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003923 gui.toolbar = gtk_toolbar_new();
3924 gtk_widget_set_name(gui.toolbar, "vim-toolbar");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003925 set_toolbar_style(GTK_TOOLBAR(gui.toolbar));
3926
3927# ifdef FEAT_GUI_GNOME
3928 if (using_gnome)
3929 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003930 BonoboDockItem *dockitem;
3931
3932 gnome_app_set_toolbar(GNOME_APP(gui.mainwin), GTK_TOOLBAR(gui.toolbar));
3933 dockitem = gnome_app_get_dock_item_by_name(GNOME_APP(gui.mainwin),
3934 GNOME_APP_TOOLBAR_NAME);
3935 gui.toolbar_h = GTK_WIDGET(dockitem);
Bram Moolenaar30613902019-12-01 22:11:18 +01003936 // When the toolbar is floating it gets stuck. So long as that isn't
3937 // fixed let's disallow floating.
Bram Moolenaare580b0c2006-03-21 21:33:03 +00003938 bonobo_dock_item_set_behavior(dockitem,
Bram Moolenaardb552d602006-03-23 22:59:57 +00003939 bonobo_dock_item_get_behavior(dockitem)
3940 | BONOBO_DOCK_ITEM_BEH_NEVER_FLOATING);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003941 gtk_container_set_border_width(GTK_CONTAINER(gui.toolbar), 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003942 }
3943 else
Bram Moolenaar30613902019-12-01 22:11:18 +01003944# endif // FEAT_GUI_GNOME
Bram Moolenaar071d4272004-06-13 20:20:40 +00003945 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003946 if (vim_strchr(p_go, GO_TOOLBAR) != NULL
3947 && (toolbar_flags & (TOOLBAR_TEXT | TOOLBAR_ICONS)))
3948 gtk_widget_show(gui.toolbar);
3949 gtk_box_pack_start(GTK_BOX(vbox), gui.toolbar, FALSE, FALSE, 0);
3950 }
Bram Moolenaar30613902019-12-01 22:11:18 +01003951#endif // FEAT_TOOLBAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00003952
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003953#ifdef FEAT_GUI_TABLINE
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00003954 /*
3955 * Use a Notebook for the tab pages labels. The labels are hidden by
3956 * default.
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00003957 */
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003958 gui.tabline = gtk_notebook_new();
3959 gtk_widget_show(gui.tabline);
3960 gtk_box_pack_start(GTK_BOX(vbox), gui.tabline, FALSE, FALSE, 0);
3961 gtk_notebook_set_show_border(GTK_NOTEBOOK(gui.tabline), FALSE);
3962 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(gui.tabline), FALSE);
Bram Moolenaar4a85b412006-04-23 22:40:29 +00003963 gtk_notebook_set_scrollable(GTK_NOTEBOOK(gui.tabline), TRUE);
Bram Moolenaar98921892016-02-23 17:14:37 +01003964# if !GTK_CHECK_VERSION(3,0,0)
Bram Moolenaar96351572006-05-05 21:16:59 +00003965 gtk_notebook_set_tab_border(GTK_NOTEBOOK(gui.tabline), FALSE);
Bram Moolenaar98921892016-02-23 17:14:37 +01003966# endif
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003967
Bram Moolenaar98921892016-02-23 17:14:37 +01003968# if !GTK_CHECK_VERSION(3,0,0)
Bram Moolenaar437df8f2006-04-27 21:47:44 +00003969 tabline_tooltip = gtk_tooltips_new();
3970 gtk_tooltips_enable(GTK_TOOLTIPS(tabline_tooltip));
Bram Moolenaar98921892016-02-23 17:14:37 +01003971# endif
Bram Moolenaar437df8f2006-04-27 21:47:44 +00003972
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003973 {
Bram Moolenaar437df8f2006-04-27 21:47:44 +00003974 GtkWidget *page, *label, *event_box;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003975
Bram Moolenaar30613902019-12-01 22:11:18 +01003976 // Add the first tab.
Bram Moolenaar98921892016-02-23 17:14:37 +01003977# if GTK_CHECK_VERSION(3,2,0)
3978 page = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
3979 gtk_box_set_homogeneous(GTK_BOX(page), FALSE);
3980# else
Bram Moolenaar437df8f2006-04-27 21:47:44 +00003981 page = gtk_vbox_new(FALSE, 0);
Bram Moolenaar98921892016-02-23 17:14:37 +01003982# endif
Bram Moolenaar437df8f2006-04-27 21:47:44 +00003983 gtk_widget_show(page);
3984 gtk_container_add(GTK_CONTAINER(gui.tabline), page);
3985 label = gtk_label_new("-Empty-");
3986 gtk_widget_show(label);
Bram Moolenaar437df8f2006-04-27 21:47:44 +00003987 event_box = gtk_event_box_new();
3988 gtk_widget_show(event_box);
Bram Moolenaar98921892016-02-23 17:14:37 +01003989 g_object_set_data(G_OBJECT(event_box), "tab_num", GINT_TO_POINTER(1L));
Bram Moolenaar98921892016-02-23 17:14:37 +01003990# if !GTK_CHECK_VERSION(3,14,0)
Bram Moolenaar8ea91232006-04-28 22:41:43 +00003991 gtk_misc_set_padding(GTK_MISC(label), 2, 2);
Bram Moolenaar98921892016-02-23 17:14:37 +01003992# endif
Bram Moolenaar437df8f2006-04-27 21:47:44 +00003993 gtk_container_add(GTK_CONTAINER(event_box), label);
Bram Moolenaar437df8f2006-04-27 21:47:44 +00003994 gtk_notebook_set_tab_label(GTK_NOTEBOOK(gui.tabline), page, event_box);
Bram Moolenaar92cbf622018-09-19 22:40:03 +02003995# if GTK_CHECK_VERSION(2,10,0)
Bram Moolenaarca05aa22017-10-22 15:36:14 +02003996 gtk_notebook_set_tab_reorderable(GTK_NOTEBOOK(gui.tabline), page, TRUE);
Bram Moolenaar92cbf622018-09-19 22:40:03 +02003997# endif
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00003998 }
Bram Moolenaar54a709e2006-05-04 21:57:11 +00003999
Bram Moolenaar98921892016-02-23 17:14:37 +01004000 g_signal_connect(G_OBJECT(gui.tabline), "switch-page",
4001 G_CALLBACK(on_select_tab), NULL);
Bram Moolenaar92cbf622018-09-19 22:40:03 +02004002# if GTK_CHECK_VERSION(2,10,0)
Bram Moolenaarca05aa22017-10-22 15:36:14 +02004003 g_signal_connect(G_OBJECT(gui.tabline), "page-reordered",
4004 G_CALLBACK(on_tab_reordered), NULL);
Bram Moolenaar92cbf622018-09-19 22:40:03 +02004005# endif
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004006
Bram Moolenaar30613902019-12-01 22:11:18 +01004007 // Create a popup menu for the tab line and connect it.
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004008 tabline_menu = create_tabline_menu();
Bram Moolenaar98921892016-02-23 17:14:37 +01004009 g_signal_connect_swapped(G_OBJECT(gui.tabline), "button-press-event",
4010 G_CALLBACK(on_tabline_menu), G_OBJECT(tabline_menu));
Bram Moolenaar30613902019-12-01 22:11:18 +01004011#endif // FEAT_GUI_TABLINE
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004012
Bram Moolenaar8a99e662020-10-21 16:10:21 +02004013 gui.formwin = gui_gtk_form_new();
Bram Moolenaar98921892016-02-23 17:14:37 +01004014 gtk_container_set_border_width(GTK_CONTAINER(gui.formwin), 0);
Bram Moolenaar98921892016-02-23 17:14:37 +01004015#if !GTK_CHECK_VERSION(3,0,0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004016 gtk_widget_set_events(gui.formwin, GDK_EXPOSURE_MASK);
Bram Moolenaar98921892016-02-23 17:14:37 +01004017#endif
Bram Moolenaar023fd5d2020-12-08 20:31:16 +01004018#if GTK_CHECK_VERSION(3,22,2)
4019 gtk_widget_set_name(gui.formwin, "vim-gtk-form");
4020#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004021
4022 gui.drawarea = gtk_drawing_area_new();
Bram Moolenaar98921892016-02-23 17:14:37 +01004023#if GTK_CHECK_VERSION(3,0,0)
4024 gui.surface = NULL;
Bram Moolenaar98921892016-02-23 17:14:37 +01004025#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004026
Bram Moolenaar30613902019-12-01 22:11:18 +01004027 // Determine which events we will filter.
lilydjwgc4d4a1e2024-02-09 19:13:12 +01004028 gint event_mask =
4029 GDK_EXPOSURE_MASK |
4030 GDK_ENTER_NOTIFY_MASK |
4031 GDK_LEAVE_NOTIFY_MASK |
4032 GDK_BUTTON_PRESS_MASK |
4033 GDK_BUTTON_RELEASE_MASK |
4034 GDK_KEY_PRESS_MASK |
4035 GDK_KEY_RELEASE_MASK |
4036 GDK_POINTER_MOTION_MASK |
4037 GDK_POINTER_MOTION_HINT_MASK;
lilydjwg6e0a18f2024-01-29 20:54:28 +01004038#if GTK_CHECK_VERSION(3,4,0)
lilydjwgc4d4a1e2024-02-09 19:13:12 +01004039 if (GDK_IS_X11_DISPLAY(gdk_display_get_default()))
4040 {
4041 // for X11, if we were using smooth scroll events, we
4042 // would get an scroll without deltas on the very first user scroll* and
4043 // get both "unsmooth" scroll and smooth scroll events after
4044 // copying into the primary selection
4045 //
4046 // *: https://bugzilla.gnome.org/show_bug.cgi?id=675959
4047 event_mask |= GDK_SCROLL_MASK;
4048 }
4049 else
4050 {
4051 // for Wayland, touchpads don't generate "unsmooth" scroll events. Both
4052 // touchpads and wheels generate smooth scroll events expectedly.
4053 event_mask |= GDK_SMOOTH_SCROLL_MASK;
4054 }
4055#else
4056 event_mask |= GDK_SCROLL_MASK;
lilydjwg6e0a18f2024-01-29 20:54:28 +01004057#endif
lilydjwgc4d4a1e2024-02-09 19:13:12 +01004058 gtk_widget_set_events(gui.drawarea, event_mask);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004059
4060 gtk_widget_show(gui.drawarea);
Bram Moolenaar8a99e662020-10-21 16:10:21 +02004061 gui_gtk_form_put(GTK_FORM(gui.formwin), gui.drawarea, 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004062 gtk_widget_show(gui.formwin);
4063 gtk_box_pack_start(GTK_BOX(vbox), gui.formwin, TRUE, TRUE, 0);
4064
Bram Moolenaar30613902019-12-01 22:11:18 +01004065 // For GtkSockets, key-presses must go to the focus widget (drawarea)
4066 // and not the window.
Bram Moolenaar98921892016-02-23 17:14:37 +01004067 g_signal_connect((gtk_socket_id == 0) ? G_OBJECT(gui.mainwin)
4068 : G_OBJECT(gui.drawarea),
4069 "key-press-event",
4070 G_CALLBACK(key_press_event), NULL);
Bram Moolenaar98921892016-02-23 17:14:37 +01004071#if defined(FEAT_XIM) || GTK_CHECK_VERSION(3,0,0)
Bram Moolenaar30613902019-12-01 22:11:18 +01004072 // Also forward key release events for the benefit of GTK+ 2 input
4073 // modules. Try CTRL-SHIFT-xdigits to enter a Unicode code point.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004074 g_signal_connect((gtk_socket_id == 0) ? G_OBJECT(gui.mainwin)
4075 : G_OBJECT(gui.drawarea),
Bram Moolenaar98921892016-02-23 17:14:37 +01004076 "key-release-event",
Bram Moolenaar071d4272004-06-13 20:20:40 +00004077 G_CALLBACK(&key_release_event), NULL);
4078#endif
Bram Moolenaar98921892016-02-23 17:14:37 +01004079 g_signal_connect(G_OBJECT(gui.drawarea), "realize",
4080 G_CALLBACK(drawarea_realize_cb), NULL);
4081 g_signal_connect(G_OBJECT(gui.drawarea), "unrealize",
4082 G_CALLBACK(drawarea_unrealize_cb), NULL);
Bram Moolenaar664323e2018-09-18 22:30:07 +02004083#if GTK_CHECK_VERSION(3,0,0)
Bram Moolenaar98921892016-02-23 17:14:37 +01004084 g_signal_connect(G_OBJECT(gui.drawarea), "configure-event",
4085 G_CALLBACK(drawarea_configure_event_cb), NULL);
Bram Moolenaar664323e2018-09-18 22:30:07 +02004086#endif
4087#if GTK_CHECK_VERSION(3,22,2)
Bram Moolenaara859f042016-11-17 19:11:55 +01004088 g_signal_connect_after(G_OBJECT(gui.drawarea), "style-updated",
4089 G_CALLBACK(&drawarea_style_updated_cb), NULL);
Bram Moolenaar664323e2018-09-18 22:30:07 +02004090#else
Bram Moolenaar98921892016-02-23 17:14:37 +01004091 g_signal_connect_after(G_OBJECT(gui.drawarea), "style-set",
4092 G_CALLBACK(&drawarea_style_set_cb), NULL);
Bram Moolenaar98921892016-02-23 17:14:37 +01004093#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004094
Bram Moolenaar98921892016-02-23 17:14:37 +01004095#if !GTK_CHECK_VERSION(3,0,0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004096 gui.visibility = GDK_VISIBILITY_UNOBSCURED;
Bram Moolenaar98921892016-02-23 17:14:37 +01004097#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004098
Bram Moolenaarf96ae0b2019-07-28 15:21:55 +02004099#if !defined(USE_GNOME_SESSION)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004100 wm_protocols_atom = gdk_atom_intern("WM_PROTOCOLS", FALSE);
4101 save_yourself_atom = gdk_atom_intern("WM_SAVE_YOURSELF", FALSE);
4102#endif
4103
4104 if (gtk_socket_id != 0)
Bram Moolenaar30613902019-12-01 22:11:18 +01004105 // make sure keyboard input can go to the drawarea
Bram Moolenaar98921892016-02-23 17:14:37 +01004106 gtk_widget_set_can_focus(gui.drawarea, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004107
4108 /*
4109 * Set clipboard specific atoms
4110 */
4111 vim_atom = gdk_atom_intern(VIM_ATOM_NAME, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004112 vimenc_atom = gdk_atom_intern(VIMENC_ATOM_NAME, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004113 clip_star.gtk_sel_atom = GDK_SELECTION_PRIMARY;
4114 clip_plus.gtk_sel_atom = gdk_atom_intern("CLIPBOARD", FALSE);
4115
4116 /*
4117 * Start out by adding the configured border width into the border offset.
4118 */
4119 gui.border_offset = gui.border_width;
4120
Bram Moolenaar98921892016-02-23 17:14:37 +01004121#if GTK_CHECK_VERSION(3,0,0)
4122 g_signal_connect(G_OBJECT(gui.drawarea), "draw",
4123 G_CALLBACK(draw_event), NULL);
4124#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004125 gtk_signal_connect(GTK_OBJECT(gui.mainwin), "visibility_notify_event",
4126 GTK_SIGNAL_FUNC(visibility_event), NULL);
4127 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "expose_event",
4128 GTK_SIGNAL_FUNC(expose_event), NULL);
Bram Moolenaar98921892016-02-23 17:14:37 +01004129#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004130
4131 /*
4132 * Only install these enter/leave callbacks when 'p' in 'guioptions'.
4133 * Only needed for some window managers.
4134 */
4135 if (vim_strchr(p_go, GO_POINTER) != NULL)
4136 {
Bram Moolenaar98921892016-02-23 17:14:37 +01004137 g_signal_connect(G_OBJECT(gui.drawarea), "leave-notify-event",
4138 G_CALLBACK(leave_notify_event), NULL);
4139 g_signal_connect(G_OBJECT(gui.drawarea), "enter-notify-event",
4140 G_CALLBACK(enter_notify_event), NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004141 }
4142
Bram Moolenaar30613902019-12-01 22:11:18 +01004143 // Real windows can get focus ... GtkPlug, being a mere container can't,
Christian Brabandtee17b6f2023-09-09 11:23:50 +02004144 // only its widgets. Arguably, this could be common code and we do not
4145 // use the window focus at all, but let's be safe.
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004146 if (gtk_socket_id == 0)
4147 {
Bram Moolenaar98921892016-02-23 17:14:37 +01004148 g_signal_connect(G_OBJECT(gui.mainwin), "focus-out-event",
4149 G_CALLBACK(focus_out_event), NULL);
4150 g_signal_connect(G_OBJECT(gui.mainwin), "focus-in-event",
4151 G_CALLBACK(focus_in_event), NULL);
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004152 }
4153 else
4154 {
Bram Moolenaar98921892016-02-23 17:14:37 +01004155 g_signal_connect(G_OBJECT(gui.drawarea), "focus-out-event",
4156 G_CALLBACK(focus_out_event), NULL);
4157 g_signal_connect(G_OBJECT(gui.drawarea), "focus-in-event",
4158 G_CALLBACK(focus_in_event), NULL);
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004159#ifdef FEAT_GUI_TABLINE
Bram Moolenaar98921892016-02-23 17:14:37 +01004160 g_signal_connect(G_OBJECT(gui.tabline), "focus-out-event",
4161 G_CALLBACK(focus_out_event), NULL);
4162 g_signal_connect(G_OBJECT(gui.tabline), "focus-in-event",
4163 G_CALLBACK(focus_in_event), NULL);
Bram Moolenaar30613902019-12-01 22:11:18 +01004164#endif // FEAT_GUI_TABLINE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004165 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004166
Bram Moolenaar98921892016-02-23 17:14:37 +01004167 g_signal_connect(G_OBJECT(gui.drawarea), "motion-notify-event",
4168 G_CALLBACK(motion_notify_event), NULL);
4169 g_signal_connect(G_OBJECT(gui.drawarea), "button-press-event",
4170 G_CALLBACK(button_press_event), NULL);
4171 g_signal_connect(G_OBJECT(gui.drawarea), "button-release-event",
4172 G_CALLBACK(button_release_event), NULL);
4173 g_signal_connect(G_OBJECT(gui.drawarea), "scroll-event",
lilydjwg94ff09a2024-01-29 20:14:01 +01004174 G_CALLBACK(scroll_event), NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004175
Bram Moolenaar30613902019-12-01 22:11:18 +01004176 // Pretend we don't have input focus, we will get an event if we do.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004177 gui.in_focus = FALSE;
4178
Bram Moolenaar7ebf4e12018-08-07 20:01:40 +02004179 // Handle changes to the "Xft/DPI" setting.
4180 {
4181 GtkSettings *gtk_settings =
4182 gtk_settings_get_for_screen(gdk_screen_get_default());
4183
4184 g_signal_connect(gtk_settings, "notify::gtk-xft-dpi",
4185 G_CALLBACK(gtk_settings_xft_dpi_changed_cb), NULL);
4186 }
4187
Bram Moolenaar071d4272004-06-13 20:20:40 +00004188 return OK;
4189}
4190
Bram Moolenaarf96ae0b2019-07-28 15:21:55 +02004191#if defined(USE_GNOME_SESSION) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004192/*
4193 * This is called from gui_start() after a fork() has been done.
4194 * We have to tell the session manager our new PID.
4195 */
4196 void
4197gui_mch_forked(void)
4198{
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00004199 if (!using_gnome)
4200 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004201
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00004202 GnomeClient *client;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004203
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00004204 client = gnome_master_client();
4205
4206 if (client != NULL)
4207 gnome_client_set_process_id(client, getpid());
Bram Moolenaar071d4272004-06-13 20:20:40 +00004208}
Bram Moolenaarf96ae0b2019-07-28 15:21:55 +02004209#endif // USE_GNOME_SESSION
Bram Moolenaar071d4272004-06-13 20:20:40 +00004210
Bram Moolenaar98921892016-02-23 17:14:37 +01004211#if GTK_CHECK_VERSION(3,0,0)
Bram Moolenaar36edf062016-07-21 22:10:12 +02004212 static GdkRGBA
4213color_to_rgba(guicolor_T color)
Bram Moolenaar98921892016-02-23 17:14:37 +01004214{
Bram Moolenaar36edf062016-07-21 22:10:12 +02004215 GdkRGBA rgba;
4216 rgba.red = ((color & 0xff0000) >> 16) / 255.0;
4217 rgba.green = ((color & 0xff00) >> 8) / 255.0;
4218 rgba.blue = ((color & 0xff)) / 255.0;
4219 rgba.alpha = 1.0;
4220 return rgba;
Bram Moolenaar98921892016-02-23 17:14:37 +01004221}
4222
4223 static void
Bram Moolenaar36edf062016-07-21 22:10:12 +02004224set_cairo_source_rgba_from_color(cairo_t *cr, guicolor_T color)
Bram Moolenaar98921892016-02-23 17:14:37 +01004225{
Bram Moolenaar36edf062016-07-21 22:10:12 +02004226 const GdkRGBA rgba = color_to_rgba(color);
4227 cairo_set_source_rgba(cr, rgba.red, rgba.green, rgba.blue, rgba.alpha);
Bram Moolenaar98921892016-02-23 17:14:37 +01004228}
Bram Moolenaar30613902019-12-01 22:11:18 +01004229#endif // GTK_CHECK_VERSION(3,0,0)
Bram Moolenaar98921892016-02-23 17:14:37 +01004230
Bram Moolenaar071d4272004-06-13 20:20:40 +00004231/*
4232 * Called when the foreground or background color has been changed.
4233 * This used to change the graphics contexts directly but we are
4234 * currently manipulating them where desired.
4235 */
4236 void
4237gui_mch_new_colors(void)
4238{
Bram Moolenaar7988a6f2020-12-09 15:53:27 +01004239 if (gui.drawarea != NULL
4240#if GTK_CHECK_VERSION(3,22,2)
4241 && gui.formwin != NULL
4242#endif
4243 && gtk_widget_get_window(gui.drawarea) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004244 {
Bram Moolenaar664323e2018-09-18 22:30:07 +02004245#if !GTK_CHECK_VERSION(3,22,2)
Bram Moolenaar7988a6f2020-12-09 15:53:27 +01004246 GdkWindow * const da_win = gtk_widget_get_window(gui.drawarea);
Bram Moolenaar664323e2018-09-18 22:30:07 +02004247#endif
Bram Moolenaara859f042016-11-17 19:11:55 +01004248#if GTK_CHECK_VERSION(3,22,2)
Bram Moolenaar7988a6f2020-12-09 15:53:27 +01004249 GtkStyleContext * const context =
4250 gtk_widget_get_style_context(gui.formwin);
Bram Moolenaara859f042016-11-17 19:11:55 +01004251 GtkCssProvider * const provider = gtk_css_provider_new();
4252 gchar * const css = g_strdup_printf(
Bram Moolenaar023fd5d2020-12-08 20:31:16 +01004253 "widget#vim-gtk-form {\n"
Bram Moolenaara859f042016-11-17 19:11:55 +01004254 " background-color: #%.2lx%.2lx%.2lx;\n"
4255 "}\n",
4256 (gui.back_pixel >> 16) & 0xff,
4257 (gui.back_pixel >> 8) & 0xff,
4258 gui.back_pixel & 0xff);
4259
4260 gtk_css_provider_load_from_data(provider, css, -1, NULL);
4261 gtk_style_context_add_provider(context,
4262 GTK_STYLE_PROVIDER(provider), G_MAXUINT);
4263
4264 g_free(css);
4265 g_object_unref(provider);
Bram Moolenaar30613902019-12-01 22:11:18 +01004266#elif GTK_CHECK_VERSION(3,4,0) // !GTK_CHECK_VERSION(3,22,2)
Bram Moolenaar36edf062016-07-21 22:10:12 +02004267 GdkRGBA rgba;
Bram Moolenaar98921892016-02-23 17:14:37 +01004268
Bram Moolenaar36edf062016-07-21 22:10:12 +02004269 rgba = color_to_rgba(gui.back_pixel);
Bram Moolenaar98921892016-02-23 17:14:37 +01004270 {
4271 cairo_pattern_t * const pat = cairo_pattern_create_rgba(
Bram Moolenaar36edf062016-07-21 22:10:12 +02004272 rgba.red, rgba.green, rgba.blue, rgba.alpha);
Bram Moolenaar98921892016-02-23 17:14:37 +01004273 if (pat != NULL)
4274 {
4275 gdk_window_set_background_pattern(da_win, pat);
4276 cairo_pattern_destroy(pat);
4277 }
4278 else
Bram Moolenaar36edf062016-07-21 22:10:12 +02004279 gdk_window_set_background_rgba(da_win, &rgba);
Bram Moolenaar98921892016-02-23 17:14:37 +01004280 }
Bram Moolenaar30613902019-12-01 22:11:18 +01004281#else // !GTK_CHECK_VERSION(3,4,0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004282 GdkColor color = { 0, 0, 0, 0 };
4283
4284 color.pixel = gui.back_pixel;
Bram Moolenaar98921892016-02-23 17:14:37 +01004285 gdk_window_set_background(da_win, &color);
Bram Moolenaar30613902019-12-01 22:11:18 +01004286#endif // !GTK_CHECK_VERSION(3,22,2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004287 }
4288}
4289
Bram Moolenaar071d4272004-06-13 20:20:40 +00004290/*
4291 * This signal informs us about the need to rearrange our sub-widgets.
4292 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004293 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +00004294form_configure_event(GtkWidget *widget UNUSED,
4295 GdkEventConfigure *event,
4296 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004297{
Ernie Raeld42b8392022-04-23 19:52:23 +01004298 int usable_height = event->height;
4299#ifdef TRACK_RESIZE_HISTORY
4300 // Resize requests are made for gui.mainwin;
4301 // get its dimensions for searching if this event
Ernie Rael9f53e7b2022-04-17 18:27:49 +01004302 // is a response to a vim request.
Ernie Raeld42b8392022-04-23 19:52:23 +01004303 int w, h;
4304 gtk_window_get_size(GTK_WINDOW(gui.mainwin), &w, &h);
Ernie Rael9f53e7b2022-04-17 18:27:49 +01004305
Ernie Raeld42b8392022-04-23 19:52:23 +01004306# ifdef ENABLE_RESIZE_HISTORY_LOG
Ernie Rael9f53e7b2022-04-17 18:27:49 +01004307 ch_log(NULL, "gui_gtk: form_configure_event: (%d, %d) [%d, %d]",
4308 w, h, (int)Columns, (int)Rows);
Ernie Raeld42b8392022-04-23 19:52:23 +01004309# endif
Ernie Rael9f53e7b2022-04-17 18:27:49 +01004310
Ernie Raeld42b8392022-04-23 19:52:23 +01004311 // Look through history of recent vim resize requests.
Ernie Rael9f53e7b2022-04-17 18:27:49 +01004312 // If this event matches:
4313 // - "latest resize hist" We're caught up;
4314 // clear the history and process this event.
4315 // If history is, old to new, 100, 99, 100, 99. If this event is
4316 // 99 for the stale, it is matched against the current. History
Ernie Raeld42b8392022-04-23 19:52:23 +01004317 // is cleared, we may bounce, but no worse than before.
Ernie Rael9f53e7b2022-04-17 18:27:49 +01004318 // - "older/stale hist" If match an unused event in history,
4319 // then discard this event, and mark the matching event as used.
4320 // - "no match" Figure it's a user resize event, clear history.
4321 // NOTE: clear history is default, then all incoming events are processed
4322
4323 if (!MATCH_WIDTH_HEIGHT(latest_resize_hist, w, h)
4324 && match_stale_width_height(w, h))
4325 // discard stale event
4326 return TRUE;
4327 clear_resize_hists();
Ernie Raeld42b8392022-04-23 19:52:23 +01004328#endif
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004329
Bram Moolenaar182707a2016-11-21 20:55:58 +01004330#if GTK_CHECK_VERSION(3,22,2) && !GTK_CHECK_VERSION(3,22,4)
Bram Moolenaar30613902019-12-01 22:11:18 +01004331 // As of 3.22.2, GdkWindows have started distributing configure events to
4332 // their "native" children (https://git.gnome.org/browse/gtk+/commit/?h=gtk-3-22&id=12579fe71b3b8f79eb9c1b80e429443bcc437dd0).
4333 //
4334 // As can be seen from the implementation of move_native_children() and
4335 // configure_native_child() in gdkwindow.c, those functions actually
4336 // propagate configure events to every child, failing to distinguish
4337 // "native" one from non-native one.
4338 //
4339 // Naturally, configure events propagated to here like that are fallacious
4340 // and, as a matter of fact, they trigger a geometric collapse of
4341 // gui.formwin.
4342 //
4343 // To filter out such fallacious events, check if the given event is the
4344 // one that was sent out to the right place. Ignore it if not.
4345 //
4346 // Follow-up
4347 // After a few weeks later, the GdkWindow change mentioned above was
4348 // reverted (https://git.gnome.org/browse/gtk+/commit/?h=gtk-3-22&id=f70039cb9603a02d2369fec4038abf40a1711155).
4349 // The corresponding official release is 3.22.4.
Bram Moolenaara859f042016-11-17 19:11:55 +01004350 if (event->window != gtk_widget_get_window(gui.formwin))
4351 return TRUE;
4352#endif
4353
Bram Moolenaar30613902019-12-01 22:11:18 +01004354 // When in a GtkPlug, we can't guarantee valid heights (as a round
4355 // no. of char-heights), so we have to manually sanitise them.
4356 // Widths seem to sort themselves out, don't ask me why.
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004357 if (gtk_socket_id != 0)
Bram Moolenaar30613902019-12-01 22:11:18 +01004358 usable_height -= (gui.char_height - (gui.char_height/2)); // sic.
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004359
Bram Moolenaar8a99e662020-10-21 16:10:21 +02004360 gui_gtk_form_freeze(GTK_FORM(gui.formwin));
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004361 gui_resize_shell(event->width, usable_height);
Bram Moolenaar8a99e662020-10-21 16:10:21 +02004362 gui_gtk_form_thaw(GTK_FORM(gui.formwin));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004363
4364 return TRUE;
4365}
4366
4367/*
4368 * Function called when window already closed.
4369 * We can't do much more here than to trying to preserve what had been done,
4370 * since the window is already inevitably going away.
4371 */
Bram Moolenaar98921892016-02-23 17:14:37 +01004372 static void
4373mainwin_destroy_cb(GObject *object UNUSED, gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004374{
Bram Moolenaar30613902019-12-01 22:11:18 +01004375 // Don't write messages to the GUI anymore
Bram Moolenaar071d4272004-06-13 20:20:40 +00004376 full_screen = FALSE;
4377
4378 gui.mainwin = NULL;
4379 gui.drawarea = NULL;
4380
Bram Moolenaar30613902019-12-01 22:11:18 +01004381 if (!exiting) // only do anything if the destroy was unexpected
Bram Moolenaar071d4272004-06-13 20:20:40 +00004382 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004383 vim_strncpy(IObuff,
4384 (char_u *)_("Vim: Main window unexpectedly destroyed\n"),
4385 IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004386 preserve_exit();
4387 }
Bram Moolenaar36e294c2015-12-29 18:55:46 +01004388#ifdef USE_GRESOURCE
4389 gui_gtk_unregister_resource();
4390#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004391}
4392
Bram Moolenaara555e6f2021-03-18 22:28:57 +01004393 void
4394gui_gtk_get_screen_geom_of_win(
4395 GtkWidget *wid,
4396 int point_x, // x position of window if not initialized
4397 int point_y, // y position of window if not initialized
4398 int *screen_x,
4399 int *screen_y,
4400 int *width,
4401 int *height)
4402{
4403 GdkRectangle geometry;
4404 GdkWindow *win = gtk_widget_get_window(wid);
4405#if GTK_CHECK_VERSION(3,22,0)
Bram Moolenaar24001432021-03-20 12:36:46 +01004406 GdkDisplay *dpy;
4407 GdkMonitor *monitor;
Bram Moolenaara555e6f2021-03-18 22:28:57 +01004408
Bram Moolenaar24001432021-03-20 12:36:46 +01004409 if (wid != NULL && gtk_widget_get_realized(wid))
4410 dpy = gtk_widget_get_display(wid);
4411 else
4412 dpy = gdk_display_get_default();
4413 if (win != NULL)
4414 monitor = gdk_display_get_monitor_at_window(dpy, win);
4415 else
4416 monitor = gdk_display_get_monitor_at_point(dpy, point_x, point_y);
Bram Moolenaara555e6f2021-03-18 22:28:57 +01004417 gdk_monitor_get_geometry(monitor, &geometry);
4418#else
4419 GdkScreen* screen;
4420 int monitor;
4421
4422 if (wid != NULL && gtk_widget_has_screen(wid))
4423 screen = gtk_widget_get_screen(wid);
4424 else
4425 screen = gdk_screen_get_default();
Bram Moolenaar24001432021-03-20 12:36:46 +01004426 if (win != NULL)
Bram Moolenaara555e6f2021-03-18 22:28:57 +01004427 monitor = gdk_screen_get_monitor_at_window(screen, win);
Bram Moolenaar24001432021-03-20 12:36:46 +01004428 else
4429 monitor = gdk_screen_get_monitor_at_point(screen, point_x, point_y);
Bram Moolenaara555e6f2021-03-18 22:28:57 +01004430 gdk_screen_get_monitor_geometry(screen, monitor, &geometry);
4431#endif
4432 *screen_x = geometry.x;
4433 *screen_y = geometry.y;
4434 *width = geometry.width;
4435 *height = geometry.height;
4436}
4437
4438/*
4439 * The screen size is used to make sure the initial window doesn't get bigger
4440 * than the screen. This subtracts some room for menubar, toolbar and window
4441 * decorations.
4442 */
4443 static void
4444gui_gtk_get_screen_dimensions(
4445 int point_x,
4446 int point_y,
4447 int *screen_w,
4448 int *screen_h)
4449{
4450 int x, y;
4451
4452 gui_gtk_get_screen_geom_of_win(gui.mainwin, point_x, point_y,
4453 &x, &y, screen_w, screen_h);
4454
4455 // Subtract 'guiheadroom' from the height to allow some room for the
4456 // window manager (task list and window title bar).
4457 *screen_h -= p_ghr;
4458
4459 /*
4460 * FIXME: dirty trick: Because the gui_get_base_height() doesn't include
4461 * the toolbar and menubar for GTK, we subtract them from the screen
4462 * height, so that the window size can be made to fit on the screen.
4463 * This should be completely changed later.
4464 */
4465 *screen_w -= get_menu_tool_width();
4466 *screen_h -= get_menu_tool_height();
4467}
4468
4469 void
4470gui_mch_get_screen_dimensions(int *screen_w, int *screen_h)
4471{
4472 gui_gtk_get_screen_dimensions(0, 0, screen_w, screen_h);
4473}
4474
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004475
4476/*
4477 * Bit of a hack to ensure we start GtkPlug windows with the correct window
4478 * hints (and thus the required size from -geom), but that after that we
4479 * put the hints back to normal (the actual minimum size) so we may
4480 * subsequently be resized smaller. GtkSocket (the parent end) uses the
Bram Moolenaarc4568ab2018-11-16 16:21:05 +01004481 * plug's window 'min hints to set *its* minimum size, but that's also the
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004482 * only way we have of making ourselves bigger (by set lines/columns).
4483 * Thus set hints at start-up to ensure correct init. size, then a
Bram Moolenaar3f2a5d82016-02-27 22:08:16 +01004484 * second after the final attempt to reset the real minimum hints (done by
4485 * scrollbar init.), actually do the standard hints and stop the timer.
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004486 * We'll not let the default hints be set while this timer's active.
4487 */
Bram Moolenaar4ab79682017-08-27 14:50:47 +02004488 static timeout_cb_type
Bram Moolenaarb85cb212009-05-17 14:24:23 +00004489check_startup_plug_hints(gpointer data UNUSED)
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004490{
4491 if (init_window_hints_state == 1)
4492 {
Bram Moolenaar30613902019-12-01 22:11:18 +01004493 // Safe to use normal hints now
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004494 init_window_hints_state = 0;
4495 update_window_manager_hints(0, 0);
Bram Moolenaar30613902019-12-01 22:11:18 +01004496 return FALSE; // stop timer
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004497 }
4498
Bram Moolenaar30613902019-12-01 22:11:18 +01004499 // Keep on trying
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004500 init_window_hints_state = 1;
4501 return TRUE;
4502}
4503
Bram Moolenaar071d4272004-06-13 20:20:40 +00004504/*
4505 * Open the GUI window which was created by a call to gui_mch_init().
4506 */
4507 int
4508gui_mch_open(void)
4509{
4510 guicolor_T fg_pixel = INVALCOLOR;
4511 guicolor_T bg_pixel = INVALCOLOR;
Bram Moolenaar79ef6d62009-09-23 15:35:48 +00004512 guint pixel_width;
4513 guint pixel_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004514
Bram Moolenaar071d4272004-06-13 20:20:40 +00004515 /*
4516 * Allow setting a window role on the command line, or invent one
4517 * if none was specified. This is mainly useful for GNOME session
4518 * support; allowing the WM to restore window placement.
4519 */
4520 if (role_argument != NULL)
4521 {
4522 gtk_window_set_role(GTK_WINDOW(gui.mainwin), role_argument);
4523 }
4524 else
4525 {
4526 char *role;
4527
Bram Moolenaar30613902019-12-01 22:11:18 +01004528 // Invent a unique-enough ID string for the role
Bram Moolenaar071d4272004-06-13 20:20:40 +00004529 role = g_strdup_printf("vim-%u-%u-%u",
4530 (unsigned)mch_get_pid(),
4531 (unsigned)g_random_int(),
4532 (unsigned)time(NULL));
4533
4534 gtk_window_set_role(GTK_WINDOW(gui.mainwin), role);
4535 g_free(role);
4536 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004537
4538 if (gui_win_x != -1 && gui_win_y != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004539 gtk_window_move(GTK_WINDOW(gui.mainwin), gui_win_x, gui_win_y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004540
Bram Moolenaar30613902019-12-01 22:11:18 +01004541 // Determine user specified geometry, if present.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004542 if (gui.geom != NULL)
4543 {
4544 int mask;
4545 unsigned int w, h;
4546 int x = 0;
4547 int y = 0;
4548
4549 mask = XParseGeometry((char *)gui.geom, &x, &y, &w, &h);
4550
4551 if (mask & WidthValue)
4552 Columns = w;
4553 if (mask & HeightValue)
Bram Moolenaard68071d2006-05-02 22:08:30 +00004554 {
Bram Moolenaarb85cb212009-05-17 14:24:23 +00004555 if (p_window > (long)h - 1 || !option_was_set((char_u *)"window"))
Bram Moolenaard68071d2006-05-02 22:08:30 +00004556 p_window = h - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004557 Rows = h;
Bram Moolenaard68071d2006-05-02 22:08:30 +00004558 }
Bram Moolenaare057d402013-06-30 17:51:51 +02004559 limit_screen_size();
Bram Moolenaar2dd8b522007-10-19 12:33:44 +00004560
4561 pixel_width = (guint)(gui_get_base_width() + Columns * gui.char_width);
4562 pixel_height = (guint)(gui_get_base_height() + Rows * gui.char_height);
4563
Bram Moolenaar2dd8b522007-10-19 12:33:44 +00004564 pixel_width += get_menu_tool_width();
4565 pixel_height += get_menu_tool_height();
Bram Moolenaar2dd8b522007-10-19 12:33:44 +00004566
Bram Moolenaar071d4272004-06-13 20:20:40 +00004567 if (mask & (XValue | YValue))
Bram Moolenaar2dd8b522007-10-19 12:33:44 +00004568 {
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004569 int ww, hh;
Bram Moolenaara555e6f2021-03-18 22:28:57 +01004570
4571#ifdef FEAT_GUI_GTK
4572 gui_gtk_get_screen_dimensions(x, y, &ww, &hh);
4573#else
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004574 gui_mch_get_screen_dimensions(&ww, &hh);
Bram Moolenaara555e6f2021-03-18 22:28:57 +01004575#endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004576 hh += p_ghr + get_menu_tool_height();
4577 ww += get_menu_tool_width();
Bram Moolenaar2dd8b522007-10-19 12:33:44 +00004578 if (mask & XNegative)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004579 x += ww - pixel_width;
Bram Moolenaar2dd8b522007-10-19 12:33:44 +00004580 if (mask & YNegative)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004581 y += hh - pixel_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004582 gtk_window_move(GTK_WINDOW(gui.mainwin), x, y);
Bram Moolenaar2dd8b522007-10-19 12:33:44 +00004583 }
Bram Moolenaard23a8232018-02-10 18:45:26 +01004584 VIM_CLEAR(gui.geom);
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004585
Bram Moolenaar30613902019-12-01 22:11:18 +01004586 // From now until everyone's stopped trying to set the window hints
4587 // to their correct minimum values, stop them being set as we need
4588 // them to remain at our required size for the parent GtkSocket to
4589 // give us the right initial size.
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004590 if (gtk_socket_id != 0 && (mask & WidthValue || mask & HeightValue))
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004591 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004592 update_window_manager_hints(pixel_width, pixel_height);
4593 init_window_hints_state = 1;
Bram Moolenaar4ab79682017-08-27 14:50:47 +02004594 timeout_add(1000, check_startup_plug_hints, NULL);
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004595 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004596 }
4597
Bram Moolenaar79ef6d62009-09-23 15:35:48 +00004598 pixel_width = (guint)(gui_get_base_width() + Columns * gui.char_width);
4599 pixel_height = (guint)(gui_get_base_height() + Rows * gui.char_height);
Bram Moolenaar30613902019-12-01 22:11:18 +01004600 // For GTK2 changing the size of the form widget doesn't cause window
4601 // resizing.
Bram Moolenaardebe25a2010-06-06 17:41:24 +02004602 if (gtk_socket_id == 0)
Bram Moolenaar79ef6d62009-09-23 15:35:48 +00004603 gtk_window_resize(GTK_WINDOW(gui.mainwin), pixel_width, pixel_height);
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004604 update_window_manager_hints(0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004605
4606 if (foreground_argument != NULL)
4607 fg_pixel = gui_get_color((char_u *)foreground_argument);
4608 if (fg_pixel == INVALCOLOR)
4609 fg_pixel = gui_get_color((char_u *)"Black");
4610
4611 if (background_argument != NULL)
4612 bg_pixel = gui_get_color((char_u *)background_argument);
4613 if (bg_pixel == INVALCOLOR)
4614 bg_pixel = gui_get_color((char_u *)"White");
4615
4616 if (found_reverse_arg)
4617 {
4618 gui.def_norm_pixel = bg_pixel;
4619 gui.def_back_pixel = fg_pixel;
4620 }
4621 else
4622 {
4623 gui.def_norm_pixel = fg_pixel;
4624 gui.def_back_pixel = bg_pixel;
4625 }
4626
Bram Moolenaar30613902019-12-01 22:11:18 +01004627 // Get the colors from the "Normal" and "Menu" group (set in syntax.c or
4628 // in a vimrc file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004629 set_normal_colors();
4630
Bram Moolenaar30613902019-12-01 22:11:18 +01004631 // Check that none of the colors are the same as the background color
Bram Moolenaar071d4272004-06-13 20:20:40 +00004632 gui_check_colors();
4633
Bram Moolenaar30613902019-12-01 22:11:18 +01004634 // Get the colors for the highlight groups (gui_check_colors() might have
4635 // changed them).
4636 highlight_gui_started(); // re-init colors and fonts
Bram Moolenaar071d4272004-06-13 20:20:40 +00004637
Bram Moolenaar98921892016-02-23 17:14:37 +01004638 g_signal_connect(G_OBJECT(gui.mainwin), "destroy",
4639 G_CALLBACK(mainwin_destroy_cb), NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004640
Bram Moolenaar071d4272004-06-13 20:20:40 +00004641 /*
4642 * Notify the fixed area about the need to resize the contents of the
4643 * gui.formwin, which we use for random positioning of the included
4644 * components.
4645 *
4646 * We connect this signal deferred finally after anything is in place,
4647 * since this is intended to handle resizements coming from the window
4648 * manager upon us and should not interfere with what VIM is requesting
4649 * upon startup.
4650 */
Ernie Raeld42b8392022-04-23 19:52:23 +01004651#ifdef TRACK_RESIZE_HISTORY
Ernie Rael9f53e7b2022-04-17 18:27:49 +01004652 latest_resize_hist = ALLOC_CLEAR_ONE(resize_hist_T);
Ernie Raeld42b8392022-04-23 19:52:23 +01004653#endif
Bram Moolenaar98921892016-02-23 17:14:37 +01004654 g_signal_connect(G_OBJECT(gui.formwin), "configure-event",
Ernie Rael9f53e7b2022-04-17 18:27:49 +01004655 G_CALLBACK(form_configure_event), NULL);
lilydjwg6e0a18f2024-01-29 20:54:28 +01004656#if GTK_CHECK_VERSION(3,10,0)
4657 g_signal_connect(G_OBJECT(gui.formwin), "notify::scale-factor",
4658 G_CALLBACK(scale_factor_event), NULL);
4659#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004660
4661#ifdef FEAT_DND
Bram Moolenaar30613902019-12-01 22:11:18 +01004662 // Set up for receiving DND items.
Bram Moolenaara76638f2010-06-05 12:49:46 +02004663 gui_gtk_set_dnd_targets();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004664
Bram Moolenaar98921892016-02-23 17:14:37 +01004665 g_signal_connect(G_OBJECT(gui.drawarea), "drag-data-received",
Ernie Rael9f53e7b2022-04-17 18:27:49 +01004666 G_CALLBACK(drag_data_received_cb), NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004667#endif
4668
Bram Moolenaar30613902019-12-01 22:11:18 +01004669 // With GTK+ 2, we need to iconify the window before calling show()
4670 // to avoid mapping the window for a short time.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004671 if (found_iconic_arg && gtk_socket_id == 0)
4672 gui_mch_iconify();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004673
4674 {
Bram Moolenaar182c5be2010-06-25 05:37:59 +02004675#if defined(FEAT_GUI_GNOME) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004676 unsigned long menu_handler = 0;
4677# ifdef FEAT_TOOLBAR
4678 unsigned long tool_handler = 0;
4679# endif
4680 /*
4681 * Urgh hackish :/ For some reason BonoboDockLayout always forces a
4682 * show when restoring the saved layout configuration. We can't just
4683 * hide the widgets again after gtk_widget_show(gui.mainwin) since it's
4684 * a toplevel window and thus will be realized immediately. Instead,
4685 * connect signal handlers to hide the widgets just after they've been
4686 * marked visible, but before the main window is realized.
4687 */
4688 if (using_gnome && vim_strchr(p_go, GO_MENUS) == NULL)
4689 menu_handler = g_signal_connect_after(gui.menubar_h, "show",
4690 G_CALLBACK(&gtk_widget_hide),
4691 NULL);
4692# ifdef FEAT_TOOLBAR
4693 if (using_gnome && vim_strchr(p_go, GO_TOOLBAR) == NULL
4694 && (toolbar_flags & (TOOLBAR_TEXT | TOOLBAR_ICONS)))
4695 tool_handler = g_signal_connect_after(gui.toolbar_h, "show",
4696 G_CALLBACK(&gtk_widget_hide),
4697 NULL);
4698# endif
4699#endif
4700 gtk_widget_show(gui.mainwin);
4701
Bram Moolenaar182c5be2010-06-25 05:37:59 +02004702#if defined(FEAT_GUI_GNOME) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004703 if (menu_handler != 0)
4704 g_signal_handler_disconnect(gui.menubar_h, menu_handler);
4705# ifdef FEAT_TOOLBAR
4706 if (tool_handler != 0)
4707 g_signal_handler_disconnect(gui.toolbar_h, tool_handler);
4708# endif
4709#endif
4710 }
4711
lilydjwg94ff09a2024-01-29 20:14:01 +01004712 /*
4713 * Add selection handler functions.
4714 */
4715 g_signal_connect(G_OBJECT(gui.drawarea), "selection-clear-event",
4716 G_CALLBACK(selection_clear_event), NULL);
4717 g_signal_connect(G_OBJECT(gui.drawarea), "selection-received",
4718 G_CALLBACK(selection_received_cb), NULL);
4719
4720 g_signal_connect(G_OBJECT(gui.drawarea), "selection-get",
4721 G_CALLBACK(selection_get_cb), NULL);
4722
Bram Moolenaar071d4272004-06-13 20:20:40 +00004723 return OK;
4724}
4725
Bram Moolenaar32fbc4f2020-09-29 22:16:09 +02004726/*
4727 * Clean up for when exiting Vim.
4728 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004729 void
Bram Moolenaarb85cb212009-05-17 14:24:23 +00004730gui_mch_exit(int rc UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004731{
Bram Moolenaar32fbc4f2020-09-29 22:16:09 +02004732 // Clean up, unless we don't want to invoke free().
4733 if (gui.mainwin != NULL && !really_exiting)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004734 gtk_widget_destroy(gui.mainwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004735}
4736
4737/*
4738 * Get the position of the top left corner of the window.
4739 */
4740 int
4741gui_mch_get_winpos(int *x, int *y)
4742{
lilydjwg6e0a18f2024-01-29 20:54:28 +01004743 if (gui_mch_get_display())
4744 {
4745 gtk_window_get_position(GTK_WINDOW(gui.mainwin), x, y);
4746 return OK;
4747 }
4748 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004749}
4750
4751/*
4752 * Set the position of the top left corner of the window to the given
4753 * coordinates.
4754 */
4755 void
4756gui_mch_set_winpos(int x, int y)
4757{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004758 gtk_window_move(GTK_WINDOW(gui.mainwin), x, y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004759}
4760
Bram Moolenaar98921892016-02-23 17:14:37 +01004761#if !GTK_CHECK_VERSION(3,0,0)
4762# if 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004763static int resize_idle_installed = FALSE;
4764/*
4765 * Idle handler to force resize. Used by gui_mch_set_shellsize() to ensure
4766 * the shell size doesn't exceed the window size, i.e. if the window manager
4767 * ignored our size request. Usually this happens if the window is maximized.
4768 *
4769 * FIXME: It'd be nice if we could find a little more orthodox solution.
4770 * See also the remark below in gui_mch_set_shellsize().
4771 *
4772 * DISABLED: When doing ":set lines+=1" this function would first invoke
4773 * gui_resize_shell() with the old size, then the normal callback would
4774 * report the new size through form_configure_event(). That caused the window
4775 * layout to be messed up.
4776 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004777 static gboolean
4778force_shell_resize_idle(gpointer data)
4779{
4780 if (gui.mainwin != NULL
4781 && GTK_WIDGET_REALIZED(gui.mainwin)
4782 && GTK_WIDGET_VISIBLE(gui.mainwin))
4783 {
4784 int width;
4785 int height;
4786
4787 gtk_window_get_size(GTK_WINDOW(gui.mainwin), &width, &height);
4788
4789 width -= get_menu_tool_width();
4790 height -= get_menu_tool_height();
4791
4792 gui_resize_shell(width, height);
4793 }
4794
4795 resize_idle_installed = FALSE;
Bram Moolenaar30613902019-12-01 22:11:18 +01004796 return FALSE; // don't call me again
Bram Moolenaar071d4272004-06-13 20:20:40 +00004797}
Bram Moolenaar98921892016-02-23 17:14:37 +01004798# endif
Bram Moolenaar30613902019-12-01 22:11:18 +01004799#endif // !GTK_CHECK_VERSION(3,0,0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004800
Bram Moolenaar09736232009-09-23 16:14:49 +00004801/*
4802 * Return TRUE if the main window is maximized.
4803 */
4804 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004805gui_mch_maximized(void)
Bram Moolenaar09736232009-09-23 16:14:49 +00004806{
Bram Moolenaar98921892016-02-23 17:14:37 +01004807 return (gui.mainwin != NULL && gtk_widget_get_window(gui.mainwin) != NULL
4808 && (gdk_window_get_state(gtk_widget_get_window(gui.mainwin))
4809 & GDK_WINDOW_STATE_MAXIMIZED));
Bram Moolenaar09736232009-09-23 16:14:49 +00004810}
4811
4812/*
4813 * Unmaximize the main window
4814 */
4815 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004816gui_mch_unmaximize(void)
Bram Moolenaar09736232009-09-23 16:14:49 +00004817{
4818 if (gui.mainwin != NULL)
4819 gtk_window_unmaximize(GTK_WINDOW(gui.mainwin));
4820}
Bram Moolenaar09736232009-09-23 16:14:49 +00004821
Bram Moolenaar071d4272004-06-13 20:20:40 +00004822/*
Bram Moolenaar8ac44152017-11-09 18:33:29 +01004823 * Called when the font changed while the window is maximized or GO_KEEPWINSIZE
4824 * is set. Compute the new Rows and Columns. This is like resizing the
4825 * window.
Bram Moolenaar12bc1b52011-08-10 17:44:45 +02004826 */
4827 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004828gui_mch_newfont(void)
Bram Moolenaar12bc1b52011-08-10 17:44:45 +02004829{
4830 int w, h;
4831
4832 gtk_window_get_size(GTK_WINDOW(gui.mainwin), &w, &h);
4833 w -= get_menu_tool_width();
4834 h -= get_menu_tool_height();
4835 gui_resize_shell(w, h);
4836}
4837
4838/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004839 * Set the windows size.
4840 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004841 void
4842gui_mch_set_shellsize(int width, int height,
Bram Moolenaarb85cb212009-05-17 14:24:23 +00004843 int min_width UNUSED, int min_height UNUSED,
4844 int base_width UNUSED, int base_height UNUSED,
4845 int direction UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004846{
Bram Moolenaar30613902019-12-01 22:11:18 +01004847 // give GTK+ a chance to put all widget's into place
Bram Moolenaar071d4272004-06-13 20:20:40 +00004848 gui_mch_update();
4849
Bram Moolenaar30613902019-12-01 22:11:18 +01004850 // this will cause the proper resizement to happen too
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004851 if (gtk_socket_id == 0)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004852 update_window_manager_hints(0, 0);
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004853
Bram Moolenaar30613902019-12-01 22:11:18 +01004854 // With GTK+ 2, changing the size of the form widget doesn't resize
4855 // the window. So let's do it the other way around and resize the
4856 // main window instead.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004857 width += get_menu_tool_width();
4858 height += get_menu_tool_height();
4859
Ernie Raeld42b8392022-04-23 19:52:23 +01004860#ifdef TRACK_RESIZE_HISTORY
Ernie Rael9f53e7b2022-04-17 18:27:49 +01004861 alloc_resize_hist(width, height); // track the resize request
Ernie Raeld42b8392022-04-23 19:52:23 +01004862#endif
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004863 if (gtk_socket_id == 0)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004864 gtk_window_resize(GTK_WINDOW(gui.mainwin), width, height);
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004865 else
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004866 update_window_manager_hints(width, height);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004867
Bram Moolenaar98921892016-02-23 17:14:37 +01004868# if !GTK_CHECK_VERSION(3,0,0)
4869# if 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004870 if (!resize_idle_installed)
4871 {
4872 g_idle_add_full(GDK_PRIORITY_EVENTS + 10,
4873 &force_shell_resize_idle, NULL, NULL);
4874 resize_idle_installed = TRUE;
4875 }
Bram Moolenaar98921892016-02-23 17:14:37 +01004876# endif
Bram Moolenaar30613902019-12-01 22:11:18 +01004877# endif // !GTK_CHECK_VERSION(3,0,0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004878 /*
4879 * Wait until all events are processed to prevent a crash because the
4880 * real size of the drawing area doesn't reflect Vim's internal ideas.
4881 *
4882 * This is a bit of a hack, since Vim is a terminal application with a GUI
4883 * on top, while the GUI expects to be the boss.
4884 */
4885 gui_mch_update();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004886}
4887
Bram Moolenaar071d4272004-06-13 20:20:40 +00004888 void
Bram Moolenaarb85cb212009-05-17 14:24:23 +00004889gui_mch_settitle(char_u *title, char_u *icon UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004890{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004891 if (title != NULL && output_conv.vc_type != CONV_NONE)
4892 title = string_convert(&output_conv, title, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004893
4894 gtk_window_set_title(GTK_WINDOW(gui.mainwin), (const char *)title);
4895
Bram Moolenaar071d4272004-06-13 20:20:40 +00004896 if (output_conv.vc_type != CONV_NONE)
4897 vim_free(title);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004898}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004899
4900#if defined(FEAT_MENU) || defined(PROTO)
4901 void
4902gui_mch_enable_menu(int showit)
4903{
4904 GtkWidget *widget;
4905
4906# ifdef FEAT_GUI_GNOME
4907 if (using_gnome)
4908 widget = gui.menubar_h;
4909 else
4910# endif
4911 widget = gui.menubar;
4912
Bram Moolenaar30613902019-12-01 22:11:18 +01004913 // Do not disable the menu while starting up, otherwise F10 doesn't work.
Bram Moolenaar98921892016-02-23 17:14:37 +01004914 if (!showit != !gtk_widget_get_visible(widget) && !gui.starting)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004915 {
4916 if (showit)
4917 gtk_widget_show(widget);
4918 else
4919 gtk_widget_hide(widget);
4920
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004921 update_window_manager_hints(0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004922 }
4923}
Bram Moolenaar30613902019-12-01 22:11:18 +01004924#endif // FEAT_MENU
Bram Moolenaar071d4272004-06-13 20:20:40 +00004925
4926#if defined(FEAT_TOOLBAR) || defined(PROTO)
4927 void
4928gui_mch_show_toolbar(int showit)
4929{
4930 GtkWidget *widget;
4931
4932 if (gui.toolbar == NULL)
4933 return;
4934
4935# ifdef FEAT_GUI_GNOME
4936 if (using_gnome)
4937 widget = gui.toolbar_h;
4938 else
4939# endif
4940 widget = gui.toolbar;
4941
4942 if (showit)
4943 set_toolbar_style(GTK_TOOLBAR(gui.toolbar));
4944
Bram Moolenaar98921892016-02-23 17:14:37 +01004945 if (!showit != !gtk_widget_get_visible(widget))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004946 {
4947 if (showit)
4948 gtk_widget_show(widget);
4949 else
4950 gtk_widget_hide(widget);
4951
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004952 update_window_manager_hints(0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004953 }
4954}
Bram Moolenaar30613902019-12-01 22:11:18 +01004955#endif // FEAT_TOOLBAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00004956
Bram Moolenaar071d4272004-06-13 20:20:40 +00004957/*
4958 * Check if a given font is a CJK font. This is done in a very crude manner. It
4959 * just see if U+04E00 for zh and ja and U+AC00 for ko are covered in a given
4960 * font. Consequently, this function cannot be used as a general purpose check
4961 * for CJK-ness for which fontconfig APIs should be used. This is only used by
4962 * gui_mch_init_font() to deal with 'CJK fixed width fonts'.
4963 */
4964 static int
4965is_cjk_font(PangoFontDescription *font_desc)
4966{
4967 static const char * const cjk_langs[] =
4968 {"zh_CN", "zh_TW", "zh_HK", "ja", "ko"};
4969
4970 PangoFont *font;
4971 unsigned i;
4972 int is_cjk = FALSE;
4973
4974 font = pango_context_load_font(gui.text_context, font_desc);
4975
4976 if (font == NULL)
4977 return FALSE;
4978
4979 for (i = 0; !is_cjk && i < G_N_ELEMENTS(cjk_langs); ++i)
4980 {
4981 PangoCoverage *coverage;
4982 gunichar uc;
4983
Bram Moolenaar14285cb2020-03-27 20:58:37 +01004984 // Valgrind reports a leak for pango_language_from_string(), but the
4985 // documentation says "This is owned by Pango and should not be freed".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004986 coverage = pango_font_get_coverage(
4987 font, pango_language_from_string(cjk_langs[i]));
4988
4989 if (coverage != NULL)
4990 {
4991 uc = (cjk_langs[i][0] == 'k') ? 0xAC00 : 0x4E00;
4992 is_cjk = (pango_coverage_get(coverage, uc) == PANGO_COVERAGE_EXACT);
James McCoyfe146362023-08-28 21:29:13 +02004993#if PANGO_VERSION_CHECK(1, 52, 0)
4994 g_object_unref(coverage);
4995#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004996 pango_coverage_unref(coverage);
James McCoyfe146362023-08-28 21:29:13 +02004997#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004998 }
4999 }
5000
5001 g_object_unref(font);
5002
5003 return is_cjk;
5004}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005005
Bram Moolenaar231334e2005-07-25 20:46:57 +00005006/*
5007 * Adjust gui.char_height (after 'linespace' was changed).
5008 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005009 int
Bram Moolenaar231334e2005-07-25 20:46:57 +00005010gui_mch_adjust_charheight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005011{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005012 PangoFontMetrics *metrics;
5013 int ascent;
5014 int descent;
5015
5016 metrics = pango_context_get_metrics(gui.text_context, gui.norm_font,
5017 pango_context_get_language(gui.text_context));
5018 ascent = pango_font_metrics_get_ascent(metrics);
5019 descent = pango_font_metrics_get_descent(metrics);
5020
5021 pango_font_metrics_unref(metrics);
5022
Bram Moolenaar70cf4582020-10-27 20:43:26 +01005023 // Round up when the value is more than about 1/16 of a pixel above a whole
5024 // pixel (12.0624 becomes 12, 12.07 becomes 13). Then add 'linespace'.
5025 gui.char_height = (ascent + descent + (PANGO_SCALE * 15) / 16)
5026 / PANGO_SCALE + p_linespace;
Bram Moolenaar30613902019-12-01 22:11:18 +01005027 // LINTED: avoid warning: bitwise operation on signed value
Bram Moolenaar071d4272004-06-13 20:20:40 +00005028 gui.char_ascent = PANGO_PIXELS(ascent + p_linespace * PANGO_SCALE / 2);
5029
Bram Moolenaar30613902019-12-01 22:11:18 +01005030 // A not-positive value of char_height may crash Vim. Only happens
5031 // if 'linespace' is negative (which does make sense sometimes).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005032 gui.char_ascent = MAX(gui.char_ascent, 0);
5033 gui.char_height = MAX(gui.char_height, gui.char_ascent + 1);
5034
5035 return OK;
5036}
5037
Bram Moolenaar98921892016-02-23 17:14:37 +01005038#if GTK_CHECK_VERSION(3,0,0)
Bram Moolenaar30613902019-12-01 22:11:18 +01005039// Callback function used in gui_mch_font_dialog()
Bram Moolenaar98921892016-02-23 17:14:37 +01005040 static gboolean
5041font_filter(const PangoFontFamily *family,
5042 const PangoFontFace *face UNUSED,
5043 gpointer data UNUSED)
5044{
5045 return pango_font_family_is_monospace((PangoFontFamily *)family);
5046}
5047#endif
5048
Bram Moolenaar071d4272004-06-13 20:20:40 +00005049/*
5050 * Put up a font dialog and return the selected font name in allocated memory.
5051 * "oldval" is the previous value. Return NULL when cancelled.
5052 * This should probably go into gui_gtk.c. Hmm.
5053 * FIXME:
5054 * The GTK2 font selection dialog has no filtering API. So we could either
5055 * a) implement our own (possibly copying the code from somewhere else) or
5056 * b) just live with it.
5057 */
5058 char_u *
5059gui_mch_font_dialog(char_u *oldval)
5060{
5061 GtkWidget *dialog;
5062 int response;
5063 char_u *fontname = NULL;
5064 char_u *oldname;
5065
Bram Moolenaar98921892016-02-23 17:14:37 +01005066#if GTK_CHECK_VERSION(3,2,0)
5067 dialog = gtk_font_chooser_dialog_new(NULL, NULL);
5068 gtk_font_chooser_set_filter_func(GTK_FONT_CHOOSER(dialog), font_filter,
5069 NULL, NULL);
5070#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005071 dialog = gtk_font_selection_dialog_new(NULL);
Bram Moolenaar98921892016-02-23 17:14:37 +01005072#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005073
5074 gtk_window_set_transient_for(GTK_WINDOW(dialog), GTK_WINDOW(gui.mainwin));
5075 gtk_window_set_destroy_with_parent(GTK_WINDOW(dialog), TRUE);
5076
5077 if (oldval != NULL && oldval[0] != NUL)
5078 {
5079 if (output_conv.vc_type != CONV_NONE)
5080 oldname = string_convert(&output_conv, oldval, NULL);
5081 else
5082 oldname = oldval;
5083
Bram Moolenaar30613902019-12-01 22:11:18 +01005084 // Annoying bug in GTK (or Pango): if the font name does not include a
5085 // size, zero is used. Use default point size ten.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005086 if (!vim_isdigit(oldname[STRLEN(oldname) - 1]))
5087 {
5088 char_u *p = vim_strnsave(oldname, STRLEN(oldname) + 3);
5089
5090 if (p != NULL)
5091 {
5092 STRCPY(p + STRLEN(p), " 10");
5093 if (oldname != oldval)
5094 vim_free(oldname);
5095 oldname = p;
5096 }
5097 }
5098
Bram Moolenaar98921892016-02-23 17:14:37 +01005099#if GTK_CHECK_VERSION(3,2,0)
5100 gtk_font_chooser_set_font(
5101 GTK_FONT_CHOOSER(dialog), (const gchar *)oldname);
5102#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005103 gtk_font_selection_dialog_set_font_name(
5104 GTK_FONT_SELECTION_DIALOG(dialog), (const char *)oldname);
Bram Moolenaar98921892016-02-23 17:14:37 +01005105#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005106
5107 if (oldname != oldval)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005108 vim_free(oldname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005109 }
Bram Moolenaarbef9d832009-09-11 13:46:41 +00005110 else
Bram Moolenaar98921892016-02-23 17:14:37 +01005111#if GTK_CHECK_VERSION(3,2,0)
5112 gtk_font_chooser_set_font(
5113 GTK_FONT_CHOOSER(dialog), DEFAULT_FONT);
5114#else
Bram Moolenaarbef9d832009-09-11 13:46:41 +00005115 gtk_font_selection_dialog_set_font_name(
5116 GTK_FONT_SELECTION_DIALOG(dialog), DEFAULT_FONT);
Bram Moolenaar98921892016-02-23 17:14:37 +01005117#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005118
5119 response = gtk_dialog_run(GTK_DIALOG(dialog));
5120
5121 if (response == GTK_RESPONSE_OK)
5122 {
5123 char *name;
5124
Bram Moolenaar98921892016-02-23 17:14:37 +01005125#if GTK_CHECK_VERSION(3,2,0)
5126 name = gtk_font_chooser_get_font(GTK_FONT_CHOOSER(dialog));
5127#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005128 name = gtk_font_selection_dialog_get_font_name(
5129 GTK_FONT_SELECTION_DIALOG(dialog));
Bram Moolenaar98921892016-02-23 17:14:37 +01005130#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005131 if (name != NULL)
5132 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005133 char_u *p;
5134
Bram Moolenaar30613902019-12-01 22:11:18 +01005135 // Apparently some font names include a comma, need to escape
5136 // that, because in 'guifont' it separates names.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005137 p = vim_strsave_escaped((char_u *)name, (char_u *)",");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005138 g_free(name);
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005139 if (p != NULL && input_conv.vc_type != CONV_NONE)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005140 {
5141 fontname = string_convert(&input_conv, p, NULL);
5142 vim_free(p);
5143 }
5144 else
5145 fontname = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005146 }
5147 }
5148
5149 if (response != GTK_RESPONSE_NONE)
5150 gtk_widget_destroy(dialog);
5151
5152 return fontname;
5153}
5154
5155/*
5156 * Some monospace fonts don't support a bold weight, and fall back
5157 * silently to the regular weight. But this is no good since our text
5158 * drawing function can emulate bold by overstriking. So let's try
5159 * to detect whether bold weight is actually available and emulate it
5160 * otherwise.
5161 *
5162 * Note that we don't need to check for italic style since Xft can
5163 * emulate italic on its own, provided you have a proper fontconfig
5164 * setup. We wouldn't be able to emulate it in Vim anyway.
5165 */
5166 static void
5167get_styled_font_variants(void)
5168{
5169 PangoFontDescription *bold_font_desc;
5170 PangoFont *plain_font;
5171 PangoFont *bold_font;
5172
5173 gui.font_can_bold = FALSE;
5174
5175 plain_font = pango_context_load_font(gui.text_context, gui.norm_font);
5176
5177 if (plain_font == NULL)
5178 return;
5179
5180 bold_font_desc = pango_font_description_copy_static(gui.norm_font);
5181 pango_font_description_set_weight(bold_font_desc, PANGO_WEIGHT_BOLD);
5182
5183 bold_font = pango_context_load_font(gui.text_context, bold_font_desc);
5184 /*
5185 * The comparison relies on the unique handle nature of a PangoFont*,
5186 * i.e. it's assumed that a different PangoFont* won't refer to the
5187 * same font. Seems to work, and failing here isn't critical anyway.
5188 */
5189 if (bold_font != NULL)
5190 {
5191 gui.font_can_bold = (bold_font != plain_font);
5192 g_object_unref(bold_font);
5193 }
5194
5195 pango_font_description_free(bold_font_desc);
Christian Brabandta5218a72023-11-19 16:25:45 +01005196 if (bold_font != NULL && gui.font_can_bold)
5197 g_object_unref(plain_font);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005198}
5199
Bram Moolenaar071d4272004-06-13 20:20:40 +00005200static PangoEngineShape *default_shape_engine = NULL;
5201
5202/*
5203 * Create a map from ASCII characters in the range [32,126] to glyphs
5204 * of the current font. This is used by gui_gtk2_draw_string() to skip
5205 * the itemize and shaping process for the most common case.
5206 */
5207 static void
5208ascii_glyph_table_init(void)
5209{
Bram Moolenaar16350cb2016-08-14 20:27:34 +02005210 char_u ascii_chars[2 * 128];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005211 PangoAttrList *attr_list;
5212 GList *item_list;
5213 int i;
5214
5215 if (gui.ascii_glyphs != NULL)
5216 pango_glyph_string_free(gui.ascii_glyphs);
5217 if (gui.ascii_font != NULL)
5218 g_object_unref(gui.ascii_font);
5219
5220 gui.ascii_glyphs = NULL;
5221 gui.ascii_font = NULL;
5222
Bram Moolenaar30613902019-12-01 22:11:18 +01005223 // For safety, fill in question marks for the control characters.
5224 // Put a space between characters to avoid shaping.
Bram Moolenaar16350cb2016-08-14 20:27:34 +02005225 for (i = 0; i < 128; ++i)
5226 {
5227 if (i >= 32 && i < 127)
5228 ascii_chars[2 * i] = i;
5229 else
5230 ascii_chars[2 * i] = '?';
5231 ascii_chars[2 * i + 1] = ' ';
5232 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005233
5234 attr_list = pango_attr_list_new();
5235 item_list = pango_itemize(gui.text_context, (const char *)ascii_chars,
5236 0, sizeof(ascii_chars), attr_list, NULL);
5237
Bram Moolenaar30613902019-12-01 22:11:18 +01005238 if (item_list != NULL && item_list->next == NULL) // play safe
Bram Moolenaar071d4272004-06-13 20:20:40 +00005239 {
5240 PangoItem *item;
5241 int width;
5242
5243 item = (PangoItem *)item_list->data;
5244 width = gui.char_width * PANGO_SCALE;
5245
Bram Moolenaar30613902019-12-01 22:11:18 +01005246 // Remember the shape engine used for ASCII.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005247 default_shape_engine = item->analysis.shape_engine;
5248
5249 gui.ascii_font = item->analysis.font;
5250 g_object_ref(gui.ascii_font);
5251
5252 gui.ascii_glyphs = pango_glyph_string_new();
5253
5254 pango_shape((const char *)ascii_chars, sizeof(ascii_chars),
5255 &item->analysis, gui.ascii_glyphs);
5256
5257 g_return_if_fail(gui.ascii_glyphs->num_glyphs == sizeof(ascii_chars));
5258
5259 for (i = 0; i < gui.ascii_glyphs->num_glyphs; ++i)
5260 {
5261 PangoGlyphGeometry *geom;
5262
5263 geom = &gui.ascii_glyphs->glyphs[i].geometry;
5264 geom->x_offset += MAX(0, width - geom->width) / 2;
5265 geom->width = width;
5266 }
5267 }
5268
Bram Moolenaar81a4cf42020-09-09 19:05:13 +02005269 // TODO: is this type cast OK?
5270 g_list_foreach(item_list, (GFunc)(void *)&pango_item_free, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005271 g_list_free(item_list);
5272 pango_attr_list_unref(attr_list);
5273}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005274
5275/*
5276 * Initialize Vim to use the font or fontset with the given name.
5277 * Return FAIL if the font could not be loaded, OK otherwise.
5278 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005279 int
Bram Moolenaarb85cb212009-05-17 14:24:23 +00005280gui_mch_init_font(char_u *font_name, int fontset UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005281{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005282 PangoFontDescription *font_desc;
5283 PangoLayout *layout;
5284 int width;
5285
Bram Moolenaar30613902019-12-01 22:11:18 +01005286 // If font_name is NULL, this means to use the default, which should
5287 // be present on all proper Pango/fontconfig installations.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005288 if (font_name == NULL)
5289 font_name = (char_u *)DEFAULT_FONT;
5290
5291 font_desc = gui_mch_get_font(font_name, FALSE);
5292
5293 if (font_desc == NULL)
5294 return FAIL;
5295
5296 gui_mch_free_font(gui.norm_font);
5297 gui.norm_font = font_desc;
5298
5299 pango_context_set_font_description(gui.text_context, font_desc);
5300
5301 layout = pango_layout_new(gui.text_context);
5302 pango_layout_set_text(layout, "MW", 2);
5303 pango_layout_get_size(layout, &width, NULL);
5304 /*
5305 * Set char_width to half the width obtained from pango_layout_get_size()
5306 * for CJK fixed_width/bi-width fonts. An unpatched version of Xft leads
5307 * Pango to use the same width for both non-CJK characters (e.g. Latin
5308 * letters and numbers) and CJK characters. This results in 's p a c e d
5309 * o u t' rendering when a CJK 'fixed width' font is used. To work around
5310 * that, divide the width returned by Pango by 2 if cjk_width is equal to
5311 * width for CJK fonts.
5312 *
5313 * For related bugs, see:
5314 * http://bugzilla.gnome.org/show_bug.cgi?id=106618
5315 * http://bugzilla.gnome.org/show_bug.cgi?id=106624
5316 *
5317 * With this, for all four of the following cases, Vim works fine:
5318 * guifont=CJK_fixed_width_font
5319 * guifont=Non_CJK_fixed_font
5320 * guifont=Non_CJK_fixed_font,CJK_Fixed_font
5321 * guifont=Non_CJK_fixed_font guifontwide=CJK_fixed_font
5322 */
5323 if (is_cjk_font(gui.norm_font))
5324 {
5325 int cjk_width;
5326
Bram Moolenaar30613902019-12-01 22:11:18 +01005327 // Measure the text extent of U+4E00 and U+4E8C
Bram Moolenaar071d4272004-06-13 20:20:40 +00005328 pango_layout_set_text(layout, "\344\270\200\344\272\214", -1);
5329 pango_layout_get_size(layout, &cjk_width, NULL);
5330
Bram Moolenaar30613902019-12-01 22:11:18 +01005331 if (width == cjk_width) // Xft not patched
Bram Moolenaar071d4272004-06-13 20:20:40 +00005332 width /= 2;
5333 }
5334 g_object_unref(layout);
5335
5336 gui.char_width = (width / 2 + PANGO_SCALE - 1) / PANGO_SCALE;
5337
Bram Moolenaar30613902019-12-01 22:11:18 +01005338 // A zero width may cause a crash. Happens for semi-invalid fontsets.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005339 if (gui.char_width <= 0)
5340 gui.char_width = 8;
5341
Bram Moolenaar231334e2005-07-25 20:46:57 +00005342 gui_mch_adjust_charheight();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005343
Bram Moolenaar30613902019-12-01 22:11:18 +01005344 // Set the fontname, which will be used for information purposes
Bram Moolenaar071d4272004-06-13 20:20:40 +00005345 hl_set_font_name(font_name);
5346
5347 get_styled_font_variants();
5348 ascii_glyph_table_init();
5349
Bram Moolenaar30613902019-12-01 22:11:18 +01005350 // Avoid unnecessary overhead if 'guifontwide' is equal to 'guifont'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005351 if (gui.wide_font != NULL
5352 && pango_font_description_equal(gui.norm_font, gui.wide_font))
5353 {
5354 pango_font_description_free(gui.wide_font);
5355 gui.wide_font = NULL;
5356 }
5357
Bram Moolenaare161c792009-11-03 17:13:59 +00005358 if (gui_mch_maximized())
5359 {
Bram Moolenaar30613902019-12-01 22:11:18 +01005360 // Update lines and columns in accordance with the new font, keep the
5361 // window maximized.
Bram Moolenaar12bc1b52011-08-10 17:44:45 +02005362 gui_mch_newfont();
Bram Moolenaare161c792009-11-03 17:13:59 +00005363 }
5364 else
Bram Moolenaare161c792009-11-03 17:13:59 +00005365 {
Bram Moolenaar30613902019-12-01 22:11:18 +01005366 // Preserve the logical dimensions of the screen.
Bram Moolenaare161c792009-11-03 17:13:59 +00005367 update_window_manager_hints(0, 0);
5368 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005369
5370 return OK;
5371}
5372
5373/*
5374 * Get a reference to the font "name".
5375 * Return zero for failure.
5376 */
5377 GuiFont
5378gui_mch_get_font(char_u *name, int report_error)
5379{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005380 PangoFontDescription *font;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005381
Bram Moolenaar30613902019-12-01 22:11:18 +01005382 // can't do this when GUI is not running
Bram Moolenaar071d4272004-06-13 20:20:40 +00005383 if (!gui.in_use || name == NULL)
5384 return NULL;
5385
Bram Moolenaar071d4272004-06-13 20:20:40 +00005386 if (output_conv.vc_type != CONV_NONE)
5387 {
5388 char_u *buf;
5389
5390 buf = string_convert(&output_conv, name, NULL);
5391 if (buf != NULL)
5392 {
5393 font = pango_font_description_from_string((const char *)buf);
5394 vim_free(buf);
5395 }
5396 else
5397 font = NULL;
5398 }
5399 else
5400 font = pango_font_description_from_string((const char *)name);
5401
5402 if (font != NULL)
5403 {
5404 PangoFont *real_font;
5405
Bram Moolenaar30613902019-12-01 22:11:18 +01005406 // pango_context_load_font() bails out if no font size is set
Bram Moolenaar071d4272004-06-13 20:20:40 +00005407 if (pango_font_description_get_size(font) <= 0)
5408 pango_font_description_set_size(font, 10 * PANGO_SCALE);
5409
5410 real_font = pango_context_load_font(gui.text_context, font);
5411
5412 if (real_font == NULL)
5413 {
5414 pango_font_description_free(font);
5415 font = NULL;
5416 }
5417 else
5418 g_object_unref(real_font);
5419 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005420
5421 if (font == NULL)
5422 {
5423 if (report_error)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00005424 semsg(_((char *)e_unknown_font_str), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005425 return NULL;
5426 }
5427
Bram Moolenaar071d4272004-06-13 20:20:40 +00005428 return font;
5429}
5430
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005431#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar46c9c732004-12-12 11:37:09 +00005432/*
5433 * Return the name of font "font" in allocated memory.
5434 */
Bram Moolenaar46c9c732004-12-12 11:37:09 +00005435 char_u *
Bram Moolenaarb85cb212009-05-17 14:24:23 +00005436gui_mch_get_fontname(GuiFont font, char_u *name UNUSED)
Bram Moolenaar46c9c732004-12-12 11:37:09 +00005437{
Bram Moolenaar46c9c732004-12-12 11:37:09 +00005438 if (font != NOFONT)
5439 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00005440 char *pangoname = pango_font_description_to_string(font);
Bram Moolenaar46c9c732004-12-12 11:37:09 +00005441
Bram Moolenaar89d40322006-08-29 15:30:07 +00005442 if (pangoname != NULL)
Bram Moolenaar46c9c732004-12-12 11:37:09 +00005443 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00005444 char_u *s = vim_strsave((char_u *)pangoname);
Bram Moolenaar46c9c732004-12-12 11:37:09 +00005445
Bram Moolenaar89d40322006-08-29 15:30:07 +00005446 g_free(pangoname);
Bram Moolenaar46c9c732004-12-12 11:37:09 +00005447 return s;
5448 }
5449 }
Bram Moolenaar46c9c732004-12-12 11:37:09 +00005450 return NULL;
5451}
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005452#endif
Bram Moolenaar46c9c732004-12-12 11:37:09 +00005453
Bram Moolenaar071d4272004-06-13 20:20:40 +00005454/*
5455 * If a font is not going to be used, free its structure.
5456 */
5457 void
5458gui_mch_free_font(GuiFont font)
5459{
5460 if (font != NOFONT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005461 pango_font_description_free(font);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005462}
5463
Bram Moolenaar071d4272004-06-13 20:20:40 +00005464/*
Yee Cheng Chin290b8872023-10-05 20:54:21 +02005465 * Cmdline expansion for setting 'guifont' / 'guifontwide'. Will enumerate
5466 * through all fonts for completion. When setting 'guifont' it will only show
5467 * monospace fonts as it's unlikely other fonts would be useful.
5468 */
5469 void
5470gui_mch_expand_font(optexpand_T *args, void *param, int (*add_match)(char_u *val))
5471{
5472 PangoFontFamily **font_families = NULL;
5473 int n_families = 0;
5474 int wide = *(int *)param;
5475
5476 if (args->oe_include_orig_val && *args->oe_opt_value == NUL && !wide)
5477 {
5478 // If guifont is empty, and we want to fill in the orig value, suggest
5479 // the default so the user can modify it.
5480 if (add_match((char_u *)DEFAULT_FONT) != OK)
5481 return;
5482 }
5483
5484 pango_context_list_families(
5485 gui.text_context,
5486 &font_families,
5487 &n_families);
5488
5489 for (int i = 0; i < n_families; i++)
5490 {
5491 if (!wide && !pango_font_family_is_monospace(font_families[i]))
5492 continue;
5493
5494 const char* fam_name = pango_font_family_get_name(font_families[i]);
5495 if (input_conv.vc_type != CONV_NONE)
5496 {
5497 char_u *buf = string_convert(&input_conv, (char_u*)fam_name, NULL);
5498 if (buf != NULL)
5499 {
5500 if (add_match(buf) != OK)
5501 {
5502 vim_free(buf);
5503 break;
5504 }
5505 vim_free(buf);
5506 }
5507 else
5508 break;
5509 }
5510 else
5511 {
5512 if (add_match((char_u *)fam_name) != OK)
5513 break;
5514 }
5515 }
5516
5517 g_free(font_families);
5518}
5519
5520/*
Bram Moolenaar36edf062016-07-21 22:10:12 +02005521 * Return the Pixel value (color) for the given color name.
5522 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00005523 * Return INVALCOLOR for error.
5524 */
5525 guicolor_T
5526gui_mch_get_color(char_u *name)
5527{
Bram Moolenaar2e324952018-04-14 14:37:07 +02005528 guicolor_T color = INVALCOLOR;
5529
Bram Moolenaar30613902019-12-01 22:11:18 +01005530 if (!gui.in_use) // can't do this when GUI not running
Bram Moolenaar2e324952018-04-14 14:37:07 +02005531 return color;
5532
5533 if (name != NULL)
5534 color = gui_get_color_cmn(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005535
Bram Moolenaar98921892016-02-23 17:14:37 +01005536#if GTK_CHECK_VERSION(3,0,0)
Bram Moolenaar2e324952018-04-14 14:37:07 +02005537 return color;
Bram Moolenaar98921892016-02-23 17:14:37 +01005538#else
Bram Moolenaar36edf062016-07-21 22:10:12 +02005539 if (color == INVALCOLOR)
5540 return INVALCOLOR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005541
Bram Moolenaar26af85d2017-07-23 16:45:10 +02005542 return gui_mch_get_rgb_color(
5543 (color & 0xff0000) >> 16,
5544 (color & 0xff00) >> 8,
5545 color & 0xff);
5546#endif
5547}
5548
5549/*
5550 * Return the Pixel value (color) for the given RGB values.
5551 * Return INVALCOLOR for error.
5552 */
5553 guicolor_T
5554gui_mch_get_rgb_color(int r, int g, int b)
5555{
5556#if GTK_CHECK_VERSION(3,0,0)
5557 return gui_get_rgb_color_cmn(r, g, b);
5558#else
5559 GdkColor gcolor;
5560 int ret;
5561
5562 gcolor.red = (guint16)(r / 255.0 * 65535 + 0.5);
5563 gcolor.green = (guint16)(g / 255.0 * 65535 + 0.5);
5564 gcolor.blue = (guint16)(b / 255.0 * 65535 + 0.5);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005565
Bram Moolenaar36edf062016-07-21 22:10:12 +02005566 ret = gdk_colormap_alloc_color(gtk_widget_get_colormap(gui.drawarea),
5567 &gcolor, FALSE, TRUE);
5568
5569 return ret != 0 ? (guicolor_T)gcolor.pixel : INVALCOLOR;
5570#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005571}
5572
5573/*
5574 * Set the current text foreground color.
5575 */
5576 void
5577gui_mch_set_fg_color(guicolor_T color)
5578{
Bram Moolenaar36edf062016-07-21 22:10:12 +02005579#if GTK_CHECK_VERSION(3,0,0)
5580 *gui.fgcolor = color_to_rgba(color);
5581#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005582 gui.fgcolor->pixel = (unsigned long)color;
Bram Moolenaar36edf062016-07-21 22:10:12 +02005583#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005584}
5585
5586/*
5587 * Set the current text background color.
5588 */
5589 void
5590gui_mch_set_bg_color(guicolor_T color)
5591{
Bram Moolenaar36edf062016-07-21 22:10:12 +02005592#if GTK_CHECK_VERSION(3,0,0)
5593 *gui.bgcolor = color_to_rgba(color);
5594#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005595 gui.bgcolor->pixel = (unsigned long)color;
Bram Moolenaar36edf062016-07-21 22:10:12 +02005596#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005597}
5598
Bram Moolenaarf36d3692005-03-15 22:48:14 +00005599/*
5600 * Set the current text special color.
5601 */
5602 void
5603gui_mch_set_sp_color(guicolor_T color)
5604{
Bram Moolenaar36edf062016-07-21 22:10:12 +02005605#if GTK_CHECK_VERSION(3,0,0)
5606 *gui.spcolor = color_to_rgba(color);
5607#else
Bram Moolenaarf36d3692005-03-15 22:48:14 +00005608 gui.spcolor->pixel = (unsigned long)color;
Bram Moolenaar36edf062016-07-21 22:10:12 +02005609#endif
Bram Moolenaarf36d3692005-03-15 22:48:14 +00005610}
5611
Bram Moolenaar071d4272004-06-13 20:20:40 +00005612/*
5613 * Function-like convenience macro for the sake of efficiency.
5614 */
5615#define INSERT_PANGO_ATTR(Attribute, AttrList, Start, End) \
5616 G_STMT_START{ \
5617 PangoAttribute *tmp_attr_; \
5618 tmp_attr_ = (Attribute); \
5619 tmp_attr_->start_index = (Start); \
5620 tmp_attr_->end_index = (End); \
5621 pango_attr_list_insert((AttrList), tmp_attr_); \
5622 }G_STMT_END
5623
5624 static void
5625apply_wide_font_attr(char_u *s, int len, PangoAttrList *attr_list)
5626{
5627 char_u *start = NULL;
5628 char_u *p;
5629 int uc;
5630
5631 for (p = s; p < s + len; p += utf_byte2len(*p))
5632 {
5633 uc = utf_ptr2char(p);
5634
5635 if (start == NULL)
5636 {
5637 if (uc >= 0x80 && utf_char2cells(uc) == 2)
5638 start = p;
5639 }
Bram Moolenaar30613902019-12-01 22:11:18 +01005640 else if (uc < 0x80 // optimization shortcut
Bram Moolenaar071d4272004-06-13 20:20:40 +00005641 || (utf_char2cells(uc) != 2 && !utf_iscomposing(uc)))
5642 {
5643 INSERT_PANGO_ATTR(pango_attr_font_desc_new(gui.wide_font),
5644 attr_list, start - s, p - s);
5645 start = NULL;
5646 }
5647 }
5648
5649 if (start != NULL)
5650 INSERT_PANGO_ATTR(pango_attr_font_desc_new(gui.wide_font),
5651 attr_list, start - s, len);
5652}
5653
5654 static int
5655count_cluster_cells(char_u *s, PangoItem *item,
5656 PangoGlyphString* glyphs, int i,
5657 int *cluster_width,
5658 int *last_glyph_rbearing)
5659{
5660 char_u *p;
Bram Moolenaar30613902019-12-01 22:11:18 +01005661 int next; // glyph start index of next cluster
5662 int start, end; // string segment of current cluster
5663 int width; // real cluster width in Pango units
Bram Moolenaar071d4272004-06-13 20:20:40 +00005664 int uc;
5665 int cellcount = 0;
5666
5667 width = glyphs->glyphs[i].geometry.width;
5668
5669 for (next = i + 1; next < glyphs->num_glyphs; ++next)
5670 {
5671 if (glyphs->glyphs[next].attr.is_cluster_start)
5672 break;
5673 else if (glyphs->glyphs[next].geometry.width > width)
5674 width = glyphs->glyphs[next].geometry.width;
5675 }
5676
5677 start = item->offset + glyphs->log_clusters[i];
5678 end = item->offset + ((next < glyphs->num_glyphs) ?
5679 glyphs->log_clusters[next] : item->length);
5680
5681 for (p = s + start; p < s + end; p += utf_byte2len(*p))
5682 {
5683 uc = utf_ptr2char(p);
5684 if (uc < 0x80)
5685 ++cellcount;
5686 else if (!utf_iscomposing(uc))
5687 cellcount += utf_char2cells(uc);
5688 }
5689
5690 if (last_glyph_rbearing != NULL
5691 && cellcount > 0 && next == glyphs->num_glyphs)
5692 {
5693 PangoRectangle ink_rect;
5694 /*
5695 * If a certain combining mark had to be taken from a non-monospace
5696 * font, we have to compensate manually by adapting x_offset according
5697 * to the ink extents of the previous glyph.
5698 */
5699 pango_font_get_glyph_extents(item->analysis.font,
5700 glyphs->glyphs[i].glyph,
5701 &ink_rect, NULL);
5702
5703 if (PANGO_RBEARING(ink_rect) > 0)
5704 *last_glyph_rbearing = PANGO_RBEARING(ink_rect);
5705 }
5706
5707 if (cellcount > 0)
5708 *cluster_width = width;
5709
5710 return cellcount;
5711}
5712
5713/*
5714 * If there are only combining characters in the cluster, we cannot just
5715 * change the width of the previous glyph since there is none. Therefore
5716 * some guesswork is needed.
5717 *
5718 * If ink_rect.x is negative Pango apparently has taken care of the composing
5719 * by itself. Actually setting x_offset = 0 should be sufficient then, but due
5720 * to problems with composing from different fonts we still need to fine-tune
Bram Moolenaar64404472010-06-26 06:24:45 +02005721 * x_offset to avoid ugliness.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005722 *
5723 * If ink_rect.x is not negative, force overstriking by pointing x_offset to
5724 * the position of the previous glyph. Apparently this happens only with old
5725 * X fonts which don't provide the special combining information needed by
5726 * Pango.
5727 */
5728 static void
5729setup_zero_width_cluster(PangoItem *item, PangoGlyphInfo *glyph,
5730 int last_cellcount, int last_cluster_width,
5731 int last_glyph_rbearing)
5732{
5733 PangoRectangle ink_rect;
5734 PangoRectangle logical_rect;
5735 int width;
5736
5737 width = last_cellcount * gui.char_width * PANGO_SCALE;
5738 glyph->geometry.x_offset = -width + MAX(0, width - last_cluster_width) / 2;
5739 glyph->geometry.width = 0;
5740
5741 pango_font_get_glyph_extents(item->analysis.font,
5742 glyph->glyph,
5743 &ink_rect, &logical_rect);
5744 if (ink_rect.x < 0)
5745 {
5746 glyph->geometry.x_offset += last_glyph_rbearing;
5747 glyph->geometry.y_offset = logical_rect.height
5748 - (gui.char_height - p_linespace) * PANGO_SCALE;
5749 }
Bram Moolenaar706e84b2010-08-07 15:46:45 +02005750 else
Bram Moolenaar30613902019-12-01 22:11:18 +01005751 // If the accent width is smaller than the cluster width, position it
5752 // in the middle.
Bram Moolenaar706e84b2010-08-07 15:46:45 +02005753 glyph->geometry.x_offset = -width + MAX(0, width - ink_rect.width) / 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005754}
5755
Bram Moolenaar98921892016-02-23 17:14:37 +01005756#if GTK_CHECK_VERSION(3,0,0)
5757 static void
5758draw_glyph_string(int row, int col, int num_cells, int flags,
5759 PangoFont *font, PangoGlyphString *glyphs,
5760 cairo_t *cr)
5761#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005762 static void
5763draw_glyph_string(int row, int col, int num_cells, int flags,
5764 PangoFont *font, PangoGlyphString *glyphs)
Bram Moolenaar98921892016-02-23 17:14:37 +01005765#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005766{
5767 if (!(flags & DRAW_TRANSP))
5768 {
Bram Moolenaar98921892016-02-23 17:14:37 +01005769#if GTK_CHECK_VERSION(3,0,0)
Bram Moolenaar36edf062016-07-21 22:10:12 +02005770 cairo_set_source_rgba(cr,
5771 gui.bgcolor->red, gui.bgcolor->green, gui.bgcolor->blue,
5772 gui.bgcolor->alpha);
Bram Moolenaar98921892016-02-23 17:14:37 +01005773 cairo_rectangle(cr,
5774 FILL_X(col), FILL_Y(row),
5775 num_cells * gui.char_width, gui.char_height);
5776 cairo_fill(cr);
5777#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005778 gdk_gc_set_foreground(gui.text_gc, gui.bgcolor);
5779
5780 gdk_draw_rectangle(gui.drawarea->window,
5781 gui.text_gc,
5782 TRUE,
5783 FILL_X(col),
5784 FILL_Y(row),
5785 num_cells * gui.char_width,
5786 gui.char_height);
Bram Moolenaar98921892016-02-23 17:14:37 +01005787#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005788 }
5789
Bram Moolenaar98921892016-02-23 17:14:37 +01005790#if GTK_CHECK_VERSION(3,0,0)
Bram Moolenaar36edf062016-07-21 22:10:12 +02005791 cairo_set_source_rgba(cr,
5792 gui.fgcolor->red, gui.fgcolor->green, gui.fgcolor->blue,
5793 gui.fgcolor->alpha);
Bram Moolenaar98921892016-02-23 17:14:37 +01005794 cairo_move_to(cr, TEXT_X(col), TEXT_Y(row));
5795 pango_cairo_show_glyph_string(cr, font, glyphs);
5796#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005797 gdk_gc_set_foreground(gui.text_gc, gui.fgcolor);
5798
5799 gdk_draw_glyphs(gui.drawarea->window,
5800 gui.text_gc,
5801 font,
5802 TEXT_X(col),
5803 TEXT_Y(row),
5804 glyphs);
Bram Moolenaar98921892016-02-23 17:14:37 +01005805#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005806
Bram Moolenaar30613902019-12-01 22:11:18 +01005807 // redraw the contents with an offset of 1 to emulate bold
Bram Moolenaar071d4272004-06-13 20:20:40 +00005808 if ((flags & DRAW_BOLD) && !gui.font_can_bold)
Bram Moolenaar98921892016-02-23 17:14:37 +01005809#if GTK_CHECK_VERSION(3,0,0)
5810 {
Bram Moolenaar36edf062016-07-21 22:10:12 +02005811 cairo_set_source_rgba(cr,
5812 gui.fgcolor->red, gui.fgcolor->green, gui.fgcolor->blue,
5813 gui.fgcolor->alpha);
Bram Moolenaar98921892016-02-23 17:14:37 +01005814 cairo_move_to(cr, TEXT_X(col) + 1, TEXT_Y(row));
5815 pango_cairo_show_glyph_string(cr, font, glyphs);
5816 }
5817#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005818 gdk_draw_glyphs(gui.drawarea->window,
5819 gui.text_gc,
5820 font,
5821 TEXT_X(col) + 1,
5822 TEXT_Y(row),
5823 glyphs);
Bram Moolenaar98921892016-02-23 17:14:37 +01005824#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005825}
5826
Bram Moolenaarf36d3692005-03-15 22:48:14 +00005827/*
5828 * Draw underline and undercurl at the bottom of the character cell.
5829 */
Bram Moolenaar98921892016-02-23 17:14:37 +01005830#if GTK_CHECK_VERSION(3,0,0)
5831 static void
5832draw_under(int flags, int row, int col, int cells, cairo_t *cr)
5833#else
Bram Moolenaarf36d3692005-03-15 22:48:14 +00005834 static void
5835draw_under(int flags, int row, int col, int cells)
Bram Moolenaar98921892016-02-23 17:14:37 +01005836#endif
Bram Moolenaarf36d3692005-03-15 22:48:14 +00005837{
5838 int i;
5839 int offset;
Bram Moolenaarb85cb212009-05-17 14:24:23 +00005840 static const int val[8] = {1, 0, 0, 0, 1, 2, 2, 2 };
Bram Moolenaarf36d3692005-03-15 22:48:14 +00005841 int y = FILL_Y(row + 1) - 1;
5842
Bram Moolenaar30613902019-12-01 22:11:18 +01005843 // Undercurl: draw curl at the bottom of the character cell.
Bram Moolenaarf36d3692005-03-15 22:48:14 +00005844 if (flags & DRAW_UNDERC)
5845 {
Bram Moolenaar98921892016-02-23 17:14:37 +01005846#if GTK_CHECK_VERSION(3,0,0)
5847 cairo_set_line_width(cr, 1.0);
5848 cairo_set_line_cap(cr, CAIRO_LINE_CAP_BUTT);
Bram Moolenaar36edf062016-07-21 22:10:12 +02005849 cairo_set_source_rgba(cr,
5850 gui.spcolor->red, gui.spcolor->green, gui.spcolor->blue,
5851 gui.spcolor->alpha);
Yamagi9cd93852021-11-20 20:42:29 +00005852 cairo_move_to(cr, FILL_X(col) + 1, y - 2 + 0.5);
5853 for (i = FILL_X(col) + 1; i < FILL_X(col + cells); ++i)
Bram Moolenaar98921892016-02-23 17:14:37 +01005854 {
5855 offset = val[i % 8];
5856 cairo_line_to(cr, i, y - offset + 0.5);
5857 }
5858 cairo_stroke(cr);
5859#else
Bram Moolenaarf36d3692005-03-15 22:48:14 +00005860 gdk_gc_set_foreground(gui.text_gc, gui.spcolor);
5861 for (i = FILL_X(col); i < FILL_X(col + cells); ++i)
5862 {
5863 offset = val[i % 8];
5864 gdk_draw_point(gui.drawarea->window, gui.text_gc, i, y - offset);
5865 }
5866 gdk_gc_set_foreground(gui.text_gc, gui.fgcolor);
Bram Moolenaar98921892016-02-23 17:14:37 +01005867#endif
Bram Moolenaarf36d3692005-03-15 22:48:14 +00005868 }
5869
Bram Moolenaar30613902019-12-01 22:11:18 +01005870 // Draw a strikethrough line
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02005871 if (flags & DRAW_STRIKE)
5872 {
5873#if GTK_CHECK_VERSION(3,0,0)
5874 cairo_set_line_width(cr, 1.0);
5875 cairo_set_line_cap(cr, CAIRO_LINE_CAP_BUTT);
5876 cairo_set_source_rgba(cr,
5877 gui.spcolor->red, gui.spcolor->green, gui.spcolor->blue,
5878 gui.spcolor->alpha);
5879 cairo_move_to(cr, FILL_X(col), y + 1 - gui.char_height/2 + 0.5);
5880 cairo_line_to(cr, FILL_X(col + cells), y + 1 - gui.char_height/2 + 0.5);
5881 cairo_stroke(cr);
5882#else
5883 gdk_gc_set_foreground(gui.text_gc, gui.spcolor);
5884 gdk_draw_line(gui.drawarea->window, gui.text_gc,
5885 FILL_X(col), y + 1 - gui.char_height/2,
5886 FILL_X(col + cells), y + 1 - gui.char_height/2);
5887 gdk_gc_set_foreground(gui.text_gc, gui.fgcolor);
5888#endif
5889 }
5890
Bram Moolenaar30613902019-12-01 22:11:18 +01005891 // Underline: draw a line at the bottom of the character cell.
Bram Moolenaarf36d3692005-03-15 22:48:14 +00005892 if (flags & DRAW_UNDERL)
5893 {
Bram Moolenaar30613902019-12-01 22:11:18 +01005894 // When p_linespace is 0, overwrite the bottom row of pixels.
5895 // Otherwise put the line just below the character.
Bram Moolenaarf36d3692005-03-15 22:48:14 +00005896 if (p_linespace > 1)
5897 y -= p_linespace - 1;
Bram Moolenaar98921892016-02-23 17:14:37 +01005898#if GTK_CHECK_VERSION(3,0,0)
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02005899 cairo_set_line_width(cr, 1.0);
5900 cairo_set_line_cap(cr, CAIRO_LINE_CAP_BUTT);
5901 cairo_set_source_rgba(cr,
5902 gui.fgcolor->red, gui.fgcolor->green, gui.fgcolor->blue,
5903 gui.fgcolor->alpha);
5904 cairo_move_to(cr, FILL_X(col), y + 0.5);
5905 cairo_line_to(cr, FILL_X(col + cells), y + 0.5);
5906 cairo_stroke(cr);
Bram Moolenaar98921892016-02-23 17:14:37 +01005907#else
Bram Moolenaarf36d3692005-03-15 22:48:14 +00005908 gdk_draw_line(gui.drawarea->window, gui.text_gc,
5909 FILL_X(col), y,
5910 FILL_X(col + cells) - 1, y);
Bram Moolenaar98921892016-02-23 17:14:37 +01005911#endif
Bram Moolenaarf36d3692005-03-15 22:48:14 +00005912 }
5913}
5914
Bram Moolenaar071d4272004-06-13 20:20:40 +00005915 int
5916gui_gtk2_draw_string(int row, int col, char_u *s, int len, int flags)
5917{
Dusan Popovic4eeedc02021-10-16 20:52:05 +01005918 char_u *conv_buf = NULL; // result of UTF-8 conversion
5919 char_u *new_conv_buf;
5920 int convlen;
5921 char_u *sp, *bp;
5922 int plen;
5923 int len_sum; // return value needs to add up since we are
5924 // printing substrings
5925 int byte_sum; // byte position in string
5926 char_u *cs; // current *s pointer
5927 int needs_pango; // look ahead, 0=ascii 1=unicode/ligatures
Bram Moolenaar9d4b8ca2021-10-17 11:33:47 +01005928 int should_need_pango = FALSE;
Dusan Popovic4eeedc02021-10-16 20:52:05 +01005929 int slen;
5930 int is_ligature;
Dusan Popovic4eeedc02021-10-16 20:52:05 +01005931 int is_utf8;
5932 char_u backup_ch;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005933
Bram Moolenaar98921892016-02-23 17:14:37 +01005934 if (gui.text_context == NULL || gtk_widget_get_window(gui.drawarea) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005935 return len;
5936
5937 if (output_conv.vc_type != CONV_NONE)
5938 {
5939 /*
5940 * Convert characters from 'encoding' to 'termencoding', which is set
5941 * to UTF-8 by gui_mch_init(). did_set_string_option() in option.c
5942 * prohibits changing this to something else than UTF-8 if the GUI is
5943 * in use.
5944 */
5945 convlen = len;
5946 conv_buf = string_convert(&output_conv, s, &convlen);
5947 g_return_val_if_fail(conv_buf != NULL, len);
5948
Bram Moolenaar30613902019-12-01 22:11:18 +01005949 // Correct for differences in char width: some chars are
5950 // double-wide in 'encoding' but single-wide in utf-8. Add a space to
5951 // compensate for that.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005952 for (sp = s, bp = conv_buf; sp < s + len && bp < conv_buf + convlen; )
5953 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005954 plen = utf_ptr2len(bp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005955 if ((*mb_ptr2cells)(sp) == 2 && utf_ptr2cells(bp) == 1)
5956 {
5957 new_conv_buf = alloc(convlen + 2);
5958 if (new_conv_buf == NULL)
5959 return len;
5960 plen += bp - conv_buf;
5961 mch_memmove(new_conv_buf, conv_buf, plen);
5962 new_conv_buf[plen] = ' ';
5963 mch_memmove(new_conv_buf + plen + 1, conv_buf + plen,
5964 convlen - plen + 1);
5965 vim_free(conv_buf);
5966 conv_buf = new_conv_buf;
5967 ++convlen;
5968 bp = conv_buf + plen;
5969 plen = 1;
5970 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005971 sp += (*mb_ptr2len)(sp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005972 bp += plen;
5973 }
5974 s = conv_buf;
5975 len = convlen;
5976 }
5977
5978 /*
Dusan Popovic4eeedc02021-10-16 20:52:05 +01005979 * Ligature support and complex utf-8 char optimization:
5980 * String received to output to screen can print using pre-cached glyphs
5981 * (fast) or Pango (slow). Ligatures and multibype utf-8 must use Pango.
5982 * Since we receive mixed content string, split it into logical segments
dundargocc57b5bc2022-11-02 13:30:51 +00005983 * that are guaranteed to go through glyphs as much as possible. Since
Dusan Popovic4eeedc02021-10-16 20:52:05 +01005984 * single ligature char prints as ascii, print it that way.
5985 */
5986 len_sum = 0; // return value needs to add up since we are printing
5987 // substrings
5988 byte_sum = 0;
5989 cs = s;
Dusan Popovic3c19b502021-11-20 22:03:30 +00005990 // First char decides starting needs_pango mode, 0=ascii 1=utf8/ligatures.
5991 // Even if it is ligature char, two chars or more make ligature.
dundargocc57b5bc2022-11-02 13:30:51 +00005992 // Ascii followed by utf8 is also going through pango.
Dusan Popovic3c19b502021-11-20 22:03:30 +00005993 is_utf8 = (*cs & 0x80);
5994 is_ligature = gui.ligatures_map[*cs] && (len > 1);
5995 if (is_ligature)
5996 is_ligature = gui.ligatures_map[*(cs + 1)];
5997 if (!is_utf8 && len > 1)
5998 is_utf8 = (*(cs + 1) & 0x80) != 0;
5999 needs_pango = is_utf8 || is_ligature;
Dusan Popovic4eeedc02021-10-16 20:52:05 +01006000
6001 // split string into ascii and non-ascii (ligatures + utf-8) substrings,
6002 // print glyphs or use Pango
6003 while (cs < s + len)
6004 {
6005 slen = 0;
6006 while (slen < (len - byte_sum))
6007 {
6008 is_ligature = gui.ligatures_map[*(cs + slen)];
6009 // look ahead, single ligature char between ascii is ascii
6010 if (is_ligature && !needs_pango)
6011 {
6012 if ((slen + 1) < (len - byte_sum))
Dusan Popovic3c19b502021-11-20 22:03:30 +00006013 is_ligature = gui.ligatures_map[*(cs + slen + 1)];
Dusan Popovic4eeedc02021-10-16 20:52:05 +01006014 else
Dusan Popovic4eeedc02021-10-16 20:52:05 +01006015 is_ligature = 0;
Dusan Popovic4eeedc02021-10-16 20:52:05 +01006016 }
6017 is_utf8 = *(cs + slen) & 0x80;
Dusan Popovic3c19b502021-11-20 22:03:30 +00006018 // ascii followed by utf8 could be combining
dundargocc57b5bc2022-11-02 13:30:51 +00006019 // if so send it through pango
Dusan Popovic3c19b502021-11-20 22:03:30 +00006020 if ((!is_utf8) && ((slen + 1) < (len - byte_sum)))
6021 is_utf8 = (*(cs + slen + 1) & 0x80);
Dusan Popovic4eeedc02021-10-16 20:52:05 +01006022 should_need_pango = (is_ligature || is_utf8);
6023 if (needs_pango != should_need_pango) // mode switch
6024 break;
6025 if (needs_pango)
6026 {
6027 if (is_ligature)
6028 {
6029 slen++; // ligature char by char
6030 }
Bram Moolenaar2c236702021-11-21 11:15:49 +00006031 else // is_utf8
Dusan Popovic4eeedc02021-10-16 20:52:05 +01006032 {
6033 if ((*(cs + slen) & 0xC0) == 0x80)
6034 {
6035 // a continuation, find next 0xC0 != 0x80 but don't
6036 // include it
6037 while ((slen < (len - byte_sum))
6038 && ((*(cs + slen) & 0xC0) == 0x80))
6039 {
6040 slen++;
6041 }
6042 }
6043 else if ((*(cs + slen) & 0xE0) == 0xC0)
6044 {
6045 // + one byte utf8
6046 slen++;
6047 }
6048 else if ((*(cs + slen) & 0xF0) == 0xE0)
6049 {
6050 // + two bytes utf8
6051 slen += 2;
6052 }
6053 else if ((*(cs + slen) & 0xF8) == 0xF0)
6054 {
6055 // + three bytes utf8
6056 slen += 3;
6057 }
6058 else
6059 {
6060 // this should not happen, try moving forward, Pango
6061 // will catch it
6062 slen++;
6063 }
6064 }
6065 }
6066 else
6067 {
6068 slen++; // ascii
6069 }
6070 }
Bram Moolenaar9d4b8ca2021-10-17 11:33:47 +01006071
Bram Moolenaard68a0042021-10-20 23:08:11 +01006072 if (slen < len)
6073 {
6074 // temporarily zero terminate substring, print, restore char, wrap
6075 backup_ch = *(cs + slen);
6076 *(cs + slen) = NUL;
6077 }
Dusan Popovic4eeedc02021-10-16 20:52:05 +01006078 len_sum += gui_gtk2_draw_string_ext(row, col + len_sum,
6079 cs, slen, flags, needs_pango);
Bram Moolenaard68a0042021-10-20 23:08:11 +01006080 if (slen < len)
6081 *(cs + slen) = backup_ch;
Dusan Popovic4eeedc02021-10-16 20:52:05 +01006082 cs += slen;
6083 byte_sum += slen;
6084 needs_pango = should_need_pango;
6085 }
6086 vim_free(conv_buf);
6087 return len_sum;
6088}
6089
6090 int
6091gui_gtk2_draw_string_ext(
6092 int row,
6093 int col,
6094 char_u *s,
6095 int len,
6096 int flags,
6097 int force_pango)
6098{
6099 GdkRectangle area; // area for clip mask
6100 PangoGlyphString *glyphs; // glyphs of current item
6101 int column_offset = 0; // column offset in cells
6102 int i;
6103#if GTK_CHECK_VERSION(3,0,0)
6104 cairo_t *cr;
6105#endif
6106
6107 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006108 * Restrict all drawing to the current screen line in order to prevent
6109 * fuzzy font lookups from messing up the screen.
6110 */
6111 area.x = gui.border_offset;
6112 area.y = FILL_Y(row);
6113 area.width = gui.num_cols * gui.char_width;
6114 area.height = gui.char_height;
6115
Bram Moolenaar98921892016-02-23 17:14:37 +01006116#if GTK_CHECK_VERSION(3,0,0)
6117 cr = cairo_create(gui.surface);
6118 cairo_rectangle(cr, area.x, area.y, area.width, area.height);
6119 cairo_clip(cr);
6120#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006121 gdk_gc_set_clip_origin(gui.text_gc, 0, 0);
6122 gdk_gc_set_clip_rectangle(gui.text_gc, &area);
Bram Moolenaar98921892016-02-23 17:14:37 +01006123#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006124
6125 glyphs = pango_glyph_string_new();
6126
6127 /*
6128 * Optimization hack: If possible, skip the itemize and shaping process
6129 * for pure ASCII strings. This optimization is particularly effective
6130 * because Vim draws space characters to clear parts of the screen.
6131 */
6132 if (!(flags & DRAW_ITALIC)
6133 && !((flags & DRAW_BOLD) && gui.font_can_bold)
Dusan Popovic4eeedc02021-10-16 20:52:05 +01006134 && gui.ascii_glyphs != NULL
6135 && !force_pango)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006136 {
6137 char_u *p;
6138
6139 for (p = s; p < s + len; ++p)
6140 if (*p & 0x80)
6141 goto not_ascii;
6142
6143 pango_glyph_string_set_size(glyphs, len);
6144
6145 for (i = 0; i < len; ++i)
6146 {
Bram Moolenaar16350cb2016-08-14 20:27:34 +02006147 glyphs->glyphs[i] = gui.ascii_glyphs->glyphs[2 * s[i]];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006148 glyphs->log_clusters[i] = i;
6149 }
6150
Bram Moolenaar98921892016-02-23 17:14:37 +01006151#if GTK_CHECK_VERSION(3,0,0)
6152 draw_glyph_string(row, col, len, flags, gui.ascii_font, glyphs, cr);
6153#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006154 draw_glyph_string(row, col, len, flags, gui.ascii_font, glyphs);
Bram Moolenaar98921892016-02-23 17:14:37 +01006155#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006156
6157 column_offset = len;
6158 }
6159 else
6160not_ascii:
6161 {
6162 PangoAttrList *attr_list;
6163 GList *item_list;
6164 int cluster_width;
6165 int last_glyph_rbearing;
Bram Moolenaar30613902019-12-01 22:11:18 +01006166 int cells = 0; // cells occupied by current cluster
Bram Moolenaar071d4272004-06-13 20:20:40 +00006167
Bram Moolenaar30613902019-12-01 22:11:18 +01006168 // Safety check: pango crashes when invoked with invalid utf-8
6169 // characters.
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006170 if (!utf_valid_string(s, s + len))
6171 {
6172 column_offset = len;
6173 goto skipitall;
6174 }
6175
Bram Moolenaar30613902019-12-01 22:11:18 +01006176 // original width of the current cluster
Bram Moolenaar071d4272004-06-13 20:20:40 +00006177 cluster_width = PANGO_SCALE * gui.char_width;
6178
Bram Moolenaar30613902019-12-01 22:11:18 +01006179 // right bearing of the last non-composing glyph
Bram Moolenaar071d4272004-06-13 20:20:40 +00006180 last_glyph_rbearing = PANGO_SCALE * gui.char_width;
6181
6182 attr_list = pango_attr_list_new();
6183
Bram Moolenaar30613902019-12-01 22:11:18 +01006184 // If 'guifontwide' is set then use that for double-width characters.
6185 // Otherwise just go with 'guifont' and let Pango do its thing.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006186 if (gui.wide_font != NULL)
6187 apply_wide_font_attr(s, len, attr_list);
6188
6189 if ((flags & DRAW_BOLD) && gui.font_can_bold)
6190 INSERT_PANGO_ATTR(pango_attr_weight_new(PANGO_WEIGHT_BOLD),
6191 attr_list, 0, len);
6192 if (flags & DRAW_ITALIC)
6193 INSERT_PANGO_ATTR(pango_attr_style_new(PANGO_STYLE_ITALIC),
6194 attr_list, 0, len);
6195 /*
6196 * Break the text into segments with consistent directional level
6197 * and shaping engine. Pure Latin text needs only a single segment,
6198 * so there's no need to worry about the loop's efficiency. Better
6199 * try to optimize elsewhere, e.g. reducing exposes and stuff :)
6200 */
6201 item_list = pango_itemize(gui.text_context,
6202 (const char *)s, 0, len, attr_list, NULL);
6203
6204 while (item_list != NULL)
6205 {
6206 PangoItem *item;
Bram Moolenaar30613902019-12-01 22:11:18 +01006207 int item_cells = 0; // item length in cells
Bram Moolenaar071d4272004-06-13 20:20:40 +00006208
6209 item = (PangoItem *)item_list->data;
6210 item_list = g_list_delete_link(item_list, item_list);
6211 /*
6212 * Increment the bidirectional embedding level by 1 if it is not
6213 * even. An odd number means the output will be RTL, but we don't
6214 * want that since Vim handles right-to-left text on its own. It
6215 * would probably be sufficient to just set level = 0, but you can
6216 * never know :)
6217 *
6218 * Unfortunately we can't take advantage of Pango's ability to
6219 * render both LTR and RTL at the same time. In order to support
6220 * that, Vim's main screen engine would have to make use of Pango
6221 * functionality.
6222 */
6223 item->analysis.level = (item->analysis.level + 1) & (~1U);
6224
Bram Moolenaar30613902019-12-01 22:11:18 +01006225 // HACK: Overrule the shape engine, we don't want shaping to be
6226 // done, because drawing the cursor would change the display.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006227 item->analysis.shape_engine = default_shape_engine;
6228
Bram Moolenaar3cbe0c02015-09-08 20:00:22 +02006229#ifdef HAVE_PANGO_SHAPE_FULL
Bram Moolenaar7e2ec002015-09-08 16:31:06 +02006230 pango_shape_full((const char *)s + item->offset, item->length,
6231 (const char *)s, len, &item->analysis, glyphs);
Bram Moolenaar3cbe0c02015-09-08 20:00:22 +02006232#else
6233 pango_shape((const char *)s + item->offset, item->length,
6234 &item->analysis, glyphs);
6235#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006236 /*
6237 * Fixed-width hack: iterate over the array and assign a fixed
6238 * width to each glyph, thus overriding the choice made by the
6239 * shaping engine. We use utf_char2cells() to determine the
6240 * number of cells needed.
6241 *
6242 * Also perform all kind of dark magic to get composing
6243 * characters right (and pretty too of course).
6244 */
6245 for (i = 0; i < glyphs->num_glyphs; ++i)
6246 {
6247 PangoGlyphInfo *glyph;
6248
6249 glyph = &glyphs->glyphs[i];
6250
6251 if (glyph->attr.is_cluster_start)
6252 {
6253 int cellcount;
6254
6255 cellcount = count_cluster_cells(
6256 s, item, glyphs, i, &cluster_width,
6257 (item_list != NULL) ? &last_glyph_rbearing : NULL);
6258
6259 if (cellcount > 0)
6260 {
6261 int width;
6262
6263 width = cellcount * gui.char_width * PANGO_SCALE;
6264 glyph->geometry.x_offset +=
6265 MAX(0, width - cluster_width) / 2;
6266 glyph->geometry.width = width;
6267 }
6268 else
6269 {
Bram Moolenaar30613902019-12-01 22:11:18 +01006270 // If there are only combining characters in the
6271 // cluster, we cannot just change the width of the
6272 // previous glyph since there is none. Therefore
6273 // some guesswork is needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006274 setup_zero_width_cluster(item, glyph, cells,
6275 cluster_width,
6276 last_glyph_rbearing);
6277 }
6278
6279 item_cells += cellcount;
6280 cells = cellcount;
6281 }
6282 else if (i > 0)
6283 {
6284 int width;
6285
Bram Moolenaar30613902019-12-01 22:11:18 +01006286 // There is a previous glyph, so we deal with combining
6287 // characters the canonical way.
6288 // In some circumstances Pango uses a positive x_offset,
6289 // then use the width of the previous glyph for this one
6290 // and set the previous width to zero.
6291 // Otherwise we get a negative x_offset, Pango has already
6292 // positioned the combining char, keep the widths as they
6293 // are.
6294 // For both adjust the x_offset to position the glyph in
6295 // the middle.
Bram Moolenaar706e84b2010-08-07 15:46:45 +02006296 if (glyph->geometry.x_offset >= 0)
Bram Moolenaar96118f32010-08-08 14:40:37 +02006297 {
6298 glyphs->glyphs[i].geometry.width =
6299 glyphs->glyphs[i - 1].geometry.width;
Bram Moolenaar706e84b2010-08-07 15:46:45 +02006300 glyphs->glyphs[i - 1].geometry.width = 0;
Bram Moolenaar96118f32010-08-08 14:40:37 +02006301 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006302 width = cells * gui.char_width * PANGO_SCALE;
6303 glyph->geometry.x_offset +=
6304 MAX(0, width - cluster_width) / 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006305 }
Bram Moolenaar30613902019-12-01 22:11:18 +01006306 else // i == 0 "cannot happen"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006307 {
6308 glyph->geometry.width = 0;
6309 }
6310 }
6311
Bram Moolenaar30613902019-12-01 22:11:18 +01006312 //// Aaaaand action! **
Bram Moolenaar98921892016-02-23 17:14:37 +01006313#if GTK_CHECK_VERSION(3,0,0)
6314 draw_glyph_string(row, col + column_offset, item_cells,
6315 flags, item->analysis.font, glyphs,
6316 cr);
6317#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006318 draw_glyph_string(row, col + column_offset, item_cells,
6319 flags, item->analysis.font, glyphs);
Bram Moolenaar98921892016-02-23 17:14:37 +01006320#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006321
6322 pango_item_free(item);
6323
6324 column_offset += item_cells;
6325 }
6326
6327 pango_attr_list_unref(attr_list);
6328 }
6329
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006330skipitall:
Bram Moolenaar30613902019-12-01 22:11:18 +01006331 // Draw underline and undercurl.
Bram Moolenaar98921892016-02-23 17:14:37 +01006332#if GTK_CHECK_VERSION(3,0,0)
6333 draw_under(flags, row, col, column_offset, cr);
6334#else
Bram Moolenaarf36d3692005-03-15 22:48:14 +00006335 draw_under(flags, row, col, column_offset);
Bram Moolenaar98921892016-02-23 17:14:37 +01006336#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006337
6338 pango_glyph_string_free(glyphs);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006339
Bram Moolenaar98921892016-02-23 17:14:37 +01006340#if GTK_CHECK_VERSION(3,0,0)
6341 cairo_destroy(cr);
presuku9459b8d2021-11-16 20:03:56 +00006342 gtk_widget_queue_draw_area(gui.drawarea, area.x, area.y,
6343 area.width, area.height);
Bram Moolenaar98921892016-02-23 17:14:37 +01006344#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006345 gdk_gc_set_clip_rectangle(gui.text_gc, NULL);
Bram Moolenaar98921892016-02-23 17:14:37 +01006346#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006347
6348 return column_offset;
6349}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006350
6351/*
6352 * Return OK if the key with the termcap name "name" is supported.
6353 */
6354 int
6355gui_mch_haskey(char_u *name)
6356{
6357 int i;
6358
6359 for (i = 0; special_keys[i].key_sym != 0; i++)
6360 if (name[0] == special_keys[i].code0
6361 && name[1] == special_keys[i].code1)
6362 return OK;
6363 return FAIL;
6364}
6365
rbtnnb850c392022-10-01 15:47:04 +01006366#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006367/*
6368 * Return the text window-id and display. Only required for X-based GUI's
6369 */
6370 int
6371gui_get_x11_windis(Window *win, Display **dis)
6372{
lilydjwg6e0a18f2024-01-29 20:54:28 +01006373 Display * dpy = gui_mch_get_display();
6374 if (dpy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006375 {
lilydjwg6e0a18f2024-01-29 20:54:28 +01006376 *dis = dpy;
Bram Moolenaar98921892016-02-23 17:14:37 +01006377 *win = GDK_WINDOW_XID(gtk_widget_get_window(gui.mainwin));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006378 return OK;
6379 }
6380
6381 *dis = NULL;
6382 *win = 0;
6383 return FAIL;
6384}
6385#endif
6386
Bram Moolenaar071d4272004-06-13 20:20:40 +00006387 Display *
6388gui_mch_get_display(void)
6389{
lilydjwg6e0a18f2024-01-29 20:54:28 +01006390 if (gui.mainwin != NULL && gtk_widget_get_window(gui.mainwin) != NULL
6391#if GTK_CHECK_VERSION(3,0,0)
6392 && GDK_IS_X11_DISPLAY(gtk_widget_get_display(gui.mainwin))
6393#endif
6394 )
Bram Moolenaar98921892016-02-23 17:14:37 +01006395 return GDK_WINDOW_XDISPLAY(gtk_widget_get_window(gui.mainwin));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006396 else
6397 return NULL;
6398}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006399
6400 void
6401gui_mch_beep(void)
6402{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006403 GdkDisplay *display;
6404
Bram Moolenaar98921892016-02-23 17:14:37 +01006405 if (gui.mainwin != NULL && gtk_widget_get_realized(gui.mainwin))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006406 display = gtk_widget_get_display(gui.mainwin);
6407 else
6408 display = gdk_display_get_default();
6409
6410 if (display != NULL)
6411 gdk_display_beep(display);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006412}
6413
6414 void
6415gui_mch_flash(int msec)
6416{
Bram Moolenaar98921892016-02-23 17:14:37 +01006417#if GTK_CHECK_VERSION(3,0,0)
Bram Moolenaar30613902019-12-01 22:11:18 +01006418 // TODO Replace GdkGC with Cairo
Bram Moolenaar98921892016-02-23 17:14:37 +01006419 (void)msec;
6420#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006421 GdkGCValues values;
6422 GdkGC *invert_gc;
6423
6424 if (gui.drawarea->window == NULL)
6425 return;
6426
6427 values.foreground.pixel = gui.norm_pixel ^ gui.back_pixel;
6428 values.background.pixel = gui.norm_pixel ^ gui.back_pixel;
6429 values.function = GDK_XOR;
6430 invert_gc = gdk_gc_new_with_values(gui.drawarea->window,
6431 &values,
6432 GDK_GC_FOREGROUND |
6433 GDK_GC_BACKGROUND |
6434 GDK_GC_FUNCTION);
6435 gdk_gc_set_exposures(invert_gc,
6436 gui.visibility != GDK_VISIBILITY_UNOBSCURED);
6437 /*
6438 * Do a visual beep by changing back and forth in some undetermined way,
6439 * the foreground and background colors. This is due to the fact that
6440 * there can't be really any prediction about the effects of XOR on
6441 * arbitrary X11 servers. However this seems to be enough for what we
6442 * intend it to do.
6443 */
6444 gdk_draw_rectangle(gui.drawarea->window, invert_gc,
6445 TRUE,
6446 0, 0,
6447 FILL_X((int)Columns) + gui.border_offset,
6448 FILL_Y((int)Rows) + gui.border_offset);
6449
6450 gui_mch_flush();
Bram Moolenaar30613902019-12-01 22:11:18 +01006451 ui_delay((long)msec, TRUE); // wait so many msec
Bram Moolenaar071d4272004-06-13 20:20:40 +00006452
6453 gdk_draw_rectangle(gui.drawarea->window, invert_gc,
6454 TRUE,
6455 0, 0,
6456 FILL_X((int)Columns) + gui.border_offset,
6457 FILL_Y((int)Rows) + gui.border_offset);
6458
6459 gdk_gc_destroy(invert_gc);
Bram Moolenaar98921892016-02-23 17:14:37 +01006460#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006461}
6462
6463/*
6464 * Invert a rectangle from row r, column c, for nr rows and nc columns.
6465 */
6466 void
6467gui_mch_invert_rectangle(int r, int c, int nr, int nc)
6468{
Bram Moolenaar98921892016-02-23 17:14:37 +01006469#if GTK_CHECK_VERSION(3,0,0)
Bram Moolenaar4fc563b2016-03-12 12:40:58 +01006470 const GdkRectangle rect = {
6471 FILL_X(c), FILL_Y(r), nc * gui.char_width, nr * gui.char_height
6472 };
6473 cairo_t * const cr = cairo_create(gui.surface);
6474
Your Name810cb5a2022-09-13 11:25:54 +01006475 cairo_set_source_rgba(cr, 1.0, 1.0, 1.0, 1.0);
Bram Moolenaar4fc563b2016-03-12 12:40:58 +01006476# if CAIRO_VERSION >= CAIRO_VERSION_ENCODE(1,9,2)
6477 cairo_set_operator(cr, CAIRO_OPERATOR_DIFFERENCE);
6478# else
Bram Moolenaar30613902019-12-01 22:11:18 +01006479 // Give an implementation for older cairo versions if necessary.
Bram Moolenaar4fc563b2016-03-12 12:40:58 +01006480# endif
presuku9459b8d2021-11-16 20:03:56 +00006481 cairo_rectangle(cr, rect.x, rect.y, rect.width, rect.height);
Bram Moolenaar4fc563b2016-03-12 12:40:58 +01006482 cairo_fill(cr);
6483
6484 cairo_destroy(cr);
6485
presuku9459b8d2021-11-16 20:03:56 +00006486 gtk_widget_queue_draw_area(gui.drawarea, rect.x, rect.y,
6487 rect.width, rect.height);
Bram Moolenaar98921892016-02-23 17:14:37 +01006488#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006489 GdkGCValues values;
6490 GdkGC *invert_gc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006491
6492 if (gui.drawarea->window == NULL)
6493 return;
6494
Your Name810cb5a2022-09-13 11:25:54 +01006495 values.function = GDK_INVERT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006496 invert_gc = gdk_gc_new_with_values(gui.drawarea->window,
6497 &values,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006498 GDK_GC_FUNCTION);
Bram Moolenaar89d40322006-08-29 15:30:07 +00006499 gdk_gc_set_exposures(invert_gc, gui.visibility !=
6500 GDK_VISIBILITY_UNOBSCURED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006501 gdk_draw_rectangle(gui.drawarea->window, invert_gc,
6502 TRUE,
6503 FILL_X(c), FILL_Y(r),
6504 (nc) * gui.char_width, (nr) * gui.char_height);
6505 gdk_gc_destroy(invert_gc);
Bram Moolenaar98921892016-02-23 17:14:37 +01006506#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006507}
6508
6509/*
6510 * Iconify the GUI window.
6511 */
6512 void
6513gui_mch_iconify(void)
6514{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006515 gtk_window_iconify(GTK_WINDOW(gui.mainwin));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006516}
6517
6518#if defined(FEAT_EVAL) || defined(PROTO)
6519/*
6520 * Bring the Vim window to the foreground.
6521 */
6522 void
6523gui_mch_set_foreground(void)
6524{
Bram Moolenaar023930d2022-12-01 19:40:55 +00006525 // Just calling gtk_window_present() used to work in the past, but now this
6526 // sequence appears to be needed:
6527 // - Show the window on top of others.
6528 // - Present the window (also shows it above others).
6529 // - Do not the window on top of others (otherwise it would be stuck there).
6530 gtk_window_set_keep_above(GTK_WINDOW(gui.mainwin), TRUE);
6531 gui_may_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006532 gtk_window_present(GTK_WINDOW(gui.mainwin));
Bram Moolenaar023930d2022-12-01 19:40:55 +00006533 gui_may_flush();
6534 gtk_window_set_keep_above(GTK_WINDOW(gui.mainwin), FALSE);
6535 gui_may_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006536}
6537#endif
6538
6539/*
6540 * Draw a cursor without focus.
6541 */
6542 void
6543gui_mch_draw_hollow_cursor(guicolor_T color)
6544{
6545 int i = 1;
Bram Moolenaar98921892016-02-23 17:14:37 +01006546#if GTK_CHECK_VERSION(3,0,0)
6547 cairo_t *cr;
6548#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006549
Bram Moolenaar98921892016-02-23 17:14:37 +01006550 if (gtk_widget_get_window(gui.drawarea) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006551 return;
6552
Bram Moolenaar98921892016-02-23 17:14:37 +01006553#if GTK_CHECK_VERSION(3,0,0)
6554 cr = cairo_create(gui.surface);
6555#endif
6556
Bram Moolenaar071d4272004-06-13 20:20:40 +00006557 gui_mch_set_fg_color(color);
6558
Bram Moolenaar98921892016-02-23 17:14:37 +01006559#if GTK_CHECK_VERSION(3,0,0)
Bram Moolenaar36edf062016-07-21 22:10:12 +02006560 cairo_set_source_rgba(cr,
6561 gui.fgcolor->red, gui.fgcolor->green, gui.fgcolor->blue,
6562 gui.fgcolor->alpha);
Bram Moolenaar98921892016-02-23 17:14:37 +01006563#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006564 gdk_gc_set_foreground(gui.text_gc, gui.fgcolor);
Bram Moolenaar98921892016-02-23 17:14:37 +01006565#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006566 if (mb_lefthalve(gui.row, gui.col))
6567 i = 2;
Bram Moolenaar98921892016-02-23 17:14:37 +01006568#if GTK_CHECK_VERSION(3,0,0)
6569 cairo_set_line_width(cr, 1.0);
6570 cairo_set_line_cap(cr, CAIRO_LINE_CAP_BUTT);
6571 cairo_rectangle(cr,
6572 FILL_X(gui.col) + 0.5, FILL_Y(gui.row) + 0.5,
6573 i * gui.char_width - 1, gui.char_height - 1);
6574 cairo_stroke(cr);
6575 cairo_destroy(cr);
6576#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006577 gdk_draw_rectangle(gui.drawarea->window, gui.text_gc,
6578 FALSE,
6579 FILL_X(gui.col), FILL_Y(gui.row),
6580 i * gui.char_width - 1, gui.char_height - 1);
Bram Moolenaar98921892016-02-23 17:14:37 +01006581#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006582}
6583
6584/*
6585 * Draw part of a cursor, "w" pixels wide, and "h" pixels high, using
6586 * color "color".
6587 */
6588 void
6589gui_mch_draw_part_cursor(int w, int h, guicolor_T color)
6590{
Bram Moolenaar98921892016-02-23 17:14:37 +01006591 if (gtk_widget_get_window(gui.drawarea) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006592 return;
6593
6594 gui_mch_set_fg_color(color);
6595
Bram Moolenaar98921892016-02-23 17:14:37 +01006596#if GTK_CHECK_VERSION(3,0,0)
6597 {
6598 cairo_t *cr;
6599
6600 cr = cairo_create(gui.surface);
Bram Moolenaar36edf062016-07-21 22:10:12 +02006601 cairo_set_source_rgba(cr,
6602 gui.fgcolor->red, gui.fgcolor->green, gui.fgcolor->blue,
6603 gui.fgcolor->alpha);
Bram Moolenaar98921892016-02-23 17:14:37 +01006604 cairo_rectangle(cr,
6605# ifdef FEAT_RIGHTLEFT
Bram Moolenaar30613902019-12-01 22:11:18 +01006606 // vertical line should be on the right of current point
Bram Moolenaar98921892016-02-23 17:14:37 +01006607 CURSOR_BAR_RIGHT ? FILL_X(gui.col + 1) - w :
6608# endif
6609 FILL_X(gui.col), FILL_Y(gui.row) + gui.char_height - h,
6610 w, h);
6611 cairo_fill(cr);
6612 cairo_destroy(cr);
6613 }
Bram Moolenaar30613902019-12-01 22:11:18 +01006614#else // !GTK_CHECK_VERSION(3,0,0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006615 gdk_gc_set_foreground(gui.text_gc, gui.fgcolor);
6616 gdk_draw_rectangle(gui.drawarea->window, gui.text_gc,
6617 TRUE,
Bram Moolenaar98921892016-02-23 17:14:37 +01006618# ifdef FEAT_RIGHTLEFT
Bram Moolenaar30613902019-12-01 22:11:18 +01006619 // vertical line should be on the right of current point
Bram Moolenaar071d4272004-06-13 20:20:40 +00006620 CURSOR_BAR_RIGHT ? FILL_X(gui.col + 1) - w :
Bram Moolenaar98921892016-02-23 17:14:37 +01006621# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006622 FILL_X(gui.col),
6623 FILL_Y(gui.row) + gui.char_height - h,
6624 w, h);
Bram Moolenaar30613902019-12-01 22:11:18 +01006625#endif // !GTK_CHECK_VERSION(3,0,0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006626}
6627
6628
6629/*
6630 * Catch up with any queued X11 events. This may put keyboard input into the
6631 * input buffer, call resize call-backs, trigger timers etc. If there is
6632 * nothing in the X11 event queue (& no timers pending), then we return
6633 * immediately.
6634 */
6635 void
6636gui_mch_update(void)
6637{
Bram Moolenaara3f41662010-07-11 19:01:06 +02006638 while (g_main_context_pending(NULL) && !vim_is_input_buf_full())
6639 g_main_context_iteration(NULL, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006640}
6641
Bram Moolenaar4ab79682017-08-27 14:50:47 +02006642 static timeout_cb_type
Bram Moolenaar071d4272004-06-13 20:20:40 +00006643input_timer_cb(gpointer data)
6644{
6645 int *timed_out = (int *) data;
6646
Bram Moolenaar30613902019-12-01 22:11:18 +01006647 // Just inform the caller about the occurrence of it
Bram Moolenaar071d4272004-06-13 20:20:40 +00006648 *timed_out = TRUE;
6649
Bram Moolenaar30613902019-12-01 22:11:18 +01006650 return FALSE; // don't happen again
Bram Moolenaar071d4272004-06-13 20:20:40 +00006651}
6652
Bram Moolenaar4ab79682017-08-27 14:50:47 +02006653#ifdef FEAT_JOB_CHANNEL
6654 static timeout_cb_type
6655channel_poll_cb(gpointer data UNUSED)
6656{
Bram Moolenaar30613902019-12-01 22:11:18 +01006657 // Using an event handler for a channel that may be disconnected does
6658 // not work, it hangs. Instead poll for messages.
Bram Moolenaar4ab79682017-08-27 14:50:47 +02006659 channel_handle_events(TRUE);
6660 parse_queued_messages();
6661
Bram Moolenaar30613902019-12-01 22:11:18 +01006662 return TRUE; // repeat
Bram Moolenaar4ab79682017-08-27 14:50:47 +02006663}
6664#endif
6665
Bram Moolenaar071d4272004-06-13 20:20:40 +00006666/*
6667 * GUI input routine called by gui_wait_for_chars(). Waits for a character
6668 * from the keyboard.
6669 * wtime == -1 Wait forever.
6670 * wtime == 0 This should never happen.
6671 * wtime > 0 Wait wtime milliseconds for a character.
6672 * Returns OK if a character was found to be available within the given time,
6673 * or FAIL otherwise.
6674 */
6675 int
6676gui_mch_wait_for_chars(long wtime)
6677{
Bram Moolenaar4231da42016-06-02 14:30:04 +02006678 int focus;
6679 guint timer;
6680 static int timed_out;
6681 int retval = FAIL;
Bram Moolenaar4ab79682017-08-27 14:50:47 +02006682#ifdef FEAT_JOB_CHANNEL
6683 guint channel_timer = 0;
6684#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006685
6686 timed_out = FALSE;
6687
Bram Moolenaar12dfc9e2019-01-28 22:32:58 +01006688 // This timeout makes sure that we will return if no characters arrived in
6689 // time. If "wtime" is zero just use one.
6690 if (wtime >= 0)
Bram Moolenaar34a58742019-02-03 15:28:28 +01006691 timer = timeout_add(wtime == 0 ? 1L : wtime,
Bram Moolenaar12dfc9e2019-01-28 22:32:58 +01006692 input_timer_cb, &timed_out);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006693 else
6694 timer = 0;
6695
Bram Moolenaar4ab79682017-08-27 14:50:47 +02006696#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar30613902019-12-01 22:11:18 +01006697 // If there is a channel with the keep_open flag we need to poll for input
6698 // on them.
Bram Moolenaar4ab79682017-08-27 14:50:47 +02006699 if (channel_any_keep_open())
6700 channel_timer = timeout_add(20, channel_poll_cb, NULL);
6701#endif
6702
Bram Moolenaar071d4272004-06-13 20:20:40 +00006703 focus = gui.in_focus;
6704
6705 do
6706 {
Bram Moolenaar30613902019-12-01 22:11:18 +01006707 // Stop or start blinking when focus changes
Bram Moolenaar071d4272004-06-13 20:20:40 +00006708 if (gui.in_focus != focus)
6709 {
6710 if (gui.in_focus)
6711 gui_mch_start_blink();
6712 else
Bram Moolenaar1dd45fb2018-01-31 21:10:01 +01006713 gui_mch_stop_blink(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006714 focus = gui.in_focus;
6715 }
6716
Bram Moolenaar93c88e02015-09-15 14:12:05 +02006717#ifdef MESSAGE_QUEUE
Bram Moolenaar4231da42016-06-02 14:30:04 +02006718# ifdef FEAT_TIMERS
6719 did_add_timer = FALSE;
6720# endif
Bram Moolenaar93c88e02015-09-15 14:12:05 +02006721 parse_queued_messages();
Bram Moolenaar4231da42016-06-02 14:30:04 +02006722# ifdef FEAT_TIMERS
6723 if (did_add_timer)
Bram Moolenaar30613902019-12-01 22:11:18 +01006724 // Need to recompute the waiting time.
Bram Moolenaar4231da42016-06-02 14:30:04 +02006725 goto theend;
6726# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006727#endif
6728
Bram Moolenaar071d4272004-06-13 20:20:40 +00006729 /*
6730 * Loop in GTK+ processing until a timeout or input occurs.
Bram Moolenaard4755bb2004-09-02 19:12:26 +00006731 * Skip this if input is available anyway (can happen in rare
6732 * situations, sort of race condition).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006733 */
Bram Moolenaard4755bb2004-09-02 19:12:26 +00006734 if (!input_available())
Bram Moolenaara3f41662010-07-11 19:01:06 +02006735 g_main_context_iteration(NULL, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006736
Bram Moolenaar30613902019-12-01 22:11:18 +01006737 // Got char, return immediately
Bram Moolenaar071d4272004-06-13 20:20:40 +00006738 if (input_available())
6739 {
Bram Moolenaar4231da42016-06-02 14:30:04 +02006740 retval = OK;
6741 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006742 }
6743 } while (wtime < 0 || !timed_out);
6744
6745 /*
6746 * Flush all eventually pending (drawing) events.
6747 */
6748 gui_mch_update();
6749
Bram Moolenaar4231da42016-06-02 14:30:04 +02006750theend:
6751 if (timer != 0 && !timed_out)
Bram Moolenaar4ab79682017-08-27 14:50:47 +02006752 timeout_remove(timer);
6753#ifdef FEAT_JOB_CHANNEL
6754 if (channel_timer != 0)
6755 timeout_remove(channel_timer);
Bram Moolenaar4231da42016-06-02 14:30:04 +02006756#endif
6757
6758 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006759}
6760
6761
Bram Moolenaar30613902019-12-01 22:11:18 +01006762/////////////////////////////////////////////////////////////////////////////
6763// Output drawing routines.
6764//
Bram Moolenaar071d4272004-06-13 20:20:40 +00006765
6766
Bram Moolenaar30613902019-12-01 22:11:18 +01006767// Flush any output to the screen
Bram Moolenaar071d4272004-06-13 20:20:40 +00006768 void
6769gui_mch_flush(void)
6770{
Bram Moolenaar98921892016-02-23 17:14:37 +01006771 if (gui.mainwin != NULL && gtk_widget_get_realized(gui.mainwin))
Bram Moolenaar92cbf622018-09-19 22:40:03 +02006772#if GTK_CHECK_VERSION(2,4,0)
Bram Moolenaarb463e8d2017-06-05 15:07:09 +02006773 gdk_display_flush(gtk_widget_get_display(gui.mainwin));
Bram Moolenaar92cbf622018-09-19 22:40:03 +02006774#else
6775 gdk_display_sync(gtk_widget_get_display(gui.mainwin));
6776#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006777}
6778
6779/*
6780 * Clear a rectangular region of the screen from text pos (row1, col1) to
6781 * (row2, col2) inclusive.
6782 */
6783 void
Bram Moolenaarfe344a92017-02-23 12:20:35 +01006784gui_mch_clear_block(int row1arg, int col1arg, int row2arg, int col2arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006785{
Bram Moolenaarfe344a92017-02-23 12:20:35 +01006786
6787 int col1 = check_col(col1arg);
6788 int col2 = check_col(col2arg);
6789 int row1 = check_row(row1arg);
6790 int row2 = check_row(row2arg);
6791
Bram Moolenaar98921892016-02-23 17:14:37 +01006792#if GTK_CHECK_VERSION(3,0,0)
6793 if (gtk_widget_get_window(gui.drawarea) == NULL)
6794 return;
6795#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006796 GdkColor color;
6797
6798 if (gui.drawarea->window == NULL)
6799 return;
6800
6801 color.pixel = gui.back_pixel;
Bram Moolenaar98921892016-02-23 17:14:37 +01006802#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006803
Bram Moolenaar98921892016-02-23 17:14:37 +01006804#if GTK_CHECK_VERSION(3,0,0)
6805 {
Bram Moolenaar30613902019-12-01 22:11:18 +01006806 // Add one pixel to the far right column in case a double-stroked
6807 // bold glyph may sit there.
Bram Moolenaar98921892016-02-23 17:14:37 +01006808 const GdkRectangle rect = {
6809 FILL_X(col1), FILL_Y(row1),
6810 (col2 - col1 + 1) * gui.char_width + (col2 == Columns - 1),
6811 (row2 - row1 + 1) * gui.char_height
6812 };
Bram Moolenaar98921892016-02-23 17:14:37 +01006813 cairo_t * const cr = cairo_create(gui.surface);
Bram Moolenaara859f042016-11-17 19:11:55 +01006814# if GTK_CHECK_VERSION(3,22,2)
6815 set_cairo_source_rgba_from_color(cr, gui.back_pixel);
6816# else
presuku9459b8d2021-11-16 20:03:56 +00006817 GdkWindow * const win = gtk_widget_get_window(gui.drawarea);
Bram Moolenaar98921892016-02-23 17:14:37 +01006818 cairo_pattern_t * const pat = gdk_window_get_background_pattern(win);
6819 if (pat != NULL)
6820 cairo_set_source(cr, pat);
6821 else
Bram Moolenaar36edf062016-07-21 22:10:12 +02006822 set_cairo_source_rgba_from_color(cr, gui.back_pixel);
Bram Moolenaara859f042016-11-17 19:11:55 +01006823# endif
presuku9459b8d2021-11-16 20:03:56 +00006824 cairo_rectangle(cr, rect.x, rect.y, rect.width, rect.height);
Bram Moolenaar98921892016-02-23 17:14:37 +01006825 cairo_fill(cr);
6826 cairo_destroy(cr);
6827
presuku9459b8d2021-11-16 20:03:56 +00006828 gtk_widget_queue_draw_area(gui.drawarea,
6829 rect.x, rect.y, rect.width, rect.height);
Bram Moolenaar98921892016-02-23 17:14:37 +01006830 }
Bram Moolenaar30613902019-12-01 22:11:18 +01006831#else // !GTK_CHECK_VERSION(3,0,0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006832 gdk_gc_set_foreground(gui.text_gc, &color);
6833
Bram Moolenaar30613902019-12-01 22:11:18 +01006834 // Clear one extra pixel at the far right, for when bold characters have
6835 // spilled over to the window border.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006836 gdk_draw_rectangle(gui.drawarea->window, gui.text_gc, TRUE,
6837 FILL_X(col1), FILL_Y(row1),
6838 (col2 - col1 + 1) * gui.char_width
6839 + (col2 == Columns - 1),
6840 (row2 - row1 + 1) * gui.char_height);
Bram Moolenaar30613902019-12-01 22:11:18 +01006841#endif // !GTK_CHECK_VERSION(3,0,0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006842}
6843
Bram Moolenaar98921892016-02-23 17:14:37 +01006844#if GTK_CHECK_VERSION(3,0,0)
6845 static void
6846gui_gtk_window_clear(GdkWindow *win)
6847{
6848 const GdkRectangle rect = {
6849 0, 0, gdk_window_get_width(win), gdk_window_get_height(win)
6850 };
6851 cairo_t * const cr = cairo_create(gui.surface);
Bram Moolenaara859f042016-11-17 19:11:55 +01006852# if GTK_CHECK_VERSION(3,22,2)
6853 set_cairo_source_rgba_from_color(cr, gui.back_pixel);
6854# else
Bram Moolenaar98921892016-02-23 17:14:37 +01006855 cairo_pattern_t * const pat = gdk_window_get_background_pattern(win);
6856 if (pat != NULL)
6857 cairo_set_source(cr, pat);
6858 else
Bram Moolenaar36edf062016-07-21 22:10:12 +02006859 set_cairo_source_rgba_from_color(cr, gui.back_pixel);
Bram Moolenaara859f042016-11-17 19:11:55 +01006860# endif
presuku9459b8d2021-11-16 20:03:56 +00006861 cairo_rectangle(cr, rect.x, rect.y, rect.width, rect.height);
Bram Moolenaar98921892016-02-23 17:14:37 +01006862 cairo_fill(cr);
6863 cairo_destroy(cr);
6864
presuku9459b8d2021-11-16 20:03:56 +00006865 gtk_widget_queue_draw_area(gui.drawarea,
6866 rect.x, rect.y, rect.width, rect.height);
Bram Moolenaar98921892016-02-23 17:14:37 +01006867}
Bram Moolenaar25328e32018-09-11 21:30:09 +02006868#else
6869# define gui_gtk_window_clear(win) gdk_window_clear(win)
Bram Moolenaar98921892016-02-23 17:14:37 +01006870#endif
6871
Bram Moolenaar071d4272004-06-13 20:20:40 +00006872 void
6873gui_mch_clear_all(void)
6874{
Bram Moolenaar98921892016-02-23 17:14:37 +01006875 if (gtk_widget_get_window(gui.drawarea) != NULL)
6876 gui_gtk_window_clear(gtk_widget_get_window(gui.drawarea));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006877}
6878
Bram Moolenaar98921892016-02-23 17:14:37 +01006879#if !GTK_CHECK_VERSION(3,0,0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006880/*
6881 * Redraw any text revealed by scrolling up/down.
6882 */
6883 static void
6884check_copy_area(void)
6885{
6886 GdkEvent *event;
6887 int expose_count;
6888
6889 if (gui.visibility != GDK_VISIBILITY_PARTIAL)
6890 return;
6891
Bram Moolenaar30613902019-12-01 22:11:18 +01006892 // Avoid redrawing the cursor while scrolling or it'll end up where
6893 // we don't want it to be. I'm not sure if it's correct to call
6894 // gui_dont_update_cursor() at this point but it works as a quick
6895 // fix for now.
Bram Moolenaar107abd22016-08-12 14:08:25 +02006896 gui_dont_update_cursor(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006897
6898 do
6899 {
Bram Moolenaar30613902019-12-01 22:11:18 +01006900 // Wait to check whether the scroll worked or not.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006901 event = gdk_event_get_graphics_expose(gui.drawarea->window);
6902
6903 if (event == NULL)
Bram Moolenaar30613902019-12-01 22:11:18 +01006904 break; // received NoExpose event
Bram Moolenaar071d4272004-06-13 20:20:40 +00006905
6906 gui_redraw(event->expose.area.x, event->expose.area.y,
6907 event->expose.area.width, event->expose.area.height);
6908
6909 expose_count = event->expose.count;
6910 gdk_event_free(event);
6911 }
Bram Moolenaar30613902019-12-01 22:11:18 +01006912 while (expose_count > 0); // more events follow
Bram Moolenaar071d4272004-06-13 20:20:40 +00006913
6914 gui_can_update_cursor();
6915}
Bram Moolenaar30613902019-12-01 22:11:18 +01006916#endif // !GTK_CHECK_VERSION(3,0,0)
Bram Moolenaar98921892016-02-23 17:14:37 +01006917
6918#if GTK_CHECK_VERSION(3,0,0)
6919 static void
6920gui_gtk_surface_copy_rect(int dest_x, int dest_y,
6921 int src_x, int src_y,
6922 int width, int height)
6923{
6924 cairo_t * const cr = cairo_create(gui.surface);
6925
6926 cairo_rectangle(cr, dest_x, dest_y, width, height);
6927 cairo_clip(cr);
6928 cairo_push_group(cr);
6929 cairo_set_source_surface(cr, gui.surface, dest_x - src_x, dest_y - src_y);
6930 cairo_paint(cr);
6931 cairo_pop_group_to_source(cr);
6932 cairo_paint(cr);
6933
6934 cairo_destroy(cr);
6935}
6936#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006937
6938/*
6939 * Delete the given number of lines from the given row, scrolling up any
6940 * text further down within the scroll region.
6941 */
6942 void
6943gui_mch_delete_lines(int row, int num_lines)
6944{
Bram Moolenaar98921892016-02-23 17:14:37 +01006945#if GTK_CHECK_VERSION(3,0,0)
6946 const int ncols = gui.scroll_region_right - gui.scroll_region_left + 1;
6947 const int nrows = gui.scroll_region_bot - row + 1;
6948 const int src_nrows = nrows - num_lines;
6949
6950 gui_gtk_surface_copy_rect(
6951 FILL_X(gui.scroll_region_left), FILL_Y(row),
6952 FILL_X(gui.scroll_region_left), FILL_Y(row + num_lines),
6953 gui.char_width * ncols + 1, gui.char_height * src_nrows);
6954 gui_clear_block(
6955 gui.scroll_region_bot - num_lines + 1, gui.scroll_region_left,
6956 gui.scroll_region_bot, gui.scroll_region_right);
presuku9459b8d2021-11-16 20:03:56 +00006957 gtk_widget_queue_draw_area(gui.drawarea,
Bram Moolenaar98921892016-02-23 17:14:37 +01006958 FILL_X(gui.scroll_region_left), FILL_Y(row),
presuku9459b8d2021-11-16 20:03:56 +00006959 gui.char_width * ncols + 1, gui.char_height * nrows);
Bram Moolenaar98921892016-02-23 17:14:37 +01006960#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006961 if (gui.visibility == GDK_VISIBILITY_FULLY_OBSCURED)
Bram Moolenaar30613902019-12-01 22:11:18 +01006962 return; // Can't see the window
Bram Moolenaar071d4272004-06-13 20:20:40 +00006963
6964 gdk_gc_set_foreground(gui.text_gc, gui.fgcolor);
6965 gdk_gc_set_background(gui.text_gc, gui.bgcolor);
6966
Bram Moolenaar30613902019-12-01 22:11:18 +01006967 // copy one extra pixel, for when bold has spilled over
Bram Moolenaar071d4272004-06-13 20:20:40 +00006968 gdk_window_copy_area(gui.drawarea->window, gui.text_gc,
6969 FILL_X(gui.scroll_region_left), FILL_Y(row),
6970 gui.drawarea->window,
6971 FILL_X(gui.scroll_region_left),
6972 FILL_Y(row + num_lines),
6973 gui.char_width * (gui.scroll_region_right
6974 - gui.scroll_region_left + 1) + 1,
6975 gui.char_height * (gui.scroll_region_bot - row - num_lines + 1));
6976
6977 gui_clear_block(gui.scroll_region_bot - num_lines + 1,
6978 gui.scroll_region_left,
6979 gui.scroll_region_bot, gui.scroll_region_right);
6980 check_copy_area();
Bram Moolenaar30613902019-12-01 22:11:18 +01006981#endif // !GTK_CHECK_VERSION(3,0,0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006982}
6983
6984/*
6985 * Insert the given number of lines before the given row, scrolling down any
6986 * following text within the scroll region.
6987 */
6988 void
6989gui_mch_insert_lines(int row, int num_lines)
6990{
Bram Moolenaar98921892016-02-23 17:14:37 +01006991#if GTK_CHECK_VERSION(3,0,0)
6992 const int ncols = gui.scroll_region_right - gui.scroll_region_left + 1;
6993 const int nrows = gui.scroll_region_bot - row + 1;
6994 const int src_nrows = nrows - num_lines;
6995
6996 gui_gtk_surface_copy_rect(
6997 FILL_X(gui.scroll_region_left), FILL_Y(row + num_lines),
6998 FILL_X(gui.scroll_region_left), FILL_Y(row),
6999 gui.char_width * ncols + 1, gui.char_height * src_nrows);
presuku9459b8d2021-11-16 20:03:56 +00007000 gui_clear_block(
Bram Moolenaar98921892016-02-23 17:14:37 +01007001 row, gui.scroll_region_left,
7002 row + num_lines - 1, gui.scroll_region_right);
presuku9459b8d2021-11-16 20:03:56 +00007003 gtk_widget_queue_draw_area(gui.drawarea,
Bram Moolenaar98921892016-02-23 17:14:37 +01007004 FILL_X(gui.scroll_region_left), FILL_Y(row),
presuku9459b8d2021-11-16 20:03:56 +00007005 gui.char_width * ncols + 1, gui.char_height * nrows);
Bram Moolenaar98921892016-02-23 17:14:37 +01007006#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007007 if (gui.visibility == GDK_VISIBILITY_FULLY_OBSCURED)
Bram Moolenaar30613902019-12-01 22:11:18 +01007008 return; // Can't see the window
Bram Moolenaar071d4272004-06-13 20:20:40 +00007009
7010 gdk_gc_set_foreground(gui.text_gc, gui.fgcolor);
7011 gdk_gc_set_background(gui.text_gc, gui.bgcolor);
7012
Bram Moolenaar30613902019-12-01 22:11:18 +01007013 // copy one extra pixel, for when bold has spilled over
Bram Moolenaar071d4272004-06-13 20:20:40 +00007014 gdk_window_copy_area(gui.drawarea->window, gui.text_gc,
7015 FILL_X(gui.scroll_region_left), FILL_Y(row + num_lines),
7016 gui.drawarea->window,
7017 FILL_X(gui.scroll_region_left), FILL_Y(row),
7018 gui.char_width * (gui.scroll_region_right
7019 - gui.scroll_region_left + 1) + 1,
7020 gui.char_height * (gui.scroll_region_bot - row - num_lines + 1));
7021
7022 gui_clear_block(row, gui.scroll_region_left,
7023 row + num_lines - 1, gui.scroll_region_right);
7024 check_copy_area();
Bram Moolenaar30613902019-12-01 22:11:18 +01007025#endif // !GTK_CHECK_VERSION(3,0,0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007026}
7027
7028/*
7029 * X Selection stuff, for cutting and pasting text to other windows.
7030 */
7031 void
Bram Moolenaar0554fa42019-06-14 21:36:54 +02007032clip_mch_request_selection(Clipboard_T *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007033{
7034 GdkAtom target;
7035 unsigned i;
Bram Moolenaar51b5ab92008-01-06 14:17:07 +00007036 time_t start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007037
7038 for (i = 0; i < N_SELECTION_TARGETS; ++i)
7039 {
Bram Moolenaar3a6eaa52009-06-16 13:23:06 +00007040 if (!clip_html && selection_targets[i].info == TARGET_HTML)
7041 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007042 received_selection = RS_NONE;
7043 target = gdk_atom_intern(selection_targets[i].target, FALSE);
7044
7045 gtk_selection_convert(gui.drawarea,
7046 cbd->gtk_sel_atom, target,
7047 (guint32)GDK_CURRENT_TIME);
7048
Bram Moolenaar30613902019-12-01 22:11:18 +01007049 // Hack: Wait up to three seconds for the selection. A hang was
7050 // noticed here when using the netrw plugin combined with ":gui"
7051 // during the FocusGained event.
Bram Moolenaar51b5ab92008-01-06 14:17:07 +00007052 start = time(NULL);
7053 while (received_selection == RS_NONE && time(NULL) < start + 3)
Bram Moolenaar30613902019-12-01 22:11:18 +01007054 g_main_context_iteration(NULL, TRUE); // wait for selection_received_cb
Bram Moolenaar071d4272004-06-13 20:20:40 +00007055
7056 if (received_selection != RS_FAIL)
7057 return;
7058 }
7059
lilydjwg6e0a18f2024-01-29 20:54:28 +01007060 if (gui_mch_get_display())
7061 // Final fallback position - use the X CUT_BUFFER0 store
7062 yank_cut_buffer0(GDK_WINDOW_XDISPLAY(gtk_widget_get_window(gui.mainwin)),
7063 cbd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007064}
7065
7066/*
7067 * Disown the selection.
7068 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007069 void
Bram Moolenaar0554fa42019-06-14 21:36:54 +02007070clip_mch_lose_selection(Clipboard_T *cbd UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007071{
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00007072 if (in_selection_clear_event)
7073 return;
7074
7075 gtk_selection_owner_set(NULL, cbd->gtk_sel_atom, gui.event_time);
7076 gui_mch_update();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007077}
7078
7079/*
7080 * Own the selection and return OK if it worked.
7081 */
7082 int
Bram Moolenaar0554fa42019-06-14 21:36:54 +02007083clip_mch_own_selection(Clipboard_T *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007084{
lilydjwg94ff09a2024-01-29 20:14:01 +01007085 // If we're blocking autocmds, we are filling the register to offer the
7086 // selection (inside selection-get)
7087 if (is_autocmd_blocked())
7088 return OK;
7089
Bram Moolenaar071d4272004-06-13 20:20:40 +00007090 int success;
7091
7092 success = gtk_selection_owner_set(gui.drawarea, cbd->gtk_sel_atom,
Bram Moolenaar20892c12011-06-26 04:49:00 +02007093 gui.event_time);
lilydjwg94ff09a2024-01-29 20:14:01 +01007094 // don't update on every visual selection change
7095 if (!(cbd->owned && VIsual_active))
7096 gui_gtk_set_selection_targets(cbd->gtk_sel_atom);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007097 gui_mch_update();
7098 return (success) ? OK : FAIL;
7099}
7100
7101/*
7102 * Send the current selection to the clipboard. Do nothing for X because we
7103 * will fill in the selection only when requested by another app.
7104 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007105 void
Bram Moolenaar0554fa42019-06-14 21:36:54 +02007106clip_mch_set_selection(Clipboard_T *cbd UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007107{
7108}
7109
Bram Moolenaar113e1072019-01-20 15:30:40 +01007110#if (defined(FEAT_XCLIPBOARD) && defined(USE_SYSTEM)) || defined(PROTO)
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01007111 int
Bram Moolenaar0554fa42019-06-14 21:36:54 +02007112clip_gtk_owner_exists(Clipboard_T *cbd)
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01007113{
7114 return gdk_selection_owner_get(cbd->gtk_sel_atom) != NULL;
7115}
Bram Moolenaar113e1072019-01-20 15:30:40 +01007116#endif
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01007117
Bram Moolenaar071d4272004-06-13 20:20:40 +00007118
7119#if defined(FEAT_MENU) || defined(PROTO)
7120/*
7121 * Make a menu item appear either active or not active (grey or not grey).
7122 */
7123 void
7124gui_mch_menu_grey(vimmenu_T *menu, int grey)
7125{
7126 if (menu->id == NULL)
7127 return;
7128
7129 if (menu_is_separator(menu->name))
7130 grey = TRUE;
7131
7132 gui_mch_menu_hidden(menu, FALSE);
Bram Moolenaar30613902019-12-01 22:11:18 +01007133 // Be clever about bitfields versus true booleans here!
Bram Moolenaar98921892016-02-23 17:14:37 +01007134 if (!gtk_widget_get_sensitive(menu->id) == !grey)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007135 {
7136 gtk_widget_set_sensitive(menu->id, !grey);
7137 gui_mch_update();
7138 }
7139}
7140
7141/*
7142 * Make menu item hidden or not hidden.
7143 */
7144 void
7145gui_mch_menu_hidden(vimmenu_T *menu, int hidden)
7146{
7147 if (menu->id == 0)
7148 return;
7149
7150 if (hidden)
7151 {
Bram Moolenaar98921892016-02-23 17:14:37 +01007152 if (gtk_widget_get_visible(menu->id))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007153 {
7154 gtk_widget_hide(menu->id);
7155 gui_mch_update();
7156 }
7157 }
7158 else
7159 {
Bram Moolenaar98921892016-02-23 17:14:37 +01007160 if (!gtk_widget_get_visible(menu->id))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007161 {
7162 gtk_widget_show(menu->id);
7163 gui_mch_update();
7164 }
7165 }
7166}
7167
7168/*
7169 * This is called after setting all the menus to grey/hidden or not.
7170 */
7171 void
7172gui_mch_draw_menubar(void)
7173{
Bram Moolenaar30613902019-12-01 22:11:18 +01007174 // just make sure that the visual changes get effect immediately
Bram Moolenaar071d4272004-06-13 20:20:40 +00007175 gui_mch_update();
7176}
Bram Moolenaar30613902019-12-01 22:11:18 +01007177#endif // FEAT_MENU
Bram Moolenaar071d4272004-06-13 20:20:40 +00007178
7179/*
7180 * Scrollbar stuff.
7181 */
7182 void
7183gui_mch_enable_scrollbar(scrollbar_T *sb, int flag)
7184{
7185 if (sb->id == NULL)
7186 return;
7187
Bram Moolenaar98921892016-02-23 17:14:37 +01007188 gtk_widget_set_visible(sb->id, flag);
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00007189 update_window_manager_hints(0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007190}
7191
7192
7193/*
7194 * Return the RGB value of a pixel as long.
7195 */
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02007196 guicolor_T
Bram Moolenaar071d4272004-06-13 20:20:40 +00007197gui_mch_get_rgb(guicolor_T pixel)
7198{
Bram Moolenaar98921892016-02-23 17:14:37 +01007199#if GTK_CHECK_VERSION(3,0,0)
Bram Moolenaar36edf062016-07-21 22:10:12 +02007200 return (long_u)pixel;
Bram Moolenaar98921892016-02-23 17:14:37 +01007201#else
Bram Moolenaar36edf062016-07-21 22:10:12 +02007202 GdkColor color;
7203
Bram Moolenaar071d4272004-06-13 20:20:40 +00007204 gdk_colormap_query_color(gtk_widget_get_colormap(gui.drawarea),
7205 (unsigned long)pixel, &color);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007206
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02007207 return (guicolor_T)(
7208 (((unsigned)color.red & 0xff00) << 8)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007209 | ((unsigned)color.green & 0xff00)
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02007210 | (((unsigned)color.blue & 0xff00) >> 8));
Bram Moolenaar36edf062016-07-21 22:10:12 +02007211#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007212}
7213
7214/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00007215 * Get current mouse coordinates in text window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007216 */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00007217 void
7218gui_mch_getmouse(int *x, int *y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007219{
Bram Moolenaar98921892016-02-23 17:14:37 +01007220 gui_gtk_get_pointer(gui.drawarea, x, y, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007221}
7222
7223 void
7224gui_mch_setmouse(int x, int y)
7225{
Bram Moolenaar30613902019-12-01 22:11:18 +01007226 // Sorry for the Xlib call, but we can't avoid it, since there is no
7227 // internal GDK mechanism present to accomplish this. (and for good
7228 // reason...)
lilydjwg6e0a18f2024-01-29 20:54:28 +01007229 Display * dpy = gui_mch_get_display();
7230 if (dpy)
7231 XWarpPointer(dpy, (Window)0,
7232 GDK_WINDOW_XID(gtk_widget_get_window(gui.drawarea)),
7233 0, 0, 0U, 0U, x, y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007234}
7235
7236
7237#ifdef FEAT_MOUSESHAPE
Bram Moolenaar30613902019-12-01 22:11:18 +01007238// The last set mouse pointer shape is remembered, to be used when it goes
7239// from hidden to not hidden.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007240static int last_shape = 0;
7241#endif
7242
7243/*
7244 * Use the blank mouse pointer or not.
7245 *
7246 * hide: TRUE = use blank ptr, FALSE = use parent ptr
7247 */
7248 void
7249gui_mch_mousehide(int hide)
7250{
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00007251 if (gui.pointer_hidden == hide)
7252 return;
7253
7254 gui.pointer_hidden = hide;
7255 if (gtk_widget_get_window(gui.drawarea) && gui.blank_pointer != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007256 {
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00007257 if (hide)
7258 gdk_window_set_cursor(gtk_widget_get_window(gui.drawarea),
7259 gui.blank_pointer);
7260 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007261#ifdef FEAT_MOUSESHAPE
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00007262 mch_set_mouse_shape(last_shape);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007263#else
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00007264 gdk_window_set_cursor(gtk_widget_get_window(gui.drawarea), NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007265#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007266 }
7267}
7268
7269#if defined(FEAT_MOUSESHAPE) || defined(PROTO)
7270
Bram Moolenaar30613902019-12-01 22:11:18 +01007271// Table for shape IDs. Keep in sync with the mshape_names[] table in
7272// misc2.c!
Bram Moolenaar071d4272004-06-13 20:20:40 +00007273static const int mshape_ids[] =
7274{
Bram Moolenaar30613902019-12-01 22:11:18 +01007275 GDK_LEFT_PTR, // arrow
7276 GDK_CURSOR_IS_PIXMAP, // blank
7277 GDK_XTERM, // beam
7278 GDK_SB_V_DOUBLE_ARROW, // updown
7279 GDK_SIZING, // udsizing
7280 GDK_SB_H_DOUBLE_ARROW, // leftright
7281 GDK_SIZING, // lrsizing
7282 GDK_WATCH, // busy
7283 GDK_X_CURSOR, // no
7284 GDK_CROSSHAIR, // crosshair
7285 GDK_HAND1, // hand1
7286 GDK_HAND2, // hand2
7287 GDK_PENCIL, // pencil
7288 GDK_QUESTION_ARROW, // question
7289 GDK_RIGHT_PTR, // right-arrow
7290 GDK_CENTER_PTR, // up-arrow
7291 GDK_LEFT_PTR // last one
Bram Moolenaar071d4272004-06-13 20:20:40 +00007292};
7293
7294 void
7295mch_set_mouse_shape(int shape)
7296{
7297 int id;
7298 GdkCursor *c;
7299
Bram Moolenaar98921892016-02-23 17:14:37 +01007300 if (gtk_widget_get_window(gui.drawarea) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007301 return;
7302
7303 if (shape == MSHAPE_HIDE || gui.pointer_hidden)
Bram Moolenaar98921892016-02-23 17:14:37 +01007304 gdk_window_set_cursor(gtk_widget_get_window(gui.drawarea),
7305 gui.blank_pointer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007306 else
7307 {
7308 if (shape >= MSHAPE_NUMBERED)
7309 {
7310 id = shape - MSHAPE_NUMBERED;
7311 if (id >= GDK_LAST_CURSOR)
7312 id = GDK_LEFT_PTR;
7313 else
Bram Moolenaar30613902019-12-01 22:11:18 +01007314 id &= ~1; // they are always even (why?)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007315 }
K.Takataeeec2542021-06-02 13:28:16 +02007316 else if (shape < (int)ARRAY_LENGTH(mshape_ids))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007317 id = mshape_ids[shape];
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00007318 else
7319 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007320 c = gdk_cursor_new_for_display(
Bram Moolenaarb71ec9f2005-01-25 22:22:02 +00007321 gtk_widget_get_display(gui.drawarea), (GdkCursorType)id);
Bram Moolenaar98921892016-02-23 17:14:37 +01007322 gdk_window_set_cursor(gtk_widget_get_window(gui.drawarea), c);
Bram Moolenaar98921892016-02-23 17:14:37 +01007323# if GTK_CHECK_VERSION(3,0,0)
7324 g_object_unref(G_OBJECT(c));
7325# else
Bram Moolenaar30613902019-12-01 22:11:18 +01007326 gdk_cursor_destroy(c); // Unref, actually. Bloody GTK+ 1.
Bram Moolenaar98921892016-02-23 17:14:37 +01007327# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007328 }
7329 if (shape != MSHAPE_HIDE)
7330 last_shape = shape;
7331}
Bram Moolenaar30613902019-12-01 22:11:18 +01007332#endif // FEAT_MOUSESHAPE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007333
7334
7335#if defined(FEAT_SIGN_ICONS) || defined(PROTO)
7336/*
7337 * Signs are currently always 2 chars wide. With GTK+ 2, the image will be
7338 * scaled down if the current font is not big enough, or scaled up if the image
7339 * size is less than 3/4 of the maximum sign size. With GTK+ 1, the pixmap
7340 * will be cut off if the current font is not big enough, or centered if it's
7341 * too small.
7342 */
7343# define SIGN_WIDTH (2 * gui.char_width)
7344# define SIGN_HEIGHT (gui.char_height)
7345# define SIGN_ASPECT ((double)SIGN_HEIGHT / (double)SIGN_WIDTH)
7346
Bram Moolenaar071d4272004-06-13 20:20:40 +00007347 void
7348gui_mch_drawsign(int row, int col, int typenr)
7349{
7350 GdkPixbuf *sign;
7351
7352 sign = (GdkPixbuf *)sign_get_image(typenr);
7353
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00007354 if (sign == NULL || gui.drawarea == NULL
7355 || gtk_widget_get_window(gui.drawarea) == NULL)
7356 return;
7357
7358 int width;
7359 int height;
7360 int xoffset;
7361 int yoffset;
7362 int need_scale;
7363
7364 width = gdk_pixbuf_get_width(sign);
7365 height = gdk_pixbuf_get_height(sign);
7366 /*
7367 * Decide whether we need to scale. Allow one pixel of border
7368 * width to be cut off, in order to avoid excessive scaling for
7369 * tiny differences in font size.
7370 * Do scale to fit the height to avoid gaps because of linespacing.
7371 */
7372 need_scale = (width > SIGN_WIDTH + 2
7373 || height != SIGN_HEIGHT
7374 || (width < 3 * SIGN_WIDTH / 4
7375 && height < 3 * SIGN_HEIGHT / 4));
7376 if (need_scale)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007377 {
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00007378 double aspect;
7379 int w = width;
7380 int h = height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007381
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00007382 // Keep the original aspect ratio
7383 aspect = (double)height / (double)width;
7384 width = (double)SIGN_WIDTH * SIGN_ASPECT / aspect;
7385 width = MIN(width, SIGN_WIDTH);
7386 if (((double)(MAX(height, SIGN_HEIGHT)) /
7387 (double)(MIN(height, SIGN_HEIGHT))) < 1.15)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007388 {
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00007389 // Change the aspect ratio by at most 15% to fill the
7390 // available space completely.
7391 height = (double)SIGN_HEIGHT * SIGN_ASPECT / aspect;
7392 height = MIN(height, SIGN_HEIGHT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007393 }
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00007394 else
7395 height = (double)width * aspect;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007396
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00007397 if (w == width && h == height)
7398 {
7399 // no change in dimensions; don't decrease reference counter
7400 // (below)
7401 need_scale = FALSE;
7402 }
7403 else
7404 {
7405 // This doesn't seem to be worth caching, and doing so would
7406 // complicate the code quite a bit.
7407 sign = gdk_pixbuf_scale_simple(sign, width, height,
7408 GDK_INTERP_BILINEAR);
7409 if (sign == NULL)
7410 return; // out of memory
7411 }
7412 }
7413
7414 // The origin is the upper-left corner of the pixmap. Therefore
7415 // these offset may become negative if the pixmap is smaller than
7416 // the 2x1 cells reserved for the sign icon.
7417 xoffset = (width - SIGN_WIDTH) / 2;
7418 yoffset = (height - SIGN_HEIGHT) / 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007419
Bram Moolenaar98921892016-02-23 17:14:37 +01007420# if GTK_CHECK_VERSION(3,0,0)
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00007421 {
7422 cairo_t *cr;
7423 cairo_surface_t *bg_surf;
7424 cairo_t *bg_cr;
7425 cairo_surface_t *sign_surf;
7426 cairo_t *sign_cr;
Bram Moolenaar98921892016-02-23 17:14:37 +01007427
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00007428 cr = cairo_create(gui.surface);
Bram Moolenaar98921892016-02-23 17:14:37 +01007429
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00007430 bg_surf = cairo_surface_create_similar(gui.surface,
7431 cairo_surface_get_content(gui.surface),
7432 SIGN_WIDTH, SIGN_HEIGHT);
7433 bg_cr = cairo_create(bg_surf);
7434 cairo_set_source_rgba(bg_cr,
7435 gui.bgcolor->red, gui.bgcolor->green, gui.bgcolor->blue,
7436 gui.bgcolor->alpha);
7437 cairo_paint(bg_cr);
Bram Moolenaar98921892016-02-23 17:14:37 +01007438
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00007439 sign_surf = cairo_surface_create_similar(gui.surface,
7440 cairo_surface_get_content(gui.surface),
7441 SIGN_WIDTH, SIGN_HEIGHT);
7442 sign_cr = cairo_create(sign_surf);
7443 gdk_cairo_set_source_pixbuf(sign_cr, sign, -xoffset, -yoffset);
7444 cairo_paint(sign_cr);
Bram Moolenaar98921892016-02-23 17:14:37 +01007445
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00007446 cairo_set_operator(sign_cr, CAIRO_OPERATOR_DEST_OVER);
7447 cairo_set_source_surface(sign_cr, bg_surf, 0, 0);
7448 cairo_paint(sign_cr);
Bram Moolenaar98921892016-02-23 17:14:37 +01007449
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00007450 cairo_set_source_surface(cr, sign_surf, FILL_X(col), FILL_Y(row));
7451 cairo_paint(cr);
Bram Moolenaar98921892016-02-23 17:14:37 +01007452
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00007453 cairo_destroy(sign_cr);
7454 cairo_surface_destroy(sign_surf);
7455 cairo_destroy(bg_cr);
7456 cairo_surface_destroy(bg_surf);
7457 cairo_destroy(cr);
Bram Moolenaar98921892016-02-23 17:14:37 +01007458
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00007459 gtk_widget_queue_draw_area(gui.drawarea,
7460 FILL_X(col), FILL_Y(col), width, height);
Bram Moolenaar98921892016-02-23 17:14:37 +01007461
Bram Moolenaar071d4272004-06-13 20:20:40 +00007462 }
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00007463# else // !GTK_CHECK_VERSION(3,0,0)
7464 gdk_gc_set_foreground(gui.text_gc, gui.bgcolor);
7465
7466 gdk_draw_rectangle(gui.drawarea->window,
7467 gui.text_gc,
7468 TRUE,
7469 FILL_X(col),
7470 FILL_Y(row),
7471 SIGN_WIDTH,
7472 SIGN_HEIGHT);
7473
7474 gdk_pixbuf_render_to_drawable_alpha(sign,
7475 gui.drawarea->window,
7476 MAX(0, xoffset),
7477 MAX(0, yoffset),
7478 FILL_X(col) - MIN(0, xoffset),
7479 FILL_Y(row) - MIN(0, yoffset),
7480 MIN(width, SIGN_WIDTH),
7481 MIN(height, SIGN_HEIGHT),
7482 GDK_PIXBUF_ALPHA_BILEVEL,
7483 127,
7484 GDK_RGB_DITHER_NORMAL,
7485 0, 0);
7486# endif // !GTK_CHECK_VERSION(3,0,0)
7487 if (need_scale)
7488 g_object_unref(sign);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007489}
7490
7491 void *
7492gui_mch_register_sign(char_u *signfile)
7493{
7494 if (signfile[0] != NUL && signfile[0] != '-' && gui.in_use)
7495 {
7496 GdkPixbuf *sign;
7497 GError *error = NULL;
7498 char_u *message;
7499
7500 sign = gdk_pixbuf_new_from_file((const char *)signfile, &error);
7501
7502 if (error == NULL)
7503 return sign;
7504
7505 message = (char_u *)error->message;
7506
7507 if (message != NULL && input_conv.vc_type != CONV_NONE)
7508 message = string_convert(&input_conv, message, NULL);
7509
7510 if (message != NULL)
7511 {
Bram Moolenaar30613902019-12-01 22:11:18 +01007512 // The error message is already translated and will be more
7513 // descriptive than anything we could possibly do ourselves.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007514 semsg("E255: %s", message);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007515
7516 if (input_conv.vc_type != CONV_NONE)
7517 vim_free(message);
7518 }
7519 g_error_free(error);
7520 }
7521
7522 return NULL;
7523}
7524
7525 void
7526gui_mch_destroy_sign(void *sign)
7527{
7528 if (sign != NULL)
7529 g_object_unref(sign);
7530}
7531
Bram Moolenaar30613902019-12-01 22:11:18 +01007532#endif // FEAT_SIGN_ICONS