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 |
| 34 | /* Gnome redefines _() and N_(). Grrr... */ |
| 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) |
| 51 | # define ENABLE_NLS /* so the texts in the dialog boxes are translated */ |
| 52 | # endif |
| 53 | # include <gnome.h> |
| 54 | # include "version.h" |
Bram Moolenaar | db552d60 | 2006-03-23 22:59:57 +0000 | [diff] [blame] | 55 | /* missing prototype in bonobo-dock-item.h */ |
| 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) |
| 60 | /* When generating prototypes we don't want syntax errors. */ |
| 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> |
| 89 | # ifdef WIN3264 |
| 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 | |
| 108 | /* Selection type distinguishers */ |
| 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, |
| 119 | TARGET_VIM, |
| 120 | TARGET_VIMENC |
| 121 | }; |
| 122 | |
| 123 | /* |
| 124 | * Table of selection targets supported by Vim. |
| 125 | * Note: Order matters, preferred types should come first. |
| 126 | */ |
| 127 | static const GtkTargetEntry selection_targets[] = |
| 128 | { |
| 129 | {VIMENC_ATOM_NAME, 0, TARGET_VIMENC}, |
| 130 | {VIM_ATOM_NAME, 0, TARGET_VIM}, |
Bram Moolenaar | 3a6eaa5 | 2009-06-16 13:23:06 +0000 | [diff] [blame] | 131 | {"text/html", 0, TARGET_HTML}, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 132 | {"UTF8_STRING", 0, TARGET_UTF8_STRING}, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 133 | {"COMPOUND_TEXT", 0, TARGET_COMPOUND_TEXT}, |
| 134 | {"TEXT", 0, TARGET_TEXT}, |
| 135 | {"STRING", 0, TARGET_STRING} |
| 136 | }; |
| 137 | #define N_SELECTION_TARGETS (sizeof(selection_targets) / sizeof(selection_targets[0])) |
| 138 | |
| 139 | #ifdef FEAT_DND |
| 140 | /* |
| 141 | * Table of DnD targets supported by Vim. |
| 142 | * Note: Order matters, preferred types should come first. |
| 143 | */ |
| 144 | static const GtkTargetEntry dnd_targets[] = |
| 145 | { |
| 146 | {"text/uri-list", 0, TARGET_TEXT_URI_LIST}, |
Bram Moolenaar | 3a6eaa5 | 2009-06-16 13:23:06 +0000 | [diff] [blame] | 147 | {"text/html", 0, TARGET_HTML}, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 148 | {"UTF8_STRING", 0, TARGET_UTF8_STRING}, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 149 | {"STRING", 0, TARGET_STRING}, |
| 150 | {"text/plain", 0, TARGET_TEXT_PLAIN} |
| 151 | }; |
| 152 | # define N_DND_TARGETS (sizeof(dnd_targets) / sizeof(dnd_targets[0])) |
| 153 | #endif |
| 154 | |
| 155 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 156 | /* |
| 157 | * "Monospace" is a standard font alias that should be present |
| 158 | * on all proper Pango/fontconfig installations. |
| 159 | */ |
| 160 | # define DEFAULT_FONT "Monospace 10" |
| 161 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 162 | #if !(defined(FEAT_GUI_GNOME) && defined(FEAT_SESSION)) |
| 163 | /* |
| 164 | * Atoms used to communicate save-yourself from the X11 session manager. There |
| 165 | * is no need to move them into the GUI struct, since they should be constant. |
| 166 | */ |
| 167 | static GdkAtom wm_protocols_atom = GDK_NONE; |
| 168 | static GdkAtom save_yourself_atom = GDK_NONE; |
| 169 | #endif |
| 170 | |
| 171 | /* |
| 172 | * Atoms used to control/reference X11 selections. |
| 173 | */ |
Bram Moolenaar | 3a6eaa5 | 2009-06-16 13:23:06 +0000 | [diff] [blame] | 174 | static GdkAtom html_atom = GDK_NONE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 175 | static GdkAtom utf8_string_atom = GDK_NONE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 176 | static GdkAtom vim_atom = GDK_NONE; /* Vim's own special selection format */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 177 | static GdkAtom vimenc_atom = GDK_NONE; /* Vim's extended selection format */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 178 | |
| 179 | /* |
| 180 | * Keycodes recognized by vim. |
| 181 | * NOTE: when changing this, the table in gui_x11.c probably needs the same |
| 182 | * change! |
| 183 | */ |
| 184 | static struct special_key |
| 185 | { |
| 186 | guint key_sym; |
| 187 | char_u code0; |
| 188 | char_u code1; |
| 189 | } |
| 190 | const special_keys[] = |
| 191 | { |
| 192 | {GDK_Up, 'k', 'u'}, |
| 193 | {GDK_Down, 'k', 'd'}, |
| 194 | {GDK_Left, 'k', 'l'}, |
| 195 | {GDK_Right, 'k', 'r'}, |
| 196 | {GDK_F1, 'k', '1'}, |
| 197 | {GDK_F2, 'k', '2'}, |
| 198 | {GDK_F3, 'k', '3'}, |
| 199 | {GDK_F4, 'k', '4'}, |
| 200 | {GDK_F5, 'k', '5'}, |
| 201 | {GDK_F6, 'k', '6'}, |
| 202 | {GDK_F7, 'k', '7'}, |
| 203 | {GDK_F8, 'k', '8'}, |
| 204 | {GDK_F9, 'k', '9'}, |
| 205 | {GDK_F10, 'k', ';'}, |
| 206 | {GDK_F11, 'F', '1'}, |
| 207 | {GDK_F12, 'F', '2'}, |
| 208 | {GDK_F13, 'F', '3'}, |
| 209 | {GDK_F14, 'F', '4'}, |
| 210 | {GDK_F15, 'F', '5'}, |
| 211 | {GDK_F16, 'F', '6'}, |
| 212 | {GDK_F17, 'F', '7'}, |
| 213 | {GDK_F18, 'F', '8'}, |
| 214 | {GDK_F19, 'F', '9'}, |
| 215 | {GDK_F20, 'F', 'A'}, |
| 216 | {GDK_F21, 'F', 'B'}, |
| 217 | {GDK_Pause, 'F', 'B'}, /* Pause == F21 according to netbeans.txt */ |
| 218 | {GDK_F22, 'F', 'C'}, |
| 219 | {GDK_F23, 'F', 'D'}, |
| 220 | {GDK_F24, 'F', 'E'}, |
| 221 | {GDK_F25, 'F', 'F'}, |
| 222 | {GDK_F26, 'F', 'G'}, |
| 223 | {GDK_F27, 'F', 'H'}, |
| 224 | {GDK_F28, 'F', 'I'}, |
| 225 | {GDK_F29, 'F', 'J'}, |
| 226 | {GDK_F30, 'F', 'K'}, |
| 227 | {GDK_F31, 'F', 'L'}, |
| 228 | {GDK_F32, 'F', 'M'}, |
| 229 | {GDK_F33, 'F', 'N'}, |
| 230 | {GDK_F34, 'F', 'O'}, |
| 231 | {GDK_F35, 'F', 'P'}, |
| 232 | #ifdef SunXK_F36 |
| 233 | {SunXK_F36, 'F', 'Q'}, |
| 234 | {SunXK_F37, 'F', 'R'}, |
| 235 | #endif |
| 236 | {GDK_Help, '%', '1'}, |
| 237 | {GDK_Undo, '&', '8'}, |
| 238 | {GDK_BackSpace, 'k', 'b'}, |
| 239 | {GDK_Insert, 'k', 'I'}, |
| 240 | {GDK_Delete, 'k', 'D'}, |
| 241 | {GDK_3270_BackTab, 'k', 'B'}, |
| 242 | {GDK_Clear, 'k', 'C'}, |
| 243 | {GDK_Home, 'k', 'h'}, |
| 244 | {GDK_End, '@', '7'}, |
| 245 | {GDK_Prior, 'k', 'P'}, |
| 246 | {GDK_Next, 'k', 'N'}, |
| 247 | {GDK_Print, '%', '9'}, |
| 248 | /* Keypad keys: */ |
| 249 | {GDK_KP_Left, 'k', 'l'}, |
| 250 | {GDK_KP_Right, 'k', 'r'}, |
| 251 | {GDK_KP_Up, 'k', 'u'}, |
| 252 | {GDK_KP_Down, 'k', 'd'}, |
| 253 | {GDK_KP_Insert, KS_EXTRA, (char_u)KE_KINS}, |
| 254 | {GDK_KP_Delete, KS_EXTRA, (char_u)KE_KDEL}, |
| 255 | {GDK_KP_Home, 'K', '1'}, |
| 256 | {GDK_KP_End, 'K', '4'}, |
| 257 | {GDK_KP_Prior, 'K', '3'}, /* page up */ |
| 258 | {GDK_KP_Next, 'K', '5'}, /* page down */ |
| 259 | |
| 260 | {GDK_KP_Add, 'K', '6'}, |
| 261 | {GDK_KP_Subtract, 'K', '7'}, |
| 262 | {GDK_KP_Divide, 'K', '8'}, |
| 263 | {GDK_KP_Multiply, 'K', '9'}, |
| 264 | {GDK_KP_Enter, 'K', 'A'}, |
| 265 | {GDK_KP_Decimal, 'K', 'B'}, |
| 266 | |
| 267 | {GDK_KP_0, 'K', 'C'}, |
| 268 | {GDK_KP_1, 'K', 'D'}, |
| 269 | {GDK_KP_2, 'K', 'E'}, |
| 270 | {GDK_KP_3, 'K', 'F'}, |
| 271 | {GDK_KP_4, 'K', 'G'}, |
| 272 | {GDK_KP_5, 'K', 'H'}, |
| 273 | {GDK_KP_6, 'K', 'I'}, |
| 274 | {GDK_KP_7, 'K', 'J'}, |
| 275 | {GDK_KP_8, 'K', 'K'}, |
| 276 | {GDK_KP_9, 'K', 'L'}, |
| 277 | |
| 278 | /* End of list marker: */ |
| 279 | {0, 0, 0} |
| 280 | }; |
| 281 | |
| 282 | /* |
| 283 | * Flags for command line options table below. |
| 284 | */ |
| 285 | #define ARG_FONT 1 |
| 286 | #define ARG_GEOMETRY 2 |
| 287 | #define ARG_REVERSE 3 |
| 288 | #define ARG_NOREVERSE 4 |
| 289 | #define ARG_BACKGROUND 5 |
| 290 | #define ARG_FOREGROUND 6 |
| 291 | #define ARG_ICONIC 7 |
| 292 | #define ARG_ROLE 8 |
| 293 | #define ARG_NETBEANS 9 |
| 294 | #define ARG_XRM 10 /* ignored */ |
| 295 | #define ARG_MENUFONT 11 /* ignored */ |
| 296 | #define ARG_INDEX_MASK 0x00ff |
| 297 | #define ARG_HAS_VALUE 0x0100 /* a value is expected after the argument */ |
| 298 | #define ARG_NEEDS_GUI 0x0200 /* need to initialize the GUI for this */ |
| 299 | #define ARG_FOR_GTK 0x0400 /* argument is handled by GTK+ or GNOME */ |
| 300 | #define ARG_COMPAT_LONG 0x0800 /* accept -foo but substitute with --foo */ |
| 301 | #define ARG_KEEP 0x1000 /* don't remove argument from argv[] */ |
| 302 | |
| 303 | /* |
| 304 | * This table holds all the X GUI command line options allowed. This includes |
| 305 | * the standard ones so that we can skip them when Vim is started without the |
| 306 | * GUI (but the GUI might start up later). |
| 307 | * |
| 308 | * When changing this, also update doc/gui_x11.txt and the usage message!!! |
| 309 | */ |
| 310 | typedef struct |
| 311 | { |
| 312 | const char *name; |
| 313 | unsigned int flags; |
| 314 | } |
| 315 | cmdline_option_T; |
| 316 | |
| 317 | static const cmdline_option_T cmdline_options[] = |
| 318 | { |
| 319 | /* We handle these options ourselves */ |
| 320 | {"-fn", ARG_FONT|ARG_HAS_VALUE}, |
| 321 | {"-font", ARG_FONT|ARG_HAS_VALUE}, |
| 322 | {"-geom", ARG_GEOMETRY|ARG_HAS_VALUE}, |
| 323 | {"-geometry", ARG_GEOMETRY|ARG_HAS_VALUE}, |
| 324 | {"-rv", ARG_REVERSE}, |
| 325 | {"-reverse", ARG_REVERSE}, |
| 326 | {"+rv", ARG_NOREVERSE}, |
| 327 | {"+reverse", ARG_NOREVERSE}, |
| 328 | {"-bg", ARG_BACKGROUND|ARG_HAS_VALUE}, |
| 329 | {"-background", ARG_BACKGROUND|ARG_HAS_VALUE}, |
| 330 | {"-fg", ARG_FOREGROUND|ARG_HAS_VALUE}, |
| 331 | {"-foreground", ARG_FOREGROUND|ARG_HAS_VALUE}, |
| 332 | {"-iconic", ARG_ICONIC}, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 333 | {"--role", ARG_ROLE|ARG_HAS_VALUE}, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 334 | #ifdef FEAT_NETBEANS_INTG |
| 335 | {"-nb", ARG_NETBEANS}, /* non-standard value format */ |
| 336 | {"-xrm", ARG_XRM|ARG_HAS_VALUE}, /* not implemented */ |
| 337 | {"-mf", ARG_MENUFONT|ARG_HAS_VALUE}, /* not implemented */ |
| 338 | {"-menufont", ARG_MENUFONT|ARG_HAS_VALUE}, /* not implemented */ |
| 339 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 340 | /* Arguments handled by GTK (and GNOME) internally. */ |
| 341 | {"--g-fatal-warnings", ARG_FOR_GTK}, |
| 342 | {"--gdk-debug", ARG_FOR_GTK|ARG_HAS_VALUE}, |
| 343 | {"--gdk-no-debug", ARG_FOR_GTK|ARG_HAS_VALUE}, |
| 344 | {"--gtk-debug", ARG_FOR_GTK|ARG_HAS_VALUE}, |
| 345 | {"--gtk-no-debug", ARG_FOR_GTK|ARG_HAS_VALUE}, |
| 346 | {"--gtk-module", ARG_FOR_GTK|ARG_HAS_VALUE}, |
| 347 | {"--sync", ARG_FOR_GTK}, |
| 348 | {"--display", ARG_FOR_GTK|ARG_HAS_VALUE|ARG_COMPAT_LONG}, |
| 349 | {"--name", ARG_FOR_GTK|ARG_HAS_VALUE|ARG_COMPAT_LONG}, |
| 350 | {"--class", ARG_FOR_GTK|ARG_HAS_VALUE|ARG_COMPAT_LONG}, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 351 | {"--screen", ARG_FOR_GTK|ARG_HAS_VALUE}, |
| 352 | {"--gxid-host", ARG_FOR_GTK|ARG_HAS_VALUE}, |
| 353 | {"--gxid-port", ARG_FOR_GTK|ARG_HAS_VALUE}, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 354 | #ifdef FEAT_GUI_GNOME |
| 355 | {"--load-modules", ARG_FOR_GTK|ARG_HAS_VALUE}, |
| 356 | {"--sm-client-id", ARG_FOR_GTK|ARG_HAS_VALUE}, |
| 357 | {"--sm-config-prefix", ARG_FOR_GTK|ARG_HAS_VALUE}, |
| 358 | {"--sm-disable", ARG_FOR_GTK}, |
| 359 | {"--oaf-ior-fd", ARG_FOR_GTK|ARG_HAS_VALUE}, |
| 360 | {"--oaf-activate-iid", ARG_FOR_GTK|ARG_HAS_VALUE}, |
| 361 | {"--oaf-private", ARG_FOR_GTK}, |
| 362 | {"--enable-sound", ARG_FOR_GTK}, |
| 363 | {"--disable-sound", ARG_FOR_GTK}, |
| 364 | {"--espeaker", ARG_FOR_GTK|ARG_HAS_VALUE}, |
| 365 | {"-?", ARG_FOR_GTK|ARG_NEEDS_GUI}, |
| 366 | {"--help", ARG_FOR_GTK|ARG_NEEDS_GUI|ARG_KEEP}, |
| 367 | {"--usage", ARG_FOR_GTK|ARG_NEEDS_GUI}, |
| 368 | # if 0 /* conflicts with Vim's own --version argument */ |
| 369 | {"--version", ARG_FOR_GTK|ARG_NEEDS_GUI}, |
| 370 | # endif |
| 371 | {"--disable-crash-dialog", ARG_FOR_GTK}, |
| 372 | #endif |
| 373 | {NULL, 0} |
| 374 | }; |
| 375 | |
| 376 | static int gui_argc = 0; |
| 377 | static char **gui_argv = NULL; |
| 378 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 379 | static const char *role_argument = NULL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 380 | #if defined(FEAT_GUI_GNOME) && defined(FEAT_SESSION) |
| 381 | static const char *restart_command = NULL; |
Bram Moolenaar | 9085f80 | 2009-06-03 14:20:21 +0000 | [diff] [blame] | 382 | static char *abs_restart_command = NULL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 383 | #endif |
| 384 | static int found_iconic_arg = FALSE; |
| 385 | |
| 386 | #ifdef FEAT_GUI_GNOME |
| 387 | /* |
| 388 | * Can't use Gnome if --socketid given |
| 389 | */ |
| 390 | static int using_gnome = 0; |
| 391 | #else |
| 392 | # define using_gnome 0 |
| 393 | #endif |
| 394 | |
| 395 | /* |
| 396 | * Parse the GUI related command-line arguments. Any arguments used are |
| 397 | * deleted from argv, and *argc is decremented accordingly. This is called |
| 398 | * when vim is started, whether or not the GUI has been started. |
| 399 | */ |
| 400 | void |
| 401 | gui_mch_prepare(int *argc, char **argv) |
| 402 | { |
| 403 | const cmdline_option_T *option; |
| 404 | int i = 0; |
| 405 | int len = 0; |
| 406 | |
| 407 | #if defined(FEAT_GUI_GNOME) && defined(FEAT_SESSION) |
| 408 | /* |
| 409 | * Determine the command used to invoke Vim, to be passed as restart |
| 410 | * command to the session manager. If argv[0] contains any directory |
| 411 | * components try building an absolute path, otherwise leave it as is. |
| 412 | */ |
| 413 | restart_command = argv[0]; |
| 414 | |
| 415 | if (strchr(argv[0], G_DIR_SEPARATOR) != NULL) |
| 416 | { |
| 417 | char_u buf[MAXPATHL]; |
| 418 | |
| 419 | 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] | 420 | { |
| 421 | abs_restart_command = (char *)vim_strsave(buf); |
| 422 | restart_command = abs_restart_command; |
| 423 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 424 | } |
| 425 | #endif |
| 426 | |
| 427 | /* |
| 428 | * Move all the entries in argv which are relevant to GTK+ and GNOME |
| 429 | * into gui_argv. Freed later in gui_mch_init(). |
| 430 | */ |
| 431 | gui_argc = 0; |
| 432 | gui_argv = (char **)alloc((unsigned)((*argc + 1) * sizeof(char *))); |
| 433 | |
| 434 | g_return_if_fail(gui_argv != NULL); |
| 435 | |
| 436 | gui_argv[gui_argc++] = argv[i++]; |
| 437 | |
| 438 | while (i < *argc) |
| 439 | { |
| 440 | /* Don't waste CPU cycles on non-option arguments. */ |
| 441 | if (argv[i][0] != '-' && argv[i][0] != '+') |
| 442 | { |
| 443 | ++i; |
| 444 | continue; |
| 445 | } |
| 446 | |
| 447 | /* Look for argv[i] in cmdline_options[] table. */ |
| 448 | for (option = &cmdline_options[0]; option->name != NULL; ++option) |
| 449 | { |
| 450 | len = strlen(option->name); |
| 451 | |
| 452 | if (strncmp(argv[i], option->name, len) == 0) |
| 453 | { |
| 454 | if (argv[i][len] == '\0') |
| 455 | break; |
| 456 | /* allow --foo=bar style */ |
| 457 | if (argv[i][len] == '=' && (option->flags & ARG_HAS_VALUE)) |
| 458 | break; |
| 459 | #ifdef FEAT_NETBEANS_INTG |
| 460 | /* darn, -nb has non-standard syntax */ |
| 461 | if (vim_strchr((char_u *)":=", argv[i][len]) != NULL |
| 462 | && (option->flags & ARG_INDEX_MASK) == ARG_NETBEANS) |
| 463 | break; |
| 464 | #endif |
| 465 | } |
| 466 | else if ((option->flags & ARG_COMPAT_LONG) |
| 467 | && strcmp(argv[i], option->name + 1) == 0) |
| 468 | { |
| 469 | /* Replace the standard X arguments "-name" and "-display" |
| 470 | * with their GNU-style long option counterparts. */ |
| 471 | argv[i] = (char *)option->name; |
| 472 | break; |
| 473 | } |
| 474 | } |
| 475 | if (option->name == NULL) /* no match */ |
| 476 | { |
| 477 | ++i; |
| 478 | continue; |
| 479 | } |
| 480 | |
| 481 | if (option->flags & ARG_FOR_GTK) |
| 482 | { |
| 483 | /* Move the argument into gui_argv, which |
| 484 | * will later be passed to gtk_init_check() */ |
| 485 | gui_argv[gui_argc++] = argv[i]; |
| 486 | } |
| 487 | else |
| 488 | { |
| 489 | char *value = NULL; |
| 490 | |
| 491 | /* Extract the option's value if there is one. |
| 492 | * Accept both "--foo bar" and "--foo=bar" style. */ |
| 493 | if (option->flags & ARG_HAS_VALUE) |
| 494 | { |
| 495 | if (argv[i][len] == '=') |
| 496 | value = &argv[i][len + 1]; |
| 497 | else if (i + 1 < *argc && strcmp(argv[i + 1], "--") != 0) |
| 498 | value = argv[i + 1]; |
| 499 | } |
| 500 | |
| 501 | /* Check for options handled by Vim itself */ |
| 502 | switch (option->flags & ARG_INDEX_MASK) |
| 503 | { |
| 504 | case ARG_REVERSE: |
| 505 | found_reverse_arg = TRUE; |
| 506 | break; |
| 507 | case ARG_NOREVERSE: |
| 508 | found_reverse_arg = FALSE; |
| 509 | break; |
| 510 | case ARG_FONT: |
| 511 | font_argument = value; |
| 512 | break; |
| 513 | case ARG_GEOMETRY: |
| 514 | if (value != NULL) |
| 515 | gui.geom = vim_strsave((char_u *)value); |
| 516 | break; |
| 517 | case ARG_BACKGROUND: |
| 518 | background_argument = value; |
| 519 | break; |
| 520 | case ARG_FOREGROUND: |
| 521 | foreground_argument = value; |
| 522 | break; |
| 523 | case ARG_ICONIC: |
| 524 | found_iconic_arg = TRUE; |
| 525 | break; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 526 | case ARG_ROLE: |
| 527 | role_argument = value; /* used later in gui_mch_open() */ |
| 528 | break; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 529 | #ifdef FEAT_NETBEANS_INTG |
| 530 | case ARG_NETBEANS: |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 531 | gui.dofork = FALSE; /* don't fork() when starting GUI */ |
| 532 | netbeansArg = argv[i]; |
| 533 | break; |
| 534 | #endif |
| 535 | default: |
| 536 | break; |
| 537 | } |
| 538 | } |
| 539 | |
| 540 | /* These arguments make gnome_program_init() print a message and exit. |
Bram Moolenaar | 717e196 | 2016-08-10 21:28:44 +0200 | [diff] [blame] | 541 | * Must start the GUI for this, otherwise ":gui" will exit later! |
| 542 | * Only when the GUI can start. */ |
| 543 | if ((option->flags & ARG_NEEDS_GUI) |
| 544 | && gui_mch_early_init_check(FALSE) == OK) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 545 | gui.starting = TRUE; |
| 546 | |
| 547 | if (option->flags & ARG_KEEP) |
| 548 | ++i; |
| 549 | else |
| 550 | { |
| 551 | /* Remove the flag from the argument vector. */ |
| 552 | if (--*argc > i) |
| 553 | { |
| 554 | int n_strip = 1; |
| 555 | |
| 556 | /* Move the argument's value as well, if there is one. */ |
| 557 | if ((option->flags & ARG_HAS_VALUE) |
| 558 | && argv[i][len] != '=' |
| 559 | && strcmp(argv[i + 1], "--") != 0) |
| 560 | { |
| 561 | ++n_strip; |
| 562 | --*argc; |
| 563 | if (option->flags & ARG_FOR_GTK) |
| 564 | gui_argv[gui_argc++] = argv[i + 1]; |
| 565 | } |
| 566 | |
| 567 | if (*argc > i) |
| 568 | mch_memmove(&argv[i], &argv[i + n_strip], |
| 569 | (*argc - i) * sizeof(char *)); |
| 570 | } |
| 571 | argv[*argc] = NULL; |
| 572 | } |
| 573 | } |
| 574 | |
| 575 | gui_argv[gui_argc] = NULL; |
| 576 | } |
| 577 | |
Bram Moolenaar | f461c8e | 2005-06-25 23:04:51 +0000 | [diff] [blame] | 578 | #if defined(EXITFREE) || defined(PROTO) |
| 579 | void |
Bram Moolenaar | 66f948e | 2016-01-30 16:39:25 +0100 | [diff] [blame] | 580 | gui_mch_free_all(void) |
Bram Moolenaar | f461c8e | 2005-06-25 23:04:51 +0000 | [diff] [blame] | 581 | { |
| 582 | vim_free(gui_argv); |
Bram Moolenaar | 9085f80 | 2009-06-03 14:20:21 +0000 | [diff] [blame] | 583 | #if defined(FEAT_GUI_GNOME) && defined(FEAT_SESSION) |
| 584 | vim_free(abs_restart_command); |
| 585 | #endif |
Bram Moolenaar | f461c8e | 2005-06-25 23:04:51 +0000 | [diff] [blame] | 586 | } |
| 587 | #endif |
| 588 | |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 589 | #if !GTK_CHECK_VERSION(3,0,0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 590 | /* |
| 591 | * This should be maybe completely removed. |
| 592 | * Doesn't seem possible, since check_copy_area() relies on |
| 593 | * this information. --danielk |
| 594 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 595 | static gint |
Bram Moolenaar | b85cb21 | 2009-05-17 14:24:23 +0000 | [diff] [blame] | 596 | visibility_event(GtkWidget *widget UNUSED, |
| 597 | GdkEventVisibility *event, |
| 598 | gpointer data UNUSED) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 599 | { |
| 600 | gui.visibility = event->state; |
| 601 | /* |
| 602 | * When we do an gdk_window_copy_area(), and the window is partially |
| 603 | * obscured, we want to receive an event to tell us whether it worked |
| 604 | * or not. |
| 605 | */ |
| 606 | if (gui.text_gc != NULL) |
| 607 | gdk_gc_set_exposures(gui.text_gc, |
| 608 | gui.visibility != GDK_VISIBILITY_UNOBSCURED); |
| 609 | return FALSE; |
| 610 | } |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 611 | #endif /* !GTK_CHECK_VERSION(3,0,0) */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 612 | |
| 613 | /* |
| 614 | * Redraw the corresponding portions of the screen. |
| 615 | */ |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 616 | #if GTK_CHECK_VERSION(3,0,0) |
| 617 | static gboolean is_key_pressed = FALSE; |
Bram Moolenaar | 0ecbe33 | 2016-03-05 22:40:52 +0100 | [diff] [blame] | 618 | static gboolean blink_mode = TRUE; |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 619 | |
| 620 | static gboolean gui_gtk_is_blink_on(void); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 621 | static void gui_gtk_window_clear(GdkWindow *win); |
| 622 | |
| 623 | static void |
| 624 | gui_gtk3_redraw(int x, int y, int width, int height) |
| 625 | { |
Bram Moolenaar | fe344a9 | 2017-02-23 12:20:35 +0100 | [diff] [blame] | 626 | /* Range checks are left to gui_redraw_block() */ |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 627 | gui_redraw_block(Y_2_ROW(y), X_2_COL(x), |
| 628 | Y_2_ROW(y + height - 1), X_2_COL(x + width - 1), |
| 629 | GUI_MON_NOCLEAR); |
| 630 | } |
| 631 | |
| 632 | static void |
| 633 | gui_gtk3_update_cursor(cairo_t *cr) |
| 634 | { |
| 635 | if (gui.row == gui.cursor_row) |
| 636 | { |
| 637 | gui.by_signal = TRUE; |
Bram Moolenaar | 4fc563b | 2016-03-12 12:40:58 +0100 | [diff] [blame] | 638 | if (State & CMDLINE) |
| 639 | gui_update_cursor(TRUE, FALSE); |
| 640 | else |
| 641 | gui_update_cursor(TRUE, TRUE); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 642 | gui.by_signal = FALSE; |
| 643 | cairo_paint(cr); |
| 644 | } |
| 645 | } |
| 646 | |
| 647 | static gboolean |
| 648 | gui_gtk3_should_draw_cursor(void) |
| 649 | { |
| 650 | unsigned int cond = 0; |
| 651 | cond |= gui_gtk_is_blink_on(); |
Bram Moolenaar | b4ebf9a | 2016-03-12 16:28:18 +0100 | [diff] [blame] | 652 | if (gui.cursor_col >= gui.col) |
| 653 | cond |= is_key_pressed; |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 654 | cond |= gui.in_focus == FALSE; |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 655 | return cond; |
| 656 | } |
| 657 | |
| 658 | static gboolean |
Bram Moolenaar | 8e31fd5 | 2016-06-04 22:18:13 +0200 | [diff] [blame] | 659 | draw_event(GtkWidget *widget UNUSED, |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 660 | cairo_t *cr, |
| 661 | gpointer user_data UNUSED) |
| 662 | { |
| 663 | /* Skip this when the GUI isn't set up yet, will redraw later. */ |
| 664 | if (gui.starting) |
| 665 | return FALSE; |
| 666 | |
| 667 | out_flush(); /* make sure all output has been processed */ |
| 668 | /* for GTK+ 3, may induce other draw events. */ |
| 669 | |
| 670 | cairo_set_source_surface(cr, gui.surface, 0, 0); |
| 671 | |
| 672 | /* Draw the window without the cursor. */ |
| 673 | gui.by_signal = TRUE; |
| 674 | { |
| 675 | cairo_rectangle_list_t *list = NULL; |
| 676 | |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 677 | list = cairo_copy_clip_rectangle_list(cr); |
| 678 | if (list->status != CAIRO_STATUS_CLIP_NOT_REPRESENTABLE) |
| 679 | { |
| 680 | int i; |
Bram Moolenaar | fe344a9 | 2017-02-23 12:20:35 +0100 | [diff] [blame] | 681 | |
| 682 | /* First clear all the blocks and then redraw them. Just in case |
| 683 | * some blocks overlap. */ |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 684 | for (i = 0; i < list->num_rectangles; i++) |
| 685 | { |
| 686 | const cairo_rectangle_t rect = list->rectangles[i]; |
Bram Moolenaar | 8e31fd5 | 2016-06-04 22:18:13 +0200 | [diff] [blame] | 687 | |
Bram Moolenaar | fe344a9 | 2017-02-23 12:20:35 +0100 | [diff] [blame] | 688 | gui_mch_clear_block(Y_2_ROW((int)rect.y), 0, |
| 689 | Y_2_ROW((int)(rect.y + rect.height)) - 1, Columns - 1); |
| 690 | } |
| 691 | |
| 692 | for (i = 0; i < list->num_rectangles; i++) |
| 693 | { |
| 694 | const cairo_rectangle_t rect = list->rectangles[i]; |
Bram Moolenaar | 8e31fd5 | 2016-06-04 22:18:13 +0200 | [diff] [blame] | 695 | |
Bram Moolenaar | 0ecbe33 | 2016-03-05 22:40:52 +0100 | [diff] [blame] | 696 | if (blink_mode) |
| 697 | gui_gtk3_redraw(rect.x, rect.y, rect.width, rect.height); |
| 698 | else |
Bram Moolenaar | b4ebf9a | 2016-03-12 16:28:18 +0100 | [diff] [blame] | 699 | { |
| 700 | if (get_real_state() & VISUAL) |
| 701 | gui_gtk3_redraw(rect.x, rect.y, |
| 702 | rect.width, rect.height); |
| 703 | else |
| 704 | gui_redraw(rect.x, rect.y, rect.width, rect.height); |
| 705 | } |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 706 | } |
| 707 | } |
| 708 | cairo_rectangle_list_destroy(list); |
| 709 | |
Bram Moolenaar | b4ebf9a | 2016-03-12 16:28:18 +0100 | [diff] [blame] | 710 | if (get_real_state() & VISUAL) |
| 711 | { |
| 712 | if (gui.cursor_row == gui.row && gui.cursor_col >= gui.col) |
| 713 | gui_update_cursor(TRUE, TRUE); |
| 714 | } |
| 715 | |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 716 | cairo_paint(cr); |
| 717 | } |
| 718 | gui.by_signal = FALSE; |
| 719 | |
| 720 | /* Add the cursor to the window if necessary.*/ |
Bram Moolenaar | b4ebf9a | 2016-03-12 16:28:18 +0100 | [diff] [blame] | 721 | if (gui_gtk3_should_draw_cursor() && blink_mode) |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 722 | gui_gtk3_update_cursor(cr); |
| 723 | |
| 724 | return FALSE; |
| 725 | } |
| 726 | #else /* !GTK_CHECK_VERSION(3,0,0) */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 727 | static gint |
Bram Moolenaar | b85cb21 | 2009-05-17 14:24:23 +0000 | [diff] [blame] | 728 | expose_event(GtkWidget *widget UNUSED, |
| 729 | GdkEventExpose *event, |
| 730 | gpointer data UNUSED) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 731 | { |
| 732 | /* Skip this when the GUI isn't set up yet, will redraw later. */ |
| 733 | if (gui.starting) |
| 734 | return FALSE; |
| 735 | |
| 736 | out_flush(); /* make sure all output has been processed */ |
| 737 | gui_redraw(event->area.x, event->area.y, |
| 738 | event->area.width, event->area.height); |
| 739 | |
| 740 | /* Clear the border areas if needed */ |
| 741 | if (event->area.x < FILL_X(0)) |
| 742 | gdk_window_clear_area(gui.drawarea->window, 0, 0, FILL_X(0), 0); |
| 743 | if (event->area.y < FILL_Y(0)) |
| 744 | gdk_window_clear_area(gui.drawarea->window, 0, 0, 0, FILL_Y(0)); |
| 745 | if (event->area.x > FILL_X(Columns)) |
| 746 | gdk_window_clear_area(gui.drawarea->window, |
| 747 | FILL_X((int)Columns), 0, 0, 0); |
| 748 | if (event->area.y > FILL_Y(Rows)) |
| 749 | gdk_window_clear_area(gui.drawarea->window, 0, FILL_Y((int)Rows), 0, 0); |
| 750 | |
| 751 | return FALSE; |
| 752 | } |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 753 | #endif /* !GTK_CHECK_VERSION(3,0,0) */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 754 | |
| 755 | #ifdef FEAT_CLIENTSERVER |
| 756 | /* |
| 757 | * Handle changes to the "Comm" property |
| 758 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 759 | static gint |
Bram Moolenaar | b85cb21 | 2009-05-17 14:24:23 +0000 | [diff] [blame] | 760 | property_event(GtkWidget *widget, |
| 761 | GdkEventProperty *event, |
| 762 | gpointer data UNUSED) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 763 | { |
| 764 | if (event->type == GDK_PROPERTY_NOTIFY |
| 765 | && event->state == (int)GDK_PROPERTY_NEW_VALUE |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 766 | # if GTK_CHECK_VERSION(3,0,0) |
| 767 | && GDK_WINDOW_XID(event->window) == commWindow |
| 768 | # else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 769 | && GDK_WINDOW_XWINDOW(event->window) == commWindow |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 770 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 771 | && GET_X_ATOM(event->atom) == commProperty) |
| 772 | { |
| 773 | XEvent xev; |
| 774 | |
| 775 | /* Translate to XLib */ |
| 776 | xev.xproperty.type = PropertyNotify; |
| 777 | xev.xproperty.atom = commProperty; |
| 778 | xev.xproperty.window = commWindow; |
| 779 | xev.xproperty.state = PropertyNewValue; |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 780 | # if GTK_CHECK_VERSION(3,0,0) |
| 781 | serverEventProc(GDK_WINDOW_XDISPLAY(gtk_widget_get_window(widget)), |
| 782 | &xev, 0); |
| 783 | # else |
Bram Moolenaar | 93c88e0 | 2015-09-15 14:12:05 +0200 | [diff] [blame] | 784 | serverEventProc(GDK_WINDOW_XDISPLAY(widget->window), &xev, 0); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 785 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 786 | } |
| 787 | return FALSE; |
| 788 | } |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 789 | #endif /* defined(FEAT_CLIENTSERVER) */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 790 | |
Bram Moolenaar | 7ebf4e1 | 2018-08-07 20:01:40 +0200 | [diff] [blame] | 791 | /* |
| 792 | * Handle changes to the "Xft/DPI" setting |
| 793 | */ |
| 794 | static void |
| 795 | gtk_settings_xft_dpi_changed_cb(GtkSettings *gtk_settings UNUSED, |
| 796 | GParamSpec *pspec UNUSED, |
| 797 | gpointer data UNUSED) |
| 798 | { |
| 799 | // Create a new PangoContext for this screen, and initialize it |
| 800 | // with the current font if necessary. |
| 801 | if (gui.text_context != NULL) |
| 802 | g_object_unref(gui.text_context); |
| 803 | |
| 804 | gui.text_context = gtk_widget_create_pango_context(gui.mainwin); |
| 805 | pango_context_set_base_dir(gui.text_context, PANGO_DIRECTION_LTR); |
| 806 | |
| 807 | if (gui.norm_font != NULL) |
| 808 | { |
| 809 | // force default font |
| 810 | gui_mch_init_font(*p_guifont == NUL ? NULL : p_guifont, FALSE); |
| 811 | gui_set_shellsize(TRUE, FALSE, RESIZE_BOTH); |
| 812 | } |
| 813 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 814 | |
Bram Moolenaar | 4ab7968 | 2017-08-27 14:50:47 +0200 | [diff] [blame] | 815 | #if GTK_CHECK_VERSION(3,0,0) |
| 816 | typedef gboolean timeout_cb_type; |
| 817 | #else |
| 818 | typedef gint timeout_cb_type; |
| 819 | #endif |
| 820 | |
| 821 | /* |
| 822 | * Start a timer that will invoke the specified callback. |
| 823 | * Returns the ID of the timer. |
| 824 | */ |
| 825 | static guint |
| 826 | timeout_add(int time, timeout_cb_type (*callback)(gpointer), int *flagp) |
| 827 | { |
| 828 | #if GTK_CHECK_VERSION(3,0,0) |
| 829 | return g_timeout_add((guint)time, (GSourceFunc)callback, flagp); |
| 830 | #else |
| 831 | return gtk_timeout_add((guint32)time, (GtkFunction)callback, flagp); |
| 832 | #endif |
| 833 | } |
| 834 | |
| 835 | static void |
| 836 | timeout_remove(guint timer) |
| 837 | { |
| 838 | #if GTK_CHECK_VERSION(3,0,0) |
| 839 | g_source_remove(timer); |
| 840 | #else |
| 841 | gtk_timeout_remove(timer); |
| 842 | #endif |
| 843 | } |
| 844 | |
| 845 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 846 | /**************************************************************************** |
| 847 | * Focus handlers: |
| 848 | */ |
| 849 | |
| 850 | |
| 851 | /* |
| 852 | * This is a simple state machine: |
| 853 | * BLINK_NONE not blinking at all |
| 854 | * BLINK_OFF blinking, cursor is not shown |
| 855 | * BLINK_ON blinking, cursor is shown |
| 856 | */ |
| 857 | |
| 858 | #define BLINK_NONE 0 |
| 859 | #define BLINK_OFF 1 |
| 860 | #define BLINK_ON 2 |
| 861 | |
| 862 | static int blink_state = BLINK_NONE; |
| 863 | static long_u blink_waittime = 700; |
| 864 | static long_u blink_ontime = 400; |
| 865 | static long_u blink_offtime = 250; |
| 866 | static guint blink_timer = 0; |
| 867 | |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 868 | #if GTK_CHECK_VERSION(3,0,0) |
| 869 | static gboolean |
| 870 | gui_gtk_is_blink_on(void) |
| 871 | { |
| 872 | return blink_state == BLINK_ON; |
| 873 | } |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 874 | #endif |
| 875 | |
Bram Moolenaar | 703a804 | 2016-06-04 16:24:32 +0200 | [diff] [blame] | 876 | int |
| 877 | gui_mch_is_blinking(void) |
| 878 | { |
| 879 | return blink_state != BLINK_NONE; |
| 880 | } |
| 881 | |
Bram Moolenaar | 9d5d3c9 | 2016-07-07 16:43:02 +0200 | [diff] [blame] | 882 | int |
| 883 | gui_mch_is_blink_off(void) |
| 884 | { |
| 885 | return blink_state == BLINK_OFF; |
| 886 | } |
| 887 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 888 | void |
| 889 | gui_mch_set_blinking(long waittime, long on, long off) |
| 890 | { |
Bram Moolenaar | 0ecbe33 | 2016-03-05 22:40:52 +0100 | [diff] [blame] | 891 | #if GTK_CHECK_VERSION(3,0,0) |
| 892 | if (waittime == 0 || on == 0 || off == 0) |
| 893 | { |
| 894 | blink_mode = FALSE; |
| 895 | |
| 896 | blink_waittime = 700; |
| 897 | blink_ontime = 400; |
| 898 | blink_offtime = 250; |
| 899 | } |
| 900 | else |
| 901 | { |
| 902 | blink_mode = TRUE; |
| 903 | |
| 904 | blink_waittime = waittime; |
| 905 | blink_ontime = on; |
| 906 | blink_offtime = off; |
| 907 | } |
| 908 | #else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 909 | blink_waittime = waittime; |
| 910 | blink_ontime = on; |
| 911 | blink_offtime = off; |
Bram Moolenaar | 0ecbe33 | 2016-03-05 22:40:52 +0100 | [diff] [blame] | 912 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 913 | } |
| 914 | |
| 915 | /* |
| 916 | * Stop the cursor blinking. Show the cursor if it wasn't shown. |
| 917 | */ |
| 918 | void |
Bram Moolenaar | 1dd45fb | 2018-01-31 21:10:01 +0100 | [diff] [blame] | 919 | gui_mch_stop_blink(int may_call_gui_update_cursor) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 920 | { |
| 921 | if (blink_timer) |
| 922 | { |
Bram Moolenaar | 4ab7968 | 2017-08-27 14:50:47 +0200 | [diff] [blame] | 923 | timeout_remove(blink_timer); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 924 | blink_timer = 0; |
| 925 | } |
Bram Moolenaar | 1dd45fb | 2018-01-31 21:10:01 +0100 | [diff] [blame] | 926 | if (blink_state == BLINK_OFF && may_call_gui_update_cursor) |
Bram Moolenaar | da3a77d | 2016-07-10 23:16:09 +0200 | [diff] [blame] | 927 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 928 | gui_update_cursor(TRUE, FALSE); |
Bram Moolenaar | da3a77d | 2016-07-10 23:16:09 +0200 | [diff] [blame] | 929 | gui_mch_flush(); |
| 930 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 931 | blink_state = BLINK_NONE; |
| 932 | } |
| 933 | |
Bram Moolenaar | 4ab7968 | 2017-08-27 14:50:47 +0200 | [diff] [blame] | 934 | static timeout_cb_type |
Bram Moolenaar | b85cb21 | 2009-05-17 14:24:23 +0000 | [diff] [blame] | 935 | blink_cb(gpointer data UNUSED) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 936 | { |
| 937 | if (blink_state == BLINK_ON) |
| 938 | { |
| 939 | gui_undraw_cursor(); |
| 940 | blink_state = BLINK_OFF; |
Bram Moolenaar | 4ab7968 | 2017-08-27 14:50:47 +0200 | [diff] [blame] | 941 | blink_timer = timeout_add(blink_offtime, blink_cb, NULL); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 942 | } |
| 943 | else |
| 944 | { |
| 945 | gui_update_cursor(TRUE, FALSE); |
| 946 | blink_state = BLINK_ON; |
Bram Moolenaar | 4ab7968 | 2017-08-27 14:50:47 +0200 | [diff] [blame] | 947 | blink_timer = timeout_add(blink_ontime, blink_cb, NULL); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 948 | } |
Bram Moolenaar | da3a77d | 2016-07-10 23:16:09 +0200 | [diff] [blame] | 949 | gui_mch_flush(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 950 | |
| 951 | return FALSE; /* don't happen again */ |
| 952 | } |
| 953 | |
| 954 | /* |
| 955 | * Start the cursor blinking. If it was already blinking, this restarts the |
| 956 | * waiting time and shows the cursor. |
| 957 | */ |
| 958 | void |
| 959 | gui_mch_start_blink(void) |
| 960 | { |
| 961 | if (blink_timer) |
Bram Moolenaar | 7bcdb7d | 2014-04-06 21:08:45 +0200 | [diff] [blame] | 962 | { |
Bram Moolenaar | 4ab7968 | 2017-08-27 14:50:47 +0200 | [diff] [blame] | 963 | timeout_remove(blink_timer); |
Bram Moolenaar | 7bcdb7d | 2014-04-06 21:08:45 +0200 | [diff] [blame] | 964 | blink_timer = 0; |
| 965 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 966 | /* Only switch blinking on if none of the times is zero */ |
| 967 | if (blink_waittime && blink_ontime && blink_offtime && gui.in_focus) |
| 968 | { |
Bram Moolenaar | 4ab7968 | 2017-08-27 14:50:47 +0200 | [diff] [blame] | 969 | blink_timer = timeout_add(blink_waittime, blink_cb, NULL); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 970 | blink_state = BLINK_ON; |
| 971 | gui_update_cursor(TRUE, FALSE); |
Bram Moolenaar | da3a77d | 2016-07-10 23:16:09 +0200 | [diff] [blame] | 972 | gui_mch_flush(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 973 | } |
| 974 | } |
| 975 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 976 | static gint |
Bram Moolenaar | b85cb21 | 2009-05-17 14:24:23 +0000 | [diff] [blame] | 977 | enter_notify_event(GtkWidget *widget UNUSED, |
| 978 | GdkEventCrossing *event UNUSED, |
| 979 | gpointer data UNUSED) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 980 | { |
| 981 | if (blink_state == BLINK_NONE) |
| 982 | gui_mch_start_blink(); |
| 983 | |
| 984 | /* make sure keyboard input goes there */ |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 985 | #if GTK_CHECK_VERSION(3,0,0) |
| 986 | if (gtk_socket_id == 0 || !gtk_widget_has_focus(gui.drawarea)) |
| 987 | #else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 988 | if (gtk_socket_id == 0 || !GTK_WIDGET_HAS_FOCUS(gui.drawarea)) |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 989 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 990 | gtk_widget_grab_focus(gui.drawarea); |
| 991 | |
| 992 | return FALSE; |
| 993 | } |
| 994 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 995 | static gint |
Bram Moolenaar | b85cb21 | 2009-05-17 14:24:23 +0000 | [diff] [blame] | 996 | leave_notify_event(GtkWidget *widget UNUSED, |
| 997 | GdkEventCrossing *event UNUSED, |
| 998 | gpointer data UNUSED) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 999 | { |
| 1000 | if (blink_state != BLINK_NONE) |
Bram Moolenaar | 1dd45fb | 2018-01-31 21:10:01 +0100 | [diff] [blame] | 1001 | gui_mch_stop_blink(TRUE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1002 | |
| 1003 | return FALSE; |
| 1004 | } |
| 1005 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1006 | static gint |
Bram Moolenaar | b85cb21 | 2009-05-17 14:24:23 +0000 | [diff] [blame] | 1007 | focus_in_event(GtkWidget *widget, |
| 1008 | GdkEventFocus *event UNUSED, |
| 1009 | gpointer data UNUSED) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1010 | { |
| 1011 | gui_focus_change(TRUE); |
| 1012 | |
| 1013 | if (blink_state == BLINK_NONE) |
| 1014 | gui_mch_start_blink(); |
| 1015 | |
Bram Moolenaar | 9c8791f | 2007-09-05 19:47:23 +0000 | [diff] [blame] | 1016 | /* make sure keyboard input goes to the draw area (if this is focus for a |
| 1017 | * window) */ |
Bram Moolenaar | b3656ed | 2006-03-20 21:59:49 +0000 | [diff] [blame] | 1018 | if (widget != gui.drawarea) |
Bram Moolenaar | c9b4b05 | 2006-04-30 18:54:39 +0000 | [diff] [blame] | 1019 | gtk_widget_grab_focus(gui.drawarea); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1020 | |
| 1021 | return TRUE; |
| 1022 | } |
| 1023 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1024 | static gint |
Bram Moolenaar | b85cb21 | 2009-05-17 14:24:23 +0000 | [diff] [blame] | 1025 | focus_out_event(GtkWidget *widget UNUSED, |
Bram Moolenaar | cc448b3 | 2010-07-14 16:52:17 +0200 | [diff] [blame] | 1026 | GdkEventFocus *event UNUSED, |
Bram Moolenaar | b85cb21 | 2009-05-17 14:24:23 +0000 | [diff] [blame] | 1027 | gpointer data UNUSED) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1028 | { |
| 1029 | gui_focus_change(FALSE); |
| 1030 | |
| 1031 | if (blink_state != BLINK_NONE) |
Bram Moolenaar | 1dd45fb | 2018-01-31 21:10:01 +0100 | [diff] [blame] | 1032 | gui_mch_stop_blink(TRUE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1033 | |
| 1034 | return TRUE; |
| 1035 | } |
| 1036 | |
| 1037 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1038 | /* |
| 1039 | * Translate a GDK key value to UTF-8 independently of the current locale. |
| 1040 | * The output is written to string, which must have room for at least 6 bytes |
| 1041 | * plus the NUL terminator. Returns the length in bytes. |
| 1042 | * |
| 1043 | * This function is used in the GTK+ 2 GUI only. The GTK+ 1 code makes use |
| 1044 | * of GdkEventKey::string instead. But event->string is evil; see here why: |
| 1045 | * http://developer.gnome.org/doc/API/2.0/gdk/gdk-Event-Structures.html#GdkEventKey |
| 1046 | */ |
| 1047 | static int |
| 1048 | keyval_to_string(unsigned int keyval, unsigned int state, char_u *string) |
| 1049 | { |
| 1050 | int len; |
| 1051 | guint32 uc; |
| 1052 | |
| 1053 | uc = gdk_keyval_to_unicode(keyval); |
| 1054 | if (uc != 0) |
| 1055 | { |
| 1056 | /* Check for CTRL-foo */ |
| 1057 | if ((state & GDK_CONTROL_MASK) && uc >= 0x20 && uc < 0x80) |
| 1058 | { |
| 1059 | /* These mappings look arbitrary at the first glance, but in fact |
| 1060 | * resemble quite exactly the behaviour of the GTK+ 1.2 GUI on my |
| 1061 | * machine. The only difference is BS vs. DEL for CTRL-8 (makes |
| 1062 | * more sense and is consistent with usual terminal behaviour). */ |
| 1063 | if (uc >= '@') |
| 1064 | string[0] = uc & 0x1F; |
| 1065 | else if (uc == '2') |
| 1066 | string[0] = NUL; |
| 1067 | else if (uc >= '3' && uc <= '7') |
| 1068 | string[0] = uc ^ 0x28; |
| 1069 | else if (uc == '8') |
| 1070 | string[0] = BS; |
| 1071 | else if (uc == '?') |
| 1072 | string[0] = DEL; |
| 1073 | else |
| 1074 | string[0] = uc; |
| 1075 | len = 1; |
| 1076 | } |
| 1077 | else |
| 1078 | { |
| 1079 | /* Translate a normal key to UTF-8. This doesn't work for dead |
| 1080 | * keys of course, you _have_ to use an input method for that. */ |
| 1081 | len = utf_char2bytes((int)uc, string); |
| 1082 | } |
| 1083 | } |
| 1084 | else |
| 1085 | { |
| 1086 | /* Translate keys which are represented by ASCII control codes in Vim. |
| 1087 | * There are only a few of those; most control keys are translated to |
| 1088 | * special terminal-like control sequences. */ |
| 1089 | len = 1; |
| 1090 | switch (keyval) |
| 1091 | { |
| 1092 | case GDK_Tab: case GDK_KP_Tab: case GDK_ISO_Left_Tab: |
| 1093 | string[0] = TAB; |
| 1094 | break; |
| 1095 | case GDK_Linefeed: |
| 1096 | string[0] = NL; |
| 1097 | break; |
| 1098 | case GDK_Return: case GDK_ISO_Enter: case GDK_3270_Enter: |
| 1099 | string[0] = CAR; |
| 1100 | break; |
| 1101 | case GDK_Escape: |
| 1102 | string[0] = ESC; |
| 1103 | break; |
| 1104 | default: |
| 1105 | len = 0; |
| 1106 | break; |
| 1107 | } |
| 1108 | } |
| 1109 | string[len] = NUL; |
| 1110 | |
| 1111 | return len; |
| 1112 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1113 | |
Bram Moolenaar | 19a09a1 | 2005-03-04 23:39:37 +0000 | [diff] [blame] | 1114 | static int |
| 1115 | modifiers_gdk2vim(guint state) |
| 1116 | { |
| 1117 | int modifiers = 0; |
| 1118 | |
| 1119 | if (state & GDK_SHIFT_MASK) |
| 1120 | modifiers |= MOD_MASK_SHIFT; |
| 1121 | if (state & GDK_CONTROL_MASK) |
| 1122 | modifiers |= MOD_MASK_CTRL; |
| 1123 | if (state & GDK_MOD1_MASK) |
| 1124 | modifiers |= MOD_MASK_ALT; |
Bram Moolenaar | 580061a | 2010-08-13 13:57:13 +0200 | [diff] [blame] | 1125 | #if GTK_CHECK_VERSION(2,10,0) |
Bram Moolenaar | 30bb414 | 2010-05-17 22:07:15 +0200 | [diff] [blame] | 1126 | if (state & GDK_SUPER_MASK) |
| 1127 | modifiers |= MOD_MASK_META; |
| 1128 | #endif |
Bram Moolenaar | 19a09a1 | 2005-03-04 23:39:37 +0000 | [diff] [blame] | 1129 | if (state & GDK_MOD4_MASK) |
| 1130 | modifiers |= MOD_MASK_META; |
| 1131 | |
| 1132 | return modifiers; |
| 1133 | } |
| 1134 | |
| 1135 | static int |
| 1136 | modifiers_gdk2mouse(guint state) |
| 1137 | { |
| 1138 | int modifiers = 0; |
| 1139 | |
| 1140 | if (state & GDK_SHIFT_MASK) |
| 1141 | modifiers |= MOUSE_SHIFT; |
| 1142 | if (state & GDK_CONTROL_MASK) |
| 1143 | modifiers |= MOUSE_CTRL; |
| 1144 | if (state & GDK_MOD1_MASK) |
| 1145 | modifiers |= MOUSE_ALT; |
| 1146 | |
| 1147 | return modifiers; |
| 1148 | } |
| 1149 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1150 | /* |
| 1151 | * Main keyboard handler: |
| 1152 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1153 | static gint |
Bram Moolenaar | b85cb21 | 2009-05-17 14:24:23 +0000 | [diff] [blame] | 1154 | key_press_event(GtkWidget *widget UNUSED, |
| 1155 | GdkEventKey *event, |
| 1156 | gpointer data UNUSED) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1157 | { |
Bram Moolenaar | 182c5be | 2010-06-25 05:37:59 +0200 | [diff] [blame] | 1158 | /* For GTK+ 2 we know for sure how large the string might get. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1159 | * (That is, up to 6 bytes + NUL + CSI escapes + safety measure.) */ |
| 1160 | char_u string[32], string2[32]; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1161 | guint key_sym; |
| 1162 | int len; |
| 1163 | int i; |
| 1164 | int modifiers; |
| 1165 | int key; |
| 1166 | guint state; |
| 1167 | char_u *s, *d; |
| 1168 | |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 1169 | #if GTK_CHECK_VERSION(3,0,0) |
| 1170 | is_key_pressed = TRUE; |
Bram Moolenaar | 1dd45fb | 2018-01-31 21:10:01 +0100 | [diff] [blame] | 1171 | gui_mch_stop_blink(TRUE); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 1172 | #endif |
| 1173 | |
Bram Moolenaar | 20892c1 | 2011-06-26 04:49:00 +0200 | [diff] [blame] | 1174 | gui.event_time = event->time; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1175 | key_sym = event->keyval; |
| 1176 | state = event->state; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1177 | |
| 1178 | #ifdef FEAT_XIM |
| 1179 | if (xim_queue_key_press_event(event, TRUE)) |
| 1180 | return TRUE; |
| 1181 | #endif |
| 1182 | |
| 1183 | #ifdef FEAT_HANGULIN |
| 1184 | if (key_sym == GDK_space && (state & GDK_SHIFT_MASK)) |
| 1185 | { |
| 1186 | hangul_input_state_toggle(); |
| 1187 | return TRUE; |
| 1188 | } |
| 1189 | #endif |
| 1190 | |
| 1191 | #ifdef SunXK_F36 |
| 1192 | /* |
| 1193 | * These keys have bogus lookup strings, and trapping them here is |
| 1194 | * easier than trying to XRebindKeysym() on them with every possible |
| 1195 | * combination of modifiers. |
| 1196 | */ |
| 1197 | if (key_sym == SunXK_F36 || key_sym == SunXK_F37) |
| 1198 | len = 0; |
| 1199 | else |
| 1200 | #endif |
| 1201 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1202 | len = keyval_to_string(key_sym, state, string2); |
| 1203 | |
| 1204 | /* Careful: convert_input() doesn't handle the NUL character. |
| 1205 | * No need to convert pure ASCII anyway, thus the len > 1 check. */ |
| 1206 | if (len > 1 && input_conv.vc_type != CONV_NONE) |
| 1207 | len = convert_input(string2, len, sizeof(string2)); |
| 1208 | |
| 1209 | s = string2; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1210 | d = string; |
| 1211 | for (i = 0; i < len; ++i) |
| 1212 | { |
| 1213 | *d++ = s[i]; |
| 1214 | if (d[-1] == CSI && d + 2 < string + sizeof(string)) |
| 1215 | { |
| 1216 | /* Turn CSI into K_CSI. */ |
| 1217 | *d++ = KS_EXTRA; |
| 1218 | *d++ = (int)KE_CSI; |
| 1219 | } |
| 1220 | } |
| 1221 | len = d - string; |
| 1222 | } |
| 1223 | |
| 1224 | /* Shift-Tab results in Left_Tab, but we want <S-Tab> */ |
| 1225 | if (key_sym == GDK_ISO_Left_Tab) |
| 1226 | { |
| 1227 | key_sym = GDK_Tab; |
| 1228 | state |= GDK_SHIFT_MASK; |
| 1229 | } |
| 1230 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1231 | #ifdef FEAT_MENU |
| 1232 | /* If there is a menu and 'wak' is "yes", or 'wak' is "menu" and the key |
| 1233 | * is a menu shortcut, we ignore everything with the ALT modifier. */ |
| 1234 | if ((state & GDK_MOD1_MASK) |
| 1235 | && gui.menu_is_active |
| 1236 | && (*p_wak == 'y' |
| 1237 | || (*p_wak == 'm' |
| 1238 | && len == 1 |
| 1239 | && gui_is_menu_shortcut(string[0])))) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1240 | /* For GTK2 we return false to signify that we haven't handled the |
| 1241 | * keypress, so that gtk will handle the mnemonic or accelerator. */ |
| 1242 | return FALSE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1243 | #endif |
| 1244 | |
| 1245 | /* Check for Alt/Meta key (Mod1Mask), but not for a BS, DEL or character |
| 1246 | * that already has the 8th bit set. |
| 1247 | * Don't do this for <S-M-Tab>, that should become K_S_TAB with ALT. |
| 1248 | * Don't do this for double-byte encodings, it turns the char into a lead |
| 1249 | * byte. */ |
| 1250 | if (len == 1 |
Bram Moolenaar | 30bb414 | 2010-05-17 22:07:15 +0200 | [diff] [blame] | 1251 | && ((state & GDK_MOD1_MASK) |
Bram Moolenaar | 35f330c | 2010-08-15 13:53:58 +0200 | [diff] [blame] | 1252 | #if GTK_CHECK_VERSION(2,10,0) |
Bram Moolenaar | 30bb414 | 2010-05-17 22:07:15 +0200 | [diff] [blame] | 1253 | || (state & GDK_SUPER_MASK) |
| 1254 | #endif |
| 1255 | ) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1256 | && !(key_sym == GDK_BackSpace || key_sym == GDK_Delete) |
| 1257 | && (string[0] & 0x80) == 0 |
| 1258 | && !(key_sym == GDK_Tab && (state & GDK_SHIFT_MASK)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1259 | && !enc_dbcs |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1260 | ) |
| 1261 | { |
| 1262 | string[0] |= 0x80; |
| 1263 | state &= ~GDK_MOD1_MASK; /* don't use it again */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1264 | if (enc_utf8) /* convert to utf-8 */ |
| 1265 | { |
| 1266 | string[1] = string[0] & 0xbf; |
| 1267 | string[0] = ((unsigned)string[0] >> 6) + 0xc0; |
| 1268 | if (string[1] == CSI) |
| 1269 | { |
| 1270 | string[2] = KS_EXTRA; |
| 1271 | string[3] = (int)KE_CSI; |
| 1272 | len = 4; |
| 1273 | } |
| 1274 | else |
| 1275 | len = 2; |
| 1276 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1277 | } |
| 1278 | |
| 1279 | /* Check for special keys. Also do this when len == 1 (key has an ASCII |
| 1280 | * value) to detect backspace, delete and keypad keys. */ |
| 1281 | if (len == 0 || len == 1) |
| 1282 | { |
| 1283 | for (i = 0; special_keys[i].key_sym != 0; i++) |
| 1284 | { |
| 1285 | if (special_keys[i].key_sym == key_sym) |
| 1286 | { |
| 1287 | string[0] = CSI; |
| 1288 | string[1] = special_keys[i].code0; |
| 1289 | string[2] = special_keys[i].code1; |
| 1290 | len = -3; |
| 1291 | break; |
| 1292 | } |
| 1293 | } |
| 1294 | } |
| 1295 | |
| 1296 | if (len == 0) /* Unrecognized key */ |
| 1297 | return TRUE; |
| 1298 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1299 | /* Special keys (and a few others) may have modifiers. Also when using a |
| 1300 | * double-byte encoding (can't set the 8th bit). */ |
| 1301 | if (len == -3 || key_sym == GDK_space || key_sym == GDK_Tab |
| 1302 | || key_sym == GDK_Return || key_sym == GDK_Linefeed |
| 1303 | || key_sym == GDK_Escape || key_sym == GDK_KP_Tab |
| 1304 | || key_sym == GDK_ISO_Enter || key_sym == GDK_3270_Enter |
Bram Moolenaar | 30bb414 | 2010-05-17 22:07:15 +0200 | [diff] [blame] | 1305 | || (enc_dbcs && len == 1 && ((state & GDK_MOD1_MASK) |
Bram Moolenaar | 35f330c | 2010-08-15 13:53:58 +0200 | [diff] [blame] | 1306 | #if GTK_CHECK_VERSION(2,10,0) |
Bram Moolenaar | 30bb414 | 2010-05-17 22:07:15 +0200 | [diff] [blame] | 1307 | || (state & GDK_SUPER_MASK) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1308 | #endif |
Bram Moolenaar | 182c5be | 2010-06-25 05:37:59 +0200 | [diff] [blame] | 1309 | ))) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1310 | { |
Bram Moolenaar | 19a09a1 | 2005-03-04 23:39:37 +0000 | [diff] [blame] | 1311 | modifiers = modifiers_gdk2vim(state); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1312 | |
| 1313 | /* |
| 1314 | * For some keys a shift modifier is translated into another key |
| 1315 | * code. |
| 1316 | */ |
| 1317 | if (len == -3) |
| 1318 | key = TO_SPECIAL(string[1], string[2]); |
| 1319 | else |
| 1320 | key = string[0]; |
| 1321 | |
| 1322 | key = simplify_key(key, &modifiers); |
| 1323 | if (key == CSI) |
| 1324 | key = K_CSI; |
| 1325 | if (IS_SPECIAL(key)) |
| 1326 | { |
| 1327 | string[0] = CSI; |
| 1328 | string[1] = K_SECOND(key); |
| 1329 | string[2] = K_THIRD(key); |
| 1330 | len = 3; |
| 1331 | } |
| 1332 | else |
| 1333 | { |
| 1334 | string[0] = key; |
| 1335 | len = 1; |
| 1336 | } |
| 1337 | |
| 1338 | if (modifiers != 0) |
| 1339 | { |
| 1340 | string2[0] = CSI; |
| 1341 | string2[1] = KS_MODIFIER; |
| 1342 | string2[2] = modifiers; |
| 1343 | add_to_input_buf(string2, 3); |
| 1344 | } |
| 1345 | } |
| 1346 | |
| 1347 | if (len == 1 && ((string[0] == Ctrl_C && ctrl_c_interrupts) |
| 1348 | || (string[0] == intr_char && intr_char != Ctrl_C))) |
| 1349 | { |
| 1350 | trash_input_buf(); |
| 1351 | got_int = TRUE; |
| 1352 | } |
| 1353 | |
| 1354 | add_to_input_buf(string, len); |
| 1355 | |
| 1356 | /* blank out the pointer if necessary */ |
| 1357 | if (p_mh) |
| 1358 | gui_mch_mousehide(TRUE); |
| 1359 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1360 | return TRUE; |
| 1361 | } |
| 1362 | |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 1363 | #if defined(FEAT_XIM) || GTK_CHECK_VERSION(3,0,0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1364 | static gboolean |
Bram Moolenaar | b85cb21 | 2009-05-17 14:24:23 +0000 | [diff] [blame] | 1365 | key_release_event(GtkWidget *widget UNUSED, |
| 1366 | GdkEventKey *event, |
| 1367 | gpointer data UNUSED) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1368 | { |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 1369 | # if GTK_CHECK_VERSION(3,0,0) |
| 1370 | is_key_pressed = FALSE; |
| 1371 | gui_mch_start_blink(); |
| 1372 | # endif |
| 1373 | # if defined(FEAT_XIM) |
Bram Moolenaar | 20892c1 | 2011-06-26 04:49:00 +0200 | [diff] [blame] | 1374 | gui.event_time = event->time; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1375 | /* |
| 1376 | * GTK+ 2 input methods may do fancy stuff on key release events too. |
| 1377 | * With the default IM for instance, you can enter any UCS code point |
| 1378 | * by holding down CTRL-SHIFT and typing hexadecimal digits. |
| 1379 | */ |
| 1380 | return xim_queue_key_press_event(event, FALSE); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 1381 | # else |
| 1382 | return TRUE; |
| 1383 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1384 | } |
| 1385 | #endif |
| 1386 | |
| 1387 | |
| 1388 | /**************************************************************************** |
| 1389 | * Selection handlers: |
| 1390 | */ |
| 1391 | |
Bram Moolenaar | 29dfa5a | 2018-03-20 21:24:45 +0100 | [diff] [blame] | 1392 | /* Remember when clip_lose_selection was called from here, we must not call |
| 1393 | * gtk_selection_owner_set() then. */ |
| 1394 | static int in_selection_clear_event = FALSE; |
| 1395 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1396 | static gint |
Bram Moolenaar | b85cb21 | 2009-05-17 14:24:23 +0000 | [diff] [blame] | 1397 | selection_clear_event(GtkWidget *widget UNUSED, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1398 | GdkEventSelection *event, |
Bram Moolenaar | b85cb21 | 2009-05-17 14:24:23 +0000 | [diff] [blame] | 1399 | gpointer user_data UNUSED) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1400 | { |
Bram Moolenaar | 29dfa5a | 2018-03-20 21:24:45 +0100 | [diff] [blame] | 1401 | in_selection_clear_event = TRUE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1402 | if (event->selection == clip_plus.gtk_sel_atom) |
| 1403 | clip_lose_selection(&clip_plus); |
| 1404 | else |
| 1405 | clip_lose_selection(&clip_star); |
Bram Moolenaar | 29dfa5a | 2018-03-20 21:24:45 +0100 | [diff] [blame] | 1406 | in_selection_clear_event = FALSE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1407 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1408 | return TRUE; |
| 1409 | } |
| 1410 | |
| 1411 | #define RS_NONE 0 /* selection_received_cb() not called yet */ |
| 1412 | #define RS_OK 1 /* selection_received_cb() called and OK */ |
| 1413 | #define RS_FAIL 2 /* selection_received_cb() called and failed */ |
| 1414 | static int received_selection = RS_NONE; |
| 1415 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1416 | static void |
Bram Moolenaar | b85cb21 | 2009-05-17 14:24:23 +0000 | [diff] [blame] | 1417 | selection_received_cb(GtkWidget *widget UNUSED, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1418 | GtkSelectionData *data, |
Bram Moolenaar | b85cb21 | 2009-05-17 14:24:23 +0000 | [diff] [blame] | 1419 | guint time_ UNUSED, |
| 1420 | gpointer user_data UNUSED) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1421 | { |
| 1422 | VimClipboard *cbd; |
| 1423 | char_u *text; |
| 1424 | char_u *tmpbuf = NULL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1425 | guchar *tmpbuf_utf8 = NULL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1426 | int len; |
Bram Moolenaar | d44347f | 2011-06-19 01:14:29 +0200 | [diff] [blame] | 1427 | int motion_type = MAUTO; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1428 | |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 1429 | #if GTK_CHECK_VERSION(3,0,0) |
| 1430 | if (gtk_selection_data_get_selection(data) == clip_plus.gtk_sel_atom) |
| 1431 | #else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1432 | if (data->selection == clip_plus.gtk_sel_atom) |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 1433 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1434 | cbd = &clip_plus; |
| 1435 | else |
| 1436 | cbd = &clip_star; |
| 1437 | |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 1438 | #if GTK_CHECK_VERSION(3,0,0) |
| 1439 | text = (char_u *)gtk_selection_data_get_data(data); |
| 1440 | len = gtk_selection_data_get_length(data); |
| 1441 | #else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1442 | text = (char_u *)data->data; |
| 1443 | len = data->length; |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 1444 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1445 | |
| 1446 | if (text == NULL || len <= 0) |
| 1447 | { |
| 1448 | received_selection = RS_FAIL; |
| 1449 | /* clip_free_selection(cbd); ??? */ |
| 1450 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1451 | return; |
| 1452 | } |
| 1453 | |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 1454 | #if GTK_CHECK_VERSION(3,0,0) |
| 1455 | if (gtk_selection_data_get_data_type(data) == vim_atom) |
| 1456 | #else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1457 | if (data->type == vim_atom) |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 1458 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1459 | { |
| 1460 | motion_type = *text++; |
| 1461 | --len; |
| 1462 | } |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 1463 | #if GTK_CHECK_VERSION(3,0,0) |
| 1464 | else if (gtk_selection_data_get_data_type(data) == vimenc_atom) |
| 1465 | #else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1466 | else if (data->type == vimenc_atom) |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 1467 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1468 | { |
| 1469 | char_u *enc; |
| 1470 | vimconv_T conv; |
| 1471 | |
| 1472 | motion_type = *text++; |
| 1473 | --len; |
| 1474 | |
| 1475 | enc = text; |
| 1476 | text += STRLEN(text) + 1; |
| 1477 | len -= text - enc; |
| 1478 | |
| 1479 | /* If the encoding of the text is different from 'encoding', attempt |
| 1480 | * converting it. */ |
| 1481 | conv.vc_type = CONV_NONE; |
| 1482 | convert_setup(&conv, enc, p_enc); |
| 1483 | if (conv.vc_type != CONV_NONE) |
| 1484 | { |
| 1485 | tmpbuf = string_convert(&conv, text, &len); |
| 1486 | if (tmpbuf != NULL) |
| 1487 | text = tmpbuf; |
| 1488 | convert_setup(&conv, NULL, NULL); |
| 1489 | } |
| 1490 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1491 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1492 | /* gtk_selection_data_get_text() handles all the nasty details |
| 1493 | * and targets and encodings etc. This rocks so hard. */ |
| 1494 | else |
| 1495 | { |
| 1496 | tmpbuf_utf8 = gtk_selection_data_get_text(data); |
| 1497 | if (tmpbuf_utf8 != NULL) |
| 1498 | { |
| 1499 | len = STRLEN(tmpbuf_utf8); |
| 1500 | if (input_conv.vc_type != CONV_NONE) |
| 1501 | { |
| 1502 | tmpbuf = string_convert(&input_conv, tmpbuf_utf8, &len); |
| 1503 | if (tmpbuf != NULL) |
| 1504 | text = tmpbuf; |
| 1505 | } |
| 1506 | else |
| 1507 | text = tmpbuf_utf8; |
| 1508 | } |
Bram Moolenaar | 3a6eaa5 | 2009-06-16 13:23:06 +0000 | [diff] [blame] | 1509 | else if (len >= 2 && text[0] == 0xff && text[1] == 0xfe) |
| 1510 | { |
| 1511 | vimconv_T conv; |
| 1512 | |
| 1513 | /* UTF-16, we get this for HTML */ |
| 1514 | conv.vc_type = CONV_NONE; |
| 1515 | convert_setup_ext(&conv, (char_u *)"utf-16le", FALSE, p_enc, TRUE); |
| 1516 | |
| 1517 | if (conv.vc_type != CONV_NONE) |
| 1518 | { |
| 1519 | text += 2; |
| 1520 | len -= 2; |
| 1521 | tmpbuf = string_convert(&conv, text, &len); |
| 1522 | convert_setup(&conv, NULL, NULL); |
| 1523 | } |
| 1524 | if (tmpbuf != NULL) |
| 1525 | text = tmpbuf; |
| 1526 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1527 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1528 | |
Bram Moolenaar | 84a05ac | 2013-05-06 04:24:17 +0200 | [diff] [blame] | 1529 | /* Chop off any trailing NUL bytes. OpenOffice sends these. */ |
Bram Moolenaar | a76638f | 2010-06-05 12:49:46 +0200 | [diff] [blame] | 1530 | while (len > 0 && text[len - 1] == NUL) |
| 1531 | --len; |
| 1532 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1533 | clip_yank_selection(motion_type, text, (long)len, cbd); |
| 1534 | received_selection = RS_OK; |
| 1535 | vim_free(tmpbuf); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1536 | g_free(tmpbuf_utf8); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1537 | } |
| 1538 | |
| 1539 | /* |
| 1540 | * Prepare our selection data for passing it to the external selection |
| 1541 | * client. |
| 1542 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1543 | static void |
Bram Moolenaar | b85cb21 | 2009-05-17 14:24:23 +0000 | [diff] [blame] | 1544 | selection_get_cb(GtkWidget *widget UNUSED, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1545 | GtkSelectionData *selection_data, |
| 1546 | guint info, |
Bram Moolenaar | b85cb21 | 2009-05-17 14:24:23 +0000 | [diff] [blame] | 1547 | guint time_ UNUSED, |
| 1548 | gpointer user_data UNUSED) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1549 | { |
| 1550 | char_u *string; |
| 1551 | char_u *tmpbuf; |
| 1552 | long_u tmplen; |
| 1553 | int length; |
| 1554 | int motion_type; |
| 1555 | GdkAtom type; |
| 1556 | VimClipboard *cbd; |
| 1557 | |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 1558 | #if GTK_CHECK_VERSION(3,0,0) |
| 1559 | if (gtk_selection_data_get_selection(selection_data) |
| 1560 | == clip_plus.gtk_sel_atom) |
| 1561 | #else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1562 | if (selection_data->selection == clip_plus.gtk_sel_atom) |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 1563 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1564 | cbd = &clip_plus; |
| 1565 | else |
| 1566 | cbd = &clip_star; |
| 1567 | |
| 1568 | if (!cbd->owned) |
| 1569 | return; /* Shouldn't ever happen */ |
| 1570 | |
| 1571 | if (info != (guint)TARGET_STRING |
Bram Moolenaar | 3a6eaa5 | 2009-06-16 13:23:06 +0000 | [diff] [blame] | 1572 | && (!clip_html || info != (guint)TARGET_HTML) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1573 | && info != (guint)TARGET_UTF8_STRING |
| 1574 | && info != (guint)TARGET_VIMENC |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1575 | && info != (guint)TARGET_VIM |
| 1576 | && info != (guint)TARGET_COMPOUND_TEXT |
| 1577 | && info != (guint)TARGET_TEXT) |
| 1578 | return; |
| 1579 | |
| 1580 | /* get the selection from the '*'/'+' register */ |
| 1581 | clip_get_selection(cbd); |
| 1582 | |
| 1583 | motion_type = clip_convert_selection(&string, &tmplen, cbd); |
| 1584 | if (motion_type < 0 || string == NULL) |
| 1585 | return; |
| 1586 | /* Due to int arguments we can't handle more than G_MAXINT. Also |
| 1587 | * reserve one extra byte for NUL or the motion type; just in case. |
| 1588 | * (Not that pasting 2G of text is ever going to work, but... ;-) */ |
| 1589 | length = MIN(tmplen, (long_u)(G_MAXINT - 1)); |
| 1590 | |
| 1591 | if (info == (guint)TARGET_VIM) |
| 1592 | { |
| 1593 | tmpbuf = alloc((unsigned)length + 1); |
| 1594 | if (tmpbuf != NULL) |
| 1595 | { |
| 1596 | tmpbuf[0] = motion_type; |
| 1597 | mch_memmove(tmpbuf + 1, string, (size_t)length); |
| 1598 | } |
| 1599 | /* For our own format, the first byte contains the motion type */ |
| 1600 | ++length; |
| 1601 | vim_free(string); |
| 1602 | string = tmpbuf; |
| 1603 | type = vim_atom; |
| 1604 | } |
| 1605 | |
Bram Moolenaar | 3a6eaa5 | 2009-06-16 13:23:06 +0000 | [diff] [blame] | 1606 | else if (info == (guint)TARGET_HTML) |
| 1607 | { |
| 1608 | vimconv_T conv; |
| 1609 | |
| 1610 | /* Since we get utf-16, we probably should set it as well. */ |
| 1611 | conv.vc_type = CONV_NONE; |
| 1612 | convert_setup_ext(&conv, p_enc, TRUE, (char_u *)"utf-16le", FALSE); |
| 1613 | if (conv.vc_type != CONV_NONE) |
| 1614 | { |
| 1615 | tmpbuf = string_convert(&conv, string, &length); |
| 1616 | convert_setup(&conv, NULL, NULL); |
| 1617 | vim_free(string); |
| 1618 | string = tmpbuf; |
| 1619 | } |
| 1620 | |
| 1621 | /* Prepend the BOM: "fffe" */ |
| 1622 | if (string != NULL) |
| 1623 | { |
| 1624 | tmpbuf = alloc(length + 2); |
| 1625 | tmpbuf[0] = 0xff; |
| 1626 | tmpbuf[1] = 0xfe; |
| 1627 | mch_memmove(tmpbuf + 2, string, (size_t)length); |
| 1628 | vim_free(string); |
| 1629 | string = tmpbuf; |
| 1630 | length += 2; |
| 1631 | |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 1632 | #if !GTK_CHECK_VERSION(3,0,0) |
Bram Moolenaar | 3f2a5d8 | 2016-02-27 22:08:16 +0100 | [diff] [blame] | 1633 | /* Looks redundant even for GTK2 because these values are |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 1634 | * overwritten by gtk_selection_data_set() that follows. */ |
Bram Moolenaar | 3a6eaa5 | 2009-06-16 13:23:06 +0000 | [diff] [blame] | 1635 | selection_data->type = selection_data->target; |
| 1636 | selection_data->format = 16; /* 16 bits per char */ |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 1637 | #endif |
Bram Moolenaar | 3a6eaa5 | 2009-06-16 13:23:06 +0000 | [diff] [blame] | 1638 | gtk_selection_data_set(selection_data, html_atom, 16, |
| 1639 | string, length); |
| 1640 | vim_free(string); |
| 1641 | } |
| 1642 | return; |
| 1643 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1644 | else if (info == (guint)TARGET_VIMENC) |
| 1645 | { |
| 1646 | int l = STRLEN(p_enc); |
| 1647 | |
| 1648 | /* contents: motion_type 'encoding' NUL text */ |
| 1649 | tmpbuf = alloc((unsigned)length + l + 2); |
| 1650 | if (tmpbuf != NULL) |
| 1651 | { |
| 1652 | tmpbuf[0] = motion_type; |
| 1653 | STRCPY(tmpbuf + 1, p_enc); |
| 1654 | mch_memmove(tmpbuf + l + 2, string, (size_t)length); |
| 1655 | } |
| 1656 | length += l + 2; |
| 1657 | vim_free(string); |
| 1658 | string = tmpbuf; |
| 1659 | type = vimenc_atom; |
| 1660 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1661 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1662 | /* gtk_selection_data_set_text() handles everything for us. This is |
| 1663 | * so easy and simple and cool, it'd be insane not to use it. */ |
| 1664 | else |
| 1665 | { |
| 1666 | if (output_conv.vc_type != CONV_NONE) |
| 1667 | { |
| 1668 | tmpbuf = string_convert(&output_conv, string, &length); |
| 1669 | vim_free(string); |
| 1670 | if (tmpbuf == NULL) |
| 1671 | return; |
| 1672 | string = tmpbuf; |
| 1673 | } |
| 1674 | /* Validate the string to avoid runtime warnings */ |
| 1675 | if (g_utf8_validate((const char *)string, (gssize)length, NULL)) |
| 1676 | { |
| 1677 | gtk_selection_data_set_text(selection_data, |
| 1678 | (const char *)string, length); |
| 1679 | } |
| 1680 | vim_free(string); |
| 1681 | return; |
| 1682 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1683 | |
| 1684 | if (string != NULL) |
| 1685 | { |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 1686 | #if !GTK_CHECK_VERSION(3,0,0) |
Bram Moolenaar | 3f2a5d8 | 2016-02-27 22:08:16 +0100 | [diff] [blame] | 1687 | /* Looks redundant even for GTK2 because these values are |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 1688 | * overwritten by gtk_selection_data_set() that follows. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1689 | selection_data->type = selection_data->target; |
| 1690 | selection_data->format = 8; /* 8 bits per char */ |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 1691 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1692 | gtk_selection_data_set(selection_data, type, 8, string, length); |
| 1693 | vim_free(string); |
| 1694 | } |
| 1695 | } |
| 1696 | |
| 1697 | /* |
Bram Moolenaar | 2969570 | 2012-05-18 17:03:18 +0200 | [diff] [blame] | 1698 | * Check if the GUI can be started. Called before gvimrc is sourced and |
| 1699 | * before fork(). |
| 1700 | * Return OK or FAIL. |
| 1701 | */ |
| 1702 | int |
Bram Moolenaar | 717e196 | 2016-08-10 21:28:44 +0200 | [diff] [blame] | 1703 | gui_mch_early_init_check(int give_message) |
Bram Moolenaar | 2969570 | 2012-05-18 17:03:18 +0200 | [diff] [blame] | 1704 | { |
| 1705 | char_u *p; |
| 1706 | |
| 1707 | /* Guess that when $DISPLAY isn't set the GUI can't start. */ |
| 1708 | p = mch_getenv((char_u *)"DISPLAY"); |
| 1709 | if (p == NULL || *p == NUL) |
| 1710 | { |
| 1711 | gui.dying = TRUE; |
Bram Moolenaar | 717e196 | 2016-08-10 21:28:44 +0200 | [diff] [blame] | 1712 | if (give_message) |
| 1713 | EMSG(_((char *)e_opendisp)); |
Bram Moolenaar | 2969570 | 2012-05-18 17:03:18 +0200 | [diff] [blame] | 1714 | return FAIL; |
| 1715 | } |
| 1716 | return OK; |
| 1717 | } |
| 1718 | |
| 1719 | /* |
| 1720 | * Check if the GUI can be started. Called before gvimrc is sourced but after |
| 1721 | * fork(). |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1722 | * Return OK or FAIL. |
| 1723 | */ |
| 1724 | int |
| 1725 | gui_mch_init_check(void) |
| 1726 | { |
Bram Moolenaar | 3a466a8 | 2016-01-19 17:47:25 +0100 | [diff] [blame] | 1727 | #ifdef USE_GRESOURCE |
| 1728 | static int res_registered = FALSE; |
| 1729 | |
| 1730 | if (!res_registered) |
| 1731 | { |
| 1732 | /* Call this function in the GUI process; otherwise, the resources |
| 1733 | * won't be available. Don't call it twice. */ |
| 1734 | res_registered = TRUE; |
| 1735 | gui_gtk_register_resource(); |
| 1736 | } |
| 1737 | #endif |
| 1738 | |
Bram Moolenaar | f80663f | 2016-04-05 21:56:06 +0200 | [diff] [blame] | 1739 | #if GTK_CHECK_VERSION(3,10,0) |
| 1740 | /* Vim currently assumes that Gtk means X11, so it cannot use native Gtk |
| 1741 | * support for other backends such as Wayland. */ |
| 1742 | gdk_set_allowed_backends ("x11"); |
| 1743 | #endif |
| 1744 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1745 | #ifdef FEAT_GUI_GNOME |
| 1746 | if (gtk_socket_id == 0) |
| 1747 | using_gnome = 1; |
| 1748 | #endif |
| 1749 | |
Bram Moolenaar | 8dd7901 | 2013-05-21 12:52:04 +0200 | [diff] [blame] | 1750 | /* This defaults to argv[0], but we want it to match the name of the |
| 1751 | * shipped gvim.desktop so that Vim's windows can be associated with this |
| 1752 | * file. */ |
| 1753 | g_set_prgname("gvim"); |
| 1754 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1755 | /* Don't use gtk_init() or gnome_init(), it exits on failure. */ |
| 1756 | if (!gtk_init_check(&gui_argc, &gui_argv)) |
| 1757 | { |
| 1758 | gui.dying = TRUE; |
Bram Moolenaar | c93e791 | 2008-07-08 10:46:08 +0000 | [diff] [blame] | 1759 | EMSG(_((char *)e_opendisp)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1760 | return FAIL; |
| 1761 | } |
| 1762 | |
| 1763 | return OK; |
| 1764 | } |
| 1765 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1766 | /**************************************************************************** |
| 1767 | * Mouse handling callbacks |
| 1768 | */ |
| 1769 | |
| 1770 | |
| 1771 | static guint mouse_click_timer = 0; |
| 1772 | static int mouse_timed_out = TRUE; |
| 1773 | |
| 1774 | /* |
| 1775 | * Timer used to recognize multiple clicks of the mouse button |
| 1776 | */ |
Bram Moolenaar | 4ab7968 | 2017-08-27 14:50:47 +0200 | [diff] [blame] | 1777 | static timeout_cb_type |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1778 | mouse_click_timer_cb(gpointer data) |
| 1779 | { |
| 1780 | /* we don't use this information currently */ |
| 1781 | int *timed_out = (int *) data; |
| 1782 | |
| 1783 | *timed_out = TRUE; |
| 1784 | return FALSE; /* don't happen again */ |
| 1785 | } |
| 1786 | |
Bram Moolenaar | 4ab7968 | 2017-08-27 14:50:47 +0200 | [diff] [blame] | 1787 | static guint motion_repeat_timer = 0; |
| 1788 | static int motion_repeat_offset = FALSE; |
| 1789 | static timeout_cb_type motion_repeat_timer_cb(gpointer); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1790 | |
| 1791 | static void |
| 1792 | process_motion_notify(int x, int y, GdkModifierType state) |
| 1793 | { |
| 1794 | int button; |
| 1795 | int_u vim_modifiers; |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 1796 | #if GTK_CHECK_VERSION(3,0,0) |
| 1797 | GtkAllocation allocation; |
| 1798 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1799 | |
| 1800 | button = (state & (GDK_BUTTON1_MASK | GDK_BUTTON2_MASK | |
| 1801 | GDK_BUTTON3_MASK | GDK_BUTTON4_MASK | |
| 1802 | GDK_BUTTON5_MASK)) |
| 1803 | ? MOUSE_DRAG : ' '; |
| 1804 | |
| 1805 | /* If our pointer is currently hidden, then we should show it. */ |
| 1806 | gui_mch_mousehide(FALSE); |
| 1807 | |
| 1808 | /* Just moving the rodent above the drawing area without any button |
| 1809 | * being pressed. */ |
| 1810 | if (button != MOUSE_DRAG) |
| 1811 | { |
| 1812 | gui_mouse_moved(x, y); |
| 1813 | return; |
| 1814 | } |
| 1815 | |
| 1816 | /* translate modifier coding between the main engine and GTK */ |
Bram Moolenaar | 19a09a1 | 2005-03-04 23:39:37 +0000 | [diff] [blame] | 1817 | vim_modifiers = modifiers_gdk2mouse(state); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1818 | |
Bram Moolenaar | 4932594 | 2007-05-10 19:19:59 +0000 | [diff] [blame] | 1819 | /* inform the editor engine about the occurrence of this event */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1820 | gui_send_mouse_event(button, x, y, FALSE, vim_modifiers); |
| 1821 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1822 | /* |
| 1823 | * Auto repeat timer handling. |
| 1824 | */ |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 1825 | #if GTK_CHECK_VERSION(3,0,0) |
| 1826 | gtk_widget_get_allocation(gui.drawarea, &allocation); |
| 1827 | |
| 1828 | if (x < 0 || y < 0 |
| 1829 | || x >= allocation.width |
| 1830 | || y >= allocation.height) |
| 1831 | #else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1832 | if (x < 0 || y < 0 |
| 1833 | || x >= gui.drawarea->allocation.width |
| 1834 | || y >= gui.drawarea->allocation.height) |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 1835 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1836 | { |
| 1837 | |
| 1838 | int dx; |
| 1839 | int dy; |
| 1840 | int offshoot; |
| 1841 | int delay = 10; |
| 1842 | |
| 1843 | /* Calculate the maximal distance of the cursor from the drawing area. |
| 1844 | * (offshoot can't become negative here!). |
| 1845 | */ |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 1846 | #if GTK_CHECK_VERSION(3,0,0) |
| 1847 | dx = x < 0 ? -x : x - allocation.width; |
| 1848 | dy = y < 0 ? -y : y - allocation.height; |
| 1849 | #else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1850 | dx = x < 0 ? -x : x - gui.drawarea->allocation.width; |
| 1851 | dy = y < 0 ? -y : y - gui.drawarea->allocation.height; |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 1852 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1853 | |
| 1854 | offshoot = dx > dy ? dx : dy; |
| 1855 | |
Bram Moolenaar | b85cb21 | 2009-05-17 14:24:23 +0000 | [diff] [blame] | 1856 | /* Make a linearly decaying timer delay with a threshold of 5 at a |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1857 | * distance of 127 pixels from the main window. |
| 1858 | * |
| 1859 | * One could think endlessly about the most ergonomic variant here. |
| 1860 | * For example it could make sense to calculate the distance from the |
| 1861 | * drags start instead... |
| 1862 | * |
| 1863 | * Maybe a parabolic interpolation would suite us better here too... |
| 1864 | */ |
| 1865 | if (offshoot > 127) |
| 1866 | { |
| 1867 | /* 5 appears to be somehow near to my perceptual limits :-). */ |
| 1868 | delay = 5; |
| 1869 | } |
| 1870 | else |
| 1871 | { |
| 1872 | delay = (130 * (127 - offshoot)) / 127 + 5; |
| 1873 | } |
| 1874 | |
| 1875 | /* shoot again */ |
| 1876 | if (!motion_repeat_timer) |
Bram Moolenaar | 4ab7968 | 2017-08-27 14:50:47 +0200 | [diff] [blame] | 1877 | motion_repeat_timer = timeout_add(delay, motion_repeat_timer_cb, |
| 1878 | NULL); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1879 | } |
| 1880 | } |
| 1881 | |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 1882 | #if GTK_CHECK_VERSION(3,0,0) |
| 1883 | static GdkDevice * |
| 1884 | gui_gtk_get_pointer_device(GtkWidget *widget) |
| 1885 | { |
| 1886 | GdkWindow * const win = gtk_widget_get_window(widget); |
| 1887 | GdkDisplay * const dpy = gdk_window_get_display(win); |
Bram Moolenaar | 30e12d2 | 2016-04-17 20:49:53 +0200 | [diff] [blame] | 1888 | # if GTK_CHECK_VERSION(3,20,0) |
| 1889 | GdkSeat * const seat = gdk_display_get_default_seat(dpy); |
| 1890 | return gdk_seat_get_pointer(seat); |
| 1891 | # else |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 1892 | GdkDeviceManager * const mngr = gdk_display_get_device_manager(dpy); |
| 1893 | return gdk_device_manager_get_client_pointer(mngr); |
Bram Moolenaar | 30e12d2 | 2016-04-17 20:49:53 +0200 | [diff] [blame] | 1894 | # endif |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 1895 | } |
| 1896 | |
| 1897 | static GdkWindow * |
| 1898 | gui_gtk_get_pointer(GtkWidget *widget, |
| 1899 | gint *x, |
| 1900 | gint *y, |
| 1901 | GdkModifierType *state) |
| 1902 | { |
| 1903 | GdkWindow * const win = gtk_widget_get_window(widget); |
| 1904 | GdkDevice * const dev = gui_gtk_get_pointer_device(widget); |
| 1905 | return gdk_window_get_device_position(win, dev , x, y, state); |
| 1906 | } |
| 1907 | |
Bram Moolenaar | 2369c15 | 2016-03-04 23:08:25 +0100 | [diff] [blame] | 1908 | # if defined(FEAT_GUI_TABLINE) || defined(PROTO) |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 1909 | static GdkWindow * |
| 1910 | gui_gtk_window_at_position(GtkWidget *widget, |
| 1911 | gint *x, |
| 1912 | gint *y) |
| 1913 | { |
| 1914 | GdkDevice * const dev = gui_gtk_get_pointer_device(widget); |
| 1915 | return gdk_device_get_window_at_position(dev, x, y); |
| 1916 | } |
Bram Moolenaar | 2369c15 | 2016-03-04 23:08:25 +0100 | [diff] [blame] | 1917 | # endif |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 1918 | #endif |
| 1919 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1920 | /* |
| 1921 | * Timer used to recognize multiple clicks of the mouse button. |
| 1922 | */ |
Bram Moolenaar | 4ab7968 | 2017-08-27 14:50:47 +0200 | [diff] [blame] | 1923 | static timeout_cb_type |
Bram Moolenaar | b85cb21 | 2009-05-17 14:24:23 +0000 | [diff] [blame] | 1924 | motion_repeat_timer_cb(gpointer data UNUSED) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1925 | { |
| 1926 | int x; |
| 1927 | int y; |
| 1928 | GdkModifierType state; |
| 1929 | |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 1930 | #if GTK_CHECK_VERSION(3,0,0) |
| 1931 | gui_gtk_get_pointer(gui.drawarea, &x, &y, &state); |
| 1932 | #else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1933 | gdk_window_get_pointer(gui.drawarea->window, &x, &y, &state); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 1934 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1935 | |
| 1936 | if (!(state & (GDK_BUTTON1_MASK | GDK_BUTTON2_MASK | |
| 1937 | GDK_BUTTON3_MASK | GDK_BUTTON4_MASK | |
| 1938 | GDK_BUTTON5_MASK))) |
| 1939 | { |
| 1940 | motion_repeat_timer = 0; |
| 1941 | return FALSE; |
| 1942 | } |
| 1943 | |
| 1944 | /* If there already is a mouse click in the input buffer, wait another |
| 1945 | * time (otherwise we would create a backlog of clicks) */ |
| 1946 | if (vim_used_in_input_buf() > 10) |
| 1947 | return TRUE; |
| 1948 | |
| 1949 | motion_repeat_timer = 0; |
| 1950 | |
| 1951 | /* |
| 1952 | * Fake a motion event. |
| 1953 | * Trick: Pretend the mouse moved to the next character on every other |
| 1954 | * event, otherwise drag events will be discarded, because they are still |
| 1955 | * in the same character. |
| 1956 | */ |
| 1957 | if (motion_repeat_offset) |
| 1958 | x += gui.char_width; |
| 1959 | |
| 1960 | motion_repeat_offset = !motion_repeat_offset; |
| 1961 | process_motion_notify(x, y, state); |
| 1962 | |
| 1963 | /* Don't happen again. We will get reinstalled in the synthetic event |
| 1964 | * if needed -- thus repeating should still work. */ |
| 1965 | return FALSE; |
| 1966 | } |
| 1967 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1968 | static gint |
Bram Moolenaar | b85cb21 | 2009-05-17 14:24:23 +0000 | [diff] [blame] | 1969 | motion_notify_event(GtkWidget *widget, |
| 1970 | GdkEventMotion *event, |
| 1971 | gpointer data UNUSED) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1972 | { |
| 1973 | if (event->is_hint) |
| 1974 | { |
| 1975 | int x; |
| 1976 | int y; |
| 1977 | GdkModifierType state; |
| 1978 | |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 1979 | #if GTK_CHECK_VERSION(3,0,0) |
| 1980 | gui_gtk_get_pointer(widget, &x, &y, &state); |
| 1981 | #else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1982 | gdk_window_get_pointer(widget->window, &x, &y, &state); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 1983 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1984 | process_motion_notify(x, y, state); |
| 1985 | } |
| 1986 | else |
| 1987 | { |
| 1988 | process_motion_notify((int)event->x, (int)event->y, |
| 1989 | (GdkModifierType)event->state); |
| 1990 | } |
| 1991 | |
| 1992 | return TRUE; /* handled */ |
| 1993 | } |
| 1994 | |
| 1995 | |
| 1996 | /* |
| 1997 | * Mouse button handling. Note please that we are capturing multiple click's |
| 1998 | * by our own timeout mechanism instead of the one provided by GTK+ itself. |
| 1999 | * This is due to the way the generic VIM code is recognizing multiple clicks. |
| 2000 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2001 | static gint |
Bram Moolenaar | b85cb21 | 2009-05-17 14:24:23 +0000 | [diff] [blame] | 2002 | button_press_event(GtkWidget *widget, |
| 2003 | GdkEventButton *event, |
| 2004 | gpointer data UNUSED) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2005 | { |
| 2006 | int button; |
| 2007 | int repeated_click = FALSE; |
| 2008 | int x, y; |
| 2009 | int_u vim_modifiers; |
| 2010 | |
Bram Moolenaar | 20892c1 | 2011-06-26 04:49:00 +0200 | [diff] [blame] | 2011 | gui.event_time = event->time; |
Bram Moolenaar | 7cfea75 | 2010-06-22 06:07:12 +0200 | [diff] [blame] | 2012 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2013 | /* Make sure we have focus now we've been selected */ |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 2014 | #if GTK_CHECK_VERSION(3,0,0) |
| 2015 | if (gtk_socket_id != 0 && !gtk_widget_has_focus(widget)) |
| 2016 | #else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2017 | if (gtk_socket_id != 0 && !GTK_WIDGET_HAS_FOCUS(widget)) |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 2018 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2019 | gtk_widget_grab_focus(widget); |
| 2020 | |
| 2021 | /* |
| 2022 | * Don't let additional events about multiple clicks send by GTK to us |
| 2023 | * after the initial button press event confuse us. |
| 2024 | */ |
| 2025 | if (event->type != GDK_BUTTON_PRESS) |
| 2026 | return FALSE; |
| 2027 | |
| 2028 | x = event->x; |
| 2029 | y = event->y; |
| 2030 | |
| 2031 | /* Handle multiple clicks */ |
| 2032 | if (!mouse_timed_out && mouse_click_timer) |
| 2033 | { |
Bram Moolenaar | 4ab7968 | 2017-08-27 14:50:47 +0200 | [diff] [blame] | 2034 | timeout_remove(mouse_click_timer); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2035 | mouse_click_timer = 0; |
| 2036 | repeated_click = TRUE; |
| 2037 | } |
| 2038 | |
| 2039 | mouse_timed_out = FALSE; |
Bram Moolenaar | 4ab7968 | 2017-08-27 14:50:47 +0200 | [diff] [blame] | 2040 | mouse_click_timer = timeout_add(p_mouset, mouse_click_timer_cb, |
| 2041 | &mouse_timed_out); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2042 | |
| 2043 | switch (event->button) |
| 2044 | { |
Bram Moolenaar | 88e484b | 2015-11-24 15:38:44 +0100 | [diff] [blame] | 2045 | /* Keep in sync with gui_x11.c. |
| 2046 | * Buttons 4-7 are handled in scroll_event() */ |
| 2047 | case 1: button = MOUSE_LEFT; break; |
| 2048 | case 2: button = MOUSE_MIDDLE; break; |
| 2049 | case 3: button = MOUSE_RIGHT; break; |
| 2050 | case 8: button = MOUSE_X1; break; |
| 2051 | case 9: button = MOUSE_X2; break; |
| 2052 | default: |
| 2053 | return FALSE; /* Unknown button */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2054 | } |
| 2055 | |
| 2056 | #ifdef FEAT_XIM |
| 2057 | /* cancel any preediting */ |
| 2058 | if (im_is_preediting()) |
| 2059 | xim_reset(); |
| 2060 | #endif |
| 2061 | |
Bram Moolenaar | 19a09a1 | 2005-03-04 23:39:37 +0000 | [diff] [blame] | 2062 | vim_modifiers = modifiers_gdk2mouse(event->state); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2063 | |
| 2064 | gui_send_mouse_event(button, x, y, repeated_click, vim_modifiers); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2065 | |
| 2066 | return TRUE; |
| 2067 | } |
| 2068 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2069 | /* |
Bram Moolenaar | 182c5be | 2010-06-25 05:37:59 +0200 | [diff] [blame] | 2070 | * GTK+ 2 abstracts scrolling via the GdkEventScroll. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2071 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2072 | static gboolean |
Bram Moolenaar | b85cb21 | 2009-05-17 14:24:23 +0000 | [diff] [blame] | 2073 | scroll_event(GtkWidget *widget, |
| 2074 | GdkEventScroll *event, |
| 2075 | gpointer data UNUSED) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2076 | { |
| 2077 | int button; |
Bram Moolenaar | 19a09a1 | 2005-03-04 23:39:37 +0000 | [diff] [blame] | 2078 | int_u vim_modifiers; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2079 | |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 2080 | #if GTK_CHECK_VERSION(3,0,0) |
| 2081 | if (gtk_socket_id != 0 && !gtk_widget_has_focus(widget)) |
| 2082 | #else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2083 | if (gtk_socket_id != 0 && !GTK_WIDGET_HAS_FOCUS(widget)) |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 2084 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2085 | gtk_widget_grab_focus(widget); |
| 2086 | |
| 2087 | switch (event->direction) |
| 2088 | { |
| 2089 | case GDK_SCROLL_UP: |
| 2090 | button = MOUSE_4; |
| 2091 | break; |
| 2092 | case GDK_SCROLL_DOWN: |
| 2093 | button = MOUSE_5; |
| 2094 | break; |
Bram Moolenaar | 8d9b40e | 2010-07-25 15:49:07 +0200 | [diff] [blame] | 2095 | case GDK_SCROLL_LEFT: |
| 2096 | button = MOUSE_7; |
| 2097 | break; |
| 2098 | case GDK_SCROLL_RIGHT: |
| 2099 | button = MOUSE_6; |
| 2100 | break; |
| 2101 | default: /* This shouldn't happen */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2102 | return FALSE; |
| 2103 | } |
| 2104 | |
| 2105 | # ifdef FEAT_XIM |
| 2106 | /* cancel any preediting */ |
| 2107 | if (im_is_preediting()) |
| 2108 | xim_reset(); |
| 2109 | # endif |
| 2110 | |
Bram Moolenaar | 19a09a1 | 2005-03-04 23:39:37 +0000 | [diff] [blame] | 2111 | vim_modifiers = modifiers_gdk2mouse(event->state); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2112 | |
| 2113 | gui_send_mouse_event(button, (int)event->x, (int)event->y, |
| 2114 | FALSE, vim_modifiers); |
| 2115 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2116 | return TRUE; |
| 2117 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2118 | |
| 2119 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2120 | static gint |
Bram Moolenaar | b85cb21 | 2009-05-17 14:24:23 +0000 | [diff] [blame] | 2121 | button_release_event(GtkWidget *widget UNUSED, |
| 2122 | GdkEventButton *event, |
| 2123 | gpointer data UNUSED) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2124 | { |
| 2125 | int x, y; |
| 2126 | int_u vim_modifiers; |
| 2127 | |
Bram Moolenaar | 20892c1 | 2011-06-26 04:49:00 +0200 | [diff] [blame] | 2128 | gui.event_time = event->time; |
Bram Moolenaar | 7cfea75 | 2010-06-22 06:07:12 +0200 | [diff] [blame] | 2129 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2130 | /* Remove any motion "machine gun" timers used for automatic further |
| 2131 | extension of allocation areas if outside of the applications window |
| 2132 | area .*/ |
| 2133 | if (motion_repeat_timer) |
| 2134 | { |
Bram Moolenaar | 4ab7968 | 2017-08-27 14:50:47 +0200 | [diff] [blame] | 2135 | timeout_remove(motion_repeat_timer); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2136 | motion_repeat_timer = 0; |
| 2137 | } |
| 2138 | |
| 2139 | x = event->x; |
| 2140 | y = event->y; |
| 2141 | |
Bram Moolenaar | 19a09a1 | 2005-03-04 23:39:37 +0000 | [diff] [blame] | 2142 | vim_modifiers = modifiers_gdk2mouse(event->state); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2143 | |
| 2144 | gui_send_mouse_event(MOUSE_RELEASE, x, y, FALSE, vim_modifiers); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2145 | |
| 2146 | return TRUE; |
| 2147 | } |
| 2148 | |
| 2149 | |
| 2150 | #ifdef FEAT_DND |
| 2151 | /**************************************************************************** |
| 2152 | * Drag aNd Drop support handlers. |
| 2153 | */ |
| 2154 | |
| 2155 | /* |
| 2156 | * Count how many items there may be and separate them with a NUL. |
| 2157 | * Apparently the items are separated with \r\n. This is not documented, |
| 2158 | * thus be careful not to go past the end. Also allow separation with |
| 2159 | * NUL characters. |
| 2160 | */ |
| 2161 | static int |
| 2162 | count_and_decode_uri_list(char_u *out, char_u *raw, int len) |
| 2163 | { |
| 2164 | int i; |
| 2165 | char_u *p = out; |
| 2166 | int count = 0; |
| 2167 | |
| 2168 | for (i = 0; i < len; ++i) |
| 2169 | { |
| 2170 | if (raw[i] == NUL || raw[i] == '\n' || raw[i] == '\r') |
| 2171 | { |
| 2172 | if (p > out && p[-1] != NUL) |
| 2173 | { |
| 2174 | ++count; |
| 2175 | *p++ = NUL; |
| 2176 | } |
| 2177 | } |
| 2178 | else if (raw[i] == '%' && i + 2 < len && hexhex2nr(raw + i + 1) > 0) |
| 2179 | { |
| 2180 | *p++ = hexhex2nr(raw + i + 1); |
| 2181 | i += 2; |
| 2182 | } |
| 2183 | else |
| 2184 | *p++ = raw[i]; |
| 2185 | } |
| 2186 | if (p > out && p[-1] != NUL) |
| 2187 | { |
| 2188 | *p = NUL; /* last item didn't have \r or \n */ |
| 2189 | ++count; |
| 2190 | } |
| 2191 | return count; |
| 2192 | } |
| 2193 | |
| 2194 | /* |
| 2195 | * Parse NUL separated "src" strings. Make it an array "outlist" form. On |
| 2196 | * this process, URI which protocol is not "file:" are removed. Return |
| 2197 | * length of array (less than "max"). |
| 2198 | */ |
| 2199 | static int |
| 2200 | filter_uri_list(char_u **outlist, int max, char_u *src) |
| 2201 | { |
| 2202 | int i, j; |
| 2203 | |
| 2204 | for (i = j = 0; i < max; ++i) |
| 2205 | { |
| 2206 | outlist[i] = NULL; |
| 2207 | if (STRNCMP(src, "file:", 5) == 0) |
| 2208 | { |
| 2209 | src += 5; |
| 2210 | if (STRNCMP(src, "//localhost", 11) == 0) |
| 2211 | src += 11; |
| 2212 | while (src[0] == '/' && src[1] == '/') |
| 2213 | ++src; |
| 2214 | outlist[j++] = vim_strsave(src); |
| 2215 | } |
| 2216 | src += STRLEN(src) + 1; |
| 2217 | } |
| 2218 | return j; |
| 2219 | } |
| 2220 | |
| 2221 | static char_u ** |
| 2222 | parse_uri_list(int *count, char_u *data, int len) |
| 2223 | { |
| 2224 | int n = 0; |
| 2225 | char_u *tmp = NULL; |
Bram Moolenaar | 945ec09 | 2016-06-08 21:17:43 +0200 | [diff] [blame] | 2226 | char_u **array = NULL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2227 | |
| 2228 | if (data != NULL && len > 0 && (tmp = (char_u *)alloc(len + 1)) != NULL) |
| 2229 | { |
| 2230 | n = count_and_decode_uri_list(tmp, data, len); |
| 2231 | if (n > 0 && (array = (char_u **)alloc(n * sizeof(char_u *))) != NULL) |
| 2232 | n = filter_uri_list(array, n, tmp); |
| 2233 | } |
| 2234 | vim_free(tmp); |
| 2235 | *count = n; |
| 2236 | return array; |
| 2237 | } |
| 2238 | |
| 2239 | static void |
| 2240 | drag_handle_uri_list(GdkDragContext *context, |
| 2241 | GtkSelectionData *data, |
| 2242 | guint time_, |
| 2243 | GdkModifierType state, |
| 2244 | gint x, |
| 2245 | gint y) |
| 2246 | { |
| 2247 | char_u **fnames; |
| 2248 | int nfiles = 0; |
| 2249 | |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 2250 | # if GTK_CHECK_VERSION(3,0,0) |
| 2251 | fnames = parse_uri_list(&nfiles, |
| 2252 | (char_u *)gtk_selection_data_get_data(data), |
| 2253 | gtk_selection_data_get_length(data)); |
| 2254 | # else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2255 | fnames = parse_uri_list(&nfiles, data->data, data->length); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 2256 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2257 | |
| 2258 | if (fnames != NULL && nfiles > 0) |
| 2259 | { |
Bram Moolenaar | 19a09a1 | 2005-03-04 23:39:37 +0000 | [diff] [blame] | 2260 | int_u modifiers; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2261 | |
| 2262 | gtk_drag_finish(context, TRUE, FALSE, time_); /* accept */ |
| 2263 | |
Bram Moolenaar | 19a09a1 | 2005-03-04 23:39:37 +0000 | [diff] [blame] | 2264 | modifiers = modifiers_gdk2mouse(state); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2265 | |
| 2266 | gui_handle_drop(x, y, modifiers, fnames, nfiles); |
| 2267 | } |
Bram Moolenaar | eb3593b | 2006-04-22 22:33:57 +0000 | [diff] [blame] | 2268 | else |
| 2269 | vim_free(fnames); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2270 | } |
| 2271 | |
| 2272 | static void |
| 2273 | drag_handle_text(GdkDragContext *context, |
| 2274 | GtkSelectionData *data, |
| 2275 | guint time_, |
| 2276 | GdkModifierType state) |
| 2277 | { |
| 2278 | char_u dropkey[6] = {CSI, KS_MODIFIER, 0, CSI, KS_EXTRA, (char_u)KE_DROP}; |
| 2279 | char_u *text; |
| 2280 | int len; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2281 | char_u *tmpbuf = NULL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2282 | |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 2283 | # if GTK_CHECK_VERSION(3,0,0) |
| 2284 | text = (char_u *)gtk_selection_data_get_data(data); |
| 2285 | len = gtk_selection_data_get_length(data); |
| 2286 | # else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2287 | text = data->data; |
| 2288 | len = data->length; |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 2289 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2290 | |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 2291 | # if GTK_CHECK_VERSION(3,0,0) |
| 2292 | if (gtk_selection_data_get_data_type(data) == utf8_string_atom) |
| 2293 | # else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2294 | if (data->type == utf8_string_atom) |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 2295 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2296 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2297 | if (input_conv.vc_type != CONV_NONE) |
| 2298 | tmpbuf = string_convert(&input_conv, text, &len); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2299 | if (tmpbuf != NULL) |
| 2300 | text = tmpbuf; |
| 2301 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2302 | |
| 2303 | dnd_yank_drag_data(text, (long)len); |
| 2304 | gtk_drag_finish(context, TRUE, FALSE, time_); /* accept */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2305 | vim_free(tmpbuf); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2306 | |
Bram Moolenaar | 19a09a1 | 2005-03-04 23:39:37 +0000 | [diff] [blame] | 2307 | dropkey[2] = modifiers_gdk2vim(state); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2308 | |
| 2309 | if (dropkey[2] != 0) |
| 2310 | add_to_input_buf(dropkey, (int)sizeof(dropkey)); |
| 2311 | else |
| 2312 | add_to_input_buf(dropkey + 3, (int)(sizeof(dropkey) - 3)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2313 | } |
| 2314 | |
| 2315 | /* |
| 2316 | * DND receiver. |
| 2317 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2318 | static void |
| 2319 | drag_data_received_cb(GtkWidget *widget, |
| 2320 | GdkDragContext *context, |
| 2321 | gint x, |
| 2322 | gint y, |
| 2323 | GtkSelectionData *data, |
| 2324 | guint info, |
| 2325 | guint time_, |
Bram Moolenaar | b85cb21 | 2009-05-17 14:24:23 +0000 | [diff] [blame] | 2326 | gpointer user_data UNUSED) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2327 | { |
| 2328 | GdkModifierType state; |
| 2329 | |
| 2330 | /* Guard against trash */ |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 2331 | # if GTK_CHECK_VERSION(3,0,0) |
| 2332 | const guchar * const data_data = gtk_selection_data_get_data(data); |
| 2333 | const gint data_length = gtk_selection_data_get_length(data); |
| 2334 | const gint data_format = gtk_selection_data_get_format(data); |
| 2335 | |
| 2336 | if (data_data == NULL |
| 2337 | || data_length <= 0 |
| 2338 | || data_format != 8 |
| 2339 | || data_data[data_length] != '\0') |
| 2340 | # else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2341 | if (data->data == NULL |
| 2342 | || data->length <= 0 |
| 2343 | || data->format != 8 |
| 2344 | || data->data[data->length] != '\0') |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 2345 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2346 | { |
| 2347 | gtk_drag_finish(context, FALSE, FALSE, time_); |
| 2348 | return; |
| 2349 | } |
| 2350 | |
| 2351 | /* Get the current modifier state for proper distinguishment between |
| 2352 | * different operations later. */ |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 2353 | #if GTK_CHECK_VERSION(3,0,0) |
| 2354 | gui_gtk_get_pointer(widget, NULL, NULL, &state); |
| 2355 | # else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2356 | gdk_window_get_pointer(widget->window, NULL, NULL, &state); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 2357 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2358 | |
| 2359 | /* Not sure about the role of "text/plain" here... */ |
| 2360 | if (info == (guint)TARGET_TEXT_URI_LIST) |
| 2361 | drag_handle_uri_list(context, data, time_, state, x, y); |
| 2362 | else |
| 2363 | drag_handle_text(context, data, time_, state); |
| 2364 | |
| 2365 | } |
| 2366 | #endif /* FEAT_DND */ |
| 2367 | |
| 2368 | |
| 2369 | #if defined(FEAT_GUI_GNOME) && defined(FEAT_SESSION) |
| 2370 | /* |
| 2371 | * GnomeClient interact callback. Check for unsaved buffers that cannot |
| 2372 | * be abandoned and pop up a dialog asking the user for confirmation if |
| 2373 | * necessary. |
| 2374 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2375 | static void |
Bram Moolenaar | 30bb414 | 2010-05-17 22:07:15 +0200 | [diff] [blame] | 2376 | sm_client_check_changed_any(GnomeClient *client UNUSED, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2377 | gint key, |
Bram Moolenaar | 30bb414 | 2010-05-17 22:07:15 +0200 | [diff] [blame] | 2378 | GnomeDialogType type UNUSED, |
| 2379 | gpointer data UNUSED) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2380 | { |
| 2381 | cmdmod_T save_cmdmod; |
| 2382 | gboolean shutdown_cancelled; |
| 2383 | |
| 2384 | save_cmdmod = cmdmod; |
| 2385 | |
| 2386 | # ifdef FEAT_BROWSE |
| 2387 | cmdmod.browse = TRUE; |
| 2388 | # endif |
| 2389 | # if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) |
| 2390 | cmdmod.confirm = TRUE; |
| 2391 | # endif |
| 2392 | /* |
| 2393 | * If there are changed buffers, present the user with |
| 2394 | * a dialog if possible, otherwise give an error message. |
| 2395 | */ |
Bram Moolenaar | 027387f | 2016-01-02 22:25:52 +0100 | [diff] [blame] | 2396 | shutdown_cancelled = check_changed_any(FALSE, FALSE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2397 | |
| 2398 | exiting = FALSE; |
| 2399 | cmdmod = save_cmdmod; |
| 2400 | setcursor(); /* position the cursor */ |
| 2401 | out_flush(); |
| 2402 | /* |
| 2403 | * If the user hit the [Cancel] button the whole shutdown |
| 2404 | * will be cancelled. Wow, quite powerful feature (: |
| 2405 | */ |
| 2406 | gnome_interaction_key_return(key, shutdown_cancelled); |
| 2407 | } |
| 2408 | |
| 2409 | /* |
| 2410 | * Generate a script that can be used to restore the current editing session. |
| 2411 | * Save the value of v:this_session before running :mksession in order to make |
| 2412 | * automagic session save fully transparent. Return TRUE on success. |
| 2413 | */ |
| 2414 | static int |
| 2415 | write_session_file(char_u *filename) |
| 2416 | { |
| 2417 | char_u *escaped_filename; |
| 2418 | char *mksession_cmdline; |
| 2419 | unsigned int save_ssop_flags; |
| 2420 | int failed; |
| 2421 | |
| 2422 | /* |
| 2423 | * Build an ex command line to create a script that restores the current |
| 2424 | * session if executed. Escape the filename to avoid nasty surprises. |
| 2425 | */ |
| 2426 | escaped_filename = vim_strsave_escaped(filename, escape_chars); |
| 2427 | if (escaped_filename == NULL) |
| 2428 | return FALSE; |
Bram Moolenaar | dc3213d | 2007-06-19 16:03:50 +0000 | [diff] [blame] | 2429 | mksession_cmdline = g_strconcat("mksession ", (char *)escaped_filename, |
| 2430 | NULL); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2431 | vim_free(escaped_filename); |
Bram Moolenaar | dc3213d | 2007-06-19 16:03:50 +0000 | [diff] [blame] | 2432 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2433 | /* |
| 2434 | * Use a reasonable hardcoded set of 'sessionoptions' flags to avoid |
| 2435 | * unpredictable effects when the session is saved automatically. Also, |
| 2436 | * we definitely need SSOP_GLOBALS to be able to restore v:this_session. |
| 2437 | * Don't use SSOP_BUFFERS to prevent the buffer list from becoming |
| 2438 | * enormously large if the GNOME session feature is used regularly. |
| 2439 | */ |
| 2440 | save_ssop_flags = ssop_flags; |
| 2441 | ssop_flags = (SSOP_BLANK|SSOP_CURDIR|SSOP_FOLDS|SSOP_GLOBALS |
Bram Moolenaar | dc3213d | 2007-06-19 16:03:50 +0000 | [diff] [blame] | 2442 | |SSOP_HELP|SSOP_OPTIONS|SSOP_WINSIZE|SSOP_TABPAGES); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2443 | |
| 2444 | do_cmdline_cmd((char_u *)"let Save_VV_this_session = v:this_session"); |
| 2445 | failed = (do_cmdline_cmd((char_u *)mksession_cmdline) == FAIL); |
| 2446 | do_cmdline_cmd((char_u *)"let v:this_session = Save_VV_this_session"); |
Bram Moolenaar | 485db9b | 2005-01-31 19:23:41 +0000 | [diff] [blame] | 2447 | do_unlet((char_u *)"Save_VV_this_session", TRUE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2448 | |
| 2449 | ssop_flags = save_ssop_flags; |
| 2450 | g_free(mksession_cmdline); |
Bram Moolenaar | 24dc230 | 2014-05-13 20:19:58 +0200 | [diff] [blame] | 2451 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2452 | /* |
| 2453 | * Reopen the file and append a command to restore v:this_session, |
| 2454 | * as if this save never happened. This is to avoid conflicts with |
| 2455 | * the user's own sessions. FIXME: It's probably less hackish to add |
| 2456 | * a "stealth" flag to 'sessionoptions' -- gotta ask Bram. |
| 2457 | */ |
| 2458 | if (!failed) |
| 2459 | { |
| 2460 | FILE *fd; |
| 2461 | |
| 2462 | fd = open_exfile(filename, TRUE, APPENDBIN); |
| 2463 | |
| 2464 | failed = (fd == NULL |
| 2465 | || put_line(fd, "let v:this_session = Save_VV_this_session") == FAIL |
| 2466 | || put_line(fd, "unlet Save_VV_this_session") == FAIL); |
| 2467 | |
| 2468 | if (fd != NULL && fclose(fd) != 0) |
| 2469 | failed = TRUE; |
| 2470 | |
| 2471 | if (failed) |
| 2472 | mch_remove(filename); |
| 2473 | } |
| 2474 | |
| 2475 | return !failed; |
| 2476 | } |
| 2477 | |
| 2478 | /* |
| 2479 | * "save_yourself" signal handler. Initiate an interaction to ask the user |
| 2480 | * for confirmation if necessary. Save the current editing session and tell |
| 2481 | * the session manager how to restart Vim. |
| 2482 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2483 | static gboolean |
| 2484 | sm_client_save_yourself(GnomeClient *client, |
Bram Moolenaar | 30bb414 | 2010-05-17 22:07:15 +0200 | [diff] [blame] | 2485 | gint phase UNUSED, |
| 2486 | GnomeSaveStyle save_style UNUSED, |
| 2487 | gboolean shutdown UNUSED, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2488 | GnomeInteractStyle interact_style, |
Bram Moolenaar | 30bb414 | 2010-05-17 22:07:15 +0200 | [diff] [blame] | 2489 | gboolean fast UNUSED, |
| 2490 | gpointer data UNUSED) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2491 | { |
| 2492 | static const char suffix[] = "-session.vim"; |
| 2493 | char *session_file; |
| 2494 | unsigned int len; |
| 2495 | gboolean success; |
| 2496 | |
| 2497 | /* Always request an interaction if possible. check_changed_any() |
| 2498 | * won't actually show a dialog unless any buffers have been modified. |
| 2499 | * There doesn't seem to be an obvious way to check that without |
| 2500 | * automatically firing the dialog. Anyway, it works just fine. */ |
| 2501 | if (interact_style == GNOME_INTERACT_ANY) |
| 2502 | gnome_client_request_interaction(client, GNOME_DIALOG_NORMAL, |
| 2503 | &sm_client_check_changed_any, |
| 2504 | NULL); |
| 2505 | out_flush(); |
| 2506 | ml_sync_all(FALSE, FALSE); /* preserve all swap files */ |
| 2507 | |
| 2508 | /* The path is unique for each session save. We do neither know nor care |
| 2509 | * which session script will actually be used later. This decision is in |
| 2510 | * the domain of the session manager. */ |
| 2511 | session_file = gnome_config_get_real_path( |
| 2512 | gnome_client_get_config_prefix(client)); |
| 2513 | len = strlen(session_file); |
| 2514 | |
| 2515 | if (len > 0 && session_file[len-1] == G_DIR_SEPARATOR) |
| 2516 | --len; /* get rid of the superfluous trailing '/' */ |
| 2517 | |
| 2518 | session_file = g_renew(char, session_file, len + sizeof(suffix)); |
| 2519 | memcpy(session_file + len, suffix, sizeof(suffix)); |
| 2520 | |
| 2521 | success = write_session_file((char_u *)session_file); |
| 2522 | |
| 2523 | if (success) |
| 2524 | { |
| 2525 | const char *argv[8]; |
| 2526 | int i; |
| 2527 | |
| 2528 | /* Tell the session manager how to wipe out the stored session data. |
| 2529 | * This isn't as dangerous as it looks, don't worry :) session_file |
| 2530 | * is a unique absolute filename. Usually it'll be something like |
| 2531 | * `/home/user/.gnome2/vim-XXXXXX-session.vim'. */ |
| 2532 | i = 0; |
| 2533 | argv[i++] = "rm"; |
| 2534 | argv[i++] = session_file; |
| 2535 | argv[i] = NULL; |
| 2536 | |
| 2537 | gnome_client_set_discard_command(client, i, (char **)argv); |
| 2538 | |
| 2539 | /* Tell the session manager how to restore the just saved session. |
| 2540 | * This is easily done thanks to Vim's -S option. Pass the -f flag |
| 2541 | * since there's no need to fork -- it might even cause confusion. |
| 2542 | * Also pass the window role to give the WM something to match on. |
| 2543 | * The role is set in gui_mch_open(), thus should _never_ be NULL. */ |
| 2544 | i = 0; |
| 2545 | argv[i++] = restart_command; |
| 2546 | argv[i++] = "-f"; |
| 2547 | argv[i++] = "-g"; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2548 | argv[i++] = "--role"; |
| 2549 | argv[i++] = gtk_window_get_role(GTK_WINDOW(gui.mainwin)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2550 | argv[i++] = "-S"; |
| 2551 | argv[i++] = session_file; |
| 2552 | argv[i] = NULL; |
| 2553 | |
| 2554 | gnome_client_set_restart_command(client, i, (char **)argv); |
| 2555 | gnome_client_set_clone_command(client, 0, NULL); |
| 2556 | } |
| 2557 | |
| 2558 | g_free(session_file); |
| 2559 | |
| 2560 | return success; |
| 2561 | } |
| 2562 | |
| 2563 | /* |
| 2564 | * Called when the session manager wants us to die. There isn't much to save |
| 2565 | * here since "save_yourself" has been emitted before (unless serious trouble |
| 2566 | * is happening). |
| 2567 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2568 | static void |
Bram Moolenaar | 30bb414 | 2010-05-17 22:07:15 +0200 | [diff] [blame] | 2569 | sm_client_die(GnomeClient *client UNUSED, gpointer data UNUSED) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2570 | { |
| 2571 | /* Don't write messages to the GUI anymore */ |
| 2572 | full_screen = FALSE; |
| 2573 | |
Bram Moolenaar | c93e791 | 2008-07-08 10:46:08 +0000 | [diff] [blame] | 2574 | vim_strncpy(IObuff, (char_u *) |
Bram Moolenaar | ce0842a | 2005-07-18 21:58:11 +0000 | [diff] [blame] | 2575 | _("Vim: Received \"die\" request from session manager\n"), |
Bram Moolenaar | c93e791 | 2008-07-08 10:46:08 +0000 | [diff] [blame] | 2576 | IOSIZE - 1); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2577 | preserve_exit(); |
| 2578 | } |
| 2579 | |
| 2580 | /* |
| 2581 | * Connect our signal handlers to be notified on session save and shutdown. |
| 2582 | */ |
| 2583 | static void |
| 2584 | setup_save_yourself(void) |
| 2585 | { |
| 2586 | GnomeClient *client; |
| 2587 | |
| 2588 | client = gnome_master_client(); |
| 2589 | |
| 2590 | if (client != NULL) |
| 2591 | { |
| 2592 | /* Must use the deprecated gtk_signal_connect() for compatibility |
| 2593 | * with GNOME 1. Arrgh, zombies! */ |
| 2594 | gtk_signal_connect(GTK_OBJECT(client), "save_yourself", |
| 2595 | GTK_SIGNAL_FUNC(&sm_client_save_yourself), NULL); |
| 2596 | gtk_signal_connect(GTK_OBJECT(client), "die", |
| 2597 | GTK_SIGNAL_FUNC(&sm_client_die), NULL); |
| 2598 | } |
| 2599 | } |
| 2600 | |
| 2601 | #else /* !(FEAT_GUI_GNOME && FEAT_SESSION) */ |
| 2602 | |
| 2603 | # ifdef USE_XSMP |
| 2604 | /* |
| 2605 | * GTK tells us that XSMP needs attention |
| 2606 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2607 | static gboolean |
Bram Moolenaar | 66f948e | 2016-01-30 16:39:25 +0100 | [diff] [blame] | 2608 | local_xsmp_handle_requests( |
| 2609 | GIOChannel *source UNUSED, |
| 2610 | GIOCondition condition, |
| 2611 | gpointer data) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2612 | { |
| 2613 | if (condition == G_IO_IN) |
| 2614 | { |
| 2615 | /* Do stuff; maybe close connection */ |
| 2616 | if (xsmp_handle_requests() == FAIL) |
| 2617 | g_io_channel_unref((GIOChannel *)data); |
| 2618 | return TRUE; |
| 2619 | } |
| 2620 | /* Error */ |
| 2621 | g_io_channel_unref((GIOChannel *)data); |
| 2622 | xsmp_close(); |
| 2623 | return TRUE; |
| 2624 | } |
| 2625 | # endif /* USE_XSMP */ |
| 2626 | |
| 2627 | /* |
| 2628 | * Setup the WM_PROTOCOLS to indicate we want the WM_SAVE_YOURSELF event. |
| 2629 | * This is an ugly use of X functions. GTK doesn't offer an alternative. |
| 2630 | */ |
| 2631 | static void |
| 2632 | setup_save_yourself(void) |
| 2633 | { |
| 2634 | Atom *existing_atoms = NULL; |
| 2635 | int count = 0; |
| 2636 | |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 2637 | # ifdef USE_XSMP |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2638 | if (xsmp_icefd != -1) |
| 2639 | { |
| 2640 | /* |
| 2641 | * Use XSMP is preference to legacy WM_SAVE_YOURSELF; |
| 2642 | * set up GTK IO monitor |
| 2643 | */ |
| 2644 | GIOChannel *g_io = g_io_channel_unix_new(xsmp_icefd); |
| 2645 | |
| 2646 | g_io_add_watch(g_io, G_IO_IN | G_IO_ERR | G_IO_HUP, |
| 2647 | local_xsmp_handle_requests, (gpointer)g_io); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 2648 | g_io_channel_unref(g_io); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2649 | } |
| 2650 | else |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 2651 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2652 | { |
| 2653 | /* Fall back to old method */ |
| 2654 | |
| 2655 | /* first get the existing value */ |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 2656 | # if GTK_CHECK_VERSION(3,0,0) |
| 2657 | GdkWindow * const mainwin_win = gtk_widget_get_window(gui.mainwin); |
| 2658 | |
| 2659 | if (XGetWMProtocols(GDK_WINDOW_XDISPLAY(mainwin_win), |
| 2660 | GDK_WINDOW_XID(mainwin_win), |
| 2661 | &existing_atoms, &count)) |
| 2662 | # else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2663 | if (XGetWMProtocols(GDK_WINDOW_XDISPLAY(gui.mainwin->window), |
| 2664 | GDK_WINDOW_XWINDOW(gui.mainwin->window), |
| 2665 | &existing_atoms, &count)) |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 2666 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2667 | { |
| 2668 | Atom *new_atoms; |
| 2669 | Atom save_yourself_xatom; |
| 2670 | int i; |
| 2671 | |
| 2672 | save_yourself_xatom = GET_X_ATOM(save_yourself_atom); |
| 2673 | |
| 2674 | /* check if WM_SAVE_YOURSELF isn't there yet */ |
| 2675 | for (i = 0; i < count; ++i) |
| 2676 | if (existing_atoms[i] == save_yourself_xatom) |
| 2677 | break; |
| 2678 | |
| 2679 | if (i == count) |
| 2680 | { |
| 2681 | /* allocate an Atoms array which is one item longer */ |
| 2682 | new_atoms = (Atom *)alloc((unsigned)((count + 1) |
| 2683 | * sizeof(Atom))); |
| 2684 | if (new_atoms != NULL) |
| 2685 | { |
| 2686 | memcpy(new_atoms, existing_atoms, count * sizeof(Atom)); |
| 2687 | new_atoms[count] = save_yourself_xatom; |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 2688 | # if GTK_CHECK_VERSION(3,0,0) |
| 2689 | XSetWMProtocols(GDK_WINDOW_XDISPLAY(mainwin_win), |
| 2690 | GDK_WINDOW_XID(mainwin_win), |
| 2691 | # else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2692 | XSetWMProtocols(GDK_WINDOW_XDISPLAY(gui.mainwin->window), |
| 2693 | GDK_WINDOW_XWINDOW(gui.mainwin->window), |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 2694 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2695 | new_atoms, count + 1); |
| 2696 | vim_free(new_atoms); |
| 2697 | } |
| 2698 | } |
| 2699 | XFree(existing_atoms); |
| 2700 | } |
| 2701 | } |
| 2702 | } |
| 2703 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2704 | /* |
| 2705 | * Installing a global event filter seems to be the only way to catch |
| 2706 | * client messages of type WM_PROTOCOLS without overriding GDK's own |
| 2707 | * client message event filter. Well, that's still better than trying |
| 2708 | * 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] | 2709 | * |
| 2710 | * GTK2_FIXME: This doesn't seem to work. For some reason we never |
| 2711 | * receive WM_SAVE_YOURSELF even though everything is set up correctly. |
| 2712 | * I have the nasty feeling modern session managers just don't send this |
| 2713 | * deprecated message anymore. Addition: confirmed by several people. |
| 2714 | * |
| 2715 | * The GNOME session support is much cooler anyway. Unlike this ugly |
| 2716 | * WM_SAVE_YOURSELF hack it actually stores the session... And yes, |
| 2717 | * it should work with KDE as well. |
| 2718 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2719 | static GdkFilterReturn |
Bram Moolenaar | b85cb21 | 2009-05-17 14:24:23 +0000 | [diff] [blame] | 2720 | global_event_filter(GdkXEvent *xev, |
| 2721 | GdkEvent *event UNUSED, |
| 2722 | gpointer data UNUSED) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2723 | { |
| 2724 | XEvent *xevent = (XEvent *)xev; |
| 2725 | |
| 2726 | if (xevent != NULL |
| 2727 | && xevent->type == ClientMessage |
| 2728 | && xevent->xclient.message_type == GET_X_ATOM(wm_protocols_atom) |
Bram Moolenaar | b85cb21 | 2009-05-17 14:24:23 +0000 | [diff] [blame] | 2729 | && (long_u)xevent->xclient.data.l[0] |
| 2730 | == GET_X_ATOM(save_yourself_atom)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2731 | { |
| 2732 | out_flush(); |
| 2733 | ml_sync_all(FALSE, FALSE); /* preserve all swap files */ |
| 2734 | /* |
| 2735 | * Set the window's WM_COMMAND property, to let the window manager |
| 2736 | * know we are done saving ourselves. We don't want to be |
| 2737 | * restarted, thus set argv to NULL. |
| 2738 | */ |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 2739 | # if GTK_CHECK_VERSION(3,0,0) |
| 2740 | XSetCommand(GDK_WINDOW_XDISPLAY(gtk_widget_get_window(gui.mainwin)), |
| 2741 | GDK_WINDOW_XID(gtk_widget_get_window(gui.mainwin)), |
| 2742 | # else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2743 | XSetCommand(GDK_WINDOW_XDISPLAY(gui.mainwin->window), |
| 2744 | GDK_WINDOW_XWINDOW(gui.mainwin->window), |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 2745 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2746 | NULL, 0); |
| 2747 | return GDK_FILTER_REMOVE; |
| 2748 | } |
| 2749 | |
| 2750 | return GDK_FILTER_CONTINUE; |
| 2751 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2752 | #endif /* !(FEAT_GUI_GNOME && FEAT_SESSION) */ |
| 2753 | |
| 2754 | |
| 2755 | /* |
| 2756 | * Setup the window icon & xcmdsrv comm after the main window has been realized. |
| 2757 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2758 | static void |
Bram Moolenaar | b85cb21 | 2009-05-17 14:24:23 +0000 | [diff] [blame] | 2759 | mainwin_realize(GtkWidget *widget UNUSED, gpointer data UNUSED) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2760 | { |
| 2761 | /* If you get an error message here, you still need to unpack the runtime |
| 2762 | * archive! */ |
| 2763 | #ifdef magick |
| 2764 | # undef magick |
| 2765 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2766 | /* A bit hackish, but avoids casting later and allows optimization */ |
| 2767 | # define static static const |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2768 | #define magick vim32x32 |
| 2769 | #include "../runtime/vim32x32.xpm" |
| 2770 | #undef magick |
| 2771 | #define magick vim16x16 |
| 2772 | #include "../runtime/vim16x16.xpm" |
| 2773 | #undef magick |
| 2774 | #define magick vim48x48 |
| 2775 | #include "../runtime/vim48x48.xpm" |
| 2776 | #undef magick |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2777 | # undef static |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2778 | |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 2779 | #if GTK_CHECK_VERSION(3,0,0) |
| 2780 | GdkWindow * const mainwin_win = gtk_widget_get_window(gui.mainwin); |
| 2781 | #endif |
| 2782 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2783 | /* When started with "--echo-wid" argument, write window ID on stdout. */ |
| 2784 | if (echo_wid_arg) |
| 2785 | { |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 2786 | #if GTK_CHECK_VERSION(3,0,0) |
| 2787 | printf("WID: %ld\n", (long)GDK_WINDOW_XID(mainwin_win)); |
| 2788 | #else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2789 | printf("WID: %ld\n", (long)GDK_WINDOW_XWINDOW(gui.mainwin->window)); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 2790 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2791 | fflush(stdout); |
| 2792 | } |
| 2793 | |
| 2794 | if (vim_strchr(p_go, GO_ICON) != NULL) |
| 2795 | { |
| 2796 | /* |
| 2797 | * Add an icon to the main window. For fun and convenience of the user. |
| 2798 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2799 | GList *icons = NULL; |
| 2800 | |
| 2801 | icons = g_list_prepend(icons, gdk_pixbuf_new_from_xpm_data(vim16x16)); |
| 2802 | icons = g_list_prepend(icons, gdk_pixbuf_new_from_xpm_data(vim32x32)); |
| 2803 | icons = g_list_prepend(icons, gdk_pixbuf_new_from_xpm_data(vim48x48)); |
| 2804 | |
| 2805 | gtk_window_set_icon_list(GTK_WINDOW(gui.mainwin), icons); |
| 2806 | |
| 2807 | g_list_foreach(icons, (GFunc)&g_object_unref, NULL); |
| 2808 | g_list_free(icons); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2809 | } |
| 2810 | |
| 2811 | #if !(defined(FEAT_GUI_GNOME) && defined(FEAT_SESSION)) |
| 2812 | /* 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] | 2813 | gdk_window_add_filter(NULL, &global_event_filter, NULL); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2814 | #endif |
| 2815 | /* Setup to indicate to the window manager that we want to catch the |
| 2816 | * WM_SAVE_YOURSELF event. For GNOME, this connects to the session |
| 2817 | * manager instead. */ |
| 2818 | #if defined(FEAT_GUI_GNOME) && defined(FEAT_SESSION) |
| 2819 | if (using_gnome) |
| 2820 | #endif |
| 2821 | setup_save_yourself(); |
| 2822 | |
| 2823 | #ifdef FEAT_CLIENTSERVER |
| 2824 | if (serverName == NULL && serverDelayedStartName != NULL) |
| 2825 | { |
| 2826 | /* This is a :gui command in a plain vim with no previous server */ |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 2827 | # if GTK_CHECK_VERSION(3,0,0) |
| 2828 | commWindow = GDK_WINDOW_XID(mainwin_win); |
| 2829 | |
| 2830 | (void)serverRegisterName(GDK_WINDOW_XDISPLAY(mainwin_win), |
| 2831 | serverDelayedStartName); |
| 2832 | # else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2833 | commWindow = GDK_WINDOW_XWINDOW(gui.mainwin->window); |
| 2834 | |
| 2835 | (void)serverRegisterName(GDK_WINDOW_XDISPLAY(gui.mainwin->window), |
| 2836 | serverDelayedStartName); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 2837 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2838 | } |
| 2839 | else |
| 2840 | { |
| 2841 | /* |
| 2842 | * Cannot handle "XLib-only" windows with gtk event routines, we'll |
| 2843 | * have to change the "server" registration to that of the main window |
Bram Moolenaar | b463e8d | 2017-06-05 15:07:09 +0200 | [diff] [blame] | 2844 | * If we have not registered a name yet, remember the window. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2845 | */ |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 2846 | # if GTK_CHECK_VERSION(3,0,0) |
| 2847 | serverChangeRegisteredWindow(GDK_WINDOW_XDISPLAY(mainwin_win), |
| 2848 | GDK_WINDOW_XID(mainwin_win)); |
| 2849 | # else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2850 | serverChangeRegisteredWindow(GDK_WINDOW_XDISPLAY(gui.mainwin->window), |
| 2851 | GDK_WINDOW_XWINDOW(gui.mainwin->window)); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 2852 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2853 | } |
| 2854 | gtk_widget_add_events(gui.mainwin, GDK_PROPERTY_CHANGE_MASK); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 2855 | # if GTK_CHECK_VERSION(3,0,0) |
| 2856 | g_signal_connect(G_OBJECT(gui.mainwin), "property-notify-event", |
| 2857 | G_CALLBACK(property_event), NULL); |
| 2858 | # else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2859 | gtk_signal_connect(GTK_OBJECT(gui.mainwin), "property_notify_event", |
| 2860 | GTK_SIGNAL_FUNC(property_event), NULL); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 2861 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2862 | #endif |
| 2863 | } |
| 2864 | |
| 2865 | static GdkCursor * |
| 2866 | create_blank_pointer(void) |
| 2867 | { |
| 2868 | GdkWindow *root_window = NULL; |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 2869 | #if GTK_CHECK_VERSION(3,0,0) |
| 2870 | GdkPixbuf *blank_mask; |
| 2871 | #else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2872 | GdkPixmap *blank_mask; |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 2873 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2874 | GdkCursor *cursor; |
Bram Moolenaar | 36edf06 | 2016-07-21 22:10:12 +0200 | [diff] [blame] | 2875 | #if GTK_CHECK_VERSION(3,0,0) |
| 2876 | GdkRGBA color = { 0.0, 0.0, 0.0, 1.0 }; |
| 2877 | #else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2878 | GdkColor color = { 0, 0, 0, 0 }; |
| 2879 | char blank_data[] = { 0x0 }; |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 2880 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2881 | |
Bram Moolenaar | b463e8d | 2017-06-05 15:07:09 +0200 | [diff] [blame] | 2882 | #if GTK_CHECK_VERSION(3,12,0) |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 2883 | { |
| 2884 | GdkWindow * const win = gtk_widget_get_window(gui.mainwin); |
| 2885 | GdkScreen * const scrn = gdk_window_get_screen(win); |
| 2886 | root_window = gdk_screen_get_root_window(scrn); |
| 2887 | } |
Bram Moolenaar | b463e8d | 2017-06-05 15:07:09 +0200 | [diff] [blame] | 2888 | #else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2889 | root_window = gtk_widget_get_root_window(gui.mainwin); |
| 2890 | #endif |
| 2891 | |
| 2892 | /* Create a pseudo blank pointer, which is in fact one pixel by one pixel |
| 2893 | * in size. */ |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 2894 | #if GTK_CHECK_VERSION(3,0,0) |
| 2895 | { |
| 2896 | cairo_surface_t *surf; |
| 2897 | cairo_t *cr; |
| 2898 | |
| 2899 | surf = cairo_image_surface_create(CAIRO_FORMAT_A1, 1, 1); |
| 2900 | cr = cairo_create(surf); |
| 2901 | |
Bram Moolenaar | 36edf06 | 2016-07-21 22:10:12 +0200 | [diff] [blame] | 2902 | cairo_set_source_rgba(cr, |
| 2903 | color.red, |
| 2904 | color.green, |
| 2905 | color.blue, |
| 2906 | color.alpha); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 2907 | cairo_rectangle(cr, 0, 0, 1, 1); |
| 2908 | cairo_fill(cr); |
| 2909 | cairo_destroy(cr); |
| 2910 | |
| 2911 | blank_mask = gdk_pixbuf_get_from_surface(surf, 0, 0, 1, 1); |
| 2912 | cairo_surface_destroy(surf); |
| 2913 | |
| 2914 | cursor = gdk_cursor_new_from_pixbuf(gdk_window_get_display(root_window), |
| 2915 | blank_mask, 0, 0); |
| 2916 | g_object_unref(blank_mask); |
| 2917 | } |
| 2918 | #else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2919 | blank_mask = gdk_bitmap_create_from_data(root_window, blank_data, 1, 1); |
| 2920 | cursor = gdk_cursor_new_from_pixmap(blank_mask, blank_mask, |
| 2921 | &color, &color, 0, 0); |
| 2922 | gdk_bitmap_unref(blank_mask); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 2923 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2924 | |
| 2925 | return cursor; |
| 2926 | } |
| 2927 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2928 | static void |
| 2929 | mainwin_screen_changed_cb(GtkWidget *widget, |
Bram Moolenaar | b85cb21 | 2009-05-17 14:24:23 +0000 | [diff] [blame] | 2930 | GdkScreen *previous_screen UNUSED, |
| 2931 | gpointer data UNUSED) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2932 | { |
| 2933 | if (!gtk_widget_has_screen(widget)) |
| 2934 | return; |
| 2935 | |
| 2936 | /* |
Bram Moolenaar | 4932594 | 2007-05-10 19:19:59 +0000 | [diff] [blame] | 2937 | * Recreate the invisible mouse cursor. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2938 | */ |
| 2939 | if (gui.blank_pointer != NULL) |
Bram Moolenaar | b463e8d | 2017-06-05 15:07:09 +0200 | [diff] [blame] | 2940 | #if GTK_CHECK_VERSION(3,0,0) |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 2941 | g_object_unref(G_OBJECT(gui.blank_pointer)); |
Bram Moolenaar | b463e8d | 2017-06-05 15:07:09 +0200 | [diff] [blame] | 2942 | #else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2943 | gdk_cursor_unref(gui.blank_pointer); |
Bram Moolenaar | b463e8d | 2017-06-05 15:07:09 +0200 | [diff] [blame] | 2944 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2945 | |
| 2946 | gui.blank_pointer = create_blank_pointer(); |
| 2947 | |
Bram Moolenaar | b463e8d | 2017-06-05 15:07:09 +0200 | [diff] [blame] | 2948 | #if GTK_CHECK_VERSION(3,0,0) |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 2949 | if (gui.pointer_hidden && gtk_widget_get_window(gui.drawarea) != NULL) |
| 2950 | gdk_window_set_cursor(gtk_widget_get_window(gui.drawarea), |
| 2951 | gui.blank_pointer); |
Bram Moolenaar | b463e8d | 2017-06-05 15:07:09 +0200 | [diff] [blame] | 2952 | #else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2953 | if (gui.pointer_hidden && gui.drawarea->window != NULL) |
| 2954 | gdk_window_set_cursor(gui.drawarea->window, gui.blank_pointer); |
Bram Moolenaar | b463e8d | 2017-06-05 15:07:09 +0200 | [diff] [blame] | 2955 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2956 | |
| 2957 | /* |
| 2958 | * Create a new PangoContext for this screen, and initialize it |
| 2959 | * with the current font if necessary. |
| 2960 | */ |
| 2961 | if (gui.text_context != NULL) |
| 2962 | g_object_unref(gui.text_context); |
| 2963 | |
| 2964 | gui.text_context = gtk_widget_create_pango_context(widget); |
| 2965 | pango_context_set_base_dir(gui.text_context, PANGO_DIRECTION_LTR); |
| 2966 | |
| 2967 | if (gui.norm_font != NULL) |
| 2968 | { |
Bram Moolenaar | 46c9c73 | 2004-12-12 11:37:09 +0000 | [diff] [blame] | 2969 | gui_mch_init_font(p_guifont, FALSE); |
Bram Moolenaar | 8ac4415 | 2017-11-09 18:33:29 +0100 | [diff] [blame] | 2970 | gui_set_shellsize(TRUE, FALSE, RESIZE_BOTH); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2971 | } |
| 2972 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2973 | |
| 2974 | /* |
| 2975 | * After the drawing area comes up, we calculate all colors and create the |
| 2976 | * dummy blank cursor. |
| 2977 | * |
| 2978 | * Don't try to set any VIM scrollbar sizes anywhere here. I'm relying on the |
| 2979 | * fact that the main VIM engine doesn't take them into account anywhere. |
| 2980 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2981 | static void |
Bram Moolenaar | b85cb21 | 2009-05-17 14:24:23 +0000 | [diff] [blame] | 2982 | drawarea_realize_cb(GtkWidget *widget, gpointer data UNUSED) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2983 | { |
| 2984 | GtkWidget *sbar; |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 2985 | #if GTK_CHECK_VERSION(3,0,0) |
| 2986 | GtkAllocation allocation; |
| 2987 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2988 | |
| 2989 | #ifdef FEAT_XIM |
| 2990 | xim_init(); |
| 2991 | #endif |
| 2992 | gui_mch_new_colors(); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 2993 | #if GTK_CHECK_VERSION(3,0,0) |
| 2994 | gui.surface = gdk_window_create_similar_surface( |
| 2995 | gtk_widget_get_window(widget), |
| 2996 | CAIRO_CONTENT_COLOR_ALPHA, |
| 2997 | gtk_widget_get_allocated_width(widget), |
| 2998 | gtk_widget_get_allocated_height(widget)); |
| 2999 | #else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3000 | gui.text_gc = gdk_gc_new(gui.drawarea->window); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 3001 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3002 | |
| 3003 | gui.blank_pointer = create_blank_pointer(); |
| 3004 | if (gui.pointer_hidden) |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 3005 | #if GTK_CHECK_VERSION(3,0,0) |
| 3006 | gdk_window_set_cursor(gtk_widget_get_window(widget), gui.blank_pointer); |
| 3007 | #else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3008 | gdk_window_set_cursor(widget->window, gui.blank_pointer); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 3009 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3010 | |
| 3011 | /* get the actual size of the scrollbars, if they are realized */ |
| 3012 | sbar = firstwin->w_scrollbars[SBAR_LEFT].id; |
| 3013 | if (!sbar || (!gui.which_scrollbars[SBAR_LEFT] |
| 3014 | && firstwin->w_scrollbars[SBAR_RIGHT].id)) |
| 3015 | sbar = firstwin->w_scrollbars[SBAR_RIGHT].id; |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 3016 | #if GTK_CHECK_VERSION(3,0,0) |
| 3017 | gtk_widget_get_allocation(sbar, &allocation); |
| 3018 | if (sbar && gtk_widget_get_realized(sbar) && allocation.width) |
| 3019 | gui.scrollbar_width = allocation.width; |
| 3020 | #else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3021 | if (sbar && GTK_WIDGET_REALIZED(sbar) && sbar->allocation.width) |
| 3022 | gui.scrollbar_width = sbar->allocation.width; |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 3023 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3024 | |
| 3025 | sbar = gui.bottom_sbar.id; |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 3026 | #if GTK_CHECK_VERSION(3,0,0) |
| 3027 | if (sbar && gtk_widget_get_realized(sbar) && allocation.height) |
| 3028 | gui.scrollbar_height = allocation.height; |
| 3029 | #else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3030 | if (sbar && GTK_WIDGET_REALIZED(sbar) && sbar->allocation.height) |
| 3031 | gui.scrollbar_height = sbar->allocation.height; |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 3032 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3033 | } |
| 3034 | |
| 3035 | /* |
| 3036 | * Properly clean up on shutdown. |
| 3037 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3038 | static void |
Bram Moolenaar | b85cb21 | 2009-05-17 14:24:23 +0000 | [diff] [blame] | 3039 | drawarea_unrealize_cb(GtkWidget *widget UNUSED, gpointer data UNUSED) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3040 | { |
| 3041 | /* Don't write messages to the GUI anymore */ |
| 3042 | full_screen = FALSE; |
| 3043 | |
| 3044 | #ifdef FEAT_XIM |
| 3045 | im_shutdown(); |
| 3046 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3047 | if (gui.ascii_glyphs != NULL) |
| 3048 | { |
| 3049 | pango_glyph_string_free(gui.ascii_glyphs); |
| 3050 | gui.ascii_glyphs = NULL; |
| 3051 | } |
| 3052 | if (gui.ascii_font != NULL) |
| 3053 | { |
| 3054 | g_object_unref(gui.ascii_font); |
| 3055 | gui.ascii_font = NULL; |
| 3056 | } |
| 3057 | g_object_unref(gui.text_context); |
| 3058 | gui.text_context = NULL; |
| 3059 | |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 3060 | #if GTK_CHECK_VERSION(3,0,0) |
| 3061 | if (gui.surface != NULL) |
| 3062 | { |
| 3063 | cairo_surface_destroy(gui.surface); |
| 3064 | gui.surface = NULL; |
| 3065 | } |
| 3066 | #else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3067 | g_object_unref(gui.text_gc); |
| 3068 | gui.text_gc = NULL; |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 3069 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3070 | |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 3071 | #if GTK_CHECK_VERSION(3,0,0) |
| 3072 | g_object_unref(G_OBJECT(gui.blank_pointer)); |
| 3073 | #else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3074 | gdk_cursor_unref(gui.blank_pointer); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 3075 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3076 | gui.blank_pointer = NULL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3077 | } |
| 3078 | |
Bram Moolenaar | a859f04 | 2016-11-17 19:11:55 +0100 | [diff] [blame] | 3079 | #if GTK_CHECK_VERSION(3,22,2) |
| 3080 | static void |
| 3081 | drawarea_style_updated_cb(GtkWidget *widget UNUSED, |
| 3082 | gpointer data UNUSED) |
| 3083 | #else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3084 | static void |
Bram Moolenaar | b85cb21 | 2009-05-17 14:24:23 +0000 | [diff] [blame] | 3085 | drawarea_style_set_cb(GtkWidget *widget UNUSED, |
| 3086 | GtkStyle *previous_style UNUSED, |
| 3087 | gpointer data UNUSED) |
Bram Moolenaar | a859f04 | 2016-11-17 19:11:55 +0100 | [diff] [blame] | 3088 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3089 | { |
| 3090 | gui_mch_new_colors(); |
| 3091 | } |
| 3092 | |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 3093 | #if GTK_CHECK_VERSION(3,0,0) |
| 3094 | static gboolean |
| 3095 | drawarea_configure_event_cb(GtkWidget *widget, |
| 3096 | GdkEventConfigure *event, |
| 3097 | gpointer data UNUSED) |
| 3098 | { |
| 3099 | static int cur_width = 0; |
| 3100 | static int cur_height = 0; |
| 3101 | |
| 3102 | g_return_val_if_fail(event |
| 3103 | && event->width >= 1 && event->height >= 1, TRUE); |
| 3104 | |
Bram Moolenaar | 182707a | 2016-11-21 20:55:58 +0100 | [diff] [blame] | 3105 | # if GTK_CHECK_VERSION(3,22,2) && !GTK_CHECK_VERSION(3,22,4) |
Bram Moolenaar | a859f04 | 2016-11-17 19:11:55 +0100 | [diff] [blame] | 3106 | /* As of 3.22.2, GdkWindows have started distributing configure events to |
| 3107 | * their "native" children (https://git.gnome.org/browse/gtk+/commit/?h=gtk-3-22&id=12579fe71b3b8f79eb9c1b80e429443bcc437dd0). |
| 3108 | * |
| 3109 | * As can be seen from the implementation of move_native_children() and |
| 3110 | * configure_native_child() in gdkwindow.c, those functions actually |
| 3111 | * propagate configure events to every child, failing to distinguish |
| 3112 | * "native" one from non-native one. |
| 3113 | * |
| 3114 | * Naturally, configure events propagated to here like that are fallacious |
| 3115 | * and, as a matter of fact, they trigger a geometric collapse of |
| 3116 | * gui.drawarea in fullscreen and miximized modes. |
| 3117 | * |
| 3118 | * To filter out such nuisance events, we are making use of the fact that |
| 3119 | * the field send_event of such GdkEventConfigures is set to FALSE in |
| 3120 | * configure_native_child(). |
| 3121 | * |
| 3122 | * Obviously, this is a terrible hack making GVim depend on GTK's |
| 3123 | * implementation details. Therefore, watch out any relevant internal |
| 3124 | * changes happening in GTK in the feature (sigh). |
| 3125 | */ |
Bram Moolenaar | 182707a | 2016-11-21 20:55:58 +0100 | [diff] [blame] | 3126 | /* Follow-up |
| 3127 | * After a few weeks later, the GdkWindow change mentioned above was |
| 3128 | * reverted (https://git.gnome.org/browse/gtk+/commit/?h=gtk-3-22&id=f70039cb9603a02d2369fec4038abf40a1711155). |
| 3129 | * The corresponding official release is 3.22.4. */ |
Bram Moolenaar | a859f04 | 2016-11-17 19:11:55 +0100 | [diff] [blame] | 3130 | if (event->send_event == FALSE) |
| 3131 | return TRUE; |
| 3132 | # endif |
| 3133 | |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 3134 | if (event->width == cur_width && event->height == cur_height) |
| 3135 | return TRUE; |
| 3136 | |
| 3137 | cur_width = event->width; |
| 3138 | cur_height = event->height; |
| 3139 | |
| 3140 | if (gui.surface != NULL) |
| 3141 | cairo_surface_destroy(gui.surface); |
| 3142 | |
| 3143 | gui.surface = gdk_window_create_similar_surface( |
| 3144 | gtk_widget_get_window(widget), |
| 3145 | CAIRO_CONTENT_COLOR_ALPHA, |
| 3146 | event->width, event->height); |
| 3147 | |
| 3148 | gtk_widget_queue_draw(widget); |
| 3149 | |
| 3150 | return TRUE; |
| 3151 | } |
| 3152 | #endif |
| 3153 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3154 | /* |
| 3155 | * Callback routine for the "delete_event" signal on the toplevel window. |
| 3156 | * Tries to vim gracefully, or refuses to exit with changed buffers. |
| 3157 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3158 | static gint |
Bram Moolenaar | b85cb21 | 2009-05-17 14:24:23 +0000 | [diff] [blame] | 3159 | delete_event_cb(GtkWidget *widget UNUSED, |
| 3160 | GdkEventAny *event UNUSED, |
| 3161 | gpointer data UNUSED) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3162 | { |
| 3163 | gui_shell_closed(); |
| 3164 | return TRUE; |
| 3165 | } |
| 3166 | |
Bram Moolenaar | 32466aa | 2006-02-24 23:53:04 +0000 | [diff] [blame] | 3167 | #if defined(FEAT_MENU) || defined(FEAT_TOOLBAR) || defined(FEAT_GUI_TABLINE) |
| 3168 | static int |
| 3169 | get_item_dimensions(GtkWidget *widget, GtkOrientation orientation) |
| 3170 | { |
Bram Moolenaar | c4a249a | 2017-01-30 22:56:48 +0100 | [diff] [blame] | 3171 | # ifdef FEAT_GUI_GNOME |
Bram Moolenaar | 32466aa | 2006-02-24 23:53:04 +0000 | [diff] [blame] | 3172 | GtkOrientation item_orientation = GTK_ORIENTATION_HORIZONTAL; |
| 3173 | |
Bram Moolenaar | 32466aa | 2006-02-24 23:53:04 +0000 | [diff] [blame] | 3174 | if (using_gnome && widget != NULL) |
| 3175 | { |
Bram Moolenaar | c9b4b05 | 2006-04-30 18:54:39 +0000 | [diff] [blame] | 3176 | GtkWidget *parent; |
Bram Moolenaar | 32466aa | 2006-02-24 23:53:04 +0000 | [diff] [blame] | 3177 | BonoboDockItem *dockitem; |
| 3178 | |
Bram Moolenaar | b3656ed | 2006-03-20 21:59:49 +0000 | [diff] [blame] | 3179 | parent = gtk_widget_get_parent(widget); |
Bram Moolenaar | c9b4b05 | 2006-04-30 18:54:39 +0000 | [diff] [blame] | 3180 | if (G_TYPE_FROM_INSTANCE(parent) == BONOBO_TYPE_DOCK_ITEM) |
| 3181 | { |
| 3182 | /* Only menu & toolbar are dock items. Could tabline be? |
| 3183 | * Seem to be only the 2 defined in GNOME */ |
| 3184 | widget = parent; |
| 3185 | dockitem = BONOBO_DOCK_ITEM(widget); |
Bram Moolenaar | 32466aa | 2006-02-24 23:53:04 +0000 | [diff] [blame] | 3186 | |
Bram Moolenaar | c9b4b05 | 2006-04-30 18:54:39 +0000 | [diff] [blame] | 3187 | if (dockitem == NULL || dockitem->is_floating) |
| 3188 | return 0; |
| 3189 | item_orientation = bonobo_dock_item_get_orientation(dockitem); |
| 3190 | } |
Bram Moolenaar | 32466aa | 2006-02-24 23:53:04 +0000 | [diff] [blame] | 3191 | } |
Bram Moolenaar | c4a249a | 2017-01-30 22:56:48 +0100 | [diff] [blame] | 3192 | # else |
| 3193 | # define item_orientation GTK_ORIENTATION_HORIZONTAL |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 3194 | # endif |
Bram Moolenaar | c4a249a | 2017-01-30 22:56:48 +0100 | [diff] [blame] | 3195 | |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 3196 | # if GTK_CHECK_VERSION(3,0,0) |
| 3197 | if (widget != NULL |
| 3198 | && item_orientation == orientation |
| 3199 | && gtk_widget_get_realized(widget) |
| 3200 | && gtk_widget_get_visible(widget)) |
| 3201 | # else |
Bram Moolenaar | 32466aa | 2006-02-24 23:53:04 +0000 | [diff] [blame] | 3202 | if (widget != NULL |
| 3203 | && item_orientation == orientation |
| 3204 | && GTK_WIDGET_REALIZED(widget) |
| 3205 | && GTK_WIDGET_VISIBLE(widget)) |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 3206 | # endif |
Bram Moolenaar | 32466aa | 2006-02-24 23:53:04 +0000 | [diff] [blame] | 3207 | { |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 3208 | # if GTK_CHECK_VERSION(3,0,0) |
| 3209 | GtkAllocation allocation; |
| 3210 | |
| 3211 | gtk_widget_get_allocation(widget, &allocation); |
Bram Moolenaar | c4a249a | 2017-01-30 22:56:48 +0100 | [diff] [blame] | 3212 | return allocation.height; |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 3213 | # else |
Bram Moolenaar | c4a249a | 2017-01-30 22:56:48 +0100 | [diff] [blame] | 3214 | # ifdef FEAT_GUI_GNOME |
Bram Moolenaar | 32466aa | 2006-02-24 23:53:04 +0000 | [diff] [blame] | 3215 | if (orientation == GTK_ORIENTATION_HORIZONTAL) |
| 3216 | return widget->allocation.height; |
| 3217 | else |
| 3218 | return widget->allocation.width; |
Bram Moolenaar | c4a249a | 2017-01-30 22:56:48 +0100 | [diff] [blame] | 3219 | # else |
| 3220 | return widget->allocation.height; |
| 3221 | # endif |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 3222 | # endif |
Bram Moolenaar | 32466aa | 2006-02-24 23:53:04 +0000 | [diff] [blame] | 3223 | } |
| 3224 | return 0; |
| 3225 | } |
| 3226 | #endif |
| 3227 | |
| 3228 | static int |
| 3229 | get_menu_tool_width(void) |
| 3230 | { |
| 3231 | int width = 0; |
| 3232 | |
| 3233 | #ifdef FEAT_GUI_GNOME /* these are never vertical without GNOME */ |
| 3234 | # ifdef FEAT_MENU |
| 3235 | width += get_item_dimensions(gui.menubar, GTK_ORIENTATION_VERTICAL); |
| 3236 | # endif |
| 3237 | # ifdef FEAT_TOOLBAR |
| 3238 | width += get_item_dimensions(gui.toolbar, GTK_ORIENTATION_VERTICAL); |
| 3239 | # endif |
| 3240 | # ifdef FEAT_GUI_TABLINE |
Bram Moolenaar | 97b2ad3 | 2006-03-18 21:40:56 +0000 | [diff] [blame] | 3241 | if (gui.tabline != NULL) |
| 3242 | width += get_item_dimensions(gui.tabline, GTK_ORIENTATION_VERTICAL); |
Bram Moolenaar | 32466aa | 2006-02-24 23:53:04 +0000 | [diff] [blame] | 3243 | # endif |
| 3244 | #endif |
| 3245 | |
| 3246 | return width; |
| 3247 | } |
| 3248 | |
| 3249 | static int |
| 3250 | get_menu_tool_height(void) |
| 3251 | { |
| 3252 | int height = 0; |
| 3253 | |
| 3254 | #ifdef FEAT_MENU |
| 3255 | height += get_item_dimensions(gui.menubar, GTK_ORIENTATION_HORIZONTAL); |
| 3256 | #endif |
| 3257 | #ifdef FEAT_TOOLBAR |
| 3258 | height += get_item_dimensions(gui.toolbar, GTK_ORIENTATION_HORIZONTAL); |
| 3259 | #endif |
| 3260 | #ifdef FEAT_GUI_TABLINE |
Bram Moolenaar | 97b2ad3 | 2006-03-18 21:40:56 +0000 | [diff] [blame] | 3261 | if (gui.tabline != NULL) |
| 3262 | height += get_item_dimensions(gui.tabline, GTK_ORIENTATION_HORIZONTAL); |
Bram Moolenaar | 32466aa | 2006-02-24 23:53:04 +0000 | [diff] [blame] | 3263 | #endif |
| 3264 | |
| 3265 | return height; |
| 3266 | } |
| 3267 | |
Bram Moolenaar | b3656ed | 2006-03-20 21:59:49 +0000 | [diff] [blame] | 3268 | /* This controls whether we can set the real window hints at |
| 3269 | * start-up when in a GtkPlug. |
| 3270 | * 0 = normal processing (default) |
| 3271 | * 1 = init. hints set, no-one's tried to reset since last check |
| 3272 | * 2 = init. hints set, attempt made to change hints |
| 3273 | */ |
| 3274 | static int init_window_hints_state = 0; |
| 3275 | |
Bram Moolenaar | 32466aa | 2006-02-24 23:53:04 +0000 | [diff] [blame] | 3276 | static void |
Bram Moolenaar | b3656ed | 2006-03-20 21:59:49 +0000 | [diff] [blame] | 3277 | update_window_manager_hints(int force_width, int force_height) |
Bram Moolenaar | 32466aa | 2006-02-24 23:53:04 +0000 | [diff] [blame] | 3278 | { |
| 3279 | static int old_width = 0; |
| 3280 | static int old_height = 0; |
Bram Moolenaar | b3656ed | 2006-03-20 21:59:49 +0000 | [diff] [blame] | 3281 | static int old_min_width = 0; |
| 3282 | static int old_min_height = 0; |
Bram Moolenaar | 32466aa | 2006-02-24 23:53:04 +0000 | [diff] [blame] | 3283 | static int old_char_width = 0; |
| 3284 | static int old_char_height = 0; |
| 3285 | |
| 3286 | int width; |
| 3287 | int height; |
Bram Moolenaar | b3656ed | 2006-03-20 21:59:49 +0000 | [diff] [blame] | 3288 | int min_width; |
| 3289 | int min_height; |
| 3290 | |
| 3291 | /* At start-up, don't try to set the hints until the initial |
| 3292 | * values have been used (those that dictate our initial size) |
Bram Moolenaar | b85cb21 | 2009-05-17 14:24:23 +0000 | [diff] [blame] | 3293 | * Let forced (i.e., correct) values through always. |
Bram Moolenaar | b3656ed | 2006-03-20 21:59:49 +0000 | [diff] [blame] | 3294 | */ |
| 3295 | if (!(force_width && force_height) && init_window_hints_state > 0) |
| 3296 | { |
Bram Moolenaar | c9b4b05 | 2006-04-30 18:54:39 +0000 | [diff] [blame] | 3297 | /* Don't do it! */ |
| 3298 | init_window_hints_state = 2; |
| 3299 | return; |
Bram Moolenaar | b3656ed | 2006-03-20 21:59:49 +0000 | [diff] [blame] | 3300 | } |
Bram Moolenaar | 32466aa | 2006-02-24 23:53:04 +0000 | [diff] [blame] | 3301 | |
| 3302 | /* This also needs to be done when the main window isn't there yet, |
| 3303 | * otherwise the hints don't work. */ |
| 3304 | width = gui_get_base_width(); |
| 3305 | height = gui_get_base_height(); |
| 3306 | # ifdef FEAT_MENU |
| 3307 | height += tabline_height() * gui.char_height; |
| 3308 | # endif |
Bram Moolenaar | 32466aa | 2006-02-24 23:53:04 +0000 | [diff] [blame] | 3309 | width += get_menu_tool_width(); |
| 3310 | height += get_menu_tool_height(); |
Bram Moolenaar | 32466aa | 2006-02-24 23:53:04 +0000 | [diff] [blame] | 3311 | |
Bram Moolenaar | b3656ed | 2006-03-20 21:59:49 +0000 | [diff] [blame] | 3312 | /* GtkSockets use GtkPlug's [gui,mainwin] min-size hints to determine |
Bram Moolenaar | 4932594 | 2007-05-10 19:19:59 +0000 | [diff] [blame] | 3313 | * their actual widget size. When we set our size ourselves (e.g., |
Bram Moolenaar | b3656ed | 2006-03-20 21:59:49 +0000 | [diff] [blame] | 3314 | * 'set columns=' or init. -geom) we briefly set the min. to the size |
| 3315 | * we wish to be instead of the legitimate minimum so that we actually |
| 3316 | * resize correctly. |
| 3317 | */ |
| 3318 | if (force_width && force_height) |
| 3319 | { |
Bram Moolenaar | c9b4b05 | 2006-04-30 18:54:39 +0000 | [diff] [blame] | 3320 | min_width = force_width; |
| 3321 | min_height = force_height; |
Bram Moolenaar | b3656ed | 2006-03-20 21:59:49 +0000 | [diff] [blame] | 3322 | } |
| 3323 | else |
| 3324 | { |
Bram Moolenaar | c9b4b05 | 2006-04-30 18:54:39 +0000 | [diff] [blame] | 3325 | min_width = width + MIN_COLUMNS * gui.char_width; |
| 3326 | min_height = height + MIN_LINES * gui.char_height; |
Bram Moolenaar | b3656ed | 2006-03-20 21:59:49 +0000 | [diff] [blame] | 3327 | } |
| 3328 | |
Bram Moolenaar | 32466aa | 2006-02-24 23:53:04 +0000 | [diff] [blame] | 3329 | /* Avoid an expose event when the size didn't change. */ |
| 3330 | if (width != old_width |
| 3331 | || height != old_height |
Bram Moolenaar | c9b4b05 | 2006-04-30 18:54:39 +0000 | [diff] [blame] | 3332 | || min_width != old_min_width |
Bram Moolenaar | b3656ed | 2006-03-20 21:59:49 +0000 | [diff] [blame] | 3333 | || min_height != old_min_height |
Bram Moolenaar | 32466aa | 2006-02-24 23:53:04 +0000 | [diff] [blame] | 3334 | || gui.char_width != old_char_width |
| 3335 | || gui.char_height != old_char_height) |
| 3336 | { |
| 3337 | GdkGeometry geometry; |
| 3338 | GdkWindowHints geometry_mask; |
| 3339 | |
| 3340 | geometry.width_inc = gui.char_width; |
| 3341 | geometry.height_inc = gui.char_height; |
| 3342 | geometry.base_width = width; |
| 3343 | geometry.base_height = height; |
Bram Moolenaar | c9b4b05 | 2006-04-30 18:54:39 +0000 | [diff] [blame] | 3344 | geometry.min_width = min_width; |
| 3345 | geometry.min_height = min_height; |
Bram Moolenaar | 32466aa | 2006-02-24 23:53:04 +0000 | [diff] [blame] | 3346 | geometry_mask = GDK_HINT_BASE_SIZE|GDK_HINT_RESIZE_INC |
| 3347 | |GDK_HINT_MIN_SIZE; |
Bram Moolenaar | 32466aa | 2006-02-24 23:53:04 +0000 | [diff] [blame] | 3348 | /* Using gui.formwin as geometry widget doesn't work as expected |
| 3349 | * with GTK+ 2 -- dunno why. Presumably all the resizing hacks |
| 3350 | * in Vim confuse GTK+. */ |
| 3351 | gtk_window_set_geometry_hints(GTK_WINDOW(gui.mainwin), gui.mainwin, |
| 3352 | &geometry, geometry_mask); |
Bram Moolenaar | c9b4b05 | 2006-04-30 18:54:39 +0000 | [diff] [blame] | 3353 | old_width = width; |
| 3354 | old_height = height; |
| 3355 | old_min_width = min_width; |
| 3356 | old_min_height = min_height; |
| 3357 | old_char_width = gui.char_width; |
| 3358 | old_char_height = gui.char_height; |
Bram Moolenaar | 32466aa | 2006-02-24 23:53:04 +0000 | [diff] [blame] | 3359 | } |
| 3360 | } |
| 3361 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3362 | #ifdef FEAT_TOOLBAR |
| 3363 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3364 | /* |
| 3365 | * This extra effort wouldn't be necessary if we only used stock icons in the |
| 3366 | * toolbar, as we do for all builtin icons. But user-defined toolbar icons |
| 3367 | * shouldn't be treated differently, thus we do need this. |
| 3368 | */ |
| 3369 | static void |
| 3370 | icon_size_changed_foreach(GtkWidget *widget, gpointer user_data) |
| 3371 | { |
| 3372 | if (GTK_IS_IMAGE(widget)) |
| 3373 | { |
| 3374 | GtkImage *image = (GtkImage *)widget; |
| 3375 | |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 3376 | # if GTK_CHECK_VERSION(3,10,0) |
| 3377 | if (gtk_image_get_storage_type(image) == GTK_IMAGE_ICON_NAME) |
| 3378 | { |
| 3379 | const GtkIconSize icon_size = GPOINTER_TO_INT(user_data); |
| 3380 | const gchar *icon_name; |
| 3381 | |
| 3382 | gtk_image_get_icon_name(image, &icon_name, NULL); |
| 3383 | |
| 3384 | gtk_image_set_from_icon_name(image, icon_name, icon_size); |
| 3385 | } |
| 3386 | # else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3387 | /* User-defined icons are stored in a GtkIconSet */ |
| 3388 | if (gtk_image_get_storage_type(image) == GTK_IMAGE_ICON_SET) |
| 3389 | { |
| 3390 | GtkIconSet *icon_set; |
| 3391 | GtkIconSize icon_size; |
| 3392 | |
| 3393 | gtk_image_get_icon_set(image, &icon_set, &icon_size); |
| 3394 | icon_size = (GtkIconSize)(long)user_data; |
| 3395 | |
| 3396 | gtk_icon_set_ref(icon_set); |
| 3397 | gtk_image_set_from_icon_set(image, icon_set, icon_size); |
| 3398 | gtk_icon_set_unref(icon_set); |
| 3399 | } |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 3400 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3401 | } |
| 3402 | else if (GTK_IS_CONTAINER(widget)) |
| 3403 | { |
| 3404 | gtk_container_foreach((GtkContainer *)widget, |
| 3405 | &icon_size_changed_foreach, |
| 3406 | user_data); |
| 3407 | } |
| 3408 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3409 | |
| 3410 | static void |
| 3411 | set_toolbar_style(GtkToolbar *toolbar) |
| 3412 | { |
| 3413 | GtkToolbarStyle style; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3414 | GtkIconSize size; |
| 3415 | GtkIconSize oldsize; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3416 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3417 | if ((toolbar_flags & (TOOLBAR_TEXT | TOOLBAR_ICONS | TOOLBAR_HORIZ)) |
| 3418 | == (TOOLBAR_TEXT | TOOLBAR_ICONS | TOOLBAR_HORIZ)) |
| 3419 | style = GTK_TOOLBAR_BOTH_HORIZ; |
Bram Moolenaar | 182c5be | 2010-06-25 05:37:59 +0200 | [diff] [blame] | 3420 | else if ((toolbar_flags & (TOOLBAR_TEXT | TOOLBAR_ICONS)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3421 | == (TOOLBAR_TEXT | TOOLBAR_ICONS)) |
| 3422 | style = GTK_TOOLBAR_BOTH; |
| 3423 | else if (toolbar_flags & TOOLBAR_TEXT) |
| 3424 | style = GTK_TOOLBAR_TEXT; |
| 3425 | else |
| 3426 | style = GTK_TOOLBAR_ICONS; |
| 3427 | |
| 3428 | gtk_toolbar_set_style(toolbar, style); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 3429 | # if !GTK_CHECK_VERSION(3,0,0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3430 | gtk_toolbar_set_tooltips(toolbar, (toolbar_flags & TOOLBAR_TOOLTIPS) != 0); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 3431 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3432 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3433 | switch (tbis_flags) |
| 3434 | { |
| 3435 | case TBIS_TINY: size = GTK_ICON_SIZE_MENU; break; |
| 3436 | case TBIS_SMALL: size = GTK_ICON_SIZE_SMALL_TOOLBAR; break; |
| 3437 | case TBIS_MEDIUM: size = GTK_ICON_SIZE_BUTTON; break; |
| 3438 | case TBIS_LARGE: size = GTK_ICON_SIZE_LARGE_TOOLBAR; break; |
Bram Moolenaar | beb003b | 2016-03-08 22:47:17 +0100 | [diff] [blame] | 3439 | case TBIS_HUGE: size = GTK_ICON_SIZE_DND; break; |
| 3440 | case TBIS_GIANT: size = GTK_ICON_SIZE_DIALOG; break; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3441 | default: size = GTK_ICON_SIZE_INVALID; break; |
| 3442 | } |
| 3443 | oldsize = gtk_toolbar_get_icon_size(toolbar); |
| 3444 | |
| 3445 | if (size == GTK_ICON_SIZE_INVALID) |
| 3446 | { |
| 3447 | /* Let global user preferences decide the icon size. */ |
| 3448 | gtk_toolbar_unset_icon_size(toolbar); |
| 3449 | size = gtk_toolbar_get_icon_size(toolbar); |
| 3450 | } |
| 3451 | if (size != oldsize) |
| 3452 | { |
| 3453 | gtk_container_foreach(GTK_CONTAINER(toolbar), |
| 3454 | &icon_size_changed_foreach, |
| 3455 | GINT_TO_POINTER((int)size)); |
| 3456 | } |
| 3457 | gtk_toolbar_set_icon_size(toolbar, size); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3458 | } |
| 3459 | |
| 3460 | #endif /* FEAT_TOOLBAR */ |
| 3461 | |
Bram Moolenaar | 32466aa | 2006-02-24 23:53:04 +0000 | [diff] [blame] | 3462 | #if defined(FEAT_GUI_TABLINE) || defined(PROTO) |
| 3463 | static int ignore_tabline_evt = FALSE; |
Bram Moolenaar | a562149 | 2006-02-25 21:55:24 +0000 | [diff] [blame] | 3464 | static GtkWidget *tabline_menu; |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 3465 | # if !GTK_CHECK_VERSION(3,0,0) |
Bram Moolenaar | 437df8f | 2006-04-27 21:47:44 +0000 | [diff] [blame] | 3466 | static GtkTooltips *tabline_tooltip; |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 3467 | # endif |
Bram Moolenaar | a562149 | 2006-02-25 21:55:24 +0000 | [diff] [blame] | 3468 | static int clicked_page; /* page clicked in tab line */ |
| 3469 | |
| 3470 | /* |
| 3471 | * Handle selecting an item in the tab line popup menu. |
| 3472 | */ |
Bram Moolenaar | a562149 | 2006-02-25 21:55:24 +0000 | [diff] [blame] | 3473 | static void |
Bram Moolenaar | b85cb21 | 2009-05-17 14:24:23 +0000 | [diff] [blame] | 3474 | tabline_menu_handler(GtkMenuItem *item UNUSED, gpointer user_data) |
Bram Moolenaar | a562149 | 2006-02-25 21:55:24 +0000 | [diff] [blame] | 3475 | { |
Bram Moolenaar | a562149 | 2006-02-25 21:55:24 +0000 | [diff] [blame] | 3476 | /* Add the string cmd into input buffer */ |
Bram Moolenaar | c6fe919 | 2006-04-09 21:54:49 +0000 | [diff] [blame] | 3477 | send_tabline_menu_event(clicked_page, (int)(long)user_data); |
Bram Moolenaar | a562149 | 2006-02-25 21:55:24 +0000 | [diff] [blame] | 3478 | } |
| 3479 | |
Bram Moolenaar | 8ea9123 | 2006-04-28 22:41:43 +0000 | [diff] [blame] | 3480 | static void |
| 3481 | add_tabline_menu_item(GtkWidget *menu, char_u *text, int resp) |
| 3482 | { |
| 3483 | GtkWidget *item; |
| 3484 | char_u *utf_text; |
| 3485 | |
| 3486 | utf_text = CONVERT_TO_UTF8(text); |
| 3487 | item = gtk_menu_item_new_with_label((const char *)utf_text); |
| 3488 | gtk_widget_show(item); |
| 3489 | CONVERT_TO_UTF8_FREE(utf_text); |
| 3490 | |
| 3491 | gtk_container_add(GTK_CONTAINER(menu), item); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 3492 | # if GTK_CHECK_VERSION(3,0,0) |
| 3493 | g_signal_connect(G_OBJECT(item), "activate", |
| 3494 | G_CALLBACK(tabline_menu_handler), |
| 3495 | GINT_TO_POINTER(resp)); |
| 3496 | # else |
Bram Moolenaar | 8ea9123 | 2006-04-28 22:41:43 +0000 | [diff] [blame] | 3497 | gtk_signal_connect(GTK_OBJECT(item), "activate", |
| 3498 | GTK_SIGNAL_FUNC(tabline_menu_handler), |
Bram Moolenaar | 47b46d7 | 2008-07-02 19:05:48 +0000 | [diff] [blame] | 3499 | (gpointer)(long)resp); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 3500 | # endif |
Bram Moolenaar | 8ea9123 | 2006-04-28 22:41:43 +0000 | [diff] [blame] | 3501 | } |
| 3502 | |
Bram Moolenaar | a562149 | 2006-02-25 21:55:24 +0000 | [diff] [blame] | 3503 | /* |
Bram Moolenaar | a562149 | 2006-02-25 21:55:24 +0000 | [diff] [blame] | 3504 | * Create a menu for the tab line. |
| 3505 | */ |
| 3506 | static GtkWidget * |
| 3507 | create_tabline_menu(void) |
| 3508 | { |
Bram Moolenaar | 8ea9123 | 2006-04-28 22:41:43 +0000 | [diff] [blame] | 3509 | GtkWidget *menu; |
Bram Moolenaar | a562149 | 2006-02-25 21:55:24 +0000 | [diff] [blame] | 3510 | |
| 3511 | menu = gtk_menu_new(); |
Bram Moolenaar | 7098ee5 | 2015-06-09 19:14:24 +0200 | [diff] [blame] | 3512 | if (first_tabpage->tp_next != NULL) |
| 3513 | add_tabline_menu_item(menu, (char_u *)_("Close tab"), |
| 3514 | TABLINE_MENU_CLOSE); |
Bram Moolenaar | 8ea9123 | 2006-04-28 22:41:43 +0000 | [diff] [blame] | 3515 | add_tabline_menu_item(menu, (char_u *)_("New tab"), TABLINE_MENU_NEW); |
| 3516 | add_tabline_menu_item(menu, (char_u *)_("Open Tab..."), TABLINE_MENU_OPEN); |
Bram Moolenaar | a562149 | 2006-02-25 21:55:24 +0000 | [diff] [blame] | 3517 | |
| 3518 | return menu; |
| 3519 | } |
| 3520 | |
| 3521 | static gboolean |
| 3522 | on_tabline_menu(GtkWidget *widget, GdkEvent *event) |
| 3523 | { |
| 3524 | /* Was this button press event ? */ |
| 3525 | if (event->type == GDK_BUTTON_PRESS) |
| 3526 | { |
| 3527 | GdkEventButton *bevent = (GdkEventButton *)event; |
| 3528 | int x = bevent->x; |
Bram Moolenaar | 8ea9123 | 2006-04-28 22:41:43 +0000 | [diff] [blame] | 3529 | int y = bevent->y; |
| 3530 | GtkWidget *tabwidget; |
| 3531 | GdkWindow *tabwin; |
Bram Moolenaar | a562149 | 2006-02-25 21:55:24 +0000 | [diff] [blame] | 3532 | |
Bram Moolenaar | f193fff | 2006-04-27 00:02:13 +0000 | [diff] [blame] | 3533 | /* When ignoring events return TRUE so that the selected page doesn't |
| 3534 | * change. */ |
| 3535 | if (hold_gui_events |
| 3536 | # ifdef FEAT_CMDWIN |
| 3537 | || cmdwin_type != 0 |
| 3538 | # endif |
| 3539 | ) |
| 3540 | return TRUE; |
| 3541 | |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 3542 | # if GTK_CHECK_VERSION(3,0,0) |
| 3543 | tabwin = gui_gtk_window_at_position(gui.mainwin, &x, &y); |
| 3544 | # else |
Bram Moolenaar | 8ea9123 | 2006-04-28 22:41:43 +0000 | [diff] [blame] | 3545 | tabwin = gdk_window_at_pointer(&x, &y); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 3546 | # endif |
| 3547 | |
Bram Moolenaar | 8ea9123 | 2006-04-28 22:41:43 +0000 | [diff] [blame] | 3548 | gdk_window_get_user_data(tabwin, (gpointer)&tabwidget); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 3549 | # if GTK_CHECK_VERSION(3,0,0) |
| 3550 | clicked_page = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(tabwidget), |
| 3551 | "tab_num")); |
| 3552 | # else |
Bram Moolenaar | 8ea9123 | 2006-04-28 22:41:43 +0000 | [diff] [blame] | 3553 | clicked_page = (int)(long)gtk_object_get_user_data( |
| 3554 | GTK_OBJECT(tabwidget)); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 3555 | # endif |
Bram Moolenaar | a562149 | 2006-02-25 21:55:24 +0000 | [diff] [blame] | 3556 | |
| 3557 | /* If the event was generated for 3rd button popup the menu. */ |
| 3558 | if (bevent->button == 3) |
| 3559 | { |
Bram Moolenaar | a859f04 | 2016-11-17 19:11:55 +0100 | [diff] [blame] | 3560 | # if GTK_CHECK_VERSION(3,22,2) |
| 3561 | gtk_menu_popup_at_pointer(GTK_MENU(widget), event); |
| 3562 | # else |
Bram Moolenaar | a562149 | 2006-02-25 21:55:24 +0000 | [diff] [blame] | 3563 | gtk_menu_popup(GTK_MENU(widget), NULL, NULL, NULL, NULL, |
| 3564 | bevent->button, bevent->time); |
Bram Moolenaar | a859f04 | 2016-11-17 19:11:55 +0100 | [diff] [blame] | 3565 | # endif |
Bram Moolenaar | a562149 | 2006-02-25 21:55:24 +0000 | [diff] [blame] | 3566 | /* We handled the event. */ |
| 3567 | return TRUE; |
| 3568 | } |
Bram Moolenaar | 9635157 | 2006-05-05 21:16:59 +0000 | [diff] [blame] | 3569 | else if (bevent->button == 1) |
Bram Moolenaar | eddf53b | 2006-02-27 00:11:10 +0000 | [diff] [blame] | 3570 | { |
Bram Moolenaar | 9635157 | 2006-05-05 21:16:59 +0000 | [diff] [blame] | 3571 | if (clicked_page == 0) |
| 3572 | { |
Bram Moolenaar | 0735454 | 2007-09-15 12:07:46 +0000 | [diff] [blame] | 3573 | /* Click after all tabs moves to next tab page. When "x" is |
| 3574 | * small guess it's the left button. */ |
Bram Moolenaar | a3f4166 | 2010-07-11 19:01:06 +0200 | [diff] [blame] | 3575 | send_tabline_event(x < 50 ? -1 : 0); |
Bram Moolenaar | 9635157 | 2006-05-05 21:16:59 +0000 | [diff] [blame] | 3576 | } |
Bram Moolenaar | eddf53b | 2006-02-27 00:11:10 +0000 | [diff] [blame] | 3577 | } |
Bram Moolenaar | a562149 | 2006-02-25 21:55:24 +0000 | [diff] [blame] | 3578 | } |
Bram Moolenaar | f193fff | 2006-04-27 00:02:13 +0000 | [diff] [blame] | 3579 | |
Bram Moolenaar | a562149 | 2006-02-25 21:55:24 +0000 | [diff] [blame] | 3580 | /* We didn't handle the event. */ |
| 3581 | return FALSE; |
| 3582 | } |
Bram Moolenaar | 32466aa | 2006-02-24 23:53:04 +0000 | [diff] [blame] | 3583 | |
| 3584 | /* |
| 3585 | * Handle selecting one of the tabs. |
| 3586 | */ |
Bram Moolenaar | 32466aa | 2006-02-24 23:53:04 +0000 | [diff] [blame] | 3587 | static void |
| 3588 | on_select_tab( |
Bram Moolenaar | b85cb21 | 2009-05-17 14:24:23 +0000 | [diff] [blame] | 3589 | GtkNotebook *notebook UNUSED, |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 3590 | # if GTK_CHECK_VERSION(3,0,0) |
| 3591 | gpointer *page UNUSED, |
| 3592 | # else |
Bram Moolenaar | b85cb21 | 2009-05-17 14:24:23 +0000 | [diff] [blame] | 3593 | GtkNotebookPage *page UNUSED, |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 3594 | # endif |
Bram Moolenaar | 89d4032 | 2006-08-29 15:30:07 +0000 | [diff] [blame] | 3595 | gint idx, |
Bram Moolenaar | b85cb21 | 2009-05-17 14:24:23 +0000 | [diff] [blame] | 3596 | gpointer data UNUSED) |
Bram Moolenaar | 32466aa | 2006-02-24 23:53:04 +0000 | [diff] [blame] | 3597 | { |
Bram Moolenaar | 32466aa | 2006-02-24 23:53:04 +0000 | [diff] [blame] | 3598 | if (!ignore_tabline_evt) |
Bram Moolenaar | a3f4166 | 2010-07-11 19:01:06 +0200 | [diff] [blame] | 3599 | send_tabline_event(idx + 1); |
Bram Moolenaar | ca05aa2 | 2017-10-22 15:36:14 +0200 | [diff] [blame] | 3600 | } |
| 3601 | |
| 3602 | /* |
| 3603 | * Handle reordering the tabs (using D&D). |
| 3604 | */ |
| 3605 | static void |
| 3606 | on_tab_reordered( |
| 3607 | GtkNotebook *notebook UNUSED, |
| 3608 | # if GTK_CHECK_VERSION(3,0,0) |
| 3609 | gpointer *page UNUSED, |
| 3610 | # else |
| 3611 | GtkNotebookPage *page UNUSED, |
| 3612 | # endif |
| 3613 | gint idx, |
| 3614 | gpointer data UNUSED) |
| 3615 | { |
| 3616 | if (!ignore_tabline_evt) |
| 3617 | { |
| 3618 | if ((tabpage_index(curtab) - 1) < idx) |
| 3619 | tabpage_move(idx + 1); |
| 3620 | else |
| 3621 | tabpage_move(idx); |
Bram Moolenaar | eddf53b | 2006-02-27 00:11:10 +0000 | [diff] [blame] | 3622 | } |
Bram Moolenaar | 32466aa | 2006-02-24 23:53:04 +0000 | [diff] [blame] | 3623 | } |
| 3624 | |
| 3625 | /* |
| 3626 | * Show or hide the tabline. |
| 3627 | */ |
| 3628 | void |
| 3629 | gui_mch_show_tabline(int showit) |
| 3630 | { |
| 3631 | if (gui.tabline == NULL) |
| 3632 | return; |
| 3633 | |
| 3634 | if (!showit != !gtk_notebook_get_show_tabs(GTK_NOTEBOOK(gui.tabline))) |
| 3635 | { |
Bram Moolenaar | 3517bb1 | 2006-03-03 22:58:45 +0000 | [diff] [blame] | 3636 | /* Note: this may cause a resize event */ |
Bram Moolenaar | 32466aa | 2006-02-24 23:53:04 +0000 | [diff] [blame] | 3637 | gtk_notebook_set_show_tabs(GTK_NOTEBOOK(gui.tabline), showit); |
Bram Moolenaar | b3656ed | 2006-03-20 21:59:49 +0000 | [diff] [blame] | 3638 | update_window_manager_hints(0, 0); |
Bram Moolenaar | 9635157 | 2006-05-05 21:16:59 +0000 | [diff] [blame] | 3639 | if (showit) |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 3640 | # if GTK_CHECK_VERSION(3,0,0) |
| 3641 | gtk_widget_set_can_focus(GTK_WIDGET(gui.tabline), FALSE); |
| 3642 | # else |
Bram Moolenaar | 9635157 | 2006-05-05 21:16:59 +0000 | [diff] [blame] | 3643 | GTK_WIDGET_UNSET_FLAGS(GTK_WIDGET(gui.tabline), GTK_CAN_FOCUS); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 3644 | # endif |
Bram Moolenaar | 32466aa | 2006-02-24 23:53:04 +0000 | [diff] [blame] | 3645 | } |
Bram Moolenaar | 9635157 | 2006-05-05 21:16:59 +0000 | [diff] [blame] | 3646 | |
| 3647 | gui_mch_update(); |
Bram Moolenaar | 32466aa | 2006-02-24 23:53:04 +0000 | [diff] [blame] | 3648 | } |
| 3649 | |
| 3650 | /* |
Bram Moolenaar | 3517bb1 | 2006-03-03 22:58:45 +0000 | [diff] [blame] | 3651 | * Return TRUE when tabline is displayed. |
| 3652 | */ |
| 3653 | int |
| 3654 | gui_mch_showing_tabline(void) |
| 3655 | { |
| 3656 | return gui.tabline != NULL |
Bram Moolenaar | 182c5be | 2010-06-25 05:37:59 +0200 | [diff] [blame] | 3657 | && gtk_notebook_get_show_tabs(GTK_NOTEBOOK(gui.tabline)); |
Bram Moolenaar | 3517bb1 | 2006-03-03 22:58:45 +0000 | [diff] [blame] | 3658 | } |
| 3659 | |
| 3660 | /* |
Bram Moolenaar | 32466aa | 2006-02-24 23:53:04 +0000 | [diff] [blame] | 3661 | * Update the labels of the tabline. |
| 3662 | */ |
| 3663 | void |
| 3664 | gui_mch_update_tabline(void) |
| 3665 | { |
| 3666 | GtkWidget *page; |
Bram Moolenaar | 437df8f | 2006-04-27 21:47:44 +0000 | [diff] [blame] | 3667 | GtkWidget *event_box; |
Bram Moolenaar | 32466aa | 2006-02-24 23:53:04 +0000 | [diff] [blame] | 3668 | GtkWidget *label; |
| 3669 | tabpage_T *tp; |
| 3670 | int nr = 0; |
Bram Moolenaar | 8ea9123 | 2006-04-28 22:41:43 +0000 | [diff] [blame] | 3671 | int tab_num; |
Bram Moolenaar | 32466aa | 2006-02-24 23:53:04 +0000 | [diff] [blame] | 3672 | int curtabidx = 0; |
Bram Moolenaar | c1e3790 | 2006-04-18 21:55:01 +0000 | [diff] [blame] | 3673 | char_u *labeltext; |
Bram Moolenaar | 32466aa | 2006-02-24 23:53:04 +0000 | [diff] [blame] | 3674 | |
| 3675 | if (gui.tabline == NULL) |
| 3676 | return; |
| 3677 | |
| 3678 | ignore_tabline_evt = TRUE; |
| 3679 | |
| 3680 | /* Add a label for each tab page. They all contain the same text area. */ |
| 3681 | for (tp = first_tabpage; tp != NULL; tp = tp->tp_next, ++nr) |
| 3682 | { |
| 3683 | if (tp == curtab) |
| 3684 | curtabidx = nr; |
| 3685 | |
Bram Moolenaar | 8ea9123 | 2006-04-28 22:41:43 +0000 | [diff] [blame] | 3686 | tab_num = nr + 1; |
| 3687 | |
Bram Moolenaar | 32466aa | 2006-02-24 23:53:04 +0000 | [diff] [blame] | 3688 | page = gtk_notebook_get_nth_page(GTK_NOTEBOOK(gui.tabline), nr); |
| 3689 | if (page == NULL) |
| 3690 | { |
| 3691 | /* Add notebook page */ |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 3692 | # if GTK_CHECK_VERSION(3,2,0) |
| 3693 | page = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0); |
| 3694 | gtk_box_set_homogeneous(GTK_BOX(page), FALSE); |
| 3695 | # else |
Bram Moolenaar | 32466aa | 2006-02-24 23:53:04 +0000 | [diff] [blame] | 3696 | page = gtk_vbox_new(FALSE, 0); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 3697 | # endif |
Bram Moolenaar | 32466aa | 2006-02-24 23:53:04 +0000 | [diff] [blame] | 3698 | gtk_widget_show(page); |
Bram Moolenaar | 437df8f | 2006-04-27 21:47:44 +0000 | [diff] [blame] | 3699 | event_box = gtk_event_box_new(); |
| 3700 | gtk_widget_show(event_box); |
Bram Moolenaar | 32466aa | 2006-02-24 23:53:04 +0000 | [diff] [blame] | 3701 | label = gtk_label_new("-Empty-"); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 3702 | # if !GTK_CHECK_VERSION(3,14,0) |
Bram Moolenaar | 8ea9123 | 2006-04-28 22:41:43 +0000 | [diff] [blame] | 3703 | gtk_misc_set_padding(GTK_MISC(label), 2, 2); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 3704 | # endif |
Bram Moolenaar | 437df8f | 2006-04-27 21:47:44 +0000 | [diff] [blame] | 3705 | gtk_container_add(GTK_CONTAINER(event_box), label); |
Bram Moolenaar | 32466aa | 2006-02-24 23:53:04 +0000 | [diff] [blame] | 3706 | gtk_widget_show(label); |
| 3707 | gtk_notebook_insert_page(GTK_NOTEBOOK(gui.tabline), |
| 3708 | page, |
Bram Moolenaar | 437df8f | 2006-04-27 21:47:44 +0000 | [diff] [blame] | 3709 | event_box, |
Bram Moolenaar | 32466aa | 2006-02-24 23:53:04 +0000 | [diff] [blame] | 3710 | nr++); |
Bram Moolenaar | ca05aa2 | 2017-10-22 15:36:14 +0200 | [diff] [blame] | 3711 | gtk_notebook_set_tab_reorderable(GTK_NOTEBOOK(gui.tabline), |
| 3712 | page, |
| 3713 | TRUE); |
Bram Moolenaar | 32466aa | 2006-02-24 23:53:04 +0000 | [diff] [blame] | 3714 | } |
| 3715 | |
Bram Moolenaar | 437df8f | 2006-04-27 21:47:44 +0000 | [diff] [blame] | 3716 | event_box = gtk_notebook_get_tab_label(GTK_NOTEBOOK(gui.tabline), page); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 3717 | # if GTK_CHECK_VERSION(3,0,0) |
| 3718 | g_object_set_data(G_OBJECT(event_box), "tab_num", |
| 3719 | GINT_TO_POINTER(tab_num)); |
| 3720 | # else |
Bram Moolenaar | 47b46d7 | 2008-07-02 19:05:48 +0000 | [diff] [blame] | 3721 | gtk_object_set_user_data(GTK_OBJECT(event_box), |
| 3722 | (gpointer)(long)tab_num); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 3723 | # endif |
| 3724 | # if GTK_CHECK_VERSION(3,0,0) |
| 3725 | label = gtk_bin_get_child(GTK_BIN(event_box)); |
| 3726 | # else |
Bram Moolenaar | 437df8f | 2006-04-27 21:47:44 +0000 | [diff] [blame] | 3727 | label = GTK_BIN(event_box)->child; |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 3728 | # endif |
Bram Moolenaar | 57657d8 | 2006-04-21 22:12:41 +0000 | [diff] [blame] | 3729 | get_tabline_label(tp, FALSE); |
Bram Moolenaar | c1e3790 | 2006-04-18 21:55:01 +0000 | [diff] [blame] | 3730 | labeltext = CONVERT_TO_UTF8(NameBuff); |
Bram Moolenaar | 437df8f | 2006-04-27 21:47:44 +0000 | [diff] [blame] | 3731 | gtk_label_set_text(GTK_LABEL(label), (const char *)labeltext); |
Bram Moolenaar | c1e3790 | 2006-04-18 21:55:01 +0000 | [diff] [blame] | 3732 | CONVERT_TO_UTF8_FREE(labeltext); |
Bram Moolenaar | 437df8f | 2006-04-27 21:47:44 +0000 | [diff] [blame] | 3733 | |
Bram Moolenaar | 437df8f | 2006-04-27 21:47:44 +0000 | [diff] [blame] | 3734 | get_tabline_label(tp, TRUE); |
| 3735 | labeltext = CONVERT_TO_UTF8(NameBuff); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 3736 | # if GTK_CHECK_VERSION(3,0,0) |
| 3737 | gtk_widget_set_tooltip_text(event_box, (const gchar *)labeltext); |
| 3738 | # else |
Bram Moolenaar | 437df8f | 2006-04-27 21:47:44 +0000 | [diff] [blame] | 3739 | gtk_tooltips_set_tip(GTK_TOOLTIPS(tabline_tooltip), event_box, |
| 3740 | (const char *)labeltext, NULL); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 3741 | # endif |
Bram Moolenaar | 437df8f | 2006-04-27 21:47:44 +0000 | [diff] [blame] | 3742 | CONVERT_TO_UTF8_FREE(labeltext); |
Bram Moolenaar | 32466aa | 2006-02-24 23:53:04 +0000 | [diff] [blame] | 3743 | } |
| 3744 | |
| 3745 | /* Remove any old labels. */ |
| 3746 | while (gtk_notebook_get_nth_page(GTK_NOTEBOOK(gui.tabline), nr) != NULL) |
| 3747 | gtk_notebook_remove_page(GTK_NOTEBOOK(gui.tabline), nr); |
| 3748 | |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 3749 | # if GTK_CHECK_VERSION(3,0,0) |
| 3750 | if (gtk_notebook_get_current_page(GTK_NOTEBOOK(gui.tabline)) != curtabidx) |
| 3751 | gtk_notebook_set_current_page(GTK_NOTEBOOK(gui.tabline), curtabidx); |
| 3752 | # else |
Bram Moolenaar | 32466aa | 2006-02-24 23:53:04 +0000 | [diff] [blame] | 3753 | if (gtk_notebook_current_page(GTK_NOTEBOOK(gui.tabline)) != curtabidx) |
Bram Moolenaar | c9b4b05 | 2006-04-30 18:54:39 +0000 | [diff] [blame] | 3754 | gtk_notebook_set_page(GTK_NOTEBOOK(gui.tabline), curtabidx); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 3755 | # endif |
Bram Moolenaar | 32466aa | 2006-02-24 23:53:04 +0000 | [diff] [blame] | 3756 | |
Bram Moolenaar | 8ea9123 | 2006-04-28 22:41:43 +0000 | [diff] [blame] | 3757 | /* Make sure everything is in place before drawing text. */ |
| 3758 | gui_mch_update(); |
| 3759 | |
Bram Moolenaar | 32466aa | 2006-02-24 23:53:04 +0000 | [diff] [blame] | 3760 | ignore_tabline_evt = FALSE; |
| 3761 | } |
| 3762 | |
| 3763 | /* |
| 3764 | * Set the current tab to "nr". First tab is 1. |
| 3765 | */ |
| 3766 | void |
Bram Moolenaar | 66f948e | 2016-01-30 16:39:25 +0100 | [diff] [blame] | 3767 | gui_mch_set_curtab(int nr) |
Bram Moolenaar | 32466aa | 2006-02-24 23:53:04 +0000 | [diff] [blame] | 3768 | { |
| 3769 | if (gui.tabline == NULL) |
| 3770 | return; |
| 3771 | |
| 3772 | ignore_tabline_evt = TRUE; |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 3773 | # if GTK_CHECK_VERSION(3,0,0) |
| 3774 | if (gtk_notebook_get_current_page(GTK_NOTEBOOK(gui.tabline)) != nr - 1) |
| 3775 | gtk_notebook_set_current_page(GTK_NOTEBOOK(gui.tabline), nr - 1); |
| 3776 | # else |
Bram Moolenaar | 32466aa | 2006-02-24 23:53:04 +0000 | [diff] [blame] | 3777 | if (gtk_notebook_current_page(GTK_NOTEBOOK(gui.tabline)) != nr - 1) |
Bram Moolenaar | c9b4b05 | 2006-04-30 18:54:39 +0000 | [diff] [blame] | 3778 | gtk_notebook_set_page(GTK_NOTEBOOK(gui.tabline), nr - 1); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 3779 | # endif |
Bram Moolenaar | 32466aa | 2006-02-24 23:53:04 +0000 | [diff] [blame] | 3780 | ignore_tabline_evt = FALSE; |
| 3781 | } |
| 3782 | |
| 3783 | #endif /* FEAT_GUI_TABLINE */ |
| 3784 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3785 | /* |
Bram Moolenaar | a76638f | 2010-06-05 12:49:46 +0200 | [diff] [blame] | 3786 | * Add selection targets for PRIMARY and CLIPBOARD selections. |
| 3787 | */ |
| 3788 | void |
| 3789 | gui_gtk_set_selection_targets(void) |
| 3790 | { |
| 3791 | int i, j = 0; |
| 3792 | int n_targets = N_SELECTION_TARGETS; |
| 3793 | GtkTargetEntry targets[N_SELECTION_TARGETS]; |
| 3794 | |
| 3795 | for (i = 0; i < (int)N_SELECTION_TARGETS; ++i) |
| 3796 | { |
Bram Moolenaar | 2969570 | 2012-05-18 17:03:18 +0200 | [diff] [blame] | 3797 | /* OpenOffice tries to use TARGET_HTML and fails when we don't |
Bram Moolenaar | a76638f | 2010-06-05 12:49:46 +0200 | [diff] [blame] | 3798 | * return something, instead of trying another target. Therefore only |
| 3799 | * offer TARGET_HTML when it works. */ |
| 3800 | if (!clip_html && selection_targets[i].info == TARGET_HTML) |
| 3801 | n_targets--; |
| 3802 | else |
Bram Moolenaar | a76638f | 2010-06-05 12:49:46 +0200 | [diff] [blame] | 3803 | targets[j++] = selection_targets[i]; |
| 3804 | } |
| 3805 | |
| 3806 | gtk_selection_clear_targets(gui.drawarea, (GdkAtom)GDK_SELECTION_PRIMARY); |
| 3807 | gtk_selection_clear_targets(gui.drawarea, (GdkAtom)clip_plus.gtk_sel_atom); |
| 3808 | gtk_selection_add_targets(gui.drawarea, |
| 3809 | (GdkAtom)GDK_SELECTION_PRIMARY, |
| 3810 | targets, n_targets); |
| 3811 | gtk_selection_add_targets(gui.drawarea, |
| 3812 | (GdkAtom)clip_plus.gtk_sel_atom, |
| 3813 | targets, n_targets); |
| 3814 | } |
| 3815 | |
| 3816 | /* |
| 3817 | * Set up for receiving DND items. |
| 3818 | */ |
| 3819 | void |
| 3820 | gui_gtk_set_dnd_targets(void) |
| 3821 | { |
| 3822 | int i, j = 0; |
| 3823 | int n_targets = N_DND_TARGETS; |
| 3824 | GtkTargetEntry targets[N_DND_TARGETS]; |
| 3825 | |
| 3826 | for (i = 0; i < (int)N_DND_TARGETS; ++i) |
| 3827 | { |
Bram Moolenaar | b931d74 | 2011-10-26 11:36:25 +0200 | [diff] [blame] | 3828 | if (!clip_html && dnd_targets[i].info == TARGET_HTML) |
Bram Moolenaar | a76638f | 2010-06-05 12:49:46 +0200 | [diff] [blame] | 3829 | n_targets--; |
| 3830 | else |
Bram Moolenaar | a76638f | 2010-06-05 12:49:46 +0200 | [diff] [blame] | 3831 | targets[j++] = dnd_targets[i]; |
| 3832 | } |
| 3833 | |
| 3834 | gtk_drag_dest_unset(gui.drawarea); |
| 3835 | gtk_drag_dest_set(gui.drawarea, |
| 3836 | GTK_DEST_DEFAULT_ALL, |
| 3837 | targets, n_targets, |
Bram Moolenaar | ba7cc9f | 2011-02-25 17:10:27 +0100 | [diff] [blame] | 3838 | GDK_ACTION_COPY | GDK_ACTION_MOVE); |
Bram Moolenaar | a76638f | 2010-06-05 12:49:46 +0200 | [diff] [blame] | 3839 | } |
| 3840 | |
| 3841 | /* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3842 | * Initialize the GUI. Create all the windows, set up all the callbacks etc. |
| 3843 | * Returns OK for success, FAIL when the GUI can't be started. |
| 3844 | */ |
| 3845 | int |
| 3846 | gui_mch_init(void) |
| 3847 | { |
| 3848 | GtkWidget *vbox; |
| 3849 | |
| 3850 | #ifdef FEAT_GUI_GNOME |
| 3851 | /* Initialize the GNOME libraries. gnome_program_init()/gnome_init() |
| 3852 | * exits on failure, but that's a non-issue because we already called |
| 3853 | * gtk_init_check() in gui_mch_init_check(). */ |
| 3854 | if (using_gnome) |
Bram Moolenaar | 3be71ce | 2013-01-23 16:00:11 +0100 | [diff] [blame] | 3855 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3856 | gnome_program_init(VIMPACKAGE, VIM_VERSION_SHORT, |
| 3857 | LIBGNOMEUI_MODULE, gui_argc, gui_argv, NULL); |
Bram Moolenaar | 3be71ce | 2013-01-23 16:00:11 +0100 | [diff] [blame] | 3858 | # if defined(FEAT_FLOAT) && defined(LC_NUMERIC) |
Bram Moolenaar | 1ff32c5 | 2014-04-29 15:11:43 +0200 | [diff] [blame] | 3859 | { |
| 3860 | char *p = setlocale(LC_NUMERIC, NULL); |
| 3861 | |
| 3862 | /* Make sure strtod() uses a decimal point, not a comma. Gnome |
| 3863 | * init may change it. */ |
| 3864 | if (p == NULL || strcmp(p, "C") != 0) |
| 3865 | setlocale(LC_NUMERIC, "C"); |
| 3866 | } |
Bram Moolenaar | 3be71ce | 2013-01-23 16:00:11 +0100 | [diff] [blame] | 3867 | # endif |
| 3868 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3869 | #endif |
Bram Moolenaar | d23a823 | 2018-02-10 18:45:26 +0100 | [diff] [blame] | 3870 | VIM_CLEAR(gui_argv); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3871 | |
Bram Moolenaar | 182c5be | 2010-06-25 05:37:59 +0200 | [diff] [blame] | 3872 | #if GLIB_CHECK_VERSION(2,1,3) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3873 | /* Set the human-readable application name */ |
| 3874 | g_set_application_name("Vim"); |
Bram Moolenaar | 182c5be | 2010-06-25 05:37:59 +0200 | [diff] [blame] | 3875 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3876 | /* |
| 3877 | * Force UTF-8 output no matter what the value of 'encoding' is. |
| 3878 | * did_set_string_option() in option.c prohibits changing 'termencoding' |
| 3879 | * to something else than UTF-8 if the GUI is in use. |
| 3880 | */ |
| 3881 | set_option_value((char_u *)"termencoding", 0L, (char_u *)"utf-8", 0); |
| 3882 | |
Bram Moolenaar | 182c5be | 2010-06-25 05:37:59 +0200 | [diff] [blame] | 3883 | #ifdef FEAT_TOOLBAR |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3884 | gui_gtk_register_stock_icons(); |
Bram Moolenaar | 182c5be | 2010-06-25 05:37:59 +0200 | [diff] [blame] | 3885 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3886 | /* FIXME: Need to install the classic icons and a gtkrc.classic file. |
| 3887 | * The hard part is deciding install locations and the Makefile magic. */ |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 3888 | #if !GTK_CHECK_VERSION(3,0,0) |
| 3889 | # if 0 |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3890 | gtk_rc_parse("gtkrc"); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 3891 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3892 | #endif |
| 3893 | |
| 3894 | /* Initialize values */ |
| 3895 | gui.border_width = 2; |
| 3896 | gui.scrollbar_width = SB_DEFAULT_WIDTH; |
| 3897 | gui.scrollbar_height = SB_DEFAULT_WIDTH; |
Bram Moolenaar | 36edf06 | 2016-07-21 22:10:12 +0200 | [diff] [blame] | 3898 | #if GTK_CHECK_VERSION(3,0,0) |
| 3899 | gui.fgcolor = g_new(GdkRGBA, 1); |
| 3900 | gui.bgcolor = g_new(GdkRGBA, 1); |
| 3901 | gui.spcolor = g_new(GdkRGBA, 1); |
| 3902 | #else |
Bram Moolenaar | b71ec9f | 2005-01-25 22:22:02 +0000 | [diff] [blame] | 3903 | /* LINTED: avoid warning: conversion to 'unsigned long' */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3904 | gui.fgcolor = g_new0(GdkColor, 1); |
Bram Moolenaar | b71ec9f | 2005-01-25 22:22:02 +0000 | [diff] [blame] | 3905 | /* LINTED: avoid warning: conversion to 'unsigned long' */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3906 | gui.bgcolor = g_new0(GdkColor, 1); |
Bram Moolenaar | f36d369 | 2005-03-15 22:48:14 +0000 | [diff] [blame] | 3907 | /* LINTED: avoid warning: conversion to 'unsigned long' */ |
| 3908 | gui.spcolor = g_new0(GdkColor, 1); |
Bram Moolenaar | 36edf06 | 2016-07-21 22:10:12 +0200 | [diff] [blame] | 3909 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3910 | |
| 3911 | /* Initialise atoms */ |
Bram Moolenaar | 3a6eaa5 | 2009-06-16 13:23:06 +0000 | [diff] [blame] | 3912 | html_atom = gdk_atom_intern("text/html", FALSE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3913 | utf8_string_atom = gdk_atom_intern("UTF8_STRING", FALSE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3914 | |
| 3915 | /* Set default foreground and background colors. */ |
| 3916 | gui.norm_pixel = gui.def_norm_pixel; |
| 3917 | gui.back_pixel = gui.def_back_pixel; |
| 3918 | |
| 3919 | if (gtk_socket_id != 0) |
| 3920 | { |
| 3921 | GtkWidget *plug; |
| 3922 | |
| 3923 | /* Use GtkSocket from another app. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3924 | plug = gtk_plug_new_for_display(gdk_display_get_default(), |
| 3925 | gtk_socket_id); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 3926 | #if GTK_CHECK_VERSION(3,0,0) |
| 3927 | if (plug != NULL && gtk_plug_get_socket_window(GTK_PLUG(plug)) != NULL) |
| 3928 | #else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3929 | if (plug != NULL && GTK_PLUG(plug)->socket_window != NULL) |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 3930 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3931 | { |
| 3932 | gui.mainwin = plug; |
| 3933 | } |
| 3934 | else |
| 3935 | { |
| 3936 | g_warning("Connection to GTK+ socket (ID %u) failed", |
| 3937 | (unsigned int)gtk_socket_id); |
| 3938 | /* Pretend we never wanted it if it failed (get own window) */ |
| 3939 | gtk_socket_id = 0; |
| 3940 | } |
| 3941 | } |
| 3942 | |
| 3943 | if (gtk_socket_id == 0) |
| 3944 | { |
| 3945 | #ifdef FEAT_GUI_GNOME |
| 3946 | if (using_gnome) |
| 3947 | { |
| 3948 | gui.mainwin = gnome_app_new("Vim", NULL); |
| 3949 | # ifdef USE_XSMP |
| 3950 | /* Use the GNOME save-yourself functionality now. */ |
| 3951 | xsmp_close(); |
| 3952 | # endif |
| 3953 | } |
| 3954 | else |
| 3955 | #endif |
| 3956 | gui.mainwin = gtk_window_new(GTK_WINDOW_TOPLEVEL); |
| 3957 | } |
| 3958 | |
| 3959 | gtk_widget_set_name(gui.mainwin, "vim-main-window"); |
| 3960 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3961 | /* Create the PangoContext used for drawing all text. */ |
| 3962 | gui.text_context = gtk_widget_create_pango_context(gui.mainwin); |
| 3963 | pango_context_set_base_dir(gui.text_context, PANGO_DIRECTION_LTR); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3964 | |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 3965 | #if GTK_CHECK_VERSION(3,0,0) |
| 3966 | gtk_container_set_border_width(GTK_CONTAINER(gui.mainwin), 0); |
| 3967 | #else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3968 | gtk_container_border_width(GTK_CONTAINER(gui.mainwin), 0); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 3969 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3970 | gtk_widget_add_events(gui.mainwin, GDK_VISIBILITY_NOTIFY_MASK); |
| 3971 | |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 3972 | #if GTK_CHECK_VERSION(3,0,0) |
| 3973 | g_signal_connect(G_OBJECT(gui.mainwin), "delete-event", |
| 3974 | G_CALLBACK(&delete_event_cb), NULL); |
| 3975 | |
| 3976 | g_signal_connect(G_OBJECT(gui.mainwin), "realize", |
| 3977 | G_CALLBACK(&mainwin_realize), NULL); |
| 3978 | #else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3979 | gtk_signal_connect(GTK_OBJECT(gui.mainwin), "delete_event", |
| 3980 | GTK_SIGNAL_FUNC(&delete_event_cb), NULL); |
| 3981 | |
| 3982 | gtk_signal_connect(GTK_OBJECT(gui.mainwin), "realize", |
| 3983 | GTK_SIGNAL_FUNC(&mainwin_realize), NULL); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 3984 | #endif |
Bram Moolenaar | b463e8d | 2017-06-05 15:07:09 +0200 | [diff] [blame] | 3985 | |
| 3986 | g_signal_connect(G_OBJECT(gui.mainwin), "screen-changed", |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3987 | G_CALLBACK(&mainwin_screen_changed_cb), NULL); |
Bram Moolenaar | b463e8d | 2017-06-05 15:07:09 +0200 | [diff] [blame] | 3988 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3989 | gui.accel_group = gtk_accel_group_new(); |
| 3990 | gtk_window_add_accel_group(GTK_WINDOW(gui.mainwin), gui.accel_group); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3991 | |
Bram Moolenaar | 32466aa | 2006-02-24 23:53:04 +0000 | [diff] [blame] | 3992 | /* A vertical box holds the menubar, toolbar and main text window. */ |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 3993 | #if GTK_CHECK_VERSION(3,2,0) |
| 3994 | vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0); |
| 3995 | gtk_box_set_homogeneous(GTK_BOX(vbox), FALSE); |
| 3996 | #else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3997 | vbox = gtk_vbox_new(FALSE, 0); |
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 | |
| 4000 | #ifdef FEAT_GUI_GNOME |
| 4001 | if (using_gnome) |
| 4002 | { |
Bram Moolenaar | 182c5be | 2010-06-25 05:37:59 +0200 | [diff] [blame] | 4003 | # if defined(FEAT_MENU) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4004 | /* automagically restore menubar/toolbar placement */ |
| 4005 | gnome_app_enable_layout_config(GNOME_APP(gui.mainwin), TRUE); |
| 4006 | # endif |
| 4007 | gnome_app_set_contents(GNOME_APP(gui.mainwin), vbox); |
| 4008 | } |
| 4009 | else |
| 4010 | #endif |
| 4011 | { |
| 4012 | gtk_container_add(GTK_CONTAINER(gui.mainwin), vbox); |
| 4013 | gtk_widget_show(vbox); |
| 4014 | } |
| 4015 | |
| 4016 | #ifdef FEAT_MENU |
| 4017 | /* |
| 4018 | * Create the menubar and handle |
| 4019 | */ |
| 4020 | gui.menubar = gtk_menu_bar_new(); |
| 4021 | gtk_widget_set_name(gui.menubar, "vim-menubar"); |
| 4022 | |
Bram Moolenaar | 293ee4d | 2004-12-09 21:34:53 +0000 | [diff] [blame] | 4023 | /* Avoid that GTK takes <F10> away from us. */ |
| 4024 | { |
| 4025 | GtkSettings *gtk_settings; |
| 4026 | |
| 4027 | gtk_settings = gtk_settings_get_for_screen(gdk_screen_get_default()); |
| 4028 | g_object_set(gtk_settings, "gtk-menu-bar-accel", NULL, NULL); |
| 4029 | } |
Bram Moolenaar | 293ee4d | 2004-12-09 21:34:53 +0000 | [diff] [blame] | 4030 | |
| 4031 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4032 | # ifdef FEAT_GUI_GNOME |
| 4033 | if (using_gnome) |
| 4034 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4035 | BonoboDockItem *dockitem; |
| 4036 | |
| 4037 | gnome_app_set_menus(GNOME_APP(gui.mainwin), GTK_MENU_BAR(gui.menubar)); |
| 4038 | dockitem = gnome_app_get_dock_item_by_name(GNOME_APP(gui.mainwin), |
| 4039 | GNOME_APP_MENUBAR_NAME); |
Bram Moolenaar | db552d60 | 2006-03-23 22:59:57 +0000 | [diff] [blame] | 4040 | /* We don't want the menu to float. */ |
| 4041 | bonobo_dock_item_set_behavior(dockitem, |
| 4042 | bonobo_dock_item_get_behavior(dockitem) |
| 4043 | | BONOBO_DOCK_ITEM_BEH_NEVER_FLOATING); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4044 | gui.menubar_h = GTK_WIDGET(dockitem); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4045 | } |
| 4046 | else |
| 4047 | # endif /* FEAT_GUI_GNOME */ |
| 4048 | { |
Bram Moolenaar | 18144c8 | 2006-04-12 21:52:12 +0000 | [diff] [blame] | 4049 | /* Always show the menubar, otherwise <F10> doesn't work. It may be |
| 4050 | * disabled in gui_init() later. */ |
| 4051 | gtk_widget_show(gui.menubar); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4052 | gtk_box_pack_start(GTK_BOX(vbox), gui.menubar, FALSE, FALSE, 0); |
| 4053 | } |
| 4054 | #endif /* FEAT_MENU */ |
| 4055 | |
| 4056 | #ifdef FEAT_TOOLBAR |
| 4057 | /* |
| 4058 | * Create the toolbar and handle |
| 4059 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4060 | /* some aesthetics on the toolbar */ |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 4061 | # ifdef USE_GTK3 |
Bram Moolenaar | 3f2a5d8 | 2016-02-27 22:08:16 +0100 | [diff] [blame] | 4062 | /* TODO: Add GTK+ 3 code here using GtkCssProvider if necessary. */ |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 4063 | /* N.B. Since the default value of GtkToolbar::button-relief is |
| 4064 | * GTK_RELIEF_NONE, there's no need to specify that, probably. */ |
| 4065 | # else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4066 | gtk_rc_parse_string( |
| 4067 | "style \"vim-toolbar-style\" {\n" |
| 4068 | " GtkToolbar::button_relief = GTK_RELIEF_NONE\n" |
| 4069 | "}\n" |
| 4070 | "widget \"*.vim-toolbar\" style \"vim-toolbar-style\"\n"); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 4071 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4072 | gui.toolbar = gtk_toolbar_new(); |
| 4073 | gtk_widget_set_name(gui.toolbar, "vim-toolbar"); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4074 | set_toolbar_style(GTK_TOOLBAR(gui.toolbar)); |
| 4075 | |
| 4076 | # ifdef FEAT_GUI_GNOME |
| 4077 | if (using_gnome) |
| 4078 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4079 | BonoboDockItem *dockitem; |
| 4080 | |
| 4081 | gnome_app_set_toolbar(GNOME_APP(gui.mainwin), GTK_TOOLBAR(gui.toolbar)); |
| 4082 | dockitem = gnome_app_get_dock_item_by_name(GNOME_APP(gui.mainwin), |
| 4083 | GNOME_APP_TOOLBAR_NAME); |
| 4084 | gui.toolbar_h = GTK_WIDGET(dockitem); |
Bram Moolenaar | e580b0c | 2006-03-21 21:33:03 +0000 | [diff] [blame] | 4085 | /* When the toolbar is floating it gets stuck. So long as that isn't |
Bram Moolenaar | db552d60 | 2006-03-23 22:59:57 +0000 | [diff] [blame] | 4086 | * fixed let's disallow floating. */ |
Bram Moolenaar | e580b0c | 2006-03-21 21:33:03 +0000 | [diff] [blame] | 4087 | bonobo_dock_item_set_behavior(dockitem, |
Bram Moolenaar | db552d60 | 2006-03-23 22:59:57 +0000 | [diff] [blame] | 4088 | bonobo_dock_item_get_behavior(dockitem) |
| 4089 | | BONOBO_DOCK_ITEM_BEH_NEVER_FLOATING); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4090 | gtk_container_set_border_width(GTK_CONTAINER(gui.toolbar), 0); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4091 | } |
| 4092 | else |
| 4093 | # endif /* FEAT_GUI_GNOME */ |
| 4094 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4095 | if (vim_strchr(p_go, GO_TOOLBAR) != NULL |
| 4096 | && (toolbar_flags & (TOOLBAR_TEXT | TOOLBAR_ICONS))) |
| 4097 | gtk_widget_show(gui.toolbar); |
| 4098 | gtk_box_pack_start(GTK_BOX(vbox), gui.toolbar, FALSE, FALSE, 0); |
| 4099 | } |
| 4100 | #endif /* FEAT_TOOLBAR */ |
| 4101 | |
Bram Moolenaar | 32466aa | 2006-02-24 23:53:04 +0000 | [diff] [blame] | 4102 | #ifdef FEAT_GUI_TABLINE |
Bram Moolenaar | 97b2ad3 | 2006-03-18 21:40:56 +0000 | [diff] [blame] | 4103 | /* |
| 4104 | * Use a Notebook for the tab pages labels. The labels are hidden by |
| 4105 | * default. |
Bram Moolenaar | 97b2ad3 | 2006-03-18 21:40:56 +0000 | [diff] [blame] | 4106 | */ |
Bram Moolenaar | b3656ed | 2006-03-20 21:59:49 +0000 | [diff] [blame] | 4107 | gui.tabline = gtk_notebook_new(); |
| 4108 | gtk_widget_show(gui.tabline); |
| 4109 | gtk_box_pack_start(GTK_BOX(vbox), gui.tabline, FALSE, FALSE, 0); |
| 4110 | gtk_notebook_set_show_border(GTK_NOTEBOOK(gui.tabline), FALSE); |
| 4111 | gtk_notebook_set_show_tabs(GTK_NOTEBOOK(gui.tabline), FALSE); |
Bram Moolenaar | 4a85b41 | 2006-04-23 22:40:29 +0000 | [diff] [blame] | 4112 | gtk_notebook_set_scrollable(GTK_NOTEBOOK(gui.tabline), TRUE); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 4113 | # if !GTK_CHECK_VERSION(3,0,0) |
Bram Moolenaar | 9635157 | 2006-05-05 21:16:59 +0000 | [diff] [blame] | 4114 | gtk_notebook_set_tab_border(GTK_NOTEBOOK(gui.tabline), FALSE); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 4115 | # endif |
Bram Moolenaar | b3656ed | 2006-03-20 21:59:49 +0000 | [diff] [blame] | 4116 | |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 4117 | # if !GTK_CHECK_VERSION(3,0,0) |
Bram Moolenaar | 437df8f | 2006-04-27 21:47:44 +0000 | [diff] [blame] | 4118 | tabline_tooltip = gtk_tooltips_new(); |
| 4119 | gtk_tooltips_enable(GTK_TOOLTIPS(tabline_tooltip)); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 4120 | # endif |
Bram Moolenaar | 437df8f | 2006-04-27 21:47:44 +0000 | [diff] [blame] | 4121 | |
Bram Moolenaar | 32466aa | 2006-02-24 23:53:04 +0000 | [diff] [blame] | 4122 | { |
Bram Moolenaar | 437df8f | 2006-04-27 21:47:44 +0000 | [diff] [blame] | 4123 | GtkWidget *page, *label, *event_box; |
Bram Moolenaar | 32466aa | 2006-02-24 23:53:04 +0000 | [diff] [blame] | 4124 | |
Bram Moolenaar | c9b4b05 | 2006-04-30 18:54:39 +0000 | [diff] [blame] | 4125 | /* Add the first tab. */ |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 4126 | # if GTK_CHECK_VERSION(3,2,0) |
| 4127 | page = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0); |
| 4128 | gtk_box_set_homogeneous(GTK_BOX(page), FALSE); |
| 4129 | # else |
Bram Moolenaar | 437df8f | 2006-04-27 21:47:44 +0000 | [diff] [blame] | 4130 | page = gtk_vbox_new(FALSE, 0); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 4131 | # endif |
Bram Moolenaar | 437df8f | 2006-04-27 21:47:44 +0000 | [diff] [blame] | 4132 | gtk_widget_show(page); |
| 4133 | gtk_container_add(GTK_CONTAINER(gui.tabline), page); |
| 4134 | label = gtk_label_new("-Empty-"); |
| 4135 | gtk_widget_show(label); |
Bram Moolenaar | 437df8f | 2006-04-27 21:47:44 +0000 | [diff] [blame] | 4136 | event_box = gtk_event_box_new(); |
| 4137 | gtk_widget_show(event_box); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 4138 | # if GTK_CHECK_VERSION(3,0,0) |
| 4139 | g_object_set_data(G_OBJECT(event_box), "tab_num", GINT_TO_POINTER(1L)); |
| 4140 | # else |
Bram Moolenaar | 47b46d7 | 2008-07-02 19:05:48 +0000 | [diff] [blame] | 4141 | gtk_object_set_user_data(GTK_OBJECT(event_box), (gpointer)1L); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 4142 | # endif |
| 4143 | # if !GTK_CHECK_VERSION(3,14,0) |
Bram Moolenaar | 8ea9123 | 2006-04-28 22:41:43 +0000 | [diff] [blame] | 4144 | gtk_misc_set_padding(GTK_MISC(label), 2, 2); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 4145 | # endif |
Bram Moolenaar | 437df8f | 2006-04-27 21:47:44 +0000 | [diff] [blame] | 4146 | gtk_container_add(GTK_CONTAINER(event_box), label); |
Bram Moolenaar | 437df8f | 2006-04-27 21:47:44 +0000 | [diff] [blame] | 4147 | gtk_notebook_set_tab_label(GTK_NOTEBOOK(gui.tabline), page, event_box); |
Bram Moolenaar | ca05aa2 | 2017-10-22 15:36:14 +0200 | [diff] [blame] | 4148 | gtk_notebook_set_tab_reorderable(GTK_NOTEBOOK(gui.tabline), page, TRUE); |
Bram Moolenaar | 97b2ad3 | 2006-03-18 21:40:56 +0000 | [diff] [blame] | 4149 | } |
Bram Moolenaar | 54a709e | 2006-05-04 21:57:11 +0000 | [diff] [blame] | 4150 | |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 4151 | # if GTK_CHECK_VERSION(3,0,0) |
| 4152 | g_signal_connect(G_OBJECT(gui.tabline), "switch-page", |
| 4153 | G_CALLBACK(on_select_tab), NULL); |
Bram Moolenaar | ca05aa2 | 2017-10-22 15:36:14 +0200 | [diff] [blame] | 4154 | g_signal_connect(G_OBJECT(gui.tabline), "page-reordered", |
| 4155 | G_CALLBACK(on_tab_reordered), NULL); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 4156 | # else |
Bram Moolenaar | b3656ed | 2006-03-20 21:59:49 +0000 | [diff] [blame] | 4157 | gtk_signal_connect(GTK_OBJECT(gui.tabline), "switch_page", |
Bram Moolenaar | c9b4b05 | 2006-04-30 18:54:39 +0000 | [diff] [blame] | 4158 | GTK_SIGNAL_FUNC(on_select_tab), NULL); |
Bram Moolenaar | ca05aa2 | 2017-10-22 15:36:14 +0200 | [diff] [blame] | 4159 | gtk_signal_connect(GTK_OBJECT(gui.tabline), "page-reordered", |
| 4160 | GTK_SIGNAL_FUNC(on_tab_reordered), NULL); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 4161 | # endif |
Bram Moolenaar | b3656ed | 2006-03-20 21:59:49 +0000 | [diff] [blame] | 4162 | |
| 4163 | /* Create a popup menu for the tab line and connect it. */ |
| 4164 | tabline_menu = create_tabline_menu(); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 4165 | # if GTK_CHECK_VERSION(3,0,0) |
| 4166 | g_signal_connect_swapped(G_OBJECT(gui.tabline), "button-press-event", |
| 4167 | G_CALLBACK(on_tabline_menu), G_OBJECT(tabline_menu)); |
| 4168 | # else |
Bram Moolenaar | b3656ed | 2006-03-20 21:59:49 +0000 | [diff] [blame] | 4169 | gtk_signal_connect_object(GTK_OBJECT(gui.tabline), "button_press_event", |
Bram Moolenaar | c9b4b05 | 2006-04-30 18:54:39 +0000 | [diff] [blame] | 4170 | GTK_SIGNAL_FUNC(on_tabline_menu), GTK_OBJECT(tabline_menu)); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 4171 | # endif |
| 4172 | #endif /* FEAT_GUI_TABLINE */ |
Bram Moolenaar | 32466aa | 2006-02-24 23:53:04 +0000 | [diff] [blame] | 4173 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4174 | gui.formwin = gtk_form_new(); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 4175 | #if GTK_CHECK_VERSION(3,0,0) |
| 4176 | gtk_container_set_border_width(GTK_CONTAINER(gui.formwin), 0); |
| 4177 | #else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4178 | gtk_container_border_width(GTK_CONTAINER(gui.formwin), 0); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 4179 | #endif |
| 4180 | #if !GTK_CHECK_VERSION(3,0,0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4181 | gtk_widget_set_events(gui.formwin, GDK_EXPOSURE_MASK); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 4182 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4183 | |
| 4184 | gui.drawarea = gtk_drawing_area_new(); |
Bram Moolenaar | a859f04 | 2016-11-17 19:11:55 +0100 | [diff] [blame] | 4185 | #if GTK_CHECK_VERSION(3,22,2) |
| 4186 | gtk_widget_set_name(gui.drawarea, "vim-gui-drawarea"); |
| 4187 | #endif |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 4188 | #if GTK_CHECK_VERSION(3,0,0) |
| 4189 | gui.surface = NULL; |
| 4190 | gui.by_signal = FALSE; |
| 4191 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4192 | |
| 4193 | /* Determine which events we will filter. */ |
| 4194 | gtk_widget_set_events(gui.drawarea, |
| 4195 | GDK_EXPOSURE_MASK | |
| 4196 | GDK_ENTER_NOTIFY_MASK | |
| 4197 | GDK_LEAVE_NOTIFY_MASK | |
| 4198 | GDK_BUTTON_PRESS_MASK | |
| 4199 | GDK_BUTTON_RELEASE_MASK | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4200 | GDK_SCROLL_MASK | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4201 | GDK_KEY_PRESS_MASK | |
| 4202 | GDK_KEY_RELEASE_MASK | |
| 4203 | GDK_POINTER_MOTION_MASK | |
| 4204 | GDK_POINTER_MOTION_HINT_MASK); |
| 4205 | |
| 4206 | gtk_widget_show(gui.drawarea); |
| 4207 | gtk_form_put(GTK_FORM(gui.formwin), gui.drawarea, 0, 0); |
| 4208 | gtk_widget_show(gui.formwin); |
| 4209 | gtk_box_pack_start(GTK_BOX(vbox), gui.formwin, TRUE, TRUE, 0); |
| 4210 | |
| 4211 | /* For GtkSockets, key-presses must go to the focus widget (drawarea) |
| 4212 | * and not the window. */ |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 4213 | #if GTK_CHECK_VERSION(3,0,0) |
| 4214 | g_signal_connect((gtk_socket_id == 0) ? G_OBJECT(gui.mainwin) |
| 4215 | : G_OBJECT(gui.drawarea), |
| 4216 | "key-press-event", |
| 4217 | G_CALLBACK(key_press_event), NULL); |
| 4218 | #else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4219 | gtk_signal_connect((gtk_socket_id == 0) ? GTK_OBJECT(gui.mainwin) |
| 4220 | : GTK_OBJECT(gui.drawarea), |
| 4221 | "key_press_event", |
| 4222 | GTK_SIGNAL_FUNC(key_press_event), NULL); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 4223 | #endif |
| 4224 | #if defined(FEAT_XIM) || GTK_CHECK_VERSION(3,0,0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4225 | /* Also forward key release events for the benefit of GTK+ 2 input |
| 4226 | * modules. Try CTRL-SHIFT-xdigits to enter a Unicode code point. */ |
| 4227 | g_signal_connect((gtk_socket_id == 0) ? G_OBJECT(gui.mainwin) |
| 4228 | : G_OBJECT(gui.drawarea), |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 4229 | "key-release-event", |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4230 | G_CALLBACK(&key_release_event), NULL); |
| 4231 | #endif |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 4232 | #if GTK_CHECK_VERSION(3,0,0) |
| 4233 | g_signal_connect(G_OBJECT(gui.drawarea), "realize", |
| 4234 | G_CALLBACK(drawarea_realize_cb), NULL); |
| 4235 | g_signal_connect(G_OBJECT(gui.drawarea), "unrealize", |
| 4236 | G_CALLBACK(drawarea_unrealize_cb), NULL); |
| 4237 | g_signal_connect(G_OBJECT(gui.drawarea), "configure-event", |
| 4238 | G_CALLBACK(drawarea_configure_event_cb), NULL); |
Bram Moolenaar | a859f04 | 2016-11-17 19:11:55 +0100 | [diff] [blame] | 4239 | # if GTK_CHECK_VERSION(3,22,2) |
| 4240 | g_signal_connect_after(G_OBJECT(gui.drawarea), "style-updated", |
| 4241 | G_CALLBACK(&drawarea_style_updated_cb), NULL); |
| 4242 | # else |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 4243 | g_signal_connect_after(G_OBJECT(gui.drawarea), "style-set", |
| 4244 | G_CALLBACK(&drawarea_style_set_cb), NULL); |
Bram Moolenaar | a859f04 | 2016-11-17 19:11:55 +0100 | [diff] [blame] | 4245 | # endif |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 4246 | #else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4247 | gtk_signal_connect(GTK_OBJECT(gui.drawarea), "realize", |
| 4248 | GTK_SIGNAL_FUNC(drawarea_realize_cb), NULL); |
| 4249 | gtk_signal_connect(GTK_OBJECT(gui.drawarea), "unrealize", |
| 4250 | GTK_SIGNAL_FUNC(drawarea_unrealize_cb), NULL); |
| 4251 | |
| 4252 | gtk_signal_connect_after(GTK_OBJECT(gui.drawarea), "style_set", |
| 4253 | GTK_SIGNAL_FUNC(&drawarea_style_set_cb), NULL); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 4254 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4255 | |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 4256 | #if !GTK_CHECK_VERSION(3,0,0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4257 | gui.visibility = GDK_VISIBILITY_UNOBSCURED; |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 4258 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4259 | |
| 4260 | #if !(defined(FEAT_GUI_GNOME) && defined(FEAT_SESSION)) |
| 4261 | wm_protocols_atom = gdk_atom_intern("WM_PROTOCOLS", FALSE); |
| 4262 | save_yourself_atom = gdk_atom_intern("WM_SAVE_YOURSELF", FALSE); |
| 4263 | #endif |
| 4264 | |
| 4265 | if (gtk_socket_id != 0) |
Bram Moolenaar | b85cb21 | 2009-05-17 14:24:23 +0000 | [diff] [blame] | 4266 | /* make sure keyboard input can go to the drawarea */ |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 4267 | #if GTK_CHECK_VERSION(3,0,0) |
| 4268 | gtk_widget_set_can_focus(gui.drawarea, TRUE); |
| 4269 | #else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4270 | GTK_WIDGET_SET_FLAGS(gui.drawarea, GTK_CAN_FOCUS); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 4271 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4272 | |
| 4273 | /* |
| 4274 | * Set clipboard specific atoms |
| 4275 | */ |
| 4276 | vim_atom = gdk_atom_intern(VIM_ATOM_NAME, FALSE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4277 | vimenc_atom = gdk_atom_intern(VIMENC_ATOM_NAME, FALSE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4278 | clip_star.gtk_sel_atom = GDK_SELECTION_PRIMARY; |
| 4279 | clip_plus.gtk_sel_atom = gdk_atom_intern("CLIPBOARD", FALSE); |
| 4280 | |
| 4281 | /* |
| 4282 | * Start out by adding the configured border width into the border offset. |
| 4283 | */ |
| 4284 | gui.border_offset = gui.border_width; |
| 4285 | |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 4286 | #if GTK_CHECK_VERSION(3,0,0) |
| 4287 | g_signal_connect(G_OBJECT(gui.drawarea), "draw", |
| 4288 | G_CALLBACK(draw_event), NULL); |
| 4289 | #else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4290 | gtk_signal_connect(GTK_OBJECT(gui.mainwin), "visibility_notify_event", |
| 4291 | GTK_SIGNAL_FUNC(visibility_event), NULL); |
| 4292 | gtk_signal_connect(GTK_OBJECT(gui.drawarea), "expose_event", |
| 4293 | GTK_SIGNAL_FUNC(expose_event), NULL); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 4294 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4295 | |
| 4296 | /* |
| 4297 | * Only install these enter/leave callbacks when 'p' in 'guioptions'. |
| 4298 | * Only needed for some window managers. |
| 4299 | */ |
| 4300 | if (vim_strchr(p_go, GO_POINTER) != NULL) |
| 4301 | { |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 4302 | #if GTK_CHECK_VERSION(3,0,0) |
| 4303 | g_signal_connect(G_OBJECT(gui.drawarea), "leave-notify-event", |
| 4304 | G_CALLBACK(leave_notify_event), NULL); |
| 4305 | g_signal_connect(G_OBJECT(gui.drawarea), "enter-notify-event", |
| 4306 | G_CALLBACK(enter_notify_event), NULL); |
| 4307 | #else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4308 | gtk_signal_connect(GTK_OBJECT(gui.drawarea), "leave_notify_event", |
| 4309 | GTK_SIGNAL_FUNC(leave_notify_event), NULL); |
| 4310 | gtk_signal_connect(GTK_OBJECT(gui.drawarea), "enter_notify_event", |
| 4311 | GTK_SIGNAL_FUNC(enter_notify_event), NULL); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 4312 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4313 | } |
| 4314 | |
Bram Moolenaar | b3656ed | 2006-03-20 21:59:49 +0000 | [diff] [blame] | 4315 | /* Real windows can get focus ... GtkPlug, being a mere container can't, |
| 4316 | * only its widgets. Arguably, this could be common code and we not use |
| 4317 | * the window focus at all, but let's be safe. |
| 4318 | */ |
| 4319 | if (gtk_socket_id == 0) |
| 4320 | { |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 4321 | #if GTK_CHECK_VERSION(3,0,0) |
| 4322 | g_signal_connect(G_OBJECT(gui.mainwin), "focus-out-event", |
| 4323 | G_CALLBACK(focus_out_event), NULL); |
| 4324 | g_signal_connect(G_OBJECT(gui.mainwin), "focus-in-event", |
| 4325 | G_CALLBACK(focus_in_event), NULL); |
| 4326 | #else |
Bram Moolenaar | c9b4b05 | 2006-04-30 18:54:39 +0000 | [diff] [blame] | 4327 | gtk_signal_connect(GTK_OBJECT(gui.mainwin), "focus_out_event", |
| 4328 | GTK_SIGNAL_FUNC(focus_out_event), NULL); |
| 4329 | gtk_signal_connect(GTK_OBJECT(gui.mainwin), "focus_in_event", |
| 4330 | GTK_SIGNAL_FUNC(focus_in_event), NULL); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 4331 | #endif |
Bram Moolenaar | b3656ed | 2006-03-20 21:59:49 +0000 | [diff] [blame] | 4332 | } |
| 4333 | else |
| 4334 | { |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 4335 | #if GTK_CHECK_VERSION(3,0,0) |
| 4336 | g_signal_connect(G_OBJECT(gui.drawarea), "focus-out-event", |
| 4337 | G_CALLBACK(focus_out_event), NULL); |
| 4338 | g_signal_connect(G_OBJECT(gui.drawarea), "focus-in-event", |
| 4339 | G_CALLBACK(focus_in_event), NULL); |
| 4340 | #else |
Bram Moolenaar | c9b4b05 | 2006-04-30 18:54:39 +0000 | [diff] [blame] | 4341 | gtk_signal_connect(GTK_OBJECT(gui.drawarea), "focus_out_event", |
| 4342 | GTK_SIGNAL_FUNC(focus_out_event), NULL); |
| 4343 | gtk_signal_connect(GTK_OBJECT(gui.drawarea), "focus_in_event", |
| 4344 | GTK_SIGNAL_FUNC(focus_in_event), NULL); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 4345 | #endif |
Bram Moolenaar | b3656ed | 2006-03-20 21:59:49 +0000 | [diff] [blame] | 4346 | #ifdef FEAT_GUI_TABLINE |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 4347 | # if GTK_CHECK_VERSION(3,0,0) |
| 4348 | g_signal_connect(G_OBJECT(gui.tabline), "focus-out-event", |
| 4349 | G_CALLBACK(focus_out_event), NULL); |
| 4350 | g_signal_connect(G_OBJECT(gui.tabline), "focus-in-event", |
| 4351 | G_CALLBACK(focus_in_event), NULL); |
| 4352 | # else |
Bram Moolenaar | c9b4b05 | 2006-04-30 18:54:39 +0000 | [diff] [blame] | 4353 | gtk_signal_connect(GTK_OBJECT(gui.tabline), "focus_out_event", |
| 4354 | GTK_SIGNAL_FUNC(focus_out_event), NULL); |
| 4355 | gtk_signal_connect(GTK_OBJECT(gui.tabline), "focus_in_event", |
| 4356 | GTK_SIGNAL_FUNC(focus_in_event), NULL); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 4357 | # endif |
Bram Moolenaar | b3656ed | 2006-03-20 21:59:49 +0000 | [diff] [blame] | 4358 | #endif /* FEAT_GUI_TABLINE */ |
| 4359 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4360 | |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 4361 | #if GTK_CHECK_VERSION(3,0,0) |
| 4362 | g_signal_connect(G_OBJECT(gui.drawarea), "motion-notify-event", |
| 4363 | G_CALLBACK(motion_notify_event), NULL); |
| 4364 | g_signal_connect(G_OBJECT(gui.drawarea), "button-press-event", |
| 4365 | G_CALLBACK(button_press_event), NULL); |
| 4366 | g_signal_connect(G_OBJECT(gui.drawarea), "button-release-event", |
| 4367 | G_CALLBACK(button_release_event), NULL); |
| 4368 | g_signal_connect(G_OBJECT(gui.drawarea), "scroll-event", |
| 4369 | G_CALLBACK(&scroll_event), NULL); |
| 4370 | #else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4371 | gtk_signal_connect(GTK_OBJECT(gui.drawarea), "motion_notify_event", |
| 4372 | GTK_SIGNAL_FUNC(motion_notify_event), NULL); |
| 4373 | gtk_signal_connect(GTK_OBJECT(gui.drawarea), "button_press_event", |
| 4374 | GTK_SIGNAL_FUNC(button_press_event), NULL); |
| 4375 | gtk_signal_connect(GTK_OBJECT(gui.drawarea), "button_release_event", |
| 4376 | GTK_SIGNAL_FUNC(button_release_event), NULL); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4377 | g_signal_connect(G_OBJECT(gui.drawarea), "scroll_event", |
| 4378 | G_CALLBACK(&scroll_event), NULL); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 4379 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4380 | |
| 4381 | /* |
| 4382 | * Add selection handler functions. |
| 4383 | */ |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 4384 | #if GTK_CHECK_VERSION(3,0,0) |
| 4385 | g_signal_connect(G_OBJECT(gui.drawarea), "selection-clear-event", |
| 4386 | G_CALLBACK(selection_clear_event), NULL); |
| 4387 | g_signal_connect(G_OBJECT(gui.drawarea), "selection-received", |
| 4388 | G_CALLBACK(selection_received_cb), NULL); |
| 4389 | #else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4390 | gtk_signal_connect(GTK_OBJECT(gui.drawarea), "selection_clear_event", |
| 4391 | GTK_SIGNAL_FUNC(selection_clear_event), NULL); |
| 4392 | gtk_signal_connect(GTK_OBJECT(gui.drawarea), "selection_received", |
| 4393 | GTK_SIGNAL_FUNC(selection_received_cb), NULL); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 4394 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4395 | |
Bram Moolenaar | a76638f | 2010-06-05 12:49:46 +0200 | [diff] [blame] | 4396 | gui_gtk_set_selection_targets(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4397 | |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 4398 | #if GTK_CHECK_VERSION(3,0,0) |
| 4399 | g_signal_connect(G_OBJECT(gui.drawarea), "selection-get", |
| 4400 | G_CALLBACK(selection_get_cb), NULL); |
| 4401 | #else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4402 | gtk_signal_connect(GTK_OBJECT(gui.drawarea), "selection_get", |
| 4403 | GTK_SIGNAL_FUNC(selection_get_cb), NULL); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 4404 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4405 | |
| 4406 | /* Pretend we don't have input focus, we will get an event if we do. */ |
| 4407 | gui.in_focus = FALSE; |
| 4408 | |
Bram Moolenaar | 7ebf4e1 | 2018-08-07 20:01:40 +0200 | [diff] [blame] | 4409 | // Handle changes to the "Xft/DPI" setting. |
| 4410 | { |
| 4411 | GtkSettings *gtk_settings = |
| 4412 | gtk_settings_get_for_screen(gdk_screen_get_default()); |
| 4413 | |
| 4414 | g_signal_connect(gtk_settings, "notify::gtk-xft-dpi", |
| 4415 | G_CALLBACK(gtk_settings_xft_dpi_changed_cb), NULL); |
| 4416 | } |
| 4417 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4418 | return OK; |
| 4419 | } |
| 4420 | |
| 4421 | #if (defined(FEAT_GUI_GNOME) && defined(FEAT_SESSION)) || defined(PROTO) |
| 4422 | /* |
| 4423 | * This is called from gui_start() after a fork() has been done. |
| 4424 | * We have to tell the session manager our new PID. |
| 4425 | */ |
| 4426 | void |
| 4427 | gui_mch_forked(void) |
| 4428 | { |
| 4429 | if (using_gnome) |
| 4430 | { |
| 4431 | GnomeClient *client; |
| 4432 | |
| 4433 | client = gnome_master_client(); |
| 4434 | |
| 4435 | if (client != NULL) |
| 4436 | gnome_client_set_process_id(client, getpid()); |
| 4437 | } |
| 4438 | } |
| 4439 | #endif /* FEAT_GUI_GNOME && FEAT_SESSION */ |
| 4440 | |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 4441 | #if GTK_CHECK_VERSION(3,0,0) |
Bram Moolenaar | 36edf06 | 2016-07-21 22:10:12 +0200 | [diff] [blame] | 4442 | static GdkRGBA |
| 4443 | color_to_rgba(guicolor_T color) |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 4444 | { |
Bram Moolenaar | 36edf06 | 2016-07-21 22:10:12 +0200 | [diff] [blame] | 4445 | GdkRGBA rgba; |
| 4446 | rgba.red = ((color & 0xff0000) >> 16) / 255.0; |
| 4447 | rgba.green = ((color & 0xff00) >> 8) / 255.0; |
| 4448 | rgba.blue = ((color & 0xff)) / 255.0; |
| 4449 | rgba.alpha = 1.0; |
| 4450 | return rgba; |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 4451 | } |
| 4452 | |
| 4453 | static void |
Bram Moolenaar | 36edf06 | 2016-07-21 22:10:12 +0200 | [diff] [blame] | 4454 | set_cairo_source_rgba_from_color(cairo_t *cr, guicolor_T color) |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 4455 | { |
Bram Moolenaar | 36edf06 | 2016-07-21 22:10:12 +0200 | [diff] [blame] | 4456 | const GdkRGBA rgba = color_to_rgba(color); |
| 4457 | 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] | 4458 | } |
| 4459 | #endif /* GTK_CHECK_VERSION(3,0,0) */ |
| 4460 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4461 | /* |
| 4462 | * Called when the foreground or background color has been changed. |
| 4463 | * This used to change the graphics contexts directly but we are |
| 4464 | * currently manipulating them where desired. |
| 4465 | */ |
| 4466 | void |
| 4467 | gui_mch_new_colors(void) |
| 4468 | { |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 4469 | #if GTK_CHECK_VERSION(3,0,0) |
Bram Moolenaar | a859f04 | 2016-11-17 19:11:55 +0100 | [diff] [blame] | 4470 | # if !GTK_CHECK_VERSION(3,22,2) |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 4471 | GdkWindow * const da_win = gtk_widget_get_window(gui.drawarea); |
Bram Moolenaar | a859f04 | 2016-11-17 19:11:55 +0100 | [diff] [blame] | 4472 | # endif |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 4473 | |
| 4474 | if (gui.drawarea != NULL && gtk_widget_get_window(gui.drawarea) != NULL) |
| 4475 | #else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4476 | if (gui.drawarea != NULL && gui.drawarea->window != NULL) |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 4477 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4478 | { |
Bram Moolenaar | a859f04 | 2016-11-17 19:11:55 +0100 | [diff] [blame] | 4479 | #if GTK_CHECK_VERSION(3,22,2) |
| 4480 | GtkStyleContext * const context |
| 4481 | = gtk_widget_get_style_context(gui.drawarea); |
| 4482 | GtkCssProvider * const provider = gtk_css_provider_new(); |
| 4483 | gchar * const css = g_strdup_printf( |
| 4484 | "widget#vim-gui-drawarea {\n" |
| 4485 | " background-color: #%.2lx%.2lx%.2lx;\n" |
| 4486 | "}\n", |
| 4487 | (gui.back_pixel >> 16) & 0xff, |
| 4488 | (gui.back_pixel >> 8) & 0xff, |
| 4489 | gui.back_pixel & 0xff); |
| 4490 | |
| 4491 | gtk_css_provider_load_from_data(provider, css, -1, NULL); |
| 4492 | gtk_style_context_add_provider(context, |
| 4493 | GTK_STYLE_PROVIDER(provider), G_MAXUINT); |
| 4494 | |
| 4495 | g_free(css); |
| 4496 | g_object_unref(provider); |
| 4497 | #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] | 4498 | GdkRGBA rgba; |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 4499 | |
Bram Moolenaar | 36edf06 | 2016-07-21 22:10:12 +0200 | [diff] [blame] | 4500 | rgba = color_to_rgba(gui.back_pixel); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 4501 | { |
| 4502 | cairo_pattern_t * const pat = cairo_pattern_create_rgba( |
Bram Moolenaar | 36edf06 | 2016-07-21 22:10:12 +0200 | [diff] [blame] | 4503 | rgba.red, rgba.green, rgba.blue, rgba.alpha); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 4504 | if (pat != NULL) |
| 4505 | { |
| 4506 | gdk_window_set_background_pattern(da_win, pat); |
| 4507 | cairo_pattern_destroy(pat); |
| 4508 | } |
| 4509 | else |
Bram Moolenaar | 36edf06 | 2016-07-21 22:10:12 +0200 | [diff] [blame] | 4510 | gdk_window_set_background_rgba(da_win, &rgba); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 4511 | } |
| 4512 | #else /* !GTK_CHECK_VERSION(3,4,0) */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4513 | GdkColor color = { 0, 0, 0, 0 }; |
| 4514 | |
| 4515 | color.pixel = gui.back_pixel; |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 4516 | # if GTK_CHECK_VERSION(3,0,0) |
| 4517 | gdk_window_set_background(da_win, &color); |
| 4518 | # else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4519 | gdk_window_set_background(gui.drawarea->window, &color); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 4520 | # endif |
Bram Moolenaar | a859f04 | 2016-11-17 19:11:55 +0100 | [diff] [blame] | 4521 | #endif /* !GTK_CHECK_VERSION(3,22,2) */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4522 | } |
| 4523 | } |
| 4524 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4525 | /* |
| 4526 | * This signal informs us about the need to rearrange our sub-widgets. |
| 4527 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4528 | static gint |
Bram Moolenaar | b85cb21 | 2009-05-17 14:24:23 +0000 | [diff] [blame] | 4529 | form_configure_event(GtkWidget *widget UNUSED, |
| 4530 | GdkEventConfigure *event, |
| 4531 | gpointer data UNUSED) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4532 | { |
Bram Moolenaar | b3656ed | 2006-03-20 21:59:49 +0000 | [diff] [blame] | 4533 | int usable_height = event->height; |
| 4534 | |
Bram Moolenaar | 182707a | 2016-11-21 20:55:58 +0100 | [diff] [blame] | 4535 | #if GTK_CHECK_VERSION(3,22,2) && !GTK_CHECK_VERSION(3,22,4) |
Bram Moolenaar | a859f04 | 2016-11-17 19:11:55 +0100 | [diff] [blame] | 4536 | /* As of 3.22.2, GdkWindows have started distributing configure events to |
| 4537 | * their "native" children (https://git.gnome.org/browse/gtk+/commit/?h=gtk-3-22&id=12579fe71b3b8f79eb9c1b80e429443bcc437dd0). |
| 4538 | * |
| 4539 | * As can be seen from the implementation of move_native_children() and |
| 4540 | * configure_native_child() in gdkwindow.c, those functions actually |
| 4541 | * propagate configure events to every child, failing to distinguish |
| 4542 | * "native" one from non-native one. |
| 4543 | * |
| 4544 | * Naturally, configure events propagated to here like that are fallacious |
| 4545 | * and, as a matter of fact, they trigger a geometric collapse of |
| 4546 | * gui.formwin. |
| 4547 | * |
| 4548 | * To filter out such fallacious events, check if the given event is the |
| 4549 | * one that was sent out to the right place. Ignore it if not. |
| 4550 | */ |
Bram Moolenaar | 182707a | 2016-11-21 20:55:58 +0100 | [diff] [blame] | 4551 | /* Follow-up |
| 4552 | * After a few weeks later, the GdkWindow change mentioned above was |
| 4553 | * reverted (https://git.gnome.org/browse/gtk+/commit/?h=gtk-3-22&id=f70039cb9603a02d2369fec4038abf40a1711155). |
| 4554 | * The corresponding official release is 3.22.4. */ |
Bram Moolenaar | a859f04 | 2016-11-17 19:11:55 +0100 | [diff] [blame] | 4555 | if (event->window != gtk_widget_get_window(gui.formwin)) |
| 4556 | return TRUE; |
| 4557 | #endif |
| 4558 | |
Bram Moolenaar | b3656ed | 2006-03-20 21:59:49 +0000 | [diff] [blame] | 4559 | /* When in a GtkPlug, we can't guarantee valid heights (as a round |
| 4560 | * no. of char-heights), so we have to manually sanitise them. |
| 4561 | * Widths seem to sort themselves out, don't ask me why. |
| 4562 | */ |
| 4563 | if (gtk_socket_id != 0) |
Bram Moolenaar | c9b4b05 | 2006-04-30 18:54:39 +0000 | [diff] [blame] | 4564 | usable_height -= (gui.char_height - (gui.char_height/2)); /* sic. */ |
Bram Moolenaar | b3656ed | 2006-03-20 21:59:49 +0000 | [diff] [blame] | 4565 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4566 | gtk_form_freeze(GTK_FORM(gui.formwin)); |
Bram Moolenaar | b3656ed | 2006-03-20 21:59:49 +0000 | [diff] [blame] | 4567 | gui_resize_shell(event->width, usable_height); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4568 | gtk_form_thaw(GTK_FORM(gui.formwin)); |
| 4569 | |
| 4570 | return TRUE; |
| 4571 | } |
| 4572 | |
| 4573 | /* |
| 4574 | * Function called when window already closed. |
| 4575 | * We can't do much more here than to trying to preserve what had been done, |
| 4576 | * since the window is already inevitably going away. |
| 4577 | */ |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 4578 | #if GTK_CHECK_VERSION(3,0,0) |
| 4579 | static void |
| 4580 | mainwin_destroy_cb(GObject *object UNUSED, gpointer data UNUSED) |
| 4581 | #else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4582 | static void |
Bram Moolenaar | b85cb21 | 2009-05-17 14:24:23 +0000 | [diff] [blame] | 4583 | mainwin_destroy_cb(GtkObject *object UNUSED, gpointer data UNUSED) |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 4584 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4585 | { |
| 4586 | /* Don't write messages to the GUI anymore */ |
| 4587 | full_screen = FALSE; |
| 4588 | |
| 4589 | gui.mainwin = NULL; |
| 4590 | gui.drawarea = NULL; |
| 4591 | |
| 4592 | if (!exiting) /* only do anything if the destroy was unexpected */ |
| 4593 | { |
Bram Moolenaar | ce0842a | 2005-07-18 21:58:11 +0000 | [diff] [blame] | 4594 | vim_strncpy(IObuff, |
| 4595 | (char_u *)_("Vim: Main window unexpectedly destroyed\n"), |
| 4596 | IOSIZE - 1); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4597 | preserve_exit(); |
| 4598 | } |
Bram Moolenaar | 36e294c | 2015-12-29 18:55:46 +0100 | [diff] [blame] | 4599 | #ifdef USE_GRESOURCE |
| 4600 | gui_gtk_unregister_resource(); |
| 4601 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4602 | } |
| 4603 | |
Bram Moolenaar | b3656ed | 2006-03-20 21:59:49 +0000 | [diff] [blame] | 4604 | |
| 4605 | /* |
| 4606 | * Bit of a hack to ensure we start GtkPlug windows with the correct window |
| 4607 | * hints (and thus the required size from -geom), but that after that we |
| 4608 | * put the hints back to normal (the actual minimum size) so we may |
| 4609 | * subsequently be resized smaller. GtkSocket (the parent end) uses the |
Bram Moolenaar | 4932594 | 2007-05-10 19:19:59 +0000 | [diff] [blame] | 4610 | * plug's window 'min hints to set *it's* minimum size, but that's also the |
Bram Moolenaar | b3656ed | 2006-03-20 21:59:49 +0000 | [diff] [blame] | 4611 | * only way we have of making ourselves bigger (by set lines/columns). |
| 4612 | * 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] | 4613 | * second after the final attempt to reset the real minimum hints (done by |
| 4614 | * scrollbar init.), actually do the standard hints and stop the timer. |
Bram Moolenaar | b3656ed | 2006-03-20 21:59:49 +0000 | [diff] [blame] | 4615 | * We'll not let the default hints be set while this timer's active. |
| 4616 | */ |
Bram Moolenaar | 4ab7968 | 2017-08-27 14:50:47 +0200 | [diff] [blame] | 4617 | static timeout_cb_type |
Bram Moolenaar | b85cb21 | 2009-05-17 14:24:23 +0000 | [diff] [blame] | 4618 | check_startup_plug_hints(gpointer data UNUSED) |
Bram Moolenaar | b3656ed | 2006-03-20 21:59:49 +0000 | [diff] [blame] | 4619 | { |
| 4620 | if (init_window_hints_state == 1) |
| 4621 | { |
Bram Moolenaar | c9b4b05 | 2006-04-30 18:54:39 +0000 | [diff] [blame] | 4622 | /* Safe to use normal hints now */ |
| 4623 | init_window_hints_state = 0; |
| 4624 | update_window_manager_hints(0, 0); |
| 4625 | return FALSE; /* stop timer */ |
Bram Moolenaar | b3656ed | 2006-03-20 21:59:49 +0000 | [diff] [blame] | 4626 | } |
| 4627 | |
| 4628 | /* Keep on trying */ |
| 4629 | init_window_hints_state = 1; |
| 4630 | return TRUE; |
| 4631 | } |
| 4632 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4633 | /* |
| 4634 | * Open the GUI window which was created by a call to gui_mch_init(). |
| 4635 | */ |
| 4636 | int |
| 4637 | gui_mch_open(void) |
| 4638 | { |
| 4639 | guicolor_T fg_pixel = INVALCOLOR; |
| 4640 | guicolor_T bg_pixel = INVALCOLOR; |
Bram Moolenaar | 79ef6d6 | 2009-09-23 15:35:48 +0000 | [diff] [blame] | 4641 | guint pixel_width; |
| 4642 | guint pixel_height; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4643 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4644 | /* |
| 4645 | * Allow setting a window role on the command line, or invent one |
| 4646 | * if none was specified. This is mainly useful for GNOME session |
| 4647 | * support; allowing the WM to restore window placement. |
| 4648 | */ |
| 4649 | if (role_argument != NULL) |
| 4650 | { |
| 4651 | gtk_window_set_role(GTK_WINDOW(gui.mainwin), role_argument); |
| 4652 | } |
| 4653 | else |
| 4654 | { |
| 4655 | char *role; |
| 4656 | |
| 4657 | /* Invent a unique-enough ID string for the role */ |
| 4658 | role = g_strdup_printf("vim-%u-%u-%u", |
| 4659 | (unsigned)mch_get_pid(), |
| 4660 | (unsigned)g_random_int(), |
| 4661 | (unsigned)time(NULL)); |
| 4662 | |
| 4663 | gtk_window_set_role(GTK_WINDOW(gui.mainwin), role); |
| 4664 | g_free(role); |
| 4665 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4666 | |
| 4667 | if (gui_win_x != -1 && gui_win_y != -1) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4668 | 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] | 4669 | |
| 4670 | /* Determine user specified geometry, if present. */ |
| 4671 | if (gui.geom != NULL) |
| 4672 | { |
| 4673 | int mask; |
| 4674 | unsigned int w, h; |
| 4675 | int x = 0; |
| 4676 | int y = 0; |
| 4677 | |
| 4678 | mask = XParseGeometry((char *)gui.geom, &x, &y, &w, &h); |
| 4679 | |
| 4680 | if (mask & WidthValue) |
| 4681 | Columns = w; |
| 4682 | if (mask & HeightValue) |
Bram Moolenaar | d68071d | 2006-05-02 22:08:30 +0000 | [diff] [blame] | 4683 | { |
Bram Moolenaar | b85cb21 | 2009-05-17 14:24:23 +0000 | [diff] [blame] | 4684 | if (p_window > (long)h - 1 || !option_was_set((char_u *)"window")) |
Bram Moolenaar | d68071d | 2006-05-02 22:08:30 +0000 | [diff] [blame] | 4685 | p_window = h - 1; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4686 | Rows = h; |
Bram Moolenaar | d68071d | 2006-05-02 22:08:30 +0000 | [diff] [blame] | 4687 | } |
Bram Moolenaar | e057d40 | 2013-06-30 17:51:51 +0200 | [diff] [blame] | 4688 | limit_screen_size(); |
Bram Moolenaar | 2dd8b52 | 2007-10-19 12:33:44 +0000 | [diff] [blame] | 4689 | |
| 4690 | pixel_width = (guint)(gui_get_base_width() + Columns * gui.char_width); |
| 4691 | pixel_height = (guint)(gui_get_base_height() + Rows * gui.char_height); |
| 4692 | |
Bram Moolenaar | 2dd8b52 | 2007-10-19 12:33:44 +0000 | [diff] [blame] | 4693 | pixel_width += get_menu_tool_width(); |
| 4694 | pixel_height += get_menu_tool_height(); |
Bram Moolenaar | 2dd8b52 | 2007-10-19 12:33:44 +0000 | [diff] [blame] | 4695 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4696 | if (mask & (XValue | YValue)) |
Bram Moolenaar | 2dd8b52 | 2007-10-19 12:33:44 +0000 | [diff] [blame] | 4697 | { |
Bram Moolenaar | fe86f2d | 2008-11-28 20:29:07 +0000 | [diff] [blame] | 4698 | int ww, hh; |
| 4699 | gui_mch_get_screen_dimensions(&ww, &hh); |
| 4700 | hh += p_ghr + get_menu_tool_height(); |
| 4701 | ww += get_menu_tool_width(); |
Bram Moolenaar | 2dd8b52 | 2007-10-19 12:33:44 +0000 | [diff] [blame] | 4702 | if (mask & XNegative) |
Bram Moolenaar | fe86f2d | 2008-11-28 20:29:07 +0000 | [diff] [blame] | 4703 | x += ww - pixel_width; |
Bram Moolenaar | 2dd8b52 | 2007-10-19 12:33:44 +0000 | [diff] [blame] | 4704 | if (mask & YNegative) |
Bram Moolenaar | fe86f2d | 2008-11-28 20:29:07 +0000 | [diff] [blame] | 4705 | y += hh - pixel_height; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4706 | gtk_window_move(GTK_WINDOW(gui.mainwin), x, y); |
Bram Moolenaar | 2dd8b52 | 2007-10-19 12:33:44 +0000 | [diff] [blame] | 4707 | } |
Bram Moolenaar | d23a823 | 2018-02-10 18:45:26 +0100 | [diff] [blame] | 4708 | VIM_CLEAR(gui.geom); |
Bram Moolenaar | b3656ed | 2006-03-20 21:59:49 +0000 | [diff] [blame] | 4709 | |
Bram Moolenaar | c9b4b05 | 2006-04-30 18:54:39 +0000 | [diff] [blame] | 4710 | /* From now until everyone's stopped trying to set the window hints |
| 4711 | * to their correct minimum values, stop them being set as we need |
| 4712 | * them to remain at our required size for the parent GtkSocket to |
| 4713 | * give us the right initial size. |
| 4714 | */ |
Bram Moolenaar | b3656ed | 2006-03-20 21:59:49 +0000 | [diff] [blame] | 4715 | if (gtk_socket_id != 0 && (mask & WidthValue || mask & HeightValue)) |
Bram Moolenaar | c9b4b05 | 2006-04-30 18:54:39 +0000 | [diff] [blame] | 4716 | { |
Bram Moolenaar | c9b4b05 | 2006-04-30 18:54:39 +0000 | [diff] [blame] | 4717 | update_window_manager_hints(pixel_width, pixel_height); |
| 4718 | init_window_hints_state = 1; |
Bram Moolenaar | 4ab7968 | 2017-08-27 14:50:47 +0200 | [diff] [blame] | 4719 | timeout_add(1000, check_startup_plug_hints, NULL); |
Bram Moolenaar | c9b4b05 | 2006-04-30 18:54:39 +0000 | [diff] [blame] | 4720 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4721 | } |
| 4722 | |
Bram Moolenaar | 79ef6d6 | 2009-09-23 15:35:48 +0000 | [diff] [blame] | 4723 | pixel_width = (guint)(gui_get_base_width() + Columns * gui.char_width); |
| 4724 | pixel_height = (guint)(gui_get_base_height() + Rows * gui.char_height); |
Bram Moolenaar | 79ef6d6 | 2009-09-23 15:35:48 +0000 | [diff] [blame] | 4725 | /* For GTK2 changing the size of the form widget doesn't cause window |
| 4726 | * resizing. */ |
Bram Moolenaar | debe25a | 2010-06-06 17:41:24 +0200 | [diff] [blame] | 4727 | if (gtk_socket_id == 0) |
Bram Moolenaar | 79ef6d6 | 2009-09-23 15:35:48 +0000 | [diff] [blame] | 4728 | gtk_window_resize(GTK_WINDOW(gui.mainwin), pixel_width, pixel_height); |
Bram Moolenaar | b3656ed | 2006-03-20 21:59:49 +0000 | [diff] [blame] | 4729 | update_window_manager_hints(0, 0); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4730 | |
| 4731 | if (foreground_argument != NULL) |
| 4732 | fg_pixel = gui_get_color((char_u *)foreground_argument); |
| 4733 | if (fg_pixel == INVALCOLOR) |
| 4734 | fg_pixel = gui_get_color((char_u *)"Black"); |
| 4735 | |
| 4736 | if (background_argument != NULL) |
| 4737 | bg_pixel = gui_get_color((char_u *)background_argument); |
| 4738 | if (bg_pixel == INVALCOLOR) |
| 4739 | bg_pixel = gui_get_color((char_u *)"White"); |
| 4740 | |
| 4741 | if (found_reverse_arg) |
| 4742 | { |
| 4743 | gui.def_norm_pixel = bg_pixel; |
| 4744 | gui.def_back_pixel = fg_pixel; |
| 4745 | } |
| 4746 | else |
| 4747 | { |
| 4748 | gui.def_norm_pixel = fg_pixel; |
| 4749 | gui.def_back_pixel = bg_pixel; |
| 4750 | } |
| 4751 | |
| 4752 | /* Get the colors from the "Normal" and "Menu" group (set in syntax.c or |
| 4753 | * in a vimrc file) */ |
| 4754 | set_normal_colors(); |
| 4755 | |
| 4756 | /* Check that none of the colors are the same as the background color */ |
| 4757 | gui_check_colors(); |
| 4758 | |
| 4759 | /* Get the colors for the highlight groups (gui_check_colors() might have |
| 4760 | * changed them). */ |
| 4761 | highlight_gui_started(); /* re-init colors and fonts */ |
| 4762 | |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 4763 | #if GTK_CHECK_VERSION(3,0,0) |
| 4764 | g_signal_connect(G_OBJECT(gui.mainwin), "destroy", |
| 4765 | G_CALLBACK(mainwin_destroy_cb), NULL); |
| 4766 | #else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4767 | gtk_signal_connect(GTK_OBJECT(gui.mainwin), "destroy", |
| 4768 | GTK_SIGNAL_FUNC(mainwin_destroy_cb), NULL); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 4769 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4770 | |
| 4771 | #ifdef FEAT_HANGULIN |
| 4772 | hangul_keyboard_set(); |
| 4773 | #endif |
| 4774 | |
| 4775 | /* |
| 4776 | * Notify the fixed area about the need to resize the contents of the |
| 4777 | * gui.formwin, which we use for random positioning of the included |
| 4778 | * components. |
| 4779 | * |
| 4780 | * We connect this signal deferred finally after anything is in place, |
| 4781 | * since this is intended to handle resizements coming from the window |
| 4782 | * manager upon us and should not interfere with what VIM is requesting |
| 4783 | * upon startup. |
| 4784 | */ |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 4785 | #if GTK_CHECK_VERSION(3,0,0) |
| 4786 | g_signal_connect(G_OBJECT(gui.formwin), "configure-event", |
| 4787 | G_CALLBACK(form_configure_event), NULL); |
| 4788 | #else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4789 | gtk_signal_connect(GTK_OBJECT(gui.formwin), "configure_event", |
| 4790 | GTK_SIGNAL_FUNC(form_configure_event), NULL); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 4791 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4792 | |
| 4793 | #ifdef FEAT_DND |
Bram Moolenaar | a76638f | 2010-06-05 12:49:46 +0200 | [diff] [blame] | 4794 | /* Set up for receiving DND items. */ |
| 4795 | gui_gtk_set_dnd_targets(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4796 | |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 4797 | # if GTK_CHECK_VERSION(3,0,0) |
| 4798 | g_signal_connect(G_OBJECT(gui.drawarea), "drag-data-received", |
| 4799 | G_CALLBACK(drag_data_received_cb), NULL); |
| 4800 | # else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4801 | gtk_signal_connect(GTK_OBJECT(gui.drawarea), "drag_data_received", |
| 4802 | GTK_SIGNAL_FUNC(drag_data_received_cb), NULL); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 4803 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4804 | #endif |
| 4805 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4806 | /* With GTK+ 2, we need to iconify the window before calling show() |
Bram Moolenaar | 182c5be | 2010-06-25 05:37:59 +0200 | [diff] [blame] | 4807 | * to avoid mapping the window for a short time. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4808 | if (found_iconic_arg && gtk_socket_id == 0) |
| 4809 | gui_mch_iconify(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4810 | |
| 4811 | { |
Bram Moolenaar | 182c5be | 2010-06-25 05:37:59 +0200 | [diff] [blame] | 4812 | #if defined(FEAT_GUI_GNOME) && defined(FEAT_MENU) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4813 | unsigned long menu_handler = 0; |
| 4814 | # ifdef FEAT_TOOLBAR |
| 4815 | unsigned long tool_handler = 0; |
| 4816 | # endif |
| 4817 | /* |
| 4818 | * Urgh hackish :/ For some reason BonoboDockLayout always forces a |
| 4819 | * show when restoring the saved layout configuration. We can't just |
| 4820 | * hide the widgets again after gtk_widget_show(gui.mainwin) since it's |
| 4821 | * a toplevel window and thus will be realized immediately. Instead, |
| 4822 | * connect signal handlers to hide the widgets just after they've been |
| 4823 | * marked visible, but before the main window is realized. |
| 4824 | */ |
| 4825 | if (using_gnome && vim_strchr(p_go, GO_MENUS) == NULL) |
| 4826 | menu_handler = g_signal_connect_after(gui.menubar_h, "show", |
| 4827 | G_CALLBACK(>k_widget_hide), |
| 4828 | NULL); |
| 4829 | # ifdef FEAT_TOOLBAR |
| 4830 | if (using_gnome && vim_strchr(p_go, GO_TOOLBAR) == NULL |
| 4831 | && (toolbar_flags & (TOOLBAR_TEXT | TOOLBAR_ICONS))) |
| 4832 | tool_handler = g_signal_connect_after(gui.toolbar_h, "show", |
| 4833 | G_CALLBACK(>k_widget_hide), |
| 4834 | NULL); |
| 4835 | # endif |
| 4836 | #endif |
| 4837 | gtk_widget_show(gui.mainwin); |
| 4838 | |
Bram Moolenaar | 182c5be | 2010-06-25 05:37:59 +0200 | [diff] [blame] | 4839 | #if defined(FEAT_GUI_GNOME) && defined(FEAT_MENU) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4840 | if (menu_handler != 0) |
| 4841 | g_signal_handler_disconnect(gui.menubar_h, menu_handler); |
| 4842 | # ifdef FEAT_TOOLBAR |
| 4843 | if (tool_handler != 0) |
| 4844 | g_signal_handler_disconnect(gui.toolbar_h, tool_handler); |
| 4845 | # endif |
| 4846 | #endif |
| 4847 | } |
| 4848 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4849 | return OK; |
| 4850 | } |
| 4851 | |
| 4852 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4853 | void |
Bram Moolenaar | b85cb21 | 2009-05-17 14:24:23 +0000 | [diff] [blame] | 4854 | gui_mch_exit(int rc UNUSED) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4855 | { |
| 4856 | if (gui.mainwin != NULL) |
| 4857 | gtk_widget_destroy(gui.mainwin); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4858 | } |
| 4859 | |
| 4860 | /* |
| 4861 | * Get the position of the top left corner of the window. |
| 4862 | */ |
| 4863 | int |
| 4864 | gui_mch_get_winpos(int *x, int *y) |
| 4865 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4866 | gtk_window_get_position(GTK_WINDOW(gui.mainwin), x, y); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4867 | return OK; |
| 4868 | } |
| 4869 | |
| 4870 | /* |
| 4871 | * Set the position of the top left corner of the window to the given |
| 4872 | * coordinates. |
| 4873 | */ |
| 4874 | void |
| 4875 | gui_mch_set_winpos(int x, int y) |
| 4876 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4877 | gtk_window_move(GTK_WINDOW(gui.mainwin), x, y); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4878 | } |
| 4879 | |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 4880 | #if !GTK_CHECK_VERSION(3,0,0) |
| 4881 | # if 0 |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4882 | static int resize_idle_installed = FALSE; |
| 4883 | /* |
| 4884 | * Idle handler to force resize. Used by gui_mch_set_shellsize() to ensure |
| 4885 | * the shell size doesn't exceed the window size, i.e. if the window manager |
| 4886 | * ignored our size request. Usually this happens if the window is maximized. |
| 4887 | * |
| 4888 | * FIXME: It'd be nice if we could find a little more orthodox solution. |
| 4889 | * See also the remark below in gui_mch_set_shellsize(). |
| 4890 | * |
| 4891 | * DISABLED: When doing ":set lines+=1" this function would first invoke |
| 4892 | * gui_resize_shell() with the old size, then the normal callback would |
| 4893 | * report the new size through form_configure_event(). That caused the window |
| 4894 | * layout to be messed up. |
| 4895 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4896 | static gboolean |
| 4897 | force_shell_resize_idle(gpointer data) |
| 4898 | { |
| 4899 | if (gui.mainwin != NULL |
| 4900 | && GTK_WIDGET_REALIZED(gui.mainwin) |
| 4901 | && GTK_WIDGET_VISIBLE(gui.mainwin)) |
| 4902 | { |
| 4903 | int width; |
| 4904 | int height; |
| 4905 | |
| 4906 | gtk_window_get_size(GTK_WINDOW(gui.mainwin), &width, &height); |
| 4907 | |
| 4908 | width -= get_menu_tool_width(); |
| 4909 | height -= get_menu_tool_height(); |
| 4910 | |
| 4911 | gui_resize_shell(width, height); |
| 4912 | } |
| 4913 | |
| 4914 | resize_idle_installed = FALSE; |
| 4915 | return FALSE; /* don't call me again */ |
| 4916 | } |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 4917 | # endif |
| 4918 | #endif /* !GTK_CHECK_VERSION(3,0,0) */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4919 | |
Bram Moolenaar | 0973623 | 2009-09-23 16:14:49 +0000 | [diff] [blame] | 4920 | /* |
| 4921 | * Return TRUE if the main window is maximized. |
| 4922 | */ |
| 4923 | int |
Bram Moolenaar | 66f948e | 2016-01-30 16:39:25 +0100 | [diff] [blame] | 4924 | gui_mch_maximized(void) |
Bram Moolenaar | 0973623 | 2009-09-23 16:14:49 +0000 | [diff] [blame] | 4925 | { |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 4926 | #if GTK_CHECK_VERSION(3,0,0) |
| 4927 | return (gui.mainwin != NULL && gtk_widget_get_window(gui.mainwin) != NULL |
| 4928 | && (gdk_window_get_state(gtk_widget_get_window(gui.mainwin)) |
| 4929 | & GDK_WINDOW_STATE_MAXIMIZED)); |
| 4930 | #else |
Bram Moolenaar | 0973623 | 2009-09-23 16:14:49 +0000 | [diff] [blame] | 4931 | return (gui.mainwin != NULL && gui.mainwin->window != NULL |
| 4932 | && (gdk_window_get_state(gui.mainwin->window) |
| 4933 | & GDK_WINDOW_STATE_MAXIMIZED)); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 4934 | #endif |
Bram Moolenaar | 0973623 | 2009-09-23 16:14:49 +0000 | [diff] [blame] | 4935 | } |
| 4936 | |
| 4937 | /* |
| 4938 | * Unmaximize the main window |
| 4939 | */ |
| 4940 | void |
Bram Moolenaar | 66f948e | 2016-01-30 16:39:25 +0100 | [diff] [blame] | 4941 | gui_mch_unmaximize(void) |
Bram Moolenaar | 0973623 | 2009-09-23 16:14:49 +0000 | [diff] [blame] | 4942 | { |
| 4943 | if (gui.mainwin != NULL) |
| 4944 | gtk_window_unmaximize(GTK_WINDOW(gui.mainwin)); |
| 4945 | } |
Bram Moolenaar | 0973623 | 2009-09-23 16:14:49 +0000 | [diff] [blame] | 4946 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4947 | /* |
Bram Moolenaar | 8ac4415 | 2017-11-09 18:33:29 +0100 | [diff] [blame] | 4948 | * Called when the font changed while the window is maximized or GO_KEEPWINSIZE |
| 4949 | * is set. Compute the new Rows and Columns. This is like resizing the |
| 4950 | * window. |
Bram Moolenaar | 12bc1b5 | 2011-08-10 17:44:45 +0200 | [diff] [blame] | 4951 | */ |
| 4952 | void |
Bram Moolenaar | 66f948e | 2016-01-30 16:39:25 +0100 | [diff] [blame] | 4953 | gui_mch_newfont(void) |
Bram Moolenaar | 12bc1b5 | 2011-08-10 17:44:45 +0200 | [diff] [blame] | 4954 | { |
| 4955 | int w, h; |
| 4956 | |
| 4957 | gtk_window_get_size(GTK_WINDOW(gui.mainwin), &w, &h); |
| 4958 | w -= get_menu_tool_width(); |
| 4959 | h -= get_menu_tool_height(); |
| 4960 | gui_resize_shell(w, h); |
| 4961 | } |
| 4962 | |
| 4963 | /* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4964 | * Set the windows size. |
| 4965 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4966 | void |
| 4967 | gui_mch_set_shellsize(int width, int height, |
Bram Moolenaar | b85cb21 | 2009-05-17 14:24:23 +0000 | [diff] [blame] | 4968 | int min_width UNUSED, int min_height UNUSED, |
| 4969 | int base_width UNUSED, int base_height UNUSED, |
| 4970 | int direction UNUSED) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4971 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4972 | /* give GTK+ a chance to put all widget's into place */ |
| 4973 | gui_mch_update(); |
| 4974 | |
Bram Moolenaar | b3656ed | 2006-03-20 21:59:49 +0000 | [diff] [blame] | 4975 | /* this will cause the proper resizement to happen too */ |
| 4976 | if (gtk_socket_id == 0) |
Bram Moolenaar | c9b4b05 | 2006-04-30 18:54:39 +0000 | [diff] [blame] | 4977 | update_window_manager_hints(0, 0); |
Bram Moolenaar | b3656ed | 2006-03-20 21:59:49 +0000 | [diff] [blame] | 4978 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4979 | /* With GTK+ 2, changing the size of the form widget doesn't resize |
Bram Moolenaar | b3656ed | 2006-03-20 21:59:49 +0000 | [diff] [blame] | 4980 | * the window. So let's do it the other way around and resize the |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4981 | * main window instead. */ |
| 4982 | width += get_menu_tool_width(); |
| 4983 | height += get_menu_tool_height(); |
| 4984 | |
Bram Moolenaar | b3656ed | 2006-03-20 21:59:49 +0000 | [diff] [blame] | 4985 | if (gtk_socket_id == 0) |
Bram Moolenaar | c9b4b05 | 2006-04-30 18:54:39 +0000 | [diff] [blame] | 4986 | gtk_window_resize(GTK_WINDOW(gui.mainwin), width, height); |
Bram Moolenaar | b3656ed | 2006-03-20 21:59:49 +0000 | [diff] [blame] | 4987 | else |
Bram Moolenaar | c9b4b05 | 2006-04-30 18:54:39 +0000 | [diff] [blame] | 4988 | update_window_manager_hints(width, height); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4989 | |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 4990 | # if !GTK_CHECK_VERSION(3,0,0) |
| 4991 | # if 0 |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4992 | if (!resize_idle_installed) |
| 4993 | { |
| 4994 | g_idle_add_full(GDK_PRIORITY_EVENTS + 10, |
| 4995 | &force_shell_resize_idle, NULL, NULL); |
| 4996 | resize_idle_installed = TRUE; |
| 4997 | } |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 4998 | # endif |
| 4999 | # endif /* !GTK_CHECK_VERSION(3,0,0) */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5000 | /* |
| 5001 | * Wait until all events are processed to prevent a crash because the |
| 5002 | * real size of the drawing area doesn't reflect Vim's internal ideas. |
| 5003 | * |
| 5004 | * This is a bit of a hack, since Vim is a terminal application with a GUI |
| 5005 | * on top, while the GUI expects to be the boss. |
| 5006 | */ |
| 5007 | gui_mch_update(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5008 | } |
| 5009 | |
Bram Moolenaar | 7be9b50 | 2017-09-09 18:45:26 +0200 | [diff] [blame] | 5010 | void |
Bram Moolenaar | 8696bba | 2017-09-09 23:00:56 +0200 | [diff] [blame] | 5011 | gui_gtk_get_screen_size_of_win(GtkWidget *wid, int *width, int *height) |
Bram Moolenaar | 7be9b50 | 2017-09-09 18:45:26 +0200 | [diff] [blame] | 5012 | { |
| 5013 | #if GTK_CHECK_VERSION(3,22,0) |
Bram Moolenaar | 8696bba | 2017-09-09 23:00:56 +0200 | [diff] [blame] | 5014 | GdkDisplay *dpy = gtk_widget_get_display(wid); |
| 5015 | GdkWindow *win = gtk_widget_get_window(wid); |
Bram Moolenaar | 7be9b50 | 2017-09-09 18:45:26 +0200 | [diff] [blame] | 5016 | GdkMonitor *monitor = gdk_display_get_monitor_at_window(dpy, win); |
| 5017 | GdkRectangle geometry; |
| 5018 | |
| 5019 | gdk_monitor_get_geometry(monitor, &geometry); |
| 5020 | *width = geometry.width; |
| 5021 | *height = geometry.height; |
| 5022 | #else |
| 5023 | GdkScreen* screen; |
| 5024 | |
Bram Moolenaar | 8696bba | 2017-09-09 23:00:56 +0200 | [diff] [blame] | 5025 | if (wid != NULL && gtk_widget_has_screen(wid)) |
| 5026 | screen = gtk_widget_get_screen(wid); |
Bram Moolenaar | 7be9b50 | 2017-09-09 18:45:26 +0200 | [diff] [blame] | 5027 | else |
| 5028 | screen = gdk_screen_get_default(); |
| 5029 | *width = gdk_screen_get_width(screen); |
| 5030 | *height = gdk_screen_get_height(screen); |
| 5031 | #endif |
| 5032 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5033 | |
| 5034 | /* |
| 5035 | * The screen size is used to make sure the initial window doesn't get bigger |
| 5036 | * than the screen. This subtracts some room for menubar, toolbar and window |
| 5037 | * decorations. |
| 5038 | */ |
| 5039 | void |
| 5040 | gui_mch_get_screen_dimensions(int *screen_w, int *screen_h) |
| 5041 | { |
Bram Moolenaar | 7be9b50 | 2017-09-09 18:45:26 +0200 | [diff] [blame] | 5042 | gui_gtk_get_screen_size_of_win(gui.mainwin, screen_w, screen_h); |
Bram Moolenaar | a859f04 | 2016-11-17 19:11:55 +0100 | [diff] [blame] | 5043 | |
Bram Moolenaar | b463e8d | 2017-06-05 15:07:09 +0200 | [diff] [blame] | 5044 | /* Subtract 'guiheadroom' from the height to allow some room for the |
| 5045 | * window manager (task list and window title bar). */ |
Bram Moolenaar | 7be9b50 | 2017-09-09 18:45:26 +0200 | [diff] [blame] | 5046 | *screen_h -= p_ghr; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5047 | |
| 5048 | /* |
| 5049 | * FIXME: dirty trick: Because the gui_get_base_height() doesn't include |
| 5050 | * the toolbar and menubar for GTK, we subtract them from the screen |
Bram Moolenaar | 6440447 | 2010-06-26 06:24:45 +0200 | [diff] [blame] | 5051 | * height, so that the window size can be made to fit on the screen. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5052 | * This should be completely changed later. |
| 5053 | */ |
| 5054 | *screen_w -= get_menu_tool_width(); |
| 5055 | *screen_h -= get_menu_tool_height(); |
| 5056 | } |
| 5057 | |
| 5058 | #if defined(FEAT_TITLE) || defined(PROTO) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5059 | void |
Bram Moolenaar | b85cb21 | 2009-05-17 14:24:23 +0000 | [diff] [blame] | 5060 | gui_mch_settitle(char_u *title, char_u *icon UNUSED) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5061 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5062 | if (title != NULL && output_conv.vc_type != CONV_NONE) |
| 5063 | title = string_convert(&output_conv, title, NULL); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5064 | |
| 5065 | gtk_window_set_title(GTK_WINDOW(gui.mainwin), (const char *)title); |
| 5066 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5067 | if (output_conv.vc_type != CONV_NONE) |
| 5068 | vim_free(title); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5069 | } |
| 5070 | #endif /* FEAT_TITLE */ |
| 5071 | |
| 5072 | #if defined(FEAT_MENU) || defined(PROTO) |
| 5073 | void |
| 5074 | gui_mch_enable_menu(int showit) |
| 5075 | { |
| 5076 | GtkWidget *widget; |
| 5077 | |
| 5078 | # ifdef FEAT_GUI_GNOME |
| 5079 | if (using_gnome) |
| 5080 | widget = gui.menubar_h; |
| 5081 | else |
| 5082 | # endif |
| 5083 | widget = gui.menubar; |
| 5084 | |
Bram Moolenaar | 18144c8 | 2006-04-12 21:52:12 +0000 | [diff] [blame] | 5085 | /* 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] | 5086 | # if GTK_CHECK_VERSION(3,0,0) |
| 5087 | if (!showit != !gtk_widget_get_visible(widget) && !gui.starting) |
| 5088 | # else |
Bram Moolenaar | 18144c8 | 2006-04-12 21:52:12 +0000 | [diff] [blame] | 5089 | if (!showit != !GTK_WIDGET_VISIBLE(widget) && !gui.starting) |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 5090 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5091 | { |
| 5092 | if (showit) |
| 5093 | gtk_widget_show(widget); |
| 5094 | else |
| 5095 | gtk_widget_hide(widget); |
| 5096 | |
Bram Moolenaar | b3656ed | 2006-03-20 21:59:49 +0000 | [diff] [blame] | 5097 | update_window_manager_hints(0, 0); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5098 | } |
| 5099 | } |
| 5100 | #endif /* FEAT_MENU */ |
| 5101 | |
| 5102 | #if defined(FEAT_TOOLBAR) || defined(PROTO) |
| 5103 | void |
| 5104 | gui_mch_show_toolbar(int showit) |
| 5105 | { |
| 5106 | GtkWidget *widget; |
| 5107 | |
| 5108 | if (gui.toolbar == NULL) |
| 5109 | return; |
| 5110 | |
| 5111 | # ifdef FEAT_GUI_GNOME |
| 5112 | if (using_gnome) |
| 5113 | widget = gui.toolbar_h; |
| 5114 | else |
| 5115 | # endif |
| 5116 | widget = gui.toolbar; |
| 5117 | |
| 5118 | if (showit) |
| 5119 | set_toolbar_style(GTK_TOOLBAR(gui.toolbar)); |
| 5120 | |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 5121 | # if GTK_CHECK_VERSION(3,0,0) |
| 5122 | if (!showit != !gtk_widget_get_visible(widget)) |
| 5123 | # else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5124 | if (!showit != !GTK_WIDGET_VISIBLE(widget)) |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 5125 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5126 | { |
| 5127 | if (showit) |
| 5128 | gtk_widget_show(widget); |
| 5129 | else |
| 5130 | gtk_widget_hide(widget); |
| 5131 | |
Bram Moolenaar | b3656ed | 2006-03-20 21:59:49 +0000 | [diff] [blame] | 5132 | update_window_manager_hints(0, 0); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5133 | } |
| 5134 | } |
| 5135 | #endif /* FEAT_TOOLBAR */ |
| 5136 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5137 | /* |
| 5138 | * Check if a given font is a CJK font. This is done in a very crude manner. It |
| 5139 | * just see if U+04E00 for zh and ja and U+AC00 for ko are covered in a given |
| 5140 | * font. Consequently, this function cannot be used as a general purpose check |
| 5141 | * for CJK-ness for which fontconfig APIs should be used. This is only used by |
| 5142 | * gui_mch_init_font() to deal with 'CJK fixed width fonts'. |
| 5143 | */ |
| 5144 | static int |
| 5145 | is_cjk_font(PangoFontDescription *font_desc) |
| 5146 | { |
| 5147 | static const char * const cjk_langs[] = |
| 5148 | {"zh_CN", "zh_TW", "zh_HK", "ja", "ko"}; |
| 5149 | |
| 5150 | PangoFont *font; |
| 5151 | unsigned i; |
| 5152 | int is_cjk = FALSE; |
| 5153 | |
| 5154 | font = pango_context_load_font(gui.text_context, font_desc); |
| 5155 | |
| 5156 | if (font == NULL) |
| 5157 | return FALSE; |
| 5158 | |
| 5159 | for (i = 0; !is_cjk && i < G_N_ELEMENTS(cjk_langs); ++i) |
| 5160 | { |
| 5161 | PangoCoverage *coverage; |
| 5162 | gunichar uc; |
| 5163 | |
| 5164 | coverage = pango_font_get_coverage( |
| 5165 | font, pango_language_from_string(cjk_langs[i])); |
| 5166 | |
| 5167 | if (coverage != NULL) |
| 5168 | { |
| 5169 | uc = (cjk_langs[i][0] == 'k') ? 0xAC00 : 0x4E00; |
| 5170 | is_cjk = (pango_coverage_get(coverage, uc) == PANGO_COVERAGE_EXACT); |
| 5171 | pango_coverage_unref(coverage); |
| 5172 | } |
| 5173 | } |
| 5174 | |
| 5175 | g_object_unref(font); |
| 5176 | |
| 5177 | return is_cjk; |
| 5178 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5179 | |
Bram Moolenaar | 231334e | 2005-07-25 20:46:57 +0000 | [diff] [blame] | 5180 | /* |
| 5181 | * Adjust gui.char_height (after 'linespace' was changed). |
| 5182 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5183 | int |
Bram Moolenaar | 231334e | 2005-07-25 20:46:57 +0000 | [diff] [blame] | 5184 | gui_mch_adjust_charheight(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5185 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5186 | PangoFontMetrics *metrics; |
| 5187 | int ascent; |
| 5188 | int descent; |
| 5189 | |
| 5190 | metrics = pango_context_get_metrics(gui.text_context, gui.norm_font, |
| 5191 | pango_context_get_language(gui.text_context)); |
| 5192 | ascent = pango_font_metrics_get_ascent(metrics); |
| 5193 | descent = pango_font_metrics_get_descent(metrics); |
| 5194 | |
| 5195 | pango_font_metrics_unref(metrics); |
| 5196 | |
| 5197 | gui.char_height = (ascent + descent + PANGO_SCALE - 1) / PANGO_SCALE |
Bram Moolenaar | 231334e | 2005-07-25 20:46:57 +0000 | [diff] [blame] | 5198 | + p_linespace; |
Bram Moolenaar | b71ec9f | 2005-01-25 22:22:02 +0000 | [diff] [blame] | 5199 | /* LINTED: avoid warning: bitwise operation on signed value */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5200 | gui.char_ascent = PANGO_PIXELS(ascent + p_linespace * PANGO_SCALE / 2); |
| 5201 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5202 | /* A not-positive value of char_height may crash Vim. Only happens |
| 5203 | * if 'linespace' is negative (which does make sense sometimes). */ |
| 5204 | gui.char_ascent = MAX(gui.char_ascent, 0); |
| 5205 | gui.char_height = MAX(gui.char_height, gui.char_ascent + 1); |
| 5206 | |
| 5207 | return OK; |
| 5208 | } |
| 5209 | |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 5210 | #if GTK_CHECK_VERSION(3,0,0) |
| 5211 | /* Callback function used in gui_mch_font_dialog() */ |
| 5212 | static gboolean |
| 5213 | font_filter(const PangoFontFamily *family, |
| 5214 | const PangoFontFace *face UNUSED, |
| 5215 | gpointer data UNUSED) |
| 5216 | { |
| 5217 | return pango_font_family_is_monospace((PangoFontFamily *)family); |
| 5218 | } |
| 5219 | #endif |
| 5220 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5221 | /* |
| 5222 | * Put up a font dialog and return the selected font name in allocated memory. |
| 5223 | * "oldval" is the previous value. Return NULL when cancelled. |
| 5224 | * This should probably go into gui_gtk.c. Hmm. |
| 5225 | * FIXME: |
| 5226 | * The GTK2 font selection dialog has no filtering API. So we could either |
| 5227 | * a) implement our own (possibly copying the code from somewhere else) or |
| 5228 | * b) just live with it. |
| 5229 | */ |
| 5230 | char_u * |
| 5231 | gui_mch_font_dialog(char_u *oldval) |
| 5232 | { |
| 5233 | GtkWidget *dialog; |
| 5234 | int response; |
| 5235 | char_u *fontname = NULL; |
| 5236 | char_u *oldname; |
| 5237 | |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 5238 | #if GTK_CHECK_VERSION(3,2,0) |
| 5239 | dialog = gtk_font_chooser_dialog_new(NULL, NULL); |
| 5240 | gtk_font_chooser_set_filter_func(GTK_FONT_CHOOSER(dialog), font_filter, |
| 5241 | NULL, NULL); |
| 5242 | #else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5243 | dialog = gtk_font_selection_dialog_new(NULL); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 5244 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5245 | |
| 5246 | gtk_window_set_transient_for(GTK_WINDOW(dialog), GTK_WINDOW(gui.mainwin)); |
| 5247 | gtk_window_set_destroy_with_parent(GTK_WINDOW(dialog), TRUE); |
| 5248 | |
| 5249 | if (oldval != NULL && oldval[0] != NUL) |
| 5250 | { |
| 5251 | if (output_conv.vc_type != CONV_NONE) |
| 5252 | oldname = string_convert(&output_conv, oldval, NULL); |
| 5253 | else |
| 5254 | oldname = oldval; |
| 5255 | |
| 5256 | /* Annoying bug in GTK (or Pango): if the font name does not include a |
| 5257 | * size, zero is used. Use default point size ten. */ |
| 5258 | if (!vim_isdigit(oldname[STRLEN(oldname) - 1])) |
| 5259 | { |
| 5260 | char_u *p = vim_strnsave(oldname, STRLEN(oldname) + 3); |
| 5261 | |
| 5262 | if (p != NULL) |
| 5263 | { |
| 5264 | STRCPY(p + STRLEN(p), " 10"); |
| 5265 | if (oldname != oldval) |
| 5266 | vim_free(oldname); |
| 5267 | oldname = p; |
| 5268 | } |
| 5269 | } |
| 5270 | |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 5271 | #if GTK_CHECK_VERSION(3,2,0) |
| 5272 | gtk_font_chooser_set_font( |
| 5273 | GTK_FONT_CHOOSER(dialog), (const gchar *)oldname); |
| 5274 | #else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5275 | gtk_font_selection_dialog_set_font_name( |
| 5276 | GTK_FONT_SELECTION_DIALOG(dialog), (const char *)oldname); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 5277 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5278 | |
| 5279 | if (oldname != oldval) |
Bram Moolenaar | 8c71145 | 2005-01-14 21:53:12 +0000 | [diff] [blame] | 5280 | vim_free(oldname); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5281 | } |
Bram Moolenaar | bef9d83 | 2009-09-11 13:46:41 +0000 | [diff] [blame] | 5282 | else |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 5283 | #if GTK_CHECK_VERSION(3,2,0) |
| 5284 | gtk_font_chooser_set_font( |
| 5285 | GTK_FONT_CHOOSER(dialog), DEFAULT_FONT); |
| 5286 | #else |
Bram Moolenaar | bef9d83 | 2009-09-11 13:46:41 +0000 | [diff] [blame] | 5287 | gtk_font_selection_dialog_set_font_name( |
| 5288 | GTK_FONT_SELECTION_DIALOG(dialog), DEFAULT_FONT); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 5289 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5290 | |
| 5291 | response = gtk_dialog_run(GTK_DIALOG(dialog)); |
| 5292 | |
| 5293 | if (response == GTK_RESPONSE_OK) |
| 5294 | { |
| 5295 | char *name; |
| 5296 | |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 5297 | #if GTK_CHECK_VERSION(3,2,0) |
| 5298 | name = gtk_font_chooser_get_font(GTK_FONT_CHOOSER(dialog)); |
| 5299 | #else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5300 | name = gtk_font_selection_dialog_get_font_name( |
| 5301 | GTK_FONT_SELECTION_DIALOG(dialog)); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 5302 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5303 | if (name != NULL) |
| 5304 | { |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 5305 | char_u *p; |
| 5306 | |
| 5307 | /* Apparently some font names include a comma, need to escape |
| 5308 | * that, because in 'guifont' it separates names. */ |
| 5309 | p = vim_strsave_escaped((char_u *)name, (char_u *)","); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5310 | g_free(name); |
Bram Moolenaar | eb3593b | 2006-04-22 22:33:57 +0000 | [diff] [blame] | 5311 | if (p != NULL && input_conv.vc_type != CONV_NONE) |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 5312 | { |
| 5313 | fontname = string_convert(&input_conv, p, NULL); |
| 5314 | vim_free(p); |
| 5315 | } |
| 5316 | else |
| 5317 | fontname = p; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5318 | } |
| 5319 | } |
| 5320 | |
| 5321 | if (response != GTK_RESPONSE_NONE) |
| 5322 | gtk_widget_destroy(dialog); |
| 5323 | |
| 5324 | return fontname; |
| 5325 | } |
| 5326 | |
| 5327 | /* |
| 5328 | * Some monospace fonts don't support a bold weight, and fall back |
| 5329 | * silently to the regular weight. But this is no good since our text |
| 5330 | * drawing function can emulate bold by overstriking. So let's try |
| 5331 | * to detect whether bold weight is actually available and emulate it |
| 5332 | * otherwise. |
| 5333 | * |
| 5334 | * Note that we don't need to check for italic style since Xft can |
| 5335 | * emulate italic on its own, provided you have a proper fontconfig |
| 5336 | * setup. We wouldn't be able to emulate it in Vim anyway. |
| 5337 | */ |
| 5338 | static void |
| 5339 | get_styled_font_variants(void) |
| 5340 | { |
| 5341 | PangoFontDescription *bold_font_desc; |
| 5342 | PangoFont *plain_font; |
| 5343 | PangoFont *bold_font; |
| 5344 | |
| 5345 | gui.font_can_bold = FALSE; |
| 5346 | |
| 5347 | plain_font = pango_context_load_font(gui.text_context, gui.norm_font); |
| 5348 | |
| 5349 | if (plain_font == NULL) |
| 5350 | return; |
| 5351 | |
| 5352 | bold_font_desc = pango_font_description_copy_static(gui.norm_font); |
| 5353 | pango_font_description_set_weight(bold_font_desc, PANGO_WEIGHT_BOLD); |
| 5354 | |
| 5355 | bold_font = pango_context_load_font(gui.text_context, bold_font_desc); |
| 5356 | /* |
| 5357 | * The comparison relies on the unique handle nature of a PangoFont*, |
| 5358 | * i.e. it's assumed that a different PangoFont* won't refer to the |
| 5359 | * same font. Seems to work, and failing here isn't critical anyway. |
| 5360 | */ |
| 5361 | if (bold_font != NULL) |
| 5362 | { |
| 5363 | gui.font_can_bold = (bold_font != plain_font); |
| 5364 | g_object_unref(bold_font); |
| 5365 | } |
| 5366 | |
| 5367 | pango_font_description_free(bold_font_desc); |
| 5368 | g_object_unref(plain_font); |
| 5369 | } |
| 5370 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5371 | static PangoEngineShape *default_shape_engine = NULL; |
| 5372 | |
| 5373 | /* |
| 5374 | * Create a map from ASCII characters in the range [32,126] to glyphs |
| 5375 | * of the current font. This is used by gui_gtk2_draw_string() to skip |
| 5376 | * the itemize and shaping process for the most common case. |
| 5377 | */ |
| 5378 | static void |
| 5379 | ascii_glyph_table_init(void) |
| 5380 | { |
Bram Moolenaar | 16350cb | 2016-08-14 20:27:34 +0200 | [diff] [blame] | 5381 | char_u ascii_chars[2 * 128]; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5382 | PangoAttrList *attr_list; |
| 5383 | GList *item_list; |
| 5384 | int i; |
| 5385 | |
| 5386 | if (gui.ascii_glyphs != NULL) |
| 5387 | pango_glyph_string_free(gui.ascii_glyphs); |
| 5388 | if (gui.ascii_font != NULL) |
| 5389 | g_object_unref(gui.ascii_font); |
| 5390 | |
| 5391 | gui.ascii_glyphs = NULL; |
| 5392 | gui.ascii_font = NULL; |
| 5393 | |
Bram Moolenaar | 16350cb | 2016-08-14 20:27:34 +0200 | [diff] [blame] | 5394 | /* For safety, fill in question marks for the control characters. |
| 5395 | * Put a space between characters to avoid shaping. */ |
| 5396 | for (i = 0; i < 128; ++i) |
| 5397 | { |
| 5398 | if (i >= 32 && i < 127) |
| 5399 | ascii_chars[2 * i] = i; |
| 5400 | else |
| 5401 | ascii_chars[2 * i] = '?'; |
| 5402 | ascii_chars[2 * i + 1] = ' '; |
| 5403 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5404 | |
| 5405 | attr_list = pango_attr_list_new(); |
| 5406 | item_list = pango_itemize(gui.text_context, (const char *)ascii_chars, |
| 5407 | 0, sizeof(ascii_chars), attr_list, NULL); |
| 5408 | |
| 5409 | if (item_list != NULL && item_list->next == NULL) /* play safe */ |
| 5410 | { |
| 5411 | PangoItem *item; |
| 5412 | int width; |
| 5413 | |
| 5414 | item = (PangoItem *)item_list->data; |
| 5415 | width = gui.char_width * PANGO_SCALE; |
| 5416 | |
| 5417 | /* Remember the shape engine used for ASCII. */ |
| 5418 | default_shape_engine = item->analysis.shape_engine; |
| 5419 | |
| 5420 | gui.ascii_font = item->analysis.font; |
| 5421 | g_object_ref(gui.ascii_font); |
| 5422 | |
| 5423 | gui.ascii_glyphs = pango_glyph_string_new(); |
| 5424 | |
| 5425 | pango_shape((const char *)ascii_chars, sizeof(ascii_chars), |
| 5426 | &item->analysis, gui.ascii_glyphs); |
| 5427 | |
| 5428 | g_return_if_fail(gui.ascii_glyphs->num_glyphs == sizeof(ascii_chars)); |
| 5429 | |
| 5430 | for (i = 0; i < gui.ascii_glyphs->num_glyphs; ++i) |
| 5431 | { |
| 5432 | PangoGlyphGeometry *geom; |
| 5433 | |
| 5434 | geom = &gui.ascii_glyphs->glyphs[i].geometry; |
| 5435 | geom->x_offset += MAX(0, width - geom->width) / 2; |
| 5436 | geom->width = width; |
| 5437 | } |
| 5438 | } |
| 5439 | |
| 5440 | g_list_foreach(item_list, (GFunc)&pango_item_free, NULL); |
| 5441 | g_list_free(item_list); |
| 5442 | pango_attr_list_unref(attr_list); |
| 5443 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5444 | |
| 5445 | /* |
| 5446 | * Initialize Vim to use the font or fontset with the given name. |
| 5447 | * Return FAIL if the font could not be loaded, OK otherwise. |
| 5448 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5449 | int |
Bram Moolenaar | b85cb21 | 2009-05-17 14:24:23 +0000 | [diff] [blame] | 5450 | gui_mch_init_font(char_u *font_name, int fontset UNUSED) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5451 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5452 | PangoFontDescription *font_desc; |
| 5453 | PangoLayout *layout; |
| 5454 | int width; |
| 5455 | |
| 5456 | /* If font_name is NULL, this means to use the default, which should |
| 5457 | * be present on all proper Pango/fontconfig installations. */ |
| 5458 | if (font_name == NULL) |
| 5459 | font_name = (char_u *)DEFAULT_FONT; |
| 5460 | |
| 5461 | font_desc = gui_mch_get_font(font_name, FALSE); |
| 5462 | |
| 5463 | if (font_desc == NULL) |
| 5464 | return FAIL; |
| 5465 | |
| 5466 | gui_mch_free_font(gui.norm_font); |
| 5467 | gui.norm_font = font_desc; |
| 5468 | |
| 5469 | pango_context_set_font_description(gui.text_context, font_desc); |
| 5470 | |
| 5471 | layout = pango_layout_new(gui.text_context); |
| 5472 | pango_layout_set_text(layout, "MW", 2); |
| 5473 | pango_layout_get_size(layout, &width, NULL); |
| 5474 | /* |
| 5475 | * Set char_width to half the width obtained from pango_layout_get_size() |
| 5476 | * for CJK fixed_width/bi-width fonts. An unpatched version of Xft leads |
| 5477 | * Pango to use the same width for both non-CJK characters (e.g. Latin |
| 5478 | * letters and numbers) and CJK characters. This results in 's p a c e d |
| 5479 | * o u t' rendering when a CJK 'fixed width' font is used. To work around |
| 5480 | * that, divide the width returned by Pango by 2 if cjk_width is equal to |
| 5481 | * width for CJK fonts. |
| 5482 | * |
| 5483 | * For related bugs, see: |
| 5484 | * http://bugzilla.gnome.org/show_bug.cgi?id=106618 |
| 5485 | * http://bugzilla.gnome.org/show_bug.cgi?id=106624 |
| 5486 | * |
| 5487 | * With this, for all four of the following cases, Vim works fine: |
| 5488 | * guifont=CJK_fixed_width_font |
| 5489 | * guifont=Non_CJK_fixed_font |
| 5490 | * guifont=Non_CJK_fixed_font,CJK_Fixed_font |
| 5491 | * guifont=Non_CJK_fixed_font guifontwide=CJK_fixed_font |
| 5492 | */ |
| 5493 | if (is_cjk_font(gui.norm_font)) |
| 5494 | { |
| 5495 | int cjk_width; |
| 5496 | |
| 5497 | /* Measure the text extent of U+4E00 and U+4E8C */ |
| 5498 | pango_layout_set_text(layout, "\344\270\200\344\272\214", -1); |
| 5499 | pango_layout_get_size(layout, &cjk_width, NULL); |
| 5500 | |
| 5501 | if (width == cjk_width) /* Xft not patched */ |
| 5502 | width /= 2; |
| 5503 | } |
| 5504 | g_object_unref(layout); |
| 5505 | |
| 5506 | gui.char_width = (width / 2 + PANGO_SCALE - 1) / PANGO_SCALE; |
| 5507 | |
| 5508 | /* A zero width may cause a crash. Happens for semi-invalid fontsets. */ |
| 5509 | if (gui.char_width <= 0) |
| 5510 | gui.char_width = 8; |
| 5511 | |
Bram Moolenaar | 231334e | 2005-07-25 20:46:57 +0000 | [diff] [blame] | 5512 | gui_mch_adjust_charheight(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5513 | |
| 5514 | /* Set the fontname, which will be used for information purposes */ |
| 5515 | hl_set_font_name(font_name); |
| 5516 | |
| 5517 | get_styled_font_variants(); |
| 5518 | ascii_glyph_table_init(); |
| 5519 | |
| 5520 | /* Avoid unnecessary overhead if 'guifontwide' is equal to 'guifont'. */ |
| 5521 | if (gui.wide_font != NULL |
| 5522 | && pango_font_description_equal(gui.norm_font, gui.wide_font)) |
| 5523 | { |
| 5524 | pango_font_description_free(gui.wide_font); |
| 5525 | gui.wide_font = NULL; |
| 5526 | } |
| 5527 | |
Bram Moolenaar | e161c79 | 2009-11-03 17:13:59 +0000 | [diff] [blame] | 5528 | if (gui_mch_maximized()) |
| 5529 | { |
Bram Moolenaar | e161c79 | 2009-11-03 17:13:59 +0000 | [diff] [blame] | 5530 | /* Update lines and columns in accordance with the new font, keep the |
| 5531 | * window maximized. */ |
Bram Moolenaar | 12bc1b5 | 2011-08-10 17:44:45 +0200 | [diff] [blame] | 5532 | gui_mch_newfont(); |
Bram Moolenaar | e161c79 | 2009-11-03 17:13:59 +0000 | [diff] [blame] | 5533 | } |
| 5534 | else |
Bram Moolenaar | e161c79 | 2009-11-03 17:13:59 +0000 | [diff] [blame] | 5535 | { |
| 5536 | /* Preserve the logical dimensions of the screen. */ |
| 5537 | update_window_manager_hints(0, 0); |
| 5538 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5539 | |
| 5540 | return OK; |
| 5541 | } |
| 5542 | |
| 5543 | /* |
| 5544 | * Get a reference to the font "name". |
| 5545 | * Return zero for failure. |
| 5546 | */ |
| 5547 | GuiFont |
| 5548 | gui_mch_get_font(char_u *name, int report_error) |
| 5549 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5550 | PangoFontDescription *font; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5551 | |
| 5552 | /* can't do this when GUI is not running */ |
| 5553 | if (!gui.in_use || name == NULL) |
| 5554 | return NULL; |
| 5555 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5556 | if (output_conv.vc_type != CONV_NONE) |
| 5557 | { |
| 5558 | char_u *buf; |
| 5559 | |
| 5560 | buf = string_convert(&output_conv, name, NULL); |
| 5561 | if (buf != NULL) |
| 5562 | { |
| 5563 | font = pango_font_description_from_string((const char *)buf); |
| 5564 | vim_free(buf); |
| 5565 | } |
| 5566 | else |
| 5567 | font = NULL; |
| 5568 | } |
| 5569 | else |
| 5570 | font = pango_font_description_from_string((const char *)name); |
| 5571 | |
| 5572 | if (font != NULL) |
| 5573 | { |
| 5574 | PangoFont *real_font; |
| 5575 | |
| 5576 | /* pango_context_load_font() bails out if no font size is set */ |
| 5577 | if (pango_font_description_get_size(font) <= 0) |
| 5578 | pango_font_description_set_size(font, 10 * PANGO_SCALE); |
| 5579 | |
| 5580 | real_font = pango_context_load_font(gui.text_context, font); |
| 5581 | |
| 5582 | if (real_font == NULL) |
| 5583 | { |
| 5584 | pango_font_description_free(font); |
| 5585 | font = NULL; |
| 5586 | } |
| 5587 | else |
| 5588 | g_object_unref(real_font); |
| 5589 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5590 | |
| 5591 | if (font == NULL) |
| 5592 | { |
| 5593 | if (report_error) |
Bram Moolenaar | c93e791 | 2008-07-08 10:46:08 +0000 | [diff] [blame] | 5594 | EMSG2(_((char *)e_font), name); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5595 | return NULL; |
| 5596 | } |
| 5597 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5598 | return font; |
| 5599 | } |
| 5600 | |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 5601 | #if defined(FEAT_EVAL) || defined(PROTO) |
Bram Moolenaar | 46c9c73 | 2004-12-12 11:37:09 +0000 | [diff] [blame] | 5602 | /* |
| 5603 | * Return the name of font "font" in allocated memory. |
| 5604 | */ |
Bram Moolenaar | 46c9c73 | 2004-12-12 11:37:09 +0000 | [diff] [blame] | 5605 | char_u * |
Bram Moolenaar | b85cb21 | 2009-05-17 14:24:23 +0000 | [diff] [blame] | 5606 | gui_mch_get_fontname(GuiFont font, char_u *name UNUSED) |
Bram Moolenaar | 46c9c73 | 2004-12-12 11:37:09 +0000 | [diff] [blame] | 5607 | { |
Bram Moolenaar | 46c9c73 | 2004-12-12 11:37:09 +0000 | [diff] [blame] | 5608 | if (font != NOFONT) |
| 5609 | { |
Bram Moolenaar | 89d4032 | 2006-08-29 15:30:07 +0000 | [diff] [blame] | 5610 | char *pangoname = pango_font_description_to_string(font); |
Bram Moolenaar | 46c9c73 | 2004-12-12 11:37:09 +0000 | [diff] [blame] | 5611 | |
Bram Moolenaar | 89d4032 | 2006-08-29 15:30:07 +0000 | [diff] [blame] | 5612 | if (pangoname != NULL) |
Bram Moolenaar | 46c9c73 | 2004-12-12 11:37:09 +0000 | [diff] [blame] | 5613 | { |
Bram Moolenaar | 89d4032 | 2006-08-29 15:30:07 +0000 | [diff] [blame] | 5614 | char_u *s = vim_strsave((char_u *)pangoname); |
Bram Moolenaar | 46c9c73 | 2004-12-12 11:37:09 +0000 | [diff] [blame] | 5615 | |
Bram Moolenaar | 89d4032 | 2006-08-29 15:30:07 +0000 | [diff] [blame] | 5616 | g_free(pangoname); |
Bram Moolenaar | 46c9c73 | 2004-12-12 11:37:09 +0000 | [diff] [blame] | 5617 | return s; |
| 5618 | } |
| 5619 | } |
Bram Moolenaar | 46c9c73 | 2004-12-12 11:37:09 +0000 | [diff] [blame] | 5620 | return NULL; |
| 5621 | } |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 5622 | #endif |
Bram Moolenaar | 46c9c73 | 2004-12-12 11:37:09 +0000 | [diff] [blame] | 5623 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5624 | /* |
| 5625 | * If a font is not going to be used, free its structure. |
| 5626 | */ |
| 5627 | void |
| 5628 | gui_mch_free_font(GuiFont font) |
| 5629 | { |
| 5630 | if (font != NOFONT) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5631 | pango_font_description_free(font); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5632 | } |
| 5633 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5634 | /* |
Bram Moolenaar | 36edf06 | 2016-07-21 22:10:12 +0200 | [diff] [blame] | 5635 | * Return the Pixel value (color) for the given color name. |
| 5636 | * |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5637 | * Return INVALCOLOR for error. |
| 5638 | */ |
| 5639 | guicolor_T |
| 5640 | gui_mch_get_color(char_u *name) |
| 5641 | { |
Bram Moolenaar | 2e32495 | 2018-04-14 14:37:07 +0200 | [diff] [blame] | 5642 | guicolor_T color = INVALCOLOR; |
| 5643 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5644 | if (!gui.in_use) /* can't do this when GUI not running */ |
Bram Moolenaar | 2e32495 | 2018-04-14 14:37:07 +0200 | [diff] [blame] | 5645 | return color; |
| 5646 | |
| 5647 | if (name != NULL) |
| 5648 | color = gui_get_color_cmn(name); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5649 | |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 5650 | #if GTK_CHECK_VERSION(3,0,0) |
Bram Moolenaar | 2e32495 | 2018-04-14 14:37:07 +0200 | [diff] [blame] | 5651 | return color; |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 5652 | #else |
Bram Moolenaar | 36edf06 | 2016-07-21 22:10:12 +0200 | [diff] [blame] | 5653 | if (color == INVALCOLOR) |
| 5654 | return INVALCOLOR; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5655 | |
Bram Moolenaar | 26af85d | 2017-07-23 16:45:10 +0200 | [diff] [blame] | 5656 | return gui_mch_get_rgb_color( |
| 5657 | (color & 0xff0000) >> 16, |
| 5658 | (color & 0xff00) >> 8, |
| 5659 | color & 0xff); |
| 5660 | #endif |
| 5661 | } |
| 5662 | |
| 5663 | /* |
| 5664 | * Return the Pixel value (color) for the given RGB values. |
| 5665 | * Return INVALCOLOR for error. |
| 5666 | */ |
| 5667 | guicolor_T |
| 5668 | gui_mch_get_rgb_color(int r, int g, int b) |
| 5669 | { |
| 5670 | #if GTK_CHECK_VERSION(3,0,0) |
| 5671 | return gui_get_rgb_color_cmn(r, g, b); |
| 5672 | #else |
| 5673 | GdkColor gcolor; |
| 5674 | int ret; |
| 5675 | |
| 5676 | gcolor.red = (guint16)(r / 255.0 * 65535 + 0.5); |
| 5677 | gcolor.green = (guint16)(g / 255.0 * 65535 + 0.5); |
| 5678 | gcolor.blue = (guint16)(b / 255.0 * 65535 + 0.5); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5679 | |
Bram Moolenaar | 36edf06 | 2016-07-21 22:10:12 +0200 | [diff] [blame] | 5680 | ret = gdk_colormap_alloc_color(gtk_widget_get_colormap(gui.drawarea), |
| 5681 | &gcolor, FALSE, TRUE); |
| 5682 | |
| 5683 | return ret != 0 ? (guicolor_T)gcolor.pixel : INVALCOLOR; |
| 5684 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5685 | } |
| 5686 | |
| 5687 | /* |
| 5688 | * Set the current text foreground color. |
| 5689 | */ |
| 5690 | void |
| 5691 | gui_mch_set_fg_color(guicolor_T color) |
| 5692 | { |
Bram Moolenaar | 36edf06 | 2016-07-21 22:10:12 +0200 | [diff] [blame] | 5693 | #if GTK_CHECK_VERSION(3,0,0) |
| 5694 | *gui.fgcolor = color_to_rgba(color); |
| 5695 | #else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5696 | gui.fgcolor->pixel = (unsigned long)color; |
Bram Moolenaar | 36edf06 | 2016-07-21 22:10:12 +0200 | [diff] [blame] | 5697 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5698 | } |
| 5699 | |
| 5700 | /* |
| 5701 | * Set the current text background color. |
| 5702 | */ |
| 5703 | void |
| 5704 | gui_mch_set_bg_color(guicolor_T color) |
| 5705 | { |
Bram Moolenaar | 36edf06 | 2016-07-21 22:10:12 +0200 | [diff] [blame] | 5706 | #if GTK_CHECK_VERSION(3,0,0) |
| 5707 | *gui.bgcolor = color_to_rgba(color); |
| 5708 | #else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5709 | gui.bgcolor->pixel = (unsigned long)color; |
Bram Moolenaar | 36edf06 | 2016-07-21 22:10:12 +0200 | [diff] [blame] | 5710 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5711 | } |
| 5712 | |
Bram Moolenaar | f36d369 | 2005-03-15 22:48:14 +0000 | [diff] [blame] | 5713 | /* |
| 5714 | * Set the current text special color. |
| 5715 | */ |
| 5716 | void |
| 5717 | gui_mch_set_sp_color(guicolor_T color) |
| 5718 | { |
Bram Moolenaar | 36edf06 | 2016-07-21 22:10:12 +0200 | [diff] [blame] | 5719 | #if GTK_CHECK_VERSION(3,0,0) |
| 5720 | *gui.spcolor = color_to_rgba(color); |
| 5721 | #else |
Bram Moolenaar | f36d369 | 2005-03-15 22:48:14 +0000 | [diff] [blame] | 5722 | gui.spcolor->pixel = (unsigned long)color; |
Bram Moolenaar | 36edf06 | 2016-07-21 22:10:12 +0200 | [diff] [blame] | 5723 | #endif |
Bram Moolenaar | f36d369 | 2005-03-15 22:48:14 +0000 | [diff] [blame] | 5724 | } |
| 5725 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5726 | /* |
| 5727 | * Function-like convenience macro for the sake of efficiency. |
| 5728 | */ |
| 5729 | #define INSERT_PANGO_ATTR(Attribute, AttrList, Start, End) \ |
| 5730 | G_STMT_START{ \ |
| 5731 | PangoAttribute *tmp_attr_; \ |
| 5732 | tmp_attr_ = (Attribute); \ |
| 5733 | tmp_attr_->start_index = (Start); \ |
| 5734 | tmp_attr_->end_index = (End); \ |
| 5735 | pango_attr_list_insert((AttrList), tmp_attr_); \ |
| 5736 | }G_STMT_END |
| 5737 | |
| 5738 | static void |
| 5739 | apply_wide_font_attr(char_u *s, int len, PangoAttrList *attr_list) |
| 5740 | { |
| 5741 | char_u *start = NULL; |
| 5742 | char_u *p; |
| 5743 | int uc; |
| 5744 | |
| 5745 | for (p = s; p < s + len; p += utf_byte2len(*p)) |
| 5746 | { |
| 5747 | uc = utf_ptr2char(p); |
| 5748 | |
| 5749 | if (start == NULL) |
| 5750 | { |
| 5751 | if (uc >= 0x80 && utf_char2cells(uc) == 2) |
| 5752 | start = p; |
| 5753 | } |
| 5754 | else if (uc < 0x80 /* optimization shortcut */ |
| 5755 | || (utf_char2cells(uc) != 2 && !utf_iscomposing(uc))) |
| 5756 | { |
| 5757 | INSERT_PANGO_ATTR(pango_attr_font_desc_new(gui.wide_font), |
| 5758 | attr_list, start - s, p - s); |
| 5759 | start = NULL; |
| 5760 | } |
| 5761 | } |
| 5762 | |
| 5763 | if (start != NULL) |
| 5764 | INSERT_PANGO_ATTR(pango_attr_font_desc_new(gui.wide_font), |
| 5765 | attr_list, start - s, len); |
| 5766 | } |
| 5767 | |
| 5768 | static int |
| 5769 | count_cluster_cells(char_u *s, PangoItem *item, |
| 5770 | PangoGlyphString* glyphs, int i, |
| 5771 | int *cluster_width, |
| 5772 | int *last_glyph_rbearing) |
| 5773 | { |
| 5774 | char_u *p; |
| 5775 | int next; /* glyph start index of next cluster */ |
| 5776 | int start, end; /* string segment of current cluster */ |
| 5777 | int width; /* real cluster width in Pango units */ |
| 5778 | int uc; |
| 5779 | int cellcount = 0; |
| 5780 | |
| 5781 | width = glyphs->glyphs[i].geometry.width; |
| 5782 | |
| 5783 | for (next = i + 1; next < glyphs->num_glyphs; ++next) |
| 5784 | { |
| 5785 | if (glyphs->glyphs[next].attr.is_cluster_start) |
| 5786 | break; |
| 5787 | else if (glyphs->glyphs[next].geometry.width > width) |
| 5788 | width = glyphs->glyphs[next].geometry.width; |
| 5789 | } |
| 5790 | |
| 5791 | start = item->offset + glyphs->log_clusters[i]; |
| 5792 | end = item->offset + ((next < glyphs->num_glyphs) ? |
| 5793 | glyphs->log_clusters[next] : item->length); |
| 5794 | |
| 5795 | for (p = s + start; p < s + end; p += utf_byte2len(*p)) |
| 5796 | { |
| 5797 | uc = utf_ptr2char(p); |
| 5798 | if (uc < 0x80) |
| 5799 | ++cellcount; |
| 5800 | else if (!utf_iscomposing(uc)) |
| 5801 | cellcount += utf_char2cells(uc); |
| 5802 | } |
| 5803 | |
| 5804 | if (last_glyph_rbearing != NULL |
| 5805 | && cellcount > 0 && next == glyphs->num_glyphs) |
| 5806 | { |
| 5807 | PangoRectangle ink_rect; |
| 5808 | /* |
| 5809 | * If a certain combining mark had to be taken from a non-monospace |
| 5810 | * font, we have to compensate manually by adapting x_offset according |
| 5811 | * to the ink extents of the previous glyph. |
| 5812 | */ |
| 5813 | pango_font_get_glyph_extents(item->analysis.font, |
| 5814 | glyphs->glyphs[i].glyph, |
| 5815 | &ink_rect, NULL); |
| 5816 | |
| 5817 | if (PANGO_RBEARING(ink_rect) > 0) |
| 5818 | *last_glyph_rbearing = PANGO_RBEARING(ink_rect); |
| 5819 | } |
| 5820 | |
| 5821 | if (cellcount > 0) |
| 5822 | *cluster_width = width; |
| 5823 | |
| 5824 | return cellcount; |
| 5825 | } |
| 5826 | |
| 5827 | /* |
| 5828 | * If there are only combining characters in the cluster, we cannot just |
| 5829 | * change the width of the previous glyph since there is none. Therefore |
| 5830 | * some guesswork is needed. |
| 5831 | * |
| 5832 | * If ink_rect.x is negative Pango apparently has taken care of the composing |
| 5833 | * by itself. Actually setting x_offset = 0 should be sufficient then, but due |
| 5834 | * 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] | 5835 | * x_offset to avoid ugliness. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5836 | * |
| 5837 | * If ink_rect.x is not negative, force overstriking by pointing x_offset to |
| 5838 | * the position of the previous glyph. Apparently this happens only with old |
| 5839 | * X fonts which don't provide the special combining information needed by |
| 5840 | * Pango. |
| 5841 | */ |
| 5842 | static void |
| 5843 | setup_zero_width_cluster(PangoItem *item, PangoGlyphInfo *glyph, |
| 5844 | int last_cellcount, int last_cluster_width, |
| 5845 | int last_glyph_rbearing) |
| 5846 | { |
| 5847 | PangoRectangle ink_rect; |
| 5848 | PangoRectangle logical_rect; |
| 5849 | int width; |
| 5850 | |
| 5851 | width = last_cellcount * gui.char_width * PANGO_SCALE; |
| 5852 | glyph->geometry.x_offset = -width + MAX(0, width - last_cluster_width) / 2; |
| 5853 | glyph->geometry.width = 0; |
| 5854 | |
| 5855 | pango_font_get_glyph_extents(item->analysis.font, |
| 5856 | glyph->glyph, |
| 5857 | &ink_rect, &logical_rect); |
| 5858 | if (ink_rect.x < 0) |
| 5859 | { |
| 5860 | glyph->geometry.x_offset += last_glyph_rbearing; |
| 5861 | glyph->geometry.y_offset = logical_rect.height |
| 5862 | - (gui.char_height - p_linespace) * PANGO_SCALE; |
| 5863 | } |
Bram Moolenaar | 706e84b | 2010-08-07 15:46:45 +0200 | [diff] [blame] | 5864 | else |
| 5865 | /* If the accent width is smaller than the cluster width, position it |
| 5866 | * in the middle. */ |
| 5867 | glyph->geometry.x_offset = -width + MAX(0, width - ink_rect.width) / 2; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5868 | } |
| 5869 | |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 5870 | #if GTK_CHECK_VERSION(3,0,0) |
| 5871 | static void |
| 5872 | draw_glyph_string(int row, int col, int num_cells, int flags, |
| 5873 | PangoFont *font, PangoGlyphString *glyphs, |
| 5874 | cairo_t *cr) |
| 5875 | #else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5876 | static void |
| 5877 | draw_glyph_string(int row, int col, int num_cells, int flags, |
| 5878 | PangoFont *font, PangoGlyphString *glyphs) |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 5879 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5880 | { |
| 5881 | if (!(flags & DRAW_TRANSP)) |
| 5882 | { |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 5883 | #if GTK_CHECK_VERSION(3,0,0) |
Bram Moolenaar | 36edf06 | 2016-07-21 22:10:12 +0200 | [diff] [blame] | 5884 | cairo_set_source_rgba(cr, |
| 5885 | gui.bgcolor->red, gui.bgcolor->green, gui.bgcolor->blue, |
| 5886 | gui.bgcolor->alpha); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 5887 | cairo_rectangle(cr, |
| 5888 | FILL_X(col), FILL_Y(row), |
| 5889 | num_cells * gui.char_width, gui.char_height); |
| 5890 | cairo_fill(cr); |
| 5891 | #else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5892 | gdk_gc_set_foreground(gui.text_gc, gui.bgcolor); |
| 5893 | |
| 5894 | gdk_draw_rectangle(gui.drawarea->window, |
| 5895 | gui.text_gc, |
| 5896 | TRUE, |
| 5897 | FILL_X(col), |
| 5898 | FILL_Y(row), |
| 5899 | num_cells * gui.char_width, |
| 5900 | gui.char_height); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 5901 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5902 | } |
| 5903 | |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 5904 | #if GTK_CHECK_VERSION(3,0,0) |
Bram Moolenaar | 36edf06 | 2016-07-21 22:10:12 +0200 | [diff] [blame] | 5905 | cairo_set_source_rgba(cr, |
| 5906 | gui.fgcolor->red, gui.fgcolor->green, gui.fgcolor->blue, |
| 5907 | gui.fgcolor->alpha); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 5908 | cairo_move_to(cr, TEXT_X(col), TEXT_Y(row)); |
| 5909 | pango_cairo_show_glyph_string(cr, font, glyphs); |
| 5910 | #else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5911 | gdk_gc_set_foreground(gui.text_gc, gui.fgcolor); |
| 5912 | |
| 5913 | gdk_draw_glyphs(gui.drawarea->window, |
| 5914 | gui.text_gc, |
| 5915 | font, |
| 5916 | TEXT_X(col), |
| 5917 | TEXT_Y(row), |
| 5918 | glyphs); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 5919 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5920 | |
| 5921 | /* redraw the contents with an offset of 1 to emulate bold */ |
| 5922 | if ((flags & DRAW_BOLD) && !gui.font_can_bold) |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 5923 | #if GTK_CHECK_VERSION(3,0,0) |
| 5924 | { |
Bram Moolenaar | 36edf06 | 2016-07-21 22:10:12 +0200 | [diff] [blame] | 5925 | cairo_set_source_rgba(cr, |
| 5926 | gui.fgcolor->red, gui.fgcolor->green, gui.fgcolor->blue, |
| 5927 | gui.fgcolor->alpha); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 5928 | cairo_move_to(cr, TEXT_X(col) + 1, TEXT_Y(row)); |
| 5929 | pango_cairo_show_glyph_string(cr, font, glyphs); |
| 5930 | } |
| 5931 | #else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5932 | gdk_draw_glyphs(gui.drawarea->window, |
| 5933 | gui.text_gc, |
| 5934 | font, |
| 5935 | TEXT_X(col) + 1, |
| 5936 | TEXT_Y(row), |
| 5937 | glyphs); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 5938 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5939 | } |
| 5940 | |
Bram Moolenaar | f36d369 | 2005-03-15 22:48:14 +0000 | [diff] [blame] | 5941 | /* |
| 5942 | * Draw underline and undercurl at the bottom of the character cell. |
| 5943 | */ |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 5944 | #if GTK_CHECK_VERSION(3,0,0) |
| 5945 | static void |
| 5946 | draw_under(int flags, int row, int col, int cells, cairo_t *cr) |
| 5947 | #else |
Bram Moolenaar | f36d369 | 2005-03-15 22:48:14 +0000 | [diff] [blame] | 5948 | static void |
| 5949 | draw_under(int flags, int row, int col, int cells) |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 5950 | #endif |
Bram Moolenaar | f36d369 | 2005-03-15 22:48:14 +0000 | [diff] [blame] | 5951 | { |
| 5952 | int i; |
| 5953 | int offset; |
Bram Moolenaar | b85cb21 | 2009-05-17 14:24:23 +0000 | [diff] [blame] | 5954 | 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] | 5955 | int y = FILL_Y(row + 1) - 1; |
| 5956 | |
| 5957 | /* Undercurl: draw curl at the bottom of the character cell. */ |
| 5958 | if (flags & DRAW_UNDERC) |
| 5959 | { |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 5960 | #if GTK_CHECK_VERSION(3,0,0) |
| 5961 | cairo_set_line_width(cr, 1.0); |
| 5962 | cairo_set_line_cap(cr, CAIRO_LINE_CAP_BUTT); |
Bram Moolenaar | 36edf06 | 2016-07-21 22:10:12 +0200 | [diff] [blame] | 5963 | cairo_set_source_rgba(cr, |
| 5964 | gui.spcolor->red, gui.spcolor->green, gui.spcolor->blue, |
| 5965 | gui.spcolor->alpha); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 5966 | for (i = FILL_X(col); i < FILL_X(col + cells); ++i) |
| 5967 | { |
| 5968 | offset = val[i % 8]; |
| 5969 | cairo_line_to(cr, i, y - offset + 0.5); |
| 5970 | } |
| 5971 | cairo_stroke(cr); |
| 5972 | #else |
Bram Moolenaar | f36d369 | 2005-03-15 22:48:14 +0000 | [diff] [blame] | 5973 | gdk_gc_set_foreground(gui.text_gc, gui.spcolor); |
| 5974 | for (i = FILL_X(col); i < FILL_X(col + cells); ++i) |
| 5975 | { |
| 5976 | offset = val[i % 8]; |
| 5977 | gdk_draw_point(gui.drawarea->window, gui.text_gc, i, y - offset); |
| 5978 | } |
| 5979 | gdk_gc_set_foreground(gui.text_gc, gui.fgcolor); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 5980 | #endif |
Bram Moolenaar | f36d369 | 2005-03-15 22:48:14 +0000 | [diff] [blame] | 5981 | } |
| 5982 | |
Bram Moolenaar | cf4b00c | 2017-09-02 18:33:56 +0200 | [diff] [blame] | 5983 | /* Draw a strikethrough line */ |
| 5984 | if (flags & DRAW_STRIKE) |
| 5985 | { |
| 5986 | #if GTK_CHECK_VERSION(3,0,0) |
| 5987 | cairo_set_line_width(cr, 1.0); |
| 5988 | cairo_set_line_cap(cr, CAIRO_LINE_CAP_BUTT); |
| 5989 | cairo_set_source_rgba(cr, |
| 5990 | gui.spcolor->red, gui.spcolor->green, gui.spcolor->blue, |
| 5991 | gui.spcolor->alpha); |
| 5992 | cairo_move_to(cr, FILL_X(col), y + 1 - gui.char_height/2 + 0.5); |
| 5993 | cairo_line_to(cr, FILL_X(col + cells), y + 1 - gui.char_height/2 + 0.5); |
| 5994 | cairo_stroke(cr); |
| 5995 | #else |
| 5996 | gdk_gc_set_foreground(gui.text_gc, gui.spcolor); |
| 5997 | gdk_draw_line(gui.drawarea->window, gui.text_gc, |
| 5998 | FILL_X(col), y + 1 - gui.char_height/2, |
| 5999 | FILL_X(col + cells), y + 1 - gui.char_height/2); |
| 6000 | gdk_gc_set_foreground(gui.text_gc, gui.fgcolor); |
| 6001 | #endif |
| 6002 | } |
| 6003 | |
Bram Moolenaar | f36d369 | 2005-03-15 22:48:14 +0000 | [diff] [blame] | 6004 | /* Underline: draw a line at the bottom of the character cell. */ |
| 6005 | if (flags & DRAW_UNDERL) |
| 6006 | { |
| 6007 | /* When p_linespace is 0, overwrite the bottom row of pixels. |
| 6008 | * Otherwise put the line just below the character. */ |
| 6009 | if (p_linespace > 1) |
| 6010 | y -= p_linespace - 1; |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 6011 | #if GTK_CHECK_VERSION(3,0,0) |
Bram Moolenaar | cf4b00c | 2017-09-02 18:33:56 +0200 | [diff] [blame] | 6012 | cairo_set_line_width(cr, 1.0); |
| 6013 | cairo_set_line_cap(cr, CAIRO_LINE_CAP_BUTT); |
| 6014 | cairo_set_source_rgba(cr, |
| 6015 | gui.fgcolor->red, gui.fgcolor->green, gui.fgcolor->blue, |
| 6016 | gui.fgcolor->alpha); |
| 6017 | cairo_move_to(cr, FILL_X(col), y + 0.5); |
| 6018 | cairo_line_to(cr, FILL_X(col + cells), y + 0.5); |
| 6019 | cairo_stroke(cr); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 6020 | #else |
Bram Moolenaar | f36d369 | 2005-03-15 22:48:14 +0000 | [diff] [blame] | 6021 | gdk_draw_line(gui.drawarea->window, gui.text_gc, |
| 6022 | FILL_X(col), y, |
| 6023 | FILL_X(col + cells) - 1, y); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 6024 | #endif |
Bram Moolenaar | f36d369 | 2005-03-15 22:48:14 +0000 | [diff] [blame] | 6025 | } |
| 6026 | } |
| 6027 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6028 | int |
| 6029 | gui_gtk2_draw_string(int row, int col, char_u *s, int len, int flags) |
| 6030 | { |
| 6031 | GdkRectangle area; /* area for clip mask */ |
| 6032 | PangoGlyphString *glyphs; /* glyphs of current item */ |
| 6033 | int column_offset = 0; /* column offset in cells */ |
| 6034 | int i; |
| 6035 | char_u *conv_buf = NULL; /* result of UTF-8 conversion */ |
| 6036 | char_u *new_conv_buf; |
| 6037 | int convlen; |
| 6038 | char_u *sp, *bp; |
| 6039 | int plen; |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 6040 | #if GTK_CHECK_VERSION(3,0,0) |
| 6041 | cairo_t *cr; |
| 6042 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6043 | |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 6044 | #if GTK_CHECK_VERSION(3,0,0) |
| 6045 | if (gui.text_context == NULL || gtk_widget_get_window(gui.drawarea) == NULL) |
| 6046 | #else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6047 | if (gui.text_context == NULL || gui.drawarea->window == NULL) |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 6048 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6049 | return len; |
| 6050 | |
| 6051 | if (output_conv.vc_type != CONV_NONE) |
| 6052 | { |
| 6053 | /* |
| 6054 | * Convert characters from 'encoding' to 'termencoding', which is set |
| 6055 | * to UTF-8 by gui_mch_init(). did_set_string_option() in option.c |
| 6056 | * prohibits changing this to something else than UTF-8 if the GUI is |
| 6057 | * in use. |
| 6058 | */ |
| 6059 | convlen = len; |
| 6060 | conv_buf = string_convert(&output_conv, s, &convlen); |
| 6061 | g_return_val_if_fail(conv_buf != NULL, len); |
| 6062 | |
| 6063 | /* Correct for differences in char width: some chars are |
| 6064 | * double-wide in 'encoding' but single-wide in utf-8. Add a space to |
| 6065 | * compensate for that. */ |
| 6066 | for (sp = s, bp = conv_buf; sp < s + len && bp < conv_buf + convlen; ) |
| 6067 | { |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 6068 | plen = utf_ptr2len(bp); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6069 | if ((*mb_ptr2cells)(sp) == 2 && utf_ptr2cells(bp) == 1) |
| 6070 | { |
| 6071 | new_conv_buf = alloc(convlen + 2); |
| 6072 | if (new_conv_buf == NULL) |
| 6073 | return len; |
| 6074 | plen += bp - conv_buf; |
| 6075 | mch_memmove(new_conv_buf, conv_buf, plen); |
| 6076 | new_conv_buf[plen] = ' '; |
| 6077 | mch_memmove(new_conv_buf + plen + 1, conv_buf + plen, |
| 6078 | convlen - plen + 1); |
| 6079 | vim_free(conv_buf); |
| 6080 | conv_buf = new_conv_buf; |
| 6081 | ++convlen; |
| 6082 | bp = conv_buf + plen; |
| 6083 | plen = 1; |
| 6084 | } |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 6085 | sp += (*mb_ptr2len)(sp); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6086 | bp += plen; |
| 6087 | } |
| 6088 | s = conv_buf; |
| 6089 | len = convlen; |
| 6090 | } |
| 6091 | |
| 6092 | /* |
| 6093 | * Restrict all drawing to the current screen line in order to prevent |
| 6094 | * fuzzy font lookups from messing up the screen. |
| 6095 | */ |
| 6096 | area.x = gui.border_offset; |
| 6097 | area.y = FILL_Y(row); |
| 6098 | area.width = gui.num_cols * gui.char_width; |
| 6099 | area.height = gui.char_height; |
| 6100 | |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 6101 | #if GTK_CHECK_VERSION(3,0,0) |
| 6102 | cr = cairo_create(gui.surface); |
| 6103 | cairo_rectangle(cr, area.x, area.y, area.width, area.height); |
| 6104 | cairo_clip(cr); |
| 6105 | #else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6106 | gdk_gc_set_clip_origin(gui.text_gc, 0, 0); |
| 6107 | gdk_gc_set_clip_rectangle(gui.text_gc, &area); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 6108 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6109 | |
| 6110 | glyphs = pango_glyph_string_new(); |
| 6111 | |
| 6112 | /* |
| 6113 | * Optimization hack: If possible, skip the itemize and shaping process |
| 6114 | * for pure ASCII strings. This optimization is particularly effective |
| 6115 | * because Vim draws space characters to clear parts of the screen. |
| 6116 | */ |
| 6117 | if (!(flags & DRAW_ITALIC) |
| 6118 | && !((flags & DRAW_BOLD) && gui.font_can_bold) |
| 6119 | && gui.ascii_glyphs != NULL) |
| 6120 | { |
| 6121 | char_u *p; |
| 6122 | |
| 6123 | for (p = s; p < s + len; ++p) |
| 6124 | if (*p & 0x80) |
| 6125 | goto not_ascii; |
| 6126 | |
| 6127 | pango_glyph_string_set_size(glyphs, len); |
| 6128 | |
| 6129 | for (i = 0; i < len; ++i) |
| 6130 | { |
Bram Moolenaar | 16350cb | 2016-08-14 20:27:34 +0200 | [diff] [blame] | 6131 | glyphs->glyphs[i] = gui.ascii_glyphs->glyphs[2 * s[i]]; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6132 | glyphs->log_clusters[i] = i; |
| 6133 | } |
| 6134 | |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 6135 | #if GTK_CHECK_VERSION(3,0,0) |
| 6136 | draw_glyph_string(row, col, len, flags, gui.ascii_font, glyphs, cr); |
| 6137 | #else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6138 | draw_glyph_string(row, col, len, flags, gui.ascii_font, glyphs); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 6139 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6140 | |
| 6141 | column_offset = len; |
| 6142 | } |
| 6143 | else |
| 6144 | not_ascii: |
| 6145 | { |
| 6146 | PangoAttrList *attr_list; |
| 6147 | GList *item_list; |
| 6148 | int cluster_width; |
| 6149 | int last_glyph_rbearing; |
| 6150 | int cells = 0; /* cells occupied by current cluster */ |
| 6151 | |
Bram Moolenaar | 3fdfa4a | 2004-10-07 21:02:47 +0000 | [diff] [blame] | 6152 | /* Safety check: pango crashes when invoked with invalid utf-8 |
| 6153 | * characters. */ |
| 6154 | if (!utf_valid_string(s, s + len)) |
| 6155 | { |
| 6156 | column_offset = len; |
| 6157 | goto skipitall; |
| 6158 | } |
| 6159 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6160 | /* original width of the current cluster */ |
| 6161 | cluster_width = PANGO_SCALE * gui.char_width; |
| 6162 | |
| 6163 | /* right bearing of the last non-composing glyph */ |
| 6164 | last_glyph_rbearing = PANGO_SCALE * gui.char_width; |
| 6165 | |
| 6166 | attr_list = pango_attr_list_new(); |
| 6167 | |
| 6168 | /* If 'guifontwide' is set then use that for double-width characters. |
| 6169 | * Otherwise just go with 'guifont' and let Pango do its thing. */ |
| 6170 | if (gui.wide_font != NULL) |
| 6171 | apply_wide_font_attr(s, len, attr_list); |
| 6172 | |
| 6173 | if ((flags & DRAW_BOLD) && gui.font_can_bold) |
| 6174 | INSERT_PANGO_ATTR(pango_attr_weight_new(PANGO_WEIGHT_BOLD), |
| 6175 | attr_list, 0, len); |
| 6176 | if (flags & DRAW_ITALIC) |
| 6177 | INSERT_PANGO_ATTR(pango_attr_style_new(PANGO_STYLE_ITALIC), |
| 6178 | attr_list, 0, len); |
| 6179 | /* |
| 6180 | * Break the text into segments with consistent directional level |
| 6181 | * and shaping engine. Pure Latin text needs only a single segment, |
| 6182 | * so there's no need to worry about the loop's efficiency. Better |
| 6183 | * try to optimize elsewhere, e.g. reducing exposes and stuff :) |
| 6184 | */ |
| 6185 | item_list = pango_itemize(gui.text_context, |
| 6186 | (const char *)s, 0, len, attr_list, NULL); |
| 6187 | |
| 6188 | while (item_list != NULL) |
| 6189 | { |
| 6190 | PangoItem *item; |
| 6191 | int item_cells = 0; /* item length in cells */ |
| 6192 | |
| 6193 | item = (PangoItem *)item_list->data; |
| 6194 | item_list = g_list_delete_link(item_list, item_list); |
| 6195 | /* |
| 6196 | * Increment the bidirectional embedding level by 1 if it is not |
| 6197 | * even. An odd number means the output will be RTL, but we don't |
| 6198 | * want that since Vim handles right-to-left text on its own. It |
| 6199 | * would probably be sufficient to just set level = 0, but you can |
| 6200 | * never know :) |
| 6201 | * |
| 6202 | * Unfortunately we can't take advantage of Pango's ability to |
| 6203 | * render both LTR and RTL at the same time. In order to support |
| 6204 | * that, Vim's main screen engine would have to make use of Pango |
| 6205 | * functionality. |
| 6206 | */ |
| 6207 | item->analysis.level = (item->analysis.level + 1) & (~1U); |
| 6208 | |
| 6209 | /* HACK: Overrule the shape engine, we don't want shaping to be |
| 6210 | * done, because drawing the cursor would change the display. */ |
| 6211 | item->analysis.shape_engine = default_shape_engine; |
| 6212 | |
Bram Moolenaar | 3cbe0c0 | 2015-09-08 20:00:22 +0200 | [diff] [blame] | 6213 | #ifdef HAVE_PANGO_SHAPE_FULL |
Bram Moolenaar | 7e2ec00 | 2015-09-08 16:31:06 +0200 | [diff] [blame] | 6214 | pango_shape_full((const char *)s + item->offset, item->length, |
| 6215 | (const char *)s, len, &item->analysis, glyphs); |
Bram Moolenaar | 3cbe0c0 | 2015-09-08 20:00:22 +0200 | [diff] [blame] | 6216 | #else |
| 6217 | pango_shape((const char *)s + item->offset, item->length, |
| 6218 | &item->analysis, glyphs); |
| 6219 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6220 | /* |
| 6221 | * Fixed-width hack: iterate over the array and assign a fixed |
| 6222 | * width to each glyph, thus overriding the choice made by the |
| 6223 | * shaping engine. We use utf_char2cells() to determine the |
| 6224 | * number of cells needed. |
| 6225 | * |
| 6226 | * Also perform all kind of dark magic to get composing |
| 6227 | * characters right (and pretty too of course). |
| 6228 | */ |
| 6229 | for (i = 0; i < glyphs->num_glyphs; ++i) |
| 6230 | { |
| 6231 | PangoGlyphInfo *glyph; |
| 6232 | |
| 6233 | glyph = &glyphs->glyphs[i]; |
| 6234 | |
| 6235 | if (glyph->attr.is_cluster_start) |
| 6236 | { |
| 6237 | int cellcount; |
| 6238 | |
| 6239 | cellcount = count_cluster_cells( |
| 6240 | s, item, glyphs, i, &cluster_width, |
| 6241 | (item_list != NULL) ? &last_glyph_rbearing : NULL); |
| 6242 | |
| 6243 | if (cellcount > 0) |
| 6244 | { |
| 6245 | int width; |
| 6246 | |
| 6247 | width = cellcount * gui.char_width * PANGO_SCALE; |
| 6248 | glyph->geometry.x_offset += |
| 6249 | MAX(0, width - cluster_width) / 2; |
| 6250 | glyph->geometry.width = width; |
| 6251 | } |
| 6252 | else |
| 6253 | { |
| 6254 | /* If there are only combining characters in the |
| 6255 | * cluster, we cannot just change the width of the |
| 6256 | * previous glyph since there is none. Therefore |
| 6257 | * some guesswork is needed. */ |
| 6258 | setup_zero_width_cluster(item, glyph, cells, |
| 6259 | cluster_width, |
| 6260 | last_glyph_rbearing); |
| 6261 | } |
| 6262 | |
| 6263 | item_cells += cellcount; |
| 6264 | cells = cellcount; |
| 6265 | } |
| 6266 | else if (i > 0) |
| 6267 | { |
| 6268 | int width; |
| 6269 | |
| 6270 | /* There is a previous glyph, so we deal with combining |
Bram Moolenaar | 706e84b | 2010-08-07 15:46:45 +0200 | [diff] [blame] | 6271 | * characters the canonical way. |
Bram Moolenaar | 96118f3 | 2010-08-08 14:40:37 +0200 | [diff] [blame] | 6272 | * In some circumstances Pango uses a positive x_offset, |
| 6273 | * then use the width of the previous glyph for this one |
| 6274 | * and set the previous width to zero. |
| 6275 | * Otherwise we get a negative x_offset, Pango has already |
| 6276 | * positioned the combining char, keep the widths as they |
| 6277 | * are. |
Bram Moolenaar | 706e84b | 2010-08-07 15:46:45 +0200 | [diff] [blame] | 6278 | * For both adjust the x_offset to position the glyph in |
Bram Moolenaar | 96118f3 | 2010-08-08 14:40:37 +0200 | [diff] [blame] | 6279 | * the middle. */ |
Bram Moolenaar | 706e84b | 2010-08-07 15:46:45 +0200 | [diff] [blame] | 6280 | if (glyph->geometry.x_offset >= 0) |
Bram Moolenaar | 96118f3 | 2010-08-08 14:40:37 +0200 | [diff] [blame] | 6281 | { |
| 6282 | glyphs->glyphs[i].geometry.width = |
| 6283 | glyphs->glyphs[i - 1].geometry.width; |
Bram Moolenaar | 706e84b | 2010-08-07 15:46:45 +0200 | [diff] [blame] | 6284 | glyphs->glyphs[i - 1].geometry.width = 0; |
Bram Moolenaar | 96118f3 | 2010-08-08 14:40:37 +0200 | [diff] [blame] | 6285 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6286 | width = cells * gui.char_width * PANGO_SCALE; |
| 6287 | glyph->geometry.x_offset += |
| 6288 | MAX(0, width - cluster_width) / 2; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6289 | } |
| 6290 | else /* i == 0 "cannot happen" */ |
| 6291 | { |
| 6292 | glyph->geometry.width = 0; |
| 6293 | } |
| 6294 | } |
| 6295 | |
| 6296 | /*** Aaaaand action! ***/ |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 6297 | #if GTK_CHECK_VERSION(3,0,0) |
| 6298 | draw_glyph_string(row, col + column_offset, item_cells, |
| 6299 | flags, item->analysis.font, glyphs, |
| 6300 | cr); |
| 6301 | #else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6302 | draw_glyph_string(row, col + column_offset, item_cells, |
| 6303 | flags, item->analysis.font, glyphs); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 6304 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6305 | |
| 6306 | pango_item_free(item); |
| 6307 | |
| 6308 | column_offset += item_cells; |
| 6309 | } |
| 6310 | |
| 6311 | pango_attr_list_unref(attr_list); |
| 6312 | } |
| 6313 | |
Bram Moolenaar | 3fdfa4a | 2004-10-07 21:02:47 +0000 | [diff] [blame] | 6314 | skipitall: |
Bram Moolenaar | f36d369 | 2005-03-15 22:48:14 +0000 | [diff] [blame] | 6315 | /* Draw underline and undercurl. */ |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 6316 | #if GTK_CHECK_VERSION(3,0,0) |
| 6317 | draw_under(flags, row, col, column_offset, cr); |
| 6318 | #else |
Bram Moolenaar | f36d369 | 2005-03-15 22:48:14 +0000 | [diff] [blame] | 6319 | draw_under(flags, row, col, column_offset); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 6320 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6321 | |
| 6322 | pango_glyph_string_free(glyphs); |
| 6323 | vim_free(conv_buf); |
| 6324 | |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 6325 | #if GTK_CHECK_VERSION(3,0,0) |
| 6326 | cairo_destroy(cr); |
| 6327 | if (!gui.by_signal) |
| 6328 | gdk_window_invalidate_rect(gtk_widget_get_window(gui.drawarea), |
| 6329 | &area, FALSE); |
| 6330 | #else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6331 | gdk_gc_set_clip_rectangle(gui.text_gc, NULL); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 6332 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6333 | |
| 6334 | return column_offset; |
| 6335 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6336 | |
| 6337 | /* |
| 6338 | * Return OK if the key with the termcap name "name" is supported. |
| 6339 | */ |
| 6340 | int |
| 6341 | gui_mch_haskey(char_u *name) |
| 6342 | { |
| 6343 | int i; |
| 6344 | |
| 6345 | for (i = 0; special_keys[i].key_sym != 0; i++) |
| 6346 | if (name[0] == special_keys[i].code0 |
| 6347 | && name[1] == special_keys[i].code1) |
| 6348 | return OK; |
| 6349 | return FAIL; |
| 6350 | } |
| 6351 | |
Bram Moolenaar | b2c5a5a | 2013-02-14 22:11:39 +0100 | [diff] [blame] | 6352 | #if defined(FEAT_TITLE) || defined(FEAT_EVAL) || defined(PROTO) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6353 | /* |
| 6354 | * Return the text window-id and display. Only required for X-based GUI's |
| 6355 | */ |
| 6356 | int |
| 6357 | gui_get_x11_windis(Window *win, Display **dis) |
| 6358 | { |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 6359 | #if GTK_CHECK_VERSION(3,0,0) |
| 6360 | if (gui.mainwin != NULL && gtk_widget_get_window(gui.mainwin) != NULL) |
| 6361 | #else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6362 | if (gui.mainwin != NULL && gui.mainwin->window != NULL) |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 6363 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6364 | { |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 6365 | #if GTK_CHECK_VERSION(3,0,0) |
| 6366 | *dis = GDK_WINDOW_XDISPLAY(gtk_widget_get_window(gui.mainwin)); |
| 6367 | *win = GDK_WINDOW_XID(gtk_widget_get_window(gui.mainwin)); |
| 6368 | #else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6369 | *dis = GDK_WINDOW_XDISPLAY(gui.mainwin->window); |
| 6370 | *win = GDK_WINDOW_XWINDOW(gui.mainwin->window); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 6371 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6372 | return OK; |
| 6373 | } |
| 6374 | |
| 6375 | *dis = NULL; |
| 6376 | *win = 0; |
| 6377 | return FAIL; |
| 6378 | } |
| 6379 | #endif |
| 6380 | |
| 6381 | #if defined(FEAT_CLIENTSERVER) \ |
| 6382 | || (defined(FEAT_X11) && defined(FEAT_CLIPBOARD)) || defined(PROTO) |
| 6383 | |
| 6384 | Display * |
| 6385 | gui_mch_get_display(void) |
| 6386 | { |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 6387 | #if GTK_CHECK_VERSION(3,0,0) |
| 6388 | if (gui.mainwin != NULL && gtk_widget_get_window(gui.mainwin) != NULL) |
| 6389 | return GDK_WINDOW_XDISPLAY(gtk_widget_get_window(gui.mainwin)); |
| 6390 | #else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6391 | if (gui.mainwin != NULL && gui.mainwin->window != NULL) |
| 6392 | return GDK_WINDOW_XDISPLAY(gui.mainwin->window); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 6393 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6394 | else |
| 6395 | return NULL; |
| 6396 | } |
| 6397 | #endif |
| 6398 | |
| 6399 | void |
| 6400 | gui_mch_beep(void) |
| 6401 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6402 | GdkDisplay *display; |
| 6403 | |
Bram Moolenaar | b463e8d | 2017-06-05 15:07:09 +0200 | [diff] [blame] | 6404 | #if GTK_CHECK_VERSION(3,0,0) |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 6405 | if (gui.mainwin != NULL && gtk_widget_get_realized(gui.mainwin)) |
Bram Moolenaar | b463e8d | 2017-06-05 15:07:09 +0200 | [diff] [blame] | 6406 | #else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6407 | if (gui.mainwin != NULL && GTK_WIDGET_REALIZED(gui.mainwin)) |
Bram Moolenaar | b463e8d | 2017-06-05 15:07:09 +0200 | [diff] [blame] | 6408 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6409 | display = gtk_widget_get_display(gui.mainwin); |
| 6410 | else |
| 6411 | display = gdk_display_get_default(); |
| 6412 | |
| 6413 | if (display != NULL) |
| 6414 | gdk_display_beep(display); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6415 | } |
| 6416 | |
| 6417 | void |
| 6418 | gui_mch_flash(int msec) |
| 6419 | { |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 6420 | #if GTK_CHECK_VERSION(3,0,0) |
| 6421 | /* TODO Replace GdkGC with Cairo */ |
| 6422 | (void)msec; |
| 6423 | #else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6424 | GdkGCValues values; |
| 6425 | GdkGC *invert_gc; |
| 6426 | |
| 6427 | if (gui.drawarea->window == NULL) |
| 6428 | return; |
| 6429 | |
| 6430 | values.foreground.pixel = gui.norm_pixel ^ gui.back_pixel; |
| 6431 | values.background.pixel = gui.norm_pixel ^ gui.back_pixel; |
| 6432 | values.function = GDK_XOR; |
| 6433 | invert_gc = gdk_gc_new_with_values(gui.drawarea->window, |
| 6434 | &values, |
| 6435 | GDK_GC_FOREGROUND | |
| 6436 | GDK_GC_BACKGROUND | |
| 6437 | GDK_GC_FUNCTION); |
| 6438 | gdk_gc_set_exposures(invert_gc, |
| 6439 | gui.visibility != GDK_VISIBILITY_UNOBSCURED); |
| 6440 | /* |
| 6441 | * Do a visual beep by changing back and forth in some undetermined way, |
| 6442 | * the foreground and background colors. This is due to the fact that |
| 6443 | * there can't be really any prediction about the effects of XOR on |
| 6444 | * arbitrary X11 servers. However this seems to be enough for what we |
| 6445 | * intend it to do. |
| 6446 | */ |
| 6447 | gdk_draw_rectangle(gui.drawarea->window, invert_gc, |
| 6448 | TRUE, |
| 6449 | 0, 0, |
| 6450 | FILL_X((int)Columns) + gui.border_offset, |
| 6451 | FILL_Y((int)Rows) + gui.border_offset); |
| 6452 | |
| 6453 | gui_mch_flush(); |
| 6454 | ui_delay((long)msec, TRUE); /* wait so many msec */ |
| 6455 | |
| 6456 | gdk_draw_rectangle(gui.drawarea->window, invert_gc, |
| 6457 | TRUE, |
| 6458 | 0, 0, |
| 6459 | FILL_X((int)Columns) + gui.border_offset, |
| 6460 | FILL_Y((int)Rows) + gui.border_offset); |
| 6461 | |
| 6462 | gdk_gc_destroy(invert_gc); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 6463 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6464 | } |
| 6465 | |
| 6466 | /* |
| 6467 | * Invert a rectangle from row r, column c, for nr rows and nc columns. |
| 6468 | */ |
| 6469 | void |
| 6470 | gui_mch_invert_rectangle(int r, int c, int nr, int nc) |
| 6471 | { |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 6472 | #if GTK_CHECK_VERSION(3,0,0) |
Bram Moolenaar | 4fc563b | 2016-03-12 12:40:58 +0100 | [diff] [blame] | 6473 | const GdkRectangle rect = { |
| 6474 | FILL_X(c), FILL_Y(r), nc * gui.char_width, nr * gui.char_height |
| 6475 | }; |
| 6476 | cairo_t * const cr = cairo_create(gui.surface); |
| 6477 | |
Bram Moolenaar | 36edf06 | 2016-07-21 22:10:12 +0200 | [diff] [blame] | 6478 | set_cairo_source_rgba_from_color(cr, gui.norm_pixel ^ gui.back_pixel); |
Bram Moolenaar | 4fc563b | 2016-03-12 12:40:58 +0100 | [diff] [blame] | 6479 | # if CAIRO_VERSION >= CAIRO_VERSION_ENCODE(1,9,2) |
| 6480 | cairo_set_operator(cr, CAIRO_OPERATOR_DIFFERENCE); |
| 6481 | # else |
| 6482 | /* Give an implementation for older cairo versions if necessary. */ |
| 6483 | # endif |
| 6484 | gdk_cairo_rectangle(cr, &rect); |
| 6485 | cairo_fill(cr); |
| 6486 | |
| 6487 | cairo_destroy(cr); |
| 6488 | |
| 6489 | if (!gui.by_signal) |
| 6490 | gtk_widget_queue_draw_area(gui.drawarea, rect.x, rect.y, |
| 6491 | rect.width, rect.height); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 6492 | #else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6493 | GdkGCValues values; |
| 6494 | GdkGC *invert_gc; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6495 | |
| 6496 | if (gui.drawarea->window == NULL) |
| 6497 | return; |
| 6498 | |
Bram Moolenaar | 89d4032 | 2006-08-29 15:30:07 +0000 | [diff] [blame] | 6499 | values.foreground.pixel = gui.norm_pixel ^ gui.back_pixel; |
| 6500 | values.background.pixel = gui.norm_pixel ^ gui.back_pixel; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6501 | values.function = GDK_XOR; |
| 6502 | invert_gc = gdk_gc_new_with_values(gui.drawarea->window, |
| 6503 | &values, |
| 6504 | GDK_GC_FOREGROUND | |
| 6505 | GDK_GC_BACKGROUND | |
| 6506 | GDK_GC_FUNCTION); |
Bram Moolenaar | 89d4032 | 2006-08-29 15:30:07 +0000 | [diff] [blame] | 6507 | gdk_gc_set_exposures(invert_gc, gui.visibility != |
| 6508 | GDK_VISIBILITY_UNOBSCURED); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6509 | gdk_draw_rectangle(gui.drawarea->window, invert_gc, |
| 6510 | TRUE, |
| 6511 | FILL_X(c), FILL_Y(r), |
| 6512 | (nc) * gui.char_width, (nr) * gui.char_height); |
| 6513 | gdk_gc_destroy(invert_gc); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 6514 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6515 | } |
| 6516 | |
| 6517 | /* |
| 6518 | * Iconify the GUI window. |
| 6519 | */ |
| 6520 | void |
| 6521 | gui_mch_iconify(void) |
| 6522 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6523 | gtk_window_iconify(GTK_WINDOW(gui.mainwin)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6524 | } |
| 6525 | |
| 6526 | #if defined(FEAT_EVAL) || defined(PROTO) |
| 6527 | /* |
| 6528 | * Bring the Vim window to the foreground. |
| 6529 | */ |
| 6530 | void |
| 6531 | gui_mch_set_foreground(void) |
| 6532 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6533 | gtk_window_present(GTK_WINDOW(gui.mainwin)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6534 | } |
| 6535 | #endif |
| 6536 | |
| 6537 | /* |
| 6538 | * Draw a cursor without focus. |
| 6539 | */ |
| 6540 | void |
| 6541 | gui_mch_draw_hollow_cursor(guicolor_T color) |
| 6542 | { |
| 6543 | int i = 1; |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 6544 | #if GTK_CHECK_VERSION(3,0,0) |
| 6545 | cairo_t *cr; |
| 6546 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6547 | |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 6548 | #if GTK_CHECK_VERSION(3,0,0) |
| 6549 | if (gtk_widget_get_window(gui.drawarea) == NULL) |
| 6550 | #else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6551 | if (gui.drawarea->window == NULL) |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 6552 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6553 | return; |
| 6554 | |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 6555 | #if GTK_CHECK_VERSION(3,0,0) |
| 6556 | cr = cairo_create(gui.surface); |
| 6557 | #endif |
| 6558 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6559 | gui_mch_set_fg_color(color); |
| 6560 | |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 6561 | #if GTK_CHECK_VERSION(3,0,0) |
Bram Moolenaar | 36edf06 | 2016-07-21 22:10:12 +0200 | [diff] [blame] | 6562 | cairo_set_source_rgba(cr, |
| 6563 | gui.fgcolor->red, gui.fgcolor->green, gui.fgcolor->blue, |
| 6564 | gui.fgcolor->alpha); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 6565 | #else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6566 | gdk_gc_set_foreground(gui.text_gc, gui.fgcolor); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 6567 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6568 | if (mb_lefthalve(gui.row, gui.col)) |
| 6569 | i = 2; |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 6570 | #if GTK_CHECK_VERSION(3,0,0) |
| 6571 | cairo_set_line_width(cr, 1.0); |
| 6572 | cairo_set_line_cap(cr, CAIRO_LINE_CAP_BUTT); |
| 6573 | cairo_rectangle(cr, |
| 6574 | FILL_X(gui.col) + 0.5, FILL_Y(gui.row) + 0.5, |
| 6575 | i * gui.char_width - 1, gui.char_height - 1); |
| 6576 | cairo_stroke(cr); |
| 6577 | cairo_destroy(cr); |
| 6578 | #else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6579 | gdk_draw_rectangle(gui.drawarea->window, gui.text_gc, |
| 6580 | FALSE, |
| 6581 | FILL_X(gui.col), FILL_Y(gui.row), |
| 6582 | i * gui.char_width - 1, gui.char_height - 1); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 6583 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6584 | } |
| 6585 | |
| 6586 | /* |
| 6587 | * Draw part of a cursor, "w" pixels wide, and "h" pixels high, using |
| 6588 | * color "color". |
| 6589 | */ |
| 6590 | void |
| 6591 | gui_mch_draw_part_cursor(int w, int h, guicolor_T color) |
| 6592 | { |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 6593 | #if GTK_CHECK_VERSION(3,0,0) |
| 6594 | if (gtk_widget_get_window(gui.drawarea) == NULL) |
| 6595 | #else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6596 | if (gui.drawarea->window == NULL) |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 6597 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6598 | return; |
| 6599 | |
| 6600 | gui_mch_set_fg_color(color); |
| 6601 | |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 6602 | #if GTK_CHECK_VERSION(3,0,0) |
| 6603 | { |
| 6604 | cairo_t *cr; |
| 6605 | |
| 6606 | cr = cairo_create(gui.surface); |
Bram Moolenaar | 36edf06 | 2016-07-21 22:10:12 +0200 | [diff] [blame] | 6607 | cairo_set_source_rgba(cr, |
| 6608 | gui.fgcolor->red, gui.fgcolor->green, gui.fgcolor->blue, |
| 6609 | gui.fgcolor->alpha); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 6610 | cairo_rectangle(cr, |
| 6611 | # ifdef FEAT_RIGHTLEFT |
| 6612 | /* vertical line should be on the right of current point */ |
| 6613 | CURSOR_BAR_RIGHT ? FILL_X(gui.col + 1) - w : |
| 6614 | # endif |
| 6615 | FILL_X(gui.col), FILL_Y(gui.row) + gui.char_height - h, |
| 6616 | w, h); |
| 6617 | cairo_fill(cr); |
| 6618 | cairo_destroy(cr); |
| 6619 | } |
| 6620 | #else /* !GTK_CHECK_VERSION(3,0,0) */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6621 | gdk_gc_set_foreground(gui.text_gc, gui.fgcolor); |
| 6622 | gdk_draw_rectangle(gui.drawarea->window, gui.text_gc, |
| 6623 | TRUE, |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 6624 | # ifdef FEAT_RIGHTLEFT |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6625 | /* vertical line should be on the right of current point */ |
| 6626 | CURSOR_BAR_RIGHT ? FILL_X(gui.col + 1) - w : |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 6627 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6628 | FILL_X(gui.col), |
| 6629 | FILL_Y(gui.row) + gui.char_height - h, |
| 6630 | w, h); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 6631 | #endif /* !GTK_CHECK_VERSION(3,0,0) */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6632 | } |
| 6633 | |
| 6634 | |
| 6635 | /* |
| 6636 | * Catch up with any queued X11 events. This may put keyboard input into the |
| 6637 | * input buffer, call resize call-backs, trigger timers etc. If there is |
| 6638 | * nothing in the X11 event queue (& no timers pending), then we return |
| 6639 | * immediately. |
| 6640 | */ |
| 6641 | void |
| 6642 | gui_mch_update(void) |
| 6643 | { |
Bram Moolenaar | a3f4166 | 2010-07-11 19:01:06 +0200 | [diff] [blame] | 6644 | while (g_main_context_pending(NULL) && !vim_is_input_buf_full()) |
| 6645 | g_main_context_iteration(NULL, TRUE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6646 | } |
| 6647 | |
Bram Moolenaar | 4ab7968 | 2017-08-27 14:50:47 +0200 | [diff] [blame] | 6648 | static timeout_cb_type |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6649 | input_timer_cb(gpointer data) |
| 6650 | { |
| 6651 | int *timed_out = (int *) data; |
| 6652 | |
Bram Moolenaar | 4932594 | 2007-05-10 19:19:59 +0000 | [diff] [blame] | 6653 | /* Just inform the caller about the occurrence of it */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6654 | *timed_out = TRUE; |
| 6655 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6656 | return FALSE; /* don't happen again */ |
| 6657 | } |
| 6658 | |
Bram Moolenaar | 4ab7968 | 2017-08-27 14:50:47 +0200 | [diff] [blame] | 6659 | #ifdef FEAT_JOB_CHANNEL |
| 6660 | static timeout_cb_type |
| 6661 | channel_poll_cb(gpointer data UNUSED) |
| 6662 | { |
| 6663 | /* Using an event handler for a channel that may be disconnected does |
| 6664 | * not work, it hangs. Instead poll for messages. */ |
| 6665 | channel_handle_events(TRUE); |
| 6666 | parse_queued_messages(); |
| 6667 | |
| 6668 | return TRUE; /* repeat */ |
| 6669 | } |
| 6670 | #endif |
| 6671 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6672 | /* |
| 6673 | * GUI input routine called by gui_wait_for_chars(). Waits for a character |
| 6674 | * from the keyboard. |
| 6675 | * wtime == -1 Wait forever. |
| 6676 | * wtime == 0 This should never happen. |
| 6677 | * wtime > 0 Wait wtime milliseconds for a character. |
| 6678 | * Returns OK if a character was found to be available within the given time, |
| 6679 | * or FAIL otherwise. |
| 6680 | */ |
| 6681 | int |
| 6682 | gui_mch_wait_for_chars(long wtime) |
| 6683 | { |
Bram Moolenaar | 4231da4 | 2016-06-02 14:30:04 +0200 | [diff] [blame] | 6684 | int focus; |
| 6685 | guint timer; |
| 6686 | static int timed_out; |
| 6687 | int retval = FAIL; |
Bram Moolenaar | 4ab7968 | 2017-08-27 14:50:47 +0200 | [diff] [blame] | 6688 | #ifdef FEAT_JOB_CHANNEL |
| 6689 | guint channel_timer = 0; |
| 6690 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6691 | |
| 6692 | timed_out = FALSE; |
| 6693 | |
| 6694 | /* this timeout makes sure that we will return if no characters arrived in |
| 6695 | * time */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6696 | if (wtime > 0) |
Bram Moolenaar | 4ab7968 | 2017-08-27 14:50:47 +0200 | [diff] [blame] | 6697 | timer = timeout_add(wtime, input_timer_cb, &timed_out); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6698 | else |
| 6699 | timer = 0; |
| 6700 | |
Bram Moolenaar | 4ab7968 | 2017-08-27 14:50:47 +0200 | [diff] [blame] | 6701 | #ifdef FEAT_JOB_CHANNEL |
| 6702 | /* If there is a channel with the keep_open flag we need to poll for input |
| 6703 | * on them. */ |
| 6704 | if (channel_any_keep_open()) |
| 6705 | channel_timer = timeout_add(20, channel_poll_cb, NULL); |
| 6706 | #endif |
| 6707 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6708 | focus = gui.in_focus; |
| 6709 | |
| 6710 | do |
| 6711 | { |
| 6712 | /* Stop or start blinking when focus changes */ |
| 6713 | if (gui.in_focus != focus) |
| 6714 | { |
| 6715 | if (gui.in_focus) |
| 6716 | gui_mch_start_blink(); |
| 6717 | else |
Bram Moolenaar | 1dd45fb | 2018-01-31 21:10:01 +0100 | [diff] [blame] | 6718 | gui_mch_stop_blink(TRUE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6719 | focus = gui.in_focus; |
| 6720 | } |
| 6721 | |
Bram Moolenaar | 93c88e0 | 2015-09-15 14:12:05 +0200 | [diff] [blame] | 6722 | #ifdef MESSAGE_QUEUE |
Bram Moolenaar | 4231da4 | 2016-06-02 14:30:04 +0200 | [diff] [blame] | 6723 | # ifdef FEAT_TIMERS |
| 6724 | did_add_timer = FALSE; |
| 6725 | # endif |
Bram Moolenaar | 93c88e0 | 2015-09-15 14:12:05 +0200 | [diff] [blame] | 6726 | parse_queued_messages(); |
Bram Moolenaar | 4231da4 | 2016-06-02 14:30:04 +0200 | [diff] [blame] | 6727 | # ifdef FEAT_TIMERS |
| 6728 | if (did_add_timer) |
| 6729 | /* Need to recompute the waiting time. */ |
| 6730 | goto theend; |
| 6731 | # endif |
Bram Moolenaar | 8c8de83 | 2008-06-24 22:58:06 +0000 | [diff] [blame] | 6732 | #endif |
| 6733 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6734 | /* |
| 6735 | * Loop in GTK+ processing until a timeout or input occurs. |
Bram Moolenaar | d4755bb | 2004-09-02 19:12:26 +0000 | [diff] [blame] | 6736 | * Skip this if input is available anyway (can happen in rare |
| 6737 | * situations, sort of race condition). |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6738 | */ |
Bram Moolenaar | d4755bb | 2004-09-02 19:12:26 +0000 | [diff] [blame] | 6739 | if (!input_available()) |
Bram Moolenaar | a3f4166 | 2010-07-11 19:01:06 +0200 | [diff] [blame] | 6740 | g_main_context_iteration(NULL, TRUE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6741 | |
| 6742 | /* Got char, return immediately */ |
| 6743 | if (input_available()) |
| 6744 | { |
Bram Moolenaar | 4231da4 | 2016-06-02 14:30:04 +0200 | [diff] [blame] | 6745 | retval = OK; |
| 6746 | goto theend; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6747 | } |
| 6748 | } while (wtime < 0 || !timed_out); |
| 6749 | |
| 6750 | /* |
| 6751 | * Flush all eventually pending (drawing) events. |
| 6752 | */ |
| 6753 | gui_mch_update(); |
| 6754 | |
Bram Moolenaar | 4231da4 | 2016-06-02 14:30:04 +0200 | [diff] [blame] | 6755 | theend: |
| 6756 | if (timer != 0 && !timed_out) |
Bram Moolenaar | 4ab7968 | 2017-08-27 14:50:47 +0200 | [diff] [blame] | 6757 | timeout_remove(timer); |
| 6758 | #ifdef FEAT_JOB_CHANNEL |
| 6759 | if (channel_timer != 0) |
| 6760 | timeout_remove(channel_timer); |
Bram Moolenaar | 4231da4 | 2016-06-02 14:30:04 +0200 | [diff] [blame] | 6761 | #endif |
| 6762 | |
| 6763 | return retval; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6764 | } |
| 6765 | |
| 6766 | |
| 6767 | /**************************************************************************** |
| 6768 | * Output drawing routines. |
| 6769 | ****************************************************************************/ |
| 6770 | |
| 6771 | |
| 6772 | /* Flush any output to the screen */ |
| 6773 | void |
| 6774 | gui_mch_flush(void) |
| 6775 | { |
Bram Moolenaar | b463e8d | 2017-06-05 15:07:09 +0200 | [diff] [blame] | 6776 | #if GTK_CHECK_VERSION(3,0,0) |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 6777 | if (gui.mainwin != NULL && gtk_widget_get_realized(gui.mainwin)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6778 | #else |
Bram Moolenaar | b463e8d | 2017-06-05 15:07:09 +0200 | [diff] [blame] | 6779 | if (gui.mainwin != NULL && GTK_WIDGET_REALIZED(gui.mainwin)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6780 | #endif |
Bram Moolenaar | b463e8d | 2017-06-05 15:07:09 +0200 | [diff] [blame] | 6781 | gdk_display_flush(gtk_widget_get_display(gui.mainwin)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6782 | } |
| 6783 | |
| 6784 | /* |
| 6785 | * Clear a rectangular region of the screen from text pos (row1, col1) to |
| 6786 | * (row2, col2) inclusive. |
| 6787 | */ |
| 6788 | void |
Bram Moolenaar | fe344a9 | 2017-02-23 12:20:35 +0100 | [diff] [blame] | 6789 | gui_mch_clear_block(int row1arg, int col1arg, int row2arg, int col2arg) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6790 | { |
Bram Moolenaar | fe344a9 | 2017-02-23 12:20:35 +0100 | [diff] [blame] | 6791 | |
| 6792 | int col1 = check_col(col1arg); |
| 6793 | int col2 = check_col(col2arg); |
| 6794 | int row1 = check_row(row1arg); |
| 6795 | int row2 = check_row(row2arg); |
| 6796 | |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 6797 | #if GTK_CHECK_VERSION(3,0,0) |
| 6798 | if (gtk_widget_get_window(gui.drawarea) == NULL) |
| 6799 | return; |
| 6800 | #else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6801 | GdkColor color; |
| 6802 | |
| 6803 | if (gui.drawarea->window == NULL) |
| 6804 | return; |
| 6805 | |
| 6806 | color.pixel = gui.back_pixel; |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 6807 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6808 | |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 6809 | #if GTK_CHECK_VERSION(3,0,0) |
| 6810 | { |
| 6811 | /* Add one pixel to the far right column in case a double-stroked |
| 6812 | * bold glyph may sit there. */ |
| 6813 | const GdkRectangle rect = { |
| 6814 | FILL_X(col1), FILL_Y(row1), |
| 6815 | (col2 - col1 + 1) * gui.char_width + (col2 == Columns - 1), |
| 6816 | (row2 - row1 + 1) * gui.char_height |
| 6817 | }; |
| 6818 | GdkWindow * const win = gtk_widget_get_window(gui.drawarea); |
| 6819 | cairo_t * const cr = cairo_create(gui.surface); |
Bram Moolenaar | a859f04 | 2016-11-17 19:11:55 +0100 | [diff] [blame] | 6820 | # if GTK_CHECK_VERSION(3,22,2) |
| 6821 | set_cairo_source_rgba_from_color(cr, gui.back_pixel); |
| 6822 | # else |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 6823 | cairo_pattern_t * const pat = gdk_window_get_background_pattern(win); |
| 6824 | if (pat != NULL) |
| 6825 | cairo_set_source(cr, pat); |
| 6826 | else |
Bram Moolenaar | 36edf06 | 2016-07-21 22:10:12 +0200 | [diff] [blame] | 6827 | set_cairo_source_rgba_from_color(cr, gui.back_pixel); |
Bram Moolenaar | a859f04 | 2016-11-17 19:11:55 +0100 | [diff] [blame] | 6828 | # endif |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 6829 | gdk_cairo_rectangle(cr, &rect); |
| 6830 | cairo_fill(cr); |
| 6831 | cairo_destroy(cr); |
| 6832 | |
| 6833 | if (!gui.by_signal) |
| 6834 | gdk_window_invalidate_rect(win, &rect, FALSE); |
| 6835 | } |
| 6836 | #else /* !GTK_CHECK_VERSION(3,0,0) */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6837 | gdk_gc_set_foreground(gui.text_gc, &color); |
| 6838 | |
| 6839 | /* Clear one extra pixel at the far right, for when bold characters have |
| 6840 | * spilled over to the window border. */ |
| 6841 | gdk_draw_rectangle(gui.drawarea->window, gui.text_gc, TRUE, |
| 6842 | FILL_X(col1), FILL_Y(row1), |
| 6843 | (col2 - col1 + 1) * gui.char_width |
| 6844 | + (col2 == Columns - 1), |
| 6845 | (row2 - row1 + 1) * gui.char_height); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 6846 | #endif /* !GTK_CHECK_VERSION(3,0,0) */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6847 | } |
| 6848 | |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 6849 | #if GTK_CHECK_VERSION(3,0,0) |
| 6850 | static void |
| 6851 | gui_gtk_window_clear(GdkWindow *win) |
| 6852 | { |
| 6853 | const GdkRectangle rect = { |
| 6854 | 0, 0, gdk_window_get_width(win), gdk_window_get_height(win) |
| 6855 | }; |
| 6856 | cairo_t * const cr = cairo_create(gui.surface); |
Bram Moolenaar | a859f04 | 2016-11-17 19:11:55 +0100 | [diff] [blame] | 6857 | # if GTK_CHECK_VERSION(3,22,2) |
| 6858 | set_cairo_source_rgba_from_color(cr, gui.back_pixel); |
| 6859 | # else |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 6860 | cairo_pattern_t * const pat = gdk_window_get_background_pattern(win); |
| 6861 | if (pat != NULL) |
| 6862 | cairo_set_source(cr, pat); |
| 6863 | else |
Bram Moolenaar | 36edf06 | 2016-07-21 22:10:12 +0200 | [diff] [blame] | 6864 | set_cairo_source_rgba_from_color(cr, gui.back_pixel); |
Bram Moolenaar | a859f04 | 2016-11-17 19:11:55 +0100 | [diff] [blame] | 6865 | # endif |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 6866 | gdk_cairo_rectangle(cr, &rect); |
| 6867 | cairo_fill(cr); |
| 6868 | cairo_destroy(cr); |
| 6869 | |
| 6870 | if (!gui.by_signal) |
| 6871 | gdk_window_invalidate_rect(win, &rect, FALSE); |
| 6872 | } |
| 6873 | #endif |
| 6874 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6875 | void |
| 6876 | gui_mch_clear_all(void) |
| 6877 | { |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 6878 | #if GTK_CHECK_VERSION(3,0,0) |
| 6879 | if (gtk_widget_get_window(gui.drawarea) != NULL) |
| 6880 | gui_gtk_window_clear(gtk_widget_get_window(gui.drawarea)); |
| 6881 | #else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6882 | if (gui.drawarea->window != NULL) |
| 6883 | gdk_window_clear(gui.drawarea->window); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 6884 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6885 | } |
| 6886 | |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 6887 | #if !GTK_CHECK_VERSION(3,0,0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6888 | /* |
| 6889 | * Redraw any text revealed by scrolling up/down. |
| 6890 | */ |
| 6891 | static void |
| 6892 | check_copy_area(void) |
| 6893 | { |
| 6894 | GdkEvent *event; |
| 6895 | int expose_count; |
| 6896 | |
| 6897 | if (gui.visibility != GDK_VISIBILITY_PARTIAL) |
| 6898 | return; |
| 6899 | |
| 6900 | /* Avoid redrawing the cursor while scrolling or it'll end up where |
| 6901 | * we don't want it to be. I'm not sure if it's correct to call |
| 6902 | * gui_dont_update_cursor() at this point but it works as a quick |
| 6903 | * fix for now. */ |
Bram Moolenaar | 107abd2 | 2016-08-12 14:08:25 +0200 | [diff] [blame] | 6904 | gui_dont_update_cursor(TRUE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6905 | |
| 6906 | do |
| 6907 | { |
| 6908 | /* Wait to check whether the scroll worked or not. */ |
| 6909 | event = gdk_event_get_graphics_expose(gui.drawarea->window); |
| 6910 | |
| 6911 | if (event == NULL) |
| 6912 | break; /* received NoExpose event */ |
| 6913 | |
| 6914 | gui_redraw(event->expose.area.x, event->expose.area.y, |
| 6915 | event->expose.area.width, event->expose.area.height); |
| 6916 | |
| 6917 | expose_count = event->expose.count; |
| 6918 | gdk_event_free(event); |
| 6919 | } |
| 6920 | while (expose_count > 0); /* more events follow */ |
| 6921 | |
| 6922 | gui_can_update_cursor(); |
| 6923 | } |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 6924 | #endif /* !GTK_CHECK_VERSION(3,0,0) */ |
| 6925 | |
| 6926 | #if GTK_CHECK_VERSION(3,0,0) |
| 6927 | static void |
| 6928 | gui_gtk_surface_copy_rect(int dest_x, int dest_y, |
| 6929 | int src_x, int src_y, |
| 6930 | int width, int height) |
| 6931 | { |
| 6932 | cairo_t * const cr = cairo_create(gui.surface); |
| 6933 | |
| 6934 | cairo_rectangle(cr, dest_x, dest_y, width, height); |
| 6935 | cairo_clip(cr); |
| 6936 | cairo_push_group(cr); |
| 6937 | cairo_set_source_surface(cr, gui.surface, dest_x - src_x, dest_y - src_y); |
| 6938 | cairo_paint(cr); |
| 6939 | cairo_pop_group_to_source(cr); |
| 6940 | cairo_paint(cr); |
| 6941 | |
| 6942 | cairo_destroy(cr); |
| 6943 | } |
| 6944 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6945 | |
| 6946 | /* |
| 6947 | * Delete the given number of lines from the given row, scrolling up any |
| 6948 | * text further down within the scroll region. |
| 6949 | */ |
| 6950 | void |
| 6951 | gui_mch_delete_lines(int row, int num_lines) |
| 6952 | { |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 6953 | #if GTK_CHECK_VERSION(3,0,0) |
| 6954 | const int ncols = gui.scroll_region_right - gui.scroll_region_left + 1; |
| 6955 | const int nrows = gui.scroll_region_bot - row + 1; |
| 6956 | const int src_nrows = nrows - num_lines; |
| 6957 | |
| 6958 | gui_gtk_surface_copy_rect( |
| 6959 | FILL_X(gui.scroll_region_left), FILL_Y(row), |
| 6960 | FILL_X(gui.scroll_region_left), FILL_Y(row + num_lines), |
| 6961 | gui.char_width * ncols + 1, gui.char_height * src_nrows); |
| 6962 | gui_clear_block( |
| 6963 | gui.scroll_region_bot - num_lines + 1, gui.scroll_region_left, |
| 6964 | gui.scroll_region_bot, gui.scroll_region_right); |
| 6965 | gui_gtk3_redraw( |
| 6966 | FILL_X(gui.scroll_region_left), FILL_Y(row), |
| 6967 | gui.char_width * ncols + 1, gui.char_height * nrows); |
| 6968 | if (!gui.by_signal) |
| 6969 | gtk_widget_queue_draw_area(gui.drawarea, |
| 6970 | FILL_X(gui.scroll_region_left), FILL_Y(row), |
| 6971 | gui.char_width * ncols + 1, gui.char_height * nrows); |
| 6972 | #else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6973 | if (gui.visibility == GDK_VISIBILITY_FULLY_OBSCURED) |
| 6974 | return; /* Can't see the window */ |
| 6975 | |
| 6976 | gdk_gc_set_foreground(gui.text_gc, gui.fgcolor); |
| 6977 | gdk_gc_set_background(gui.text_gc, gui.bgcolor); |
| 6978 | |
| 6979 | /* copy one extra pixel, for when bold has spilled over */ |
| 6980 | gdk_window_copy_area(gui.drawarea->window, gui.text_gc, |
| 6981 | FILL_X(gui.scroll_region_left), FILL_Y(row), |
| 6982 | gui.drawarea->window, |
| 6983 | FILL_X(gui.scroll_region_left), |
| 6984 | FILL_Y(row + num_lines), |
| 6985 | gui.char_width * (gui.scroll_region_right |
| 6986 | - gui.scroll_region_left + 1) + 1, |
| 6987 | gui.char_height * (gui.scroll_region_bot - row - num_lines + 1)); |
| 6988 | |
| 6989 | gui_clear_block(gui.scroll_region_bot - num_lines + 1, |
| 6990 | gui.scroll_region_left, |
| 6991 | gui.scroll_region_bot, gui.scroll_region_right); |
| 6992 | check_copy_area(); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 6993 | #endif /* !GTK_CHECK_VERSION(3,0,0) */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6994 | } |
| 6995 | |
| 6996 | /* |
| 6997 | * Insert the given number of lines before the given row, scrolling down any |
| 6998 | * following text within the scroll region. |
| 6999 | */ |
| 7000 | void |
| 7001 | gui_mch_insert_lines(int row, int num_lines) |
| 7002 | { |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 7003 | #if GTK_CHECK_VERSION(3,0,0) |
| 7004 | const int ncols = gui.scroll_region_right - gui.scroll_region_left + 1; |
| 7005 | const int nrows = gui.scroll_region_bot - row + 1; |
| 7006 | const int src_nrows = nrows - num_lines; |
| 7007 | |
| 7008 | gui_gtk_surface_copy_rect( |
| 7009 | FILL_X(gui.scroll_region_left), FILL_Y(row + num_lines), |
| 7010 | FILL_X(gui.scroll_region_left), FILL_Y(row), |
| 7011 | gui.char_width * ncols + 1, gui.char_height * src_nrows); |
| 7012 | gui_mch_clear_block( |
| 7013 | row, gui.scroll_region_left, |
| 7014 | row + num_lines - 1, gui.scroll_region_right); |
| 7015 | gui_gtk3_redraw( |
| 7016 | FILL_X(gui.scroll_region_left), FILL_Y(row), |
| 7017 | gui.char_width * ncols + 1, gui.char_height * nrows); |
| 7018 | if (!gui.by_signal) |
| 7019 | gtk_widget_queue_draw_area(gui.drawarea, |
| 7020 | FILL_X(gui.scroll_region_left), FILL_Y(row), |
| 7021 | gui.char_width * ncols + 1, gui.char_height * nrows); |
| 7022 | #else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7023 | if (gui.visibility == GDK_VISIBILITY_FULLY_OBSCURED) |
| 7024 | return; /* Can't see the window */ |
| 7025 | |
| 7026 | gdk_gc_set_foreground(gui.text_gc, gui.fgcolor); |
| 7027 | gdk_gc_set_background(gui.text_gc, gui.bgcolor); |
| 7028 | |
| 7029 | /* copy one extra pixel, for when bold has spilled over */ |
| 7030 | gdk_window_copy_area(gui.drawarea->window, gui.text_gc, |
| 7031 | FILL_X(gui.scroll_region_left), FILL_Y(row + num_lines), |
| 7032 | gui.drawarea->window, |
| 7033 | FILL_X(gui.scroll_region_left), FILL_Y(row), |
| 7034 | gui.char_width * (gui.scroll_region_right |
| 7035 | - gui.scroll_region_left + 1) + 1, |
| 7036 | gui.char_height * (gui.scroll_region_bot - row - num_lines + 1)); |
| 7037 | |
| 7038 | gui_clear_block(row, gui.scroll_region_left, |
| 7039 | row + num_lines - 1, gui.scroll_region_right); |
| 7040 | check_copy_area(); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 7041 | #endif /* !GTK_CHECK_VERSION(3,0,0) */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7042 | } |
| 7043 | |
| 7044 | /* |
| 7045 | * X Selection stuff, for cutting and pasting text to other windows. |
| 7046 | */ |
| 7047 | void |
| 7048 | clip_mch_request_selection(VimClipboard *cbd) |
| 7049 | { |
| 7050 | GdkAtom target; |
| 7051 | unsigned i; |
Bram Moolenaar | 51b5ab9 | 2008-01-06 14:17:07 +0000 | [diff] [blame] | 7052 | time_t start; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7053 | |
| 7054 | for (i = 0; i < N_SELECTION_TARGETS; ++i) |
| 7055 | { |
Bram Moolenaar | 3a6eaa5 | 2009-06-16 13:23:06 +0000 | [diff] [blame] | 7056 | if (!clip_html && selection_targets[i].info == TARGET_HTML) |
| 7057 | continue; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7058 | received_selection = RS_NONE; |
| 7059 | target = gdk_atom_intern(selection_targets[i].target, FALSE); |
| 7060 | |
| 7061 | gtk_selection_convert(gui.drawarea, |
| 7062 | cbd->gtk_sel_atom, target, |
| 7063 | (guint32)GDK_CURRENT_TIME); |
| 7064 | |
Bram Moolenaar | 51b5ab9 | 2008-01-06 14:17:07 +0000 | [diff] [blame] | 7065 | /* Hack: Wait up to three seconds for the selection. A hang was |
| 7066 | * noticed here when using the netrw plugin combined with ":gui" |
| 7067 | * during the FocusGained event. */ |
| 7068 | start = time(NULL); |
| 7069 | while (received_selection == RS_NONE && time(NULL) < start + 3) |
Bram Moolenaar | a3f4166 | 2010-07-11 19:01:06 +0200 | [diff] [blame] | 7070 | g_main_context_iteration(NULL, TRUE); /* wait for selection_received_cb */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7071 | |
| 7072 | if (received_selection != RS_FAIL) |
| 7073 | return; |
| 7074 | } |
| 7075 | |
| 7076 | /* Final fallback position - use the X CUT_BUFFER0 store */ |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 7077 | #if GTK_CHECK_VERSION(3,0,0) |
| 7078 | yank_cut_buffer0(GDK_WINDOW_XDISPLAY(gtk_widget_get_window(gui.mainwin)), |
| 7079 | cbd); |
| 7080 | #else |
Bram Moolenaar | bbc936b | 2009-07-01 16:04:58 +0000 | [diff] [blame] | 7081 | yank_cut_buffer0(GDK_WINDOW_XDISPLAY(gui.mainwin->window), cbd); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 7082 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7083 | } |
| 7084 | |
| 7085 | /* |
| 7086 | * Disown the selection. |
| 7087 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7088 | void |
Bram Moolenaar | b85cb21 | 2009-05-17 14:24:23 +0000 | [diff] [blame] | 7089 | clip_mch_lose_selection(VimClipboard *cbd UNUSED) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7090 | { |
Bram Moolenaar | 29dfa5a | 2018-03-20 21:24:45 +0100 | [diff] [blame] | 7091 | if (!in_selection_clear_event) |
| 7092 | { |
| 7093 | gtk_selection_owner_set(NULL, cbd->gtk_sel_atom, gui.event_time); |
| 7094 | gui_mch_update(); |
| 7095 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7096 | } |
| 7097 | |
| 7098 | /* |
| 7099 | * Own the selection and return OK if it worked. |
| 7100 | */ |
| 7101 | int |
| 7102 | clip_mch_own_selection(VimClipboard *cbd) |
| 7103 | { |
| 7104 | int success; |
| 7105 | |
| 7106 | success = gtk_selection_owner_set(gui.drawarea, cbd->gtk_sel_atom, |
Bram Moolenaar | 20892c1 | 2011-06-26 04:49:00 +0200 | [diff] [blame] | 7107 | gui.event_time); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7108 | gui_mch_update(); |
| 7109 | return (success) ? OK : FAIL; |
| 7110 | } |
| 7111 | |
| 7112 | /* |
| 7113 | * Send the current selection to the clipboard. Do nothing for X because we |
| 7114 | * will fill in the selection only when requested by another app. |
| 7115 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7116 | void |
Bram Moolenaar | b85cb21 | 2009-05-17 14:24:23 +0000 | [diff] [blame] | 7117 | clip_mch_set_selection(VimClipboard *cbd UNUSED) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7118 | { |
| 7119 | } |
| 7120 | |
Bram Moolenaar | 1a0316c | 2013-03-13 17:50:25 +0100 | [diff] [blame] | 7121 | int |
| 7122 | clip_gtk_owner_exists(VimClipboard *cbd) |
| 7123 | { |
| 7124 | return gdk_selection_owner_get(cbd->gtk_sel_atom) != NULL; |
| 7125 | } |
| 7126 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7127 | |
| 7128 | #if defined(FEAT_MENU) || defined(PROTO) |
| 7129 | /* |
| 7130 | * Make a menu item appear either active or not active (grey or not grey). |
| 7131 | */ |
| 7132 | void |
| 7133 | gui_mch_menu_grey(vimmenu_T *menu, int grey) |
| 7134 | { |
| 7135 | if (menu->id == NULL) |
| 7136 | return; |
| 7137 | |
| 7138 | if (menu_is_separator(menu->name)) |
| 7139 | grey = TRUE; |
| 7140 | |
| 7141 | gui_mch_menu_hidden(menu, FALSE); |
| 7142 | /* Be clever about bitfields versus true booleans here! */ |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 7143 | # if GTK_CHECK_VERSION(3,0,0) |
| 7144 | if (!gtk_widget_get_sensitive(menu->id) == !grey) |
| 7145 | # else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7146 | if (!GTK_WIDGET_SENSITIVE(menu->id) == !grey) |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 7147 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7148 | { |
| 7149 | gtk_widget_set_sensitive(menu->id, !grey); |
| 7150 | gui_mch_update(); |
| 7151 | } |
| 7152 | } |
| 7153 | |
| 7154 | /* |
| 7155 | * Make menu item hidden or not hidden. |
| 7156 | */ |
| 7157 | void |
| 7158 | gui_mch_menu_hidden(vimmenu_T *menu, int hidden) |
| 7159 | { |
| 7160 | if (menu->id == 0) |
| 7161 | return; |
| 7162 | |
| 7163 | if (hidden) |
| 7164 | { |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 7165 | # if GTK_CHECK_VERSION(3,0,0) |
| 7166 | if (gtk_widget_get_visible(menu->id)) |
| 7167 | # else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7168 | if (GTK_WIDGET_VISIBLE(menu->id)) |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 7169 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7170 | { |
| 7171 | gtk_widget_hide(menu->id); |
| 7172 | gui_mch_update(); |
| 7173 | } |
| 7174 | } |
| 7175 | else |
| 7176 | { |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 7177 | # if GTK_CHECK_VERSION(3,0,0) |
| 7178 | if (!gtk_widget_get_visible(menu->id)) |
| 7179 | # else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7180 | if (!GTK_WIDGET_VISIBLE(menu->id)) |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 7181 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7182 | { |
| 7183 | gtk_widget_show(menu->id); |
| 7184 | gui_mch_update(); |
| 7185 | } |
| 7186 | } |
| 7187 | } |
| 7188 | |
| 7189 | /* |
| 7190 | * This is called after setting all the menus to grey/hidden or not. |
| 7191 | */ |
| 7192 | void |
| 7193 | gui_mch_draw_menubar(void) |
| 7194 | { |
| 7195 | /* just make sure that the visual changes get effect immediately */ |
| 7196 | gui_mch_update(); |
| 7197 | } |
| 7198 | #endif /* FEAT_MENU */ |
| 7199 | |
| 7200 | /* |
| 7201 | * Scrollbar stuff. |
| 7202 | */ |
| 7203 | void |
| 7204 | gui_mch_enable_scrollbar(scrollbar_T *sb, int flag) |
| 7205 | { |
| 7206 | if (sb->id == NULL) |
| 7207 | return; |
| 7208 | |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 7209 | #if GTK_CHECK_VERSION(3,0,0) |
| 7210 | gtk_widget_set_visible(sb->id, flag); |
| 7211 | #else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7212 | if (flag) |
| 7213 | gtk_widget_show(sb->id); |
| 7214 | else |
| 7215 | gtk_widget_hide(sb->id); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 7216 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7217 | |
Bram Moolenaar | b3656ed | 2006-03-20 21:59:49 +0000 | [diff] [blame] | 7218 | update_window_manager_hints(0, 0); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7219 | } |
| 7220 | |
| 7221 | |
| 7222 | /* |
| 7223 | * Return the RGB value of a pixel as long. |
| 7224 | */ |
Bram Moolenaar | 1b58cdd | 2016-08-22 23:04:33 +0200 | [diff] [blame] | 7225 | guicolor_T |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7226 | gui_mch_get_rgb(guicolor_T pixel) |
| 7227 | { |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 7228 | #if GTK_CHECK_VERSION(3,0,0) |
Bram Moolenaar | 36edf06 | 2016-07-21 22:10:12 +0200 | [diff] [blame] | 7229 | return (long_u)pixel; |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 7230 | #else |
Bram Moolenaar | 36edf06 | 2016-07-21 22:10:12 +0200 | [diff] [blame] | 7231 | GdkColor color; |
| 7232 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7233 | gdk_colormap_query_color(gtk_widget_get_colormap(gui.drawarea), |
| 7234 | (unsigned long)pixel, &color); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7235 | |
Bram Moolenaar | 1b58cdd | 2016-08-22 23:04:33 +0200 | [diff] [blame] | 7236 | return (guicolor_T)( |
| 7237 | (((unsigned)color.red & 0xff00) << 8) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7238 | | ((unsigned)color.green & 0xff00) |
Bram Moolenaar | 1b58cdd | 2016-08-22 23:04:33 +0200 | [diff] [blame] | 7239 | | (((unsigned)color.blue & 0xff00) >> 8)); |
Bram Moolenaar | 36edf06 | 2016-07-21 22:10:12 +0200 | [diff] [blame] | 7240 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7241 | } |
| 7242 | |
| 7243 | /* |
Bram Moolenaar | 6cc1619 | 2005-01-08 21:49:45 +0000 | [diff] [blame] | 7244 | * Get current mouse coordinates in text window. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7245 | */ |
Bram Moolenaar | 6cc1619 | 2005-01-08 21:49:45 +0000 | [diff] [blame] | 7246 | void |
| 7247 | gui_mch_getmouse(int *x, int *y) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7248 | { |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 7249 | #if GTK_CHECK_VERSION(3,0,0) |
| 7250 | gui_gtk_get_pointer(gui.drawarea, x, y, NULL); |
| 7251 | #else |
Bram Moolenaar | 6cc1619 | 2005-01-08 21:49:45 +0000 | [diff] [blame] | 7252 | gdk_window_get_pointer(gui.drawarea->window, x, y, NULL); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 7253 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7254 | } |
| 7255 | |
| 7256 | void |
| 7257 | gui_mch_setmouse(int x, int y) |
| 7258 | { |
| 7259 | /* Sorry for the Xlib call, but we can't avoid it, since there is no |
| 7260 | * internal GDK mechanism present to accomplish this. (and for good |
| 7261 | * reason...) */ |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 7262 | #if GTK_CHECK_VERSION(3,0,0) |
| 7263 | XWarpPointer(GDK_WINDOW_XDISPLAY(gtk_widget_get_window(gui.drawarea)), |
| 7264 | (Window)0, GDK_WINDOW_XID(gtk_widget_get_window(gui.drawarea)), |
| 7265 | 0, 0, 0U, 0U, x, y); |
| 7266 | #else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7267 | XWarpPointer(GDK_WINDOW_XDISPLAY(gui.drawarea->window), |
| 7268 | (Window)0, GDK_WINDOW_XWINDOW(gui.drawarea->window), |
| 7269 | 0, 0, 0U, 0U, x, y); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 7270 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7271 | } |
| 7272 | |
| 7273 | |
| 7274 | #ifdef FEAT_MOUSESHAPE |
| 7275 | /* The last set mouse pointer shape is remembered, to be used when it goes |
| 7276 | * from hidden to not hidden. */ |
| 7277 | static int last_shape = 0; |
| 7278 | #endif |
| 7279 | |
| 7280 | /* |
| 7281 | * Use the blank mouse pointer or not. |
| 7282 | * |
| 7283 | * hide: TRUE = use blank ptr, FALSE = use parent ptr |
| 7284 | */ |
| 7285 | void |
| 7286 | gui_mch_mousehide(int hide) |
| 7287 | { |
| 7288 | if (gui.pointer_hidden != hide) |
| 7289 | { |
| 7290 | gui.pointer_hidden = hide; |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 7291 | #if GTK_CHECK_VERSION(3,0,0) |
| 7292 | if (gtk_widget_get_window(gui.drawarea) && gui.blank_pointer != NULL) |
| 7293 | #else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7294 | if (gui.drawarea->window && gui.blank_pointer != NULL) |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 7295 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7296 | { |
| 7297 | if (hide) |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 7298 | #if GTK_CHECK_VERSION(3,0,0) |
| 7299 | gdk_window_set_cursor(gtk_widget_get_window(gui.drawarea), |
| 7300 | gui.blank_pointer); |
| 7301 | #else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7302 | gdk_window_set_cursor(gui.drawarea->window, gui.blank_pointer); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 7303 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7304 | else |
| 7305 | #ifdef FEAT_MOUSESHAPE |
| 7306 | mch_set_mouse_shape(last_shape); |
Bram Moolenaar | 3f2a5d8 | 2016-02-27 22:08:16 +0100 | [diff] [blame] | 7307 | #elif GTK_CHECK_VERSION(3,0,0) |
| 7308 | gdk_window_set_cursor(gtk_widget_get_window(gui.drawarea), NULL); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7309 | #else |
| 7310 | gdk_window_set_cursor(gui.drawarea->window, NULL); |
| 7311 | #endif |
| 7312 | } |
| 7313 | } |
| 7314 | } |
| 7315 | |
| 7316 | #if defined(FEAT_MOUSESHAPE) || defined(PROTO) |
| 7317 | |
| 7318 | /* Table for shape IDs. Keep in sync with the mshape_names[] table in |
| 7319 | * misc2.c! */ |
| 7320 | static const int mshape_ids[] = |
| 7321 | { |
| 7322 | GDK_LEFT_PTR, /* arrow */ |
| 7323 | GDK_CURSOR_IS_PIXMAP, /* blank */ |
| 7324 | GDK_XTERM, /* beam */ |
| 7325 | GDK_SB_V_DOUBLE_ARROW, /* updown */ |
| 7326 | GDK_SIZING, /* udsizing */ |
| 7327 | GDK_SB_H_DOUBLE_ARROW, /* leftright */ |
| 7328 | GDK_SIZING, /* lrsizing */ |
| 7329 | GDK_WATCH, /* busy */ |
| 7330 | GDK_X_CURSOR, /* no */ |
| 7331 | GDK_CROSSHAIR, /* crosshair */ |
| 7332 | GDK_HAND1, /* hand1 */ |
| 7333 | GDK_HAND2, /* hand2 */ |
| 7334 | GDK_PENCIL, /* pencil */ |
| 7335 | GDK_QUESTION_ARROW, /* question */ |
| 7336 | GDK_RIGHT_PTR, /* right-arrow */ |
| 7337 | GDK_CENTER_PTR, /* up-arrow */ |
| 7338 | GDK_LEFT_PTR /* last one */ |
| 7339 | }; |
| 7340 | |
| 7341 | void |
| 7342 | mch_set_mouse_shape(int shape) |
| 7343 | { |
| 7344 | int id; |
| 7345 | GdkCursor *c; |
| 7346 | |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 7347 | # if GTK_CHECK_VERSION(3,0,0) |
| 7348 | if (gtk_widget_get_window(gui.drawarea) == NULL) |
| 7349 | # else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7350 | if (gui.drawarea->window == NULL) |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 7351 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7352 | return; |
| 7353 | |
| 7354 | if (shape == MSHAPE_HIDE || gui.pointer_hidden) |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 7355 | # if GTK_CHECK_VERSION(3,0,0) |
| 7356 | gdk_window_set_cursor(gtk_widget_get_window(gui.drawarea), |
| 7357 | gui.blank_pointer); |
| 7358 | # else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7359 | gdk_window_set_cursor(gui.drawarea->window, gui.blank_pointer); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 7360 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7361 | else |
| 7362 | { |
| 7363 | if (shape >= MSHAPE_NUMBERED) |
| 7364 | { |
| 7365 | id = shape - MSHAPE_NUMBERED; |
| 7366 | if (id >= GDK_LAST_CURSOR) |
| 7367 | id = GDK_LEFT_PTR; |
| 7368 | else |
| 7369 | id &= ~1; /* they are always even (why?) */ |
| 7370 | } |
Bram Moolenaar | b85cb21 | 2009-05-17 14:24:23 +0000 | [diff] [blame] | 7371 | else if (shape < (int)(sizeof(mshape_ids) / sizeof(int))) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7372 | id = mshape_ids[shape]; |
Bram Moolenaar | f9393ef | 2006-04-24 19:47:27 +0000 | [diff] [blame] | 7373 | else |
| 7374 | return; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7375 | c = gdk_cursor_new_for_display( |
Bram Moolenaar | b71ec9f | 2005-01-25 22:22:02 +0000 | [diff] [blame] | 7376 | gtk_widget_get_display(gui.drawarea), (GdkCursorType)id); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 7377 | # if GTK_CHECK_VERSION(3,0,0) |
| 7378 | gdk_window_set_cursor(gtk_widget_get_window(gui.drawarea), c); |
| 7379 | # else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7380 | gdk_window_set_cursor(gui.drawarea->window, c); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 7381 | # endif |
| 7382 | # if GTK_CHECK_VERSION(3,0,0) |
| 7383 | g_object_unref(G_OBJECT(c)); |
| 7384 | # else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7385 | gdk_cursor_destroy(c); /* Unref, actually. Bloody GTK+ 1. */ |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 7386 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7387 | } |
| 7388 | if (shape != MSHAPE_HIDE) |
| 7389 | last_shape = shape; |
| 7390 | } |
| 7391 | #endif /* FEAT_MOUSESHAPE */ |
| 7392 | |
| 7393 | |
| 7394 | #if defined(FEAT_SIGN_ICONS) || defined(PROTO) |
| 7395 | /* |
| 7396 | * Signs are currently always 2 chars wide. With GTK+ 2, the image will be |
| 7397 | * scaled down if the current font is not big enough, or scaled up if the image |
| 7398 | * size is less than 3/4 of the maximum sign size. With GTK+ 1, the pixmap |
| 7399 | * will be cut off if the current font is not big enough, or centered if it's |
| 7400 | * too small. |
| 7401 | */ |
| 7402 | # define SIGN_WIDTH (2 * gui.char_width) |
| 7403 | # define SIGN_HEIGHT (gui.char_height) |
| 7404 | # define SIGN_ASPECT ((double)SIGN_HEIGHT / (double)SIGN_WIDTH) |
| 7405 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7406 | void |
| 7407 | gui_mch_drawsign(int row, int col, int typenr) |
| 7408 | { |
| 7409 | GdkPixbuf *sign; |
| 7410 | |
| 7411 | sign = (GdkPixbuf *)sign_get_image(typenr); |
| 7412 | |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 7413 | # if GTK_CHECK_VERSION(3,0,0) |
| 7414 | if (sign != NULL && gui.drawarea != NULL |
| 7415 | && gtk_widget_get_window(gui.drawarea) != NULL) |
| 7416 | # else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7417 | if (sign != NULL && gui.drawarea != NULL && gui.drawarea->window != NULL) |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 7418 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7419 | { |
| 7420 | int width; |
| 7421 | int height; |
| 7422 | int xoffset; |
| 7423 | int yoffset; |
| 7424 | int need_scale; |
| 7425 | |
| 7426 | width = gdk_pixbuf_get_width(sign); |
| 7427 | height = gdk_pixbuf_get_height(sign); |
| 7428 | /* |
| 7429 | * Decide whether we need to scale. Allow one pixel of border |
| 7430 | * width to be cut off, in order to avoid excessive scaling for |
| 7431 | * tiny differences in font size. |
Bram Moolenaar | 58cbc91 | 2014-06-17 18:47:02 +0200 | [diff] [blame] | 7432 | * Do scale to fit the height to avoid gaps because of linespacing. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7433 | */ |
| 7434 | need_scale = (width > SIGN_WIDTH + 2 |
Bram Moolenaar | 58cbc91 | 2014-06-17 18:47:02 +0200 | [diff] [blame] | 7435 | || height != SIGN_HEIGHT |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7436 | || (width < 3 * SIGN_WIDTH / 4 |
| 7437 | && height < 3 * SIGN_HEIGHT / 4)); |
| 7438 | if (need_scale) |
| 7439 | { |
Bram Moolenaar | 58cbc91 | 2014-06-17 18:47:02 +0200 | [diff] [blame] | 7440 | double aspect; |
| 7441 | int w = width; |
| 7442 | int h = height; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7443 | |
| 7444 | /* Keep the original aspect ratio */ |
| 7445 | aspect = (double)height / (double)width; |
| 7446 | width = (double)SIGN_WIDTH * SIGN_ASPECT / aspect; |
| 7447 | width = MIN(width, SIGN_WIDTH); |
Bram Moolenaar | 58cbc91 | 2014-06-17 18:47:02 +0200 | [diff] [blame] | 7448 | if (((double)(MAX(height, SIGN_HEIGHT)) / |
| 7449 | (double)(MIN(height, SIGN_HEIGHT))) < 1.15) |
| 7450 | { |
| 7451 | /* Change the aspect ratio by at most 15% to fill the |
| 7452 | * available space completly. */ |
| 7453 | height = (double)SIGN_HEIGHT * SIGN_ASPECT / aspect; |
| 7454 | height = MIN(height, SIGN_HEIGHT); |
| 7455 | } |
| 7456 | else |
| 7457 | height = (double)width * aspect; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7458 | |
Bram Moolenaar | 58cbc91 | 2014-06-17 18:47:02 +0200 | [diff] [blame] | 7459 | if (w == width && h == height) |
| 7460 | { |
| 7461 | /* no change in dimensions; don't decrease reference counter |
| 7462 | * (below) */ |
| 7463 | need_scale = FALSE; |
| 7464 | } |
| 7465 | else |
| 7466 | { |
| 7467 | /* This doesn't seem to be worth caching, and doing so would |
| 7468 | * complicate the code quite a bit. */ |
| 7469 | sign = gdk_pixbuf_scale_simple(sign, width, height, |
| 7470 | GDK_INTERP_BILINEAR); |
| 7471 | if (sign == NULL) |
| 7472 | return; /* out of memory */ |
| 7473 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7474 | } |
| 7475 | |
| 7476 | /* The origin is the upper-left corner of the pixmap. Therefore |
| 7477 | * these offset may become negative if the pixmap is smaller than |
| 7478 | * the 2x1 cells reserved for the sign icon. */ |
| 7479 | xoffset = (width - SIGN_WIDTH) / 2; |
| 7480 | yoffset = (height - SIGN_HEIGHT) / 2; |
| 7481 | |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 7482 | # if GTK_CHECK_VERSION(3,0,0) |
| 7483 | { |
| 7484 | cairo_t *cr; |
| 7485 | cairo_surface_t *bg_surf; |
| 7486 | cairo_t *bg_cr; |
| 7487 | cairo_surface_t *sign_surf; |
| 7488 | cairo_t *sign_cr; |
| 7489 | |
| 7490 | cr = cairo_create(gui.surface); |
| 7491 | |
| 7492 | bg_surf = cairo_surface_create_similar(gui.surface, |
| 7493 | cairo_surface_get_content(gui.surface), |
| 7494 | SIGN_WIDTH, SIGN_HEIGHT); |
| 7495 | bg_cr = cairo_create(bg_surf); |
Bram Moolenaar | 36edf06 | 2016-07-21 22:10:12 +0200 | [diff] [blame] | 7496 | cairo_set_source_rgba(bg_cr, |
| 7497 | gui.bgcolor->red, gui.bgcolor->green, gui.bgcolor->blue, |
| 7498 | gui.bgcolor->alpha); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 7499 | cairo_paint(bg_cr); |
| 7500 | |
| 7501 | sign_surf = cairo_surface_create_similar(gui.surface, |
| 7502 | cairo_surface_get_content(gui.surface), |
| 7503 | SIGN_WIDTH, SIGN_HEIGHT); |
| 7504 | sign_cr = cairo_create(sign_surf); |
| 7505 | gdk_cairo_set_source_pixbuf(sign_cr, sign, -xoffset, -yoffset); |
| 7506 | cairo_paint(sign_cr); |
| 7507 | |
| 7508 | cairo_set_operator(sign_cr, CAIRO_OPERATOR_DEST_OVER); |
| 7509 | cairo_set_source_surface(sign_cr, bg_surf, 0, 0); |
| 7510 | cairo_paint(sign_cr); |
| 7511 | |
| 7512 | cairo_set_source_surface(cr, sign_surf, FILL_X(col), FILL_Y(row)); |
| 7513 | cairo_paint(cr); |
| 7514 | |
| 7515 | cairo_destroy(sign_cr); |
| 7516 | cairo_surface_destroy(sign_surf); |
| 7517 | cairo_destroy(bg_cr); |
| 7518 | cairo_surface_destroy(bg_surf); |
| 7519 | cairo_destroy(cr); |
| 7520 | |
| 7521 | if (!gui.by_signal) |
| 7522 | gtk_widget_queue_draw_area(gui.drawarea, |
| 7523 | FILL_X(col), FILL_Y(col), width, height); |
| 7524 | |
| 7525 | } |
| 7526 | # else /* !GTK_CHECK_VERSION(3,0,0) */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7527 | gdk_gc_set_foreground(gui.text_gc, gui.bgcolor); |
| 7528 | |
| 7529 | gdk_draw_rectangle(gui.drawarea->window, |
| 7530 | gui.text_gc, |
| 7531 | TRUE, |
| 7532 | FILL_X(col), |
| 7533 | FILL_Y(row), |
| 7534 | SIGN_WIDTH, |
| 7535 | SIGN_HEIGHT); |
| 7536 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7537 | gdk_pixbuf_render_to_drawable_alpha(sign, |
| 7538 | gui.drawarea->window, |
| 7539 | MAX(0, xoffset), |
| 7540 | MAX(0, yoffset), |
| 7541 | FILL_X(col) - MIN(0, xoffset), |
| 7542 | FILL_Y(row) - MIN(0, yoffset), |
| 7543 | MIN(width, SIGN_WIDTH), |
| 7544 | MIN(height, SIGN_HEIGHT), |
| 7545 | GDK_PIXBUF_ALPHA_BILEVEL, |
| 7546 | 127, |
| 7547 | GDK_RGB_DITHER_NORMAL, |
| 7548 | 0, 0); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 7549 | # endif /* !GTK_CHECK_VERSION(3,0,0) */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7550 | if (need_scale) |
| 7551 | g_object_unref(sign); |
| 7552 | } |
| 7553 | } |
| 7554 | |
| 7555 | void * |
| 7556 | gui_mch_register_sign(char_u *signfile) |
| 7557 | { |
| 7558 | if (signfile[0] != NUL && signfile[0] != '-' && gui.in_use) |
| 7559 | { |
| 7560 | GdkPixbuf *sign; |
| 7561 | GError *error = NULL; |
| 7562 | char_u *message; |
| 7563 | |
| 7564 | sign = gdk_pixbuf_new_from_file((const char *)signfile, &error); |
| 7565 | |
| 7566 | if (error == NULL) |
| 7567 | return sign; |
| 7568 | |
| 7569 | message = (char_u *)error->message; |
| 7570 | |
| 7571 | if (message != NULL && input_conv.vc_type != CONV_NONE) |
| 7572 | message = string_convert(&input_conv, message, NULL); |
| 7573 | |
| 7574 | if (message != NULL) |
| 7575 | { |
| 7576 | /* The error message is already translated and will be more |
| 7577 | * descriptive than anything we could possibly do ourselves. */ |
| 7578 | EMSG2("E255: %s", message); |
| 7579 | |
| 7580 | if (input_conv.vc_type != CONV_NONE) |
| 7581 | vim_free(message); |
| 7582 | } |
| 7583 | g_error_free(error); |
| 7584 | } |
| 7585 | |
| 7586 | return NULL; |
| 7587 | } |
| 7588 | |
| 7589 | void |
| 7590 | gui_mch_destroy_sign(void *sign) |
| 7591 | { |
| 7592 | if (sign != NULL) |
| 7593 | g_object_unref(sign); |
| 7594 | } |
| 7595 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7596 | #endif /* FEAT_SIGN_ICONS */ |