Bram Moolenaar | edf3f97 | 2016-08-29 22:49:24 +0200 | [diff] [blame] | 1 | /* vi:set ts=8 sts=4 sw=4 noet: |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2 | * |
| 3 | * VIM - Vi IMproved by Bram Moolenaar |
| 4 | * |
| 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 Moolenaar | 0a56cb8 | 2005-01-04 21:45:14 +0000 | [diff] [blame] | 13 | * (C) 1998,1999,2000 by Marcin Dalecki <martin@dalecki.de> |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 14 | * |
| 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 Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 22 | * |
| 23 | * Support for GTK+ 3 was added by: |
| 24 | * |
| 25 | * 2016 Kazunobu Kuriyama <kazunobu.kuriyama@gmail.com> |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 26 | */ |
| 27 | |
| 28 | #include "vim.h" |
Bram Moolenaar | 36e294c | 2015-12-29 18:55:46 +0100 | [diff] [blame] | 29 | #ifdef USE_GRESOURCE |
| 30 | #include "auto/gui_gtk_gresources.h" |
| 31 | #endif |
Bram Moolenaar | c93e791 | 2008-07-08 10:46:08 +0000 | [diff] [blame] | 32 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 33 | #ifdef FEAT_GUI_GNOME |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 34 | // Gnome redefines _() and N_(). Grrr... |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 35 | # 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 Moolenaar | a2dd900 | 2007-05-14 17:38:30 +0000 | [diff] [blame] | 47 | # ifdef bind_textdomain_codeset |
| 48 | # undef bind_textdomain_codeset |
Bram Moolenaar | 4932594 | 2007-05-10 19:19:59 +0000 | [diff] [blame] | 49 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 50 | # if defined(FEAT_GETTEXT) && !defined(ENABLE_NLS) |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 51 | # define ENABLE_NLS // so the texts in the dialog boxes are translated |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 52 | # endif |
| 53 | # include <gnome.h> |
| 54 | # include "version.h" |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 55 | // missing prototype in bonobo-dock-item.h |
Bram Moolenaar | db552d60 | 2006-03-23 22:59:57 +0000 | [diff] [blame] | 56 | extern void bonobo_dock_item_set_behavior(BonoboDockItem *dock_item, BonoboDockItemBehavior beh); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 57 | #endif |
| 58 | |
| 59 | #if !defined(FEAT_GUI_GTK) && defined(PROTO) |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 60 | // When generating prototypes we don't want syntax errors. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 61 | # 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 Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 82 | # 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 Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 88 | # include <gdk/gdk.h> |
Bram Moolenaar | 4f97475 | 2019-02-17 17:44:42 +0100 | [diff] [blame] | 89 | # ifdef MSWIN |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 90 | # include <gdk/gdkwin32.h> |
| 91 | # else |
| 92 | # include <gdk/gdkx.h> |
| 93 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 94 | # 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 Moolenaar | b463e8d | 2017-06-05 15:07:09 +0200 | [diff] [blame] | 105 | #define GET_X_ATOM(atom) gdk_x11_atom_to_xatom_for_display( \ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 106 | gtk_widget_get_display(gui.mainwin), atom) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 107 | |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 108 | // Selection type distinguishers |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 109 | enum |
| 110 | { |
| 111 | TARGET_TYPE_NONE, |
| 112 | TARGET_UTF8_STRING, |
| 113 | TARGET_STRING, |
| 114 | TARGET_COMPOUND_TEXT, |
Bram Moolenaar | 3a6eaa5 | 2009-06-16 13:23:06 +0000 | [diff] [blame] | 115 | TARGET_HTML, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 116 | TARGET_TEXT, |
| 117 | TARGET_TEXT_URI_LIST, |
| 118 | TARGET_TEXT_PLAIN, |
lilydjwg | 94ff09a | 2024-01-29 20:14:01 +0100 | [diff] [blame] | 119 | TARGET_TEXT_PLAIN_UTF8, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 120 | 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 | */ |
| 128 | static const GtkTargetEntry selection_targets[] = |
| 129 | { |
| 130 | {VIMENC_ATOM_NAME, 0, TARGET_VIMENC}, |
| 131 | {VIM_ATOM_NAME, 0, TARGET_VIM}, |
Bram Moolenaar | 3a6eaa5 | 2009-06-16 13:23:06 +0000 | [diff] [blame] | 132 | {"text/html", 0, TARGET_HTML}, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 133 | {"UTF8_STRING", 0, TARGET_UTF8_STRING}, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 134 | {"COMPOUND_TEXT", 0, TARGET_COMPOUND_TEXT}, |
| 135 | {"TEXT", 0, TARGET_TEXT}, |
lilydjwg | 94ff09a | 2024-01-29 20:14:01 +0100 | [diff] [blame] | 136 | {"STRING", 0, TARGET_STRING}, |
| 137 | {"text/plain;charset=utf-8", 0, TARGET_TEXT_PLAIN_UTF8}, |
| 138 | {"text/plain", 0, TARGET_TEXT_PLAIN} |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 139 | }; |
K.Takata | eeec254 | 2021-06-02 13:28:16 +0200 | [diff] [blame] | 140 | #define N_SELECTION_TARGETS ARRAY_LENGTH(selection_targets) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 141 | |
| 142 | #ifdef FEAT_DND |
| 143 | /* |
| 144 | * Table of DnD targets supported by Vim. |
| 145 | * Note: Order matters, preferred types should come first. |
| 146 | */ |
| 147 | static const GtkTargetEntry dnd_targets[] = |
| 148 | { |
| 149 | {"text/uri-list", 0, TARGET_TEXT_URI_LIST}, |
Bram Moolenaar | 3a6eaa5 | 2009-06-16 13:23:06 +0000 | [diff] [blame] | 150 | {"text/html", 0, TARGET_HTML}, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 151 | {"UTF8_STRING", 0, TARGET_UTF8_STRING}, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 152 | {"STRING", 0, TARGET_STRING}, |
| 153 | {"text/plain", 0, TARGET_TEXT_PLAIN} |
| 154 | }; |
K.Takata | eeec254 | 2021-06-02 13:28:16 +0200 | [diff] [blame] | 155 | # define N_DND_TARGETS ARRAY_LENGTH(dnd_targets) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 156 | #endif |
| 157 | |
| 158 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 159 | /* |
| 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 Moolenaar | f96ae0b | 2019-07-28 15:21:55 +0200 | [diff] [blame] | 165 | #if defined(FEAT_GUI_GNOME) && defined(FEAT_SESSION) |
| 166 | # define USE_GNOME_SESSION |
| 167 | #endif |
| 168 | |
| 169 | #if !defined(FEAT_GUI_GNOME) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 170 | /* |
| 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 | */ |
| 174 | static GdkAtom wm_protocols_atom = GDK_NONE; |
| 175 | static GdkAtom save_yourself_atom = GDK_NONE; |
| 176 | #endif |
| 177 | |
| 178 | /* |
| 179 | * Atoms used to control/reference X11 selections. |
| 180 | */ |
Bram Moolenaar | 3a6eaa5 | 2009-06-16 13:23:06 +0000 | [diff] [blame] | 181 | static GdkAtom html_atom = GDK_NONE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 182 | static GdkAtom utf8_string_atom = GDK_NONE; |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 183 | static GdkAtom vim_atom = GDK_NONE; // Vim's own special selection format |
| 184 | static GdkAtom vimenc_atom = GDK_NONE; // Vim's extended selection format |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 185 | |
| 186 | /* |
| 187 | * Keycodes recognized by vim. |
| 188 | * NOTE: when changing this, the table in gui_x11.c probably needs the same |
| 189 | * change! |
| 190 | */ |
| 191 | static struct special_key |
| 192 | { |
| 193 | guint key_sym; |
| 194 | char_u code0; |
| 195 | char_u code1; |
| 196 | } |
| 197 | const 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 Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 224 | {GDK_Pause, 'F', 'B'}, // Pause == F21 according to netbeans.txt |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 225 | {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 Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 255 | // Keypad keys: |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 256 | {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 Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 264 | {GDK_KP_Prior, 'K', '3'}, // page up |
| 265 | {GDK_KP_Next, 'K', '5'}, // page down |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 266 | |
| 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 Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 285 | // End of list marker: |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 286 | {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 Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 301 | #define ARG_XRM 10 // ignored |
| 302 | #define ARG_MENUFONT 11 // ignored |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 303 | #define ARG_INDEX_MASK 0x00ff |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 304 | #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 Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 309 | |
| 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 | */ |
| 317 | typedef struct |
| 318 | { |
| 319 | const char *name; |
| 320 | unsigned int flags; |
| 321 | } |
| 322 | cmdline_option_T; |
| 323 | |
| 324 | static const cmdline_option_T cmdline_options[] = |
| 325 | { |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 326 | // We handle these options ourselves |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 327 | {"-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 Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 340 | {"--role", ARG_ROLE|ARG_HAS_VALUE}, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 341 | #ifdef FEAT_NETBEANS_INTG |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 342 | {"-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 Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 346 | #endif |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 347 | // Arguments handled by GTK (and GNOME) internally. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 348 | {"--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 Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 358 | {"--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 Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 361 | #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 Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 375 | # if 0 // conflicts with Vim's own --version argument |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 376 | {"--version", ARG_FOR_GTK|ARG_NEEDS_GUI}, |
| 377 | # endif |
| 378 | {"--disable-crash-dialog", ARG_FOR_GTK}, |
| 379 | #endif |
| 380 | {NULL, 0} |
| 381 | }; |
| 382 | |
| 383 | static int gui_argc = 0; |
| 384 | static char **gui_argv = NULL; |
| 385 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 386 | static const char *role_argument = NULL; |
Bram Moolenaar | f96ae0b | 2019-07-28 15:21:55 +0200 | [diff] [blame] | 387 | #if defined(USE_GNOME_SESSION) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 388 | static const char *restart_command = NULL; |
Bram Moolenaar | 9085f80 | 2009-06-03 14:20:21 +0000 | [diff] [blame] | 389 | static char *abs_restart_command = NULL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 390 | #endif |
| 391 | static int found_iconic_arg = FALSE; |
| 392 | |
| 393 | #ifdef FEAT_GUI_GNOME |
| 394 | /* |
| 395 | * Can't use Gnome if --socketid given |
| 396 | */ |
| 397 | static int using_gnome = 0; |
| 398 | #else |
| 399 | # define using_gnome 0 |
| 400 | #endif |
| 401 | |
Ernie Rael | d42b839 | 2022-04-23 19:52:23 +0100 | [diff] [blame] | 402 | // Comment out the following line to ignore code for resize history tracking. |
| 403 | #define TRACK_RESIZE_HISTORY |
| 404 | #ifdef TRACK_RESIZE_HISTORY |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 405 | /* |
Ernie Rael | 9f53e7b | 2022-04-17 18:27:49 +0100 | [diff] [blame] | 406 | * 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 Rael | d42b839 | 2022-04-23 19:52:23 +0100 | [diff] [blame] | 412 | # if 0 // Change to 1 to enable ch_log() calls for debugging. |
Bram Moolenaar | 4c5678f | 2022-11-30 18:12:19 +0000 | [diff] [blame] | 413 | # ifdef FEAT_EVAL |
Ernie Rael | d42b839 | 2022-04-23 19:52:23 +0100 | [diff] [blame] | 414 | # define ENABLE_RESIZE_HISTORY_LOG |
| 415 | # endif |
Ernie Rael | 9f53e7b | 2022-04-17 18:27:49 +0100 | [diff] [blame] | 416 | # endif |
Ernie Rael | 9f53e7b | 2022-04-17 18:27:49 +0100 | [diff] [blame] | 417 | |
| 418 | /* |
| 419 | * History item of a resize request. |
| 420 | * Width and height are of gui.mainwin. |
| 421 | */ |
| 422 | typedef struct resize_history { |
| 423 | int used; // If true, can't match for discard. Only matches once. |
| 424 | int width; |
| 425 | int height; |
Ernie Rael | d42b839 | 2022-04-23 19:52:23 +0100 | [diff] [blame] | 426 | # ifdef ENABLE_RESIZE_HISTORY_LOG |
Ernie Rael | 9f53e7b | 2022-04-17 18:27:49 +0100 | [diff] [blame] | 427 | int seq; // for ch_log messages |
Ernie Rael | d42b839 | 2022-04-23 19:52:23 +0100 | [diff] [blame] | 428 | # endif |
Ernie Rael | 9f53e7b | 2022-04-17 18:27:49 +0100 | [diff] [blame] | 429 | struct resize_history *next; |
| 430 | } resize_hist_T; |
| 431 | |
| 432 | // never NULL during execution |
| 433 | static resize_hist_T *latest_resize_hist; |
| 434 | // list of stale resize requests |
| 435 | static 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 |
| 443 | alloc_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 Rael | d42b839 | 2022-04-23 19:52:23 +0100 | [diff] [blame] | 457 | # ifdef ENABLE_RESIZE_HISTORY_LOG |
Ernie Rael | 9f53e7b | 2022-04-17 18:27:49 +0100 | [diff] [blame] | 458 | 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 Rael | d42b839 | 2022-04-23 19:52:23 +0100 | [diff] [blame] | 461 | # endif |
Ernie Rael | 9f53e7b | 2022-04-17 18:27:49 +0100 | [diff] [blame] | 462 | } |
| 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 Lakshmanan | a23a11b | 2023-02-21 14:27:41 +0000 | [diff] [blame] | 469 | clear_resize_hists(void) |
Ernie Rael | 9f53e7b | 2022-04-17 18:27:49 +0100 | [diff] [blame] | 470 | { |
Ernie Rael | d42b839 | 2022-04-23 19:52:23 +0100 | [diff] [blame] | 471 | # ifdef ENABLE_RESIZE_HISTORY_LOG |
Ernie Rael | 9f53e7b | 2022-04-17 18:27:49 +0100 | [diff] [blame] | 472 | int i = 0; |
Ernie Rael | d42b839 | 2022-04-23 19:52:23 +0100 | [diff] [blame] | 473 | # endif |
Ernie Rael | 9f53e7b | 2022-04-17 18:27:49 +0100 | [diff] [blame] | 474 | |
| 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 Rael | d42b839 | 2022-04-23 19:52:23 +0100 | [diff] [blame] | 483 | # ifdef ENABLE_RESIZE_HISTORY_LOG |
Ernie Rael | 9f53e7b | 2022-04-17 18:27:49 +0100 | [diff] [blame] | 484 | i++; |
Ernie Rael | d42b839 | 2022-04-23 19:52:23 +0100 | [diff] [blame] | 485 | # endif |
Ernie Rael | 9f53e7b | 2022-04-17 18:27:49 +0100 | [diff] [blame] | 486 | } |
Ernie Rael | d42b839 | 2022-04-23 19:52:23 +0100 | [diff] [blame] | 487 | # ifdef ENABLE_RESIZE_HISTORY_LOG |
Ernie Rael | 9f53e7b | 2022-04-17 18:27:49 +0100 | [diff] [blame] | 488 | ch_log(NULL, "gui_gtk: free %d hists", i); |
Ernie Rael | d42b839 | 2022-04-23 19:52:23 +0100 | [diff] [blame] | 489 | # endif |
Ernie Rael | 9f53e7b | 2022-04-17 18:27:49 +0100 | [diff] [blame] | 490 | } |
| 491 | |
| 492 | // true if hist item is unused and matches w,h |
Ernie Rael | d42b839 | 2022-04-23 19:52:23 +0100 | [diff] [blame] | 493 | # define MATCH_WIDTH_HEIGHT(hist, w, h) \ |
Ernie Rael | 9f53e7b | 2022-04-17 18:27:49 +0100 | [diff] [blame] | 494 | (!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 |
| 503 | match_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 Rael | d42b839 | 2022-04-23 19:52:23 +0100 | [diff] [blame] | 510 | # ifdef ENABLE_RESIZE_HISTORY_LOG |
Ernie Rael | 9f53e7b | 2022-04-17 18:27:49 +0100 | [diff] [blame] | 511 | ch_log(NULL, "gui_gtk: discard seq %d, cur seq %d", |
| 512 | hist->seq, latest_resize_hist->seq); |
Ernie Rael | d42b839 | 2022-04-23 19:52:23 +0100 | [diff] [blame] | 513 | # endif |
Ernie Rael | 9f53e7b | 2022-04-17 18:27:49 +0100 | [diff] [blame] | 514 | hist->used = TRUE; |
| 515 | return TRUE; |
| 516 | } |
| 517 | return FALSE; |
| 518 | } |
| 519 | |
Ernie Rael | d42b839 | 2022-04-23 19:52:23 +0100 | [diff] [blame] | 520 | # if defined(EXITFREE) |
Ernie Rael | 9f53e7b | 2022-04-17 18:27:49 +0100 | [diff] [blame] | 521 | static void |
Yegappan Lakshmanan | a23a11b | 2023-02-21 14:27:41 +0000 | [diff] [blame] | 522 | free_all_resize_hist(void) |
Ernie Rael | 9f53e7b | 2022-04-17 18:27:49 +0100 | [diff] [blame] | 523 | { |
| 524 | clear_resize_hists(); |
| 525 | vim_free(latest_resize_hist); |
| 526 | } |
Ernie Rael | d42b839 | 2022-04-23 19:52:23 +0100 | [diff] [blame] | 527 | # endif |
Ernie Rael | 9f53e7b | 2022-04-17 18:27:49 +0100 | [diff] [blame] | 528 | #endif |
| 529 | |
| 530 | /* |
Chris Dalton | ee93e32 | 2021-11-23 12:27:48 +0000 | [diff] [blame] | 531 | * GTK doesn't set the GDK_BUTTON1_MASK state when dragging a touch. Add this |
| 532 | * state when dragging. |
| 533 | */ |
| 534 | static guint dragging_button_state = 0; |
| 535 | |
| 536 | /* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 537 | * 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 |
| 542 | gui_mch_prepare(int *argc, char **argv) |
| 543 | { |
| 544 | const cmdline_option_T *option; |
| 545 | int i = 0; |
| 546 | int len = 0; |
| 547 | |
Bram Moolenaar | f96ae0b | 2019-07-28 15:21:55 +0200 | [diff] [blame] | 548 | #if defined(USE_GNOME_SESSION) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 549 | /* |
| 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 Moolenaar | 9085f80 | 2009-06-03 14:20:21 +0000 | [diff] [blame] | 561 | { |
| 562 | abs_restart_command = (char *)vim_strsave(buf); |
| 563 | restart_command = abs_restart_command; |
| 564 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 565 | } |
| 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 Moolenaar | c799fe2 | 2019-05-28 23:08:19 +0200 | [diff] [blame] | 573 | gui_argv = ALLOC_MULT(char *, *argc + 1); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 574 | |
| 575 | g_return_if_fail(gui_argv != NULL); |
| 576 | |
| 577 | gui_argv[gui_argc++] = argv[i++]; |
| 578 | |
| 579 | while (i < *argc) |
| 580 | { |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 581 | // Don't waste CPU cycles on non-option arguments. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 582 | if (argv[i][0] != '-' && argv[i][0] != '+') |
| 583 | { |
| 584 | ++i; |
| 585 | continue; |
| 586 | } |
| 587 | |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 588 | // Look for argv[i] in cmdline_options[] table. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 589 | 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 Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 597 | // allow --foo=bar style |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 598 | if (argv[i][len] == '=' && (option->flags & ARG_HAS_VALUE)) |
| 599 | break; |
| 600 | #ifdef FEAT_NETBEANS_INTG |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 601 | // darn, -nb has non-standard syntax |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 602 | 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 Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 610 | // Replace the standard X arguments "-name" and "-display" |
| 611 | // with their GNU-style long option counterparts. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 612 | argv[i] = (char *)option->name; |
| 613 | break; |
| 614 | } |
| 615 | } |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 616 | if (option->name == NULL) // no match |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 617 | { |
| 618 | ++i; |
| 619 | continue; |
| 620 | } |
| 621 | |
| 622 | if (option->flags & ARG_FOR_GTK) |
| 623 | { |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 624 | // Move the argument into gui_argv, which |
| 625 | // will later be passed to gtk_init_check() |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 626 | gui_argv[gui_argc++] = argv[i]; |
| 627 | } |
| 628 | else |
| 629 | { |
| 630 | char *value = NULL; |
| 631 | |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 632 | // Extract the option's value if there is one. |
| 633 | // Accept both "--foo bar" and "--foo=bar" style. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 634 | 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 Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 642 | // Check for options handled by Vim itself |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 643 | 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 Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 667 | case ARG_ROLE: |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 668 | role_argument = value; // used later in gui_mch_open() |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 669 | break; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 670 | #ifdef FEAT_NETBEANS_INTG |
| 671 | case ARG_NETBEANS: |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 672 | gui.dofork = FALSE; // don't fork() when starting GUI |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 673 | netbeansArg = argv[i]; |
| 674 | break; |
| 675 | #endif |
| 676 | default: |
| 677 | break; |
| 678 | } |
| 679 | } |
| 680 | |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 681 | // 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 Moolenaar | 717e196 | 2016-08-10 21:28:44 +0200 | [diff] [blame] | 684 | if ((option->flags & ARG_NEEDS_GUI) |
| 685 | && gui_mch_early_init_check(FALSE) == OK) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 686 | gui.starting = TRUE; |
| 687 | |
| 688 | if (option->flags & ARG_KEEP) |
| 689 | ++i; |
| 690 | else |
| 691 | { |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 692 | // Remove the flag from the argument vector. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 693 | if (--*argc > i) |
| 694 | { |
| 695 | int n_strip = 1; |
| 696 | |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 697 | // Move the argument's value as well, if there is one. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 698 | 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 Moolenaar | f461c8e | 2005-06-25 23:04:51 +0000 | [diff] [blame] | 719 | #if defined(EXITFREE) || defined(PROTO) |
| 720 | void |
Bram Moolenaar | 66f948e | 2016-01-30 16:39:25 +0100 | [diff] [blame] | 721 | gui_mch_free_all(void) |
Bram Moolenaar | f461c8e | 2005-06-25 23:04:51 +0000 | [diff] [blame] | 722 | { |
| 723 | vim_free(gui_argv); |
Bram Moolenaar | f96ae0b | 2019-07-28 15:21:55 +0200 | [diff] [blame] | 724 | #if defined(USE_GNOME_SESSION) |
Bram Moolenaar | 9085f80 | 2009-06-03 14:20:21 +0000 | [diff] [blame] | 725 | vim_free(abs_restart_command); |
| 726 | #endif |
Ernie Rael | d42b839 | 2022-04-23 19:52:23 +0100 | [diff] [blame] | 727 | #ifdef TRACK_RESIZE_HISTORY |
Ernie Rael | 9f53e7b | 2022-04-17 18:27:49 +0100 | [diff] [blame] | 728 | free_all_resize_hist(); |
Ernie Rael | d42b839 | 2022-04-23 19:52:23 +0100 | [diff] [blame] | 729 | #endif |
Bram Moolenaar | f461c8e | 2005-06-25 23:04:51 +0000 | [diff] [blame] | 730 | } |
| 731 | #endif |
| 732 | |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 733 | #if !GTK_CHECK_VERSION(3,0,0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 734 | /* |
| 735 | * This should be maybe completely removed. |
| 736 | * Doesn't seem possible, since check_copy_area() relies on |
| 737 | * this information. --danielk |
| 738 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 739 | static gint |
Bram Moolenaar | b85cb21 | 2009-05-17 14:24:23 +0000 | [diff] [blame] | 740 | visibility_event(GtkWidget *widget UNUSED, |
| 741 | GdkEventVisibility *event, |
| 742 | gpointer data UNUSED) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 743 | { |
| 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 Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 755 | #endif // !GTK_CHECK_VERSION(3,0,0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 756 | |
| 757 | /* |
| 758 | * Redraw the corresponding portions of the screen. |
| 759 | */ |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 760 | #if GTK_CHECK_VERSION(3,0,0) |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 761 | static gboolean |
Bram Moolenaar | 8e31fd5 | 2016-06-04 22:18:13 +0200 | [diff] [blame] | 762 | draw_event(GtkWidget *widget UNUSED, |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 763 | cairo_t *cr, |
| 764 | gpointer user_data UNUSED) |
| 765 | { |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 766 | // Skip this when the GUI isn't set up yet, will redraw later. |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 767 | if (gui.starting) |
| 768 | return FALSE; |
| 769 | |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 770 | out_flush(); // make sure all output has been processed |
| 771 | // for GTK+ 3, may induce other draw events. |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 772 | |
| 773 | cairo_set_source_surface(cr, gui.surface, 0, 0); |
| 774 | |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 775 | { |
| 776 | cairo_rectangle_list_t *list = NULL; |
| 777 | |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 778 | list = cairo_copy_clip_rectangle_list(cr); |
| 779 | if (list->status != CAIRO_STATUS_CLIP_NOT_REPRESENTABLE) |
| 780 | { |
| 781 | int i; |
Bram Moolenaar | fe344a9 | 2017-02-23 12:20:35 +0100 | [diff] [blame] | 782 | |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 783 | for (i = 0; i < list->num_rectangles; i++) |
| 784 | { |
presuku | 9459b8d | 2021-11-16 20:03:56 +0000 | [diff] [blame] | 785 | const cairo_rectangle_t *rect = &list->rectangles[i]; |
Bram Moolenaar | 023930d | 2022-12-01 19:40:55 +0000 | [diff] [blame] | 786 | cairo_rectangle(cr, rect->x, rect->y, |
| 787 | rect->width, rect->height); |
presuku | 9459b8d | 2021-11-16 20:03:56 +0000 | [diff] [blame] | 788 | cairo_fill(cr); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 789 | } |
| 790 | } |
| 791 | cairo_rectangle_list_destroy(list); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 792 | } |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 793 | |
| 794 | return FALSE; |
| 795 | } |
lilydjwg | 6e0a18f | 2024-01-29 20:54:28 +0100 | [diff] [blame] | 796 | |
| 797 | # if GTK_CHECK_VERSION(3,10,0) |
| 798 | static gboolean |
| 799 | scale_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 Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 826 | #else // !GTK_CHECK_VERSION(3,0,0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 827 | static gint |
Bram Moolenaar | b85cb21 | 2009-05-17 14:24:23 +0000 | [diff] [blame] | 828 | expose_event(GtkWidget *widget UNUSED, |
| 829 | GdkEventExpose *event, |
| 830 | gpointer data UNUSED) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 831 | { |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 832 | // Skip this when the GUI isn't set up yet, will redraw later. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 833 | if (gui.starting) |
| 834 | return FALSE; |
| 835 | |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 836 | out_flush(); // make sure all output has been processed |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 837 | gui_redraw(event->area.x, event->area.y, |
| 838 | event->area.width, event->area.height); |
| 839 | |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 840 | // Clear the border areas if needed |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 841 | 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 Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 853 | #endif // !GTK_CHECK_VERSION(3,0,0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 854 | |
| 855 | #ifdef FEAT_CLIENTSERVER |
| 856 | /* |
| 857 | * Handle changes to the "Comm" property |
| 858 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 859 | static gint |
Bram Moolenaar | b85cb21 | 2009-05-17 14:24:23 +0000 | [diff] [blame] | 860 | property_event(GtkWidget *widget, |
| 861 | GdkEventProperty *event, |
| 862 | gpointer data UNUSED) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 863 | { |
| 864 | if (event->type == GDK_PROPERTY_NOTIFY |
| 865 | && event->state == (int)GDK_PROPERTY_NEW_VALUE |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 866 | && GDK_WINDOW_XID(event->window) == commWindow |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 867 | && GET_X_ATOM(event->atom) == commProperty) |
| 868 | { |
| 869 | XEvent xev; |
| 870 | |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 871 | // Translate to XLib |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 872 | xev.xproperty.type = PropertyNotify; |
| 873 | xev.xproperty.atom = commProperty; |
| 874 | xev.xproperty.window = commWindow; |
| 875 | xev.xproperty.state = PropertyNewValue; |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 876 | serverEventProc(GDK_WINDOW_XDISPLAY(gtk_widget_get_window(widget)), |
Ernie Rael | 9f53e7b | 2022-04-17 18:27:49 +0100 | [diff] [blame] | 877 | &xev, 0); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 878 | } |
| 879 | return FALSE; |
| 880 | } |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 881 | #endif // defined(FEAT_CLIENTSERVER) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 882 | |
Bram Moolenaar | 7ebf4e1 | 2018-08-07 20:01:40 +0200 | [diff] [blame] | 883 | /* |
| 884 | * Handle changes to the "Xft/DPI" setting |
| 885 | */ |
| 886 | static void |
| 887 | gtk_settings_xft_dpi_changed_cb(GtkSettings *gtk_settings UNUSED, |
Ernie Rael | 9f53e7b | 2022-04-17 18:27:49 +0100 | [diff] [blame] | 888 | GParamSpec *pspec UNUSED, |
| 889 | gpointer data UNUSED) |
Bram Moolenaar | 7ebf4e1 | 2018-08-07 20:01:40 +0200 | [diff] [blame] | 890 | { |
| 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 Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 906 | |
Bram Moolenaar | 4ab7968 | 2017-08-27 14:50:47 +0200 | [diff] [blame] | 907 | typedef gboolean timeout_cb_type; |
Bram Moolenaar | 4ab7968 | 2017-08-27 14:50:47 +0200 | [diff] [blame] | 908 | |
| 909 | /* |
| 910 | * Start a timer that will invoke the specified callback. |
| 911 | * Returns the ID of the timer. |
| 912 | */ |
| 913 | static guint |
| 914 | timeout_add(int time, timeout_cb_type (*callback)(gpointer), int *flagp) |
| 915 | { |
Bram Moolenaar | 4ab7968 | 2017-08-27 14:50:47 +0200 | [diff] [blame] | 916 | return g_timeout_add((guint)time, (GSourceFunc)callback, flagp); |
Bram Moolenaar | 4ab7968 | 2017-08-27 14:50:47 +0200 | [diff] [blame] | 917 | } |
| 918 | |
| 919 | static void |
| 920 | timeout_remove(guint timer) |
| 921 | { |
Bram Moolenaar | 4ab7968 | 2017-08-27 14:50:47 +0200 | [diff] [blame] | 922 | g_source_remove(timer); |
Bram Moolenaar | 4ab7968 | 2017-08-27 14:50:47 +0200 | [diff] [blame] | 923 | } |
| 924 | |
| 925 | |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 926 | ///////////////////////////////////////////////////////////////////////////// |
| 927 | // Focus handlers: |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 928 | |
| 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 | |
| 941 | static int blink_state = BLINK_NONE; |
| 942 | static long_u blink_waittime = 700; |
| 943 | static long_u blink_ontime = 400; |
| 944 | static long_u blink_offtime = 250; |
| 945 | static guint blink_timer = 0; |
| 946 | |
Bram Moolenaar | 703a804 | 2016-06-04 16:24:32 +0200 | [diff] [blame] | 947 | int |
| 948 | gui_mch_is_blinking(void) |
| 949 | { |
| 950 | return blink_state != BLINK_NONE; |
| 951 | } |
| 952 | |
Bram Moolenaar | 9d5d3c9 | 2016-07-07 16:43:02 +0200 | [diff] [blame] | 953 | int |
| 954 | gui_mch_is_blink_off(void) |
| 955 | { |
| 956 | return blink_state == BLINK_OFF; |
| 957 | } |
| 958 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 959 | void |
| 960 | gui_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 Moolenaar | 1dd45fb | 2018-01-31 21:10:01 +0100 | [diff] [blame] | 971 | gui_mch_stop_blink(int may_call_gui_update_cursor) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 972 | { |
| 973 | if (blink_timer) |
| 974 | { |
Bram Moolenaar | 4ab7968 | 2017-08-27 14:50:47 +0200 | [diff] [blame] | 975 | timeout_remove(blink_timer); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 976 | blink_timer = 0; |
| 977 | } |
Bram Moolenaar | 1dd45fb | 2018-01-31 21:10:01 +0100 | [diff] [blame] | 978 | if (blink_state == BLINK_OFF && may_call_gui_update_cursor) |
Bram Moolenaar | da3a77d | 2016-07-10 23:16:09 +0200 | [diff] [blame] | 979 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 980 | gui_update_cursor(TRUE, FALSE); |
presuku | 9459b8d | 2021-11-16 20:03:56 +0000 | [diff] [blame] | 981 | #if !GTK_CHECK_VERSION(3,0,0) |
Bram Moolenaar | da3a77d | 2016-07-10 23:16:09 +0200 | [diff] [blame] | 982 | gui_mch_flush(); |
presuku | 9459b8d | 2021-11-16 20:03:56 +0000 | [diff] [blame] | 983 | #endif |
Bram Moolenaar | da3a77d | 2016-07-10 23:16:09 +0200 | [diff] [blame] | 984 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 985 | blink_state = BLINK_NONE; |
| 986 | } |
| 987 | |
Bram Moolenaar | 4ab7968 | 2017-08-27 14:50:47 +0200 | [diff] [blame] | 988 | static timeout_cb_type |
Bram Moolenaar | b85cb21 | 2009-05-17 14:24:23 +0000 | [diff] [blame] | 989 | blink_cb(gpointer data UNUSED) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 990 | { |
| 991 | if (blink_state == BLINK_ON) |
| 992 | { |
| 993 | gui_undraw_cursor(); |
| 994 | blink_state = BLINK_OFF; |
Bram Moolenaar | 4ab7968 | 2017-08-27 14:50:47 +0200 | [diff] [blame] | 995 | blink_timer = timeout_add(blink_offtime, blink_cb, NULL); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 996 | } |
| 997 | else |
| 998 | { |
| 999 | gui_update_cursor(TRUE, FALSE); |
| 1000 | blink_state = BLINK_ON; |
Bram Moolenaar | 4ab7968 | 2017-08-27 14:50:47 +0200 | [diff] [blame] | 1001 | blink_timer = timeout_add(blink_ontime, blink_cb, NULL); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1002 | } |
presuku | 9459b8d | 2021-11-16 20:03:56 +0000 | [diff] [blame] | 1003 | #if !GTK_CHECK_VERSION(3,0,0) |
Bram Moolenaar | da3a77d | 2016-07-10 23:16:09 +0200 | [diff] [blame] | 1004 | gui_mch_flush(); |
presuku | 9459b8d | 2021-11-16 20:03:56 +0000 | [diff] [blame] | 1005 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1006 | |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 1007 | return FALSE; // don't happen again |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1008 | } |
| 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 |
| 1015 | gui_mch_start_blink(void) |
| 1016 | { |
| 1017 | if (blink_timer) |
Bram Moolenaar | 7bcdb7d | 2014-04-06 21:08:45 +0200 | [diff] [blame] | 1018 | { |
Bram Moolenaar | 4ab7968 | 2017-08-27 14:50:47 +0200 | [diff] [blame] | 1019 | timeout_remove(blink_timer); |
Bram Moolenaar | 7bcdb7d | 2014-04-06 21:08:45 +0200 | [diff] [blame] | 1020 | blink_timer = 0; |
| 1021 | } |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 1022 | // Only switch blinking on if none of the times is zero |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1023 | if (blink_waittime && blink_ontime && blink_offtime && gui.in_focus) |
| 1024 | { |
Bram Moolenaar | 4ab7968 | 2017-08-27 14:50:47 +0200 | [diff] [blame] | 1025 | blink_timer = timeout_add(blink_waittime, blink_cb, NULL); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1026 | blink_state = BLINK_ON; |
| 1027 | gui_update_cursor(TRUE, FALSE); |
presuku | 9459b8d | 2021-11-16 20:03:56 +0000 | [diff] [blame] | 1028 | #if !GTK_CHECK_VERSION(3,0,0) |
Bram Moolenaar | da3a77d | 2016-07-10 23:16:09 +0200 | [diff] [blame] | 1029 | gui_mch_flush(); |
presuku | 9459b8d | 2021-11-16 20:03:56 +0000 | [diff] [blame] | 1030 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1031 | } |
| 1032 | } |
| 1033 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1034 | static gint |
Bram Moolenaar | b85cb21 | 2009-05-17 14:24:23 +0000 | [diff] [blame] | 1035 | enter_notify_event(GtkWidget *widget UNUSED, |
| 1036 | GdkEventCrossing *event UNUSED, |
| 1037 | gpointer data UNUSED) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1038 | { |
| 1039 | if (blink_state == BLINK_NONE) |
| 1040 | gui_mch_start_blink(); |
| 1041 | |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 1042 | // make sure keyboard input goes there |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 1043 | if (gtk_socket_id == 0 || !gtk_widget_has_focus(gui.drawarea)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1044 | gtk_widget_grab_focus(gui.drawarea); |
| 1045 | |
| 1046 | return FALSE; |
| 1047 | } |
| 1048 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1049 | static gint |
Bram Moolenaar | b85cb21 | 2009-05-17 14:24:23 +0000 | [diff] [blame] | 1050 | leave_notify_event(GtkWidget *widget UNUSED, |
| 1051 | GdkEventCrossing *event UNUSED, |
| 1052 | gpointer data UNUSED) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1053 | { |
| 1054 | if (blink_state != BLINK_NONE) |
Bram Moolenaar | 1dd45fb | 2018-01-31 21:10:01 +0100 | [diff] [blame] | 1055 | gui_mch_stop_blink(TRUE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1056 | |
| 1057 | return FALSE; |
| 1058 | } |
| 1059 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1060 | static gint |
Bram Moolenaar | b85cb21 | 2009-05-17 14:24:23 +0000 | [diff] [blame] | 1061 | focus_in_event(GtkWidget *widget, |
| 1062 | GdkEventFocus *event UNUSED, |
| 1063 | gpointer data UNUSED) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1064 | { |
| 1065 | gui_focus_change(TRUE); |
| 1066 | |
| 1067 | if (blink_state == BLINK_NONE) |
| 1068 | gui_mch_start_blink(); |
| 1069 | |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 1070 | // make sure keyboard input goes to the draw area (if this is focus for a |
| 1071 | // window) |
Bram Moolenaar | b3656ed | 2006-03-20 21:59:49 +0000 | [diff] [blame] | 1072 | if (widget != gui.drawarea) |
Bram Moolenaar | c9b4b05 | 2006-04-30 18:54:39 +0000 | [diff] [blame] | 1073 | gtk_widget_grab_focus(gui.drawarea); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1074 | |
| 1075 | return TRUE; |
| 1076 | } |
| 1077 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1078 | static gint |
Bram Moolenaar | b85cb21 | 2009-05-17 14:24:23 +0000 | [diff] [blame] | 1079 | focus_out_event(GtkWidget *widget UNUSED, |
Bram Moolenaar | cc448b3 | 2010-07-14 16:52:17 +0200 | [diff] [blame] | 1080 | GdkEventFocus *event UNUSED, |
Bram Moolenaar | b85cb21 | 2009-05-17 14:24:23 +0000 | [diff] [blame] | 1081 | gpointer data UNUSED) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1082 | { |
| 1083 | gui_focus_change(FALSE); |
| 1084 | |
| 1085 | if (blink_state != BLINK_NONE) |
Bram Moolenaar | 1dd45fb | 2018-01-31 21:10:01 +0100 | [diff] [blame] | 1086 | gui_mch_stop_blink(TRUE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1087 | |
| 1088 | return TRUE; |
| 1089 | } |
| 1090 | |
| 1091 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1092 | /* |
| 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 Moolenaar | f4ae6b2 | 2020-05-30 19:52:46 +0200 | [diff] [blame] | 1097 | * event->string is evil; see here why: |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1098 | * http://developer.gnome.org/doc/API/2.0/gdk/gdk-Event-Structures.html#GdkEventKey |
| 1099 | */ |
| 1100 | static int |
Bram Moolenaar | f4ae6b2 | 2020-05-30 19:52:46 +0200 | [diff] [blame] | 1101 | keyval_to_string(unsigned int keyval, char_u *string) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1102 | { |
| 1103 | int len; |
| 1104 | guint32 uc; |
| 1105 | |
| 1106 | uc = gdk_keyval_to_unicode(keyval); |
| 1107 | if (uc != 0) |
| 1108 | { |
Bram Moolenaar | f4ae6b2 | 2020-05-30 19:52:46 +0200 | [diff] [blame] | 1109 | // 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 Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1112 | } |
| 1113 | else |
| 1114 | { |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 1115 | // 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 Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1118 | 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 Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1142 | |
Bram Moolenaar | 19a09a1 | 2005-03-04 23:39:37 +0000 | [diff] [blame] | 1143 | static int |
| 1144 | modifiers_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 Moolenaar | 580061a | 2010-08-13 13:57:13 +0200 | [diff] [blame] | 1154 | #if GTK_CHECK_VERSION(2,10,0) |
Casey Tucker | 92e90a1 | 2024-01-25 22:44:00 +0100 | [diff] [blame] | 1155 | if (state & GDK_META_MASK) |
| 1156 | modifiers |= MOD_MASK_META; |
Bram Moolenaar | 30bb414 | 2010-05-17 22:07:15 +0200 | [diff] [blame] | 1157 | if (state & GDK_SUPER_MASK) |
Casey Tucker | 92e90a1 | 2024-01-25 22:44:00 +0100 | [diff] [blame] | 1158 | modifiers |= MOD_MASK_CMD; |
| 1159 | #else |
Bram Moolenaar | 19a09a1 | 2005-03-04 23:39:37 +0000 | [diff] [blame] | 1160 | if (state & GDK_MOD4_MASK) |
Casey Tucker | 92e90a1 | 2024-01-25 22:44:00 +0100 | [diff] [blame] | 1161 | modifiers |= MOD_MASK_CMD; |
| 1162 | #endif |
Bram Moolenaar | 19a09a1 | 2005-03-04 23:39:37 +0000 | [diff] [blame] | 1163 | |
| 1164 | return modifiers; |
| 1165 | } |
| 1166 | |
| 1167 | static int |
| 1168 | modifiers_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 Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1182 | /* |
| 1183 | * Main keyboard handler: |
| 1184 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1185 | static gint |
Bram Moolenaar | b85cb21 | 2009-05-17 14:24:23 +0000 | [diff] [blame] | 1186 | key_press_event(GtkWidget *widget UNUSED, |
| 1187 | GdkEventKey *event, |
| 1188 | gpointer data UNUSED) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1189 | { |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 1190 | // 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 Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1192 | char_u string[32], string2[32]; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1193 | guint key_sym; |
| 1194 | int len; |
| 1195 | int i; |
| 1196 | int modifiers; |
| 1197 | int key; |
| 1198 | guint state; |
| 1199 | char_u *s, *d; |
Anton Sharonov | 4dd9252 | 2022-07-04 10:47:31 +0100 | [diff] [blame] | 1200 | int ctrl_prefix_added = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1201 | |
Bram Moolenaar | 20892c1 | 2011-06-26 04:49:00 +0200 | [diff] [blame] | 1202 | gui.event_time = event->time; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1203 | key_sym = event->keyval; |
| 1204 | state = event->state; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1205 | |
| 1206 | #ifdef FEAT_XIM |
| 1207 | if (xim_queue_key_press_event(event, TRUE)) |
| 1208 | return TRUE; |
| 1209 | #endif |
| 1210 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1211 | #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 Moolenaar | f4ae6b2 | 2020-05-30 19:52:46 +0200 | [diff] [blame] | 1222 | len = keyval_to_string(key_sym, string2); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1223 | |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 1224 | // Careful: convert_input() doesn't handle the NUL character. |
| 1225 | // No need to convert pure ASCII anyway, thus the len > 1 check. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1226 | if (len > 1 && input_conv.vc_type != CONV_NONE) |
| 1227 | len = convert_input(string2, len, sizeof(string2)); |
| 1228 | |
| 1229 | s = string2; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1230 | 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 Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 1236 | // Turn CSI into K_CSI. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1237 | *d++ = KS_EXTRA; |
| 1238 | *d++ = (int)KE_CSI; |
| 1239 | } |
| 1240 | } |
| 1241 | len = d - string; |
| 1242 | } |
| 1243 | |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 1244 | // Shift-Tab results in Left_Tab, but we want <S-Tab> |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1245 | if (key_sym == GDK_ISO_Left_Tab) |
| 1246 | { |
| 1247 | key_sym = GDK_Tab; |
| 1248 | state |= GDK_SHIFT_MASK; |
| 1249 | } |
| 1250 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1251 | #ifdef FEAT_MENU |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 1252 | // 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 Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1254 | 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 Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 1260 | // 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 Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1262 | return FALSE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1263 | #endif |
| 1264 | |
Bram Moolenaar | fd615a3 | 2020-05-16 14:01:51 +0200 | [diff] [blame] | 1265 | // 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 Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1268 | |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 1269 | // Check for special keys. Also do this when len == 1 (key has an ASCII |
| 1270 | // value) to detect backspace, delete and keypad keys. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1271 | 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 Moolenaar | 4c99e62 | 2022-07-04 19:58:17 +0100 | [diff] [blame] | 1286 | #ifdef GDK_KEY_dead_circumflex |
Anton Sharonov | 4dd9252 | 2022-07-04 10:47:31 +0100 | [diff] [blame] | 1287 | // 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 Moolenaar | 4c99e62 | 2022-07-04 19:58:17 +0100 | [diff] [blame] | 1300 | #endif |
Anton Sharonov | 4dd9252 | 2022-07-04 10:47:31 +0100 | [diff] [blame] | 1301 | |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 1302 | if (len == 0) // Unrecognized key |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1303 | return TRUE; |
| 1304 | |
Bram Moolenaar | fd615a3 | 2020-05-16 14:01:51 +0200 | [diff] [blame] | 1305 | // 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 Moolenaar | 820ffa5 | 2020-06-21 15:09:14 +0200 | [diff] [blame] | 1309 | { |
| 1310 | string[len] = NUL; |
| 1311 | key = mb_ptr2char(string); |
| 1312 | } |
Bram Moolenaar | fd615a3 | 2020-05-16 14:01:51 +0200 | [diff] [blame] | 1313 | |
Bram Moolenaar | ef6746f | 2020-06-20 14:43:23 +0200 | [diff] [blame] | 1314 | // Handle modifiers. |
| 1315 | modifiers = modifiers_gdk2vim(state); |
| 1316 | |
| 1317 | // Recognize special keys. |
Bram Moolenaar | fd615a3 | 2020-05-16 14:01:51 +0200 | [diff] [blame] | 1318 | key = simplify_key(key, &modifiers); |
| 1319 | if (key == CSI) |
| 1320 | key = K_CSI; |
| 1321 | if (IS_SPECIAL(key)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1322 | { |
Bram Moolenaar | fd615a3 | 2020-05-16 14:01:51 +0200 | [diff] [blame] | 1323 | string[0] = CSI; |
| 1324 | string[1] = K_SECOND(key); |
| 1325 | string[2] = K_THIRD(key); |
| 1326 | len = 3; |
| 1327 | } |
| 1328 | else |
| 1329 | { |
Bram Moolenaar | 4e2114e | 2020-10-07 16:12:37 +0200 | [diff] [blame] | 1330 | // Some keys need adjustment when the Ctrl modifier is used. |
| 1331 | key = may_adjust_key_for_ctrl(modifiers, key); |
Bram Moolenaar | ef6746f | 2020-06-20 14:43:23 +0200 | [diff] [blame] | 1332 | |
Bram Moolenaar | 4e2114e | 2020-10-07 16:12:37 +0200 | [diff] [blame] | 1333 | // May remove the Shift modifier if it's included in the key. |
Bram Moolenaar | ef6746f | 2020-06-20 14:43:23 +0200 | [diff] [blame] | 1334 | modifiers = may_remove_shift_modifier(modifiers, key); |
| 1335 | |
Bram Moolenaar | 820ffa5 | 2020-06-21 15:09:14 +0200 | [diff] [blame] | 1336 | len = mb_char2bytes(key, string); |
Bram Moolenaar | fd615a3 | 2020-05-16 14:01:51 +0200 | [diff] [blame] | 1337 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1338 | |
Bram Moolenaar | fd615a3 | 2020-05-16 14:01:51 +0200 | [diff] [blame] | 1339 | 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 Sharonov | 4dd9252 | 2022-07-04 10:47:31 +0100 | [diff] [blame] | 1345 | if (modifiers == 0x4) |
| 1346 | ctrl_prefix_added = 1; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1347 | } |
| 1348 | |
Bram Moolenaar | 4e1d8bd | 2020-08-01 13:10:14 +0200 | [diff] [blame] | 1349 | // Check if the key interrupts. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1350 | { |
Bram Moolenaar | 4e1d8bd | 2020-08-01 13:10:14 +0200 | [diff] [blame] | 1351 | 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 Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1359 | } |
| 1360 | |
Anton Sharonov | 4dd9252 | 2022-07-04 10:47:31 +0100 | [diff] [blame] | 1361 | // 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 Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1370 | add_to_input_buf(string, len); |
| 1371 | |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 1372 | // blank out the pointer if necessary |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1373 | if (p_mh) |
| 1374 | gui_mch_mousehide(TRUE); |
| 1375 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1376 | return TRUE; |
| 1377 | } |
| 1378 | |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 1379 | #if defined(FEAT_XIM) || GTK_CHECK_VERSION(3,0,0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1380 | static gboolean |
Bram Moolenaar | b85cb21 | 2009-05-17 14:24:23 +0000 | [diff] [blame] | 1381 | key_release_event(GtkWidget *widget UNUSED, |
Bram Moolenaar | 0a748d9 | 2022-04-28 15:39:08 +0100 | [diff] [blame] | 1382 | GdkEventKey *event UNUSED, |
Bram Moolenaar | b85cb21 | 2009-05-17 14:24:23 +0000 | [diff] [blame] | 1383 | gpointer data UNUSED) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1384 | { |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 1385 | # if defined(FEAT_XIM) |
Bram Moolenaar | 20892c1 | 2011-06-26 04:49:00 +0200 | [diff] [blame] | 1386 | gui.event_time = event->time; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1387 | /* |
| 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 Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 1393 | # else |
| 1394 | return TRUE; |
| 1395 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1396 | } |
| 1397 | #endif |
| 1398 | |
| 1399 | |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 1400 | ///////////////////////////////////////////////////////////////////////////// |
| 1401 | // Selection handlers: |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1402 | |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 1403 | // Remember when clip_lose_selection was called from here, we must not call |
| 1404 | // gtk_selection_owner_set() then. |
Bram Moolenaar | 29dfa5a | 2018-03-20 21:24:45 +0100 | [diff] [blame] | 1405 | static int in_selection_clear_event = FALSE; |
| 1406 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1407 | static gint |
Bram Moolenaar | b85cb21 | 2009-05-17 14:24:23 +0000 | [diff] [blame] | 1408 | selection_clear_event(GtkWidget *widget UNUSED, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1409 | GdkEventSelection *event, |
Bram Moolenaar | b85cb21 | 2009-05-17 14:24:23 +0000 | [diff] [blame] | 1410 | gpointer user_data UNUSED) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1411 | { |
Bram Moolenaar | 29dfa5a | 2018-03-20 21:24:45 +0100 | [diff] [blame] | 1412 | in_selection_clear_event = TRUE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1413 | if (event->selection == clip_plus.gtk_sel_atom) |
| 1414 | clip_lose_selection(&clip_plus); |
| 1415 | else |
| 1416 | clip_lose_selection(&clip_star); |
Bram Moolenaar | 29dfa5a | 2018-03-20 21:24:45 +0100 | [diff] [blame] | 1417 | in_selection_clear_event = FALSE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1418 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1419 | return TRUE; |
| 1420 | } |
| 1421 | |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 1422 | #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 Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1425 | static int received_selection = RS_NONE; |
| 1426 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1427 | static void |
Bram Moolenaar | b85cb21 | 2009-05-17 14:24:23 +0000 | [diff] [blame] | 1428 | selection_received_cb(GtkWidget *widget UNUSED, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1429 | GtkSelectionData *data, |
Bram Moolenaar | b85cb21 | 2009-05-17 14:24:23 +0000 | [diff] [blame] | 1430 | guint time_ UNUSED, |
| 1431 | gpointer user_data UNUSED) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1432 | { |
Bram Moolenaar | 0554fa4 | 2019-06-14 21:36:54 +0200 | [diff] [blame] | 1433 | Clipboard_T *cbd; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1434 | char_u *text; |
| 1435 | char_u *tmpbuf = NULL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1436 | guchar *tmpbuf_utf8 = NULL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1437 | int len; |
Bram Moolenaar | d44347f | 2011-06-19 01:14:29 +0200 | [diff] [blame] | 1438 | int motion_type = MAUTO; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1439 | |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 1440 | if (gtk_selection_data_get_selection(data) == clip_plus.gtk_sel_atom) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1441 | cbd = &clip_plus; |
| 1442 | else |
| 1443 | cbd = &clip_star; |
| 1444 | |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 1445 | text = (char_u *)gtk_selection_data_get_data(data); |
| 1446 | len = gtk_selection_data_get_length(data); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1447 | |
| 1448 | if (text == NULL || len <= 0) |
| 1449 | { |
| 1450 | received_selection = RS_FAIL; |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 1451 | // clip_free_selection(cbd); ??? |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1452 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1453 | return; |
| 1454 | } |
| 1455 | |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 1456 | if (gtk_selection_data_get_data_type(data) == vim_atom) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1457 | { |
| 1458 | motion_type = *text++; |
| 1459 | --len; |
| 1460 | } |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 1461 | else if (gtk_selection_data_get_data_type(data) == vimenc_atom) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1462 | { |
| 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 Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 1473 | // If the encoding of the text is different from 'encoding', attempt |
| 1474 | // converting it. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1475 | 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 Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1485 | |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 1486 | // gtk_selection_data_get_text() handles all the nasty details |
| 1487 | // and targets and encodings etc. This rocks so hard. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1488 | 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 Moolenaar | 3a6eaa5 | 2009-06-16 13:23:06 +0000 | [diff] [blame] | 1503 | else if (len >= 2 && text[0] == 0xff && text[1] == 0xfe) |
| 1504 | { |
| 1505 | vimconv_T conv; |
| 1506 | |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 1507 | // UTF-16, we get this for HTML |
Bram Moolenaar | 3a6eaa5 | 2009-06-16 13:23:06 +0000 | [diff] [blame] | 1508 | 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 Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1521 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1522 | |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 1523 | // Chop off any trailing NUL bytes. OpenOffice sends these. |
Bram Moolenaar | a76638f | 2010-06-05 12:49:46 +0200 | [diff] [blame] | 1524 | while (len > 0 && text[len - 1] == NUL) |
| 1525 | --len; |
| 1526 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1527 | clip_yank_selection(motion_type, text, (long)len, cbd); |
| 1528 | received_selection = RS_OK; |
| 1529 | vim_free(tmpbuf); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1530 | g_free(tmpbuf_utf8); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1531 | } |
| 1532 | |
| 1533 | /* |
| 1534 | * Prepare our selection data for passing it to the external selection |
| 1535 | * client. |
| 1536 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1537 | static void |
Bram Moolenaar | b85cb21 | 2009-05-17 14:24:23 +0000 | [diff] [blame] | 1538 | selection_get_cb(GtkWidget *widget UNUSED, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1539 | GtkSelectionData *selection_data, |
| 1540 | guint info, |
Bram Moolenaar | b85cb21 | 2009-05-17 14:24:23 +0000 | [diff] [blame] | 1541 | guint time_ UNUSED, |
| 1542 | gpointer user_data UNUSED) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1543 | { |
| 1544 | char_u *string; |
| 1545 | char_u *tmpbuf; |
| 1546 | long_u tmplen; |
| 1547 | int length; |
| 1548 | int motion_type; |
| 1549 | GdkAtom type; |
Bram Moolenaar | 0554fa4 | 2019-06-14 21:36:54 +0200 | [diff] [blame] | 1550 | Clipboard_T *cbd; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1551 | |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 1552 | if (gtk_selection_data_get_selection(selection_data) |
| 1553 | == clip_plus.gtk_sel_atom) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1554 | cbd = &clip_plus; |
| 1555 | else |
| 1556 | cbd = &clip_star; |
| 1557 | |
| 1558 | if (!cbd->owned) |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 1559 | return; // Shouldn't ever happen |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1560 | |
| 1561 | if (info != (guint)TARGET_STRING |
Bram Moolenaar | 3a6eaa5 | 2009-06-16 13:23:06 +0000 | [diff] [blame] | 1562 | && (!clip_html || info != (guint)TARGET_HTML) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1563 | && info != (guint)TARGET_UTF8_STRING |
| 1564 | && info != (guint)TARGET_VIMENC |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1565 | && info != (guint)TARGET_VIM |
| 1566 | && info != (guint)TARGET_COMPOUND_TEXT |
lilydjwg | 94ff09a | 2024-01-29 20:14:01 +0100 | [diff] [blame] | 1567 | && info != (guint)TARGET_TEXT_PLAIN |
| 1568 | && info != (guint)TARGET_TEXT_PLAIN_UTF8 |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1569 | && info != (guint)TARGET_TEXT) |
| 1570 | return; |
| 1571 | |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 1572 | // get the selection from the '*'/'+' register |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1573 | clip_get_selection(cbd); |
| 1574 | |
| 1575 | motion_type = clip_convert_selection(&string, &tmplen, cbd); |
| 1576 | if (motion_type < 0 || string == NULL) |
| 1577 | return; |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 1578 | // 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 Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1581 | length = MIN(tmplen, (long_u)(G_MAXINT - 1)); |
| 1582 | |
| 1583 | if (info == (guint)TARGET_VIM) |
| 1584 | { |
Bram Moolenaar | 964b374 | 2019-05-24 18:54:09 +0200 | [diff] [blame] | 1585 | tmpbuf = alloc(length + 1); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1586 | if (tmpbuf != NULL) |
| 1587 | { |
| 1588 | tmpbuf[0] = motion_type; |
| 1589 | mch_memmove(tmpbuf + 1, string, (size_t)length); |
| 1590 | } |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 1591 | // For our own format, the first byte contains the motion type |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1592 | ++length; |
| 1593 | vim_free(string); |
| 1594 | string = tmpbuf; |
| 1595 | type = vim_atom; |
| 1596 | } |
| 1597 | |
Bram Moolenaar | 3a6eaa5 | 2009-06-16 13:23:06 +0000 | [diff] [blame] | 1598 | else if (info == (guint)TARGET_HTML) |
| 1599 | { |
| 1600 | vimconv_T conv; |
| 1601 | |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 1602 | // Since we get utf-16, we probably should set it as well. |
Bram Moolenaar | 3a6eaa5 | 2009-06-16 13:23:06 +0000 | [diff] [blame] | 1603 | 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 Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 1613 | // Prepend the BOM: "fffe" |
Bram Moolenaar | 3a6eaa5 | 2009-06-16 13:23:06 +0000 | [diff] [blame] | 1614 | if (string != NULL) |
| 1615 | { |
| 1616 | tmpbuf = alloc(length + 2); |
Bram Moolenaar | 6ee9658 | 2019-04-27 22:06:37 +0200 | [diff] [blame] | 1617 | 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 Moolenaar | 3a6eaa5 | 2009-06-16 13:23:06 +0000 | [diff] [blame] | 1626 | |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 1627 | #if !GTK_CHECK_VERSION(3,0,0) |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 1628 | // Looks redundant even for GTK2 because these values are |
| 1629 | // overwritten by gtk_selection_data_set() that follows. |
Bram Moolenaar | 3a6eaa5 | 2009-06-16 13:23:06 +0000 | [diff] [blame] | 1630 | selection_data->type = selection_data->target; |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 1631 | selection_data->format = 16; // 16 bits per char |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 1632 | #endif |
Bram Moolenaar | 3a6eaa5 | 2009-06-16 13:23:06 +0000 | [diff] [blame] | 1633 | gtk_selection_data_set(selection_data, html_atom, 16, |
| 1634 | string, length); |
| 1635 | vim_free(string); |
| 1636 | } |
| 1637 | return; |
| 1638 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1639 | else if (info == (guint)TARGET_VIMENC) |
| 1640 | { |
| 1641 | int l = STRLEN(p_enc); |
| 1642 | |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 1643 | // contents: motion_type 'encoding' NUL text |
Bram Moolenaar | 964b374 | 2019-05-24 18:54:09 +0200 | [diff] [blame] | 1644 | tmpbuf = alloc(length + l + 2); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1645 | 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 Moolenaar | 6ee9658 | 2019-04-27 22:06:37 +0200 | [diff] [blame] | 1650 | length += l + 2; |
| 1651 | vim_free(string); |
| 1652 | string = tmpbuf; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1653 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1654 | type = vimenc_atom; |
| 1655 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1656 | |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 1657 | // 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 Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1659 | 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 Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 1669 | // Validate the string to avoid runtime warnings |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1670 | 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 Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1678 | |
| 1679 | if (string != NULL) |
| 1680 | { |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 1681 | #if !GTK_CHECK_VERSION(3,0,0) |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 1682 | // Looks redundant even for GTK2 because these values are |
| 1683 | // overwritten by gtk_selection_data_set() that follows. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1684 | selection_data->type = selection_data->target; |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 1685 | selection_data->format = 8; // 8 bits per char |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 1686 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1687 | gtk_selection_data_set(selection_data, type, 8, string, length); |
| 1688 | vim_free(string); |
| 1689 | } |
| 1690 | } |
| 1691 | |
| 1692 | /* |
Bram Moolenaar | 2969570 | 2012-05-18 17:03:18 +0200 | [diff] [blame] | 1693 | * 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 Moolenaar | 717e196 | 2016-08-10 21:28:44 +0200 | [diff] [blame] | 1698 | gui_mch_early_init_check(int give_message) |
Bram Moolenaar | 2969570 | 2012-05-18 17:03:18 +0200 | [diff] [blame] | 1699 | { |
lilydjwg | 6e0a18f | 2024-01-29 20:54:28 +0100 | [diff] [blame] | 1700 | char_u *p, *q; |
Bram Moolenaar | 2969570 | 2012-05-18 17:03:18 +0200 | [diff] [blame] | 1701 | |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 1702 | // Guess that when $DISPLAY isn't set the GUI can't start. |
Bram Moolenaar | 2969570 | 2012-05-18 17:03:18 +0200 | [diff] [blame] | 1703 | p = mch_getenv((char_u *)"DISPLAY"); |
lilydjwg | 6e0a18f | 2024-01-29 20:54:28 +0100 | [diff] [blame] | 1704 | q = mch_getenv((char_u *)"WAYLAND_DISPLAY"); |
| 1705 | if ((p == NULL || *p == NUL) && (q == NULL || *q == NUL)) |
Bram Moolenaar | 2969570 | 2012-05-18 17:03:18 +0200 | [diff] [blame] | 1706 | { |
| 1707 | gui.dying = TRUE; |
Bram Moolenaar | 717e196 | 2016-08-10 21:28:44 +0200 | [diff] [blame] | 1708 | if (give_message) |
Bram Moolenaar | 460ae5d | 2022-01-01 14:19:49 +0000 | [diff] [blame] | 1709 | emsg(_((char *)e_cannot_open_display)); |
Bram Moolenaar | 2969570 | 2012-05-18 17:03:18 +0200 | [diff] [blame] | 1710 | 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 Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1718 | * Return OK or FAIL. |
| 1719 | */ |
| 1720 | int |
| 1721 | gui_mch_init_check(void) |
| 1722 | { |
Bram Moolenaar | 3a466a8 | 2016-01-19 17:47:25 +0100 | [diff] [blame] | 1723 | #ifdef USE_GRESOURCE |
| 1724 | static int res_registered = FALSE; |
| 1725 | |
| 1726 | if (!res_registered) |
| 1727 | { |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 1728 | // Call this function in the GUI process; otherwise, the resources |
| 1729 | // won't be available. Don't call it twice. |
Bram Moolenaar | 3a466a8 | 2016-01-19 17:47:25 +0100 | [diff] [blame] | 1730 | res_registered = TRUE; |
| 1731 | gui_gtk_register_resource(); |
| 1732 | } |
| 1733 | #endif |
| 1734 | |
Bram Moolenaar | f80663f | 2016-04-05 21:56:06 +0200 | [diff] [blame] | 1735 | #if GTK_CHECK_VERSION(3,10,0) |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 1736 | // Vim currently assumes that Gtk means X11, so it cannot use native Gtk |
| 1737 | // support for other backends such as Wayland. |
lilydjwg | 6e0a18f | 2024-01-29 20:54:28 +0100 | [diff] [blame] | 1738 | // |
| 1739 | // Use an environment variable to enable unfinished Wayland support. |
| 1740 | if (getenv("GVIM_ENABLE_WAYLAND") == NULL) |
| 1741 | gdk_set_allowed_backends ("x11"); |
Bram Moolenaar | f80663f | 2016-04-05 21:56:06 +0200 | [diff] [blame] | 1742 | #endif |
| 1743 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1744 | #ifdef FEAT_GUI_GNOME |
| 1745 | if (gtk_socket_id == 0) |
| 1746 | using_gnome = 1; |
| 1747 | #endif |
| 1748 | |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 1749 | // 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 Moolenaar | 8dd7901 | 2013-05-21 12:52:04 +0200 | [diff] [blame] | 1752 | g_set_prgname("gvim"); |
| 1753 | |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 1754 | // Don't use gtk_init() or gnome_init(), it exits on failure. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1755 | if (!gtk_init_check(&gui_argc, &gui_argv)) |
| 1756 | { |
| 1757 | gui.dying = TRUE; |
Bram Moolenaar | 460ae5d | 2022-01-01 14:19:49 +0000 | [diff] [blame] | 1758 | emsg(_((char *)e_cannot_open_display)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1759 | return FAIL; |
| 1760 | } |
| 1761 | |
| 1762 | return OK; |
| 1763 | } |
| 1764 | |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 1765 | ///////////////////////////////////////////////////////////////////////////// |
| 1766 | // Mouse handling callbacks |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1767 | |
| 1768 | |
| 1769 | static guint mouse_click_timer = 0; |
| 1770 | static int mouse_timed_out = TRUE; |
| 1771 | |
| 1772 | /* |
| 1773 | * Timer used to recognize multiple clicks of the mouse button |
| 1774 | */ |
Bram Moolenaar | 4ab7968 | 2017-08-27 14:50:47 +0200 | [diff] [blame] | 1775 | static timeout_cb_type |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1776 | mouse_click_timer_cb(gpointer data) |
| 1777 | { |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 1778 | // we don't use this information currently |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1779 | int *timed_out = (int *) data; |
| 1780 | |
| 1781 | *timed_out = TRUE; |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 1782 | return FALSE; // don't happen again |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1783 | } |
| 1784 | |
Bram Moolenaar | 4ab7968 | 2017-08-27 14:50:47 +0200 | [diff] [blame] | 1785 | static guint motion_repeat_timer = 0; |
| 1786 | static int motion_repeat_offset = FALSE; |
| 1787 | static timeout_cb_type motion_repeat_timer_cb(gpointer); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1788 | |
| 1789 | static void |
| 1790 | process_motion_notify(int x, int y, GdkModifierType state) |
| 1791 | { |
| 1792 | int button; |
| 1793 | int_u vim_modifiers; |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 1794 | GtkAllocation allocation; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1795 | |
Chris Dalton | ee93e32 | 2021-11-23 12:27:48 +0000 | [diff] [blame] | 1796 | // Need to add GDK_BUTTON1_MASK state when dragging a touch. |
| 1797 | state |= dragging_button_state; |
| 1798 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1799 | button = (state & (GDK_BUTTON1_MASK | GDK_BUTTON2_MASK | |
| 1800 | GDK_BUTTON3_MASK | GDK_BUTTON4_MASK | |
| 1801 | GDK_BUTTON5_MASK)) |
| 1802 | ? MOUSE_DRAG : ' '; |
| 1803 | |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 1804 | // If our pointer is currently hidden, then we should show it. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1805 | gui_mch_mousehide(FALSE); |
| 1806 | |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 1807 | // Just moving the rodent above the drawing area without any button |
| 1808 | // being pressed. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1809 | if (button != MOUSE_DRAG) |
| 1810 | { |
| 1811 | gui_mouse_moved(x, y); |
| 1812 | return; |
| 1813 | } |
| 1814 | |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 1815 | // translate modifier coding between the main engine and GTK |
Bram Moolenaar | 19a09a1 | 2005-03-04 23:39:37 +0000 | [diff] [blame] | 1816 | vim_modifiers = modifiers_gdk2mouse(state); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1817 | |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 1818 | // inform the editor engine about the occurrence of this event |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1819 | gui_send_mouse_event(button, x, y, FALSE, vim_modifiers); |
| 1820 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1821 | /* |
| 1822 | * Auto repeat timer handling. |
| 1823 | */ |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 1824 | gtk_widget_get_allocation(gui.drawarea, &allocation); |
| 1825 | |
| 1826 | if (x < 0 || y < 0 |
| 1827 | || x >= allocation.width |
| 1828 | || y >= allocation.height) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1829 | { |
| 1830 | |
| 1831 | int dx; |
| 1832 | int dy; |
| 1833 | int offshoot; |
| 1834 | int delay = 10; |
| 1835 | |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 1836 | // Calculate the maximal distance of the cursor from the drawing area. |
| 1837 | // (offshoot can't become negative here!). |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 1838 | dx = x < 0 ? -x : x - allocation.width; |
| 1839 | dy = y < 0 ? -y : y - allocation.height; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1840 | |
| 1841 | offshoot = dx > dy ? dx : dy; |
| 1842 | |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 1843 | // 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 Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1851 | if (offshoot > 127) |
| 1852 | { |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 1853 | // 5 appears to be somehow near to my perceptual limits :-). |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1854 | delay = 5; |
| 1855 | } |
| 1856 | else |
| 1857 | { |
| 1858 | delay = (130 * (127 - offshoot)) / 127 + 5; |
| 1859 | } |
| 1860 | |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 1861 | // shoot again |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1862 | if (!motion_repeat_timer) |
Bram Moolenaar | 4ab7968 | 2017-08-27 14:50:47 +0200 | [diff] [blame] | 1863 | motion_repeat_timer = timeout_add(delay, motion_repeat_timer_cb, |
| 1864 | NULL); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1865 | } |
| 1866 | } |
| 1867 | |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 1868 | #if GTK_CHECK_VERSION(3,0,0) |
| 1869 | static GdkDevice * |
| 1870 | gui_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 Moolenaar | 30e12d2 | 2016-04-17 20:49:53 +0200 | [diff] [blame] | 1874 | # 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 Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 1878 | GdkDeviceManager * const mngr = gdk_display_get_device_manager(dpy); |
| 1879 | return gdk_device_manager_get_client_pointer(mngr); |
Bram Moolenaar | 30e12d2 | 2016-04-17 20:49:53 +0200 | [diff] [blame] | 1880 | # endif |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 1881 | } |
| 1882 | |
| 1883 | static GdkWindow * |
| 1884 | gui_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 Moolenaar | 2369c15 | 2016-03-04 23:08:25 +0100 | [diff] [blame] | 1894 | # if defined(FEAT_GUI_TABLINE) || defined(PROTO) |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 1895 | static GdkWindow * |
| 1896 | gui_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 Moolenaar | 2369c15 | 2016-03-04 23:08:25 +0100 | [diff] [blame] | 1903 | # endif |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 1904 | #else // !GTK_CHECK_VERSION(3,0,0) |
Bram Moolenaar | 664323e | 2018-09-18 22:30:07 +0200 | [diff] [blame] | 1905 | # 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 Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 1908 | #endif |
| 1909 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1910 | /* |
| 1911 | * Timer used to recognize multiple clicks of the mouse button. |
| 1912 | */ |
Bram Moolenaar | 4ab7968 | 2017-08-27 14:50:47 +0200 | [diff] [blame] | 1913 | static timeout_cb_type |
Bram Moolenaar | b85cb21 | 2009-05-17 14:24:23 +0000 | [diff] [blame] | 1914 | motion_repeat_timer_cb(gpointer data UNUSED) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1915 | { |
| 1916 | int x; |
| 1917 | int y; |
| 1918 | GdkModifierType state; |
| 1919 | |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 1920 | gui_gtk_get_pointer(gui.drawarea, &x, &y, &state); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1921 | |
| 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 Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 1930 | // If there already is a mouse click in the input buffer, wait another |
| 1931 | // time (otherwise we would create a backlog of clicks) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1932 | 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 Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 1949 | // Don't happen again. We will get reinstalled in the synthetic event |
| 1950 | // if needed -- thus repeating should still work. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1951 | return FALSE; |
| 1952 | } |
| 1953 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1954 | static gint |
Bram Moolenaar | b85cb21 | 2009-05-17 14:24:23 +0000 | [diff] [blame] | 1955 | motion_notify_event(GtkWidget *widget, |
| 1956 | GdkEventMotion *event, |
| 1957 | gpointer data UNUSED) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1958 | { |
| 1959 | if (event->is_hint) |
| 1960 | { |
| 1961 | int x; |
| 1962 | int y; |
| 1963 | GdkModifierType state; |
| 1964 | |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 1965 | gui_gtk_get_pointer(widget, &x, &y, &state); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1966 | 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 Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 1974 | return TRUE; // handled |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1975 | } |
| 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 Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1983 | static gint |
Bram Moolenaar | b85cb21 | 2009-05-17 14:24:23 +0000 | [diff] [blame] | 1984 | button_press_event(GtkWidget *widget, |
| 1985 | GdkEventButton *event, |
| 1986 | gpointer data UNUSED) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1987 | { |
| 1988 | int button; |
| 1989 | int repeated_click = FALSE; |
| 1990 | int x, y; |
| 1991 | int_u vim_modifiers; |
| 1992 | |
Bram Moolenaar | 20892c1 | 2011-06-26 04:49:00 +0200 | [diff] [blame] | 1993 | gui.event_time = event->time; |
Bram Moolenaar | 7cfea75 | 2010-06-22 06:07:12 +0200 | [diff] [blame] | 1994 | |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 1995 | // Make sure we have focus now we've been selected |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 1996 | if (gtk_socket_id != 0 && !gtk_widget_has_focus(widget)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1997 | 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 Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 2009 | // Handle multiple clicks |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2010 | if (!mouse_timed_out && mouse_click_timer) |
| 2011 | { |
Bram Moolenaar | 4ab7968 | 2017-08-27 14:50:47 +0200 | [diff] [blame] | 2012 | timeout_remove(mouse_click_timer); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2013 | mouse_click_timer = 0; |
| 2014 | repeated_click = TRUE; |
| 2015 | } |
| 2016 | |
| 2017 | mouse_timed_out = FALSE; |
Bram Moolenaar | 4ab7968 | 2017-08-27 14:50:47 +0200 | [diff] [blame] | 2018 | mouse_click_timer = timeout_add(p_mouset, mouse_click_timer_cb, |
| 2019 | &mouse_timed_out); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2020 | |
| 2021 | switch (event->button) |
| 2022 | { |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 2023 | // Keep in sync with gui_x11.c. |
| 2024 | // Buttons 4-7 are handled in scroll_event() |
Chris Dalton | ee93e32 | 2021-11-23 12:27:48 +0000 | [diff] [blame] | 2025 | case 1: |
| 2026 | button = MOUSE_LEFT; |
| 2027 | // needed for touch-drag |
| 2028 | dragging_button_state |= GDK_BUTTON1_MASK; |
| 2029 | break; |
Bram Moolenaar | 88e484b | 2015-11-24 15:38:44 +0100 | [diff] [blame] | 2030 | 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 Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 2035 | return FALSE; // Unknown button |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2036 | } |
| 2037 | |
| 2038 | #ifdef FEAT_XIM |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 2039 | // cancel any preediting |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2040 | if (im_is_preediting()) |
| 2041 | xim_reset(); |
| 2042 | #endif |
| 2043 | |
Bram Moolenaar | 19a09a1 | 2005-03-04 23:39:37 +0000 | [diff] [blame] | 2044 | vim_modifiers = modifiers_gdk2mouse(event->state); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2045 | |
| 2046 | gui_send_mouse_event(button, x, y, repeated_click, vim_modifiers); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2047 | |
| 2048 | return TRUE; |
| 2049 | } |
| 2050 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2051 | /* |
Bram Moolenaar | 182c5be | 2010-06-25 05:37:59 +0200 | [diff] [blame] | 2052 | * GTK+ 2 abstracts scrolling via the GdkEventScroll. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2053 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2054 | static gboolean |
Bram Moolenaar | b85cb21 | 2009-05-17 14:24:23 +0000 | [diff] [blame] | 2055 | scroll_event(GtkWidget *widget, |
| 2056 | GdkEventScroll *event, |
| 2057 | gpointer data UNUSED) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2058 | { |
| 2059 | int button; |
Bram Moolenaar | 19a09a1 | 2005-03-04 23:39:37 +0000 | [diff] [blame] | 2060 | int_u vim_modifiers; |
lilydjwg | 6e0a18f | 2024-01-29 20:54:28 +0100 | [diff] [blame] | 2061 | #if GTK_CHECK_VERSION(3,4,0) |
| 2062 | static double acc_x, acc_y; |
| 2063 | static guint32 last_smooth_event_time; |
| 2064 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2065 | |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 2066 | if (gtk_socket_id != 0 && !gtk_widget_has_focus(widget)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2067 | gtk_widget_grab_focus(widget); |
| 2068 | |
| 2069 | switch (event->direction) |
| 2070 | { |
| 2071 | case GDK_SCROLL_UP: |
| 2072 | button = MOUSE_4; |
| 2073 | break; |
| 2074 | case GDK_SCROLL_DOWN: |
| 2075 | button = MOUSE_5; |
| 2076 | break; |
Bram Moolenaar | 8d9b40e | 2010-07-25 15:49:07 +0200 | [diff] [blame] | 2077 | case GDK_SCROLL_LEFT: |
| 2078 | button = MOUSE_7; |
| 2079 | break; |
| 2080 | case GDK_SCROLL_RIGHT: |
| 2081 | button = MOUSE_6; |
| 2082 | break; |
lilydjwg | 6e0a18f | 2024-01-29 20:54:28 +0100 | [diff] [blame] | 2083 | #if GTK_CHECK_VERSION(3,4,0) |
| 2084 | case GDK_SCROLL_SMOOTH: |
| 2085 | if (event->time - last_smooth_event_time > 50) |
| 2086 | // reset our accumulations after 50ms of silence |
| 2087 | acc_x = acc_y = 0; |
| 2088 | acc_x += event->delta_x; |
| 2089 | acc_y += event->delta_y; |
| 2090 | last_smooth_event_time = event->time; |
| 2091 | break; |
| 2092 | #endif |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 2093 | default: // This shouldn't happen |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2094 | return FALSE; |
| 2095 | } |
| 2096 | |
| 2097 | # ifdef FEAT_XIM |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 2098 | // cancel any preediting |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2099 | if (im_is_preediting()) |
| 2100 | xim_reset(); |
| 2101 | # endif |
| 2102 | |
Bram Moolenaar | 19a09a1 | 2005-03-04 23:39:37 +0000 | [diff] [blame] | 2103 | vim_modifiers = modifiers_gdk2mouse(event->state); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2104 | |
lilydjwg | 6e0a18f | 2024-01-29 20:54:28 +0100 | [diff] [blame] | 2105 | #if GTK_CHECK_VERSION(3,4,0) |
| 2106 | if (event->direction == GDK_SCROLL_SMOOTH) |
| 2107 | { |
| 2108 | while (acc_x > 1.0) |
| 2109 | { // right |
| 2110 | acc_x = MAX(0.0, acc_x - 1.0); |
| 2111 | gui_send_mouse_event(MOUSE_6, (int)event->x, (int)event->y, |
| 2112 | FALSE, vim_modifiers); |
| 2113 | } |
| 2114 | while (acc_x < -1.0) |
| 2115 | { // left |
| 2116 | acc_x = MIN(0.0, acc_x + 1.0); |
| 2117 | gui_send_mouse_event(MOUSE_7, (int)event->x, (int)event->y, |
| 2118 | FALSE, vim_modifiers); |
| 2119 | } |
| 2120 | while (acc_y > 1.0) |
| 2121 | { // down |
| 2122 | acc_y = MAX(0.0, acc_y - 1.0); |
| 2123 | gui_send_mouse_event(MOUSE_5, (int)event->x, (int)event->y, |
| 2124 | FALSE, vim_modifiers); |
| 2125 | } |
| 2126 | while (acc_y < -1.0) |
| 2127 | { // up |
| 2128 | acc_y = MIN(0.0, acc_y + 1.0); |
| 2129 | gui_send_mouse_event(MOUSE_4, (int)event->x, (int)event->y, |
| 2130 | FALSE, vim_modifiers); |
| 2131 | } |
| 2132 | } |
| 2133 | else |
| 2134 | #endif |
| 2135 | gui_send_mouse_event(button, (int)event->x, (int)event->y, |
| 2136 | FALSE, vim_modifiers); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2137 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2138 | return TRUE; |
| 2139 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2140 | |
| 2141 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2142 | static gint |
Bram Moolenaar | b85cb21 | 2009-05-17 14:24:23 +0000 | [diff] [blame] | 2143 | button_release_event(GtkWidget *widget UNUSED, |
| 2144 | GdkEventButton *event, |
| 2145 | gpointer data UNUSED) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2146 | { |
| 2147 | int x, y; |
| 2148 | int_u vim_modifiers; |
| 2149 | |
Bram Moolenaar | 20892c1 | 2011-06-26 04:49:00 +0200 | [diff] [blame] | 2150 | gui.event_time = event->time; |
Bram Moolenaar | 7cfea75 | 2010-06-22 06:07:12 +0200 | [diff] [blame] | 2151 | |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 2152 | // Remove any motion "machine gun" timers used for automatic further |
| 2153 | // extension of allocation areas if outside of the applications window |
| 2154 | // area . |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2155 | if (motion_repeat_timer) |
| 2156 | { |
Bram Moolenaar | 4ab7968 | 2017-08-27 14:50:47 +0200 | [diff] [blame] | 2157 | timeout_remove(motion_repeat_timer); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2158 | motion_repeat_timer = 0; |
| 2159 | } |
| 2160 | |
| 2161 | x = event->x; |
| 2162 | y = event->y; |
| 2163 | |
Bram Moolenaar | 19a09a1 | 2005-03-04 23:39:37 +0000 | [diff] [blame] | 2164 | vim_modifiers = modifiers_gdk2mouse(event->state); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2165 | |
| 2166 | gui_send_mouse_event(MOUSE_RELEASE, x, y, FALSE, vim_modifiers); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2167 | |
Chris Dalton | ee93e32 | 2021-11-23 12:27:48 +0000 | [diff] [blame] | 2168 | switch (event->button) |
| 2169 | { |
| 2170 | case 1: // MOUSE_LEFT |
| 2171 | dragging_button_state = 0; |
| 2172 | break; |
| 2173 | } |
| 2174 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2175 | return TRUE; |
| 2176 | } |
| 2177 | |
| 2178 | |
| 2179 | #ifdef FEAT_DND |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 2180 | ///////////////////////////////////////////////////////////////////////////// |
| 2181 | // Drag aNd Drop support handlers. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2182 | |
| 2183 | /* |
| 2184 | * Count how many items there may be and separate them with a NUL. |
| 2185 | * Apparently the items are separated with \r\n. This is not documented, |
| 2186 | * thus be careful not to go past the end. Also allow separation with |
| 2187 | * NUL characters. |
| 2188 | */ |
| 2189 | static int |
| 2190 | count_and_decode_uri_list(char_u *out, char_u *raw, int len) |
| 2191 | { |
| 2192 | int i; |
| 2193 | char_u *p = out; |
| 2194 | int count = 0; |
| 2195 | |
| 2196 | for (i = 0; i < len; ++i) |
| 2197 | { |
| 2198 | if (raw[i] == NUL || raw[i] == '\n' || raw[i] == '\r') |
| 2199 | { |
| 2200 | if (p > out && p[-1] != NUL) |
| 2201 | { |
| 2202 | ++count; |
| 2203 | *p++ = NUL; |
| 2204 | } |
| 2205 | } |
| 2206 | else if (raw[i] == '%' && i + 2 < len && hexhex2nr(raw + i + 1) > 0) |
| 2207 | { |
| 2208 | *p++ = hexhex2nr(raw + i + 1); |
| 2209 | i += 2; |
| 2210 | } |
| 2211 | else |
| 2212 | *p++ = raw[i]; |
| 2213 | } |
| 2214 | if (p > out && p[-1] != NUL) |
| 2215 | { |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 2216 | *p = NUL; // last item didn't have \r or \n |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2217 | ++count; |
| 2218 | } |
| 2219 | return count; |
| 2220 | } |
| 2221 | |
| 2222 | /* |
| 2223 | * Parse NUL separated "src" strings. Make it an array "outlist" form. On |
| 2224 | * this process, URI which protocol is not "file:" are removed. Return |
| 2225 | * length of array (less than "max"). |
| 2226 | */ |
| 2227 | static int |
| 2228 | filter_uri_list(char_u **outlist, int max, char_u *src) |
| 2229 | { |
| 2230 | int i, j; |
| 2231 | |
| 2232 | for (i = j = 0; i < max; ++i) |
| 2233 | { |
| 2234 | outlist[i] = NULL; |
| 2235 | if (STRNCMP(src, "file:", 5) == 0) |
| 2236 | { |
| 2237 | src += 5; |
| 2238 | if (STRNCMP(src, "//localhost", 11) == 0) |
| 2239 | src += 11; |
| 2240 | while (src[0] == '/' && src[1] == '/') |
| 2241 | ++src; |
| 2242 | outlist[j++] = vim_strsave(src); |
| 2243 | } |
| 2244 | src += STRLEN(src) + 1; |
| 2245 | } |
| 2246 | return j; |
| 2247 | } |
| 2248 | |
| 2249 | static char_u ** |
| 2250 | parse_uri_list(int *count, char_u *data, int len) |
| 2251 | { |
| 2252 | int n = 0; |
| 2253 | char_u *tmp = NULL; |
Bram Moolenaar | 945ec09 | 2016-06-08 21:17:43 +0200 | [diff] [blame] | 2254 | char_u **array = NULL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2255 | |
Bram Moolenaar | c799fe2 | 2019-05-28 23:08:19 +0200 | [diff] [blame] | 2256 | if (data != NULL && len > 0 && (tmp = alloc(len + 1)) != NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2257 | { |
| 2258 | n = count_and_decode_uri_list(tmp, data, len); |
Bram Moolenaar | c799fe2 | 2019-05-28 23:08:19 +0200 | [diff] [blame] | 2259 | if (n > 0 && (array = ALLOC_MULT(char_u *, n)) != NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2260 | n = filter_uri_list(array, n, tmp); |
| 2261 | } |
| 2262 | vim_free(tmp); |
| 2263 | *count = n; |
| 2264 | return array; |
| 2265 | } |
| 2266 | |
| 2267 | static void |
| 2268 | drag_handle_uri_list(GdkDragContext *context, |
| 2269 | GtkSelectionData *data, |
| 2270 | guint time_, |
| 2271 | GdkModifierType state, |
| 2272 | gint x, |
| 2273 | gint y) |
| 2274 | { |
| 2275 | char_u **fnames; |
| 2276 | int nfiles = 0; |
| 2277 | |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 2278 | fnames = parse_uri_list(&nfiles, |
| 2279 | (char_u *)gtk_selection_data_get_data(data), |
| 2280 | gtk_selection_data_get_length(data)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2281 | |
| 2282 | if (fnames != NULL && nfiles > 0) |
| 2283 | { |
Bram Moolenaar | 19a09a1 | 2005-03-04 23:39:37 +0000 | [diff] [blame] | 2284 | int_u modifiers; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2285 | |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 2286 | gtk_drag_finish(context, TRUE, FALSE, time_); // accept |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2287 | |
Bram Moolenaar | 19a09a1 | 2005-03-04 23:39:37 +0000 | [diff] [blame] | 2288 | modifiers = modifiers_gdk2mouse(state); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2289 | |
| 2290 | gui_handle_drop(x, y, modifiers, fnames, nfiles); |
| 2291 | } |
Bram Moolenaar | eb3593b | 2006-04-22 22:33:57 +0000 | [diff] [blame] | 2292 | else |
| 2293 | vim_free(fnames); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2294 | } |
| 2295 | |
| 2296 | static void |
| 2297 | drag_handle_text(GdkDragContext *context, |
| 2298 | GtkSelectionData *data, |
| 2299 | guint time_, |
| 2300 | GdkModifierType state) |
| 2301 | { |
| 2302 | char_u dropkey[6] = {CSI, KS_MODIFIER, 0, CSI, KS_EXTRA, (char_u)KE_DROP}; |
| 2303 | char_u *text; |
| 2304 | int len; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2305 | char_u *tmpbuf = NULL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2306 | |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 2307 | text = (char_u *)gtk_selection_data_get_data(data); |
| 2308 | len = gtk_selection_data_get_length(data); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2309 | |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 2310 | if (gtk_selection_data_get_data_type(data) == utf8_string_atom) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2311 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2312 | if (input_conv.vc_type != CONV_NONE) |
| 2313 | tmpbuf = string_convert(&input_conv, text, &len); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2314 | if (tmpbuf != NULL) |
| 2315 | text = tmpbuf; |
| 2316 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2317 | |
| 2318 | dnd_yank_drag_data(text, (long)len); |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 2319 | gtk_drag_finish(context, TRUE, FALSE, time_); // accept |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2320 | vim_free(tmpbuf); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2321 | |
Bram Moolenaar | 19a09a1 | 2005-03-04 23:39:37 +0000 | [diff] [blame] | 2322 | dropkey[2] = modifiers_gdk2vim(state); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2323 | |
| 2324 | if (dropkey[2] != 0) |
| 2325 | add_to_input_buf(dropkey, (int)sizeof(dropkey)); |
| 2326 | else |
| 2327 | add_to_input_buf(dropkey + 3, (int)(sizeof(dropkey) - 3)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2328 | } |
| 2329 | |
| 2330 | /* |
| 2331 | * DND receiver. |
| 2332 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2333 | static void |
| 2334 | drag_data_received_cb(GtkWidget *widget, |
| 2335 | GdkDragContext *context, |
| 2336 | gint x, |
| 2337 | gint y, |
| 2338 | GtkSelectionData *data, |
| 2339 | guint info, |
| 2340 | guint time_, |
Bram Moolenaar | b85cb21 | 2009-05-17 14:24:23 +0000 | [diff] [blame] | 2341 | gpointer user_data UNUSED) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2342 | { |
| 2343 | GdkModifierType state; |
| 2344 | |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 2345 | // Guard against trash |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 2346 | const guchar * const data_data = gtk_selection_data_get_data(data); |
| 2347 | const gint data_length = gtk_selection_data_get_length(data); |
| 2348 | const gint data_format = gtk_selection_data_get_format(data); |
| 2349 | |
| 2350 | if (data_data == NULL |
| 2351 | || data_length <= 0 |
| 2352 | || data_format != 8 |
| 2353 | || data_data[data_length] != '\0') |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2354 | { |
| 2355 | gtk_drag_finish(context, FALSE, FALSE, time_); |
| 2356 | return; |
| 2357 | } |
| 2358 | |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 2359 | // Get the current modifier state for proper distinguishment between |
| 2360 | // different operations later. |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 2361 | gui_gtk_get_pointer(widget, NULL, NULL, &state); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2362 | |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 2363 | // Not sure about the role of "text/plain" here... |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2364 | if (info == (guint)TARGET_TEXT_URI_LIST) |
| 2365 | drag_handle_uri_list(context, data, time_, state, x, y); |
| 2366 | else |
| 2367 | drag_handle_text(context, data, time_, state); |
| 2368 | |
| 2369 | } |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 2370 | #endif // FEAT_DND |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2371 | |
| 2372 | |
Bram Moolenaar | f96ae0b | 2019-07-28 15:21:55 +0200 | [diff] [blame] | 2373 | #if defined(USE_GNOME_SESSION) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2374 | /* |
| 2375 | * GnomeClient interact callback. Check for unsaved buffers that cannot |
| 2376 | * be abandoned and pop up a dialog asking the user for confirmation if |
| 2377 | * necessary. |
| 2378 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2379 | static void |
Bram Moolenaar | 30bb414 | 2010-05-17 22:07:15 +0200 | [diff] [blame] | 2380 | sm_client_check_changed_any(GnomeClient *client UNUSED, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2381 | gint key, |
Bram Moolenaar | 30bb414 | 2010-05-17 22:07:15 +0200 | [diff] [blame] | 2382 | GnomeDialogType type UNUSED, |
| 2383 | gpointer data UNUSED) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2384 | { |
| 2385 | cmdmod_T save_cmdmod; |
| 2386 | gboolean shutdown_cancelled; |
| 2387 | |
| 2388 | save_cmdmod = cmdmod; |
| 2389 | |
| 2390 | # ifdef FEAT_BROWSE |
Bram Moolenaar | e100440 | 2020-10-24 20:49:43 +0200 | [diff] [blame] | 2391 | cmdmod.cmod_flags |= CMOD_BROWSE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2392 | # endif |
| 2393 | # if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) |
Bram Moolenaar | e100440 | 2020-10-24 20:49:43 +0200 | [diff] [blame] | 2394 | cmdmod.cmod_flags |= CMOD_CONFIRM; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2395 | # endif |
| 2396 | /* |
| 2397 | * If there are changed buffers, present the user with |
| 2398 | * a dialog if possible, otherwise give an error message. |
| 2399 | */ |
Bram Moolenaar | 027387f | 2016-01-02 22:25:52 +0100 | [diff] [blame] | 2400 | shutdown_cancelled = check_changed_any(FALSE, FALSE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2401 | |
| 2402 | exiting = FALSE; |
| 2403 | cmdmod = save_cmdmod; |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 2404 | setcursor(); // position the cursor |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2405 | out_flush(); |
| 2406 | /* |
| 2407 | * If the user hit the [Cancel] button the whole shutdown |
| 2408 | * will be cancelled. Wow, quite powerful feature (: |
| 2409 | */ |
| 2410 | gnome_interaction_key_return(key, shutdown_cancelled); |
| 2411 | } |
| 2412 | |
| 2413 | /* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2414 | * "save_yourself" signal handler. Initiate an interaction to ask the user |
| 2415 | * for confirmation if necessary. Save the current editing session and tell |
| 2416 | * the session manager how to restart Vim. |
| 2417 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2418 | static gboolean |
| 2419 | sm_client_save_yourself(GnomeClient *client, |
Bram Moolenaar | 30bb414 | 2010-05-17 22:07:15 +0200 | [diff] [blame] | 2420 | gint phase UNUSED, |
| 2421 | GnomeSaveStyle save_style UNUSED, |
| 2422 | gboolean shutdown UNUSED, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2423 | GnomeInteractStyle interact_style, |
Bram Moolenaar | 30bb414 | 2010-05-17 22:07:15 +0200 | [diff] [blame] | 2424 | gboolean fast UNUSED, |
| 2425 | gpointer data UNUSED) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2426 | { |
| 2427 | static const char suffix[] = "-session.vim"; |
| 2428 | char *session_file; |
| 2429 | unsigned int len; |
| 2430 | gboolean success; |
| 2431 | |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 2432 | // Always request an interaction if possible. check_changed_any() |
| 2433 | // won't actually show a dialog unless any buffers have been modified. |
| 2434 | // There doesn't seem to be an obvious way to check that without |
| 2435 | // automatically firing the dialog. Anyway, it works just fine. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2436 | if (interact_style == GNOME_INTERACT_ANY) |
| 2437 | gnome_client_request_interaction(client, GNOME_DIALOG_NORMAL, |
| 2438 | &sm_client_check_changed_any, |
| 2439 | NULL); |
| 2440 | out_flush(); |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 2441 | ml_sync_all(FALSE, FALSE); // preserve all swap files |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2442 | |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 2443 | // The path is unique for each session save. We do neither know nor care |
| 2444 | // which session script will actually be used later. This decision is in |
| 2445 | // the domain of the session manager. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2446 | session_file = gnome_config_get_real_path( |
| 2447 | gnome_client_get_config_prefix(client)); |
| 2448 | len = strlen(session_file); |
| 2449 | |
| 2450 | if (len > 0 && session_file[len-1] == G_DIR_SEPARATOR) |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 2451 | --len; // get rid of the superfluous trailing '/' |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2452 | |
| 2453 | session_file = g_renew(char, session_file, len + sizeof(suffix)); |
| 2454 | memcpy(session_file + len, suffix, sizeof(suffix)); |
| 2455 | |
| 2456 | success = write_session_file((char_u *)session_file); |
| 2457 | |
| 2458 | if (success) |
| 2459 | { |
| 2460 | const char *argv[8]; |
| 2461 | int i; |
| 2462 | |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 2463 | // Tell the session manager how to wipe out the stored session data. |
| 2464 | // This isn't as dangerous as it looks, don't worry :) session_file |
| 2465 | // is a unique absolute filename. Usually it'll be something like |
| 2466 | // `/home/user/.gnome2/vim-XXXXXX-session.vim'. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2467 | i = 0; |
| 2468 | argv[i++] = "rm"; |
| 2469 | argv[i++] = session_file; |
| 2470 | argv[i] = NULL; |
| 2471 | |
| 2472 | gnome_client_set_discard_command(client, i, (char **)argv); |
| 2473 | |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 2474 | // Tell the session manager how to restore the just saved session. |
| 2475 | // This is easily done thanks to Vim's -S option. Pass the -f flag |
| 2476 | // since there's no need to fork -- it might even cause confusion. |
| 2477 | // Also pass the window role to give the WM something to match on. |
| 2478 | // The role is set in gui_mch_open(), thus should _never_ be NULL. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2479 | i = 0; |
| 2480 | argv[i++] = restart_command; |
| 2481 | argv[i++] = "-f"; |
| 2482 | argv[i++] = "-g"; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2483 | argv[i++] = "--role"; |
| 2484 | argv[i++] = gtk_window_get_role(GTK_WINDOW(gui.mainwin)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2485 | argv[i++] = "-S"; |
| 2486 | argv[i++] = session_file; |
| 2487 | argv[i] = NULL; |
| 2488 | |
| 2489 | gnome_client_set_restart_command(client, i, (char **)argv); |
| 2490 | gnome_client_set_clone_command(client, 0, NULL); |
| 2491 | } |
| 2492 | |
| 2493 | g_free(session_file); |
| 2494 | |
| 2495 | return success; |
| 2496 | } |
| 2497 | |
| 2498 | /* |
| 2499 | * Called when the session manager wants us to die. There isn't much to save |
| 2500 | * here since "save_yourself" has been emitted before (unless serious trouble |
| 2501 | * is happening). |
| 2502 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2503 | static void |
Bram Moolenaar | 30bb414 | 2010-05-17 22:07:15 +0200 | [diff] [blame] | 2504 | sm_client_die(GnomeClient *client UNUSED, gpointer data UNUSED) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2505 | { |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 2506 | // Don't write messages to the GUI anymore |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2507 | full_screen = FALSE; |
| 2508 | |
Bram Moolenaar | c93e791 | 2008-07-08 10:46:08 +0000 | [diff] [blame] | 2509 | vim_strncpy(IObuff, (char_u *) |
Bram Moolenaar | ce0842a | 2005-07-18 21:58:11 +0000 | [diff] [blame] | 2510 | _("Vim: Received \"die\" request from session manager\n"), |
Bram Moolenaar | c93e791 | 2008-07-08 10:46:08 +0000 | [diff] [blame] | 2511 | IOSIZE - 1); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2512 | preserve_exit(); |
| 2513 | } |
| 2514 | |
| 2515 | /* |
| 2516 | * Connect our signal handlers to be notified on session save and shutdown. |
| 2517 | */ |
| 2518 | static void |
| 2519 | setup_save_yourself(void) |
| 2520 | { |
| 2521 | GnomeClient *client; |
| 2522 | |
| 2523 | client = gnome_master_client(); |
Yegappan Lakshmanan | 7f8b255 | 2023-01-08 13:44:24 +0000 | [diff] [blame] | 2524 | if (client == NULL) |
| 2525 | return; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2526 | |
Yegappan Lakshmanan | 7f8b255 | 2023-01-08 13:44:24 +0000 | [diff] [blame] | 2527 | // Must use the deprecated gtk_signal_connect() for compatibility |
| 2528 | // with GNOME 1. Arrgh, zombies! |
| 2529 | gtk_signal_connect(GTK_OBJECT(client), "save_yourself", |
| 2530 | GTK_SIGNAL_FUNC(&sm_client_save_yourself), NULL); |
| 2531 | gtk_signal_connect(GTK_OBJECT(client), "die", |
| 2532 | GTK_SIGNAL_FUNC(&sm_client_die), NULL); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2533 | } |
| 2534 | |
Bram Moolenaar | f96ae0b | 2019-07-28 15:21:55 +0200 | [diff] [blame] | 2535 | #else // !USE_GNOME_SESSION |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2536 | |
| 2537 | # ifdef USE_XSMP |
| 2538 | /* |
| 2539 | * GTK tells us that XSMP needs attention |
| 2540 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2541 | static gboolean |
Bram Moolenaar | 66f948e | 2016-01-30 16:39:25 +0100 | [diff] [blame] | 2542 | local_xsmp_handle_requests( |
| 2543 | GIOChannel *source UNUSED, |
| 2544 | GIOCondition condition, |
| 2545 | gpointer data) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2546 | { |
| 2547 | if (condition == G_IO_IN) |
| 2548 | { |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 2549 | // Do stuff; maybe close connection |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2550 | if (xsmp_handle_requests() == FAIL) |
| 2551 | g_io_channel_unref((GIOChannel *)data); |
| 2552 | return TRUE; |
| 2553 | } |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 2554 | // Error |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2555 | g_io_channel_unref((GIOChannel *)data); |
| 2556 | xsmp_close(); |
| 2557 | return TRUE; |
| 2558 | } |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 2559 | # endif // USE_XSMP |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2560 | |
| 2561 | /* |
| 2562 | * Setup the WM_PROTOCOLS to indicate we want the WM_SAVE_YOURSELF event. |
| 2563 | * This is an ugly use of X functions. GTK doesn't offer an alternative. |
| 2564 | */ |
| 2565 | static void |
| 2566 | setup_save_yourself(void) |
| 2567 | { |
| 2568 | Atom *existing_atoms = NULL; |
| 2569 | int count = 0; |
| 2570 | |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 2571 | # ifdef USE_XSMP |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2572 | if (xsmp_icefd != -1) |
| 2573 | { |
| 2574 | /* |
| 2575 | * Use XSMP is preference to legacy WM_SAVE_YOURSELF; |
| 2576 | * set up GTK IO monitor |
| 2577 | */ |
| 2578 | GIOChannel *g_io = g_io_channel_unix_new(xsmp_icefd); |
| 2579 | |
| 2580 | g_io_add_watch(g_io, G_IO_IN | G_IO_ERR | G_IO_HUP, |
| 2581 | local_xsmp_handle_requests, (gpointer)g_io); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 2582 | g_io_channel_unref(g_io); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2583 | } |
| 2584 | else |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 2585 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2586 | { |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 2587 | // Fall back to old method |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2588 | |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 2589 | // first get the existing value |
lilydjwg | 6e0a18f | 2024-01-29 20:54:28 +0100 | [diff] [blame] | 2590 | Display * dpy = gui_mch_get_display(); |
| 2591 | if (!dpy) |
| 2592 | return; |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 2593 | |
lilydjwg | 6e0a18f | 2024-01-29 20:54:28 +0100 | [diff] [blame] | 2594 | GdkWindow * const mainwin_win = gtk_widget_get_window(gui.mainwin); |
| 2595 | if (XGetWMProtocols(dpy, GDK_WINDOW_XID(mainwin_win), |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 2596 | &existing_atoms, &count)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2597 | { |
| 2598 | Atom *new_atoms; |
| 2599 | Atom save_yourself_xatom; |
| 2600 | int i; |
| 2601 | |
| 2602 | save_yourself_xatom = GET_X_ATOM(save_yourself_atom); |
| 2603 | |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 2604 | // check if WM_SAVE_YOURSELF isn't there yet |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2605 | for (i = 0; i < count; ++i) |
| 2606 | if (existing_atoms[i] == save_yourself_xatom) |
| 2607 | break; |
| 2608 | |
| 2609 | if (i == count) |
| 2610 | { |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 2611 | // allocate an Atoms array which is one item longer |
Bram Moolenaar | c799fe2 | 2019-05-28 23:08:19 +0200 | [diff] [blame] | 2612 | new_atoms = ALLOC_MULT(Atom, count + 1); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2613 | if (new_atoms != NULL) |
| 2614 | { |
| 2615 | memcpy(new_atoms, existing_atoms, count * sizeof(Atom)); |
| 2616 | new_atoms[count] = save_yourself_xatom; |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 2617 | XSetWMProtocols(GDK_WINDOW_XDISPLAY(mainwin_win), |
| 2618 | GDK_WINDOW_XID(mainwin_win), |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2619 | new_atoms, count + 1); |
| 2620 | vim_free(new_atoms); |
| 2621 | } |
| 2622 | } |
| 2623 | XFree(existing_atoms); |
| 2624 | } |
| 2625 | } |
| 2626 | } |
| 2627 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2628 | /* |
| 2629 | * Installing a global event filter seems to be the only way to catch |
| 2630 | * client messages of type WM_PROTOCOLS without overriding GDK's own |
| 2631 | * client message event filter. Well, that's still better than trying |
| 2632 | * to guess what the GDK filter had done if it had been invoked instead |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2633 | * |
| 2634 | * GTK2_FIXME: This doesn't seem to work. For some reason we never |
| 2635 | * receive WM_SAVE_YOURSELF even though everything is set up correctly. |
| 2636 | * I have the nasty feeling modern session managers just don't send this |
| 2637 | * deprecated message anymore. Addition: confirmed by several people. |
| 2638 | * |
| 2639 | * The GNOME session support is much cooler anyway. Unlike this ugly |
| 2640 | * WM_SAVE_YOURSELF hack it actually stores the session... And yes, |
| 2641 | * it should work with KDE as well. |
| 2642 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2643 | static GdkFilterReturn |
Bram Moolenaar | b85cb21 | 2009-05-17 14:24:23 +0000 | [diff] [blame] | 2644 | global_event_filter(GdkXEvent *xev, |
| 2645 | GdkEvent *event UNUSED, |
| 2646 | gpointer data UNUSED) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2647 | { |
| 2648 | XEvent *xevent = (XEvent *)xev; |
| 2649 | |
| 2650 | if (xevent != NULL |
| 2651 | && xevent->type == ClientMessage |
| 2652 | && xevent->xclient.message_type == GET_X_ATOM(wm_protocols_atom) |
Bram Moolenaar | b85cb21 | 2009-05-17 14:24:23 +0000 | [diff] [blame] | 2653 | && (long_u)xevent->xclient.data.l[0] |
| 2654 | == GET_X_ATOM(save_yourself_atom)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2655 | { |
| 2656 | out_flush(); |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 2657 | ml_sync_all(FALSE, FALSE); // preserve all swap files |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2658 | /* |
| 2659 | * Set the window's WM_COMMAND property, to let the window manager |
| 2660 | * know we are done saving ourselves. We don't want to be |
| 2661 | * restarted, thus set argv to NULL. |
| 2662 | */ |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 2663 | XSetCommand(GDK_WINDOW_XDISPLAY(gtk_widget_get_window(gui.mainwin)), |
| 2664 | GDK_WINDOW_XID(gtk_widget_get_window(gui.mainwin)), |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2665 | NULL, 0); |
| 2666 | return GDK_FILTER_REMOVE; |
| 2667 | } |
| 2668 | |
| 2669 | return GDK_FILTER_CONTINUE; |
| 2670 | } |
Bram Moolenaar | f96ae0b | 2019-07-28 15:21:55 +0200 | [diff] [blame] | 2671 | #endif // !USE_GNOME_SESSION |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2672 | |
| 2673 | |
| 2674 | /* |
| 2675 | * Setup the window icon & xcmdsrv comm after the main window has been realized. |
| 2676 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2677 | static void |
Bram Moolenaar | b85cb21 | 2009-05-17 14:24:23 +0000 | [diff] [blame] | 2678 | mainwin_realize(GtkWidget *widget UNUSED, gpointer data UNUSED) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2679 | { |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 2680 | // If you get an error message here, you still need to unpack the runtime |
| 2681 | // archive! |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2682 | #ifdef magick |
| 2683 | # undef magick |
| 2684 | #endif |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 2685 | // A bit hackish, but avoids casting later and allows optimization |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2686 | # define static static const |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2687 | #define magick vim32x32 |
| 2688 | #include "../runtime/vim32x32.xpm" |
| 2689 | #undef magick |
| 2690 | #define magick vim16x16 |
| 2691 | #include "../runtime/vim16x16.xpm" |
| 2692 | #undef magick |
| 2693 | #define magick vim48x48 |
| 2694 | #include "../runtime/vim48x48.xpm" |
| 2695 | #undef magick |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2696 | # undef static |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2697 | |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 2698 | GdkWindow * const mainwin_win = gtk_widget_get_window(gui.mainwin); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 2699 | |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 2700 | // When started with "--echo-wid" argument, write window ID on stdout. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2701 | if (echo_wid_arg) |
| 2702 | { |
lilydjwg | 6e0a18f | 2024-01-29 20:54:28 +0100 | [diff] [blame] | 2703 | if (gui_mch_get_display()) |
| 2704 | printf("WID: %ld\n", (long)GDK_WINDOW_XID(mainwin_win)); |
| 2705 | else |
| 2706 | printf("WID: 0\n"); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2707 | fflush(stdout); |
| 2708 | } |
| 2709 | |
| 2710 | if (vim_strchr(p_go, GO_ICON) != NULL) |
| 2711 | { |
| 2712 | /* |
| 2713 | * Add an icon to the main window. For fun and convenience of the user. |
| 2714 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2715 | GList *icons = NULL; |
| 2716 | |
| 2717 | icons = g_list_prepend(icons, gdk_pixbuf_new_from_xpm_data(vim16x16)); |
| 2718 | icons = g_list_prepend(icons, gdk_pixbuf_new_from_xpm_data(vim32x32)); |
| 2719 | icons = g_list_prepend(icons, gdk_pixbuf_new_from_xpm_data(vim48x48)); |
| 2720 | |
| 2721 | gtk_window_set_icon_list(GTK_WINDOW(gui.mainwin), icons); |
| 2722 | |
Bram Moolenaar | 81a4cf4 | 2020-09-09 19:05:13 +0200 | [diff] [blame] | 2723 | // TODO: is this type cast OK? |
| 2724 | g_list_foreach(icons, (GFunc)(void *)&g_object_unref, NULL); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2725 | g_list_free(icons); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2726 | } |
| 2727 | |
Bram Moolenaar | f96ae0b | 2019-07-28 15:21:55 +0200 | [diff] [blame] | 2728 | #if !defined(USE_GNOME_SESSION) |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 2729 | // Register a handler for WM_SAVE_YOURSELF with GDK's low-level X I/F |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2730 | gdk_window_add_filter(NULL, &global_event_filter, NULL); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2731 | #endif |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 2732 | // Setup to indicate to the window manager that we want to catch the |
| 2733 | // WM_SAVE_YOURSELF event. For GNOME, this connects to the session |
| 2734 | // manager instead. |
Bram Moolenaar | f96ae0b | 2019-07-28 15:21:55 +0200 | [diff] [blame] | 2735 | #if defined(USE_GNOME_SESSION) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2736 | if (using_gnome) |
| 2737 | #endif |
| 2738 | setup_save_yourself(); |
| 2739 | |
| 2740 | #ifdef FEAT_CLIENTSERVER |
lilydjwg | 6e0a18f | 2024-01-29 20:54:28 +0100 | [diff] [blame] | 2741 | if (gui_mch_get_display()) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2742 | { |
lilydjwg | 6e0a18f | 2024-01-29 20:54:28 +0100 | [diff] [blame] | 2743 | if (serverName == NULL && serverDelayedStartName != NULL) |
| 2744 | { |
| 2745 | // This is a :gui command in a plain vim with no previous server |
| 2746 | commWindow = GDK_WINDOW_XID(mainwin_win); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 2747 | |
lilydjwg | 6e0a18f | 2024-01-29 20:54:28 +0100 | [diff] [blame] | 2748 | (void)serverRegisterName(GDK_WINDOW_XDISPLAY(mainwin_win), |
| 2749 | serverDelayedStartName); |
| 2750 | } |
| 2751 | else |
| 2752 | { |
| 2753 | /* |
| 2754 | * Cannot handle "XLib-only" windows with gtk event routines, we'll |
| 2755 | * have to change the "server" registration to that of the main window |
| 2756 | * If we have not registered a name yet, remember the window. |
| 2757 | */ |
| 2758 | serverChangeRegisteredWindow(GDK_WINDOW_XDISPLAY(mainwin_win), |
| 2759 | GDK_WINDOW_XID(mainwin_win)); |
| 2760 | } |
| 2761 | gtk_widget_add_events(gui.mainwin, GDK_PROPERTY_CHANGE_MASK); |
| 2762 | g_signal_connect(G_OBJECT(gui.mainwin), "property-notify-event", |
| 2763 | G_CALLBACK(property_event), NULL); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2764 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2765 | #endif |
| 2766 | } |
| 2767 | |
| 2768 | static GdkCursor * |
| 2769 | create_blank_pointer(void) |
| 2770 | { |
| 2771 | GdkWindow *root_window = NULL; |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 2772 | #if GTK_CHECK_VERSION(3,0,0) |
| 2773 | GdkPixbuf *blank_mask; |
| 2774 | #else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2775 | GdkPixmap *blank_mask; |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 2776 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2777 | GdkCursor *cursor; |
Bram Moolenaar | 36edf06 | 2016-07-21 22:10:12 +0200 | [diff] [blame] | 2778 | #if GTK_CHECK_VERSION(3,0,0) |
Kenny Stauffer | 660eb9f | 2023-04-28 16:36:51 +0100 | [diff] [blame] | 2779 | GdkRGBA color = { 0.0, 0.0, 0.0, 0.0 }; |
Bram Moolenaar | 36edf06 | 2016-07-21 22:10:12 +0200 | [diff] [blame] | 2780 | #else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2781 | GdkColor color = { 0, 0, 0, 0 }; |
| 2782 | char blank_data[] = { 0x0 }; |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 2783 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2784 | |
Bram Moolenaar | b463e8d | 2017-06-05 15:07:09 +0200 | [diff] [blame] | 2785 | #if GTK_CHECK_VERSION(3,12,0) |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 2786 | { |
| 2787 | GdkWindow * const win = gtk_widget_get_window(gui.mainwin); |
| 2788 | GdkScreen * const scrn = gdk_window_get_screen(win); |
| 2789 | root_window = gdk_screen_get_root_window(scrn); |
| 2790 | } |
Bram Moolenaar | b463e8d | 2017-06-05 15:07:09 +0200 | [diff] [blame] | 2791 | #else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2792 | root_window = gtk_widget_get_root_window(gui.mainwin); |
| 2793 | #endif |
| 2794 | |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 2795 | // Create a pseudo blank pointer, which is in fact one pixel by one pixel |
| 2796 | // in size. |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 2797 | #if GTK_CHECK_VERSION(3,0,0) |
| 2798 | { |
| 2799 | cairo_surface_t *surf; |
| 2800 | cairo_t *cr; |
| 2801 | |
| 2802 | surf = cairo_image_surface_create(CAIRO_FORMAT_A1, 1, 1); |
| 2803 | cr = cairo_create(surf); |
| 2804 | |
Bram Moolenaar | 36edf06 | 2016-07-21 22:10:12 +0200 | [diff] [blame] | 2805 | cairo_set_source_rgba(cr, |
| 2806 | color.red, |
| 2807 | color.green, |
| 2808 | color.blue, |
| 2809 | color.alpha); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 2810 | cairo_rectangle(cr, 0, 0, 1, 1); |
| 2811 | cairo_fill(cr); |
| 2812 | cairo_destroy(cr); |
| 2813 | |
| 2814 | blank_mask = gdk_pixbuf_get_from_surface(surf, 0, 0, 1, 1); |
| 2815 | cairo_surface_destroy(surf); |
| 2816 | |
| 2817 | cursor = gdk_cursor_new_from_pixbuf(gdk_window_get_display(root_window), |
| 2818 | blank_mask, 0, 0); |
| 2819 | g_object_unref(blank_mask); |
| 2820 | } |
| 2821 | #else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2822 | blank_mask = gdk_bitmap_create_from_data(root_window, blank_data, 1, 1); |
| 2823 | cursor = gdk_cursor_new_from_pixmap(blank_mask, blank_mask, |
| 2824 | &color, &color, 0, 0); |
| 2825 | gdk_bitmap_unref(blank_mask); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 2826 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2827 | |
| 2828 | return cursor; |
| 2829 | } |
| 2830 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2831 | static void |
| 2832 | mainwin_screen_changed_cb(GtkWidget *widget, |
Bram Moolenaar | b85cb21 | 2009-05-17 14:24:23 +0000 | [diff] [blame] | 2833 | GdkScreen *previous_screen UNUSED, |
| 2834 | gpointer data UNUSED) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2835 | { |
| 2836 | if (!gtk_widget_has_screen(widget)) |
| 2837 | return; |
| 2838 | |
| 2839 | /* |
Bram Moolenaar | 4932594 | 2007-05-10 19:19:59 +0000 | [diff] [blame] | 2840 | * Recreate the invisible mouse cursor. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2841 | */ |
| 2842 | if (gui.blank_pointer != NULL) |
Bram Moolenaar | b463e8d | 2017-06-05 15:07:09 +0200 | [diff] [blame] | 2843 | #if GTK_CHECK_VERSION(3,0,0) |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 2844 | g_object_unref(G_OBJECT(gui.blank_pointer)); |
Bram Moolenaar | b463e8d | 2017-06-05 15:07:09 +0200 | [diff] [blame] | 2845 | #else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2846 | gdk_cursor_unref(gui.blank_pointer); |
Bram Moolenaar | b463e8d | 2017-06-05 15:07:09 +0200 | [diff] [blame] | 2847 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2848 | |
| 2849 | gui.blank_pointer = create_blank_pointer(); |
| 2850 | |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 2851 | if (gui.pointer_hidden && gtk_widget_get_window(gui.drawarea) != NULL) |
| 2852 | gdk_window_set_cursor(gtk_widget_get_window(gui.drawarea), |
| 2853 | gui.blank_pointer); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2854 | |
| 2855 | /* |
| 2856 | * Create a new PangoContext for this screen, and initialize it |
| 2857 | * with the current font if necessary. |
| 2858 | */ |
| 2859 | if (gui.text_context != NULL) |
| 2860 | g_object_unref(gui.text_context); |
| 2861 | |
| 2862 | gui.text_context = gtk_widget_create_pango_context(widget); |
| 2863 | pango_context_set_base_dir(gui.text_context, PANGO_DIRECTION_LTR); |
| 2864 | |
| 2865 | if (gui.norm_font != NULL) |
| 2866 | { |
Bram Moolenaar | 46c9c73 | 2004-12-12 11:37:09 +0000 | [diff] [blame] | 2867 | gui_mch_init_font(p_guifont, FALSE); |
Bram Moolenaar | 8ac4415 | 2017-11-09 18:33:29 +0100 | [diff] [blame] | 2868 | gui_set_shellsize(TRUE, FALSE, RESIZE_BOTH); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2869 | } |
| 2870 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2871 | |
| 2872 | /* |
| 2873 | * After the drawing area comes up, we calculate all colors and create the |
| 2874 | * dummy blank cursor. |
| 2875 | * |
| 2876 | * Don't try to set any VIM scrollbar sizes anywhere here. I'm relying on the |
| 2877 | * fact that the main VIM engine doesn't take them into account anywhere. |
| 2878 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2879 | static void |
Bram Moolenaar | b85cb21 | 2009-05-17 14:24:23 +0000 | [diff] [blame] | 2880 | drawarea_realize_cb(GtkWidget *widget, gpointer data UNUSED) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2881 | { |
| 2882 | GtkWidget *sbar; |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 2883 | GtkAllocation allocation; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2884 | |
| 2885 | #ifdef FEAT_XIM |
| 2886 | xim_init(); |
| 2887 | #endif |
| 2888 | gui_mch_new_colors(); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 2889 | #if GTK_CHECK_VERSION(3,0,0) |
| 2890 | gui.surface = gdk_window_create_similar_surface( |
| 2891 | gtk_widget_get_window(widget), |
| 2892 | CAIRO_CONTENT_COLOR_ALPHA, |
| 2893 | gtk_widget_get_allocated_width(widget), |
| 2894 | gtk_widget_get_allocated_height(widget)); |
| 2895 | #else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2896 | gui.text_gc = gdk_gc_new(gui.drawarea->window); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 2897 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2898 | |
| 2899 | gui.blank_pointer = create_blank_pointer(); |
| 2900 | if (gui.pointer_hidden) |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 2901 | gdk_window_set_cursor(gtk_widget_get_window(widget), gui.blank_pointer); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2902 | |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 2903 | // get the actual size of the scrollbars, if they are realized |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2904 | sbar = firstwin->w_scrollbars[SBAR_LEFT].id; |
| 2905 | if (!sbar || (!gui.which_scrollbars[SBAR_LEFT] |
| 2906 | && firstwin->w_scrollbars[SBAR_RIGHT].id)) |
| 2907 | sbar = firstwin->w_scrollbars[SBAR_RIGHT].id; |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 2908 | gtk_widget_get_allocation(sbar, &allocation); |
| 2909 | if (sbar && gtk_widget_get_realized(sbar) && allocation.width) |
| 2910 | gui.scrollbar_width = allocation.width; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2911 | |
| 2912 | sbar = gui.bottom_sbar.id; |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 2913 | if (sbar && gtk_widget_get_realized(sbar) && allocation.height) |
| 2914 | gui.scrollbar_height = allocation.height; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2915 | } |
| 2916 | |
| 2917 | /* |
| 2918 | * Properly clean up on shutdown. |
| 2919 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2920 | static void |
Bram Moolenaar | b85cb21 | 2009-05-17 14:24:23 +0000 | [diff] [blame] | 2921 | drawarea_unrealize_cb(GtkWidget *widget UNUSED, gpointer data UNUSED) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2922 | { |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 2923 | // Don't write messages to the GUI anymore |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2924 | full_screen = FALSE; |
| 2925 | |
| 2926 | #ifdef FEAT_XIM |
| 2927 | im_shutdown(); |
| 2928 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2929 | if (gui.ascii_glyphs != NULL) |
| 2930 | { |
| 2931 | pango_glyph_string_free(gui.ascii_glyphs); |
| 2932 | gui.ascii_glyphs = NULL; |
| 2933 | } |
| 2934 | if (gui.ascii_font != NULL) |
| 2935 | { |
| 2936 | g_object_unref(gui.ascii_font); |
| 2937 | gui.ascii_font = NULL; |
| 2938 | } |
| 2939 | g_object_unref(gui.text_context); |
| 2940 | gui.text_context = NULL; |
| 2941 | |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 2942 | #if GTK_CHECK_VERSION(3,0,0) |
| 2943 | if (gui.surface != NULL) |
| 2944 | { |
| 2945 | cairo_surface_destroy(gui.surface); |
| 2946 | gui.surface = NULL; |
| 2947 | } |
| 2948 | #else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2949 | g_object_unref(gui.text_gc); |
| 2950 | gui.text_gc = NULL; |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 2951 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2952 | |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 2953 | #if GTK_CHECK_VERSION(3,0,0) |
| 2954 | g_object_unref(G_OBJECT(gui.blank_pointer)); |
| 2955 | #else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2956 | gdk_cursor_unref(gui.blank_pointer); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 2957 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2958 | gui.blank_pointer = NULL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2959 | } |
| 2960 | |
Bram Moolenaar | a859f04 | 2016-11-17 19:11:55 +0100 | [diff] [blame] | 2961 | #if GTK_CHECK_VERSION(3,22,2) |
| 2962 | static void |
| 2963 | drawarea_style_updated_cb(GtkWidget *widget UNUSED, |
| 2964 | gpointer data UNUSED) |
| 2965 | #else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2966 | static void |
Bram Moolenaar | b85cb21 | 2009-05-17 14:24:23 +0000 | [diff] [blame] | 2967 | drawarea_style_set_cb(GtkWidget *widget UNUSED, |
| 2968 | GtkStyle *previous_style UNUSED, |
| 2969 | gpointer data UNUSED) |
Bram Moolenaar | a859f04 | 2016-11-17 19:11:55 +0100 | [diff] [blame] | 2970 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2971 | { |
| 2972 | gui_mch_new_colors(); |
| 2973 | } |
| 2974 | |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 2975 | #if GTK_CHECK_VERSION(3,0,0) |
| 2976 | static gboolean |
| 2977 | drawarea_configure_event_cb(GtkWidget *widget, |
| 2978 | GdkEventConfigure *event, |
| 2979 | gpointer data UNUSED) |
| 2980 | { |
| 2981 | static int cur_width = 0; |
| 2982 | static int cur_height = 0; |
| 2983 | |
| 2984 | g_return_val_if_fail(event |
| 2985 | && event->width >= 1 && event->height >= 1, TRUE); |
| 2986 | |
Bram Moolenaar | 182707a | 2016-11-21 20:55:58 +0100 | [diff] [blame] | 2987 | # if GTK_CHECK_VERSION(3,22,2) && !GTK_CHECK_VERSION(3,22,4) |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 2988 | // As of 3.22.2, GdkWindows have started distributing configure events to |
| 2989 | // their "native" children (https://git.gnome.org/browse/gtk+/commit/?h=gtk-3-22&id=12579fe71b3b8f79eb9c1b80e429443bcc437dd0). |
| 2990 | // |
| 2991 | // As can be seen from the implementation of move_native_children() and |
| 2992 | // configure_native_child() in gdkwindow.c, those functions actually |
| 2993 | // propagate configure events to every child, failing to distinguish |
| 2994 | // "native" one from non-native one. |
| 2995 | // |
| 2996 | // Naturally, configure events propagated to here like that are fallacious |
| 2997 | // and, as a matter of fact, they trigger a geometric collapse of |
| 2998 | // gui.drawarea in fullscreen and maximized modes. |
| 2999 | // |
| 3000 | // To filter out such nuisance events, we are making use of the fact that |
| 3001 | // the field send_event of such GdkEventConfigures is set to FALSE in |
| 3002 | // configure_native_child(). |
| 3003 | // |
| 3004 | // Obviously, this is a terrible hack making GVim depend on GTK's |
| 3005 | // implementation details. Therefore, watch out any relevant internal |
| 3006 | // changes happening in GTK in the feature (sigh). |
| 3007 | // |
| 3008 | // Follow-up |
| 3009 | // After a few weeks later, the GdkWindow change mentioned above was |
| 3010 | // reverted (https://git.gnome.org/browse/gtk+/commit/?h=gtk-3-22&id=f70039cb9603a02d2369fec4038abf40a1711155). |
| 3011 | // The corresponding official release is 3.22.4. |
Bram Moolenaar | a859f04 | 2016-11-17 19:11:55 +0100 | [diff] [blame] | 3012 | if (event->send_event == FALSE) |
| 3013 | return TRUE; |
| 3014 | # endif |
| 3015 | |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 3016 | if (event->width == cur_width && event->height == cur_height) |
| 3017 | return TRUE; |
| 3018 | |
| 3019 | cur_width = event->width; |
| 3020 | cur_height = event->height; |
| 3021 | |
| 3022 | if (gui.surface != NULL) |
| 3023 | cairo_surface_destroy(gui.surface); |
| 3024 | |
| 3025 | gui.surface = gdk_window_create_similar_surface( |
| 3026 | gtk_widget_get_window(widget), |
| 3027 | CAIRO_CONTENT_COLOR_ALPHA, |
| 3028 | event->width, event->height); |
| 3029 | |
| 3030 | gtk_widget_queue_draw(widget); |
| 3031 | |
| 3032 | return TRUE; |
| 3033 | } |
| 3034 | #endif |
| 3035 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3036 | /* |
| 3037 | * Callback routine for the "delete_event" signal on the toplevel window. |
| 3038 | * Tries to vim gracefully, or refuses to exit with changed buffers. |
| 3039 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3040 | static gint |
Bram Moolenaar | b85cb21 | 2009-05-17 14:24:23 +0000 | [diff] [blame] | 3041 | delete_event_cb(GtkWidget *widget UNUSED, |
| 3042 | GdkEventAny *event UNUSED, |
| 3043 | gpointer data UNUSED) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3044 | { |
| 3045 | gui_shell_closed(); |
| 3046 | return TRUE; |
| 3047 | } |
| 3048 | |
Bram Moolenaar | 32466aa | 2006-02-24 23:53:04 +0000 | [diff] [blame] | 3049 | #if defined(FEAT_MENU) || defined(FEAT_TOOLBAR) || defined(FEAT_GUI_TABLINE) |
| 3050 | static int |
| 3051 | get_item_dimensions(GtkWidget *widget, GtkOrientation orientation) |
| 3052 | { |
Bram Moolenaar | c4a249a | 2017-01-30 22:56:48 +0100 | [diff] [blame] | 3053 | # ifdef FEAT_GUI_GNOME |
Bram Moolenaar | 32466aa | 2006-02-24 23:53:04 +0000 | [diff] [blame] | 3054 | GtkOrientation item_orientation = GTK_ORIENTATION_HORIZONTAL; |
| 3055 | |
Bram Moolenaar | 32466aa | 2006-02-24 23:53:04 +0000 | [diff] [blame] | 3056 | if (using_gnome && widget != NULL) |
| 3057 | { |
Bram Moolenaar | c9b4b05 | 2006-04-30 18:54:39 +0000 | [diff] [blame] | 3058 | GtkWidget *parent; |
Bram Moolenaar | 32466aa | 2006-02-24 23:53:04 +0000 | [diff] [blame] | 3059 | BonoboDockItem *dockitem; |
| 3060 | |
Bram Moolenaar | b3656ed | 2006-03-20 21:59:49 +0000 | [diff] [blame] | 3061 | parent = gtk_widget_get_parent(widget); |
Bram Moolenaar | c9b4b05 | 2006-04-30 18:54:39 +0000 | [diff] [blame] | 3062 | if (G_TYPE_FROM_INSTANCE(parent) == BONOBO_TYPE_DOCK_ITEM) |
| 3063 | { |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 3064 | // Only menu & toolbar are dock items. Could tabline be? |
| 3065 | // Seem to be only the 2 defined in GNOME |
Bram Moolenaar | c9b4b05 | 2006-04-30 18:54:39 +0000 | [diff] [blame] | 3066 | widget = parent; |
| 3067 | dockitem = BONOBO_DOCK_ITEM(widget); |
Bram Moolenaar | 32466aa | 2006-02-24 23:53:04 +0000 | [diff] [blame] | 3068 | |
Bram Moolenaar | c9b4b05 | 2006-04-30 18:54:39 +0000 | [diff] [blame] | 3069 | if (dockitem == NULL || dockitem->is_floating) |
| 3070 | return 0; |
| 3071 | item_orientation = bonobo_dock_item_get_orientation(dockitem); |
| 3072 | } |
Bram Moolenaar | 32466aa | 2006-02-24 23:53:04 +0000 | [diff] [blame] | 3073 | } |
Bram Moolenaar | c4a249a | 2017-01-30 22:56:48 +0100 | [diff] [blame] | 3074 | # else |
| 3075 | # define item_orientation GTK_ORIENTATION_HORIZONTAL |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 3076 | # endif |
Bram Moolenaar | c4a249a | 2017-01-30 22:56:48 +0100 | [diff] [blame] | 3077 | |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 3078 | if (widget != NULL |
| 3079 | && item_orientation == orientation |
| 3080 | && gtk_widget_get_realized(widget) |
| 3081 | && gtk_widget_get_visible(widget)) |
Bram Moolenaar | 32466aa | 2006-02-24 23:53:04 +0000 | [diff] [blame] | 3082 | { |
Bram Moolenaar | 664323e | 2018-09-18 22:30:07 +0200 | [diff] [blame] | 3083 | # if GTK_CHECK_VERSION(3,0,0) || !defined(FEAT_GUI_GNOME) |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 3084 | GtkAllocation allocation; |
| 3085 | |
| 3086 | gtk_widget_get_allocation(widget, &allocation); |
Bram Moolenaar | c4a249a | 2017-01-30 22:56:48 +0100 | [diff] [blame] | 3087 | return allocation.height; |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 3088 | # else |
Bram Moolenaar | 32466aa | 2006-02-24 23:53:04 +0000 | [diff] [blame] | 3089 | if (orientation == GTK_ORIENTATION_HORIZONTAL) |
| 3090 | return widget->allocation.height; |
| 3091 | else |
| 3092 | return widget->allocation.width; |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 3093 | # endif |
Bram Moolenaar | 32466aa | 2006-02-24 23:53:04 +0000 | [diff] [blame] | 3094 | } |
| 3095 | return 0; |
| 3096 | } |
| 3097 | #endif |
| 3098 | |
| 3099 | static int |
| 3100 | get_menu_tool_width(void) |
| 3101 | { |
| 3102 | int width = 0; |
| 3103 | |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 3104 | #ifdef FEAT_GUI_GNOME // these are never vertical without GNOME |
Bram Moolenaar | 32466aa | 2006-02-24 23:53:04 +0000 | [diff] [blame] | 3105 | # ifdef FEAT_MENU |
| 3106 | width += get_item_dimensions(gui.menubar, GTK_ORIENTATION_VERTICAL); |
| 3107 | # endif |
| 3108 | # ifdef FEAT_TOOLBAR |
| 3109 | width += get_item_dimensions(gui.toolbar, GTK_ORIENTATION_VERTICAL); |
| 3110 | # endif |
| 3111 | # ifdef FEAT_GUI_TABLINE |
Bram Moolenaar | 97b2ad3 | 2006-03-18 21:40:56 +0000 | [diff] [blame] | 3112 | if (gui.tabline != NULL) |
| 3113 | width += get_item_dimensions(gui.tabline, GTK_ORIENTATION_VERTICAL); |
Bram Moolenaar | 32466aa | 2006-02-24 23:53:04 +0000 | [diff] [blame] | 3114 | # endif |
| 3115 | #endif |
| 3116 | |
| 3117 | return width; |
| 3118 | } |
| 3119 | |
| 3120 | static int |
| 3121 | get_menu_tool_height(void) |
| 3122 | { |
| 3123 | int height = 0; |
| 3124 | |
| 3125 | #ifdef FEAT_MENU |
| 3126 | height += get_item_dimensions(gui.menubar, GTK_ORIENTATION_HORIZONTAL); |
| 3127 | #endif |
| 3128 | #ifdef FEAT_TOOLBAR |
| 3129 | height += get_item_dimensions(gui.toolbar, GTK_ORIENTATION_HORIZONTAL); |
| 3130 | #endif |
| 3131 | #ifdef FEAT_GUI_TABLINE |
Bram Moolenaar | 97b2ad3 | 2006-03-18 21:40:56 +0000 | [diff] [blame] | 3132 | if (gui.tabline != NULL) |
| 3133 | height += get_item_dimensions(gui.tabline, GTK_ORIENTATION_HORIZONTAL); |
Bram Moolenaar | 32466aa | 2006-02-24 23:53:04 +0000 | [diff] [blame] | 3134 | #endif |
| 3135 | |
| 3136 | return height; |
| 3137 | } |
| 3138 | |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 3139 | // This controls whether we can set the real window hints at |
| 3140 | // start-up when in a GtkPlug. |
| 3141 | // 0 = normal processing (default) |
| 3142 | // 1 = init. hints set, no-one's tried to reset since last check |
| 3143 | // 2 = init. hints set, attempt made to change hints |
Bram Moolenaar | b3656ed | 2006-03-20 21:59:49 +0000 | [diff] [blame] | 3144 | static int init_window_hints_state = 0; |
| 3145 | |
Bram Moolenaar | 32466aa | 2006-02-24 23:53:04 +0000 | [diff] [blame] | 3146 | static void |
Bram Moolenaar | b3656ed | 2006-03-20 21:59:49 +0000 | [diff] [blame] | 3147 | update_window_manager_hints(int force_width, int force_height) |
Bram Moolenaar | 32466aa | 2006-02-24 23:53:04 +0000 | [diff] [blame] | 3148 | { |
| 3149 | static int old_width = 0; |
| 3150 | static int old_height = 0; |
Bram Moolenaar | b3656ed | 2006-03-20 21:59:49 +0000 | [diff] [blame] | 3151 | static int old_min_width = 0; |
| 3152 | static int old_min_height = 0; |
Bram Moolenaar | 32466aa | 2006-02-24 23:53:04 +0000 | [diff] [blame] | 3153 | static int old_char_width = 0; |
| 3154 | static int old_char_height = 0; |
| 3155 | |
| 3156 | int width; |
| 3157 | int height; |
Bram Moolenaar | b3656ed | 2006-03-20 21:59:49 +0000 | [diff] [blame] | 3158 | int min_width; |
| 3159 | int min_height; |
| 3160 | |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 3161 | // At start-up, don't try to set the hints until the initial |
| 3162 | // values have been used (those that dictate our initial size) |
| 3163 | // Let forced (i.e., correct) values through always. |
Bram Moolenaar | b3656ed | 2006-03-20 21:59:49 +0000 | [diff] [blame] | 3164 | if (!(force_width && force_height) && init_window_hints_state > 0) |
| 3165 | { |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 3166 | // Don't do it! |
Bram Moolenaar | c9b4b05 | 2006-04-30 18:54:39 +0000 | [diff] [blame] | 3167 | init_window_hints_state = 2; |
| 3168 | return; |
Bram Moolenaar | b3656ed | 2006-03-20 21:59:49 +0000 | [diff] [blame] | 3169 | } |
Bram Moolenaar | 32466aa | 2006-02-24 23:53:04 +0000 | [diff] [blame] | 3170 | |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 3171 | // This also needs to be done when the main window isn't there yet, |
| 3172 | // otherwise the hints don't work. |
Bram Moolenaar | 32466aa | 2006-02-24 23:53:04 +0000 | [diff] [blame] | 3173 | width = gui_get_base_width(); |
| 3174 | height = gui_get_base_height(); |
| 3175 | # ifdef FEAT_MENU |
| 3176 | height += tabline_height() * gui.char_height; |
| 3177 | # endif |
Bram Moolenaar | 32466aa | 2006-02-24 23:53:04 +0000 | [diff] [blame] | 3178 | width += get_menu_tool_width(); |
| 3179 | height += get_menu_tool_height(); |
Bram Moolenaar | 32466aa | 2006-02-24 23:53:04 +0000 | [diff] [blame] | 3180 | |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 3181 | // GtkSockets use GtkPlug's [gui,mainwin] min-size hints to determine |
| 3182 | // their actual widget size. When we set our size ourselves (e.g., |
| 3183 | // 'set columns=' or init. -geom) we briefly set the min. to the size |
| 3184 | // we wish to be instead of the legitimate minimum so that we actually |
| 3185 | // resize correctly. |
Bram Moolenaar | b3656ed | 2006-03-20 21:59:49 +0000 | [diff] [blame] | 3186 | if (force_width && force_height) |
| 3187 | { |
Bram Moolenaar | c9b4b05 | 2006-04-30 18:54:39 +0000 | [diff] [blame] | 3188 | min_width = force_width; |
| 3189 | min_height = force_height; |
Bram Moolenaar | b3656ed | 2006-03-20 21:59:49 +0000 | [diff] [blame] | 3190 | } |
| 3191 | else |
| 3192 | { |
Bram Moolenaar | c9b4b05 | 2006-04-30 18:54:39 +0000 | [diff] [blame] | 3193 | min_width = width + MIN_COLUMNS * gui.char_width; |
| 3194 | min_height = height + MIN_LINES * gui.char_height; |
Bram Moolenaar | b3656ed | 2006-03-20 21:59:49 +0000 | [diff] [blame] | 3195 | } |
| 3196 | |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 3197 | // Avoid an expose event when the size didn't change. |
Bram Moolenaar | 32466aa | 2006-02-24 23:53:04 +0000 | [diff] [blame] | 3198 | if (width != old_width |
| 3199 | || height != old_height |
Bram Moolenaar | c9b4b05 | 2006-04-30 18:54:39 +0000 | [diff] [blame] | 3200 | || min_width != old_min_width |
Bram Moolenaar | b3656ed | 2006-03-20 21:59:49 +0000 | [diff] [blame] | 3201 | || min_height != old_min_height |
Bram Moolenaar | 32466aa | 2006-02-24 23:53:04 +0000 | [diff] [blame] | 3202 | || gui.char_width != old_char_width |
| 3203 | || gui.char_height != old_char_height) |
| 3204 | { |
| 3205 | GdkGeometry geometry; |
| 3206 | GdkWindowHints geometry_mask; |
| 3207 | |
| 3208 | geometry.width_inc = gui.char_width; |
| 3209 | geometry.height_inc = gui.char_height; |
| 3210 | geometry.base_width = width; |
| 3211 | geometry.base_height = height; |
Bram Moolenaar | c9b4b05 | 2006-04-30 18:54:39 +0000 | [diff] [blame] | 3212 | geometry.min_width = min_width; |
| 3213 | geometry.min_height = min_height; |
Bram Moolenaar | 32466aa | 2006-02-24 23:53:04 +0000 | [diff] [blame] | 3214 | geometry_mask = GDK_HINT_BASE_SIZE|GDK_HINT_RESIZE_INC |
| 3215 | |GDK_HINT_MIN_SIZE; |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 3216 | // Using gui.formwin as geometry widget doesn't work as expected |
| 3217 | // with GTK+ 2 -- dunno why. Presumably all the resizing hacks |
Bram Moolenaar | c0da540 | 2023-05-16 22:15:51 +0100 | [diff] [blame] | 3218 | // in Vim confuse GTK+. For GTK 3 the second argument should be NULL |
| 3219 | // to make the width/height inc works, despite the docs saying |
| 3220 | // something else. |
| 3221 | gtk_window_set_geometry_hints(GTK_WINDOW(gui.mainwin), NULL, |
Bram Moolenaar | 32466aa | 2006-02-24 23:53:04 +0000 | [diff] [blame] | 3222 | &geometry, geometry_mask); |
Bram Moolenaar | c9b4b05 | 2006-04-30 18:54:39 +0000 | [diff] [blame] | 3223 | old_width = width; |
| 3224 | old_height = height; |
| 3225 | old_min_width = min_width; |
| 3226 | old_min_height = min_height; |
| 3227 | old_char_width = gui.char_width; |
| 3228 | old_char_height = gui.char_height; |
Bram Moolenaar | 32466aa | 2006-02-24 23:53:04 +0000 | [diff] [blame] | 3229 | } |
| 3230 | } |
| 3231 | |
Bram Moolenaar | 50bf7ce | 2019-09-15 13:17:00 +0200 | [diff] [blame] | 3232 | #if defined(FEAT_GUI_DARKTHEME) || defined(PROTO) |
| 3233 | void |
| 3234 | gui_mch_set_dark_theme(int dark) |
| 3235 | { |
| 3236 | # if GTK_CHECK_VERSION(3,0,0) |
| 3237 | GtkSettings *gtk_settings; |
| 3238 | |
| 3239 | gtk_settings = gtk_settings_get_for_screen(gdk_screen_get_default()); |
| 3240 | g_object_set(gtk_settings, "gtk-application-prefer-dark-theme", (gboolean)dark, NULL); |
| 3241 | # endif |
| 3242 | } |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 3243 | #endif // FEAT_GUI_DARKTHEME |
Bram Moolenaar | 50bf7ce | 2019-09-15 13:17:00 +0200 | [diff] [blame] | 3244 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3245 | #ifdef FEAT_TOOLBAR |
| 3246 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3247 | /* |
| 3248 | * This extra effort wouldn't be necessary if we only used stock icons in the |
| 3249 | * toolbar, as we do for all builtin icons. But user-defined toolbar icons |
| 3250 | * shouldn't be treated differently, thus we do need this. |
| 3251 | */ |
| 3252 | static void |
| 3253 | icon_size_changed_foreach(GtkWidget *widget, gpointer user_data) |
| 3254 | { |
| 3255 | if (GTK_IS_IMAGE(widget)) |
| 3256 | { |
| 3257 | GtkImage *image = (GtkImage *)widget; |
| 3258 | |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 3259 | # if GTK_CHECK_VERSION(3,10,0) |
| 3260 | if (gtk_image_get_storage_type(image) == GTK_IMAGE_ICON_NAME) |
| 3261 | { |
| 3262 | const GtkIconSize icon_size = GPOINTER_TO_INT(user_data); |
| 3263 | const gchar *icon_name; |
| 3264 | |
| 3265 | gtk_image_get_icon_name(image, &icon_name, NULL); |
Bram Moolenaar | 81a4cf4 | 2020-09-09 19:05:13 +0200 | [diff] [blame] | 3266 | image = (GtkImage *)gtk_image_new_from_icon_name( |
| 3267 | icon_name, icon_size); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 3268 | } |
| 3269 | # else |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 3270 | // User-defined icons are stored in a GtkIconSet |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3271 | if (gtk_image_get_storage_type(image) == GTK_IMAGE_ICON_SET) |
| 3272 | { |
| 3273 | GtkIconSet *icon_set; |
| 3274 | GtkIconSize icon_size; |
| 3275 | |
| 3276 | gtk_image_get_icon_set(image, &icon_set, &icon_size); |
| 3277 | icon_size = (GtkIconSize)(long)user_data; |
| 3278 | |
| 3279 | gtk_icon_set_ref(icon_set); |
| 3280 | gtk_image_set_from_icon_set(image, icon_set, icon_size); |
| 3281 | gtk_icon_set_unref(icon_set); |
| 3282 | } |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 3283 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3284 | } |
| 3285 | else if (GTK_IS_CONTAINER(widget)) |
| 3286 | { |
| 3287 | gtk_container_foreach((GtkContainer *)widget, |
| 3288 | &icon_size_changed_foreach, |
| 3289 | user_data); |
| 3290 | } |
| 3291 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3292 | |
| 3293 | static void |
| 3294 | set_toolbar_style(GtkToolbar *toolbar) |
| 3295 | { |
| 3296 | GtkToolbarStyle style; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3297 | GtkIconSize size; |
| 3298 | GtkIconSize oldsize; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3299 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3300 | if ((toolbar_flags & (TOOLBAR_TEXT | TOOLBAR_ICONS | TOOLBAR_HORIZ)) |
| 3301 | == (TOOLBAR_TEXT | TOOLBAR_ICONS | TOOLBAR_HORIZ)) |
| 3302 | style = GTK_TOOLBAR_BOTH_HORIZ; |
Bram Moolenaar | 182c5be | 2010-06-25 05:37:59 +0200 | [diff] [blame] | 3303 | else if ((toolbar_flags & (TOOLBAR_TEXT | TOOLBAR_ICONS)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3304 | == (TOOLBAR_TEXT | TOOLBAR_ICONS)) |
| 3305 | style = GTK_TOOLBAR_BOTH; |
| 3306 | else if (toolbar_flags & TOOLBAR_TEXT) |
| 3307 | style = GTK_TOOLBAR_TEXT; |
| 3308 | else |
| 3309 | style = GTK_TOOLBAR_ICONS; |
| 3310 | |
| 3311 | gtk_toolbar_set_style(toolbar, style); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 3312 | # if !GTK_CHECK_VERSION(3,0,0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3313 | gtk_toolbar_set_tooltips(toolbar, (toolbar_flags & TOOLBAR_TOOLTIPS) != 0); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 3314 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3315 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3316 | switch (tbis_flags) |
| 3317 | { |
| 3318 | case TBIS_TINY: size = GTK_ICON_SIZE_MENU; break; |
| 3319 | case TBIS_SMALL: size = GTK_ICON_SIZE_SMALL_TOOLBAR; break; |
| 3320 | case TBIS_MEDIUM: size = GTK_ICON_SIZE_BUTTON; break; |
| 3321 | case TBIS_LARGE: size = GTK_ICON_SIZE_LARGE_TOOLBAR; break; |
Bram Moolenaar | beb003b | 2016-03-08 22:47:17 +0100 | [diff] [blame] | 3322 | case TBIS_HUGE: size = GTK_ICON_SIZE_DND; break; |
| 3323 | case TBIS_GIANT: size = GTK_ICON_SIZE_DIALOG; break; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3324 | default: size = GTK_ICON_SIZE_INVALID; break; |
| 3325 | } |
| 3326 | oldsize = gtk_toolbar_get_icon_size(toolbar); |
| 3327 | |
| 3328 | if (size == GTK_ICON_SIZE_INVALID) |
| 3329 | { |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 3330 | // Let global user preferences decide the icon size. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3331 | gtk_toolbar_unset_icon_size(toolbar); |
| 3332 | size = gtk_toolbar_get_icon_size(toolbar); |
| 3333 | } |
| 3334 | if (size != oldsize) |
| 3335 | { |
| 3336 | gtk_container_foreach(GTK_CONTAINER(toolbar), |
| 3337 | &icon_size_changed_foreach, |
| 3338 | GINT_TO_POINTER((int)size)); |
| 3339 | } |
| 3340 | gtk_toolbar_set_icon_size(toolbar, size); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3341 | } |
| 3342 | |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 3343 | #endif // FEAT_TOOLBAR |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3344 | |
Bram Moolenaar | 32466aa | 2006-02-24 23:53:04 +0000 | [diff] [blame] | 3345 | #if defined(FEAT_GUI_TABLINE) || defined(PROTO) |
| 3346 | static int ignore_tabline_evt = FALSE; |
Bram Moolenaar | a562149 | 2006-02-25 21:55:24 +0000 | [diff] [blame] | 3347 | static GtkWidget *tabline_menu; |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 3348 | # if !GTK_CHECK_VERSION(3,0,0) |
Bram Moolenaar | 437df8f | 2006-04-27 21:47:44 +0000 | [diff] [blame] | 3349 | static GtkTooltips *tabline_tooltip; |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 3350 | # endif |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 3351 | static int clicked_page; // page clicked in tab line |
Bram Moolenaar | a562149 | 2006-02-25 21:55:24 +0000 | [diff] [blame] | 3352 | |
| 3353 | /* |
| 3354 | * Handle selecting an item in the tab line popup menu. |
| 3355 | */ |
Bram Moolenaar | a562149 | 2006-02-25 21:55:24 +0000 | [diff] [blame] | 3356 | static void |
Bram Moolenaar | b85cb21 | 2009-05-17 14:24:23 +0000 | [diff] [blame] | 3357 | tabline_menu_handler(GtkMenuItem *item UNUSED, gpointer user_data) |
Bram Moolenaar | a562149 | 2006-02-25 21:55:24 +0000 | [diff] [blame] | 3358 | { |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 3359 | // Add the string cmd into input buffer |
Bram Moolenaar | c6fe919 | 2006-04-09 21:54:49 +0000 | [diff] [blame] | 3360 | send_tabline_menu_event(clicked_page, (int)(long)user_data); |
Bram Moolenaar | a562149 | 2006-02-25 21:55:24 +0000 | [diff] [blame] | 3361 | } |
| 3362 | |
Bram Moolenaar | 8ea9123 | 2006-04-28 22:41:43 +0000 | [diff] [blame] | 3363 | static void |
| 3364 | add_tabline_menu_item(GtkWidget *menu, char_u *text, int resp) |
| 3365 | { |
| 3366 | GtkWidget *item; |
| 3367 | char_u *utf_text; |
| 3368 | |
| 3369 | utf_text = CONVERT_TO_UTF8(text); |
| 3370 | item = gtk_menu_item_new_with_label((const char *)utf_text); |
| 3371 | gtk_widget_show(item); |
| 3372 | CONVERT_TO_UTF8_FREE(utf_text); |
| 3373 | |
| 3374 | gtk_container_add(GTK_CONTAINER(menu), item); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 3375 | g_signal_connect(G_OBJECT(item), "activate", |
| 3376 | G_CALLBACK(tabline_menu_handler), |
| 3377 | GINT_TO_POINTER(resp)); |
Bram Moolenaar | 8ea9123 | 2006-04-28 22:41:43 +0000 | [diff] [blame] | 3378 | } |
| 3379 | |
Bram Moolenaar | a562149 | 2006-02-25 21:55:24 +0000 | [diff] [blame] | 3380 | /* |
Bram Moolenaar | a562149 | 2006-02-25 21:55:24 +0000 | [diff] [blame] | 3381 | * Create a menu for the tab line. |
| 3382 | */ |
| 3383 | static GtkWidget * |
| 3384 | create_tabline_menu(void) |
| 3385 | { |
Bram Moolenaar | 8ea9123 | 2006-04-28 22:41:43 +0000 | [diff] [blame] | 3386 | GtkWidget *menu; |
Bram Moolenaar | a562149 | 2006-02-25 21:55:24 +0000 | [diff] [blame] | 3387 | |
| 3388 | menu = gtk_menu_new(); |
Bram Moolenaar | 2954719 | 2018-12-11 20:39:19 +0100 | [diff] [blame] | 3389 | add_tabline_menu_item(menu, (char_u *)_("Close tab"), TABLINE_MENU_CLOSE); |
Bram Moolenaar | 8ea9123 | 2006-04-28 22:41:43 +0000 | [diff] [blame] | 3390 | add_tabline_menu_item(menu, (char_u *)_("New tab"), TABLINE_MENU_NEW); |
| 3391 | add_tabline_menu_item(menu, (char_u *)_("Open Tab..."), TABLINE_MENU_OPEN); |
Bram Moolenaar | a562149 | 2006-02-25 21:55:24 +0000 | [diff] [blame] | 3392 | |
| 3393 | return menu; |
| 3394 | } |
| 3395 | |
| 3396 | static gboolean |
| 3397 | on_tabline_menu(GtkWidget *widget, GdkEvent *event) |
| 3398 | { |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 3399 | // Was this button press event ? |
Bram Moolenaar | a562149 | 2006-02-25 21:55:24 +0000 | [diff] [blame] | 3400 | if (event->type == GDK_BUTTON_PRESS) |
| 3401 | { |
| 3402 | GdkEventButton *bevent = (GdkEventButton *)event; |
| 3403 | int x = bevent->x; |
Bram Moolenaar | 8ea9123 | 2006-04-28 22:41:43 +0000 | [diff] [blame] | 3404 | int y = bevent->y; |
| 3405 | GtkWidget *tabwidget; |
| 3406 | GdkWindow *tabwin; |
Bram Moolenaar | a562149 | 2006-02-25 21:55:24 +0000 | [diff] [blame] | 3407 | |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 3408 | // When ignoring events return TRUE so that the selected page doesn't |
| 3409 | // change. |
Martin Tournoij | 7904fa4 | 2022-10-04 16:28:45 +0100 | [diff] [blame] | 3410 | if (hold_gui_events || cmdwin_type != 0) |
Bram Moolenaar | f193fff | 2006-04-27 00:02:13 +0000 | [diff] [blame] | 3411 | return TRUE; |
| 3412 | |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 3413 | tabwin = gui_gtk_window_at_position(gui.mainwin, &x, &y); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 3414 | |
Bram Moolenaar | 8ea9123 | 2006-04-28 22:41:43 +0000 | [diff] [blame] | 3415 | gdk_window_get_user_data(tabwin, (gpointer)&tabwidget); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 3416 | clicked_page = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(tabwidget), |
| 3417 | "tab_num")); |
Bram Moolenaar | a562149 | 2006-02-25 21:55:24 +0000 | [diff] [blame] | 3418 | |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 3419 | // If the event was generated for 3rd button popup the menu. |
Bram Moolenaar | a562149 | 2006-02-25 21:55:24 +0000 | [diff] [blame] | 3420 | if (bevent->button == 3) |
| 3421 | { |
Bram Moolenaar | a859f04 | 2016-11-17 19:11:55 +0100 | [diff] [blame] | 3422 | # if GTK_CHECK_VERSION(3,22,2) |
| 3423 | gtk_menu_popup_at_pointer(GTK_MENU(widget), event); |
| 3424 | # else |
Bram Moolenaar | a562149 | 2006-02-25 21:55:24 +0000 | [diff] [blame] | 3425 | gtk_menu_popup(GTK_MENU(widget), NULL, NULL, NULL, NULL, |
| 3426 | bevent->button, bevent->time); |
Bram Moolenaar | a859f04 | 2016-11-17 19:11:55 +0100 | [diff] [blame] | 3427 | # endif |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 3428 | // We handled the event. |
Bram Moolenaar | a562149 | 2006-02-25 21:55:24 +0000 | [diff] [blame] | 3429 | return TRUE; |
| 3430 | } |
Bram Moolenaar | 9635157 | 2006-05-05 21:16:59 +0000 | [diff] [blame] | 3431 | else if (bevent->button == 1) |
Bram Moolenaar | eddf53b | 2006-02-27 00:11:10 +0000 | [diff] [blame] | 3432 | { |
Bram Moolenaar | 9635157 | 2006-05-05 21:16:59 +0000 | [diff] [blame] | 3433 | if (clicked_page == 0) |
| 3434 | { |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 3435 | // Click after all tabs moves to next tab page. When "x" is |
| 3436 | // small guess it's the left button. |
Bram Moolenaar | a3f4166 | 2010-07-11 19:01:06 +0200 | [diff] [blame] | 3437 | send_tabline_event(x < 50 ? -1 : 0); |
Bram Moolenaar | 9635157 | 2006-05-05 21:16:59 +0000 | [diff] [blame] | 3438 | } |
Bram Moolenaar | eddf53b | 2006-02-27 00:11:10 +0000 | [diff] [blame] | 3439 | } |
regomne | 3bdef10 | 2022-09-26 20:48:32 +0100 | [diff] [blame] | 3440 | else if (bevent->button == 2) |
| 3441 | { |
| 3442 | if (clicked_page != 0) |
| 3443 | // Middle mouse click on tabpage label closes that tab. |
| 3444 | send_tabline_menu_event(clicked_page, TABLINE_MENU_CLOSE); |
| 3445 | } |
Bram Moolenaar | a562149 | 2006-02-25 21:55:24 +0000 | [diff] [blame] | 3446 | } |
Bram Moolenaar | f193fff | 2006-04-27 00:02:13 +0000 | [diff] [blame] | 3447 | |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 3448 | // We didn't handle the event. |
Bram Moolenaar | a562149 | 2006-02-25 21:55:24 +0000 | [diff] [blame] | 3449 | return FALSE; |
| 3450 | } |
Bram Moolenaar | 32466aa | 2006-02-24 23:53:04 +0000 | [diff] [blame] | 3451 | |
| 3452 | /* |
| 3453 | * Handle selecting one of the tabs. |
| 3454 | */ |
Bram Moolenaar | 32466aa | 2006-02-24 23:53:04 +0000 | [diff] [blame] | 3455 | static void |
| 3456 | on_select_tab( |
Bram Moolenaar | b85cb21 | 2009-05-17 14:24:23 +0000 | [diff] [blame] | 3457 | GtkNotebook *notebook UNUSED, |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 3458 | gpointer *page UNUSED, |
Bram Moolenaar | 89d4032 | 2006-08-29 15:30:07 +0000 | [diff] [blame] | 3459 | gint idx, |
Bram Moolenaar | b85cb21 | 2009-05-17 14:24:23 +0000 | [diff] [blame] | 3460 | gpointer data UNUSED) |
Bram Moolenaar | 32466aa | 2006-02-24 23:53:04 +0000 | [diff] [blame] | 3461 | { |
Bram Moolenaar | 32466aa | 2006-02-24 23:53:04 +0000 | [diff] [blame] | 3462 | if (!ignore_tabline_evt) |
Bram Moolenaar | a3f4166 | 2010-07-11 19:01:06 +0200 | [diff] [blame] | 3463 | send_tabline_event(idx + 1); |
Bram Moolenaar | ca05aa2 | 2017-10-22 15:36:14 +0200 | [diff] [blame] | 3464 | } |
| 3465 | |
Bram Moolenaar | 92cbf62 | 2018-09-19 22:40:03 +0200 | [diff] [blame] | 3466 | # if GTK_CHECK_VERSION(2,10,0) |
Bram Moolenaar | ca05aa2 | 2017-10-22 15:36:14 +0200 | [diff] [blame] | 3467 | /* |
| 3468 | * Handle reordering the tabs (using D&D). |
| 3469 | */ |
| 3470 | static void |
| 3471 | on_tab_reordered( |
| 3472 | GtkNotebook *notebook UNUSED, |
Bram Moolenaar | ca05aa2 | 2017-10-22 15:36:14 +0200 | [diff] [blame] | 3473 | gpointer *page UNUSED, |
Bram Moolenaar | ca05aa2 | 2017-10-22 15:36:14 +0200 | [diff] [blame] | 3474 | gint idx, |
| 3475 | gpointer data UNUSED) |
| 3476 | { |
Yegappan Lakshmanan | 7f8b255 | 2023-01-08 13:44:24 +0000 | [diff] [blame] | 3477 | if (ignore_tabline_evt) |
| 3478 | return; |
| 3479 | |
| 3480 | if ((tabpage_index(curtab) - 1) < idx) |
| 3481 | tabpage_move(idx + 1); |
| 3482 | else |
| 3483 | tabpage_move(idx); |
Bram Moolenaar | 32466aa | 2006-02-24 23:53:04 +0000 | [diff] [blame] | 3484 | } |
Bram Moolenaar | 92cbf62 | 2018-09-19 22:40:03 +0200 | [diff] [blame] | 3485 | # endif |
Bram Moolenaar | 32466aa | 2006-02-24 23:53:04 +0000 | [diff] [blame] | 3486 | |
| 3487 | /* |
| 3488 | * Show or hide the tabline. |
| 3489 | */ |
| 3490 | void |
| 3491 | gui_mch_show_tabline(int showit) |
| 3492 | { |
| 3493 | if (gui.tabline == NULL) |
| 3494 | return; |
| 3495 | |
| 3496 | if (!showit != !gtk_notebook_get_show_tabs(GTK_NOTEBOOK(gui.tabline))) |
| 3497 | { |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 3498 | // Note: this may cause a resize event |
Bram Moolenaar | 32466aa | 2006-02-24 23:53:04 +0000 | [diff] [blame] | 3499 | gtk_notebook_set_show_tabs(GTK_NOTEBOOK(gui.tabline), showit); |
Bram Moolenaar | b3656ed | 2006-03-20 21:59:49 +0000 | [diff] [blame] | 3500 | update_window_manager_hints(0, 0); |
Bram Moolenaar | 9635157 | 2006-05-05 21:16:59 +0000 | [diff] [blame] | 3501 | if (showit) |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 3502 | gtk_widget_set_can_focus(GTK_WIDGET(gui.tabline), FALSE); |
Bram Moolenaar | 32466aa | 2006-02-24 23:53:04 +0000 | [diff] [blame] | 3503 | } |
Bram Moolenaar | 9635157 | 2006-05-05 21:16:59 +0000 | [diff] [blame] | 3504 | |
| 3505 | gui_mch_update(); |
Bram Moolenaar | 32466aa | 2006-02-24 23:53:04 +0000 | [diff] [blame] | 3506 | } |
| 3507 | |
| 3508 | /* |
Bram Moolenaar | 3517bb1 | 2006-03-03 22:58:45 +0000 | [diff] [blame] | 3509 | * Return TRUE when tabline is displayed. |
| 3510 | */ |
| 3511 | int |
| 3512 | gui_mch_showing_tabline(void) |
| 3513 | { |
| 3514 | return gui.tabline != NULL |
Bram Moolenaar | 182c5be | 2010-06-25 05:37:59 +0200 | [diff] [blame] | 3515 | && gtk_notebook_get_show_tabs(GTK_NOTEBOOK(gui.tabline)); |
Bram Moolenaar | 3517bb1 | 2006-03-03 22:58:45 +0000 | [diff] [blame] | 3516 | } |
| 3517 | |
| 3518 | /* |
Bram Moolenaar | 32466aa | 2006-02-24 23:53:04 +0000 | [diff] [blame] | 3519 | * Update the labels of the tabline. |
| 3520 | */ |
| 3521 | void |
| 3522 | gui_mch_update_tabline(void) |
| 3523 | { |
| 3524 | GtkWidget *page; |
Bram Moolenaar | 437df8f | 2006-04-27 21:47:44 +0000 | [diff] [blame] | 3525 | GtkWidget *event_box; |
Bram Moolenaar | 32466aa | 2006-02-24 23:53:04 +0000 | [diff] [blame] | 3526 | GtkWidget *label; |
| 3527 | tabpage_T *tp; |
| 3528 | int nr = 0; |
Bram Moolenaar | 8ea9123 | 2006-04-28 22:41:43 +0000 | [diff] [blame] | 3529 | int tab_num; |
Bram Moolenaar | 32466aa | 2006-02-24 23:53:04 +0000 | [diff] [blame] | 3530 | int curtabidx = 0; |
Bram Moolenaar | c1e3790 | 2006-04-18 21:55:01 +0000 | [diff] [blame] | 3531 | char_u *labeltext; |
Bram Moolenaar | 32466aa | 2006-02-24 23:53:04 +0000 | [diff] [blame] | 3532 | |
| 3533 | if (gui.tabline == NULL) |
| 3534 | return; |
| 3535 | |
| 3536 | ignore_tabline_evt = TRUE; |
| 3537 | |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 3538 | // Add a label for each tab page. They all contain the same text area. |
Bram Moolenaar | 32466aa | 2006-02-24 23:53:04 +0000 | [diff] [blame] | 3539 | for (tp = first_tabpage; tp != NULL; tp = tp->tp_next, ++nr) |
| 3540 | { |
| 3541 | if (tp == curtab) |
| 3542 | curtabidx = nr; |
| 3543 | |
Bram Moolenaar | 8ea9123 | 2006-04-28 22:41:43 +0000 | [diff] [blame] | 3544 | tab_num = nr + 1; |
| 3545 | |
Bram Moolenaar | 32466aa | 2006-02-24 23:53:04 +0000 | [diff] [blame] | 3546 | page = gtk_notebook_get_nth_page(GTK_NOTEBOOK(gui.tabline), nr); |
| 3547 | if (page == NULL) |
| 3548 | { |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 3549 | // Add notebook page |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 3550 | # if GTK_CHECK_VERSION(3,2,0) |
| 3551 | page = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0); |
| 3552 | gtk_box_set_homogeneous(GTK_BOX(page), FALSE); |
| 3553 | # else |
Bram Moolenaar | 32466aa | 2006-02-24 23:53:04 +0000 | [diff] [blame] | 3554 | page = gtk_vbox_new(FALSE, 0); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 3555 | # endif |
Bram Moolenaar | 32466aa | 2006-02-24 23:53:04 +0000 | [diff] [blame] | 3556 | gtk_widget_show(page); |
Bram Moolenaar | 437df8f | 2006-04-27 21:47:44 +0000 | [diff] [blame] | 3557 | event_box = gtk_event_box_new(); |
| 3558 | gtk_widget_show(event_box); |
Bram Moolenaar | 32466aa | 2006-02-24 23:53:04 +0000 | [diff] [blame] | 3559 | label = gtk_label_new("-Empty-"); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 3560 | # if !GTK_CHECK_VERSION(3,14,0) |
Bram Moolenaar | 8ea9123 | 2006-04-28 22:41:43 +0000 | [diff] [blame] | 3561 | gtk_misc_set_padding(GTK_MISC(label), 2, 2); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 3562 | # endif |
Bram Moolenaar | 437df8f | 2006-04-27 21:47:44 +0000 | [diff] [blame] | 3563 | gtk_container_add(GTK_CONTAINER(event_box), label); |
Bram Moolenaar | 32466aa | 2006-02-24 23:53:04 +0000 | [diff] [blame] | 3564 | gtk_widget_show(label); |
| 3565 | gtk_notebook_insert_page(GTK_NOTEBOOK(gui.tabline), |
| 3566 | page, |
Bram Moolenaar | 437df8f | 2006-04-27 21:47:44 +0000 | [diff] [blame] | 3567 | event_box, |
Bram Moolenaar | 32466aa | 2006-02-24 23:53:04 +0000 | [diff] [blame] | 3568 | nr++); |
Bram Moolenaar | 92cbf62 | 2018-09-19 22:40:03 +0200 | [diff] [blame] | 3569 | # if GTK_CHECK_VERSION(2,10,0) |
Bram Moolenaar | ca05aa2 | 2017-10-22 15:36:14 +0200 | [diff] [blame] | 3570 | gtk_notebook_set_tab_reorderable(GTK_NOTEBOOK(gui.tabline), |
| 3571 | page, |
| 3572 | TRUE); |
Bram Moolenaar | 92cbf62 | 2018-09-19 22:40:03 +0200 | [diff] [blame] | 3573 | # endif |
Bram Moolenaar | 32466aa | 2006-02-24 23:53:04 +0000 | [diff] [blame] | 3574 | } |
| 3575 | |
Bram Moolenaar | 437df8f | 2006-04-27 21:47:44 +0000 | [diff] [blame] | 3576 | event_box = gtk_notebook_get_tab_label(GTK_NOTEBOOK(gui.tabline), page); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 3577 | g_object_set_data(G_OBJECT(event_box), "tab_num", |
| 3578 | GINT_TO_POINTER(tab_num)); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 3579 | label = gtk_bin_get_child(GTK_BIN(event_box)); |
Bram Moolenaar | 57657d8 | 2006-04-21 22:12:41 +0000 | [diff] [blame] | 3580 | get_tabline_label(tp, FALSE); |
Bram Moolenaar | c1e3790 | 2006-04-18 21:55:01 +0000 | [diff] [blame] | 3581 | labeltext = CONVERT_TO_UTF8(NameBuff); |
Bram Moolenaar | 437df8f | 2006-04-27 21:47:44 +0000 | [diff] [blame] | 3582 | gtk_label_set_text(GTK_LABEL(label), (const char *)labeltext); |
Bram Moolenaar | c1e3790 | 2006-04-18 21:55:01 +0000 | [diff] [blame] | 3583 | CONVERT_TO_UTF8_FREE(labeltext); |
Bram Moolenaar | 437df8f | 2006-04-27 21:47:44 +0000 | [diff] [blame] | 3584 | |
Bram Moolenaar | 437df8f | 2006-04-27 21:47:44 +0000 | [diff] [blame] | 3585 | get_tabline_label(tp, TRUE); |
| 3586 | labeltext = CONVERT_TO_UTF8(NameBuff); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 3587 | # if GTK_CHECK_VERSION(3,0,0) |
| 3588 | gtk_widget_set_tooltip_text(event_box, (const gchar *)labeltext); |
| 3589 | # else |
Bram Moolenaar | 437df8f | 2006-04-27 21:47:44 +0000 | [diff] [blame] | 3590 | gtk_tooltips_set_tip(GTK_TOOLTIPS(tabline_tooltip), event_box, |
| 3591 | (const char *)labeltext, NULL); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 3592 | # endif |
Bram Moolenaar | 437df8f | 2006-04-27 21:47:44 +0000 | [diff] [blame] | 3593 | CONVERT_TO_UTF8_FREE(labeltext); |
Bram Moolenaar | 32466aa | 2006-02-24 23:53:04 +0000 | [diff] [blame] | 3594 | } |
| 3595 | |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 3596 | // Remove any old labels. |
Bram Moolenaar | 32466aa | 2006-02-24 23:53:04 +0000 | [diff] [blame] | 3597 | while (gtk_notebook_get_nth_page(GTK_NOTEBOOK(gui.tabline), nr) != NULL) |
| 3598 | gtk_notebook_remove_page(GTK_NOTEBOOK(gui.tabline), nr); |
| 3599 | |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 3600 | if (gtk_notebook_get_current_page(GTK_NOTEBOOK(gui.tabline)) != curtabidx) |
| 3601 | gtk_notebook_set_current_page(GTK_NOTEBOOK(gui.tabline), curtabidx); |
Bram Moolenaar | 32466aa | 2006-02-24 23:53:04 +0000 | [diff] [blame] | 3602 | |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 3603 | // Make sure everything is in place before drawing text. |
Bram Moolenaar | 8ea9123 | 2006-04-28 22:41:43 +0000 | [diff] [blame] | 3604 | gui_mch_update(); |
| 3605 | |
Bram Moolenaar | 32466aa | 2006-02-24 23:53:04 +0000 | [diff] [blame] | 3606 | ignore_tabline_evt = FALSE; |
| 3607 | } |
| 3608 | |
| 3609 | /* |
| 3610 | * Set the current tab to "nr". First tab is 1. |
| 3611 | */ |
| 3612 | void |
Bram Moolenaar | 66f948e | 2016-01-30 16:39:25 +0100 | [diff] [blame] | 3613 | gui_mch_set_curtab(int nr) |
Bram Moolenaar | 32466aa | 2006-02-24 23:53:04 +0000 | [diff] [blame] | 3614 | { |
| 3615 | if (gui.tabline == NULL) |
| 3616 | return; |
| 3617 | |
| 3618 | ignore_tabline_evt = TRUE; |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 3619 | if (gtk_notebook_get_current_page(GTK_NOTEBOOK(gui.tabline)) != nr - 1) |
| 3620 | gtk_notebook_set_current_page(GTK_NOTEBOOK(gui.tabline), nr - 1); |
Bram Moolenaar | 32466aa | 2006-02-24 23:53:04 +0000 | [diff] [blame] | 3621 | ignore_tabline_evt = FALSE; |
| 3622 | } |
| 3623 | |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 3624 | #endif // FEAT_GUI_TABLINE |
Bram Moolenaar | 32466aa | 2006-02-24 23:53:04 +0000 | [diff] [blame] | 3625 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3626 | /* |
Bram Moolenaar | a76638f | 2010-06-05 12:49:46 +0200 | [diff] [blame] | 3627 | * Add selection targets for PRIMARY and CLIPBOARD selections. |
| 3628 | */ |
| 3629 | void |
lilydjwg | 94ff09a | 2024-01-29 20:14:01 +0100 | [diff] [blame] | 3630 | gui_gtk_set_selection_targets(GdkAtom selection) |
Bram Moolenaar | a76638f | 2010-06-05 12:49:46 +0200 | [diff] [blame] | 3631 | { |
| 3632 | int i, j = 0; |
lilydjwg | 94ff09a | 2024-01-29 20:14:01 +0100 | [diff] [blame] | 3633 | static int n_targets = N_SELECTION_TARGETS; |
| 3634 | static GtkTargetEntry targets[N_SELECTION_TARGETS]; |
Bram Moolenaar | a76638f | 2010-06-05 12:49:46 +0200 | [diff] [blame] | 3635 | |
lilydjwg | 94ff09a | 2024-01-29 20:14:01 +0100 | [diff] [blame] | 3636 | if (targets[0].target == NULL) |
Bram Moolenaar | a76638f | 2010-06-05 12:49:46 +0200 | [diff] [blame] | 3637 | { |
lilydjwg | 94ff09a | 2024-01-29 20:14:01 +0100 | [diff] [blame] | 3638 | for (i = 0; i < (int)N_SELECTION_TARGETS; ++i) |
| 3639 | { |
| 3640 | // OpenOffice tries to use TARGET_HTML and fails when we don't |
| 3641 | // return something, instead of trying another target. Therefore only |
| 3642 | // offer TARGET_HTML when it works. |
| 3643 | if (!clip_html && selection_targets[i].info == TARGET_HTML) |
| 3644 | n_targets--; |
| 3645 | else |
| 3646 | targets[j++] = selection_targets[i]; |
| 3647 | } |
Bram Moolenaar | a76638f | 2010-06-05 12:49:46 +0200 | [diff] [blame] | 3648 | } |
| 3649 | |
lilydjwg | 94ff09a | 2024-01-29 20:14:01 +0100 | [diff] [blame] | 3650 | gtk_selection_clear_targets(gui.drawarea, selection); |
| 3651 | gtk_selection_add_targets(gui.drawarea, selection, |
Bram Moolenaar | a76638f | 2010-06-05 12:49:46 +0200 | [diff] [blame] | 3652 | targets, n_targets); |
| 3653 | } |
| 3654 | |
| 3655 | /* |
| 3656 | * Set up for receiving DND items. |
| 3657 | */ |
| 3658 | void |
| 3659 | gui_gtk_set_dnd_targets(void) |
| 3660 | { |
| 3661 | int i, j = 0; |
| 3662 | int n_targets = N_DND_TARGETS; |
| 3663 | GtkTargetEntry targets[N_DND_TARGETS]; |
| 3664 | |
| 3665 | for (i = 0; i < (int)N_DND_TARGETS; ++i) |
| 3666 | { |
Bram Moolenaar | b931d74 | 2011-10-26 11:36:25 +0200 | [diff] [blame] | 3667 | if (!clip_html && dnd_targets[i].info == TARGET_HTML) |
Bram Moolenaar | a76638f | 2010-06-05 12:49:46 +0200 | [diff] [blame] | 3668 | n_targets--; |
| 3669 | else |
Bram Moolenaar | a76638f | 2010-06-05 12:49:46 +0200 | [diff] [blame] | 3670 | targets[j++] = dnd_targets[i]; |
| 3671 | } |
| 3672 | |
| 3673 | gtk_drag_dest_unset(gui.drawarea); |
| 3674 | gtk_drag_dest_set(gui.drawarea, |
| 3675 | GTK_DEST_DEFAULT_ALL, |
| 3676 | targets, n_targets, |
Bram Moolenaar | ba7cc9f | 2011-02-25 17:10:27 +0100 | [diff] [blame] | 3677 | GDK_ACTION_COPY | GDK_ACTION_MOVE); |
Bram Moolenaar | a76638f | 2010-06-05 12:49:46 +0200 | [diff] [blame] | 3678 | } |
| 3679 | |
| 3680 | /* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3681 | * Initialize the GUI. Create all the windows, set up all the callbacks etc. |
| 3682 | * Returns OK for success, FAIL when the GUI can't be started. |
| 3683 | */ |
| 3684 | int |
| 3685 | gui_mch_init(void) |
| 3686 | { |
| 3687 | GtkWidget *vbox; |
| 3688 | |
| 3689 | #ifdef FEAT_GUI_GNOME |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 3690 | // Initialize the GNOME libraries. gnome_program_init()/gnome_init() |
| 3691 | // exits on failure, but that's a non-issue because we already called |
| 3692 | // gtk_init_check() in gui_mch_init_check(). |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3693 | if (using_gnome) |
Bram Moolenaar | 3be71ce | 2013-01-23 16:00:11 +0100 | [diff] [blame] | 3694 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3695 | gnome_program_init(VIMPACKAGE, VIM_VERSION_SHORT, |
| 3696 | LIBGNOMEUI_MODULE, gui_argc, gui_argv, NULL); |
Bram Moolenaar | 73e28dc | 2022-09-17 21:08:33 +0100 | [diff] [blame] | 3697 | # if defined(LC_NUMERIC) |
Bram Moolenaar | 1ff32c5 | 2014-04-29 15:11:43 +0200 | [diff] [blame] | 3698 | { |
| 3699 | char *p = setlocale(LC_NUMERIC, NULL); |
| 3700 | |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 3701 | // Make sure strtod() uses a decimal point, not a comma. Gnome |
| 3702 | // init may change it. |
Bram Moolenaar | 1ff32c5 | 2014-04-29 15:11:43 +0200 | [diff] [blame] | 3703 | if (p == NULL || strcmp(p, "C") != 0) |
| 3704 | setlocale(LC_NUMERIC, "C"); |
| 3705 | } |
Bram Moolenaar | 3be71ce | 2013-01-23 16:00:11 +0100 | [diff] [blame] | 3706 | # endif |
| 3707 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3708 | #endif |
Bram Moolenaar | d23a823 | 2018-02-10 18:45:26 +0100 | [diff] [blame] | 3709 | VIM_CLEAR(gui_argv); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3710 | |
Bram Moolenaar | 182c5be | 2010-06-25 05:37:59 +0200 | [diff] [blame] | 3711 | #if GLIB_CHECK_VERSION(2,1,3) |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 3712 | // Set the human-readable application name |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3713 | g_set_application_name("Vim"); |
Bram Moolenaar | 182c5be | 2010-06-25 05:37:59 +0200 | [diff] [blame] | 3714 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3715 | /* |
| 3716 | * Force UTF-8 output no matter what the value of 'encoding' is. |
| 3717 | * did_set_string_option() in option.c prohibits changing 'termencoding' |
| 3718 | * to something else than UTF-8 if the GUI is in use. |
| 3719 | */ |
Bram Moolenaar | 31e5c60 | 2022-04-15 13:53:33 +0100 | [diff] [blame] | 3720 | set_option_value_give_err((char_u *)"termencoding", |
| 3721 | 0L, (char_u *)"utf-8", 0); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3722 | |
Bram Moolenaar | 182c5be | 2010-06-25 05:37:59 +0200 | [diff] [blame] | 3723 | #ifdef FEAT_TOOLBAR |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3724 | gui_gtk_register_stock_icons(); |
Bram Moolenaar | 182c5be | 2010-06-25 05:37:59 +0200 | [diff] [blame] | 3725 | #endif |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 3726 | // FIXME: Need to install the classic icons and a gtkrc.classic file. |
| 3727 | // The hard part is deciding install locations and the Makefile magic. |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 3728 | #if !GTK_CHECK_VERSION(3,0,0) |
| 3729 | # if 0 |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3730 | gtk_rc_parse("gtkrc"); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 3731 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3732 | #endif |
| 3733 | |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 3734 | // Initialize values |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3735 | gui.border_width = 2; |
| 3736 | gui.scrollbar_width = SB_DEFAULT_WIDTH; |
| 3737 | gui.scrollbar_height = SB_DEFAULT_WIDTH; |
Bram Moolenaar | 36edf06 | 2016-07-21 22:10:12 +0200 | [diff] [blame] | 3738 | #if GTK_CHECK_VERSION(3,0,0) |
| 3739 | gui.fgcolor = g_new(GdkRGBA, 1); |
| 3740 | gui.bgcolor = g_new(GdkRGBA, 1); |
| 3741 | gui.spcolor = g_new(GdkRGBA, 1); |
| 3742 | #else |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 3743 | // LINTED: avoid warning: conversion to 'unsigned long' |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3744 | gui.fgcolor = g_new0(GdkColor, 1); |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 3745 | // LINTED: avoid warning: conversion to 'unsigned long' |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3746 | gui.bgcolor = g_new0(GdkColor, 1); |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 3747 | // LINTED: avoid warning: conversion to 'unsigned long' |
Bram Moolenaar | f36d369 | 2005-03-15 22:48:14 +0000 | [diff] [blame] | 3748 | gui.spcolor = g_new0(GdkColor, 1); |
Bram Moolenaar | 36edf06 | 2016-07-21 22:10:12 +0200 | [diff] [blame] | 3749 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3750 | |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 3751 | // Initialise atoms |
Bram Moolenaar | 3a6eaa5 | 2009-06-16 13:23:06 +0000 | [diff] [blame] | 3752 | html_atom = gdk_atom_intern("text/html", FALSE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3753 | utf8_string_atom = gdk_atom_intern("UTF8_STRING", FALSE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3754 | |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 3755 | // Set default foreground and background colors. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3756 | gui.norm_pixel = gui.def_norm_pixel; |
| 3757 | gui.back_pixel = gui.def_back_pixel; |
| 3758 | |
| 3759 | if (gtk_socket_id != 0) |
| 3760 | { |
| 3761 | GtkWidget *plug; |
| 3762 | |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 3763 | // Use GtkSocket from another app. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3764 | plug = gtk_plug_new_for_display(gdk_display_get_default(), |
| 3765 | gtk_socket_id); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 3766 | if (plug != NULL && gtk_plug_get_socket_window(GTK_PLUG(plug)) != NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3767 | { |
| 3768 | gui.mainwin = plug; |
| 3769 | } |
| 3770 | else |
| 3771 | { |
| 3772 | g_warning("Connection to GTK+ socket (ID %u) failed", |
| 3773 | (unsigned int)gtk_socket_id); |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 3774 | // Pretend we never wanted it if it failed (get own window) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3775 | gtk_socket_id = 0; |
| 3776 | } |
| 3777 | } |
| 3778 | |
| 3779 | if (gtk_socket_id == 0) |
| 3780 | { |
| 3781 | #ifdef FEAT_GUI_GNOME |
| 3782 | if (using_gnome) |
| 3783 | { |
| 3784 | gui.mainwin = gnome_app_new("Vim", NULL); |
| 3785 | # ifdef USE_XSMP |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 3786 | // Use the GNOME save-yourself functionality now. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3787 | xsmp_close(); |
| 3788 | # endif |
| 3789 | } |
| 3790 | else |
| 3791 | #endif |
| 3792 | gui.mainwin = gtk_window_new(GTK_WINDOW_TOPLEVEL); |
| 3793 | } |
| 3794 | |
| 3795 | gtk_widget_set_name(gui.mainwin, "vim-main-window"); |
| 3796 | |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 3797 | // Create the PangoContext used for drawing all text. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3798 | gui.text_context = gtk_widget_create_pango_context(gui.mainwin); |
| 3799 | pango_context_set_base_dir(gui.text_context, PANGO_DIRECTION_LTR); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3800 | |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 3801 | gtk_container_set_border_width(GTK_CONTAINER(gui.mainwin), 0); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3802 | gtk_widget_add_events(gui.mainwin, GDK_VISIBILITY_NOTIFY_MASK); |
| 3803 | |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 3804 | g_signal_connect(G_OBJECT(gui.mainwin), "delete-event", |
| 3805 | G_CALLBACK(&delete_event_cb), NULL); |
| 3806 | |
| 3807 | g_signal_connect(G_OBJECT(gui.mainwin), "realize", |
| 3808 | G_CALLBACK(&mainwin_realize), NULL); |
Bram Moolenaar | b463e8d | 2017-06-05 15:07:09 +0200 | [diff] [blame] | 3809 | |
| 3810 | g_signal_connect(G_OBJECT(gui.mainwin), "screen-changed", |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3811 | G_CALLBACK(&mainwin_screen_changed_cb), NULL); |
Bram Moolenaar | b463e8d | 2017-06-05 15:07:09 +0200 | [diff] [blame] | 3812 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3813 | gui.accel_group = gtk_accel_group_new(); |
| 3814 | gtk_window_add_accel_group(GTK_WINDOW(gui.mainwin), gui.accel_group); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3815 | |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 3816 | // A vertical box holds the menubar, toolbar and main text window. |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 3817 | #if GTK_CHECK_VERSION(3,2,0) |
| 3818 | vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0); |
| 3819 | gtk_box_set_homogeneous(GTK_BOX(vbox), FALSE); |
| 3820 | #else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3821 | vbox = gtk_vbox_new(FALSE, 0); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 3822 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3823 | |
| 3824 | #ifdef FEAT_GUI_GNOME |
| 3825 | if (using_gnome) |
| 3826 | { |
Bram Moolenaar | 182c5be | 2010-06-25 05:37:59 +0200 | [diff] [blame] | 3827 | # if defined(FEAT_MENU) |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 3828 | // automagically restore menubar/toolbar placement |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3829 | gnome_app_enable_layout_config(GNOME_APP(gui.mainwin), TRUE); |
| 3830 | # endif |
| 3831 | gnome_app_set_contents(GNOME_APP(gui.mainwin), vbox); |
| 3832 | } |
| 3833 | else |
| 3834 | #endif |
| 3835 | { |
| 3836 | gtk_container_add(GTK_CONTAINER(gui.mainwin), vbox); |
| 3837 | gtk_widget_show(vbox); |
| 3838 | } |
| 3839 | |
| 3840 | #ifdef FEAT_MENU |
| 3841 | /* |
| 3842 | * Create the menubar and handle |
| 3843 | */ |
| 3844 | gui.menubar = gtk_menu_bar_new(); |
| 3845 | gtk_widget_set_name(gui.menubar, "vim-menubar"); |
| 3846 | |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 3847 | // Avoid that GTK takes <F10> away from us. |
Bram Moolenaar | 293ee4d | 2004-12-09 21:34:53 +0000 | [diff] [blame] | 3848 | { |
| 3849 | GtkSettings *gtk_settings; |
| 3850 | |
| 3851 | gtk_settings = gtk_settings_get_for_screen(gdk_screen_get_default()); |
| 3852 | g_object_set(gtk_settings, "gtk-menu-bar-accel", NULL, NULL); |
| 3853 | } |
Bram Moolenaar | 293ee4d | 2004-12-09 21:34:53 +0000 | [diff] [blame] | 3854 | |
| 3855 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3856 | # ifdef FEAT_GUI_GNOME |
| 3857 | if (using_gnome) |
| 3858 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3859 | BonoboDockItem *dockitem; |
| 3860 | |
| 3861 | gnome_app_set_menus(GNOME_APP(gui.mainwin), GTK_MENU_BAR(gui.menubar)); |
| 3862 | dockitem = gnome_app_get_dock_item_by_name(GNOME_APP(gui.mainwin), |
| 3863 | GNOME_APP_MENUBAR_NAME); |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 3864 | // We don't want the menu to float. |
Bram Moolenaar | db552d60 | 2006-03-23 22:59:57 +0000 | [diff] [blame] | 3865 | bonobo_dock_item_set_behavior(dockitem, |
| 3866 | bonobo_dock_item_get_behavior(dockitem) |
| 3867 | | BONOBO_DOCK_ITEM_BEH_NEVER_FLOATING); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3868 | gui.menubar_h = GTK_WIDGET(dockitem); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3869 | } |
| 3870 | else |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 3871 | # endif // FEAT_GUI_GNOME |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3872 | { |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 3873 | // Always show the menubar, otherwise <F10> doesn't work. It may be |
| 3874 | // disabled in gui_init() later. |
Bram Moolenaar | 18144c8 | 2006-04-12 21:52:12 +0000 | [diff] [blame] | 3875 | gtk_widget_show(gui.menubar); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3876 | gtk_box_pack_start(GTK_BOX(vbox), gui.menubar, FALSE, FALSE, 0); |
| 3877 | } |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 3878 | #endif // FEAT_MENU |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3879 | |
| 3880 | #ifdef FEAT_TOOLBAR |
| 3881 | /* |
| 3882 | * Create the toolbar and handle |
| 3883 | */ |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 3884 | // some aesthetics on the toolbar |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 3885 | # ifdef USE_GTK3 |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 3886 | // TODO: Add GTK+ 3 code here using GtkCssProvider if necessary. |
| 3887 | // N.B. Since the default value of GtkToolbar::button-relief is |
| 3888 | // GTK_RELIEF_NONE, there's no need to specify that, probably. |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 3889 | # else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3890 | gtk_rc_parse_string( |
| 3891 | "style \"vim-toolbar-style\" {\n" |
| 3892 | " GtkToolbar::button_relief = GTK_RELIEF_NONE\n" |
| 3893 | "}\n" |
| 3894 | "widget \"*.vim-toolbar\" style \"vim-toolbar-style\"\n"); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 3895 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3896 | gui.toolbar = gtk_toolbar_new(); |
| 3897 | gtk_widget_set_name(gui.toolbar, "vim-toolbar"); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3898 | set_toolbar_style(GTK_TOOLBAR(gui.toolbar)); |
| 3899 | |
| 3900 | # ifdef FEAT_GUI_GNOME |
| 3901 | if (using_gnome) |
| 3902 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3903 | BonoboDockItem *dockitem; |
| 3904 | |
| 3905 | gnome_app_set_toolbar(GNOME_APP(gui.mainwin), GTK_TOOLBAR(gui.toolbar)); |
| 3906 | dockitem = gnome_app_get_dock_item_by_name(GNOME_APP(gui.mainwin), |
| 3907 | GNOME_APP_TOOLBAR_NAME); |
| 3908 | gui.toolbar_h = GTK_WIDGET(dockitem); |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 3909 | // When the toolbar is floating it gets stuck. So long as that isn't |
| 3910 | // fixed let's disallow floating. |
Bram Moolenaar | e580b0c | 2006-03-21 21:33:03 +0000 | [diff] [blame] | 3911 | bonobo_dock_item_set_behavior(dockitem, |
Bram Moolenaar | db552d60 | 2006-03-23 22:59:57 +0000 | [diff] [blame] | 3912 | bonobo_dock_item_get_behavior(dockitem) |
| 3913 | | BONOBO_DOCK_ITEM_BEH_NEVER_FLOATING); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3914 | gtk_container_set_border_width(GTK_CONTAINER(gui.toolbar), 0); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3915 | } |
| 3916 | else |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 3917 | # endif // FEAT_GUI_GNOME |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3918 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3919 | if (vim_strchr(p_go, GO_TOOLBAR) != NULL |
| 3920 | && (toolbar_flags & (TOOLBAR_TEXT | TOOLBAR_ICONS))) |
| 3921 | gtk_widget_show(gui.toolbar); |
| 3922 | gtk_box_pack_start(GTK_BOX(vbox), gui.toolbar, FALSE, FALSE, 0); |
| 3923 | } |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 3924 | #endif // FEAT_TOOLBAR |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3925 | |
Bram Moolenaar | 32466aa | 2006-02-24 23:53:04 +0000 | [diff] [blame] | 3926 | #ifdef FEAT_GUI_TABLINE |
Bram Moolenaar | 97b2ad3 | 2006-03-18 21:40:56 +0000 | [diff] [blame] | 3927 | /* |
| 3928 | * Use a Notebook for the tab pages labels. The labels are hidden by |
| 3929 | * default. |
Bram Moolenaar | 97b2ad3 | 2006-03-18 21:40:56 +0000 | [diff] [blame] | 3930 | */ |
Bram Moolenaar | b3656ed | 2006-03-20 21:59:49 +0000 | [diff] [blame] | 3931 | gui.tabline = gtk_notebook_new(); |
| 3932 | gtk_widget_show(gui.tabline); |
| 3933 | gtk_box_pack_start(GTK_BOX(vbox), gui.tabline, FALSE, FALSE, 0); |
| 3934 | gtk_notebook_set_show_border(GTK_NOTEBOOK(gui.tabline), FALSE); |
| 3935 | gtk_notebook_set_show_tabs(GTK_NOTEBOOK(gui.tabline), FALSE); |
Bram Moolenaar | 4a85b41 | 2006-04-23 22:40:29 +0000 | [diff] [blame] | 3936 | gtk_notebook_set_scrollable(GTK_NOTEBOOK(gui.tabline), TRUE); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 3937 | # if !GTK_CHECK_VERSION(3,0,0) |
Bram Moolenaar | 9635157 | 2006-05-05 21:16:59 +0000 | [diff] [blame] | 3938 | gtk_notebook_set_tab_border(GTK_NOTEBOOK(gui.tabline), FALSE); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 3939 | # endif |
Bram Moolenaar | b3656ed | 2006-03-20 21:59:49 +0000 | [diff] [blame] | 3940 | |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 3941 | # if !GTK_CHECK_VERSION(3,0,0) |
Bram Moolenaar | 437df8f | 2006-04-27 21:47:44 +0000 | [diff] [blame] | 3942 | tabline_tooltip = gtk_tooltips_new(); |
| 3943 | gtk_tooltips_enable(GTK_TOOLTIPS(tabline_tooltip)); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 3944 | # endif |
Bram Moolenaar | 437df8f | 2006-04-27 21:47:44 +0000 | [diff] [blame] | 3945 | |
Bram Moolenaar | 32466aa | 2006-02-24 23:53:04 +0000 | [diff] [blame] | 3946 | { |
Bram Moolenaar | 437df8f | 2006-04-27 21:47:44 +0000 | [diff] [blame] | 3947 | GtkWidget *page, *label, *event_box; |
Bram Moolenaar | 32466aa | 2006-02-24 23:53:04 +0000 | [diff] [blame] | 3948 | |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 3949 | // Add the first tab. |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 3950 | # if GTK_CHECK_VERSION(3,2,0) |
| 3951 | page = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0); |
| 3952 | gtk_box_set_homogeneous(GTK_BOX(page), FALSE); |
| 3953 | # else |
Bram Moolenaar | 437df8f | 2006-04-27 21:47:44 +0000 | [diff] [blame] | 3954 | page = gtk_vbox_new(FALSE, 0); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 3955 | # endif |
Bram Moolenaar | 437df8f | 2006-04-27 21:47:44 +0000 | [diff] [blame] | 3956 | gtk_widget_show(page); |
| 3957 | gtk_container_add(GTK_CONTAINER(gui.tabline), page); |
| 3958 | label = gtk_label_new("-Empty-"); |
| 3959 | gtk_widget_show(label); |
Bram Moolenaar | 437df8f | 2006-04-27 21:47:44 +0000 | [diff] [blame] | 3960 | event_box = gtk_event_box_new(); |
| 3961 | gtk_widget_show(event_box); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 3962 | g_object_set_data(G_OBJECT(event_box), "tab_num", GINT_TO_POINTER(1L)); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 3963 | # if !GTK_CHECK_VERSION(3,14,0) |
Bram Moolenaar | 8ea9123 | 2006-04-28 22:41:43 +0000 | [diff] [blame] | 3964 | gtk_misc_set_padding(GTK_MISC(label), 2, 2); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 3965 | # endif |
Bram Moolenaar | 437df8f | 2006-04-27 21:47:44 +0000 | [diff] [blame] | 3966 | gtk_container_add(GTK_CONTAINER(event_box), label); |
Bram Moolenaar | 437df8f | 2006-04-27 21:47:44 +0000 | [diff] [blame] | 3967 | gtk_notebook_set_tab_label(GTK_NOTEBOOK(gui.tabline), page, event_box); |
Bram Moolenaar | 92cbf62 | 2018-09-19 22:40:03 +0200 | [diff] [blame] | 3968 | # if GTK_CHECK_VERSION(2,10,0) |
Bram Moolenaar | ca05aa2 | 2017-10-22 15:36:14 +0200 | [diff] [blame] | 3969 | gtk_notebook_set_tab_reorderable(GTK_NOTEBOOK(gui.tabline), page, TRUE); |
Bram Moolenaar | 92cbf62 | 2018-09-19 22:40:03 +0200 | [diff] [blame] | 3970 | # endif |
Bram Moolenaar | 97b2ad3 | 2006-03-18 21:40:56 +0000 | [diff] [blame] | 3971 | } |
Bram Moolenaar | 54a709e | 2006-05-04 21:57:11 +0000 | [diff] [blame] | 3972 | |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 3973 | g_signal_connect(G_OBJECT(gui.tabline), "switch-page", |
| 3974 | G_CALLBACK(on_select_tab), NULL); |
Bram Moolenaar | 92cbf62 | 2018-09-19 22:40:03 +0200 | [diff] [blame] | 3975 | # if GTK_CHECK_VERSION(2,10,0) |
Bram Moolenaar | ca05aa2 | 2017-10-22 15:36:14 +0200 | [diff] [blame] | 3976 | g_signal_connect(G_OBJECT(gui.tabline), "page-reordered", |
| 3977 | G_CALLBACK(on_tab_reordered), NULL); |
Bram Moolenaar | 92cbf62 | 2018-09-19 22:40:03 +0200 | [diff] [blame] | 3978 | # endif |
Bram Moolenaar | b3656ed | 2006-03-20 21:59:49 +0000 | [diff] [blame] | 3979 | |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 3980 | // Create a popup menu for the tab line and connect it. |
Bram Moolenaar | b3656ed | 2006-03-20 21:59:49 +0000 | [diff] [blame] | 3981 | tabline_menu = create_tabline_menu(); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 3982 | g_signal_connect_swapped(G_OBJECT(gui.tabline), "button-press-event", |
| 3983 | G_CALLBACK(on_tabline_menu), G_OBJECT(tabline_menu)); |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 3984 | #endif // FEAT_GUI_TABLINE |
Bram Moolenaar | 32466aa | 2006-02-24 23:53:04 +0000 | [diff] [blame] | 3985 | |
Bram Moolenaar | 8a99e66 | 2020-10-21 16:10:21 +0200 | [diff] [blame] | 3986 | gui.formwin = gui_gtk_form_new(); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 3987 | gtk_container_set_border_width(GTK_CONTAINER(gui.formwin), 0); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 3988 | #if !GTK_CHECK_VERSION(3,0,0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3989 | gtk_widget_set_events(gui.formwin, GDK_EXPOSURE_MASK); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 3990 | #endif |
Bram Moolenaar | 023fd5d | 2020-12-08 20:31:16 +0100 | [diff] [blame] | 3991 | #if GTK_CHECK_VERSION(3,22,2) |
| 3992 | gtk_widget_set_name(gui.formwin, "vim-gtk-form"); |
| 3993 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3994 | |
| 3995 | gui.drawarea = gtk_drawing_area_new(); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 3996 | #if GTK_CHECK_VERSION(3,0,0) |
| 3997 | gui.surface = NULL; |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 3998 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3999 | |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 4000 | // Determine which events we will filter. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4001 | gtk_widget_set_events(gui.drawarea, |
| 4002 | GDK_EXPOSURE_MASK | |
| 4003 | GDK_ENTER_NOTIFY_MASK | |
| 4004 | GDK_LEAVE_NOTIFY_MASK | |
| 4005 | GDK_BUTTON_PRESS_MASK | |
| 4006 | GDK_BUTTON_RELEASE_MASK | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4007 | GDK_SCROLL_MASK | |
lilydjwg | 6e0a18f | 2024-01-29 20:54:28 +0100 | [diff] [blame] | 4008 | #if GTK_CHECK_VERSION(3,4,0) |
| 4009 | GDK_SMOOTH_SCROLL_MASK | |
| 4010 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4011 | GDK_KEY_PRESS_MASK | |
| 4012 | GDK_KEY_RELEASE_MASK | |
| 4013 | GDK_POINTER_MOTION_MASK | |
| 4014 | GDK_POINTER_MOTION_HINT_MASK); |
| 4015 | |
| 4016 | gtk_widget_show(gui.drawarea); |
Bram Moolenaar | 8a99e66 | 2020-10-21 16:10:21 +0200 | [diff] [blame] | 4017 | gui_gtk_form_put(GTK_FORM(gui.formwin), gui.drawarea, 0, 0); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4018 | gtk_widget_show(gui.formwin); |
| 4019 | gtk_box_pack_start(GTK_BOX(vbox), gui.formwin, TRUE, TRUE, 0); |
| 4020 | |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 4021 | // For GtkSockets, key-presses must go to the focus widget (drawarea) |
| 4022 | // and not the window. |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 4023 | g_signal_connect((gtk_socket_id == 0) ? G_OBJECT(gui.mainwin) |
| 4024 | : G_OBJECT(gui.drawarea), |
| 4025 | "key-press-event", |
| 4026 | G_CALLBACK(key_press_event), NULL); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 4027 | #if defined(FEAT_XIM) || GTK_CHECK_VERSION(3,0,0) |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 4028 | // Also forward key release events for the benefit of GTK+ 2 input |
| 4029 | // modules. Try CTRL-SHIFT-xdigits to enter a Unicode code point. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4030 | g_signal_connect((gtk_socket_id == 0) ? G_OBJECT(gui.mainwin) |
| 4031 | : G_OBJECT(gui.drawarea), |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 4032 | "key-release-event", |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4033 | G_CALLBACK(&key_release_event), NULL); |
| 4034 | #endif |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 4035 | g_signal_connect(G_OBJECT(gui.drawarea), "realize", |
| 4036 | G_CALLBACK(drawarea_realize_cb), NULL); |
| 4037 | g_signal_connect(G_OBJECT(gui.drawarea), "unrealize", |
| 4038 | G_CALLBACK(drawarea_unrealize_cb), NULL); |
Bram Moolenaar | 664323e | 2018-09-18 22:30:07 +0200 | [diff] [blame] | 4039 | #if GTK_CHECK_VERSION(3,0,0) |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 4040 | g_signal_connect(G_OBJECT(gui.drawarea), "configure-event", |
| 4041 | G_CALLBACK(drawarea_configure_event_cb), NULL); |
Bram Moolenaar | 664323e | 2018-09-18 22:30:07 +0200 | [diff] [blame] | 4042 | #endif |
| 4043 | #if GTK_CHECK_VERSION(3,22,2) |
Bram Moolenaar | a859f04 | 2016-11-17 19:11:55 +0100 | [diff] [blame] | 4044 | g_signal_connect_after(G_OBJECT(gui.drawarea), "style-updated", |
| 4045 | G_CALLBACK(&drawarea_style_updated_cb), NULL); |
Bram Moolenaar | 664323e | 2018-09-18 22:30:07 +0200 | [diff] [blame] | 4046 | #else |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 4047 | g_signal_connect_after(G_OBJECT(gui.drawarea), "style-set", |
| 4048 | G_CALLBACK(&drawarea_style_set_cb), NULL); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 4049 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4050 | |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 4051 | #if !GTK_CHECK_VERSION(3,0,0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4052 | gui.visibility = GDK_VISIBILITY_UNOBSCURED; |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 4053 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4054 | |
Bram Moolenaar | f96ae0b | 2019-07-28 15:21:55 +0200 | [diff] [blame] | 4055 | #if !defined(USE_GNOME_SESSION) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4056 | wm_protocols_atom = gdk_atom_intern("WM_PROTOCOLS", FALSE); |
| 4057 | save_yourself_atom = gdk_atom_intern("WM_SAVE_YOURSELF", FALSE); |
| 4058 | #endif |
| 4059 | |
| 4060 | if (gtk_socket_id != 0) |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 4061 | // make sure keyboard input can go to the drawarea |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 4062 | gtk_widget_set_can_focus(gui.drawarea, TRUE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4063 | |
| 4064 | /* |
| 4065 | * Set clipboard specific atoms |
| 4066 | */ |
| 4067 | vim_atom = gdk_atom_intern(VIM_ATOM_NAME, FALSE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4068 | vimenc_atom = gdk_atom_intern(VIMENC_ATOM_NAME, FALSE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4069 | clip_star.gtk_sel_atom = GDK_SELECTION_PRIMARY; |
| 4070 | clip_plus.gtk_sel_atom = gdk_atom_intern("CLIPBOARD", FALSE); |
| 4071 | |
| 4072 | /* |
| 4073 | * Start out by adding the configured border width into the border offset. |
| 4074 | */ |
| 4075 | gui.border_offset = gui.border_width; |
| 4076 | |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 4077 | #if GTK_CHECK_VERSION(3,0,0) |
| 4078 | g_signal_connect(G_OBJECT(gui.drawarea), "draw", |
| 4079 | G_CALLBACK(draw_event), NULL); |
| 4080 | #else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4081 | gtk_signal_connect(GTK_OBJECT(gui.mainwin), "visibility_notify_event", |
| 4082 | GTK_SIGNAL_FUNC(visibility_event), NULL); |
| 4083 | gtk_signal_connect(GTK_OBJECT(gui.drawarea), "expose_event", |
| 4084 | GTK_SIGNAL_FUNC(expose_event), NULL); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 4085 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4086 | |
| 4087 | /* |
| 4088 | * Only install these enter/leave callbacks when 'p' in 'guioptions'. |
| 4089 | * Only needed for some window managers. |
| 4090 | */ |
| 4091 | if (vim_strchr(p_go, GO_POINTER) != NULL) |
| 4092 | { |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 4093 | g_signal_connect(G_OBJECT(gui.drawarea), "leave-notify-event", |
| 4094 | G_CALLBACK(leave_notify_event), NULL); |
| 4095 | g_signal_connect(G_OBJECT(gui.drawarea), "enter-notify-event", |
| 4096 | G_CALLBACK(enter_notify_event), NULL); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4097 | } |
| 4098 | |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 4099 | // Real windows can get focus ... GtkPlug, being a mere container can't, |
Christian Brabandt | ee17b6f | 2023-09-09 11:23:50 +0200 | [diff] [blame] | 4100 | // only its widgets. Arguably, this could be common code and we do not |
| 4101 | // use the window focus at all, but let's be safe. |
Bram Moolenaar | b3656ed | 2006-03-20 21:59:49 +0000 | [diff] [blame] | 4102 | if (gtk_socket_id == 0) |
| 4103 | { |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 4104 | g_signal_connect(G_OBJECT(gui.mainwin), "focus-out-event", |
| 4105 | G_CALLBACK(focus_out_event), NULL); |
| 4106 | g_signal_connect(G_OBJECT(gui.mainwin), "focus-in-event", |
| 4107 | G_CALLBACK(focus_in_event), NULL); |
Bram Moolenaar | b3656ed | 2006-03-20 21:59:49 +0000 | [diff] [blame] | 4108 | } |
| 4109 | else |
| 4110 | { |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 4111 | g_signal_connect(G_OBJECT(gui.drawarea), "focus-out-event", |
| 4112 | G_CALLBACK(focus_out_event), NULL); |
| 4113 | g_signal_connect(G_OBJECT(gui.drawarea), "focus-in-event", |
| 4114 | G_CALLBACK(focus_in_event), NULL); |
Bram Moolenaar | b3656ed | 2006-03-20 21:59:49 +0000 | [diff] [blame] | 4115 | #ifdef FEAT_GUI_TABLINE |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 4116 | g_signal_connect(G_OBJECT(gui.tabline), "focus-out-event", |
| 4117 | G_CALLBACK(focus_out_event), NULL); |
| 4118 | g_signal_connect(G_OBJECT(gui.tabline), "focus-in-event", |
| 4119 | G_CALLBACK(focus_in_event), NULL); |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 4120 | #endif // FEAT_GUI_TABLINE |
Bram Moolenaar | b3656ed | 2006-03-20 21:59:49 +0000 | [diff] [blame] | 4121 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4122 | |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 4123 | g_signal_connect(G_OBJECT(gui.drawarea), "motion-notify-event", |
| 4124 | G_CALLBACK(motion_notify_event), NULL); |
| 4125 | g_signal_connect(G_OBJECT(gui.drawarea), "button-press-event", |
| 4126 | G_CALLBACK(button_press_event), NULL); |
| 4127 | g_signal_connect(G_OBJECT(gui.drawarea), "button-release-event", |
| 4128 | G_CALLBACK(button_release_event), NULL); |
| 4129 | g_signal_connect(G_OBJECT(gui.drawarea), "scroll-event", |
lilydjwg | 94ff09a | 2024-01-29 20:14:01 +0100 | [diff] [blame] | 4130 | G_CALLBACK(scroll_event), NULL); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4131 | |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 4132 | // Pretend we don't have input focus, we will get an event if we do. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4133 | gui.in_focus = FALSE; |
| 4134 | |
Bram Moolenaar | 7ebf4e1 | 2018-08-07 20:01:40 +0200 | [diff] [blame] | 4135 | // Handle changes to the "Xft/DPI" setting. |
| 4136 | { |
| 4137 | GtkSettings *gtk_settings = |
| 4138 | gtk_settings_get_for_screen(gdk_screen_get_default()); |
| 4139 | |
| 4140 | g_signal_connect(gtk_settings, "notify::gtk-xft-dpi", |
| 4141 | G_CALLBACK(gtk_settings_xft_dpi_changed_cb), NULL); |
| 4142 | } |
| 4143 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4144 | return OK; |
| 4145 | } |
| 4146 | |
Bram Moolenaar | f96ae0b | 2019-07-28 15:21:55 +0200 | [diff] [blame] | 4147 | #if defined(USE_GNOME_SESSION) || defined(PROTO) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4148 | /* |
| 4149 | * This is called from gui_start() after a fork() has been done. |
| 4150 | * We have to tell the session manager our new PID. |
| 4151 | */ |
| 4152 | void |
| 4153 | gui_mch_forked(void) |
| 4154 | { |
Yegappan Lakshmanan | 7f8b255 | 2023-01-08 13:44:24 +0000 | [diff] [blame] | 4155 | if (!using_gnome) |
| 4156 | return; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4157 | |
Yegappan Lakshmanan | 7f8b255 | 2023-01-08 13:44:24 +0000 | [diff] [blame] | 4158 | GnomeClient *client; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4159 | |
Yegappan Lakshmanan | 7f8b255 | 2023-01-08 13:44:24 +0000 | [diff] [blame] | 4160 | client = gnome_master_client(); |
| 4161 | |
| 4162 | if (client != NULL) |
| 4163 | gnome_client_set_process_id(client, getpid()); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4164 | } |
Bram Moolenaar | f96ae0b | 2019-07-28 15:21:55 +0200 | [diff] [blame] | 4165 | #endif // USE_GNOME_SESSION |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4166 | |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 4167 | #if GTK_CHECK_VERSION(3,0,0) |
Bram Moolenaar | 36edf06 | 2016-07-21 22:10:12 +0200 | [diff] [blame] | 4168 | static GdkRGBA |
| 4169 | color_to_rgba(guicolor_T color) |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 4170 | { |
Bram Moolenaar | 36edf06 | 2016-07-21 22:10:12 +0200 | [diff] [blame] | 4171 | GdkRGBA rgba; |
| 4172 | rgba.red = ((color & 0xff0000) >> 16) / 255.0; |
| 4173 | rgba.green = ((color & 0xff00) >> 8) / 255.0; |
| 4174 | rgba.blue = ((color & 0xff)) / 255.0; |
| 4175 | rgba.alpha = 1.0; |
| 4176 | return rgba; |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 4177 | } |
| 4178 | |
| 4179 | static void |
Bram Moolenaar | 36edf06 | 2016-07-21 22:10:12 +0200 | [diff] [blame] | 4180 | set_cairo_source_rgba_from_color(cairo_t *cr, guicolor_T color) |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 4181 | { |
Bram Moolenaar | 36edf06 | 2016-07-21 22:10:12 +0200 | [diff] [blame] | 4182 | const GdkRGBA rgba = color_to_rgba(color); |
| 4183 | cairo_set_source_rgba(cr, rgba.red, rgba.green, rgba.blue, rgba.alpha); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 4184 | } |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 4185 | #endif // GTK_CHECK_VERSION(3,0,0) |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 4186 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4187 | /* |
| 4188 | * Called when the foreground or background color has been changed. |
| 4189 | * This used to change the graphics contexts directly but we are |
| 4190 | * currently manipulating them where desired. |
| 4191 | */ |
| 4192 | void |
| 4193 | gui_mch_new_colors(void) |
| 4194 | { |
Bram Moolenaar | 7988a6f | 2020-12-09 15:53:27 +0100 | [diff] [blame] | 4195 | if (gui.drawarea != NULL |
| 4196 | #if GTK_CHECK_VERSION(3,22,2) |
| 4197 | && gui.formwin != NULL |
| 4198 | #endif |
| 4199 | && gtk_widget_get_window(gui.drawarea) != NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4200 | { |
Bram Moolenaar | 664323e | 2018-09-18 22:30:07 +0200 | [diff] [blame] | 4201 | #if !GTK_CHECK_VERSION(3,22,2) |
Bram Moolenaar | 7988a6f | 2020-12-09 15:53:27 +0100 | [diff] [blame] | 4202 | GdkWindow * const da_win = gtk_widget_get_window(gui.drawarea); |
Bram Moolenaar | 664323e | 2018-09-18 22:30:07 +0200 | [diff] [blame] | 4203 | #endif |
Bram Moolenaar | a859f04 | 2016-11-17 19:11:55 +0100 | [diff] [blame] | 4204 | #if GTK_CHECK_VERSION(3,22,2) |
Bram Moolenaar | 7988a6f | 2020-12-09 15:53:27 +0100 | [diff] [blame] | 4205 | GtkStyleContext * const context = |
| 4206 | gtk_widget_get_style_context(gui.formwin); |
Bram Moolenaar | a859f04 | 2016-11-17 19:11:55 +0100 | [diff] [blame] | 4207 | GtkCssProvider * const provider = gtk_css_provider_new(); |
| 4208 | gchar * const css = g_strdup_printf( |
Bram Moolenaar | 023fd5d | 2020-12-08 20:31:16 +0100 | [diff] [blame] | 4209 | "widget#vim-gtk-form {\n" |
Bram Moolenaar | a859f04 | 2016-11-17 19:11:55 +0100 | [diff] [blame] | 4210 | " background-color: #%.2lx%.2lx%.2lx;\n" |
| 4211 | "}\n", |
| 4212 | (gui.back_pixel >> 16) & 0xff, |
| 4213 | (gui.back_pixel >> 8) & 0xff, |
| 4214 | gui.back_pixel & 0xff); |
| 4215 | |
| 4216 | gtk_css_provider_load_from_data(provider, css, -1, NULL); |
| 4217 | gtk_style_context_add_provider(context, |
| 4218 | GTK_STYLE_PROVIDER(provider), G_MAXUINT); |
| 4219 | |
| 4220 | g_free(css); |
| 4221 | g_object_unref(provider); |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 4222 | #elif GTK_CHECK_VERSION(3,4,0) // !GTK_CHECK_VERSION(3,22,2) |
Bram Moolenaar | 36edf06 | 2016-07-21 22:10:12 +0200 | [diff] [blame] | 4223 | GdkRGBA rgba; |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 4224 | |
Bram Moolenaar | 36edf06 | 2016-07-21 22:10:12 +0200 | [diff] [blame] | 4225 | rgba = color_to_rgba(gui.back_pixel); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 4226 | { |
| 4227 | cairo_pattern_t * const pat = cairo_pattern_create_rgba( |
Bram Moolenaar | 36edf06 | 2016-07-21 22:10:12 +0200 | [diff] [blame] | 4228 | rgba.red, rgba.green, rgba.blue, rgba.alpha); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 4229 | if (pat != NULL) |
| 4230 | { |
| 4231 | gdk_window_set_background_pattern(da_win, pat); |
| 4232 | cairo_pattern_destroy(pat); |
| 4233 | } |
| 4234 | else |
Bram Moolenaar | 36edf06 | 2016-07-21 22:10:12 +0200 | [diff] [blame] | 4235 | gdk_window_set_background_rgba(da_win, &rgba); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 4236 | } |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 4237 | #else // !GTK_CHECK_VERSION(3,4,0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4238 | GdkColor color = { 0, 0, 0, 0 }; |
| 4239 | |
| 4240 | color.pixel = gui.back_pixel; |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 4241 | gdk_window_set_background(da_win, &color); |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 4242 | #endif // !GTK_CHECK_VERSION(3,22,2) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4243 | } |
| 4244 | } |
| 4245 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4246 | /* |
| 4247 | * This signal informs us about the need to rearrange our sub-widgets. |
| 4248 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4249 | static gint |
Bram Moolenaar | b85cb21 | 2009-05-17 14:24:23 +0000 | [diff] [blame] | 4250 | form_configure_event(GtkWidget *widget UNUSED, |
| 4251 | GdkEventConfigure *event, |
| 4252 | gpointer data UNUSED) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4253 | { |
Ernie Rael | d42b839 | 2022-04-23 19:52:23 +0100 | [diff] [blame] | 4254 | int usable_height = event->height; |
| 4255 | #ifdef TRACK_RESIZE_HISTORY |
| 4256 | // Resize requests are made for gui.mainwin; |
| 4257 | // get its dimensions for searching if this event |
Ernie Rael | 9f53e7b | 2022-04-17 18:27:49 +0100 | [diff] [blame] | 4258 | // is a response to a vim request. |
Ernie Rael | d42b839 | 2022-04-23 19:52:23 +0100 | [diff] [blame] | 4259 | int w, h; |
| 4260 | gtk_window_get_size(GTK_WINDOW(gui.mainwin), &w, &h); |
Ernie Rael | 9f53e7b | 2022-04-17 18:27:49 +0100 | [diff] [blame] | 4261 | |
Ernie Rael | d42b839 | 2022-04-23 19:52:23 +0100 | [diff] [blame] | 4262 | # ifdef ENABLE_RESIZE_HISTORY_LOG |
Ernie Rael | 9f53e7b | 2022-04-17 18:27:49 +0100 | [diff] [blame] | 4263 | ch_log(NULL, "gui_gtk: form_configure_event: (%d, %d) [%d, %d]", |
| 4264 | w, h, (int)Columns, (int)Rows); |
Ernie Rael | d42b839 | 2022-04-23 19:52:23 +0100 | [diff] [blame] | 4265 | # endif |
Ernie Rael | 9f53e7b | 2022-04-17 18:27:49 +0100 | [diff] [blame] | 4266 | |
Ernie Rael | d42b839 | 2022-04-23 19:52:23 +0100 | [diff] [blame] | 4267 | // Look through history of recent vim resize requests. |
Ernie Rael | 9f53e7b | 2022-04-17 18:27:49 +0100 | [diff] [blame] | 4268 | // If this event matches: |
| 4269 | // - "latest resize hist" We're caught up; |
| 4270 | // clear the history and process this event. |
| 4271 | // If history is, old to new, 100, 99, 100, 99. If this event is |
| 4272 | // 99 for the stale, it is matched against the current. History |
Ernie Rael | d42b839 | 2022-04-23 19:52:23 +0100 | [diff] [blame] | 4273 | // is cleared, we may bounce, but no worse than before. |
Ernie Rael | 9f53e7b | 2022-04-17 18:27:49 +0100 | [diff] [blame] | 4274 | // - "older/stale hist" If match an unused event in history, |
| 4275 | // then discard this event, and mark the matching event as used. |
| 4276 | // - "no match" Figure it's a user resize event, clear history. |
| 4277 | // NOTE: clear history is default, then all incoming events are processed |
| 4278 | |
| 4279 | if (!MATCH_WIDTH_HEIGHT(latest_resize_hist, w, h) |
| 4280 | && match_stale_width_height(w, h)) |
| 4281 | // discard stale event |
| 4282 | return TRUE; |
| 4283 | clear_resize_hists(); |
Ernie Rael | d42b839 | 2022-04-23 19:52:23 +0100 | [diff] [blame] | 4284 | #endif |
Bram Moolenaar | b3656ed | 2006-03-20 21:59:49 +0000 | [diff] [blame] | 4285 | |
Bram Moolenaar | 182707a | 2016-11-21 20:55:58 +0100 | [diff] [blame] | 4286 | #if GTK_CHECK_VERSION(3,22,2) && !GTK_CHECK_VERSION(3,22,4) |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 4287 | // As of 3.22.2, GdkWindows have started distributing configure events to |
| 4288 | // their "native" children (https://git.gnome.org/browse/gtk+/commit/?h=gtk-3-22&id=12579fe71b3b8f79eb9c1b80e429443bcc437dd0). |
| 4289 | // |
| 4290 | // As can be seen from the implementation of move_native_children() and |
| 4291 | // configure_native_child() in gdkwindow.c, those functions actually |
| 4292 | // propagate configure events to every child, failing to distinguish |
| 4293 | // "native" one from non-native one. |
| 4294 | // |
| 4295 | // Naturally, configure events propagated to here like that are fallacious |
| 4296 | // and, as a matter of fact, they trigger a geometric collapse of |
| 4297 | // gui.formwin. |
| 4298 | // |
| 4299 | // To filter out such fallacious events, check if the given event is the |
| 4300 | // one that was sent out to the right place. Ignore it if not. |
| 4301 | // |
| 4302 | // Follow-up |
| 4303 | // After a few weeks later, the GdkWindow change mentioned above was |
| 4304 | // reverted (https://git.gnome.org/browse/gtk+/commit/?h=gtk-3-22&id=f70039cb9603a02d2369fec4038abf40a1711155). |
| 4305 | // The corresponding official release is 3.22.4. |
Bram Moolenaar | a859f04 | 2016-11-17 19:11:55 +0100 | [diff] [blame] | 4306 | if (event->window != gtk_widget_get_window(gui.formwin)) |
| 4307 | return TRUE; |
| 4308 | #endif |
| 4309 | |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 4310 | // When in a GtkPlug, we can't guarantee valid heights (as a round |
| 4311 | // no. of char-heights), so we have to manually sanitise them. |
| 4312 | // Widths seem to sort themselves out, don't ask me why. |
Bram Moolenaar | b3656ed | 2006-03-20 21:59:49 +0000 | [diff] [blame] | 4313 | if (gtk_socket_id != 0) |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 4314 | usable_height -= (gui.char_height - (gui.char_height/2)); // sic. |
Bram Moolenaar | b3656ed | 2006-03-20 21:59:49 +0000 | [diff] [blame] | 4315 | |
Bram Moolenaar | 8a99e66 | 2020-10-21 16:10:21 +0200 | [diff] [blame] | 4316 | gui_gtk_form_freeze(GTK_FORM(gui.formwin)); |
Bram Moolenaar | b3656ed | 2006-03-20 21:59:49 +0000 | [diff] [blame] | 4317 | gui_resize_shell(event->width, usable_height); |
Bram Moolenaar | 8a99e66 | 2020-10-21 16:10:21 +0200 | [diff] [blame] | 4318 | gui_gtk_form_thaw(GTK_FORM(gui.formwin)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4319 | |
| 4320 | return TRUE; |
| 4321 | } |
| 4322 | |
| 4323 | /* |
| 4324 | * Function called when window already closed. |
| 4325 | * We can't do much more here than to trying to preserve what had been done, |
| 4326 | * since the window is already inevitably going away. |
| 4327 | */ |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 4328 | static void |
| 4329 | mainwin_destroy_cb(GObject *object UNUSED, gpointer data UNUSED) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4330 | { |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 4331 | // Don't write messages to the GUI anymore |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4332 | full_screen = FALSE; |
| 4333 | |
| 4334 | gui.mainwin = NULL; |
| 4335 | gui.drawarea = NULL; |
| 4336 | |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 4337 | if (!exiting) // only do anything if the destroy was unexpected |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4338 | { |
Bram Moolenaar | ce0842a | 2005-07-18 21:58:11 +0000 | [diff] [blame] | 4339 | vim_strncpy(IObuff, |
| 4340 | (char_u *)_("Vim: Main window unexpectedly destroyed\n"), |
| 4341 | IOSIZE - 1); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4342 | preserve_exit(); |
| 4343 | } |
Bram Moolenaar | 36e294c | 2015-12-29 18:55:46 +0100 | [diff] [blame] | 4344 | #ifdef USE_GRESOURCE |
| 4345 | gui_gtk_unregister_resource(); |
| 4346 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4347 | } |
| 4348 | |
Bram Moolenaar | a555e6f | 2021-03-18 22:28:57 +0100 | [diff] [blame] | 4349 | void |
| 4350 | gui_gtk_get_screen_geom_of_win( |
| 4351 | GtkWidget *wid, |
| 4352 | int point_x, // x position of window if not initialized |
| 4353 | int point_y, // y position of window if not initialized |
| 4354 | int *screen_x, |
| 4355 | int *screen_y, |
| 4356 | int *width, |
| 4357 | int *height) |
| 4358 | { |
| 4359 | GdkRectangle geometry; |
| 4360 | GdkWindow *win = gtk_widget_get_window(wid); |
| 4361 | #if GTK_CHECK_VERSION(3,22,0) |
Bram Moolenaar | 2400143 | 2021-03-20 12:36:46 +0100 | [diff] [blame] | 4362 | GdkDisplay *dpy; |
| 4363 | GdkMonitor *monitor; |
Bram Moolenaar | a555e6f | 2021-03-18 22:28:57 +0100 | [diff] [blame] | 4364 | |
Bram Moolenaar | 2400143 | 2021-03-20 12:36:46 +0100 | [diff] [blame] | 4365 | if (wid != NULL && gtk_widget_get_realized(wid)) |
| 4366 | dpy = gtk_widget_get_display(wid); |
| 4367 | else |
| 4368 | dpy = gdk_display_get_default(); |
| 4369 | if (win != NULL) |
| 4370 | monitor = gdk_display_get_monitor_at_window(dpy, win); |
| 4371 | else |
| 4372 | monitor = gdk_display_get_monitor_at_point(dpy, point_x, point_y); |
Bram Moolenaar | a555e6f | 2021-03-18 22:28:57 +0100 | [diff] [blame] | 4373 | gdk_monitor_get_geometry(monitor, &geometry); |
| 4374 | #else |
| 4375 | GdkScreen* screen; |
| 4376 | int monitor; |
| 4377 | |
| 4378 | if (wid != NULL && gtk_widget_has_screen(wid)) |
| 4379 | screen = gtk_widget_get_screen(wid); |
| 4380 | else |
| 4381 | screen = gdk_screen_get_default(); |
Bram Moolenaar | 2400143 | 2021-03-20 12:36:46 +0100 | [diff] [blame] | 4382 | if (win != NULL) |
Bram Moolenaar | a555e6f | 2021-03-18 22:28:57 +0100 | [diff] [blame] | 4383 | monitor = gdk_screen_get_monitor_at_window(screen, win); |
Bram Moolenaar | 2400143 | 2021-03-20 12:36:46 +0100 | [diff] [blame] | 4384 | else |
| 4385 | monitor = gdk_screen_get_monitor_at_point(screen, point_x, point_y); |
Bram Moolenaar | a555e6f | 2021-03-18 22:28:57 +0100 | [diff] [blame] | 4386 | gdk_screen_get_monitor_geometry(screen, monitor, &geometry); |
| 4387 | #endif |
| 4388 | *screen_x = geometry.x; |
| 4389 | *screen_y = geometry.y; |
| 4390 | *width = geometry.width; |
| 4391 | *height = geometry.height; |
| 4392 | } |
| 4393 | |
| 4394 | /* |
| 4395 | * The screen size is used to make sure the initial window doesn't get bigger |
| 4396 | * than the screen. This subtracts some room for menubar, toolbar and window |
| 4397 | * decorations. |
| 4398 | */ |
| 4399 | static void |
| 4400 | gui_gtk_get_screen_dimensions( |
| 4401 | int point_x, |
| 4402 | int point_y, |
| 4403 | int *screen_w, |
| 4404 | int *screen_h) |
| 4405 | { |
| 4406 | int x, y; |
| 4407 | |
| 4408 | gui_gtk_get_screen_geom_of_win(gui.mainwin, point_x, point_y, |
| 4409 | &x, &y, screen_w, screen_h); |
| 4410 | |
| 4411 | // Subtract 'guiheadroom' from the height to allow some room for the |
| 4412 | // window manager (task list and window title bar). |
| 4413 | *screen_h -= p_ghr; |
| 4414 | |
| 4415 | /* |
| 4416 | * FIXME: dirty trick: Because the gui_get_base_height() doesn't include |
| 4417 | * the toolbar and menubar for GTK, we subtract them from the screen |
| 4418 | * height, so that the window size can be made to fit on the screen. |
| 4419 | * This should be completely changed later. |
| 4420 | */ |
| 4421 | *screen_w -= get_menu_tool_width(); |
| 4422 | *screen_h -= get_menu_tool_height(); |
| 4423 | } |
| 4424 | |
| 4425 | void |
| 4426 | gui_mch_get_screen_dimensions(int *screen_w, int *screen_h) |
| 4427 | { |
| 4428 | gui_gtk_get_screen_dimensions(0, 0, screen_w, screen_h); |
| 4429 | } |
| 4430 | |
Bram Moolenaar | b3656ed | 2006-03-20 21:59:49 +0000 | [diff] [blame] | 4431 | |
| 4432 | /* |
| 4433 | * Bit of a hack to ensure we start GtkPlug windows with the correct window |
| 4434 | * hints (and thus the required size from -geom), but that after that we |
| 4435 | * put the hints back to normal (the actual minimum size) so we may |
| 4436 | * subsequently be resized smaller. GtkSocket (the parent end) uses the |
Bram Moolenaar | c4568ab | 2018-11-16 16:21:05 +0100 | [diff] [blame] | 4437 | * plug's window 'min hints to set *its* minimum size, but that's also the |
Bram Moolenaar | b3656ed | 2006-03-20 21:59:49 +0000 | [diff] [blame] | 4438 | * only way we have of making ourselves bigger (by set lines/columns). |
| 4439 | * Thus set hints at start-up to ensure correct init. size, then a |
Bram Moolenaar | 3f2a5d8 | 2016-02-27 22:08:16 +0100 | [diff] [blame] | 4440 | * second after the final attempt to reset the real minimum hints (done by |
| 4441 | * scrollbar init.), actually do the standard hints and stop the timer. |
Bram Moolenaar | b3656ed | 2006-03-20 21:59:49 +0000 | [diff] [blame] | 4442 | * We'll not let the default hints be set while this timer's active. |
| 4443 | */ |
Bram Moolenaar | 4ab7968 | 2017-08-27 14:50:47 +0200 | [diff] [blame] | 4444 | static timeout_cb_type |
Bram Moolenaar | b85cb21 | 2009-05-17 14:24:23 +0000 | [diff] [blame] | 4445 | check_startup_plug_hints(gpointer data UNUSED) |
Bram Moolenaar | b3656ed | 2006-03-20 21:59:49 +0000 | [diff] [blame] | 4446 | { |
| 4447 | if (init_window_hints_state == 1) |
| 4448 | { |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 4449 | // Safe to use normal hints now |
Bram Moolenaar | c9b4b05 | 2006-04-30 18:54:39 +0000 | [diff] [blame] | 4450 | init_window_hints_state = 0; |
| 4451 | update_window_manager_hints(0, 0); |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 4452 | return FALSE; // stop timer |
Bram Moolenaar | b3656ed | 2006-03-20 21:59:49 +0000 | [diff] [blame] | 4453 | } |
| 4454 | |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 4455 | // Keep on trying |
Bram Moolenaar | b3656ed | 2006-03-20 21:59:49 +0000 | [diff] [blame] | 4456 | init_window_hints_state = 1; |
| 4457 | return TRUE; |
| 4458 | } |
| 4459 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4460 | /* |
| 4461 | * Open the GUI window which was created by a call to gui_mch_init(). |
| 4462 | */ |
| 4463 | int |
| 4464 | gui_mch_open(void) |
| 4465 | { |
| 4466 | guicolor_T fg_pixel = INVALCOLOR; |
| 4467 | guicolor_T bg_pixel = INVALCOLOR; |
Bram Moolenaar | 79ef6d6 | 2009-09-23 15:35:48 +0000 | [diff] [blame] | 4468 | guint pixel_width; |
| 4469 | guint pixel_height; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4470 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4471 | /* |
| 4472 | * Allow setting a window role on the command line, or invent one |
| 4473 | * if none was specified. This is mainly useful for GNOME session |
| 4474 | * support; allowing the WM to restore window placement. |
| 4475 | */ |
| 4476 | if (role_argument != NULL) |
| 4477 | { |
| 4478 | gtk_window_set_role(GTK_WINDOW(gui.mainwin), role_argument); |
| 4479 | } |
| 4480 | else |
| 4481 | { |
| 4482 | char *role; |
| 4483 | |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 4484 | // Invent a unique-enough ID string for the role |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4485 | role = g_strdup_printf("vim-%u-%u-%u", |
| 4486 | (unsigned)mch_get_pid(), |
| 4487 | (unsigned)g_random_int(), |
| 4488 | (unsigned)time(NULL)); |
| 4489 | |
| 4490 | gtk_window_set_role(GTK_WINDOW(gui.mainwin), role); |
| 4491 | g_free(role); |
| 4492 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4493 | |
| 4494 | if (gui_win_x != -1 && gui_win_y != -1) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4495 | gtk_window_move(GTK_WINDOW(gui.mainwin), gui_win_x, gui_win_y); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4496 | |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 4497 | // Determine user specified geometry, if present. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4498 | if (gui.geom != NULL) |
| 4499 | { |
| 4500 | int mask; |
| 4501 | unsigned int w, h; |
| 4502 | int x = 0; |
| 4503 | int y = 0; |
| 4504 | |
| 4505 | mask = XParseGeometry((char *)gui.geom, &x, &y, &w, &h); |
| 4506 | |
| 4507 | if (mask & WidthValue) |
| 4508 | Columns = w; |
| 4509 | if (mask & HeightValue) |
Bram Moolenaar | d68071d | 2006-05-02 22:08:30 +0000 | [diff] [blame] | 4510 | { |
Bram Moolenaar | b85cb21 | 2009-05-17 14:24:23 +0000 | [diff] [blame] | 4511 | if (p_window > (long)h - 1 || !option_was_set((char_u *)"window")) |
Bram Moolenaar | d68071d | 2006-05-02 22:08:30 +0000 | [diff] [blame] | 4512 | p_window = h - 1; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4513 | Rows = h; |
Bram Moolenaar | d68071d | 2006-05-02 22:08:30 +0000 | [diff] [blame] | 4514 | } |
Bram Moolenaar | e057d40 | 2013-06-30 17:51:51 +0200 | [diff] [blame] | 4515 | limit_screen_size(); |
Bram Moolenaar | 2dd8b52 | 2007-10-19 12:33:44 +0000 | [diff] [blame] | 4516 | |
| 4517 | pixel_width = (guint)(gui_get_base_width() + Columns * gui.char_width); |
| 4518 | pixel_height = (guint)(gui_get_base_height() + Rows * gui.char_height); |
| 4519 | |
Bram Moolenaar | 2dd8b52 | 2007-10-19 12:33:44 +0000 | [diff] [blame] | 4520 | pixel_width += get_menu_tool_width(); |
| 4521 | pixel_height += get_menu_tool_height(); |
Bram Moolenaar | 2dd8b52 | 2007-10-19 12:33:44 +0000 | [diff] [blame] | 4522 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4523 | if (mask & (XValue | YValue)) |
Bram Moolenaar | 2dd8b52 | 2007-10-19 12:33:44 +0000 | [diff] [blame] | 4524 | { |
Bram Moolenaar | fe86f2d | 2008-11-28 20:29:07 +0000 | [diff] [blame] | 4525 | int ww, hh; |
Bram Moolenaar | a555e6f | 2021-03-18 22:28:57 +0100 | [diff] [blame] | 4526 | |
| 4527 | #ifdef FEAT_GUI_GTK |
| 4528 | gui_gtk_get_screen_dimensions(x, y, &ww, &hh); |
| 4529 | #else |
Bram Moolenaar | fe86f2d | 2008-11-28 20:29:07 +0000 | [diff] [blame] | 4530 | gui_mch_get_screen_dimensions(&ww, &hh); |
Bram Moolenaar | a555e6f | 2021-03-18 22:28:57 +0100 | [diff] [blame] | 4531 | #endif |
Bram Moolenaar | fe86f2d | 2008-11-28 20:29:07 +0000 | [diff] [blame] | 4532 | hh += p_ghr + get_menu_tool_height(); |
| 4533 | ww += get_menu_tool_width(); |
Bram Moolenaar | 2dd8b52 | 2007-10-19 12:33:44 +0000 | [diff] [blame] | 4534 | if (mask & XNegative) |
Bram Moolenaar | fe86f2d | 2008-11-28 20:29:07 +0000 | [diff] [blame] | 4535 | x += ww - pixel_width; |
Bram Moolenaar | 2dd8b52 | 2007-10-19 12:33:44 +0000 | [diff] [blame] | 4536 | if (mask & YNegative) |
Bram Moolenaar | fe86f2d | 2008-11-28 20:29:07 +0000 | [diff] [blame] | 4537 | y += hh - pixel_height; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4538 | gtk_window_move(GTK_WINDOW(gui.mainwin), x, y); |
Bram Moolenaar | 2dd8b52 | 2007-10-19 12:33:44 +0000 | [diff] [blame] | 4539 | } |
Bram Moolenaar | d23a823 | 2018-02-10 18:45:26 +0100 | [diff] [blame] | 4540 | VIM_CLEAR(gui.geom); |
Bram Moolenaar | b3656ed | 2006-03-20 21:59:49 +0000 | [diff] [blame] | 4541 | |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 4542 | // From now until everyone's stopped trying to set the window hints |
| 4543 | // to their correct minimum values, stop them being set as we need |
| 4544 | // them to remain at our required size for the parent GtkSocket to |
| 4545 | // give us the right initial size. |
Bram Moolenaar | b3656ed | 2006-03-20 21:59:49 +0000 | [diff] [blame] | 4546 | if (gtk_socket_id != 0 && (mask & WidthValue || mask & HeightValue)) |
Bram Moolenaar | c9b4b05 | 2006-04-30 18:54:39 +0000 | [diff] [blame] | 4547 | { |
Bram Moolenaar | c9b4b05 | 2006-04-30 18:54:39 +0000 | [diff] [blame] | 4548 | update_window_manager_hints(pixel_width, pixel_height); |
| 4549 | init_window_hints_state = 1; |
Bram Moolenaar | 4ab7968 | 2017-08-27 14:50:47 +0200 | [diff] [blame] | 4550 | timeout_add(1000, check_startup_plug_hints, NULL); |
Bram Moolenaar | c9b4b05 | 2006-04-30 18:54:39 +0000 | [diff] [blame] | 4551 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4552 | } |
| 4553 | |
Bram Moolenaar | 79ef6d6 | 2009-09-23 15:35:48 +0000 | [diff] [blame] | 4554 | pixel_width = (guint)(gui_get_base_width() + Columns * gui.char_width); |
| 4555 | pixel_height = (guint)(gui_get_base_height() + Rows * gui.char_height); |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 4556 | // For GTK2 changing the size of the form widget doesn't cause window |
| 4557 | // resizing. |
Bram Moolenaar | debe25a | 2010-06-06 17:41:24 +0200 | [diff] [blame] | 4558 | if (gtk_socket_id == 0) |
Bram Moolenaar | 79ef6d6 | 2009-09-23 15:35:48 +0000 | [diff] [blame] | 4559 | gtk_window_resize(GTK_WINDOW(gui.mainwin), pixel_width, pixel_height); |
Bram Moolenaar | b3656ed | 2006-03-20 21:59:49 +0000 | [diff] [blame] | 4560 | update_window_manager_hints(0, 0); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4561 | |
| 4562 | if (foreground_argument != NULL) |
| 4563 | fg_pixel = gui_get_color((char_u *)foreground_argument); |
| 4564 | if (fg_pixel == INVALCOLOR) |
| 4565 | fg_pixel = gui_get_color((char_u *)"Black"); |
| 4566 | |
| 4567 | if (background_argument != NULL) |
| 4568 | bg_pixel = gui_get_color((char_u *)background_argument); |
| 4569 | if (bg_pixel == INVALCOLOR) |
| 4570 | bg_pixel = gui_get_color((char_u *)"White"); |
| 4571 | |
| 4572 | if (found_reverse_arg) |
| 4573 | { |
| 4574 | gui.def_norm_pixel = bg_pixel; |
| 4575 | gui.def_back_pixel = fg_pixel; |
| 4576 | } |
| 4577 | else |
| 4578 | { |
| 4579 | gui.def_norm_pixel = fg_pixel; |
| 4580 | gui.def_back_pixel = bg_pixel; |
| 4581 | } |
| 4582 | |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 4583 | // Get the colors from the "Normal" and "Menu" group (set in syntax.c or |
| 4584 | // in a vimrc file) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4585 | set_normal_colors(); |
| 4586 | |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 4587 | // Check that none of the colors are the same as the background color |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4588 | gui_check_colors(); |
| 4589 | |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 4590 | // Get the colors for the highlight groups (gui_check_colors() might have |
| 4591 | // changed them). |
| 4592 | highlight_gui_started(); // re-init colors and fonts |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4593 | |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 4594 | g_signal_connect(G_OBJECT(gui.mainwin), "destroy", |
| 4595 | G_CALLBACK(mainwin_destroy_cb), NULL); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4596 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4597 | /* |
| 4598 | * Notify the fixed area about the need to resize the contents of the |
| 4599 | * gui.formwin, which we use for random positioning of the included |
| 4600 | * components. |
| 4601 | * |
| 4602 | * We connect this signal deferred finally after anything is in place, |
| 4603 | * since this is intended to handle resizements coming from the window |
| 4604 | * manager upon us and should not interfere with what VIM is requesting |
| 4605 | * upon startup. |
| 4606 | */ |
Ernie Rael | d42b839 | 2022-04-23 19:52:23 +0100 | [diff] [blame] | 4607 | #ifdef TRACK_RESIZE_HISTORY |
Ernie Rael | 9f53e7b | 2022-04-17 18:27:49 +0100 | [diff] [blame] | 4608 | latest_resize_hist = ALLOC_CLEAR_ONE(resize_hist_T); |
Ernie Rael | d42b839 | 2022-04-23 19:52:23 +0100 | [diff] [blame] | 4609 | #endif |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 4610 | g_signal_connect(G_OBJECT(gui.formwin), "configure-event", |
Ernie Rael | 9f53e7b | 2022-04-17 18:27:49 +0100 | [diff] [blame] | 4611 | G_CALLBACK(form_configure_event), NULL); |
lilydjwg | 6e0a18f | 2024-01-29 20:54:28 +0100 | [diff] [blame] | 4612 | #if GTK_CHECK_VERSION(3,10,0) |
| 4613 | g_signal_connect(G_OBJECT(gui.formwin), "notify::scale-factor", |
| 4614 | G_CALLBACK(scale_factor_event), NULL); |
| 4615 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4616 | |
| 4617 | #ifdef FEAT_DND |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 4618 | // Set up for receiving DND items. |
Bram Moolenaar | a76638f | 2010-06-05 12:49:46 +0200 | [diff] [blame] | 4619 | gui_gtk_set_dnd_targets(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4620 | |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 4621 | g_signal_connect(G_OBJECT(gui.drawarea), "drag-data-received", |
Ernie Rael | 9f53e7b | 2022-04-17 18:27:49 +0100 | [diff] [blame] | 4622 | G_CALLBACK(drag_data_received_cb), NULL); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4623 | #endif |
| 4624 | |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 4625 | // With GTK+ 2, we need to iconify the window before calling show() |
| 4626 | // to avoid mapping the window for a short time. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4627 | if (found_iconic_arg && gtk_socket_id == 0) |
| 4628 | gui_mch_iconify(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4629 | |
| 4630 | { |
Bram Moolenaar | 182c5be | 2010-06-25 05:37:59 +0200 | [diff] [blame] | 4631 | #if defined(FEAT_GUI_GNOME) && defined(FEAT_MENU) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4632 | unsigned long menu_handler = 0; |
| 4633 | # ifdef FEAT_TOOLBAR |
| 4634 | unsigned long tool_handler = 0; |
| 4635 | # endif |
| 4636 | /* |
| 4637 | * Urgh hackish :/ For some reason BonoboDockLayout always forces a |
| 4638 | * show when restoring the saved layout configuration. We can't just |
| 4639 | * hide the widgets again after gtk_widget_show(gui.mainwin) since it's |
| 4640 | * a toplevel window and thus will be realized immediately. Instead, |
| 4641 | * connect signal handlers to hide the widgets just after they've been |
| 4642 | * marked visible, but before the main window is realized. |
| 4643 | */ |
| 4644 | if (using_gnome && vim_strchr(p_go, GO_MENUS) == NULL) |
| 4645 | menu_handler = g_signal_connect_after(gui.menubar_h, "show", |
| 4646 | G_CALLBACK(>k_widget_hide), |
| 4647 | NULL); |
| 4648 | # ifdef FEAT_TOOLBAR |
| 4649 | if (using_gnome && vim_strchr(p_go, GO_TOOLBAR) == NULL |
| 4650 | && (toolbar_flags & (TOOLBAR_TEXT | TOOLBAR_ICONS))) |
| 4651 | tool_handler = g_signal_connect_after(gui.toolbar_h, "show", |
| 4652 | G_CALLBACK(>k_widget_hide), |
| 4653 | NULL); |
| 4654 | # endif |
| 4655 | #endif |
| 4656 | gtk_widget_show(gui.mainwin); |
| 4657 | |
Bram Moolenaar | 182c5be | 2010-06-25 05:37:59 +0200 | [diff] [blame] | 4658 | #if defined(FEAT_GUI_GNOME) && defined(FEAT_MENU) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4659 | if (menu_handler != 0) |
| 4660 | g_signal_handler_disconnect(gui.menubar_h, menu_handler); |
| 4661 | # ifdef FEAT_TOOLBAR |
| 4662 | if (tool_handler != 0) |
| 4663 | g_signal_handler_disconnect(gui.toolbar_h, tool_handler); |
| 4664 | # endif |
| 4665 | #endif |
| 4666 | } |
| 4667 | |
lilydjwg | 94ff09a | 2024-01-29 20:14:01 +0100 | [diff] [blame] | 4668 | /* |
| 4669 | * Add selection handler functions. |
| 4670 | */ |
| 4671 | g_signal_connect(G_OBJECT(gui.drawarea), "selection-clear-event", |
| 4672 | G_CALLBACK(selection_clear_event), NULL); |
| 4673 | g_signal_connect(G_OBJECT(gui.drawarea), "selection-received", |
| 4674 | G_CALLBACK(selection_received_cb), NULL); |
| 4675 | |
| 4676 | g_signal_connect(G_OBJECT(gui.drawarea), "selection-get", |
| 4677 | G_CALLBACK(selection_get_cb), NULL); |
| 4678 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4679 | return OK; |
| 4680 | } |
| 4681 | |
Bram Moolenaar | 32fbc4f | 2020-09-29 22:16:09 +0200 | [diff] [blame] | 4682 | /* |
| 4683 | * Clean up for when exiting Vim. |
| 4684 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4685 | void |
Bram Moolenaar | b85cb21 | 2009-05-17 14:24:23 +0000 | [diff] [blame] | 4686 | gui_mch_exit(int rc UNUSED) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4687 | { |
Bram Moolenaar | 32fbc4f | 2020-09-29 22:16:09 +0200 | [diff] [blame] | 4688 | // Clean up, unless we don't want to invoke free(). |
| 4689 | if (gui.mainwin != NULL && !really_exiting) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4690 | gtk_widget_destroy(gui.mainwin); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4691 | } |
| 4692 | |
| 4693 | /* |
| 4694 | * Get the position of the top left corner of the window. |
| 4695 | */ |
| 4696 | int |
| 4697 | gui_mch_get_winpos(int *x, int *y) |
| 4698 | { |
lilydjwg | 6e0a18f | 2024-01-29 20:54:28 +0100 | [diff] [blame] | 4699 | if (gui_mch_get_display()) |
| 4700 | { |
| 4701 | gtk_window_get_position(GTK_WINDOW(gui.mainwin), x, y); |
| 4702 | return OK; |
| 4703 | } |
| 4704 | return FAIL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4705 | } |
| 4706 | |
| 4707 | /* |
| 4708 | * Set the position of the top left corner of the window to the given |
| 4709 | * coordinates. |
| 4710 | */ |
| 4711 | void |
| 4712 | gui_mch_set_winpos(int x, int y) |
| 4713 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4714 | gtk_window_move(GTK_WINDOW(gui.mainwin), x, y); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4715 | } |
| 4716 | |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 4717 | #if !GTK_CHECK_VERSION(3,0,0) |
| 4718 | # if 0 |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4719 | static int resize_idle_installed = FALSE; |
| 4720 | /* |
| 4721 | * Idle handler to force resize. Used by gui_mch_set_shellsize() to ensure |
| 4722 | * the shell size doesn't exceed the window size, i.e. if the window manager |
| 4723 | * ignored our size request. Usually this happens if the window is maximized. |
| 4724 | * |
| 4725 | * FIXME: It'd be nice if we could find a little more orthodox solution. |
| 4726 | * See also the remark below in gui_mch_set_shellsize(). |
| 4727 | * |
| 4728 | * DISABLED: When doing ":set lines+=1" this function would first invoke |
| 4729 | * gui_resize_shell() with the old size, then the normal callback would |
| 4730 | * report the new size through form_configure_event(). That caused the window |
| 4731 | * layout to be messed up. |
| 4732 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4733 | static gboolean |
| 4734 | force_shell_resize_idle(gpointer data) |
| 4735 | { |
| 4736 | if (gui.mainwin != NULL |
| 4737 | && GTK_WIDGET_REALIZED(gui.mainwin) |
| 4738 | && GTK_WIDGET_VISIBLE(gui.mainwin)) |
| 4739 | { |
| 4740 | int width; |
| 4741 | int height; |
| 4742 | |
| 4743 | gtk_window_get_size(GTK_WINDOW(gui.mainwin), &width, &height); |
| 4744 | |
| 4745 | width -= get_menu_tool_width(); |
| 4746 | height -= get_menu_tool_height(); |
| 4747 | |
| 4748 | gui_resize_shell(width, height); |
| 4749 | } |
| 4750 | |
| 4751 | resize_idle_installed = FALSE; |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 4752 | return FALSE; // don't call me again |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4753 | } |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 4754 | # endif |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 4755 | #endif // !GTK_CHECK_VERSION(3,0,0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4756 | |
Bram Moolenaar | 0973623 | 2009-09-23 16:14:49 +0000 | [diff] [blame] | 4757 | /* |
| 4758 | * Return TRUE if the main window is maximized. |
| 4759 | */ |
| 4760 | int |
Bram Moolenaar | 66f948e | 2016-01-30 16:39:25 +0100 | [diff] [blame] | 4761 | gui_mch_maximized(void) |
Bram Moolenaar | 0973623 | 2009-09-23 16:14:49 +0000 | [diff] [blame] | 4762 | { |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 4763 | return (gui.mainwin != NULL && gtk_widget_get_window(gui.mainwin) != NULL |
| 4764 | && (gdk_window_get_state(gtk_widget_get_window(gui.mainwin)) |
| 4765 | & GDK_WINDOW_STATE_MAXIMIZED)); |
Bram Moolenaar | 0973623 | 2009-09-23 16:14:49 +0000 | [diff] [blame] | 4766 | } |
| 4767 | |
| 4768 | /* |
| 4769 | * Unmaximize the main window |
| 4770 | */ |
| 4771 | void |
Bram Moolenaar | 66f948e | 2016-01-30 16:39:25 +0100 | [diff] [blame] | 4772 | gui_mch_unmaximize(void) |
Bram Moolenaar | 0973623 | 2009-09-23 16:14:49 +0000 | [diff] [blame] | 4773 | { |
| 4774 | if (gui.mainwin != NULL) |
| 4775 | gtk_window_unmaximize(GTK_WINDOW(gui.mainwin)); |
| 4776 | } |
Bram Moolenaar | 0973623 | 2009-09-23 16:14:49 +0000 | [diff] [blame] | 4777 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4778 | /* |
Bram Moolenaar | 8ac4415 | 2017-11-09 18:33:29 +0100 | [diff] [blame] | 4779 | * Called when the font changed while the window is maximized or GO_KEEPWINSIZE |
| 4780 | * is set. Compute the new Rows and Columns. This is like resizing the |
| 4781 | * window. |
Bram Moolenaar | 12bc1b5 | 2011-08-10 17:44:45 +0200 | [diff] [blame] | 4782 | */ |
| 4783 | void |
Bram Moolenaar | 66f948e | 2016-01-30 16:39:25 +0100 | [diff] [blame] | 4784 | gui_mch_newfont(void) |
Bram Moolenaar | 12bc1b5 | 2011-08-10 17:44:45 +0200 | [diff] [blame] | 4785 | { |
| 4786 | int w, h; |
| 4787 | |
| 4788 | gtk_window_get_size(GTK_WINDOW(gui.mainwin), &w, &h); |
| 4789 | w -= get_menu_tool_width(); |
| 4790 | h -= get_menu_tool_height(); |
| 4791 | gui_resize_shell(w, h); |
| 4792 | } |
| 4793 | |
| 4794 | /* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4795 | * Set the windows size. |
| 4796 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4797 | void |
| 4798 | gui_mch_set_shellsize(int width, int height, |
Bram Moolenaar | b85cb21 | 2009-05-17 14:24:23 +0000 | [diff] [blame] | 4799 | int min_width UNUSED, int min_height UNUSED, |
| 4800 | int base_width UNUSED, int base_height UNUSED, |
| 4801 | int direction UNUSED) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4802 | { |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 4803 | // give GTK+ a chance to put all widget's into place |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4804 | gui_mch_update(); |
| 4805 | |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 4806 | // this will cause the proper resizement to happen too |
Bram Moolenaar | b3656ed | 2006-03-20 21:59:49 +0000 | [diff] [blame] | 4807 | if (gtk_socket_id == 0) |
Bram Moolenaar | c9b4b05 | 2006-04-30 18:54:39 +0000 | [diff] [blame] | 4808 | update_window_manager_hints(0, 0); |
Bram Moolenaar | b3656ed | 2006-03-20 21:59:49 +0000 | [diff] [blame] | 4809 | |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 4810 | // With GTK+ 2, changing the size of the form widget doesn't resize |
| 4811 | // the window. So let's do it the other way around and resize the |
| 4812 | // main window instead. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4813 | width += get_menu_tool_width(); |
| 4814 | height += get_menu_tool_height(); |
| 4815 | |
Ernie Rael | d42b839 | 2022-04-23 19:52:23 +0100 | [diff] [blame] | 4816 | #ifdef TRACK_RESIZE_HISTORY |
Ernie Rael | 9f53e7b | 2022-04-17 18:27:49 +0100 | [diff] [blame] | 4817 | alloc_resize_hist(width, height); // track the resize request |
Ernie Rael | d42b839 | 2022-04-23 19:52:23 +0100 | [diff] [blame] | 4818 | #endif |
Bram Moolenaar | b3656ed | 2006-03-20 21:59:49 +0000 | [diff] [blame] | 4819 | if (gtk_socket_id == 0) |
Bram Moolenaar | c9b4b05 | 2006-04-30 18:54:39 +0000 | [diff] [blame] | 4820 | gtk_window_resize(GTK_WINDOW(gui.mainwin), width, height); |
Bram Moolenaar | b3656ed | 2006-03-20 21:59:49 +0000 | [diff] [blame] | 4821 | else |
Bram Moolenaar | c9b4b05 | 2006-04-30 18:54:39 +0000 | [diff] [blame] | 4822 | update_window_manager_hints(width, height); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4823 | |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 4824 | # if !GTK_CHECK_VERSION(3,0,0) |
| 4825 | # if 0 |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4826 | if (!resize_idle_installed) |
| 4827 | { |
| 4828 | g_idle_add_full(GDK_PRIORITY_EVENTS + 10, |
| 4829 | &force_shell_resize_idle, NULL, NULL); |
| 4830 | resize_idle_installed = TRUE; |
| 4831 | } |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 4832 | # endif |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 4833 | # endif // !GTK_CHECK_VERSION(3,0,0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4834 | /* |
| 4835 | * Wait until all events are processed to prevent a crash because the |
| 4836 | * real size of the drawing area doesn't reflect Vim's internal ideas. |
| 4837 | * |
| 4838 | * This is a bit of a hack, since Vim is a terminal application with a GUI |
| 4839 | * on top, while the GUI expects to be the boss. |
| 4840 | */ |
| 4841 | gui_mch_update(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4842 | } |
| 4843 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4844 | void |
Bram Moolenaar | b85cb21 | 2009-05-17 14:24:23 +0000 | [diff] [blame] | 4845 | gui_mch_settitle(char_u *title, char_u *icon UNUSED) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4846 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4847 | if (title != NULL && output_conv.vc_type != CONV_NONE) |
| 4848 | title = string_convert(&output_conv, title, NULL); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4849 | |
| 4850 | gtk_window_set_title(GTK_WINDOW(gui.mainwin), (const char *)title); |
| 4851 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4852 | if (output_conv.vc_type != CONV_NONE) |
| 4853 | vim_free(title); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4854 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4855 | |
| 4856 | #if defined(FEAT_MENU) || defined(PROTO) |
| 4857 | void |
| 4858 | gui_mch_enable_menu(int showit) |
| 4859 | { |
| 4860 | GtkWidget *widget; |
| 4861 | |
| 4862 | # ifdef FEAT_GUI_GNOME |
| 4863 | if (using_gnome) |
| 4864 | widget = gui.menubar_h; |
| 4865 | else |
| 4866 | # endif |
| 4867 | widget = gui.menubar; |
| 4868 | |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 4869 | // Do not disable the menu while starting up, otherwise F10 doesn't work. |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 4870 | if (!showit != !gtk_widget_get_visible(widget) && !gui.starting) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4871 | { |
| 4872 | if (showit) |
| 4873 | gtk_widget_show(widget); |
| 4874 | else |
| 4875 | gtk_widget_hide(widget); |
| 4876 | |
Bram Moolenaar | b3656ed | 2006-03-20 21:59:49 +0000 | [diff] [blame] | 4877 | update_window_manager_hints(0, 0); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4878 | } |
| 4879 | } |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 4880 | #endif // FEAT_MENU |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4881 | |
| 4882 | #if defined(FEAT_TOOLBAR) || defined(PROTO) |
| 4883 | void |
| 4884 | gui_mch_show_toolbar(int showit) |
| 4885 | { |
| 4886 | GtkWidget *widget; |
| 4887 | |
| 4888 | if (gui.toolbar == NULL) |
| 4889 | return; |
| 4890 | |
| 4891 | # ifdef FEAT_GUI_GNOME |
| 4892 | if (using_gnome) |
| 4893 | widget = gui.toolbar_h; |
| 4894 | else |
| 4895 | # endif |
| 4896 | widget = gui.toolbar; |
| 4897 | |
| 4898 | if (showit) |
| 4899 | set_toolbar_style(GTK_TOOLBAR(gui.toolbar)); |
| 4900 | |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 4901 | if (!showit != !gtk_widget_get_visible(widget)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4902 | { |
| 4903 | if (showit) |
| 4904 | gtk_widget_show(widget); |
| 4905 | else |
| 4906 | gtk_widget_hide(widget); |
| 4907 | |
Bram Moolenaar | b3656ed | 2006-03-20 21:59:49 +0000 | [diff] [blame] | 4908 | update_window_manager_hints(0, 0); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4909 | } |
| 4910 | } |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 4911 | #endif // FEAT_TOOLBAR |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4912 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4913 | /* |
| 4914 | * Check if a given font is a CJK font. This is done in a very crude manner. It |
| 4915 | * just see if U+04E00 for zh and ja and U+AC00 for ko are covered in a given |
| 4916 | * font. Consequently, this function cannot be used as a general purpose check |
| 4917 | * for CJK-ness for which fontconfig APIs should be used. This is only used by |
| 4918 | * gui_mch_init_font() to deal with 'CJK fixed width fonts'. |
| 4919 | */ |
| 4920 | static int |
| 4921 | is_cjk_font(PangoFontDescription *font_desc) |
| 4922 | { |
| 4923 | static const char * const cjk_langs[] = |
| 4924 | {"zh_CN", "zh_TW", "zh_HK", "ja", "ko"}; |
| 4925 | |
| 4926 | PangoFont *font; |
| 4927 | unsigned i; |
| 4928 | int is_cjk = FALSE; |
| 4929 | |
| 4930 | font = pango_context_load_font(gui.text_context, font_desc); |
| 4931 | |
| 4932 | if (font == NULL) |
| 4933 | return FALSE; |
| 4934 | |
| 4935 | for (i = 0; !is_cjk && i < G_N_ELEMENTS(cjk_langs); ++i) |
| 4936 | { |
| 4937 | PangoCoverage *coverage; |
| 4938 | gunichar uc; |
| 4939 | |
Bram Moolenaar | 14285cb | 2020-03-27 20:58:37 +0100 | [diff] [blame] | 4940 | // Valgrind reports a leak for pango_language_from_string(), but the |
| 4941 | // documentation says "This is owned by Pango and should not be freed". |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4942 | coverage = pango_font_get_coverage( |
| 4943 | font, pango_language_from_string(cjk_langs[i])); |
| 4944 | |
| 4945 | if (coverage != NULL) |
| 4946 | { |
| 4947 | uc = (cjk_langs[i][0] == 'k') ? 0xAC00 : 0x4E00; |
| 4948 | is_cjk = (pango_coverage_get(coverage, uc) == PANGO_COVERAGE_EXACT); |
James McCoy | fe14636 | 2023-08-28 21:29:13 +0200 | [diff] [blame] | 4949 | #if PANGO_VERSION_CHECK(1, 52, 0) |
| 4950 | g_object_unref(coverage); |
| 4951 | #else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4952 | pango_coverage_unref(coverage); |
James McCoy | fe14636 | 2023-08-28 21:29:13 +0200 | [diff] [blame] | 4953 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4954 | } |
| 4955 | } |
| 4956 | |
| 4957 | g_object_unref(font); |
| 4958 | |
| 4959 | return is_cjk; |
| 4960 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4961 | |
Bram Moolenaar | 231334e | 2005-07-25 20:46:57 +0000 | [diff] [blame] | 4962 | /* |
| 4963 | * Adjust gui.char_height (after 'linespace' was changed). |
| 4964 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4965 | int |
Bram Moolenaar | 231334e | 2005-07-25 20:46:57 +0000 | [diff] [blame] | 4966 | gui_mch_adjust_charheight(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4967 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4968 | PangoFontMetrics *metrics; |
| 4969 | int ascent; |
| 4970 | int descent; |
| 4971 | |
| 4972 | metrics = pango_context_get_metrics(gui.text_context, gui.norm_font, |
| 4973 | pango_context_get_language(gui.text_context)); |
| 4974 | ascent = pango_font_metrics_get_ascent(metrics); |
| 4975 | descent = pango_font_metrics_get_descent(metrics); |
| 4976 | |
| 4977 | pango_font_metrics_unref(metrics); |
| 4978 | |
Bram Moolenaar | 70cf458 | 2020-10-27 20:43:26 +0100 | [diff] [blame] | 4979 | // Round up when the value is more than about 1/16 of a pixel above a whole |
| 4980 | // pixel (12.0624 becomes 12, 12.07 becomes 13). Then add 'linespace'. |
| 4981 | gui.char_height = (ascent + descent + (PANGO_SCALE * 15) / 16) |
| 4982 | / PANGO_SCALE + p_linespace; |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 4983 | // LINTED: avoid warning: bitwise operation on signed value |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4984 | gui.char_ascent = PANGO_PIXELS(ascent + p_linespace * PANGO_SCALE / 2); |
| 4985 | |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 4986 | // A not-positive value of char_height may crash Vim. Only happens |
| 4987 | // if 'linespace' is negative (which does make sense sometimes). |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4988 | gui.char_ascent = MAX(gui.char_ascent, 0); |
| 4989 | gui.char_height = MAX(gui.char_height, gui.char_ascent + 1); |
| 4990 | |
| 4991 | return OK; |
| 4992 | } |
| 4993 | |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 4994 | #if GTK_CHECK_VERSION(3,0,0) |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 4995 | // Callback function used in gui_mch_font_dialog() |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 4996 | static gboolean |
| 4997 | font_filter(const PangoFontFamily *family, |
| 4998 | const PangoFontFace *face UNUSED, |
| 4999 | gpointer data UNUSED) |
| 5000 | { |
| 5001 | return pango_font_family_is_monospace((PangoFontFamily *)family); |
| 5002 | } |
| 5003 | #endif |
| 5004 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5005 | /* |
| 5006 | * Put up a font dialog and return the selected font name in allocated memory. |
| 5007 | * "oldval" is the previous value. Return NULL when cancelled. |
| 5008 | * This should probably go into gui_gtk.c. Hmm. |
| 5009 | * FIXME: |
| 5010 | * The GTK2 font selection dialog has no filtering API. So we could either |
| 5011 | * a) implement our own (possibly copying the code from somewhere else) or |
| 5012 | * b) just live with it. |
| 5013 | */ |
| 5014 | char_u * |
| 5015 | gui_mch_font_dialog(char_u *oldval) |
| 5016 | { |
| 5017 | GtkWidget *dialog; |
| 5018 | int response; |
| 5019 | char_u *fontname = NULL; |
| 5020 | char_u *oldname; |
| 5021 | |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 5022 | #if GTK_CHECK_VERSION(3,2,0) |
| 5023 | dialog = gtk_font_chooser_dialog_new(NULL, NULL); |
| 5024 | gtk_font_chooser_set_filter_func(GTK_FONT_CHOOSER(dialog), font_filter, |
| 5025 | NULL, NULL); |
| 5026 | #else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5027 | dialog = gtk_font_selection_dialog_new(NULL); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 5028 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5029 | |
| 5030 | gtk_window_set_transient_for(GTK_WINDOW(dialog), GTK_WINDOW(gui.mainwin)); |
| 5031 | gtk_window_set_destroy_with_parent(GTK_WINDOW(dialog), TRUE); |
| 5032 | |
| 5033 | if (oldval != NULL && oldval[0] != NUL) |
| 5034 | { |
| 5035 | if (output_conv.vc_type != CONV_NONE) |
| 5036 | oldname = string_convert(&output_conv, oldval, NULL); |
| 5037 | else |
| 5038 | oldname = oldval; |
| 5039 | |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 5040 | // Annoying bug in GTK (or Pango): if the font name does not include a |
| 5041 | // size, zero is used. Use default point size ten. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5042 | if (!vim_isdigit(oldname[STRLEN(oldname) - 1])) |
| 5043 | { |
| 5044 | char_u *p = vim_strnsave(oldname, STRLEN(oldname) + 3); |
| 5045 | |
| 5046 | if (p != NULL) |
| 5047 | { |
| 5048 | STRCPY(p + STRLEN(p), " 10"); |
| 5049 | if (oldname != oldval) |
| 5050 | vim_free(oldname); |
| 5051 | oldname = p; |
| 5052 | } |
| 5053 | } |
| 5054 | |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 5055 | #if GTK_CHECK_VERSION(3,2,0) |
| 5056 | gtk_font_chooser_set_font( |
| 5057 | GTK_FONT_CHOOSER(dialog), (const gchar *)oldname); |
| 5058 | #else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5059 | gtk_font_selection_dialog_set_font_name( |
| 5060 | GTK_FONT_SELECTION_DIALOG(dialog), (const char *)oldname); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 5061 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5062 | |
| 5063 | if (oldname != oldval) |
Bram Moolenaar | 8c71145 | 2005-01-14 21:53:12 +0000 | [diff] [blame] | 5064 | vim_free(oldname); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5065 | } |
Bram Moolenaar | bef9d83 | 2009-09-11 13:46:41 +0000 | [diff] [blame] | 5066 | else |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 5067 | #if GTK_CHECK_VERSION(3,2,0) |
| 5068 | gtk_font_chooser_set_font( |
| 5069 | GTK_FONT_CHOOSER(dialog), DEFAULT_FONT); |
| 5070 | #else |
Bram Moolenaar | bef9d83 | 2009-09-11 13:46:41 +0000 | [diff] [blame] | 5071 | gtk_font_selection_dialog_set_font_name( |
| 5072 | GTK_FONT_SELECTION_DIALOG(dialog), DEFAULT_FONT); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 5073 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5074 | |
| 5075 | response = gtk_dialog_run(GTK_DIALOG(dialog)); |
| 5076 | |
| 5077 | if (response == GTK_RESPONSE_OK) |
| 5078 | { |
| 5079 | char *name; |
| 5080 | |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 5081 | #if GTK_CHECK_VERSION(3,2,0) |
| 5082 | name = gtk_font_chooser_get_font(GTK_FONT_CHOOSER(dialog)); |
| 5083 | #else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5084 | name = gtk_font_selection_dialog_get_font_name( |
| 5085 | GTK_FONT_SELECTION_DIALOG(dialog)); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 5086 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5087 | if (name != NULL) |
| 5088 | { |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 5089 | char_u *p; |
| 5090 | |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 5091 | // Apparently some font names include a comma, need to escape |
| 5092 | // that, because in 'guifont' it separates names. |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 5093 | p = vim_strsave_escaped((char_u *)name, (char_u *)","); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5094 | g_free(name); |
Bram Moolenaar | eb3593b | 2006-04-22 22:33:57 +0000 | [diff] [blame] | 5095 | if (p != NULL && input_conv.vc_type != CONV_NONE) |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 5096 | { |
| 5097 | fontname = string_convert(&input_conv, p, NULL); |
| 5098 | vim_free(p); |
| 5099 | } |
| 5100 | else |
| 5101 | fontname = p; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5102 | } |
| 5103 | } |
| 5104 | |
| 5105 | if (response != GTK_RESPONSE_NONE) |
| 5106 | gtk_widget_destroy(dialog); |
| 5107 | |
| 5108 | return fontname; |
| 5109 | } |
| 5110 | |
| 5111 | /* |
| 5112 | * Some monospace fonts don't support a bold weight, and fall back |
| 5113 | * silently to the regular weight. But this is no good since our text |
| 5114 | * drawing function can emulate bold by overstriking. So let's try |
| 5115 | * to detect whether bold weight is actually available and emulate it |
| 5116 | * otherwise. |
| 5117 | * |
| 5118 | * Note that we don't need to check for italic style since Xft can |
| 5119 | * emulate italic on its own, provided you have a proper fontconfig |
| 5120 | * setup. We wouldn't be able to emulate it in Vim anyway. |
| 5121 | */ |
| 5122 | static void |
| 5123 | get_styled_font_variants(void) |
| 5124 | { |
| 5125 | PangoFontDescription *bold_font_desc; |
| 5126 | PangoFont *plain_font; |
| 5127 | PangoFont *bold_font; |
| 5128 | |
| 5129 | gui.font_can_bold = FALSE; |
| 5130 | |
| 5131 | plain_font = pango_context_load_font(gui.text_context, gui.norm_font); |
| 5132 | |
| 5133 | if (plain_font == NULL) |
| 5134 | return; |
| 5135 | |
| 5136 | bold_font_desc = pango_font_description_copy_static(gui.norm_font); |
| 5137 | pango_font_description_set_weight(bold_font_desc, PANGO_WEIGHT_BOLD); |
| 5138 | |
| 5139 | bold_font = pango_context_load_font(gui.text_context, bold_font_desc); |
| 5140 | /* |
| 5141 | * The comparison relies on the unique handle nature of a PangoFont*, |
| 5142 | * i.e. it's assumed that a different PangoFont* won't refer to the |
| 5143 | * same font. Seems to work, and failing here isn't critical anyway. |
| 5144 | */ |
| 5145 | if (bold_font != NULL) |
| 5146 | { |
| 5147 | gui.font_can_bold = (bold_font != plain_font); |
| 5148 | g_object_unref(bold_font); |
| 5149 | } |
| 5150 | |
| 5151 | pango_font_description_free(bold_font_desc); |
Christian Brabandt | a5218a7 | 2023-11-19 16:25:45 +0100 | [diff] [blame] | 5152 | if (bold_font != NULL && gui.font_can_bold) |
| 5153 | g_object_unref(plain_font); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5154 | } |
| 5155 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5156 | static PangoEngineShape *default_shape_engine = NULL; |
| 5157 | |
| 5158 | /* |
| 5159 | * Create a map from ASCII characters in the range [32,126] to glyphs |
| 5160 | * of the current font. This is used by gui_gtk2_draw_string() to skip |
| 5161 | * the itemize and shaping process for the most common case. |
| 5162 | */ |
| 5163 | static void |
| 5164 | ascii_glyph_table_init(void) |
| 5165 | { |
Bram Moolenaar | 16350cb | 2016-08-14 20:27:34 +0200 | [diff] [blame] | 5166 | char_u ascii_chars[2 * 128]; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5167 | PangoAttrList *attr_list; |
| 5168 | GList *item_list; |
| 5169 | int i; |
| 5170 | |
| 5171 | if (gui.ascii_glyphs != NULL) |
| 5172 | pango_glyph_string_free(gui.ascii_glyphs); |
| 5173 | if (gui.ascii_font != NULL) |
| 5174 | g_object_unref(gui.ascii_font); |
| 5175 | |
| 5176 | gui.ascii_glyphs = NULL; |
| 5177 | gui.ascii_font = NULL; |
| 5178 | |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 5179 | // For safety, fill in question marks for the control characters. |
| 5180 | // Put a space between characters to avoid shaping. |
Bram Moolenaar | 16350cb | 2016-08-14 20:27:34 +0200 | [diff] [blame] | 5181 | for (i = 0; i < 128; ++i) |
| 5182 | { |
| 5183 | if (i >= 32 && i < 127) |
| 5184 | ascii_chars[2 * i] = i; |
| 5185 | else |
| 5186 | ascii_chars[2 * i] = '?'; |
| 5187 | ascii_chars[2 * i + 1] = ' '; |
| 5188 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5189 | |
| 5190 | attr_list = pango_attr_list_new(); |
| 5191 | item_list = pango_itemize(gui.text_context, (const char *)ascii_chars, |
| 5192 | 0, sizeof(ascii_chars), attr_list, NULL); |
| 5193 | |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 5194 | if (item_list != NULL && item_list->next == NULL) // play safe |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5195 | { |
| 5196 | PangoItem *item; |
| 5197 | int width; |
| 5198 | |
| 5199 | item = (PangoItem *)item_list->data; |
| 5200 | width = gui.char_width * PANGO_SCALE; |
| 5201 | |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 5202 | // Remember the shape engine used for ASCII. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5203 | default_shape_engine = item->analysis.shape_engine; |
| 5204 | |
| 5205 | gui.ascii_font = item->analysis.font; |
| 5206 | g_object_ref(gui.ascii_font); |
| 5207 | |
| 5208 | gui.ascii_glyphs = pango_glyph_string_new(); |
| 5209 | |
| 5210 | pango_shape((const char *)ascii_chars, sizeof(ascii_chars), |
| 5211 | &item->analysis, gui.ascii_glyphs); |
| 5212 | |
| 5213 | g_return_if_fail(gui.ascii_glyphs->num_glyphs == sizeof(ascii_chars)); |
| 5214 | |
| 5215 | for (i = 0; i < gui.ascii_glyphs->num_glyphs; ++i) |
| 5216 | { |
| 5217 | PangoGlyphGeometry *geom; |
| 5218 | |
| 5219 | geom = &gui.ascii_glyphs->glyphs[i].geometry; |
| 5220 | geom->x_offset += MAX(0, width - geom->width) / 2; |
| 5221 | geom->width = width; |
| 5222 | } |
| 5223 | } |
| 5224 | |
Bram Moolenaar | 81a4cf4 | 2020-09-09 19:05:13 +0200 | [diff] [blame] | 5225 | // TODO: is this type cast OK? |
| 5226 | g_list_foreach(item_list, (GFunc)(void *)&pango_item_free, NULL); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5227 | g_list_free(item_list); |
| 5228 | pango_attr_list_unref(attr_list); |
| 5229 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5230 | |
| 5231 | /* |
| 5232 | * Initialize Vim to use the font or fontset with the given name. |
| 5233 | * Return FAIL if the font could not be loaded, OK otherwise. |
| 5234 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5235 | int |
Bram Moolenaar | b85cb21 | 2009-05-17 14:24:23 +0000 | [diff] [blame] | 5236 | gui_mch_init_font(char_u *font_name, int fontset UNUSED) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5237 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5238 | PangoFontDescription *font_desc; |
| 5239 | PangoLayout *layout; |
| 5240 | int width; |
| 5241 | |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 5242 | // If font_name is NULL, this means to use the default, which should |
| 5243 | // be present on all proper Pango/fontconfig installations. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5244 | if (font_name == NULL) |
| 5245 | font_name = (char_u *)DEFAULT_FONT; |
| 5246 | |
| 5247 | font_desc = gui_mch_get_font(font_name, FALSE); |
| 5248 | |
| 5249 | if (font_desc == NULL) |
| 5250 | return FAIL; |
| 5251 | |
| 5252 | gui_mch_free_font(gui.norm_font); |
| 5253 | gui.norm_font = font_desc; |
| 5254 | |
| 5255 | pango_context_set_font_description(gui.text_context, font_desc); |
| 5256 | |
| 5257 | layout = pango_layout_new(gui.text_context); |
| 5258 | pango_layout_set_text(layout, "MW", 2); |
| 5259 | pango_layout_get_size(layout, &width, NULL); |
| 5260 | /* |
| 5261 | * Set char_width to half the width obtained from pango_layout_get_size() |
| 5262 | * for CJK fixed_width/bi-width fonts. An unpatched version of Xft leads |
| 5263 | * Pango to use the same width for both non-CJK characters (e.g. Latin |
| 5264 | * letters and numbers) and CJK characters. This results in 's p a c e d |
| 5265 | * o u t' rendering when a CJK 'fixed width' font is used. To work around |
| 5266 | * that, divide the width returned by Pango by 2 if cjk_width is equal to |
| 5267 | * width for CJK fonts. |
| 5268 | * |
| 5269 | * For related bugs, see: |
| 5270 | * http://bugzilla.gnome.org/show_bug.cgi?id=106618 |
| 5271 | * http://bugzilla.gnome.org/show_bug.cgi?id=106624 |
| 5272 | * |
| 5273 | * With this, for all four of the following cases, Vim works fine: |
| 5274 | * guifont=CJK_fixed_width_font |
| 5275 | * guifont=Non_CJK_fixed_font |
| 5276 | * guifont=Non_CJK_fixed_font,CJK_Fixed_font |
| 5277 | * guifont=Non_CJK_fixed_font guifontwide=CJK_fixed_font |
| 5278 | */ |
| 5279 | if (is_cjk_font(gui.norm_font)) |
| 5280 | { |
| 5281 | int cjk_width; |
| 5282 | |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 5283 | // Measure the text extent of U+4E00 and U+4E8C |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5284 | pango_layout_set_text(layout, "\344\270\200\344\272\214", -1); |
| 5285 | pango_layout_get_size(layout, &cjk_width, NULL); |
| 5286 | |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 5287 | if (width == cjk_width) // Xft not patched |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5288 | width /= 2; |
| 5289 | } |
| 5290 | g_object_unref(layout); |
| 5291 | |
| 5292 | gui.char_width = (width / 2 + PANGO_SCALE - 1) / PANGO_SCALE; |
| 5293 | |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 5294 | // A zero width may cause a crash. Happens for semi-invalid fontsets. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5295 | if (gui.char_width <= 0) |
| 5296 | gui.char_width = 8; |
| 5297 | |
Bram Moolenaar | 231334e | 2005-07-25 20:46:57 +0000 | [diff] [blame] | 5298 | gui_mch_adjust_charheight(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5299 | |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 5300 | // Set the fontname, which will be used for information purposes |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5301 | hl_set_font_name(font_name); |
| 5302 | |
| 5303 | get_styled_font_variants(); |
| 5304 | ascii_glyph_table_init(); |
| 5305 | |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 5306 | // Avoid unnecessary overhead if 'guifontwide' is equal to 'guifont'. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5307 | if (gui.wide_font != NULL |
| 5308 | && pango_font_description_equal(gui.norm_font, gui.wide_font)) |
| 5309 | { |
| 5310 | pango_font_description_free(gui.wide_font); |
| 5311 | gui.wide_font = NULL; |
| 5312 | } |
| 5313 | |
Bram Moolenaar | e161c79 | 2009-11-03 17:13:59 +0000 | [diff] [blame] | 5314 | if (gui_mch_maximized()) |
| 5315 | { |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 5316 | // Update lines and columns in accordance with the new font, keep the |
| 5317 | // window maximized. |
Bram Moolenaar | 12bc1b5 | 2011-08-10 17:44:45 +0200 | [diff] [blame] | 5318 | gui_mch_newfont(); |
Bram Moolenaar | e161c79 | 2009-11-03 17:13:59 +0000 | [diff] [blame] | 5319 | } |
| 5320 | else |
Bram Moolenaar | e161c79 | 2009-11-03 17:13:59 +0000 | [diff] [blame] | 5321 | { |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 5322 | // Preserve the logical dimensions of the screen. |
Bram Moolenaar | e161c79 | 2009-11-03 17:13:59 +0000 | [diff] [blame] | 5323 | update_window_manager_hints(0, 0); |
| 5324 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5325 | |
| 5326 | return OK; |
| 5327 | } |
| 5328 | |
| 5329 | /* |
| 5330 | * Get a reference to the font "name". |
| 5331 | * Return zero for failure. |
| 5332 | */ |
| 5333 | GuiFont |
| 5334 | gui_mch_get_font(char_u *name, int report_error) |
| 5335 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5336 | PangoFontDescription *font; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5337 | |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 5338 | // can't do this when GUI is not running |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5339 | if (!gui.in_use || name == NULL) |
| 5340 | return NULL; |
| 5341 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5342 | if (output_conv.vc_type != CONV_NONE) |
| 5343 | { |
| 5344 | char_u *buf; |
| 5345 | |
| 5346 | buf = string_convert(&output_conv, name, NULL); |
| 5347 | if (buf != NULL) |
| 5348 | { |
| 5349 | font = pango_font_description_from_string((const char *)buf); |
| 5350 | vim_free(buf); |
| 5351 | } |
| 5352 | else |
| 5353 | font = NULL; |
| 5354 | } |
| 5355 | else |
| 5356 | font = pango_font_description_from_string((const char *)name); |
| 5357 | |
| 5358 | if (font != NULL) |
| 5359 | { |
| 5360 | PangoFont *real_font; |
| 5361 | |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 5362 | // pango_context_load_font() bails out if no font size is set |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5363 | if (pango_font_description_get_size(font) <= 0) |
| 5364 | pango_font_description_set_size(font, 10 * PANGO_SCALE); |
| 5365 | |
| 5366 | real_font = pango_context_load_font(gui.text_context, font); |
| 5367 | |
| 5368 | if (real_font == NULL) |
| 5369 | { |
| 5370 | pango_font_description_free(font); |
| 5371 | font = NULL; |
| 5372 | } |
| 5373 | else |
| 5374 | g_object_unref(real_font); |
| 5375 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5376 | |
| 5377 | if (font == NULL) |
| 5378 | { |
| 5379 | if (report_error) |
Bram Moolenaar | 436b5ad | 2021-12-31 22:49:24 +0000 | [diff] [blame] | 5380 | semsg(_((char *)e_unknown_font_str), name); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5381 | return NULL; |
| 5382 | } |
| 5383 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5384 | return font; |
| 5385 | } |
| 5386 | |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 5387 | #if defined(FEAT_EVAL) || defined(PROTO) |
Bram Moolenaar | 46c9c73 | 2004-12-12 11:37:09 +0000 | [diff] [blame] | 5388 | /* |
| 5389 | * Return the name of font "font" in allocated memory. |
| 5390 | */ |
Bram Moolenaar | 46c9c73 | 2004-12-12 11:37:09 +0000 | [diff] [blame] | 5391 | char_u * |
Bram Moolenaar | b85cb21 | 2009-05-17 14:24:23 +0000 | [diff] [blame] | 5392 | gui_mch_get_fontname(GuiFont font, char_u *name UNUSED) |
Bram Moolenaar | 46c9c73 | 2004-12-12 11:37:09 +0000 | [diff] [blame] | 5393 | { |
Bram Moolenaar | 46c9c73 | 2004-12-12 11:37:09 +0000 | [diff] [blame] | 5394 | if (font != NOFONT) |
| 5395 | { |
Bram Moolenaar | 89d4032 | 2006-08-29 15:30:07 +0000 | [diff] [blame] | 5396 | char *pangoname = pango_font_description_to_string(font); |
Bram Moolenaar | 46c9c73 | 2004-12-12 11:37:09 +0000 | [diff] [blame] | 5397 | |
Bram Moolenaar | 89d4032 | 2006-08-29 15:30:07 +0000 | [diff] [blame] | 5398 | if (pangoname != NULL) |
Bram Moolenaar | 46c9c73 | 2004-12-12 11:37:09 +0000 | [diff] [blame] | 5399 | { |
Bram Moolenaar | 89d4032 | 2006-08-29 15:30:07 +0000 | [diff] [blame] | 5400 | char_u *s = vim_strsave((char_u *)pangoname); |
Bram Moolenaar | 46c9c73 | 2004-12-12 11:37:09 +0000 | [diff] [blame] | 5401 | |
Bram Moolenaar | 89d4032 | 2006-08-29 15:30:07 +0000 | [diff] [blame] | 5402 | g_free(pangoname); |
Bram Moolenaar | 46c9c73 | 2004-12-12 11:37:09 +0000 | [diff] [blame] | 5403 | return s; |
| 5404 | } |
| 5405 | } |
Bram Moolenaar | 46c9c73 | 2004-12-12 11:37:09 +0000 | [diff] [blame] | 5406 | return NULL; |
| 5407 | } |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 5408 | #endif |
Bram Moolenaar | 46c9c73 | 2004-12-12 11:37:09 +0000 | [diff] [blame] | 5409 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5410 | /* |
| 5411 | * If a font is not going to be used, free its structure. |
| 5412 | */ |
| 5413 | void |
| 5414 | gui_mch_free_font(GuiFont font) |
| 5415 | { |
| 5416 | if (font != NOFONT) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5417 | pango_font_description_free(font); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5418 | } |
| 5419 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5420 | /* |
Yee Cheng Chin | 290b887 | 2023-10-05 20:54:21 +0200 | [diff] [blame] | 5421 | * Cmdline expansion for setting 'guifont' / 'guifontwide'. Will enumerate |
| 5422 | * through all fonts for completion. When setting 'guifont' it will only show |
| 5423 | * monospace fonts as it's unlikely other fonts would be useful. |
| 5424 | */ |
| 5425 | void |
| 5426 | gui_mch_expand_font(optexpand_T *args, void *param, int (*add_match)(char_u *val)) |
| 5427 | { |
| 5428 | PangoFontFamily **font_families = NULL; |
| 5429 | int n_families = 0; |
| 5430 | int wide = *(int *)param; |
| 5431 | |
| 5432 | if (args->oe_include_orig_val && *args->oe_opt_value == NUL && !wide) |
| 5433 | { |
| 5434 | // If guifont is empty, and we want to fill in the orig value, suggest |
| 5435 | // the default so the user can modify it. |
| 5436 | if (add_match((char_u *)DEFAULT_FONT) != OK) |
| 5437 | return; |
| 5438 | } |
| 5439 | |
| 5440 | pango_context_list_families( |
| 5441 | gui.text_context, |
| 5442 | &font_families, |
| 5443 | &n_families); |
| 5444 | |
| 5445 | for (int i = 0; i < n_families; i++) |
| 5446 | { |
| 5447 | if (!wide && !pango_font_family_is_monospace(font_families[i])) |
| 5448 | continue; |
| 5449 | |
| 5450 | const char* fam_name = pango_font_family_get_name(font_families[i]); |
| 5451 | if (input_conv.vc_type != CONV_NONE) |
| 5452 | { |
| 5453 | char_u *buf = string_convert(&input_conv, (char_u*)fam_name, NULL); |
| 5454 | if (buf != NULL) |
| 5455 | { |
| 5456 | if (add_match(buf) != OK) |
| 5457 | { |
| 5458 | vim_free(buf); |
| 5459 | break; |
| 5460 | } |
| 5461 | vim_free(buf); |
| 5462 | } |
| 5463 | else |
| 5464 | break; |
| 5465 | } |
| 5466 | else |
| 5467 | { |
| 5468 | if (add_match((char_u *)fam_name) != OK) |
| 5469 | break; |
| 5470 | } |
| 5471 | } |
| 5472 | |
| 5473 | g_free(font_families); |
| 5474 | } |
| 5475 | |
| 5476 | /* |
Bram Moolenaar | 36edf06 | 2016-07-21 22:10:12 +0200 | [diff] [blame] | 5477 | * Return the Pixel value (color) for the given color name. |
| 5478 | * |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5479 | * Return INVALCOLOR for error. |
| 5480 | */ |
| 5481 | guicolor_T |
| 5482 | gui_mch_get_color(char_u *name) |
| 5483 | { |
Bram Moolenaar | 2e32495 | 2018-04-14 14:37:07 +0200 | [diff] [blame] | 5484 | guicolor_T color = INVALCOLOR; |
| 5485 | |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 5486 | if (!gui.in_use) // can't do this when GUI not running |
Bram Moolenaar | 2e32495 | 2018-04-14 14:37:07 +0200 | [diff] [blame] | 5487 | return color; |
| 5488 | |
| 5489 | if (name != NULL) |
| 5490 | color = gui_get_color_cmn(name); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5491 | |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 5492 | #if GTK_CHECK_VERSION(3,0,0) |
Bram Moolenaar | 2e32495 | 2018-04-14 14:37:07 +0200 | [diff] [blame] | 5493 | return color; |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 5494 | #else |
Bram Moolenaar | 36edf06 | 2016-07-21 22:10:12 +0200 | [diff] [blame] | 5495 | if (color == INVALCOLOR) |
| 5496 | return INVALCOLOR; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5497 | |
Bram Moolenaar | 26af85d | 2017-07-23 16:45:10 +0200 | [diff] [blame] | 5498 | return gui_mch_get_rgb_color( |
| 5499 | (color & 0xff0000) >> 16, |
| 5500 | (color & 0xff00) >> 8, |
| 5501 | color & 0xff); |
| 5502 | #endif |
| 5503 | } |
| 5504 | |
| 5505 | /* |
| 5506 | * Return the Pixel value (color) for the given RGB values. |
| 5507 | * Return INVALCOLOR for error. |
| 5508 | */ |
| 5509 | guicolor_T |
| 5510 | gui_mch_get_rgb_color(int r, int g, int b) |
| 5511 | { |
| 5512 | #if GTK_CHECK_VERSION(3,0,0) |
| 5513 | return gui_get_rgb_color_cmn(r, g, b); |
| 5514 | #else |
| 5515 | GdkColor gcolor; |
| 5516 | int ret; |
| 5517 | |
| 5518 | gcolor.red = (guint16)(r / 255.0 * 65535 + 0.5); |
| 5519 | gcolor.green = (guint16)(g / 255.0 * 65535 + 0.5); |
| 5520 | gcolor.blue = (guint16)(b / 255.0 * 65535 + 0.5); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5521 | |
Bram Moolenaar | 36edf06 | 2016-07-21 22:10:12 +0200 | [diff] [blame] | 5522 | ret = gdk_colormap_alloc_color(gtk_widget_get_colormap(gui.drawarea), |
| 5523 | &gcolor, FALSE, TRUE); |
| 5524 | |
| 5525 | return ret != 0 ? (guicolor_T)gcolor.pixel : INVALCOLOR; |
| 5526 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5527 | } |
| 5528 | |
| 5529 | /* |
| 5530 | * Set the current text foreground color. |
| 5531 | */ |
| 5532 | void |
| 5533 | gui_mch_set_fg_color(guicolor_T color) |
| 5534 | { |
Bram Moolenaar | 36edf06 | 2016-07-21 22:10:12 +0200 | [diff] [blame] | 5535 | #if GTK_CHECK_VERSION(3,0,0) |
| 5536 | *gui.fgcolor = color_to_rgba(color); |
| 5537 | #else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5538 | gui.fgcolor->pixel = (unsigned long)color; |
Bram Moolenaar | 36edf06 | 2016-07-21 22:10:12 +0200 | [diff] [blame] | 5539 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5540 | } |
| 5541 | |
| 5542 | /* |
| 5543 | * Set the current text background color. |
| 5544 | */ |
| 5545 | void |
| 5546 | gui_mch_set_bg_color(guicolor_T color) |
| 5547 | { |
Bram Moolenaar | 36edf06 | 2016-07-21 22:10:12 +0200 | [diff] [blame] | 5548 | #if GTK_CHECK_VERSION(3,0,0) |
| 5549 | *gui.bgcolor = color_to_rgba(color); |
| 5550 | #else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5551 | gui.bgcolor->pixel = (unsigned long)color; |
Bram Moolenaar | 36edf06 | 2016-07-21 22:10:12 +0200 | [diff] [blame] | 5552 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5553 | } |
| 5554 | |
Bram Moolenaar | f36d369 | 2005-03-15 22:48:14 +0000 | [diff] [blame] | 5555 | /* |
| 5556 | * Set the current text special color. |
| 5557 | */ |
| 5558 | void |
| 5559 | gui_mch_set_sp_color(guicolor_T color) |
| 5560 | { |
Bram Moolenaar | 36edf06 | 2016-07-21 22:10:12 +0200 | [diff] [blame] | 5561 | #if GTK_CHECK_VERSION(3,0,0) |
| 5562 | *gui.spcolor = color_to_rgba(color); |
| 5563 | #else |
Bram Moolenaar | f36d369 | 2005-03-15 22:48:14 +0000 | [diff] [blame] | 5564 | gui.spcolor->pixel = (unsigned long)color; |
Bram Moolenaar | 36edf06 | 2016-07-21 22:10:12 +0200 | [diff] [blame] | 5565 | #endif |
Bram Moolenaar | f36d369 | 2005-03-15 22:48:14 +0000 | [diff] [blame] | 5566 | } |
| 5567 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5568 | /* |
| 5569 | * Function-like convenience macro for the sake of efficiency. |
| 5570 | */ |
| 5571 | #define INSERT_PANGO_ATTR(Attribute, AttrList, Start, End) \ |
| 5572 | G_STMT_START{ \ |
| 5573 | PangoAttribute *tmp_attr_; \ |
| 5574 | tmp_attr_ = (Attribute); \ |
| 5575 | tmp_attr_->start_index = (Start); \ |
| 5576 | tmp_attr_->end_index = (End); \ |
| 5577 | pango_attr_list_insert((AttrList), tmp_attr_); \ |
| 5578 | }G_STMT_END |
| 5579 | |
| 5580 | static void |
| 5581 | apply_wide_font_attr(char_u *s, int len, PangoAttrList *attr_list) |
| 5582 | { |
| 5583 | char_u *start = NULL; |
| 5584 | char_u *p; |
| 5585 | int uc; |
| 5586 | |
| 5587 | for (p = s; p < s + len; p += utf_byte2len(*p)) |
| 5588 | { |
| 5589 | uc = utf_ptr2char(p); |
| 5590 | |
| 5591 | if (start == NULL) |
| 5592 | { |
| 5593 | if (uc >= 0x80 && utf_char2cells(uc) == 2) |
| 5594 | start = p; |
| 5595 | } |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 5596 | else if (uc < 0x80 // optimization shortcut |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5597 | || (utf_char2cells(uc) != 2 && !utf_iscomposing(uc))) |
| 5598 | { |
| 5599 | INSERT_PANGO_ATTR(pango_attr_font_desc_new(gui.wide_font), |
| 5600 | attr_list, start - s, p - s); |
| 5601 | start = NULL; |
| 5602 | } |
| 5603 | } |
| 5604 | |
| 5605 | if (start != NULL) |
| 5606 | INSERT_PANGO_ATTR(pango_attr_font_desc_new(gui.wide_font), |
| 5607 | attr_list, start - s, len); |
| 5608 | } |
| 5609 | |
| 5610 | static int |
| 5611 | count_cluster_cells(char_u *s, PangoItem *item, |
| 5612 | PangoGlyphString* glyphs, int i, |
| 5613 | int *cluster_width, |
| 5614 | int *last_glyph_rbearing) |
| 5615 | { |
| 5616 | char_u *p; |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 5617 | int next; // glyph start index of next cluster |
| 5618 | int start, end; // string segment of current cluster |
| 5619 | int width; // real cluster width in Pango units |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5620 | int uc; |
| 5621 | int cellcount = 0; |
| 5622 | |
| 5623 | width = glyphs->glyphs[i].geometry.width; |
| 5624 | |
| 5625 | for (next = i + 1; next < glyphs->num_glyphs; ++next) |
| 5626 | { |
| 5627 | if (glyphs->glyphs[next].attr.is_cluster_start) |
| 5628 | break; |
| 5629 | else if (glyphs->glyphs[next].geometry.width > width) |
| 5630 | width = glyphs->glyphs[next].geometry.width; |
| 5631 | } |
| 5632 | |
| 5633 | start = item->offset + glyphs->log_clusters[i]; |
| 5634 | end = item->offset + ((next < glyphs->num_glyphs) ? |
| 5635 | glyphs->log_clusters[next] : item->length); |
| 5636 | |
| 5637 | for (p = s + start; p < s + end; p += utf_byte2len(*p)) |
| 5638 | { |
| 5639 | uc = utf_ptr2char(p); |
| 5640 | if (uc < 0x80) |
| 5641 | ++cellcount; |
| 5642 | else if (!utf_iscomposing(uc)) |
| 5643 | cellcount += utf_char2cells(uc); |
| 5644 | } |
| 5645 | |
| 5646 | if (last_glyph_rbearing != NULL |
| 5647 | && cellcount > 0 && next == glyphs->num_glyphs) |
| 5648 | { |
| 5649 | PangoRectangle ink_rect; |
| 5650 | /* |
| 5651 | * If a certain combining mark had to be taken from a non-monospace |
| 5652 | * font, we have to compensate manually by adapting x_offset according |
| 5653 | * to the ink extents of the previous glyph. |
| 5654 | */ |
| 5655 | pango_font_get_glyph_extents(item->analysis.font, |
| 5656 | glyphs->glyphs[i].glyph, |
| 5657 | &ink_rect, NULL); |
| 5658 | |
| 5659 | if (PANGO_RBEARING(ink_rect) > 0) |
| 5660 | *last_glyph_rbearing = PANGO_RBEARING(ink_rect); |
| 5661 | } |
| 5662 | |
| 5663 | if (cellcount > 0) |
| 5664 | *cluster_width = width; |
| 5665 | |
| 5666 | return cellcount; |
| 5667 | } |
| 5668 | |
| 5669 | /* |
| 5670 | * If there are only combining characters in the cluster, we cannot just |
| 5671 | * change the width of the previous glyph since there is none. Therefore |
| 5672 | * some guesswork is needed. |
| 5673 | * |
| 5674 | * If ink_rect.x is negative Pango apparently has taken care of the composing |
| 5675 | * by itself. Actually setting x_offset = 0 should be sufficient then, but due |
| 5676 | * to problems with composing from different fonts we still need to fine-tune |
Bram Moolenaar | 6440447 | 2010-06-26 06:24:45 +0200 | [diff] [blame] | 5677 | * x_offset to avoid ugliness. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5678 | * |
| 5679 | * If ink_rect.x is not negative, force overstriking by pointing x_offset to |
| 5680 | * the position of the previous glyph. Apparently this happens only with old |
| 5681 | * X fonts which don't provide the special combining information needed by |
| 5682 | * Pango. |
| 5683 | */ |
| 5684 | static void |
| 5685 | setup_zero_width_cluster(PangoItem *item, PangoGlyphInfo *glyph, |
| 5686 | int last_cellcount, int last_cluster_width, |
| 5687 | int last_glyph_rbearing) |
| 5688 | { |
| 5689 | PangoRectangle ink_rect; |
| 5690 | PangoRectangle logical_rect; |
| 5691 | int width; |
| 5692 | |
| 5693 | width = last_cellcount * gui.char_width * PANGO_SCALE; |
| 5694 | glyph->geometry.x_offset = -width + MAX(0, width - last_cluster_width) / 2; |
| 5695 | glyph->geometry.width = 0; |
| 5696 | |
| 5697 | pango_font_get_glyph_extents(item->analysis.font, |
| 5698 | glyph->glyph, |
| 5699 | &ink_rect, &logical_rect); |
| 5700 | if (ink_rect.x < 0) |
| 5701 | { |
| 5702 | glyph->geometry.x_offset += last_glyph_rbearing; |
| 5703 | glyph->geometry.y_offset = logical_rect.height |
| 5704 | - (gui.char_height - p_linespace) * PANGO_SCALE; |
| 5705 | } |
Bram Moolenaar | 706e84b | 2010-08-07 15:46:45 +0200 | [diff] [blame] | 5706 | else |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 5707 | // If the accent width is smaller than the cluster width, position it |
| 5708 | // in the middle. |
Bram Moolenaar | 706e84b | 2010-08-07 15:46:45 +0200 | [diff] [blame] | 5709 | glyph->geometry.x_offset = -width + MAX(0, width - ink_rect.width) / 2; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5710 | } |
| 5711 | |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 5712 | #if GTK_CHECK_VERSION(3,0,0) |
| 5713 | static void |
| 5714 | draw_glyph_string(int row, int col, int num_cells, int flags, |
| 5715 | PangoFont *font, PangoGlyphString *glyphs, |
| 5716 | cairo_t *cr) |
| 5717 | #else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5718 | static void |
| 5719 | draw_glyph_string(int row, int col, int num_cells, int flags, |
| 5720 | PangoFont *font, PangoGlyphString *glyphs) |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 5721 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5722 | { |
| 5723 | if (!(flags & DRAW_TRANSP)) |
| 5724 | { |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 5725 | #if GTK_CHECK_VERSION(3,0,0) |
Bram Moolenaar | 36edf06 | 2016-07-21 22:10:12 +0200 | [diff] [blame] | 5726 | cairo_set_source_rgba(cr, |
| 5727 | gui.bgcolor->red, gui.bgcolor->green, gui.bgcolor->blue, |
| 5728 | gui.bgcolor->alpha); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 5729 | cairo_rectangle(cr, |
| 5730 | FILL_X(col), FILL_Y(row), |
| 5731 | num_cells * gui.char_width, gui.char_height); |
| 5732 | cairo_fill(cr); |
| 5733 | #else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5734 | gdk_gc_set_foreground(gui.text_gc, gui.bgcolor); |
| 5735 | |
| 5736 | gdk_draw_rectangle(gui.drawarea->window, |
| 5737 | gui.text_gc, |
| 5738 | TRUE, |
| 5739 | FILL_X(col), |
| 5740 | FILL_Y(row), |
| 5741 | num_cells * gui.char_width, |
| 5742 | gui.char_height); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 5743 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5744 | } |
| 5745 | |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 5746 | #if GTK_CHECK_VERSION(3,0,0) |
Bram Moolenaar | 36edf06 | 2016-07-21 22:10:12 +0200 | [diff] [blame] | 5747 | cairo_set_source_rgba(cr, |
| 5748 | gui.fgcolor->red, gui.fgcolor->green, gui.fgcolor->blue, |
| 5749 | gui.fgcolor->alpha); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 5750 | cairo_move_to(cr, TEXT_X(col), TEXT_Y(row)); |
| 5751 | pango_cairo_show_glyph_string(cr, font, glyphs); |
| 5752 | #else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5753 | gdk_gc_set_foreground(gui.text_gc, gui.fgcolor); |
| 5754 | |
| 5755 | gdk_draw_glyphs(gui.drawarea->window, |
| 5756 | gui.text_gc, |
| 5757 | font, |
| 5758 | TEXT_X(col), |
| 5759 | TEXT_Y(row), |
| 5760 | glyphs); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 5761 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5762 | |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 5763 | // redraw the contents with an offset of 1 to emulate bold |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5764 | if ((flags & DRAW_BOLD) && !gui.font_can_bold) |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 5765 | #if GTK_CHECK_VERSION(3,0,0) |
| 5766 | { |
Bram Moolenaar | 36edf06 | 2016-07-21 22:10:12 +0200 | [diff] [blame] | 5767 | cairo_set_source_rgba(cr, |
| 5768 | gui.fgcolor->red, gui.fgcolor->green, gui.fgcolor->blue, |
| 5769 | gui.fgcolor->alpha); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 5770 | cairo_move_to(cr, TEXT_X(col) + 1, TEXT_Y(row)); |
| 5771 | pango_cairo_show_glyph_string(cr, font, glyphs); |
| 5772 | } |
| 5773 | #else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5774 | gdk_draw_glyphs(gui.drawarea->window, |
| 5775 | gui.text_gc, |
| 5776 | font, |
| 5777 | TEXT_X(col) + 1, |
| 5778 | TEXT_Y(row), |
| 5779 | glyphs); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 5780 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5781 | } |
| 5782 | |
Bram Moolenaar | f36d369 | 2005-03-15 22:48:14 +0000 | [diff] [blame] | 5783 | /* |
| 5784 | * Draw underline and undercurl at the bottom of the character cell. |
| 5785 | */ |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 5786 | #if GTK_CHECK_VERSION(3,0,0) |
| 5787 | static void |
| 5788 | draw_under(int flags, int row, int col, int cells, cairo_t *cr) |
| 5789 | #else |
Bram Moolenaar | f36d369 | 2005-03-15 22:48:14 +0000 | [diff] [blame] | 5790 | static void |
| 5791 | draw_under(int flags, int row, int col, int cells) |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 5792 | #endif |
Bram Moolenaar | f36d369 | 2005-03-15 22:48:14 +0000 | [diff] [blame] | 5793 | { |
| 5794 | int i; |
| 5795 | int offset; |
Bram Moolenaar | b85cb21 | 2009-05-17 14:24:23 +0000 | [diff] [blame] | 5796 | static const int val[8] = {1, 0, 0, 0, 1, 2, 2, 2 }; |
Bram Moolenaar | f36d369 | 2005-03-15 22:48:14 +0000 | [diff] [blame] | 5797 | int y = FILL_Y(row + 1) - 1; |
| 5798 | |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 5799 | // Undercurl: draw curl at the bottom of the character cell. |
Bram Moolenaar | f36d369 | 2005-03-15 22:48:14 +0000 | [diff] [blame] | 5800 | if (flags & DRAW_UNDERC) |
| 5801 | { |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 5802 | #if GTK_CHECK_VERSION(3,0,0) |
| 5803 | cairo_set_line_width(cr, 1.0); |
| 5804 | cairo_set_line_cap(cr, CAIRO_LINE_CAP_BUTT); |
Bram Moolenaar | 36edf06 | 2016-07-21 22:10:12 +0200 | [diff] [blame] | 5805 | cairo_set_source_rgba(cr, |
| 5806 | gui.spcolor->red, gui.spcolor->green, gui.spcolor->blue, |
| 5807 | gui.spcolor->alpha); |
Yamagi | 9cd9385 | 2021-11-20 20:42:29 +0000 | [diff] [blame] | 5808 | cairo_move_to(cr, FILL_X(col) + 1, y - 2 + 0.5); |
| 5809 | for (i = FILL_X(col) + 1; i < FILL_X(col + cells); ++i) |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 5810 | { |
| 5811 | offset = val[i % 8]; |
| 5812 | cairo_line_to(cr, i, y - offset + 0.5); |
| 5813 | } |
| 5814 | cairo_stroke(cr); |
| 5815 | #else |
Bram Moolenaar | f36d369 | 2005-03-15 22:48:14 +0000 | [diff] [blame] | 5816 | gdk_gc_set_foreground(gui.text_gc, gui.spcolor); |
| 5817 | for (i = FILL_X(col); i < FILL_X(col + cells); ++i) |
| 5818 | { |
| 5819 | offset = val[i % 8]; |
| 5820 | gdk_draw_point(gui.drawarea->window, gui.text_gc, i, y - offset); |
| 5821 | } |
| 5822 | gdk_gc_set_foreground(gui.text_gc, gui.fgcolor); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 5823 | #endif |
Bram Moolenaar | f36d369 | 2005-03-15 22:48:14 +0000 | [diff] [blame] | 5824 | } |
| 5825 | |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 5826 | // Draw a strikethrough line |
Bram Moolenaar | cf4b00c | 2017-09-02 18:33:56 +0200 | [diff] [blame] | 5827 | if (flags & DRAW_STRIKE) |
| 5828 | { |
| 5829 | #if GTK_CHECK_VERSION(3,0,0) |
| 5830 | cairo_set_line_width(cr, 1.0); |
| 5831 | cairo_set_line_cap(cr, CAIRO_LINE_CAP_BUTT); |
| 5832 | cairo_set_source_rgba(cr, |
| 5833 | gui.spcolor->red, gui.spcolor->green, gui.spcolor->blue, |
| 5834 | gui.spcolor->alpha); |
| 5835 | cairo_move_to(cr, FILL_X(col), y + 1 - gui.char_height/2 + 0.5); |
| 5836 | cairo_line_to(cr, FILL_X(col + cells), y + 1 - gui.char_height/2 + 0.5); |
| 5837 | cairo_stroke(cr); |
| 5838 | #else |
| 5839 | gdk_gc_set_foreground(gui.text_gc, gui.spcolor); |
| 5840 | gdk_draw_line(gui.drawarea->window, gui.text_gc, |
| 5841 | FILL_X(col), y + 1 - gui.char_height/2, |
| 5842 | FILL_X(col + cells), y + 1 - gui.char_height/2); |
| 5843 | gdk_gc_set_foreground(gui.text_gc, gui.fgcolor); |
| 5844 | #endif |
| 5845 | } |
| 5846 | |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 5847 | // Underline: draw a line at the bottom of the character cell. |
Bram Moolenaar | f36d369 | 2005-03-15 22:48:14 +0000 | [diff] [blame] | 5848 | if (flags & DRAW_UNDERL) |
| 5849 | { |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 5850 | // When p_linespace is 0, overwrite the bottom row of pixels. |
| 5851 | // Otherwise put the line just below the character. |
Bram Moolenaar | f36d369 | 2005-03-15 22:48:14 +0000 | [diff] [blame] | 5852 | if (p_linespace > 1) |
| 5853 | y -= p_linespace - 1; |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 5854 | #if GTK_CHECK_VERSION(3,0,0) |
Bram Moolenaar | cf4b00c | 2017-09-02 18:33:56 +0200 | [diff] [blame] | 5855 | cairo_set_line_width(cr, 1.0); |
| 5856 | cairo_set_line_cap(cr, CAIRO_LINE_CAP_BUTT); |
| 5857 | cairo_set_source_rgba(cr, |
| 5858 | gui.fgcolor->red, gui.fgcolor->green, gui.fgcolor->blue, |
| 5859 | gui.fgcolor->alpha); |
| 5860 | cairo_move_to(cr, FILL_X(col), y + 0.5); |
| 5861 | cairo_line_to(cr, FILL_X(col + cells), y + 0.5); |
| 5862 | cairo_stroke(cr); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 5863 | #else |
Bram Moolenaar | f36d369 | 2005-03-15 22:48:14 +0000 | [diff] [blame] | 5864 | gdk_draw_line(gui.drawarea->window, gui.text_gc, |
| 5865 | FILL_X(col), y, |
| 5866 | FILL_X(col + cells) - 1, y); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 5867 | #endif |
Bram Moolenaar | f36d369 | 2005-03-15 22:48:14 +0000 | [diff] [blame] | 5868 | } |
| 5869 | } |
| 5870 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5871 | int |
| 5872 | gui_gtk2_draw_string(int row, int col, char_u *s, int len, int flags) |
| 5873 | { |
Dusan Popovic | 4eeedc0 | 2021-10-16 20:52:05 +0100 | [diff] [blame] | 5874 | char_u *conv_buf = NULL; // result of UTF-8 conversion |
| 5875 | char_u *new_conv_buf; |
| 5876 | int convlen; |
| 5877 | char_u *sp, *bp; |
| 5878 | int plen; |
| 5879 | int len_sum; // return value needs to add up since we are |
| 5880 | // printing substrings |
| 5881 | int byte_sum; // byte position in string |
| 5882 | char_u *cs; // current *s pointer |
| 5883 | int needs_pango; // look ahead, 0=ascii 1=unicode/ligatures |
Bram Moolenaar | 9d4b8ca | 2021-10-17 11:33:47 +0100 | [diff] [blame] | 5884 | int should_need_pango = FALSE; |
Dusan Popovic | 4eeedc0 | 2021-10-16 20:52:05 +0100 | [diff] [blame] | 5885 | int slen; |
| 5886 | int is_ligature; |
Dusan Popovic | 4eeedc0 | 2021-10-16 20:52:05 +0100 | [diff] [blame] | 5887 | int is_utf8; |
| 5888 | char_u backup_ch; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5889 | |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 5890 | if (gui.text_context == NULL || gtk_widget_get_window(gui.drawarea) == NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5891 | return len; |
| 5892 | |
| 5893 | if (output_conv.vc_type != CONV_NONE) |
| 5894 | { |
| 5895 | /* |
| 5896 | * Convert characters from 'encoding' to 'termencoding', which is set |
| 5897 | * to UTF-8 by gui_mch_init(). did_set_string_option() in option.c |
| 5898 | * prohibits changing this to something else than UTF-8 if the GUI is |
| 5899 | * in use. |
| 5900 | */ |
| 5901 | convlen = len; |
| 5902 | conv_buf = string_convert(&output_conv, s, &convlen); |
| 5903 | g_return_val_if_fail(conv_buf != NULL, len); |
| 5904 | |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 5905 | // Correct for differences in char width: some chars are |
| 5906 | // double-wide in 'encoding' but single-wide in utf-8. Add a space to |
| 5907 | // compensate for that. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5908 | for (sp = s, bp = conv_buf; sp < s + len && bp < conv_buf + convlen; ) |
| 5909 | { |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 5910 | plen = utf_ptr2len(bp); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5911 | if ((*mb_ptr2cells)(sp) == 2 && utf_ptr2cells(bp) == 1) |
| 5912 | { |
| 5913 | new_conv_buf = alloc(convlen + 2); |
| 5914 | if (new_conv_buf == NULL) |
| 5915 | return len; |
| 5916 | plen += bp - conv_buf; |
| 5917 | mch_memmove(new_conv_buf, conv_buf, plen); |
| 5918 | new_conv_buf[plen] = ' '; |
| 5919 | mch_memmove(new_conv_buf + plen + 1, conv_buf + plen, |
| 5920 | convlen - plen + 1); |
| 5921 | vim_free(conv_buf); |
| 5922 | conv_buf = new_conv_buf; |
| 5923 | ++convlen; |
| 5924 | bp = conv_buf + plen; |
| 5925 | plen = 1; |
| 5926 | } |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 5927 | sp += (*mb_ptr2len)(sp); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5928 | bp += plen; |
| 5929 | } |
| 5930 | s = conv_buf; |
| 5931 | len = convlen; |
| 5932 | } |
| 5933 | |
| 5934 | /* |
Dusan Popovic | 4eeedc0 | 2021-10-16 20:52:05 +0100 | [diff] [blame] | 5935 | * Ligature support and complex utf-8 char optimization: |
| 5936 | * String received to output to screen can print using pre-cached glyphs |
| 5937 | * (fast) or Pango (slow). Ligatures and multibype utf-8 must use Pango. |
| 5938 | * Since we receive mixed content string, split it into logical segments |
dundargoc | c57b5bc | 2022-11-02 13:30:51 +0000 | [diff] [blame] | 5939 | * that are guaranteed to go through glyphs as much as possible. Since |
Dusan Popovic | 4eeedc0 | 2021-10-16 20:52:05 +0100 | [diff] [blame] | 5940 | * single ligature char prints as ascii, print it that way. |
| 5941 | */ |
| 5942 | len_sum = 0; // return value needs to add up since we are printing |
| 5943 | // substrings |
| 5944 | byte_sum = 0; |
| 5945 | cs = s; |
Dusan Popovic | 3c19b50 | 2021-11-20 22:03:30 +0000 | [diff] [blame] | 5946 | // First char decides starting needs_pango mode, 0=ascii 1=utf8/ligatures. |
| 5947 | // Even if it is ligature char, two chars or more make ligature. |
dundargoc | c57b5bc | 2022-11-02 13:30:51 +0000 | [diff] [blame] | 5948 | // Ascii followed by utf8 is also going through pango. |
Dusan Popovic | 3c19b50 | 2021-11-20 22:03:30 +0000 | [diff] [blame] | 5949 | is_utf8 = (*cs & 0x80); |
| 5950 | is_ligature = gui.ligatures_map[*cs] && (len > 1); |
| 5951 | if (is_ligature) |
| 5952 | is_ligature = gui.ligatures_map[*(cs + 1)]; |
| 5953 | if (!is_utf8 && len > 1) |
| 5954 | is_utf8 = (*(cs + 1) & 0x80) != 0; |
| 5955 | needs_pango = is_utf8 || is_ligature; |
Dusan Popovic | 4eeedc0 | 2021-10-16 20:52:05 +0100 | [diff] [blame] | 5956 | |
| 5957 | // split string into ascii and non-ascii (ligatures + utf-8) substrings, |
| 5958 | // print glyphs or use Pango |
| 5959 | while (cs < s + len) |
| 5960 | { |
| 5961 | slen = 0; |
| 5962 | while (slen < (len - byte_sum)) |
| 5963 | { |
| 5964 | is_ligature = gui.ligatures_map[*(cs + slen)]; |
| 5965 | // look ahead, single ligature char between ascii is ascii |
| 5966 | if (is_ligature && !needs_pango) |
| 5967 | { |
| 5968 | if ((slen + 1) < (len - byte_sum)) |
Dusan Popovic | 3c19b50 | 2021-11-20 22:03:30 +0000 | [diff] [blame] | 5969 | is_ligature = gui.ligatures_map[*(cs + slen + 1)]; |
Dusan Popovic | 4eeedc0 | 2021-10-16 20:52:05 +0100 | [diff] [blame] | 5970 | else |
Dusan Popovic | 4eeedc0 | 2021-10-16 20:52:05 +0100 | [diff] [blame] | 5971 | is_ligature = 0; |
Dusan Popovic | 4eeedc0 | 2021-10-16 20:52:05 +0100 | [diff] [blame] | 5972 | } |
| 5973 | is_utf8 = *(cs + slen) & 0x80; |
Dusan Popovic | 3c19b50 | 2021-11-20 22:03:30 +0000 | [diff] [blame] | 5974 | // ascii followed by utf8 could be combining |
dundargoc | c57b5bc | 2022-11-02 13:30:51 +0000 | [diff] [blame] | 5975 | // if so send it through pango |
Dusan Popovic | 3c19b50 | 2021-11-20 22:03:30 +0000 | [diff] [blame] | 5976 | if ((!is_utf8) && ((slen + 1) < (len - byte_sum))) |
| 5977 | is_utf8 = (*(cs + slen + 1) & 0x80); |
Dusan Popovic | 4eeedc0 | 2021-10-16 20:52:05 +0100 | [diff] [blame] | 5978 | should_need_pango = (is_ligature || is_utf8); |
| 5979 | if (needs_pango != should_need_pango) // mode switch |
| 5980 | break; |
| 5981 | if (needs_pango) |
| 5982 | { |
| 5983 | if (is_ligature) |
| 5984 | { |
| 5985 | slen++; // ligature char by char |
| 5986 | } |
Bram Moolenaar | 2c23670 | 2021-11-21 11:15:49 +0000 | [diff] [blame] | 5987 | else // is_utf8 |
Dusan Popovic | 4eeedc0 | 2021-10-16 20:52:05 +0100 | [diff] [blame] | 5988 | { |
| 5989 | if ((*(cs + slen) & 0xC0) == 0x80) |
| 5990 | { |
| 5991 | // a continuation, find next 0xC0 != 0x80 but don't |
| 5992 | // include it |
| 5993 | while ((slen < (len - byte_sum)) |
| 5994 | && ((*(cs + slen) & 0xC0) == 0x80)) |
| 5995 | { |
| 5996 | slen++; |
| 5997 | } |
| 5998 | } |
| 5999 | else if ((*(cs + slen) & 0xE0) == 0xC0) |
| 6000 | { |
| 6001 | // + one byte utf8 |
| 6002 | slen++; |
| 6003 | } |
| 6004 | else if ((*(cs + slen) & 0xF0) == 0xE0) |
| 6005 | { |
| 6006 | // + two bytes utf8 |
| 6007 | slen += 2; |
| 6008 | } |
| 6009 | else if ((*(cs + slen) & 0xF8) == 0xF0) |
| 6010 | { |
| 6011 | // + three bytes utf8 |
| 6012 | slen += 3; |
| 6013 | } |
| 6014 | else |
| 6015 | { |
| 6016 | // this should not happen, try moving forward, Pango |
| 6017 | // will catch it |
| 6018 | slen++; |
| 6019 | } |
| 6020 | } |
| 6021 | } |
| 6022 | else |
| 6023 | { |
| 6024 | slen++; // ascii |
| 6025 | } |
| 6026 | } |
Bram Moolenaar | 9d4b8ca | 2021-10-17 11:33:47 +0100 | [diff] [blame] | 6027 | |
Bram Moolenaar | d68a004 | 2021-10-20 23:08:11 +0100 | [diff] [blame] | 6028 | if (slen < len) |
| 6029 | { |
| 6030 | // temporarily zero terminate substring, print, restore char, wrap |
| 6031 | backup_ch = *(cs + slen); |
| 6032 | *(cs + slen) = NUL; |
| 6033 | } |
Dusan Popovic | 4eeedc0 | 2021-10-16 20:52:05 +0100 | [diff] [blame] | 6034 | len_sum += gui_gtk2_draw_string_ext(row, col + len_sum, |
| 6035 | cs, slen, flags, needs_pango); |
Bram Moolenaar | d68a004 | 2021-10-20 23:08:11 +0100 | [diff] [blame] | 6036 | if (slen < len) |
| 6037 | *(cs + slen) = backup_ch; |
Dusan Popovic | 4eeedc0 | 2021-10-16 20:52:05 +0100 | [diff] [blame] | 6038 | cs += slen; |
| 6039 | byte_sum += slen; |
| 6040 | needs_pango = should_need_pango; |
| 6041 | } |
| 6042 | vim_free(conv_buf); |
| 6043 | return len_sum; |
| 6044 | } |
| 6045 | |
| 6046 | int |
| 6047 | gui_gtk2_draw_string_ext( |
| 6048 | int row, |
| 6049 | int col, |
| 6050 | char_u *s, |
| 6051 | int len, |
| 6052 | int flags, |
| 6053 | int force_pango) |
| 6054 | { |
| 6055 | GdkRectangle area; // area for clip mask |
| 6056 | PangoGlyphString *glyphs; // glyphs of current item |
| 6057 | int column_offset = 0; // column offset in cells |
| 6058 | int i; |
| 6059 | #if GTK_CHECK_VERSION(3,0,0) |
| 6060 | cairo_t *cr; |
| 6061 | #endif |
| 6062 | |
| 6063 | /* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6064 | * Restrict all drawing to the current screen line in order to prevent |
| 6065 | * fuzzy font lookups from messing up the screen. |
| 6066 | */ |
| 6067 | area.x = gui.border_offset; |
| 6068 | area.y = FILL_Y(row); |
| 6069 | area.width = gui.num_cols * gui.char_width; |
| 6070 | area.height = gui.char_height; |
| 6071 | |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 6072 | #if GTK_CHECK_VERSION(3,0,0) |
| 6073 | cr = cairo_create(gui.surface); |
| 6074 | cairo_rectangle(cr, area.x, area.y, area.width, area.height); |
| 6075 | cairo_clip(cr); |
| 6076 | #else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6077 | gdk_gc_set_clip_origin(gui.text_gc, 0, 0); |
| 6078 | gdk_gc_set_clip_rectangle(gui.text_gc, &area); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 6079 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6080 | |
| 6081 | glyphs = pango_glyph_string_new(); |
| 6082 | |
| 6083 | /* |
| 6084 | * Optimization hack: If possible, skip the itemize and shaping process |
| 6085 | * for pure ASCII strings. This optimization is particularly effective |
| 6086 | * because Vim draws space characters to clear parts of the screen. |
| 6087 | */ |
| 6088 | if (!(flags & DRAW_ITALIC) |
| 6089 | && !((flags & DRAW_BOLD) && gui.font_can_bold) |
Dusan Popovic | 4eeedc0 | 2021-10-16 20:52:05 +0100 | [diff] [blame] | 6090 | && gui.ascii_glyphs != NULL |
| 6091 | && !force_pango) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6092 | { |
| 6093 | char_u *p; |
| 6094 | |
| 6095 | for (p = s; p < s + len; ++p) |
| 6096 | if (*p & 0x80) |
| 6097 | goto not_ascii; |
| 6098 | |
| 6099 | pango_glyph_string_set_size(glyphs, len); |
| 6100 | |
| 6101 | for (i = 0; i < len; ++i) |
| 6102 | { |
Bram Moolenaar | 16350cb | 2016-08-14 20:27:34 +0200 | [diff] [blame] | 6103 | glyphs->glyphs[i] = gui.ascii_glyphs->glyphs[2 * s[i]]; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6104 | glyphs->log_clusters[i] = i; |
| 6105 | } |
| 6106 | |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 6107 | #if GTK_CHECK_VERSION(3,0,0) |
| 6108 | draw_glyph_string(row, col, len, flags, gui.ascii_font, glyphs, cr); |
| 6109 | #else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6110 | draw_glyph_string(row, col, len, flags, gui.ascii_font, glyphs); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 6111 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6112 | |
| 6113 | column_offset = len; |
| 6114 | } |
| 6115 | else |
| 6116 | not_ascii: |
| 6117 | { |
| 6118 | PangoAttrList *attr_list; |
| 6119 | GList *item_list; |
| 6120 | int cluster_width; |
| 6121 | int last_glyph_rbearing; |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 6122 | int cells = 0; // cells occupied by current cluster |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6123 | |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 6124 | // Safety check: pango crashes when invoked with invalid utf-8 |
| 6125 | // characters. |
Bram Moolenaar | 3fdfa4a | 2004-10-07 21:02:47 +0000 | [diff] [blame] | 6126 | if (!utf_valid_string(s, s + len)) |
| 6127 | { |
| 6128 | column_offset = len; |
| 6129 | goto skipitall; |
| 6130 | } |
| 6131 | |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 6132 | // original width of the current cluster |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6133 | cluster_width = PANGO_SCALE * gui.char_width; |
| 6134 | |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 6135 | // right bearing of the last non-composing glyph |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6136 | last_glyph_rbearing = PANGO_SCALE * gui.char_width; |
| 6137 | |
| 6138 | attr_list = pango_attr_list_new(); |
| 6139 | |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 6140 | // If 'guifontwide' is set then use that for double-width characters. |
| 6141 | // Otherwise just go with 'guifont' and let Pango do its thing. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6142 | if (gui.wide_font != NULL) |
| 6143 | apply_wide_font_attr(s, len, attr_list); |
| 6144 | |
| 6145 | if ((flags & DRAW_BOLD) && gui.font_can_bold) |
| 6146 | INSERT_PANGO_ATTR(pango_attr_weight_new(PANGO_WEIGHT_BOLD), |
| 6147 | attr_list, 0, len); |
| 6148 | if (flags & DRAW_ITALIC) |
| 6149 | INSERT_PANGO_ATTR(pango_attr_style_new(PANGO_STYLE_ITALIC), |
| 6150 | attr_list, 0, len); |
| 6151 | /* |
| 6152 | * Break the text into segments with consistent directional level |
| 6153 | * and shaping engine. Pure Latin text needs only a single segment, |
| 6154 | * so there's no need to worry about the loop's efficiency. Better |
| 6155 | * try to optimize elsewhere, e.g. reducing exposes and stuff :) |
| 6156 | */ |
| 6157 | item_list = pango_itemize(gui.text_context, |
| 6158 | (const char *)s, 0, len, attr_list, NULL); |
| 6159 | |
| 6160 | while (item_list != NULL) |
| 6161 | { |
| 6162 | PangoItem *item; |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 6163 | int item_cells = 0; // item length in cells |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6164 | |
| 6165 | item = (PangoItem *)item_list->data; |
| 6166 | item_list = g_list_delete_link(item_list, item_list); |
| 6167 | /* |
| 6168 | * Increment the bidirectional embedding level by 1 if it is not |
| 6169 | * even. An odd number means the output will be RTL, but we don't |
| 6170 | * want that since Vim handles right-to-left text on its own. It |
| 6171 | * would probably be sufficient to just set level = 0, but you can |
| 6172 | * never know :) |
| 6173 | * |
| 6174 | * Unfortunately we can't take advantage of Pango's ability to |
| 6175 | * render both LTR and RTL at the same time. In order to support |
| 6176 | * that, Vim's main screen engine would have to make use of Pango |
| 6177 | * functionality. |
| 6178 | */ |
| 6179 | item->analysis.level = (item->analysis.level + 1) & (~1U); |
| 6180 | |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 6181 | // HACK: Overrule the shape engine, we don't want shaping to be |
| 6182 | // done, because drawing the cursor would change the display. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6183 | item->analysis.shape_engine = default_shape_engine; |
| 6184 | |
Bram Moolenaar | 3cbe0c0 | 2015-09-08 20:00:22 +0200 | [diff] [blame] | 6185 | #ifdef HAVE_PANGO_SHAPE_FULL |
Bram Moolenaar | 7e2ec00 | 2015-09-08 16:31:06 +0200 | [diff] [blame] | 6186 | pango_shape_full((const char *)s + item->offset, item->length, |
| 6187 | (const char *)s, len, &item->analysis, glyphs); |
Bram Moolenaar | 3cbe0c0 | 2015-09-08 20:00:22 +0200 | [diff] [blame] | 6188 | #else |
| 6189 | pango_shape((const char *)s + item->offset, item->length, |
| 6190 | &item->analysis, glyphs); |
| 6191 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6192 | /* |
| 6193 | * Fixed-width hack: iterate over the array and assign a fixed |
| 6194 | * width to each glyph, thus overriding the choice made by the |
| 6195 | * shaping engine. We use utf_char2cells() to determine the |
| 6196 | * number of cells needed. |
| 6197 | * |
| 6198 | * Also perform all kind of dark magic to get composing |
| 6199 | * characters right (and pretty too of course). |
| 6200 | */ |
| 6201 | for (i = 0; i < glyphs->num_glyphs; ++i) |
| 6202 | { |
| 6203 | PangoGlyphInfo *glyph; |
| 6204 | |
| 6205 | glyph = &glyphs->glyphs[i]; |
| 6206 | |
| 6207 | if (glyph->attr.is_cluster_start) |
| 6208 | { |
| 6209 | int cellcount; |
| 6210 | |
| 6211 | cellcount = count_cluster_cells( |
| 6212 | s, item, glyphs, i, &cluster_width, |
| 6213 | (item_list != NULL) ? &last_glyph_rbearing : NULL); |
| 6214 | |
| 6215 | if (cellcount > 0) |
| 6216 | { |
| 6217 | int width; |
| 6218 | |
| 6219 | width = cellcount * gui.char_width * PANGO_SCALE; |
| 6220 | glyph->geometry.x_offset += |
| 6221 | MAX(0, width - cluster_width) / 2; |
| 6222 | glyph->geometry.width = width; |
| 6223 | } |
| 6224 | else |
| 6225 | { |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 6226 | // If there are only combining characters in the |
| 6227 | // cluster, we cannot just change the width of the |
| 6228 | // previous glyph since there is none. Therefore |
| 6229 | // some guesswork is needed. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6230 | setup_zero_width_cluster(item, glyph, cells, |
| 6231 | cluster_width, |
| 6232 | last_glyph_rbearing); |
| 6233 | } |
| 6234 | |
| 6235 | item_cells += cellcount; |
| 6236 | cells = cellcount; |
| 6237 | } |
| 6238 | else if (i > 0) |
| 6239 | { |
| 6240 | int width; |
| 6241 | |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 6242 | // There is a previous glyph, so we deal with combining |
| 6243 | // characters the canonical way. |
| 6244 | // In some circumstances Pango uses a positive x_offset, |
| 6245 | // then use the width of the previous glyph for this one |
| 6246 | // and set the previous width to zero. |
| 6247 | // Otherwise we get a negative x_offset, Pango has already |
| 6248 | // positioned the combining char, keep the widths as they |
| 6249 | // are. |
| 6250 | // For both adjust the x_offset to position the glyph in |
| 6251 | // the middle. |
Bram Moolenaar | 706e84b | 2010-08-07 15:46:45 +0200 | [diff] [blame] | 6252 | if (glyph->geometry.x_offset >= 0) |
Bram Moolenaar | 96118f3 | 2010-08-08 14:40:37 +0200 | [diff] [blame] | 6253 | { |
| 6254 | glyphs->glyphs[i].geometry.width = |
| 6255 | glyphs->glyphs[i - 1].geometry.width; |
Bram Moolenaar | 706e84b | 2010-08-07 15:46:45 +0200 | [diff] [blame] | 6256 | glyphs->glyphs[i - 1].geometry.width = 0; |
Bram Moolenaar | 96118f3 | 2010-08-08 14:40:37 +0200 | [diff] [blame] | 6257 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6258 | width = cells * gui.char_width * PANGO_SCALE; |
| 6259 | glyph->geometry.x_offset += |
| 6260 | MAX(0, width - cluster_width) / 2; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6261 | } |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 6262 | else // i == 0 "cannot happen" |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6263 | { |
| 6264 | glyph->geometry.width = 0; |
| 6265 | } |
| 6266 | } |
| 6267 | |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 6268 | //// Aaaaand action! ** |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 6269 | #if GTK_CHECK_VERSION(3,0,0) |
| 6270 | draw_glyph_string(row, col + column_offset, item_cells, |
| 6271 | flags, item->analysis.font, glyphs, |
| 6272 | cr); |
| 6273 | #else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6274 | draw_glyph_string(row, col + column_offset, item_cells, |
| 6275 | flags, item->analysis.font, glyphs); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 6276 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6277 | |
| 6278 | pango_item_free(item); |
| 6279 | |
| 6280 | column_offset += item_cells; |
| 6281 | } |
| 6282 | |
| 6283 | pango_attr_list_unref(attr_list); |
| 6284 | } |
| 6285 | |
Bram Moolenaar | 3fdfa4a | 2004-10-07 21:02:47 +0000 | [diff] [blame] | 6286 | skipitall: |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 6287 | // Draw underline and undercurl. |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 6288 | #if GTK_CHECK_VERSION(3,0,0) |
| 6289 | draw_under(flags, row, col, column_offset, cr); |
| 6290 | #else |
Bram Moolenaar | f36d369 | 2005-03-15 22:48:14 +0000 | [diff] [blame] | 6291 | draw_under(flags, row, col, column_offset); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 6292 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6293 | |
| 6294 | pango_glyph_string_free(glyphs); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6295 | |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 6296 | #if GTK_CHECK_VERSION(3,0,0) |
| 6297 | cairo_destroy(cr); |
presuku | 9459b8d | 2021-11-16 20:03:56 +0000 | [diff] [blame] | 6298 | gtk_widget_queue_draw_area(gui.drawarea, area.x, area.y, |
| 6299 | area.width, area.height); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 6300 | #else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6301 | gdk_gc_set_clip_rectangle(gui.text_gc, NULL); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 6302 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6303 | |
| 6304 | return column_offset; |
| 6305 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6306 | |
| 6307 | /* |
| 6308 | * Return OK if the key with the termcap name "name" is supported. |
| 6309 | */ |
| 6310 | int |
| 6311 | gui_mch_haskey(char_u *name) |
| 6312 | { |
| 6313 | int i; |
| 6314 | |
| 6315 | for (i = 0; special_keys[i].key_sym != 0; i++) |
| 6316 | if (name[0] == special_keys[i].code0 |
| 6317 | && name[1] == special_keys[i].code1) |
| 6318 | return OK; |
| 6319 | return FAIL; |
| 6320 | } |
| 6321 | |
rbtnn | b850c39 | 2022-10-01 15:47:04 +0100 | [diff] [blame] | 6322 | #if defined(FEAT_EVAL) || defined(PROTO) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6323 | /* |
| 6324 | * Return the text window-id and display. Only required for X-based GUI's |
| 6325 | */ |
| 6326 | int |
| 6327 | gui_get_x11_windis(Window *win, Display **dis) |
| 6328 | { |
lilydjwg | 6e0a18f | 2024-01-29 20:54:28 +0100 | [diff] [blame] | 6329 | Display * dpy = gui_mch_get_display(); |
| 6330 | if (dpy) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6331 | { |
lilydjwg | 6e0a18f | 2024-01-29 20:54:28 +0100 | [diff] [blame] | 6332 | *dis = dpy; |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 6333 | *win = GDK_WINDOW_XID(gtk_widget_get_window(gui.mainwin)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6334 | return OK; |
| 6335 | } |
| 6336 | |
| 6337 | *dis = NULL; |
| 6338 | *win = 0; |
| 6339 | return FAIL; |
| 6340 | } |
| 6341 | #endif |
| 6342 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6343 | Display * |
| 6344 | gui_mch_get_display(void) |
| 6345 | { |
lilydjwg | 6e0a18f | 2024-01-29 20:54:28 +0100 | [diff] [blame] | 6346 | if (gui.mainwin != NULL && gtk_widget_get_window(gui.mainwin) != NULL |
| 6347 | #if GTK_CHECK_VERSION(3,0,0) |
| 6348 | && GDK_IS_X11_DISPLAY(gtk_widget_get_display(gui.mainwin)) |
| 6349 | #endif |
| 6350 | ) |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 6351 | return GDK_WINDOW_XDISPLAY(gtk_widget_get_window(gui.mainwin)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6352 | else |
| 6353 | return NULL; |
| 6354 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6355 | |
| 6356 | void |
| 6357 | gui_mch_beep(void) |
| 6358 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6359 | GdkDisplay *display; |
| 6360 | |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 6361 | if (gui.mainwin != NULL && gtk_widget_get_realized(gui.mainwin)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6362 | display = gtk_widget_get_display(gui.mainwin); |
| 6363 | else |
| 6364 | display = gdk_display_get_default(); |
| 6365 | |
| 6366 | if (display != NULL) |
| 6367 | gdk_display_beep(display); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6368 | } |
| 6369 | |
| 6370 | void |
| 6371 | gui_mch_flash(int msec) |
| 6372 | { |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 6373 | #if GTK_CHECK_VERSION(3,0,0) |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 6374 | // TODO Replace GdkGC with Cairo |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 6375 | (void)msec; |
| 6376 | #else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6377 | GdkGCValues values; |
| 6378 | GdkGC *invert_gc; |
| 6379 | |
| 6380 | if (gui.drawarea->window == NULL) |
| 6381 | return; |
| 6382 | |
| 6383 | values.foreground.pixel = gui.norm_pixel ^ gui.back_pixel; |
| 6384 | values.background.pixel = gui.norm_pixel ^ gui.back_pixel; |
| 6385 | values.function = GDK_XOR; |
| 6386 | invert_gc = gdk_gc_new_with_values(gui.drawarea->window, |
| 6387 | &values, |
| 6388 | GDK_GC_FOREGROUND | |
| 6389 | GDK_GC_BACKGROUND | |
| 6390 | GDK_GC_FUNCTION); |
| 6391 | gdk_gc_set_exposures(invert_gc, |
| 6392 | gui.visibility != GDK_VISIBILITY_UNOBSCURED); |
| 6393 | /* |
| 6394 | * Do a visual beep by changing back and forth in some undetermined way, |
| 6395 | * the foreground and background colors. This is due to the fact that |
| 6396 | * there can't be really any prediction about the effects of XOR on |
| 6397 | * arbitrary X11 servers. However this seems to be enough for what we |
| 6398 | * intend it to do. |
| 6399 | */ |
| 6400 | gdk_draw_rectangle(gui.drawarea->window, invert_gc, |
| 6401 | TRUE, |
| 6402 | 0, 0, |
| 6403 | FILL_X((int)Columns) + gui.border_offset, |
| 6404 | FILL_Y((int)Rows) + gui.border_offset); |
| 6405 | |
| 6406 | gui_mch_flush(); |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 6407 | ui_delay((long)msec, TRUE); // wait so many msec |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6408 | |
| 6409 | gdk_draw_rectangle(gui.drawarea->window, invert_gc, |
| 6410 | TRUE, |
| 6411 | 0, 0, |
| 6412 | FILL_X((int)Columns) + gui.border_offset, |
| 6413 | FILL_Y((int)Rows) + gui.border_offset); |
| 6414 | |
| 6415 | gdk_gc_destroy(invert_gc); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 6416 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6417 | } |
| 6418 | |
| 6419 | /* |
| 6420 | * Invert a rectangle from row r, column c, for nr rows and nc columns. |
| 6421 | */ |
| 6422 | void |
| 6423 | gui_mch_invert_rectangle(int r, int c, int nr, int nc) |
| 6424 | { |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 6425 | #if GTK_CHECK_VERSION(3,0,0) |
Bram Moolenaar | 4fc563b | 2016-03-12 12:40:58 +0100 | [diff] [blame] | 6426 | const GdkRectangle rect = { |
| 6427 | FILL_X(c), FILL_Y(r), nc * gui.char_width, nr * gui.char_height |
| 6428 | }; |
| 6429 | cairo_t * const cr = cairo_create(gui.surface); |
| 6430 | |
Your Name | 810cb5a | 2022-09-13 11:25:54 +0100 | [diff] [blame] | 6431 | cairo_set_source_rgba(cr, 1.0, 1.0, 1.0, 1.0); |
Bram Moolenaar | 4fc563b | 2016-03-12 12:40:58 +0100 | [diff] [blame] | 6432 | # if CAIRO_VERSION >= CAIRO_VERSION_ENCODE(1,9,2) |
| 6433 | cairo_set_operator(cr, CAIRO_OPERATOR_DIFFERENCE); |
| 6434 | # else |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 6435 | // Give an implementation for older cairo versions if necessary. |
Bram Moolenaar | 4fc563b | 2016-03-12 12:40:58 +0100 | [diff] [blame] | 6436 | # endif |
presuku | 9459b8d | 2021-11-16 20:03:56 +0000 | [diff] [blame] | 6437 | cairo_rectangle(cr, rect.x, rect.y, rect.width, rect.height); |
Bram Moolenaar | 4fc563b | 2016-03-12 12:40:58 +0100 | [diff] [blame] | 6438 | cairo_fill(cr); |
| 6439 | |
| 6440 | cairo_destroy(cr); |
| 6441 | |
presuku | 9459b8d | 2021-11-16 20:03:56 +0000 | [diff] [blame] | 6442 | gtk_widget_queue_draw_area(gui.drawarea, rect.x, rect.y, |
| 6443 | rect.width, rect.height); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 6444 | #else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6445 | GdkGCValues values; |
| 6446 | GdkGC *invert_gc; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6447 | |
| 6448 | if (gui.drawarea->window == NULL) |
| 6449 | return; |
| 6450 | |
Your Name | 810cb5a | 2022-09-13 11:25:54 +0100 | [diff] [blame] | 6451 | values.function = GDK_INVERT; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6452 | invert_gc = gdk_gc_new_with_values(gui.drawarea->window, |
| 6453 | &values, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6454 | GDK_GC_FUNCTION); |
Bram Moolenaar | 89d4032 | 2006-08-29 15:30:07 +0000 | [diff] [blame] | 6455 | gdk_gc_set_exposures(invert_gc, gui.visibility != |
| 6456 | GDK_VISIBILITY_UNOBSCURED); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6457 | gdk_draw_rectangle(gui.drawarea->window, invert_gc, |
| 6458 | TRUE, |
| 6459 | FILL_X(c), FILL_Y(r), |
| 6460 | (nc) * gui.char_width, (nr) * gui.char_height); |
| 6461 | gdk_gc_destroy(invert_gc); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 6462 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6463 | } |
| 6464 | |
| 6465 | /* |
| 6466 | * Iconify the GUI window. |
| 6467 | */ |
| 6468 | void |
| 6469 | gui_mch_iconify(void) |
| 6470 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6471 | gtk_window_iconify(GTK_WINDOW(gui.mainwin)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6472 | } |
| 6473 | |
| 6474 | #if defined(FEAT_EVAL) || defined(PROTO) |
| 6475 | /* |
| 6476 | * Bring the Vim window to the foreground. |
| 6477 | */ |
| 6478 | void |
| 6479 | gui_mch_set_foreground(void) |
| 6480 | { |
Bram Moolenaar | 023930d | 2022-12-01 19:40:55 +0000 | [diff] [blame] | 6481 | // Just calling gtk_window_present() used to work in the past, but now this |
| 6482 | // sequence appears to be needed: |
| 6483 | // - Show the window on top of others. |
| 6484 | // - Present the window (also shows it above others). |
| 6485 | // - Do not the window on top of others (otherwise it would be stuck there). |
| 6486 | gtk_window_set_keep_above(GTK_WINDOW(gui.mainwin), TRUE); |
| 6487 | gui_may_flush(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6488 | gtk_window_present(GTK_WINDOW(gui.mainwin)); |
Bram Moolenaar | 023930d | 2022-12-01 19:40:55 +0000 | [diff] [blame] | 6489 | gui_may_flush(); |
| 6490 | gtk_window_set_keep_above(GTK_WINDOW(gui.mainwin), FALSE); |
| 6491 | gui_may_flush(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6492 | } |
| 6493 | #endif |
| 6494 | |
| 6495 | /* |
| 6496 | * Draw a cursor without focus. |
| 6497 | */ |
| 6498 | void |
| 6499 | gui_mch_draw_hollow_cursor(guicolor_T color) |
| 6500 | { |
| 6501 | int i = 1; |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 6502 | #if GTK_CHECK_VERSION(3,0,0) |
| 6503 | cairo_t *cr; |
| 6504 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6505 | |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 6506 | if (gtk_widget_get_window(gui.drawarea) == NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6507 | return; |
| 6508 | |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 6509 | #if GTK_CHECK_VERSION(3,0,0) |
| 6510 | cr = cairo_create(gui.surface); |
| 6511 | #endif |
| 6512 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6513 | gui_mch_set_fg_color(color); |
| 6514 | |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 6515 | #if GTK_CHECK_VERSION(3,0,0) |
Bram Moolenaar | 36edf06 | 2016-07-21 22:10:12 +0200 | [diff] [blame] | 6516 | cairo_set_source_rgba(cr, |
| 6517 | gui.fgcolor->red, gui.fgcolor->green, gui.fgcolor->blue, |
| 6518 | gui.fgcolor->alpha); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 6519 | #else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6520 | gdk_gc_set_foreground(gui.text_gc, gui.fgcolor); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 6521 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6522 | if (mb_lefthalve(gui.row, gui.col)) |
| 6523 | i = 2; |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 6524 | #if GTK_CHECK_VERSION(3,0,0) |
| 6525 | cairo_set_line_width(cr, 1.0); |
| 6526 | cairo_set_line_cap(cr, CAIRO_LINE_CAP_BUTT); |
| 6527 | cairo_rectangle(cr, |
| 6528 | FILL_X(gui.col) + 0.5, FILL_Y(gui.row) + 0.5, |
| 6529 | i * gui.char_width - 1, gui.char_height - 1); |
| 6530 | cairo_stroke(cr); |
| 6531 | cairo_destroy(cr); |
| 6532 | #else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6533 | gdk_draw_rectangle(gui.drawarea->window, gui.text_gc, |
| 6534 | FALSE, |
| 6535 | FILL_X(gui.col), FILL_Y(gui.row), |
| 6536 | i * gui.char_width - 1, gui.char_height - 1); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 6537 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6538 | } |
| 6539 | |
| 6540 | /* |
| 6541 | * Draw part of a cursor, "w" pixels wide, and "h" pixels high, using |
| 6542 | * color "color". |
| 6543 | */ |
| 6544 | void |
| 6545 | gui_mch_draw_part_cursor(int w, int h, guicolor_T color) |
| 6546 | { |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 6547 | if (gtk_widget_get_window(gui.drawarea) == NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6548 | return; |
| 6549 | |
| 6550 | gui_mch_set_fg_color(color); |
| 6551 | |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 6552 | #if GTK_CHECK_VERSION(3,0,0) |
| 6553 | { |
| 6554 | cairo_t *cr; |
| 6555 | |
| 6556 | cr = cairo_create(gui.surface); |
Bram Moolenaar | 36edf06 | 2016-07-21 22:10:12 +0200 | [diff] [blame] | 6557 | cairo_set_source_rgba(cr, |
| 6558 | gui.fgcolor->red, gui.fgcolor->green, gui.fgcolor->blue, |
| 6559 | gui.fgcolor->alpha); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 6560 | cairo_rectangle(cr, |
| 6561 | # ifdef FEAT_RIGHTLEFT |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 6562 | // vertical line should be on the right of current point |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 6563 | CURSOR_BAR_RIGHT ? FILL_X(gui.col + 1) - w : |
| 6564 | # endif |
| 6565 | FILL_X(gui.col), FILL_Y(gui.row) + gui.char_height - h, |
| 6566 | w, h); |
| 6567 | cairo_fill(cr); |
| 6568 | cairo_destroy(cr); |
| 6569 | } |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 6570 | #else // !GTK_CHECK_VERSION(3,0,0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6571 | gdk_gc_set_foreground(gui.text_gc, gui.fgcolor); |
| 6572 | gdk_draw_rectangle(gui.drawarea->window, gui.text_gc, |
| 6573 | TRUE, |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 6574 | # ifdef FEAT_RIGHTLEFT |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 6575 | // vertical line should be on the right of current point |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6576 | CURSOR_BAR_RIGHT ? FILL_X(gui.col + 1) - w : |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 6577 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6578 | FILL_X(gui.col), |
| 6579 | FILL_Y(gui.row) + gui.char_height - h, |
| 6580 | w, h); |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 6581 | #endif // !GTK_CHECK_VERSION(3,0,0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6582 | } |
| 6583 | |
| 6584 | |
| 6585 | /* |
| 6586 | * Catch up with any queued X11 events. This may put keyboard input into the |
| 6587 | * input buffer, call resize call-backs, trigger timers etc. If there is |
| 6588 | * nothing in the X11 event queue (& no timers pending), then we return |
| 6589 | * immediately. |
| 6590 | */ |
| 6591 | void |
| 6592 | gui_mch_update(void) |
| 6593 | { |
Bram Moolenaar | a3f4166 | 2010-07-11 19:01:06 +0200 | [diff] [blame] | 6594 | while (g_main_context_pending(NULL) && !vim_is_input_buf_full()) |
| 6595 | g_main_context_iteration(NULL, TRUE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6596 | } |
| 6597 | |
Bram Moolenaar | 4ab7968 | 2017-08-27 14:50:47 +0200 | [diff] [blame] | 6598 | static timeout_cb_type |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6599 | input_timer_cb(gpointer data) |
| 6600 | { |
| 6601 | int *timed_out = (int *) data; |
| 6602 | |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 6603 | // Just inform the caller about the occurrence of it |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6604 | *timed_out = TRUE; |
| 6605 | |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 6606 | return FALSE; // don't happen again |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6607 | } |
| 6608 | |
Bram Moolenaar | 4ab7968 | 2017-08-27 14:50:47 +0200 | [diff] [blame] | 6609 | #ifdef FEAT_JOB_CHANNEL |
| 6610 | static timeout_cb_type |
| 6611 | channel_poll_cb(gpointer data UNUSED) |
| 6612 | { |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 6613 | // Using an event handler for a channel that may be disconnected does |
| 6614 | // not work, it hangs. Instead poll for messages. |
Bram Moolenaar | 4ab7968 | 2017-08-27 14:50:47 +0200 | [diff] [blame] | 6615 | channel_handle_events(TRUE); |
| 6616 | parse_queued_messages(); |
| 6617 | |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 6618 | return TRUE; // repeat |
Bram Moolenaar | 4ab7968 | 2017-08-27 14:50:47 +0200 | [diff] [blame] | 6619 | } |
| 6620 | #endif |
| 6621 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6622 | /* |
| 6623 | * GUI input routine called by gui_wait_for_chars(). Waits for a character |
| 6624 | * from the keyboard. |
| 6625 | * wtime == -1 Wait forever. |
| 6626 | * wtime == 0 This should never happen. |
| 6627 | * wtime > 0 Wait wtime milliseconds for a character. |
| 6628 | * Returns OK if a character was found to be available within the given time, |
| 6629 | * or FAIL otherwise. |
| 6630 | */ |
| 6631 | int |
| 6632 | gui_mch_wait_for_chars(long wtime) |
| 6633 | { |
Bram Moolenaar | 4231da4 | 2016-06-02 14:30:04 +0200 | [diff] [blame] | 6634 | int focus; |
| 6635 | guint timer; |
| 6636 | static int timed_out; |
| 6637 | int retval = FAIL; |
Bram Moolenaar | 4ab7968 | 2017-08-27 14:50:47 +0200 | [diff] [blame] | 6638 | #ifdef FEAT_JOB_CHANNEL |
| 6639 | guint channel_timer = 0; |
| 6640 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6641 | |
| 6642 | timed_out = FALSE; |
| 6643 | |
Bram Moolenaar | 12dfc9e | 2019-01-28 22:32:58 +0100 | [diff] [blame] | 6644 | // This timeout makes sure that we will return if no characters arrived in |
| 6645 | // time. If "wtime" is zero just use one. |
| 6646 | if (wtime >= 0) |
Bram Moolenaar | 34a5874 | 2019-02-03 15:28:28 +0100 | [diff] [blame] | 6647 | timer = timeout_add(wtime == 0 ? 1L : wtime, |
Bram Moolenaar | 12dfc9e | 2019-01-28 22:32:58 +0100 | [diff] [blame] | 6648 | input_timer_cb, &timed_out); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6649 | else |
| 6650 | timer = 0; |
| 6651 | |
Bram Moolenaar | 4ab7968 | 2017-08-27 14:50:47 +0200 | [diff] [blame] | 6652 | #ifdef FEAT_JOB_CHANNEL |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 6653 | // If there is a channel with the keep_open flag we need to poll for input |
| 6654 | // on them. |
Bram Moolenaar | 4ab7968 | 2017-08-27 14:50:47 +0200 | [diff] [blame] | 6655 | if (channel_any_keep_open()) |
| 6656 | channel_timer = timeout_add(20, channel_poll_cb, NULL); |
| 6657 | #endif |
| 6658 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6659 | focus = gui.in_focus; |
| 6660 | |
| 6661 | do |
| 6662 | { |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 6663 | // Stop or start blinking when focus changes |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6664 | if (gui.in_focus != focus) |
| 6665 | { |
| 6666 | if (gui.in_focus) |
| 6667 | gui_mch_start_blink(); |
| 6668 | else |
Bram Moolenaar | 1dd45fb | 2018-01-31 21:10:01 +0100 | [diff] [blame] | 6669 | gui_mch_stop_blink(TRUE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6670 | focus = gui.in_focus; |
| 6671 | } |
| 6672 | |
Bram Moolenaar | 93c88e0 | 2015-09-15 14:12:05 +0200 | [diff] [blame] | 6673 | #ifdef MESSAGE_QUEUE |
Bram Moolenaar | 4231da4 | 2016-06-02 14:30:04 +0200 | [diff] [blame] | 6674 | # ifdef FEAT_TIMERS |
| 6675 | did_add_timer = FALSE; |
| 6676 | # endif |
Bram Moolenaar | 93c88e0 | 2015-09-15 14:12:05 +0200 | [diff] [blame] | 6677 | parse_queued_messages(); |
Bram Moolenaar | 4231da4 | 2016-06-02 14:30:04 +0200 | [diff] [blame] | 6678 | # ifdef FEAT_TIMERS |
| 6679 | if (did_add_timer) |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 6680 | // Need to recompute the waiting time. |
Bram Moolenaar | 4231da4 | 2016-06-02 14:30:04 +0200 | [diff] [blame] | 6681 | goto theend; |
| 6682 | # endif |
Bram Moolenaar | 8c8de83 | 2008-06-24 22:58:06 +0000 | [diff] [blame] | 6683 | #endif |
| 6684 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6685 | /* |
| 6686 | * Loop in GTK+ processing until a timeout or input occurs. |
Bram Moolenaar | d4755bb | 2004-09-02 19:12:26 +0000 | [diff] [blame] | 6687 | * Skip this if input is available anyway (can happen in rare |
| 6688 | * situations, sort of race condition). |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6689 | */ |
Bram Moolenaar | d4755bb | 2004-09-02 19:12:26 +0000 | [diff] [blame] | 6690 | if (!input_available()) |
Bram Moolenaar | a3f4166 | 2010-07-11 19:01:06 +0200 | [diff] [blame] | 6691 | g_main_context_iteration(NULL, TRUE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6692 | |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 6693 | // Got char, return immediately |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6694 | if (input_available()) |
| 6695 | { |
Bram Moolenaar | 4231da4 | 2016-06-02 14:30:04 +0200 | [diff] [blame] | 6696 | retval = OK; |
| 6697 | goto theend; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6698 | } |
| 6699 | } while (wtime < 0 || !timed_out); |
| 6700 | |
| 6701 | /* |
| 6702 | * Flush all eventually pending (drawing) events. |
| 6703 | */ |
| 6704 | gui_mch_update(); |
| 6705 | |
Bram Moolenaar | 4231da4 | 2016-06-02 14:30:04 +0200 | [diff] [blame] | 6706 | theend: |
| 6707 | if (timer != 0 && !timed_out) |
Bram Moolenaar | 4ab7968 | 2017-08-27 14:50:47 +0200 | [diff] [blame] | 6708 | timeout_remove(timer); |
| 6709 | #ifdef FEAT_JOB_CHANNEL |
| 6710 | if (channel_timer != 0) |
| 6711 | timeout_remove(channel_timer); |
Bram Moolenaar | 4231da4 | 2016-06-02 14:30:04 +0200 | [diff] [blame] | 6712 | #endif |
| 6713 | |
| 6714 | return retval; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6715 | } |
| 6716 | |
| 6717 | |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 6718 | ///////////////////////////////////////////////////////////////////////////// |
| 6719 | // Output drawing routines. |
| 6720 | // |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6721 | |
| 6722 | |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 6723 | // Flush any output to the screen |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6724 | void |
| 6725 | gui_mch_flush(void) |
| 6726 | { |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 6727 | if (gui.mainwin != NULL && gtk_widget_get_realized(gui.mainwin)) |
Bram Moolenaar | 92cbf62 | 2018-09-19 22:40:03 +0200 | [diff] [blame] | 6728 | #if GTK_CHECK_VERSION(2,4,0) |
Bram Moolenaar | b463e8d | 2017-06-05 15:07:09 +0200 | [diff] [blame] | 6729 | gdk_display_flush(gtk_widget_get_display(gui.mainwin)); |
Bram Moolenaar | 92cbf62 | 2018-09-19 22:40:03 +0200 | [diff] [blame] | 6730 | #else |
| 6731 | gdk_display_sync(gtk_widget_get_display(gui.mainwin)); |
| 6732 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6733 | } |
| 6734 | |
| 6735 | /* |
| 6736 | * Clear a rectangular region of the screen from text pos (row1, col1) to |
| 6737 | * (row2, col2) inclusive. |
| 6738 | */ |
| 6739 | void |
Bram Moolenaar | fe344a9 | 2017-02-23 12:20:35 +0100 | [diff] [blame] | 6740 | gui_mch_clear_block(int row1arg, int col1arg, int row2arg, int col2arg) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6741 | { |
Bram Moolenaar | fe344a9 | 2017-02-23 12:20:35 +0100 | [diff] [blame] | 6742 | |
| 6743 | int col1 = check_col(col1arg); |
| 6744 | int col2 = check_col(col2arg); |
| 6745 | int row1 = check_row(row1arg); |
| 6746 | int row2 = check_row(row2arg); |
| 6747 | |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 6748 | #if GTK_CHECK_VERSION(3,0,0) |
| 6749 | if (gtk_widget_get_window(gui.drawarea) == NULL) |
| 6750 | return; |
| 6751 | #else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6752 | GdkColor color; |
| 6753 | |
| 6754 | if (gui.drawarea->window == NULL) |
| 6755 | return; |
| 6756 | |
| 6757 | color.pixel = gui.back_pixel; |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 6758 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6759 | |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 6760 | #if GTK_CHECK_VERSION(3,0,0) |
| 6761 | { |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 6762 | // Add one pixel to the far right column in case a double-stroked |
| 6763 | // bold glyph may sit there. |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 6764 | const GdkRectangle rect = { |
| 6765 | FILL_X(col1), FILL_Y(row1), |
| 6766 | (col2 - col1 + 1) * gui.char_width + (col2 == Columns - 1), |
| 6767 | (row2 - row1 + 1) * gui.char_height |
| 6768 | }; |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 6769 | cairo_t * const cr = cairo_create(gui.surface); |
Bram Moolenaar | a859f04 | 2016-11-17 19:11:55 +0100 | [diff] [blame] | 6770 | # if GTK_CHECK_VERSION(3,22,2) |
| 6771 | set_cairo_source_rgba_from_color(cr, gui.back_pixel); |
| 6772 | # else |
presuku | 9459b8d | 2021-11-16 20:03:56 +0000 | [diff] [blame] | 6773 | GdkWindow * const win = gtk_widget_get_window(gui.drawarea); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 6774 | cairo_pattern_t * const pat = gdk_window_get_background_pattern(win); |
| 6775 | if (pat != NULL) |
| 6776 | cairo_set_source(cr, pat); |
| 6777 | else |
Bram Moolenaar | 36edf06 | 2016-07-21 22:10:12 +0200 | [diff] [blame] | 6778 | set_cairo_source_rgba_from_color(cr, gui.back_pixel); |
Bram Moolenaar | a859f04 | 2016-11-17 19:11:55 +0100 | [diff] [blame] | 6779 | # endif |
presuku | 9459b8d | 2021-11-16 20:03:56 +0000 | [diff] [blame] | 6780 | cairo_rectangle(cr, rect.x, rect.y, rect.width, rect.height); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 6781 | cairo_fill(cr); |
| 6782 | cairo_destroy(cr); |
| 6783 | |
presuku | 9459b8d | 2021-11-16 20:03:56 +0000 | [diff] [blame] | 6784 | gtk_widget_queue_draw_area(gui.drawarea, |
| 6785 | rect.x, rect.y, rect.width, rect.height); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 6786 | } |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 6787 | #else // !GTK_CHECK_VERSION(3,0,0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6788 | gdk_gc_set_foreground(gui.text_gc, &color); |
| 6789 | |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 6790 | // Clear one extra pixel at the far right, for when bold characters have |
| 6791 | // spilled over to the window border. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6792 | gdk_draw_rectangle(gui.drawarea->window, gui.text_gc, TRUE, |
| 6793 | FILL_X(col1), FILL_Y(row1), |
| 6794 | (col2 - col1 + 1) * gui.char_width |
| 6795 | + (col2 == Columns - 1), |
| 6796 | (row2 - row1 + 1) * gui.char_height); |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 6797 | #endif // !GTK_CHECK_VERSION(3,0,0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6798 | } |
| 6799 | |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 6800 | #if GTK_CHECK_VERSION(3,0,0) |
| 6801 | static void |
| 6802 | gui_gtk_window_clear(GdkWindow *win) |
| 6803 | { |
| 6804 | const GdkRectangle rect = { |
| 6805 | 0, 0, gdk_window_get_width(win), gdk_window_get_height(win) |
| 6806 | }; |
| 6807 | cairo_t * const cr = cairo_create(gui.surface); |
Bram Moolenaar | a859f04 | 2016-11-17 19:11:55 +0100 | [diff] [blame] | 6808 | # if GTK_CHECK_VERSION(3,22,2) |
| 6809 | set_cairo_source_rgba_from_color(cr, gui.back_pixel); |
| 6810 | # else |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 6811 | cairo_pattern_t * const pat = gdk_window_get_background_pattern(win); |
| 6812 | if (pat != NULL) |
| 6813 | cairo_set_source(cr, pat); |
| 6814 | else |
Bram Moolenaar | 36edf06 | 2016-07-21 22:10:12 +0200 | [diff] [blame] | 6815 | set_cairo_source_rgba_from_color(cr, gui.back_pixel); |
Bram Moolenaar | a859f04 | 2016-11-17 19:11:55 +0100 | [diff] [blame] | 6816 | # endif |
presuku | 9459b8d | 2021-11-16 20:03:56 +0000 | [diff] [blame] | 6817 | cairo_rectangle(cr, rect.x, rect.y, rect.width, rect.height); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 6818 | cairo_fill(cr); |
| 6819 | cairo_destroy(cr); |
| 6820 | |
presuku | 9459b8d | 2021-11-16 20:03:56 +0000 | [diff] [blame] | 6821 | gtk_widget_queue_draw_area(gui.drawarea, |
| 6822 | rect.x, rect.y, rect.width, rect.height); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 6823 | } |
Bram Moolenaar | 25328e3 | 2018-09-11 21:30:09 +0200 | [diff] [blame] | 6824 | #else |
| 6825 | # define gui_gtk_window_clear(win) gdk_window_clear(win) |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 6826 | #endif |
| 6827 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6828 | void |
| 6829 | gui_mch_clear_all(void) |
| 6830 | { |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 6831 | if (gtk_widget_get_window(gui.drawarea) != NULL) |
| 6832 | gui_gtk_window_clear(gtk_widget_get_window(gui.drawarea)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6833 | } |
| 6834 | |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 6835 | #if !GTK_CHECK_VERSION(3,0,0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6836 | /* |
| 6837 | * Redraw any text revealed by scrolling up/down. |
| 6838 | */ |
| 6839 | static void |
| 6840 | check_copy_area(void) |
| 6841 | { |
| 6842 | GdkEvent *event; |
| 6843 | int expose_count; |
| 6844 | |
| 6845 | if (gui.visibility != GDK_VISIBILITY_PARTIAL) |
| 6846 | return; |
| 6847 | |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 6848 | // Avoid redrawing the cursor while scrolling or it'll end up where |
| 6849 | // we don't want it to be. I'm not sure if it's correct to call |
| 6850 | // gui_dont_update_cursor() at this point but it works as a quick |
| 6851 | // fix for now. |
Bram Moolenaar | 107abd2 | 2016-08-12 14:08:25 +0200 | [diff] [blame] | 6852 | gui_dont_update_cursor(TRUE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6853 | |
| 6854 | do |
| 6855 | { |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 6856 | // Wait to check whether the scroll worked or not. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6857 | event = gdk_event_get_graphics_expose(gui.drawarea->window); |
| 6858 | |
| 6859 | if (event == NULL) |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 6860 | break; // received NoExpose event |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6861 | |
| 6862 | gui_redraw(event->expose.area.x, event->expose.area.y, |
| 6863 | event->expose.area.width, event->expose.area.height); |
| 6864 | |
| 6865 | expose_count = event->expose.count; |
| 6866 | gdk_event_free(event); |
| 6867 | } |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 6868 | while (expose_count > 0); // more events follow |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6869 | |
| 6870 | gui_can_update_cursor(); |
| 6871 | } |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 6872 | #endif // !GTK_CHECK_VERSION(3,0,0) |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 6873 | |
| 6874 | #if GTK_CHECK_VERSION(3,0,0) |
| 6875 | static void |
| 6876 | gui_gtk_surface_copy_rect(int dest_x, int dest_y, |
| 6877 | int src_x, int src_y, |
| 6878 | int width, int height) |
| 6879 | { |
| 6880 | cairo_t * const cr = cairo_create(gui.surface); |
| 6881 | |
| 6882 | cairo_rectangle(cr, dest_x, dest_y, width, height); |
| 6883 | cairo_clip(cr); |
| 6884 | cairo_push_group(cr); |
| 6885 | cairo_set_source_surface(cr, gui.surface, dest_x - src_x, dest_y - src_y); |
| 6886 | cairo_paint(cr); |
| 6887 | cairo_pop_group_to_source(cr); |
| 6888 | cairo_paint(cr); |
| 6889 | |
| 6890 | cairo_destroy(cr); |
| 6891 | } |
| 6892 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6893 | |
| 6894 | /* |
| 6895 | * Delete the given number of lines from the given row, scrolling up any |
| 6896 | * text further down within the scroll region. |
| 6897 | */ |
| 6898 | void |
| 6899 | gui_mch_delete_lines(int row, int num_lines) |
| 6900 | { |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 6901 | #if GTK_CHECK_VERSION(3,0,0) |
| 6902 | const int ncols = gui.scroll_region_right - gui.scroll_region_left + 1; |
| 6903 | const int nrows = gui.scroll_region_bot - row + 1; |
| 6904 | const int src_nrows = nrows - num_lines; |
| 6905 | |
| 6906 | gui_gtk_surface_copy_rect( |
| 6907 | FILL_X(gui.scroll_region_left), FILL_Y(row), |
| 6908 | FILL_X(gui.scroll_region_left), FILL_Y(row + num_lines), |
| 6909 | gui.char_width * ncols + 1, gui.char_height * src_nrows); |
| 6910 | gui_clear_block( |
| 6911 | gui.scroll_region_bot - num_lines + 1, gui.scroll_region_left, |
| 6912 | gui.scroll_region_bot, gui.scroll_region_right); |
presuku | 9459b8d | 2021-11-16 20:03:56 +0000 | [diff] [blame] | 6913 | gtk_widget_queue_draw_area(gui.drawarea, |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 6914 | FILL_X(gui.scroll_region_left), FILL_Y(row), |
presuku | 9459b8d | 2021-11-16 20:03:56 +0000 | [diff] [blame] | 6915 | gui.char_width * ncols + 1, gui.char_height * nrows); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 6916 | #else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6917 | if (gui.visibility == GDK_VISIBILITY_FULLY_OBSCURED) |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 6918 | return; // Can't see the window |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6919 | |
| 6920 | gdk_gc_set_foreground(gui.text_gc, gui.fgcolor); |
| 6921 | gdk_gc_set_background(gui.text_gc, gui.bgcolor); |
| 6922 | |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 6923 | // copy one extra pixel, for when bold has spilled over |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6924 | gdk_window_copy_area(gui.drawarea->window, gui.text_gc, |
| 6925 | FILL_X(gui.scroll_region_left), FILL_Y(row), |
| 6926 | gui.drawarea->window, |
| 6927 | FILL_X(gui.scroll_region_left), |
| 6928 | FILL_Y(row + num_lines), |
| 6929 | gui.char_width * (gui.scroll_region_right |
| 6930 | - gui.scroll_region_left + 1) + 1, |
| 6931 | gui.char_height * (gui.scroll_region_bot - row - num_lines + 1)); |
| 6932 | |
| 6933 | gui_clear_block(gui.scroll_region_bot - num_lines + 1, |
| 6934 | gui.scroll_region_left, |
| 6935 | gui.scroll_region_bot, gui.scroll_region_right); |
| 6936 | check_copy_area(); |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 6937 | #endif // !GTK_CHECK_VERSION(3,0,0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6938 | } |
| 6939 | |
| 6940 | /* |
| 6941 | * Insert the given number of lines before the given row, scrolling down any |
| 6942 | * following text within the scroll region. |
| 6943 | */ |
| 6944 | void |
| 6945 | gui_mch_insert_lines(int row, int num_lines) |
| 6946 | { |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 6947 | #if GTK_CHECK_VERSION(3,0,0) |
| 6948 | const int ncols = gui.scroll_region_right - gui.scroll_region_left + 1; |
| 6949 | const int nrows = gui.scroll_region_bot - row + 1; |
| 6950 | const int src_nrows = nrows - num_lines; |
| 6951 | |
| 6952 | gui_gtk_surface_copy_rect( |
| 6953 | FILL_X(gui.scroll_region_left), FILL_Y(row + num_lines), |
| 6954 | FILL_X(gui.scroll_region_left), FILL_Y(row), |
| 6955 | gui.char_width * ncols + 1, gui.char_height * src_nrows); |
presuku | 9459b8d | 2021-11-16 20:03:56 +0000 | [diff] [blame] | 6956 | gui_clear_block( |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 6957 | row, gui.scroll_region_left, |
| 6958 | row + num_lines - 1, gui.scroll_region_right); |
presuku | 9459b8d | 2021-11-16 20:03:56 +0000 | [diff] [blame] | 6959 | gtk_widget_queue_draw_area(gui.drawarea, |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 6960 | FILL_X(gui.scroll_region_left), FILL_Y(row), |
presuku | 9459b8d | 2021-11-16 20:03:56 +0000 | [diff] [blame] | 6961 | gui.char_width * ncols + 1, gui.char_height * nrows); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 6962 | #else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6963 | if (gui.visibility == GDK_VISIBILITY_FULLY_OBSCURED) |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 6964 | return; // Can't see the window |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6965 | |
| 6966 | gdk_gc_set_foreground(gui.text_gc, gui.fgcolor); |
| 6967 | gdk_gc_set_background(gui.text_gc, gui.bgcolor); |
| 6968 | |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 6969 | // copy one extra pixel, for when bold has spilled over |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6970 | gdk_window_copy_area(gui.drawarea->window, gui.text_gc, |
| 6971 | FILL_X(gui.scroll_region_left), FILL_Y(row + num_lines), |
| 6972 | gui.drawarea->window, |
| 6973 | FILL_X(gui.scroll_region_left), FILL_Y(row), |
| 6974 | gui.char_width * (gui.scroll_region_right |
| 6975 | - gui.scroll_region_left + 1) + 1, |
| 6976 | gui.char_height * (gui.scroll_region_bot - row - num_lines + 1)); |
| 6977 | |
| 6978 | gui_clear_block(row, gui.scroll_region_left, |
| 6979 | row + num_lines - 1, gui.scroll_region_right); |
| 6980 | check_copy_area(); |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 6981 | #endif // !GTK_CHECK_VERSION(3,0,0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6982 | } |
| 6983 | |
| 6984 | /* |
| 6985 | * X Selection stuff, for cutting and pasting text to other windows. |
| 6986 | */ |
| 6987 | void |
Bram Moolenaar | 0554fa4 | 2019-06-14 21:36:54 +0200 | [diff] [blame] | 6988 | clip_mch_request_selection(Clipboard_T *cbd) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6989 | { |
| 6990 | GdkAtom target; |
| 6991 | unsigned i; |
Bram Moolenaar | 51b5ab9 | 2008-01-06 14:17:07 +0000 | [diff] [blame] | 6992 | time_t start; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6993 | |
| 6994 | for (i = 0; i < N_SELECTION_TARGETS; ++i) |
| 6995 | { |
Bram Moolenaar | 3a6eaa5 | 2009-06-16 13:23:06 +0000 | [diff] [blame] | 6996 | if (!clip_html && selection_targets[i].info == TARGET_HTML) |
| 6997 | continue; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6998 | received_selection = RS_NONE; |
| 6999 | target = gdk_atom_intern(selection_targets[i].target, FALSE); |
| 7000 | |
| 7001 | gtk_selection_convert(gui.drawarea, |
| 7002 | cbd->gtk_sel_atom, target, |
| 7003 | (guint32)GDK_CURRENT_TIME); |
| 7004 | |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 7005 | // Hack: Wait up to three seconds for the selection. A hang was |
| 7006 | // noticed here when using the netrw plugin combined with ":gui" |
| 7007 | // during the FocusGained event. |
Bram Moolenaar | 51b5ab9 | 2008-01-06 14:17:07 +0000 | [diff] [blame] | 7008 | start = time(NULL); |
| 7009 | while (received_selection == RS_NONE && time(NULL) < start + 3) |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 7010 | g_main_context_iteration(NULL, TRUE); // wait for selection_received_cb |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7011 | |
| 7012 | if (received_selection != RS_FAIL) |
| 7013 | return; |
| 7014 | } |
| 7015 | |
lilydjwg | 6e0a18f | 2024-01-29 20:54:28 +0100 | [diff] [blame] | 7016 | if (gui_mch_get_display()) |
| 7017 | // Final fallback position - use the X CUT_BUFFER0 store |
| 7018 | yank_cut_buffer0(GDK_WINDOW_XDISPLAY(gtk_widget_get_window(gui.mainwin)), |
| 7019 | cbd); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7020 | } |
| 7021 | |
| 7022 | /* |
| 7023 | * Disown the selection. |
| 7024 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7025 | void |
Bram Moolenaar | 0554fa4 | 2019-06-14 21:36:54 +0200 | [diff] [blame] | 7026 | clip_mch_lose_selection(Clipboard_T *cbd UNUSED) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7027 | { |
Yegappan Lakshmanan | 7f8b255 | 2023-01-08 13:44:24 +0000 | [diff] [blame] | 7028 | if (in_selection_clear_event) |
| 7029 | return; |
| 7030 | |
| 7031 | gtk_selection_owner_set(NULL, cbd->gtk_sel_atom, gui.event_time); |
| 7032 | gui_mch_update(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7033 | } |
| 7034 | |
| 7035 | /* |
| 7036 | * Own the selection and return OK if it worked. |
| 7037 | */ |
| 7038 | int |
Bram Moolenaar | 0554fa4 | 2019-06-14 21:36:54 +0200 | [diff] [blame] | 7039 | clip_mch_own_selection(Clipboard_T *cbd) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7040 | { |
lilydjwg | 94ff09a | 2024-01-29 20:14:01 +0100 | [diff] [blame] | 7041 | // If we're blocking autocmds, we are filling the register to offer the |
| 7042 | // selection (inside selection-get) |
| 7043 | if (is_autocmd_blocked()) |
| 7044 | return OK; |
| 7045 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7046 | int success; |
| 7047 | |
| 7048 | success = gtk_selection_owner_set(gui.drawarea, cbd->gtk_sel_atom, |
Bram Moolenaar | 20892c1 | 2011-06-26 04:49:00 +0200 | [diff] [blame] | 7049 | gui.event_time); |
lilydjwg | 94ff09a | 2024-01-29 20:14:01 +0100 | [diff] [blame] | 7050 | // don't update on every visual selection change |
| 7051 | if (!(cbd->owned && VIsual_active)) |
| 7052 | gui_gtk_set_selection_targets(cbd->gtk_sel_atom); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7053 | gui_mch_update(); |
| 7054 | return (success) ? OK : FAIL; |
| 7055 | } |
| 7056 | |
| 7057 | /* |
| 7058 | * Send the current selection to the clipboard. Do nothing for X because we |
| 7059 | * will fill in the selection only when requested by another app. |
| 7060 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7061 | void |
Bram Moolenaar | 0554fa4 | 2019-06-14 21:36:54 +0200 | [diff] [blame] | 7062 | clip_mch_set_selection(Clipboard_T *cbd UNUSED) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7063 | { |
| 7064 | } |
| 7065 | |
Bram Moolenaar | 113e107 | 2019-01-20 15:30:40 +0100 | [diff] [blame] | 7066 | #if (defined(FEAT_XCLIPBOARD) && defined(USE_SYSTEM)) || defined(PROTO) |
Bram Moolenaar | 1a0316c | 2013-03-13 17:50:25 +0100 | [diff] [blame] | 7067 | int |
Bram Moolenaar | 0554fa4 | 2019-06-14 21:36:54 +0200 | [diff] [blame] | 7068 | clip_gtk_owner_exists(Clipboard_T *cbd) |
Bram Moolenaar | 1a0316c | 2013-03-13 17:50:25 +0100 | [diff] [blame] | 7069 | { |
| 7070 | return gdk_selection_owner_get(cbd->gtk_sel_atom) != NULL; |
| 7071 | } |
Bram Moolenaar | 113e107 | 2019-01-20 15:30:40 +0100 | [diff] [blame] | 7072 | #endif |
Bram Moolenaar | 1a0316c | 2013-03-13 17:50:25 +0100 | [diff] [blame] | 7073 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7074 | |
| 7075 | #if defined(FEAT_MENU) || defined(PROTO) |
| 7076 | /* |
| 7077 | * Make a menu item appear either active or not active (grey or not grey). |
| 7078 | */ |
| 7079 | void |
| 7080 | gui_mch_menu_grey(vimmenu_T *menu, int grey) |
| 7081 | { |
| 7082 | if (menu->id == NULL) |
| 7083 | return; |
| 7084 | |
| 7085 | if (menu_is_separator(menu->name)) |
| 7086 | grey = TRUE; |
| 7087 | |
| 7088 | gui_mch_menu_hidden(menu, FALSE); |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 7089 | // Be clever about bitfields versus true booleans here! |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 7090 | if (!gtk_widget_get_sensitive(menu->id) == !grey) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7091 | { |
| 7092 | gtk_widget_set_sensitive(menu->id, !grey); |
| 7093 | gui_mch_update(); |
| 7094 | } |
| 7095 | } |
| 7096 | |
| 7097 | /* |
| 7098 | * Make menu item hidden or not hidden. |
| 7099 | */ |
| 7100 | void |
| 7101 | gui_mch_menu_hidden(vimmenu_T *menu, int hidden) |
| 7102 | { |
| 7103 | if (menu->id == 0) |
| 7104 | return; |
| 7105 | |
| 7106 | if (hidden) |
| 7107 | { |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 7108 | if (gtk_widget_get_visible(menu->id)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7109 | { |
| 7110 | gtk_widget_hide(menu->id); |
| 7111 | gui_mch_update(); |
| 7112 | } |
| 7113 | } |
| 7114 | else |
| 7115 | { |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 7116 | if (!gtk_widget_get_visible(menu->id)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7117 | { |
| 7118 | gtk_widget_show(menu->id); |
| 7119 | gui_mch_update(); |
| 7120 | } |
| 7121 | } |
| 7122 | } |
| 7123 | |
| 7124 | /* |
| 7125 | * This is called after setting all the menus to grey/hidden or not. |
| 7126 | */ |
| 7127 | void |
| 7128 | gui_mch_draw_menubar(void) |
| 7129 | { |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 7130 | // just make sure that the visual changes get effect immediately |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7131 | gui_mch_update(); |
| 7132 | } |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 7133 | #endif // FEAT_MENU |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7134 | |
| 7135 | /* |
| 7136 | * Scrollbar stuff. |
| 7137 | */ |
| 7138 | void |
| 7139 | gui_mch_enable_scrollbar(scrollbar_T *sb, int flag) |
| 7140 | { |
| 7141 | if (sb->id == NULL) |
| 7142 | return; |
| 7143 | |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 7144 | gtk_widget_set_visible(sb->id, flag); |
Bram Moolenaar | b3656ed | 2006-03-20 21:59:49 +0000 | [diff] [blame] | 7145 | update_window_manager_hints(0, 0); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7146 | } |
| 7147 | |
| 7148 | |
| 7149 | /* |
| 7150 | * Return the RGB value of a pixel as long. |
| 7151 | */ |
Bram Moolenaar | 1b58cdd | 2016-08-22 23:04:33 +0200 | [diff] [blame] | 7152 | guicolor_T |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7153 | gui_mch_get_rgb(guicolor_T pixel) |
| 7154 | { |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 7155 | #if GTK_CHECK_VERSION(3,0,0) |
Bram Moolenaar | 36edf06 | 2016-07-21 22:10:12 +0200 | [diff] [blame] | 7156 | return (long_u)pixel; |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 7157 | #else |
Bram Moolenaar | 36edf06 | 2016-07-21 22:10:12 +0200 | [diff] [blame] | 7158 | GdkColor color; |
| 7159 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7160 | gdk_colormap_query_color(gtk_widget_get_colormap(gui.drawarea), |
| 7161 | (unsigned long)pixel, &color); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7162 | |
Bram Moolenaar | 1b58cdd | 2016-08-22 23:04:33 +0200 | [diff] [blame] | 7163 | return (guicolor_T)( |
| 7164 | (((unsigned)color.red & 0xff00) << 8) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7165 | | ((unsigned)color.green & 0xff00) |
Bram Moolenaar | 1b58cdd | 2016-08-22 23:04:33 +0200 | [diff] [blame] | 7166 | | (((unsigned)color.blue & 0xff00) >> 8)); |
Bram Moolenaar | 36edf06 | 2016-07-21 22:10:12 +0200 | [diff] [blame] | 7167 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7168 | } |
| 7169 | |
| 7170 | /* |
Bram Moolenaar | 6cc1619 | 2005-01-08 21:49:45 +0000 | [diff] [blame] | 7171 | * Get current mouse coordinates in text window. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7172 | */ |
Bram Moolenaar | 6cc1619 | 2005-01-08 21:49:45 +0000 | [diff] [blame] | 7173 | void |
| 7174 | gui_mch_getmouse(int *x, int *y) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7175 | { |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 7176 | gui_gtk_get_pointer(gui.drawarea, x, y, NULL); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7177 | } |
| 7178 | |
| 7179 | void |
| 7180 | gui_mch_setmouse(int x, int y) |
| 7181 | { |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 7182 | // Sorry for the Xlib call, but we can't avoid it, since there is no |
| 7183 | // internal GDK mechanism present to accomplish this. (and for good |
| 7184 | // reason...) |
lilydjwg | 6e0a18f | 2024-01-29 20:54:28 +0100 | [diff] [blame] | 7185 | Display * dpy = gui_mch_get_display(); |
| 7186 | if (dpy) |
| 7187 | XWarpPointer(dpy, (Window)0, |
| 7188 | GDK_WINDOW_XID(gtk_widget_get_window(gui.drawarea)), |
| 7189 | 0, 0, 0U, 0U, x, y); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7190 | } |
| 7191 | |
| 7192 | |
| 7193 | #ifdef FEAT_MOUSESHAPE |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 7194 | // The last set mouse pointer shape is remembered, to be used when it goes |
| 7195 | // from hidden to not hidden. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7196 | static int last_shape = 0; |
| 7197 | #endif |
| 7198 | |
| 7199 | /* |
| 7200 | * Use the blank mouse pointer or not. |
| 7201 | * |
| 7202 | * hide: TRUE = use blank ptr, FALSE = use parent ptr |
| 7203 | */ |
| 7204 | void |
| 7205 | gui_mch_mousehide(int hide) |
| 7206 | { |
Yegappan Lakshmanan | 7f8b255 | 2023-01-08 13:44:24 +0000 | [diff] [blame] | 7207 | if (gui.pointer_hidden == hide) |
| 7208 | return; |
| 7209 | |
| 7210 | gui.pointer_hidden = hide; |
| 7211 | if (gtk_widget_get_window(gui.drawarea) && gui.blank_pointer != NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7212 | { |
Yegappan Lakshmanan | 7f8b255 | 2023-01-08 13:44:24 +0000 | [diff] [blame] | 7213 | if (hide) |
| 7214 | gdk_window_set_cursor(gtk_widget_get_window(gui.drawarea), |
| 7215 | gui.blank_pointer); |
| 7216 | else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7217 | #ifdef FEAT_MOUSESHAPE |
Yegappan Lakshmanan | 7f8b255 | 2023-01-08 13:44:24 +0000 | [diff] [blame] | 7218 | mch_set_mouse_shape(last_shape); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7219 | #else |
Yegappan Lakshmanan | 7f8b255 | 2023-01-08 13:44:24 +0000 | [diff] [blame] | 7220 | gdk_window_set_cursor(gtk_widget_get_window(gui.drawarea), NULL); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7221 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7222 | } |
| 7223 | } |
| 7224 | |
| 7225 | #if defined(FEAT_MOUSESHAPE) || defined(PROTO) |
| 7226 | |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 7227 | // Table for shape IDs. Keep in sync with the mshape_names[] table in |
| 7228 | // misc2.c! |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7229 | static const int mshape_ids[] = |
| 7230 | { |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 7231 | GDK_LEFT_PTR, // arrow |
| 7232 | GDK_CURSOR_IS_PIXMAP, // blank |
| 7233 | GDK_XTERM, // beam |
| 7234 | GDK_SB_V_DOUBLE_ARROW, // updown |
| 7235 | GDK_SIZING, // udsizing |
| 7236 | GDK_SB_H_DOUBLE_ARROW, // leftright |
| 7237 | GDK_SIZING, // lrsizing |
| 7238 | GDK_WATCH, // busy |
| 7239 | GDK_X_CURSOR, // no |
| 7240 | GDK_CROSSHAIR, // crosshair |
| 7241 | GDK_HAND1, // hand1 |
| 7242 | GDK_HAND2, // hand2 |
| 7243 | GDK_PENCIL, // pencil |
| 7244 | GDK_QUESTION_ARROW, // question |
| 7245 | GDK_RIGHT_PTR, // right-arrow |
| 7246 | GDK_CENTER_PTR, // up-arrow |
| 7247 | GDK_LEFT_PTR // last one |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7248 | }; |
| 7249 | |
| 7250 | void |
| 7251 | mch_set_mouse_shape(int shape) |
| 7252 | { |
| 7253 | int id; |
| 7254 | GdkCursor *c; |
| 7255 | |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 7256 | if (gtk_widget_get_window(gui.drawarea) == NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7257 | return; |
| 7258 | |
| 7259 | if (shape == MSHAPE_HIDE || gui.pointer_hidden) |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 7260 | gdk_window_set_cursor(gtk_widget_get_window(gui.drawarea), |
| 7261 | gui.blank_pointer); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7262 | else |
| 7263 | { |
| 7264 | if (shape >= MSHAPE_NUMBERED) |
| 7265 | { |
| 7266 | id = shape - MSHAPE_NUMBERED; |
| 7267 | if (id >= GDK_LAST_CURSOR) |
| 7268 | id = GDK_LEFT_PTR; |
| 7269 | else |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 7270 | id &= ~1; // they are always even (why?) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7271 | } |
K.Takata | eeec254 | 2021-06-02 13:28:16 +0200 | [diff] [blame] | 7272 | else if (shape < (int)ARRAY_LENGTH(mshape_ids)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7273 | id = mshape_ids[shape]; |
Bram Moolenaar | f9393ef | 2006-04-24 19:47:27 +0000 | [diff] [blame] | 7274 | else |
| 7275 | return; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7276 | c = gdk_cursor_new_for_display( |
Bram Moolenaar | b71ec9f | 2005-01-25 22:22:02 +0000 | [diff] [blame] | 7277 | gtk_widget_get_display(gui.drawarea), (GdkCursorType)id); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 7278 | gdk_window_set_cursor(gtk_widget_get_window(gui.drawarea), c); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 7279 | # if GTK_CHECK_VERSION(3,0,0) |
| 7280 | g_object_unref(G_OBJECT(c)); |
| 7281 | # else |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 7282 | gdk_cursor_destroy(c); // Unref, actually. Bloody GTK+ 1. |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 7283 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7284 | } |
| 7285 | if (shape != MSHAPE_HIDE) |
| 7286 | last_shape = shape; |
| 7287 | } |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 7288 | #endif // FEAT_MOUSESHAPE |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7289 | |
| 7290 | |
| 7291 | #if defined(FEAT_SIGN_ICONS) || defined(PROTO) |
| 7292 | /* |
| 7293 | * Signs are currently always 2 chars wide. With GTK+ 2, the image will be |
| 7294 | * scaled down if the current font is not big enough, or scaled up if the image |
| 7295 | * size is less than 3/4 of the maximum sign size. With GTK+ 1, the pixmap |
| 7296 | * will be cut off if the current font is not big enough, or centered if it's |
| 7297 | * too small. |
| 7298 | */ |
| 7299 | # define SIGN_WIDTH (2 * gui.char_width) |
| 7300 | # define SIGN_HEIGHT (gui.char_height) |
| 7301 | # define SIGN_ASPECT ((double)SIGN_HEIGHT / (double)SIGN_WIDTH) |
| 7302 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7303 | void |
| 7304 | gui_mch_drawsign(int row, int col, int typenr) |
| 7305 | { |
| 7306 | GdkPixbuf *sign; |
| 7307 | |
| 7308 | sign = (GdkPixbuf *)sign_get_image(typenr); |
| 7309 | |
Yegappan Lakshmanan | 7f8b255 | 2023-01-08 13:44:24 +0000 | [diff] [blame] | 7310 | if (sign == NULL || gui.drawarea == NULL |
| 7311 | || gtk_widget_get_window(gui.drawarea) == NULL) |
| 7312 | return; |
| 7313 | |
| 7314 | int width; |
| 7315 | int height; |
| 7316 | int xoffset; |
| 7317 | int yoffset; |
| 7318 | int need_scale; |
| 7319 | |
| 7320 | width = gdk_pixbuf_get_width(sign); |
| 7321 | height = gdk_pixbuf_get_height(sign); |
| 7322 | /* |
| 7323 | * Decide whether we need to scale. Allow one pixel of border |
| 7324 | * width to be cut off, in order to avoid excessive scaling for |
| 7325 | * tiny differences in font size. |
| 7326 | * Do scale to fit the height to avoid gaps because of linespacing. |
| 7327 | */ |
| 7328 | need_scale = (width > SIGN_WIDTH + 2 |
| 7329 | || height != SIGN_HEIGHT |
| 7330 | || (width < 3 * SIGN_WIDTH / 4 |
| 7331 | && height < 3 * SIGN_HEIGHT / 4)); |
| 7332 | if (need_scale) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7333 | { |
Yegappan Lakshmanan | 7f8b255 | 2023-01-08 13:44:24 +0000 | [diff] [blame] | 7334 | double aspect; |
| 7335 | int w = width; |
| 7336 | int h = height; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7337 | |
Yegappan Lakshmanan | 7f8b255 | 2023-01-08 13:44:24 +0000 | [diff] [blame] | 7338 | // Keep the original aspect ratio |
| 7339 | aspect = (double)height / (double)width; |
| 7340 | width = (double)SIGN_WIDTH * SIGN_ASPECT / aspect; |
| 7341 | width = MIN(width, SIGN_WIDTH); |
| 7342 | if (((double)(MAX(height, SIGN_HEIGHT)) / |
| 7343 | (double)(MIN(height, SIGN_HEIGHT))) < 1.15) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7344 | { |
Yegappan Lakshmanan | 7f8b255 | 2023-01-08 13:44:24 +0000 | [diff] [blame] | 7345 | // Change the aspect ratio by at most 15% to fill the |
| 7346 | // available space completely. |
| 7347 | height = (double)SIGN_HEIGHT * SIGN_ASPECT / aspect; |
| 7348 | height = MIN(height, SIGN_HEIGHT); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7349 | } |
Yegappan Lakshmanan | 7f8b255 | 2023-01-08 13:44:24 +0000 | [diff] [blame] | 7350 | else |
| 7351 | height = (double)width * aspect; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7352 | |
Yegappan Lakshmanan | 7f8b255 | 2023-01-08 13:44:24 +0000 | [diff] [blame] | 7353 | if (w == width && h == height) |
| 7354 | { |
| 7355 | // no change in dimensions; don't decrease reference counter |
| 7356 | // (below) |
| 7357 | need_scale = FALSE; |
| 7358 | } |
| 7359 | else |
| 7360 | { |
| 7361 | // This doesn't seem to be worth caching, and doing so would |
| 7362 | // complicate the code quite a bit. |
| 7363 | sign = gdk_pixbuf_scale_simple(sign, width, height, |
| 7364 | GDK_INTERP_BILINEAR); |
| 7365 | if (sign == NULL) |
| 7366 | return; // out of memory |
| 7367 | } |
| 7368 | } |
| 7369 | |
| 7370 | // The origin is the upper-left corner of the pixmap. Therefore |
| 7371 | // these offset may become negative if the pixmap is smaller than |
| 7372 | // the 2x1 cells reserved for the sign icon. |
| 7373 | xoffset = (width - SIGN_WIDTH) / 2; |
| 7374 | yoffset = (height - SIGN_HEIGHT) / 2; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7375 | |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 7376 | # if GTK_CHECK_VERSION(3,0,0) |
Yegappan Lakshmanan | 7f8b255 | 2023-01-08 13:44:24 +0000 | [diff] [blame] | 7377 | { |
| 7378 | cairo_t *cr; |
| 7379 | cairo_surface_t *bg_surf; |
| 7380 | cairo_t *bg_cr; |
| 7381 | cairo_surface_t *sign_surf; |
| 7382 | cairo_t *sign_cr; |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 7383 | |
Yegappan Lakshmanan | 7f8b255 | 2023-01-08 13:44:24 +0000 | [diff] [blame] | 7384 | cr = cairo_create(gui.surface); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 7385 | |
Yegappan Lakshmanan | 7f8b255 | 2023-01-08 13:44:24 +0000 | [diff] [blame] | 7386 | bg_surf = cairo_surface_create_similar(gui.surface, |
| 7387 | cairo_surface_get_content(gui.surface), |
| 7388 | SIGN_WIDTH, SIGN_HEIGHT); |
| 7389 | bg_cr = cairo_create(bg_surf); |
| 7390 | cairo_set_source_rgba(bg_cr, |
| 7391 | gui.bgcolor->red, gui.bgcolor->green, gui.bgcolor->blue, |
| 7392 | gui.bgcolor->alpha); |
| 7393 | cairo_paint(bg_cr); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 7394 | |
Yegappan Lakshmanan | 7f8b255 | 2023-01-08 13:44:24 +0000 | [diff] [blame] | 7395 | sign_surf = cairo_surface_create_similar(gui.surface, |
| 7396 | cairo_surface_get_content(gui.surface), |
| 7397 | SIGN_WIDTH, SIGN_HEIGHT); |
| 7398 | sign_cr = cairo_create(sign_surf); |
| 7399 | gdk_cairo_set_source_pixbuf(sign_cr, sign, -xoffset, -yoffset); |
| 7400 | cairo_paint(sign_cr); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 7401 | |
Yegappan Lakshmanan | 7f8b255 | 2023-01-08 13:44:24 +0000 | [diff] [blame] | 7402 | cairo_set_operator(sign_cr, CAIRO_OPERATOR_DEST_OVER); |
| 7403 | cairo_set_source_surface(sign_cr, bg_surf, 0, 0); |
| 7404 | cairo_paint(sign_cr); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 7405 | |
Yegappan Lakshmanan | 7f8b255 | 2023-01-08 13:44:24 +0000 | [diff] [blame] | 7406 | cairo_set_source_surface(cr, sign_surf, FILL_X(col), FILL_Y(row)); |
| 7407 | cairo_paint(cr); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 7408 | |
Yegappan Lakshmanan | 7f8b255 | 2023-01-08 13:44:24 +0000 | [diff] [blame] | 7409 | cairo_destroy(sign_cr); |
| 7410 | cairo_surface_destroy(sign_surf); |
| 7411 | cairo_destroy(bg_cr); |
| 7412 | cairo_surface_destroy(bg_surf); |
| 7413 | cairo_destroy(cr); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 7414 | |
Yegappan Lakshmanan | 7f8b255 | 2023-01-08 13:44:24 +0000 | [diff] [blame] | 7415 | gtk_widget_queue_draw_area(gui.drawarea, |
| 7416 | FILL_X(col), FILL_Y(col), width, height); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 7417 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7418 | } |
Yegappan Lakshmanan | 7f8b255 | 2023-01-08 13:44:24 +0000 | [diff] [blame] | 7419 | # else // !GTK_CHECK_VERSION(3,0,0) |
| 7420 | gdk_gc_set_foreground(gui.text_gc, gui.bgcolor); |
| 7421 | |
| 7422 | gdk_draw_rectangle(gui.drawarea->window, |
| 7423 | gui.text_gc, |
| 7424 | TRUE, |
| 7425 | FILL_X(col), |
| 7426 | FILL_Y(row), |
| 7427 | SIGN_WIDTH, |
| 7428 | SIGN_HEIGHT); |
| 7429 | |
| 7430 | gdk_pixbuf_render_to_drawable_alpha(sign, |
| 7431 | gui.drawarea->window, |
| 7432 | MAX(0, xoffset), |
| 7433 | MAX(0, yoffset), |
| 7434 | FILL_X(col) - MIN(0, xoffset), |
| 7435 | FILL_Y(row) - MIN(0, yoffset), |
| 7436 | MIN(width, SIGN_WIDTH), |
| 7437 | MIN(height, SIGN_HEIGHT), |
| 7438 | GDK_PIXBUF_ALPHA_BILEVEL, |
| 7439 | 127, |
| 7440 | GDK_RGB_DITHER_NORMAL, |
| 7441 | 0, 0); |
| 7442 | # endif // !GTK_CHECK_VERSION(3,0,0) |
| 7443 | if (need_scale) |
| 7444 | g_object_unref(sign); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7445 | } |
| 7446 | |
| 7447 | void * |
| 7448 | gui_mch_register_sign(char_u *signfile) |
| 7449 | { |
| 7450 | if (signfile[0] != NUL && signfile[0] != '-' && gui.in_use) |
| 7451 | { |
| 7452 | GdkPixbuf *sign; |
| 7453 | GError *error = NULL; |
| 7454 | char_u *message; |
| 7455 | |
| 7456 | sign = gdk_pixbuf_new_from_file((const char *)signfile, &error); |
| 7457 | |
| 7458 | if (error == NULL) |
| 7459 | return sign; |
| 7460 | |
| 7461 | message = (char_u *)error->message; |
| 7462 | |
| 7463 | if (message != NULL && input_conv.vc_type != CONV_NONE) |
| 7464 | message = string_convert(&input_conv, message, NULL); |
| 7465 | |
| 7466 | if (message != NULL) |
| 7467 | { |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 7468 | // The error message is already translated and will be more |
| 7469 | // descriptive than anything we could possibly do ourselves. |
Bram Moolenaar | f9e3e09 | 2019-01-13 23:38:42 +0100 | [diff] [blame] | 7470 | semsg("E255: %s", message); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7471 | |
| 7472 | if (input_conv.vc_type != CONV_NONE) |
| 7473 | vim_free(message); |
| 7474 | } |
| 7475 | g_error_free(error); |
| 7476 | } |
| 7477 | |
| 7478 | return NULL; |
| 7479 | } |
| 7480 | |
| 7481 | void |
| 7482 | gui_mch_destroy_sign(void *sign) |
| 7483 | { |
| 7484 | if (sign != NULL) |
| 7485 | g_object_unref(sign); |
| 7486 | } |
| 7487 | |
Bram Moolenaar | 3061390 | 2019-12-01 22:11:18 +0100 | [diff] [blame] | 7488 | #endif // FEAT_SIGN_ICONS |