Mercurial > vim
annotate src/gui_gtk_x11.c @ 7127:e945a8e35535 v7.4.875
commit https://github.com/vim/vim/commit/3fe076f0feb91460266fdf7f9133a59c49a53c4e
Author: Bram Moolenaar <Bram@vim.org>
Date: Fri Sep 25 15:00:56 2015 +0200
patch 7.4.875
Problem: Not obvious how to contribute.
Solution: Add a remark about CONTRIBUTING.md to README.md
author | Christian Brabandt <cb@256bit.org> |
---|---|
date | Fri, 25 Sep 2015 15:15:05 +0200 |
parents | fa95595fbc52 |
children | 8ba562cb3e07 |
rev | line source |
---|---|
7 | 1 /* vi:set ts=8 sts=4 sw=4: |
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 * | |
70 | 13 * (C) 1998,1999,2000 by Marcin Dalecki <martin@dalecki.de> |
7 | 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> | |
22 */ | |
23 | |
24 #include "vim.h" | |
1666 | 25 |
7 | 26 #ifdef FEAT_GUI_GNOME |
27 /* Gnome redefines _() and N_(). Grrr... */ | |
28 # ifdef _ | |
29 # undef _ | |
30 # endif | |
31 # ifdef N_ | |
32 # undef N_ | |
33 # endif | |
34 # ifdef textdomain | |
35 # undef textdomain | |
36 # endif | |
37 # ifdef bindtextdomain | |
38 # undef bindtextdomain | |
39 # endif | |
1286 | 40 # ifdef bind_textdomain_codeset |
41 # undef bind_textdomain_codeset | |
1226 | 42 # endif |
7 | 43 # if defined(FEAT_GETTEXT) && !defined(ENABLE_NLS) |
44 # define ENABLE_NLS /* so the texts in the dialog boxes are translated */ | |
45 # endif | |
46 # include <gnome.h> | |
47 # include "version.h" | |
798 | 48 /* missing prototype in bonobo-dock-item.h */ |
49 extern void bonobo_dock_item_set_behavior(BonoboDockItem *dock_item, BonoboDockItemBehavior beh); | |
7 | 50 #endif |
51 | |
52 #if !defined(FEAT_GUI_GTK) && defined(PROTO) | |
53 /* When generating prototypes we don't want syntax errors. */ | |
54 # define GdkAtom int | |
55 # define GdkEventExpose int | |
56 # define GdkEventFocus int | |
57 # define GdkEventVisibility int | |
58 # define GdkEventProperty int | |
59 # define GtkContainer int | |
60 # define GtkTargetEntry int | |
61 # define GtkType int | |
62 # define GtkWidget int | |
63 # define gint int | |
64 # define gpointer int | |
65 # define guint int | |
66 # define GdkEventKey int | |
67 # define GdkEventSelection int | |
68 # define GtkSelectionData int | |
69 # define GdkEventMotion int | |
70 # define GdkEventButton int | |
71 # define GdkDragContext int | |
72 # define GdkEventConfigure int | |
73 # define GdkEventClient int | |
74 #else | |
75 # include <gdk/gdkkeysyms.h> | |
76 # include <gdk/gdk.h> | |
77 # ifdef WIN3264 | |
78 # include <gdk/gdkwin32.h> | |
79 # else | |
80 # include <gdk/gdkx.h> | |
81 # endif | |
82 | |
83 # include <gtk/gtk.h> | |
84 # include "gui_gtk_f.h" | |
85 #endif | |
86 | |
87 #ifdef HAVE_X11_SUNKEYSYM_H | |
88 # include <X11/Sunkeysym.h> | |
2714 | 89 #endif |
90 | |
7 | 91 /* |
92 * Easy-to-use macro for multihead support. | |
93 */ | |
94 #ifdef HAVE_GTK_MULTIHEAD | |
95 # define GET_X_ATOM(atom) gdk_x11_atom_to_xatom_for_display( \ | |
96 gtk_widget_get_display(gui.mainwin), atom) | |
97 #else | |
98 # define GET_X_ATOM(atom) ((Atom)(atom)) | |
99 #endif | |
100 | |
101 /* Selection type distinguishers */ | |
102 enum | |
103 { | |
104 TARGET_TYPE_NONE, | |
105 TARGET_UTF8_STRING, | |
106 TARGET_STRING, | |
107 TARGET_COMPOUND_TEXT, | |
1904 | 108 TARGET_HTML, |
7 | 109 TARGET_TEXT, |
110 TARGET_TEXT_URI_LIST, | |
111 TARGET_TEXT_PLAIN, | |
112 TARGET_VIM, | |
113 TARGET_VIMENC | |
114 }; | |
115 | |
116 /* | |
117 * Table of selection targets supported by Vim. | |
118 * Note: Order matters, preferred types should come first. | |
119 */ | |
120 static const GtkTargetEntry selection_targets[] = | |
121 { | |
122 {VIMENC_ATOM_NAME, 0, TARGET_VIMENC}, | |
123 {VIM_ATOM_NAME, 0, TARGET_VIM}, | |
1904 | 124 {"text/html", 0, TARGET_HTML}, |
7 | 125 {"UTF8_STRING", 0, TARGET_UTF8_STRING}, |
126 {"COMPOUND_TEXT", 0, TARGET_COMPOUND_TEXT}, | |
127 {"TEXT", 0, TARGET_TEXT}, | |
128 {"STRING", 0, TARGET_STRING} | |
129 }; | |
130 #define N_SELECTION_TARGETS (sizeof(selection_targets) / sizeof(selection_targets[0])) | |
131 | |
132 #ifdef FEAT_DND | |
133 /* | |
134 * Table of DnD targets supported by Vim. | |
135 * Note: Order matters, preferred types should come first. | |
136 */ | |
137 static const GtkTargetEntry dnd_targets[] = | |
138 { | |
139 {"text/uri-list", 0, TARGET_TEXT_URI_LIST}, | |
1904 | 140 {"text/html", 0, TARGET_HTML}, |
7 | 141 {"UTF8_STRING", 0, TARGET_UTF8_STRING}, |
142 {"STRING", 0, TARGET_STRING}, | |
143 {"text/plain", 0, TARGET_TEXT_PLAIN} | |
144 }; | |
145 # define N_DND_TARGETS (sizeof(dnd_targets) / sizeof(dnd_targets[0])) | |
146 #endif | |
147 | |
148 | |
149 /* | |
150 * "Monospace" is a standard font alias that should be present | |
151 * on all proper Pango/fontconfig installations. | |
152 */ | |
153 # define DEFAULT_FONT "Monospace 10" | |
154 | |
155 #if !(defined(FEAT_GUI_GNOME) && defined(FEAT_SESSION)) | |
156 /* | |
157 * Atoms used to communicate save-yourself from the X11 session manager. There | |
158 * is no need to move them into the GUI struct, since they should be constant. | |
159 */ | |
160 static GdkAtom wm_protocols_atom = GDK_NONE; | |
161 static GdkAtom save_yourself_atom = GDK_NONE; | |
162 #endif | |
163 | |
164 /* | |
165 * Atoms used to control/reference X11 selections. | |
166 */ | |
1904 | 167 static GdkAtom html_atom = GDK_NONE; |
7 | 168 static GdkAtom utf8_string_atom = GDK_NONE; |
169 static GdkAtom vim_atom = GDK_NONE; /* Vim's own special selection format */ | |
170 static GdkAtom vimenc_atom = GDK_NONE; /* Vim's extended selection format */ | |
171 | |
172 /* | |
173 * Keycodes recognized by vim. | |
174 * NOTE: when changing this, the table in gui_x11.c probably needs the same | |
175 * change! | |
176 */ | |
177 static struct special_key | |
178 { | |
179 guint key_sym; | |
180 char_u code0; | |
181 char_u code1; | |
182 } | |
183 const special_keys[] = | |
184 { | |
185 {GDK_Up, 'k', 'u'}, | |
186 {GDK_Down, 'k', 'd'}, | |
187 {GDK_Left, 'k', 'l'}, | |
188 {GDK_Right, 'k', 'r'}, | |
189 {GDK_F1, 'k', '1'}, | |
190 {GDK_F2, 'k', '2'}, | |
191 {GDK_F3, 'k', '3'}, | |
192 {GDK_F4, 'k', '4'}, | |
193 {GDK_F5, 'k', '5'}, | |
194 {GDK_F6, 'k', '6'}, | |
195 {GDK_F7, 'k', '7'}, | |
196 {GDK_F8, 'k', '8'}, | |
197 {GDK_F9, 'k', '9'}, | |
198 {GDK_F10, 'k', ';'}, | |
199 {GDK_F11, 'F', '1'}, | |
200 {GDK_F12, 'F', '2'}, | |
201 {GDK_F13, 'F', '3'}, | |
202 {GDK_F14, 'F', '4'}, | |
203 {GDK_F15, 'F', '5'}, | |
204 {GDK_F16, 'F', '6'}, | |
205 {GDK_F17, 'F', '7'}, | |
206 {GDK_F18, 'F', '8'}, | |
207 {GDK_F19, 'F', '9'}, | |
208 {GDK_F20, 'F', 'A'}, | |
209 {GDK_F21, 'F', 'B'}, | |
210 {GDK_Pause, 'F', 'B'}, /* Pause == F21 according to netbeans.txt */ | |
211 {GDK_F22, 'F', 'C'}, | |
212 {GDK_F23, 'F', 'D'}, | |
213 {GDK_F24, 'F', 'E'}, | |
214 {GDK_F25, 'F', 'F'}, | |
215 {GDK_F26, 'F', 'G'}, | |
216 {GDK_F27, 'F', 'H'}, | |
217 {GDK_F28, 'F', 'I'}, | |
218 {GDK_F29, 'F', 'J'}, | |
219 {GDK_F30, 'F', 'K'}, | |
220 {GDK_F31, 'F', 'L'}, | |
221 {GDK_F32, 'F', 'M'}, | |
222 {GDK_F33, 'F', 'N'}, | |
223 {GDK_F34, 'F', 'O'}, | |
224 {GDK_F35, 'F', 'P'}, | |
225 #ifdef SunXK_F36 | |
226 {SunXK_F36, 'F', 'Q'}, | |
227 {SunXK_F37, 'F', 'R'}, | |
228 #endif | |
229 {GDK_Help, '%', '1'}, | |
230 {GDK_Undo, '&', '8'}, | |
231 {GDK_BackSpace, 'k', 'b'}, | |
232 {GDK_Insert, 'k', 'I'}, | |
233 {GDK_Delete, 'k', 'D'}, | |
234 {GDK_3270_BackTab, 'k', 'B'}, | |
235 {GDK_Clear, 'k', 'C'}, | |
236 {GDK_Home, 'k', 'h'}, | |
237 {GDK_End, '@', '7'}, | |
238 {GDK_Prior, 'k', 'P'}, | |
239 {GDK_Next, 'k', 'N'}, | |
240 {GDK_Print, '%', '9'}, | |
241 /* Keypad keys: */ | |
242 {GDK_KP_Left, 'k', 'l'}, | |
243 {GDK_KP_Right, 'k', 'r'}, | |
244 {GDK_KP_Up, 'k', 'u'}, | |
245 {GDK_KP_Down, 'k', 'd'}, | |
246 {GDK_KP_Insert, KS_EXTRA, (char_u)KE_KINS}, | |
247 {GDK_KP_Delete, KS_EXTRA, (char_u)KE_KDEL}, | |
248 {GDK_KP_Home, 'K', '1'}, | |
249 {GDK_KP_End, 'K', '4'}, | |
250 {GDK_KP_Prior, 'K', '3'}, /* page up */ | |
251 {GDK_KP_Next, 'K', '5'}, /* page down */ | |
252 | |
253 {GDK_KP_Add, 'K', '6'}, | |
254 {GDK_KP_Subtract, 'K', '7'}, | |
255 {GDK_KP_Divide, 'K', '8'}, | |
256 {GDK_KP_Multiply, 'K', '9'}, | |
257 {GDK_KP_Enter, 'K', 'A'}, | |
258 {GDK_KP_Decimal, 'K', 'B'}, | |
259 | |
260 {GDK_KP_0, 'K', 'C'}, | |
261 {GDK_KP_1, 'K', 'D'}, | |
262 {GDK_KP_2, 'K', 'E'}, | |
263 {GDK_KP_3, 'K', 'F'}, | |
264 {GDK_KP_4, 'K', 'G'}, | |
265 {GDK_KP_5, 'K', 'H'}, | |
266 {GDK_KP_6, 'K', 'I'}, | |
267 {GDK_KP_7, 'K', 'J'}, | |
268 {GDK_KP_8, 'K', 'K'}, | |
269 {GDK_KP_9, 'K', 'L'}, | |
270 | |
271 /* End of list marker: */ | |
272 {0, 0, 0} | |
273 }; | |
274 | |
275 /* | |
276 * Flags for command line options table below. | |
277 */ | |
278 #define ARG_FONT 1 | |
279 #define ARG_GEOMETRY 2 | |
280 #define ARG_REVERSE 3 | |
281 #define ARG_NOREVERSE 4 | |
282 #define ARG_BACKGROUND 5 | |
283 #define ARG_FOREGROUND 6 | |
284 #define ARG_ICONIC 7 | |
285 #define ARG_ROLE 8 | |
286 #define ARG_NETBEANS 9 | |
287 #define ARG_XRM 10 /* ignored */ | |
288 #define ARG_MENUFONT 11 /* ignored */ | |
289 #define ARG_INDEX_MASK 0x00ff | |
290 #define ARG_HAS_VALUE 0x0100 /* a value is expected after the argument */ | |
291 #define ARG_NEEDS_GUI 0x0200 /* need to initialize the GUI for this */ | |
292 #define ARG_FOR_GTK 0x0400 /* argument is handled by GTK+ or GNOME */ | |
293 #define ARG_COMPAT_LONG 0x0800 /* accept -foo but substitute with --foo */ | |
294 #define ARG_KEEP 0x1000 /* don't remove argument from argv[] */ | |
295 | |
296 /* | |
297 * This table holds all the X GUI command line options allowed. This includes | |
298 * the standard ones so that we can skip them when Vim is started without the | |
299 * GUI (but the GUI might start up later). | |
300 * | |
301 * When changing this, also update doc/gui_x11.txt and the usage message!!! | |
302 */ | |
303 typedef struct | |
304 { | |
305 const char *name; | |
306 unsigned int flags; | |
307 } | |
308 cmdline_option_T; | |
309 | |
310 static const cmdline_option_T cmdline_options[] = | |
311 { | |
312 /* We handle these options ourselves */ | |
313 {"-fn", ARG_FONT|ARG_HAS_VALUE}, | |
314 {"-font", ARG_FONT|ARG_HAS_VALUE}, | |
315 {"-geom", ARG_GEOMETRY|ARG_HAS_VALUE}, | |
316 {"-geometry", ARG_GEOMETRY|ARG_HAS_VALUE}, | |
317 {"-rv", ARG_REVERSE}, | |
318 {"-reverse", ARG_REVERSE}, | |
319 {"+rv", ARG_NOREVERSE}, | |
320 {"+reverse", ARG_NOREVERSE}, | |
321 {"-bg", ARG_BACKGROUND|ARG_HAS_VALUE}, | |
322 {"-background", ARG_BACKGROUND|ARG_HAS_VALUE}, | |
323 {"-fg", ARG_FOREGROUND|ARG_HAS_VALUE}, | |
324 {"-foreground", ARG_FOREGROUND|ARG_HAS_VALUE}, | |
325 {"-iconic", ARG_ICONIC}, | |
326 {"--role", ARG_ROLE|ARG_HAS_VALUE}, | |
327 #ifdef FEAT_NETBEANS_INTG | |
328 {"-nb", ARG_NETBEANS}, /* non-standard value format */ | |
329 {"-xrm", ARG_XRM|ARG_HAS_VALUE}, /* not implemented */ | |
330 {"-mf", ARG_MENUFONT|ARG_HAS_VALUE}, /* not implemented */ | |
331 {"-menufont", ARG_MENUFONT|ARG_HAS_VALUE}, /* not implemented */ | |
332 #endif | |
333 /* Arguments handled by GTK (and GNOME) internally. */ | |
334 {"--g-fatal-warnings", ARG_FOR_GTK}, | |
335 {"--gdk-debug", ARG_FOR_GTK|ARG_HAS_VALUE}, | |
336 {"--gdk-no-debug", ARG_FOR_GTK|ARG_HAS_VALUE}, | |
337 {"--gtk-debug", ARG_FOR_GTK|ARG_HAS_VALUE}, | |
338 {"--gtk-no-debug", ARG_FOR_GTK|ARG_HAS_VALUE}, | |
339 {"--gtk-module", ARG_FOR_GTK|ARG_HAS_VALUE}, | |
340 {"--sync", ARG_FOR_GTK}, | |
341 {"--display", ARG_FOR_GTK|ARG_HAS_VALUE|ARG_COMPAT_LONG}, | |
342 {"--name", ARG_FOR_GTK|ARG_HAS_VALUE|ARG_COMPAT_LONG}, | |
343 {"--class", ARG_FOR_GTK|ARG_HAS_VALUE|ARG_COMPAT_LONG}, | |
344 {"--screen", ARG_FOR_GTK|ARG_HAS_VALUE}, | |
345 {"--gxid-host", ARG_FOR_GTK|ARG_HAS_VALUE}, | |
346 {"--gxid-port", ARG_FOR_GTK|ARG_HAS_VALUE}, | |
347 #ifdef FEAT_GUI_GNOME | |
348 {"--load-modules", ARG_FOR_GTK|ARG_HAS_VALUE}, | |
349 {"--sm-client-id", ARG_FOR_GTK|ARG_HAS_VALUE}, | |
350 {"--sm-config-prefix", ARG_FOR_GTK|ARG_HAS_VALUE}, | |
351 {"--sm-disable", ARG_FOR_GTK}, | |
352 {"--oaf-ior-fd", ARG_FOR_GTK|ARG_HAS_VALUE}, | |
353 {"--oaf-activate-iid", ARG_FOR_GTK|ARG_HAS_VALUE}, | |
354 {"--oaf-private", ARG_FOR_GTK}, | |
355 {"--enable-sound", ARG_FOR_GTK}, | |
356 {"--disable-sound", ARG_FOR_GTK}, | |
357 {"--espeaker", ARG_FOR_GTK|ARG_HAS_VALUE}, | |
358 {"-?", ARG_FOR_GTK|ARG_NEEDS_GUI}, | |
359 {"--help", ARG_FOR_GTK|ARG_NEEDS_GUI|ARG_KEEP}, | |
360 {"--usage", ARG_FOR_GTK|ARG_NEEDS_GUI}, | |
361 # if 0 /* conflicts with Vim's own --version argument */ | |
362 {"--version", ARG_FOR_GTK|ARG_NEEDS_GUI}, | |
363 # endif | |
364 {"--disable-crash-dialog", ARG_FOR_GTK}, | |
365 #endif | |
366 {NULL, 0} | |
367 }; | |
368 | |
369 static int gui_argc = 0; | |
370 static char **gui_argv = NULL; | |
371 | |
372 static const char *role_argument = NULL; | |
373 #if defined(FEAT_GUI_GNOME) && defined(FEAT_SESSION) | |
374 static const char *restart_command = NULL; | |
1898 | 375 static char *abs_restart_command = NULL; |
7 | 376 #endif |
377 static int found_iconic_arg = FALSE; | |
378 | |
379 #ifdef FEAT_GUI_GNOME | |
380 /* | |
381 * Can't use Gnome if --socketid given | |
382 */ | |
383 static int using_gnome = 0; | |
384 #else | |
385 # define using_gnome 0 | |
386 #endif | |
387 | |
388 /* | |
389 * Parse the GUI related command-line arguments. Any arguments used are | |
390 * deleted from argv, and *argc is decremented accordingly. This is called | |
391 * when vim is started, whether or not the GUI has been started. | |
392 */ | |
393 void | |
394 gui_mch_prepare(int *argc, char **argv) | |
395 { | |
396 const cmdline_option_T *option; | |
397 int i = 0; | |
398 int len = 0; | |
399 | |
400 #if defined(FEAT_GUI_GNOME) && defined(FEAT_SESSION) | |
401 /* | |
402 * Determine the command used to invoke Vim, to be passed as restart | |
403 * command to the session manager. If argv[0] contains any directory | |
404 * components try building an absolute path, otherwise leave it as is. | |
405 */ | |
406 restart_command = argv[0]; | |
407 | |
408 if (strchr(argv[0], G_DIR_SEPARATOR) != NULL) | |
409 { | |
410 char_u buf[MAXPATHL]; | |
411 | |
412 if (mch_FullName((char_u *)argv[0], buf, (int)sizeof(buf), TRUE) == OK) | |
1898 | 413 { |
414 abs_restart_command = (char *)vim_strsave(buf); | |
415 restart_command = abs_restart_command; | |
416 } | |
7 | 417 } |
418 #endif | |
419 | |
420 /* | |
421 * Move all the entries in argv which are relevant to GTK+ and GNOME | |
422 * into gui_argv. Freed later in gui_mch_init(). | |
423 */ | |
424 gui_argc = 0; | |
425 gui_argv = (char **)alloc((unsigned)((*argc + 1) * sizeof(char *))); | |
426 | |
427 g_return_if_fail(gui_argv != NULL); | |
428 | |
429 gui_argv[gui_argc++] = argv[i++]; | |
430 | |
431 while (i < *argc) | |
432 { | |
433 /* Don't waste CPU cycles on non-option arguments. */ | |
434 if (argv[i][0] != '-' && argv[i][0] != '+') | |
435 { | |
436 ++i; | |
437 continue; | |
438 } | |
439 | |
440 /* Look for argv[i] in cmdline_options[] table. */ | |
441 for (option = &cmdline_options[0]; option->name != NULL; ++option) | |
442 { | |
443 len = strlen(option->name); | |
444 | |
445 if (strncmp(argv[i], option->name, len) == 0) | |
446 { | |
447 if (argv[i][len] == '\0') | |
448 break; | |
449 /* allow --foo=bar style */ | |
450 if (argv[i][len] == '=' && (option->flags & ARG_HAS_VALUE)) | |
451 break; | |
452 #ifdef FEAT_NETBEANS_INTG | |
453 /* darn, -nb has non-standard syntax */ | |
454 if (vim_strchr((char_u *)":=", argv[i][len]) != NULL | |
455 && (option->flags & ARG_INDEX_MASK) == ARG_NETBEANS) | |
456 break; | |
457 #endif | |
458 } | |
459 else if ((option->flags & ARG_COMPAT_LONG) | |
460 && strcmp(argv[i], option->name + 1) == 0) | |
461 { | |
462 /* Replace the standard X arguments "-name" and "-display" | |
463 * with their GNU-style long option counterparts. */ | |
464 argv[i] = (char *)option->name; | |
465 break; | |
466 } | |
467 } | |
468 if (option->name == NULL) /* no match */ | |
469 { | |
470 ++i; | |
471 continue; | |
472 } | |
473 | |
474 if (option->flags & ARG_FOR_GTK) | |
475 { | |
476 /* Move the argument into gui_argv, which | |
477 * will later be passed to gtk_init_check() */ | |
478 gui_argv[gui_argc++] = argv[i]; | |
479 } | |
480 else | |
481 { | |
482 char *value = NULL; | |
483 | |
484 /* Extract the option's value if there is one. | |
485 * Accept both "--foo bar" and "--foo=bar" style. */ | |
486 if (option->flags & ARG_HAS_VALUE) | |
487 { | |
488 if (argv[i][len] == '=') | |
489 value = &argv[i][len + 1]; | |
490 else if (i + 1 < *argc && strcmp(argv[i + 1], "--") != 0) | |
491 value = argv[i + 1]; | |
492 } | |
493 | |
494 /* Check for options handled by Vim itself */ | |
495 switch (option->flags & ARG_INDEX_MASK) | |
496 { | |
497 case ARG_REVERSE: | |
498 found_reverse_arg = TRUE; | |
499 break; | |
500 case ARG_NOREVERSE: | |
501 found_reverse_arg = FALSE; | |
502 break; | |
503 case ARG_FONT: | |
504 font_argument = value; | |
505 break; | |
506 case ARG_GEOMETRY: | |
507 if (value != NULL) | |
508 gui.geom = vim_strsave((char_u *)value); | |
509 break; | |
510 case ARG_BACKGROUND: | |
511 background_argument = value; | |
512 break; | |
513 case ARG_FOREGROUND: | |
514 foreground_argument = value; | |
515 break; | |
516 case ARG_ICONIC: | |
517 found_iconic_arg = TRUE; | |
518 break; | |
519 case ARG_ROLE: | |
520 role_argument = value; /* used later in gui_mch_open() */ | |
521 break; | |
522 #ifdef FEAT_NETBEANS_INTG | |
523 case ARG_NETBEANS: | |
524 gui.dofork = FALSE; /* don't fork() when starting GUI */ | |
525 netbeansArg = argv[i]; | |
526 break; | |
527 #endif | |
528 default: | |
529 break; | |
530 } | |
531 } | |
532 | |
533 /* These arguments make gnome_program_init() print a message and exit. | |
534 * Must start the GUI for this, otherwise ":gui" will exit later! */ | |
535 if (option->flags & ARG_NEEDS_GUI) | |
536 gui.starting = TRUE; | |
537 | |
538 if (option->flags & ARG_KEEP) | |
539 ++i; | |
540 else | |
541 { | |
542 /* Remove the flag from the argument vector. */ | |
543 if (--*argc > i) | |
544 { | |
545 int n_strip = 1; | |
546 | |
547 /* Move the argument's value as well, if there is one. */ | |
548 if ((option->flags & ARG_HAS_VALUE) | |
549 && argv[i][len] != '=' | |
550 && strcmp(argv[i + 1], "--") != 0) | |
551 { | |
552 ++n_strip; | |
553 --*argc; | |
554 if (option->flags & ARG_FOR_GTK) | |
555 gui_argv[gui_argc++] = argv[i + 1]; | |
556 } | |
557 | |
558 if (*argc > i) | |
559 mch_memmove(&argv[i], &argv[i + n_strip], | |
560 (*argc - i) * sizeof(char *)); | |
561 } | |
562 argv[*argc] = NULL; | |
563 } | |
564 } | |
565 | |
566 gui_argv[gui_argc] = NULL; | |
567 } | |
568 | |
359 | 569 #if defined(EXITFREE) || defined(PROTO) |
570 void | |
571 gui_mch_free_all() | |
572 { | |
573 vim_free(gui_argv); | |
1898 | 574 #if defined(FEAT_GUI_GNOME) && defined(FEAT_SESSION) |
575 vim_free(abs_restart_command); | |
576 #endif | |
359 | 577 } |
578 #endif | |
579 | |
7 | 580 /* |
581 * This should be maybe completely removed. | |
582 * Doesn't seem possible, since check_copy_area() relies on | |
583 * this information. --danielk | |
584 */ | |
585 static gint | |
1884 | 586 visibility_event(GtkWidget *widget UNUSED, |
587 GdkEventVisibility *event, | |
588 gpointer data UNUSED) | |
7 | 589 { |
590 gui.visibility = event->state; | |
591 /* | |
592 * When we do an gdk_window_copy_area(), and the window is partially | |
593 * obscured, we want to receive an event to tell us whether it worked | |
594 * or not. | |
595 */ | |
596 if (gui.text_gc != NULL) | |
597 gdk_gc_set_exposures(gui.text_gc, | |
598 gui.visibility != GDK_VISIBILITY_UNOBSCURED); | |
599 return FALSE; | |
600 } | |
601 | |
602 /* | |
603 * Redraw the corresponding portions of the screen. | |
604 */ | |
605 static gint | |
1884 | 606 expose_event(GtkWidget *widget UNUSED, |
607 GdkEventExpose *event, | |
608 gpointer data UNUSED) | |
7 | 609 { |
610 /* Skip this when the GUI isn't set up yet, will redraw later. */ | |
611 if (gui.starting) | |
612 return FALSE; | |
613 | |
614 out_flush(); /* make sure all output has been processed */ | |
615 gui_redraw(event->area.x, event->area.y, | |
616 event->area.width, event->area.height); | |
617 | |
618 /* Clear the border areas if needed */ | |
619 if (event->area.x < FILL_X(0)) | |
620 gdk_window_clear_area(gui.drawarea->window, 0, 0, FILL_X(0), 0); | |
621 if (event->area.y < FILL_Y(0)) | |
622 gdk_window_clear_area(gui.drawarea->window, 0, 0, 0, FILL_Y(0)); | |
623 if (event->area.x > FILL_X(Columns)) | |
624 gdk_window_clear_area(gui.drawarea->window, | |
625 FILL_X((int)Columns), 0, 0, 0); | |
626 if (event->area.y > FILL_Y(Rows)) | |
627 gdk_window_clear_area(gui.drawarea->window, 0, FILL_Y((int)Rows), 0, 0); | |
628 | |
629 return FALSE; | |
630 } | |
631 | |
632 #ifdef FEAT_CLIENTSERVER | |
633 /* | |
634 * Handle changes to the "Comm" property | |
635 */ | |
636 static gint | |
1884 | 637 property_event(GtkWidget *widget, |
638 GdkEventProperty *event, | |
639 gpointer data UNUSED) | |
7 | 640 { |
641 if (event->type == GDK_PROPERTY_NOTIFY | |
642 && event->state == (int)GDK_PROPERTY_NEW_VALUE | |
643 && GDK_WINDOW_XWINDOW(event->window) == commWindow | |
644 && GET_X_ATOM(event->atom) == commProperty) | |
645 { | |
646 XEvent xev; | |
647 | |
648 /* Translate to XLib */ | |
649 xev.xproperty.type = PropertyNotify; | |
650 xev.xproperty.atom = commProperty; | |
651 xev.xproperty.window = commWindow; | |
652 xev.xproperty.state = PropertyNewValue; | |
7109
fa95595fbc52
commit https://github.com/vim/vim/commit/93c88e0f6a4a8f7634ed84721daf4af46fc0d5db
Christian Brabandt <cb@256bit.org>
parents:
7098
diff
changeset
|
653 serverEventProc(GDK_WINDOW_XDISPLAY(widget->window), &xev, 0); |
7 | 654 } |
655 return FALSE; | |
656 } | |
657 #endif | |
658 | |
659 | |
660 /**************************************************************************** | |
661 * Focus handlers: | |
662 */ | |
663 | |
664 | |
665 /* | |
666 * This is a simple state machine: | |
667 * BLINK_NONE not blinking at all | |
668 * BLINK_OFF blinking, cursor is not shown | |
669 * BLINK_ON blinking, cursor is shown | |
670 */ | |
671 | |
672 #define BLINK_NONE 0 | |
673 #define BLINK_OFF 1 | |
674 #define BLINK_ON 2 | |
675 | |
676 static int blink_state = BLINK_NONE; | |
677 static long_u blink_waittime = 700; | |
678 static long_u blink_ontime = 400; | |
679 static long_u blink_offtime = 250; | |
680 static guint blink_timer = 0; | |
681 | |
682 void | |
683 gui_mch_set_blinking(long waittime, long on, long off) | |
684 { | |
685 blink_waittime = waittime; | |
686 blink_ontime = on; | |
687 blink_offtime = off; | |
688 } | |
689 | |
690 /* | |
691 * Stop the cursor blinking. Show the cursor if it wasn't shown. | |
692 */ | |
693 void | |
694 gui_mch_stop_blink(void) | |
695 { | |
696 if (blink_timer) | |
697 { | |
698 gtk_timeout_remove(blink_timer); | |
699 blink_timer = 0; | |
700 } | |
701 if (blink_state == BLINK_OFF) | |
702 gui_update_cursor(TRUE, FALSE); | |
703 blink_state = BLINK_NONE; | |
704 } | |
705 | |
706 static gint | |
1884 | 707 blink_cb(gpointer data UNUSED) |
7 | 708 { |
709 if (blink_state == BLINK_ON) | |
710 { | |
711 gui_undraw_cursor(); | |
712 blink_state = BLINK_OFF; | |
713 blink_timer = gtk_timeout_add((guint32)blink_offtime, | |
714 (GtkFunction) blink_cb, NULL); | |
715 } | |
716 else | |
717 { | |
718 gui_update_cursor(TRUE, FALSE); | |
719 blink_state = BLINK_ON; | |
720 blink_timer = gtk_timeout_add((guint32)blink_ontime, | |
721 (GtkFunction) blink_cb, NULL); | |
722 } | |
723 | |
724 return FALSE; /* don't happen again */ | |
725 } | |
726 | |
727 /* | |
728 * Start the cursor blinking. If it was already blinking, this restarts the | |
729 * waiting time and shows the cursor. | |
730 */ | |
731 void | |
732 gui_mch_start_blink(void) | |
733 { | |
734 if (blink_timer) | |
5818 | 735 { |
7 | 736 gtk_timeout_remove(blink_timer); |
5818 | 737 blink_timer = 0; |
738 } | |
7 | 739 /* Only switch blinking on if none of the times is zero */ |
740 if (blink_waittime && blink_ontime && blink_offtime && gui.in_focus) | |
741 { | |
742 blink_timer = gtk_timeout_add((guint32)blink_waittime, | |
743 (GtkFunction) blink_cb, NULL); | |
744 blink_state = BLINK_ON; | |
745 gui_update_cursor(TRUE, FALSE); | |
746 } | |
747 } | |
748 | |
749 static gint | |
1884 | 750 enter_notify_event(GtkWidget *widget UNUSED, |
751 GdkEventCrossing *event UNUSED, | |
752 gpointer data UNUSED) | |
7 | 753 { |
754 if (blink_state == BLINK_NONE) | |
755 gui_mch_start_blink(); | |
756 | |
757 /* make sure keyboard input goes there */ | |
758 if (gtk_socket_id == 0 || !GTK_WIDGET_HAS_FOCUS(gui.drawarea)) | |
759 gtk_widget_grab_focus(gui.drawarea); | |
760 | |
761 return FALSE; | |
762 } | |
763 | |
764 static gint | |
1884 | 765 leave_notify_event(GtkWidget *widget UNUSED, |
766 GdkEventCrossing *event UNUSED, | |
767 gpointer data UNUSED) | |
7 | 768 { |
769 if (blink_state != BLINK_NONE) | |
770 gui_mch_stop_blink(); | |
771 | |
772 return FALSE; | |
773 } | |
774 | |
775 static gint | |
1884 | 776 focus_in_event(GtkWidget *widget, |
777 GdkEventFocus *event UNUSED, | |
778 gpointer data UNUSED) | |
7 | 779 { |
780 gui_focus_change(TRUE); | |
781 | |
782 if (blink_state == BLINK_NONE) | |
783 gui_mch_start_blink(); | |
784 | |
1380 | 785 /* make sure keyboard input goes to the draw area (if this is focus for a |
786 * window) */ | |
791 | 787 if (widget != gui.drawarea) |
856 | 788 gtk_widget_grab_focus(gui.drawarea); |
7 | 789 |
790 return TRUE; | |
791 } | |
792 | |
793 static gint | |
1884 | 794 focus_out_event(GtkWidget *widget UNUSED, |
2311
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2301
diff
changeset
|
795 GdkEventFocus *event UNUSED, |
1884 | 796 gpointer data UNUSED) |
7 | 797 { |
798 gui_focus_change(FALSE); | |
799 | |
800 if (blink_state != BLINK_NONE) | |
801 gui_mch_stop_blink(); | |
802 | |
803 return TRUE; | |
804 } | |
805 | |
806 | |
807 /* | |
808 * Translate a GDK key value to UTF-8 independently of the current locale. | |
809 * The output is written to string, which must have room for at least 6 bytes | |
810 * plus the NUL terminator. Returns the length in bytes. | |
811 * | |
812 * This function is used in the GTK+ 2 GUI only. The GTK+ 1 code makes use | |
813 * of GdkEventKey::string instead. But event->string is evil; see here why: | |
814 * http://developer.gnome.org/doc/API/2.0/gdk/gdk-Event-Structures.html#GdkEventKey | |
815 */ | |
816 static int | |
817 keyval_to_string(unsigned int keyval, unsigned int state, char_u *string) | |
818 { | |
819 int len; | |
820 guint32 uc; | |
821 | |
822 uc = gdk_keyval_to_unicode(keyval); | |
823 if (uc != 0) | |
824 { | |
825 /* Check for CTRL-foo */ | |
826 if ((state & GDK_CONTROL_MASK) && uc >= 0x20 && uc < 0x80) | |
827 { | |
828 /* These mappings look arbitrary at the first glance, but in fact | |
829 * resemble quite exactly the behaviour of the GTK+ 1.2 GUI on my | |
830 * machine. The only difference is BS vs. DEL for CTRL-8 (makes | |
831 * more sense and is consistent with usual terminal behaviour). */ | |
832 if (uc >= '@') | |
833 string[0] = uc & 0x1F; | |
834 else if (uc == '2') | |
835 string[0] = NUL; | |
836 else if (uc >= '3' && uc <= '7') | |
837 string[0] = uc ^ 0x28; | |
838 else if (uc == '8') | |
839 string[0] = BS; | |
840 else if (uc == '?') | |
841 string[0] = DEL; | |
842 else | |
843 string[0] = uc; | |
844 len = 1; | |
845 } | |
846 else | |
847 { | |
848 /* Translate a normal key to UTF-8. This doesn't work for dead | |
849 * keys of course, you _have_ to use an input method for that. */ | |
850 len = utf_char2bytes((int)uc, string); | |
851 } | |
852 } | |
853 else | |
854 { | |
855 /* Translate keys which are represented by ASCII control codes in Vim. | |
856 * There are only a few of those; most control keys are translated to | |
857 * special terminal-like control sequences. */ | |
858 len = 1; | |
859 switch (keyval) | |
860 { | |
861 case GDK_Tab: case GDK_KP_Tab: case GDK_ISO_Left_Tab: | |
862 string[0] = TAB; | |
863 break; | |
864 case GDK_Linefeed: | |
865 string[0] = NL; | |
866 break; | |
867 case GDK_Return: case GDK_ISO_Enter: case GDK_3270_Enter: | |
868 string[0] = CAR; | |
869 break; | |
870 case GDK_Escape: | |
871 string[0] = ESC; | |
872 break; | |
873 default: | |
874 len = 0; | |
875 break; | |
876 } | |
877 } | |
878 string[len] = NUL; | |
879 | |
880 return len; | |
881 } | |
882 | |
179 | 883 static int |
884 modifiers_gdk2vim(guint state) | |
885 { | |
886 int modifiers = 0; | |
887 | |
888 if (state & GDK_SHIFT_MASK) | |
889 modifiers |= MOD_MASK_SHIFT; | |
890 if (state & GDK_CONTROL_MASK) | |
891 modifiers |= MOD_MASK_CTRL; | |
892 if (state & GDK_MOD1_MASK) | |
893 modifiers |= MOD_MASK_ALT; | |
2550
7c1f73452ea6
Fix #ifdef for GDK_SUPER_MASK.
Bram Moolenaar <bram@vim.org>
parents:
2520
diff
changeset
|
894 #if GTK_CHECK_VERSION(2,10,0) |
2188
722e390945d7
Avoid warnings for unused arguments when compiling with Gnome.
Bram Moolenaar <bram@vim.org>
parents:
2154
diff
changeset
|
895 if (state & GDK_SUPER_MASK) |
722e390945d7
Avoid warnings for unused arguments when compiling with Gnome.
Bram Moolenaar <bram@vim.org>
parents:
2154
diff
changeset
|
896 modifiers |= MOD_MASK_META; |
722e390945d7
Avoid warnings for unused arguments when compiling with Gnome.
Bram Moolenaar <bram@vim.org>
parents:
2154
diff
changeset
|
897 #endif |
179 | 898 if (state & GDK_MOD4_MASK) |
899 modifiers |= MOD_MASK_META; | |
900 | |
901 return modifiers; | |
902 } | |
903 | |
904 static int | |
905 modifiers_gdk2mouse(guint state) | |
906 { | |
907 int modifiers = 0; | |
908 | |
909 if (state & GDK_SHIFT_MASK) | |
910 modifiers |= MOUSE_SHIFT; | |
911 if (state & GDK_CONTROL_MASK) | |
912 modifiers |= MOUSE_CTRL; | |
913 if (state & GDK_MOD1_MASK) | |
914 modifiers |= MOUSE_ALT; | |
915 | |
916 return modifiers; | |
917 } | |
918 | |
7 | 919 /* |
920 * Main keyboard handler: | |
921 */ | |
922 static gint | |
1884 | 923 key_press_event(GtkWidget *widget UNUSED, |
924 GdkEventKey *event, | |
925 gpointer data UNUSED) | |
7 | 926 { |
2275
e4d849f4df03
Remove the old and not well supported GTK 1 code. (James Vega)
Bram Moolenaar <bram@vim.org>
parents:
2270
diff
changeset
|
927 /* For GTK+ 2 we know for sure how large the string might get. |
7 | 928 * (That is, up to 6 bytes + NUL + CSI escapes + safety measure.) */ |
929 char_u string[32], string2[32]; | |
930 guint key_sym; | |
931 int len; | |
932 int i; | |
933 int modifiers; | |
934 int key; | |
935 guint state; | |
936 char_u *s, *d; | |
937 | |
2923 | 938 gui.event_time = event->time; |
7 | 939 key_sym = event->keyval; |
940 state = event->state; | |
941 | |
942 #ifdef FEAT_XIM | |
943 if (xim_queue_key_press_event(event, TRUE)) | |
944 return TRUE; | |
945 #endif | |
946 | |
947 #ifdef FEAT_HANGULIN | |
948 if (key_sym == GDK_space && (state & GDK_SHIFT_MASK)) | |
949 { | |
950 hangul_input_state_toggle(); | |
951 return TRUE; | |
952 } | |
953 #endif | |
954 | |
955 #ifdef SunXK_F36 | |
956 /* | |
957 * These keys have bogus lookup strings, and trapping them here is | |
958 * easier than trying to XRebindKeysym() on them with every possible | |
959 * combination of modifiers. | |
960 */ | |
961 if (key_sym == SunXK_F36 || key_sym == SunXK_F37) | |
962 len = 0; | |
963 else | |
964 #endif | |
965 { | |
966 len = keyval_to_string(key_sym, state, string2); | |
967 | |
968 /* Careful: convert_input() doesn't handle the NUL character. | |
969 * No need to convert pure ASCII anyway, thus the len > 1 check. */ | |
970 if (len > 1 && input_conv.vc_type != CONV_NONE) | |
971 len = convert_input(string2, len, sizeof(string2)); | |
972 | |
973 s = string2; | |
974 d = string; | |
975 for (i = 0; i < len; ++i) | |
976 { | |
977 *d++ = s[i]; | |
978 if (d[-1] == CSI && d + 2 < string + sizeof(string)) | |
979 { | |
980 /* Turn CSI into K_CSI. */ | |
981 *d++ = KS_EXTRA; | |
982 *d++ = (int)KE_CSI; | |
983 } | |
984 } | |
985 len = d - string; | |
986 } | |
987 | |
988 /* Shift-Tab results in Left_Tab, but we want <S-Tab> */ | |
989 if (key_sym == GDK_ISO_Left_Tab) | |
990 { | |
991 key_sym = GDK_Tab; | |
992 state |= GDK_SHIFT_MASK; | |
993 } | |
994 | |
995 #ifdef FEAT_MENU | |
996 /* If there is a menu and 'wak' is "yes", or 'wak' is "menu" and the key | |
997 * is a menu shortcut, we ignore everything with the ALT modifier. */ | |
998 if ((state & GDK_MOD1_MASK) | |
999 && gui.menu_is_active | |
1000 && (*p_wak == 'y' | |
1001 || (*p_wak == 'm' | |
1002 && len == 1 | |
1003 && gui_is_menu_shortcut(string[0])))) | |
1004 /* For GTK2 we return false to signify that we haven't handled the | |
1005 * keypress, so that gtk will handle the mnemonic or accelerator. */ | |
1006 return FALSE; | |
1007 #endif | |
1008 | |
1009 /* Check for Alt/Meta key (Mod1Mask), but not for a BS, DEL or character | |
1010 * that already has the 8th bit set. | |
1011 * Don't do this for <S-M-Tab>, that should become K_S_TAB with ALT. | |
1012 * Don't do this for double-byte encodings, it turns the char into a lead | |
1013 * byte. */ | |
1014 if (len == 1 | |
2188
722e390945d7
Avoid warnings for unused arguments when compiling with Gnome.
Bram Moolenaar <bram@vim.org>
parents:
2154
diff
changeset
|
1015 && ((state & GDK_MOD1_MASK) |
2571
42bdbc2bbb59
Fix #ifdef for GDK_SUPER_MASK. (Stephan Schulz)
Bram Moolenaar <bram@vim.org>
parents:
2550
diff
changeset
|
1016 #if GTK_CHECK_VERSION(2,10,0) |
2188
722e390945d7
Avoid warnings for unused arguments when compiling with Gnome.
Bram Moolenaar <bram@vim.org>
parents:
2154
diff
changeset
|
1017 || (state & GDK_SUPER_MASK) |
722e390945d7
Avoid warnings for unused arguments when compiling with Gnome.
Bram Moolenaar <bram@vim.org>
parents:
2154
diff
changeset
|
1018 #endif |
722e390945d7
Avoid warnings for unused arguments when compiling with Gnome.
Bram Moolenaar <bram@vim.org>
parents:
2154
diff
changeset
|
1019 ) |
7 | 1020 && !(key_sym == GDK_BackSpace || key_sym == GDK_Delete) |
1021 && (string[0] & 0x80) == 0 | |
1022 && !(key_sym == GDK_Tab && (state & GDK_SHIFT_MASK)) | |
1023 && !enc_dbcs | |
1024 ) | |
1025 { | |
1026 string[0] |= 0x80; | |
1027 state &= ~GDK_MOD1_MASK; /* don't use it again */ | |
1028 if (enc_utf8) /* convert to utf-8 */ | |
1029 { | |
1030 string[1] = string[0] & 0xbf; | |
1031 string[0] = ((unsigned)string[0] >> 6) + 0xc0; | |
1032 if (string[1] == CSI) | |
1033 { | |
1034 string[2] = KS_EXTRA; | |
1035 string[3] = (int)KE_CSI; | |
1036 len = 4; | |
1037 } | |
1038 else | |
1039 len = 2; | |
1040 } | |
1041 } | |
1042 | |
1043 /* Check for special keys. Also do this when len == 1 (key has an ASCII | |
1044 * value) to detect backspace, delete and keypad keys. */ | |
1045 if (len == 0 || len == 1) | |
1046 { | |
1047 for (i = 0; special_keys[i].key_sym != 0; i++) | |
1048 { | |
1049 if (special_keys[i].key_sym == key_sym) | |
1050 { | |
1051 string[0] = CSI; | |
1052 string[1] = special_keys[i].code0; | |
1053 string[2] = special_keys[i].code1; | |
1054 len = -3; | |
1055 break; | |
1056 } | |
1057 } | |
1058 } | |
1059 | |
1060 if (len == 0) /* Unrecognized key */ | |
1061 return TRUE; | |
1062 | |
1063 /* Special keys (and a few others) may have modifiers. Also when using a | |
1064 * double-byte encoding (can't set the 8th bit). */ | |
1065 if (len == -3 || key_sym == GDK_space || key_sym == GDK_Tab | |
1066 || key_sym == GDK_Return || key_sym == GDK_Linefeed | |
1067 || key_sym == GDK_Escape || key_sym == GDK_KP_Tab | |
1068 || key_sym == GDK_ISO_Enter || key_sym == GDK_3270_Enter | |
2188
722e390945d7
Avoid warnings for unused arguments when compiling with Gnome.
Bram Moolenaar <bram@vim.org>
parents:
2154
diff
changeset
|
1069 || (enc_dbcs && len == 1 && ((state & GDK_MOD1_MASK) |
2571
42bdbc2bbb59
Fix #ifdef for GDK_SUPER_MASK. (Stephan Schulz)
Bram Moolenaar <bram@vim.org>
parents:
2550
diff
changeset
|
1070 #if GTK_CHECK_VERSION(2,10,0) |
2188
722e390945d7
Avoid warnings for unused arguments when compiling with Gnome.
Bram Moolenaar <bram@vim.org>
parents:
2154
diff
changeset
|
1071 || (state & GDK_SUPER_MASK) |
7 | 1072 #endif |
2275
e4d849f4df03
Remove the old and not well supported GTK 1 code. (James Vega)
Bram Moolenaar <bram@vim.org>
parents:
2270
diff
changeset
|
1073 ))) |
7 | 1074 { |
179 | 1075 modifiers = modifiers_gdk2vim(state); |
7 | 1076 |
1077 /* | |
1078 * For some keys a shift modifier is translated into another key | |
1079 * code. | |
1080 */ | |
1081 if (len == -3) | |
1082 key = TO_SPECIAL(string[1], string[2]); | |
1083 else | |
1084 key = string[0]; | |
1085 | |
1086 key = simplify_key(key, &modifiers); | |
1087 if (key == CSI) | |
1088 key = K_CSI; | |
1089 if (IS_SPECIAL(key)) | |
1090 { | |
1091 string[0] = CSI; | |
1092 string[1] = K_SECOND(key); | |
1093 string[2] = K_THIRD(key); | |
1094 len = 3; | |
1095 } | |
1096 else | |
1097 { | |
1098 string[0] = key; | |
1099 len = 1; | |
1100 } | |
1101 | |
1102 if (modifiers != 0) | |
1103 { | |
1104 string2[0] = CSI; | |
1105 string2[1] = KS_MODIFIER; | |
1106 string2[2] = modifiers; | |
1107 add_to_input_buf(string2, 3); | |
1108 } | |
1109 } | |
1110 | |
1111 if (len == 1 && ((string[0] == Ctrl_C && ctrl_c_interrupts) | |
1112 || (string[0] == intr_char && intr_char != Ctrl_C))) | |
1113 { | |
1114 trash_input_buf(); | |
1115 got_int = TRUE; | |
1116 } | |
1117 | |
1118 add_to_input_buf(string, len); | |
1119 | |
1120 /* blank out the pointer if necessary */ | |
1121 if (p_mh) | |
1122 gui_mch_mousehide(TRUE); | |
1123 | |
1124 return TRUE; | |
1125 } | |
1126 | |
2275
e4d849f4df03
Remove the old and not well supported GTK 1 code. (James Vega)
Bram Moolenaar <bram@vim.org>
parents:
2270
diff
changeset
|
1127 #if defined(FEAT_XIM) |
7 | 1128 static gboolean |
1884 | 1129 key_release_event(GtkWidget *widget UNUSED, |
1130 GdkEventKey *event, | |
1131 gpointer data UNUSED) | |
7 | 1132 { |
2923 | 1133 gui.event_time = event->time; |
7 | 1134 /* |
1135 * GTK+ 2 input methods may do fancy stuff on key release events too. | |
1136 * With the default IM for instance, you can enter any UCS code point | |
1137 * by holding down CTRL-SHIFT and typing hexadecimal digits. | |
1138 */ | |
1139 return xim_queue_key_press_event(event, FALSE); | |
1140 } | |
1141 #endif | |
1142 | |
1143 | |
1144 /**************************************************************************** | |
1145 * Selection handlers: | |
1146 */ | |
1147 | |
1148 static gint | |
1884 | 1149 selection_clear_event(GtkWidget *widget UNUSED, |
7 | 1150 GdkEventSelection *event, |
1884 | 1151 gpointer user_data UNUSED) |
7 | 1152 { |
1153 if (event->selection == clip_plus.gtk_sel_atom) | |
1154 clip_lose_selection(&clip_plus); | |
1155 else | |
1156 clip_lose_selection(&clip_star); | |
1157 | |
1158 return TRUE; | |
1159 } | |
1160 | |
1161 #define RS_NONE 0 /* selection_received_cb() not called yet */ | |
1162 #define RS_OK 1 /* selection_received_cb() called and OK */ | |
1163 #define RS_FAIL 2 /* selection_received_cb() called and failed */ | |
1164 static int received_selection = RS_NONE; | |
1165 | |
1166 static void | |
1884 | 1167 selection_received_cb(GtkWidget *widget UNUSED, |
7 | 1168 GtkSelectionData *data, |
1884 | 1169 guint time_ UNUSED, |
1170 gpointer user_data UNUSED) | |
7 | 1171 { |
1172 VimClipboard *cbd; | |
1173 char_u *text; | |
1174 char_u *tmpbuf = NULL; | |
1175 guchar *tmpbuf_utf8 = NULL; | |
1176 int len; | |
2896 | 1177 int motion_type = MAUTO; |
7 | 1178 |
1179 if (data->selection == clip_plus.gtk_sel_atom) | |
1180 cbd = &clip_plus; | |
1181 else | |
1182 cbd = &clip_star; | |
1183 | |
1184 text = (char_u *)data->data; | |
1185 len = data->length; | |
1186 | |
1187 if (text == NULL || len <= 0) | |
1188 { | |
1189 received_selection = RS_FAIL; | |
1190 /* clip_free_selection(cbd); ??? */ | |
1191 | |
1192 return; | |
1193 } | |
1194 | |
1195 if (data->type == vim_atom) | |
1196 { | |
1197 motion_type = *text++; | |
1198 --len; | |
1199 } | |
1200 | |
1201 else if (data->type == vimenc_atom) | |
1202 { | |
1203 char_u *enc; | |
1204 vimconv_T conv; | |
1205 | |
1206 motion_type = *text++; | |
1207 --len; | |
1208 | |
1209 enc = text; | |
1210 text += STRLEN(text) + 1; | |
1211 len -= text - enc; | |
1212 | |
1213 /* If the encoding of the text is different from 'encoding', attempt | |
1214 * converting it. */ | |
1215 conv.vc_type = CONV_NONE; | |
1216 convert_setup(&conv, enc, p_enc); | |
1217 if (conv.vc_type != CONV_NONE) | |
1218 { | |
1219 tmpbuf = string_convert(&conv, text, &len); | |
1220 if (tmpbuf != NULL) | |
1221 text = tmpbuf; | |
1222 convert_setup(&conv, NULL, NULL); | |
1223 } | |
1224 } | |
2275
e4d849f4df03
Remove the old and not well supported GTK 1 code. (James Vega)
Bram Moolenaar <bram@vim.org>
parents:
2270
diff
changeset
|
1225 |
7 | 1226 /* gtk_selection_data_get_text() handles all the nasty details |
1227 * and targets and encodings etc. This rocks so hard. */ | |
1228 else | |
1229 { | |
1230 tmpbuf_utf8 = gtk_selection_data_get_text(data); | |
1231 if (tmpbuf_utf8 != NULL) | |
1232 { | |
1233 len = STRLEN(tmpbuf_utf8); | |
1234 if (input_conv.vc_type != CONV_NONE) | |
1235 { | |
1236 tmpbuf = string_convert(&input_conv, tmpbuf_utf8, &len); | |
1237 if (tmpbuf != NULL) | |
1238 text = tmpbuf; | |
1239 } | |
1240 else | |
1241 text = tmpbuf_utf8; | |
1242 } | |
1904 | 1243 else if (len >= 2 && text[0] == 0xff && text[1] == 0xfe) |
1244 { | |
1245 vimconv_T conv; | |
1246 | |
1247 /* UTF-16, we get this for HTML */ | |
1248 conv.vc_type = CONV_NONE; | |
1249 convert_setup_ext(&conv, (char_u *)"utf-16le", FALSE, p_enc, TRUE); | |
1250 | |
1251 if (conv.vc_type != CONV_NONE) | |
1252 { | |
1253 text += 2; | |
1254 len -= 2; | |
1255 tmpbuf = string_convert(&conv, text, &len); | |
1256 convert_setup(&conv, NULL, NULL); | |
1257 } | |
1258 if (tmpbuf != NULL) | |
1259 text = tmpbuf; | |
1260 } | |
7 | 1261 } |
1262 | |
4352 | 1263 /* Chop off any trailing NUL bytes. OpenOffice sends these. */ |
2183 | 1264 while (len > 0 && text[len - 1] == NUL) |
1265 --len; | |
1266 | |
7 | 1267 clip_yank_selection(motion_type, text, (long)len, cbd); |
1268 received_selection = RS_OK; | |
1269 vim_free(tmpbuf); | |
1270 g_free(tmpbuf_utf8); | |
1271 } | |
1272 | |
1273 /* | |
1274 * Prepare our selection data for passing it to the external selection | |
1275 * client. | |
1276 */ | |
1277 static void | |
1884 | 1278 selection_get_cb(GtkWidget *widget UNUSED, |
7 | 1279 GtkSelectionData *selection_data, |
1280 guint info, | |
1884 | 1281 guint time_ UNUSED, |
1282 gpointer user_data UNUSED) | |
7 | 1283 { |
1284 char_u *string; | |
1285 char_u *tmpbuf; | |
1286 long_u tmplen; | |
1287 int length; | |
1288 int motion_type; | |
1289 GdkAtom type; | |
1290 VimClipboard *cbd; | |
1291 | |
1292 if (selection_data->selection == clip_plus.gtk_sel_atom) | |
1293 cbd = &clip_plus; | |
1294 else | |
1295 cbd = &clip_star; | |
1296 | |
1297 if (!cbd->owned) | |
1298 return; /* Shouldn't ever happen */ | |
1299 | |
1300 if (info != (guint)TARGET_STRING | |
1904 | 1301 && (!clip_html || info != (guint)TARGET_HTML) |
7 | 1302 && info != (guint)TARGET_UTF8_STRING |
1303 && info != (guint)TARGET_VIMENC | |
1304 && info != (guint)TARGET_VIM | |
1305 && info != (guint)TARGET_COMPOUND_TEXT | |
1306 && info != (guint)TARGET_TEXT) | |
1307 return; | |
1308 | |
1309 /* get the selection from the '*'/'+' register */ | |
1310 clip_get_selection(cbd); | |
1311 | |
1312 motion_type = clip_convert_selection(&string, &tmplen, cbd); | |
1313 if (motion_type < 0 || string == NULL) | |
1314 return; | |
1315 /* Due to int arguments we can't handle more than G_MAXINT. Also | |
1316 * reserve one extra byte for NUL or the motion type; just in case. | |
1317 * (Not that pasting 2G of text is ever going to work, but... ;-) */ | |
1318 length = MIN(tmplen, (long_u)(G_MAXINT - 1)); | |
1319 | |
1320 if (info == (guint)TARGET_VIM) | |
1321 { | |
1322 tmpbuf = alloc((unsigned)length + 1); | |
1323 if (tmpbuf != NULL) | |
1324 { | |
1325 tmpbuf[0] = motion_type; | |
1326 mch_memmove(tmpbuf + 1, string, (size_t)length); | |
1327 } | |
1328 /* For our own format, the first byte contains the motion type */ | |
1329 ++length; | |
1330 vim_free(string); | |
1331 string = tmpbuf; | |
1332 type = vim_atom; | |
1333 } | |
1334 | |
1904 | 1335 else if (info == (guint)TARGET_HTML) |
1336 { | |
1337 vimconv_T conv; | |
1338 | |
1339 /* Since we get utf-16, we probably should set it as well. */ | |
1340 conv.vc_type = CONV_NONE; | |
1341 convert_setup_ext(&conv, p_enc, TRUE, (char_u *)"utf-16le", FALSE); | |
1342 if (conv.vc_type != CONV_NONE) | |
1343 { | |
1344 tmpbuf = string_convert(&conv, string, &length); | |
1345 convert_setup(&conv, NULL, NULL); | |
1346 vim_free(string); | |
1347 string = tmpbuf; | |
1348 } | |
1349 | |
1350 /* Prepend the BOM: "fffe" */ | |
1351 if (string != NULL) | |
1352 { | |
1353 tmpbuf = alloc(length + 2); | |
1354 tmpbuf[0] = 0xff; | |
1355 tmpbuf[1] = 0xfe; | |
1356 mch_memmove(tmpbuf + 2, string, (size_t)length); | |
1357 vim_free(string); | |
1358 string = tmpbuf; | |
1359 length += 2; | |
1360 | |
1361 selection_data->type = selection_data->target; | |
1362 selection_data->format = 16; /* 16 bits per char */ | |
1363 gtk_selection_data_set(selection_data, html_atom, 16, | |
1364 string, length); | |
1365 vim_free(string); | |
1366 } | |
1367 return; | |
1368 } | |
7 | 1369 else if (info == (guint)TARGET_VIMENC) |
1370 { | |
1371 int l = STRLEN(p_enc); | |
1372 | |
1373 /* contents: motion_type 'encoding' NUL text */ | |
1374 tmpbuf = alloc((unsigned)length + l + 2); | |
1375 if (tmpbuf != NULL) | |
1376 { | |
1377 tmpbuf[0] = motion_type; | |
1378 STRCPY(tmpbuf + 1, p_enc); | |
1379 mch_memmove(tmpbuf + l + 2, string, (size_t)length); | |
1380 } | |
1381 length += l + 2; | |
1382 vim_free(string); | |
1383 string = tmpbuf; | |
1384 type = vimenc_atom; | |
1385 } | |
2275
e4d849f4df03
Remove the old and not well supported GTK 1 code. (James Vega)
Bram Moolenaar <bram@vim.org>
parents:
2270
diff
changeset
|
1386 |
7 | 1387 /* gtk_selection_data_set_text() handles everything for us. This is |
1388 * so easy and simple and cool, it'd be insane not to use it. */ | |
1389 else | |
1390 { | |
1391 if (output_conv.vc_type != CONV_NONE) | |
1392 { | |
1393 tmpbuf = string_convert(&output_conv, string, &length); | |
1394 vim_free(string); | |
1395 if (tmpbuf == NULL) | |
1396 return; | |
1397 string = tmpbuf; | |
1398 } | |
1399 /* Validate the string to avoid runtime warnings */ | |
1400 if (g_utf8_validate((const char *)string, (gssize)length, NULL)) | |
1401 { | |
1402 gtk_selection_data_set_text(selection_data, | |
1403 (const char *)string, length); | |
1404 } | |
1405 vim_free(string); | |
1406 return; | |
1407 } | |
1408 | |
1409 if (string != NULL) | |
1410 { | |
1411 selection_data->type = selection_data->target; | |
1412 selection_data->format = 8; /* 8 bits per char */ | |
1413 | |
1414 gtk_selection_data_set(selection_data, type, 8, string, length); | |
1415 vim_free(string); | |
1416 } | |
1417 } | |
1418 | |
1419 /* | |
3518 | 1420 * Check if the GUI can be started. Called before gvimrc is sourced and |
1421 * before fork(). | |
1422 * Return OK or FAIL. | |
1423 */ | |
1424 int | |
1425 gui_mch_early_init_check(void) | |
1426 { | |
1427 char_u *p; | |
1428 | |
1429 /* Guess that when $DISPLAY isn't set the GUI can't start. */ | |
1430 p = mch_getenv((char_u *)"DISPLAY"); | |
1431 if (p == NULL || *p == NUL) | |
1432 { | |
1433 gui.dying = TRUE; | |
1434 EMSG(_((char *)e_opendisp)); | |
1435 return FAIL; | |
1436 } | |
1437 return OK; | |
1438 } | |
1439 | |
1440 /* | |
1441 * Check if the GUI can be started. Called before gvimrc is sourced but after | |
1442 * fork(). | |
7 | 1443 * Return OK or FAIL. |
1444 */ | |
1445 int | |
1446 gui_mch_init_check(void) | |
1447 { | |
1448 #ifdef FEAT_GUI_GNOME | |
1449 if (gtk_socket_id == 0) | |
1450 using_gnome = 1; | |
1451 #endif | |
1452 | |
4474 | 1453 /* This defaults to argv[0], but we want it to match the name of the |
1454 * shipped gvim.desktop so that Vim's windows can be associated with this | |
1455 * file. */ | |
1456 g_set_prgname("gvim"); | |
1457 | |
7 | 1458 /* Don't use gtk_init() or gnome_init(), it exits on failure. */ |
1459 if (!gtk_init_check(&gui_argc, &gui_argv)) | |
1460 { | |
1461 gui.dying = TRUE; | |
1666 | 1462 EMSG(_((char *)e_opendisp)); |
7 | 1463 return FAIL; |
1464 } | |
1465 | |
1466 return OK; | |
1467 } | |
1468 | |
1469 | |
1470 /**************************************************************************** | |
1471 * Mouse handling callbacks | |
1472 */ | |
1473 | |
1474 | |
1475 static guint mouse_click_timer = 0; | |
1476 static int mouse_timed_out = TRUE; | |
1477 | |
1478 /* | |
1479 * Timer used to recognize multiple clicks of the mouse button | |
1480 */ | |
1481 static gint | |
1482 mouse_click_timer_cb(gpointer data) | |
1483 { | |
1484 /* we don't use this information currently */ | |
1485 int *timed_out = (int *) data; | |
1486 | |
1487 *timed_out = TRUE; | |
1488 return FALSE; /* don't happen again */ | |
1489 } | |
1490 | |
1491 static guint motion_repeat_timer = 0; | |
1492 static int motion_repeat_offset = FALSE; | |
1493 static gint motion_repeat_timer_cb(gpointer); | |
1494 | |
1495 static void | |
1496 process_motion_notify(int x, int y, GdkModifierType state) | |
1497 { | |
1498 int button; | |
1499 int_u vim_modifiers; | |
1500 | |
1501 button = (state & (GDK_BUTTON1_MASK | GDK_BUTTON2_MASK | | |
1502 GDK_BUTTON3_MASK | GDK_BUTTON4_MASK | | |
1503 GDK_BUTTON5_MASK)) | |
1504 ? MOUSE_DRAG : ' '; | |
1505 | |
1506 /* If our pointer is currently hidden, then we should show it. */ | |
1507 gui_mch_mousehide(FALSE); | |
1508 | |
1509 /* Just moving the rodent above the drawing area without any button | |
1510 * being pressed. */ | |
1511 if (button != MOUSE_DRAG) | |
1512 { | |
1513 gui_mouse_moved(x, y); | |
1514 return; | |
1515 } | |
1516 | |
1517 /* translate modifier coding between the main engine and GTK */ | |
179 | 1518 vim_modifiers = modifiers_gdk2mouse(state); |
7 | 1519 |
1226 | 1520 /* inform the editor engine about the occurrence of this event */ |
7 | 1521 gui_send_mouse_event(button, x, y, FALSE, vim_modifiers); |
1522 | |
1523 /* | |
1524 * Auto repeat timer handling. | |
1525 */ | |
1526 if (x < 0 || y < 0 | |
1527 || x >= gui.drawarea->allocation.width | |
1528 || y >= gui.drawarea->allocation.height) | |
1529 { | |
1530 | |
1531 int dx; | |
1532 int dy; | |
1533 int offshoot; | |
1534 int delay = 10; | |
1535 | |
1536 /* Calculate the maximal distance of the cursor from the drawing area. | |
1537 * (offshoot can't become negative here!). | |
1538 */ | |
1539 dx = x < 0 ? -x : x - gui.drawarea->allocation.width; | |
1540 dy = y < 0 ? -y : y - gui.drawarea->allocation.height; | |
1541 | |
1542 offshoot = dx > dy ? dx : dy; | |
1543 | |
1884 | 1544 /* Make a linearly decaying timer delay with a threshold of 5 at a |
7 | 1545 * distance of 127 pixels from the main window. |
1546 * | |
1547 * One could think endlessly about the most ergonomic variant here. | |
1548 * For example it could make sense to calculate the distance from the | |
1549 * drags start instead... | |
1550 * | |
1551 * Maybe a parabolic interpolation would suite us better here too... | |
1552 */ | |
1553 if (offshoot > 127) | |
1554 { | |
1555 /* 5 appears to be somehow near to my perceptual limits :-). */ | |
1556 delay = 5; | |
1557 } | |
1558 else | |
1559 { | |
1560 delay = (130 * (127 - offshoot)) / 127 + 5; | |
1561 } | |
1562 | |
1563 /* shoot again */ | |
1564 if (!motion_repeat_timer) | |
1565 motion_repeat_timer = gtk_timeout_add((guint32)delay, | |
1566 motion_repeat_timer_cb, NULL); | |
1567 } | |
1568 } | |
1569 | |
1570 /* | |
1571 * Timer used to recognize multiple clicks of the mouse button. | |
1572 */ | |
1573 static gint | |
1884 | 1574 motion_repeat_timer_cb(gpointer data UNUSED) |
7 | 1575 { |
1576 int x; | |
1577 int y; | |
1578 GdkModifierType state; | |
1579 | |
1580 gdk_window_get_pointer(gui.drawarea->window, &x, &y, &state); | |
1581 | |
1582 if (!(state & (GDK_BUTTON1_MASK | GDK_BUTTON2_MASK | | |
1583 GDK_BUTTON3_MASK | GDK_BUTTON4_MASK | | |
1584 GDK_BUTTON5_MASK))) | |
1585 { | |
1586 motion_repeat_timer = 0; | |
1587 return FALSE; | |
1588 } | |
1589 | |
1590 /* If there already is a mouse click in the input buffer, wait another | |
1591 * time (otherwise we would create a backlog of clicks) */ | |
1592 if (vim_used_in_input_buf() > 10) | |
1593 return TRUE; | |
1594 | |
1595 motion_repeat_timer = 0; | |
1596 | |
1597 /* | |
1598 * Fake a motion event. | |
1599 * Trick: Pretend the mouse moved to the next character on every other | |
1600 * event, otherwise drag events will be discarded, because they are still | |
1601 * in the same character. | |
1602 */ | |
1603 if (motion_repeat_offset) | |
1604 x += gui.char_width; | |
1605 | |
1606 motion_repeat_offset = !motion_repeat_offset; | |
1607 process_motion_notify(x, y, state); | |
1608 | |
1609 /* Don't happen again. We will get reinstalled in the synthetic event | |
1610 * if needed -- thus repeating should still work. */ | |
1611 return FALSE; | |
1612 } | |
1613 | |
1614 static gint | |
1884 | 1615 motion_notify_event(GtkWidget *widget, |
1616 GdkEventMotion *event, | |
1617 gpointer data UNUSED) | |
7 | 1618 { |
1619 if (event->is_hint) | |
1620 { | |
1621 int x; | |
1622 int y; | |
1623 GdkModifierType state; | |
1624 | |
1625 gdk_window_get_pointer(widget->window, &x, &y, &state); | |
1626 process_motion_notify(x, y, state); | |
1627 } | |
1628 else | |
1629 { | |
1630 process_motion_notify((int)event->x, (int)event->y, | |
1631 (GdkModifierType)event->state); | |
1632 } | |
1633 | |
1634 return TRUE; /* handled */ | |
1635 } | |
1636 | |
1637 | |
1638 /* | |
1639 * Mouse button handling. Note please that we are capturing multiple click's | |
1640 * by our own timeout mechanism instead of the one provided by GTK+ itself. | |
1641 * This is due to the way the generic VIM code is recognizing multiple clicks. | |
1642 */ | |
1643 static gint | |
1884 | 1644 button_press_event(GtkWidget *widget, |
1645 GdkEventButton *event, | |
1646 gpointer data UNUSED) | |
7 | 1647 { |
1648 int button; | |
1649 int repeated_click = FALSE; | |
1650 int x, y; | |
1651 int_u vim_modifiers; | |
1652 | |
2923 | 1653 gui.event_time = event->time; |
2270
917fff7bc09d
Fixes for time in clipboard request. Also fix ownership. (David Fries)
Bram Moolenaar <bram@vim.org>
parents:
2254
diff
changeset
|
1654 |
7 | 1655 /* Make sure we have focus now we've been selected */ |
1656 if (gtk_socket_id != 0 && !GTK_WIDGET_HAS_FOCUS(widget)) | |
1657 gtk_widget_grab_focus(widget); | |
1658 | |
1659 /* | |
1660 * Don't let additional events about multiple clicks send by GTK to us | |
1661 * after the initial button press event confuse us. | |
1662 */ | |
1663 if (event->type != GDK_BUTTON_PRESS) | |
1664 return FALSE; | |
1665 | |
1666 x = event->x; | |
1667 y = event->y; | |
1668 | |
1669 /* Handle multiple clicks */ | |
1670 if (!mouse_timed_out && mouse_click_timer) | |
1671 { | |
1672 gtk_timeout_remove(mouse_click_timer); | |
1673 mouse_click_timer = 0; | |
1674 repeated_click = TRUE; | |
1675 } | |
1676 | |
1677 mouse_timed_out = FALSE; | |
1678 mouse_click_timer = gtk_timeout_add((guint32)p_mouset, | |
1679 mouse_click_timer_cb, &mouse_timed_out); | |
1680 | |
1681 switch (event->button) | |
1682 { | |
1683 case 1: | |
1684 button = MOUSE_LEFT; | |
1685 break; | |
1686 case 2: | |
1687 button = MOUSE_MIDDLE; | |
1688 break; | |
1689 case 3: | |
1690 button = MOUSE_RIGHT; | |
1691 break; | |
1692 default: | |
1693 return FALSE; /* Unknown button */ | |
1694 } | |
1695 | |
1696 #ifdef FEAT_XIM | |
1697 /* cancel any preediting */ | |
1698 if (im_is_preediting()) | |
1699 xim_reset(); | |
1700 #endif | |
1701 | |
179 | 1702 vim_modifiers = modifiers_gdk2mouse(event->state); |
7 | 1703 |
1704 gui_send_mouse_event(button, x, y, repeated_click, vim_modifiers); | |
1705 | |
1706 return TRUE; | |
1707 } | |
1708 | |
1709 /* | |
2275
e4d849f4df03
Remove the old and not well supported GTK 1 code. (James Vega)
Bram Moolenaar <bram@vim.org>
parents:
2270
diff
changeset
|
1710 * GTK+ 2 abstracts scrolling via the GdkEventScroll. |
7 | 1711 */ |
1712 static gboolean | |
1884 | 1713 scroll_event(GtkWidget *widget, |
1714 GdkEventScroll *event, | |
1715 gpointer data UNUSED) | |
7 | 1716 { |
1717 int button; | |
179 | 1718 int_u vim_modifiers; |
7 | 1719 |
1720 if (gtk_socket_id != 0 && !GTK_WIDGET_HAS_FOCUS(widget)) | |
1721 gtk_widget_grab_focus(widget); | |
1722 | |
1723 switch (event->direction) | |
1724 { | |
1725 case GDK_SCROLL_UP: | |
1726 button = MOUSE_4; | |
1727 break; | |
1728 case GDK_SCROLL_DOWN: | |
1729 button = MOUSE_5; | |
1730 break; | |
2409
0ca06a92adfb
Add support for horizontal scroll wheel. (Bjorn Winckler)
Bram Moolenaar <bram@vim.org>
parents:
2311
diff
changeset
|
1731 case GDK_SCROLL_LEFT: |
0ca06a92adfb
Add support for horizontal scroll wheel. (Bjorn Winckler)
Bram Moolenaar <bram@vim.org>
parents:
2311
diff
changeset
|
1732 button = MOUSE_7; |
0ca06a92adfb
Add support for horizontal scroll wheel. (Bjorn Winckler)
Bram Moolenaar <bram@vim.org>
parents:
2311
diff
changeset
|
1733 break; |
0ca06a92adfb
Add support for horizontal scroll wheel. (Bjorn Winckler)
Bram Moolenaar <bram@vim.org>
parents:
2311
diff
changeset
|
1734 case GDK_SCROLL_RIGHT: |
0ca06a92adfb
Add support for horizontal scroll wheel. (Bjorn Winckler)
Bram Moolenaar <bram@vim.org>
parents:
2311
diff
changeset
|
1735 button = MOUSE_6; |
0ca06a92adfb
Add support for horizontal scroll wheel. (Bjorn Winckler)
Bram Moolenaar <bram@vim.org>
parents:
2311
diff
changeset
|
1736 break; |
0ca06a92adfb
Add support for horizontal scroll wheel. (Bjorn Winckler)
Bram Moolenaar <bram@vim.org>
parents:
2311
diff
changeset
|
1737 default: /* This shouldn't happen */ |
7 | 1738 return FALSE; |
1739 } | |
1740 | |
1741 # ifdef FEAT_XIM | |
1742 /* cancel any preediting */ | |
1743 if (im_is_preediting()) | |
1744 xim_reset(); | |
1745 # endif | |
1746 | |
179 | 1747 vim_modifiers = modifiers_gdk2mouse(event->state); |
7 | 1748 |
1749 gui_send_mouse_event(button, (int)event->x, (int)event->y, | |
1750 FALSE, vim_modifiers); | |
1751 | |
1752 return TRUE; | |
1753 } | |
1754 | |
1755 | |
1756 static gint | |
1884 | 1757 button_release_event(GtkWidget *widget UNUSED, |
1758 GdkEventButton *event, | |
1759 gpointer data UNUSED) | |
7 | 1760 { |
1761 int x, y; | |
1762 int_u vim_modifiers; | |
1763 | |
2923 | 1764 gui.event_time = event->time; |
2270
917fff7bc09d
Fixes for time in clipboard request. Also fix ownership. (David Fries)
Bram Moolenaar <bram@vim.org>
parents:
2254
diff
changeset
|
1765 |
7 | 1766 /* Remove any motion "machine gun" timers used for automatic further |
1767 extension of allocation areas if outside of the applications window | |
1768 area .*/ | |
1769 if (motion_repeat_timer) | |
1770 { | |
1771 gtk_timeout_remove(motion_repeat_timer); | |
1772 motion_repeat_timer = 0; | |
1773 } | |
1774 | |
1775 x = event->x; | |
1776 y = event->y; | |
1777 | |
179 | 1778 vim_modifiers = modifiers_gdk2mouse(event->state); |
7 | 1779 |
1780 gui_send_mouse_event(MOUSE_RELEASE, x, y, FALSE, vim_modifiers); | |
1781 | |
1782 return TRUE; | |
1783 } | |
1784 | |
1785 | |
1786 #ifdef FEAT_DND | |
1787 /**************************************************************************** | |
1788 * Drag aNd Drop support handlers. | |
1789 */ | |
1790 | |
1791 /* | |
1792 * Count how many items there may be and separate them with a NUL. | |
1793 * Apparently the items are separated with \r\n. This is not documented, | |
1794 * thus be careful not to go past the end. Also allow separation with | |
1795 * NUL characters. | |
1796 */ | |
1797 static int | |
1798 count_and_decode_uri_list(char_u *out, char_u *raw, int len) | |
1799 { | |
1800 int i; | |
1801 char_u *p = out; | |
1802 int count = 0; | |
1803 | |
1804 for (i = 0; i < len; ++i) | |
1805 { | |
1806 if (raw[i] == NUL || raw[i] == '\n' || raw[i] == '\r') | |
1807 { | |
1808 if (p > out && p[-1] != NUL) | |
1809 { | |
1810 ++count; | |
1811 *p++ = NUL; | |
1812 } | |
1813 } | |
1814 else if (raw[i] == '%' && i + 2 < len && hexhex2nr(raw + i + 1) > 0) | |
1815 { | |
1816 *p++ = hexhex2nr(raw + i + 1); | |
1817 i += 2; | |
1818 } | |
1819 else | |
1820 *p++ = raw[i]; | |
1821 } | |
1822 if (p > out && p[-1] != NUL) | |
1823 { | |
1824 *p = NUL; /* last item didn't have \r or \n */ | |
1825 ++count; | |
1826 } | |
1827 return count; | |
1828 } | |
1829 | |
1830 /* | |
1831 * Parse NUL separated "src" strings. Make it an array "outlist" form. On | |
1832 * this process, URI which protocol is not "file:" are removed. Return | |
1833 * length of array (less than "max"). | |
1834 */ | |
1835 static int | |
1836 filter_uri_list(char_u **outlist, int max, char_u *src) | |
1837 { | |
1838 int i, j; | |
1839 | |
1840 for (i = j = 0; i < max; ++i) | |
1841 { | |
1842 outlist[i] = NULL; | |
1843 if (STRNCMP(src, "file:", 5) == 0) | |
1844 { | |
1845 src += 5; | |
1846 if (STRNCMP(src, "//localhost", 11) == 0) | |
1847 src += 11; | |
1848 while (src[0] == '/' && src[1] == '/') | |
1849 ++src; | |
1850 outlist[j++] = vim_strsave(src); | |
1851 } | |
1852 src += STRLEN(src) + 1; | |
1853 } | |
1854 return j; | |
1855 } | |
1856 | |
1857 static char_u ** | |
1858 parse_uri_list(int *count, char_u *data, int len) | |
1859 { | |
1860 int n = 0; | |
1861 char_u *tmp = NULL; | |
1862 char_u **array = NULL;; | |
1863 | |
1864 if (data != NULL && len > 0 && (tmp = (char_u *)alloc(len + 1)) != NULL) | |
1865 { | |
1866 n = count_and_decode_uri_list(tmp, data, len); | |
1867 if (n > 0 && (array = (char_u **)alloc(n * sizeof(char_u *))) != NULL) | |
1868 n = filter_uri_list(array, n, tmp); | |
1869 } | |
1870 vim_free(tmp); | |
1871 *count = n; | |
1872 return array; | |
1873 } | |
1874 | |
1875 static void | |
1876 drag_handle_uri_list(GdkDragContext *context, | |
1877 GtkSelectionData *data, | |
1878 guint time_, | |
1879 GdkModifierType state, | |
1880 gint x, | |
1881 gint y) | |
1882 { | |
1883 char_u **fnames; | |
1884 int nfiles = 0; | |
1885 | |
1886 fnames = parse_uri_list(&nfiles, data->data, data->length); | |
1887 | |
1888 if (fnames != NULL && nfiles > 0) | |
1889 { | |
179 | 1890 int_u modifiers; |
7 | 1891 |
1892 gtk_drag_finish(context, TRUE, FALSE, time_); /* accept */ | |
1893 | |
179 | 1894 modifiers = modifiers_gdk2mouse(state); |
7 | 1895 |
1896 gui_handle_drop(x, y, modifiers, fnames, nfiles); | |
1897 } | |
840 | 1898 else |
1899 vim_free(fnames); | |
7 | 1900 } |
1901 | |
1902 static void | |
1903 drag_handle_text(GdkDragContext *context, | |
1904 GtkSelectionData *data, | |
1905 guint time_, | |
1906 GdkModifierType state) | |
1907 { | |
1908 char_u dropkey[6] = {CSI, KS_MODIFIER, 0, CSI, KS_EXTRA, (char_u)KE_DROP}; | |
1909 char_u *text; | |
1910 int len; | |
1911 char_u *tmpbuf = NULL; | |
1912 | |
1913 text = data->data; | |
1914 len = data->length; | |
1915 | |
1916 if (data->type == utf8_string_atom) | |
1917 { | |
1918 if (input_conv.vc_type != CONV_NONE) | |
1919 tmpbuf = string_convert(&input_conv, text, &len); | |
1920 if (tmpbuf != NULL) | |
1921 text = tmpbuf; | |
1922 } | |
1923 | |
1924 dnd_yank_drag_data(text, (long)len); | |
1925 gtk_drag_finish(context, TRUE, FALSE, time_); /* accept */ | |
1926 vim_free(tmpbuf); | |
1927 | |
179 | 1928 dropkey[2] = modifiers_gdk2vim(state); |
7 | 1929 |
1930 if (dropkey[2] != 0) | |
1931 add_to_input_buf(dropkey, (int)sizeof(dropkey)); | |
1932 else | |
1933 add_to_input_buf(dropkey + 3, (int)(sizeof(dropkey) - 3)); | |
1934 } | |
1935 | |
1936 /* | |
1937 * DND receiver. | |
1938 */ | |
1939 static void | |
1940 drag_data_received_cb(GtkWidget *widget, | |
1941 GdkDragContext *context, | |
1942 gint x, | |
1943 gint y, | |
1944 GtkSelectionData *data, | |
1945 guint info, | |
1946 guint time_, | |
1884 | 1947 gpointer user_data UNUSED) |
7 | 1948 { |
1949 GdkModifierType state; | |
1950 | |
1951 /* Guard against trash */ | |
1952 if (data->data == NULL | |
1953 || data->length <= 0 | |
1954 || data->format != 8 | |
1955 || data->data[data->length] != '\0') | |
1956 { | |
1957 gtk_drag_finish(context, FALSE, FALSE, time_); | |
1958 return; | |
1959 } | |
1960 | |
1961 /* Get the current modifier state for proper distinguishment between | |
1962 * different operations later. */ | |
1963 gdk_window_get_pointer(widget->window, NULL, NULL, &state); | |
1964 | |
1965 /* Not sure about the role of "text/plain" here... */ | |
1966 if (info == (guint)TARGET_TEXT_URI_LIST) | |
1967 drag_handle_uri_list(context, data, time_, state, x, y); | |
1968 else | |
1969 drag_handle_text(context, data, time_, state); | |
1970 | |
1971 } | |
1972 #endif /* FEAT_DND */ | |
1973 | |
1974 | |
1975 #if defined(FEAT_GUI_GNOME) && defined(FEAT_SESSION) | |
1976 /* | |
1977 * GnomeClient interact callback. Check for unsaved buffers that cannot | |
1978 * be abandoned and pop up a dialog asking the user for confirmation if | |
1979 * necessary. | |
1980 */ | |
1981 static void | |
2188
722e390945d7
Avoid warnings for unused arguments when compiling with Gnome.
Bram Moolenaar <bram@vim.org>
parents:
2154
diff
changeset
|
1982 sm_client_check_changed_any(GnomeClient *client UNUSED, |
7 | 1983 gint key, |
2188
722e390945d7
Avoid warnings for unused arguments when compiling with Gnome.
Bram Moolenaar <bram@vim.org>
parents:
2154
diff
changeset
|
1984 GnomeDialogType type UNUSED, |
722e390945d7
Avoid warnings for unused arguments when compiling with Gnome.
Bram Moolenaar <bram@vim.org>
parents:
2154
diff
changeset
|
1985 gpointer data UNUSED) |
7 | 1986 { |
1987 cmdmod_T save_cmdmod; | |
1988 gboolean shutdown_cancelled; | |
1989 | |
1990 save_cmdmod = cmdmod; | |
1991 | |
1992 # ifdef FEAT_BROWSE | |
1993 cmdmod.browse = TRUE; | |
1994 # endif | |
1995 # if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) | |
1996 cmdmod.confirm = TRUE; | |
1997 # endif | |
1998 /* | |
1999 * If there are changed buffers, present the user with | |
2000 * a dialog if possible, otherwise give an error message. | |
2001 */ | |
2002 shutdown_cancelled = check_changed_any(FALSE); | |
2003 | |
2004 exiting = FALSE; | |
2005 cmdmod = save_cmdmod; | |
2006 setcursor(); /* position the cursor */ | |
2007 out_flush(); | |
2008 /* | |
2009 * If the user hit the [Cancel] button the whole shutdown | |
2010 * will be cancelled. Wow, quite powerful feature (: | |
2011 */ | |
2012 gnome_interaction_key_return(key, shutdown_cancelled); | |
2013 } | |
2014 | |
2015 /* | |
2016 * Generate a script that can be used to restore the current editing session. | |
2017 * Save the value of v:this_session before running :mksession in order to make | |
2018 * automagic session save fully transparent. Return TRUE on success. | |
2019 */ | |
2020 static int | |
2021 write_session_file(char_u *filename) | |
2022 { | |
2023 char_u *escaped_filename; | |
2024 char *mksession_cmdline; | |
2025 unsigned int save_ssop_flags; | |
2026 int failed; | |
2027 | |
2028 /* | |
2029 * Build an ex command line to create a script that restores the current | |
2030 * session if executed. Escape the filename to avoid nasty surprises. | |
2031 */ | |
2032 escaped_filename = vim_strsave_escaped(filename, escape_chars); | |
2033 if (escaped_filename == NULL) | |
2034 return FALSE; | |
1296 | 2035 mksession_cmdline = g_strconcat("mksession ", (char *)escaped_filename, |
2036 NULL); | |
7 | 2037 vim_free(escaped_filename); |
1296 | 2038 |
7 | 2039 /* |
2040 * Use a reasonable hardcoded set of 'sessionoptions' flags to avoid | |
2041 * unpredictable effects when the session is saved automatically. Also, | |
2042 * we definitely need SSOP_GLOBALS to be able to restore v:this_session. | |
2043 * Don't use SSOP_BUFFERS to prevent the buffer list from becoming | |
2044 * enormously large if the GNOME session feature is used regularly. | |
2045 */ | |
2046 save_ssop_flags = ssop_flags; | |
2047 ssop_flags = (SSOP_BLANK|SSOP_CURDIR|SSOP_FOLDS|SSOP_GLOBALS | |
1296 | 2048 |SSOP_HELP|SSOP_OPTIONS|SSOP_WINSIZE|SSOP_TABPAGES); |
7 | 2049 |
2050 do_cmdline_cmd((char_u *)"let Save_VV_this_session = v:this_session"); | |
2051 failed = (do_cmdline_cmd((char_u *)mksession_cmdline) == FAIL); | |
2052 do_cmdline_cmd((char_u *)"let v:this_session = Save_VV_this_session"); | |
150 | 2053 do_unlet((char_u *)"Save_VV_this_session", TRUE); |
7 | 2054 |
2055 ssop_flags = save_ssop_flags; | |
2056 g_free(mksession_cmdline); | |
5905 | 2057 |
7 | 2058 /* |
2059 * Reopen the file and append a command to restore v:this_session, | |
2060 * as if this save never happened. This is to avoid conflicts with | |
2061 * the user's own sessions. FIXME: It's probably less hackish to add | |
2062 * a "stealth" flag to 'sessionoptions' -- gotta ask Bram. | |
2063 */ | |
2064 if (!failed) | |
2065 { | |
2066 FILE *fd; | |
2067 | |
2068 fd = open_exfile(filename, TRUE, APPENDBIN); | |
2069 | |
2070 failed = (fd == NULL | |
2071 || put_line(fd, "let v:this_session = Save_VV_this_session") == FAIL | |
2072 || put_line(fd, "unlet Save_VV_this_session") == FAIL); | |
2073 | |
2074 if (fd != NULL && fclose(fd) != 0) | |
2075 failed = TRUE; | |
2076 | |
2077 if (failed) | |
2078 mch_remove(filename); | |
2079 } | |
2080 | |
2081 return !failed; | |
2082 } | |
2083 | |
2084 /* | |
2085 * "save_yourself" signal handler. Initiate an interaction to ask the user | |
2086 * for confirmation if necessary. Save the current editing session and tell | |
2087 * the session manager how to restart Vim. | |
2088 */ | |
2089 static gboolean | |
2090 sm_client_save_yourself(GnomeClient *client, | |
2188
722e390945d7
Avoid warnings for unused arguments when compiling with Gnome.
Bram Moolenaar <bram@vim.org>
parents:
2154
diff
changeset
|
2091 gint phase UNUSED, |
722e390945d7
Avoid warnings for unused arguments when compiling with Gnome.
Bram Moolenaar <bram@vim.org>
parents:
2154
diff
changeset
|
2092 GnomeSaveStyle save_style UNUSED, |
722e390945d7
Avoid warnings for unused arguments when compiling with Gnome.
Bram Moolenaar <bram@vim.org>
parents:
2154
diff
changeset
|
2093 gboolean shutdown UNUSED, |
7 | 2094 GnomeInteractStyle interact_style, |
2188
722e390945d7
Avoid warnings for unused arguments when compiling with Gnome.
Bram Moolenaar <bram@vim.org>
parents:
2154
diff
changeset
|
2095 gboolean fast UNUSED, |
722e390945d7
Avoid warnings for unused arguments when compiling with Gnome.
Bram Moolenaar <bram@vim.org>
parents:
2154
diff
changeset
|
2096 gpointer data UNUSED) |
7 | 2097 { |
2098 static const char suffix[] = "-session.vim"; | |
2099 char *session_file; | |
2100 unsigned int len; | |
2101 gboolean success; | |
2102 | |
2103 /* Always request an interaction if possible. check_changed_any() | |
2104 * won't actually show a dialog unless any buffers have been modified. | |
2105 * There doesn't seem to be an obvious way to check that without | |
2106 * automatically firing the dialog. Anyway, it works just fine. */ | |
2107 if (interact_style == GNOME_INTERACT_ANY) | |
2108 gnome_client_request_interaction(client, GNOME_DIALOG_NORMAL, | |
2109 &sm_client_check_changed_any, | |
2110 NULL); | |
2111 out_flush(); | |
2112 ml_sync_all(FALSE, FALSE); /* preserve all swap files */ | |
2113 | |
2114 /* The path is unique for each session save. We do neither know nor care | |
2115 * which session script will actually be used later. This decision is in | |
2116 * the domain of the session manager. */ | |
2117 session_file = gnome_config_get_real_path( | |
2118 gnome_client_get_config_prefix(client)); | |
2119 len = strlen(session_file); | |
2120 | |
2121 if (len > 0 && session_file[len-1] == G_DIR_SEPARATOR) | |
2122 --len; /* get rid of the superfluous trailing '/' */ | |
2123 | |
2124 session_file = g_renew(char, session_file, len + sizeof(suffix)); | |
2125 memcpy(session_file + len, suffix, sizeof(suffix)); | |
2126 | |
2127 success = write_session_file((char_u *)session_file); | |
2128 | |
2129 if (success) | |
2130 { | |
2131 const char *argv[8]; | |
2132 int i; | |
2133 | |
2134 /* Tell the session manager how to wipe out the stored session data. | |
2135 * This isn't as dangerous as it looks, don't worry :) session_file | |
2136 * is a unique absolute filename. Usually it'll be something like | |
2137 * `/home/user/.gnome2/vim-XXXXXX-session.vim'. */ | |
2138 i = 0; | |
2139 argv[i++] = "rm"; | |
2140 argv[i++] = session_file; | |
2141 argv[i] = NULL; | |
2142 | |
2143 gnome_client_set_discard_command(client, i, (char **)argv); | |
2144 | |
2145 /* Tell the session manager how to restore the just saved session. | |
2146 * This is easily done thanks to Vim's -S option. Pass the -f flag | |
2147 * since there's no need to fork -- it might even cause confusion. | |
2148 * Also pass the window role to give the WM something to match on. | |
2149 * The role is set in gui_mch_open(), thus should _never_ be NULL. */ | |
2150 i = 0; | |
2151 argv[i++] = restart_command; | |
2152 argv[i++] = "-f"; | |
2153 argv[i++] = "-g"; | |
2154 argv[i++] = "--role"; | |
2155 argv[i++] = gtk_window_get_role(GTK_WINDOW(gui.mainwin)); | |
2156 argv[i++] = "-S"; | |
2157 argv[i++] = session_file; | |
2158 argv[i] = NULL; | |
2159 | |
2160 gnome_client_set_restart_command(client, i, (char **)argv); | |
2161 gnome_client_set_clone_command(client, 0, NULL); | |
2162 } | |
2163 | |
2164 g_free(session_file); | |
2165 | |
2166 return success; | |
2167 } | |
2168 | |
2169 /* | |
2170 * Called when the session manager wants us to die. There isn't much to save | |
2171 * here since "save_yourself" has been emitted before (unless serious trouble | |
2172 * is happening). | |
2173 */ | |
2174 static void | |
2188
722e390945d7
Avoid warnings for unused arguments when compiling with Gnome.
Bram Moolenaar <bram@vim.org>
parents:
2154
diff
changeset
|
2175 sm_client_die(GnomeClient *client UNUSED, gpointer data UNUSED) |
7 | 2176 { |
2177 /* Don't write messages to the GUI anymore */ | |
2178 full_screen = FALSE; | |
2179 | |
1666 | 2180 vim_strncpy(IObuff, (char_u *) |
419 | 2181 _("Vim: Received \"die\" request from session manager\n"), |
1666 | 2182 IOSIZE - 1); |
7 | 2183 preserve_exit(); |
2184 } | |
2185 | |
2186 /* | |
2187 * Connect our signal handlers to be notified on session save and shutdown. | |
2188 */ | |
2189 static void | |
2190 setup_save_yourself(void) | |
2191 { | |
2192 GnomeClient *client; | |
2193 | |
2194 client = gnome_master_client(); | |
2195 | |
2196 if (client != NULL) | |
2197 { | |
2198 /* Must use the deprecated gtk_signal_connect() for compatibility | |
2199 * with GNOME 1. Arrgh, zombies! */ | |
2200 gtk_signal_connect(GTK_OBJECT(client), "save_yourself", | |
2201 GTK_SIGNAL_FUNC(&sm_client_save_yourself), NULL); | |
2202 gtk_signal_connect(GTK_OBJECT(client), "die", | |
2203 GTK_SIGNAL_FUNC(&sm_client_die), NULL); | |
2204 } | |
2205 } | |
2206 | |
2207 #else /* !(FEAT_GUI_GNOME && FEAT_SESSION) */ | |
2208 | |
2209 # ifdef USE_XSMP | |
2210 /* | |
2211 * GTK tells us that XSMP needs attention | |
2212 */ | |
2213 static gboolean | |
2214 local_xsmp_handle_requests(source, condition, data) | |
1884 | 2215 GIOChannel *source UNUSED; |
7 | 2216 GIOCondition condition; |
2217 gpointer data; | |
2218 { | |
2219 if (condition == G_IO_IN) | |
2220 { | |
2221 /* Do stuff; maybe close connection */ | |
2222 if (xsmp_handle_requests() == FAIL) | |
2223 g_io_channel_unref((GIOChannel *)data); | |
2224 return TRUE; | |
2225 } | |
2226 /* Error */ | |
2227 g_io_channel_unref((GIOChannel *)data); | |
2228 xsmp_close(); | |
2229 return TRUE; | |
2230 } | |
2231 # endif /* USE_XSMP */ | |
2232 | |
2233 /* | |
2234 * Setup the WM_PROTOCOLS to indicate we want the WM_SAVE_YOURSELF event. | |
2235 * This is an ugly use of X functions. GTK doesn't offer an alternative. | |
2236 */ | |
2237 static void | |
2238 setup_save_yourself(void) | |
2239 { | |
2240 Atom *existing_atoms = NULL; | |
2241 int count = 0; | |
2242 | |
2243 #ifdef USE_XSMP | |
2244 if (xsmp_icefd != -1) | |
2245 { | |
2246 /* | |
2247 * Use XSMP is preference to legacy WM_SAVE_YOURSELF; | |
2248 * set up GTK IO monitor | |
2249 */ | |
2250 GIOChannel *g_io = g_io_channel_unix_new(xsmp_icefd); | |
2251 | |
2252 g_io_add_watch(g_io, G_IO_IN | G_IO_ERR | G_IO_HUP, | |
2253 local_xsmp_handle_requests, (gpointer)g_io); | |
2254 } | |
2255 else | |
2256 #endif | |
2257 { | |
2258 /* Fall back to old method */ | |
2259 | |
2260 /* first get the existing value */ | |
2261 if (XGetWMProtocols(GDK_WINDOW_XDISPLAY(gui.mainwin->window), | |
2262 GDK_WINDOW_XWINDOW(gui.mainwin->window), | |
2263 &existing_atoms, &count)) | |
2264 { | |
2265 Atom *new_atoms; | |
2266 Atom save_yourself_xatom; | |
2267 int i; | |
2268 | |
2269 save_yourself_xatom = GET_X_ATOM(save_yourself_atom); | |
2270 | |
2271 /* check if WM_SAVE_YOURSELF isn't there yet */ | |
2272 for (i = 0; i < count; ++i) | |
2273 if (existing_atoms[i] == save_yourself_xatom) | |
2274 break; | |
2275 | |
2276 if (i == count) | |
2277 { | |
2278 /* allocate an Atoms array which is one item longer */ | |
2279 new_atoms = (Atom *)alloc((unsigned)((count + 1) | |
2280 * sizeof(Atom))); | |
2281 if (new_atoms != NULL) | |
2282 { | |
2283 memcpy(new_atoms, existing_atoms, count * sizeof(Atom)); | |
2284 new_atoms[count] = save_yourself_xatom; | |
2285 XSetWMProtocols(GDK_WINDOW_XDISPLAY(gui.mainwin->window), | |
2286 GDK_WINDOW_XWINDOW(gui.mainwin->window), | |
2287 new_atoms, count + 1); | |
2288 vim_free(new_atoms); | |
2289 } | |
2290 } | |
2291 XFree(existing_atoms); | |
2292 } | |
2293 } | |
2294 } | |
2295 | |
2296 /* | |
2297 * Installing a global event filter seems to be the only way to catch | |
2298 * client messages of type WM_PROTOCOLS without overriding GDK's own | |
2299 * client message event filter. Well, that's still better than trying | |
2300 * to guess what the GDK filter had done if it had been invoked instead | |
2301 * | |
2302 * GTK2_FIXME: This doesn't seem to work. For some reason we never | |
2303 * receive WM_SAVE_YOURSELF even though everything is set up correctly. | |
2304 * I have the nasty feeling modern session managers just don't send this | |
2305 * deprecated message anymore. Addition: confirmed by several people. | |
2306 * | |
2307 * The GNOME session support is much cooler anyway. Unlike this ugly | |
2308 * WM_SAVE_YOURSELF hack it actually stores the session... And yes, | |
2309 * it should work with KDE as well. | |
2310 */ | |
2311 static GdkFilterReturn | |
1884 | 2312 global_event_filter(GdkXEvent *xev, |
2313 GdkEvent *event UNUSED, | |
2314 gpointer data UNUSED) | |
7 | 2315 { |
2316 XEvent *xevent = (XEvent *)xev; | |
2317 | |
2318 if (xevent != NULL | |
2319 && xevent->type == ClientMessage | |
2320 && xevent->xclient.message_type == GET_X_ATOM(wm_protocols_atom) | |
1884 | 2321 && (long_u)xevent->xclient.data.l[0] |
2322 == GET_X_ATOM(save_yourself_atom)) | |
7 | 2323 { |
2324 out_flush(); | |
2325 ml_sync_all(FALSE, FALSE); /* preserve all swap files */ | |
2326 /* | |
2327 * Set the window's WM_COMMAND property, to let the window manager | |
2328 * know we are done saving ourselves. We don't want to be | |
2329 * restarted, thus set argv to NULL. | |
2330 */ | |
2331 XSetCommand(GDK_WINDOW_XDISPLAY(gui.mainwin->window), | |
2332 GDK_WINDOW_XWINDOW(gui.mainwin->window), | |
2333 NULL, 0); | |
2334 return GDK_FILTER_REMOVE; | |
2335 } | |
2336 | |
2337 return GDK_FILTER_CONTINUE; | |
2338 } | |
2339 #endif /* !(FEAT_GUI_GNOME && FEAT_SESSION) */ | |
2340 | |
2341 | |
2342 /* | |
2343 * Setup the window icon & xcmdsrv comm after the main window has been realized. | |
2344 */ | |
2345 static void | |
1884 | 2346 mainwin_realize(GtkWidget *widget UNUSED, gpointer data UNUSED) |
7 | 2347 { |
2348 /* If you get an error message here, you still need to unpack the runtime | |
2349 * archive! */ | |
2350 #ifdef magick | |
2351 # undef magick | |
2352 #endif | |
2353 /* A bit hackish, but avoids casting later and allows optimization */ | |
2354 # define static static const | |
2355 #define magick vim32x32 | |
2356 #include "../runtime/vim32x32.xpm" | |
2357 #undef magick | |
2358 #define magick vim16x16 | |
2359 #include "../runtime/vim16x16.xpm" | |
2360 #undef magick | |
2361 #define magick vim48x48 | |
2362 #include "../runtime/vim48x48.xpm" | |
2363 #undef magick | |
2364 # undef static | |
2365 | |
2366 /* When started with "--echo-wid" argument, write window ID on stdout. */ | |
2367 if (echo_wid_arg) | |
2368 { | |
2369 printf("WID: %ld\n", (long)GDK_WINDOW_XWINDOW(gui.mainwin->window)); | |
2370 fflush(stdout); | |
2371 } | |
2372 | |
2373 if (vim_strchr(p_go, GO_ICON) != NULL) | |
2374 { | |
2375 /* | |
2376 * Add an icon to the main window. For fun and convenience of the user. | |
2377 */ | |
2378 GList *icons = NULL; | |
2379 | |
2380 icons = g_list_prepend(icons, gdk_pixbuf_new_from_xpm_data(vim16x16)); | |
2381 icons = g_list_prepend(icons, gdk_pixbuf_new_from_xpm_data(vim32x32)); | |
2382 icons = g_list_prepend(icons, gdk_pixbuf_new_from_xpm_data(vim48x48)); | |
2383 | |
2384 gtk_window_set_icon_list(GTK_WINDOW(gui.mainwin), icons); | |
2385 | |
2386 g_list_foreach(icons, (GFunc)&g_object_unref, NULL); | |
2387 g_list_free(icons); | |
2388 } | |
2389 | |
2390 #if !(defined(FEAT_GUI_GNOME) && defined(FEAT_SESSION)) | |
2391 /* Register a handler for WM_SAVE_YOURSELF with GDK's low-level X I/F */ | |
2392 gdk_window_add_filter(NULL, &global_event_filter, NULL); | |
2393 #endif | |
2394 /* Setup to indicate to the window manager that we want to catch the | |
2395 * WM_SAVE_YOURSELF event. For GNOME, this connects to the session | |
2396 * manager instead. */ | |
2397 #if defined(FEAT_GUI_GNOME) && defined(FEAT_SESSION) | |
2398 if (using_gnome) | |
2399 #endif | |
2400 setup_save_yourself(); | |
2401 | |
2402 #ifdef FEAT_CLIENTSERVER | |
2403 if (serverName == NULL && serverDelayedStartName != NULL) | |
2404 { | |
2405 /* This is a :gui command in a plain vim with no previous server */ | |
2406 commWindow = GDK_WINDOW_XWINDOW(gui.mainwin->window); | |
2407 | |
2408 (void)serverRegisterName(GDK_WINDOW_XDISPLAY(gui.mainwin->window), | |
2409 serverDelayedStartName); | |
2410 } | |
2411 else | |
2412 { | |
2413 /* | |
2414 * Cannot handle "XLib-only" windows with gtk event routines, we'll | |
2415 * have to change the "server" registration to that of the main window | |
2416 * If we have not registered a name yet, remember the window | |
2417 */ | |
2418 serverChangeRegisteredWindow(GDK_WINDOW_XDISPLAY(gui.mainwin->window), | |
2419 GDK_WINDOW_XWINDOW(gui.mainwin->window)); | |
2420 } | |
2421 gtk_widget_add_events(gui.mainwin, GDK_PROPERTY_CHANGE_MASK); | |
2422 gtk_signal_connect(GTK_OBJECT(gui.mainwin), "property_notify_event", | |
2423 GTK_SIGNAL_FUNC(property_event), NULL); | |
2424 #endif | |
2425 } | |
2426 | |
2427 static GdkCursor * | |
2428 create_blank_pointer(void) | |
2429 { | |
2430 GdkWindow *root_window = NULL; | |
2431 GdkPixmap *blank_mask; | |
2432 GdkCursor *cursor; | |
2433 GdkColor color = { 0, 0, 0, 0 }; | |
2434 char blank_data[] = { 0x0 }; | |
2435 | |
2436 #ifdef HAVE_GTK_MULTIHEAD | |
2437 root_window = gtk_widget_get_root_window(gui.mainwin); | |
2438 #endif | |
2439 | |
2440 /* Create a pseudo blank pointer, which is in fact one pixel by one pixel | |
2441 * in size. */ | |
2442 blank_mask = gdk_bitmap_create_from_data(root_window, blank_data, 1, 1); | |
2443 cursor = gdk_cursor_new_from_pixmap(blank_mask, blank_mask, | |
2444 &color, &color, 0, 0); | |
2445 gdk_bitmap_unref(blank_mask); | |
2446 | |
2447 return cursor; | |
2448 } | |
2449 | |
2450 #ifdef HAVE_GTK_MULTIHEAD | |
2451 static void | |
2452 mainwin_screen_changed_cb(GtkWidget *widget, | |
1884 | 2453 GdkScreen *previous_screen UNUSED, |
2454 gpointer data UNUSED) | |
7 | 2455 { |
2456 if (!gtk_widget_has_screen(widget)) | |
2457 return; | |
2458 | |
2459 /* | |
1226 | 2460 * Recreate the invisible mouse cursor. |
7 | 2461 */ |
2462 if (gui.blank_pointer != NULL) | |
2463 gdk_cursor_unref(gui.blank_pointer); | |
2464 | |
2465 gui.blank_pointer = create_blank_pointer(); | |
2466 | |
2467 if (gui.pointer_hidden && gui.drawarea->window != NULL) | |
2468 gdk_window_set_cursor(gui.drawarea->window, gui.blank_pointer); | |
2469 | |
2470 /* | |
2471 * Create a new PangoContext for this screen, and initialize it | |
2472 * with the current font if necessary. | |
2473 */ | |
2474 if (gui.text_context != NULL) | |
2475 g_object_unref(gui.text_context); | |
2476 | |
2477 gui.text_context = gtk_widget_create_pango_context(widget); | |
2478 pango_context_set_base_dir(gui.text_context, PANGO_DIRECTION_LTR); | |
2479 | |
2480 if (gui.norm_font != NULL) | |
2481 { | |
38 | 2482 gui_mch_init_font(p_guifont, FALSE); |
814 | 2483 gui_set_shellsize(FALSE, FALSE, RESIZE_BOTH); |
7 | 2484 } |
2485 } | |
2486 #endif /* HAVE_GTK_MULTIHEAD */ | |
2487 | |
2488 /* | |
2489 * After the drawing area comes up, we calculate all colors and create the | |
2490 * dummy blank cursor. | |
2491 * | |
2492 * Don't try to set any VIM scrollbar sizes anywhere here. I'm relying on the | |
2493 * fact that the main VIM engine doesn't take them into account anywhere. | |
2494 */ | |
2495 static void | |
1884 | 2496 drawarea_realize_cb(GtkWidget *widget, gpointer data UNUSED) |
7 | 2497 { |
2498 GtkWidget *sbar; | |
2499 | |
2500 #ifdef FEAT_XIM | |
2501 xim_init(); | |
2502 #endif | |
2503 gui_mch_new_colors(); | |
2504 gui.text_gc = gdk_gc_new(gui.drawarea->window); | |
2505 | |
2506 gui.blank_pointer = create_blank_pointer(); | |
2507 if (gui.pointer_hidden) | |
2508 gdk_window_set_cursor(widget->window, gui.blank_pointer); | |
2509 | |
2510 /* get the actual size of the scrollbars, if they are realized */ | |
2511 sbar = firstwin->w_scrollbars[SBAR_LEFT].id; | |
2512 if (!sbar || (!gui.which_scrollbars[SBAR_LEFT] | |
2513 && firstwin->w_scrollbars[SBAR_RIGHT].id)) | |
2514 sbar = firstwin->w_scrollbars[SBAR_RIGHT].id; | |
2515 if (sbar && GTK_WIDGET_REALIZED(sbar) && sbar->allocation.width) | |
2516 gui.scrollbar_width = sbar->allocation.width; | |
2517 | |
2518 sbar = gui.bottom_sbar.id; | |
2519 if (sbar && GTK_WIDGET_REALIZED(sbar) && sbar->allocation.height) | |
2520 gui.scrollbar_height = sbar->allocation.height; | |
2521 } | |
2522 | |
2523 /* | |
2524 * Properly clean up on shutdown. | |
2525 */ | |
2526 static void | |
1884 | 2527 drawarea_unrealize_cb(GtkWidget *widget UNUSED, gpointer data UNUSED) |
7 | 2528 { |
2529 /* Don't write messages to the GUI anymore */ | |
2530 full_screen = FALSE; | |
2531 | |
2532 #ifdef FEAT_XIM | |
2533 im_shutdown(); | |
2534 #endif | |
2535 if (gui.ascii_glyphs != NULL) | |
2536 { | |
2537 pango_glyph_string_free(gui.ascii_glyphs); | |
2538 gui.ascii_glyphs = NULL; | |
2539 } | |
2540 if (gui.ascii_font != NULL) | |
2541 { | |
2542 g_object_unref(gui.ascii_font); | |
2543 gui.ascii_font = NULL; | |
2544 } | |
2545 g_object_unref(gui.text_context); | |
2546 gui.text_context = NULL; | |
2547 | |
2548 g_object_unref(gui.text_gc); | |
2549 gui.text_gc = NULL; | |
2550 | |
2551 gdk_cursor_unref(gui.blank_pointer); | |
2552 gui.blank_pointer = NULL; | |
2553 } | |
2554 | |
2555 static void | |
1884 | 2556 drawarea_style_set_cb(GtkWidget *widget UNUSED, |
2557 GtkStyle *previous_style UNUSED, | |
2558 gpointer data UNUSED) | |
7 | 2559 { |
2560 gui_mch_new_colors(); | |
2561 } | |
2562 | |
2563 /* | |
2564 * Callback routine for the "delete_event" signal on the toplevel window. | |
2565 * Tries to vim gracefully, or refuses to exit with changed buffers. | |
2566 */ | |
2567 static gint | |
1884 | 2568 delete_event_cb(GtkWidget *widget UNUSED, |
2569 GdkEventAny *event UNUSED, | |
2570 gpointer data UNUSED) | |
7 | 2571 { |
2572 gui_shell_closed(); | |
2573 return TRUE; | |
2574 } | |
2575 | |
685 | 2576 #if defined(FEAT_MENU) || defined(FEAT_TOOLBAR) || defined(FEAT_GUI_TABLINE) |
2577 static int | |
2578 get_item_dimensions(GtkWidget *widget, GtkOrientation orientation) | |
2579 { | |
2580 GtkOrientation item_orientation = GTK_ORIENTATION_HORIZONTAL; | |
2581 | |
2582 #ifdef FEAT_GUI_GNOME | |
2583 if (using_gnome && widget != NULL) | |
2584 { | |
856 | 2585 GtkWidget *parent; |
685 | 2586 BonoboDockItem *dockitem; |
2587 | |
791 | 2588 parent = gtk_widget_get_parent(widget); |
856 | 2589 if (G_TYPE_FROM_INSTANCE(parent) == BONOBO_TYPE_DOCK_ITEM) |
2590 { | |
2591 /* Only menu & toolbar are dock items. Could tabline be? | |
2592 * Seem to be only the 2 defined in GNOME */ | |
2593 widget = parent; | |
2594 dockitem = BONOBO_DOCK_ITEM(widget); | |
2595 | |
2596 if (dockitem == NULL || dockitem->is_floating) | |
2597 return 0; | |
2598 item_orientation = bonobo_dock_item_get_orientation(dockitem); | |
2599 } | |
685 | 2600 } |
2601 #endif | |
2602 if (widget != NULL | |
2603 && item_orientation == orientation | |
2604 && GTK_WIDGET_REALIZED(widget) | |
2605 && GTK_WIDGET_VISIBLE(widget)) | |
2606 { | |
2607 if (orientation == GTK_ORIENTATION_HORIZONTAL) | |
2608 return widget->allocation.height; | |
2609 else | |
2610 return widget->allocation.width; | |
2611 } | |
2612 return 0; | |
2613 } | |
2614 #endif | |
2615 | |
2616 static int | |
2617 get_menu_tool_width(void) | |
2618 { | |
2619 int width = 0; | |
2620 | |
2621 #ifdef FEAT_GUI_GNOME /* these are never vertical without GNOME */ | |
2622 # ifdef FEAT_MENU | |
2623 width += get_item_dimensions(gui.menubar, GTK_ORIENTATION_VERTICAL); | |
2624 # endif | |
2625 # ifdef FEAT_TOOLBAR | |
2626 width += get_item_dimensions(gui.toolbar, GTK_ORIENTATION_VERTICAL); | |
2627 # endif | |
2628 # ifdef FEAT_GUI_TABLINE | |
782 | 2629 if (gui.tabline != NULL) |
2630 width += get_item_dimensions(gui.tabline, GTK_ORIENTATION_VERTICAL); | |
685 | 2631 # endif |
2632 #endif | |
2633 | |
2634 return width; | |
2635 } | |
2636 | |
2637 static int | |
2638 get_menu_tool_height(void) | |
2639 { | |
2640 int height = 0; | |
2641 | |
2642 #ifdef FEAT_MENU | |
2643 height += get_item_dimensions(gui.menubar, GTK_ORIENTATION_HORIZONTAL); | |
2644 #endif | |
2645 #ifdef FEAT_TOOLBAR | |
2646 height += get_item_dimensions(gui.toolbar, GTK_ORIENTATION_HORIZONTAL); | |
2647 #endif | |
2648 #ifdef FEAT_GUI_TABLINE | |
782 | 2649 if (gui.tabline != NULL) |
2650 height += get_item_dimensions(gui.tabline, GTK_ORIENTATION_HORIZONTAL); | |
685 | 2651 #endif |
2652 | |
2653 return height; | |
2654 } | |
2655 | |
791 | 2656 /* This controls whether we can set the real window hints at |
2657 * start-up when in a GtkPlug. | |
2658 * 0 = normal processing (default) | |
2659 * 1 = init. hints set, no-one's tried to reset since last check | |
2660 * 2 = init. hints set, attempt made to change hints | |
2661 */ | |
2662 static int init_window_hints_state = 0; | |
2663 | |
685 | 2664 static void |
791 | 2665 update_window_manager_hints(int force_width, int force_height) |
685 | 2666 { |
2667 static int old_width = 0; | |
2668 static int old_height = 0; | |
791 | 2669 static int old_min_width = 0; |
2670 static int old_min_height = 0; | |
685 | 2671 static int old_char_width = 0; |
2672 static int old_char_height = 0; | |
2673 | |
2674 int width; | |
2675 int height; | |
791 | 2676 int min_width; |
2677 int min_height; | |
2678 | |
2679 /* At start-up, don't try to set the hints until the initial | |
2680 * values have been used (those that dictate our initial size) | |
1884 | 2681 * Let forced (i.e., correct) values through always. |
791 | 2682 */ |
2683 if (!(force_width && force_height) && init_window_hints_state > 0) | |
2684 { | |
856 | 2685 /* Don't do it! */ |
2686 init_window_hints_state = 2; | |
2687 return; | |
791 | 2688 } |
685 | 2689 |
2690 /* This also needs to be done when the main window isn't there yet, | |
2691 * otherwise the hints don't work. */ | |
2692 width = gui_get_base_width(); | |
2693 height = gui_get_base_height(); | |
2694 # ifdef FEAT_MENU | |
2695 height += tabline_height() * gui.char_height; | |
2696 # endif | |
2697 width += get_menu_tool_width(); | |
2698 height += get_menu_tool_height(); | |
2699 | |
791 | 2700 /* GtkSockets use GtkPlug's [gui,mainwin] min-size hints to determine |
1226 | 2701 * their actual widget size. When we set our size ourselves (e.g., |
791 | 2702 * 'set columns=' or init. -geom) we briefly set the min. to the size |
2703 * we wish to be instead of the legitimate minimum so that we actually | |
2704 * resize correctly. | |
2705 */ | |
2706 if (force_width && force_height) | |
2707 { | |
856 | 2708 min_width = force_width; |
2709 min_height = force_height; | |
791 | 2710 } |
2711 else | |
2712 { | |
856 | 2713 min_width = width + MIN_COLUMNS * gui.char_width; |
2714 min_height = height + MIN_LINES * gui.char_height; | |
791 | 2715 } |
2716 | |
685 | 2717 /* Avoid an expose event when the size didn't change. */ |
2718 if (width != old_width | |
2719 || height != old_height | |
856 | 2720 || min_width != old_min_width |
791 | 2721 || min_height != old_min_height |
685 | 2722 || gui.char_width != old_char_width |
2723 || gui.char_height != old_char_height) | |
2724 { | |
2725 GdkGeometry geometry; | |
2726 GdkWindowHints geometry_mask; | |
2727 | |
2728 geometry.width_inc = gui.char_width; | |
2729 geometry.height_inc = gui.char_height; | |
2730 geometry.base_width = width; | |
2731 geometry.base_height = height; | |
856 | 2732 geometry.min_width = min_width; |
2733 geometry.min_height = min_height; | |
685 | 2734 geometry_mask = GDK_HINT_BASE_SIZE|GDK_HINT_RESIZE_INC |
2735 |GDK_HINT_MIN_SIZE; | |
2736 /* Using gui.formwin as geometry widget doesn't work as expected | |
2737 * with GTK+ 2 -- dunno why. Presumably all the resizing hacks | |
2738 * in Vim confuse GTK+. */ | |
2739 gtk_window_set_geometry_hints(GTK_WINDOW(gui.mainwin), gui.mainwin, | |
2740 &geometry, geometry_mask); | |
856 | 2741 old_width = width; |
2742 old_height = height; | |
2743 old_min_width = min_width; | |
2744 old_min_height = min_height; | |
2745 old_char_width = gui.char_width; | |
2746 old_char_height = gui.char_height; | |
685 | 2747 } |
2748 } | |
2749 | |
7 | 2750 #ifdef FEAT_TOOLBAR |
2751 | |
2752 /* | |
2753 * This extra effort wouldn't be necessary if we only used stock icons in the | |
2754 * toolbar, as we do for all builtin icons. But user-defined toolbar icons | |
2755 * shouldn't be treated differently, thus we do need this. | |
2756 */ | |
2757 static void | |
2758 icon_size_changed_foreach(GtkWidget *widget, gpointer user_data) | |
2759 { | |
2760 if (GTK_IS_IMAGE(widget)) | |
2761 { | |
2762 GtkImage *image = (GtkImage *)widget; | |
2763 | |
2764 /* User-defined icons are stored in a GtkIconSet */ | |
2765 if (gtk_image_get_storage_type(image) == GTK_IMAGE_ICON_SET) | |
2766 { | |
2767 GtkIconSet *icon_set; | |
2768 GtkIconSize icon_size; | |
2769 | |
2770 gtk_image_get_icon_set(image, &icon_set, &icon_size); | |
2771 icon_size = (GtkIconSize)(long)user_data; | |
2772 | |
2773 gtk_icon_set_ref(icon_set); | |
2774 gtk_image_set_from_icon_set(image, icon_set, icon_size); | |
2775 gtk_icon_set_unref(icon_set); | |
2776 } | |
2777 } | |
2778 else if (GTK_IS_CONTAINER(widget)) | |
2779 { | |
2780 gtk_container_foreach((GtkContainer *)widget, | |
2781 &icon_size_changed_foreach, | |
2782 user_data); | |
2783 } | |
2784 } | |
2785 | |
2786 static void | |
2787 set_toolbar_style(GtkToolbar *toolbar) | |
2788 { | |
2789 GtkToolbarStyle style; | |
2790 GtkIconSize size; | |
2791 GtkIconSize oldsize; | |
2275
e4d849f4df03
Remove the old and not well supported GTK 1 code. (James Vega)
Bram Moolenaar <bram@vim.org>
parents:
2270
diff
changeset
|
2792 |
7 | 2793 if ((toolbar_flags & (TOOLBAR_TEXT | TOOLBAR_ICONS | TOOLBAR_HORIZ)) |
2794 == (TOOLBAR_TEXT | TOOLBAR_ICONS | TOOLBAR_HORIZ)) | |
2795 style = GTK_TOOLBAR_BOTH_HORIZ; | |
2275
e4d849f4df03
Remove the old and not well supported GTK 1 code. (James Vega)
Bram Moolenaar <bram@vim.org>
parents:
2270
diff
changeset
|
2796 else if ((toolbar_flags & (TOOLBAR_TEXT | TOOLBAR_ICONS)) |
7 | 2797 == (TOOLBAR_TEXT | TOOLBAR_ICONS)) |
2798 style = GTK_TOOLBAR_BOTH; | |
2799 else if (toolbar_flags & TOOLBAR_TEXT) | |
2800 style = GTK_TOOLBAR_TEXT; | |
2801 else | |
2802 style = GTK_TOOLBAR_ICONS; | |
2803 | |
2804 gtk_toolbar_set_style(toolbar, style); | |
2805 gtk_toolbar_set_tooltips(toolbar, (toolbar_flags & TOOLBAR_TOOLTIPS) != 0); | |
2806 | |
2807 switch (tbis_flags) | |
2808 { | |
2809 case TBIS_TINY: size = GTK_ICON_SIZE_MENU; break; | |
2810 case TBIS_SMALL: size = GTK_ICON_SIZE_SMALL_TOOLBAR; break; | |
2811 case TBIS_MEDIUM: size = GTK_ICON_SIZE_BUTTON; break; | |
2812 case TBIS_LARGE: size = GTK_ICON_SIZE_LARGE_TOOLBAR; break; | |
2813 default: size = GTK_ICON_SIZE_INVALID; break; | |
2814 } | |
2815 oldsize = gtk_toolbar_get_icon_size(toolbar); | |
2816 | |
2817 if (size == GTK_ICON_SIZE_INVALID) | |
2818 { | |
2819 /* Let global user preferences decide the icon size. */ | |
2820 gtk_toolbar_unset_icon_size(toolbar); | |
2821 size = gtk_toolbar_get_icon_size(toolbar); | |
2822 } | |
2823 if (size != oldsize) | |
2824 { | |
2825 gtk_container_foreach(GTK_CONTAINER(toolbar), | |
2826 &icon_size_changed_foreach, | |
2827 GINT_TO_POINTER((int)size)); | |
2828 } | |
2829 gtk_toolbar_set_icon_size(toolbar, size); | |
2830 } | |
2831 | |
2832 #endif /* FEAT_TOOLBAR */ | |
2833 | |
685 | 2834 #if defined(FEAT_GUI_TABLINE) || defined(PROTO) |
2835 static int ignore_tabline_evt = FALSE; | |
689 | 2836 static GtkWidget *tabline_menu; |
846 | 2837 static GtkTooltips *tabline_tooltip; |
689 | 2838 static int clicked_page; /* page clicked in tab line */ |
2839 | |
2840 /* | |
2841 * Handle selecting an item in the tab line popup menu. | |
2842 */ | |
2843 static void | |
1884 | 2844 tabline_menu_handler(GtkMenuItem *item UNUSED, gpointer user_data) |
689 | 2845 { |
2846 /* Add the string cmd into input buffer */ | |
824 | 2847 send_tabline_menu_event(clicked_page, (int)(long)user_data); |
689 | 2848 } |
2849 | |
851 | 2850 static void |
2851 add_tabline_menu_item(GtkWidget *menu, char_u *text, int resp) | |
2852 { | |
2853 GtkWidget *item; | |
2854 char_u *utf_text; | |
2855 | |
2856 utf_text = CONVERT_TO_UTF8(text); | |
2857 item = gtk_menu_item_new_with_label((const char *)utf_text); | |
2858 gtk_widget_show(item); | |
2859 CONVERT_TO_UTF8_FREE(utf_text); | |
2860 | |
2861 gtk_container_add(GTK_CONTAINER(menu), item); | |
2862 gtk_signal_connect(GTK_OBJECT(item), "activate", | |
2863 GTK_SIGNAL_FUNC(tabline_menu_handler), | |
1660 | 2864 (gpointer)(long)resp); |
851 | 2865 } |
2866 | |
689 | 2867 /* |
2868 * Create a menu for the tab line. | |
2869 */ | |
2870 static GtkWidget * | |
2871 create_tabline_menu(void) | |
2872 { | |
851 | 2873 GtkWidget *menu; |
689 | 2874 |
2875 menu = gtk_menu_new(); | |
6819 | 2876 if (first_tabpage->tp_next != NULL) |
2877 add_tabline_menu_item(menu, (char_u *)_("Close tab"), | |
2878 TABLINE_MENU_CLOSE); | |
851 | 2879 add_tabline_menu_item(menu, (char_u *)_("New tab"), TABLINE_MENU_NEW); |
2880 add_tabline_menu_item(menu, (char_u *)_("Open Tab..."), TABLINE_MENU_OPEN); | |
689 | 2881 |
2882 return menu; | |
2883 } | |
2884 | |
2885 static gboolean | |
2886 on_tabline_menu(GtkWidget *widget, GdkEvent *event) | |
2887 { | |
2888 /* Was this button press event ? */ | |
2889 if (event->type == GDK_BUTTON_PRESS) | |
2890 { | |
2891 GdkEventButton *bevent = (GdkEventButton *)event; | |
2892 int x = bevent->x; | |
851 | 2893 int y = bevent->y; |
2894 GtkWidget *tabwidget; | |
2895 GdkWindow *tabwin; | |
689 | 2896 |
844 | 2897 /* When ignoring events return TRUE so that the selected page doesn't |
2898 * change. */ | |
2899 if (hold_gui_events | |
2900 # ifdef FEAT_CMDWIN | |
2901 || cmdwin_type != 0 | |
2902 # endif | |
2903 ) | |
2904 return TRUE; | |
2905 | |
851 | 2906 tabwin = gdk_window_at_pointer(&x, &y); |
2907 gdk_window_get_user_data(tabwin, (gpointer)&tabwidget); | |
2908 clicked_page = (int)(long)gtk_object_get_user_data( | |
2909 GTK_OBJECT(tabwidget)); | |
689 | 2910 |
2911 /* If the event was generated for 3rd button popup the menu. */ | |
2912 if (bevent->button == 3) | |
2913 { | |
2914 gtk_menu_popup(GTK_MENU(widget), NULL, NULL, NULL, NULL, | |
2915 bevent->button, bevent->time); | |
2916 /* We handled the event. */ | |
2917 return TRUE; | |
2918 } | |
868 | 2919 else if (bevent->button == 1) |
693 | 2920 { |
868 | 2921 if (clicked_page == 0) |
2922 { | |
1394 | 2923 /* Click after all tabs moves to next tab page. When "x" is |
2924 * small guess it's the left button. */ | |
2301
6f63294a1781
Avoid use of the GTK mail_loop() so that the GtkFileChooser can be used.
Bram Moolenaar <bram@vim.org>
parents:
2278
diff
changeset
|
2925 send_tabline_event(x < 50 ? -1 : 0); |
868 | 2926 } |
693 | 2927 } |
689 | 2928 } |
844 | 2929 |
689 | 2930 /* We didn't handle the event. */ |
2931 return FALSE; | |
2932 } | |
685 | 2933 |
2934 /* | |
2935 * Handle selecting one of the tabs. | |
2936 */ | |
2937 static void | |
2938 on_select_tab( | |
1884 | 2939 GtkNotebook *notebook UNUSED, |
2940 GtkNotebookPage *page UNUSED, | |
944 | 2941 gint idx, |
1884 | 2942 gpointer data UNUSED) |
685 | 2943 { |
2944 if (!ignore_tabline_evt) | |
693 | 2945 { |
2301
6f63294a1781
Avoid use of the GTK mail_loop() so that the GtkFileChooser can be used.
Bram Moolenaar <bram@vim.org>
parents:
2278
diff
changeset
|
2946 send_tabline_event(idx + 1); |
693 | 2947 } |
685 | 2948 } |
2949 | |
2950 /* | |
2951 * Show or hide the tabline. | |
2952 */ | |
2953 void | |
2954 gui_mch_show_tabline(int showit) | |
2955 { | |
2956 if (gui.tabline == NULL) | |
2957 return; | |
2958 | |
2959 if (!showit != !gtk_notebook_get_show_tabs(GTK_NOTEBOOK(gui.tabline))) | |
2960 { | |
708 | 2961 /* Note: this may cause a resize event */ |
685 | 2962 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(gui.tabline), showit); |
791 | 2963 update_window_manager_hints(0, 0); |
868 | 2964 if (showit) |
2965 GTK_WIDGET_UNSET_FLAGS(GTK_WIDGET(gui.tabline), GTK_CAN_FOCUS); | |
2966 } | |
2967 | |
2968 gui_mch_update(); | |
685 | 2969 } |
2970 | |
2971 /* | |
708 | 2972 * Return TRUE when tabline is displayed. |
2973 */ | |
2974 int | |
2975 gui_mch_showing_tabline(void) | |
2976 { | |
2977 return gui.tabline != NULL | |
2275
e4d849f4df03
Remove the old and not well supported GTK 1 code. (James Vega)
Bram Moolenaar <bram@vim.org>
parents:
2270
diff
changeset
|
2978 && gtk_notebook_get_show_tabs(GTK_NOTEBOOK(gui.tabline)); |
708 | 2979 } |
2980 | |
2981 /* | |
685 | 2982 * Update the labels of the tabline. |
2983 */ | |
2984 void | |
2985 gui_mch_update_tabline(void) | |
2986 { | |
2987 GtkWidget *page; | |
846 | 2988 GtkWidget *event_box; |
685 | 2989 GtkWidget *label; |
2990 tabpage_T *tp; | |
2991 int nr = 0; | |
851 | 2992 int tab_num; |
685 | 2993 int curtabidx = 0; |
836 | 2994 char_u *labeltext; |
685 | 2995 |
2996 if (gui.tabline == NULL) | |
2997 return; | |
2998 | |
2999 ignore_tabline_evt = TRUE; | |
3000 | |
3001 /* Add a label for each tab page. They all contain the same text area. */ | |
3002 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next, ++nr) | |
3003 { | |
3004 if (tp == curtab) | |
3005 curtabidx = nr; | |
3006 | |
851 | 3007 tab_num = nr + 1; |
3008 | |
685 | 3009 page = gtk_notebook_get_nth_page(GTK_NOTEBOOK(gui.tabline), nr); |
3010 if (page == NULL) | |
3011 { | |
3012 /* Add notebook page */ | |
3013 page = gtk_vbox_new(FALSE, 0); | |
3014 gtk_widget_show(page); | |
846 | 3015 event_box = gtk_event_box_new(); |
3016 gtk_widget_show(event_box); | |
685 | 3017 label = gtk_label_new("-Empty-"); |
851 | 3018 gtk_misc_set_padding(GTK_MISC(label), 2, 2); |
846 | 3019 gtk_container_add(GTK_CONTAINER(event_box), label); |
685 | 3020 gtk_widget_show(label); |
3021 gtk_notebook_insert_page(GTK_NOTEBOOK(gui.tabline), | |
3022 page, | |
846 | 3023 event_box, |
685 | 3024 nr++); |
3025 } | |
3026 | |
846 | 3027 event_box = gtk_notebook_get_tab_label(GTK_NOTEBOOK(gui.tabline), page); |
1660 | 3028 gtk_object_set_user_data(GTK_OBJECT(event_box), |
3029 (gpointer)(long)tab_num); | |
846 | 3030 label = GTK_BIN(event_box)->child; |
839 | 3031 get_tabline_label(tp, FALSE); |
836 | 3032 labeltext = CONVERT_TO_UTF8(NameBuff); |
846 | 3033 gtk_label_set_text(GTK_LABEL(label), (const char *)labeltext); |
836 | 3034 CONVERT_TO_UTF8_FREE(labeltext); |
846 | 3035 |
3036 get_tabline_label(tp, TRUE); | |
3037 labeltext = CONVERT_TO_UTF8(NameBuff); | |
3038 gtk_tooltips_set_tip(GTK_TOOLTIPS(tabline_tooltip), event_box, | |
3039 (const char *)labeltext, NULL); | |
3040 CONVERT_TO_UTF8_FREE(labeltext); | |
685 | 3041 } |
3042 | |
3043 /* Remove any old labels. */ | |
3044 while (gtk_notebook_get_nth_page(GTK_NOTEBOOK(gui.tabline), nr) != NULL) | |
3045 gtk_notebook_remove_page(GTK_NOTEBOOK(gui.tabline), nr); | |
3046 | |
3047 if (gtk_notebook_current_page(GTK_NOTEBOOK(gui.tabline)) != curtabidx) | |
856 | 3048 gtk_notebook_set_page(GTK_NOTEBOOK(gui.tabline), curtabidx); |
685 | 3049 |
851 | 3050 /* Make sure everything is in place before drawing text. */ |
3051 gui_mch_update(); | |
3052 | |
685 | 3053 ignore_tabline_evt = FALSE; |
3054 } | |
3055 | |
3056 /* | |
3057 * Set the current tab to "nr". First tab is 1. | |
3058 */ | |
3059 void | |
3060 gui_mch_set_curtab(nr) | |
3061 int nr; | |
3062 { | |
3063 if (gui.tabline == NULL) | |
3064 return; | |
3065 | |
3066 ignore_tabline_evt = TRUE; | |
3067 if (gtk_notebook_current_page(GTK_NOTEBOOK(gui.tabline)) != nr - 1) | |
856 | 3068 gtk_notebook_set_page(GTK_NOTEBOOK(gui.tabline), nr - 1); |
685 | 3069 ignore_tabline_evt = FALSE; |
3070 } | |
3071 | |
3072 #endif /* FEAT_GUI_TABLINE */ | |
3073 | |
7 | 3074 /* |
2183 | 3075 * Add selection targets for PRIMARY and CLIPBOARD selections. |
3076 */ | |
3077 void | |
3078 gui_gtk_set_selection_targets(void) | |
3079 { | |
3080 int i, j = 0; | |
3081 int n_targets = N_SELECTION_TARGETS; | |
3082 GtkTargetEntry targets[N_SELECTION_TARGETS]; | |
3083 | |
3084 for (i = 0; i < (int)N_SELECTION_TARGETS; ++i) | |
3085 { | |
3518 | 3086 /* OpenOffice tries to use TARGET_HTML and fails when we don't |
2183 | 3087 * return something, instead of trying another target. Therefore only |
3088 * offer TARGET_HTML when it works. */ | |
3089 if (!clip_html && selection_targets[i].info == TARGET_HTML) | |
3090 n_targets--; | |
3091 else | |
3092 targets[j++] = selection_targets[i]; | |
3093 } | |
3094 | |
3095 gtk_selection_clear_targets(gui.drawarea, (GdkAtom)GDK_SELECTION_PRIMARY); | |
3096 gtk_selection_clear_targets(gui.drawarea, (GdkAtom)clip_plus.gtk_sel_atom); | |
3097 gtk_selection_add_targets(gui.drawarea, | |
3098 (GdkAtom)GDK_SELECTION_PRIMARY, | |
3099 targets, n_targets); | |
3100 gtk_selection_add_targets(gui.drawarea, | |
3101 (GdkAtom)clip_plus.gtk_sel_atom, | |
3102 targets, n_targets); | |
3103 } | |
3104 | |
3105 /* | |
3106 * Set up for receiving DND items. | |
3107 */ | |
3108 void | |
3109 gui_gtk_set_dnd_targets(void) | |
3110 { | |
3111 int i, j = 0; | |
3112 int n_targets = N_DND_TARGETS; | |
3113 GtkTargetEntry targets[N_DND_TARGETS]; | |
3114 | |
3115 for (i = 0; i < (int)N_DND_TARGETS; ++i) | |
3116 { | |
3154 | 3117 if (!clip_html && dnd_targets[i].info == TARGET_HTML) |
2183 | 3118 n_targets--; |
3119 else | |
3120 targets[j++] = dnd_targets[i]; | |
3121 } | |
3122 | |
3123 gtk_drag_dest_unset(gui.drawarea); | |
3124 gtk_drag_dest_set(gui.drawarea, | |
3125 GTK_DEST_DEFAULT_ALL, | |
3126 targets, n_targets, | |
2718 | 3127 GDK_ACTION_COPY | GDK_ACTION_MOVE); |
2183 | 3128 } |
3129 | |
3130 /* | |
7 | 3131 * Initialize the GUI. Create all the windows, set up all the callbacks etc. |
3132 * Returns OK for success, FAIL when the GUI can't be started. | |
3133 */ | |
3134 int | |
3135 gui_mch_init(void) | |
3136 { | |
3137 GtkWidget *vbox; | |
3138 | |
3139 #ifdef FEAT_GUI_GNOME | |
3140 /* Initialize the GNOME libraries. gnome_program_init()/gnome_init() | |
3141 * exits on failure, but that's a non-issue because we already called | |
3142 * gtk_init_check() in gui_mch_init_check(). */ | |
3143 if (using_gnome) | |
4045 | 3144 { |
7 | 3145 gnome_program_init(VIMPACKAGE, VIM_VERSION_SHORT, |
3146 LIBGNOMEUI_MODULE, gui_argc, gui_argv, NULL); | |
4045 | 3147 # if defined(FEAT_FLOAT) && defined(LC_NUMERIC) |
5854 | 3148 { |
3149 char *p = setlocale(LC_NUMERIC, NULL); | |
3150 | |
3151 /* Make sure strtod() uses a decimal point, not a comma. Gnome | |
3152 * init may change it. */ | |
3153 if (p == NULL || strcmp(p, "C") != 0) | |
3154 setlocale(LC_NUMERIC, "C"); | |
3155 } | |
4045 | 3156 # endif |
3157 } | |
7 | 3158 #endif |
3159 vim_free(gui_argv); | |
3160 gui_argv = NULL; | |
3161 | |
2275
e4d849f4df03
Remove the old and not well supported GTK 1 code. (James Vega)
Bram Moolenaar <bram@vim.org>
parents:
2270
diff
changeset
|
3162 #if GLIB_CHECK_VERSION(2,1,3) |
7 | 3163 /* Set the human-readable application name */ |
3164 g_set_application_name("Vim"); | |
2275
e4d849f4df03
Remove the old and not well supported GTK 1 code. (James Vega)
Bram Moolenaar <bram@vim.org>
parents:
2270
diff
changeset
|
3165 #endif |
7 | 3166 /* |
3167 * Force UTF-8 output no matter what the value of 'encoding' is. | |
3168 * did_set_string_option() in option.c prohibits changing 'termencoding' | |
3169 * to something else than UTF-8 if the GUI is in use. | |
3170 */ | |
3171 set_option_value((char_u *)"termencoding", 0L, (char_u *)"utf-8", 0); | |
3172 | |
2275
e4d849f4df03
Remove the old and not well supported GTK 1 code. (James Vega)
Bram Moolenaar <bram@vim.org>
parents:
2270
diff
changeset
|
3173 #ifdef FEAT_TOOLBAR |
7 | 3174 gui_gtk_register_stock_icons(); |
2275
e4d849f4df03
Remove the old and not well supported GTK 1 code. (James Vega)
Bram Moolenaar <bram@vim.org>
parents:
2270
diff
changeset
|
3175 #endif |
7 | 3176 /* FIXME: Need to install the classic icons and a gtkrc.classic file. |
3177 * The hard part is deciding install locations and the Makefile magic. */ | |
2275
e4d849f4df03
Remove the old and not well supported GTK 1 code. (James Vega)
Bram Moolenaar <bram@vim.org>
parents:
2270
diff
changeset
|
3178 #if 0 |
7 | 3179 gtk_rc_parse("gtkrc"); |
3180 #endif | |
3181 | |
3182 /* Initialize values */ | |
3183 gui.border_width = 2; | |
3184 gui.scrollbar_width = SB_DEFAULT_WIDTH; | |
3185 gui.scrollbar_height = SB_DEFAULT_WIDTH; | |
136 | 3186 /* LINTED: avoid warning: conversion to 'unsigned long' */ |
7 | 3187 gui.fgcolor = g_new0(GdkColor, 1); |
136 | 3188 /* LINTED: avoid warning: conversion to 'unsigned long' */ |
7 | 3189 gui.bgcolor = g_new0(GdkColor, 1); |
207 | 3190 /* LINTED: avoid warning: conversion to 'unsigned long' */ |
3191 gui.spcolor = g_new0(GdkColor, 1); | |
7 | 3192 |
3193 /* Initialise atoms */ | |
1904 | 3194 html_atom = gdk_atom_intern("text/html", FALSE); |
7 | 3195 utf8_string_atom = gdk_atom_intern("UTF8_STRING", FALSE); |
3196 | |
3197 /* Set default foreground and background colors. */ | |
3198 gui.norm_pixel = gui.def_norm_pixel; | |
3199 gui.back_pixel = gui.def_back_pixel; | |
3200 | |
3201 if (gtk_socket_id != 0) | |
3202 { | |
3203 GtkWidget *plug; | |
3204 | |
3205 /* Use GtkSocket from another app. */ | |
3206 #ifdef HAVE_GTK_MULTIHEAD | |
3207 plug = gtk_plug_new_for_display(gdk_display_get_default(), | |
3208 gtk_socket_id); | |
3209 #else | |
3210 plug = gtk_plug_new(gtk_socket_id); | |
3211 #endif | |
3212 if (plug != NULL && GTK_PLUG(plug)->socket_window != NULL) | |
3213 { | |
3214 gui.mainwin = plug; | |
3215 } | |
3216 else | |
3217 { | |
3218 g_warning("Connection to GTK+ socket (ID %u) failed", | |
3219 (unsigned int)gtk_socket_id); | |
3220 /* Pretend we never wanted it if it failed (get own window) */ | |
3221 gtk_socket_id = 0; | |
3222 } | |
3223 } | |
3224 | |
3225 if (gtk_socket_id == 0) | |
3226 { | |
3227 #ifdef FEAT_GUI_GNOME | |
3228 if (using_gnome) | |
3229 { | |
3230 gui.mainwin = gnome_app_new("Vim", NULL); | |
3231 # ifdef USE_XSMP | |
3232 /* Use the GNOME save-yourself functionality now. */ | |
3233 xsmp_close(); | |
3234 # endif | |
3235 } | |
3236 else | |
3237 #endif | |
3238 gui.mainwin = gtk_window_new(GTK_WINDOW_TOPLEVEL); | |
3239 } | |
3240 | |
3241 gtk_widget_set_name(gui.mainwin, "vim-main-window"); | |
3242 | |
3243 /* Create the PangoContext used for drawing all text. */ | |
3244 gui.text_context = gtk_widget_create_pango_context(gui.mainwin); | |
3245 pango_context_set_base_dir(gui.text_context, PANGO_DIRECTION_LTR); | |
2275
e4d849f4df03
Remove the old and not well supported GTK 1 code. (James Vega)
Bram Moolenaar <bram@vim.org>
parents:
2270
diff
changeset
|
3246 |
7 | 3247 gtk_container_border_width(GTK_CONTAINER(gui.mainwin), 0); |
3248 gtk_widget_add_events(gui.mainwin, GDK_VISIBILITY_NOTIFY_MASK); | |
3249 | |
3250 gtk_signal_connect(GTK_OBJECT(gui.mainwin), "delete_event", | |
3251 GTK_SIGNAL_FUNC(&delete_event_cb), NULL); | |
3252 | |
3253 gtk_signal_connect(GTK_OBJECT(gui.mainwin), "realize", | |
3254 GTK_SIGNAL_FUNC(&mainwin_realize), NULL); | |
3255 #ifdef HAVE_GTK_MULTIHEAD | |
3256 g_signal_connect(G_OBJECT(gui.mainwin), "screen_changed", | |
3257 G_CALLBACK(&mainwin_screen_changed_cb), NULL); | |
3258 #endif | |
3259 gui.accel_group = gtk_accel_group_new(); | |
3260 gtk_window_add_accel_group(GTK_WINDOW(gui.mainwin), gui.accel_group); | |
3261 | |
685 | 3262 /* A vertical box holds the menubar, toolbar and main text window. */ |
7 | 3263 vbox = gtk_vbox_new(FALSE, 0); |
3264 | |
3265 #ifdef FEAT_GUI_GNOME | |
3266 if (using_gnome) | |
3267 { | |
2275
e4d849f4df03
Remove the old and not well supported GTK 1 code. (James Vega)
Bram Moolenaar <bram@vim.org>
parents:
2270
diff
changeset
|
3268 # if defined(FEAT_MENU) |
7 | 3269 /* automagically restore menubar/toolbar placement */ |
3270 gnome_app_enable_layout_config(GNOME_APP(gui.mainwin), TRUE); | |
3271 # endif | |
3272 gnome_app_set_contents(GNOME_APP(gui.mainwin), vbox); | |
3273 } | |
3274 else | |
3275 #endif | |
3276 { | |
3277 gtk_container_add(GTK_CONTAINER(gui.mainwin), vbox); | |
3278 gtk_widget_show(vbox); | |
3279 } | |
3280 | |
3281 #ifdef FEAT_MENU | |
3282 /* | |
3283 * Create the menubar and handle | |
3284 */ | |
3285 gui.menubar = gtk_menu_bar_new(); | |
3286 gtk_widget_set_name(gui.menubar, "vim-menubar"); | |
3287 | |
36 | 3288 /* Avoid that GTK takes <F10> away from us. */ |
3289 { | |
3290 GtkSettings *gtk_settings; | |
3291 | |
3292 gtk_settings = gtk_settings_get_for_screen(gdk_screen_get_default()); | |
3293 g_object_set(gtk_settings, "gtk-menu-bar-accel", NULL, NULL); | |
3294 } | |
3295 | |
3296 | |
7 | 3297 # ifdef FEAT_GUI_GNOME |
3298 if (using_gnome) | |
3299 { | |
3300 BonoboDockItem *dockitem; | |
3301 | |
3302 gnome_app_set_menus(GNOME_APP(gui.mainwin), GTK_MENU_BAR(gui.menubar)); | |
3303 dockitem = gnome_app_get_dock_item_by_name(GNOME_APP(gui.mainwin), | |
3304 GNOME_APP_MENUBAR_NAME); | |
798 | 3305 /* We don't want the menu to float. */ |
3306 bonobo_dock_item_set_behavior(dockitem, | |
3307 bonobo_dock_item_get_behavior(dockitem) | |
3308 | BONOBO_DOCK_ITEM_BEH_NEVER_FLOATING); | |
7 | 3309 gui.menubar_h = GTK_WIDGET(dockitem); |
3310 } | |
3311 else | |
3312 # endif /* FEAT_GUI_GNOME */ | |
3313 { | |
827 | 3314 /* Always show the menubar, otherwise <F10> doesn't work. It may be |
3315 * disabled in gui_init() later. */ | |
3316 gtk_widget_show(gui.menubar); | |
7 | 3317 gtk_box_pack_start(GTK_BOX(vbox), gui.menubar, FALSE, FALSE, 0); |
3318 } | |
3319 #endif /* FEAT_MENU */ | |
3320 | |
3321 #ifdef FEAT_TOOLBAR | |
3322 /* | |
3323 * Create the toolbar and handle | |
3324 */ | |
3325 /* some aesthetics on the toolbar */ | |
3326 gtk_rc_parse_string( | |
3327 "style \"vim-toolbar-style\" {\n" | |
3328 " GtkToolbar::button_relief = GTK_RELIEF_NONE\n" | |
3329 "}\n" | |
3330 "widget \"*.vim-toolbar\" style \"vim-toolbar-style\"\n"); | |
3331 gui.toolbar = gtk_toolbar_new(); | |
3332 gtk_widget_set_name(gui.toolbar, "vim-toolbar"); | |
3333 set_toolbar_style(GTK_TOOLBAR(gui.toolbar)); | |
3334 | |
3335 # ifdef FEAT_GUI_GNOME | |
3336 if (using_gnome) | |
3337 { | |
3338 BonoboDockItem *dockitem; | |
3339 | |
3340 gnome_app_set_toolbar(GNOME_APP(gui.mainwin), GTK_TOOLBAR(gui.toolbar)); | |
3341 dockitem = gnome_app_get_dock_item_by_name(GNOME_APP(gui.mainwin), | |
3342 GNOME_APP_TOOLBAR_NAME); | |
3343 gui.toolbar_h = GTK_WIDGET(dockitem); | |
795 | 3344 /* When the toolbar is floating it gets stuck. So long as that isn't |
798 | 3345 * fixed let's disallow floating. */ |
795 | 3346 bonobo_dock_item_set_behavior(dockitem, |
798 | 3347 bonobo_dock_item_get_behavior(dockitem) |
3348 | BONOBO_DOCK_ITEM_BEH_NEVER_FLOATING); | |
7 | 3349 gtk_container_set_border_width(GTK_CONTAINER(gui.toolbar), 0); |
3350 } | |
3351 else | |
3352 # endif /* FEAT_GUI_GNOME */ | |
3353 { | |
3354 if (vim_strchr(p_go, GO_TOOLBAR) != NULL | |
3355 && (toolbar_flags & (TOOLBAR_TEXT | TOOLBAR_ICONS))) | |
3356 gtk_widget_show(gui.toolbar); | |
3357 gtk_box_pack_start(GTK_BOX(vbox), gui.toolbar, FALSE, FALSE, 0); | |
3358 } | |
3359 #endif /* FEAT_TOOLBAR */ | |
3360 | |
685 | 3361 #ifdef FEAT_GUI_TABLINE |
782 | 3362 /* |
3363 * Use a Notebook for the tab pages labels. The labels are hidden by | |
3364 * default. | |
3365 */ | |
791 | 3366 gui.tabline = gtk_notebook_new(); |
3367 gtk_widget_show(gui.tabline); | |
3368 gtk_box_pack_start(GTK_BOX(vbox), gui.tabline, FALSE, FALSE, 0); | |
3369 gtk_notebook_set_show_border(GTK_NOTEBOOK(gui.tabline), FALSE); | |
3370 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(gui.tabline), FALSE); | |
841 | 3371 gtk_notebook_set_scrollable(GTK_NOTEBOOK(gui.tabline), TRUE); |
868 | 3372 gtk_notebook_set_tab_border(GTK_NOTEBOOK(gui.tabline), FALSE); |
791 | 3373 |
846 | 3374 tabline_tooltip = gtk_tooltips_new(); |
3375 gtk_tooltips_enable(GTK_TOOLTIPS(tabline_tooltip)); | |
3376 | |
3377 { | |
3378 GtkWidget *page, *label, *event_box; | |
791 | 3379 |
856 | 3380 /* Add the first tab. */ |
846 | 3381 page = gtk_vbox_new(FALSE, 0); |
3382 gtk_widget_show(page); | |
3383 gtk_container_add(GTK_CONTAINER(gui.tabline), page); | |
3384 label = gtk_label_new("-Empty-"); | |
3385 gtk_widget_show(label); | |
3386 event_box = gtk_event_box_new(); | |
3387 gtk_widget_show(event_box); | |
1660 | 3388 gtk_object_set_user_data(GTK_OBJECT(event_box), (gpointer)1L); |
851 | 3389 gtk_misc_set_padding(GTK_MISC(label), 2, 2); |
846 | 3390 gtk_container_add(GTK_CONTAINER(event_box), label); |
3391 gtk_notebook_set_tab_label(GTK_NOTEBOOK(gui.tabline), page, event_box); | |
791 | 3392 } |
865 | 3393 |
791 | 3394 gtk_signal_connect(GTK_OBJECT(gui.tabline), "switch_page", |
856 | 3395 GTK_SIGNAL_FUNC(on_select_tab), NULL); |
791 | 3396 |
3397 /* Create a popup menu for the tab line and connect it. */ | |
3398 tabline_menu = create_tabline_menu(); | |
3399 gtk_signal_connect_object(GTK_OBJECT(gui.tabline), "button_press_event", | |
856 | 3400 GTK_SIGNAL_FUNC(on_tabline_menu), GTK_OBJECT(tabline_menu)); |
685 | 3401 #endif |
3402 | |
7 | 3403 gui.formwin = gtk_form_new(); |
3404 gtk_container_border_width(GTK_CONTAINER(gui.formwin), 0); | |
3405 gtk_widget_set_events(gui.formwin, GDK_EXPOSURE_MASK); | |
3406 | |
3407 gui.drawarea = gtk_drawing_area_new(); | |
3408 | |
3409 /* Determine which events we will filter. */ | |
3410 gtk_widget_set_events(gui.drawarea, | |
3411 GDK_EXPOSURE_MASK | | |
3412 GDK_ENTER_NOTIFY_MASK | | |
3413 GDK_LEAVE_NOTIFY_MASK | | |
3414 GDK_BUTTON_PRESS_MASK | | |
3415 GDK_BUTTON_RELEASE_MASK | | |
3416 GDK_SCROLL_MASK | | |
3417 GDK_KEY_PRESS_MASK | | |
3418 GDK_KEY_RELEASE_MASK | | |
3419 GDK_POINTER_MOTION_MASK | | |
3420 GDK_POINTER_MOTION_HINT_MASK); | |
3421 | |
3422 gtk_widget_show(gui.drawarea); | |
3423 gtk_form_put(GTK_FORM(gui.formwin), gui.drawarea, 0, 0); | |
3424 gtk_widget_show(gui.formwin); | |
3425 gtk_box_pack_start(GTK_BOX(vbox), gui.formwin, TRUE, TRUE, 0); | |
3426 | |
3427 /* For GtkSockets, key-presses must go to the focus widget (drawarea) | |
3428 * and not the window. */ | |
3429 gtk_signal_connect((gtk_socket_id == 0) ? GTK_OBJECT(gui.mainwin) | |
3430 : GTK_OBJECT(gui.drawarea), | |
3431 "key_press_event", | |
3432 GTK_SIGNAL_FUNC(key_press_event), NULL); | |
2275
e4d849f4df03
Remove the old and not well supported GTK 1 code. (James Vega)
Bram Moolenaar <bram@vim.org>
parents:
2270
diff
changeset
|
3433 #if defined(FEAT_XIM) |
7 | 3434 /* Also forward key release events for the benefit of GTK+ 2 input |
3435 * modules. Try CTRL-SHIFT-xdigits to enter a Unicode code point. */ | |
3436 g_signal_connect((gtk_socket_id == 0) ? G_OBJECT(gui.mainwin) | |
3437 : G_OBJECT(gui.drawarea), | |
3438 "key_release_event", | |
3439 G_CALLBACK(&key_release_event), NULL); | |
3440 #endif | |
3441 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "realize", | |
3442 GTK_SIGNAL_FUNC(drawarea_realize_cb), NULL); | |
3443 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "unrealize", | |
3444 GTK_SIGNAL_FUNC(drawarea_unrealize_cb), NULL); | |
3445 | |
3446 gtk_signal_connect_after(GTK_OBJECT(gui.drawarea), "style_set", | |
3447 GTK_SIGNAL_FUNC(&drawarea_style_set_cb), NULL); | |
3448 | |
3449 gui.visibility = GDK_VISIBILITY_UNOBSCURED; | |
3450 | |
3451 #if !(defined(FEAT_GUI_GNOME) && defined(FEAT_SESSION)) | |
3452 wm_protocols_atom = gdk_atom_intern("WM_PROTOCOLS", FALSE); | |
3453 save_yourself_atom = gdk_atom_intern("WM_SAVE_YOURSELF", FALSE); | |
3454 #endif | |
3455 | |
3456 if (gtk_socket_id != 0) | |
1884 | 3457 /* make sure keyboard input can go to the drawarea */ |
7 | 3458 GTK_WIDGET_SET_FLAGS(gui.drawarea, GTK_CAN_FOCUS); |
3459 | |
3460 /* | |
3461 * Set clipboard specific atoms | |
3462 */ | |
3463 vim_atom = gdk_atom_intern(VIM_ATOM_NAME, FALSE); | |
3464 vimenc_atom = gdk_atom_intern(VIMENC_ATOM_NAME, FALSE); | |
3465 clip_star.gtk_sel_atom = GDK_SELECTION_PRIMARY; | |
3466 clip_plus.gtk_sel_atom = gdk_atom_intern("CLIPBOARD", FALSE); | |
3467 | |
3468 /* | |
3469 * Start out by adding the configured border width into the border offset. | |
3470 */ | |
3471 gui.border_offset = gui.border_width; | |
3472 | |
3473 gtk_signal_connect(GTK_OBJECT(gui.mainwin), "visibility_notify_event", | |
3474 GTK_SIGNAL_FUNC(visibility_event), NULL); | |
3475 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "expose_event", | |
3476 GTK_SIGNAL_FUNC(expose_event), NULL); | |
3477 | |
3478 /* | |
3479 * Only install these enter/leave callbacks when 'p' in 'guioptions'. | |
3480 * Only needed for some window managers. | |
3481 */ | |
3482 if (vim_strchr(p_go, GO_POINTER) != NULL) | |
3483 { | |
3484 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "leave_notify_event", | |
3485 GTK_SIGNAL_FUNC(leave_notify_event), NULL); | |
3486 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "enter_notify_event", | |
3487 GTK_SIGNAL_FUNC(enter_notify_event), NULL); | |
3488 } | |
3489 | |
791 | 3490 /* Real windows can get focus ... GtkPlug, being a mere container can't, |
3491 * only its widgets. Arguably, this could be common code and we not use | |
3492 * the window focus at all, but let's be safe. | |
3493 */ | |
3494 if (gtk_socket_id == 0) | |
3495 { | |
856 | 3496 gtk_signal_connect(GTK_OBJECT(gui.mainwin), "focus_out_event", |
3497 GTK_SIGNAL_FUNC(focus_out_event), NULL); | |
3498 gtk_signal_connect(GTK_OBJECT(gui.mainwin), "focus_in_event", | |
3499 GTK_SIGNAL_FUNC(focus_in_event), NULL); | |
791 | 3500 } |
3501 else | |
3502 { | |
856 | 3503 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "focus_out_event", |
3504 GTK_SIGNAL_FUNC(focus_out_event), NULL); | |
3505 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "focus_in_event", | |
3506 GTK_SIGNAL_FUNC(focus_in_event), NULL); | |
791 | 3507 #ifdef FEAT_GUI_TABLINE |
856 | 3508 gtk_signal_connect(GTK_OBJECT(gui.tabline), "focus_out_event", |
3509 GTK_SIGNAL_FUNC(focus_out_event), NULL); | |
3510 gtk_signal_connect(GTK_OBJECT(gui.tabline), "focus_in_event", | |
3511 GTK_SIGNAL_FUNC(focus_in_event), NULL); | |
791 | 3512 #endif /* FEAT_GUI_TABLINE */ |
3513 } | |
7 | 3514 |
3515 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "motion_notify_event", | |
3516 GTK_SIGNAL_FUNC(motion_notify_event), NULL); | |
3517 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "button_press_event", | |
3518 GTK_SIGNAL_FUNC(button_press_event), NULL); | |
3519 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "button_release_event", | |
3520 GTK_SIGNAL_FUNC(button_release_event), NULL); | |
3521 g_signal_connect(G_OBJECT(gui.drawarea), "scroll_event", | |
3522 G_CALLBACK(&scroll_event), NULL); | |
3523 | |
3524 /* | |
3525 * Add selection handler functions. | |
3526 */ | |
3527 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "selection_clear_event", | |
3528 GTK_SIGNAL_FUNC(selection_clear_event), NULL); | |
3529 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "selection_received", | |
3530 GTK_SIGNAL_FUNC(selection_received_cb), NULL); | |
3531 | |
2183 | 3532 gui_gtk_set_selection_targets(); |
7 | 3533 |
3534 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "selection_get", | |
3535 GTK_SIGNAL_FUNC(selection_get_cb), NULL); | |
3536 | |
3537 /* Pretend we don't have input focus, we will get an event if we do. */ | |
3538 gui.in_focus = FALSE; | |
3539 | |
3540 return OK; | |
3541 } | |
3542 | |
3543 #if (defined(FEAT_GUI_GNOME) && defined(FEAT_SESSION)) || defined(PROTO) | |
3544 /* | |
3545 * This is called from gui_start() after a fork() has been done. | |
3546 * We have to tell the session manager our new PID. | |
3547 */ | |
3548 void | |
3549 gui_mch_forked(void) | |
3550 { | |
3551 if (using_gnome) | |
3552 { | |
3553 GnomeClient *client; | |
3554 | |
3555 client = gnome_master_client(); | |
3556 | |
3557 if (client != NULL) | |
3558 gnome_client_set_process_id(client, getpid()); | |
3559 } | |
3560 } | |
3561 #endif /* FEAT_GUI_GNOME && FEAT_SESSION */ | |
3562 | |
3563 /* | |
3564 * Called when the foreground or background color has been changed. | |
3565 * This used to change the graphics contexts directly but we are | |
3566 * currently manipulating them where desired. | |
3567 */ | |
3568 void | |
3569 gui_mch_new_colors(void) | |
3570 { | |
3571 if (gui.drawarea != NULL && gui.drawarea->window != NULL) | |
3572 { | |
3573 GdkColor color = { 0, 0, 0, 0 }; | |
3574 | |
3575 color.pixel = gui.back_pixel; | |
3576 gdk_window_set_background(gui.drawarea->window, &color); | |
3577 } | |
3578 } | |
3579 | |
3580 /* | |
3581 * This signal informs us about the need to rearrange our sub-widgets. | |
3582 */ | |
3583 static gint | |
1884 | 3584 form_configure_event(GtkWidget *widget UNUSED, |
3585 GdkEventConfigure *event, | |
3586 gpointer data UNUSED) | |
7 | 3587 { |
791 | 3588 int usable_height = event->height; |
3589 | |
3590 /* When in a GtkPlug, we can't guarantee valid heights (as a round | |
3591 * no. of char-heights), so we have to manually sanitise them. | |
3592 * Widths seem to sort themselves out, don't ask me why. | |
3593 */ | |
3594 if (gtk_socket_id != 0) | |
856 | 3595 usable_height -= (gui.char_height - (gui.char_height/2)); /* sic. */ |
791 | 3596 |
7 | 3597 gtk_form_freeze(GTK_FORM(gui.formwin)); |
791 | 3598 gui_resize_shell(event->width, usable_height); |
7 | 3599 gtk_form_thaw(GTK_FORM(gui.formwin)); |
3600 | |
3601 return TRUE; | |
3602 } | |
3603 | |
3604 /* | |
3605 * Function called when window already closed. | |
3606 * We can't do much more here than to trying to preserve what had been done, | |
3607 * since the window is already inevitably going away. | |
3608 */ | |
3609 static void | |
1884 | 3610 mainwin_destroy_cb(GtkObject *object UNUSED, gpointer data UNUSED) |
7 | 3611 { |
3612 /* Don't write messages to the GUI anymore */ | |
3613 full_screen = FALSE; | |
3614 | |
3615 gui.mainwin = NULL; | |
3616 gui.drawarea = NULL; | |
3617 | |
3618 if (!exiting) /* only do anything if the destroy was unexpected */ | |
3619 { | |
419 | 3620 vim_strncpy(IObuff, |
3621 (char_u *)_("Vim: Main window unexpectedly destroyed\n"), | |
3622 IOSIZE - 1); | |
7 | 3623 preserve_exit(); |
3624 } | |
3625 } | |
3626 | |
791 | 3627 |
3628 /* | |
3629 * Bit of a hack to ensure we start GtkPlug windows with the correct window | |
3630 * hints (and thus the required size from -geom), but that after that we | |
3631 * put the hints back to normal (the actual minimum size) so we may | |
3632 * subsequently be resized smaller. GtkSocket (the parent end) uses the | |
1226 | 3633 * plug's window 'min hints to set *it's* minimum size, but that's also the |
791 | 3634 * only way we have of making ourselves bigger (by set lines/columns). |
3635 * Thus set hints at start-up to ensure correct init. size, then a | |
3636 * second after the final attempt to reset the real minimum hinst (done by | |
1226 | 3637 * scrollbar init.), actually do the standard hinst and stop the timer. |
791 | 3638 * We'll not let the default hints be set while this timer's active. |
3639 */ | |
3640 static gboolean | |
1884 | 3641 check_startup_plug_hints(gpointer data UNUSED) |
791 | 3642 { |
3643 if (init_window_hints_state == 1) | |
3644 { | |
856 | 3645 /* Safe to use normal hints now */ |
3646 init_window_hints_state = 0; | |
3647 update_window_manager_hints(0, 0); | |
3648 return FALSE; /* stop timer */ | |
791 | 3649 } |
3650 | |
3651 /* Keep on trying */ | |
3652 init_window_hints_state = 1; | |
3653 return TRUE; | |
3654 } | |
3655 | |
7 | 3656 /* |
3657 * Open the GUI window which was created by a call to gui_mch_init(). | |
3658 */ | |
3659 int | |
3660 gui_mch_open(void) | |
3661 { | |
3662 guicolor_T fg_pixel = INVALCOLOR; | |
3663 guicolor_T bg_pixel = INVALCOLOR; | |
1966 | 3664 guint pixel_width; |
3665 guint pixel_height; | |
7 | 3666 |
3667 /* | |
3668 * Allow setting a window role on the command line, or invent one | |
3669 * if none was specified. This is mainly useful for GNOME session | |
3670 * support; allowing the WM to restore window placement. | |
3671 */ | |
3672 if (role_argument != NULL) | |
3673 { | |
3674 gtk_window_set_role(GTK_WINDOW(gui.mainwin), role_argument); | |
3675 } | |
3676 else | |
3677 { | |
3678 char *role; | |
3679 | |
3680 /* Invent a unique-enough ID string for the role */ | |
3681 role = g_strdup_printf("vim-%u-%u-%u", | |
3682 (unsigned)mch_get_pid(), | |
3683 (unsigned)g_random_int(), | |
3684 (unsigned)time(NULL)); | |
3685 | |
3686 gtk_window_set_role(GTK_WINDOW(gui.mainwin), role); | |
3687 g_free(role); | |
3688 } | |
3689 | |
3690 if (gui_win_x != -1 && gui_win_y != -1) | |
3691 gtk_window_move(GTK_WINDOW(gui.mainwin), gui_win_x, gui_win_y); | |
3692 | |
3693 /* Determine user specified geometry, if present. */ | |
3694 if (gui.geom != NULL) | |
3695 { | |
3696 int mask; | |
3697 unsigned int w, h; | |
3698 int x = 0; | |
3699 int y = 0; | |
3700 | |
3701 mask = XParseGeometry((char *)gui.geom, &x, &y, &w, &h); | |
3702 | |
3703 if (mask & WidthValue) | |
3704 Columns = w; | |
3705 if (mask & HeightValue) | |
857 | 3706 { |
1884 | 3707 if (p_window > (long)h - 1 || !option_was_set((char_u *)"window")) |
857 | 3708 p_window = h - 1; |
7 | 3709 Rows = h; |
857 | 3710 } |
5070
cf52d2a8c05c
updated for version 7.3.1278
Bram Moolenaar <bram@vim.org>
parents:
4474
diff
changeset
|
3711 limit_screen_size(); |
1426 | 3712 |
3713 pixel_width = (guint)(gui_get_base_width() + Columns * gui.char_width); | |
3714 pixel_height = (guint)(gui_get_base_height() + Rows * gui.char_height); | |
3715 | |
3716 pixel_width += get_menu_tool_width(); | |
3717 pixel_height += get_menu_tool_height(); | |
3718 | |
7 | 3719 if (mask & (XValue | YValue)) |
1426 | 3720 { |
1757 | 3721 int ww, hh; |
3722 gui_mch_get_screen_dimensions(&ww, &hh); | |
3723 hh += p_ghr + get_menu_tool_height(); | |
3724 ww += get_menu_tool_width(); | |
1426 | 3725 if (mask & XNegative) |
1757 | 3726 x += ww - pixel_width; |
1426 | 3727 if (mask & YNegative) |
1757 | 3728 y += hh - pixel_height; |
7 | 3729 gtk_window_move(GTK_WINDOW(gui.mainwin), x, y); |
1426 | 3730 } |
7 | 3731 vim_free(gui.geom); |
3732 gui.geom = NULL; | |
791 | 3733 |
856 | 3734 /* From now until everyone's stopped trying to set the window hints |
3735 * to their correct minimum values, stop them being set as we need | |
3736 * them to remain at our required size for the parent GtkSocket to | |
3737 * give us the right initial size. | |
3738 */ | |
791 | 3739 if (gtk_socket_id != 0 && (mask & WidthValue || mask & HeightValue)) |
856 | 3740 { |
3741 update_window_manager_hints(pixel_width, pixel_height); | |
3742 init_window_hints_state = 1; | |
3743 g_timeout_add(1000, check_startup_plug_hints, NULL); | |
3744 } | |
7 | 3745 } |
3746 | |
1966 | 3747 pixel_width = (guint)(gui_get_base_width() + Columns * gui.char_width); |
3748 pixel_height = (guint)(gui_get_base_height() + Rows * gui.char_height); | |
3749 /* For GTK2 changing the size of the form widget doesn't cause window | |
3750 * resizing. */ | |
2254
4620acaf4814
One more fix for conceal patch.
Bram Moolenaar <bram@vim.org>
parents:
2248
diff
changeset
|
3751 if (gtk_socket_id == 0) |
1966 | 3752 gtk_window_resize(GTK_WINDOW(gui.mainwin), pixel_width, pixel_height); |
791 | 3753 update_window_manager_hints(0, 0); |
7 | 3754 |
3755 if (foreground_argument != NULL) | |
3756 fg_pixel = gui_get_color((char_u *)foreground_argument); | |
3757 if (fg_pixel == INVALCOLOR) | |
3758 fg_pixel = gui_get_color((char_u *)"Black"); | |
3759 | |
3760 if (background_argument != NULL) | |
3761 bg_pixel = gui_get_color((char_u *)background_argument); | |
3762 if (bg_pixel == INVALCOLOR) | |
3763 bg_pixel = gui_get_color((char_u *)"White"); | |
3764 | |
3765 if (found_reverse_arg) | |
3766 { | |
3767 gui.def_norm_pixel = bg_pixel; | |
3768 gui.def_back_pixel = fg_pixel; | |
3769 } | |
3770 else | |
3771 { | |
3772 gui.def_norm_pixel = fg_pixel; | |
3773 gui.def_back_pixel = bg_pixel; | |
3774 } | |
3775 | |
3776 /* Get the colors from the "Normal" and "Menu" group (set in syntax.c or | |
3777 * in a vimrc file) */ | |
3778 set_normal_colors(); | |
3779 | |
3780 /* Check that none of the colors are the same as the background color */ | |
3781 gui_check_colors(); | |
3782 | |
3783 /* Get the colors for the highlight groups (gui_check_colors() might have | |
3784 * changed them). */ | |
3785 highlight_gui_started(); /* re-init colors and fonts */ | |
3786 | |
3787 gtk_signal_connect(GTK_OBJECT(gui.mainwin), "destroy", | |
3788 GTK_SIGNAL_FUNC(mainwin_destroy_cb), NULL); | |
3789 | |
3790 #ifdef FEAT_HANGULIN | |
3791 hangul_keyboard_set(); | |
3792 #endif | |
3793 | |
3794 /* | |
3795 * Notify the fixed area about the need to resize the contents of the | |
3796 * gui.formwin, which we use for random positioning of the included | |
3797 * components. | |
3798 * | |
3799 * We connect this signal deferred finally after anything is in place, | |
3800 * since this is intended to handle resizements coming from the window | |
3801 * manager upon us and should not interfere with what VIM is requesting | |
3802 * upon startup. | |
3803 */ | |
3804 gtk_signal_connect(GTK_OBJECT(gui.formwin), "configure_event", | |
3805 GTK_SIGNAL_FUNC(form_configure_event), NULL); | |
3806 | |
3807 #ifdef FEAT_DND | |
2183 | 3808 /* Set up for receiving DND items. */ |
3809 gui_gtk_set_dnd_targets(); | |
7 | 3810 |
3811 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "drag_data_received", | |
3812 GTK_SIGNAL_FUNC(drag_data_received_cb), NULL); | |
3813 #endif | |
3814 | |
3815 /* With GTK+ 2, we need to iconify the window before calling show() | |
2275
e4d849f4df03
Remove the old and not well supported GTK 1 code. (James Vega)
Bram Moolenaar <bram@vim.org>
parents:
2270
diff
changeset
|
3816 * to avoid mapping the window for a short time. */ |
7 | 3817 if (found_iconic_arg && gtk_socket_id == 0) |
3818 gui_mch_iconify(); | |
3819 | |
3820 { | |
2275
e4d849f4df03
Remove the old and not well supported GTK 1 code. (James Vega)
Bram Moolenaar <bram@vim.org>
parents:
2270
diff
changeset
|
3821 #if defined(FEAT_GUI_GNOME) && defined(FEAT_MENU) |
7 | 3822 unsigned long menu_handler = 0; |
3823 # ifdef FEAT_TOOLBAR | |
3824 unsigned long tool_handler = 0; | |
3825 # endif | |
3826 /* | |
3827 * Urgh hackish :/ For some reason BonoboDockLayout always forces a | |
3828 * show when restoring the saved layout configuration. We can't just | |
3829 * hide the widgets again after gtk_widget_show(gui.mainwin) since it's | |
3830 * a toplevel window and thus will be realized immediately. Instead, | |
3831 * connect signal handlers to hide the widgets just after they've been | |
3832 * marked visible, but before the main window is realized. | |
3833 */ | |
3834 if (using_gnome && vim_strchr(p_go, GO_MENUS) == NULL) | |
3835 menu_handler = g_signal_connect_after(gui.menubar_h, "show", | |
3836 G_CALLBACK(>k_widget_hide), | |
3837 NULL); | |
3838 # ifdef FEAT_TOOLBAR | |
3839 if (using_gnome && vim_strchr(p_go, GO_TOOLBAR) == NULL | |
3840 && (toolbar_flags & (TOOLBAR_TEXT | TOOLBAR_ICONS))) | |
3841 tool_handler = g_signal_connect_after(gui.toolbar_h, "show", | |
3842 G_CALLBACK(>k_widget_hide), | |
3843 NULL); | |
3844 # endif | |
3845 #endif | |
3846 gtk_widget_show(gui.mainwin); | |
3847 | |
2275
e4d849f4df03
Remove the old and not well supported GTK 1 code. (James Vega)
Bram Moolenaar <bram@vim.org>
parents:
2270
diff
changeset
|
3848 #if defined(FEAT_GUI_GNOME) && defined(FEAT_MENU) |
7 | 3849 if (menu_handler != 0) |
3850 g_signal_handler_disconnect(gui.menubar_h, menu_handler); | |
3851 # ifdef FEAT_TOOLBAR | |
3852 if (tool_handler != 0) | |
3853 g_signal_handler_disconnect(gui.toolbar_h, tool_handler); | |
3854 # endif | |
3855 #endif | |
3856 } | |
3857 | |
3858 return OK; | |
3859 } | |
3860 | |
3861 | |
3862 void | |
1884 | 3863 gui_mch_exit(int rc UNUSED) |
7 | 3864 { |
3865 if (gui.mainwin != NULL) | |
3866 gtk_widget_destroy(gui.mainwin); | |
3867 } | |
3868 | |
3869 /* | |
3870 * Get the position of the top left corner of the window. | |
3871 */ | |
3872 int | |
3873 gui_mch_get_winpos(int *x, int *y) | |
3874 { | |
3875 gtk_window_get_position(GTK_WINDOW(gui.mainwin), x, y); | |
3876 return OK; | |
3877 } | |
3878 | |
3879 /* | |
3880 * Set the position of the top left corner of the window to the given | |
3881 * coordinates. | |
3882 */ | |
3883 void | |
3884 gui_mch_set_winpos(int x, int y) | |
3885 { | |
3886 gtk_window_move(GTK_WINDOW(gui.mainwin), x, y); | |
3887 } | |
3888 | |
3889 #if 0 | |
3890 static int resize_idle_installed = FALSE; | |
3891 /* | |
3892 * Idle handler to force resize. Used by gui_mch_set_shellsize() to ensure | |
3893 * the shell size doesn't exceed the window size, i.e. if the window manager | |
3894 * ignored our size request. Usually this happens if the window is maximized. | |
3895 * | |
3896 * FIXME: It'd be nice if we could find a little more orthodox solution. | |
3897 * See also the remark below in gui_mch_set_shellsize(). | |
3898 * | |
3899 * DISABLED: When doing ":set lines+=1" this function would first invoke | |
3900 * gui_resize_shell() with the old size, then the normal callback would | |
3901 * report the new size through form_configure_event(). That caused the window | |
3902 * layout to be messed up. | |
3903 */ | |
3904 static gboolean | |
3905 force_shell_resize_idle(gpointer data) | |
3906 { | |
3907 if (gui.mainwin != NULL | |
3908 && GTK_WIDGET_REALIZED(gui.mainwin) | |
3909 && GTK_WIDGET_VISIBLE(gui.mainwin)) | |
3910 { | |
3911 int width; | |
3912 int height; | |
3913 | |
3914 gtk_window_get_size(GTK_WINDOW(gui.mainwin), &width, &height); | |
3915 | |
3916 width -= get_menu_tool_width(); | |
3917 height -= get_menu_tool_height(); | |
3918 | |
3919 gui_resize_shell(width, height); | |
3920 } | |
3921 | |
3922 resize_idle_installed = FALSE; | |
3923 return FALSE; /* don't call me again */ | |
3924 } | |
3925 #endif | |
2275
e4d849f4df03
Remove the old and not well supported GTK 1 code. (James Vega)
Bram Moolenaar <bram@vim.org>
parents:
2270
diff
changeset
|
3926 |
1967 | 3927 /* |
3928 * Return TRUE if the main window is maximized. | |
3929 */ | |
3930 int | |
3931 gui_mch_maximized() | |
3932 { | |
3933 return (gui.mainwin != NULL && gui.mainwin->window != NULL | |
3934 && (gdk_window_get_state(gui.mainwin->window) | |
3935 & GDK_WINDOW_STATE_MAXIMIZED)); | |
3936 } | |
3937 | |
3938 /* | |
3939 * Unmaximize the main window | |
3940 */ | |
3941 void | |
3942 gui_mch_unmaximize() | |
3943 { | |
3944 if (gui.mainwin != NULL) | |
3945 gtk_window_unmaximize(GTK_WINDOW(gui.mainwin)); | |
3946 } | |
3947 | |
7 | 3948 /* |
3014 | 3949 * Called when the font changed while the window is maximized. Compute the |
3950 * new Rows and Columns. This is like resizing the window. | |
3951 */ | |
3952 void | |
3953 gui_mch_newfont() | |
3954 { | |
3955 int w, h; | |
3956 | |
3957 gtk_window_get_size(GTK_WINDOW(gui.mainwin), &w, &h); | |
3958 w -= get_menu_tool_width(); | |
3959 h -= get_menu_tool_height(); | |
3960 gui_resize_shell(w, h); | |
3961 } | |
3962 | |
3963 /* | |
7 | 3964 * Set the windows size. |
3965 */ | |
3966 void | |
3967 gui_mch_set_shellsize(int width, int height, | |
1884 | 3968 int min_width UNUSED, int min_height UNUSED, |
3969 int base_width UNUSED, int base_height UNUSED, | |
3970 int direction UNUSED) | |
7 | 3971 { |
3972 /* give GTK+ a chance to put all widget's into place */ | |
3973 gui_mch_update(); | |
3974 | |
791 | 3975 /* this will cause the proper resizement to happen too */ |
3976 if (gtk_socket_id == 0) | |
856 | 3977 update_window_manager_hints(0, 0); |
791 | 3978 |
7 | 3979 /* With GTK+ 2, changing the size of the form widget doesn't resize |
791 | 3980 * the window. So let's do it the other way around and resize the |
7 | 3981 * main window instead. */ |
3982 width += get_menu_tool_width(); | |
3983 height += get_menu_tool_height(); | |
3984 | |
791 | 3985 if (gtk_socket_id == 0) |
856 | 3986 gtk_window_resize(GTK_WINDOW(gui.mainwin), width, height); |
791 | 3987 else |
856 | 3988 update_window_manager_hints(width, height); |
7 | 3989 |
2183 | 3990 # if 0 |
7 | 3991 if (!resize_idle_installed) |
3992 { | |
3993 g_idle_add_full(GDK_PRIORITY_EVENTS + 10, | |
3994 &force_shell_resize_idle, NULL, NULL); | |
3995 resize_idle_installed = TRUE; | |
3996 } | |
2183 | 3997 # endif |
7 | 3998 /* |
3999 * Wait until all events are processed to prevent a crash because the | |
4000 * real size of the drawing area doesn't reflect Vim's internal ideas. | |
4001 * | |
4002 * This is a bit of a hack, since Vim is a terminal application with a GUI | |
4003 * on top, while the GUI expects to be the boss. | |
4004 */ | |
4005 gui_mch_update(); | |
4006 } | |
4007 | |
4008 | |
4009 /* | |
4010 * The screen size is used to make sure the initial window doesn't get bigger | |
4011 * than the screen. This subtracts some room for menubar, toolbar and window | |
4012 * decorations. | |
4013 */ | |
4014 void | |
4015 gui_mch_get_screen_dimensions(int *screen_w, int *screen_h) | |
4016 { | |
4017 #ifdef HAVE_GTK_MULTIHEAD | |
4018 GdkScreen* screen; | |
4019 | |
4020 if (gui.mainwin != NULL && gtk_widget_has_screen(gui.mainwin)) | |
4021 screen = gtk_widget_get_screen(gui.mainwin); | |
4022 else | |
4023 screen = gdk_screen_get_default(); | |
4024 | |
4025 *screen_w = gdk_screen_get_width(screen); | |
4026 *screen_h = gdk_screen_get_height(screen) - p_ghr; | |
4027 #else | |
4028 *screen_w = gdk_screen_width(); | |
4029 /* Subtract 'guiheadroom' from the height to allow some room for the | |
4030 * window manager (task list and window title bar). */ | |
4031 *screen_h = gdk_screen_height() - p_ghr; | |
4032 #endif | |
4033 | |
4034 /* | |
4035 * FIXME: dirty trick: Because the gui_get_base_height() doesn't include | |
4036 * the toolbar and menubar for GTK, we subtract them from the screen | |
2278
0b3be97064e5
Various small fixes from Dominique Pelle.
Bram Moolenaar <bram@vim.org>
parents:
2275
diff
changeset
|
4037 * height, so that the window size can be made to fit on the screen. |
7 | 4038 * This should be completely changed later. |
4039 */ | |
4040 *screen_w -= get_menu_tool_width(); | |
4041 *screen_h -= get_menu_tool_height(); | |
4042 } | |
4043 | |
4044 #if defined(FEAT_TITLE) || defined(PROTO) | |
4045 void | |
1884 | 4046 gui_mch_settitle(char_u *title, char_u *icon UNUSED) |
7 | 4047 { |
4048 if (title != NULL && output_conv.vc_type != CONV_NONE) | |
4049 title = string_convert(&output_conv, title, NULL); | |
4050 | |
4051 gtk_window_set_title(GTK_WINDOW(gui.mainwin), (const char *)title); | |
4052 | |
4053 if (output_conv.vc_type != CONV_NONE) | |
4054 vim_free(title); | |
4055 } | |
4056 #endif /* FEAT_TITLE */ | |
4057 | |
4058 #if defined(FEAT_MENU) || defined(PROTO) | |
4059 void | |
4060 gui_mch_enable_menu(int showit) | |
4061 { | |
4062 GtkWidget *widget; | |
4063 | |
4064 # ifdef FEAT_GUI_GNOME | |
4065 if (using_gnome) | |
4066 widget = gui.menubar_h; | |
4067 else | |
4068 # endif | |
4069 widget = gui.menubar; | |
4070 | |
827 | 4071 /* Do not disable the menu while starting up, otherwise F10 doesn't work. */ |
4072 if (!showit != !GTK_WIDGET_VISIBLE(widget) && !gui.starting) | |
7 | 4073 { |
4074 if (showit) | |
4075 gtk_widget_show(widget); | |
4076 else | |
4077 gtk_widget_hide(widget); | |
4078 | |
791 | 4079 update_window_manager_hints(0, 0); |
7 | 4080 } |
4081 } | |
4082 #endif /* FEAT_MENU */ | |
4083 | |
4084 #if defined(FEAT_TOOLBAR) || defined(PROTO) | |
4085 void | |
4086 gui_mch_show_toolbar(int showit) | |
4087 { | |
4088 GtkWidget *widget; | |
4089 | |
4090 if (gui.toolbar == NULL) | |
4091 return; | |
4092 | |
4093 # ifdef FEAT_GUI_GNOME | |
4094 if (using_gnome) | |
4095 widget = gui.toolbar_h; | |
4096 else | |
4097 # endif | |
4098 widget = gui.toolbar; | |
4099 | |
4100 if (showit) | |
4101 set_toolbar_style(GTK_TOOLBAR(gui.toolbar)); | |
4102 | |
4103 if (!showit != !GTK_WIDGET_VISIBLE(widget)) | |
4104 { | |
4105 if (showit) | |
4106 gtk_widget_show(widget); | |
4107 else | |
4108 gtk_widget_hide(widget); | |
4109 | |
791 | 4110 update_window_manager_hints(0, 0); |
7 | 4111 } |
4112 } | |
4113 #endif /* FEAT_TOOLBAR */ | |
4114 | |
4115 /* | |
4116 * Check if a given font is a CJK font. This is done in a very crude manner. It | |
4117 * just see if U+04E00 for zh and ja and U+AC00 for ko are covered in a given | |
4118 * font. Consequently, this function cannot be used as a general purpose check | |
4119 * for CJK-ness for which fontconfig APIs should be used. This is only used by | |
4120 * gui_mch_init_font() to deal with 'CJK fixed width fonts'. | |
4121 */ | |
4122 static int | |
4123 is_cjk_font(PangoFontDescription *font_desc) | |
4124 { | |
4125 static const char * const cjk_langs[] = | |
4126 {"zh_CN", "zh_TW", "zh_HK", "ja", "ko"}; | |
4127 | |
4128 PangoFont *font; | |
4129 unsigned i; | |
4130 int is_cjk = FALSE; | |
4131 | |
4132 font = pango_context_load_font(gui.text_context, font_desc); | |
4133 | |
4134 if (font == NULL) | |
4135 return FALSE; | |
4136 | |
4137 for (i = 0; !is_cjk && i < G_N_ELEMENTS(cjk_langs); ++i) | |
4138 { | |
4139 PangoCoverage *coverage; | |
4140 gunichar uc; | |
4141 | |
4142 coverage = pango_font_get_coverage( | |
4143 font, pango_language_from_string(cjk_langs[i])); | |
4144 | |
4145 if (coverage != NULL) | |
4146 { | |
4147 uc = (cjk_langs[i][0] == 'k') ? 0xAC00 : 0x4E00; | |
4148 is_cjk = (pango_coverage_get(coverage, uc) == PANGO_COVERAGE_EXACT); | |
4149 pango_coverage_unref(coverage); | |
4150 } | |
4151 } | |
4152 | |
4153 g_object_unref(font); | |
4154 | |
4155 return is_cjk; | |
4156 } | |
4157 | |
445 | 4158 /* |
4159 * Adjust gui.char_height (after 'linespace' was changed). | |
4160 */ | |
7 | 4161 int |
445 | 4162 gui_mch_adjust_charheight(void) |
7 | 4163 { |
4164 PangoFontMetrics *metrics; | |
4165 int ascent; | |
4166 int descent; | |
4167 | |
4168 metrics = pango_context_get_metrics(gui.text_context, gui.norm_font, | |
4169 pango_context_get_language(gui.text_context)); | |
4170 ascent = pango_font_metrics_get_ascent(metrics); | |
4171 descent = pango_font_metrics_get_descent(metrics); | |
4172 | |
4173 pango_font_metrics_unref(metrics); | |
4174 | |
4175 gui.char_height = (ascent + descent + PANGO_SCALE - 1) / PANGO_SCALE | |
445 | 4176 + p_linespace; |
136 | 4177 /* LINTED: avoid warning: bitwise operation on signed value */ |
7 | 4178 gui.char_ascent = PANGO_PIXELS(ascent + p_linespace * PANGO_SCALE / 2); |
4179 | |
4180 /* A not-positive value of char_height may crash Vim. Only happens | |
4181 * if 'linespace' is negative (which does make sense sometimes). */ | |
4182 gui.char_ascent = MAX(gui.char_ascent, 0); | |
4183 gui.char_height = MAX(gui.char_height, gui.char_ascent + 1); | |
4184 | |
4185 return OK; | |
4186 } | |
4187 | |
4188 /* | |
4189 * Put up a font dialog and return the selected font name in allocated memory. | |
4190 * "oldval" is the previous value. Return NULL when cancelled. | |
4191 * This should probably go into gui_gtk.c. Hmm. | |
4192 * FIXME: | |
4193 * The GTK2 font selection dialog has no filtering API. So we could either | |
4194 * a) implement our own (possibly copying the code from somewhere else) or | |
4195 * b) just live with it. | |
4196 */ | |
4197 char_u * | |
4198 gui_mch_font_dialog(char_u *oldval) | |
4199 { | |
4200 GtkWidget *dialog; | |
4201 int response; | |
4202 char_u *fontname = NULL; | |
4203 char_u *oldname; | |
4204 | |
4205 dialog = gtk_font_selection_dialog_new(NULL); | |
4206 | |
4207 gtk_window_set_transient_for(GTK_WINDOW(dialog), GTK_WINDOW(gui.mainwin)); | |
4208 gtk_window_set_destroy_with_parent(GTK_WINDOW(dialog), TRUE); | |
4209 | |
4210 if (oldval != NULL && oldval[0] != NUL) | |
4211 { | |
4212 if (output_conv.vc_type != CONV_NONE) | |
4213 oldname = string_convert(&output_conv, oldval, NULL); | |
4214 else | |
4215 oldname = oldval; | |
4216 | |
4217 /* Annoying bug in GTK (or Pango): if the font name does not include a | |
4218 * size, zero is used. Use default point size ten. */ | |
4219 if (!vim_isdigit(oldname[STRLEN(oldname) - 1])) | |
4220 { | |
4221 char_u *p = vim_strnsave(oldname, STRLEN(oldname) + 3); | |
4222 | |
4223 if (p != NULL) | |
4224 { | |
4225 STRCPY(p + STRLEN(p), " 10"); | |
4226 if (oldname != oldval) | |
4227 vim_free(oldname); | |
4228 oldname = p; | |
4229 } | |
4230 } | |
4231 | |
4232 gtk_font_selection_dialog_set_font_name( | |
4233 GTK_FONT_SELECTION_DIALOG(dialog), (const char *)oldname); | |
4234 | |
4235 if (oldname != oldval) | |
100 | 4236 vim_free(oldname); |
7 | 4237 } |
1959 | 4238 else |
4239 gtk_font_selection_dialog_set_font_name( | |
4240 GTK_FONT_SELECTION_DIALOG(dialog), DEFAULT_FONT); | |
7 | 4241 |
4242 response = gtk_dialog_run(GTK_DIALOG(dialog)); | |
4243 | |
4244 if (response == GTK_RESPONSE_OK) | |
4245 { | |
4246 char *name; | |
4247 | |
4248 name = gtk_font_selection_dialog_get_font_name( | |
4249 GTK_FONT_SELECTION_DIALOG(dialog)); | |
4250 if (name != NULL) | |
4251 { | |
714 | 4252 char_u *p; |
4253 | |
4254 /* Apparently some font names include a comma, need to escape | |
4255 * that, because in 'guifont' it separates names. */ | |
4256 p = vim_strsave_escaped((char_u *)name, (char_u *)","); | |
4257 g_free(name); | |
840 | 4258 if (p != NULL && input_conv.vc_type != CONV_NONE) |
714 | 4259 { |
4260 fontname = string_convert(&input_conv, p, NULL); | |
4261 vim_free(p); | |
4262 } | |
7 | 4263 else |
714 | 4264 fontname = p; |
7 | 4265 } |
4266 } | |
4267 | |
4268 if (response != GTK_RESPONSE_NONE) | |
4269 gtk_widget_destroy(dialog); | |
4270 | |
4271 return fontname; | |
4272 } | |
4273 | |
4274 /* | |
4275 * Some monospace fonts don't support a bold weight, and fall back | |
4276 * silently to the regular weight. But this is no good since our text | |
4277 * drawing function can emulate bold by overstriking. So let's try | |
4278 * to detect whether bold weight is actually available and emulate it | |
4279 * otherwise. | |
4280 * | |
4281 * Note that we don't need to check for italic style since Xft can | |
4282 * emulate italic on its own, provided you have a proper fontconfig | |
4283 * setup. We wouldn't be able to emulate it in Vim anyway. | |
4284 */ | |
4285 static void | |
4286 get_styled_font_variants(void) | |
4287 { | |
4288 PangoFontDescription *bold_font_desc; | |
4289 PangoFont *plain_font; | |
4290 PangoFont *bold_font; | |
4291 | |
4292 gui.font_can_bold = FALSE; | |
4293 | |
4294 plain_font = pango_context_load_font(gui.text_context, gui.norm_font); | |
4295 | |
4296 if (plain_font == NULL) | |
4297 return; | |
4298 | |
4299 bold_font_desc = pango_font_description_copy_static(gui.norm_font); | |
4300 pango_font_description_set_weight(bold_font_desc, PANGO_WEIGHT_BOLD); | |
4301 | |
4302 bold_font = pango_context_load_font(gui.text_context, bold_font_desc); | |
4303 /* | |
4304 * The comparison relies on the unique handle nature of a PangoFont*, | |
4305 * i.e. it's assumed that a different PangoFont* won't refer to the | |
4306 * same font. Seems to work, and failing here isn't critical anyway. | |
4307 */ | |
4308 if (bold_font != NULL) | |
4309 { | |
4310 gui.font_can_bold = (bold_font != plain_font); | |
4311 g_object_unref(bold_font); | |
4312 } | |
4313 | |
4314 pango_font_description_free(bold_font_desc); | |
4315 g_object_unref(plain_font); | |
4316 } | |
4317 | |
4318 static PangoEngineShape *default_shape_engine = NULL; | |
4319 | |
4320 /* | |
4321 * Create a map from ASCII characters in the range [32,126] to glyphs | |
4322 * of the current font. This is used by gui_gtk2_draw_string() to skip | |
4323 * the itemize and shaping process for the most common case. | |
4324 */ | |
4325 static void | |
4326 ascii_glyph_table_init(void) | |
4327 { | |
4328 char_u ascii_chars[128]; | |
4329 PangoAttrList *attr_list; | |
4330 GList *item_list; | |
4331 int i; | |
4332 | |
4333 if (gui.ascii_glyphs != NULL) | |
4334 pango_glyph_string_free(gui.ascii_glyphs); | |
4335 if (gui.ascii_font != NULL) | |
4336 g_object_unref(gui.ascii_font); | |
4337 | |
4338 gui.ascii_glyphs = NULL; | |
4339 gui.ascii_font = NULL; | |
4340 | |
4341 /* For safety, fill in question marks for the control characters. */ | |
4342 for (i = 0; i < 32; ++i) | |
4343 ascii_chars[i] = '?'; | |
4344 for (; i < 127; ++i) | |
4345 ascii_chars[i] = i; | |
4346 ascii_chars[i] = '?'; | |
4347 | |
4348 attr_list = pango_attr_list_new(); | |
4349 item_list = pango_itemize(gui.text_context, (const char *)ascii_chars, | |
4350 0, sizeof(ascii_chars), attr_list, NULL); | |
4351 | |
4352 if (item_list != NULL && item_list->next == NULL) /* play safe */ | |
4353 { | |
4354 PangoItem *item; | |
4355 int width; | |
4356 | |
4357 item = (PangoItem *)item_list->data; | |
4358 width = gui.char_width * PANGO_SCALE; | |
4359 | |
4360 /* Remember the shape engine used for ASCII. */ | |
4361 default_shape_engine = item->analysis.shape_engine; | |
4362 | |
4363 gui.ascii_font = item->analysis.font; | |
4364 g_object_ref(gui.ascii_font); | |
4365 | |
4366 gui.ascii_glyphs = pango_glyph_string_new(); | |
4367 | |
4368 pango_shape((const char *)ascii_chars, sizeof(ascii_chars), | |
4369 &item->analysis, gui.ascii_glyphs); | |
4370 | |
4371 g_return_if_fail(gui.ascii_glyphs->num_glyphs == sizeof(ascii_chars)); | |
4372 | |
4373 for (i = 0; i < gui.ascii_glyphs->num_glyphs; ++i) | |
4374 { | |
4375 PangoGlyphGeometry *geom; | |
4376 | |
4377 geom = &gui.ascii_glyphs->glyphs[i].geometry; | |
4378 geom->x_offset += MAX(0, width - geom->width) / 2; | |
4379 geom->width = width; | |
4380 } | |
4381 } | |
4382 | |
4383 g_list_foreach(item_list, (GFunc)&pango_item_free, NULL); | |
4384 g_list_free(item_list); | |
4385 pango_attr_list_unref(attr_list); | |
4386 } | |
4387 | |
4388 /* | |
4389 * Initialize Vim to use the font or fontset with the given name. | |
4390 * Return FAIL if the font could not be loaded, OK otherwise. | |
4391 */ | |
4392 int | |
1884 | 4393 gui_mch_init_font(char_u *font_name, int fontset UNUSED) |
7 | 4394 { |
4395 PangoFontDescription *font_desc; | |
4396 PangoLayout *layout; | |
4397 int width; | |
4398 | |
4399 /* If font_name is NULL, this means to use the default, which should | |
4400 * be present on all proper Pango/fontconfig installations. */ | |
4401 if (font_name == NULL) | |
4402 font_name = (char_u *)DEFAULT_FONT; | |
4403 | |
4404 font_desc = gui_mch_get_font(font_name, FALSE); | |
4405 | |
4406 if (font_desc == NULL) | |
4407 return FAIL; | |
4408 | |
4409 gui_mch_free_font(gui.norm_font); | |
4410 gui.norm_font = font_desc; | |
4411 | |
4412 pango_context_set_font_description(gui.text_context, font_desc); | |
4413 | |
4414 layout = pango_layout_new(gui.text_context); | |
4415 pango_layout_set_text(layout, "MW", 2); | |
4416 pango_layout_get_size(layout, &width, NULL); | |
4417 /* | |
4418 * Set char_width to half the width obtained from pango_layout_get_size() | |
4419 * for CJK fixed_width/bi-width fonts. An unpatched version of Xft leads | |
4420 * Pango to use the same width for both non-CJK characters (e.g. Latin | |
4421 * letters and numbers) and CJK characters. This results in 's p a c e d | |
4422 * o u t' rendering when a CJK 'fixed width' font is used. To work around | |
4423 * that, divide the width returned by Pango by 2 if cjk_width is equal to | |
4424 * width for CJK fonts. | |
4425 * | |
4426 * For related bugs, see: | |
4427 * http://bugzilla.gnome.org/show_bug.cgi?id=106618 | |
4428 * http://bugzilla.gnome.org/show_bug.cgi?id=106624 | |
4429 * | |
4430 * With this, for all four of the following cases, Vim works fine: | |
4431 * guifont=CJK_fixed_width_font | |
4432 * guifont=Non_CJK_fixed_font | |
4433 * guifont=Non_CJK_fixed_font,CJK_Fixed_font | |
4434 * guifont=Non_CJK_fixed_font guifontwide=CJK_fixed_font | |
4435 */ | |
4436 if (is_cjk_font(gui.norm_font)) | |
4437 { | |
4438 int cjk_width; | |
4439 | |
4440 /* Measure the text extent of U+4E00 and U+4E8C */ | |
4441 pango_layout_set_text(layout, "\344\270\200\344\272\214", -1); | |
4442 pango_layout_get_size(layout, &cjk_width, NULL); | |
4443 | |
4444 if (width == cjk_width) /* Xft not patched */ | |
4445 width /= 2; | |
4446 } | |
4447 g_object_unref(layout); | |
4448 | |
4449 gui.char_width = (width / 2 + PANGO_SCALE - 1) / PANGO_SCALE; | |
4450 | |
4451 /* A zero width may cause a crash. Happens for semi-invalid fontsets. */ | |
4452 if (gui.char_width <= 0) | |
4453 gui.char_width = 8; | |
4454 | |
445 | 4455 gui_mch_adjust_charheight(); |
7 | 4456 |
4457 /* Set the fontname, which will be used for information purposes */ | |
4458 hl_set_font_name(font_name); | |
4459 | |
4460 get_styled_font_variants(); | |
4461 ascii_glyph_table_init(); | |
4462 | |
4463 /* Avoid unnecessary overhead if 'guifontwide' is equal to 'guifont'. */ | |
4464 if (gui.wide_font != NULL | |
4465 && pango_font_description_equal(gui.norm_font, gui.wide_font)) | |
4466 { | |
4467 pango_font_description_free(gui.wide_font); | |
4468 gui.wide_font = NULL; | |
4469 } | |
4470 | |
1986 | 4471 if (gui_mch_maximized()) |
4472 { | |
4473 /* Update lines and columns in accordance with the new font, keep the | |
4474 * window maximized. */ | |
3014 | 4475 gui_mch_newfont(); |
1986 | 4476 } |
4477 else | |
4478 { | |
4479 /* Preserve the logical dimensions of the screen. */ | |
4480 update_window_manager_hints(0, 0); | |
4481 } | |
7 | 4482 |
4483 return OK; | |
4484 } | |
4485 | |
4486 /* | |
4487 * Get a reference to the font "name". | |
4488 * Return zero for failure. | |
4489 */ | |
4490 GuiFont | |
4491 gui_mch_get_font(char_u *name, int report_error) | |
4492 { | |
4493 PangoFontDescription *font; | |
4494 | |
4495 /* can't do this when GUI is not running */ | |
4496 if (!gui.in_use || name == NULL) | |
4497 return NULL; | |
4498 | |
4499 if (output_conv.vc_type != CONV_NONE) | |
4500 { | |
4501 char_u *buf; | |
4502 | |
4503 buf = string_convert(&output_conv, name, NULL); | |
4504 if (buf != NULL) | |
4505 { | |
4506 font = pango_font_description_from_string((const char *)buf); | |
4507 vim_free(buf); | |
4508 } | |
4509 else | |
4510 font = NULL; | |
4511 } | |
4512 else | |
4513 font = pango_font_description_from_string((const char *)name); | |
4514 | |
4515 if (font != NULL) | |
4516 { | |
4517 PangoFont *real_font; | |
4518 | |
4519 /* pango_context_load_font() bails out if no font size is set */ | |
4520 if (pango_font_description_get_size(font) <= 0) | |
4521 pango_font_description_set_size(font, 10 * PANGO_SCALE); | |
4522 | |
4523 real_font = pango_context_load_font(gui.text_context, font); | |
4524 | |
4525 if (real_font == NULL) | |
4526 { | |
4527 pango_font_description_free(font); | |
4528 font = NULL; | |
4529 } | |
4530 else | |
4531 g_object_unref(real_font); | |
4532 } | |
4533 | |
4534 if (font == NULL) | |
4535 { | |
4536 if (report_error) | |
1666 | 4537 EMSG2(_((char *)e_font), name); |
7 | 4538 return NULL; |
4539 } | |
4540 | |
4541 return font; | |
4542 } | |
4543 | |
39 | 4544 #if defined(FEAT_EVAL) || defined(PROTO) |
38 | 4545 /* |
4546 * Return the name of font "font" in allocated memory. | |
4547 */ | |
4548 char_u * | |
1884 | 4549 gui_mch_get_fontname(GuiFont font, char_u *name UNUSED) |
38 | 4550 { |
4551 if (font != NOFONT) | |
4552 { | |
944 | 4553 char *pangoname = pango_font_description_to_string(font); |
4554 | |
4555 if (pangoname != NULL) | |
38 | 4556 { |
944 | 4557 char_u *s = vim_strsave((char_u *)pangoname); |
4558 | |
4559 g_free(pangoname); | |
38 | 4560 return s; |
4561 } | |
4562 } | |
4563 return NULL; | |
4564 } | |
39 | 4565 #endif |
38 | 4566 |
7 | 4567 /* |
4568 * If a font is not going to be used, free its structure. | |
4569 */ | |
4570 void | |
4571 gui_mch_free_font(GuiFont font) | |
4572 { | |
4573 if (font != NOFONT) | |
4574 pango_font_description_free(font); | |
4575 } | |
4576 | |
4577 /* | |
4578 * Return the Pixel value (color) for the given color name. This routine was | |
4579 * pretty much taken from example code in the Silicon Graphics OSF/Motif | |
4580 * Programmer's Guide. | |
4581 * Return INVALCOLOR for error. | |
4582 */ | |
4583 guicolor_T | |
4584 gui_mch_get_color(char_u *name) | |
4585 { | |
4586 /* A number of colors that some X11 systems don't have */ | |
4587 static const char *const vimnames[][2] = | |
4588 { | |
834 | 4589 {"LightRed", "#FFBBBB"}, |
4590 {"LightGreen", "#88FF88"}, | |
4591 {"LightMagenta","#FFBBFF"}, | |
4592 {"DarkCyan", "#008888"}, | |
4593 {"DarkBlue", "#0000BB"}, | |
4594 {"DarkRed", "#BB0000"}, | |
4595 {"DarkMagenta", "#BB00BB"}, | |
4596 {"DarkGrey", "#BBBBBB"}, | |
4597 {"DarkYellow", "#BBBB00"}, | |
4598 {"Gray10", "#1A1A1A"}, | |
4599 {"Grey10", "#1A1A1A"}, | |
4600 {"Gray20", "#333333"}, | |
4601 {"Grey20", "#333333"}, | |
4602 {"Gray30", "#4D4D4D"}, | |
4603 {"Grey30", "#4D4D4D"}, | |
4604 {"Gray40", "#666666"}, | |
4605 {"Grey40", "#666666"}, | |
4606 {"Gray50", "#7F7F7F"}, | |
4607 {"Grey50", "#7F7F7F"}, | |
4608 {"Gray60", "#999999"}, | |
4609 {"Grey60", "#999999"}, | |
4610 {"Gray70", "#B3B3B3"}, | |
4611 {"Grey70", "#B3B3B3"}, | |
4612 {"Gray80", "#CCCCCC"}, | |
4613 {"Grey80", "#CCCCCC"}, | |
4614 {"Gray90", "#E5E5E5"}, | |
4615 {"Grey90", "#E5E5E5"}, | |
7 | 4616 {NULL, NULL} |
4617 }; | |
4618 | |
4619 if (!gui.in_use) /* can't do this when GUI not running */ | |
4620 return INVALCOLOR; | |
4621 | |
4622 while (name != NULL) | |
4623 { | |
4624 GdkColor color; | |
4625 int parsed; | |
4626 int i; | |
4627 | |
4628 parsed = gdk_color_parse((const char *)name, &color); | |
4629 | |
4630 if (parsed) | |
4631 { | |
4632 gdk_colormap_alloc_color(gtk_widget_get_colormap(gui.drawarea), | |
4633 &color, FALSE, TRUE); | |
4634 return (guicolor_T)color.pixel; | |
4635 } | |
4636 /* add a few builtin names and try again */ | |
4637 for (i = 0; ; ++i) | |
4638 { | |
4639 if (vimnames[i][0] == NULL) | |
4640 { | |
4641 name = NULL; | |
4642 break; | |
4643 } | |
4644 if (STRICMP(name, vimnames[i][0]) == 0) | |
4645 { | |
4646 name = (char_u *)vimnames[i][1]; | |
4647 break; | |
4648 } | |
4649 } | |
4650 } | |
4651 | |
4652 return INVALCOLOR; | |
4653 } | |
4654 | |
4655 /* | |
4656 * Set the current text foreground color. | |
4657 */ | |
4658 void | |
4659 gui_mch_set_fg_color(guicolor_T color) | |
4660 { | |
4661 gui.fgcolor->pixel = (unsigned long)color; | |
4662 } | |
4663 | |
4664 /* | |
4665 * Set the current text background color. | |
4666 */ | |
4667 void | |
4668 gui_mch_set_bg_color(guicolor_T color) | |
4669 { | |
4670 gui.bgcolor->pixel = (unsigned long)color; | |
4671 } | |
4672 | |
207 | 4673 /* |
4674 * Set the current text special color. | |
4675 */ | |
4676 void | |
4677 gui_mch_set_sp_color(guicolor_T color) | |
4678 { | |
4679 gui.spcolor->pixel = (unsigned long)color; | |
4680 } | |
4681 | |
7 | 4682 /* |
4683 * Function-like convenience macro for the sake of efficiency. | |
4684 */ | |
4685 #define INSERT_PANGO_ATTR(Attribute, AttrList, Start, End) \ | |
4686 G_STMT_START{ \ | |
4687 PangoAttribute *tmp_attr_; \ | |
4688 tmp_attr_ = (Attribute); \ | |
4689 tmp_attr_->start_index = (Start); \ | |
4690 tmp_attr_->end_index = (End); \ | |
4691 pango_attr_list_insert((AttrList), tmp_attr_); \ | |
4692 }G_STMT_END | |
4693 | |
4694 static void | |
4695 apply_wide_font_attr(char_u *s, int len, PangoAttrList *attr_list) | |
4696 { | |
4697 char_u *start = NULL; | |
4698 char_u *p; | |
4699 int uc; | |
4700 | |
4701 for (p = s; p < s + len; p += utf_byte2len(*p)) | |
4702 { | |
4703 uc = utf_ptr2char(p); | |
4704 | |
4705 if (start == NULL) | |
4706 { | |
4707 if (uc >= 0x80 && utf_char2cells(uc) == 2) | |
4708 start = p; | |
4709 } | |
4710 else if (uc < 0x80 /* optimization shortcut */ | |
4711 || (utf_char2cells(uc) != 2 && !utf_iscomposing(uc))) | |
4712 { | |
4713 INSERT_PANGO_ATTR(pango_attr_font_desc_new(gui.wide_font), | |
4714 attr_list, start - s, p - s); | |
4715 start = NULL; | |
4716 } | |
4717 } | |
4718 | |
4719 if (start != NULL) | |
4720 INSERT_PANGO_ATTR(pango_attr_font_desc_new(gui.wide_font), | |
4721 attr_list, start - s, len); | |
4722 } | |
4723 | |
4724 static int | |
4725 count_cluster_cells(char_u *s, PangoItem *item, | |
4726 PangoGlyphString* glyphs, int i, | |
4727 int *cluster_width, | |
4728 int *last_glyph_rbearing) | |
4729 { | |
4730 char_u *p; | |
4731 int next; /* glyph start index of next cluster */ | |
4732 int start, end; /* string segment of current cluster */ | |
4733 int width; /* real cluster width in Pango units */ | |
4734 int uc; | |
4735 int cellcount = 0; | |
4736 | |
4737 width = glyphs->glyphs[i].geometry.width; | |
4738 | |
4739 for (next = i + 1; next < glyphs->num_glyphs; ++next) | |
4740 { | |
4741 if (glyphs->glyphs[next].attr.is_cluster_start) | |
4742 break; | |
4743 else if (glyphs->glyphs[next].geometry.width > width) | |
4744 width = glyphs->glyphs[next].geometry.width; | |
4745 } | |
4746 | |
4747 start = item->offset + glyphs->log_clusters[i]; | |
4748 end = item->offset + ((next < glyphs->num_glyphs) ? | |
4749 glyphs->log_clusters[next] : item->length); | |
4750 | |
4751 for (p = s + start; p < s + end; p += utf_byte2len(*p)) | |
4752 { | |
4753 uc = utf_ptr2char(p); | |
4754 if (uc < 0x80) | |
4755 ++cellcount; | |
4756 else if (!utf_iscomposing(uc)) | |
4757 cellcount += utf_char2cells(uc); | |
4758 } | |
4759 | |
4760 if (last_glyph_rbearing != NULL | |
4761 && cellcount > 0 && next == glyphs->num_glyphs) | |
4762 { | |
4763 PangoRectangle ink_rect; | |
4764 /* | |
4765 * If a certain combining mark had to be taken from a non-monospace | |
4766 * font, we have to compensate manually by adapting x_offset according | |
4767 * to the ink extents of the previous glyph. | |
4768 */ | |
4769 pango_font_get_glyph_extents(item->analysis.font, | |
4770 glyphs->glyphs[i].glyph, | |
4771 &ink_rect, NULL); | |
4772 | |
4773 if (PANGO_RBEARING(ink_rect) > 0) | |
4774 *last_glyph_rbearing = PANGO_RBEARING(ink_rect); | |
4775 } | |
4776 | |
4777 if (cellcount > 0) | |
4778 *cluster_width = width; | |
4779 | |
4780 return cellcount; | |
4781 } | |
4782 | |
4783 /* | |
4784 * If there are only combining characters in the cluster, we cannot just | |
4785 * change the width of the previous glyph since there is none. Therefore | |
4786 * some guesswork is needed. | |
4787 * | |
4788 * If ink_rect.x is negative Pango apparently has taken care of the composing | |
4789 * by itself. Actually setting x_offset = 0 should be sufficient then, but due | |
4790 * to problems with composing from different fonts we still need to fine-tune | |
2278
0b3be97064e5
Various small fixes from Dominique Pelle.
Bram Moolenaar <bram@vim.org>
parents:
2275
diff
changeset
|
4791 * x_offset to avoid ugliness. |
7 | 4792 * |
4793 * If ink_rect.x is not negative, force overstriking by pointing x_offset to | |
4794 * the position of the previous glyph. Apparently this happens only with old | |
4795 * X fonts which don't provide the special combining information needed by | |
4796 * Pango. | |
4797 */ | |
4798 static void | |
4799 setup_zero_width_cluster(PangoItem *item, PangoGlyphInfo *glyph, | |
4800 int last_cellcount, int last_cluster_width, | |
4801 int last_glyph_rbearing) | |
4802 { | |
4803 PangoRectangle ink_rect; | |
4804 PangoRectangle logical_rect; | |
4805 int width; | |
4806 | |
4807 width = last_cellcount * gui.char_width * PANGO_SCALE; | |
4808 glyph->geometry.x_offset = -width + MAX(0, width - last_cluster_width) / 2; | |
4809 glyph->geometry.width = 0; | |
4810 | |
4811 pango_font_get_glyph_extents(item->analysis.font, | |
4812 glyph->glyph, | |
4813 &ink_rect, &logical_rect); | |
4814 if (ink_rect.x < 0) | |
4815 { | |
4816 glyph->geometry.x_offset += last_glyph_rbearing; | |
4817 glyph->geometry.y_offset = logical_rect.height | |
4818 - (gui.char_height - p_linespace) * PANGO_SCALE; | |
4819 } | |
2507
2070b63605a7
Fix: with newer GTK versions accented characters were drawn too much to the
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
4820 else |
2070b63605a7
Fix: with newer GTK versions accented characters were drawn too much to the
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
4821 /* If the accent width is smaller than the cluster width, position it |
2070b63605a7
Fix: with newer GTK versions accented characters were drawn too much to the
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
4822 * in the middle. */ |
2070b63605a7
Fix: with newer GTK versions accented characters were drawn too much to the
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
4823 glyph->geometry.x_offset = -width + MAX(0, width - ink_rect.width) / 2; |
7 | 4824 } |
4825 | |
4826 static void | |
4827 draw_glyph_string(int row, int col, int num_cells, int flags, | |
4828 PangoFont *font, PangoGlyphString *glyphs) | |
4829 { | |
4830 if (!(flags & DRAW_TRANSP)) | |
4831 { | |
4832 gdk_gc_set_foreground(gui.text_gc, gui.bgcolor); | |
4833 | |
4834 gdk_draw_rectangle(gui.drawarea->window, | |
4835 gui.text_gc, | |
4836 TRUE, | |
4837 FILL_X(col), | |
4838 FILL_Y(row), | |
4839 num_cells * gui.char_width, | |
4840 gui.char_height); | |
4841 } | |
4842 | |
4843 gdk_gc_set_foreground(gui.text_gc, gui.fgcolor); | |
4844 | |
4845 gdk_draw_glyphs(gui.drawarea->window, | |
4846 gui.text_gc, | |
4847 font, | |
4848 TEXT_X(col), | |
4849 TEXT_Y(row), | |
4850 glyphs); | |
4851 | |
4852 /* redraw the contents with an offset of 1 to emulate bold */ | |
4853 if ((flags & DRAW_BOLD) && !gui.font_can_bold) | |
4854 gdk_draw_glyphs(gui.drawarea->window, | |
4855 gui.text_gc, | |
4856 font, | |
4857 TEXT_X(col) + 1, | |
4858 TEXT_Y(row), | |
4859 glyphs); | |
4860 } | |
4861 | |
207 | 4862 /* |
4863 * Draw underline and undercurl at the bottom of the character cell. | |
4864 */ | |
4865 static void | |
4866 draw_under(int flags, int row, int col, int cells) | |
4867 { | |
4868 int i; | |
4869 int offset; | |
1884 | 4870 static const int val[8] = {1, 0, 0, 0, 1, 2, 2, 2 }; |
207 | 4871 int y = FILL_Y(row + 1) - 1; |
4872 | |
4873 /* Undercurl: draw curl at the bottom of the character cell. */ | |
4874 if (flags & DRAW_UNDERC) | |
4875 { | |
4876 gdk_gc_set_foreground(gui.text_gc, gui.spcolor); | |
4877 for (i = FILL_X(col); i < FILL_X(col + cells); ++i) | |
4878 { | |
4879 offset = val[i % 8]; | |
4880 gdk_draw_point(gui.drawarea->window, gui.text_gc, i, y - offset); | |
4881 } | |
4882 gdk_gc_set_foreground(gui.text_gc, gui.fgcolor); | |
4883 } | |
4884 | |
4885 /* Underline: draw a line at the bottom of the character cell. */ | |
4886 if (flags & DRAW_UNDERL) | |
4887 { | |
4888 /* When p_linespace is 0, overwrite the bottom row of pixels. | |
4889 * Otherwise put the line just below the character. */ | |
4890 if (p_linespace > 1) | |
4891 y -= p_linespace - 1; | |
4892 gdk_draw_line(gui.drawarea->window, gui.text_gc, | |
4893 FILL_X(col), y, | |
4894 FILL_X(col + cells) - 1, y); | |
4895 } | |
4896 } | |
4897 | |
7 | 4898 int |
4899 gui_gtk2_draw_string(int row, int col, char_u *s, int len, int flags) | |
4900 { | |
4901 GdkRectangle area; /* area for clip mask */ | |
4902 PangoGlyphString *glyphs; /* glyphs of current item */ | |
4903 int column_offset = 0; /* column offset in cells */ | |
4904 int i; | |
4905 char_u *conv_buf = NULL; /* result of UTF-8 conversion */ | |
4906 char_u *new_conv_buf; | |
4907 int convlen; | |
4908 char_u *sp, *bp; | |
4909 int plen; | |
4910 | |
4911 if (gui.text_context == NULL || gui.drawarea->window == NULL) | |
4912 return len; | |
4913 | |
4914 if (output_conv.vc_type != CONV_NONE) | |
4915 { | |
4916 /* | |
4917 * Convert characters from 'encoding' to 'termencoding', which is set | |
4918 * to UTF-8 by gui_mch_init(). did_set_string_option() in option.c | |
4919 * prohibits changing this to something else than UTF-8 if the GUI is | |
4920 * in use. | |
4921 */ | |
4922 convlen = len; | |
4923 conv_buf = string_convert(&output_conv, s, &convlen); | |
4924 g_return_val_if_fail(conv_buf != NULL, len); | |
4925 | |
4926 /* Correct for differences in char width: some chars are | |
4927 * double-wide in 'encoding' but single-wide in utf-8. Add a space to | |
4928 * compensate for that. */ | |
4929 for (sp = s, bp = conv_buf; sp < s + len && bp < conv_buf + convlen; ) | |
4930 { | |
474 | 4931 plen = utf_ptr2len(bp); |
7 | 4932 if ((*mb_ptr2cells)(sp) == 2 && utf_ptr2cells(bp) == 1) |
4933 { | |
4934 new_conv_buf = alloc(convlen + 2); | |
4935 if (new_conv_buf == NULL) | |
4936 return len; | |
4937 plen += bp - conv_buf; | |
4938 mch_memmove(new_conv_buf, conv_buf, plen); | |
4939 new_conv_buf[plen] = ' '; | |
4940 mch_memmove(new_conv_buf + plen + 1, conv_buf + plen, | |
4941 convlen - plen + 1); | |
4942 vim_free(conv_buf); | |
4943 conv_buf = new_conv_buf; | |
4944 ++convlen; | |
4945 bp = conv_buf + plen; | |
4946 plen = 1; | |
4947 } | |
474 | 4948 sp += (*mb_ptr2len)(sp); |
7 | 4949 bp += plen; |
4950 } | |
4951 s = conv_buf; | |
4952 len = convlen; | |
4953 } | |
4954 | |
4955 /* | |
4956 * Restrict all drawing to the current screen line in order to prevent | |
4957 * fuzzy font lookups from messing up the screen. | |
4958 */ | |
4959 area.x = gui.border_offset; | |
4960 area.y = FILL_Y(row); | |
4961 area.width = gui.num_cols * gui.char_width; | |
4962 area.height = gui.char_height; | |
4963 | |
4964 gdk_gc_set_clip_origin(gui.text_gc, 0, 0); | |
4965 gdk_gc_set_clip_rectangle(gui.text_gc, &area); | |
4966 | |
4967 glyphs = pango_glyph_string_new(); | |
4968 | |
4969 /* | |
4970 * Optimization hack: If possible, skip the itemize and shaping process | |
4971 * for pure ASCII strings. This optimization is particularly effective | |
4972 * because Vim draws space characters to clear parts of the screen. | |
4973 */ | |
4974 if (!(flags & DRAW_ITALIC) | |
4975 && !((flags & DRAW_BOLD) && gui.font_can_bold) | |
4976 && gui.ascii_glyphs != NULL) | |
4977 { | |
4978 char_u *p; | |
4979 | |
4980 for (p = s; p < s + len; ++p) | |
4981 if (*p & 0x80) | |
4982 goto not_ascii; | |
4983 | |
4984 pango_glyph_string_set_size(glyphs, len); | |
4985 | |
4986 for (i = 0; i < len; ++i) | |
4987 { | |
4988 glyphs->glyphs[i] = gui.ascii_glyphs->glyphs[s[i]]; | |
4989 glyphs->log_clusters[i] = i; | |
4990 } | |
4991 | |
4992 draw_glyph_string(row, col, len, flags, gui.ascii_font, glyphs); | |
4993 | |
4994 column_offset = len; | |
4995 } | |
4996 else | |
4997 not_ascii: | |
4998 { | |
4999 PangoAttrList *attr_list; | |
5000 GList *item_list; | |
5001 int cluster_width; | |
5002 int last_glyph_rbearing; | |
5003 int cells = 0; /* cells occupied by current cluster */ | |
5004 | |
26 | 5005 /* Safety check: pango crashes when invoked with invalid utf-8 |
5006 * characters. */ | |
5007 if (!utf_valid_string(s, s + len)) | |
5008 { | |
5009 column_offset = len; | |
5010 goto skipitall; | |
5011 } | |
5012 | |
7 | 5013 /* original width of the current cluster */ |
5014 cluster_width = PANGO_SCALE * gui.char_width; | |
5015 | |
5016 /* right bearing of the last non-composing glyph */ | |
5017 last_glyph_rbearing = PANGO_SCALE * gui.char_width; | |
5018 | |
5019 attr_list = pango_attr_list_new(); | |
5020 | |
5021 /* If 'guifontwide' is set then use that for double-width characters. | |
5022 * Otherwise just go with 'guifont' and let Pango do its thing. */ | |
5023 if (gui.wide_font != NULL) | |
5024 apply_wide_font_attr(s, len, attr_list); | |
5025 | |
5026 if ((flags & DRAW_BOLD) && gui.font_can_bold) | |
5027 INSERT_PANGO_ATTR(pango_attr_weight_new(PANGO_WEIGHT_BOLD), | |
5028 attr_list, 0, len); | |
5029 if (flags & DRAW_ITALIC) | |
5030 INSERT_PANGO_ATTR(pango_attr_style_new(PANGO_STYLE_ITALIC), | |
5031 attr_list, 0, len); | |
5032 /* | |
5033 * Break the text into segments with consistent directional level | |
5034 * and shaping engine. Pure Latin text needs only a single segment, | |
5035 * so there's no need to worry about the loop's efficiency. Better | |
5036 * try to optimize elsewhere, e.g. reducing exposes and stuff :) | |
5037 */ | |
5038 item_list = pango_itemize(gui.text_context, | |
5039 (const char *)s, 0, len, attr_list, NULL); | |
5040 | |
5041 while (item_list != NULL) | |
5042 { | |
5043 PangoItem *item; | |
5044 int item_cells = 0; /* item length in cells */ | |
5045 | |
5046 item = (PangoItem *)item_list->data; | |
5047 item_list = g_list_delete_link(item_list, item_list); | |
5048 /* | |
5049 * Increment the bidirectional embedding level by 1 if it is not | |
5050 * even. An odd number means the output will be RTL, but we don't | |
5051 * want that since Vim handles right-to-left text on its own. It | |
5052 * would probably be sufficient to just set level = 0, but you can | |
5053 * never know :) | |
5054 * | |
5055 * Unfortunately we can't take advantage of Pango's ability to | |
5056 * render both LTR and RTL at the same time. In order to support | |
5057 * that, Vim's main screen engine would have to make use of Pango | |
5058 * functionality. | |
5059 */ | |
5060 item->analysis.level = (item->analysis.level + 1) & (~1U); | |
5061 | |
5062 /* HACK: Overrule the shape engine, we don't want shaping to be | |
5063 * done, because drawing the cursor would change the display. */ | |
5064 item->analysis.shape_engine = default_shape_engine; | |
5065 | |
7098
70b56e5eccb2
commit https://github.com/vim/vim/commit/3cbe0c01ad71875bd662edb629f9e792a734f292
Christian Brabandt <cb@256bit.org>
parents:
7086
diff
changeset
|
5066 #ifdef HAVE_PANGO_SHAPE_FULL |
7086
3825da022231
commit https://github.com/vim/vim/commit/7e2ec008f5c5152205d0b8a7d88177b374225d8d
Christian Brabandt <cb@256bit.org>
parents:
6819
diff
changeset
|
5067 pango_shape_full((const char *)s + item->offset, item->length, |
3825da022231
commit https://github.com/vim/vim/commit/7e2ec008f5c5152205d0b8a7d88177b374225d8d
Christian Brabandt <cb@256bit.org>
parents:
6819
diff
changeset
|
5068 (const char *)s, len, &item->analysis, glyphs); |
7098
70b56e5eccb2
commit https://github.com/vim/vim/commit/3cbe0c01ad71875bd662edb629f9e792a734f292
Christian Brabandt <cb@256bit.org>
parents:
7086
diff
changeset
|
5069 #else |
70b56e5eccb2
commit https://github.com/vim/vim/commit/3cbe0c01ad71875bd662edb629f9e792a734f292
Christian Brabandt <cb@256bit.org>
parents:
7086
diff
changeset
|
5070 pango_shape((const char *)s + item->offset, item->length, |
70b56e5eccb2
commit https://github.com/vim/vim/commit/3cbe0c01ad71875bd662edb629f9e792a734f292
Christian Brabandt <cb@256bit.org>
parents:
7086
diff
changeset
|
5071 &item->analysis, glyphs); |
70b56e5eccb2
commit https://github.com/vim/vim/commit/3cbe0c01ad71875bd662edb629f9e792a734f292
Christian Brabandt <cb@256bit.org>
parents:
7086
diff
changeset
|
5072 #endif |
7 | 5073 /* |
5074 * Fixed-width hack: iterate over the array and assign a fixed | |
5075 * width to each glyph, thus overriding the choice made by the | |
5076 * shaping engine. We use utf_char2cells() to determine the | |
5077 * number of cells needed. | |
5078 * | |
5079 * Also perform all kind of dark magic to get composing | |
5080 * characters right (and pretty too of course). | |
5081 */ | |
5082 for (i = 0; i < glyphs->num_glyphs; ++i) | |
5083 { | |
5084 PangoGlyphInfo *glyph; | |
5085 | |
5086 glyph = &glyphs->glyphs[i]; | |
5087 | |
5088 if (glyph->attr.is_cluster_start) | |
5089 { | |
5090 int cellcount; | |
5091 | |
5092 cellcount = count_cluster_cells( | |
5093 s, item, glyphs, i, &cluster_width, | |
5094 (item_list != NULL) ? &last_glyph_rbearing : NULL); | |
5095 | |
5096 if (cellcount > 0) | |
5097 { | |
5098 int width; | |
5099 | |
5100 width = cellcount * gui.char_width * PANGO_SCALE; | |
5101 glyph->geometry.x_offset += | |
5102 MAX(0, width - cluster_width) / 2; | |
5103 glyph->geometry.width = width; | |
5104 } | |
5105 else | |
5106 { | |
5107 /* If there are only combining characters in the | |
5108 * cluster, we cannot just change the width of the | |
5109 * previous glyph since there is none. Therefore | |
5110 * some guesswork is needed. */ | |
5111 setup_zero_width_cluster(item, glyph, cells, | |
5112 cluster_width, | |
5113 last_glyph_rbearing); | |
5114 } | |
5115 | |
5116 item_cells += cellcount; | |
5117 cells = cellcount; | |
5118 } | |
5119 else if (i > 0) | |
5120 { | |
5121 int width; | |
5122 | |
5123 /* There is a previous glyph, so we deal with combining | |
2507
2070b63605a7
Fix: with newer GTK versions accented characters were drawn too much to the
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
5124 * characters the canonical way. |
2517
fe5709686d05
Improve positioning of combining characters in GTK.
Bram Moolenaar <bram@vim.org>
parents:
2507
diff
changeset
|
5125 * In some circumstances Pango uses a positive x_offset, |
fe5709686d05
Improve positioning of combining characters in GTK.
Bram Moolenaar <bram@vim.org>
parents:
2507
diff
changeset
|
5126 * then use the width of the previous glyph for this one |
fe5709686d05
Improve positioning of combining characters in GTK.
Bram Moolenaar <bram@vim.org>
parents:
2507
diff
changeset
|
5127 * and set the previous width to zero. |
fe5709686d05
Improve positioning of combining characters in GTK.
Bram Moolenaar <bram@vim.org>
parents:
2507
diff
changeset
|
5128 * Otherwise we get a negative x_offset, Pango has already |
fe5709686d05
Improve positioning of combining characters in GTK.
Bram Moolenaar <bram@vim.org>
parents:
2507
diff
changeset
|
5129 * positioned the combining char, keep the widths as they |
fe5709686d05
Improve positioning of combining characters in GTK.
Bram Moolenaar <bram@vim.org>
parents:
2507
diff
changeset
|
5130 * are. |
2507
2070b63605a7
Fix: with newer GTK versions accented characters were drawn too much to the
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
5131 * For both adjust the x_offset to position the glyph in |
2517
fe5709686d05
Improve positioning of combining characters in GTK.
Bram Moolenaar <bram@vim.org>
parents:
2507
diff
changeset
|
5132 * the middle. */ |
2507
2070b63605a7
Fix: with newer GTK versions accented characters were drawn too much to the
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
5133 if (glyph->geometry.x_offset >= 0) |
2517
fe5709686d05
Improve positioning of combining characters in GTK.
Bram Moolenaar <bram@vim.org>
parents:
2507
diff
changeset
|
5134 { |
fe5709686d05
Improve positioning of combining characters in GTK.
Bram Moolenaar <bram@vim.org>
parents:
2507
diff
changeset
|
5135 glyphs->glyphs[i].geometry.width = |
fe5709686d05
Improve positioning of combining characters in GTK.
Bram Moolenaar <bram@vim.org>
parents:
2507
diff
changeset
|
5136 glyphs->glyphs[i - 1].geometry.width; |
2507
2070b63605a7
Fix: with newer GTK versions accented characters were drawn too much to the
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
5137 glyphs->glyphs[i - 1].geometry.width = 0; |
2517
fe5709686d05
Improve positioning of combining characters in GTK.
Bram Moolenaar <bram@vim.org>
parents:
2507
diff
changeset
|
5138 } |
7 | 5139 width = cells * gui.char_width * PANGO_SCALE; |
5140 glyph->geometry.x_offset += | |
5141 MAX(0, width - cluster_width) / 2; | |
5142 } | |
5143 else /* i == 0 "cannot happen" */ | |
5144 { | |
5145 glyph->geometry.width = 0; | |
5146 } | |
5147 } | |
5148 | |
5149 /*** Aaaaand action! ***/ | |
5150 draw_glyph_string(row, col + column_offset, item_cells, | |
5151 flags, item->analysis.font, glyphs); | |
5152 | |
5153 pango_item_free(item); | |
5154 | |
5155 column_offset += item_cells; | |
5156 } | |
5157 | |
5158 pango_attr_list_unref(attr_list); | |
5159 } | |
5160 | |
26 | 5161 skipitall: |
207 | 5162 /* Draw underline and undercurl. */ |
5163 draw_under(flags, row, col, column_offset); | |
7 | 5164 |
5165 pango_glyph_string_free(glyphs); | |
5166 vim_free(conv_buf); | |
5167 | |
5168 gdk_gc_set_clip_rectangle(gui.text_gc, NULL); | |
5169 | |
5170 return column_offset; | |
5171 } | |
5172 | |
5173 /* | |
5174 * Return OK if the key with the termcap name "name" is supported. | |
5175 */ | |
5176 int | |
5177 gui_mch_haskey(char_u *name) | |
5178 { | |
5179 int i; | |
5180 | |
5181 for (i = 0; special_keys[i].key_sym != 0; i++) | |
5182 if (name[0] == special_keys[i].code0 | |
5183 && name[1] == special_keys[i].code1) | |
5184 return OK; | |
5185 return FAIL; | |
5186 } | |
5187 | |
4133 | 5188 #if defined(FEAT_TITLE) || defined(FEAT_EVAL) || defined(PROTO) |
7 | 5189 /* |
5190 * Return the text window-id and display. Only required for X-based GUI's | |
5191 */ | |
5192 int | |
5193 gui_get_x11_windis(Window *win, Display **dis) | |
5194 { | |
5195 if (gui.mainwin != NULL && gui.mainwin->window != NULL) | |
5196 { | |
5197 *dis = GDK_WINDOW_XDISPLAY(gui.mainwin->window); | |
5198 *win = GDK_WINDOW_XWINDOW(gui.mainwin->window); | |
5199 return OK; | |
5200 } | |
5201 | |
5202 *dis = NULL; | |
5203 *win = 0; | |
5204 return FAIL; | |
5205 } | |
5206 #endif | |
5207 | |
5208 #if defined(FEAT_CLIENTSERVER) \ | |
5209 || (defined(FEAT_X11) && defined(FEAT_CLIPBOARD)) || defined(PROTO) | |
5210 | |
5211 Display * | |
5212 gui_mch_get_display(void) | |
5213 { | |
5214 if (gui.mainwin != NULL && gui.mainwin->window != NULL) | |
5215 return GDK_WINDOW_XDISPLAY(gui.mainwin->window); | |
5216 else | |
5217 return NULL; | |
5218 } | |
5219 #endif | |
5220 | |
5221 void | |
5222 gui_mch_beep(void) | |
5223 { | |
5224 #ifdef HAVE_GTK_MULTIHEAD | |
5225 GdkDisplay *display; | |
5226 | |
5227 if (gui.mainwin != NULL && GTK_WIDGET_REALIZED(gui.mainwin)) | |
5228 display = gtk_widget_get_display(gui.mainwin); | |
5229 else | |
5230 display = gdk_display_get_default(); | |
5231 | |
5232 if (display != NULL) | |
5233 gdk_display_beep(display); | |
5234 #else | |
5235 gdk_beep(); | |
5236 #endif | |
5237 } | |
5238 | |
5239 void | |
5240 gui_mch_flash(int msec) | |
5241 { | |
5242 GdkGCValues values; | |
5243 GdkGC *invert_gc; | |
5244 | |
5245 if (gui.drawarea->window == NULL) | |
5246 return; | |
5247 | |
5248 values.foreground.pixel = gui.norm_pixel ^ gui.back_pixel; | |
5249 values.background.pixel = gui.norm_pixel ^ gui.back_pixel; | |
5250 values.function = GDK_XOR; | |
5251 invert_gc = gdk_gc_new_with_values(gui.drawarea->window, | |
5252 &values, | |
5253 GDK_GC_FOREGROUND | | |
5254 GDK_GC_BACKGROUND | | |
5255 GDK_GC_FUNCTION); | |
5256 gdk_gc_set_exposures(invert_gc, | |
5257 gui.visibility != GDK_VISIBILITY_UNOBSCURED); | |
5258 /* | |
5259 * Do a visual beep by changing back and forth in some undetermined way, | |
5260 * the foreground and background colors. This is due to the fact that | |
5261 * there can't be really any prediction about the effects of XOR on | |
5262 * arbitrary X11 servers. However this seems to be enough for what we | |
5263 * intend it to do. | |
5264 */ | |
5265 gdk_draw_rectangle(gui.drawarea->window, invert_gc, | |
5266 TRUE, | |
5267 0, 0, | |
5268 FILL_X((int)Columns) + gui.border_offset, | |
5269 FILL_Y((int)Rows) + gui.border_offset); | |
5270 | |
5271 gui_mch_flush(); | |
5272 ui_delay((long)msec, TRUE); /* wait so many msec */ | |
5273 | |
5274 gdk_draw_rectangle(gui.drawarea->window, invert_gc, | |
5275 TRUE, | |
5276 0, 0, | |
5277 FILL_X((int)Columns) + gui.border_offset, | |
5278 FILL_Y((int)Rows) + gui.border_offset); | |
5279 | |
5280 gdk_gc_destroy(invert_gc); | |
5281 } | |
5282 | |
5283 /* | |
5284 * Invert a rectangle from row r, column c, for nr rows and nc columns. | |
5285 */ | |
5286 void | |
5287 gui_mch_invert_rectangle(int r, int c, int nr, int nc) | |
5288 { | |
5289 GdkGCValues values; | |
5290 GdkGC *invert_gc; | |
5291 | |
5292 if (gui.drawarea->window == NULL) | |
5293 return; | |
5294 | |
944 | 5295 values.foreground.pixel = gui.norm_pixel ^ gui.back_pixel; |
5296 values.background.pixel = gui.norm_pixel ^ gui.back_pixel; | |
7 | 5297 values.function = GDK_XOR; |
5298 invert_gc = gdk_gc_new_with_values(gui.drawarea->window, | |
5299 &values, | |
5300 GDK_GC_FOREGROUND | | |
5301 GDK_GC_BACKGROUND | | |
5302 GDK_GC_FUNCTION); | |
944 | 5303 gdk_gc_set_exposures(invert_gc, gui.visibility != |
5304 GDK_VISIBILITY_UNOBSCURED); | |
7 | 5305 gdk_draw_rectangle(gui.drawarea->window, invert_gc, |
5306 TRUE, | |
5307 FILL_X(c), FILL_Y(r), | |
5308 (nc) * gui.char_width, (nr) * gui.char_height); | |
5309 gdk_gc_destroy(invert_gc); | |
5310 } | |
5311 | |
5312 /* | |
5313 * Iconify the GUI window. | |
5314 */ | |
5315 void | |
5316 gui_mch_iconify(void) | |
5317 { | |
5318 gtk_window_iconify(GTK_WINDOW(gui.mainwin)); | |
5319 } | |
5320 | |
5321 #if defined(FEAT_EVAL) || defined(PROTO) | |
5322 /* | |
5323 * Bring the Vim window to the foreground. | |
5324 */ | |
5325 void | |
5326 gui_mch_set_foreground(void) | |
5327 { | |
5328 gtk_window_present(GTK_WINDOW(gui.mainwin)); | |
5329 } | |
5330 #endif | |
5331 | |
5332 /* | |
5333 * Draw a cursor without focus. | |
5334 */ | |
5335 void | |
5336 gui_mch_draw_hollow_cursor(guicolor_T color) | |
5337 { | |
5338 int i = 1; | |
5339 | |
5340 if (gui.drawarea->window == NULL) | |
5341 return; | |
5342 | |
5343 gui_mch_set_fg_color(color); | |
5344 | |
5345 gdk_gc_set_foreground(gui.text_gc, gui.fgcolor); | |
5346 if (mb_lefthalve(gui.row, gui.col)) | |
5347 i = 2; | |
5348 gdk_draw_rectangle(gui.drawarea->window, gui.text_gc, | |
5349 FALSE, | |
5350 FILL_X(gui.col), FILL_Y(gui.row), | |
5351 i * gui.char_width - 1, gui.char_height - 1); | |
5352 } | |
5353 | |
5354 /* | |
5355 * Draw part of a cursor, "w" pixels wide, and "h" pixels high, using | |
5356 * color "color". | |
5357 */ | |
5358 void | |
5359 gui_mch_draw_part_cursor(int w, int h, guicolor_T color) | |
5360 { | |
5361 if (gui.drawarea->window == NULL) | |
5362 return; | |
5363 | |
5364 gui_mch_set_fg_color(color); | |
5365 | |
5366 gdk_gc_set_foreground(gui.text_gc, gui.fgcolor); | |
5367 gdk_draw_rectangle(gui.drawarea->window, gui.text_gc, | |
5368 TRUE, | |
5369 #ifdef FEAT_RIGHTLEFT | |
5370 /* vertical line should be on the right of current point */ | |
5371 CURSOR_BAR_RIGHT ? FILL_X(gui.col + 1) - w : | |
5372 #endif | |
5373 FILL_X(gui.col), | |
5374 FILL_Y(gui.row) + gui.char_height - h, | |
5375 w, h); | |
5376 } | |
5377 | |
5378 | |
5379 /* | |
5380 * Catch up with any queued X11 events. This may put keyboard input into the | |
5381 * input buffer, call resize call-backs, trigger timers etc. If there is | |
5382 * nothing in the X11 event queue (& no timers pending), then we return | |
5383 * immediately. | |
5384 */ | |
5385 void | |
5386 gui_mch_update(void) | |
5387 { | |
2301
6f63294a1781
Avoid use of the GTK mail_loop() so that the GtkFileChooser can be used.
Bram Moolenaar <bram@vim.org>
parents:
2278
diff
changeset
|
5388 while (g_main_context_pending(NULL) && !vim_is_input_buf_full()) |
6f63294a1781
Avoid use of the GTK mail_loop() so that the GtkFileChooser can be used.
Bram Moolenaar <bram@vim.org>
parents:
2278
diff
changeset
|
5389 g_main_context_iteration(NULL, TRUE); |
7 | 5390 } |
5391 | |
5392 static gint | |
5393 input_timer_cb(gpointer data) | |
5394 { | |
5395 int *timed_out = (int *) data; | |
5396 | |
1226 | 5397 /* Just inform the caller about the occurrence of it */ |
7 | 5398 *timed_out = TRUE; |
5399 | |
5400 return FALSE; /* don't happen again */ | |
5401 } | |
5402 | |
5403 #ifdef FEAT_SNIFF | |
5404 /* | |
5405 * Callback function, used when data is available on the SNiFF connection. | |
5406 */ | |
5407 static void | |
5408 sniff_request_cb( | |
5409 gpointer data, | |
5410 gint source_fd, | |
5411 GdkInputCondition condition) | |
5412 { | |
5413 static char_u bytes[3] = {CSI, (int)KS_EXTRA, (int)KE_SNIFF}; | |
5414 | |
5415 add_to_input_buf(bytes, 3); | |
5416 } | |
5417 #endif | |
5418 | |
5419 /* | |
5420 * GUI input routine called by gui_wait_for_chars(). Waits for a character | |
5421 * from the keyboard. | |
5422 * wtime == -1 Wait forever. | |
5423 * wtime == 0 This should never happen. | |
5424 * wtime > 0 Wait wtime milliseconds for a character. | |
5425 * Returns OK if a character was found to be available within the given time, | |
5426 * or FAIL otherwise. | |
5427 */ | |
5428 int | |
5429 gui_mch_wait_for_chars(long wtime) | |
5430 { | |
5431 int focus; | |
5432 guint timer; | |
5433 static int timed_out; | |
5434 #ifdef FEAT_SNIFF | |
5435 static int sniff_on = 0; | |
5436 static gint sniff_input_id = 0; | |
5437 #endif | |
5438 | |
5439 #ifdef FEAT_SNIFF | |
5440 if (sniff_on && !want_sniff_request) | |
5441 { | |
5442 if (sniff_input_id) | |
5443 gdk_input_remove(sniff_input_id); | |
5444 sniff_on = 0; | |
5445 } | |
5446 else if (!sniff_on && want_sniff_request) | |
5447 { | |
5448 /* Add fd_from_sniff to watch for available data in main loop. */ | |
5449 sniff_input_id = gdk_input_add(fd_from_sniff, | |
5450 GDK_INPUT_READ, sniff_request_cb, NULL); | |
5451 sniff_on = 1; | |
5452 } | |
5453 #endif | |
5454 | |
5455 timed_out = FALSE; | |
5456 | |
5457 /* this timeout makes sure that we will return if no characters arrived in | |
5458 * time */ | |
5459 | |
5460 if (wtime > 0) | |
5461 timer = gtk_timeout_add((guint32)wtime, input_timer_cb, &timed_out); | |
5462 else | |
5463 timer = 0; | |
5464 | |
5465 focus = gui.in_focus; | |
5466 | |
5467 do | |
5468 { | |
5469 /* Stop or start blinking when focus changes */ | |
5470 if (gui.in_focus != focus) | |
5471 { | |
5472 if (gui.in_focus) | |
5473 gui_mch_start_blink(); | |
5474 else | |
5475 gui_mch_stop_blink(); | |
5476 focus = gui.in_focus; | |
5477 } | |
5478 | |
7109
fa95595fbc52
commit https://github.com/vim/vim/commit/93c88e0f6a4a8f7634ed84721daf4af46fc0d5db
Christian Brabandt <cb@256bit.org>
parents:
7098
diff
changeset
|
5479 #ifdef MESSAGE_QUEUE |
fa95595fbc52
commit https://github.com/vim/vim/commit/93c88e0f6a4a8f7634ed84721daf4af46fc0d5db
Christian Brabandt <cb@256bit.org>
parents:
7098
diff
changeset
|
5480 parse_queued_messages(); |
1624 | 5481 #endif |
5482 | |
7 | 5483 /* |
5484 * Loop in GTK+ processing until a timeout or input occurs. | |
22 | 5485 * Skip this if input is available anyway (can happen in rare |
5486 * situations, sort of race condition). | |
7 | 5487 */ |
22 | 5488 if (!input_available()) |
2301
6f63294a1781
Avoid use of the GTK mail_loop() so that the GtkFileChooser can be used.
Bram Moolenaar <bram@vim.org>
parents:
2278
diff
changeset
|
5489 g_main_context_iteration(NULL, TRUE); |
7 | 5490 |
5491 /* Got char, return immediately */ | |
5492 if (input_available()) | |
5493 { | |
5494 if (timer != 0 && !timed_out) | |
5495 gtk_timeout_remove(timer); | |
5496 return OK; | |
5497 } | |
5498 } while (wtime < 0 || !timed_out); | |
5499 | |
5500 /* | |
5501 * Flush all eventually pending (drawing) events. | |
5502 */ | |
5503 gui_mch_update(); | |
5504 | |
5505 return FAIL; | |
5506 } | |
5507 | |
5508 | |
5509 /**************************************************************************** | |
5510 * Output drawing routines. | |
5511 ****************************************************************************/ | |
5512 | |
5513 | |
5514 /* Flush any output to the screen */ | |
5515 void | |
5516 gui_mch_flush(void) | |
5517 { | |
5518 #ifdef HAVE_GTK_MULTIHEAD | |
5519 if (gui.mainwin != NULL && GTK_WIDGET_REALIZED(gui.mainwin)) | |
5520 gdk_display_sync(gtk_widget_get_display(gui.mainwin)); | |
5521 #else | |
5522 gdk_flush(); /* historical misnomer: calls XSync(), not XFlush() */ | |
5523 #endif | |
5524 /* This happens to actually do what gui_mch_flush() is supposed to do, | |
5525 * according to the comment above. */ | |
5526 if (gui.drawarea != NULL && gui.drawarea->window != NULL) | |
5527 gdk_window_process_updates(gui.drawarea->window, FALSE); | |
5528 } | |
5529 | |
5530 /* | |
5531 * Clear a rectangular region of the screen from text pos (row1, col1) to | |
5532 * (row2, col2) inclusive. | |
5533 */ | |
5534 void | |
5535 gui_mch_clear_block(int row1, int col1, int row2, int col2) | |
5536 { | |
5537 GdkColor color; | |
5538 | |
5539 if (gui.drawarea->window == NULL) | |
5540 return; | |
5541 | |
5542 color.pixel = gui.back_pixel; | |
5543 | |
5544 gdk_gc_set_foreground(gui.text_gc, &color); | |
5545 | |
5546 /* Clear one extra pixel at the far right, for when bold characters have | |
5547 * spilled over to the window border. */ | |
5548 gdk_draw_rectangle(gui.drawarea->window, gui.text_gc, TRUE, | |
5549 FILL_X(col1), FILL_Y(row1), | |
5550 (col2 - col1 + 1) * gui.char_width | |
5551 + (col2 == Columns - 1), | |
5552 (row2 - row1 + 1) * gui.char_height); | |
5553 } | |
5554 | |
5555 void | |
5556 gui_mch_clear_all(void) | |
5557 { | |
5558 if (gui.drawarea->window != NULL) | |
5559 gdk_window_clear(gui.drawarea->window); | |
5560 } | |
5561 | |
5562 /* | |
5563 * Redraw any text revealed by scrolling up/down. | |
5564 */ | |
5565 static void | |
5566 check_copy_area(void) | |
5567 { | |
5568 GdkEvent *event; | |
5569 int expose_count; | |
5570 | |
5571 if (gui.visibility != GDK_VISIBILITY_PARTIAL) | |
5572 return; | |
5573 | |
5574 /* Avoid redrawing the cursor while scrolling or it'll end up where | |
5575 * we don't want it to be. I'm not sure if it's correct to call | |
5576 * gui_dont_update_cursor() at this point but it works as a quick | |
5577 * fix for now. */ | |
5578 gui_dont_update_cursor(); | |
5579 | |
5580 do | |
5581 { | |
5582 /* Wait to check whether the scroll worked or not. */ | |
5583 event = gdk_event_get_graphics_expose(gui.drawarea->window); | |
5584 | |
5585 if (event == NULL) | |
5586 break; /* received NoExpose event */ | |
5587 | |
5588 gui_redraw(event->expose.area.x, event->expose.area.y, | |
5589 event->expose.area.width, event->expose.area.height); | |
5590 | |
5591 expose_count = event->expose.count; | |
5592 gdk_event_free(event); | |
5593 } | |
5594 while (expose_count > 0); /* more events follow */ | |
5595 | |
5596 gui_can_update_cursor(); | |
5597 } | |
5598 | |
5599 /* | |
5600 * Delete the given number of lines from the given row, scrolling up any | |
5601 * text further down within the scroll region. | |
5602 */ | |
5603 void | |
5604 gui_mch_delete_lines(int row, int num_lines) | |
5605 { | |
5606 if (gui.visibility == GDK_VISIBILITY_FULLY_OBSCURED) | |
5607 return; /* Can't see the window */ | |
5608 | |
5609 gdk_gc_set_foreground(gui.text_gc, gui.fgcolor); | |
5610 gdk_gc_set_background(gui.text_gc, gui.bgcolor); | |
5611 | |
5612 /* copy one extra pixel, for when bold has spilled over */ | |
5613 gdk_window_copy_area(gui.drawarea->window, gui.text_gc, | |
5614 FILL_X(gui.scroll_region_left), FILL_Y(row), | |
5615 gui.drawarea->window, | |
5616 FILL_X(gui.scroll_region_left), | |
5617 FILL_Y(row + num_lines), | |
5618 gui.char_width * (gui.scroll_region_right | |
5619 - gui.scroll_region_left + 1) + 1, | |
5620 gui.char_height * (gui.scroll_region_bot - row - num_lines + 1)); | |
5621 | |
5622 gui_clear_block(gui.scroll_region_bot - num_lines + 1, | |
5623 gui.scroll_region_left, | |
5624 gui.scroll_region_bot, gui.scroll_region_right); | |
5625 check_copy_area(); | |
5626 } | |
5627 | |
5628 /* | |
5629 * Insert the given number of lines before the given row, scrolling down any | |
5630 * following text within the scroll region. | |
5631 */ | |
5632 void | |
5633 gui_mch_insert_lines(int row, int num_lines) | |
5634 { | |
5635 if (gui.visibility == GDK_VISIBILITY_FULLY_OBSCURED) | |
5636 return; /* Can't see the window */ | |
5637 | |
5638 gdk_gc_set_foreground(gui.text_gc, gui.fgcolor); | |
5639 gdk_gc_set_background(gui.text_gc, gui.bgcolor); | |
5640 | |
5641 /* copy one extra pixel, for when bold has spilled over */ | |
5642 gdk_window_copy_area(gui.drawarea->window, gui.text_gc, | |
5643 FILL_X(gui.scroll_region_left), FILL_Y(row + num_lines), | |
5644 gui.drawarea->window, | |
5645 FILL_X(gui.scroll_region_left), FILL_Y(row), | |
5646 gui.char_width * (gui.scroll_region_right | |
5647 - gui.scroll_region_left + 1) + 1, | |
5648 gui.char_height * (gui.scroll_region_bot - row - num_lines + 1)); | |
5649 | |
5650 gui_clear_block(row, gui.scroll_region_left, | |
5651 row + num_lines - 1, gui.scroll_region_right); | |
5652 check_copy_area(); | |
5653 } | |
5654 | |
5655 /* | |
5656 * X Selection stuff, for cutting and pasting text to other windows. | |
5657 */ | |
5658 void | |
5659 clip_mch_request_selection(VimClipboard *cbd) | |
5660 { | |
5661 GdkAtom target; | |
5662 unsigned i; | |
1494 | 5663 time_t start; |
7 | 5664 |
5665 for (i = 0; i < N_SELECTION_TARGETS; ++i) | |
5666 { | |
1904 | 5667 if (!clip_html && selection_targets[i].info == TARGET_HTML) |
5668 continue; | |
7 | 5669 received_selection = RS_NONE; |
5670 target = gdk_atom_intern(selection_targets[i].target, FALSE); | |
5671 | |
5672 gtk_selection_convert(gui.drawarea, | |
5673 cbd->gtk_sel_atom, target, | |
5674 (guint32)GDK_CURRENT_TIME); | |
5675 | |
1494 | 5676 /* Hack: Wait up to three seconds for the selection. A hang was |
5677 * noticed here when using the netrw plugin combined with ":gui" | |
5678 * during the FocusGained event. */ | |
5679 start = time(NULL); | |
5680 while (received_selection == RS_NONE && time(NULL) < start + 3) | |
2301
6f63294a1781
Avoid use of the GTK mail_loop() so that the GtkFileChooser can be used.
Bram Moolenaar <bram@vim.org>
parents:
2278
diff
changeset
|
5681 g_main_context_iteration(NULL, TRUE); /* wait for selection_received_cb */ |
7 | 5682 |
5683 if (received_selection != RS_FAIL) | |
5684 return; | |
5685 } | |
5686 | |
5687 /* Final fallback position - use the X CUT_BUFFER0 store */ | |
1924 | 5688 yank_cut_buffer0(GDK_WINDOW_XDISPLAY(gui.mainwin->window), cbd); |
7 | 5689 } |
5690 | |
5691 /* | |
5692 * Disown the selection. | |
5693 */ | |
5694 void | |
1884 | 5695 clip_mch_lose_selection(VimClipboard *cbd UNUSED) |
7 | 5696 { |
4209 | 5697 gtk_selection_owner_set(NULL, cbd->gtk_sel_atom, gui.event_time); |
7 | 5698 gui_mch_update(); |
5699 } | |
5700 | |
5701 /* | |
5702 * Own the selection and return OK if it worked. | |
5703 */ | |
5704 int | |
5705 clip_mch_own_selection(VimClipboard *cbd) | |
5706 { | |
5707 int success; | |
5708 | |
5709 success = gtk_selection_owner_set(gui.drawarea, cbd->gtk_sel_atom, | |
2923 | 5710 gui.event_time); |
7 | 5711 gui_mch_update(); |
5712 return (success) ? OK : FAIL; | |
5713 } | |
5714 | |
5715 /* | |
5716 * Send the current selection to the clipboard. Do nothing for X because we | |
5717 * will fill in the selection only when requested by another app. | |
5718 */ | |
5719 void | |
1884 | 5720 clip_mch_set_selection(VimClipboard *cbd UNUSED) |
7 | 5721 { |
5722 } | |
5723 | |
4209 | 5724 int |
5725 clip_gtk_owner_exists(VimClipboard *cbd) | |
5726 { | |
5727 return gdk_selection_owner_get(cbd->gtk_sel_atom) != NULL; | |
5728 } | |
5729 | |
7 | 5730 |
5731 #if defined(FEAT_MENU) || defined(PROTO) | |
5732 /* | |
5733 * Make a menu item appear either active or not active (grey or not grey). | |
5734 */ | |
5735 void | |
5736 gui_mch_menu_grey(vimmenu_T *menu, int grey) | |
5737 { | |
5738 if (menu->id == NULL) | |
5739 return; | |
5740 | |
5741 if (menu_is_separator(menu->name)) | |
5742 grey = TRUE; | |
5743 | |
5744 gui_mch_menu_hidden(menu, FALSE); | |
5745 /* Be clever about bitfields versus true booleans here! */ | |
5746 if (!GTK_WIDGET_SENSITIVE(menu->id) == !grey) | |
5747 { | |
5748 gtk_widget_set_sensitive(menu->id, !grey); | |
5749 gui_mch_update(); | |
5750 } | |
5751 } | |
5752 | |
5753 /* | |
5754 * Make menu item hidden or not hidden. | |
5755 */ | |
5756 void | |
5757 gui_mch_menu_hidden(vimmenu_T *menu, int hidden) | |
5758 { | |
5759 if (menu->id == 0) | |
5760 return; | |
5761 | |
5762 if (hidden) | |
5763 { | |
5764 if (GTK_WIDGET_VISIBLE(menu->id)) | |
5765 { | |
5766 gtk_widget_hide(menu->id); | |
5767 gui_mch_update(); | |
5768 } | |
5769 } | |
5770 else | |
5771 { | |
5772 if (!GTK_WIDGET_VISIBLE(menu->id)) | |
5773 { | |
5774 gtk_widget_show(menu->id); | |
5775 gui_mch_update(); | |
5776 } | |
5777 } | |
5778 } | |
5779 | |
5780 /* | |
5781 * This is called after setting all the menus to grey/hidden or not. | |
5782 */ | |
5783 void | |
5784 gui_mch_draw_menubar(void) | |
5785 { | |
5786 /* just make sure that the visual changes get effect immediately */ | |
5787 gui_mch_update(); | |
5788 } | |
5789 #endif /* FEAT_MENU */ | |
5790 | |
5791 /* | |
5792 * Scrollbar stuff. | |
5793 */ | |
5794 void | |
5795 gui_mch_enable_scrollbar(scrollbar_T *sb, int flag) | |
5796 { | |
5797 if (sb->id == NULL) | |
5798 return; | |
5799 | |
5800 if (flag) | |
5801 gtk_widget_show(sb->id); | |
5802 else | |
5803 gtk_widget_hide(sb->id); | |
5804 | |
791 | 5805 update_window_manager_hints(0, 0); |
7 | 5806 } |
5807 | |
5808 | |
5809 /* | |
5810 * Return the RGB value of a pixel as long. | |
5811 */ | |
5812 long_u | |
5813 gui_mch_get_rgb(guicolor_T pixel) | |
5814 { | |
5815 GdkColor color; | |
5816 gdk_colormap_query_color(gtk_widget_get_colormap(gui.drawarea), | |
5817 (unsigned long)pixel, &color); | |
5818 | |
5819 return (((unsigned)color.red & 0xff00) << 8) | |
5820 | ((unsigned)color.green & 0xff00) | |
5821 | (((unsigned)color.blue & 0xff00) >> 8); | |
5822 } | |
5823 | |
5824 /* | |
88 | 5825 * Get current mouse coordinates in text window. |
7 | 5826 */ |
88 | 5827 void |
5828 gui_mch_getmouse(int *x, int *y) | |
7 | 5829 { |
88 | 5830 gdk_window_get_pointer(gui.drawarea->window, x, y, NULL); |
7 | 5831 } |
5832 | |
5833 void | |
5834 gui_mch_setmouse(int x, int y) | |
5835 { | |
5836 /* Sorry for the Xlib call, but we can't avoid it, since there is no | |
5837 * internal GDK mechanism present to accomplish this. (and for good | |
5838 * reason...) */ | |
5839 XWarpPointer(GDK_WINDOW_XDISPLAY(gui.drawarea->window), | |
5840 (Window)0, GDK_WINDOW_XWINDOW(gui.drawarea->window), | |
5841 0, 0, 0U, 0U, x, y); | |
5842 } | |
5843 | |
5844 | |
5845 #ifdef FEAT_MOUSESHAPE | |
5846 /* The last set mouse pointer shape is remembered, to be used when it goes | |
5847 * from hidden to not hidden. */ | |
5848 static int last_shape = 0; | |
5849 #endif | |
5850 | |
5851 /* | |
5852 * Use the blank mouse pointer or not. | |
5853 * | |
5854 * hide: TRUE = use blank ptr, FALSE = use parent ptr | |
5855 */ | |
5856 void | |
5857 gui_mch_mousehide(int hide) | |
5858 { | |
5859 if (gui.pointer_hidden != hide) | |
5860 { | |
5861 gui.pointer_hidden = hide; | |
5862 if (gui.drawarea->window && gui.blank_pointer != NULL) | |
5863 { | |
5864 if (hide) | |
5865 gdk_window_set_cursor(gui.drawarea->window, gui.blank_pointer); | |
5866 else | |
5867 #ifdef FEAT_MOUSESHAPE | |
5868 mch_set_mouse_shape(last_shape); | |
5869 #else | |
5870 gdk_window_set_cursor(gui.drawarea->window, NULL); | |
5871 #endif | |
5872 } | |
5873 } | |
5874 } | |
5875 | |
5876 #if defined(FEAT_MOUSESHAPE) || defined(PROTO) | |
5877 | |
5878 /* Table for shape IDs. Keep in sync with the mshape_names[] table in | |
5879 * misc2.c! */ | |
5880 static const int mshape_ids[] = | |
5881 { | |
5882 GDK_LEFT_PTR, /* arrow */ | |
5883 GDK_CURSOR_IS_PIXMAP, /* blank */ | |
5884 GDK_XTERM, /* beam */ | |
5885 GDK_SB_V_DOUBLE_ARROW, /* updown */ | |
5886 GDK_SIZING, /* udsizing */ | |
5887 GDK_SB_H_DOUBLE_ARROW, /* leftright */ | |
5888 GDK_SIZING, /* lrsizing */ | |
5889 GDK_WATCH, /* busy */ | |
5890 GDK_X_CURSOR, /* no */ | |
5891 GDK_CROSSHAIR, /* crosshair */ | |
5892 GDK_HAND1, /* hand1 */ | |
5893 GDK_HAND2, /* hand2 */ | |
5894 GDK_PENCIL, /* pencil */ | |
5895 GDK_QUESTION_ARROW, /* question */ | |
5896 GDK_RIGHT_PTR, /* right-arrow */ | |
5897 GDK_CENTER_PTR, /* up-arrow */ | |
5898 GDK_LEFT_PTR /* last one */ | |
5899 }; | |
5900 | |
5901 void | |
5902 mch_set_mouse_shape(int shape) | |
5903 { | |
5904 int id; | |
5905 GdkCursor *c; | |
5906 | |
5907 if (gui.drawarea->window == NULL) | |
5908 return; | |
5909 | |
5910 if (shape == MSHAPE_HIDE || gui.pointer_hidden) | |
5911 gdk_window_set_cursor(gui.drawarea->window, gui.blank_pointer); | |
5912 else | |
5913 { | |
5914 if (shape >= MSHAPE_NUMBERED) | |
5915 { | |
5916 id = shape - MSHAPE_NUMBERED; | |
5917 if (id >= GDK_LAST_CURSOR) | |
5918 id = GDK_LEFT_PTR; | |
5919 else | |
5920 id &= ~1; /* they are always even (why?) */ | |
5921 } | |
1884 | 5922 else if (shape < (int)(sizeof(mshape_ids) / sizeof(int))) |
7 | 5923 id = mshape_ids[shape]; |
842 | 5924 else |
5925 return; | |
7 | 5926 # ifdef HAVE_GTK_MULTIHEAD |
5927 c = gdk_cursor_new_for_display( | |
136 | 5928 gtk_widget_get_display(gui.drawarea), (GdkCursorType)id); |
7 | 5929 # else |
136 | 5930 c = gdk_cursor_new((GdkCursorType)id); |
7 | 5931 # endif |
5932 gdk_window_set_cursor(gui.drawarea->window, c); | |
5933 gdk_cursor_destroy(c); /* Unref, actually. Bloody GTK+ 1. */ | |
5934 } | |
5935 if (shape != MSHAPE_HIDE) | |
5936 last_shape = shape; | |
5937 } | |
5938 #endif /* FEAT_MOUSESHAPE */ | |
5939 | |
5940 | |
5941 #if defined(FEAT_SIGN_ICONS) || defined(PROTO) | |
5942 /* | |
5943 * Signs are currently always 2 chars wide. With GTK+ 2, the image will be | |
5944 * scaled down if the current font is not big enough, or scaled up if the image | |
5945 * size is less than 3/4 of the maximum sign size. With GTK+ 1, the pixmap | |
5946 * will be cut off if the current font is not big enough, or centered if it's | |
5947 * too small. | |
5948 */ | |
5949 # define SIGN_WIDTH (2 * gui.char_width) | |
5950 # define SIGN_HEIGHT (gui.char_height) | |
5951 # define SIGN_ASPECT ((double)SIGN_HEIGHT / (double)SIGN_WIDTH) | |
5952 | |
5953 void | |
5954 gui_mch_drawsign(int row, int col, int typenr) | |
5955 { | |
5956 GdkPixbuf *sign; | |
5957 | |
5958 sign = (GdkPixbuf *)sign_get_image(typenr); | |
5959 | |
5960 if (sign != NULL && gui.drawarea != NULL && gui.drawarea->window != NULL) | |
5961 { | |
5962 int width; | |
5963 int height; | |
5964 int xoffset; | |
5965 int yoffset; | |
5966 int need_scale; | |
5967 | |
5968 width = gdk_pixbuf_get_width(sign); | |
5969 height = gdk_pixbuf_get_height(sign); | |
5970 /* | |
5971 * Decide whether we need to scale. Allow one pixel of border | |
5972 * width to be cut off, in order to avoid excessive scaling for | |
5973 * tiny differences in font size. | |
5983 | 5974 * Do scale to fit the height to avoid gaps because of linespacing. |
7 | 5975 */ |
5976 need_scale = (width > SIGN_WIDTH + 2 | |
5983 | 5977 || height != SIGN_HEIGHT |
7 | 5978 || (width < 3 * SIGN_WIDTH / 4 |
5979 && height < 3 * SIGN_HEIGHT / 4)); | |
5980 if (need_scale) | |
5981 { | |
5983 | 5982 double aspect; |
5983 int w = width; | |
5984 int h = height; | |
7 | 5985 |
5986 /* Keep the original aspect ratio */ | |
5987 aspect = (double)height / (double)width; | |
5988 width = (double)SIGN_WIDTH * SIGN_ASPECT / aspect; | |
5989 width = MIN(width, SIGN_WIDTH); | |
5983 | 5990 if (((double)(MAX(height, SIGN_HEIGHT)) / |
5991 (double)(MIN(height, SIGN_HEIGHT))) < 1.15) | |
5992 { | |
5993 /* Change the aspect ratio by at most 15% to fill the | |
5994 * available space completly. */ | |
5995 height = (double)SIGN_HEIGHT * SIGN_ASPECT / aspect; | |
5996 height = MIN(height, SIGN_HEIGHT); | |
5997 } | |
5998 else | |
5999 height = (double)width * aspect; | |
6000 | |
6001 if (w == width && h == height) | |
6002 { | |
6003 /* no change in dimensions; don't decrease reference counter | |
6004 * (below) */ | |
6005 need_scale = FALSE; | |
6006 } | |
6007 else | |
6008 { | |
6009 /* This doesn't seem to be worth caching, and doing so would | |
6010 * complicate the code quite a bit. */ | |
6011 sign = gdk_pixbuf_scale_simple(sign, width, height, | |
6012 GDK_INTERP_BILINEAR); | |
6013 if (sign == NULL) | |
6014 return; /* out of memory */ | |
6015 } | |
7 | 6016 } |
6017 | |
6018 /* The origin is the upper-left corner of the pixmap. Therefore | |
6019 * these offset may become negative if the pixmap is smaller than | |
6020 * the 2x1 cells reserved for the sign icon. */ | |
6021 xoffset = (width - SIGN_WIDTH) / 2; | |
6022 yoffset = (height - SIGN_HEIGHT) / 2; | |
6023 | |
6024 gdk_gc_set_foreground(gui.text_gc, gui.bgcolor); | |
6025 | |
6026 gdk_draw_rectangle(gui.drawarea->window, | |
6027 gui.text_gc, | |
6028 TRUE, | |
6029 FILL_X(col), | |
6030 FILL_Y(row), | |
6031 SIGN_WIDTH, | |
6032 SIGN_HEIGHT); | |
6033 | |
6034 gdk_pixbuf_render_to_drawable_alpha(sign, | |
6035 gui.drawarea->window, | |
6036 MAX(0, xoffset), | |
6037 MAX(0, yoffset), | |
6038 FILL_X(col) - MIN(0, xoffset), | |
6039 FILL_Y(row) - MIN(0, yoffset), | |
6040 MIN(width, SIGN_WIDTH), | |
6041 MIN(height, SIGN_HEIGHT), | |
6042 GDK_PIXBUF_ALPHA_BILEVEL, | |
6043 127, | |
6044 GDK_RGB_DITHER_NORMAL, | |
6045 0, 0); | |
6046 if (need_scale) | |
6047 g_object_unref(sign); | |
6048 } | |
6049 } | |
6050 | |
6051 void * | |
6052 gui_mch_register_sign(char_u *signfile) | |
6053 { | |
6054 if (signfile[0] != NUL && signfile[0] != '-' && gui.in_use) | |
6055 { | |
6056 GdkPixbuf *sign; | |
6057 GError *error = NULL; | |
6058 char_u *message; | |
6059 | |
6060 sign = gdk_pixbuf_new_from_file((const char *)signfile, &error); | |
6061 | |
6062 if (error == NULL) | |
6063 return sign; | |
6064 | |
6065 message = (char_u *)error->message; | |
6066 | |
6067 if (message != NULL && input_conv.vc_type != CONV_NONE) | |
6068 message = string_convert(&input_conv, message, NULL); | |
6069 | |
6070 if (message != NULL) | |
6071 { | |
6072 /* The error message is already translated and will be more | |
6073 * descriptive than anything we could possibly do ourselves. */ | |
6074 EMSG2("E255: %s", message); | |
6075 | |
6076 if (input_conv.vc_type != CONV_NONE) | |
6077 vim_free(message); | |
6078 } | |
6079 g_error_free(error); | |
6080 } | |
6081 | |
6082 return NULL; | |
6083 } | |
6084 | |
6085 void | |
6086 gui_mch_destroy_sign(void *sign) | |
6087 { | |
6088 if (sign != NULL) | |
6089 g_object_unref(sign); | |
6090 } | |
6091 | |
6092 #endif /* FEAT_SIGN_ICONS */ |