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 | |
| 791 | |
Bram Moolenaar | 4ab7968 | 2017-08-27 14:50:47 +0200 | [diff] [blame] | 792 | #if GTK_CHECK_VERSION(3,0,0) |
| 793 | typedef gboolean timeout_cb_type; |
| 794 | #else |
| 795 | typedef gint timeout_cb_type; |
| 796 | #endif |
| 797 | |
| 798 | /* |
| 799 | * Start a timer that will invoke the specified callback. |
| 800 | * Returns the ID of the timer. |
| 801 | */ |
| 802 | static guint |
| 803 | timeout_add(int time, timeout_cb_type (*callback)(gpointer), int *flagp) |
| 804 | { |
| 805 | #if GTK_CHECK_VERSION(3,0,0) |
| 806 | return g_timeout_add((guint)time, (GSourceFunc)callback, flagp); |
| 807 | #else |
| 808 | return gtk_timeout_add((guint32)time, (GtkFunction)callback, flagp); |
| 809 | #endif |
| 810 | } |
| 811 | |
| 812 | static void |
| 813 | timeout_remove(guint timer) |
| 814 | { |
| 815 | #if GTK_CHECK_VERSION(3,0,0) |
| 816 | g_source_remove(timer); |
| 817 | #else |
| 818 | gtk_timeout_remove(timer); |
| 819 | #endif |
| 820 | } |
| 821 | |
| 822 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 823 | /**************************************************************************** |
| 824 | * Focus handlers: |
| 825 | */ |
| 826 | |
| 827 | |
| 828 | /* |
| 829 | * This is a simple state machine: |
| 830 | * BLINK_NONE not blinking at all |
| 831 | * BLINK_OFF blinking, cursor is not shown |
| 832 | * BLINK_ON blinking, cursor is shown |
| 833 | */ |
| 834 | |
| 835 | #define BLINK_NONE 0 |
| 836 | #define BLINK_OFF 1 |
| 837 | #define BLINK_ON 2 |
| 838 | |
| 839 | static int blink_state = BLINK_NONE; |
| 840 | static long_u blink_waittime = 700; |
| 841 | static long_u blink_ontime = 400; |
| 842 | static long_u blink_offtime = 250; |
| 843 | static guint blink_timer = 0; |
| 844 | |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 845 | #if GTK_CHECK_VERSION(3,0,0) |
| 846 | static gboolean |
| 847 | gui_gtk_is_blink_on(void) |
| 848 | { |
| 849 | return blink_state == BLINK_ON; |
| 850 | } |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 851 | #endif |
| 852 | |
Bram Moolenaar | 703a804 | 2016-06-04 16:24:32 +0200 | [diff] [blame] | 853 | int |
| 854 | gui_mch_is_blinking(void) |
| 855 | { |
| 856 | return blink_state != BLINK_NONE; |
| 857 | } |
| 858 | |
Bram Moolenaar | 9d5d3c9 | 2016-07-07 16:43:02 +0200 | [diff] [blame] | 859 | int |
| 860 | gui_mch_is_blink_off(void) |
| 861 | { |
| 862 | return blink_state == BLINK_OFF; |
| 863 | } |
| 864 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 865 | void |
| 866 | gui_mch_set_blinking(long waittime, long on, long off) |
| 867 | { |
Bram Moolenaar | 0ecbe33 | 2016-03-05 22:40:52 +0100 | [diff] [blame] | 868 | #if GTK_CHECK_VERSION(3,0,0) |
| 869 | if (waittime == 0 || on == 0 || off == 0) |
| 870 | { |
| 871 | blink_mode = FALSE; |
| 872 | |
| 873 | blink_waittime = 700; |
| 874 | blink_ontime = 400; |
| 875 | blink_offtime = 250; |
| 876 | } |
| 877 | else |
| 878 | { |
| 879 | blink_mode = TRUE; |
| 880 | |
| 881 | blink_waittime = waittime; |
| 882 | blink_ontime = on; |
| 883 | blink_offtime = off; |
| 884 | } |
| 885 | #else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 886 | blink_waittime = waittime; |
| 887 | blink_ontime = on; |
| 888 | blink_offtime = off; |
Bram Moolenaar | 0ecbe33 | 2016-03-05 22:40:52 +0100 | [diff] [blame] | 889 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 890 | } |
| 891 | |
| 892 | /* |
| 893 | * Stop the cursor blinking. Show the cursor if it wasn't shown. |
| 894 | */ |
| 895 | void |
| 896 | gui_mch_stop_blink(void) |
| 897 | { |
| 898 | if (blink_timer) |
| 899 | { |
Bram Moolenaar | 4ab7968 | 2017-08-27 14:50:47 +0200 | [diff] [blame] | 900 | timeout_remove(blink_timer); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 901 | blink_timer = 0; |
| 902 | } |
| 903 | if (blink_state == BLINK_OFF) |
Bram Moolenaar | da3a77d | 2016-07-10 23:16:09 +0200 | [diff] [blame] | 904 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 905 | gui_update_cursor(TRUE, FALSE); |
Bram Moolenaar | da3a77d | 2016-07-10 23:16:09 +0200 | [diff] [blame] | 906 | gui_mch_flush(); |
| 907 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 908 | blink_state = BLINK_NONE; |
| 909 | } |
| 910 | |
Bram Moolenaar | 4ab7968 | 2017-08-27 14:50:47 +0200 | [diff] [blame] | 911 | static timeout_cb_type |
Bram Moolenaar | b85cb21 | 2009-05-17 14:24:23 +0000 | [diff] [blame] | 912 | blink_cb(gpointer data UNUSED) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 913 | { |
| 914 | if (blink_state == BLINK_ON) |
| 915 | { |
| 916 | gui_undraw_cursor(); |
| 917 | blink_state = BLINK_OFF; |
Bram Moolenaar | 4ab7968 | 2017-08-27 14:50:47 +0200 | [diff] [blame] | 918 | blink_timer = timeout_add(blink_offtime, blink_cb, NULL); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 919 | } |
| 920 | else |
| 921 | { |
| 922 | gui_update_cursor(TRUE, FALSE); |
| 923 | blink_state = BLINK_ON; |
Bram Moolenaar | 4ab7968 | 2017-08-27 14:50:47 +0200 | [diff] [blame] | 924 | blink_timer = timeout_add(blink_ontime, blink_cb, NULL); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 925 | } |
Bram Moolenaar | da3a77d | 2016-07-10 23:16:09 +0200 | [diff] [blame] | 926 | gui_mch_flush(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 927 | |
| 928 | return FALSE; /* don't happen again */ |
| 929 | } |
| 930 | |
| 931 | /* |
| 932 | * Start the cursor blinking. If it was already blinking, this restarts the |
| 933 | * waiting time and shows the cursor. |
| 934 | */ |
| 935 | void |
| 936 | gui_mch_start_blink(void) |
| 937 | { |
| 938 | if (blink_timer) |
Bram Moolenaar | 7bcdb7d | 2014-04-06 21:08:45 +0200 | [diff] [blame] | 939 | { |
Bram Moolenaar | 4ab7968 | 2017-08-27 14:50:47 +0200 | [diff] [blame] | 940 | timeout_remove(blink_timer); |
Bram Moolenaar | 7bcdb7d | 2014-04-06 21:08:45 +0200 | [diff] [blame] | 941 | blink_timer = 0; |
| 942 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 943 | /* Only switch blinking on if none of the times is zero */ |
| 944 | if (blink_waittime && blink_ontime && blink_offtime && gui.in_focus) |
| 945 | { |
Bram Moolenaar | 4ab7968 | 2017-08-27 14:50:47 +0200 | [diff] [blame] | 946 | blink_timer = timeout_add(blink_waittime, blink_cb, NULL); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 947 | blink_state = BLINK_ON; |
| 948 | gui_update_cursor(TRUE, FALSE); |
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 | } |
| 952 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 953 | static gint |
Bram Moolenaar | b85cb21 | 2009-05-17 14:24:23 +0000 | [diff] [blame] | 954 | enter_notify_event(GtkWidget *widget UNUSED, |
| 955 | GdkEventCrossing *event UNUSED, |
| 956 | gpointer data UNUSED) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 957 | { |
| 958 | if (blink_state == BLINK_NONE) |
| 959 | gui_mch_start_blink(); |
| 960 | |
| 961 | /* make sure keyboard input goes there */ |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 962 | #if GTK_CHECK_VERSION(3,0,0) |
| 963 | if (gtk_socket_id == 0 || !gtk_widget_has_focus(gui.drawarea)) |
| 964 | #else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 965 | if (gtk_socket_id == 0 || !GTK_WIDGET_HAS_FOCUS(gui.drawarea)) |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 966 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 967 | gtk_widget_grab_focus(gui.drawarea); |
| 968 | |
| 969 | return FALSE; |
| 970 | } |
| 971 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 972 | static gint |
Bram Moolenaar | b85cb21 | 2009-05-17 14:24:23 +0000 | [diff] [blame] | 973 | leave_notify_event(GtkWidget *widget UNUSED, |
| 974 | GdkEventCrossing *event UNUSED, |
| 975 | gpointer data UNUSED) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 976 | { |
| 977 | if (blink_state != BLINK_NONE) |
| 978 | gui_mch_stop_blink(); |
| 979 | |
| 980 | return FALSE; |
| 981 | } |
| 982 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 983 | static gint |
Bram Moolenaar | b85cb21 | 2009-05-17 14:24:23 +0000 | [diff] [blame] | 984 | focus_in_event(GtkWidget *widget, |
| 985 | GdkEventFocus *event UNUSED, |
| 986 | gpointer data UNUSED) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 987 | { |
| 988 | gui_focus_change(TRUE); |
| 989 | |
| 990 | if (blink_state == BLINK_NONE) |
| 991 | gui_mch_start_blink(); |
| 992 | |
Bram Moolenaar | 9c8791f | 2007-09-05 19:47:23 +0000 | [diff] [blame] | 993 | /* make sure keyboard input goes to the draw area (if this is focus for a |
| 994 | * window) */ |
Bram Moolenaar | b3656ed | 2006-03-20 21:59:49 +0000 | [diff] [blame] | 995 | if (widget != gui.drawarea) |
Bram Moolenaar | c9b4b05 | 2006-04-30 18:54:39 +0000 | [diff] [blame] | 996 | gtk_widget_grab_focus(gui.drawarea); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 997 | |
| 998 | return TRUE; |
| 999 | } |
| 1000 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1001 | static gint |
Bram Moolenaar | b85cb21 | 2009-05-17 14:24:23 +0000 | [diff] [blame] | 1002 | focus_out_event(GtkWidget *widget UNUSED, |
Bram Moolenaar | cc448b3 | 2010-07-14 16:52:17 +0200 | [diff] [blame] | 1003 | GdkEventFocus *event UNUSED, |
Bram Moolenaar | b85cb21 | 2009-05-17 14:24:23 +0000 | [diff] [blame] | 1004 | gpointer data UNUSED) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1005 | { |
| 1006 | gui_focus_change(FALSE); |
| 1007 | |
| 1008 | if (blink_state != BLINK_NONE) |
| 1009 | gui_mch_stop_blink(); |
| 1010 | |
| 1011 | return TRUE; |
| 1012 | } |
| 1013 | |
| 1014 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1015 | /* |
| 1016 | * Translate a GDK key value to UTF-8 independently of the current locale. |
| 1017 | * The output is written to string, which must have room for at least 6 bytes |
| 1018 | * plus the NUL terminator. Returns the length in bytes. |
| 1019 | * |
| 1020 | * This function is used in the GTK+ 2 GUI only. The GTK+ 1 code makes use |
| 1021 | * of GdkEventKey::string instead. But event->string is evil; see here why: |
| 1022 | * http://developer.gnome.org/doc/API/2.0/gdk/gdk-Event-Structures.html#GdkEventKey |
| 1023 | */ |
| 1024 | static int |
| 1025 | keyval_to_string(unsigned int keyval, unsigned int state, char_u *string) |
| 1026 | { |
| 1027 | int len; |
| 1028 | guint32 uc; |
| 1029 | |
| 1030 | uc = gdk_keyval_to_unicode(keyval); |
| 1031 | if (uc != 0) |
| 1032 | { |
| 1033 | /* Check for CTRL-foo */ |
| 1034 | if ((state & GDK_CONTROL_MASK) && uc >= 0x20 && uc < 0x80) |
| 1035 | { |
| 1036 | /* These mappings look arbitrary at the first glance, but in fact |
| 1037 | * resemble quite exactly the behaviour of the GTK+ 1.2 GUI on my |
| 1038 | * machine. The only difference is BS vs. DEL for CTRL-8 (makes |
| 1039 | * more sense and is consistent with usual terminal behaviour). */ |
| 1040 | if (uc >= '@') |
| 1041 | string[0] = uc & 0x1F; |
| 1042 | else if (uc == '2') |
| 1043 | string[0] = NUL; |
| 1044 | else if (uc >= '3' && uc <= '7') |
| 1045 | string[0] = uc ^ 0x28; |
| 1046 | else if (uc == '8') |
| 1047 | string[0] = BS; |
| 1048 | else if (uc == '?') |
| 1049 | string[0] = DEL; |
| 1050 | else |
| 1051 | string[0] = uc; |
| 1052 | len = 1; |
| 1053 | } |
| 1054 | else |
| 1055 | { |
| 1056 | /* Translate a normal key to UTF-8. This doesn't work for dead |
| 1057 | * keys of course, you _have_ to use an input method for that. */ |
| 1058 | len = utf_char2bytes((int)uc, string); |
| 1059 | } |
| 1060 | } |
| 1061 | else |
| 1062 | { |
| 1063 | /* Translate keys which are represented by ASCII control codes in Vim. |
| 1064 | * There are only a few of those; most control keys are translated to |
| 1065 | * special terminal-like control sequences. */ |
| 1066 | len = 1; |
| 1067 | switch (keyval) |
| 1068 | { |
| 1069 | case GDK_Tab: case GDK_KP_Tab: case GDK_ISO_Left_Tab: |
| 1070 | string[0] = TAB; |
| 1071 | break; |
| 1072 | case GDK_Linefeed: |
| 1073 | string[0] = NL; |
| 1074 | break; |
| 1075 | case GDK_Return: case GDK_ISO_Enter: case GDK_3270_Enter: |
| 1076 | string[0] = CAR; |
| 1077 | break; |
| 1078 | case GDK_Escape: |
| 1079 | string[0] = ESC; |
| 1080 | break; |
| 1081 | default: |
| 1082 | len = 0; |
| 1083 | break; |
| 1084 | } |
| 1085 | } |
| 1086 | string[len] = NUL; |
| 1087 | |
| 1088 | return len; |
| 1089 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1090 | |
Bram Moolenaar | 19a09a1 | 2005-03-04 23:39:37 +0000 | [diff] [blame] | 1091 | static int |
| 1092 | modifiers_gdk2vim(guint state) |
| 1093 | { |
| 1094 | int modifiers = 0; |
| 1095 | |
| 1096 | if (state & GDK_SHIFT_MASK) |
| 1097 | modifiers |= MOD_MASK_SHIFT; |
| 1098 | if (state & GDK_CONTROL_MASK) |
| 1099 | modifiers |= MOD_MASK_CTRL; |
| 1100 | if (state & GDK_MOD1_MASK) |
| 1101 | modifiers |= MOD_MASK_ALT; |
Bram Moolenaar | 580061a | 2010-08-13 13:57:13 +0200 | [diff] [blame] | 1102 | #if GTK_CHECK_VERSION(2,10,0) |
Bram Moolenaar | 30bb414 | 2010-05-17 22:07:15 +0200 | [diff] [blame] | 1103 | if (state & GDK_SUPER_MASK) |
| 1104 | modifiers |= MOD_MASK_META; |
| 1105 | #endif |
Bram Moolenaar | 19a09a1 | 2005-03-04 23:39:37 +0000 | [diff] [blame] | 1106 | if (state & GDK_MOD4_MASK) |
| 1107 | modifiers |= MOD_MASK_META; |
| 1108 | |
| 1109 | return modifiers; |
| 1110 | } |
| 1111 | |
| 1112 | static int |
| 1113 | modifiers_gdk2mouse(guint state) |
| 1114 | { |
| 1115 | int modifiers = 0; |
| 1116 | |
| 1117 | if (state & GDK_SHIFT_MASK) |
| 1118 | modifiers |= MOUSE_SHIFT; |
| 1119 | if (state & GDK_CONTROL_MASK) |
| 1120 | modifiers |= MOUSE_CTRL; |
| 1121 | if (state & GDK_MOD1_MASK) |
| 1122 | modifiers |= MOUSE_ALT; |
| 1123 | |
| 1124 | return modifiers; |
| 1125 | } |
| 1126 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1127 | /* |
| 1128 | * Main keyboard handler: |
| 1129 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1130 | static gint |
Bram Moolenaar | b85cb21 | 2009-05-17 14:24:23 +0000 | [diff] [blame] | 1131 | key_press_event(GtkWidget *widget UNUSED, |
| 1132 | GdkEventKey *event, |
| 1133 | gpointer data UNUSED) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1134 | { |
Bram Moolenaar | 182c5be | 2010-06-25 05:37:59 +0200 | [diff] [blame] | 1135 | /* 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] | 1136 | * (That is, up to 6 bytes + NUL + CSI escapes + safety measure.) */ |
| 1137 | char_u string[32], string2[32]; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1138 | guint key_sym; |
| 1139 | int len; |
| 1140 | int i; |
| 1141 | int modifiers; |
| 1142 | int key; |
| 1143 | guint state; |
| 1144 | char_u *s, *d; |
| 1145 | |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 1146 | #if GTK_CHECK_VERSION(3,0,0) |
| 1147 | is_key_pressed = TRUE; |
| 1148 | gui_mch_stop_blink(); |
| 1149 | #endif |
| 1150 | |
Bram Moolenaar | 20892c1 | 2011-06-26 04:49:00 +0200 | [diff] [blame] | 1151 | gui.event_time = event->time; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1152 | key_sym = event->keyval; |
| 1153 | state = event->state; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1154 | |
| 1155 | #ifdef FEAT_XIM |
| 1156 | if (xim_queue_key_press_event(event, TRUE)) |
| 1157 | return TRUE; |
| 1158 | #endif |
| 1159 | |
| 1160 | #ifdef FEAT_HANGULIN |
| 1161 | if (key_sym == GDK_space && (state & GDK_SHIFT_MASK)) |
| 1162 | { |
| 1163 | hangul_input_state_toggle(); |
| 1164 | return TRUE; |
| 1165 | } |
| 1166 | #endif |
| 1167 | |
| 1168 | #ifdef SunXK_F36 |
| 1169 | /* |
| 1170 | * These keys have bogus lookup strings, and trapping them here is |
| 1171 | * easier than trying to XRebindKeysym() on them with every possible |
| 1172 | * combination of modifiers. |
| 1173 | */ |
| 1174 | if (key_sym == SunXK_F36 || key_sym == SunXK_F37) |
| 1175 | len = 0; |
| 1176 | else |
| 1177 | #endif |
| 1178 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1179 | len = keyval_to_string(key_sym, state, string2); |
| 1180 | |
| 1181 | /* Careful: convert_input() doesn't handle the NUL character. |
| 1182 | * No need to convert pure ASCII anyway, thus the len > 1 check. */ |
| 1183 | if (len > 1 && input_conv.vc_type != CONV_NONE) |
| 1184 | len = convert_input(string2, len, sizeof(string2)); |
| 1185 | |
| 1186 | s = string2; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1187 | d = string; |
| 1188 | for (i = 0; i < len; ++i) |
| 1189 | { |
| 1190 | *d++ = s[i]; |
| 1191 | if (d[-1] == CSI && d + 2 < string + sizeof(string)) |
| 1192 | { |
| 1193 | /* Turn CSI into K_CSI. */ |
| 1194 | *d++ = KS_EXTRA; |
| 1195 | *d++ = (int)KE_CSI; |
| 1196 | } |
| 1197 | } |
| 1198 | len = d - string; |
| 1199 | } |
| 1200 | |
| 1201 | /* Shift-Tab results in Left_Tab, but we want <S-Tab> */ |
| 1202 | if (key_sym == GDK_ISO_Left_Tab) |
| 1203 | { |
| 1204 | key_sym = GDK_Tab; |
| 1205 | state |= GDK_SHIFT_MASK; |
| 1206 | } |
| 1207 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1208 | #ifdef FEAT_MENU |
| 1209 | /* If there is a menu and 'wak' is "yes", or 'wak' is "menu" and the key |
| 1210 | * is a menu shortcut, we ignore everything with the ALT modifier. */ |
| 1211 | if ((state & GDK_MOD1_MASK) |
| 1212 | && gui.menu_is_active |
| 1213 | && (*p_wak == 'y' |
| 1214 | || (*p_wak == 'm' |
| 1215 | && len == 1 |
| 1216 | && gui_is_menu_shortcut(string[0])))) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1217 | /* For GTK2 we return false to signify that we haven't handled the |
| 1218 | * keypress, so that gtk will handle the mnemonic or accelerator. */ |
| 1219 | return FALSE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1220 | #endif |
| 1221 | |
| 1222 | /* Check for Alt/Meta key (Mod1Mask), but not for a BS, DEL or character |
| 1223 | * that already has the 8th bit set. |
| 1224 | * Don't do this for <S-M-Tab>, that should become K_S_TAB with ALT. |
| 1225 | * Don't do this for double-byte encodings, it turns the char into a lead |
| 1226 | * byte. */ |
| 1227 | if (len == 1 |
Bram Moolenaar | 30bb414 | 2010-05-17 22:07:15 +0200 | [diff] [blame] | 1228 | && ((state & GDK_MOD1_MASK) |
Bram Moolenaar | 35f330c | 2010-08-15 13:53:58 +0200 | [diff] [blame] | 1229 | #if GTK_CHECK_VERSION(2,10,0) |
Bram Moolenaar | 30bb414 | 2010-05-17 22:07:15 +0200 | [diff] [blame] | 1230 | || (state & GDK_SUPER_MASK) |
| 1231 | #endif |
| 1232 | ) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1233 | && !(key_sym == GDK_BackSpace || key_sym == GDK_Delete) |
| 1234 | && (string[0] & 0x80) == 0 |
| 1235 | && !(key_sym == GDK_Tab && (state & GDK_SHIFT_MASK)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1236 | && !enc_dbcs |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1237 | ) |
| 1238 | { |
| 1239 | string[0] |= 0x80; |
| 1240 | state &= ~GDK_MOD1_MASK; /* don't use it again */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1241 | if (enc_utf8) /* convert to utf-8 */ |
| 1242 | { |
| 1243 | string[1] = string[0] & 0xbf; |
| 1244 | string[0] = ((unsigned)string[0] >> 6) + 0xc0; |
| 1245 | if (string[1] == CSI) |
| 1246 | { |
| 1247 | string[2] = KS_EXTRA; |
| 1248 | string[3] = (int)KE_CSI; |
| 1249 | len = 4; |
| 1250 | } |
| 1251 | else |
| 1252 | len = 2; |
| 1253 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1254 | } |
| 1255 | |
| 1256 | /* Check for special keys. Also do this when len == 1 (key has an ASCII |
| 1257 | * value) to detect backspace, delete and keypad keys. */ |
| 1258 | if (len == 0 || len == 1) |
| 1259 | { |
| 1260 | for (i = 0; special_keys[i].key_sym != 0; i++) |
| 1261 | { |
| 1262 | if (special_keys[i].key_sym == key_sym) |
| 1263 | { |
| 1264 | string[0] = CSI; |
| 1265 | string[1] = special_keys[i].code0; |
| 1266 | string[2] = special_keys[i].code1; |
| 1267 | len = -3; |
| 1268 | break; |
| 1269 | } |
| 1270 | } |
| 1271 | } |
| 1272 | |
| 1273 | if (len == 0) /* Unrecognized key */ |
| 1274 | return TRUE; |
| 1275 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1276 | /* Special keys (and a few others) may have modifiers. Also when using a |
| 1277 | * double-byte encoding (can't set the 8th bit). */ |
| 1278 | if (len == -3 || key_sym == GDK_space || key_sym == GDK_Tab |
| 1279 | || key_sym == GDK_Return || key_sym == GDK_Linefeed |
| 1280 | || key_sym == GDK_Escape || key_sym == GDK_KP_Tab |
| 1281 | || key_sym == GDK_ISO_Enter || key_sym == GDK_3270_Enter |
Bram Moolenaar | 30bb414 | 2010-05-17 22:07:15 +0200 | [diff] [blame] | 1282 | || (enc_dbcs && len == 1 && ((state & GDK_MOD1_MASK) |
Bram Moolenaar | 35f330c | 2010-08-15 13:53:58 +0200 | [diff] [blame] | 1283 | #if GTK_CHECK_VERSION(2,10,0) |
Bram Moolenaar | 30bb414 | 2010-05-17 22:07:15 +0200 | [diff] [blame] | 1284 | || (state & GDK_SUPER_MASK) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1285 | #endif |
Bram Moolenaar | 182c5be | 2010-06-25 05:37:59 +0200 | [diff] [blame] | 1286 | ))) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1287 | { |
Bram Moolenaar | 19a09a1 | 2005-03-04 23:39:37 +0000 | [diff] [blame] | 1288 | modifiers = modifiers_gdk2vim(state); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1289 | |
| 1290 | /* |
| 1291 | * For some keys a shift modifier is translated into another key |
| 1292 | * code. |
| 1293 | */ |
| 1294 | if (len == -3) |
| 1295 | key = TO_SPECIAL(string[1], string[2]); |
| 1296 | else |
| 1297 | key = string[0]; |
| 1298 | |
| 1299 | key = simplify_key(key, &modifiers); |
| 1300 | if (key == CSI) |
| 1301 | key = K_CSI; |
| 1302 | if (IS_SPECIAL(key)) |
| 1303 | { |
| 1304 | string[0] = CSI; |
| 1305 | string[1] = K_SECOND(key); |
| 1306 | string[2] = K_THIRD(key); |
| 1307 | len = 3; |
| 1308 | } |
| 1309 | else |
| 1310 | { |
| 1311 | string[0] = key; |
| 1312 | len = 1; |
| 1313 | } |
| 1314 | |
| 1315 | if (modifiers != 0) |
| 1316 | { |
| 1317 | string2[0] = CSI; |
| 1318 | string2[1] = KS_MODIFIER; |
| 1319 | string2[2] = modifiers; |
| 1320 | add_to_input_buf(string2, 3); |
| 1321 | } |
| 1322 | } |
| 1323 | |
| 1324 | if (len == 1 && ((string[0] == Ctrl_C && ctrl_c_interrupts) |
| 1325 | || (string[0] == intr_char && intr_char != Ctrl_C))) |
| 1326 | { |
| 1327 | trash_input_buf(); |
| 1328 | got_int = TRUE; |
| 1329 | } |
| 1330 | |
| 1331 | add_to_input_buf(string, len); |
| 1332 | |
| 1333 | /* blank out the pointer if necessary */ |
| 1334 | if (p_mh) |
| 1335 | gui_mch_mousehide(TRUE); |
| 1336 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1337 | return TRUE; |
| 1338 | } |
| 1339 | |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 1340 | #if defined(FEAT_XIM) || GTK_CHECK_VERSION(3,0,0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1341 | static gboolean |
Bram Moolenaar | b85cb21 | 2009-05-17 14:24:23 +0000 | [diff] [blame] | 1342 | key_release_event(GtkWidget *widget UNUSED, |
| 1343 | GdkEventKey *event, |
| 1344 | gpointer data UNUSED) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1345 | { |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 1346 | # if GTK_CHECK_VERSION(3,0,0) |
| 1347 | is_key_pressed = FALSE; |
| 1348 | gui_mch_start_blink(); |
| 1349 | # endif |
| 1350 | # if defined(FEAT_XIM) |
Bram Moolenaar | 20892c1 | 2011-06-26 04:49:00 +0200 | [diff] [blame] | 1351 | gui.event_time = event->time; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1352 | /* |
| 1353 | * GTK+ 2 input methods may do fancy stuff on key release events too. |
| 1354 | * With the default IM for instance, you can enter any UCS code point |
| 1355 | * by holding down CTRL-SHIFT and typing hexadecimal digits. |
| 1356 | */ |
| 1357 | return xim_queue_key_press_event(event, FALSE); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 1358 | # else |
| 1359 | return TRUE; |
| 1360 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1361 | } |
| 1362 | #endif |
| 1363 | |
| 1364 | |
| 1365 | /**************************************************************************** |
| 1366 | * Selection handlers: |
| 1367 | */ |
| 1368 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1369 | static gint |
Bram Moolenaar | b85cb21 | 2009-05-17 14:24:23 +0000 | [diff] [blame] | 1370 | selection_clear_event(GtkWidget *widget UNUSED, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1371 | GdkEventSelection *event, |
Bram Moolenaar | b85cb21 | 2009-05-17 14:24:23 +0000 | [diff] [blame] | 1372 | gpointer user_data UNUSED) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1373 | { |
| 1374 | if (event->selection == clip_plus.gtk_sel_atom) |
| 1375 | clip_lose_selection(&clip_plus); |
| 1376 | else |
| 1377 | clip_lose_selection(&clip_star); |
| 1378 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1379 | return TRUE; |
| 1380 | } |
| 1381 | |
| 1382 | #define RS_NONE 0 /* selection_received_cb() not called yet */ |
| 1383 | #define RS_OK 1 /* selection_received_cb() called and OK */ |
| 1384 | #define RS_FAIL 2 /* selection_received_cb() called and failed */ |
| 1385 | static int received_selection = RS_NONE; |
| 1386 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1387 | static void |
Bram Moolenaar | b85cb21 | 2009-05-17 14:24:23 +0000 | [diff] [blame] | 1388 | selection_received_cb(GtkWidget *widget UNUSED, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1389 | GtkSelectionData *data, |
Bram Moolenaar | b85cb21 | 2009-05-17 14:24:23 +0000 | [diff] [blame] | 1390 | guint time_ UNUSED, |
| 1391 | gpointer user_data UNUSED) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1392 | { |
| 1393 | VimClipboard *cbd; |
| 1394 | char_u *text; |
| 1395 | char_u *tmpbuf = NULL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1396 | guchar *tmpbuf_utf8 = NULL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1397 | int len; |
Bram Moolenaar | d44347f | 2011-06-19 01:14:29 +0200 | [diff] [blame] | 1398 | int motion_type = MAUTO; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1399 | |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 1400 | #if GTK_CHECK_VERSION(3,0,0) |
| 1401 | if (gtk_selection_data_get_selection(data) == clip_plus.gtk_sel_atom) |
| 1402 | #else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1403 | if (data->selection == clip_plus.gtk_sel_atom) |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 1404 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1405 | cbd = &clip_plus; |
| 1406 | else |
| 1407 | cbd = &clip_star; |
| 1408 | |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 1409 | #if GTK_CHECK_VERSION(3,0,0) |
| 1410 | text = (char_u *)gtk_selection_data_get_data(data); |
| 1411 | len = gtk_selection_data_get_length(data); |
| 1412 | #else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1413 | text = (char_u *)data->data; |
| 1414 | len = data->length; |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 1415 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1416 | |
| 1417 | if (text == NULL || len <= 0) |
| 1418 | { |
| 1419 | received_selection = RS_FAIL; |
| 1420 | /* clip_free_selection(cbd); ??? */ |
| 1421 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1422 | return; |
| 1423 | } |
| 1424 | |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 1425 | #if GTK_CHECK_VERSION(3,0,0) |
| 1426 | if (gtk_selection_data_get_data_type(data) == vim_atom) |
| 1427 | #else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1428 | if (data->type == vim_atom) |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 1429 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1430 | { |
| 1431 | motion_type = *text++; |
| 1432 | --len; |
| 1433 | } |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 1434 | #if GTK_CHECK_VERSION(3,0,0) |
| 1435 | else if (gtk_selection_data_get_data_type(data) == vimenc_atom) |
| 1436 | #else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1437 | else if (data->type == vimenc_atom) |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 1438 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1439 | { |
| 1440 | char_u *enc; |
| 1441 | vimconv_T conv; |
| 1442 | |
| 1443 | motion_type = *text++; |
| 1444 | --len; |
| 1445 | |
| 1446 | enc = text; |
| 1447 | text += STRLEN(text) + 1; |
| 1448 | len -= text - enc; |
| 1449 | |
| 1450 | /* If the encoding of the text is different from 'encoding', attempt |
| 1451 | * converting it. */ |
| 1452 | conv.vc_type = CONV_NONE; |
| 1453 | convert_setup(&conv, enc, p_enc); |
| 1454 | if (conv.vc_type != CONV_NONE) |
| 1455 | { |
| 1456 | tmpbuf = string_convert(&conv, text, &len); |
| 1457 | if (tmpbuf != NULL) |
| 1458 | text = tmpbuf; |
| 1459 | convert_setup(&conv, NULL, NULL); |
| 1460 | } |
| 1461 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1462 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1463 | /* gtk_selection_data_get_text() handles all the nasty details |
| 1464 | * and targets and encodings etc. This rocks so hard. */ |
| 1465 | else |
| 1466 | { |
| 1467 | tmpbuf_utf8 = gtk_selection_data_get_text(data); |
| 1468 | if (tmpbuf_utf8 != NULL) |
| 1469 | { |
| 1470 | len = STRLEN(tmpbuf_utf8); |
| 1471 | if (input_conv.vc_type != CONV_NONE) |
| 1472 | { |
| 1473 | tmpbuf = string_convert(&input_conv, tmpbuf_utf8, &len); |
| 1474 | if (tmpbuf != NULL) |
| 1475 | text = tmpbuf; |
| 1476 | } |
| 1477 | else |
| 1478 | text = tmpbuf_utf8; |
| 1479 | } |
Bram Moolenaar | 3a6eaa5 | 2009-06-16 13:23:06 +0000 | [diff] [blame] | 1480 | else if (len >= 2 && text[0] == 0xff && text[1] == 0xfe) |
| 1481 | { |
| 1482 | vimconv_T conv; |
| 1483 | |
| 1484 | /* UTF-16, we get this for HTML */ |
| 1485 | conv.vc_type = CONV_NONE; |
| 1486 | convert_setup_ext(&conv, (char_u *)"utf-16le", FALSE, p_enc, TRUE); |
| 1487 | |
| 1488 | if (conv.vc_type != CONV_NONE) |
| 1489 | { |
| 1490 | text += 2; |
| 1491 | len -= 2; |
| 1492 | tmpbuf = string_convert(&conv, text, &len); |
| 1493 | convert_setup(&conv, NULL, NULL); |
| 1494 | } |
| 1495 | if (tmpbuf != NULL) |
| 1496 | text = tmpbuf; |
| 1497 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1498 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1499 | |
Bram Moolenaar | 84a05ac | 2013-05-06 04:24:17 +0200 | [diff] [blame] | 1500 | /* Chop off any trailing NUL bytes. OpenOffice sends these. */ |
Bram Moolenaar | a76638f | 2010-06-05 12:49:46 +0200 | [diff] [blame] | 1501 | while (len > 0 && text[len - 1] == NUL) |
| 1502 | --len; |
| 1503 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1504 | clip_yank_selection(motion_type, text, (long)len, cbd); |
| 1505 | received_selection = RS_OK; |
| 1506 | vim_free(tmpbuf); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1507 | g_free(tmpbuf_utf8); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1508 | } |
| 1509 | |
| 1510 | /* |
| 1511 | * Prepare our selection data for passing it to the external selection |
| 1512 | * client. |
| 1513 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1514 | static void |
Bram Moolenaar | b85cb21 | 2009-05-17 14:24:23 +0000 | [diff] [blame] | 1515 | selection_get_cb(GtkWidget *widget UNUSED, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1516 | GtkSelectionData *selection_data, |
| 1517 | guint info, |
Bram Moolenaar | b85cb21 | 2009-05-17 14:24:23 +0000 | [diff] [blame] | 1518 | guint time_ UNUSED, |
| 1519 | gpointer user_data UNUSED) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1520 | { |
| 1521 | char_u *string; |
| 1522 | char_u *tmpbuf; |
| 1523 | long_u tmplen; |
| 1524 | int length; |
| 1525 | int motion_type; |
| 1526 | GdkAtom type; |
| 1527 | VimClipboard *cbd; |
| 1528 | |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 1529 | #if GTK_CHECK_VERSION(3,0,0) |
| 1530 | if (gtk_selection_data_get_selection(selection_data) |
| 1531 | == clip_plus.gtk_sel_atom) |
| 1532 | #else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1533 | if (selection_data->selection == clip_plus.gtk_sel_atom) |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 1534 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1535 | cbd = &clip_plus; |
| 1536 | else |
| 1537 | cbd = &clip_star; |
| 1538 | |
| 1539 | if (!cbd->owned) |
| 1540 | return; /* Shouldn't ever happen */ |
| 1541 | |
| 1542 | if (info != (guint)TARGET_STRING |
Bram Moolenaar | 3a6eaa5 | 2009-06-16 13:23:06 +0000 | [diff] [blame] | 1543 | && (!clip_html || info != (guint)TARGET_HTML) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1544 | && info != (guint)TARGET_UTF8_STRING |
| 1545 | && info != (guint)TARGET_VIMENC |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1546 | && info != (guint)TARGET_VIM |
| 1547 | && info != (guint)TARGET_COMPOUND_TEXT |
| 1548 | && info != (guint)TARGET_TEXT) |
| 1549 | return; |
| 1550 | |
| 1551 | /* get the selection from the '*'/'+' register */ |
| 1552 | clip_get_selection(cbd); |
| 1553 | |
| 1554 | motion_type = clip_convert_selection(&string, &tmplen, cbd); |
| 1555 | if (motion_type < 0 || string == NULL) |
| 1556 | return; |
| 1557 | /* Due to int arguments we can't handle more than G_MAXINT. Also |
| 1558 | * reserve one extra byte for NUL or the motion type; just in case. |
| 1559 | * (Not that pasting 2G of text is ever going to work, but... ;-) */ |
| 1560 | length = MIN(tmplen, (long_u)(G_MAXINT - 1)); |
| 1561 | |
| 1562 | if (info == (guint)TARGET_VIM) |
| 1563 | { |
| 1564 | tmpbuf = alloc((unsigned)length + 1); |
| 1565 | if (tmpbuf != NULL) |
| 1566 | { |
| 1567 | tmpbuf[0] = motion_type; |
| 1568 | mch_memmove(tmpbuf + 1, string, (size_t)length); |
| 1569 | } |
| 1570 | /* For our own format, the first byte contains the motion type */ |
| 1571 | ++length; |
| 1572 | vim_free(string); |
| 1573 | string = tmpbuf; |
| 1574 | type = vim_atom; |
| 1575 | } |
| 1576 | |
Bram Moolenaar | 3a6eaa5 | 2009-06-16 13:23:06 +0000 | [diff] [blame] | 1577 | else if (info == (guint)TARGET_HTML) |
| 1578 | { |
| 1579 | vimconv_T conv; |
| 1580 | |
| 1581 | /* Since we get utf-16, we probably should set it as well. */ |
| 1582 | conv.vc_type = CONV_NONE; |
| 1583 | convert_setup_ext(&conv, p_enc, TRUE, (char_u *)"utf-16le", FALSE); |
| 1584 | if (conv.vc_type != CONV_NONE) |
| 1585 | { |
| 1586 | tmpbuf = string_convert(&conv, string, &length); |
| 1587 | convert_setup(&conv, NULL, NULL); |
| 1588 | vim_free(string); |
| 1589 | string = tmpbuf; |
| 1590 | } |
| 1591 | |
| 1592 | /* Prepend the BOM: "fffe" */ |
| 1593 | if (string != NULL) |
| 1594 | { |
| 1595 | tmpbuf = alloc(length + 2); |
| 1596 | tmpbuf[0] = 0xff; |
| 1597 | tmpbuf[1] = 0xfe; |
| 1598 | mch_memmove(tmpbuf + 2, string, (size_t)length); |
| 1599 | vim_free(string); |
| 1600 | string = tmpbuf; |
| 1601 | length += 2; |
| 1602 | |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 1603 | #if !GTK_CHECK_VERSION(3,0,0) |
Bram Moolenaar | 3f2a5d8 | 2016-02-27 22:08:16 +0100 | [diff] [blame] | 1604 | /* Looks redundant even for GTK2 because these values are |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 1605 | * overwritten by gtk_selection_data_set() that follows. */ |
Bram Moolenaar | 3a6eaa5 | 2009-06-16 13:23:06 +0000 | [diff] [blame] | 1606 | selection_data->type = selection_data->target; |
| 1607 | selection_data->format = 16; /* 16 bits per char */ |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 1608 | #endif |
Bram Moolenaar | 3a6eaa5 | 2009-06-16 13:23:06 +0000 | [diff] [blame] | 1609 | gtk_selection_data_set(selection_data, html_atom, 16, |
| 1610 | string, length); |
| 1611 | vim_free(string); |
| 1612 | } |
| 1613 | return; |
| 1614 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1615 | else if (info == (guint)TARGET_VIMENC) |
| 1616 | { |
| 1617 | int l = STRLEN(p_enc); |
| 1618 | |
| 1619 | /* contents: motion_type 'encoding' NUL text */ |
| 1620 | tmpbuf = alloc((unsigned)length + l + 2); |
| 1621 | if (tmpbuf != NULL) |
| 1622 | { |
| 1623 | tmpbuf[0] = motion_type; |
| 1624 | STRCPY(tmpbuf + 1, p_enc); |
| 1625 | mch_memmove(tmpbuf + l + 2, string, (size_t)length); |
| 1626 | } |
| 1627 | length += l + 2; |
| 1628 | vim_free(string); |
| 1629 | string = tmpbuf; |
| 1630 | type = vimenc_atom; |
| 1631 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1632 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1633 | /* gtk_selection_data_set_text() handles everything for us. This is |
| 1634 | * so easy and simple and cool, it'd be insane not to use it. */ |
| 1635 | else |
| 1636 | { |
| 1637 | if (output_conv.vc_type != CONV_NONE) |
| 1638 | { |
| 1639 | tmpbuf = string_convert(&output_conv, string, &length); |
| 1640 | vim_free(string); |
| 1641 | if (tmpbuf == NULL) |
| 1642 | return; |
| 1643 | string = tmpbuf; |
| 1644 | } |
| 1645 | /* Validate the string to avoid runtime warnings */ |
| 1646 | if (g_utf8_validate((const char *)string, (gssize)length, NULL)) |
| 1647 | { |
| 1648 | gtk_selection_data_set_text(selection_data, |
| 1649 | (const char *)string, length); |
| 1650 | } |
| 1651 | vim_free(string); |
| 1652 | return; |
| 1653 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1654 | |
| 1655 | if (string != NULL) |
| 1656 | { |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 1657 | #if !GTK_CHECK_VERSION(3,0,0) |
Bram Moolenaar | 3f2a5d8 | 2016-02-27 22:08:16 +0100 | [diff] [blame] | 1658 | /* Looks redundant even for GTK2 because these values are |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 1659 | * overwritten by gtk_selection_data_set() that follows. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1660 | selection_data->type = selection_data->target; |
| 1661 | selection_data->format = 8; /* 8 bits per char */ |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 1662 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1663 | gtk_selection_data_set(selection_data, type, 8, string, length); |
| 1664 | vim_free(string); |
| 1665 | } |
| 1666 | } |
| 1667 | |
| 1668 | /* |
Bram Moolenaar | 2969570 | 2012-05-18 17:03:18 +0200 | [diff] [blame] | 1669 | * Check if the GUI can be started. Called before gvimrc is sourced and |
| 1670 | * before fork(). |
| 1671 | * Return OK or FAIL. |
| 1672 | */ |
| 1673 | int |
Bram Moolenaar | 717e196 | 2016-08-10 21:28:44 +0200 | [diff] [blame] | 1674 | gui_mch_early_init_check(int give_message) |
Bram Moolenaar | 2969570 | 2012-05-18 17:03:18 +0200 | [diff] [blame] | 1675 | { |
| 1676 | char_u *p; |
| 1677 | |
| 1678 | /* Guess that when $DISPLAY isn't set the GUI can't start. */ |
| 1679 | p = mch_getenv((char_u *)"DISPLAY"); |
| 1680 | if (p == NULL || *p == NUL) |
| 1681 | { |
| 1682 | gui.dying = TRUE; |
Bram Moolenaar | 717e196 | 2016-08-10 21:28:44 +0200 | [diff] [blame] | 1683 | if (give_message) |
| 1684 | EMSG(_((char *)e_opendisp)); |
Bram Moolenaar | 2969570 | 2012-05-18 17:03:18 +0200 | [diff] [blame] | 1685 | return FAIL; |
| 1686 | } |
| 1687 | return OK; |
| 1688 | } |
| 1689 | |
| 1690 | /* |
| 1691 | * Check if the GUI can be started. Called before gvimrc is sourced but after |
| 1692 | * fork(). |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1693 | * Return OK or FAIL. |
| 1694 | */ |
| 1695 | int |
| 1696 | gui_mch_init_check(void) |
| 1697 | { |
Bram Moolenaar | 3a466a8 | 2016-01-19 17:47:25 +0100 | [diff] [blame] | 1698 | #ifdef USE_GRESOURCE |
| 1699 | static int res_registered = FALSE; |
| 1700 | |
| 1701 | if (!res_registered) |
| 1702 | { |
| 1703 | /* Call this function in the GUI process; otherwise, the resources |
| 1704 | * won't be available. Don't call it twice. */ |
| 1705 | res_registered = TRUE; |
| 1706 | gui_gtk_register_resource(); |
| 1707 | } |
| 1708 | #endif |
| 1709 | |
Bram Moolenaar | f80663f | 2016-04-05 21:56:06 +0200 | [diff] [blame] | 1710 | #if GTK_CHECK_VERSION(3,10,0) |
| 1711 | /* Vim currently assumes that Gtk means X11, so it cannot use native Gtk |
| 1712 | * support for other backends such as Wayland. */ |
| 1713 | gdk_set_allowed_backends ("x11"); |
| 1714 | #endif |
| 1715 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1716 | #ifdef FEAT_GUI_GNOME |
| 1717 | if (gtk_socket_id == 0) |
| 1718 | using_gnome = 1; |
| 1719 | #endif |
| 1720 | |
Bram Moolenaar | 8dd7901 | 2013-05-21 12:52:04 +0200 | [diff] [blame] | 1721 | /* This defaults to argv[0], but we want it to match the name of the |
| 1722 | * shipped gvim.desktop so that Vim's windows can be associated with this |
| 1723 | * file. */ |
| 1724 | g_set_prgname("gvim"); |
| 1725 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1726 | /* Don't use gtk_init() or gnome_init(), it exits on failure. */ |
| 1727 | if (!gtk_init_check(&gui_argc, &gui_argv)) |
| 1728 | { |
| 1729 | gui.dying = TRUE; |
Bram Moolenaar | c93e791 | 2008-07-08 10:46:08 +0000 | [diff] [blame] | 1730 | EMSG(_((char *)e_opendisp)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1731 | return FAIL; |
| 1732 | } |
| 1733 | |
| 1734 | return OK; |
| 1735 | } |
| 1736 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1737 | /**************************************************************************** |
| 1738 | * Mouse handling callbacks |
| 1739 | */ |
| 1740 | |
| 1741 | |
| 1742 | static guint mouse_click_timer = 0; |
| 1743 | static int mouse_timed_out = TRUE; |
| 1744 | |
| 1745 | /* |
| 1746 | * Timer used to recognize multiple clicks of the mouse button |
| 1747 | */ |
Bram Moolenaar | 4ab7968 | 2017-08-27 14:50:47 +0200 | [diff] [blame] | 1748 | static timeout_cb_type |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1749 | mouse_click_timer_cb(gpointer data) |
| 1750 | { |
| 1751 | /* we don't use this information currently */ |
| 1752 | int *timed_out = (int *) data; |
| 1753 | |
| 1754 | *timed_out = TRUE; |
| 1755 | return FALSE; /* don't happen again */ |
| 1756 | } |
| 1757 | |
Bram Moolenaar | 4ab7968 | 2017-08-27 14:50:47 +0200 | [diff] [blame] | 1758 | static guint motion_repeat_timer = 0; |
| 1759 | static int motion_repeat_offset = FALSE; |
| 1760 | static timeout_cb_type motion_repeat_timer_cb(gpointer); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1761 | |
| 1762 | static void |
| 1763 | process_motion_notify(int x, int y, GdkModifierType state) |
| 1764 | { |
| 1765 | int button; |
| 1766 | int_u vim_modifiers; |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 1767 | #if GTK_CHECK_VERSION(3,0,0) |
| 1768 | GtkAllocation allocation; |
| 1769 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1770 | |
| 1771 | button = (state & (GDK_BUTTON1_MASK | GDK_BUTTON2_MASK | |
| 1772 | GDK_BUTTON3_MASK | GDK_BUTTON4_MASK | |
| 1773 | GDK_BUTTON5_MASK)) |
| 1774 | ? MOUSE_DRAG : ' '; |
| 1775 | |
| 1776 | /* If our pointer is currently hidden, then we should show it. */ |
| 1777 | gui_mch_mousehide(FALSE); |
| 1778 | |
| 1779 | /* Just moving the rodent above the drawing area without any button |
| 1780 | * being pressed. */ |
| 1781 | if (button != MOUSE_DRAG) |
| 1782 | { |
| 1783 | gui_mouse_moved(x, y); |
| 1784 | return; |
| 1785 | } |
| 1786 | |
| 1787 | /* translate modifier coding between the main engine and GTK */ |
Bram Moolenaar | 19a09a1 | 2005-03-04 23:39:37 +0000 | [diff] [blame] | 1788 | vim_modifiers = modifiers_gdk2mouse(state); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1789 | |
Bram Moolenaar | 4932594 | 2007-05-10 19:19:59 +0000 | [diff] [blame] | 1790 | /* inform the editor engine about the occurrence of this event */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1791 | gui_send_mouse_event(button, x, y, FALSE, vim_modifiers); |
| 1792 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1793 | /* |
| 1794 | * Auto repeat timer handling. |
| 1795 | */ |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 1796 | #if GTK_CHECK_VERSION(3,0,0) |
| 1797 | gtk_widget_get_allocation(gui.drawarea, &allocation); |
| 1798 | |
| 1799 | if (x < 0 || y < 0 |
| 1800 | || x >= allocation.width |
| 1801 | || y >= allocation.height) |
| 1802 | #else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1803 | if (x < 0 || y < 0 |
| 1804 | || x >= gui.drawarea->allocation.width |
| 1805 | || y >= gui.drawarea->allocation.height) |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 1806 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1807 | { |
| 1808 | |
| 1809 | int dx; |
| 1810 | int dy; |
| 1811 | int offshoot; |
| 1812 | int delay = 10; |
| 1813 | |
| 1814 | /* Calculate the maximal distance of the cursor from the drawing area. |
| 1815 | * (offshoot can't become negative here!). |
| 1816 | */ |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 1817 | #if GTK_CHECK_VERSION(3,0,0) |
| 1818 | dx = x < 0 ? -x : x - allocation.width; |
| 1819 | dy = y < 0 ? -y : y - allocation.height; |
| 1820 | #else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1821 | dx = x < 0 ? -x : x - gui.drawarea->allocation.width; |
| 1822 | dy = y < 0 ? -y : y - gui.drawarea->allocation.height; |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 1823 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1824 | |
| 1825 | offshoot = dx > dy ? dx : dy; |
| 1826 | |
Bram Moolenaar | b85cb21 | 2009-05-17 14:24:23 +0000 | [diff] [blame] | 1827 | /* 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] | 1828 | * distance of 127 pixels from the main window. |
| 1829 | * |
| 1830 | * One could think endlessly about the most ergonomic variant here. |
| 1831 | * For example it could make sense to calculate the distance from the |
| 1832 | * drags start instead... |
| 1833 | * |
| 1834 | * Maybe a parabolic interpolation would suite us better here too... |
| 1835 | */ |
| 1836 | if (offshoot > 127) |
| 1837 | { |
| 1838 | /* 5 appears to be somehow near to my perceptual limits :-). */ |
| 1839 | delay = 5; |
| 1840 | } |
| 1841 | else |
| 1842 | { |
| 1843 | delay = (130 * (127 - offshoot)) / 127 + 5; |
| 1844 | } |
| 1845 | |
| 1846 | /* shoot again */ |
| 1847 | if (!motion_repeat_timer) |
Bram Moolenaar | 4ab7968 | 2017-08-27 14:50:47 +0200 | [diff] [blame] | 1848 | motion_repeat_timer = timeout_add(delay, motion_repeat_timer_cb, |
| 1849 | NULL); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1850 | } |
| 1851 | } |
| 1852 | |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 1853 | #if GTK_CHECK_VERSION(3,0,0) |
| 1854 | static GdkDevice * |
| 1855 | gui_gtk_get_pointer_device(GtkWidget *widget) |
| 1856 | { |
| 1857 | GdkWindow * const win = gtk_widget_get_window(widget); |
| 1858 | GdkDisplay * const dpy = gdk_window_get_display(win); |
Bram Moolenaar | 30e12d2 | 2016-04-17 20:49:53 +0200 | [diff] [blame] | 1859 | # if GTK_CHECK_VERSION(3,20,0) |
| 1860 | GdkSeat * const seat = gdk_display_get_default_seat(dpy); |
| 1861 | return gdk_seat_get_pointer(seat); |
| 1862 | # else |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 1863 | GdkDeviceManager * const mngr = gdk_display_get_device_manager(dpy); |
| 1864 | return gdk_device_manager_get_client_pointer(mngr); |
Bram Moolenaar | 30e12d2 | 2016-04-17 20:49:53 +0200 | [diff] [blame] | 1865 | # endif |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 1866 | } |
| 1867 | |
| 1868 | static GdkWindow * |
| 1869 | gui_gtk_get_pointer(GtkWidget *widget, |
| 1870 | gint *x, |
| 1871 | gint *y, |
| 1872 | GdkModifierType *state) |
| 1873 | { |
| 1874 | GdkWindow * const win = gtk_widget_get_window(widget); |
| 1875 | GdkDevice * const dev = gui_gtk_get_pointer_device(widget); |
| 1876 | return gdk_window_get_device_position(win, dev , x, y, state); |
| 1877 | } |
| 1878 | |
Bram Moolenaar | 2369c15 | 2016-03-04 23:08:25 +0100 | [diff] [blame] | 1879 | # if defined(FEAT_GUI_TABLINE) || defined(PROTO) |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 1880 | static GdkWindow * |
| 1881 | gui_gtk_window_at_position(GtkWidget *widget, |
| 1882 | gint *x, |
| 1883 | gint *y) |
| 1884 | { |
| 1885 | GdkDevice * const dev = gui_gtk_get_pointer_device(widget); |
| 1886 | return gdk_device_get_window_at_position(dev, x, y); |
| 1887 | } |
Bram Moolenaar | 2369c15 | 2016-03-04 23:08:25 +0100 | [diff] [blame] | 1888 | # endif |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 1889 | #endif |
| 1890 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1891 | /* |
| 1892 | * Timer used to recognize multiple clicks of the mouse button. |
| 1893 | */ |
Bram Moolenaar | 4ab7968 | 2017-08-27 14:50:47 +0200 | [diff] [blame] | 1894 | static timeout_cb_type |
Bram Moolenaar | b85cb21 | 2009-05-17 14:24:23 +0000 | [diff] [blame] | 1895 | motion_repeat_timer_cb(gpointer data UNUSED) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1896 | { |
| 1897 | int x; |
| 1898 | int y; |
| 1899 | GdkModifierType state; |
| 1900 | |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 1901 | #if GTK_CHECK_VERSION(3,0,0) |
| 1902 | gui_gtk_get_pointer(gui.drawarea, &x, &y, &state); |
| 1903 | #else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1904 | gdk_window_get_pointer(gui.drawarea->window, &x, &y, &state); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 1905 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1906 | |
| 1907 | if (!(state & (GDK_BUTTON1_MASK | GDK_BUTTON2_MASK | |
| 1908 | GDK_BUTTON3_MASK | GDK_BUTTON4_MASK | |
| 1909 | GDK_BUTTON5_MASK))) |
| 1910 | { |
| 1911 | motion_repeat_timer = 0; |
| 1912 | return FALSE; |
| 1913 | } |
| 1914 | |
| 1915 | /* If there already is a mouse click in the input buffer, wait another |
| 1916 | * time (otherwise we would create a backlog of clicks) */ |
| 1917 | if (vim_used_in_input_buf() > 10) |
| 1918 | return TRUE; |
| 1919 | |
| 1920 | motion_repeat_timer = 0; |
| 1921 | |
| 1922 | /* |
| 1923 | * Fake a motion event. |
| 1924 | * Trick: Pretend the mouse moved to the next character on every other |
| 1925 | * event, otherwise drag events will be discarded, because they are still |
| 1926 | * in the same character. |
| 1927 | */ |
| 1928 | if (motion_repeat_offset) |
| 1929 | x += gui.char_width; |
| 1930 | |
| 1931 | motion_repeat_offset = !motion_repeat_offset; |
| 1932 | process_motion_notify(x, y, state); |
| 1933 | |
| 1934 | /* Don't happen again. We will get reinstalled in the synthetic event |
| 1935 | * if needed -- thus repeating should still work. */ |
| 1936 | return FALSE; |
| 1937 | } |
| 1938 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1939 | static gint |
Bram Moolenaar | b85cb21 | 2009-05-17 14:24:23 +0000 | [diff] [blame] | 1940 | motion_notify_event(GtkWidget *widget, |
| 1941 | GdkEventMotion *event, |
| 1942 | gpointer data UNUSED) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1943 | { |
| 1944 | if (event->is_hint) |
| 1945 | { |
| 1946 | int x; |
| 1947 | int y; |
| 1948 | GdkModifierType state; |
| 1949 | |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 1950 | #if GTK_CHECK_VERSION(3,0,0) |
| 1951 | gui_gtk_get_pointer(widget, &x, &y, &state); |
| 1952 | #else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1953 | gdk_window_get_pointer(widget->window, &x, &y, &state); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 1954 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1955 | process_motion_notify(x, y, state); |
| 1956 | } |
| 1957 | else |
| 1958 | { |
| 1959 | process_motion_notify((int)event->x, (int)event->y, |
| 1960 | (GdkModifierType)event->state); |
| 1961 | } |
| 1962 | |
| 1963 | return TRUE; /* handled */ |
| 1964 | } |
| 1965 | |
| 1966 | |
| 1967 | /* |
| 1968 | * Mouse button handling. Note please that we are capturing multiple click's |
| 1969 | * by our own timeout mechanism instead of the one provided by GTK+ itself. |
| 1970 | * This is due to the way the generic VIM code is recognizing multiple clicks. |
| 1971 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1972 | static gint |
Bram Moolenaar | b85cb21 | 2009-05-17 14:24:23 +0000 | [diff] [blame] | 1973 | button_press_event(GtkWidget *widget, |
| 1974 | GdkEventButton *event, |
| 1975 | gpointer data UNUSED) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1976 | { |
| 1977 | int button; |
| 1978 | int repeated_click = FALSE; |
| 1979 | int x, y; |
| 1980 | int_u vim_modifiers; |
| 1981 | |
Bram Moolenaar | 20892c1 | 2011-06-26 04:49:00 +0200 | [diff] [blame] | 1982 | gui.event_time = event->time; |
Bram Moolenaar | 7cfea75 | 2010-06-22 06:07:12 +0200 | [diff] [blame] | 1983 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1984 | /* Make sure we have focus now we've been selected */ |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 1985 | #if GTK_CHECK_VERSION(3,0,0) |
| 1986 | if (gtk_socket_id != 0 && !gtk_widget_has_focus(widget)) |
| 1987 | #else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1988 | if (gtk_socket_id != 0 && !GTK_WIDGET_HAS_FOCUS(widget)) |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 1989 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1990 | gtk_widget_grab_focus(widget); |
| 1991 | |
| 1992 | /* |
| 1993 | * Don't let additional events about multiple clicks send by GTK to us |
| 1994 | * after the initial button press event confuse us. |
| 1995 | */ |
| 1996 | if (event->type != GDK_BUTTON_PRESS) |
| 1997 | return FALSE; |
| 1998 | |
| 1999 | x = event->x; |
| 2000 | y = event->y; |
| 2001 | |
| 2002 | /* Handle multiple clicks */ |
| 2003 | if (!mouse_timed_out && mouse_click_timer) |
| 2004 | { |
Bram Moolenaar | 4ab7968 | 2017-08-27 14:50:47 +0200 | [diff] [blame] | 2005 | timeout_remove(mouse_click_timer); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2006 | mouse_click_timer = 0; |
| 2007 | repeated_click = TRUE; |
| 2008 | } |
| 2009 | |
| 2010 | mouse_timed_out = FALSE; |
Bram Moolenaar | 4ab7968 | 2017-08-27 14:50:47 +0200 | [diff] [blame] | 2011 | mouse_click_timer = timeout_add(p_mouset, mouse_click_timer_cb, |
| 2012 | &mouse_timed_out); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2013 | |
| 2014 | switch (event->button) |
| 2015 | { |
Bram Moolenaar | 88e484b | 2015-11-24 15:38:44 +0100 | [diff] [blame] | 2016 | /* Keep in sync with gui_x11.c. |
| 2017 | * Buttons 4-7 are handled in scroll_event() */ |
| 2018 | case 1: button = MOUSE_LEFT; break; |
| 2019 | case 2: button = MOUSE_MIDDLE; break; |
| 2020 | case 3: button = MOUSE_RIGHT; break; |
| 2021 | case 8: button = MOUSE_X1; break; |
| 2022 | case 9: button = MOUSE_X2; break; |
| 2023 | default: |
| 2024 | return FALSE; /* Unknown button */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2025 | } |
| 2026 | |
| 2027 | #ifdef FEAT_XIM |
| 2028 | /* cancel any preediting */ |
| 2029 | if (im_is_preediting()) |
| 2030 | xim_reset(); |
| 2031 | #endif |
| 2032 | |
Bram Moolenaar | 19a09a1 | 2005-03-04 23:39:37 +0000 | [diff] [blame] | 2033 | vim_modifiers = modifiers_gdk2mouse(event->state); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2034 | |
| 2035 | gui_send_mouse_event(button, x, y, repeated_click, vim_modifiers); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2036 | |
| 2037 | return TRUE; |
| 2038 | } |
| 2039 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2040 | /* |
Bram Moolenaar | 182c5be | 2010-06-25 05:37:59 +0200 | [diff] [blame] | 2041 | * GTK+ 2 abstracts scrolling via the GdkEventScroll. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2042 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2043 | static gboolean |
Bram Moolenaar | b85cb21 | 2009-05-17 14:24:23 +0000 | [diff] [blame] | 2044 | scroll_event(GtkWidget *widget, |
| 2045 | GdkEventScroll *event, |
| 2046 | gpointer data UNUSED) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2047 | { |
| 2048 | int button; |
Bram Moolenaar | 19a09a1 | 2005-03-04 23:39:37 +0000 | [diff] [blame] | 2049 | int_u vim_modifiers; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2050 | |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 2051 | #if GTK_CHECK_VERSION(3,0,0) |
| 2052 | if (gtk_socket_id != 0 && !gtk_widget_has_focus(widget)) |
| 2053 | #else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2054 | if (gtk_socket_id != 0 && !GTK_WIDGET_HAS_FOCUS(widget)) |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 2055 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2056 | gtk_widget_grab_focus(widget); |
| 2057 | |
| 2058 | switch (event->direction) |
| 2059 | { |
| 2060 | case GDK_SCROLL_UP: |
| 2061 | button = MOUSE_4; |
| 2062 | break; |
| 2063 | case GDK_SCROLL_DOWN: |
| 2064 | button = MOUSE_5; |
| 2065 | break; |
Bram Moolenaar | 8d9b40e | 2010-07-25 15:49:07 +0200 | [diff] [blame] | 2066 | case GDK_SCROLL_LEFT: |
| 2067 | button = MOUSE_7; |
| 2068 | break; |
| 2069 | case GDK_SCROLL_RIGHT: |
| 2070 | button = MOUSE_6; |
| 2071 | break; |
| 2072 | default: /* This shouldn't happen */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2073 | return FALSE; |
| 2074 | } |
| 2075 | |
| 2076 | # ifdef FEAT_XIM |
| 2077 | /* cancel any preediting */ |
| 2078 | if (im_is_preediting()) |
| 2079 | xim_reset(); |
| 2080 | # endif |
| 2081 | |
Bram Moolenaar | 19a09a1 | 2005-03-04 23:39:37 +0000 | [diff] [blame] | 2082 | vim_modifiers = modifiers_gdk2mouse(event->state); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2083 | |
| 2084 | gui_send_mouse_event(button, (int)event->x, (int)event->y, |
| 2085 | FALSE, vim_modifiers); |
| 2086 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2087 | return TRUE; |
| 2088 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2089 | |
| 2090 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2091 | static gint |
Bram Moolenaar | b85cb21 | 2009-05-17 14:24:23 +0000 | [diff] [blame] | 2092 | button_release_event(GtkWidget *widget UNUSED, |
| 2093 | GdkEventButton *event, |
| 2094 | gpointer data UNUSED) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2095 | { |
| 2096 | int x, y; |
| 2097 | int_u vim_modifiers; |
| 2098 | |
Bram Moolenaar | 20892c1 | 2011-06-26 04:49:00 +0200 | [diff] [blame] | 2099 | gui.event_time = event->time; |
Bram Moolenaar | 7cfea75 | 2010-06-22 06:07:12 +0200 | [diff] [blame] | 2100 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2101 | /* Remove any motion "machine gun" timers used for automatic further |
| 2102 | extension of allocation areas if outside of the applications window |
| 2103 | area .*/ |
| 2104 | if (motion_repeat_timer) |
| 2105 | { |
Bram Moolenaar | 4ab7968 | 2017-08-27 14:50:47 +0200 | [diff] [blame] | 2106 | timeout_remove(motion_repeat_timer); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2107 | motion_repeat_timer = 0; |
| 2108 | } |
| 2109 | |
| 2110 | x = event->x; |
| 2111 | y = event->y; |
| 2112 | |
Bram Moolenaar | 19a09a1 | 2005-03-04 23:39:37 +0000 | [diff] [blame] | 2113 | vim_modifiers = modifiers_gdk2mouse(event->state); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2114 | |
| 2115 | gui_send_mouse_event(MOUSE_RELEASE, x, y, FALSE, vim_modifiers); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2116 | |
| 2117 | return TRUE; |
| 2118 | } |
| 2119 | |
| 2120 | |
| 2121 | #ifdef FEAT_DND |
| 2122 | /**************************************************************************** |
| 2123 | * Drag aNd Drop support handlers. |
| 2124 | */ |
| 2125 | |
| 2126 | /* |
| 2127 | * Count how many items there may be and separate them with a NUL. |
| 2128 | * Apparently the items are separated with \r\n. This is not documented, |
| 2129 | * thus be careful not to go past the end. Also allow separation with |
| 2130 | * NUL characters. |
| 2131 | */ |
| 2132 | static int |
| 2133 | count_and_decode_uri_list(char_u *out, char_u *raw, int len) |
| 2134 | { |
| 2135 | int i; |
| 2136 | char_u *p = out; |
| 2137 | int count = 0; |
| 2138 | |
| 2139 | for (i = 0; i < len; ++i) |
| 2140 | { |
| 2141 | if (raw[i] == NUL || raw[i] == '\n' || raw[i] == '\r') |
| 2142 | { |
| 2143 | if (p > out && p[-1] != NUL) |
| 2144 | { |
| 2145 | ++count; |
| 2146 | *p++ = NUL; |
| 2147 | } |
| 2148 | } |
| 2149 | else if (raw[i] == '%' && i + 2 < len && hexhex2nr(raw + i + 1) > 0) |
| 2150 | { |
| 2151 | *p++ = hexhex2nr(raw + i + 1); |
| 2152 | i += 2; |
| 2153 | } |
| 2154 | else |
| 2155 | *p++ = raw[i]; |
| 2156 | } |
| 2157 | if (p > out && p[-1] != NUL) |
| 2158 | { |
| 2159 | *p = NUL; /* last item didn't have \r or \n */ |
| 2160 | ++count; |
| 2161 | } |
| 2162 | return count; |
| 2163 | } |
| 2164 | |
| 2165 | /* |
| 2166 | * Parse NUL separated "src" strings. Make it an array "outlist" form. On |
| 2167 | * this process, URI which protocol is not "file:" are removed. Return |
| 2168 | * length of array (less than "max"). |
| 2169 | */ |
| 2170 | static int |
| 2171 | filter_uri_list(char_u **outlist, int max, char_u *src) |
| 2172 | { |
| 2173 | int i, j; |
| 2174 | |
| 2175 | for (i = j = 0; i < max; ++i) |
| 2176 | { |
| 2177 | outlist[i] = NULL; |
| 2178 | if (STRNCMP(src, "file:", 5) == 0) |
| 2179 | { |
| 2180 | src += 5; |
| 2181 | if (STRNCMP(src, "//localhost", 11) == 0) |
| 2182 | src += 11; |
| 2183 | while (src[0] == '/' && src[1] == '/') |
| 2184 | ++src; |
| 2185 | outlist[j++] = vim_strsave(src); |
| 2186 | } |
| 2187 | src += STRLEN(src) + 1; |
| 2188 | } |
| 2189 | return j; |
| 2190 | } |
| 2191 | |
| 2192 | static char_u ** |
| 2193 | parse_uri_list(int *count, char_u *data, int len) |
| 2194 | { |
| 2195 | int n = 0; |
| 2196 | char_u *tmp = NULL; |
Bram Moolenaar | 945ec09 | 2016-06-08 21:17:43 +0200 | [diff] [blame] | 2197 | char_u **array = NULL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2198 | |
| 2199 | if (data != NULL && len > 0 && (tmp = (char_u *)alloc(len + 1)) != NULL) |
| 2200 | { |
| 2201 | n = count_and_decode_uri_list(tmp, data, len); |
| 2202 | if (n > 0 && (array = (char_u **)alloc(n * sizeof(char_u *))) != NULL) |
| 2203 | n = filter_uri_list(array, n, tmp); |
| 2204 | } |
| 2205 | vim_free(tmp); |
| 2206 | *count = n; |
| 2207 | return array; |
| 2208 | } |
| 2209 | |
| 2210 | static void |
| 2211 | drag_handle_uri_list(GdkDragContext *context, |
| 2212 | GtkSelectionData *data, |
| 2213 | guint time_, |
| 2214 | GdkModifierType state, |
| 2215 | gint x, |
| 2216 | gint y) |
| 2217 | { |
| 2218 | char_u **fnames; |
| 2219 | int nfiles = 0; |
| 2220 | |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 2221 | # if GTK_CHECK_VERSION(3,0,0) |
| 2222 | fnames = parse_uri_list(&nfiles, |
| 2223 | (char_u *)gtk_selection_data_get_data(data), |
| 2224 | gtk_selection_data_get_length(data)); |
| 2225 | # else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2226 | fnames = parse_uri_list(&nfiles, data->data, data->length); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 2227 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2228 | |
| 2229 | if (fnames != NULL && nfiles > 0) |
| 2230 | { |
Bram Moolenaar | 19a09a1 | 2005-03-04 23:39:37 +0000 | [diff] [blame] | 2231 | int_u modifiers; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2232 | |
| 2233 | gtk_drag_finish(context, TRUE, FALSE, time_); /* accept */ |
| 2234 | |
Bram Moolenaar | 19a09a1 | 2005-03-04 23:39:37 +0000 | [diff] [blame] | 2235 | modifiers = modifiers_gdk2mouse(state); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2236 | |
| 2237 | gui_handle_drop(x, y, modifiers, fnames, nfiles); |
| 2238 | } |
Bram Moolenaar | eb3593b | 2006-04-22 22:33:57 +0000 | [diff] [blame] | 2239 | else |
| 2240 | vim_free(fnames); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2241 | } |
| 2242 | |
| 2243 | static void |
| 2244 | drag_handle_text(GdkDragContext *context, |
| 2245 | GtkSelectionData *data, |
| 2246 | guint time_, |
| 2247 | GdkModifierType state) |
| 2248 | { |
| 2249 | char_u dropkey[6] = {CSI, KS_MODIFIER, 0, CSI, KS_EXTRA, (char_u)KE_DROP}; |
| 2250 | char_u *text; |
| 2251 | int len; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2252 | char_u *tmpbuf = NULL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2253 | |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 2254 | # if GTK_CHECK_VERSION(3,0,0) |
| 2255 | text = (char_u *)gtk_selection_data_get_data(data); |
| 2256 | len = gtk_selection_data_get_length(data); |
| 2257 | # else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2258 | text = data->data; |
| 2259 | len = data->length; |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 2260 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2261 | |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 2262 | # if GTK_CHECK_VERSION(3,0,0) |
| 2263 | if (gtk_selection_data_get_data_type(data) == utf8_string_atom) |
| 2264 | # else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2265 | if (data->type == utf8_string_atom) |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 2266 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2267 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2268 | if (input_conv.vc_type != CONV_NONE) |
| 2269 | tmpbuf = string_convert(&input_conv, text, &len); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2270 | if (tmpbuf != NULL) |
| 2271 | text = tmpbuf; |
| 2272 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2273 | |
| 2274 | dnd_yank_drag_data(text, (long)len); |
| 2275 | gtk_drag_finish(context, TRUE, FALSE, time_); /* accept */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2276 | vim_free(tmpbuf); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2277 | |
Bram Moolenaar | 19a09a1 | 2005-03-04 23:39:37 +0000 | [diff] [blame] | 2278 | dropkey[2] = modifiers_gdk2vim(state); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2279 | |
| 2280 | if (dropkey[2] != 0) |
| 2281 | add_to_input_buf(dropkey, (int)sizeof(dropkey)); |
| 2282 | else |
| 2283 | add_to_input_buf(dropkey + 3, (int)(sizeof(dropkey) - 3)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2284 | } |
| 2285 | |
| 2286 | /* |
| 2287 | * DND receiver. |
| 2288 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2289 | static void |
| 2290 | drag_data_received_cb(GtkWidget *widget, |
| 2291 | GdkDragContext *context, |
| 2292 | gint x, |
| 2293 | gint y, |
| 2294 | GtkSelectionData *data, |
| 2295 | guint info, |
| 2296 | guint time_, |
Bram Moolenaar | b85cb21 | 2009-05-17 14:24:23 +0000 | [diff] [blame] | 2297 | gpointer user_data UNUSED) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2298 | { |
| 2299 | GdkModifierType state; |
| 2300 | |
| 2301 | /* Guard against trash */ |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 2302 | # if GTK_CHECK_VERSION(3,0,0) |
| 2303 | const guchar * const data_data = gtk_selection_data_get_data(data); |
| 2304 | const gint data_length = gtk_selection_data_get_length(data); |
| 2305 | const gint data_format = gtk_selection_data_get_format(data); |
| 2306 | |
| 2307 | if (data_data == NULL |
| 2308 | || data_length <= 0 |
| 2309 | || data_format != 8 |
| 2310 | || data_data[data_length] != '\0') |
| 2311 | # else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2312 | if (data->data == NULL |
| 2313 | || data->length <= 0 |
| 2314 | || data->format != 8 |
| 2315 | || data->data[data->length] != '\0') |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 2316 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2317 | { |
| 2318 | gtk_drag_finish(context, FALSE, FALSE, time_); |
| 2319 | return; |
| 2320 | } |
| 2321 | |
| 2322 | /* Get the current modifier state for proper distinguishment between |
| 2323 | * different operations later. */ |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 2324 | #if GTK_CHECK_VERSION(3,0,0) |
| 2325 | gui_gtk_get_pointer(widget, NULL, NULL, &state); |
| 2326 | # else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2327 | gdk_window_get_pointer(widget->window, NULL, NULL, &state); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 2328 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2329 | |
| 2330 | /* Not sure about the role of "text/plain" here... */ |
| 2331 | if (info == (guint)TARGET_TEXT_URI_LIST) |
| 2332 | drag_handle_uri_list(context, data, time_, state, x, y); |
| 2333 | else |
| 2334 | drag_handle_text(context, data, time_, state); |
| 2335 | |
| 2336 | } |
| 2337 | #endif /* FEAT_DND */ |
| 2338 | |
| 2339 | |
| 2340 | #if defined(FEAT_GUI_GNOME) && defined(FEAT_SESSION) |
| 2341 | /* |
| 2342 | * GnomeClient interact callback. Check for unsaved buffers that cannot |
| 2343 | * be abandoned and pop up a dialog asking the user for confirmation if |
| 2344 | * necessary. |
| 2345 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2346 | static void |
Bram Moolenaar | 30bb414 | 2010-05-17 22:07:15 +0200 | [diff] [blame] | 2347 | sm_client_check_changed_any(GnomeClient *client UNUSED, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2348 | gint key, |
Bram Moolenaar | 30bb414 | 2010-05-17 22:07:15 +0200 | [diff] [blame] | 2349 | GnomeDialogType type UNUSED, |
| 2350 | gpointer data UNUSED) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2351 | { |
| 2352 | cmdmod_T save_cmdmod; |
| 2353 | gboolean shutdown_cancelled; |
| 2354 | |
| 2355 | save_cmdmod = cmdmod; |
| 2356 | |
| 2357 | # ifdef FEAT_BROWSE |
| 2358 | cmdmod.browse = TRUE; |
| 2359 | # endif |
| 2360 | # if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) |
| 2361 | cmdmod.confirm = TRUE; |
| 2362 | # endif |
| 2363 | /* |
| 2364 | * If there are changed buffers, present the user with |
| 2365 | * a dialog if possible, otherwise give an error message. |
| 2366 | */ |
Bram Moolenaar | 027387f | 2016-01-02 22:25:52 +0100 | [diff] [blame] | 2367 | shutdown_cancelled = check_changed_any(FALSE, FALSE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2368 | |
| 2369 | exiting = FALSE; |
| 2370 | cmdmod = save_cmdmod; |
| 2371 | setcursor(); /* position the cursor */ |
| 2372 | out_flush(); |
| 2373 | /* |
| 2374 | * If the user hit the [Cancel] button the whole shutdown |
| 2375 | * will be cancelled. Wow, quite powerful feature (: |
| 2376 | */ |
| 2377 | gnome_interaction_key_return(key, shutdown_cancelled); |
| 2378 | } |
| 2379 | |
| 2380 | /* |
| 2381 | * Generate a script that can be used to restore the current editing session. |
| 2382 | * Save the value of v:this_session before running :mksession in order to make |
| 2383 | * automagic session save fully transparent. Return TRUE on success. |
| 2384 | */ |
| 2385 | static int |
| 2386 | write_session_file(char_u *filename) |
| 2387 | { |
| 2388 | char_u *escaped_filename; |
| 2389 | char *mksession_cmdline; |
| 2390 | unsigned int save_ssop_flags; |
| 2391 | int failed; |
| 2392 | |
| 2393 | /* |
| 2394 | * Build an ex command line to create a script that restores the current |
| 2395 | * session if executed. Escape the filename to avoid nasty surprises. |
| 2396 | */ |
| 2397 | escaped_filename = vim_strsave_escaped(filename, escape_chars); |
| 2398 | if (escaped_filename == NULL) |
| 2399 | return FALSE; |
Bram Moolenaar | dc3213d | 2007-06-19 16:03:50 +0000 | [diff] [blame] | 2400 | mksession_cmdline = g_strconcat("mksession ", (char *)escaped_filename, |
| 2401 | NULL); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2402 | vim_free(escaped_filename); |
Bram Moolenaar | dc3213d | 2007-06-19 16:03:50 +0000 | [diff] [blame] | 2403 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2404 | /* |
| 2405 | * Use a reasonable hardcoded set of 'sessionoptions' flags to avoid |
| 2406 | * unpredictable effects when the session is saved automatically. Also, |
| 2407 | * we definitely need SSOP_GLOBALS to be able to restore v:this_session. |
| 2408 | * Don't use SSOP_BUFFERS to prevent the buffer list from becoming |
| 2409 | * enormously large if the GNOME session feature is used regularly. |
| 2410 | */ |
| 2411 | save_ssop_flags = ssop_flags; |
| 2412 | ssop_flags = (SSOP_BLANK|SSOP_CURDIR|SSOP_FOLDS|SSOP_GLOBALS |
Bram Moolenaar | dc3213d | 2007-06-19 16:03:50 +0000 | [diff] [blame] | 2413 | |SSOP_HELP|SSOP_OPTIONS|SSOP_WINSIZE|SSOP_TABPAGES); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2414 | |
| 2415 | do_cmdline_cmd((char_u *)"let Save_VV_this_session = v:this_session"); |
| 2416 | failed = (do_cmdline_cmd((char_u *)mksession_cmdline) == FAIL); |
| 2417 | 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] | 2418 | do_unlet((char_u *)"Save_VV_this_session", TRUE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2419 | |
| 2420 | ssop_flags = save_ssop_flags; |
| 2421 | g_free(mksession_cmdline); |
Bram Moolenaar | 24dc230 | 2014-05-13 20:19:58 +0200 | [diff] [blame] | 2422 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2423 | /* |
| 2424 | * Reopen the file and append a command to restore v:this_session, |
| 2425 | * as if this save never happened. This is to avoid conflicts with |
| 2426 | * the user's own sessions. FIXME: It's probably less hackish to add |
| 2427 | * a "stealth" flag to 'sessionoptions' -- gotta ask Bram. |
| 2428 | */ |
| 2429 | if (!failed) |
| 2430 | { |
| 2431 | FILE *fd; |
| 2432 | |
| 2433 | fd = open_exfile(filename, TRUE, APPENDBIN); |
| 2434 | |
| 2435 | failed = (fd == NULL |
| 2436 | || put_line(fd, "let v:this_session = Save_VV_this_session") == FAIL |
| 2437 | || put_line(fd, "unlet Save_VV_this_session") == FAIL); |
| 2438 | |
| 2439 | if (fd != NULL && fclose(fd) != 0) |
| 2440 | failed = TRUE; |
| 2441 | |
| 2442 | if (failed) |
| 2443 | mch_remove(filename); |
| 2444 | } |
| 2445 | |
| 2446 | return !failed; |
| 2447 | } |
| 2448 | |
| 2449 | /* |
| 2450 | * "save_yourself" signal handler. Initiate an interaction to ask the user |
| 2451 | * for confirmation if necessary. Save the current editing session and tell |
| 2452 | * the session manager how to restart Vim. |
| 2453 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2454 | static gboolean |
| 2455 | sm_client_save_yourself(GnomeClient *client, |
Bram Moolenaar | 30bb414 | 2010-05-17 22:07:15 +0200 | [diff] [blame] | 2456 | gint phase UNUSED, |
| 2457 | GnomeSaveStyle save_style UNUSED, |
| 2458 | gboolean shutdown UNUSED, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2459 | GnomeInteractStyle interact_style, |
Bram Moolenaar | 30bb414 | 2010-05-17 22:07:15 +0200 | [diff] [blame] | 2460 | gboolean fast UNUSED, |
| 2461 | gpointer data UNUSED) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2462 | { |
| 2463 | static const char suffix[] = "-session.vim"; |
| 2464 | char *session_file; |
| 2465 | unsigned int len; |
| 2466 | gboolean success; |
| 2467 | |
| 2468 | /* Always request an interaction if possible. check_changed_any() |
| 2469 | * won't actually show a dialog unless any buffers have been modified. |
| 2470 | * There doesn't seem to be an obvious way to check that without |
| 2471 | * automatically firing the dialog. Anyway, it works just fine. */ |
| 2472 | if (interact_style == GNOME_INTERACT_ANY) |
| 2473 | gnome_client_request_interaction(client, GNOME_DIALOG_NORMAL, |
| 2474 | &sm_client_check_changed_any, |
| 2475 | NULL); |
| 2476 | out_flush(); |
| 2477 | ml_sync_all(FALSE, FALSE); /* preserve all swap files */ |
| 2478 | |
| 2479 | /* The path is unique for each session save. We do neither know nor care |
| 2480 | * which session script will actually be used later. This decision is in |
| 2481 | * the domain of the session manager. */ |
| 2482 | session_file = gnome_config_get_real_path( |
| 2483 | gnome_client_get_config_prefix(client)); |
| 2484 | len = strlen(session_file); |
| 2485 | |
| 2486 | if (len > 0 && session_file[len-1] == G_DIR_SEPARATOR) |
| 2487 | --len; /* get rid of the superfluous trailing '/' */ |
| 2488 | |
| 2489 | session_file = g_renew(char, session_file, len + sizeof(suffix)); |
| 2490 | memcpy(session_file + len, suffix, sizeof(suffix)); |
| 2491 | |
| 2492 | success = write_session_file((char_u *)session_file); |
| 2493 | |
| 2494 | if (success) |
| 2495 | { |
| 2496 | const char *argv[8]; |
| 2497 | int i; |
| 2498 | |
| 2499 | /* Tell the session manager how to wipe out the stored session data. |
| 2500 | * This isn't as dangerous as it looks, don't worry :) session_file |
| 2501 | * is a unique absolute filename. Usually it'll be something like |
| 2502 | * `/home/user/.gnome2/vim-XXXXXX-session.vim'. */ |
| 2503 | i = 0; |
| 2504 | argv[i++] = "rm"; |
| 2505 | argv[i++] = session_file; |
| 2506 | argv[i] = NULL; |
| 2507 | |
| 2508 | gnome_client_set_discard_command(client, i, (char **)argv); |
| 2509 | |
| 2510 | /* Tell the session manager how to restore the just saved session. |
| 2511 | * This is easily done thanks to Vim's -S option. Pass the -f flag |
| 2512 | * since there's no need to fork -- it might even cause confusion. |
| 2513 | * Also pass the window role to give the WM something to match on. |
| 2514 | * The role is set in gui_mch_open(), thus should _never_ be NULL. */ |
| 2515 | i = 0; |
| 2516 | argv[i++] = restart_command; |
| 2517 | argv[i++] = "-f"; |
| 2518 | argv[i++] = "-g"; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2519 | argv[i++] = "--role"; |
| 2520 | argv[i++] = gtk_window_get_role(GTK_WINDOW(gui.mainwin)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2521 | argv[i++] = "-S"; |
| 2522 | argv[i++] = session_file; |
| 2523 | argv[i] = NULL; |
| 2524 | |
| 2525 | gnome_client_set_restart_command(client, i, (char **)argv); |
| 2526 | gnome_client_set_clone_command(client, 0, NULL); |
| 2527 | } |
| 2528 | |
| 2529 | g_free(session_file); |
| 2530 | |
| 2531 | return success; |
| 2532 | } |
| 2533 | |
| 2534 | /* |
| 2535 | * Called when the session manager wants us to die. There isn't much to save |
| 2536 | * here since "save_yourself" has been emitted before (unless serious trouble |
| 2537 | * is happening). |
| 2538 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2539 | static void |
Bram Moolenaar | 30bb414 | 2010-05-17 22:07:15 +0200 | [diff] [blame] | 2540 | sm_client_die(GnomeClient *client UNUSED, gpointer data UNUSED) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2541 | { |
| 2542 | /* Don't write messages to the GUI anymore */ |
| 2543 | full_screen = FALSE; |
| 2544 | |
Bram Moolenaar | c93e791 | 2008-07-08 10:46:08 +0000 | [diff] [blame] | 2545 | vim_strncpy(IObuff, (char_u *) |
Bram Moolenaar | ce0842a | 2005-07-18 21:58:11 +0000 | [diff] [blame] | 2546 | _("Vim: Received \"die\" request from session manager\n"), |
Bram Moolenaar | c93e791 | 2008-07-08 10:46:08 +0000 | [diff] [blame] | 2547 | IOSIZE - 1); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2548 | preserve_exit(); |
| 2549 | } |
| 2550 | |
| 2551 | /* |
| 2552 | * Connect our signal handlers to be notified on session save and shutdown. |
| 2553 | */ |
| 2554 | static void |
| 2555 | setup_save_yourself(void) |
| 2556 | { |
| 2557 | GnomeClient *client; |
| 2558 | |
| 2559 | client = gnome_master_client(); |
| 2560 | |
| 2561 | if (client != NULL) |
| 2562 | { |
| 2563 | /* Must use the deprecated gtk_signal_connect() for compatibility |
| 2564 | * with GNOME 1. Arrgh, zombies! */ |
| 2565 | gtk_signal_connect(GTK_OBJECT(client), "save_yourself", |
| 2566 | GTK_SIGNAL_FUNC(&sm_client_save_yourself), NULL); |
| 2567 | gtk_signal_connect(GTK_OBJECT(client), "die", |
| 2568 | GTK_SIGNAL_FUNC(&sm_client_die), NULL); |
| 2569 | } |
| 2570 | } |
| 2571 | |
| 2572 | #else /* !(FEAT_GUI_GNOME && FEAT_SESSION) */ |
| 2573 | |
| 2574 | # ifdef USE_XSMP |
| 2575 | /* |
| 2576 | * GTK tells us that XSMP needs attention |
| 2577 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2578 | static gboolean |
Bram Moolenaar | 66f948e | 2016-01-30 16:39:25 +0100 | [diff] [blame] | 2579 | local_xsmp_handle_requests( |
| 2580 | GIOChannel *source UNUSED, |
| 2581 | GIOCondition condition, |
| 2582 | gpointer data) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2583 | { |
| 2584 | if (condition == G_IO_IN) |
| 2585 | { |
| 2586 | /* Do stuff; maybe close connection */ |
| 2587 | if (xsmp_handle_requests() == FAIL) |
| 2588 | g_io_channel_unref((GIOChannel *)data); |
| 2589 | return TRUE; |
| 2590 | } |
| 2591 | /* Error */ |
| 2592 | g_io_channel_unref((GIOChannel *)data); |
| 2593 | xsmp_close(); |
| 2594 | return TRUE; |
| 2595 | } |
| 2596 | # endif /* USE_XSMP */ |
| 2597 | |
| 2598 | /* |
| 2599 | * Setup the WM_PROTOCOLS to indicate we want the WM_SAVE_YOURSELF event. |
| 2600 | * This is an ugly use of X functions. GTK doesn't offer an alternative. |
| 2601 | */ |
| 2602 | static void |
| 2603 | setup_save_yourself(void) |
| 2604 | { |
| 2605 | Atom *existing_atoms = NULL; |
| 2606 | int count = 0; |
| 2607 | |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 2608 | # ifdef USE_XSMP |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2609 | if (xsmp_icefd != -1) |
| 2610 | { |
| 2611 | /* |
| 2612 | * Use XSMP is preference to legacy WM_SAVE_YOURSELF; |
| 2613 | * set up GTK IO monitor |
| 2614 | */ |
| 2615 | GIOChannel *g_io = g_io_channel_unix_new(xsmp_icefd); |
| 2616 | |
| 2617 | g_io_add_watch(g_io, G_IO_IN | G_IO_ERR | G_IO_HUP, |
| 2618 | local_xsmp_handle_requests, (gpointer)g_io); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 2619 | g_io_channel_unref(g_io); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2620 | } |
| 2621 | else |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 2622 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2623 | { |
| 2624 | /* Fall back to old method */ |
| 2625 | |
| 2626 | /* first get the existing value */ |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 2627 | # if GTK_CHECK_VERSION(3,0,0) |
| 2628 | GdkWindow * const mainwin_win = gtk_widget_get_window(gui.mainwin); |
| 2629 | |
| 2630 | if (XGetWMProtocols(GDK_WINDOW_XDISPLAY(mainwin_win), |
| 2631 | GDK_WINDOW_XID(mainwin_win), |
| 2632 | &existing_atoms, &count)) |
| 2633 | # else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2634 | if (XGetWMProtocols(GDK_WINDOW_XDISPLAY(gui.mainwin->window), |
| 2635 | GDK_WINDOW_XWINDOW(gui.mainwin->window), |
| 2636 | &existing_atoms, &count)) |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 2637 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2638 | { |
| 2639 | Atom *new_atoms; |
| 2640 | Atom save_yourself_xatom; |
| 2641 | int i; |
| 2642 | |
| 2643 | save_yourself_xatom = GET_X_ATOM(save_yourself_atom); |
| 2644 | |
| 2645 | /* check if WM_SAVE_YOURSELF isn't there yet */ |
| 2646 | for (i = 0; i < count; ++i) |
| 2647 | if (existing_atoms[i] == save_yourself_xatom) |
| 2648 | break; |
| 2649 | |
| 2650 | if (i == count) |
| 2651 | { |
| 2652 | /* allocate an Atoms array which is one item longer */ |
| 2653 | new_atoms = (Atom *)alloc((unsigned)((count + 1) |
| 2654 | * sizeof(Atom))); |
| 2655 | if (new_atoms != NULL) |
| 2656 | { |
| 2657 | memcpy(new_atoms, existing_atoms, count * sizeof(Atom)); |
| 2658 | new_atoms[count] = save_yourself_xatom; |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 2659 | # if GTK_CHECK_VERSION(3,0,0) |
| 2660 | XSetWMProtocols(GDK_WINDOW_XDISPLAY(mainwin_win), |
| 2661 | GDK_WINDOW_XID(mainwin_win), |
| 2662 | # else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2663 | XSetWMProtocols(GDK_WINDOW_XDISPLAY(gui.mainwin->window), |
| 2664 | GDK_WINDOW_XWINDOW(gui.mainwin->window), |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 2665 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2666 | new_atoms, count + 1); |
| 2667 | vim_free(new_atoms); |
| 2668 | } |
| 2669 | } |
| 2670 | XFree(existing_atoms); |
| 2671 | } |
| 2672 | } |
| 2673 | } |
| 2674 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2675 | /* |
| 2676 | * Installing a global event filter seems to be the only way to catch |
| 2677 | * client messages of type WM_PROTOCOLS without overriding GDK's own |
| 2678 | * client message event filter. Well, that's still better than trying |
| 2679 | * 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] | 2680 | * |
| 2681 | * GTK2_FIXME: This doesn't seem to work. For some reason we never |
| 2682 | * receive WM_SAVE_YOURSELF even though everything is set up correctly. |
| 2683 | * I have the nasty feeling modern session managers just don't send this |
| 2684 | * deprecated message anymore. Addition: confirmed by several people. |
| 2685 | * |
| 2686 | * The GNOME session support is much cooler anyway. Unlike this ugly |
| 2687 | * WM_SAVE_YOURSELF hack it actually stores the session... And yes, |
| 2688 | * it should work with KDE as well. |
| 2689 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2690 | static GdkFilterReturn |
Bram Moolenaar | b85cb21 | 2009-05-17 14:24:23 +0000 | [diff] [blame] | 2691 | global_event_filter(GdkXEvent *xev, |
| 2692 | GdkEvent *event UNUSED, |
| 2693 | gpointer data UNUSED) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2694 | { |
| 2695 | XEvent *xevent = (XEvent *)xev; |
| 2696 | |
| 2697 | if (xevent != NULL |
| 2698 | && xevent->type == ClientMessage |
| 2699 | && xevent->xclient.message_type == GET_X_ATOM(wm_protocols_atom) |
Bram Moolenaar | b85cb21 | 2009-05-17 14:24:23 +0000 | [diff] [blame] | 2700 | && (long_u)xevent->xclient.data.l[0] |
| 2701 | == GET_X_ATOM(save_yourself_atom)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2702 | { |
| 2703 | out_flush(); |
| 2704 | ml_sync_all(FALSE, FALSE); /* preserve all swap files */ |
| 2705 | /* |
| 2706 | * Set the window's WM_COMMAND property, to let the window manager |
| 2707 | * know we are done saving ourselves. We don't want to be |
| 2708 | * restarted, thus set argv to NULL. |
| 2709 | */ |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 2710 | # if GTK_CHECK_VERSION(3,0,0) |
| 2711 | XSetCommand(GDK_WINDOW_XDISPLAY(gtk_widget_get_window(gui.mainwin)), |
| 2712 | GDK_WINDOW_XID(gtk_widget_get_window(gui.mainwin)), |
| 2713 | # else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2714 | XSetCommand(GDK_WINDOW_XDISPLAY(gui.mainwin->window), |
| 2715 | GDK_WINDOW_XWINDOW(gui.mainwin->window), |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 2716 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2717 | NULL, 0); |
| 2718 | return GDK_FILTER_REMOVE; |
| 2719 | } |
| 2720 | |
| 2721 | return GDK_FILTER_CONTINUE; |
| 2722 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2723 | #endif /* !(FEAT_GUI_GNOME && FEAT_SESSION) */ |
| 2724 | |
| 2725 | |
| 2726 | /* |
| 2727 | * Setup the window icon & xcmdsrv comm after the main window has been realized. |
| 2728 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2729 | static void |
Bram Moolenaar | b85cb21 | 2009-05-17 14:24:23 +0000 | [diff] [blame] | 2730 | mainwin_realize(GtkWidget *widget UNUSED, gpointer data UNUSED) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2731 | { |
| 2732 | /* If you get an error message here, you still need to unpack the runtime |
| 2733 | * archive! */ |
| 2734 | #ifdef magick |
| 2735 | # undef magick |
| 2736 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2737 | /* A bit hackish, but avoids casting later and allows optimization */ |
| 2738 | # define static static const |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2739 | #define magick vim32x32 |
| 2740 | #include "../runtime/vim32x32.xpm" |
| 2741 | #undef magick |
| 2742 | #define magick vim16x16 |
| 2743 | #include "../runtime/vim16x16.xpm" |
| 2744 | #undef magick |
| 2745 | #define magick vim48x48 |
| 2746 | #include "../runtime/vim48x48.xpm" |
| 2747 | #undef magick |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2748 | # undef static |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2749 | |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 2750 | #if GTK_CHECK_VERSION(3,0,0) |
| 2751 | GdkWindow * const mainwin_win = gtk_widget_get_window(gui.mainwin); |
| 2752 | #endif |
| 2753 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2754 | /* When started with "--echo-wid" argument, write window ID on stdout. */ |
| 2755 | if (echo_wid_arg) |
| 2756 | { |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 2757 | #if GTK_CHECK_VERSION(3,0,0) |
| 2758 | printf("WID: %ld\n", (long)GDK_WINDOW_XID(mainwin_win)); |
| 2759 | #else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2760 | printf("WID: %ld\n", (long)GDK_WINDOW_XWINDOW(gui.mainwin->window)); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 2761 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2762 | fflush(stdout); |
| 2763 | } |
| 2764 | |
| 2765 | if (vim_strchr(p_go, GO_ICON) != NULL) |
| 2766 | { |
| 2767 | /* |
| 2768 | * Add an icon to the main window. For fun and convenience of the user. |
| 2769 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2770 | GList *icons = NULL; |
| 2771 | |
| 2772 | icons = g_list_prepend(icons, gdk_pixbuf_new_from_xpm_data(vim16x16)); |
| 2773 | icons = g_list_prepend(icons, gdk_pixbuf_new_from_xpm_data(vim32x32)); |
| 2774 | icons = g_list_prepend(icons, gdk_pixbuf_new_from_xpm_data(vim48x48)); |
| 2775 | |
| 2776 | gtk_window_set_icon_list(GTK_WINDOW(gui.mainwin), icons); |
| 2777 | |
| 2778 | g_list_foreach(icons, (GFunc)&g_object_unref, NULL); |
| 2779 | g_list_free(icons); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2780 | } |
| 2781 | |
| 2782 | #if !(defined(FEAT_GUI_GNOME) && defined(FEAT_SESSION)) |
| 2783 | /* 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] | 2784 | gdk_window_add_filter(NULL, &global_event_filter, NULL); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2785 | #endif |
| 2786 | /* Setup to indicate to the window manager that we want to catch the |
| 2787 | * WM_SAVE_YOURSELF event. For GNOME, this connects to the session |
| 2788 | * manager instead. */ |
| 2789 | #if defined(FEAT_GUI_GNOME) && defined(FEAT_SESSION) |
| 2790 | if (using_gnome) |
| 2791 | #endif |
| 2792 | setup_save_yourself(); |
| 2793 | |
| 2794 | #ifdef FEAT_CLIENTSERVER |
| 2795 | if (serverName == NULL && serverDelayedStartName != NULL) |
| 2796 | { |
| 2797 | /* 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] | 2798 | # if GTK_CHECK_VERSION(3,0,0) |
| 2799 | commWindow = GDK_WINDOW_XID(mainwin_win); |
| 2800 | |
| 2801 | (void)serverRegisterName(GDK_WINDOW_XDISPLAY(mainwin_win), |
| 2802 | serverDelayedStartName); |
| 2803 | # else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2804 | commWindow = GDK_WINDOW_XWINDOW(gui.mainwin->window); |
| 2805 | |
| 2806 | (void)serverRegisterName(GDK_WINDOW_XDISPLAY(gui.mainwin->window), |
| 2807 | serverDelayedStartName); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 2808 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2809 | } |
| 2810 | else |
| 2811 | { |
| 2812 | /* |
| 2813 | * Cannot handle "XLib-only" windows with gtk event routines, we'll |
| 2814 | * have to change the "server" registration to that of the main window |
Bram Moolenaar | b463e8d | 2017-06-05 15:07:09 +0200 | [diff] [blame] | 2815 | * If we have not registered a name yet, remember the window. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2816 | */ |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 2817 | # if GTK_CHECK_VERSION(3,0,0) |
| 2818 | serverChangeRegisteredWindow(GDK_WINDOW_XDISPLAY(mainwin_win), |
| 2819 | GDK_WINDOW_XID(mainwin_win)); |
| 2820 | # else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2821 | serverChangeRegisteredWindow(GDK_WINDOW_XDISPLAY(gui.mainwin->window), |
| 2822 | GDK_WINDOW_XWINDOW(gui.mainwin->window)); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 2823 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2824 | } |
| 2825 | gtk_widget_add_events(gui.mainwin, GDK_PROPERTY_CHANGE_MASK); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 2826 | # if GTK_CHECK_VERSION(3,0,0) |
| 2827 | g_signal_connect(G_OBJECT(gui.mainwin), "property-notify-event", |
| 2828 | G_CALLBACK(property_event), NULL); |
| 2829 | # else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2830 | gtk_signal_connect(GTK_OBJECT(gui.mainwin), "property_notify_event", |
| 2831 | GTK_SIGNAL_FUNC(property_event), NULL); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 2832 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2833 | #endif |
| 2834 | } |
| 2835 | |
| 2836 | static GdkCursor * |
| 2837 | create_blank_pointer(void) |
| 2838 | { |
| 2839 | GdkWindow *root_window = NULL; |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 2840 | #if GTK_CHECK_VERSION(3,0,0) |
| 2841 | GdkPixbuf *blank_mask; |
| 2842 | #else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2843 | GdkPixmap *blank_mask; |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 2844 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2845 | GdkCursor *cursor; |
Bram Moolenaar | 36edf06 | 2016-07-21 22:10:12 +0200 | [diff] [blame] | 2846 | #if GTK_CHECK_VERSION(3,0,0) |
| 2847 | GdkRGBA color = { 0.0, 0.0, 0.0, 1.0 }; |
| 2848 | #else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2849 | GdkColor color = { 0, 0, 0, 0 }; |
| 2850 | char blank_data[] = { 0x0 }; |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 2851 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2852 | |
Bram Moolenaar | b463e8d | 2017-06-05 15:07:09 +0200 | [diff] [blame] | 2853 | #if GTK_CHECK_VERSION(3,12,0) |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 2854 | { |
| 2855 | GdkWindow * const win = gtk_widget_get_window(gui.mainwin); |
| 2856 | GdkScreen * const scrn = gdk_window_get_screen(win); |
| 2857 | root_window = gdk_screen_get_root_window(scrn); |
| 2858 | } |
Bram Moolenaar | b463e8d | 2017-06-05 15:07:09 +0200 | [diff] [blame] | 2859 | #else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2860 | root_window = gtk_widget_get_root_window(gui.mainwin); |
| 2861 | #endif |
| 2862 | |
| 2863 | /* Create a pseudo blank pointer, which is in fact one pixel by one pixel |
| 2864 | * in size. */ |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 2865 | #if GTK_CHECK_VERSION(3,0,0) |
| 2866 | { |
| 2867 | cairo_surface_t *surf; |
| 2868 | cairo_t *cr; |
| 2869 | |
| 2870 | surf = cairo_image_surface_create(CAIRO_FORMAT_A1, 1, 1); |
| 2871 | cr = cairo_create(surf); |
| 2872 | |
Bram Moolenaar | 36edf06 | 2016-07-21 22:10:12 +0200 | [diff] [blame] | 2873 | cairo_set_source_rgba(cr, |
| 2874 | color.red, |
| 2875 | color.green, |
| 2876 | color.blue, |
| 2877 | color.alpha); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 2878 | cairo_rectangle(cr, 0, 0, 1, 1); |
| 2879 | cairo_fill(cr); |
| 2880 | cairo_destroy(cr); |
| 2881 | |
| 2882 | blank_mask = gdk_pixbuf_get_from_surface(surf, 0, 0, 1, 1); |
| 2883 | cairo_surface_destroy(surf); |
| 2884 | |
| 2885 | cursor = gdk_cursor_new_from_pixbuf(gdk_window_get_display(root_window), |
| 2886 | blank_mask, 0, 0); |
| 2887 | g_object_unref(blank_mask); |
| 2888 | } |
| 2889 | #else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2890 | blank_mask = gdk_bitmap_create_from_data(root_window, blank_data, 1, 1); |
| 2891 | cursor = gdk_cursor_new_from_pixmap(blank_mask, blank_mask, |
| 2892 | &color, &color, 0, 0); |
| 2893 | gdk_bitmap_unref(blank_mask); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 2894 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2895 | |
| 2896 | return cursor; |
| 2897 | } |
| 2898 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2899 | static void |
| 2900 | mainwin_screen_changed_cb(GtkWidget *widget, |
Bram Moolenaar | b85cb21 | 2009-05-17 14:24:23 +0000 | [diff] [blame] | 2901 | GdkScreen *previous_screen UNUSED, |
| 2902 | gpointer data UNUSED) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2903 | { |
| 2904 | if (!gtk_widget_has_screen(widget)) |
| 2905 | return; |
| 2906 | |
| 2907 | /* |
Bram Moolenaar | 4932594 | 2007-05-10 19:19:59 +0000 | [diff] [blame] | 2908 | * Recreate the invisible mouse cursor. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2909 | */ |
| 2910 | if (gui.blank_pointer != NULL) |
Bram Moolenaar | b463e8d | 2017-06-05 15:07:09 +0200 | [diff] [blame] | 2911 | #if GTK_CHECK_VERSION(3,0,0) |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 2912 | g_object_unref(G_OBJECT(gui.blank_pointer)); |
Bram Moolenaar | b463e8d | 2017-06-05 15:07:09 +0200 | [diff] [blame] | 2913 | #else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2914 | gdk_cursor_unref(gui.blank_pointer); |
Bram Moolenaar | b463e8d | 2017-06-05 15:07:09 +0200 | [diff] [blame] | 2915 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2916 | |
| 2917 | gui.blank_pointer = create_blank_pointer(); |
| 2918 | |
Bram Moolenaar | b463e8d | 2017-06-05 15:07:09 +0200 | [diff] [blame] | 2919 | #if GTK_CHECK_VERSION(3,0,0) |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 2920 | if (gui.pointer_hidden && gtk_widget_get_window(gui.drawarea) != NULL) |
| 2921 | gdk_window_set_cursor(gtk_widget_get_window(gui.drawarea), |
| 2922 | gui.blank_pointer); |
Bram Moolenaar | b463e8d | 2017-06-05 15:07:09 +0200 | [diff] [blame] | 2923 | #else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2924 | if (gui.pointer_hidden && gui.drawarea->window != NULL) |
| 2925 | gdk_window_set_cursor(gui.drawarea->window, gui.blank_pointer); |
Bram Moolenaar | b463e8d | 2017-06-05 15:07:09 +0200 | [diff] [blame] | 2926 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2927 | |
| 2928 | /* |
| 2929 | * Create a new PangoContext for this screen, and initialize it |
| 2930 | * with the current font if necessary. |
| 2931 | */ |
| 2932 | if (gui.text_context != NULL) |
| 2933 | g_object_unref(gui.text_context); |
| 2934 | |
| 2935 | gui.text_context = gtk_widget_create_pango_context(widget); |
| 2936 | pango_context_set_base_dir(gui.text_context, PANGO_DIRECTION_LTR); |
| 2937 | |
| 2938 | if (gui.norm_font != NULL) |
| 2939 | { |
Bram Moolenaar | 46c9c73 | 2004-12-12 11:37:09 +0000 | [diff] [blame] | 2940 | gui_mch_init_font(p_guifont, FALSE); |
Bram Moolenaar | 04a9d45 | 2006-03-27 21:03:26 +0000 | [diff] [blame] | 2941 | gui_set_shellsize(FALSE, FALSE, RESIZE_BOTH); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2942 | } |
| 2943 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2944 | |
| 2945 | /* |
| 2946 | * After the drawing area comes up, we calculate all colors and create the |
| 2947 | * dummy blank cursor. |
| 2948 | * |
| 2949 | * Don't try to set any VIM scrollbar sizes anywhere here. I'm relying on the |
| 2950 | * fact that the main VIM engine doesn't take them into account anywhere. |
| 2951 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2952 | static void |
Bram Moolenaar | b85cb21 | 2009-05-17 14:24:23 +0000 | [diff] [blame] | 2953 | drawarea_realize_cb(GtkWidget *widget, gpointer data UNUSED) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2954 | { |
| 2955 | GtkWidget *sbar; |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 2956 | #if GTK_CHECK_VERSION(3,0,0) |
| 2957 | GtkAllocation allocation; |
| 2958 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2959 | |
| 2960 | #ifdef FEAT_XIM |
| 2961 | xim_init(); |
| 2962 | #endif |
| 2963 | gui_mch_new_colors(); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 2964 | #if GTK_CHECK_VERSION(3,0,0) |
| 2965 | gui.surface = gdk_window_create_similar_surface( |
| 2966 | gtk_widget_get_window(widget), |
| 2967 | CAIRO_CONTENT_COLOR_ALPHA, |
| 2968 | gtk_widget_get_allocated_width(widget), |
| 2969 | gtk_widget_get_allocated_height(widget)); |
| 2970 | #else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2971 | gui.text_gc = gdk_gc_new(gui.drawarea->window); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 2972 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2973 | |
| 2974 | gui.blank_pointer = create_blank_pointer(); |
| 2975 | if (gui.pointer_hidden) |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 2976 | #if GTK_CHECK_VERSION(3,0,0) |
| 2977 | gdk_window_set_cursor(gtk_widget_get_window(widget), gui.blank_pointer); |
| 2978 | #else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2979 | gdk_window_set_cursor(widget->window, gui.blank_pointer); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 2980 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2981 | |
| 2982 | /* get the actual size of the scrollbars, if they are realized */ |
| 2983 | sbar = firstwin->w_scrollbars[SBAR_LEFT].id; |
| 2984 | if (!sbar || (!gui.which_scrollbars[SBAR_LEFT] |
| 2985 | && firstwin->w_scrollbars[SBAR_RIGHT].id)) |
| 2986 | sbar = firstwin->w_scrollbars[SBAR_RIGHT].id; |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 2987 | #if GTK_CHECK_VERSION(3,0,0) |
| 2988 | gtk_widget_get_allocation(sbar, &allocation); |
| 2989 | if (sbar && gtk_widget_get_realized(sbar) && allocation.width) |
| 2990 | gui.scrollbar_width = allocation.width; |
| 2991 | #else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2992 | if (sbar && GTK_WIDGET_REALIZED(sbar) && sbar->allocation.width) |
| 2993 | gui.scrollbar_width = sbar->allocation.width; |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 2994 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2995 | |
| 2996 | sbar = gui.bottom_sbar.id; |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 2997 | #if GTK_CHECK_VERSION(3,0,0) |
| 2998 | if (sbar && gtk_widget_get_realized(sbar) && allocation.height) |
| 2999 | gui.scrollbar_height = allocation.height; |
| 3000 | #else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3001 | if (sbar && GTK_WIDGET_REALIZED(sbar) && sbar->allocation.height) |
| 3002 | gui.scrollbar_height = sbar->allocation.height; |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 3003 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3004 | } |
| 3005 | |
| 3006 | /* |
| 3007 | * Properly clean up on shutdown. |
| 3008 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3009 | static void |
Bram Moolenaar | b85cb21 | 2009-05-17 14:24:23 +0000 | [diff] [blame] | 3010 | drawarea_unrealize_cb(GtkWidget *widget UNUSED, gpointer data UNUSED) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3011 | { |
| 3012 | /* Don't write messages to the GUI anymore */ |
| 3013 | full_screen = FALSE; |
| 3014 | |
| 3015 | #ifdef FEAT_XIM |
| 3016 | im_shutdown(); |
| 3017 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3018 | if (gui.ascii_glyphs != NULL) |
| 3019 | { |
| 3020 | pango_glyph_string_free(gui.ascii_glyphs); |
| 3021 | gui.ascii_glyphs = NULL; |
| 3022 | } |
| 3023 | if (gui.ascii_font != NULL) |
| 3024 | { |
| 3025 | g_object_unref(gui.ascii_font); |
| 3026 | gui.ascii_font = NULL; |
| 3027 | } |
| 3028 | g_object_unref(gui.text_context); |
| 3029 | gui.text_context = NULL; |
| 3030 | |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 3031 | #if GTK_CHECK_VERSION(3,0,0) |
| 3032 | if (gui.surface != NULL) |
| 3033 | { |
| 3034 | cairo_surface_destroy(gui.surface); |
| 3035 | gui.surface = NULL; |
| 3036 | } |
| 3037 | #else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3038 | g_object_unref(gui.text_gc); |
| 3039 | gui.text_gc = NULL; |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 3040 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3041 | |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 3042 | #if GTK_CHECK_VERSION(3,0,0) |
| 3043 | g_object_unref(G_OBJECT(gui.blank_pointer)); |
| 3044 | #else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3045 | gdk_cursor_unref(gui.blank_pointer); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 3046 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3047 | gui.blank_pointer = NULL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3048 | } |
| 3049 | |
Bram Moolenaar | a859f04 | 2016-11-17 19:11:55 +0100 | [diff] [blame] | 3050 | #if GTK_CHECK_VERSION(3,22,2) |
| 3051 | static void |
| 3052 | drawarea_style_updated_cb(GtkWidget *widget UNUSED, |
| 3053 | gpointer data UNUSED) |
| 3054 | #else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3055 | static void |
Bram Moolenaar | b85cb21 | 2009-05-17 14:24:23 +0000 | [diff] [blame] | 3056 | drawarea_style_set_cb(GtkWidget *widget UNUSED, |
| 3057 | GtkStyle *previous_style UNUSED, |
| 3058 | gpointer data UNUSED) |
Bram Moolenaar | a859f04 | 2016-11-17 19:11:55 +0100 | [diff] [blame] | 3059 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3060 | { |
| 3061 | gui_mch_new_colors(); |
| 3062 | } |
| 3063 | |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 3064 | #if GTK_CHECK_VERSION(3,0,0) |
| 3065 | static gboolean |
| 3066 | drawarea_configure_event_cb(GtkWidget *widget, |
| 3067 | GdkEventConfigure *event, |
| 3068 | gpointer data UNUSED) |
| 3069 | { |
| 3070 | static int cur_width = 0; |
| 3071 | static int cur_height = 0; |
| 3072 | |
| 3073 | g_return_val_if_fail(event |
| 3074 | && event->width >= 1 && event->height >= 1, TRUE); |
| 3075 | |
Bram Moolenaar | 182707a | 2016-11-21 20:55:58 +0100 | [diff] [blame] | 3076 | # 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] | 3077 | /* As of 3.22.2, GdkWindows have started distributing configure events to |
| 3078 | * their "native" children (https://git.gnome.org/browse/gtk+/commit/?h=gtk-3-22&id=12579fe71b3b8f79eb9c1b80e429443bcc437dd0). |
| 3079 | * |
| 3080 | * As can be seen from the implementation of move_native_children() and |
| 3081 | * configure_native_child() in gdkwindow.c, those functions actually |
| 3082 | * propagate configure events to every child, failing to distinguish |
| 3083 | * "native" one from non-native one. |
| 3084 | * |
| 3085 | * Naturally, configure events propagated to here like that are fallacious |
| 3086 | * and, as a matter of fact, they trigger a geometric collapse of |
| 3087 | * gui.drawarea in fullscreen and miximized modes. |
| 3088 | * |
| 3089 | * To filter out such nuisance events, we are making use of the fact that |
| 3090 | * the field send_event of such GdkEventConfigures is set to FALSE in |
| 3091 | * configure_native_child(). |
| 3092 | * |
| 3093 | * Obviously, this is a terrible hack making GVim depend on GTK's |
| 3094 | * implementation details. Therefore, watch out any relevant internal |
| 3095 | * changes happening in GTK in the feature (sigh). |
| 3096 | */ |
Bram Moolenaar | 182707a | 2016-11-21 20:55:58 +0100 | [diff] [blame] | 3097 | /* Follow-up |
| 3098 | * After a few weeks later, the GdkWindow change mentioned above was |
| 3099 | * reverted (https://git.gnome.org/browse/gtk+/commit/?h=gtk-3-22&id=f70039cb9603a02d2369fec4038abf40a1711155). |
| 3100 | * The corresponding official release is 3.22.4. */ |
Bram Moolenaar | a859f04 | 2016-11-17 19:11:55 +0100 | [diff] [blame] | 3101 | if (event->send_event == FALSE) |
| 3102 | return TRUE; |
| 3103 | # endif |
| 3104 | |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 3105 | if (event->width == cur_width && event->height == cur_height) |
| 3106 | return TRUE; |
| 3107 | |
| 3108 | cur_width = event->width; |
| 3109 | cur_height = event->height; |
| 3110 | |
| 3111 | if (gui.surface != NULL) |
| 3112 | cairo_surface_destroy(gui.surface); |
| 3113 | |
| 3114 | gui.surface = gdk_window_create_similar_surface( |
| 3115 | gtk_widget_get_window(widget), |
| 3116 | CAIRO_CONTENT_COLOR_ALPHA, |
| 3117 | event->width, event->height); |
| 3118 | |
| 3119 | gtk_widget_queue_draw(widget); |
| 3120 | |
| 3121 | return TRUE; |
| 3122 | } |
| 3123 | #endif |
| 3124 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3125 | /* |
| 3126 | * Callback routine for the "delete_event" signal on the toplevel window. |
| 3127 | * Tries to vim gracefully, or refuses to exit with changed buffers. |
| 3128 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3129 | static gint |
Bram Moolenaar | b85cb21 | 2009-05-17 14:24:23 +0000 | [diff] [blame] | 3130 | delete_event_cb(GtkWidget *widget UNUSED, |
| 3131 | GdkEventAny *event UNUSED, |
| 3132 | gpointer data UNUSED) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3133 | { |
| 3134 | gui_shell_closed(); |
| 3135 | return TRUE; |
| 3136 | } |
| 3137 | |
Bram Moolenaar | 32466aa | 2006-02-24 23:53:04 +0000 | [diff] [blame] | 3138 | #if defined(FEAT_MENU) || defined(FEAT_TOOLBAR) || defined(FEAT_GUI_TABLINE) |
| 3139 | static int |
| 3140 | get_item_dimensions(GtkWidget *widget, GtkOrientation orientation) |
| 3141 | { |
Bram Moolenaar | c4a249a | 2017-01-30 22:56:48 +0100 | [diff] [blame] | 3142 | # ifdef FEAT_GUI_GNOME |
Bram Moolenaar | 32466aa | 2006-02-24 23:53:04 +0000 | [diff] [blame] | 3143 | GtkOrientation item_orientation = GTK_ORIENTATION_HORIZONTAL; |
| 3144 | |
Bram Moolenaar | 32466aa | 2006-02-24 23:53:04 +0000 | [diff] [blame] | 3145 | if (using_gnome && widget != NULL) |
| 3146 | { |
Bram Moolenaar | c9b4b05 | 2006-04-30 18:54:39 +0000 | [diff] [blame] | 3147 | GtkWidget *parent; |
Bram Moolenaar | 32466aa | 2006-02-24 23:53:04 +0000 | [diff] [blame] | 3148 | BonoboDockItem *dockitem; |
| 3149 | |
Bram Moolenaar | b3656ed | 2006-03-20 21:59:49 +0000 | [diff] [blame] | 3150 | parent = gtk_widget_get_parent(widget); |
Bram Moolenaar | c9b4b05 | 2006-04-30 18:54:39 +0000 | [diff] [blame] | 3151 | if (G_TYPE_FROM_INSTANCE(parent) == BONOBO_TYPE_DOCK_ITEM) |
| 3152 | { |
| 3153 | /* Only menu & toolbar are dock items. Could tabline be? |
| 3154 | * Seem to be only the 2 defined in GNOME */ |
| 3155 | widget = parent; |
| 3156 | dockitem = BONOBO_DOCK_ITEM(widget); |
Bram Moolenaar | 32466aa | 2006-02-24 23:53:04 +0000 | [diff] [blame] | 3157 | |
Bram Moolenaar | c9b4b05 | 2006-04-30 18:54:39 +0000 | [diff] [blame] | 3158 | if (dockitem == NULL || dockitem->is_floating) |
| 3159 | return 0; |
| 3160 | item_orientation = bonobo_dock_item_get_orientation(dockitem); |
| 3161 | } |
Bram Moolenaar | 32466aa | 2006-02-24 23:53:04 +0000 | [diff] [blame] | 3162 | } |
Bram Moolenaar | c4a249a | 2017-01-30 22:56:48 +0100 | [diff] [blame] | 3163 | # else |
| 3164 | # define item_orientation GTK_ORIENTATION_HORIZONTAL |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 3165 | # endif |
Bram Moolenaar | c4a249a | 2017-01-30 22:56:48 +0100 | [diff] [blame] | 3166 | |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 3167 | # if GTK_CHECK_VERSION(3,0,0) |
| 3168 | if (widget != NULL |
| 3169 | && item_orientation == orientation |
| 3170 | && gtk_widget_get_realized(widget) |
| 3171 | && gtk_widget_get_visible(widget)) |
| 3172 | # else |
Bram Moolenaar | 32466aa | 2006-02-24 23:53:04 +0000 | [diff] [blame] | 3173 | if (widget != NULL |
| 3174 | && item_orientation == orientation |
| 3175 | && GTK_WIDGET_REALIZED(widget) |
| 3176 | && GTK_WIDGET_VISIBLE(widget)) |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 3177 | # endif |
Bram Moolenaar | 32466aa | 2006-02-24 23:53:04 +0000 | [diff] [blame] | 3178 | { |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 3179 | # if GTK_CHECK_VERSION(3,0,0) |
| 3180 | GtkAllocation allocation; |
| 3181 | |
| 3182 | gtk_widget_get_allocation(widget, &allocation); |
Bram Moolenaar | c4a249a | 2017-01-30 22:56:48 +0100 | [diff] [blame] | 3183 | return allocation.height; |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 3184 | # else |
Bram Moolenaar | c4a249a | 2017-01-30 22:56:48 +0100 | [diff] [blame] | 3185 | # ifdef FEAT_GUI_GNOME |
Bram Moolenaar | 32466aa | 2006-02-24 23:53:04 +0000 | [diff] [blame] | 3186 | if (orientation == GTK_ORIENTATION_HORIZONTAL) |
| 3187 | return widget->allocation.height; |
| 3188 | else |
| 3189 | return widget->allocation.width; |
Bram Moolenaar | c4a249a | 2017-01-30 22:56:48 +0100 | [diff] [blame] | 3190 | # else |
| 3191 | return widget->allocation.height; |
| 3192 | # endif |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 3193 | # endif |
Bram Moolenaar | 32466aa | 2006-02-24 23:53:04 +0000 | [diff] [blame] | 3194 | } |
| 3195 | return 0; |
| 3196 | } |
| 3197 | #endif |
| 3198 | |
| 3199 | static int |
| 3200 | get_menu_tool_width(void) |
| 3201 | { |
| 3202 | int width = 0; |
| 3203 | |
| 3204 | #ifdef FEAT_GUI_GNOME /* these are never vertical without GNOME */ |
| 3205 | # ifdef FEAT_MENU |
| 3206 | width += get_item_dimensions(gui.menubar, GTK_ORIENTATION_VERTICAL); |
| 3207 | # endif |
| 3208 | # ifdef FEAT_TOOLBAR |
| 3209 | width += get_item_dimensions(gui.toolbar, GTK_ORIENTATION_VERTICAL); |
| 3210 | # endif |
| 3211 | # ifdef FEAT_GUI_TABLINE |
Bram Moolenaar | 97b2ad3 | 2006-03-18 21:40:56 +0000 | [diff] [blame] | 3212 | if (gui.tabline != NULL) |
| 3213 | width += get_item_dimensions(gui.tabline, GTK_ORIENTATION_VERTICAL); |
Bram Moolenaar | 32466aa | 2006-02-24 23:53:04 +0000 | [diff] [blame] | 3214 | # endif |
| 3215 | #endif |
| 3216 | |
| 3217 | return width; |
| 3218 | } |
| 3219 | |
| 3220 | static int |
| 3221 | get_menu_tool_height(void) |
| 3222 | { |
| 3223 | int height = 0; |
| 3224 | |
| 3225 | #ifdef FEAT_MENU |
| 3226 | height += get_item_dimensions(gui.menubar, GTK_ORIENTATION_HORIZONTAL); |
| 3227 | #endif |
| 3228 | #ifdef FEAT_TOOLBAR |
| 3229 | height += get_item_dimensions(gui.toolbar, GTK_ORIENTATION_HORIZONTAL); |
| 3230 | #endif |
| 3231 | #ifdef FEAT_GUI_TABLINE |
Bram Moolenaar | 97b2ad3 | 2006-03-18 21:40:56 +0000 | [diff] [blame] | 3232 | if (gui.tabline != NULL) |
| 3233 | height += get_item_dimensions(gui.tabline, GTK_ORIENTATION_HORIZONTAL); |
Bram Moolenaar | 32466aa | 2006-02-24 23:53:04 +0000 | [diff] [blame] | 3234 | #endif |
| 3235 | |
| 3236 | return height; |
| 3237 | } |
| 3238 | |
Bram Moolenaar | b3656ed | 2006-03-20 21:59:49 +0000 | [diff] [blame] | 3239 | /* This controls whether we can set the real window hints at |
| 3240 | * start-up when in a GtkPlug. |
| 3241 | * 0 = normal processing (default) |
| 3242 | * 1 = init. hints set, no-one's tried to reset since last check |
| 3243 | * 2 = init. hints set, attempt made to change hints |
| 3244 | */ |
| 3245 | static int init_window_hints_state = 0; |
| 3246 | |
Bram Moolenaar | 32466aa | 2006-02-24 23:53:04 +0000 | [diff] [blame] | 3247 | static void |
Bram Moolenaar | b3656ed | 2006-03-20 21:59:49 +0000 | [diff] [blame] | 3248 | update_window_manager_hints(int force_width, int force_height) |
Bram Moolenaar | 32466aa | 2006-02-24 23:53:04 +0000 | [diff] [blame] | 3249 | { |
| 3250 | static int old_width = 0; |
| 3251 | static int old_height = 0; |
Bram Moolenaar | b3656ed | 2006-03-20 21:59:49 +0000 | [diff] [blame] | 3252 | static int old_min_width = 0; |
| 3253 | static int old_min_height = 0; |
Bram Moolenaar | 32466aa | 2006-02-24 23:53:04 +0000 | [diff] [blame] | 3254 | static int old_char_width = 0; |
| 3255 | static int old_char_height = 0; |
| 3256 | |
| 3257 | int width; |
| 3258 | int height; |
Bram Moolenaar | b3656ed | 2006-03-20 21:59:49 +0000 | [diff] [blame] | 3259 | int min_width; |
| 3260 | int min_height; |
| 3261 | |
| 3262 | /* At start-up, don't try to set the hints until the initial |
| 3263 | * values have been used (those that dictate our initial size) |
Bram Moolenaar | b85cb21 | 2009-05-17 14:24:23 +0000 | [diff] [blame] | 3264 | * Let forced (i.e., correct) values through always. |
Bram Moolenaar | b3656ed | 2006-03-20 21:59:49 +0000 | [diff] [blame] | 3265 | */ |
| 3266 | if (!(force_width && force_height) && init_window_hints_state > 0) |
| 3267 | { |
Bram Moolenaar | c9b4b05 | 2006-04-30 18:54:39 +0000 | [diff] [blame] | 3268 | /* Don't do it! */ |
| 3269 | init_window_hints_state = 2; |
| 3270 | return; |
Bram Moolenaar | b3656ed | 2006-03-20 21:59:49 +0000 | [diff] [blame] | 3271 | } |
Bram Moolenaar | 32466aa | 2006-02-24 23:53:04 +0000 | [diff] [blame] | 3272 | |
| 3273 | /* This also needs to be done when the main window isn't there yet, |
| 3274 | * otherwise the hints don't work. */ |
| 3275 | width = gui_get_base_width(); |
| 3276 | height = gui_get_base_height(); |
| 3277 | # ifdef FEAT_MENU |
| 3278 | height += tabline_height() * gui.char_height; |
| 3279 | # endif |
Bram Moolenaar | 32466aa | 2006-02-24 23:53:04 +0000 | [diff] [blame] | 3280 | width += get_menu_tool_width(); |
| 3281 | height += get_menu_tool_height(); |
Bram Moolenaar | 32466aa | 2006-02-24 23:53:04 +0000 | [diff] [blame] | 3282 | |
Bram Moolenaar | b3656ed | 2006-03-20 21:59:49 +0000 | [diff] [blame] | 3283 | /* GtkSockets use GtkPlug's [gui,mainwin] min-size hints to determine |
Bram Moolenaar | 4932594 | 2007-05-10 19:19:59 +0000 | [diff] [blame] | 3284 | * their actual widget size. When we set our size ourselves (e.g., |
Bram Moolenaar | b3656ed | 2006-03-20 21:59:49 +0000 | [diff] [blame] | 3285 | * 'set columns=' or init. -geom) we briefly set the min. to the size |
| 3286 | * we wish to be instead of the legitimate minimum so that we actually |
| 3287 | * resize correctly. |
| 3288 | */ |
| 3289 | if (force_width && force_height) |
| 3290 | { |
Bram Moolenaar | c9b4b05 | 2006-04-30 18:54:39 +0000 | [diff] [blame] | 3291 | min_width = force_width; |
| 3292 | min_height = force_height; |
Bram Moolenaar | b3656ed | 2006-03-20 21:59:49 +0000 | [diff] [blame] | 3293 | } |
| 3294 | else |
| 3295 | { |
Bram Moolenaar | c9b4b05 | 2006-04-30 18:54:39 +0000 | [diff] [blame] | 3296 | min_width = width + MIN_COLUMNS * gui.char_width; |
| 3297 | min_height = height + MIN_LINES * gui.char_height; |
Bram Moolenaar | b3656ed | 2006-03-20 21:59:49 +0000 | [diff] [blame] | 3298 | } |
| 3299 | |
Bram Moolenaar | 32466aa | 2006-02-24 23:53:04 +0000 | [diff] [blame] | 3300 | /* Avoid an expose event when the size didn't change. */ |
| 3301 | if (width != old_width |
| 3302 | || height != old_height |
Bram Moolenaar | c9b4b05 | 2006-04-30 18:54:39 +0000 | [diff] [blame] | 3303 | || min_width != old_min_width |
Bram Moolenaar | b3656ed | 2006-03-20 21:59:49 +0000 | [diff] [blame] | 3304 | || min_height != old_min_height |
Bram Moolenaar | 32466aa | 2006-02-24 23:53:04 +0000 | [diff] [blame] | 3305 | || gui.char_width != old_char_width |
| 3306 | || gui.char_height != old_char_height) |
| 3307 | { |
| 3308 | GdkGeometry geometry; |
| 3309 | GdkWindowHints geometry_mask; |
| 3310 | |
| 3311 | geometry.width_inc = gui.char_width; |
| 3312 | geometry.height_inc = gui.char_height; |
| 3313 | geometry.base_width = width; |
| 3314 | geometry.base_height = height; |
Bram Moolenaar | c9b4b05 | 2006-04-30 18:54:39 +0000 | [diff] [blame] | 3315 | geometry.min_width = min_width; |
| 3316 | geometry.min_height = min_height; |
Bram Moolenaar | 32466aa | 2006-02-24 23:53:04 +0000 | [diff] [blame] | 3317 | geometry_mask = GDK_HINT_BASE_SIZE|GDK_HINT_RESIZE_INC |
| 3318 | |GDK_HINT_MIN_SIZE; |
Bram Moolenaar | 32466aa | 2006-02-24 23:53:04 +0000 | [diff] [blame] | 3319 | /* Using gui.formwin as geometry widget doesn't work as expected |
| 3320 | * with GTK+ 2 -- dunno why. Presumably all the resizing hacks |
| 3321 | * in Vim confuse GTK+. */ |
| 3322 | gtk_window_set_geometry_hints(GTK_WINDOW(gui.mainwin), gui.mainwin, |
| 3323 | &geometry, geometry_mask); |
Bram Moolenaar | c9b4b05 | 2006-04-30 18:54:39 +0000 | [diff] [blame] | 3324 | old_width = width; |
| 3325 | old_height = height; |
| 3326 | old_min_width = min_width; |
| 3327 | old_min_height = min_height; |
| 3328 | old_char_width = gui.char_width; |
| 3329 | old_char_height = gui.char_height; |
Bram Moolenaar | 32466aa | 2006-02-24 23:53:04 +0000 | [diff] [blame] | 3330 | } |
| 3331 | } |
| 3332 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3333 | #ifdef FEAT_TOOLBAR |
| 3334 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3335 | /* |
| 3336 | * This extra effort wouldn't be necessary if we only used stock icons in the |
| 3337 | * toolbar, as we do for all builtin icons. But user-defined toolbar icons |
| 3338 | * shouldn't be treated differently, thus we do need this. |
| 3339 | */ |
| 3340 | static void |
| 3341 | icon_size_changed_foreach(GtkWidget *widget, gpointer user_data) |
| 3342 | { |
| 3343 | if (GTK_IS_IMAGE(widget)) |
| 3344 | { |
| 3345 | GtkImage *image = (GtkImage *)widget; |
| 3346 | |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 3347 | # if GTK_CHECK_VERSION(3,10,0) |
| 3348 | if (gtk_image_get_storage_type(image) == GTK_IMAGE_ICON_NAME) |
| 3349 | { |
| 3350 | const GtkIconSize icon_size = GPOINTER_TO_INT(user_data); |
| 3351 | const gchar *icon_name; |
| 3352 | |
| 3353 | gtk_image_get_icon_name(image, &icon_name, NULL); |
| 3354 | |
| 3355 | gtk_image_set_from_icon_name(image, icon_name, icon_size); |
| 3356 | } |
| 3357 | # else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3358 | /* User-defined icons are stored in a GtkIconSet */ |
| 3359 | if (gtk_image_get_storage_type(image) == GTK_IMAGE_ICON_SET) |
| 3360 | { |
| 3361 | GtkIconSet *icon_set; |
| 3362 | GtkIconSize icon_size; |
| 3363 | |
| 3364 | gtk_image_get_icon_set(image, &icon_set, &icon_size); |
| 3365 | icon_size = (GtkIconSize)(long)user_data; |
| 3366 | |
| 3367 | gtk_icon_set_ref(icon_set); |
| 3368 | gtk_image_set_from_icon_set(image, icon_set, icon_size); |
| 3369 | gtk_icon_set_unref(icon_set); |
| 3370 | } |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 3371 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3372 | } |
| 3373 | else if (GTK_IS_CONTAINER(widget)) |
| 3374 | { |
| 3375 | gtk_container_foreach((GtkContainer *)widget, |
| 3376 | &icon_size_changed_foreach, |
| 3377 | user_data); |
| 3378 | } |
| 3379 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3380 | |
| 3381 | static void |
| 3382 | set_toolbar_style(GtkToolbar *toolbar) |
| 3383 | { |
| 3384 | GtkToolbarStyle style; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3385 | GtkIconSize size; |
| 3386 | GtkIconSize oldsize; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3387 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3388 | if ((toolbar_flags & (TOOLBAR_TEXT | TOOLBAR_ICONS | TOOLBAR_HORIZ)) |
| 3389 | == (TOOLBAR_TEXT | TOOLBAR_ICONS | TOOLBAR_HORIZ)) |
| 3390 | style = GTK_TOOLBAR_BOTH_HORIZ; |
Bram Moolenaar | 182c5be | 2010-06-25 05:37:59 +0200 | [diff] [blame] | 3391 | else if ((toolbar_flags & (TOOLBAR_TEXT | TOOLBAR_ICONS)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3392 | == (TOOLBAR_TEXT | TOOLBAR_ICONS)) |
| 3393 | style = GTK_TOOLBAR_BOTH; |
| 3394 | else if (toolbar_flags & TOOLBAR_TEXT) |
| 3395 | style = GTK_TOOLBAR_TEXT; |
| 3396 | else |
| 3397 | style = GTK_TOOLBAR_ICONS; |
| 3398 | |
| 3399 | gtk_toolbar_set_style(toolbar, style); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 3400 | # if !GTK_CHECK_VERSION(3,0,0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3401 | gtk_toolbar_set_tooltips(toolbar, (toolbar_flags & TOOLBAR_TOOLTIPS) != 0); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 3402 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3403 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3404 | switch (tbis_flags) |
| 3405 | { |
| 3406 | case TBIS_TINY: size = GTK_ICON_SIZE_MENU; break; |
| 3407 | case TBIS_SMALL: size = GTK_ICON_SIZE_SMALL_TOOLBAR; break; |
| 3408 | case TBIS_MEDIUM: size = GTK_ICON_SIZE_BUTTON; break; |
| 3409 | case TBIS_LARGE: size = GTK_ICON_SIZE_LARGE_TOOLBAR; break; |
Bram Moolenaar | beb003b | 2016-03-08 22:47:17 +0100 | [diff] [blame] | 3410 | case TBIS_HUGE: size = GTK_ICON_SIZE_DND; break; |
| 3411 | case TBIS_GIANT: size = GTK_ICON_SIZE_DIALOG; break; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3412 | default: size = GTK_ICON_SIZE_INVALID; break; |
| 3413 | } |
| 3414 | oldsize = gtk_toolbar_get_icon_size(toolbar); |
| 3415 | |
| 3416 | if (size == GTK_ICON_SIZE_INVALID) |
| 3417 | { |
| 3418 | /* Let global user preferences decide the icon size. */ |
| 3419 | gtk_toolbar_unset_icon_size(toolbar); |
| 3420 | size = gtk_toolbar_get_icon_size(toolbar); |
| 3421 | } |
| 3422 | if (size != oldsize) |
| 3423 | { |
| 3424 | gtk_container_foreach(GTK_CONTAINER(toolbar), |
| 3425 | &icon_size_changed_foreach, |
| 3426 | GINT_TO_POINTER((int)size)); |
| 3427 | } |
| 3428 | gtk_toolbar_set_icon_size(toolbar, size); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3429 | } |
| 3430 | |
| 3431 | #endif /* FEAT_TOOLBAR */ |
| 3432 | |
Bram Moolenaar | 32466aa | 2006-02-24 23:53:04 +0000 | [diff] [blame] | 3433 | #if defined(FEAT_GUI_TABLINE) || defined(PROTO) |
| 3434 | static int ignore_tabline_evt = FALSE; |
Bram Moolenaar | a562149 | 2006-02-25 21:55:24 +0000 | [diff] [blame] | 3435 | static GtkWidget *tabline_menu; |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 3436 | # if !GTK_CHECK_VERSION(3,0,0) |
Bram Moolenaar | 437df8f | 2006-04-27 21:47:44 +0000 | [diff] [blame] | 3437 | static GtkTooltips *tabline_tooltip; |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 3438 | # endif |
Bram Moolenaar | a562149 | 2006-02-25 21:55:24 +0000 | [diff] [blame] | 3439 | static int clicked_page; /* page clicked in tab line */ |
| 3440 | |
| 3441 | /* |
| 3442 | * Handle selecting an item in the tab line popup menu. |
| 3443 | */ |
Bram Moolenaar | a562149 | 2006-02-25 21:55:24 +0000 | [diff] [blame] | 3444 | static void |
Bram Moolenaar | b85cb21 | 2009-05-17 14:24:23 +0000 | [diff] [blame] | 3445 | tabline_menu_handler(GtkMenuItem *item UNUSED, gpointer user_data) |
Bram Moolenaar | a562149 | 2006-02-25 21:55:24 +0000 | [diff] [blame] | 3446 | { |
Bram Moolenaar | a562149 | 2006-02-25 21:55:24 +0000 | [diff] [blame] | 3447 | /* Add the string cmd into input buffer */ |
Bram Moolenaar | c6fe919 | 2006-04-09 21:54:49 +0000 | [diff] [blame] | 3448 | send_tabline_menu_event(clicked_page, (int)(long)user_data); |
Bram Moolenaar | a562149 | 2006-02-25 21:55:24 +0000 | [diff] [blame] | 3449 | } |
| 3450 | |
Bram Moolenaar | 8ea9123 | 2006-04-28 22:41:43 +0000 | [diff] [blame] | 3451 | static void |
| 3452 | add_tabline_menu_item(GtkWidget *menu, char_u *text, int resp) |
| 3453 | { |
| 3454 | GtkWidget *item; |
| 3455 | char_u *utf_text; |
| 3456 | |
| 3457 | utf_text = CONVERT_TO_UTF8(text); |
| 3458 | item = gtk_menu_item_new_with_label((const char *)utf_text); |
| 3459 | gtk_widget_show(item); |
| 3460 | CONVERT_TO_UTF8_FREE(utf_text); |
| 3461 | |
| 3462 | gtk_container_add(GTK_CONTAINER(menu), item); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 3463 | # if GTK_CHECK_VERSION(3,0,0) |
| 3464 | g_signal_connect(G_OBJECT(item), "activate", |
| 3465 | G_CALLBACK(tabline_menu_handler), |
| 3466 | GINT_TO_POINTER(resp)); |
| 3467 | # else |
Bram Moolenaar | 8ea9123 | 2006-04-28 22:41:43 +0000 | [diff] [blame] | 3468 | gtk_signal_connect(GTK_OBJECT(item), "activate", |
| 3469 | GTK_SIGNAL_FUNC(tabline_menu_handler), |
Bram Moolenaar | 47b46d7 | 2008-07-02 19:05:48 +0000 | [diff] [blame] | 3470 | (gpointer)(long)resp); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 3471 | # endif |
Bram Moolenaar | 8ea9123 | 2006-04-28 22:41:43 +0000 | [diff] [blame] | 3472 | } |
| 3473 | |
Bram Moolenaar | a562149 | 2006-02-25 21:55:24 +0000 | [diff] [blame] | 3474 | /* |
Bram Moolenaar | a562149 | 2006-02-25 21:55:24 +0000 | [diff] [blame] | 3475 | * Create a menu for the tab line. |
| 3476 | */ |
| 3477 | static GtkWidget * |
| 3478 | create_tabline_menu(void) |
| 3479 | { |
Bram Moolenaar | 8ea9123 | 2006-04-28 22:41:43 +0000 | [diff] [blame] | 3480 | GtkWidget *menu; |
Bram Moolenaar | a562149 | 2006-02-25 21:55:24 +0000 | [diff] [blame] | 3481 | |
| 3482 | menu = gtk_menu_new(); |
Bram Moolenaar | 7098ee5 | 2015-06-09 19:14:24 +0200 | [diff] [blame] | 3483 | if (first_tabpage->tp_next != NULL) |
| 3484 | add_tabline_menu_item(menu, (char_u *)_("Close tab"), |
| 3485 | TABLINE_MENU_CLOSE); |
Bram Moolenaar | 8ea9123 | 2006-04-28 22:41:43 +0000 | [diff] [blame] | 3486 | add_tabline_menu_item(menu, (char_u *)_("New tab"), TABLINE_MENU_NEW); |
| 3487 | add_tabline_menu_item(menu, (char_u *)_("Open Tab..."), TABLINE_MENU_OPEN); |
Bram Moolenaar | a562149 | 2006-02-25 21:55:24 +0000 | [diff] [blame] | 3488 | |
| 3489 | return menu; |
| 3490 | } |
| 3491 | |
| 3492 | static gboolean |
| 3493 | on_tabline_menu(GtkWidget *widget, GdkEvent *event) |
| 3494 | { |
| 3495 | /* Was this button press event ? */ |
| 3496 | if (event->type == GDK_BUTTON_PRESS) |
| 3497 | { |
| 3498 | GdkEventButton *bevent = (GdkEventButton *)event; |
| 3499 | int x = bevent->x; |
Bram Moolenaar | 8ea9123 | 2006-04-28 22:41:43 +0000 | [diff] [blame] | 3500 | int y = bevent->y; |
| 3501 | GtkWidget *tabwidget; |
| 3502 | GdkWindow *tabwin; |
Bram Moolenaar | a562149 | 2006-02-25 21:55:24 +0000 | [diff] [blame] | 3503 | |
Bram Moolenaar | f193fff | 2006-04-27 00:02:13 +0000 | [diff] [blame] | 3504 | /* When ignoring events return TRUE so that the selected page doesn't |
| 3505 | * change. */ |
| 3506 | if (hold_gui_events |
| 3507 | # ifdef FEAT_CMDWIN |
| 3508 | || cmdwin_type != 0 |
| 3509 | # endif |
| 3510 | ) |
| 3511 | return TRUE; |
| 3512 | |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 3513 | # if GTK_CHECK_VERSION(3,0,0) |
| 3514 | tabwin = gui_gtk_window_at_position(gui.mainwin, &x, &y); |
| 3515 | # else |
Bram Moolenaar | 8ea9123 | 2006-04-28 22:41:43 +0000 | [diff] [blame] | 3516 | tabwin = gdk_window_at_pointer(&x, &y); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 3517 | # endif |
| 3518 | |
Bram Moolenaar | 8ea9123 | 2006-04-28 22:41:43 +0000 | [diff] [blame] | 3519 | gdk_window_get_user_data(tabwin, (gpointer)&tabwidget); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 3520 | # if GTK_CHECK_VERSION(3,0,0) |
| 3521 | clicked_page = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(tabwidget), |
| 3522 | "tab_num")); |
| 3523 | # else |
Bram Moolenaar | 8ea9123 | 2006-04-28 22:41:43 +0000 | [diff] [blame] | 3524 | clicked_page = (int)(long)gtk_object_get_user_data( |
| 3525 | GTK_OBJECT(tabwidget)); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 3526 | # endif |
Bram Moolenaar | a562149 | 2006-02-25 21:55:24 +0000 | [diff] [blame] | 3527 | |
| 3528 | /* If the event was generated for 3rd button popup the menu. */ |
| 3529 | if (bevent->button == 3) |
| 3530 | { |
Bram Moolenaar | a859f04 | 2016-11-17 19:11:55 +0100 | [diff] [blame] | 3531 | # if GTK_CHECK_VERSION(3,22,2) |
| 3532 | gtk_menu_popup_at_pointer(GTK_MENU(widget), event); |
| 3533 | # else |
Bram Moolenaar | a562149 | 2006-02-25 21:55:24 +0000 | [diff] [blame] | 3534 | gtk_menu_popup(GTK_MENU(widget), NULL, NULL, NULL, NULL, |
| 3535 | bevent->button, bevent->time); |
Bram Moolenaar | a859f04 | 2016-11-17 19:11:55 +0100 | [diff] [blame] | 3536 | # endif |
Bram Moolenaar | a562149 | 2006-02-25 21:55:24 +0000 | [diff] [blame] | 3537 | /* We handled the event. */ |
| 3538 | return TRUE; |
| 3539 | } |
Bram Moolenaar | 9635157 | 2006-05-05 21:16:59 +0000 | [diff] [blame] | 3540 | else if (bevent->button == 1) |
Bram Moolenaar | eddf53b | 2006-02-27 00:11:10 +0000 | [diff] [blame] | 3541 | { |
Bram Moolenaar | 9635157 | 2006-05-05 21:16:59 +0000 | [diff] [blame] | 3542 | if (clicked_page == 0) |
| 3543 | { |
Bram Moolenaar | 0735454 | 2007-09-15 12:07:46 +0000 | [diff] [blame] | 3544 | /* Click after all tabs moves to next tab page. When "x" is |
| 3545 | * small guess it's the left button. */ |
Bram Moolenaar | a3f4166 | 2010-07-11 19:01:06 +0200 | [diff] [blame] | 3546 | send_tabline_event(x < 50 ? -1 : 0); |
Bram Moolenaar | 9635157 | 2006-05-05 21:16:59 +0000 | [diff] [blame] | 3547 | } |
Bram Moolenaar | eddf53b | 2006-02-27 00:11:10 +0000 | [diff] [blame] | 3548 | } |
Bram Moolenaar | a562149 | 2006-02-25 21:55:24 +0000 | [diff] [blame] | 3549 | } |
Bram Moolenaar | f193fff | 2006-04-27 00:02:13 +0000 | [diff] [blame] | 3550 | |
Bram Moolenaar | a562149 | 2006-02-25 21:55:24 +0000 | [diff] [blame] | 3551 | /* We didn't handle the event. */ |
| 3552 | return FALSE; |
| 3553 | } |
Bram Moolenaar | 32466aa | 2006-02-24 23:53:04 +0000 | [diff] [blame] | 3554 | |
| 3555 | /* |
| 3556 | * Handle selecting one of the tabs. |
| 3557 | */ |
Bram Moolenaar | 32466aa | 2006-02-24 23:53:04 +0000 | [diff] [blame] | 3558 | static void |
| 3559 | on_select_tab( |
Bram Moolenaar | b85cb21 | 2009-05-17 14:24:23 +0000 | [diff] [blame] | 3560 | GtkNotebook *notebook UNUSED, |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 3561 | # if GTK_CHECK_VERSION(3,0,0) |
| 3562 | gpointer *page UNUSED, |
| 3563 | # else |
Bram Moolenaar | b85cb21 | 2009-05-17 14:24:23 +0000 | [diff] [blame] | 3564 | GtkNotebookPage *page UNUSED, |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 3565 | # endif |
Bram Moolenaar | 89d4032 | 2006-08-29 15:30:07 +0000 | [diff] [blame] | 3566 | gint idx, |
Bram Moolenaar | b85cb21 | 2009-05-17 14:24:23 +0000 | [diff] [blame] | 3567 | gpointer data UNUSED) |
Bram Moolenaar | 32466aa | 2006-02-24 23:53:04 +0000 | [diff] [blame] | 3568 | { |
Bram Moolenaar | 32466aa | 2006-02-24 23:53:04 +0000 | [diff] [blame] | 3569 | if (!ignore_tabline_evt) |
Bram Moolenaar | eddf53b | 2006-02-27 00:11:10 +0000 | [diff] [blame] | 3570 | { |
Bram Moolenaar | a3f4166 | 2010-07-11 19:01:06 +0200 | [diff] [blame] | 3571 | send_tabline_event(idx + 1); |
Bram Moolenaar | eddf53b | 2006-02-27 00:11:10 +0000 | [diff] [blame] | 3572 | } |
Bram Moolenaar | 32466aa | 2006-02-24 23:53:04 +0000 | [diff] [blame] | 3573 | } |
| 3574 | |
| 3575 | /* |
| 3576 | * Show or hide the tabline. |
| 3577 | */ |
| 3578 | void |
| 3579 | gui_mch_show_tabline(int showit) |
| 3580 | { |
| 3581 | if (gui.tabline == NULL) |
| 3582 | return; |
| 3583 | |
| 3584 | if (!showit != !gtk_notebook_get_show_tabs(GTK_NOTEBOOK(gui.tabline))) |
| 3585 | { |
Bram Moolenaar | 3517bb1 | 2006-03-03 22:58:45 +0000 | [diff] [blame] | 3586 | /* Note: this may cause a resize event */ |
Bram Moolenaar | 32466aa | 2006-02-24 23:53:04 +0000 | [diff] [blame] | 3587 | gtk_notebook_set_show_tabs(GTK_NOTEBOOK(gui.tabline), showit); |
Bram Moolenaar | b3656ed | 2006-03-20 21:59:49 +0000 | [diff] [blame] | 3588 | update_window_manager_hints(0, 0); |
Bram Moolenaar | 9635157 | 2006-05-05 21:16:59 +0000 | [diff] [blame] | 3589 | if (showit) |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 3590 | # if GTK_CHECK_VERSION(3,0,0) |
| 3591 | gtk_widget_set_can_focus(GTK_WIDGET(gui.tabline), FALSE); |
| 3592 | # else |
Bram Moolenaar | 9635157 | 2006-05-05 21:16:59 +0000 | [diff] [blame] | 3593 | GTK_WIDGET_UNSET_FLAGS(GTK_WIDGET(gui.tabline), GTK_CAN_FOCUS); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 3594 | # endif |
Bram Moolenaar | 32466aa | 2006-02-24 23:53:04 +0000 | [diff] [blame] | 3595 | } |
Bram Moolenaar | 9635157 | 2006-05-05 21:16:59 +0000 | [diff] [blame] | 3596 | |
| 3597 | gui_mch_update(); |
Bram Moolenaar | 32466aa | 2006-02-24 23:53:04 +0000 | [diff] [blame] | 3598 | } |
| 3599 | |
| 3600 | /* |
Bram Moolenaar | 3517bb1 | 2006-03-03 22:58:45 +0000 | [diff] [blame] | 3601 | * Return TRUE when tabline is displayed. |
| 3602 | */ |
| 3603 | int |
| 3604 | gui_mch_showing_tabline(void) |
| 3605 | { |
| 3606 | return gui.tabline != NULL |
Bram Moolenaar | 182c5be | 2010-06-25 05:37:59 +0200 | [diff] [blame] | 3607 | && gtk_notebook_get_show_tabs(GTK_NOTEBOOK(gui.tabline)); |
Bram Moolenaar | 3517bb1 | 2006-03-03 22:58:45 +0000 | [diff] [blame] | 3608 | } |
| 3609 | |
| 3610 | /* |
Bram Moolenaar | 32466aa | 2006-02-24 23:53:04 +0000 | [diff] [blame] | 3611 | * Update the labels of the tabline. |
| 3612 | */ |
| 3613 | void |
| 3614 | gui_mch_update_tabline(void) |
| 3615 | { |
| 3616 | GtkWidget *page; |
Bram Moolenaar | 437df8f | 2006-04-27 21:47:44 +0000 | [diff] [blame] | 3617 | GtkWidget *event_box; |
Bram Moolenaar | 32466aa | 2006-02-24 23:53:04 +0000 | [diff] [blame] | 3618 | GtkWidget *label; |
| 3619 | tabpage_T *tp; |
| 3620 | int nr = 0; |
Bram Moolenaar | 8ea9123 | 2006-04-28 22:41:43 +0000 | [diff] [blame] | 3621 | int tab_num; |
Bram Moolenaar | 32466aa | 2006-02-24 23:53:04 +0000 | [diff] [blame] | 3622 | int curtabidx = 0; |
Bram Moolenaar | c1e3790 | 2006-04-18 21:55:01 +0000 | [diff] [blame] | 3623 | char_u *labeltext; |
Bram Moolenaar | 32466aa | 2006-02-24 23:53:04 +0000 | [diff] [blame] | 3624 | |
| 3625 | if (gui.tabline == NULL) |
| 3626 | return; |
| 3627 | |
| 3628 | ignore_tabline_evt = TRUE; |
| 3629 | |
| 3630 | /* Add a label for each tab page. They all contain the same text area. */ |
| 3631 | for (tp = first_tabpage; tp != NULL; tp = tp->tp_next, ++nr) |
| 3632 | { |
| 3633 | if (tp == curtab) |
| 3634 | curtabidx = nr; |
| 3635 | |
Bram Moolenaar | 8ea9123 | 2006-04-28 22:41:43 +0000 | [diff] [blame] | 3636 | tab_num = nr + 1; |
| 3637 | |
Bram Moolenaar | 32466aa | 2006-02-24 23:53:04 +0000 | [diff] [blame] | 3638 | page = gtk_notebook_get_nth_page(GTK_NOTEBOOK(gui.tabline), nr); |
| 3639 | if (page == NULL) |
| 3640 | { |
| 3641 | /* Add notebook page */ |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 3642 | # if GTK_CHECK_VERSION(3,2,0) |
| 3643 | page = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0); |
| 3644 | gtk_box_set_homogeneous(GTK_BOX(page), FALSE); |
| 3645 | # else |
Bram Moolenaar | 32466aa | 2006-02-24 23:53:04 +0000 | [diff] [blame] | 3646 | page = gtk_vbox_new(FALSE, 0); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 3647 | # endif |
Bram Moolenaar | 32466aa | 2006-02-24 23:53:04 +0000 | [diff] [blame] | 3648 | gtk_widget_show(page); |
Bram Moolenaar | 437df8f | 2006-04-27 21:47:44 +0000 | [diff] [blame] | 3649 | event_box = gtk_event_box_new(); |
| 3650 | gtk_widget_show(event_box); |
Bram Moolenaar | 32466aa | 2006-02-24 23:53:04 +0000 | [diff] [blame] | 3651 | label = gtk_label_new("-Empty-"); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 3652 | # if !GTK_CHECK_VERSION(3,14,0) |
Bram Moolenaar | 8ea9123 | 2006-04-28 22:41:43 +0000 | [diff] [blame] | 3653 | gtk_misc_set_padding(GTK_MISC(label), 2, 2); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 3654 | # endif |
Bram Moolenaar | 437df8f | 2006-04-27 21:47:44 +0000 | [diff] [blame] | 3655 | gtk_container_add(GTK_CONTAINER(event_box), label); |
Bram Moolenaar | 32466aa | 2006-02-24 23:53:04 +0000 | [diff] [blame] | 3656 | gtk_widget_show(label); |
| 3657 | gtk_notebook_insert_page(GTK_NOTEBOOK(gui.tabline), |
| 3658 | page, |
Bram Moolenaar | 437df8f | 2006-04-27 21:47:44 +0000 | [diff] [blame] | 3659 | event_box, |
Bram Moolenaar | 32466aa | 2006-02-24 23:53:04 +0000 | [diff] [blame] | 3660 | nr++); |
| 3661 | } |
| 3662 | |
Bram Moolenaar | 437df8f | 2006-04-27 21:47:44 +0000 | [diff] [blame] | 3663 | event_box = gtk_notebook_get_tab_label(GTK_NOTEBOOK(gui.tabline), page); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 3664 | # if GTK_CHECK_VERSION(3,0,0) |
| 3665 | g_object_set_data(G_OBJECT(event_box), "tab_num", |
| 3666 | GINT_TO_POINTER(tab_num)); |
| 3667 | # else |
Bram Moolenaar | 47b46d7 | 2008-07-02 19:05:48 +0000 | [diff] [blame] | 3668 | gtk_object_set_user_data(GTK_OBJECT(event_box), |
| 3669 | (gpointer)(long)tab_num); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 3670 | # endif |
| 3671 | # if GTK_CHECK_VERSION(3,0,0) |
| 3672 | label = gtk_bin_get_child(GTK_BIN(event_box)); |
| 3673 | # else |
Bram Moolenaar | 437df8f | 2006-04-27 21:47:44 +0000 | [diff] [blame] | 3674 | label = GTK_BIN(event_box)->child; |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 3675 | # endif |
Bram Moolenaar | 57657d8 | 2006-04-21 22:12:41 +0000 | [diff] [blame] | 3676 | get_tabline_label(tp, FALSE); |
Bram Moolenaar | c1e3790 | 2006-04-18 21:55:01 +0000 | [diff] [blame] | 3677 | labeltext = CONVERT_TO_UTF8(NameBuff); |
Bram Moolenaar | 437df8f | 2006-04-27 21:47:44 +0000 | [diff] [blame] | 3678 | gtk_label_set_text(GTK_LABEL(label), (const char *)labeltext); |
Bram Moolenaar | c1e3790 | 2006-04-18 21:55:01 +0000 | [diff] [blame] | 3679 | CONVERT_TO_UTF8_FREE(labeltext); |
Bram Moolenaar | 437df8f | 2006-04-27 21:47:44 +0000 | [diff] [blame] | 3680 | |
Bram Moolenaar | 437df8f | 2006-04-27 21:47:44 +0000 | [diff] [blame] | 3681 | get_tabline_label(tp, TRUE); |
| 3682 | labeltext = CONVERT_TO_UTF8(NameBuff); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 3683 | # if GTK_CHECK_VERSION(3,0,0) |
| 3684 | gtk_widget_set_tooltip_text(event_box, (const gchar *)labeltext); |
| 3685 | # else |
Bram Moolenaar | 437df8f | 2006-04-27 21:47:44 +0000 | [diff] [blame] | 3686 | gtk_tooltips_set_tip(GTK_TOOLTIPS(tabline_tooltip), event_box, |
| 3687 | (const char *)labeltext, NULL); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 3688 | # endif |
Bram Moolenaar | 437df8f | 2006-04-27 21:47:44 +0000 | [diff] [blame] | 3689 | CONVERT_TO_UTF8_FREE(labeltext); |
Bram Moolenaar | 32466aa | 2006-02-24 23:53:04 +0000 | [diff] [blame] | 3690 | } |
| 3691 | |
| 3692 | /* Remove any old labels. */ |
| 3693 | while (gtk_notebook_get_nth_page(GTK_NOTEBOOK(gui.tabline), nr) != NULL) |
| 3694 | gtk_notebook_remove_page(GTK_NOTEBOOK(gui.tabline), nr); |
| 3695 | |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 3696 | # if GTK_CHECK_VERSION(3,0,0) |
| 3697 | if (gtk_notebook_get_current_page(GTK_NOTEBOOK(gui.tabline)) != curtabidx) |
| 3698 | gtk_notebook_set_current_page(GTK_NOTEBOOK(gui.tabline), curtabidx); |
| 3699 | # else |
Bram Moolenaar | 32466aa | 2006-02-24 23:53:04 +0000 | [diff] [blame] | 3700 | if (gtk_notebook_current_page(GTK_NOTEBOOK(gui.tabline)) != curtabidx) |
Bram Moolenaar | c9b4b05 | 2006-04-30 18:54:39 +0000 | [diff] [blame] | 3701 | gtk_notebook_set_page(GTK_NOTEBOOK(gui.tabline), curtabidx); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 3702 | # endif |
Bram Moolenaar | 32466aa | 2006-02-24 23:53:04 +0000 | [diff] [blame] | 3703 | |
Bram Moolenaar | 8ea9123 | 2006-04-28 22:41:43 +0000 | [diff] [blame] | 3704 | /* Make sure everything is in place before drawing text. */ |
| 3705 | gui_mch_update(); |
| 3706 | |
Bram Moolenaar | 32466aa | 2006-02-24 23:53:04 +0000 | [diff] [blame] | 3707 | ignore_tabline_evt = FALSE; |
| 3708 | } |
| 3709 | |
| 3710 | /* |
| 3711 | * Set the current tab to "nr". First tab is 1. |
| 3712 | */ |
| 3713 | void |
Bram Moolenaar | 66f948e | 2016-01-30 16:39:25 +0100 | [diff] [blame] | 3714 | gui_mch_set_curtab(int nr) |
Bram Moolenaar | 32466aa | 2006-02-24 23:53:04 +0000 | [diff] [blame] | 3715 | { |
| 3716 | if (gui.tabline == NULL) |
| 3717 | return; |
| 3718 | |
| 3719 | ignore_tabline_evt = TRUE; |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 3720 | # if GTK_CHECK_VERSION(3,0,0) |
| 3721 | if (gtk_notebook_get_current_page(GTK_NOTEBOOK(gui.tabline)) != nr - 1) |
| 3722 | gtk_notebook_set_current_page(GTK_NOTEBOOK(gui.tabline), nr - 1); |
| 3723 | # else |
Bram Moolenaar | 32466aa | 2006-02-24 23:53:04 +0000 | [diff] [blame] | 3724 | if (gtk_notebook_current_page(GTK_NOTEBOOK(gui.tabline)) != nr - 1) |
Bram Moolenaar | c9b4b05 | 2006-04-30 18:54:39 +0000 | [diff] [blame] | 3725 | gtk_notebook_set_page(GTK_NOTEBOOK(gui.tabline), nr - 1); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 3726 | # endif |
Bram Moolenaar | 32466aa | 2006-02-24 23:53:04 +0000 | [diff] [blame] | 3727 | ignore_tabline_evt = FALSE; |
| 3728 | } |
| 3729 | |
| 3730 | #endif /* FEAT_GUI_TABLINE */ |
| 3731 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3732 | /* |
Bram Moolenaar | a76638f | 2010-06-05 12:49:46 +0200 | [diff] [blame] | 3733 | * Add selection targets for PRIMARY and CLIPBOARD selections. |
| 3734 | */ |
| 3735 | void |
| 3736 | gui_gtk_set_selection_targets(void) |
| 3737 | { |
| 3738 | int i, j = 0; |
| 3739 | int n_targets = N_SELECTION_TARGETS; |
| 3740 | GtkTargetEntry targets[N_SELECTION_TARGETS]; |
| 3741 | |
| 3742 | for (i = 0; i < (int)N_SELECTION_TARGETS; ++i) |
| 3743 | { |
Bram Moolenaar | 2969570 | 2012-05-18 17:03:18 +0200 | [diff] [blame] | 3744 | /* OpenOffice tries to use TARGET_HTML and fails when we don't |
Bram Moolenaar | a76638f | 2010-06-05 12:49:46 +0200 | [diff] [blame] | 3745 | * return something, instead of trying another target. Therefore only |
| 3746 | * offer TARGET_HTML when it works. */ |
| 3747 | if (!clip_html && selection_targets[i].info == TARGET_HTML) |
| 3748 | n_targets--; |
| 3749 | else |
Bram Moolenaar | a76638f | 2010-06-05 12:49:46 +0200 | [diff] [blame] | 3750 | targets[j++] = selection_targets[i]; |
| 3751 | } |
| 3752 | |
| 3753 | gtk_selection_clear_targets(gui.drawarea, (GdkAtom)GDK_SELECTION_PRIMARY); |
| 3754 | gtk_selection_clear_targets(gui.drawarea, (GdkAtom)clip_plus.gtk_sel_atom); |
| 3755 | gtk_selection_add_targets(gui.drawarea, |
| 3756 | (GdkAtom)GDK_SELECTION_PRIMARY, |
| 3757 | targets, n_targets); |
| 3758 | gtk_selection_add_targets(gui.drawarea, |
| 3759 | (GdkAtom)clip_plus.gtk_sel_atom, |
| 3760 | targets, n_targets); |
| 3761 | } |
| 3762 | |
| 3763 | /* |
| 3764 | * Set up for receiving DND items. |
| 3765 | */ |
| 3766 | void |
| 3767 | gui_gtk_set_dnd_targets(void) |
| 3768 | { |
| 3769 | int i, j = 0; |
| 3770 | int n_targets = N_DND_TARGETS; |
| 3771 | GtkTargetEntry targets[N_DND_TARGETS]; |
| 3772 | |
| 3773 | for (i = 0; i < (int)N_DND_TARGETS; ++i) |
| 3774 | { |
Bram Moolenaar | b931d74 | 2011-10-26 11:36:25 +0200 | [diff] [blame] | 3775 | if (!clip_html && dnd_targets[i].info == TARGET_HTML) |
Bram Moolenaar | a76638f | 2010-06-05 12:49:46 +0200 | [diff] [blame] | 3776 | n_targets--; |
| 3777 | else |
Bram Moolenaar | a76638f | 2010-06-05 12:49:46 +0200 | [diff] [blame] | 3778 | targets[j++] = dnd_targets[i]; |
| 3779 | } |
| 3780 | |
| 3781 | gtk_drag_dest_unset(gui.drawarea); |
| 3782 | gtk_drag_dest_set(gui.drawarea, |
| 3783 | GTK_DEST_DEFAULT_ALL, |
| 3784 | targets, n_targets, |
Bram Moolenaar | ba7cc9f | 2011-02-25 17:10:27 +0100 | [diff] [blame] | 3785 | GDK_ACTION_COPY | GDK_ACTION_MOVE); |
Bram Moolenaar | a76638f | 2010-06-05 12:49:46 +0200 | [diff] [blame] | 3786 | } |
| 3787 | |
| 3788 | /* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3789 | * Initialize the GUI. Create all the windows, set up all the callbacks etc. |
| 3790 | * Returns OK for success, FAIL when the GUI can't be started. |
| 3791 | */ |
| 3792 | int |
| 3793 | gui_mch_init(void) |
| 3794 | { |
| 3795 | GtkWidget *vbox; |
| 3796 | |
| 3797 | #ifdef FEAT_GUI_GNOME |
| 3798 | /* Initialize the GNOME libraries. gnome_program_init()/gnome_init() |
| 3799 | * exits on failure, but that's a non-issue because we already called |
| 3800 | * gtk_init_check() in gui_mch_init_check(). */ |
| 3801 | if (using_gnome) |
Bram Moolenaar | 3be71ce | 2013-01-23 16:00:11 +0100 | [diff] [blame] | 3802 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3803 | gnome_program_init(VIMPACKAGE, VIM_VERSION_SHORT, |
| 3804 | LIBGNOMEUI_MODULE, gui_argc, gui_argv, NULL); |
Bram Moolenaar | 3be71ce | 2013-01-23 16:00:11 +0100 | [diff] [blame] | 3805 | # if defined(FEAT_FLOAT) && defined(LC_NUMERIC) |
Bram Moolenaar | 1ff32c5 | 2014-04-29 15:11:43 +0200 | [diff] [blame] | 3806 | { |
| 3807 | char *p = setlocale(LC_NUMERIC, NULL); |
| 3808 | |
| 3809 | /* Make sure strtod() uses a decimal point, not a comma. Gnome |
| 3810 | * init may change it. */ |
| 3811 | if (p == NULL || strcmp(p, "C") != 0) |
| 3812 | setlocale(LC_NUMERIC, "C"); |
| 3813 | } |
Bram Moolenaar | 3be71ce | 2013-01-23 16:00:11 +0100 | [diff] [blame] | 3814 | # endif |
| 3815 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3816 | #endif |
| 3817 | vim_free(gui_argv); |
| 3818 | gui_argv = NULL; |
| 3819 | |
Bram Moolenaar | 182c5be | 2010-06-25 05:37:59 +0200 | [diff] [blame] | 3820 | #if GLIB_CHECK_VERSION(2,1,3) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3821 | /* Set the human-readable application name */ |
| 3822 | g_set_application_name("Vim"); |
Bram Moolenaar | 182c5be | 2010-06-25 05:37:59 +0200 | [diff] [blame] | 3823 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3824 | /* |
| 3825 | * Force UTF-8 output no matter what the value of 'encoding' is. |
| 3826 | * did_set_string_option() in option.c prohibits changing 'termencoding' |
| 3827 | * to something else than UTF-8 if the GUI is in use. |
| 3828 | */ |
| 3829 | set_option_value((char_u *)"termencoding", 0L, (char_u *)"utf-8", 0); |
| 3830 | |
Bram Moolenaar | 182c5be | 2010-06-25 05:37:59 +0200 | [diff] [blame] | 3831 | #ifdef FEAT_TOOLBAR |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3832 | gui_gtk_register_stock_icons(); |
Bram Moolenaar | 182c5be | 2010-06-25 05:37:59 +0200 | [diff] [blame] | 3833 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3834 | /* FIXME: Need to install the classic icons and a gtkrc.classic file. |
| 3835 | * The hard part is deciding install locations and the Makefile magic. */ |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 3836 | #if !GTK_CHECK_VERSION(3,0,0) |
| 3837 | # if 0 |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3838 | gtk_rc_parse("gtkrc"); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 3839 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3840 | #endif |
| 3841 | |
| 3842 | /* Initialize values */ |
| 3843 | gui.border_width = 2; |
| 3844 | gui.scrollbar_width = SB_DEFAULT_WIDTH; |
| 3845 | gui.scrollbar_height = SB_DEFAULT_WIDTH; |
Bram Moolenaar | 36edf06 | 2016-07-21 22:10:12 +0200 | [diff] [blame] | 3846 | #if GTK_CHECK_VERSION(3,0,0) |
| 3847 | gui.fgcolor = g_new(GdkRGBA, 1); |
| 3848 | gui.bgcolor = g_new(GdkRGBA, 1); |
| 3849 | gui.spcolor = g_new(GdkRGBA, 1); |
| 3850 | #else |
Bram Moolenaar | b71ec9f | 2005-01-25 22:22:02 +0000 | [diff] [blame] | 3851 | /* LINTED: avoid warning: conversion to 'unsigned long' */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3852 | gui.fgcolor = g_new0(GdkColor, 1); |
Bram Moolenaar | b71ec9f | 2005-01-25 22:22:02 +0000 | [diff] [blame] | 3853 | /* LINTED: avoid warning: conversion to 'unsigned long' */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3854 | gui.bgcolor = g_new0(GdkColor, 1); |
Bram Moolenaar | f36d369 | 2005-03-15 22:48:14 +0000 | [diff] [blame] | 3855 | /* LINTED: avoid warning: conversion to 'unsigned long' */ |
| 3856 | gui.spcolor = g_new0(GdkColor, 1); |
Bram Moolenaar | 36edf06 | 2016-07-21 22:10:12 +0200 | [diff] [blame] | 3857 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3858 | |
| 3859 | /* Initialise atoms */ |
Bram Moolenaar | 3a6eaa5 | 2009-06-16 13:23:06 +0000 | [diff] [blame] | 3860 | html_atom = gdk_atom_intern("text/html", FALSE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3861 | utf8_string_atom = gdk_atom_intern("UTF8_STRING", FALSE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3862 | |
| 3863 | /* Set default foreground and background colors. */ |
| 3864 | gui.norm_pixel = gui.def_norm_pixel; |
| 3865 | gui.back_pixel = gui.def_back_pixel; |
| 3866 | |
| 3867 | if (gtk_socket_id != 0) |
| 3868 | { |
| 3869 | GtkWidget *plug; |
| 3870 | |
| 3871 | /* Use GtkSocket from another app. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3872 | plug = gtk_plug_new_for_display(gdk_display_get_default(), |
| 3873 | gtk_socket_id); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 3874 | #if GTK_CHECK_VERSION(3,0,0) |
| 3875 | if (plug != NULL && gtk_plug_get_socket_window(GTK_PLUG(plug)) != NULL) |
| 3876 | #else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3877 | if (plug != NULL && GTK_PLUG(plug)->socket_window != NULL) |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 3878 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3879 | { |
| 3880 | gui.mainwin = plug; |
| 3881 | } |
| 3882 | else |
| 3883 | { |
| 3884 | g_warning("Connection to GTK+ socket (ID %u) failed", |
| 3885 | (unsigned int)gtk_socket_id); |
| 3886 | /* Pretend we never wanted it if it failed (get own window) */ |
| 3887 | gtk_socket_id = 0; |
| 3888 | } |
| 3889 | } |
| 3890 | |
| 3891 | if (gtk_socket_id == 0) |
| 3892 | { |
| 3893 | #ifdef FEAT_GUI_GNOME |
| 3894 | if (using_gnome) |
| 3895 | { |
| 3896 | gui.mainwin = gnome_app_new("Vim", NULL); |
| 3897 | # ifdef USE_XSMP |
| 3898 | /* Use the GNOME save-yourself functionality now. */ |
| 3899 | xsmp_close(); |
| 3900 | # endif |
| 3901 | } |
| 3902 | else |
| 3903 | #endif |
| 3904 | gui.mainwin = gtk_window_new(GTK_WINDOW_TOPLEVEL); |
| 3905 | } |
| 3906 | |
| 3907 | gtk_widget_set_name(gui.mainwin, "vim-main-window"); |
| 3908 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3909 | /* Create the PangoContext used for drawing all text. */ |
| 3910 | gui.text_context = gtk_widget_create_pango_context(gui.mainwin); |
| 3911 | pango_context_set_base_dir(gui.text_context, PANGO_DIRECTION_LTR); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3912 | |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 3913 | #if GTK_CHECK_VERSION(3,0,0) |
| 3914 | gtk_container_set_border_width(GTK_CONTAINER(gui.mainwin), 0); |
| 3915 | #else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3916 | gtk_container_border_width(GTK_CONTAINER(gui.mainwin), 0); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 3917 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3918 | gtk_widget_add_events(gui.mainwin, GDK_VISIBILITY_NOTIFY_MASK); |
| 3919 | |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 3920 | #if GTK_CHECK_VERSION(3,0,0) |
| 3921 | g_signal_connect(G_OBJECT(gui.mainwin), "delete-event", |
| 3922 | G_CALLBACK(&delete_event_cb), NULL); |
| 3923 | |
| 3924 | g_signal_connect(G_OBJECT(gui.mainwin), "realize", |
| 3925 | G_CALLBACK(&mainwin_realize), NULL); |
| 3926 | #else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3927 | gtk_signal_connect(GTK_OBJECT(gui.mainwin), "delete_event", |
| 3928 | GTK_SIGNAL_FUNC(&delete_event_cb), NULL); |
| 3929 | |
| 3930 | gtk_signal_connect(GTK_OBJECT(gui.mainwin), "realize", |
| 3931 | GTK_SIGNAL_FUNC(&mainwin_realize), NULL); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 3932 | #endif |
Bram Moolenaar | b463e8d | 2017-06-05 15:07:09 +0200 | [diff] [blame] | 3933 | |
| 3934 | g_signal_connect(G_OBJECT(gui.mainwin), "screen-changed", |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3935 | G_CALLBACK(&mainwin_screen_changed_cb), NULL); |
Bram Moolenaar | b463e8d | 2017-06-05 15:07:09 +0200 | [diff] [blame] | 3936 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3937 | gui.accel_group = gtk_accel_group_new(); |
| 3938 | gtk_window_add_accel_group(GTK_WINDOW(gui.mainwin), gui.accel_group); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3939 | |
Bram Moolenaar | 32466aa | 2006-02-24 23:53:04 +0000 | [diff] [blame] | 3940 | /* A vertical box holds the menubar, toolbar and main text window. */ |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 3941 | #if GTK_CHECK_VERSION(3,2,0) |
| 3942 | vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0); |
| 3943 | gtk_box_set_homogeneous(GTK_BOX(vbox), FALSE); |
| 3944 | #else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3945 | vbox = gtk_vbox_new(FALSE, 0); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 3946 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3947 | |
| 3948 | #ifdef FEAT_GUI_GNOME |
| 3949 | if (using_gnome) |
| 3950 | { |
Bram Moolenaar | 182c5be | 2010-06-25 05:37:59 +0200 | [diff] [blame] | 3951 | # if defined(FEAT_MENU) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3952 | /* automagically restore menubar/toolbar placement */ |
| 3953 | gnome_app_enable_layout_config(GNOME_APP(gui.mainwin), TRUE); |
| 3954 | # endif |
| 3955 | gnome_app_set_contents(GNOME_APP(gui.mainwin), vbox); |
| 3956 | } |
| 3957 | else |
| 3958 | #endif |
| 3959 | { |
| 3960 | gtk_container_add(GTK_CONTAINER(gui.mainwin), vbox); |
| 3961 | gtk_widget_show(vbox); |
| 3962 | } |
| 3963 | |
| 3964 | #ifdef FEAT_MENU |
| 3965 | /* |
| 3966 | * Create the menubar and handle |
| 3967 | */ |
| 3968 | gui.menubar = gtk_menu_bar_new(); |
| 3969 | gtk_widget_set_name(gui.menubar, "vim-menubar"); |
| 3970 | |
Bram Moolenaar | 293ee4d | 2004-12-09 21:34:53 +0000 | [diff] [blame] | 3971 | /* Avoid that GTK takes <F10> away from us. */ |
| 3972 | { |
| 3973 | GtkSettings *gtk_settings; |
| 3974 | |
| 3975 | gtk_settings = gtk_settings_get_for_screen(gdk_screen_get_default()); |
| 3976 | g_object_set(gtk_settings, "gtk-menu-bar-accel", NULL, NULL); |
| 3977 | } |
Bram Moolenaar | 293ee4d | 2004-12-09 21:34:53 +0000 | [diff] [blame] | 3978 | |
| 3979 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3980 | # ifdef FEAT_GUI_GNOME |
| 3981 | if (using_gnome) |
| 3982 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3983 | BonoboDockItem *dockitem; |
| 3984 | |
| 3985 | gnome_app_set_menus(GNOME_APP(gui.mainwin), GTK_MENU_BAR(gui.menubar)); |
| 3986 | dockitem = gnome_app_get_dock_item_by_name(GNOME_APP(gui.mainwin), |
| 3987 | GNOME_APP_MENUBAR_NAME); |
Bram Moolenaar | db552d60 | 2006-03-23 22:59:57 +0000 | [diff] [blame] | 3988 | /* We don't want the menu to float. */ |
| 3989 | bonobo_dock_item_set_behavior(dockitem, |
| 3990 | bonobo_dock_item_get_behavior(dockitem) |
| 3991 | | BONOBO_DOCK_ITEM_BEH_NEVER_FLOATING); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3992 | gui.menubar_h = GTK_WIDGET(dockitem); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3993 | } |
| 3994 | else |
| 3995 | # endif /* FEAT_GUI_GNOME */ |
| 3996 | { |
Bram Moolenaar | 18144c8 | 2006-04-12 21:52:12 +0000 | [diff] [blame] | 3997 | /* Always show the menubar, otherwise <F10> doesn't work. It may be |
| 3998 | * disabled in gui_init() later. */ |
| 3999 | gtk_widget_show(gui.menubar); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4000 | gtk_box_pack_start(GTK_BOX(vbox), gui.menubar, FALSE, FALSE, 0); |
| 4001 | } |
| 4002 | #endif /* FEAT_MENU */ |
| 4003 | |
| 4004 | #ifdef FEAT_TOOLBAR |
| 4005 | /* |
| 4006 | * Create the toolbar and handle |
| 4007 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4008 | /* some aesthetics on the toolbar */ |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 4009 | # ifdef USE_GTK3 |
Bram Moolenaar | 3f2a5d8 | 2016-02-27 22:08:16 +0100 | [diff] [blame] | 4010 | /* TODO: Add GTK+ 3 code here using GtkCssProvider if necessary. */ |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 4011 | /* N.B. Since the default value of GtkToolbar::button-relief is |
| 4012 | * GTK_RELIEF_NONE, there's no need to specify that, probably. */ |
| 4013 | # else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4014 | gtk_rc_parse_string( |
| 4015 | "style \"vim-toolbar-style\" {\n" |
| 4016 | " GtkToolbar::button_relief = GTK_RELIEF_NONE\n" |
| 4017 | "}\n" |
| 4018 | "widget \"*.vim-toolbar\" style \"vim-toolbar-style\"\n"); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 4019 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4020 | gui.toolbar = gtk_toolbar_new(); |
| 4021 | gtk_widget_set_name(gui.toolbar, "vim-toolbar"); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4022 | set_toolbar_style(GTK_TOOLBAR(gui.toolbar)); |
| 4023 | |
| 4024 | # ifdef FEAT_GUI_GNOME |
| 4025 | if (using_gnome) |
| 4026 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4027 | BonoboDockItem *dockitem; |
| 4028 | |
| 4029 | gnome_app_set_toolbar(GNOME_APP(gui.mainwin), GTK_TOOLBAR(gui.toolbar)); |
| 4030 | dockitem = gnome_app_get_dock_item_by_name(GNOME_APP(gui.mainwin), |
| 4031 | GNOME_APP_TOOLBAR_NAME); |
| 4032 | gui.toolbar_h = GTK_WIDGET(dockitem); |
Bram Moolenaar | e580b0c | 2006-03-21 21:33:03 +0000 | [diff] [blame] | 4033 | /* 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] | 4034 | * fixed let's disallow floating. */ |
Bram Moolenaar | e580b0c | 2006-03-21 21:33:03 +0000 | [diff] [blame] | 4035 | bonobo_dock_item_set_behavior(dockitem, |
Bram Moolenaar | db552d60 | 2006-03-23 22:59:57 +0000 | [diff] [blame] | 4036 | bonobo_dock_item_get_behavior(dockitem) |
| 4037 | | BONOBO_DOCK_ITEM_BEH_NEVER_FLOATING); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4038 | gtk_container_set_border_width(GTK_CONTAINER(gui.toolbar), 0); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4039 | } |
| 4040 | else |
| 4041 | # endif /* FEAT_GUI_GNOME */ |
| 4042 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4043 | if (vim_strchr(p_go, GO_TOOLBAR) != NULL |
| 4044 | && (toolbar_flags & (TOOLBAR_TEXT | TOOLBAR_ICONS))) |
| 4045 | gtk_widget_show(gui.toolbar); |
| 4046 | gtk_box_pack_start(GTK_BOX(vbox), gui.toolbar, FALSE, FALSE, 0); |
| 4047 | } |
| 4048 | #endif /* FEAT_TOOLBAR */ |
| 4049 | |
Bram Moolenaar | 32466aa | 2006-02-24 23:53:04 +0000 | [diff] [blame] | 4050 | #ifdef FEAT_GUI_TABLINE |
Bram Moolenaar | 97b2ad3 | 2006-03-18 21:40:56 +0000 | [diff] [blame] | 4051 | /* |
| 4052 | * Use a Notebook for the tab pages labels. The labels are hidden by |
| 4053 | * default. |
Bram Moolenaar | 97b2ad3 | 2006-03-18 21:40:56 +0000 | [diff] [blame] | 4054 | */ |
Bram Moolenaar | b3656ed | 2006-03-20 21:59:49 +0000 | [diff] [blame] | 4055 | gui.tabline = gtk_notebook_new(); |
| 4056 | gtk_widget_show(gui.tabline); |
| 4057 | gtk_box_pack_start(GTK_BOX(vbox), gui.tabline, FALSE, FALSE, 0); |
| 4058 | gtk_notebook_set_show_border(GTK_NOTEBOOK(gui.tabline), FALSE); |
| 4059 | gtk_notebook_set_show_tabs(GTK_NOTEBOOK(gui.tabline), FALSE); |
Bram Moolenaar | 4a85b41 | 2006-04-23 22:40:29 +0000 | [diff] [blame] | 4060 | gtk_notebook_set_scrollable(GTK_NOTEBOOK(gui.tabline), TRUE); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 4061 | # if !GTK_CHECK_VERSION(3,0,0) |
Bram Moolenaar | 9635157 | 2006-05-05 21:16:59 +0000 | [diff] [blame] | 4062 | gtk_notebook_set_tab_border(GTK_NOTEBOOK(gui.tabline), FALSE); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 4063 | # endif |
Bram Moolenaar | b3656ed | 2006-03-20 21:59:49 +0000 | [diff] [blame] | 4064 | |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 4065 | # if !GTK_CHECK_VERSION(3,0,0) |
Bram Moolenaar | 437df8f | 2006-04-27 21:47:44 +0000 | [diff] [blame] | 4066 | tabline_tooltip = gtk_tooltips_new(); |
| 4067 | gtk_tooltips_enable(GTK_TOOLTIPS(tabline_tooltip)); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 4068 | # endif |
Bram Moolenaar | 437df8f | 2006-04-27 21:47:44 +0000 | [diff] [blame] | 4069 | |
Bram Moolenaar | 32466aa | 2006-02-24 23:53:04 +0000 | [diff] [blame] | 4070 | { |
Bram Moolenaar | 437df8f | 2006-04-27 21:47:44 +0000 | [diff] [blame] | 4071 | GtkWidget *page, *label, *event_box; |
Bram Moolenaar | 32466aa | 2006-02-24 23:53:04 +0000 | [diff] [blame] | 4072 | |
Bram Moolenaar | c9b4b05 | 2006-04-30 18:54:39 +0000 | [diff] [blame] | 4073 | /* Add the first tab. */ |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 4074 | # if GTK_CHECK_VERSION(3,2,0) |
| 4075 | page = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0); |
| 4076 | gtk_box_set_homogeneous(GTK_BOX(page), FALSE); |
| 4077 | # else |
Bram Moolenaar | 437df8f | 2006-04-27 21:47:44 +0000 | [diff] [blame] | 4078 | page = gtk_vbox_new(FALSE, 0); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 4079 | # endif |
Bram Moolenaar | 437df8f | 2006-04-27 21:47:44 +0000 | [diff] [blame] | 4080 | gtk_widget_show(page); |
| 4081 | gtk_container_add(GTK_CONTAINER(gui.tabline), page); |
| 4082 | label = gtk_label_new("-Empty-"); |
| 4083 | gtk_widget_show(label); |
Bram Moolenaar | 437df8f | 2006-04-27 21:47:44 +0000 | [diff] [blame] | 4084 | event_box = gtk_event_box_new(); |
| 4085 | gtk_widget_show(event_box); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 4086 | # if GTK_CHECK_VERSION(3,0,0) |
| 4087 | g_object_set_data(G_OBJECT(event_box), "tab_num", GINT_TO_POINTER(1L)); |
| 4088 | # else |
Bram Moolenaar | 47b46d7 | 2008-07-02 19:05:48 +0000 | [diff] [blame] | 4089 | gtk_object_set_user_data(GTK_OBJECT(event_box), (gpointer)1L); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 4090 | # endif |
| 4091 | # if !GTK_CHECK_VERSION(3,14,0) |
Bram Moolenaar | 8ea9123 | 2006-04-28 22:41:43 +0000 | [diff] [blame] | 4092 | gtk_misc_set_padding(GTK_MISC(label), 2, 2); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 4093 | # endif |
Bram Moolenaar | 437df8f | 2006-04-27 21:47:44 +0000 | [diff] [blame] | 4094 | gtk_container_add(GTK_CONTAINER(event_box), label); |
Bram Moolenaar | 437df8f | 2006-04-27 21:47:44 +0000 | [diff] [blame] | 4095 | gtk_notebook_set_tab_label(GTK_NOTEBOOK(gui.tabline), page, event_box); |
Bram Moolenaar | 97b2ad3 | 2006-03-18 21:40:56 +0000 | [diff] [blame] | 4096 | } |
Bram Moolenaar | 54a709e | 2006-05-04 21:57:11 +0000 | [diff] [blame] | 4097 | |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 4098 | # if GTK_CHECK_VERSION(3,0,0) |
| 4099 | g_signal_connect(G_OBJECT(gui.tabline), "switch-page", |
| 4100 | G_CALLBACK(on_select_tab), NULL); |
| 4101 | # else |
Bram Moolenaar | b3656ed | 2006-03-20 21:59:49 +0000 | [diff] [blame] | 4102 | gtk_signal_connect(GTK_OBJECT(gui.tabline), "switch_page", |
Bram Moolenaar | c9b4b05 | 2006-04-30 18:54:39 +0000 | [diff] [blame] | 4103 | GTK_SIGNAL_FUNC(on_select_tab), NULL); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 4104 | # endif |
Bram Moolenaar | b3656ed | 2006-03-20 21:59:49 +0000 | [diff] [blame] | 4105 | |
| 4106 | /* Create a popup menu for the tab line and connect it. */ |
| 4107 | tabline_menu = create_tabline_menu(); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 4108 | # if GTK_CHECK_VERSION(3,0,0) |
| 4109 | g_signal_connect_swapped(G_OBJECT(gui.tabline), "button-press-event", |
| 4110 | G_CALLBACK(on_tabline_menu), G_OBJECT(tabline_menu)); |
| 4111 | # else |
Bram Moolenaar | b3656ed | 2006-03-20 21:59:49 +0000 | [diff] [blame] | 4112 | gtk_signal_connect_object(GTK_OBJECT(gui.tabline), "button_press_event", |
Bram Moolenaar | c9b4b05 | 2006-04-30 18:54:39 +0000 | [diff] [blame] | 4113 | GTK_SIGNAL_FUNC(on_tabline_menu), GTK_OBJECT(tabline_menu)); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 4114 | # endif |
| 4115 | #endif /* FEAT_GUI_TABLINE */ |
Bram Moolenaar | 32466aa | 2006-02-24 23:53:04 +0000 | [diff] [blame] | 4116 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4117 | gui.formwin = gtk_form_new(); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 4118 | #if GTK_CHECK_VERSION(3,0,0) |
| 4119 | gtk_container_set_border_width(GTK_CONTAINER(gui.formwin), 0); |
| 4120 | #else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4121 | gtk_container_border_width(GTK_CONTAINER(gui.formwin), 0); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 4122 | #endif |
| 4123 | #if !GTK_CHECK_VERSION(3,0,0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4124 | gtk_widget_set_events(gui.formwin, GDK_EXPOSURE_MASK); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 4125 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4126 | |
| 4127 | gui.drawarea = gtk_drawing_area_new(); |
Bram Moolenaar | a859f04 | 2016-11-17 19:11:55 +0100 | [diff] [blame] | 4128 | #if GTK_CHECK_VERSION(3,22,2) |
| 4129 | gtk_widget_set_name(gui.drawarea, "vim-gui-drawarea"); |
| 4130 | #endif |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 4131 | #if GTK_CHECK_VERSION(3,0,0) |
| 4132 | gui.surface = NULL; |
| 4133 | gui.by_signal = FALSE; |
| 4134 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4135 | |
| 4136 | /* Determine which events we will filter. */ |
| 4137 | gtk_widget_set_events(gui.drawarea, |
| 4138 | GDK_EXPOSURE_MASK | |
| 4139 | GDK_ENTER_NOTIFY_MASK | |
| 4140 | GDK_LEAVE_NOTIFY_MASK | |
| 4141 | GDK_BUTTON_PRESS_MASK | |
| 4142 | GDK_BUTTON_RELEASE_MASK | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4143 | GDK_SCROLL_MASK | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4144 | GDK_KEY_PRESS_MASK | |
| 4145 | GDK_KEY_RELEASE_MASK | |
| 4146 | GDK_POINTER_MOTION_MASK | |
| 4147 | GDK_POINTER_MOTION_HINT_MASK); |
| 4148 | |
| 4149 | gtk_widget_show(gui.drawarea); |
| 4150 | gtk_form_put(GTK_FORM(gui.formwin), gui.drawarea, 0, 0); |
| 4151 | gtk_widget_show(gui.formwin); |
| 4152 | gtk_box_pack_start(GTK_BOX(vbox), gui.formwin, TRUE, TRUE, 0); |
| 4153 | |
| 4154 | /* For GtkSockets, key-presses must go to the focus widget (drawarea) |
| 4155 | * and not the window. */ |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 4156 | #if GTK_CHECK_VERSION(3,0,0) |
| 4157 | g_signal_connect((gtk_socket_id == 0) ? G_OBJECT(gui.mainwin) |
| 4158 | : G_OBJECT(gui.drawarea), |
| 4159 | "key-press-event", |
| 4160 | G_CALLBACK(key_press_event), NULL); |
| 4161 | #else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4162 | gtk_signal_connect((gtk_socket_id == 0) ? GTK_OBJECT(gui.mainwin) |
| 4163 | : GTK_OBJECT(gui.drawarea), |
| 4164 | "key_press_event", |
| 4165 | GTK_SIGNAL_FUNC(key_press_event), NULL); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 4166 | #endif |
| 4167 | #if defined(FEAT_XIM) || GTK_CHECK_VERSION(3,0,0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4168 | /* Also forward key release events for the benefit of GTK+ 2 input |
| 4169 | * modules. Try CTRL-SHIFT-xdigits to enter a Unicode code point. */ |
| 4170 | g_signal_connect((gtk_socket_id == 0) ? G_OBJECT(gui.mainwin) |
| 4171 | : G_OBJECT(gui.drawarea), |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 4172 | "key-release-event", |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4173 | G_CALLBACK(&key_release_event), NULL); |
| 4174 | #endif |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 4175 | #if GTK_CHECK_VERSION(3,0,0) |
| 4176 | g_signal_connect(G_OBJECT(gui.drawarea), "realize", |
| 4177 | G_CALLBACK(drawarea_realize_cb), NULL); |
| 4178 | g_signal_connect(G_OBJECT(gui.drawarea), "unrealize", |
| 4179 | G_CALLBACK(drawarea_unrealize_cb), NULL); |
| 4180 | g_signal_connect(G_OBJECT(gui.drawarea), "configure-event", |
| 4181 | G_CALLBACK(drawarea_configure_event_cb), NULL); |
Bram Moolenaar | a859f04 | 2016-11-17 19:11:55 +0100 | [diff] [blame] | 4182 | # if GTK_CHECK_VERSION(3,22,2) |
| 4183 | g_signal_connect_after(G_OBJECT(gui.drawarea), "style-updated", |
| 4184 | G_CALLBACK(&drawarea_style_updated_cb), NULL); |
| 4185 | # else |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 4186 | g_signal_connect_after(G_OBJECT(gui.drawarea), "style-set", |
| 4187 | G_CALLBACK(&drawarea_style_set_cb), NULL); |
Bram Moolenaar | a859f04 | 2016-11-17 19:11:55 +0100 | [diff] [blame] | 4188 | # endif |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 4189 | #else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4190 | gtk_signal_connect(GTK_OBJECT(gui.drawarea), "realize", |
| 4191 | GTK_SIGNAL_FUNC(drawarea_realize_cb), NULL); |
| 4192 | gtk_signal_connect(GTK_OBJECT(gui.drawarea), "unrealize", |
| 4193 | GTK_SIGNAL_FUNC(drawarea_unrealize_cb), NULL); |
| 4194 | |
| 4195 | gtk_signal_connect_after(GTK_OBJECT(gui.drawarea), "style_set", |
| 4196 | GTK_SIGNAL_FUNC(&drawarea_style_set_cb), NULL); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 4197 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4198 | |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 4199 | #if !GTK_CHECK_VERSION(3,0,0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4200 | gui.visibility = GDK_VISIBILITY_UNOBSCURED; |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 4201 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4202 | |
| 4203 | #if !(defined(FEAT_GUI_GNOME) && defined(FEAT_SESSION)) |
| 4204 | wm_protocols_atom = gdk_atom_intern("WM_PROTOCOLS", FALSE); |
| 4205 | save_yourself_atom = gdk_atom_intern("WM_SAVE_YOURSELF", FALSE); |
| 4206 | #endif |
| 4207 | |
| 4208 | if (gtk_socket_id != 0) |
Bram Moolenaar | b85cb21 | 2009-05-17 14:24:23 +0000 | [diff] [blame] | 4209 | /* make sure keyboard input can go to the drawarea */ |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 4210 | #if GTK_CHECK_VERSION(3,0,0) |
| 4211 | gtk_widget_set_can_focus(gui.drawarea, TRUE); |
| 4212 | #else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4213 | GTK_WIDGET_SET_FLAGS(gui.drawarea, GTK_CAN_FOCUS); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 4214 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4215 | |
| 4216 | /* |
| 4217 | * Set clipboard specific atoms |
| 4218 | */ |
| 4219 | vim_atom = gdk_atom_intern(VIM_ATOM_NAME, FALSE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4220 | vimenc_atom = gdk_atom_intern(VIMENC_ATOM_NAME, FALSE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4221 | clip_star.gtk_sel_atom = GDK_SELECTION_PRIMARY; |
| 4222 | clip_plus.gtk_sel_atom = gdk_atom_intern("CLIPBOARD", FALSE); |
| 4223 | |
| 4224 | /* |
| 4225 | * Start out by adding the configured border width into the border offset. |
| 4226 | */ |
| 4227 | gui.border_offset = gui.border_width; |
| 4228 | |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 4229 | #if GTK_CHECK_VERSION(3,0,0) |
| 4230 | g_signal_connect(G_OBJECT(gui.drawarea), "draw", |
| 4231 | G_CALLBACK(draw_event), NULL); |
| 4232 | #else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4233 | gtk_signal_connect(GTK_OBJECT(gui.mainwin), "visibility_notify_event", |
| 4234 | GTK_SIGNAL_FUNC(visibility_event), NULL); |
| 4235 | gtk_signal_connect(GTK_OBJECT(gui.drawarea), "expose_event", |
| 4236 | GTK_SIGNAL_FUNC(expose_event), NULL); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 4237 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4238 | |
| 4239 | /* |
| 4240 | * Only install these enter/leave callbacks when 'p' in 'guioptions'. |
| 4241 | * Only needed for some window managers. |
| 4242 | */ |
| 4243 | if (vim_strchr(p_go, GO_POINTER) != NULL) |
| 4244 | { |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 4245 | #if GTK_CHECK_VERSION(3,0,0) |
| 4246 | g_signal_connect(G_OBJECT(gui.drawarea), "leave-notify-event", |
| 4247 | G_CALLBACK(leave_notify_event), NULL); |
| 4248 | g_signal_connect(G_OBJECT(gui.drawarea), "enter-notify-event", |
| 4249 | G_CALLBACK(enter_notify_event), NULL); |
| 4250 | #else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4251 | gtk_signal_connect(GTK_OBJECT(gui.drawarea), "leave_notify_event", |
| 4252 | GTK_SIGNAL_FUNC(leave_notify_event), NULL); |
| 4253 | gtk_signal_connect(GTK_OBJECT(gui.drawarea), "enter_notify_event", |
| 4254 | GTK_SIGNAL_FUNC(enter_notify_event), NULL); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 4255 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4256 | } |
| 4257 | |
Bram Moolenaar | b3656ed | 2006-03-20 21:59:49 +0000 | [diff] [blame] | 4258 | /* Real windows can get focus ... GtkPlug, being a mere container can't, |
| 4259 | * only its widgets. Arguably, this could be common code and we not use |
| 4260 | * the window focus at all, but let's be safe. |
| 4261 | */ |
| 4262 | if (gtk_socket_id == 0) |
| 4263 | { |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 4264 | #if GTK_CHECK_VERSION(3,0,0) |
| 4265 | g_signal_connect(G_OBJECT(gui.mainwin), "focus-out-event", |
| 4266 | G_CALLBACK(focus_out_event), NULL); |
| 4267 | g_signal_connect(G_OBJECT(gui.mainwin), "focus-in-event", |
| 4268 | G_CALLBACK(focus_in_event), NULL); |
| 4269 | #else |
Bram Moolenaar | c9b4b05 | 2006-04-30 18:54:39 +0000 | [diff] [blame] | 4270 | gtk_signal_connect(GTK_OBJECT(gui.mainwin), "focus_out_event", |
| 4271 | GTK_SIGNAL_FUNC(focus_out_event), NULL); |
| 4272 | gtk_signal_connect(GTK_OBJECT(gui.mainwin), "focus_in_event", |
| 4273 | GTK_SIGNAL_FUNC(focus_in_event), NULL); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 4274 | #endif |
Bram Moolenaar | b3656ed | 2006-03-20 21:59:49 +0000 | [diff] [blame] | 4275 | } |
| 4276 | else |
| 4277 | { |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 4278 | #if GTK_CHECK_VERSION(3,0,0) |
| 4279 | g_signal_connect(G_OBJECT(gui.drawarea), "focus-out-event", |
| 4280 | G_CALLBACK(focus_out_event), NULL); |
| 4281 | g_signal_connect(G_OBJECT(gui.drawarea), "focus-in-event", |
| 4282 | G_CALLBACK(focus_in_event), NULL); |
| 4283 | #else |
Bram Moolenaar | c9b4b05 | 2006-04-30 18:54:39 +0000 | [diff] [blame] | 4284 | gtk_signal_connect(GTK_OBJECT(gui.drawarea), "focus_out_event", |
| 4285 | GTK_SIGNAL_FUNC(focus_out_event), NULL); |
| 4286 | gtk_signal_connect(GTK_OBJECT(gui.drawarea), "focus_in_event", |
| 4287 | GTK_SIGNAL_FUNC(focus_in_event), NULL); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 4288 | #endif |
Bram Moolenaar | b3656ed | 2006-03-20 21:59:49 +0000 | [diff] [blame] | 4289 | #ifdef FEAT_GUI_TABLINE |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 4290 | # if GTK_CHECK_VERSION(3,0,0) |
| 4291 | g_signal_connect(G_OBJECT(gui.tabline), "focus-out-event", |
| 4292 | G_CALLBACK(focus_out_event), NULL); |
| 4293 | g_signal_connect(G_OBJECT(gui.tabline), "focus-in-event", |
| 4294 | G_CALLBACK(focus_in_event), NULL); |
| 4295 | # else |
Bram Moolenaar | c9b4b05 | 2006-04-30 18:54:39 +0000 | [diff] [blame] | 4296 | gtk_signal_connect(GTK_OBJECT(gui.tabline), "focus_out_event", |
| 4297 | GTK_SIGNAL_FUNC(focus_out_event), NULL); |
| 4298 | gtk_signal_connect(GTK_OBJECT(gui.tabline), "focus_in_event", |
| 4299 | GTK_SIGNAL_FUNC(focus_in_event), NULL); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 4300 | # endif |
Bram Moolenaar | b3656ed | 2006-03-20 21:59:49 +0000 | [diff] [blame] | 4301 | #endif /* FEAT_GUI_TABLINE */ |
| 4302 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4303 | |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 4304 | #if GTK_CHECK_VERSION(3,0,0) |
| 4305 | g_signal_connect(G_OBJECT(gui.drawarea), "motion-notify-event", |
| 4306 | G_CALLBACK(motion_notify_event), NULL); |
| 4307 | g_signal_connect(G_OBJECT(gui.drawarea), "button-press-event", |
| 4308 | G_CALLBACK(button_press_event), NULL); |
| 4309 | g_signal_connect(G_OBJECT(gui.drawarea), "button-release-event", |
| 4310 | G_CALLBACK(button_release_event), NULL); |
| 4311 | g_signal_connect(G_OBJECT(gui.drawarea), "scroll-event", |
| 4312 | G_CALLBACK(&scroll_event), NULL); |
| 4313 | #else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4314 | gtk_signal_connect(GTK_OBJECT(gui.drawarea), "motion_notify_event", |
| 4315 | GTK_SIGNAL_FUNC(motion_notify_event), NULL); |
| 4316 | gtk_signal_connect(GTK_OBJECT(gui.drawarea), "button_press_event", |
| 4317 | GTK_SIGNAL_FUNC(button_press_event), NULL); |
| 4318 | gtk_signal_connect(GTK_OBJECT(gui.drawarea), "button_release_event", |
| 4319 | GTK_SIGNAL_FUNC(button_release_event), NULL); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4320 | g_signal_connect(G_OBJECT(gui.drawarea), "scroll_event", |
| 4321 | G_CALLBACK(&scroll_event), NULL); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 4322 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4323 | |
| 4324 | /* |
| 4325 | * Add selection handler functions. |
| 4326 | */ |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 4327 | #if GTK_CHECK_VERSION(3,0,0) |
| 4328 | g_signal_connect(G_OBJECT(gui.drawarea), "selection-clear-event", |
| 4329 | G_CALLBACK(selection_clear_event), NULL); |
| 4330 | g_signal_connect(G_OBJECT(gui.drawarea), "selection-received", |
| 4331 | G_CALLBACK(selection_received_cb), NULL); |
| 4332 | #else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4333 | gtk_signal_connect(GTK_OBJECT(gui.drawarea), "selection_clear_event", |
| 4334 | GTK_SIGNAL_FUNC(selection_clear_event), NULL); |
| 4335 | gtk_signal_connect(GTK_OBJECT(gui.drawarea), "selection_received", |
| 4336 | GTK_SIGNAL_FUNC(selection_received_cb), NULL); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 4337 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4338 | |
Bram Moolenaar | a76638f | 2010-06-05 12:49:46 +0200 | [diff] [blame] | 4339 | gui_gtk_set_selection_targets(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4340 | |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 4341 | #if GTK_CHECK_VERSION(3,0,0) |
| 4342 | g_signal_connect(G_OBJECT(gui.drawarea), "selection-get", |
| 4343 | G_CALLBACK(selection_get_cb), NULL); |
| 4344 | #else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4345 | gtk_signal_connect(GTK_OBJECT(gui.drawarea), "selection_get", |
| 4346 | GTK_SIGNAL_FUNC(selection_get_cb), NULL); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 4347 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4348 | |
| 4349 | /* Pretend we don't have input focus, we will get an event if we do. */ |
| 4350 | gui.in_focus = FALSE; |
| 4351 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4352 | return OK; |
| 4353 | } |
| 4354 | |
| 4355 | #if (defined(FEAT_GUI_GNOME) && defined(FEAT_SESSION)) || defined(PROTO) |
| 4356 | /* |
| 4357 | * This is called from gui_start() after a fork() has been done. |
| 4358 | * We have to tell the session manager our new PID. |
| 4359 | */ |
| 4360 | void |
| 4361 | gui_mch_forked(void) |
| 4362 | { |
| 4363 | if (using_gnome) |
| 4364 | { |
| 4365 | GnomeClient *client; |
| 4366 | |
| 4367 | client = gnome_master_client(); |
| 4368 | |
| 4369 | if (client != NULL) |
| 4370 | gnome_client_set_process_id(client, getpid()); |
| 4371 | } |
| 4372 | } |
| 4373 | #endif /* FEAT_GUI_GNOME && FEAT_SESSION */ |
| 4374 | |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 4375 | #if GTK_CHECK_VERSION(3,0,0) |
Bram Moolenaar | 36edf06 | 2016-07-21 22:10:12 +0200 | [diff] [blame] | 4376 | static GdkRGBA |
| 4377 | color_to_rgba(guicolor_T color) |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 4378 | { |
Bram Moolenaar | 36edf06 | 2016-07-21 22:10:12 +0200 | [diff] [blame] | 4379 | GdkRGBA rgba; |
| 4380 | rgba.red = ((color & 0xff0000) >> 16) / 255.0; |
| 4381 | rgba.green = ((color & 0xff00) >> 8) / 255.0; |
| 4382 | rgba.blue = ((color & 0xff)) / 255.0; |
| 4383 | rgba.alpha = 1.0; |
| 4384 | return rgba; |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 4385 | } |
| 4386 | |
| 4387 | static void |
Bram Moolenaar | 36edf06 | 2016-07-21 22:10:12 +0200 | [diff] [blame] | 4388 | set_cairo_source_rgba_from_color(cairo_t *cr, guicolor_T color) |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 4389 | { |
Bram Moolenaar | 36edf06 | 2016-07-21 22:10:12 +0200 | [diff] [blame] | 4390 | const GdkRGBA rgba = color_to_rgba(color); |
| 4391 | 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] | 4392 | } |
| 4393 | #endif /* GTK_CHECK_VERSION(3,0,0) */ |
| 4394 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4395 | /* |
| 4396 | * Called when the foreground or background color has been changed. |
| 4397 | * This used to change the graphics contexts directly but we are |
| 4398 | * currently manipulating them where desired. |
| 4399 | */ |
| 4400 | void |
| 4401 | gui_mch_new_colors(void) |
| 4402 | { |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 4403 | #if GTK_CHECK_VERSION(3,0,0) |
Bram Moolenaar | a859f04 | 2016-11-17 19:11:55 +0100 | [diff] [blame] | 4404 | # if !GTK_CHECK_VERSION(3,22,2) |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 4405 | GdkWindow * const da_win = gtk_widget_get_window(gui.drawarea); |
Bram Moolenaar | a859f04 | 2016-11-17 19:11:55 +0100 | [diff] [blame] | 4406 | # endif |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 4407 | |
| 4408 | if (gui.drawarea != NULL && gtk_widget_get_window(gui.drawarea) != NULL) |
| 4409 | #else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4410 | if (gui.drawarea != NULL && gui.drawarea->window != NULL) |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 4411 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4412 | { |
Bram Moolenaar | a859f04 | 2016-11-17 19:11:55 +0100 | [diff] [blame] | 4413 | #if GTK_CHECK_VERSION(3,22,2) |
| 4414 | GtkStyleContext * const context |
| 4415 | = gtk_widget_get_style_context(gui.drawarea); |
| 4416 | GtkCssProvider * const provider = gtk_css_provider_new(); |
| 4417 | gchar * const css = g_strdup_printf( |
| 4418 | "widget#vim-gui-drawarea {\n" |
| 4419 | " background-color: #%.2lx%.2lx%.2lx;\n" |
| 4420 | "}\n", |
| 4421 | (gui.back_pixel >> 16) & 0xff, |
| 4422 | (gui.back_pixel >> 8) & 0xff, |
| 4423 | gui.back_pixel & 0xff); |
| 4424 | |
| 4425 | gtk_css_provider_load_from_data(provider, css, -1, NULL); |
| 4426 | gtk_style_context_add_provider(context, |
| 4427 | GTK_STYLE_PROVIDER(provider), G_MAXUINT); |
| 4428 | |
| 4429 | g_free(css); |
| 4430 | g_object_unref(provider); |
| 4431 | #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] | 4432 | GdkRGBA rgba; |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 4433 | |
Bram Moolenaar | 36edf06 | 2016-07-21 22:10:12 +0200 | [diff] [blame] | 4434 | rgba = color_to_rgba(gui.back_pixel); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 4435 | { |
| 4436 | cairo_pattern_t * const pat = cairo_pattern_create_rgba( |
Bram Moolenaar | 36edf06 | 2016-07-21 22:10:12 +0200 | [diff] [blame] | 4437 | rgba.red, rgba.green, rgba.blue, rgba.alpha); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 4438 | if (pat != NULL) |
| 4439 | { |
| 4440 | gdk_window_set_background_pattern(da_win, pat); |
| 4441 | cairo_pattern_destroy(pat); |
| 4442 | } |
| 4443 | else |
Bram Moolenaar | 36edf06 | 2016-07-21 22:10:12 +0200 | [diff] [blame] | 4444 | gdk_window_set_background_rgba(da_win, &rgba); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 4445 | } |
| 4446 | #else /* !GTK_CHECK_VERSION(3,4,0) */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4447 | GdkColor color = { 0, 0, 0, 0 }; |
| 4448 | |
| 4449 | color.pixel = gui.back_pixel; |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 4450 | # if GTK_CHECK_VERSION(3,0,0) |
| 4451 | gdk_window_set_background(da_win, &color); |
| 4452 | # else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4453 | gdk_window_set_background(gui.drawarea->window, &color); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 4454 | # endif |
Bram Moolenaar | a859f04 | 2016-11-17 19:11:55 +0100 | [diff] [blame] | 4455 | #endif /* !GTK_CHECK_VERSION(3,22,2) */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4456 | } |
| 4457 | } |
| 4458 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4459 | /* |
| 4460 | * This signal informs us about the need to rearrange our sub-widgets. |
| 4461 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4462 | static gint |
Bram Moolenaar | b85cb21 | 2009-05-17 14:24:23 +0000 | [diff] [blame] | 4463 | form_configure_event(GtkWidget *widget UNUSED, |
| 4464 | GdkEventConfigure *event, |
| 4465 | gpointer data UNUSED) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4466 | { |
Bram Moolenaar | b3656ed | 2006-03-20 21:59:49 +0000 | [diff] [blame] | 4467 | int usable_height = event->height; |
| 4468 | |
Bram Moolenaar | 182707a | 2016-11-21 20:55:58 +0100 | [diff] [blame] | 4469 | #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] | 4470 | /* As of 3.22.2, GdkWindows have started distributing configure events to |
| 4471 | * their "native" children (https://git.gnome.org/browse/gtk+/commit/?h=gtk-3-22&id=12579fe71b3b8f79eb9c1b80e429443bcc437dd0). |
| 4472 | * |
| 4473 | * As can be seen from the implementation of move_native_children() and |
| 4474 | * configure_native_child() in gdkwindow.c, those functions actually |
| 4475 | * propagate configure events to every child, failing to distinguish |
| 4476 | * "native" one from non-native one. |
| 4477 | * |
| 4478 | * Naturally, configure events propagated to here like that are fallacious |
| 4479 | * and, as a matter of fact, they trigger a geometric collapse of |
| 4480 | * gui.formwin. |
| 4481 | * |
| 4482 | * To filter out such fallacious events, check if the given event is the |
| 4483 | * one that was sent out to the right place. Ignore it if not. |
| 4484 | */ |
Bram Moolenaar | 182707a | 2016-11-21 20:55:58 +0100 | [diff] [blame] | 4485 | /* Follow-up |
| 4486 | * After a few weeks later, the GdkWindow change mentioned above was |
| 4487 | * reverted (https://git.gnome.org/browse/gtk+/commit/?h=gtk-3-22&id=f70039cb9603a02d2369fec4038abf40a1711155). |
| 4488 | * The corresponding official release is 3.22.4. */ |
Bram Moolenaar | a859f04 | 2016-11-17 19:11:55 +0100 | [diff] [blame] | 4489 | if (event->window != gtk_widget_get_window(gui.formwin)) |
| 4490 | return TRUE; |
| 4491 | #endif |
| 4492 | |
Bram Moolenaar | b3656ed | 2006-03-20 21:59:49 +0000 | [diff] [blame] | 4493 | /* When in a GtkPlug, we can't guarantee valid heights (as a round |
| 4494 | * no. of char-heights), so we have to manually sanitise them. |
| 4495 | * Widths seem to sort themselves out, don't ask me why. |
| 4496 | */ |
| 4497 | if (gtk_socket_id != 0) |
Bram Moolenaar | c9b4b05 | 2006-04-30 18:54:39 +0000 | [diff] [blame] | 4498 | usable_height -= (gui.char_height - (gui.char_height/2)); /* sic. */ |
Bram Moolenaar | b3656ed | 2006-03-20 21:59:49 +0000 | [diff] [blame] | 4499 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4500 | gtk_form_freeze(GTK_FORM(gui.formwin)); |
Bram Moolenaar | b3656ed | 2006-03-20 21:59:49 +0000 | [diff] [blame] | 4501 | gui_resize_shell(event->width, usable_height); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4502 | gtk_form_thaw(GTK_FORM(gui.formwin)); |
| 4503 | |
| 4504 | return TRUE; |
| 4505 | } |
| 4506 | |
| 4507 | /* |
| 4508 | * Function called when window already closed. |
| 4509 | * We can't do much more here than to trying to preserve what had been done, |
| 4510 | * since the window is already inevitably going away. |
| 4511 | */ |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 4512 | #if GTK_CHECK_VERSION(3,0,0) |
| 4513 | static void |
| 4514 | mainwin_destroy_cb(GObject *object UNUSED, gpointer data UNUSED) |
| 4515 | #else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4516 | static void |
Bram Moolenaar | b85cb21 | 2009-05-17 14:24:23 +0000 | [diff] [blame] | 4517 | mainwin_destroy_cb(GtkObject *object UNUSED, gpointer data UNUSED) |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 4518 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4519 | { |
| 4520 | /* Don't write messages to the GUI anymore */ |
| 4521 | full_screen = FALSE; |
| 4522 | |
| 4523 | gui.mainwin = NULL; |
| 4524 | gui.drawarea = NULL; |
| 4525 | |
| 4526 | if (!exiting) /* only do anything if the destroy was unexpected */ |
| 4527 | { |
Bram Moolenaar | ce0842a | 2005-07-18 21:58:11 +0000 | [diff] [blame] | 4528 | vim_strncpy(IObuff, |
| 4529 | (char_u *)_("Vim: Main window unexpectedly destroyed\n"), |
| 4530 | IOSIZE - 1); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4531 | preserve_exit(); |
| 4532 | } |
Bram Moolenaar | 36e294c | 2015-12-29 18:55:46 +0100 | [diff] [blame] | 4533 | #ifdef USE_GRESOURCE |
| 4534 | gui_gtk_unregister_resource(); |
| 4535 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4536 | } |
| 4537 | |
Bram Moolenaar | b3656ed | 2006-03-20 21:59:49 +0000 | [diff] [blame] | 4538 | |
| 4539 | /* |
| 4540 | * Bit of a hack to ensure we start GtkPlug windows with the correct window |
| 4541 | * hints (and thus the required size from -geom), but that after that we |
| 4542 | * put the hints back to normal (the actual minimum size) so we may |
| 4543 | * subsequently be resized smaller. GtkSocket (the parent end) uses the |
Bram Moolenaar | 4932594 | 2007-05-10 19:19:59 +0000 | [diff] [blame] | 4544 | * 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] | 4545 | * only way we have of making ourselves bigger (by set lines/columns). |
| 4546 | * 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] | 4547 | * second after the final attempt to reset the real minimum hints (done by |
| 4548 | * scrollbar init.), actually do the standard hints and stop the timer. |
Bram Moolenaar | b3656ed | 2006-03-20 21:59:49 +0000 | [diff] [blame] | 4549 | * We'll not let the default hints be set while this timer's active. |
| 4550 | */ |
Bram Moolenaar | 4ab7968 | 2017-08-27 14:50:47 +0200 | [diff] [blame] | 4551 | static timeout_cb_type |
Bram Moolenaar | b85cb21 | 2009-05-17 14:24:23 +0000 | [diff] [blame] | 4552 | check_startup_plug_hints(gpointer data UNUSED) |
Bram Moolenaar | b3656ed | 2006-03-20 21:59:49 +0000 | [diff] [blame] | 4553 | { |
| 4554 | if (init_window_hints_state == 1) |
| 4555 | { |
Bram Moolenaar | c9b4b05 | 2006-04-30 18:54:39 +0000 | [diff] [blame] | 4556 | /* Safe to use normal hints now */ |
| 4557 | init_window_hints_state = 0; |
| 4558 | update_window_manager_hints(0, 0); |
| 4559 | return FALSE; /* stop timer */ |
Bram Moolenaar | b3656ed | 2006-03-20 21:59:49 +0000 | [diff] [blame] | 4560 | } |
| 4561 | |
| 4562 | /* Keep on trying */ |
| 4563 | init_window_hints_state = 1; |
| 4564 | return TRUE; |
| 4565 | } |
| 4566 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4567 | /* |
| 4568 | * Open the GUI window which was created by a call to gui_mch_init(). |
| 4569 | */ |
| 4570 | int |
| 4571 | gui_mch_open(void) |
| 4572 | { |
| 4573 | guicolor_T fg_pixel = INVALCOLOR; |
| 4574 | guicolor_T bg_pixel = INVALCOLOR; |
Bram Moolenaar | 79ef6d6 | 2009-09-23 15:35:48 +0000 | [diff] [blame] | 4575 | guint pixel_width; |
| 4576 | guint pixel_height; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4577 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4578 | /* |
| 4579 | * Allow setting a window role on the command line, or invent one |
| 4580 | * if none was specified. This is mainly useful for GNOME session |
| 4581 | * support; allowing the WM to restore window placement. |
| 4582 | */ |
| 4583 | if (role_argument != NULL) |
| 4584 | { |
| 4585 | gtk_window_set_role(GTK_WINDOW(gui.mainwin), role_argument); |
| 4586 | } |
| 4587 | else |
| 4588 | { |
| 4589 | char *role; |
| 4590 | |
| 4591 | /* Invent a unique-enough ID string for the role */ |
| 4592 | role = g_strdup_printf("vim-%u-%u-%u", |
| 4593 | (unsigned)mch_get_pid(), |
| 4594 | (unsigned)g_random_int(), |
| 4595 | (unsigned)time(NULL)); |
| 4596 | |
| 4597 | gtk_window_set_role(GTK_WINDOW(gui.mainwin), role); |
| 4598 | g_free(role); |
| 4599 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4600 | |
| 4601 | if (gui_win_x != -1 && gui_win_y != -1) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4602 | 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] | 4603 | |
| 4604 | /* Determine user specified geometry, if present. */ |
| 4605 | if (gui.geom != NULL) |
| 4606 | { |
| 4607 | int mask; |
| 4608 | unsigned int w, h; |
| 4609 | int x = 0; |
| 4610 | int y = 0; |
| 4611 | |
| 4612 | mask = XParseGeometry((char *)gui.geom, &x, &y, &w, &h); |
| 4613 | |
| 4614 | if (mask & WidthValue) |
| 4615 | Columns = w; |
| 4616 | if (mask & HeightValue) |
Bram Moolenaar | d68071d | 2006-05-02 22:08:30 +0000 | [diff] [blame] | 4617 | { |
Bram Moolenaar | b85cb21 | 2009-05-17 14:24:23 +0000 | [diff] [blame] | 4618 | if (p_window > (long)h - 1 || !option_was_set((char_u *)"window")) |
Bram Moolenaar | d68071d | 2006-05-02 22:08:30 +0000 | [diff] [blame] | 4619 | p_window = h - 1; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4620 | Rows = h; |
Bram Moolenaar | d68071d | 2006-05-02 22:08:30 +0000 | [diff] [blame] | 4621 | } |
Bram Moolenaar | e057d40 | 2013-06-30 17:51:51 +0200 | [diff] [blame] | 4622 | limit_screen_size(); |
Bram Moolenaar | 2dd8b52 | 2007-10-19 12:33:44 +0000 | [diff] [blame] | 4623 | |
| 4624 | pixel_width = (guint)(gui_get_base_width() + Columns * gui.char_width); |
| 4625 | pixel_height = (guint)(gui_get_base_height() + Rows * gui.char_height); |
| 4626 | |
Bram Moolenaar | 2dd8b52 | 2007-10-19 12:33:44 +0000 | [diff] [blame] | 4627 | pixel_width += get_menu_tool_width(); |
| 4628 | pixel_height += get_menu_tool_height(); |
Bram Moolenaar | 2dd8b52 | 2007-10-19 12:33:44 +0000 | [diff] [blame] | 4629 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4630 | if (mask & (XValue | YValue)) |
Bram Moolenaar | 2dd8b52 | 2007-10-19 12:33:44 +0000 | [diff] [blame] | 4631 | { |
Bram Moolenaar | fe86f2d | 2008-11-28 20:29:07 +0000 | [diff] [blame] | 4632 | int ww, hh; |
| 4633 | gui_mch_get_screen_dimensions(&ww, &hh); |
| 4634 | hh += p_ghr + get_menu_tool_height(); |
| 4635 | ww += get_menu_tool_width(); |
Bram Moolenaar | 2dd8b52 | 2007-10-19 12:33:44 +0000 | [diff] [blame] | 4636 | if (mask & XNegative) |
Bram Moolenaar | fe86f2d | 2008-11-28 20:29:07 +0000 | [diff] [blame] | 4637 | x += ww - pixel_width; |
Bram Moolenaar | 2dd8b52 | 2007-10-19 12:33:44 +0000 | [diff] [blame] | 4638 | if (mask & YNegative) |
Bram Moolenaar | fe86f2d | 2008-11-28 20:29:07 +0000 | [diff] [blame] | 4639 | y += hh - pixel_height; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4640 | gtk_window_move(GTK_WINDOW(gui.mainwin), x, y); |
Bram Moolenaar | 2dd8b52 | 2007-10-19 12:33:44 +0000 | [diff] [blame] | 4641 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4642 | vim_free(gui.geom); |
| 4643 | gui.geom = NULL; |
Bram Moolenaar | b3656ed | 2006-03-20 21:59:49 +0000 | [diff] [blame] | 4644 | |
Bram Moolenaar | c9b4b05 | 2006-04-30 18:54:39 +0000 | [diff] [blame] | 4645 | /* From now until everyone's stopped trying to set the window hints |
| 4646 | * to their correct minimum values, stop them being set as we need |
| 4647 | * them to remain at our required size for the parent GtkSocket to |
| 4648 | * give us the right initial size. |
| 4649 | */ |
Bram Moolenaar | b3656ed | 2006-03-20 21:59:49 +0000 | [diff] [blame] | 4650 | if (gtk_socket_id != 0 && (mask & WidthValue || mask & HeightValue)) |
Bram Moolenaar | c9b4b05 | 2006-04-30 18:54:39 +0000 | [diff] [blame] | 4651 | { |
Bram Moolenaar | c9b4b05 | 2006-04-30 18:54:39 +0000 | [diff] [blame] | 4652 | update_window_manager_hints(pixel_width, pixel_height); |
| 4653 | init_window_hints_state = 1; |
Bram Moolenaar | 4ab7968 | 2017-08-27 14:50:47 +0200 | [diff] [blame] | 4654 | timeout_add(1000, check_startup_plug_hints, NULL); |
Bram Moolenaar | c9b4b05 | 2006-04-30 18:54:39 +0000 | [diff] [blame] | 4655 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4656 | } |
| 4657 | |
Bram Moolenaar | 79ef6d6 | 2009-09-23 15:35:48 +0000 | [diff] [blame] | 4658 | pixel_width = (guint)(gui_get_base_width() + Columns * gui.char_width); |
| 4659 | pixel_height = (guint)(gui_get_base_height() + Rows * gui.char_height); |
Bram Moolenaar | 79ef6d6 | 2009-09-23 15:35:48 +0000 | [diff] [blame] | 4660 | /* For GTK2 changing the size of the form widget doesn't cause window |
| 4661 | * resizing. */ |
Bram Moolenaar | debe25a | 2010-06-06 17:41:24 +0200 | [diff] [blame] | 4662 | if (gtk_socket_id == 0) |
Bram Moolenaar | 79ef6d6 | 2009-09-23 15:35:48 +0000 | [diff] [blame] | 4663 | gtk_window_resize(GTK_WINDOW(gui.mainwin), pixel_width, pixel_height); |
Bram Moolenaar | b3656ed | 2006-03-20 21:59:49 +0000 | [diff] [blame] | 4664 | update_window_manager_hints(0, 0); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4665 | |
| 4666 | if (foreground_argument != NULL) |
| 4667 | fg_pixel = gui_get_color((char_u *)foreground_argument); |
| 4668 | if (fg_pixel == INVALCOLOR) |
| 4669 | fg_pixel = gui_get_color((char_u *)"Black"); |
| 4670 | |
| 4671 | if (background_argument != NULL) |
| 4672 | bg_pixel = gui_get_color((char_u *)background_argument); |
| 4673 | if (bg_pixel == INVALCOLOR) |
| 4674 | bg_pixel = gui_get_color((char_u *)"White"); |
| 4675 | |
| 4676 | if (found_reverse_arg) |
| 4677 | { |
| 4678 | gui.def_norm_pixel = bg_pixel; |
| 4679 | gui.def_back_pixel = fg_pixel; |
| 4680 | } |
| 4681 | else |
| 4682 | { |
| 4683 | gui.def_norm_pixel = fg_pixel; |
| 4684 | gui.def_back_pixel = bg_pixel; |
| 4685 | } |
| 4686 | |
| 4687 | /* Get the colors from the "Normal" and "Menu" group (set in syntax.c or |
| 4688 | * in a vimrc file) */ |
| 4689 | set_normal_colors(); |
| 4690 | |
| 4691 | /* Check that none of the colors are the same as the background color */ |
| 4692 | gui_check_colors(); |
| 4693 | |
| 4694 | /* Get the colors for the highlight groups (gui_check_colors() might have |
| 4695 | * changed them). */ |
| 4696 | highlight_gui_started(); /* re-init colors and fonts */ |
| 4697 | |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 4698 | #if GTK_CHECK_VERSION(3,0,0) |
| 4699 | g_signal_connect(G_OBJECT(gui.mainwin), "destroy", |
| 4700 | G_CALLBACK(mainwin_destroy_cb), NULL); |
| 4701 | #else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4702 | gtk_signal_connect(GTK_OBJECT(gui.mainwin), "destroy", |
| 4703 | GTK_SIGNAL_FUNC(mainwin_destroy_cb), NULL); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 4704 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4705 | |
| 4706 | #ifdef FEAT_HANGULIN |
| 4707 | hangul_keyboard_set(); |
| 4708 | #endif |
| 4709 | |
| 4710 | /* |
| 4711 | * Notify the fixed area about the need to resize the contents of the |
| 4712 | * gui.formwin, which we use for random positioning of the included |
| 4713 | * components. |
| 4714 | * |
| 4715 | * We connect this signal deferred finally after anything is in place, |
| 4716 | * since this is intended to handle resizements coming from the window |
| 4717 | * manager upon us and should not interfere with what VIM is requesting |
| 4718 | * upon startup. |
| 4719 | */ |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 4720 | #if GTK_CHECK_VERSION(3,0,0) |
| 4721 | g_signal_connect(G_OBJECT(gui.formwin), "configure-event", |
| 4722 | G_CALLBACK(form_configure_event), NULL); |
| 4723 | #else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4724 | gtk_signal_connect(GTK_OBJECT(gui.formwin), "configure_event", |
| 4725 | GTK_SIGNAL_FUNC(form_configure_event), NULL); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 4726 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4727 | |
| 4728 | #ifdef FEAT_DND |
Bram Moolenaar | a76638f | 2010-06-05 12:49:46 +0200 | [diff] [blame] | 4729 | /* Set up for receiving DND items. */ |
| 4730 | gui_gtk_set_dnd_targets(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4731 | |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 4732 | # if GTK_CHECK_VERSION(3,0,0) |
| 4733 | g_signal_connect(G_OBJECT(gui.drawarea), "drag-data-received", |
| 4734 | G_CALLBACK(drag_data_received_cb), NULL); |
| 4735 | # else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4736 | gtk_signal_connect(GTK_OBJECT(gui.drawarea), "drag_data_received", |
| 4737 | GTK_SIGNAL_FUNC(drag_data_received_cb), NULL); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 4738 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4739 | #endif |
| 4740 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4741 | /* With GTK+ 2, we need to iconify the window before calling show() |
Bram Moolenaar | 182c5be | 2010-06-25 05:37:59 +0200 | [diff] [blame] | 4742 | * to avoid mapping the window for a short time. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4743 | if (found_iconic_arg && gtk_socket_id == 0) |
| 4744 | gui_mch_iconify(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4745 | |
| 4746 | { |
Bram Moolenaar | 182c5be | 2010-06-25 05:37:59 +0200 | [diff] [blame] | 4747 | #if defined(FEAT_GUI_GNOME) && defined(FEAT_MENU) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4748 | unsigned long menu_handler = 0; |
| 4749 | # ifdef FEAT_TOOLBAR |
| 4750 | unsigned long tool_handler = 0; |
| 4751 | # endif |
| 4752 | /* |
| 4753 | * Urgh hackish :/ For some reason BonoboDockLayout always forces a |
| 4754 | * show when restoring the saved layout configuration. We can't just |
| 4755 | * hide the widgets again after gtk_widget_show(gui.mainwin) since it's |
| 4756 | * a toplevel window and thus will be realized immediately. Instead, |
| 4757 | * connect signal handlers to hide the widgets just after they've been |
| 4758 | * marked visible, but before the main window is realized. |
| 4759 | */ |
| 4760 | if (using_gnome && vim_strchr(p_go, GO_MENUS) == NULL) |
| 4761 | menu_handler = g_signal_connect_after(gui.menubar_h, "show", |
| 4762 | G_CALLBACK(>k_widget_hide), |
| 4763 | NULL); |
| 4764 | # ifdef FEAT_TOOLBAR |
| 4765 | if (using_gnome && vim_strchr(p_go, GO_TOOLBAR) == NULL |
| 4766 | && (toolbar_flags & (TOOLBAR_TEXT | TOOLBAR_ICONS))) |
| 4767 | tool_handler = g_signal_connect_after(gui.toolbar_h, "show", |
| 4768 | G_CALLBACK(>k_widget_hide), |
| 4769 | NULL); |
| 4770 | # endif |
| 4771 | #endif |
| 4772 | gtk_widget_show(gui.mainwin); |
| 4773 | |
Bram Moolenaar | 182c5be | 2010-06-25 05:37:59 +0200 | [diff] [blame] | 4774 | #if defined(FEAT_GUI_GNOME) && defined(FEAT_MENU) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4775 | if (menu_handler != 0) |
| 4776 | g_signal_handler_disconnect(gui.menubar_h, menu_handler); |
| 4777 | # ifdef FEAT_TOOLBAR |
| 4778 | if (tool_handler != 0) |
| 4779 | g_signal_handler_disconnect(gui.toolbar_h, tool_handler); |
| 4780 | # endif |
| 4781 | #endif |
| 4782 | } |
| 4783 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4784 | return OK; |
| 4785 | } |
| 4786 | |
| 4787 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4788 | void |
Bram Moolenaar | b85cb21 | 2009-05-17 14:24:23 +0000 | [diff] [blame] | 4789 | gui_mch_exit(int rc UNUSED) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4790 | { |
| 4791 | if (gui.mainwin != NULL) |
| 4792 | gtk_widget_destroy(gui.mainwin); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4793 | } |
| 4794 | |
| 4795 | /* |
| 4796 | * Get the position of the top left corner of the window. |
| 4797 | */ |
| 4798 | int |
| 4799 | gui_mch_get_winpos(int *x, int *y) |
| 4800 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4801 | gtk_window_get_position(GTK_WINDOW(gui.mainwin), x, y); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4802 | return OK; |
| 4803 | } |
| 4804 | |
| 4805 | /* |
| 4806 | * Set the position of the top left corner of the window to the given |
| 4807 | * coordinates. |
| 4808 | */ |
| 4809 | void |
| 4810 | gui_mch_set_winpos(int x, int y) |
| 4811 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4812 | gtk_window_move(GTK_WINDOW(gui.mainwin), x, y); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4813 | } |
| 4814 | |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 4815 | #if !GTK_CHECK_VERSION(3,0,0) |
| 4816 | # if 0 |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4817 | static int resize_idle_installed = FALSE; |
| 4818 | /* |
| 4819 | * Idle handler to force resize. Used by gui_mch_set_shellsize() to ensure |
| 4820 | * the shell size doesn't exceed the window size, i.e. if the window manager |
| 4821 | * ignored our size request. Usually this happens if the window is maximized. |
| 4822 | * |
| 4823 | * FIXME: It'd be nice if we could find a little more orthodox solution. |
| 4824 | * See also the remark below in gui_mch_set_shellsize(). |
| 4825 | * |
| 4826 | * DISABLED: When doing ":set lines+=1" this function would first invoke |
| 4827 | * gui_resize_shell() with the old size, then the normal callback would |
| 4828 | * report the new size through form_configure_event(). That caused the window |
| 4829 | * layout to be messed up. |
| 4830 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4831 | static gboolean |
| 4832 | force_shell_resize_idle(gpointer data) |
| 4833 | { |
| 4834 | if (gui.mainwin != NULL |
| 4835 | && GTK_WIDGET_REALIZED(gui.mainwin) |
| 4836 | && GTK_WIDGET_VISIBLE(gui.mainwin)) |
| 4837 | { |
| 4838 | int width; |
| 4839 | int height; |
| 4840 | |
| 4841 | gtk_window_get_size(GTK_WINDOW(gui.mainwin), &width, &height); |
| 4842 | |
| 4843 | width -= get_menu_tool_width(); |
| 4844 | height -= get_menu_tool_height(); |
| 4845 | |
| 4846 | gui_resize_shell(width, height); |
| 4847 | } |
| 4848 | |
| 4849 | resize_idle_installed = FALSE; |
| 4850 | return FALSE; /* don't call me again */ |
| 4851 | } |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 4852 | # endif |
| 4853 | #endif /* !GTK_CHECK_VERSION(3,0,0) */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4854 | |
Bram Moolenaar | 0973623 | 2009-09-23 16:14:49 +0000 | [diff] [blame] | 4855 | /* |
| 4856 | * Return TRUE if the main window is maximized. |
| 4857 | */ |
| 4858 | int |
Bram Moolenaar | 66f948e | 2016-01-30 16:39:25 +0100 | [diff] [blame] | 4859 | gui_mch_maximized(void) |
Bram Moolenaar | 0973623 | 2009-09-23 16:14:49 +0000 | [diff] [blame] | 4860 | { |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 4861 | #if GTK_CHECK_VERSION(3,0,0) |
| 4862 | return (gui.mainwin != NULL && gtk_widget_get_window(gui.mainwin) != NULL |
| 4863 | && (gdk_window_get_state(gtk_widget_get_window(gui.mainwin)) |
| 4864 | & GDK_WINDOW_STATE_MAXIMIZED)); |
| 4865 | #else |
Bram Moolenaar | 0973623 | 2009-09-23 16:14:49 +0000 | [diff] [blame] | 4866 | return (gui.mainwin != NULL && gui.mainwin->window != NULL |
| 4867 | && (gdk_window_get_state(gui.mainwin->window) |
| 4868 | & GDK_WINDOW_STATE_MAXIMIZED)); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 4869 | #endif |
Bram Moolenaar | 0973623 | 2009-09-23 16:14:49 +0000 | [diff] [blame] | 4870 | } |
| 4871 | |
| 4872 | /* |
| 4873 | * Unmaximize the main window |
| 4874 | */ |
| 4875 | void |
Bram Moolenaar | 66f948e | 2016-01-30 16:39:25 +0100 | [diff] [blame] | 4876 | gui_mch_unmaximize(void) |
Bram Moolenaar | 0973623 | 2009-09-23 16:14:49 +0000 | [diff] [blame] | 4877 | { |
| 4878 | if (gui.mainwin != NULL) |
| 4879 | gtk_window_unmaximize(GTK_WINDOW(gui.mainwin)); |
| 4880 | } |
Bram Moolenaar | 0973623 | 2009-09-23 16:14:49 +0000 | [diff] [blame] | 4881 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4882 | /* |
Bram Moolenaar | 12bc1b5 | 2011-08-10 17:44:45 +0200 | [diff] [blame] | 4883 | * Called when the font changed while the window is maximized. Compute the |
| 4884 | * new Rows and Columns. This is like resizing the window. |
| 4885 | */ |
| 4886 | void |
Bram Moolenaar | 66f948e | 2016-01-30 16:39:25 +0100 | [diff] [blame] | 4887 | gui_mch_newfont(void) |
Bram Moolenaar | 12bc1b5 | 2011-08-10 17:44:45 +0200 | [diff] [blame] | 4888 | { |
| 4889 | int w, h; |
| 4890 | |
| 4891 | gtk_window_get_size(GTK_WINDOW(gui.mainwin), &w, &h); |
| 4892 | w -= get_menu_tool_width(); |
| 4893 | h -= get_menu_tool_height(); |
| 4894 | gui_resize_shell(w, h); |
| 4895 | } |
| 4896 | |
| 4897 | /* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4898 | * Set the windows size. |
| 4899 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4900 | void |
| 4901 | gui_mch_set_shellsize(int width, int height, |
Bram Moolenaar | b85cb21 | 2009-05-17 14:24:23 +0000 | [diff] [blame] | 4902 | int min_width UNUSED, int min_height UNUSED, |
| 4903 | int base_width UNUSED, int base_height UNUSED, |
| 4904 | int direction UNUSED) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4905 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4906 | /* give GTK+ a chance to put all widget's into place */ |
| 4907 | gui_mch_update(); |
| 4908 | |
Bram Moolenaar | b3656ed | 2006-03-20 21:59:49 +0000 | [diff] [blame] | 4909 | /* this will cause the proper resizement to happen too */ |
| 4910 | if (gtk_socket_id == 0) |
Bram Moolenaar | c9b4b05 | 2006-04-30 18:54:39 +0000 | [diff] [blame] | 4911 | update_window_manager_hints(0, 0); |
Bram Moolenaar | b3656ed | 2006-03-20 21:59:49 +0000 | [diff] [blame] | 4912 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4913 | /* 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] | 4914 | * 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] | 4915 | * main window instead. */ |
| 4916 | width += get_menu_tool_width(); |
| 4917 | height += get_menu_tool_height(); |
| 4918 | |
Bram Moolenaar | b3656ed | 2006-03-20 21:59:49 +0000 | [diff] [blame] | 4919 | if (gtk_socket_id == 0) |
Bram Moolenaar | c9b4b05 | 2006-04-30 18:54:39 +0000 | [diff] [blame] | 4920 | gtk_window_resize(GTK_WINDOW(gui.mainwin), width, height); |
Bram Moolenaar | b3656ed | 2006-03-20 21:59:49 +0000 | [diff] [blame] | 4921 | else |
Bram Moolenaar | c9b4b05 | 2006-04-30 18:54:39 +0000 | [diff] [blame] | 4922 | update_window_manager_hints(width, height); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4923 | |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 4924 | # if !GTK_CHECK_VERSION(3,0,0) |
| 4925 | # if 0 |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4926 | if (!resize_idle_installed) |
| 4927 | { |
| 4928 | g_idle_add_full(GDK_PRIORITY_EVENTS + 10, |
| 4929 | &force_shell_resize_idle, NULL, NULL); |
| 4930 | resize_idle_installed = TRUE; |
| 4931 | } |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 4932 | # endif |
| 4933 | # endif /* !GTK_CHECK_VERSION(3,0,0) */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4934 | /* |
| 4935 | * Wait until all events are processed to prevent a crash because the |
| 4936 | * real size of the drawing area doesn't reflect Vim's internal ideas. |
| 4937 | * |
| 4938 | * This is a bit of a hack, since Vim is a terminal application with a GUI |
| 4939 | * on top, while the GUI expects to be the boss. |
| 4940 | */ |
| 4941 | gui_mch_update(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4942 | } |
| 4943 | |
| 4944 | |
| 4945 | /* |
| 4946 | * The screen size is used to make sure the initial window doesn't get bigger |
| 4947 | * than the screen. This subtracts some room for menubar, toolbar and window |
| 4948 | * decorations. |
| 4949 | */ |
| 4950 | void |
| 4951 | gui_mch_get_screen_dimensions(int *screen_w, int *screen_h) |
| 4952 | { |
Bram Moolenaar | b463e8d | 2017-06-05 15:07:09 +0200 | [diff] [blame] | 4953 | #if GTK_CHECK_VERSION(3,22,2) |
Bram Moolenaar | a859f04 | 2016-11-17 19:11:55 +0100 | [diff] [blame] | 4954 | GdkRectangle rect; |
| 4955 | GdkMonitor * const mon = gdk_display_get_monitor_at_window( |
| 4956 | gtk_widget_get_display(gui.mainwin), |
| 4957 | gtk_widget_get_window(gui.mainwin)); |
| 4958 | gdk_monitor_get_geometry(mon, &rect); |
| 4959 | |
| 4960 | *screen_w = rect.width; |
Bram Moolenaar | b463e8d | 2017-06-05 15:07:09 +0200 | [diff] [blame] | 4961 | /* Subtract 'guiheadroom' from the height to allow some room for the |
| 4962 | * window manager (task list and window title bar). */ |
Bram Moolenaar | a859f04 | 2016-11-17 19:11:55 +0100 | [diff] [blame] | 4963 | *screen_h = rect.height - p_ghr; |
Bram Moolenaar | b463e8d | 2017-06-05 15:07:09 +0200 | [diff] [blame] | 4964 | #else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4965 | GdkScreen* screen; |
| 4966 | |
| 4967 | if (gui.mainwin != NULL && gtk_widget_has_screen(gui.mainwin)) |
| 4968 | screen = gtk_widget_get_screen(gui.mainwin); |
| 4969 | else |
| 4970 | screen = gdk_screen_get_default(); |
| 4971 | |
| 4972 | *screen_w = gdk_screen_get_width(screen); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4973 | /* Subtract 'guiheadroom' from the height to allow some room for the |
| 4974 | * window manager (task list and window title bar). */ |
Bram Moolenaar | b463e8d | 2017-06-05 15:07:09 +0200 | [diff] [blame] | 4975 | *screen_h = gdk_screen_get_height(screen) - p_ghr; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4976 | #endif |
| 4977 | |
| 4978 | /* |
| 4979 | * FIXME: dirty trick: Because the gui_get_base_height() doesn't include |
| 4980 | * the toolbar and menubar for GTK, we subtract them from the screen |
Bram Moolenaar | 6440447 | 2010-06-26 06:24:45 +0200 | [diff] [blame] | 4981 | * 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] | 4982 | * This should be completely changed later. |
| 4983 | */ |
| 4984 | *screen_w -= get_menu_tool_width(); |
| 4985 | *screen_h -= get_menu_tool_height(); |
| 4986 | } |
| 4987 | |
| 4988 | #if defined(FEAT_TITLE) || defined(PROTO) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4989 | void |
Bram Moolenaar | b85cb21 | 2009-05-17 14:24:23 +0000 | [diff] [blame] | 4990 | gui_mch_settitle(char_u *title, char_u *icon UNUSED) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4991 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4992 | if (title != NULL && output_conv.vc_type != CONV_NONE) |
| 4993 | title = string_convert(&output_conv, title, NULL); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4994 | |
| 4995 | gtk_window_set_title(GTK_WINDOW(gui.mainwin), (const char *)title); |
| 4996 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4997 | if (output_conv.vc_type != CONV_NONE) |
| 4998 | vim_free(title); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4999 | } |
| 5000 | #endif /* FEAT_TITLE */ |
| 5001 | |
| 5002 | #if defined(FEAT_MENU) || defined(PROTO) |
| 5003 | void |
| 5004 | gui_mch_enable_menu(int showit) |
| 5005 | { |
| 5006 | GtkWidget *widget; |
| 5007 | |
| 5008 | # ifdef FEAT_GUI_GNOME |
| 5009 | if (using_gnome) |
| 5010 | widget = gui.menubar_h; |
| 5011 | else |
| 5012 | # endif |
| 5013 | widget = gui.menubar; |
| 5014 | |
Bram Moolenaar | 18144c8 | 2006-04-12 21:52:12 +0000 | [diff] [blame] | 5015 | /* 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] | 5016 | # if GTK_CHECK_VERSION(3,0,0) |
| 5017 | if (!showit != !gtk_widget_get_visible(widget) && !gui.starting) |
| 5018 | # else |
Bram Moolenaar | 18144c8 | 2006-04-12 21:52:12 +0000 | [diff] [blame] | 5019 | if (!showit != !GTK_WIDGET_VISIBLE(widget) && !gui.starting) |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 5020 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5021 | { |
| 5022 | if (showit) |
| 5023 | gtk_widget_show(widget); |
| 5024 | else |
| 5025 | gtk_widget_hide(widget); |
| 5026 | |
Bram Moolenaar | b3656ed | 2006-03-20 21:59:49 +0000 | [diff] [blame] | 5027 | update_window_manager_hints(0, 0); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5028 | } |
| 5029 | } |
| 5030 | #endif /* FEAT_MENU */ |
| 5031 | |
| 5032 | #if defined(FEAT_TOOLBAR) || defined(PROTO) |
| 5033 | void |
| 5034 | gui_mch_show_toolbar(int showit) |
| 5035 | { |
| 5036 | GtkWidget *widget; |
| 5037 | |
| 5038 | if (gui.toolbar == NULL) |
| 5039 | return; |
| 5040 | |
| 5041 | # ifdef FEAT_GUI_GNOME |
| 5042 | if (using_gnome) |
| 5043 | widget = gui.toolbar_h; |
| 5044 | else |
| 5045 | # endif |
| 5046 | widget = gui.toolbar; |
| 5047 | |
| 5048 | if (showit) |
| 5049 | set_toolbar_style(GTK_TOOLBAR(gui.toolbar)); |
| 5050 | |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 5051 | # if GTK_CHECK_VERSION(3,0,0) |
| 5052 | if (!showit != !gtk_widget_get_visible(widget)) |
| 5053 | # else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5054 | if (!showit != !GTK_WIDGET_VISIBLE(widget)) |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 5055 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5056 | { |
| 5057 | if (showit) |
| 5058 | gtk_widget_show(widget); |
| 5059 | else |
| 5060 | gtk_widget_hide(widget); |
| 5061 | |
Bram Moolenaar | b3656ed | 2006-03-20 21:59:49 +0000 | [diff] [blame] | 5062 | update_window_manager_hints(0, 0); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5063 | } |
| 5064 | } |
| 5065 | #endif /* FEAT_TOOLBAR */ |
| 5066 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5067 | /* |
| 5068 | * Check if a given font is a CJK font. This is done in a very crude manner. It |
| 5069 | * just see if U+04E00 for zh and ja and U+AC00 for ko are covered in a given |
| 5070 | * font. Consequently, this function cannot be used as a general purpose check |
| 5071 | * for CJK-ness for which fontconfig APIs should be used. This is only used by |
| 5072 | * gui_mch_init_font() to deal with 'CJK fixed width fonts'. |
| 5073 | */ |
| 5074 | static int |
| 5075 | is_cjk_font(PangoFontDescription *font_desc) |
| 5076 | { |
| 5077 | static const char * const cjk_langs[] = |
| 5078 | {"zh_CN", "zh_TW", "zh_HK", "ja", "ko"}; |
| 5079 | |
| 5080 | PangoFont *font; |
| 5081 | unsigned i; |
| 5082 | int is_cjk = FALSE; |
| 5083 | |
| 5084 | font = pango_context_load_font(gui.text_context, font_desc); |
| 5085 | |
| 5086 | if (font == NULL) |
| 5087 | return FALSE; |
| 5088 | |
| 5089 | for (i = 0; !is_cjk && i < G_N_ELEMENTS(cjk_langs); ++i) |
| 5090 | { |
| 5091 | PangoCoverage *coverage; |
| 5092 | gunichar uc; |
| 5093 | |
| 5094 | coverage = pango_font_get_coverage( |
| 5095 | font, pango_language_from_string(cjk_langs[i])); |
| 5096 | |
| 5097 | if (coverage != NULL) |
| 5098 | { |
| 5099 | uc = (cjk_langs[i][0] == 'k') ? 0xAC00 : 0x4E00; |
| 5100 | is_cjk = (pango_coverage_get(coverage, uc) == PANGO_COVERAGE_EXACT); |
| 5101 | pango_coverage_unref(coverage); |
| 5102 | } |
| 5103 | } |
| 5104 | |
| 5105 | g_object_unref(font); |
| 5106 | |
| 5107 | return is_cjk; |
| 5108 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5109 | |
Bram Moolenaar | 231334e | 2005-07-25 20:46:57 +0000 | [diff] [blame] | 5110 | /* |
| 5111 | * Adjust gui.char_height (after 'linespace' was changed). |
| 5112 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5113 | int |
Bram Moolenaar | 231334e | 2005-07-25 20:46:57 +0000 | [diff] [blame] | 5114 | gui_mch_adjust_charheight(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5115 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5116 | PangoFontMetrics *metrics; |
| 5117 | int ascent; |
| 5118 | int descent; |
| 5119 | |
| 5120 | metrics = pango_context_get_metrics(gui.text_context, gui.norm_font, |
| 5121 | pango_context_get_language(gui.text_context)); |
| 5122 | ascent = pango_font_metrics_get_ascent(metrics); |
| 5123 | descent = pango_font_metrics_get_descent(metrics); |
| 5124 | |
| 5125 | pango_font_metrics_unref(metrics); |
| 5126 | |
| 5127 | gui.char_height = (ascent + descent + PANGO_SCALE - 1) / PANGO_SCALE |
Bram Moolenaar | 231334e | 2005-07-25 20:46:57 +0000 | [diff] [blame] | 5128 | + p_linespace; |
Bram Moolenaar | b71ec9f | 2005-01-25 22:22:02 +0000 | [diff] [blame] | 5129 | /* LINTED: avoid warning: bitwise operation on signed value */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5130 | gui.char_ascent = PANGO_PIXELS(ascent + p_linespace * PANGO_SCALE / 2); |
| 5131 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5132 | /* A not-positive value of char_height may crash Vim. Only happens |
| 5133 | * if 'linespace' is negative (which does make sense sometimes). */ |
| 5134 | gui.char_ascent = MAX(gui.char_ascent, 0); |
| 5135 | gui.char_height = MAX(gui.char_height, gui.char_ascent + 1); |
| 5136 | |
| 5137 | return OK; |
| 5138 | } |
| 5139 | |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 5140 | #if GTK_CHECK_VERSION(3,0,0) |
| 5141 | /* Callback function used in gui_mch_font_dialog() */ |
| 5142 | static gboolean |
| 5143 | font_filter(const PangoFontFamily *family, |
| 5144 | const PangoFontFace *face UNUSED, |
| 5145 | gpointer data UNUSED) |
| 5146 | { |
| 5147 | return pango_font_family_is_monospace((PangoFontFamily *)family); |
| 5148 | } |
| 5149 | #endif |
| 5150 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5151 | /* |
| 5152 | * Put up a font dialog and return the selected font name in allocated memory. |
| 5153 | * "oldval" is the previous value. Return NULL when cancelled. |
| 5154 | * This should probably go into gui_gtk.c. Hmm. |
| 5155 | * FIXME: |
| 5156 | * The GTK2 font selection dialog has no filtering API. So we could either |
| 5157 | * a) implement our own (possibly copying the code from somewhere else) or |
| 5158 | * b) just live with it. |
| 5159 | */ |
| 5160 | char_u * |
| 5161 | gui_mch_font_dialog(char_u *oldval) |
| 5162 | { |
| 5163 | GtkWidget *dialog; |
| 5164 | int response; |
| 5165 | char_u *fontname = NULL; |
| 5166 | char_u *oldname; |
| 5167 | |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 5168 | #if GTK_CHECK_VERSION(3,2,0) |
| 5169 | dialog = gtk_font_chooser_dialog_new(NULL, NULL); |
| 5170 | gtk_font_chooser_set_filter_func(GTK_FONT_CHOOSER(dialog), font_filter, |
| 5171 | NULL, NULL); |
| 5172 | #else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5173 | dialog = gtk_font_selection_dialog_new(NULL); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 5174 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5175 | |
| 5176 | gtk_window_set_transient_for(GTK_WINDOW(dialog), GTK_WINDOW(gui.mainwin)); |
| 5177 | gtk_window_set_destroy_with_parent(GTK_WINDOW(dialog), TRUE); |
| 5178 | |
| 5179 | if (oldval != NULL && oldval[0] != NUL) |
| 5180 | { |
| 5181 | if (output_conv.vc_type != CONV_NONE) |
| 5182 | oldname = string_convert(&output_conv, oldval, NULL); |
| 5183 | else |
| 5184 | oldname = oldval; |
| 5185 | |
| 5186 | /* Annoying bug in GTK (or Pango): if the font name does not include a |
| 5187 | * size, zero is used. Use default point size ten. */ |
| 5188 | if (!vim_isdigit(oldname[STRLEN(oldname) - 1])) |
| 5189 | { |
| 5190 | char_u *p = vim_strnsave(oldname, STRLEN(oldname) + 3); |
| 5191 | |
| 5192 | if (p != NULL) |
| 5193 | { |
| 5194 | STRCPY(p + STRLEN(p), " 10"); |
| 5195 | if (oldname != oldval) |
| 5196 | vim_free(oldname); |
| 5197 | oldname = p; |
| 5198 | } |
| 5199 | } |
| 5200 | |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 5201 | #if GTK_CHECK_VERSION(3,2,0) |
| 5202 | gtk_font_chooser_set_font( |
| 5203 | GTK_FONT_CHOOSER(dialog), (const gchar *)oldname); |
| 5204 | #else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5205 | gtk_font_selection_dialog_set_font_name( |
| 5206 | GTK_FONT_SELECTION_DIALOG(dialog), (const char *)oldname); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 5207 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5208 | |
| 5209 | if (oldname != oldval) |
Bram Moolenaar | 8c71145 | 2005-01-14 21:53:12 +0000 | [diff] [blame] | 5210 | vim_free(oldname); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5211 | } |
Bram Moolenaar | bef9d83 | 2009-09-11 13:46:41 +0000 | [diff] [blame] | 5212 | else |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 5213 | #if GTK_CHECK_VERSION(3,2,0) |
| 5214 | gtk_font_chooser_set_font( |
| 5215 | GTK_FONT_CHOOSER(dialog), DEFAULT_FONT); |
| 5216 | #else |
Bram Moolenaar | bef9d83 | 2009-09-11 13:46:41 +0000 | [diff] [blame] | 5217 | gtk_font_selection_dialog_set_font_name( |
| 5218 | GTK_FONT_SELECTION_DIALOG(dialog), DEFAULT_FONT); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 5219 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5220 | |
| 5221 | response = gtk_dialog_run(GTK_DIALOG(dialog)); |
| 5222 | |
| 5223 | if (response == GTK_RESPONSE_OK) |
| 5224 | { |
| 5225 | char *name; |
| 5226 | |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 5227 | #if GTK_CHECK_VERSION(3,2,0) |
| 5228 | name = gtk_font_chooser_get_font(GTK_FONT_CHOOSER(dialog)); |
| 5229 | #else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5230 | name = gtk_font_selection_dialog_get_font_name( |
| 5231 | GTK_FONT_SELECTION_DIALOG(dialog)); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 5232 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5233 | if (name != NULL) |
| 5234 | { |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 5235 | char_u *p; |
| 5236 | |
| 5237 | /* Apparently some font names include a comma, need to escape |
| 5238 | * that, because in 'guifont' it separates names. */ |
| 5239 | p = vim_strsave_escaped((char_u *)name, (char_u *)","); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5240 | g_free(name); |
Bram Moolenaar | eb3593b | 2006-04-22 22:33:57 +0000 | [diff] [blame] | 5241 | if (p != NULL && input_conv.vc_type != CONV_NONE) |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 5242 | { |
| 5243 | fontname = string_convert(&input_conv, p, NULL); |
| 5244 | vim_free(p); |
| 5245 | } |
| 5246 | else |
| 5247 | fontname = p; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5248 | } |
| 5249 | } |
| 5250 | |
| 5251 | if (response != GTK_RESPONSE_NONE) |
| 5252 | gtk_widget_destroy(dialog); |
| 5253 | |
| 5254 | return fontname; |
| 5255 | } |
| 5256 | |
| 5257 | /* |
| 5258 | * Some monospace fonts don't support a bold weight, and fall back |
| 5259 | * silently to the regular weight. But this is no good since our text |
| 5260 | * drawing function can emulate bold by overstriking. So let's try |
| 5261 | * to detect whether bold weight is actually available and emulate it |
| 5262 | * otherwise. |
| 5263 | * |
| 5264 | * Note that we don't need to check for italic style since Xft can |
| 5265 | * emulate italic on its own, provided you have a proper fontconfig |
| 5266 | * setup. We wouldn't be able to emulate it in Vim anyway. |
| 5267 | */ |
| 5268 | static void |
| 5269 | get_styled_font_variants(void) |
| 5270 | { |
| 5271 | PangoFontDescription *bold_font_desc; |
| 5272 | PangoFont *plain_font; |
| 5273 | PangoFont *bold_font; |
| 5274 | |
| 5275 | gui.font_can_bold = FALSE; |
| 5276 | |
| 5277 | plain_font = pango_context_load_font(gui.text_context, gui.norm_font); |
| 5278 | |
| 5279 | if (plain_font == NULL) |
| 5280 | return; |
| 5281 | |
| 5282 | bold_font_desc = pango_font_description_copy_static(gui.norm_font); |
| 5283 | pango_font_description_set_weight(bold_font_desc, PANGO_WEIGHT_BOLD); |
| 5284 | |
| 5285 | bold_font = pango_context_load_font(gui.text_context, bold_font_desc); |
| 5286 | /* |
| 5287 | * The comparison relies on the unique handle nature of a PangoFont*, |
| 5288 | * i.e. it's assumed that a different PangoFont* won't refer to the |
| 5289 | * same font. Seems to work, and failing here isn't critical anyway. |
| 5290 | */ |
| 5291 | if (bold_font != NULL) |
| 5292 | { |
| 5293 | gui.font_can_bold = (bold_font != plain_font); |
| 5294 | g_object_unref(bold_font); |
| 5295 | } |
| 5296 | |
| 5297 | pango_font_description_free(bold_font_desc); |
| 5298 | g_object_unref(plain_font); |
| 5299 | } |
| 5300 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5301 | static PangoEngineShape *default_shape_engine = NULL; |
| 5302 | |
| 5303 | /* |
| 5304 | * Create a map from ASCII characters in the range [32,126] to glyphs |
| 5305 | * of the current font. This is used by gui_gtk2_draw_string() to skip |
| 5306 | * the itemize and shaping process for the most common case. |
| 5307 | */ |
| 5308 | static void |
| 5309 | ascii_glyph_table_init(void) |
| 5310 | { |
Bram Moolenaar | 16350cb | 2016-08-14 20:27:34 +0200 | [diff] [blame] | 5311 | char_u ascii_chars[2 * 128]; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5312 | PangoAttrList *attr_list; |
| 5313 | GList *item_list; |
| 5314 | int i; |
| 5315 | |
| 5316 | if (gui.ascii_glyphs != NULL) |
| 5317 | pango_glyph_string_free(gui.ascii_glyphs); |
| 5318 | if (gui.ascii_font != NULL) |
| 5319 | g_object_unref(gui.ascii_font); |
| 5320 | |
| 5321 | gui.ascii_glyphs = NULL; |
| 5322 | gui.ascii_font = NULL; |
| 5323 | |
Bram Moolenaar | 16350cb | 2016-08-14 20:27:34 +0200 | [diff] [blame] | 5324 | /* For safety, fill in question marks for the control characters. |
| 5325 | * Put a space between characters to avoid shaping. */ |
| 5326 | for (i = 0; i < 128; ++i) |
| 5327 | { |
| 5328 | if (i >= 32 && i < 127) |
| 5329 | ascii_chars[2 * i] = i; |
| 5330 | else |
| 5331 | ascii_chars[2 * i] = '?'; |
| 5332 | ascii_chars[2 * i + 1] = ' '; |
| 5333 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5334 | |
| 5335 | attr_list = pango_attr_list_new(); |
| 5336 | item_list = pango_itemize(gui.text_context, (const char *)ascii_chars, |
| 5337 | 0, sizeof(ascii_chars), attr_list, NULL); |
| 5338 | |
| 5339 | if (item_list != NULL && item_list->next == NULL) /* play safe */ |
| 5340 | { |
| 5341 | PangoItem *item; |
| 5342 | int width; |
| 5343 | |
| 5344 | item = (PangoItem *)item_list->data; |
| 5345 | width = gui.char_width * PANGO_SCALE; |
| 5346 | |
| 5347 | /* Remember the shape engine used for ASCII. */ |
| 5348 | default_shape_engine = item->analysis.shape_engine; |
| 5349 | |
| 5350 | gui.ascii_font = item->analysis.font; |
| 5351 | g_object_ref(gui.ascii_font); |
| 5352 | |
| 5353 | gui.ascii_glyphs = pango_glyph_string_new(); |
| 5354 | |
| 5355 | pango_shape((const char *)ascii_chars, sizeof(ascii_chars), |
| 5356 | &item->analysis, gui.ascii_glyphs); |
| 5357 | |
| 5358 | g_return_if_fail(gui.ascii_glyphs->num_glyphs == sizeof(ascii_chars)); |
| 5359 | |
| 5360 | for (i = 0; i < gui.ascii_glyphs->num_glyphs; ++i) |
| 5361 | { |
| 5362 | PangoGlyphGeometry *geom; |
| 5363 | |
| 5364 | geom = &gui.ascii_glyphs->glyphs[i].geometry; |
| 5365 | geom->x_offset += MAX(0, width - geom->width) / 2; |
| 5366 | geom->width = width; |
| 5367 | } |
| 5368 | } |
| 5369 | |
| 5370 | g_list_foreach(item_list, (GFunc)&pango_item_free, NULL); |
| 5371 | g_list_free(item_list); |
| 5372 | pango_attr_list_unref(attr_list); |
| 5373 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5374 | |
| 5375 | /* |
| 5376 | * Initialize Vim to use the font or fontset with the given name. |
| 5377 | * Return FAIL if the font could not be loaded, OK otherwise. |
| 5378 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5379 | int |
Bram Moolenaar | b85cb21 | 2009-05-17 14:24:23 +0000 | [diff] [blame] | 5380 | gui_mch_init_font(char_u *font_name, int fontset UNUSED) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5381 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5382 | PangoFontDescription *font_desc; |
| 5383 | PangoLayout *layout; |
| 5384 | int width; |
| 5385 | |
| 5386 | /* If font_name is NULL, this means to use the default, which should |
| 5387 | * be present on all proper Pango/fontconfig installations. */ |
| 5388 | if (font_name == NULL) |
| 5389 | font_name = (char_u *)DEFAULT_FONT; |
| 5390 | |
| 5391 | font_desc = gui_mch_get_font(font_name, FALSE); |
| 5392 | |
| 5393 | if (font_desc == NULL) |
| 5394 | return FAIL; |
| 5395 | |
| 5396 | gui_mch_free_font(gui.norm_font); |
| 5397 | gui.norm_font = font_desc; |
| 5398 | |
| 5399 | pango_context_set_font_description(gui.text_context, font_desc); |
| 5400 | |
| 5401 | layout = pango_layout_new(gui.text_context); |
| 5402 | pango_layout_set_text(layout, "MW", 2); |
| 5403 | pango_layout_get_size(layout, &width, NULL); |
| 5404 | /* |
| 5405 | * Set char_width to half the width obtained from pango_layout_get_size() |
| 5406 | * for CJK fixed_width/bi-width fonts. An unpatched version of Xft leads |
| 5407 | * Pango to use the same width for both non-CJK characters (e.g. Latin |
| 5408 | * letters and numbers) and CJK characters. This results in 's p a c e d |
| 5409 | * o u t' rendering when a CJK 'fixed width' font is used. To work around |
| 5410 | * that, divide the width returned by Pango by 2 if cjk_width is equal to |
| 5411 | * width for CJK fonts. |
| 5412 | * |
| 5413 | * For related bugs, see: |
| 5414 | * http://bugzilla.gnome.org/show_bug.cgi?id=106618 |
| 5415 | * http://bugzilla.gnome.org/show_bug.cgi?id=106624 |
| 5416 | * |
| 5417 | * With this, for all four of the following cases, Vim works fine: |
| 5418 | * guifont=CJK_fixed_width_font |
| 5419 | * guifont=Non_CJK_fixed_font |
| 5420 | * guifont=Non_CJK_fixed_font,CJK_Fixed_font |
| 5421 | * guifont=Non_CJK_fixed_font guifontwide=CJK_fixed_font |
| 5422 | */ |
| 5423 | if (is_cjk_font(gui.norm_font)) |
| 5424 | { |
| 5425 | int cjk_width; |
| 5426 | |
| 5427 | /* Measure the text extent of U+4E00 and U+4E8C */ |
| 5428 | pango_layout_set_text(layout, "\344\270\200\344\272\214", -1); |
| 5429 | pango_layout_get_size(layout, &cjk_width, NULL); |
| 5430 | |
| 5431 | if (width == cjk_width) /* Xft not patched */ |
| 5432 | width /= 2; |
| 5433 | } |
| 5434 | g_object_unref(layout); |
| 5435 | |
| 5436 | gui.char_width = (width / 2 + PANGO_SCALE - 1) / PANGO_SCALE; |
| 5437 | |
| 5438 | /* A zero width may cause a crash. Happens for semi-invalid fontsets. */ |
| 5439 | if (gui.char_width <= 0) |
| 5440 | gui.char_width = 8; |
| 5441 | |
Bram Moolenaar | 231334e | 2005-07-25 20:46:57 +0000 | [diff] [blame] | 5442 | gui_mch_adjust_charheight(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5443 | |
| 5444 | /* Set the fontname, which will be used for information purposes */ |
| 5445 | hl_set_font_name(font_name); |
| 5446 | |
| 5447 | get_styled_font_variants(); |
| 5448 | ascii_glyph_table_init(); |
| 5449 | |
| 5450 | /* Avoid unnecessary overhead if 'guifontwide' is equal to 'guifont'. */ |
| 5451 | if (gui.wide_font != NULL |
| 5452 | && pango_font_description_equal(gui.norm_font, gui.wide_font)) |
| 5453 | { |
| 5454 | pango_font_description_free(gui.wide_font); |
| 5455 | gui.wide_font = NULL; |
| 5456 | } |
| 5457 | |
Bram Moolenaar | e161c79 | 2009-11-03 17:13:59 +0000 | [diff] [blame] | 5458 | if (gui_mch_maximized()) |
| 5459 | { |
Bram Moolenaar | e161c79 | 2009-11-03 17:13:59 +0000 | [diff] [blame] | 5460 | /* Update lines and columns in accordance with the new font, keep the |
| 5461 | * window maximized. */ |
Bram Moolenaar | 12bc1b5 | 2011-08-10 17:44:45 +0200 | [diff] [blame] | 5462 | gui_mch_newfont(); |
Bram Moolenaar | e161c79 | 2009-11-03 17:13:59 +0000 | [diff] [blame] | 5463 | } |
| 5464 | else |
Bram Moolenaar | e161c79 | 2009-11-03 17:13:59 +0000 | [diff] [blame] | 5465 | { |
| 5466 | /* Preserve the logical dimensions of the screen. */ |
| 5467 | update_window_manager_hints(0, 0); |
| 5468 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5469 | |
| 5470 | return OK; |
| 5471 | } |
| 5472 | |
| 5473 | /* |
| 5474 | * Get a reference to the font "name". |
| 5475 | * Return zero for failure. |
| 5476 | */ |
| 5477 | GuiFont |
| 5478 | gui_mch_get_font(char_u *name, int report_error) |
| 5479 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5480 | PangoFontDescription *font; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5481 | |
| 5482 | /* can't do this when GUI is not running */ |
| 5483 | if (!gui.in_use || name == NULL) |
| 5484 | return NULL; |
| 5485 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5486 | if (output_conv.vc_type != CONV_NONE) |
| 5487 | { |
| 5488 | char_u *buf; |
| 5489 | |
| 5490 | buf = string_convert(&output_conv, name, NULL); |
| 5491 | if (buf != NULL) |
| 5492 | { |
| 5493 | font = pango_font_description_from_string((const char *)buf); |
| 5494 | vim_free(buf); |
| 5495 | } |
| 5496 | else |
| 5497 | font = NULL; |
| 5498 | } |
| 5499 | else |
| 5500 | font = pango_font_description_from_string((const char *)name); |
| 5501 | |
| 5502 | if (font != NULL) |
| 5503 | { |
| 5504 | PangoFont *real_font; |
| 5505 | |
| 5506 | /* pango_context_load_font() bails out if no font size is set */ |
| 5507 | if (pango_font_description_get_size(font) <= 0) |
| 5508 | pango_font_description_set_size(font, 10 * PANGO_SCALE); |
| 5509 | |
| 5510 | real_font = pango_context_load_font(gui.text_context, font); |
| 5511 | |
| 5512 | if (real_font == NULL) |
| 5513 | { |
| 5514 | pango_font_description_free(font); |
| 5515 | font = NULL; |
| 5516 | } |
| 5517 | else |
| 5518 | g_object_unref(real_font); |
| 5519 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5520 | |
| 5521 | if (font == NULL) |
| 5522 | { |
| 5523 | if (report_error) |
Bram Moolenaar | c93e791 | 2008-07-08 10:46:08 +0000 | [diff] [blame] | 5524 | EMSG2(_((char *)e_font), name); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5525 | return NULL; |
| 5526 | } |
| 5527 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5528 | return font; |
| 5529 | } |
| 5530 | |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 5531 | #if defined(FEAT_EVAL) || defined(PROTO) |
Bram Moolenaar | 46c9c73 | 2004-12-12 11:37:09 +0000 | [diff] [blame] | 5532 | /* |
| 5533 | * Return the name of font "font" in allocated memory. |
| 5534 | */ |
Bram Moolenaar | 46c9c73 | 2004-12-12 11:37:09 +0000 | [diff] [blame] | 5535 | char_u * |
Bram Moolenaar | b85cb21 | 2009-05-17 14:24:23 +0000 | [diff] [blame] | 5536 | gui_mch_get_fontname(GuiFont font, char_u *name UNUSED) |
Bram Moolenaar | 46c9c73 | 2004-12-12 11:37:09 +0000 | [diff] [blame] | 5537 | { |
Bram Moolenaar | 46c9c73 | 2004-12-12 11:37:09 +0000 | [diff] [blame] | 5538 | if (font != NOFONT) |
| 5539 | { |
Bram Moolenaar | 89d4032 | 2006-08-29 15:30:07 +0000 | [diff] [blame] | 5540 | char *pangoname = pango_font_description_to_string(font); |
Bram Moolenaar | 46c9c73 | 2004-12-12 11:37:09 +0000 | [diff] [blame] | 5541 | |
Bram Moolenaar | 89d4032 | 2006-08-29 15:30:07 +0000 | [diff] [blame] | 5542 | if (pangoname != NULL) |
Bram Moolenaar | 46c9c73 | 2004-12-12 11:37:09 +0000 | [diff] [blame] | 5543 | { |
Bram Moolenaar | 89d4032 | 2006-08-29 15:30:07 +0000 | [diff] [blame] | 5544 | char_u *s = vim_strsave((char_u *)pangoname); |
Bram Moolenaar | 46c9c73 | 2004-12-12 11:37:09 +0000 | [diff] [blame] | 5545 | |
Bram Moolenaar | 89d4032 | 2006-08-29 15:30:07 +0000 | [diff] [blame] | 5546 | g_free(pangoname); |
Bram Moolenaar | 46c9c73 | 2004-12-12 11:37:09 +0000 | [diff] [blame] | 5547 | return s; |
| 5548 | } |
| 5549 | } |
Bram Moolenaar | 46c9c73 | 2004-12-12 11:37:09 +0000 | [diff] [blame] | 5550 | return NULL; |
| 5551 | } |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 5552 | #endif |
Bram Moolenaar | 46c9c73 | 2004-12-12 11:37:09 +0000 | [diff] [blame] | 5553 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5554 | /* |
| 5555 | * If a font is not going to be used, free its structure. |
| 5556 | */ |
| 5557 | void |
| 5558 | gui_mch_free_font(GuiFont font) |
| 5559 | { |
| 5560 | if (font != NOFONT) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5561 | pango_font_description_free(font); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5562 | } |
| 5563 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5564 | /* |
Bram Moolenaar | 36edf06 | 2016-07-21 22:10:12 +0200 | [diff] [blame] | 5565 | * Return the Pixel value (color) for the given color name. |
| 5566 | * |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5567 | * Return INVALCOLOR for error. |
| 5568 | */ |
| 5569 | guicolor_T |
| 5570 | gui_mch_get_color(char_u *name) |
| 5571 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5572 | if (!gui.in_use) /* can't do this when GUI not running */ |
| 5573 | return INVALCOLOR; |
| 5574 | |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 5575 | #if GTK_CHECK_VERSION(3,0,0) |
Bram Moolenaar | 36edf06 | 2016-07-21 22:10:12 +0200 | [diff] [blame] | 5576 | return name != NULL ? gui_get_color_cmn(name) : INVALCOLOR; |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 5577 | #else |
Bram Moolenaar | 36edf06 | 2016-07-21 22:10:12 +0200 | [diff] [blame] | 5578 | guicolor_T color; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5579 | |
Bram Moolenaar | 36edf06 | 2016-07-21 22:10:12 +0200 | [diff] [blame] | 5580 | color = (name != NULL) ? gui_get_color_cmn(name) : INVALCOLOR; |
| 5581 | if (color == INVALCOLOR) |
| 5582 | return INVALCOLOR; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5583 | |
Bram Moolenaar | 26af85d | 2017-07-23 16:45:10 +0200 | [diff] [blame] | 5584 | return gui_mch_get_rgb_color( |
| 5585 | (color & 0xff0000) >> 16, |
| 5586 | (color & 0xff00) >> 8, |
| 5587 | color & 0xff); |
| 5588 | #endif |
| 5589 | } |
| 5590 | |
| 5591 | /* |
| 5592 | * Return the Pixel value (color) for the given RGB values. |
| 5593 | * Return INVALCOLOR for error. |
| 5594 | */ |
| 5595 | guicolor_T |
| 5596 | gui_mch_get_rgb_color(int r, int g, int b) |
| 5597 | { |
| 5598 | #if GTK_CHECK_VERSION(3,0,0) |
| 5599 | return gui_get_rgb_color_cmn(r, g, b); |
| 5600 | #else |
| 5601 | GdkColor gcolor; |
| 5602 | int ret; |
| 5603 | |
| 5604 | gcolor.red = (guint16)(r / 255.0 * 65535 + 0.5); |
| 5605 | gcolor.green = (guint16)(g / 255.0 * 65535 + 0.5); |
| 5606 | gcolor.blue = (guint16)(b / 255.0 * 65535 + 0.5); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5607 | |
Bram Moolenaar | 36edf06 | 2016-07-21 22:10:12 +0200 | [diff] [blame] | 5608 | ret = gdk_colormap_alloc_color(gtk_widget_get_colormap(gui.drawarea), |
| 5609 | &gcolor, FALSE, TRUE); |
| 5610 | |
| 5611 | return ret != 0 ? (guicolor_T)gcolor.pixel : INVALCOLOR; |
| 5612 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5613 | } |
| 5614 | |
| 5615 | /* |
| 5616 | * Set the current text foreground color. |
| 5617 | */ |
| 5618 | void |
| 5619 | gui_mch_set_fg_color(guicolor_T color) |
| 5620 | { |
Bram Moolenaar | 36edf06 | 2016-07-21 22:10:12 +0200 | [diff] [blame] | 5621 | #if GTK_CHECK_VERSION(3,0,0) |
| 5622 | *gui.fgcolor = color_to_rgba(color); |
| 5623 | #else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5624 | gui.fgcolor->pixel = (unsigned long)color; |
Bram Moolenaar | 36edf06 | 2016-07-21 22:10:12 +0200 | [diff] [blame] | 5625 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5626 | } |
| 5627 | |
| 5628 | /* |
| 5629 | * Set the current text background color. |
| 5630 | */ |
| 5631 | void |
| 5632 | gui_mch_set_bg_color(guicolor_T color) |
| 5633 | { |
Bram Moolenaar | 36edf06 | 2016-07-21 22:10:12 +0200 | [diff] [blame] | 5634 | #if GTK_CHECK_VERSION(3,0,0) |
| 5635 | *gui.bgcolor = color_to_rgba(color); |
| 5636 | #else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5637 | gui.bgcolor->pixel = (unsigned long)color; |
Bram Moolenaar | 36edf06 | 2016-07-21 22:10:12 +0200 | [diff] [blame] | 5638 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5639 | } |
| 5640 | |
Bram Moolenaar | f36d369 | 2005-03-15 22:48:14 +0000 | [diff] [blame] | 5641 | /* |
| 5642 | * Set the current text special color. |
| 5643 | */ |
| 5644 | void |
| 5645 | gui_mch_set_sp_color(guicolor_T color) |
| 5646 | { |
Bram Moolenaar | 36edf06 | 2016-07-21 22:10:12 +0200 | [diff] [blame] | 5647 | #if GTK_CHECK_VERSION(3,0,0) |
| 5648 | *gui.spcolor = color_to_rgba(color); |
| 5649 | #else |
Bram Moolenaar | f36d369 | 2005-03-15 22:48:14 +0000 | [diff] [blame] | 5650 | gui.spcolor->pixel = (unsigned long)color; |
Bram Moolenaar | 36edf06 | 2016-07-21 22:10:12 +0200 | [diff] [blame] | 5651 | #endif |
Bram Moolenaar | f36d369 | 2005-03-15 22:48:14 +0000 | [diff] [blame] | 5652 | } |
| 5653 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5654 | /* |
| 5655 | * Function-like convenience macro for the sake of efficiency. |
| 5656 | */ |
| 5657 | #define INSERT_PANGO_ATTR(Attribute, AttrList, Start, End) \ |
| 5658 | G_STMT_START{ \ |
| 5659 | PangoAttribute *tmp_attr_; \ |
| 5660 | tmp_attr_ = (Attribute); \ |
| 5661 | tmp_attr_->start_index = (Start); \ |
| 5662 | tmp_attr_->end_index = (End); \ |
| 5663 | pango_attr_list_insert((AttrList), tmp_attr_); \ |
| 5664 | }G_STMT_END |
| 5665 | |
| 5666 | static void |
| 5667 | apply_wide_font_attr(char_u *s, int len, PangoAttrList *attr_list) |
| 5668 | { |
| 5669 | char_u *start = NULL; |
| 5670 | char_u *p; |
| 5671 | int uc; |
| 5672 | |
| 5673 | for (p = s; p < s + len; p += utf_byte2len(*p)) |
| 5674 | { |
| 5675 | uc = utf_ptr2char(p); |
| 5676 | |
| 5677 | if (start == NULL) |
| 5678 | { |
| 5679 | if (uc >= 0x80 && utf_char2cells(uc) == 2) |
| 5680 | start = p; |
| 5681 | } |
| 5682 | else if (uc < 0x80 /* optimization shortcut */ |
| 5683 | || (utf_char2cells(uc) != 2 && !utf_iscomposing(uc))) |
| 5684 | { |
| 5685 | INSERT_PANGO_ATTR(pango_attr_font_desc_new(gui.wide_font), |
| 5686 | attr_list, start - s, p - s); |
| 5687 | start = NULL; |
| 5688 | } |
| 5689 | } |
| 5690 | |
| 5691 | if (start != NULL) |
| 5692 | INSERT_PANGO_ATTR(pango_attr_font_desc_new(gui.wide_font), |
| 5693 | attr_list, start - s, len); |
| 5694 | } |
| 5695 | |
| 5696 | static int |
| 5697 | count_cluster_cells(char_u *s, PangoItem *item, |
| 5698 | PangoGlyphString* glyphs, int i, |
| 5699 | int *cluster_width, |
| 5700 | int *last_glyph_rbearing) |
| 5701 | { |
| 5702 | char_u *p; |
| 5703 | int next; /* glyph start index of next cluster */ |
| 5704 | int start, end; /* string segment of current cluster */ |
| 5705 | int width; /* real cluster width in Pango units */ |
| 5706 | int uc; |
| 5707 | int cellcount = 0; |
| 5708 | |
| 5709 | width = glyphs->glyphs[i].geometry.width; |
| 5710 | |
| 5711 | for (next = i + 1; next < glyphs->num_glyphs; ++next) |
| 5712 | { |
| 5713 | if (glyphs->glyphs[next].attr.is_cluster_start) |
| 5714 | break; |
| 5715 | else if (glyphs->glyphs[next].geometry.width > width) |
| 5716 | width = glyphs->glyphs[next].geometry.width; |
| 5717 | } |
| 5718 | |
| 5719 | start = item->offset + glyphs->log_clusters[i]; |
| 5720 | end = item->offset + ((next < glyphs->num_glyphs) ? |
| 5721 | glyphs->log_clusters[next] : item->length); |
| 5722 | |
| 5723 | for (p = s + start; p < s + end; p += utf_byte2len(*p)) |
| 5724 | { |
| 5725 | uc = utf_ptr2char(p); |
| 5726 | if (uc < 0x80) |
| 5727 | ++cellcount; |
| 5728 | else if (!utf_iscomposing(uc)) |
| 5729 | cellcount += utf_char2cells(uc); |
| 5730 | } |
| 5731 | |
| 5732 | if (last_glyph_rbearing != NULL |
| 5733 | && cellcount > 0 && next == glyphs->num_glyphs) |
| 5734 | { |
| 5735 | PangoRectangle ink_rect; |
| 5736 | /* |
| 5737 | * If a certain combining mark had to be taken from a non-monospace |
| 5738 | * font, we have to compensate manually by adapting x_offset according |
| 5739 | * to the ink extents of the previous glyph. |
| 5740 | */ |
| 5741 | pango_font_get_glyph_extents(item->analysis.font, |
| 5742 | glyphs->glyphs[i].glyph, |
| 5743 | &ink_rect, NULL); |
| 5744 | |
| 5745 | if (PANGO_RBEARING(ink_rect) > 0) |
| 5746 | *last_glyph_rbearing = PANGO_RBEARING(ink_rect); |
| 5747 | } |
| 5748 | |
| 5749 | if (cellcount > 0) |
| 5750 | *cluster_width = width; |
| 5751 | |
| 5752 | return cellcount; |
| 5753 | } |
| 5754 | |
| 5755 | /* |
| 5756 | * If there are only combining characters in the cluster, we cannot just |
| 5757 | * change the width of the previous glyph since there is none. Therefore |
| 5758 | * some guesswork is needed. |
| 5759 | * |
| 5760 | * If ink_rect.x is negative Pango apparently has taken care of the composing |
| 5761 | * by itself. Actually setting x_offset = 0 should be sufficient then, but due |
| 5762 | * 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] | 5763 | * x_offset to avoid ugliness. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5764 | * |
| 5765 | * If ink_rect.x is not negative, force overstriking by pointing x_offset to |
| 5766 | * the position of the previous glyph. Apparently this happens only with old |
| 5767 | * X fonts which don't provide the special combining information needed by |
| 5768 | * Pango. |
| 5769 | */ |
| 5770 | static void |
| 5771 | setup_zero_width_cluster(PangoItem *item, PangoGlyphInfo *glyph, |
| 5772 | int last_cellcount, int last_cluster_width, |
| 5773 | int last_glyph_rbearing) |
| 5774 | { |
| 5775 | PangoRectangle ink_rect; |
| 5776 | PangoRectangle logical_rect; |
| 5777 | int width; |
| 5778 | |
| 5779 | width = last_cellcount * gui.char_width * PANGO_SCALE; |
| 5780 | glyph->geometry.x_offset = -width + MAX(0, width - last_cluster_width) / 2; |
| 5781 | glyph->geometry.width = 0; |
| 5782 | |
| 5783 | pango_font_get_glyph_extents(item->analysis.font, |
| 5784 | glyph->glyph, |
| 5785 | &ink_rect, &logical_rect); |
| 5786 | if (ink_rect.x < 0) |
| 5787 | { |
| 5788 | glyph->geometry.x_offset += last_glyph_rbearing; |
| 5789 | glyph->geometry.y_offset = logical_rect.height |
| 5790 | - (gui.char_height - p_linespace) * PANGO_SCALE; |
| 5791 | } |
Bram Moolenaar | 706e84b | 2010-08-07 15:46:45 +0200 | [diff] [blame] | 5792 | else |
| 5793 | /* If the accent width is smaller than the cluster width, position it |
| 5794 | * in the middle. */ |
| 5795 | glyph->geometry.x_offset = -width + MAX(0, width - ink_rect.width) / 2; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5796 | } |
| 5797 | |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 5798 | #if GTK_CHECK_VERSION(3,0,0) |
| 5799 | static void |
| 5800 | draw_glyph_string(int row, int col, int num_cells, int flags, |
| 5801 | PangoFont *font, PangoGlyphString *glyphs, |
| 5802 | cairo_t *cr) |
| 5803 | #else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5804 | static void |
| 5805 | draw_glyph_string(int row, int col, int num_cells, int flags, |
| 5806 | PangoFont *font, PangoGlyphString *glyphs) |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 5807 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5808 | { |
| 5809 | if (!(flags & DRAW_TRANSP)) |
| 5810 | { |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 5811 | #if GTK_CHECK_VERSION(3,0,0) |
Bram Moolenaar | 36edf06 | 2016-07-21 22:10:12 +0200 | [diff] [blame] | 5812 | cairo_set_source_rgba(cr, |
| 5813 | gui.bgcolor->red, gui.bgcolor->green, gui.bgcolor->blue, |
| 5814 | gui.bgcolor->alpha); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 5815 | cairo_rectangle(cr, |
| 5816 | FILL_X(col), FILL_Y(row), |
| 5817 | num_cells * gui.char_width, gui.char_height); |
| 5818 | cairo_fill(cr); |
| 5819 | #else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5820 | gdk_gc_set_foreground(gui.text_gc, gui.bgcolor); |
| 5821 | |
| 5822 | gdk_draw_rectangle(gui.drawarea->window, |
| 5823 | gui.text_gc, |
| 5824 | TRUE, |
| 5825 | FILL_X(col), |
| 5826 | FILL_Y(row), |
| 5827 | num_cells * gui.char_width, |
| 5828 | gui.char_height); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 5829 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5830 | } |
| 5831 | |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 5832 | #if GTK_CHECK_VERSION(3,0,0) |
Bram Moolenaar | 36edf06 | 2016-07-21 22:10:12 +0200 | [diff] [blame] | 5833 | cairo_set_source_rgba(cr, |
| 5834 | gui.fgcolor->red, gui.fgcolor->green, gui.fgcolor->blue, |
| 5835 | gui.fgcolor->alpha); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 5836 | cairo_move_to(cr, TEXT_X(col), TEXT_Y(row)); |
| 5837 | pango_cairo_show_glyph_string(cr, font, glyphs); |
| 5838 | #else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5839 | gdk_gc_set_foreground(gui.text_gc, gui.fgcolor); |
| 5840 | |
| 5841 | gdk_draw_glyphs(gui.drawarea->window, |
| 5842 | gui.text_gc, |
| 5843 | font, |
| 5844 | TEXT_X(col), |
| 5845 | TEXT_Y(row), |
| 5846 | glyphs); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 5847 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5848 | |
| 5849 | /* redraw the contents with an offset of 1 to emulate bold */ |
| 5850 | if ((flags & DRAW_BOLD) && !gui.font_can_bold) |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 5851 | #if GTK_CHECK_VERSION(3,0,0) |
| 5852 | { |
Bram Moolenaar | 36edf06 | 2016-07-21 22:10:12 +0200 | [diff] [blame] | 5853 | cairo_set_source_rgba(cr, |
| 5854 | gui.fgcolor->red, gui.fgcolor->green, gui.fgcolor->blue, |
| 5855 | gui.fgcolor->alpha); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 5856 | cairo_move_to(cr, TEXT_X(col) + 1, TEXT_Y(row)); |
| 5857 | pango_cairo_show_glyph_string(cr, font, glyphs); |
| 5858 | } |
| 5859 | #else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5860 | gdk_draw_glyphs(gui.drawarea->window, |
| 5861 | gui.text_gc, |
| 5862 | font, |
| 5863 | TEXT_X(col) + 1, |
| 5864 | TEXT_Y(row), |
| 5865 | glyphs); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 5866 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5867 | } |
| 5868 | |
Bram Moolenaar | f36d369 | 2005-03-15 22:48:14 +0000 | [diff] [blame] | 5869 | /* |
| 5870 | * Draw underline and undercurl at the bottom of the character cell. |
| 5871 | */ |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 5872 | #if GTK_CHECK_VERSION(3,0,0) |
| 5873 | static void |
| 5874 | draw_under(int flags, int row, int col, int cells, cairo_t *cr) |
| 5875 | #else |
Bram Moolenaar | f36d369 | 2005-03-15 22:48:14 +0000 | [diff] [blame] | 5876 | static void |
| 5877 | draw_under(int flags, int row, int col, int cells) |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 5878 | #endif |
Bram Moolenaar | f36d369 | 2005-03-15 22:48:14 +0000 | [diff] [blame] | 5879 | { |
| 5880 | int i; |
| 5881 | int offset; |
Bram Moolenaar | b85cb21 | 2009-05-17 14:24:23 +0000 | [diff] [blame] | 5882 | 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] | 5883 | int y = FILL_Y(row + 1) - 1; |
| 5884 | |
| 5885 | /* Undercurl: draw curl at the bottom of the character cell. */ |
| 5886 | if (flags & DRAW_UNDERC) |
| 5887 | { |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 5888 | #if GTK_CHECK_VERSION(3,0,0) |
| 5889 | cairo_set_line_width(cr, 1.0); |
| 5890 | cairo_set_line_cap(cr, CAIRO_LINE_CAP_BUTT); |
Bram Moolenaar | 36edf06 | 2016-07-21 22:10:12 +0200 | [diff] [blame] | 5891 | cairo_set_source_rgba(cr, |
| 5892 | gui.spcolor->red, gui.spcolor->green, gui.spcolor->blue, |
| 5893 | gui.spcolor->alpha); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 5894 | for (i = FILL_X(col); i < FILL_X(col + cells); ++i) |
| 5895 | { |
| 5896 | offset = val[i % 8]; |
| 5897 | cairo_line_to(cr, i, y - offset + 0.5); |
| 5898 | } |
| 5899 | cairo_stroke(cr); |
| 5900 | #else |
Bram Moolenaar | f36d369 | 2005-03-15 22:48:14 +0000 | [diff] [blame] | 5901 | gdk_gc_set_foreground(gui.text_gc, gui.spcolor); |
| 5902 | for (i = FILL_X(col); i < FILL_X(col + cells); ++i) |
| 5903 | { |
| 5904 | offset = val[i % 8]; |
| 5905 | gdk_draw_point(gui.drawarea->window, gui.text_gc, i, y - offset); |
| 5906 | } |
| 5907 | gdk_gc_set_foreground(gui.text_gc, gui.fgcolor); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 5908 | #endif |
Bram Moolenaar | f36d369 | 2005-03-15 22:48:14 +0000 | [diff] [blame] | 5909 | } |
| 5910 | |
Bram Moolenaar | cf4b00c | 2017-09-02 18:33:56 +0200 | [diff] [blame] | 5911 | /* Draw a strikethrough line */ |
| 5912 | if (flags & DRAW_STRIKE) |
| 5913 | { |
| 5914 | #if GTK_CHECK_VERSION(3,0,0) |
| 5915 | cairo_set_line_width(cr, 1.0); |
| 5916 | cairo_set_line_cap(cr, CAIRO_LINE_CAP_BUTT); |
| 5917 | cairo_set_source_rgba(cr, |
| 5918 | gui.spcolor->red, gui.spcolor->green, gui.spcolor->blue, |
| 5919 | gui.spcolor->alpha); |
| 5920 | cairo_move_to(cr, FILL_X(col), y + 1 - gui.char_height/2 + 0.5); |
| 5921 | cairo_line_to(cr, FILL_X(col + cells), y + 1 - gui.char_height/2 + 0.5); |
| 5922 | cairo_stroke(cr); |
| 5923 | #else |
| 5924 | gdk_gc_set_foreground(gui.text_gc, gui.spcolor); |
| 5925 | gdk_draw_line(gui.drawarea->window, gui.text_gc, |
| 5926 | FILL_X(col), y + 1 - gui.char_height/2, |
| 5927 | FILL_X(col + cells), y + 1 - gui.char_height/2); |
| 5928 | gdk_gc_set_foreground(gui.text_gc, gui.fgcolor); |
| 5929 | #endif |
| 5930 | } |
| 5931 | |
Bram Moolenaar | f36d369 | 2005-03-15 22:48:14 +0000 | [diff] [blame] | 5932 | /* Underline: draw a line at the bottom of the character cell. */ |
| 5933 | if (flags & DRAW_UNDERL) |
| 5934 | { |
| 5935 | /* When p_linespace is 0, overwrite the bottom row of pixels. |
| 5936 | * Otherwise put the line just below the character. */ |
| 5937 | if (p_linespace > 1) |
| 5938 | y -= p_linespace - 1; |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 5939 | #if GTK_CHECK_VERSION(3,0,0) |
Bram Moolenaar | cf4b00c | 2017-09-02 18:33:56 +0200 | [diff] [blame] | 5940 | cairo_set_line_width(cr, 1.0); |
| 5941 | cairo_set_line_cap(cr, CAIRO_LINE_CAP_BUTT); |
| 5942 | cairo_set_source_rgba(cr, |
| 5943 | gui.fgcolor->red, gui.fgcolor->green, gui.fgcolor->blue, |
| 5944 | gui.fgcolor->alpha); |
| 5945 | cairo_move_to(cr, FILL_X(col), y + 0.5); |
| 5946 | cairo_line_to(cr, FILL_X(col + cells), y + 0.5); |
| 5947 | cairo_stroke(cr); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 5948 | #else |
Bram Moolenaar | f36d369 | 2005-03-15 22:48:14 +0000 | [diff] [blame] | 5949 | gdk_draw_line(gui.drawarea->window, gui.text_gc, |
| 5950 | FILL_X(col), y, |
| 5951 | FILL_X(col + cells) - 1, y); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 5952 | #endif |
Bram Moolenaar | f36d369 | 2005-03-15 22:48:14 +0000 | [diff] [blame] | 5953 | } |
| 5954 | } |
| 5955 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5956 | int |
| 5957 | gui_gtk2_draw_string(int row, int col, char_u *s, int len, int flags) |
| 5958 | { |
| 5959 | GdkRectangle area; /* area for clip mask */ |
| 5960 | PangoGlyphString *glyphs; /* glyphs of current item */ |
| 5961 | int column_offset = 0; /* column offset in cells */ |
| 5962 | int i; |
| 5963 | char_u *conv_buf = NULL; /* result of UTF-8 conversion */ |
| 5964 | char_u *new_conv_buf; |
| 5965 | int convlen; |
| 5966 | char_u *sp, *bp; |
| 5967 | int plen; |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 5968 | #if GTK_CHECK_VERSION(3,0,0) |
| 5969 | cairo_t *cr; |
| 5970 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5971 | |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 5972 | #if GTK_CHECK_VERSION(3,0,0) |
| 5973 | if (gui.text_context == NULL || gtk_widget_get_window(gui.drawarea) == NULL) |
| 5974 | #else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5975 | if (gui.text_context == NULL || gui.drawarea->window == NULL) |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 5976 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5977 | return len; |
| 5978 | |
| 5979 | if (output_conv.vc_type != CONV_NONE) |
| 5980 | { |
| 5981 | /* |
| 5982 | * Convert characters from 'encoding' to 'termencoding', which is set |
| 5983 | * to UTF-8 by gui_mch_init(). did_set_string_option() in option.c |
| 5984 | * prohibits changing this to something else than UTF-8 if the GUI is |
| 5985 | * in use. |
| 5986 | */ |
| 5987 | convlen = len; |
| 5988 | conv_buf = string_convert(&output_conv, s, &convlen); |
| 5989 | g_return_val_if_fail(conv_buf != NULL, len); |
| 5990 | |
| 5991 | /* Correct for differences in char width: some chars are |
| 5992 | * double-wide in 'encoding' but single-wide in utf-8. Add a space to |
| 5993 | * compensate for that. */ |
| 5994 | for (sp = s, bp = conv_buf; sp < s + len && bp < conv_buf + convlen; ) |
| 5995 | { |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 5996 | plen = utf_ptr2len(bp); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5997 | if ((*mb_ptr2cells)(sp) == 2 && utf_ptr2cells(bp) == 1) |
| 5998 | { |
| 5999 | new_conv_buf = alloc(convlen + 2); |
| 6000 | if (new_conv_buf == NULL) |
| 6001 | return len; |
| 6002 | plen += bp - conv_buf; |
| 6003 | mch_memmove(new_conv_buf, conv_buf, plen); |
| 6004 | new_conv_buf[plen] = ' '; |
| 6005 | mch_memmove(new_conv_buf + plen + 1, conv_buf + plen, |
| 6006 | convlen - plen + 1); |
| 6007 | vim_free(conv_buf); |
| 6008 | conv_buf = new_conv_buf; |
| 6009 | ++convlen; |
| 6010 | bp = conv_buf + plen; |
| 6011 | plen = 1; |
| 6012 | } |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 6013 | sp += (*mb_ptr2len)(sp); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6014 | bp += plen; |
| 6015 | } |
| 6016 | s = conv_buf; |
| 6017 | len = convlen; |
| 6018 | } |
| 6019 | |
| 6020 | /* |
| 6021 | * Restrict all drawing to the current screen line in order to prevent |
| 6022 | * fuzzy font lookups from messing up the screen. |
| 6023 | */ |
| 6024 | area.x = gui.border_offset; |
| 6025 | area.y = FILL_Y(row); |
| 6026 | area.width = gui.num_cols * gui.char_width; |
| 6027 | area.height = gui.char_height; |
| 6028 | |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 6029 | #if GTK_CHECK_VERSION(3,0,0) |
| 6030 | cr = cairo_create(gui.surface); |
| 6031 | cairo_rectangle(cr, area.x, area.y, area.width, area.height); |
| 6032 | cairo_clip(cr); |
| 6033 | #else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6034 | gdk_gc_set_clip_origin(gui.text_gc, 0, 0); |
| 6035 | gdk_gc_set_clip_rectangle(gui.text_gc, &area); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 6036 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6037 | |
| 6038 | glyphs = pango_glyph_string_new(); |
| 6039 | |
| 6040 | /* |
| 6041 | * Optimization hack: If possible, skip the itemize and shaping process |
| 6042 | * for pure ASCII strings. This optimization is particularly effective |
| 6043 | * because Vim draws space characters to clear parts of the screen. |
| 6044 | */ |
| 6045 | if (!(flags & DRAW_ITALIC) |
| 6046 | && !((flags & DRAW_BOLD) && gui.font_can_bold) |
| 6047 | && gui.ascii_glyphs != NULL) |
| 6048 | { |
| 6049 | char_u *p; |
| 6050 | |
| 6051 | for (p = s; p < s + len; ++p) |
| 6052 | if (*p & 0x80) |
| 6053 | goto not_ascii; |
| 6054 | |
| 6055 | pango_glyph_string_set_size(glyphs, len); |
| 6056 | |
| 6057 | for (i = 0; i < len; ++i) |
| 6058 | { |
Bram Moolenaar | 16350cb | 2016-08-14 20:27:34 +0200 | [diff] [blame] | 6059 | glyphs->glyphs[i] = gui.ascii_glyphs->glyphs[2 * s[i]]; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6060 | glyphs->log_clusters[i] = i; |
| 6061 | } |
| 6062 | |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 6063 | #if GTK_CHECK_VERSION(3,0,0) |
| 6064 | draw_glyph_string(row, col, len, flags, gui.ascii_font, glyphs, cr); |
| 6065 | #else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6066 | draw_glyph_string(row, col, len, flags, gui.ascii_font, glyphs); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 6067 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6068 | |
| 6069 | column_offset = len; |
| 6070 | } |
| 6071 | else |
| 6072 | not_ascii: |
| 6073 | { |
| 6074 | PangoAttrList *attr_list; |
| 6075 | GList *item_list; |
| 6076 | int cluster_width; |
| 6077 | int last_glyph_rbearing; |
| 6078 | int cells = 0; /* cells occupied by current cluster */ |
| 6079 | |
Bram Moolenaar | 3fdfa4a | 2004-10-07 21:02:47 +0000 | [diff] [blame] | 6080 | /* Safety check: pango crashes when invoked with invalid utf-8 |
| 6081 | * characters. */ |
| 6082 | if (!utf_valid_string(s, s + len)) |
| 6083 | { |
| 6084 | column_offset = len; |
| 6085 | goto skipitall; |
| 6086 | } |
| 6087 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6088 | /* original width of the current cluster */ |
| 6089 | cluster_width = PANGO_SCALE * gui.char_width; |
| 6090 | |
| 6091 | /* right bearing of the last non-composing glyph */ |
| 6092 | last_glyph_rbearing = PANGO_SCALE * gui.char_width; |
| 6093 | |
| 6094 | attr_list = pango_attr_list_new(); |
| 6095 | |
| 6096 | /* If 'guifontwide' is set then use that for double-width characters. |
| 6097 | * Otherwise just go with 'guifont' and let Pango do its thing. */ |
| 6098 | if (gui.wide_font != NULL) |
| 6099 | apply_wide_font_attr(s, len, attr_list); |
| 6100 | |
| 6101 | if ((flags & DRAW_BOLD) && gui.font_can_bold) |
| 6102 | INSERT_PANGO_ATTR(pango_attr_weight_new(PANGO_WEIGHT_BOLD), |
| 6103 | attr_list, 0, len); |
| 6104 | if (flags & DRAW_ITALIC) |
| 6105 | INSERT_PANGO_ATTR(pango_attr_style_new(PANGO_STYLE_ITALIC), |
| 6106 | attr_list, 0, len); |
| 6107 | /* |
| 6108 | * Break the text into segments with consistent directional level |
| 6109 | * and shaping engine. Pure Latin text needs only a single segment, |
| 6110 | * so there's no need to worry about the loop's efficiency. Better |
| 6111 | * try to optimize elsewhere, e.g. reducing exposes and stuff :) |
| 6112 | */ |
| 6113 | item_list = pango_itemize(gui.text_context, |
| 6114 | (const char *)s, 0, len, attr_list, NULL); |
| 6115 | |
| 6116 | while (item_list != NULL) |
| 6117 | { |
| 6118 | PangoItem *item; |
| 6119 | int item_cells = 0; /* item length in cells */ |
| 6120 | |
| 6121 | item = (PangoItem *)item_list->data; |
| 6122 | item_list = g_list_delete_link(item_list, item_list); |
| 6123 | /* |
| 6124 | * Increment the bidirectional embedding level by 1 if it is not |
| 6125 | * even. An odd number means the output will be RTL, but we don't |
| 6126 | * want that since Vim handles right-to-left text on its own. It |
| 6127 | * would probably be sufficient to just set level = 0, but you can |
| 6128 | * never know :) |
| 6129 | * |
| 6130 | * Unfortunately we can't take advantage of Pango's ability to |
| 6131 | * render both LTR and RTL at the same time. In order to support |
| 6132 | * that, Vim's main screen engine would have to make use of Pango |
| 6133 | * functionality. |
| 6134 | */ |
| 6135 | item->analysis.level = (item->analysis.level + 1) & (~1U); |
| 6136 | |
| 6137 | /* HACK: Overrule the shape engine, we don't want shaping to be |
| 6138 | * done, because drawing the cursor would change the display. */ |
| 6139 | item->analysis.shape_engine = default_shape_engine; |
| 6140 | |
Bram Moolenaar | 3cbe0c0 | 2015-09-08 20:00:22 +0200 | [diff] [blame] | 6141 | #ifdef HAVE_PANGO_SHAPE_FULL |
Bram Moolenaar | 7e2ec00 | 2015-09-08 16:31:06 +0200 | [diff] [blame] | 6142 | pango_shape_full((const char *)s + item->offset, item->length, |
| 6143 | (const char *)s, len, &item->analysis, glyphs); |
Bram Moolenaar | 3cbe0c0 | 2015-09-08 20:00:22 +0200 | [diff] [blame] | 6144 | #else |
| 6145 | pango_shape((const char *)s + item->offset, item->length, |
| 6146 | &item->analysis, glyphs); |
| 6147 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6148 | /* |
| 6149 | * Fixed-width hack: iterate over the array and assign a fixed |
| 6150 | * width to each glyph, thus overriding the choice made by the |
| 6151 | * shaping engine. We use utf_char2cells() to determine the |
| 6152 | * number of cells needed. |
| 6153 | * |
| 6154 | * Also perform all kind of dark magic to get composing |
| 6155 | * characters right (and pretty too of course). |
| 6156 | */ |
| 6157 | for (i = 0; i < glyphs->num_glyphs; ++i) |
| 6158 | { |
| 6159 | PangoGlyphInfo *glyph; |
| 6160 | |
| 6161 | glyph = &glyphs->glyphs[i]; |
| 6162 | |
| 6163 | if (glyph->attr.is_cluster_start) |
| 6164 | { |
| 6165 | int cellcount; |
| 6166 | |
| 6167 | cellcount = count_cluster_cells( |
| 6168 | s, item, glyphs, i, &cluster_width, |
| 6169 | (item_list != NULL) ? &last_glyph_rbearing : NULL); |
| 6170 | |
| 6171 | if (cellcount > 0) |
| 6172 | { |
| 6173 | int width; |
| 6174 | |
| 6175 | width = cellcount * gui.char_width * PANGO_SCALE; |
| 6176 | glyph->geometry.x_offset += |
| 6177 | MAX(0, width - cluster_width) / 2; |
| 6178 | glyph->geometry.width = width; |
| 6179 | } |
| 6180 | else |
| 6181 | { |
| 6182 | /* If there are only combining characters in the |
| 6183 | * cluster, we cannot just change the width of the |
| 6184 | * previous glyph since there is none. Therefore |
| 6185 | * some guesswork is needed. */ |
| 6186 | setup_zero_width_cluster(item, glyph, cells, |
| 6187 | cluster_width, |
| 6188 | last_glyph_rbearing); |
| 6189 | } |
| 6190 | |
| 6191 | item_cells += cellcount; |
| 6192 | cells = cellcount; |
| 6193 | } |
| 6194 | else if (i > 0) |
| 6195 | { |
| 6196 | int width; |
| 6197 | |
| 6198 | /* There is a previous glyph, so we deal with combining |
Bram Moolenaar | 706e84b | 2010-08-07 15:46:45 +0200 | [diff] [blame] | 6199 | * characters the canonical way. |
Bram Moolenaar | 96118f3 | 2010-08-08 14:40:37 +0200 | [diff] [blame] | 6200 | * In some circumstances Pango uses a positive x_offset, |
| 6201 | * then use the width of the previous glyph for this one |
| 6202 | * and set the previous width to zero. |
| 6203 | * Otherwise we get a negative x_offset, Pango has already |
| 6204 | * positioned the combining char, keep the widths as they |
| 6205 | * are. |
Bram Moolenaar | 706e84b | 2010-08-07 15:46:45 +0200 | [diff] [blame] | 6206 | * For both adjust the x_offset to position the glyph in |
Bram Moolenaar | 96118f3 | 2010-08-08 14:40:37 +0200 | [diff] [blame] | 6207 | * the middle. */ |
Bram Moolenaar | 706e84b | 2010-08-07 15:46:45 +0200 | [diff] [blame] | 6208 | if (glyph->geometry.x_offset >= 0) |
Bram Moolenaar | 96118f3 | 2010-08-08 14:40:37 +0200 | [diff] [blame] | 6209 | { |
| 6210 | glyphs->glyphs[i].geometry.width = |
| 6211 | glyphs->glyphs[i - 1].geometry.width; |
Bram Moolenaar | 706e84b | 2010-08-07 15:46:45 +0200 | [diff] [blame] | 6212 | glyphs->glyphs[i - 1].geometry.width = 0; |
Bram Moolenaar | 96118f3 | 2010-08-08 14:40:37 +0200 | [diff] [blame] | 6213 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6214 | width = cells * gui.char_width * PANGO_SCALE; |
| 6215 | glyph->geometry.x_offset += |
| 6216 | MAX(0, width - cluster_width) / 2; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6217 | } |
| 6218 | else /* i == 0 "cannot happen" */ |
| 6219 | { |
| 6220 | glyph->geometry.width = 0; |
| 6221 | } |
| 6222 | } |
| 6223 | |
| 6224 | /*** Aaaaand action! ***/ |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 6225 | #if GTK_CHECK_VERSION(3,0,0) |
| 6226 | draw_glyph_string(row, col + column_offset, item_cells, |
| 6227 | flags, item->analysis.font, glyphs, |
| 6228 | cr); |
| 6229 | #else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6230 | draw_glyph_string(row, col + column_offset, item_cells, |
| 6231 | flags, item->analysis.font, glyphs); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 6232 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6233 | |
| 6234 | pango_item_free(item); |
| 6235 | |
| 6236 | column_offset += item_cells; |
| 6237 | } |
| 6238 | |
| 6239 | pango_attr_list_unref(attr_list); |
| 6240 | } |
| 6241 | |
Bram Moolenaar | 3fdfa4a | 2004-10-07 21:02:47 +0000 | [diff] [blame] | 6242 | skipitall: |
Bram Moolenaar | f36d369 | 2005-03-15 22:48:14 +0000 | [diff] [blame] | 6243 | /* Draw underline and undercurl. */ |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 6244 | #if GTK_CHECK_VERSION(3,0,0) |
| 6245 | draw_under(flags, row, col, column_offset, cr); |
| 6246 | #else |
Bram Moolenaar | f36d369 | 2005-03-15 22:48:14 +0000 | [diff] [blame] | 6247 | draw_under(flags, row, col, column_offset); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 6248 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6249 | |
| 6250 | pango_glyph_string_free(glyphs); |
| 6251 | vim_free(conv_buf); |
| 6252 | |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 6253 | #if GTK_CHECK_VERSION(3,0,0) |
| 6254 | cairo_destroy(cr); |
| 6255 | if (!gui.by_signal) |
| 6256 | gdk_window_invalidate_rect(gtk_widget_get_window(gui.drawarea), |
| 6257 | &area, FALSE); |
| 6258 | #else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6259 | gdk_gc_set_clip_rectangle(gui.text_gc, NULL); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 6260 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6261 | |
| 6262 | return column_offset; |
| 6263 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6264 | |
| 6265 | /* |
| 6266 | * Return OK if the key with the termcap name "name" is supported. |
| 6267 | */ |
| 6268 | int |
| 6269 | gui_mch_haskey(char_u *name) |
| 6270 | { |
| 6271 | int i; |
| 6272 | |
| 6273 | for (i = 0; special_keys[i].key_sym != 0; i++) |
| 6274 | if (name[0] == special_keys[i].code0 |
| 6275 | && name[1] == special_keys[i].code1) |
| 6276 | return OK; |
| 6277 | return FAIL; |
| 6278 | } |
| 6279 | |
Bram Moolenaar | b2c5a5a | 2013-02-14 22:11:39 +0100 | [diff] [blame] | 6280 | #if defined(FEAT_TITLE) || defined(FEAT_EVAL) || defined(PROTO) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6281 | /* |
| 6282 | * Return the text window-id and display. Only required for X-based GUI's |
| 6283 | */ |
| 6284 | int |
| 6285 | gui_get_x11_windis(Window *win, Display **dis) |
| 6286 | { |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 6287 | #if GTK_CHECK_VERSION(3,0,0) |
| 6288 | if (gui.mainwin != NULL && gtk_widget_get_window(gui.mainwin) != NULL) |
| 6289 | #else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6290 | if (gui.mainwin != NULL && gui.mainwin->window != NULL) |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 6291 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6292 | { |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 6293 | #if GTK_CHECK_VERSION(3,0,0) |
| 6294 | *dis = GDK_WINDOW_XDISPLAY(gtk_widget_get_window(gui.mainwin)); |
| 6295 | *win = GDK_WINDOW_XID(gtk_widget_get_window(gui.mainwin)); |
| 6296 | #else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6297 | *dis = GDK_WINDOW_XDISPLAY(gui.mainwin->window); |
| 6298 | *win = GDK_WINDOW_XWINDOW(gui.mainwin->window); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 6299 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6300 | return OK; |
| 6301 | } |
| 6302 | |
| 6303 | *dis = NULL; |
| 6304 | *win = 0; |
| 6305 | return FAIL; |
| 6306 | } |
| 6307 | #endif |
| 6308 | |
| 6309 | #if defined(FEAT_CLIENTSERVER) \ |
| 6310 | || (defined(FEAT_X11) && defined(FEAT_CLIPBOARD)) || defined(PROTO) |
| 6311 | |
| 6312 | Display * |
| 6313 | gui_mch_get_display(void) |
| 6314 | { |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 6315 | #if GTK_CHECK_VERSION(3,0,0) |
| 6316 | if (gui.mainwin != NULL && gtk_widget_get_window(gui.mainwin) != NULL) |
| 6317 | return GDK_WINDOW_XDISPLAY(gtk_widget_get_window(gui.mainwin)); |
| 6318 | #else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6319 | if (gui.mainwin != NULL && gui.mainwin->window != NULL) |
| 6320 | return GDK_WINDOW_XDISPLAY(gui.mainwin->window); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 6321 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6322 | else |
| 6323 | return NULL; |
| 6324 | } |
| 6325 | #endif |
| 6326 | |
| 6327 | void |
| 6328 | gui_mch_beep(void) |
| 6329 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6330 | GdkDisplay *display; |
| 6331 | |
Bram Moolenaar | b463e8d | 2017-06-05 15:07:09 +0200 | [diff] [blame] | 6332 | #if GTK_CHECK_VERSION(3,0,0) |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 6333 | if (gui.mainwin != NULL && gtk_widget_get_realized(gui.mainwin)) |
Bram Moolenaar | b463e8d | 2017-06-05 15:07:09 +0200 | [diff] [blame] | 6334 | #else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6335 | if (gui.mainwin != NULL && GTK_WIDGET_REALIZED(gui.mainwin)) |
Bram Moolenaar | b463e8d | 2017-06-05 15:07:09 +0200 | [diff] [blame] | 6336 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6337 | display = gtk_widget_get_display(gui.mainwin); |
| 6338 | else |
| 6339 | display = gdk_display_get_default(); |
| 6340 | |
| 6341 | if (display != NULL) |
| 6342 | gdk_display_beep(display); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6343 | } |
| 6344 | |
| 6345 | void |
| 6346 | gui_mch_flash(int msec) |
| 6347 | { |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 6348 | #if GTK_CHECK_VERSION(3,0,0) |
| 6349 | /* TODO Replace GdkGC with Cairo */ |
| 6350 | (void)msec; |
| 6351 | #else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6352 | GdkGCValues values; |
| 6353 | GdkGC *invert_gc; |
| 6354 | |
| 6355 | if (gui.drawarea->window == NULL) |
| 6356 | return; |
| 6357 | |
| 6358 | values.foreground.pixel = gui.norm_pixel ^ gui.back_pixel; |
| 6359 | values.background.pixel = gui.norm_pixel ^ gui.back_pixel; |
| 6360 | values.function = GDK_XOR; |
| 6361 | invert_gc = gdk_gc_new_with_values(gui.drawarea->window, |
| 6362 | &values, |
| 6363 | GDK_GC_FOREGROUND | |
| 6364 | GDK_GC_BACKGROUND | |
| 6365 | GDK_GC_FUNCTION); |
| 6366 | gdk_gc_set_exposures(invert_gc, |
| 6367 | gui.visibility != GDK_VISIBILITY_UNOBSCURED); |
| 6368 | /* |
| 6369 | * Do a visual beep by changing back and forth in some undetermined way, |
| 6370 | * the foreground and background colors. This is due to the fact that |
| 6371 | * there can't be really any prediction about the effects of XOR on |
| 6372 | * arbitrary X11 servers. However this seems to be enough for what we |
| 6373 | * intend it to do. |
| 6374 | */ |
| 6375 | gdk_draw_rectangle(gui.drawarea->window, invert_gc, |
| 6376 | TRUE, |
| 6377 | 0, 0, |
| 6378 | FILL_X((int)Columns) + gui.border_offset, |
| 6379 | FILL_Y((int)Rows) + gui.border_offset); |
| 6380 | |
| 6381 | gui_mch_flush(); |
| 6382 | ui_delay((long)msec, TRUE); /* wait so many msec */ |
| 6383 | |
| 6384 | gdk_draw_rectangle(gui.drawarea->window, invert_gc, |
| 6385 | TRUE, |
| 6386 | 0, 0, |
| 6387 | FILL_X((int)Columns) + gui.border_offset, |
| 6388 | FILL_Y((int)Rows) + gui.border_offset); |
| 6389 | |
| 6390 | gdk_gc_destroy(invert_gc); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 6391 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6392 | } |
| 6393 | |
| 6394 | /* |
| 6395 | * Invert a rectangle from row r, column c, for nr rows and nc columns. |
| 6396 | */ |
| 6397 | void |
| 6398 | gui_mch_invert_rectangle(int r, int c, int nr, int nc) |
| 6399 | { |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 6400 | #if GTK_CHECK_VERSION(3,0,0) |
Bram Moolenaar | 4fc563b | 2016-03-12 12:40:58 +0100 | [diff] [blame] | 6401 | const GdkRectangle rect = { |
| 6402 | FILL_X(c), FILL_Y(r), nc * gui.char_width, nr * gui.char_height |
| 6403 | }; |
| 6404 | cairo_t * const cr = cairo_create(gui.surface); |
| 6405 | |
Bram Moolenaar | 36edf06 | 2016-07-21 22:10:12 +0200 | [diff] [blame] | 6406 | 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] | 6407 | # if CAIRO_VERSION >= CAIRO_VERSION_ENCODE(1,9,2) |
| 6408 | cairo_set_operator(cr, CAIRO_OPERATOR_DIFFERENCE); |
| 6409 | # else |
| 6410 | /* Give an implementation for older cairo versions if necessary. */ |
| 6411 | # endif |
| 6412 | gdk_cairo_rectangle(cr, &rect); |
| 6413 | cairo_fill(cr); |
| 6414 | |
| 6415 | cairo_destroy(cr); |
| 6416 | |
| 6417 | if (!gui.by_signal) |
| 6418 | gtk_widget_queue_draw_area(gui.drawarea, rect.x, rect.y, |
| 6419 | rect.width, rect.height); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 6420 | #else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6421 | GdkGCValues values; |
| 6422 | GdkGC *invert_gc; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6423 | |
| 6424 | if (gui.drawarea->window == NULL) |
| 6425 | return; |
| 6426 | |
Bram Moolenaar | 89d4032 | 2006-08-29 15:30:07 +0000 | [diff] [blame] | 6427 | values.foreground.pixel = gui.norm_pixel ^ gui.back_pixel; |
| 6428 | values.background.pixel = gui.norm_pixel ^ gui.back_pixel; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6429 | values.function = GDK_XOR; |
| 6430 | invert_gc = gdk_gc_new_with_values(gui.drawarea->window, |
| 6431 | &values, |
| 6432 | GDK_GC_FOREGROUND | |
| 6433 | GDK_GC_BACKGROUND | |
| 6434 | GDK_GC_FUNCTION); |
Bram Moolenaar | 89d4032 | 2006-08-29 15:30:07 +0000 | [diff] [blame] | 6435 | gdk_gc_set_exposures(invert_gc, gui.visibility != |
| 6436 | GDK_VISIBILITY_UNOBSCURED); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6437 | gdk_draw_rectangle(gui.drawarea->window, invert_gc, |
| 6438 | TRUE, |
| 6439 | FILL_X(c), FILL_Y(r), |
| 6440 | (nc) * gui.char_width, (nr) * gui.char_height); |
| 6441 | gdk_gc_destroy(invert_gc); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 6442 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6443 | } |
| 6444 | |
| 6445 | /* |
| 6446 | * Iconify the GUI window. |
| 6447 | */ |
| 6448 | void |
| 6449 | gui_mch_iconify(void) |
| 6450 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6451 | gtk_window_iconify(GTK_WINDOW(gui.mainwin)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6452 | } |
| 6453 | |
| 6454 | #if defined(FEAT_EVAL) || defined(PROTO) |
| 6455 | /* |
| 6456 | * Bring the Vim window to the foreground. |
| 6457 | */ |
| 6458 | void |
| 6459 | gui_mch_set_foreground(void) |
| 6460 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6461 | gtk_window_present(GTK_WINDOW(gui.mainwin)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6462 | } |
| 6463 | #endif |
| 6464 | |
| 6465 | /* |
| 6466 | * Draw a cursor without focus. |
| 6467 | */ |
| 6468 | void |
| 6469 | gui_mch_draw_hollow_cursor(guicolor_T color) |
| 6470 | { |
| 6471 | int i = 1; |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 6472 | #if GTK_CHECK_VERSION(3,0,0) |
| 6473 | cairo_t *cr; |
| 6474 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6475 | |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 6476 | #if GTK_CHECK_VERSION(3,0,0) |
| 6477 | if (gtk_widget_get_window(gui.drawarea) == NULL) |
| 6478 | #else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6479 | if (gui.drawarea->window == NULL) |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 6480 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6481 | return; |
| 6482 | |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 6483 | #if GTK_CHECK_VERSION(3,0,0) |
| 6484 | cr = cairo_create(gui.surface); |
| 6485 | #endif |
| 6486 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6487 | gui_mch_set_fg_color(color); |
| 6488 | |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 6489 | #if GTK_CHECK_VERSION(3,0,0) |
Bram Moolenaar | 36edf06 | 2016-07-21 22:10:12 +0200 | [diff] [blame] | 6490 | cairo_set_source_rgba(cr, |
| 6491 | gui.fgcolor->red, gui.fgcolor->green, gui.fgcolor->blue, |
| 6492 | gui.fgcolor->alpha); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 6493 | #else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6494 | gdk_gc_set_foreground(gui.text_gc, gui.fgcolor); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 6495 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6496 | if (mb_lefthalve(gui.row, gui.col)) |
| 6497 | i = 2; |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 6498 | #if GTK_CHECK_VERSION(3,0,0) |
| 6499 | cairo_set_line_width(cr, 1.0); |
| 6500 | cairo_set_line_cap(cr, CAIRO_LINE_CAP_BUTT); |
| 6501 | cairo_rectangle(cr, |
| 6502 | FILL_X(gui.col) + 0.5, FILL_Y(gui.row) + 0.5, |
| 6503 | i * gui.char_width - 1, gui.char_height - 1); |
| 6504 | cairo_stroke(cr); |
| 6505 | cairo_destroy(cr); |
| 6506 | #else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6507 | gdk_draw_rectangle(gui.drawarea->window, gui.text_gc, |
| 6508 | FALSE, |
| 6509 | FILL_X(gui.col), FILL_Y(gui.row), |
| 6510 | i * gui.char_width - 1, gui.char_height - 1); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 6511 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6512 | } |
| 6513 | |
| 6514 | /* |
| 6515 | * Draw part of a cursor, "w" pixels wide, and "h" pixels high, using |
| 6516 | * color "color". |
| 6517 | */ |
| 6518 | void |
| 6519 | gui_mch_draw_part_cursor(int w, int h, guicolor_T color) |
| 6520 | { |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 6521 | #if GTK_CHECK_VERSION(3,0,0) |
| 6522 | if (gtk_widget_get_window(gui.drawarea) == NULL) |
| 6523 | #else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6524 | if (gui.drawarea->window == NULL) |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 6525 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6526 | return; |
| 6527 | |
| 6528 | gui_mch_set_fg_color(color); |
| 6529 | |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 6530 | #if GTK_CHECK_VERSION(3,0,0) |
| 6531 | { |
| 6532 | cairo_t *cr; |
| 6533 | |
| 6534 | cr = cairo_create(gui.surface); |
Bram Moolenaar | 36edf06 | 2016-07-21 22:10:12 +0200 | [diff] [blame] | 6535 | cairo_set_source_rgba(cr, |
| 6536 | gui.fgcolor->red, gui.fgcolor->green, gui.fgcolor->blue, |
| 6537 | gui.fgcolor->alpha); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 6538 | cairo_rectangle(cr, |
| 6539 | # ifdef FEAT_RIGHTLEFT |
| 6540 | /* vertical line should be on the right of current point */ |
| 6541 | CURSOR_BAR_RIGHT ? FILL_X(gui.col + 1) - w : |
| 6542 | # endif |
| 6543 | FILL_X(gui.col), FILL_Y(gui.row) + gui.char_height - h, |
| 6544 | w, h); |
| 6545 | cairo_fill(cr); |
| 6546 | cairo_destroy(cr); |
| 6547 | } |
| 6548 | #else /* !GTK_CHECK_VERSION(3,0,0) */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6549 | gdk_gc_set_foreground(gui.text_gc, gui.fgcolor); |
| 6550 | gdk_draw_rectangle(gui.drawarea->window, gui.text_gc, |
| 6551 | TRUE, |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 6552 | # ifdef FEAT_RIGHTLEFT |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6553 | /* vertical line should be on the right of current point */ |
| 6554 | CURSOR_BAR_RIGHT ? FILL_X(gui.col + 1) - w : |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 6555 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6556 | FILL_X(gui.col), |
| 6557 | FILL_Y(gui.row) + gui.char_height - h, |
| 6558 | w, h); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 6559 | #endif /* !GTK_CHECK_VERSION(3,0,0) */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6560 | } |
| 6561 | |
| 6562 | |
| 6563 | /* |
| 6564 | * Catch up with any queued X11 events. This may put keyboard input into the |
| 6565 | * input buffer, call resize call-backs, trigger timers etc. If there is |
| 6566 | * nothing in the X11 event queue (& no timers pending), then we return |
| 6567 | * immediately. |
| 6568 | */ |
| 6569 | void |
| 6570 | gui_mch_update(void) |
| 6571 | { |
Bram Moolenaar | a3f4166 | 2010-07-11 19:01:06 +0200 | [diff] [blame] | 6572 | while (g_main_context_pending(NULL) && !vim_is_input_buf_full()) |
| 6573 | g_main_context_iteration(NULL, TRUE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6574 | } |
| 6575 | |
Bram Moolenaar | 4ab7968 | 2017-08-27 14:50:47 +0200 | [diff] [blame] | 6576 | static timeout_cb_type |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6577 | input_timer_cb(gpointer data) |
| 6578 | { |
| 6579 | int *timed_out = (int *) data; |
| 6580 | |
Bram Moolenaar | 4932594 | 2007-05-10 19:19:59 +0000 | [diff] [blame] | 6581 | /* Just inform the caller about the occurrence of it */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6582 | *timed_out = TRUE; |
| 6583 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6584 | return FALSE; /* don't happen again */ |
| 6585 | } |
| 6586 | |
Bram Moolenaar | 4ab7968 | 2017-08-27 14:50:47 +0200 | [diff] [blame] | 6587 | #ifdef FEAT_JOB_CHANNEL |
| 6588 | static timeout_cb_type |
| 6589 | channel_poll_cb(gpointer data UNUSED) |
| 6590 | { |
| 6591 | /* Using an event handler for a channel that may be disconnected does |
| 6592 | * not work, it hangs. Instead poll for messages. */ |
| 6593 | channel_handle_events(TRUE); |
| 6594 | parse_queued_messages(); |
| 6595 | |
| 6596 | return TRUE; /* repeat */ |
| 6597 | } |
| 6598 | #endif |
| 6599 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6600 | /* |
| 6601 | * GUI input routine called by gui_wait_for_chars(). Waits for a character |
| 6602 | * from the keyboard. |
| 6603 | * wtime == -1 Wait forever. |
| 6604 | * wtime == 0 This should never happen. |
| 6605 | * wtime > 0 Wait wtime milliseconds for a character. |
| 6606 | * Returns OK if a character was found to be available within the given time, |
| 6607 | * or FAIL otherwise. |
| 6608 | */ |
| 6609 | int |
| 6610 | gui_mch_wait_for_chars(long wtime) |
| 6611 | { |
Bram Moolenaar | 4231da4 | 2016-06-02 14:30:04 +0200 | [diff] [blame] | 6612 | int focus; |
| 6613 | guint timer; |
| 6614 | static int timed_out; |
| 6615 | int retval = FAIL; |
Bram Moolenaar | 4ab7968 | 2017-08-27 14:50:47 +0200 | [diff] [blame] | 6616 | #ifdef FEAT_JOB_CHANNEL |
| 6617 | guint channel_timer = 0; |
| 6618 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6619 | |
| 6620 | timed_out = FALSE; |
| 6621 | |
| 6622 | /* this timeout makes sure that we will return if no characters arrived in |
| 6623 | * time */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6624 | if (wtime > 0) |
Bram Moolenaar | 4ab7968 | 2017-08-27 14:50:47 +0200 | [diff] [blame] | 6625 | timer = timeout_add(wtime, input_timer_cb, &timed_out); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6626 | else |
| 6627 | timer = 0; |
| 6628 | |
Bram Moolenaar | 4ab7968 | 2017-08-27 14:50:47 +0200 | [diff] [blame] | 6629 | #ifdef FEAT_JOB_CHANNEL |
| 6630 | /* If there is a channel with the keep_open flag we need to poll for input |
| 6631 | * on them. */ |
| 6632 | if (channel_any_keep_open()) |
| 6633 | channel_timer = timeout_add(20, channel_poll_cb, NULL); |
| 6634 | #endif |
| 6635 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6636 | focus = gui.in_focus; |
| 6637 | |
| 6638 | do |
| 6639 | { |
| 6640 | /* Stop or start blinking when focus changes */ |
| 6641 | if (gui.in_focus != focus) |
| 6642 | { |
| 6643 | if (gui.in_focus) |
| 6644 | gui_mch_start_blink(); |
| 6645 | else |
| 6646 | gui_mch_stop_blink(); |
| 6647 | focus = gui.in_focus; |
| 6648 | } |
| 6649 | |
Bram Moolenaar | 93c88e0 | 2015-09-15 14:12:05 +0200 | [diff] [blame] | 6650 | #ifdef MESSAGE_QUEUE |
Bram Moolenaar | 4231da4 | 2016-06-02 14:30:04 +0200 | [diff] [blame] | 6651 | # ifdef FEAT_TIMERS |
| 6652 | did_add_timer = FALSE; |
| 6653 | # endif |
Bram Moolenaar | 93c88e0 | 2015-09-15 14:12:05 +0200 | [diff] [blame] | 6654 | parse_queued_messages(); |
Bram Moolenaar | 4231da4 | 2016-06-02 14:30:04 +0200 | [diff] [blame] | 6655 | # ifdef FEAT_TIMERS |
| 6656 | if (did_add_timer) |
| 6657 | /* Need to recompute the waiting time. */ |
| 6658 | goto theend; |
| 6659 | # endif |
Bram Moolenaar | 8c8de83 | 2008-06-24 22:58:06 +0000 | [diff] [blame] | 6660 | #endif |
| 6661 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6662 | /* |
| 6663 | * Loop in GTK+ processing until a timeout or input occurs. |
Bram Moolenaar | d4755bb | 2004-09-02 19:12:26 +0000 | [diff] [blame] | 6664 | * Skip this if input is available anyway (can happen in rare |
| 6665 | * situations, sort of race condition). |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6666 | */ |
Bram Moolenaar | d4755bb | 2004-09-02 19:12:26 +0000 | [diff] [blame] | 6667 | if (!input_available()) |
Bram Moolenaar | a3f4166 | 2010-07-11 19:01:06 +0200 | [diff] [blame] | 6668 | g_main_context_iteration(NULL, TRUE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6669 | |
| 6670 | /* Got char, return immediately */ |
| 6671 | if (input_available()) |
| 6672 | { |
Bram Moolenaar | 4231da4 | 2016-06-02 14:30:04 +0200 | [diff] [blame] | 6673 | retval = OK; |
| 6674 | goto theend; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6675 | } |
| 6676 | } while (wtime < 0 || !timed_out); |
| 6677 | |
| 6678 | /* |
| 6679 | * Flush all eventually pending (drawing) events. |
| 6680 | */ |
| 6681 | gui_mch_update(); |
| 6682 | |
Bram Moolenaar | 4231da4 | 2016-06-02 14:30:04 +0200 | [diff] [blame] | 6683 | theend: |
| 6684 | if (timer != 0 && !timed_out) |
Bram Moolenaar | 4ab7968 | 2017-08-27 14:50:47 +0200 | [diff] [blame] | 6685 | timeout_remove(timer); |
| 6686 | #ifdef FEAT_JOB_CHANNEL |
| 6687 | if (channel_timer != 0) |
| 6688 | timeout_remove(channel_timer); |
Bram Moolenaar | 4231da4 | 2016-06-02 14:30:04 +0200 | [diff] [blame] | 6689 | #endif |
| 6690 | |
| 6691 | return retval; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6692 | } |
| 6693 | |
| 6694 | |
| 6695 | /**************************************************************************** |
| 6696 | * Output drawing routines. |
| 6697 | ****************************************************************************/ |
| 6698 | |
| 6699 | |
| 6700 | /* Flush any output to the screen */ |
| 6701 | void |
| 6702 | gui_mch_flush(void) |
| 6703 | { |
Bram Moolenaar | b463e8d | 2017-06-05 15:07:09 +0200 | [diff] [blame] | 6704 | #if GTK_CHECK_VERSION(3,0,0) |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 6705 | if (gui.mainwin != NULL && gtk_widget_get_realized(gui.mainwin)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6706 | #else |
Bram Moolenaar | b463e8d | 2017-06-05 15:07:09 +0200 | [diff] [blame] | 6707 | if (gui.mainwin != NULL && GTK_WIDGET_REALIZED(gui.mainwin)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6708 | #endif |
Bram Moolenaar | b463e8d | 2017-06-05 15:07:09 +0200 | [diff] [blame] | 6709 | gdk_display_flush(gtk_widget_get_display(gui.mainwin)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6710 | } |
| 6711 | |
| 6712 | /* |
| 6713 | * Clear a rectangular region of the screen from text pos (row1, col1) to |
| 6714 | * (row2, col2) inclusive. |
| 6715 | */ |
| 6716 | void |
Bram Moolenaar | fe344a9 | 2017-02-23 12:20:35 +0100 | [diff] [blame] | 6717 | gui_mch_clear_block(int row1arg, int col1arg, int row2arg, int col2arg) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6718 | { |
Bram Moolenaar | fe344a9 | 2017-02-23 12:20:35 +0100 | [diff] [blame] | 6719 | |
| 6720 | int col1 = check_col(col1arg); |
| 6721 | int col2 = check_col(col2arg); |
| 6722 | int row1 = check_row(row1arg); |
| 6723 | int row2 = check_row(row2arg); |
| 6724 | |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 6725 | #if GTK_CHECK_VERSION(3,0,0) |
| 6726 | if (gtk_widget_get_window(gui.drawarea) == NULL) |
| 6727 | return; |
| 6728 | #else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6729 | GdkColor color; |
| 6730 | |
| 6731 | if (gui.drawarea->window == NULL) |
| 6732 | return; |
| 6733 | |
| 6734 | color.pixel = gui.back_pixel; |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 6735 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6736 | |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 6737 | #if GTK_CHECK_VERSION(3,0,0) |
| 6738 | { |
| 6739 | /* Add one pixel to the far right column in case a double-stroked |
| 6740 | * bold glyph may sit there. */ |
| 6741 | const GdkRectangle rect = { |
| 6742 | FILL_X(col1), FILL_Y(row1), |
| 6743 | (col2 - col1 + 1) * gui.char_width + (col2 == Columns - 1), |
| 6744 | (row2 - row1 + 1) * gui.char_height |
| 6745 | }; |
| 6746 | GdkWindow * const win = gtk_widget_get_window(gui.drawarea); |
| 6747 | cairo_t * const cr = cairo_create(gui.surface); |
Bram Moolenaar | a859f04 | 2016-11-17 19:11:55 +0100 | [diff] [blame] | 6748 | # if GTK_CHECK_VERSION(3,22,2) |
| 6749 | set_cairo_source_rgba_from_color(cr, gui.back_pixel); |
| 6750 | # else |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 6751 | cairo_pattern_t * const pat = gdk_window_get_background_pattern(win); |
| 6752 | if (pat != NULL) |
| 6753 | cairo_set_source(cr, pat); |
| 6754 | else |
Bram Moolenaar | 36edf06 | 2016-07-21 22:10:12 +0200 | [diff] [blame] | 6755 | set_cairo_source_rgba_from_color(cr, gui.back_pixel); |
Bram Moolenaar | a859f04 | 2016-11-17 19:11:55 +0100 | [diff] [blame] | 6756 | # endif |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 6757 | gdk_cairo_rectangle(cr, &rect); |
| 6758 | cairo_fill(cr); |
| 6759 | cairo_destroy(cr); |
| 6760 | |
| 6761 | if (!gui.by_signal) |
| 6762 | gdk_window_invalidate_rect(win, &rect, FALSE); |
| 6763 | } |
| 6764 | #else /* !GTK_CHECK_VERSION(3,0,0) */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6765 | gdk_gc_set_foreground(gui.text_gc, &color); |
| 6766 | |
| 6767 | /* Clear one extra pixel at the far right, for when bold characters have |
| 6768 | * spilled over to the window border. */ |
| 6769 | gdk_draw_rectangle(gui.drawarea->window, gui.text_gc, TRUE, |
| 6770 | FILL_X(col1), FILL_Y(row1), |
| 6771 | (col2 - col1 + 1) * gui.char_width |
| 6772 | + (col2 == Columns - 1), |
| 6773 | (row2 - row1 + 1) * gui.char_height); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 6774 | #endif /* !GTK_CHECK_VERSION(3,0,0) */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6775 | } |
| 6776 | |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 6777 | #if GTK_CHECK_VERSION(3,0,0) |
| 6778 | static void |
| 6779 | gui_gtk_window_clear(GdkWindow *win) |
| 6780 | { |
| 6781 | const GdkRectangle rect = { |
| 6782 | 0, 0, gdk_window_get_width(win), gdk_window_get_height(win) |
| 6783 | }; |
| 6784 | cairo_t * const cr = cairo_create(gui.surface); |
Bram Moolenaar | a859f04 | 2016-11-17 19:11:55 +0100 | [diff] [blame] | 6785 | # if GTK_CHECK_VERSION(3,22,2) |
| 6786 | set_cairo_source_rgba_from_color(cr, gui.back_pixel); |
| 6787 | # else |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 6788 | cairo_pattern_t * const pat = gdk_window_get_background_pattern(win); |
| 6789 | if (pat != NULL) |
| 6790 | cairo_set_source(cr, pat); |
| 6791 | else |
Bram Moolenaar | 36edf06 | 2016-07-21 22:10:12 +0200 | [diff] [blame] | 6792 | set_cairo_source_rgba_from_color(cr, gui.back_pixel); |
Bram Moolenaar | a859f04 | 2016-11-17 19:11:55 +0100 | [diff] [blame] | 6793 | # endif |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 6794 | gdk_cairo_rectangle(cr, &rect); |
| 6795 | cairo_fill(cr); |
| 6796 | cairo_destroy(cr); |
| 6797 | |
| 6798 | if (!gui.by_signal) |
| 6799 | gdk_window_invalidate_rect(win, &rect, FALSE); |
| 6800 | } |
| 6801 | #endif |
| 6802 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6803 | void |
| 6804 | gui_mch_clear_all(void) |
| 6805 | { |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 6806 | #if GTK_CHECK_VERSION(3,0,0) |
| 6807 | if (gtk_widget_get_window(gui.drawarea) != NULL) |
| 6808 | gui_gtk_window_clear(gtk_widget_get_window(gui.drawarea)); |
| 6809 | #else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6810 | if (gui.drawarea->window != NULL) |
| 6811 | gdk_window_clear(gui.drawarea->window); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 6812 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6813 | } |
| 6814 | |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 6815 | #if !GTK_CHECK_VERSION(3,0,0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6816 | /* |
| 6817 | * Redraw any text revealed by scrolling up/down. |
| 6818 | */ |
| 6819 | static void |
| 6820 | check_copy_area(void) |
| 6821 | { |
| 6822 | GdkEvent *event; |
| 6823 | int expose_count; |
| 6824 | |
| 6825 | if (gui.visibility != GDK_VISIBILITY_PARTIAL) |
| 6826 | return; |
| 6827 | |
| 6828 | /* Avoid redrawing the cursor while scrolling or it'll end up where |
| 6829 | * we don't want it to be. I'm not sure if it's correct to call |
| 6830 | * gui_dont_update_cursor() at this point but it works as a quick |
| 6831 | * fix for now. */ |
Bram Moolenaar | 107abd2 | 2016-08-12 14:08:25 +0200 | [diff] [blame] | 6832 | gui_dont_update_cursor(TRUE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6833 | |
| 6834 | do |
| 6835 | { |
| 6836 | /* Wait to check whether the scroll worked or not. */ |
| 6837 | event = gdk_event_get_graphics_expose(gui.drawarea->window); |
| 6838 | |
| 6839 | if (event == NULL) |
| 6840 | break; /* received NoExpose event */ |
| 6841 | |
| 6842 | gui_redraw(event->expose.area.x, event->expose.area.y, |
| 6843 | event->expose.area.width, event->expose.area.height); |
| 6844 | |
| 6845 | expose_count = event->expose.count; |
| 6846 | gdk_event_free(event); |
| 6847 | } |
| 6848 | while (expose_count > 0); /* more events follow */ |
| 6849 | |
| 6850 | gui_can_update_cursor(); |
| 6851 | } |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 6852 | #endif /* !GTK_CHECK_VERSION(3,0,0) */ |
| 6853 | |
| 6854 | #if GTK_CHECK_VERSION(3,0,0) |
| 6855 | static void |
| 6856 | gui_gtk_surface_copy_rect(int dest_x, int dest_y, |
| 6857 | int src_x, int src_y, |
| 6858 | int width, int height) |
| 6859 | { |
| 6860 | cairo_t * const cr = cairo_create(gui.surface); |
| 6861 | |
| 6862 | cairo_rectangle(cr, dest_x, dest_y, width, height); |
| 6863 | cairo_clip(cr); |
| 6864 | cairo_push_group(cr); |
| 6865 | cairo_set_source_surface(cr, gui.surface, dest_x - src_x, dest_y - src_y); |
| 6866 | cairo_paint(cr); |
| 6867 | cairo_pop_group_to_source(cr); |
| 6868 | cairo_paint(cr); |
| 6869 | |
| 6870 | cairo_destroy(cr); |
| 6871 | } |
| 6872 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6873 | |
| 6874 | /* |
| 6875 | * Delete the given number of lines from the given row, scrolling up any |
| 6876 | * text further down within the scroll region. |
| 6877 | */ |
| 6878 | void |
| 6879 | gui_mch_delete_lines(int row, int num_lines) |
| 6880 | { |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 6881 | #if GTK_CHECK_VERSION(3,0,0) |
| 6882 | const int ncols = gui.scroll_region_right - gui.scroll_region_left + 1; |
| 6883 | const int nrows = gui.scroll_region_bot - row + 1; |
| 6884 | const int src_nrows = nrows - num_lines; |
| 6885 | |
| 6886 | gui_gtk_surface_copy_rect( |
| 6887 | FILL_X(gui.scroll_region_left), FILL_Y(row), |
| 6888 | FILL_X(gui.scroll_region_left), FILL_Y(row + num_lines), |
| 6889 | gui.char_width * ncols + 1, gui.char_height * src_nrows); |
| 6890 | gui_clear_block( |
| 6891 | gui.scroll_region_bot - num_lines + 1, gui.scroll_region_left, |
| 6892 | gui.scroll_region_bot, gui.scroll_region_right); |
| 6893 | gui_gtk3_redraw( |
| 6894 | FILL_X(gui.scroll_region_left), FILL_Y(row), |
| 6895 | gui.char_width * ncols + 1, gui.char_height * nrows); |
| 6896 | if (!gui.by_signal) |
| 6897 | gtk_widget_queue_draw_area(gui.drawarea, |
| 6898 | FILL_X(gui.scroll_region_left), FILL_Y(row), |
| 6899 | gui.char_width * ncols + 1, gui.char_height * nrows); |
| 6900 | #else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6901 | if (gui.visibility == GDK_VISIBILITY_FULLY_OBSCURED) |
| 6902 | return; /* Can't see the window */ |
| 6903 | |
| 6904 | gdk_gc_set_foreground(gui.text_gc, gui.fgcolor); |
| 6905 | gdk_gc_set_background(gui.text_gc, gui.bgcolor); |
| 6906 | |
| 6907 | /* copy one extra pixel, for when bold has spilled over */ |
| 6908 | gdk_window_copy_area(gui.drawarea->window, gui.text_gc, |
| 6909 | FILL_X(gui.scroll_region_left), FILL_Y(row), |
| 6910 | gui.drawarea->window, |
| 6911 | FILL_X(gui.scroll_region_left), |
| 6912 | FILL_Y(row + num_lines), |
| 6913 | gui.char_width * (gui.scroll_region_right |
| 6914 | - gui.scroll_region_left + 1) + 1, |
| 6915 | gui.char_height * (gui.scroll_region_bot - row - num_lines + 1)); |
| 6916 | |
| 6917 | gui_clear_block(gui.scroll_region_bot - num_lines + 1, |
| 6918 | gui.scroll_region_left, |
| 6919 | gui.scroll_region_bot, gui.scroll_region_right); |
| 6920 | check_copy_area(); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 6921 | #endif /* !GTK_CHECK_VERSION(3,0,0) */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6922 | } |
| 6923 | |
| 6924 | /* |
| 6925 | * Insert the given number of lines before the given row, scrolling down any |
| 6926 | * following text within the scroll region. |
| 6927 | */ |
| 6928 | void |
| 6929 | gui_mch_insert_lines(int row, int num_lines) |
| 6930 | { |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 6931 | #if GTK_CHECK_VERSION(3,0,0) |
| 6932 | const int ncols = gui.scroll_region_right - gui.scroll_region_left + 1; |
| 6933 | const int nrows = gui.scroll_region_bot - row + 1; |
| 6934 | const int src_nrows = nrows - num_lines; |
| 6935 | |
| 6936 | gui_gtk_surface_copy_rect( |
| 6937 | FILL_X(gui.scroll_region_left), FILL_Y(row + num_lines), |
| 6938 | FILL_X(gui.scroll_region_left), FILL_Y(row), |
| 6939 | gui.char_width * ncols + 1, gui.char_height * src_nrows); |
| 6940 | gui_mch_clear_block( |
| 6941 | row, gui.scroll_region_left, |
| 6942 | row + num_lines - 1, gui.scroll_region_right); |
| 6943 | gui_gtk3_redraw( |
| 6944 | FILL_X(gui.scroll_region_left), FILL_Y(row), |
| 6945 | gui.char_width * ncols + 1, gui.char_height * nrows); |
| 6946 | if (!gui.by_signal) |
| 6947 | gtk_widget_queue_draw_area(gui.drawarea, |
| 6948 | FILL_X(gui.scroll_region_left), FILL_Y(row), |
| 6949 | gui.char_width * ncols + 1, gui.char_height * nrows); |
| 6950 | #else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6951 | if (gui.visibility == GDK_VISIBILITY_FULLY_OBSCURED) |
| 6952 | return; /* Can't see the window */ |
| 6953 | |
| 6954 | gdk_gc_set_foreground(gui.text_gc, gui.fgcolor); |
| 6955 | gdk_gc_set_background(gui.text_gc, gui.bgcolor); |
| 6956 | |
| 6957 | /* copy one extra pixel, for when bold has spilled over */ |
| 6958 | gdk_window_copy_area(gui.drawarea->window, gui.text_gc, |
| 6959 | FILL_X(gui.scroll_region_left), FILL_Y(row + num_lines), |
| 6960 | gui.drawarea->window, |
| 6961 | FILL_X(gui.scroll_region_left), FILL_Y(row), |
| 6962 | gui.char_width * (gui.scroll_region_right |
| 6963 | - gui.scroll_region_left + 1) + 1, |
| 6964 | gui.char_height * (gui.scroll_region_bot - row - num_lines + 1)); |
| 6965 | |
| 6966 | gui_clear_block(row, gui.scroll_region_left, |
| 6967 | row + num_lines - 1, gui.scroll_region_right); |
| 6968 | check_copy_area(); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 6969 | #endif /* !GTK_CHECK_VERSION(3,0,0) */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6970 | } |
| 6971 | |
| 6972 | /* |
| 6973 | * X Selection stuff, for cutting and pasting text to other windows. |
| 6974 | */ |
| 6975 | void |
| 6976 | clip_mch_request_selection(VimClipboard *cbd) |
| 6977 | { |
| 6978 | GdkAtom target; |
| 6979 | unsigned i; |
Bram Moolenaar | 51b5ab9 | 2008-01-06 14:17:07 +0000 | [diff] [blame] | 6980 | time_t start; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6981 | |
| 6982 | for (i = 0; i < N_SELECTION_TARGETS; ++i) |
| 6983 | { |
Bram Moolenaar | 3a6eaa5 | 2009-06-16 13:23:06 +0000 | [diff] [blame] | 6984 | if (!clip_html && selection_targets[i].info == TARGET_HTML) |
| 6985 | continue; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6986 | received_selection = RS_NONE; |
| 6987 | target = gdk_atom_intern(selection_targets[i].target, FALSE); |
| 6988 | |
| 6989 | gtk_selection_convert(gui.drawarea, |
| 6990 | cbd->gtk_sel_atom, target, |
| 6991 | (guint32)GDK_CURRENT_TIME); |
| 6992 | |
Bram Moolenaar | 51b5ab9 | 2008-01-06 14:17:07 +0000 | [diff] [blame] | 6993 | /* Hack: Wait up to three seconds for the selection. A hang was |
| 6994 | * noticed here when using the netrw plugin combined with ":gui" |
| 6995 | * during the FocusGained event. */ |
| 6996 | start = time(NULL); |
| 6997 | while (received_selection == RS_NONE && time(NULL) < start + 3) |
Bram Moolenaar | a3f4166 | 2010-07-11 19:01:06 +0200 | [diff] [blame] | 6998 | g_main_context_iteration(NULL, TRUE); /* wait for selection_received_cb */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6999 | |
| 7000 | if (received_selection != RS_FAIL) |
| 7001 | return; |
| 7002 | } |
| 7003 | |
| 7004 | /* Final fallback position - use the X CUT_BUFFER0 store */ |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 7005 | #if GTK_CHECK_VERSION(3,0,0) |
| 7006 | yank_cut_buffer0(GDK_WINDOW_XDISPLAY(gtk_widget_get_window(gui.mainwin)), |
| 7007 | cbd); |
| 7008 | #else |
Bram Moolenaar | bbc936b | 2009-07-01 16:04:58 +0000 | [diff] [blame] | 7009 | yank_cut_buffer0(GDK_WINDOW_XDISPLAY(gui.mainwin->window), cbd); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 7010 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7011 | } |
| 7012 | |
| 7013 | /* |
| 7014 | * Disown the selection. |
| 7015 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7016 | void |
Bram Moolenaar | b85cb21 | 2009-05-17 14:24:23 +0000 | [diff] [blame] | 7017 | clip_mch_lose_selection(VimClipboard *cbd UNUSED) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7018 | { |
Bram Moolenaar | 1a0316c | 2013-03-13 17:50:25 +0100 | [diff] [blame] | 7019 | gtk_selection_owner_set(NULL, cbd->gtk_sel_atom, gui.event_time); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7020 | gui_mch_update(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7021 | } |
| 7022 | |
| 7023 | /* |
| 7024 | * Own the selection and return OK if it worked. |
| 7025 | */ |
| 7026 | int |
| 7027 | clip_mch_own_selection(VimClipboard *cbd) |
| 7028 | { |
| 7029 | int success; |
| 7030 | |
| 7031 | success = gtk_selection_owner_set(gui.drawarea, cbd->gtk_sel_atom, |
Bram Moolenaar | 20892c1 | 2011-06-26 04:49:00 +0200 | [diff] [blame] | 7032 | gui.event_time); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7033 | gui_mch_update(); |
| 7034 | return (success) ? OK : FAIL; |
| 7035 | } |
| 7036 | |
| 7037 | /* |
| 7038 | * Send the current selection to the clipboard. Do nothing for X because we |
| 7039 | * will fill in the selection only when requested by another app. |
| 7040 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7041 | void |
Bram Moolenaar | b85cb21 | 2009-05-17 14:24:23 +0000 | [diff] [blame] | 7042 | clip_mch_set_selection(VimClipboard *cbd UNUSED) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7043 | { |
| 7044 | } |
| 7045 | |
Bram Moolenaar | 1a0316c | 2013-03-13 17:50:25 +0100 | [diff] [blame] | 7046 | int |
| 7047 | clip_gtk_owner_exists(VimClipboard *cbd) |
| 7048 | { |
| 7049 | return gdk_selection_owner_get(cbd->gtk_sel_atom) != NULL; |
| 7050 | } |
| 7051 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7052 | |
| 7053 | #if defined(FEAT_MENU) || defined(PROTO) |
| 7054 | /* |
| 7055 | * Make a menu item appear either active or not active (grey or not grey). |
| 7056 | */ |
| 7057 | void |
| 7058 | gui_mch_menu_grey(vimmenu_T *menu, int grey) |
| 7059 | { |
| 7060 | if (menu->id == NULL) |
| 7061 | return; |
| 7062 | |
| 7063 | if (menu_is_separator(menu->name)) |
| 7064 | grey = TRUE; |
| 7065 | |
| 7066 | gui_mch_menu_hidden(menu, FALSE); |
| 7067 | /* Be clever about bitfields versus true booleans here! */ |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 7068 | # if GTK_CHECK_VERSION(3,0,0) |
| 7069 | if (!gtk_widget_get_sensitive(menu->id) == !grey) |
| 7070 | # else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7071 | if (!GTK_WIDGET_SENSITIVE(menu->id) == !grey) |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 7072 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7073 | { |
| 7074 | gtk_widget_set_sensitive(menu->id, !grey); |
| 7075 | gui_mch_update(); |
| 7076 | } |
| 7077 | } |
| 7078 | |
| 7079 | /* |
| 7080 | * Make menu item hidden or not hidden. |
| 7081 | */ |
| 7082 | void |
| 7083 | gui_mch_menu_hidden(vimmenu_T *menu, int hidden) |
| 7084 | { |
| 7085 | if (menu->id == 0) |
| 7086 | return; |
| 7087 | |
| 7088 | if (hidden) |
| 7089 | { |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 7090 | # if GTK_CHECK_VERSION(3,0,0) |
| 7091 | if (gtk_widget_get_visible(menu->id)) |
| 7092 | # else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7093 | if (GTK_WIDGET_VISIBLE(menu->id)) |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 7094 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7095 | { |
| 7096 | gtk_widget_hide(menu->id); |
| 7097 | gui_mch_update(); |
| 7098 | } |
| 7099 | } |
| 7100 | else |
| 7101 | { |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 7102 | # if GTK_CHECK_VERSION(3,0,0) |
| 7103 | if (!gtk_widget_get_visible(menu->id)) |
| 7104 | # else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7105 | if (!GTK_WIDGET_VISIBLE(menu->id)) |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 7106 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7107 | { |
| 7108 | gtk_widget_show(menu->id); |
| 7109 | gui_mch_update(); |
| 7110 | } |
| 7111 | } |
| 7112 | } |
| 7113 | |
| 7114 | /* |
| 7115 | * This is called after setting all the menus to grey/hidden or not. |
| 7116 | */ |
| 7117 | void |
| 7118 | gui_mch_draw_menubar(void) |
| 7119 | { |
| 7120 | /* just make sure that the visual changes get effect immediately */ |
| 7121 | gui_mch_update(); |
| 7122 | } |
| 7123 | #endif /* FEAT_MENU */ |
| 7124 | |
| 7125 | /* |
| 7126 | * Scrollbar stuff. |
| 7127 | */ |
| 7128 | void |
| 7129 | gui_mch_enable_scrollbar(scrollbar_T *sb, int flag) |
| 7130 | { |
| 7131 | if (sb->id == NULL) |
| 7132 | return; |
| 7133 | |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 7134 | #if GTK_CHECK_VERSION(3,0,0) |
| 7135 | gtk_widget_set_visible(sb->id, flag); |
| 7136 | #else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7137 | if (flag) |
| 7138 | gtk_widget_show(sb->id); |
| 7139 | else |
| 7140 | gtk_widget_hide(sb->id); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 7141 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7142 | |
Bram Moolenaar | b3656ed | 2006-03-20 21:59:49 +0000 | [diff] [blame] | 7143 | update_window_manager_hints(0, 0); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7144 | } |
| 7145 | |
| 7146 | |
| 7147 | /* |
| 7148 | * Return the RGB value of a pixel as long. |
| 7149 | */ |
Bram Moolenaar | 1b58cdd | 2016-08-22 23:04:33 +0200 | [diff] [blame] | 7150 | guicolor_T |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7151 | gui_mch_get_rgb(guicolor_T pixel) |
| 7152 | { |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 7153 | #if GTK_CHECK_VERSION(3,0,0) |
Bram Moolenaar | 36edf06 | 2016-07-21 22:10:12 +0200 | [diff] [blame] | 7154 | return (long_u)pixel; |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 7155 | #else |
Bram Moolenaar | 36edf06 | 2016-07-21 22:10:12 +0200 | [diff] [blame] | 7156 | GdkColor color; |
| 7157 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7158 | gdk_colormap_query_color(gtk_widget_get_colormap(gui.drawarea), |
| 7159 | (unsigned long)pixel, &color); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7160 | |
Bram Moolenaar | 1b58cdd | 2016-08-22 23:04:33 +0200 | [diff] [blame] | 7161 | return (guicolor_T)( |
| 7162 | (((unsigned)color.red & 0xff00) << 8) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7163 | | ((unsigned)color.green & 0xff00) |
Bram Moolenaar | 1b58cdd | 2016-08-22 23:04:33 +0200 | [diff] [blame] | 7164 | | (((unsigned)color.blue & 0xff00) >> 8)); |
Bram Moolenaar | 36edf06 | 2016-07-21 22:10:12 +0200 | [diff] [blame] | 7165 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7166 | } |
| 7167 | |
| 7168 | /* |
Bram Moolenaar | 6cc1619 | 2005-01-08 21:49:45 +0000 | [diff] [blame] | 7169 | * Get current mouse coordinates in text window. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7170 | */ |
Bram Moolenaar | 6cc1619 | 2005-01-08 21:49:45 +0000 | [diff] [blame] | 7171 | void |
| 7172 | gui_mch_getmouse(int *x, int *y) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7173 | { |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 7174 | #if GTK_CHECK_VERSION(3,0,0) |
| 7175 | gui_gtk_get_pointer(gui.drawarea, x, y, NULL); |
| 7176 | #else |
Bram Moolenaar | 6cc1619 | 2005-01-08 21:49:45 +0000 | [diff] [blame] | 7177 | gdk_window_get_pointer(gui.drawarea->window, x, y, NULL); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 7178 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7179 | } |
| 7180 | |
| 7181 | void |
| 7182 | gui_mch_setmouse(int x, int y) |
| 7183 | { |
| 7184 | /* Sorry for the Xlib call, but we can't avoid it, since there is no |
| 7185 | * internal GDK mechanism present to accomplish this. (and for good |
| 7186 | * reason...) */ |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 7187 | #if GTK_CHECK_VERSION(3,0,0) |
| 7188 | XWarpPointer(GDK_WINDOW_XDISPLAY(gtk_widget_get_window(gui.drawarea)), |
| 7189 | (Window)0, GDK_WINDOW_XID(gtk_widget_get_window(gui.drawarea)), |
| 7190 | 0, 0, 0U, 0U, x, y); |
| 7191 | #else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7192 | XWarpPointer(GDK_WINDOW_XDISPLAY(gui.drawarea->window), |
| 7193 | (Window)0, GDK_WINDOW_XWINDOW(gui.drawarea->window), |
| 7194 | 0, 0, 0U, 0U, x, y); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 7195 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7196 | } |
| 7197 | |
| 7198 | |
| 7199 | #ifdef FEAT_MOUSESHAPE |
| 7200 | /* The last set mouse pointer shape is remembered, to be used when it goes |
| 7201 | * from hidden to not hidden. */ |
| 7202 | static int last_shape = 0; |
| 7203 | #endif |
| 7204 | |
| 7205 | /* |
| 7206 | * Use the blank mouse pointer or not. |
| 7207 | * |
| 7208 | * hide: TRUE = use blank ptr, FALSE = use parent ptr |
| 7209 | */ |
| 7210 | void |
| 7211 | gui_mch_mousehide(int hide) |
| 7212 | { |
| 7213 | if (gui.pointer_hidden != hide) |
| 7214 | { |
| 7215 | gui.pointer_hidden = hide; |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 7216 | #if GTK_CHECK_VERSION(3,0,0) |
| 7217 | if (gtk_widget_get_window(gui.drawarea) && gui.blank_pointer != NULL) |
| 7218 | #else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7219 | if (gui.drawarea->window && gui.blank_pointer != NULL) |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 7220 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7221 | { |
| 7222 | if (hide) |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 7223 | #if GTK_CHECK_VERSION(3,0,0) |
| 7224 | gdk_window_set_cursor(gtk_widget_get_window(gui.drawarea), |
| 7225 | gui.blank_pointer); |
| 7226 | #else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7227 | gdk_window_set_cursor(gui.drawarea->window, gui.blank_pointer); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 7228 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7229 | else |
| 7230 | #ifdef FEAT_MOUSESHAPE |
| 7231 | mch_set_mouse_shape(last_shape); |
Bram Moolenaar | 3f2a5d8 | 2016-02-27 22:08:16 +0100 | [diff] [blame] | 7232 | #elif GTK_CHECK_VERSION(3,0,0) |
| 7233 | gdk_window_set_cursor(gtk_widget_get_window(gui.drawarea), NULL); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7234 | #else |
| 7235 | gdk_window_set_cursor(gui.drawarea->window, NULL); |
| 7236 | #endif |
| 7237 | } |
| 7238 | } |
| 7239 | } |
| 7240 | |
| 7241 | #if defined(FEAT_MOUSESHAPE) || defined(PROTO) |
| 7242 | |
| 7243 | /* Table for shape IDs. Keep in sync with the mshape_names[] table in |
| 7244 | * misc2.c! */ |
| 7245 | static const int mshape_ids[] = |
| 7246 | { |
| 7247 | GDK_LEFT_PTR, /* arrow */ |
| 7248 | GDK_CURSOR_IS_PIXMAP, /* blank */ |
| 7249 | GDK_XTERM, /* beam */ |
| 7250 | GDK_SB_V_DOUBLE_ARROW, /* updown */ |
| 7251 | GDK_SIZING, /* udsizing */ |
| 7252 | GDK_SB_H_DOUBLE_ARROW, /* leftright */ |
| 7253 | GDK_SIZING, /* lrsizing */ |
| 7254 | GDK_WATCH, /* busy */ |
| 7255 | GDK_X_CURSOR, /* no */ |
| 7256 | GDK_CROSSHAIR, /* crosshair */ |
| 7257 | GDK_HAND1, /* hand1 */ |
| 7258 | GDK_HAND2, /* hand2 */ |
| 7259 | GDK_PENCIL, /* pencil */ |
| 7260 | GDK_QUESTION_ARROW, /* question */ |
| 7261 | GDK_RIGHT_PTR, /* right-arrow */ |
| 7262 | GDK_CENTER_PTR, /* up-arrow */ |
| 7263 | GDK_LEFT_PTR /* last one */ |
| 7264 | }; |
| 7265 | |
| 7266 | void |
| 7267 | mch_set_mouse_shape(int shape) |
| 7268 | { |
| 7269 | int id; |
| 7270 | GdkCursor *c; |
| 7271 | |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 7272 | # if GTK_CHECK_VERSION(3,0,0) |
| 7273 | if (gtk_widget_get_window(gui.drawarea) == NULL) |
| 7274 | # else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7275 | if (gui.drawarea->window == NULL) |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 7276 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7277 | return; |
| 7278 | |
| 7279 | if (shape == MSHAPE_HIDE || gui.pointer_hidden) |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 7280 | # if GTK_CHECK_VERSION(3,0,0) |
| 7281 | gdk_window_set_cursor(gtk_widget_get_window(gui.drawarea), |
| 7282 | gui.blank_pointer); |
| 7283 | # else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7284 | gdk_window_set_cursor(gui.drawarea->window, gui.blank_pointer); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 7285 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7286 | else |
| 7287 | { |
| 7288 | if (shape >= MSHAPE_NUMBERED) |
| 7289 | { |
| 7290 | id = shape - MSHAPE_NUMBERED; |
| 7291 | if (id >= GDK_LAST_CURSOR) |
| 7292 | id = GDK_LEFT_PTR; |
| 7293 | else |
| 7294 | id &= ~1; /* they are always even (why?) */ |
| 7295 | } |
Bram Moolenaar | b85cb21 | 2009-05-17 14:24:23 +0000 | [diff] [blame] | 7296 | else if (shape < (int)(sizeof(mshape_ids) / sizeof(int))) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7297 | id = mshape_ids[shape]; |
Bram Moolenaar | f9393ef | 2006-04-24 19:47:27 +0000 | [diff] [blame] | 7298 | else |
| 7299 | return; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7300 | c = gdk_cursor_new_for_display( |
Bram Moolenaar | b71ec9f | 2005-01-25 22:22:02 +0000 | [diff] [blame] | 7301 | gtk_widget_get_display(gui.drawarea), (GdkCursorType)id); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 7302 | # if GTK_CHECK_VERSION(3,0,0) |
| 7303 | gdk_window_set_cursor(gtk_widget_get_window(gui.drawarea), c); |
| 7304 | # else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7305 | gdk_window_set_cursor(gui.drawarea->window, c); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 7306 | # endif |
| 7307 | # if GTK_CHECK_VERSION(3,0,0) |
| 7308 | g_object_unref(G_OBJECT(c)); |
| 7309 | # else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7310 | gdk_cursor_destroy(c); /* Unref, actually. Bloody GTK+ 1. */ |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 7311 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7312 | } |
| 7313 | if (shape != MSHAPE_HIDE) |
| 7314 | last_shape = shape; |
| 7315 | } |
| 7316 | #endif /* FEAT_MOUSESHAPE */ |
| 7317 | |
| 7318 | |
| 7319 | #if defined(FEAT_SIGN_ICONS) || defined(PROTO) |
| 7320 | /* |
| 7321 | * Signs are currently always 2 chars wide. With GTK+ 2, the image will be |
| 7322 | * scaled down if the current font is not big enough, or scaled up if the image |
| 7323 | * size is less than 3/4 of the maximum sign size. With GTK+ 1, the pixmap |
| 7324 | * will be cut off if the current font is not big enough, or centered if it's |
| 7325 | * too small. |
| 7326 | */ |
| 7327 | # define SIGN_WIDTH (2 * gui.char_width) |
| 7328 | # define SIGN_HEIGHT (gui.char_height) |
| 7329 | # define SIGN_ASPECT ((double)SIGN_HEIGHT / (double)SIGN_WIDTH) |
| 7330 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7331 | void |
| 7332 | gui_mch_drawsign(int row, int col, int typenr) |
| 7333 | { |
| 7334 | GdkPixbuf *sign; |
| 7335 | |
| 7336 | sign = (GdkPixbuf *)sign_get_image(typenr); |
| 7337 | |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 7338 | # if GTK_CHECK_VERSION(3,0,0) |
| 7339 | if (sign != NULL && gui.drawarea != NULL |
| 7340 | && gtk_widget_get_window(gui.drawarea) != NULL) |
| 7341 | # else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7342 | if (sign != NULL && gui.drawarea != NULL && gui.drawarea->window != NULL) |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 7343 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7344 | { |
| 7345 | int width; |
| 7346 | int height; |
| 7347 | int xoffset; |
| 7348 | int yoffset; |
| 7349 | int need_scale; |
| 7350 | |
| 7351 | width = gdk_pixbuf_get_width(sign); |
| 7352 | height = gdk_pixbuf_get_height(sign); |
| 7353 | /* |
| 7354 | * Decide whether we need to scale. Allow one pixel of border |
| 7355 | * width to be cut off, in order to avoid excessive scaling for |
| 7356 | * tiny differences in font size. |
Bram Moolenaar | 58cbc91 | 2014-06-17 18:47:02 +0200 | [diff] [blame] | 7357 | * Do scale to fit the height to avoid gaps because of linespacing. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7358 | */ |
| 7359 | need_scale = (width > SIGN_WIDTH + 2 |
Bram Moolenaar | 58cbc91 | 2014-06-17 18:47:02 +0200 | [diff] [blame] | 7360 | || height != SIGN_HEIGHT |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7361 | || (width < 3 * SIGN_WIDTH / 4 |
| 7362 | && height < 3 * SIGN_HEIGHT / 4)); |
| 7363 | if (need_scale) |
| 7364 | { |
Bram Moolenaar | 58cbc91 | 2014-06-17 18:47:02 +0200 | [diff] [blame] | 7365 | double aspect; |
| 7366 | int w = width; |
| 7367 | int h = height; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7368 | |
| 7369 | /* Keep the original aspect ratio */ |
| 7370 | aspect = (double)height / (double)width; |
| 7371 | width = (double)SIGN_WIDTH * SIGN_ASPECT / aspect; |
| 7372 | width = MIN(width, SIGN_WIDTH); |
Bram Moolenaar | 58cbc91 | 2014-06-17 18:47:02 +0200 | [diff] [blame] | 7373 | if (((double)(MAX(height, SIGN_HEIGHT)) / |
| 7374 | (double)(MIN(height, SIGN_HEIGHT))) < 1.15) |
| 7375 | { |
| 7376 | /* Change the aspect ratio by at most 15% to fill the |
| 7377 | * available space completly. */ |
| 7378 | height = (double)SIGN_HEIGHT * SIGN_ASPECT / aspect; |
| 7379 | height = MIN(height, SIGN_HEIGHT); |
| 7380 | } |
| 7381 | else |
| 7382 | height = (double)width * aspect; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7383 | |
Bram Moolenaar | 58cbc91 | 2014-06-17 18:47:02 +0200 | [diff] [blame] | 7384 | if (w == width && h == height) |
| 7385 | { |
| 7386 | /* no change in dimensions; don't decrease reference counter |
| 7387 | * (below) */ |
| 7388 | need_scale = FALSE; |
| 7389 | } |
| 7390 | else |
| 7391 | { |
| 7392 | /* This doesn't seem to be worth caching, and doing so would |
| 7393 | * complicate the code quite a bit. */ |
| 7394 | sign = gdk_pixbuf_scale_simple(sign, width, height, |
| 7395 | GDK_INTERP_BILINEAR); |
| 7396 | if (sign == NULL) |
| 7397 | return; /* out of memory */ |
| 7398 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7399 | } |
| 7400 | |
| 7401 | /* The origin is the upper-left corner of the pixmap. Therefore |
| 7402 | * these offset may become negative if the pixmap is smaller than |
| 7403 | * the 2x1 cells reserved for the sign icon. */ |
| 7404 | xoffset = (width - SIGN_WIDTH) / 2; |
| 7405 | yoffset = (height - SIGN_HEIGHT) / 2; |
| 7406 | |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 7407 | # if GTK_CHECK_VERSION(3,0,0) |
| 7408 | { |
| 7409 | cairo_t *cr; |
| 7410 | cairo_surface_t *bg_surf; |
| 7411 | cairo_t *bg_cr; |
| 7412 | cairo_surface_t *sign_surf; |
| 7413 | cairo_t *sign_cr; |
| 7414 | |
| 7415 | cr = cairo_create(gui.surface); |
| 7416 | |
| 7417 | bg_surf = cairo_surface_create_similar(gui.surface, |
| 7418 | cairo_surface_get_content(gui.surface), |
| 7419 | SIGN_WIDTH, SIGN_HEIGHT); |
| 7420 | bg_cr = cairo_create(bg_surf); |
Bram Moolenaar | 36edf06 | 2016-07-21 22:10:12 +0200 | [diff] [blame] | 7421 | cairo_set_source_rgba(bg_cr, |
| 7422 | gui.bgcolor->red, gui.bgcolor->green, gui.bgcolor->blue, |
| 7423 | gui.bgcolor->alpha); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 7424 | cairo_paint(bg_cr); |
| 7425 | |
| 7426 | sign_surf = cairo_surface_create_similar(gui.surface, |
| 7427 | cairo_surface_get_content(gui.surface), |
| 7428 | SIGN_WIDTH, SIGN_HEIGHT); |
| 7429 | sign_cr = cairo_create(sign_surf); |
| 7430 | gdk_cairo_set_source_pixbuf(sign_cr, sign, -xoffset, -yoffset); |
| 7431 | cairo_paint(sign_cr); |
| 7432 | |
| 7433 | cairo_set_operator(sign_cr, CAIRO_OPERATOR_DEST_OVER); |
| 7434 | cairo_set_source_surface(sign_cr, bg_surf, 0, 0); |
| 7435 | cairo_paint(sign_cr); |
| 7436 | |
| 7437 | cairo_set_source_surface(cr, sign_surf, FILL_X(col), FILL_Y(row)); |
| 7438 | cairo_paint(cr); |
| 7439 | |
| 7440 | cairo_destroy(sign_cr); |
| 7441 | cairo_surface_destroy(sign_surf); |
| 7442 | cairo_destroy(bg_cr); |
| 7443 | cairo_surface_destroy(bg_surf); |
| 7444 | cairo_destroy(cr); |
| 7445 | |
| 7446 | if (!gui.by_signal) |
| 7447 | gtk_widget_queue_draw_area(gui.drawarea, |
| 7448 | FILL_X(col), FILL_Y(col), width, height); |
| 7449 | |
| 7450 | } |
| 7451 | # else /* !GTK_CHECK_VERSION(3,0,0) */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7452 | gdk_gc_set_foreground(gui.text_gc, gui.bgcolor); |
| 7453 | |
| 7454 | gdk_draw_rectangle(gui.drawarea->window, |
| 7455 | gui.text_gc, |
| 7456 | TRUE, |
| 7457 | FILL_X(col), |
| 7458 | FILL_Y(row), |
| 7459 | SIGN_WIDTH, |
| 7460 | SIGN_HEIGHT); |
| 7461 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7462 | gdk_pixbuf_render_to_drawable_alpha(sign, |
| 7463 | gui.drawarea->window, |
| 7464 | MAX(0, xoffset), |
| 7465 | MAX(0, yoffset), |
| 7466 | FILL_X(col) - MIN(0, xoffset), |
| 7467 | FILL_Y(row) - MIN(0, yoffset), |
| 7468 | MIN(width, SIGN_WIDTH), |
| 7469 | MIN(height, SIGN_HEIGHT), |
| 7470 | GDK_PIXBUF_ALPHA_BILEVEL, |
| 7471 | 127, |
| 7472 | GDK_RGB_DITHER_NORMAL, |
| 7473 | 0, 0); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 7474 | # endif /* !GTK_CHECK_VERSION(3,0,0) */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7475 | if (need_scale) |
| 7476 | g_object_unref(sign); |
| 7477 | } |
| 7478 | } |
| 7479 | |
| 7480 | void * |
| 7481 | gui_mch_register_sign(char_u *signfile) |
| 7482 | { |
| 7483 | if (signfile[0] != NUL && signfile[0] != '-' && gui.in_use) |
| 7484 | { |
| 7485 | GdkPixbuf *sign; |
| 7486 | GError *error = NULL; |
| 7487 | char_u *message; |
| 7488 | |
| 7489 | sign = gdk_pixbuf_new_from_file((const char *)signfile, &error); |
| 7490 | |
| 7491 | if (error == NULL) |
| 7492 | return sign; |
| 7493 | |
| 7494 | message = (char_u *)error->message; |
| 7495 | |
| 7496 | if (message != NULL && input_conv.vc_type != CONV_NONE) |
| 7497 | message = string_convert(&input_conv, message, NULL); |
| 7498 | |
| 7499 | if (message != NULL) |
| 7500 | { |
| 7501 | /* The error message is already translated and will be more |
| 7502 | * descriptive than anything we could possibly do ourselves. */ |
| 7503 | EMSG2("E255: %s", message); |
| 7504 | |
| 7505 | if (input_conv.vc_type != CONV_NONE) |
| 7506 | vim_free(message); |
| 7507 | } |
| 7508 | g_error_free(error); |
| 7509 | } |
| 7510 | |
| 7511 | return NULL; |
| 7512 | } |
| 7513 | |
| 7514 | void |
| 7515 | gui_mch_destroy_sign(void *sign) |
| 7516 | { |
| 7517 | if (sign != NULL) |
| 7518 | g_object_unref(sign); |
| 7519 | } |
| 7520 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7521 | #endif /* FEAT_SIGN_ICONS */ |