forked from vim/vim
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gui_gtk_x11.c
7312 lines (6421 loc) · 193 KB
/
gui_gtk_x11.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* vi:set ts=8 sts=4 sw=4 noet:
*
* VIM - Vi IMproved by Bram Moolenaar
*
* Do ":help uganda" in Vim to read copying and usage conditions.
* Do ":help credits" in Vim to see a list of people who contributed.
* See README.txt for an overview of the Vim source code.
*/
/*
* Porting to GTK+ was done by:
*
* (C) 1998,1999,2000 by Marcin Dalecki <[email protected]>
*
* With GREAT support and continuous encouragements by Andy Kahn and of
* course Bram Moolenaar!
*
* Support for GTK+ 2 was added by:
*
* (C) 2002,2003 Jason Hildebrand <[email protected]>
* Daniel Elstner <[email protected]>
*
* Support for GTK+ 3 was added by:
*
* 2016 Kazunobu Kuriyama <[email protected]>
*/
#include "vim.h"
#ifdef USE_GRESOURCE
#include "auto/gui_gtk_gresources.h"
#endif
#ifdef FEAT_GUI_GNOME
// Gnome redefines _() and N_(). Grrr...
# ifdef _
# undef _
# endif
# ifdef N_
# undef N_
# endif
# ifdef textdomain
# undef textdomain
# endif
# ifdef bindtextdomain
# undef bindtextdomain
# endif
# ifdef bind_textdomain_codeset
# undef bind_textdomain_codeset
# endif
# if defined(FEAT_GETTEXT) && !defined(ENABLE_NLS)
# define ENABLE_NLS // so the texts in the dialog boxes are translated
# endif
# include <gnome.h>
# include "version.h"
// missing prototype in bonobo-dock-item.h
extern void bonobo_dock_item_set_behavior(BonoboDockItem *dock_item, BonoboDockItemBehavior beh);
#endif
#if !defined(FEAT_GUI_GTK) && defined(PROTO)
// When generating prototypes we don't want syntax errors.
# define GdkAtom int
# define GdkEventExpose int
# define GdkEventFocus int
# define GdkEventVisibility int
# define GdkEventProperty int
# define GtkContainer int
# define GtkTargetEntry int
# define GtkType int
# define GtkWidget int
# define gint int
# define gpointer int
# define guint int
# define GdkEventKey int
# define GdkEventSelection int
# define GtkSelectionData int
# define GdkEventMotion int
# define GdkEventButton int
# define GdkDragContext int
# define GdkEventConfigure int
# define GdkEventClient int
#else
# if GTK_CHECK_VERSION(3,0,0)
# include <gdk/gdkkeysyms-compat.h>
# include <gtk/gtkx.h>
# else
# include <gdk/gdkkeysyms.h>
# endif
# include <gdk/gdk.h>
# ifdef MSWIN
# include <gdk/gdkwin32.h>
# else
# include <gdk/gdkx.h>
# endif
# include <gtk/gtk.h>
# include "gui_gtk_f.h"
#endif
#ifdef HAVE_X11_SUNKEYSYM_H
# include <X11/Sunkeysym.h>
#endif
/*
* Easy-to-use macro for multihead support.
*/
#define GET_X_ATOM(atom) gdk_x11_atom_to_xatom_for_display( \
gtk_widget_get_display(gui.mainwin), atom)
// Selection type distinguishers
enum
{
TARGET_TYPE_NONE,
TARGET_UTF8_STRING,
TARGET_STRING,
TARGET_COMPOUND_TEXT,
TARGET_HTML,
TARGET_TEXT,
TARGET_TEXT_URI_LIST,
TARGET_TEXT_PLAIN,
TARGET_VIM,
TARGET_VIMENC
};
/*
* Table of selection targets supported by Vim.
* Note: Order matters, preferred types should come first.
*/
static const GtkTargetEntry selection_targets[] =
{
{VIMENC_ATOM_NAME, 0, TARGET_VIMENC},
{VIM_ATOM_NAME, 0, TARGET_VIM},
{"text/html", 0, TARGET_HTML},
{"UTF8_STRING", 0, TARGET_UTF8_STRING},
{"COMPOUND_TEXT", 0, TARGET_COMPOUND_TEXT},
{"TEXT", 0, TARGET_TEXT},
{"STRING", 0, TARGET_STRING}
};
#define N_SELECTION_TARGETS ARRAY_LENGTH(selection_targets)
#ifdef FEAT_DND
/*
* Table of DnD targets supported by Vim.
* Note: Order matters, preferred types should come first.
*/
static const GtkTargetEntry dnd_targets[] =
{
{"text/uri-list", 0, TARGET_TEXT_URI_LIST},
{"text/html", 0, TARGET_HTML},
{"UTF8_STRING", 0, TARGET_UTF8_STRING},
{"STRING", 0, TARGET_STRING},
{"text/plain", 0, TARGET_TEXT_PLAIN}
};
# define N_DND_TARGETS ARRAY_LENGTH(dnd_targets)
#endif
/*
* "Monospace" is a standard font alias that should be present
* on all proper Pango/fontconfig installations.
*/
# define DEFAULT_FONT "Monospace 10"
#if defined(FEAT_GUI_GNOME) && defined(FEAT_SESSION)
# define USE_GNOME_SESSION
#endif
#if !defined(FEAT_GUI_GNOME)
/*
* Atoms used to communicate save-yourself from the X11 session manager. There
* is no need to move them into the GUI struct, since they should be constant.
*/
static GdkAtom wm_protocols_atom = GDK_NONE;
static GdkAtom save_yourself_atom = GDK_NONE;
#endif
/*
* Atoms used to control/reference X11 selections.
*/
static GdkAtom html_atom = GDK_NONE;
static GdkAtom utf8_string_atom = GDK_NONE;
static GdkAtom vim_atom = GDK_NONE; // Vim's own special selection format
static GdkAtom vimenc_atom = GDK_NONE; // Vim's extended selection format
/*
* Keycodes recognized by vim.
* NOTE: when changing this, the table in gui_x11.c probably needs the same
* change!
*/
static struct special_key
{
guint key_sym;
char_u code0;
char_u code1;
}
const special_keys[] =
{
{GDK_Up, 'k', 'u'},
{GDK_Down, 'k', 'd'},
{GDK_Left, 'k', 'l'},
{GDK_Right, 'k', 'r'},
{GDK_F1, 'k', '1'},
{GDK_F2, 'k', '2'},
{GDK_F3, 'k', '3'},
{GDK_F4, 'k', '4'},
{GDK_F5, 'k', '5'},
{GDK_F6, 'k', '6'},
{GDK_F7, 'k', '7'},
{GDK_F8, 'k', '8'},
{GDK_F9, 'k', '9'},
{GDK_F10, 'k', ';'},
{GDK_F11, 'F', '1'},
{GDK_F12, 'F', '2'},
{GDK_F13, 'F', '3'},
{GDK_F14, 'F', '4'},
{GDK_F15, 'F', '5'},
{GDK_F16, 'F', '6'},
{GDK_F17, 'F', '7'},
{GDK_F18, 'F', '8'},
{GDK_F19, 'F', '9'},
{GDK_F20, 'F', 'A'},
{GDK_F21, 'F', 'B'},
{GDK_Pause, 'F', 'B'}, // Pause == F21 according to netbeans.txt
{GDK_F22, 'F', 'C'},
{GDK_F23, 'F', 'D'},
{GDK_F24, 'F', 'E'},
{GDK_F25, 'F', 'F'},
{GDK_F26, 'F', 'G'},
{GDK_F27, 'F', 'H'},
{GDK_F28, 'F', 'I'},
{GDK_F29, 'F', 'J'},
{GDK_F30, 'F', 'K'},
{GDK_F31, 'F', 'L'},
{GDK_F32, 'F', 'M'},
{GDK_F33, 'F', 'N'},
{GDK_F34, 'F', 'O'},
{GDK_F35, 'F', 'P'},
#ifdef SunXK_F36
{SunXK_F36, 'F', 'Q'},
{SunXK_F37, 'F', 'R'},
#endif
{GDK_Help, '%', '1'},
{GDK_Undo, '&', '8'},
{GDK_BackSpace, 'k', 'b'},
{GDK_Insert, 'k', 'I'},
{GDK_Delete, 'k', 'D'},
{GDK_3270_BackTab, 'k', 'B'},
{GDK_Clear, 'k', 'C'},
{GDK_Home, 'k', 'h'},
{GDK_End, '@', '7'},
{GDK_Prior, 'k', 'P'},
{GDK_Next, 'k', 'N'},
{GDK_Print, '%', '9'},
// Keypad keys:
{GDK_KP_Left, 'k', 'l'},
{GDK_KP_Right, 'k', 'r'},
{GDK_KP_Up, 'k', 'u'},
{GDK_KP_Down, 'k', 'd'},
{GDK_KP_Insert, KS_EXTRA, (char_u)KE_KINS},
{GDK_KP_Delete, KS_EXTRA, (char_u)KE_KDEL},
{GDK_KP_Home, 'K', '1'},
{GDK_KP_End, 'K', '4'},
{GDK_KP_Prior, 'K', '3'}, // page up
{GDK_KP_Next, 'K', '5'}, // page down
{GDK_KP_Add, 'K', '6'},
{GDK_KP_Subtract, 'K', '7'},
{GDK_KP_Divide, 'K', '8'},
{GDK_KP_Multiply, 'K', '9'},
{GDK_KP_Enter, 'K', 'A'},
{GDK_KP_Decimal, 'K', 'B'},
{GDK_KP_0, 'K', 'C'},
{GDK_KP_1, 'K', 'D'},
{GDK_KP_2, 'K', 'E'},
{GDK_KP_3, 'K', 'F'},
{GDK_KP_4, 'K', 'G'},
{GDK_KP_5, 'K', 'H'},
{GDK_KP_6, 'K', 'I'},
{GDK_KP_7, 'K', 'J'},
{GDK_KP_8, 'K', 'K'},
{GDK_KP_9, 'K', 'L'},
// End of list marker:
{0, 0, 0}
};
/*
* Flags for command line options table below.
*/
#define ARG_FONT 1
#define ARG_GEOMETRY 2
#define ARG_REVERSE 3
#define ARG_NOREVERSE 4
#define ARG_BACKGROUND 5
#define ARG_FOREGROUND 6
#define ARG_ICONIC 7
#define ARG_ROLE 8
#define ARG_NETBEANS 9
#define ARG_XRM 10 // ignored
#define ARG_MENUFONT 11 // ignored
#define ARG_INDEX_MASK 0x00ff
#define ARG_HAS_VALUE 0x0100 // a value is expected after the argument
#define ARG_NEEDS_GUI 0x0200 // need to initialize the GUI for this
#define ARG_FOR_GTK 0x0400 // argument is handled by GTK+ or GNOME
#define ARG_COMPAT_LONG 0x0800 // accept -foo but substitute with --foo
#define ARG_KEEP 0x1000 // don't remove argument from argv[]
/*
* This table holds all the X GUI command line options allowed. This includes
* the standard ones so that we can skip them when Vim is started without the
* GUI (but the GUI might start up later).
*
* When changing this, also update doc/gui_x11.txt and the usage message!!!
*/
typedef struct
{
const char *name;
unsigned int flags;
}
cmdline_option_T;
static const cmdline_option_T cmdline_options[] =
{
// We handle these options ourselves
{"-fn", ARG_FONT|ARG_HAS_VALUE},
{"-font", ARG_FONT|ARG_HAS_VALUE},
{"-geom", ARG_GEOMETRY|ARG_HAS_VALUE},
{"-geometry", ARG_GEOMETRY|ARG_HAS_VALUE},
{"-rv", ARG_REVERSE},
{"-reverse", ARG_REVERSE},
{"+rv", ARG_NOREVERSE},
{"+reverse", ARG_NOREVERSE},
{"-bg", ARG_BACKGROUND|ARG_HAS_VALUE},
{"-background", ARG_BACKGROUND|ARG_HAS_VALUE},
{"-fg", ARG_FOREGROUND|ARG_HAS_VALUE},
{"-foreground", ARG_FOREGROUND|ARG_HAS_VALUE},
{"-iconic", ARG_ICONIC},
{"--role", ARG_ROLE|ARG_HAS_VALUE},
#ifdef FEAT_NETBEANS_INTG
{"-nb", ARG_NETBEANS}, // non-standard value format
{"-xrm", ARG_XRM|ARG_HAS_VALUE}, // not implemented
{"-mf", ARG_MENUFONT|ARG_HAS_VALUE}, // not implemented
{"-menufont", ARG_MENUFONT|ARG_HAS_VALUE}, // not implemented
#endif
// Arguments handled by GTK (and GNOME) internally.
{"--g-fatal-warnings", ARG_FOR_GTK},
{"--gdk-debug", ARG_FOR_GTK|ARG_HAS_VALUE},
{"--gdk-no-debug", ARG_FOR_GTK|ARG_HAS_VALUE},
{"--gtk-debug", ARG_FOR_GTK|ARG_HAS_VALUE},
{"--gtk-no-debug", ARG_FOR_GTK|ARG_HAS_VALUE},
{"--gtk-module", ARG_FOR_GTK|ARG_HAS_VALUE},
{"--sync", ARG_FOR_GTK},
{"--display", ARG_FOR_GTK|ARG_HAS_VALUE|ARG_COMPAT_LONG},
{"--name", ARG_FOR_GTK|ARG_HAS_VALUE|ARG_COMPAT_LONG},
{"--class", ARG_FOR_GTK|ARG_HAS_VALUE|ARG_COMPAT_LONG},
{"--screen", ARG_FOR_GTK|ARG_HAS_VALUE},
{"--gxid-host", ARG_FOR_GTK|ARG_HAS_VALUE},
{"--gxid-port", ARG_FOR_GTK|ARG_HAS_VALUE},
#ifdef FEAT_GUI_GNOME
{"--load-modules", ARG_FOR_GTK|ARG_HAS_VALUE},
{"--sm-client-id", ARG_FOR_GTK|ARG_HAS_VALUE},
{"--sm-config-prefix", ARG_FOR_GTK|ARG_HAS_VALUE},
{"--sm-disable", ARG_FOR_GTK},
{"--oaf-ior-fd", ARG_FOR_GTK|ARG_HAS_VALUE},
{"--oaf-activate-iid", ARG_FOR_GTK|ARG_HAS_VALUE},
{"--oaf-private", ARG_FOR_GTK},
{"--enable-sound", ARG_FOR_GTK},
{"--disable-sound", ARG_FOR_GTK},
{"--espeaker", ARG_FOR_GTK|ARG_HAS_VALUE},
{"-?", ARG_FOR_GTK|ARG_NEEDS_GUI},
{"--help", ARG_FOR_GTK|ARG_NEEDS_GUI|ARG_KEEP},
{"--usage", ARG_FOR_GTK|ARG_NEEDS_GUI},
# if 0 // conflicts with Vim's own --version argument
{"--version", ARG_FOR_GTK|ARG_NEEDS_GUI},
# endif
{"--disable-crash-dialog", ARG_FOR_GTK},
#endif
{NULL, 0}
};
static int gui_argc = 0;
static char **gui_argv = NULL;
static const char *role_argument = NULL;
#if defined(USE_GNOME_SESSION)
static const char *restart_command = NULL;
static char *abs_restart_command = NULL;
#endif
static int found_iconic_arg = FALSE;
#ifdef FEAT_GUI_GNOME
/*
* Can't use Gnome if --socketid given
*/
static int using_gnome = 0;
#else
# define using_gnome 0
#endif
// Comment out the following line to ignore code for resize history tracking.
#define TRACK_RESIZE_HISTORY
#ifdef TRACK_RESIZE_HISTORY
/*
* Keep a short term resize history so that stale gtk responses can be
* discarded.
* When a gtk_window_resize() request is sent to gtk, the width/height of
* the request is saved. Recent stale requests are kept around in a list.
* See https://github.com/vim/vim/issues/10123
*/
# if 0 // Change to 1 to enable ch_log() calls for debugging.
# ifdef FEAT_EVAL
# define ENABLE_RESIZE_HISTORY_LOG
# endif
# endif
/*
* History item of a resize request.
* Width and height are of gui.mainwin.
*/
typedef struct resize_history {
int used; // If true, can't match for discard. Only matches once.
int width;
int height;
# ifdef ENABLE_RESIZE_HISTORY_LOG
int seq; // for ch_log messages
# endif
struct resize_history *next;
} resize_hist_T;
// never NULL during execution
static resize_hist_T *latest_resize_hist;
// list of stale resize requests
static resize_hist_T *old_resize_hists;
/*
* Used when calling gtk_window_resize().
* Create a resize request history item, put previous request on stale list.
* Width/height are the size of the request for the gui.mainwin.
*/
static void
alloc_resize_hist(int width, int height)
{
// alloc a new resize hist, save current in list of old history
resize_hist_T *prev_hist = latest_resize_hist;
resize_hist_T *new_hist = ALLOC_CLEAR_ONE(resize_hist_T);
new_hist->width = width;
new_hist->height = height;
latest_resize_hist = new_hist;
// previous hist item becomes head of list
prev_hist->next = old_resize_hists;
old_resize_hists = prev_hist;
# ifdef ENABLE_RESIZE_HISTORY_LOG
new_hist->seq = prev_hist->seq + 1;
ch_log(NULL, "gui_gtk: New resize seq %d (%d, %d) [%d, %d]",
new_hist->seq, width, height, (int)Columns, (int)Rows);
# endif
}
/*
* Free everything on the stale resize history list.
* This list is empty when there are no outstanding resize requests.
*/
static void
clear_resize_hists(void)
{
# ifdef ENABLE_RESIZE_HISTORY_LOG
int i = 0;
# endif
if (latest_resize_hist)
latest_resize_hist->used = TRUE;
while (old_resize_hists != NULL)
{
resize_hist_T *next_hist = old_resize_hists->next;
vim_free(old_resize_hists);
old_resize_hists = next_hist;
# ifdef ENABLE_RESIZE_HISTORY_LOG
i++;
# endif
}
# ifdef ENABLE_RESIZE_HISTORY_LOG
ch_log(NULL, "gui_gtk: free %d hists", i);
# endif
}
// true if hist item is unused and matches w,h
# define MATCH_WIDTH_HEIGHT(hist, w, h) \
(!hist->used && hist->width == w && hist->height == h)
/*
* Search the resize hist list.
* Return true if the specified width,height match an item in the list that
* has never matched before. Mark the matching item as used so it will
* not match again.
*/
static int
match_stale_width_height(int width, int height)
{
resize_hist_T *hist = old_resize_hists;
for (hist = old_resize_hists; hist != NULL; hist = hist->next)
if (MATCH_WIDTH_HEIGHT(hist, width, height))
{
# ifdef ENABLE_RESIZE_HISTORY_LOG
ch_log(NULL, "gui_gtk: discard seq %d, cur seq %d",
hist->seq, latest_resize_hist->seq);
# endif
hist->used = TRUE;
return TRUE;
}
return FALSE;
}
# if defined(EXITFREE)
static void
free_all_resize_hist(void)
{
clear_resize_hists();
vim_free(latest_resize_hist);
}
# endif
#endif
/*
* GTK doesn't set the GDK_BUTTON1_MASK state when dragging a touch. Add this
* state when dragging.
*/
static guint dragging_button_state = 0;
/*
* Parse the GUI related command-line arguments. Any arguments used are
* deleted from argv, and *argc is decremented accordingly. This is called
* when vim is started, whether or not the GUI has been started.
*/
void
gui_mch_prepare(int *argc, char **argv)
{
const cmdline_option_T *option;
int i = 0;
int len = 0;
#if defined(USE_GNOME_SESSION)
/*
* Determine the command used to invoke Vim, to be passed as restart
* command to the session manager. If argv[0] contains any directory
* components try building an absolute path, otherwise leave it as is.
*/
restart_command = argv[0];
if (strchr(argv[0], G_DIR_SEPARATOR) != NULL)
{
char_u buf[MAXPATHL];
if (mch_FullName((char_u *)argv[0], buf, (int)sizeof(buf), TRUE) == OK)
{
abs_restart_command = (char *)vim_strsave(buf);
restart_command = abs_restart_command;
}
}
#endif
/*
* Move all the entries in argv which are relevant to GTK+ and GNOME
* into gui_argv. Freed later in gui_mch_init().
*/
gui_argc = 0;
gui_argv = ALLOC_MULT(char *, *argc + 1);
g_return_if_fail(gui_argv != NULL);
gui_argv[gui_argc++] = argv[i++];
while (i < *argc)
{
// Don't waste CPU cycles on non-option arguments.
if (argv[i][0] != '-' && argv[i][0] != '+')
{
++i;
continue;
}
// Look for argv[i] in cmdline_options[] table.
for (option = &cmdline_options[0]; option->name != NULL; ++option)
{
len = strlen(option->name);
if (strncmp(argv[i], option->name, len) == 0)
{
if (argv[i][len] == '\0')
break;
// allow --foo=bar style
if (argv[i][len] == '=' && (option->flags & ARG_HAS_VALUE))
break;
#ifdef FEAT_NETBEANS_INTG
// darn, -nb has non-standard syntax
if (vim_strchr((char_u *)":=", argv[i][len]) != NULL
&& (option->flags & ARG_INDEX_MASK) == ARG_NETBEANS)
break;
#endif
}
else if ((option->flags & ARG_COMPAT_LONG)
&& strcmp(argv[i], option->name + 1) == 0)
{
// Replace the standard X arguments "-name" and "-display"
// with their GNU-style long option counterparts.
argv[i] = (char *)option->name;
break;
}
}
if (option->name == NULL) // no match
{
++i;
continue;
}
if (option->flags & ARG_FOR_GTK)
{
// Move the argument into gui_argv, which
// will later be passed to gtk_init_check()
gui_argv[gui_argc++] = argv[i];
}
else
{
char *value = NULL;
// Extract the option's value if there is one.
// Accept both "--foo bar" and "--foo=bar" style.
if (option->flags & ARG_HAS_VALUE)
{
if (argv[i][len] == '=')
value = &argv[i][len + 1];
else if (i + 1 < *argc && strcmp(argv[i + 1], "--") != 0)
value = argv[i + 1];
}
// Check for options handled by Vim itself
switch (option->flags & ARG_INDEX_MASK)
{
case ARG_REVERSE:
found_reverse_arg = TRUE;
break;
case ARG_NOREVERSE:
found_reverse_arg = FALSE;
break;
case ARG_FONT:
font_argument = value;
break;
case ARG_GEOMETRY:
if (value != NULL)
gui.geom = vim_strsave((char_u *)value);
break;
case ARG_BACKGROUND:
background_argument = value;
break;
case ARG_FOREGROUND:
foreground_argument = value;
break;
case ARG_ICONIC:
found_iconic_arg = TRUE;
break;
case ARG_ROLE:
role_argument = value; // used later in gui_mch_open()
break;
#ifdef FEAT_NETBEANS_INTG
case ARG_NETBEANS:
gui.dofork = FALSE; // don't fork() when starting GUI
netbeansArg = argv[i];
break;
#endif
default:
break;
}
}
// These arguments make gnome_program_init() print a message and exit.
// Must start the GUI for this, otherwise ":gui" will exit later!
// Only when the GUI can start.
if ((option->flags & ARG_NEEDS_GUI)
&& gui_mch_early_init_check(FALSE) == OK)
gui.starting = TRUE;
if (option->flags & ARG_KEEP)
++i;
else
{
// Remove the flag from the argument vector.
if (--*argc > i)
{
int n_strip = 1;
// Move the argument's value as well, if there is one.
if ((option->flags & ARG_HAS_VALUE)
&& argv[i][len] != '='
&& strcmp(argv[i + 1], "--") != 0)
{
++n_strip;
--*argc;
if (option->flags & ARG_FOR_GTK)
gui_argv[gui_argc++] = argv[i + 1];
}
if (*argc > i)
mch_memmove(&argv[i], &argv[i + n_strip],
(*argc - i) * sizeof(char *));
}
argv[*argc] = NULL;
}
}
gui_argv[gui_argc] = NULL;
}
#if defined(EXITFREE) || defined(PROTO)
void
gui_mch_free_all(void)
{
vim_free(gui_argv);
#if defined(USE_GNOME_SESSION)
vim_free(abs_restart_command);
#endif
#ifdef TRACK_RESIZE_HISTORY
free_all_resize_hist();
#endif
}
#endif
#if !GTK_CHECK_VERSION(3,0,0)
/*
* This should be maybe completely removed.
* Doesn't seem possible, since check_copy_area() relies on
* this information. --danielk
*/
static gint
visibility_event(GtkWidget *widget UNUSED,
GdkEventVisibility *event,
gpointer data UNUSED)
{
gui.visibility = event->state;
/*
* When we do an gdk_window_copy_area(), and the window is partially
* obscured, we want to receive an event to tell us whether it worked
* or not.
*/
if (gui.text_gc != NULL)
gdk_gc_set_exposures(gui.text_gc,
gui.visibility != GDK_VISIBILITY_UNOBSCURED);
return FALSE;
}
#endif // !GTK_CHECK_VERSION(3,0,0)
/*
* Redraw the corresponding portions of the screen.
*/
#if GTK_CHECK_VERSION(3,0,0)
static gboolean
draw_event(GtkWidget *widget UNUSED,
cairo_t *cr,
gpointer user_data UNUSED)
{
// Skip this when the GUI isn't set up yet, will redraw later.
if (gui.starting)
return FALSE;
out_flush(); // make sure all output has been processed
// for GTK+ 3, may induce other draw events.
cairo_set_source_surface(cr, gui.surface, 0, 0);
{
cairo_rectangle_list_t *list = NULL;
list = cairo_copy_clip_rectangle_list(cr);
if (list->status != CAIRO_STATUS_CLIP_NOT_REPRESENTABLE)
{
int i;
for (i = 0; i < list->num_rectangles; i++)
{
const cairo_rectangle_t *rect = &list->rectangles[i];
cairo_rectangle(cr, rect->x, rect->y,
rect->width, rect->height);
cairo_fill(cr);
}
}
cairo_rectangle_list_destroy(list);
}
return FALSE;
}
#else // !GTK_CHECK_VERSION(3,0,0)
static gint
expose_event(GtkWidget *widget UNUSED,
GdkEventExpose *event,
gpointer data UNUSED)
{
// Skip this when the GUI isn't set up yet, will redraw later.
if (gui.starting)
return FALSE;
out_flush(); // make sure all output has been processed
gui_redraw(event->area.x, event->area.y,
event->area.width, event->area.height);
// Clear the border areas if needed
if (event->area.x < FILL_X(0))
gdk_window_clear_area(gui.drawarea->window, 0, 0, FILL_X(0), 0);
if (event->area.y < FILL_Y(0))
gdk_window_clear_area(gui.drawarea->window, 0, 0, 0, FILL_Y(0));
if (event->area.x > FILL_X(Columns))
gdk_window_clear_area(gui.drawarea->window,
FILL_X((int)Columns), 0, 0, 0);
if (event->area.y > FILL_Y(Rows))
gdk_window_clear_area(gui.drawarea->window, 0, FILL_Y((int)Rows), 0, 0);
return FALSE;
}
#endif // !GTK_CHECK_VERSION(3,0,0)
#ifdef FEAT_CLIENTSERVER
/*
* Handle changes to the "Comm" property
*/
static gint
property_event(GtkWidget *widget,
GdkEventProperty *event,
gpointer data UNUSED)
{
if (event->type == GDK_PROPERTY_NOTIFY
&& event->state == (int)GDK_PROPERTY_NEW_VALUE
&& GDK_WINDOW_XID(event->window) == commWindow
&& GET_X_ATOM(event->atom) == commProperty)
{
XEvent xev;
// Translate to XLib
xev.xproperty.type = PropertyNotify;
xev.xproperty.atom = commProperty;
xev.xproperty.window = commWindow;
xev.xproperty.state = PropertyNewValue;
serverEventProc(GDK_WINDOW_XDISPLAY(gtk_widget_get_window(widget)),
&xev, 0);
}
return FALSE;
}
#endif // defined(FEAT_CLIENTSERVER)
/*
* Handle changes to the "Xft/DPI" setting
*/
static void
gtk_settings_xft_dpi_changed_cb(GtkSettings *gtk_settings UNUSED,
GParamSpec *pspec UNUSED,
gpointer data UNUSED)
{
// Create a new PangoContext for this screen, and initialize it
// with the current font if necessary.
if (gui.text_context != NULL)
g_object_unref(gui.text_context);
gui.text_context = gtk_widget_create_pango_context(gui.mainwin);
pango_context_set_base_dir(gui.text_context, PANGO_DIRECTION_LTR);
if (gui.norm_font != NULL)
{
// force default font
gui_mch_init_font(*p_guifont == NUL ? NULL : p_guifont, FALSE);
gui_set_shellsize(TRUE, FALSE, RESIZE_BOTH);
}
}
typedef gboolean timeout_cb_type;
/*
* Start a timer that will invoke the specified callback.
* Returns the ID of the timer.
*/
static guint
timeout_add(int time, timeout_cb_type (*callback)(gpointer), int *flagp)
{
return g_timeout_add((guint)time, (GSourceFunc)callback, flagp);
}
static void
timeout_remove(guint timer)
{
g_source_remove(timer);
}
/////////////////////////////////////////////////////////////////////////////
// Focus handlers:
/*
* This is a simple state machine:
* BLINK_NONE not blinking at all
* BLINK_OFF blinking, cursor is not shown
* BLINK_ON blinking, cursor is shown
*/
#define BLINK_NONE 0
#define BLINK_OFF 1
#define BLINK_ON 2
static int blink_state = BLINK_NONE;
static long_u blink_waittime = 700;
static long_u blink_ontime = 400;
static long_u blink_offtime = 250;
static guint blink_timer = 0;
int
gui_mch_is_blinking(void)
{
return blink_state != BLINK_NONE;
}
int
gui_mch_is_blink_off(void)
{
return blink_state == BLINK_OFF;
}
void
gui_mch_set_blinking(long waittime, long on, long off)
{
blink_waittime = waittime;
blink_ontime = on;
blink_offtime = off;
}
/*
* Stop the cursor blinking. Show the cursor if it wasn't shown.
*/
void
gui_mch_stop_blink(int may_call_gui_update_cursor)
{
if (blink_timer)
{
timeout_remove(blink_timer);
blink_timer = 0;
}
if (blink_state == BLINK_OFF && may_call_gui_update_cursor)
{
gui_update_cursor(TRUE, FALSE);
#if !GTK_CHECK_VERSION(3,0,0)
gui_mch_flush();
#endif
}
blink_state = BLINK_NONE;
}
static timeout_cb_type
blink_cb(gpointer data UNUSED)
{
if (blink_state == BLINK_ON)
{
gui_undraw_cursor();
blink_state = BLINK_OFF;
blink_timer = timeout_add(blink_offtime, blink_cb, NULL);
}
else
{
gui_update_cursor(TRUE, FALSE);
blink_state = BLINK_ON;
blink_timer = timeout_add(blink_ontime, blink_cb, NULL);
}
#if !GTK_CHECK_VERSION(3,0,0)
gui_mch_flush();
#endif
return FALSE; // don't happen again
}
/*
* Start the cursor blinking. If it was already blinking, this restarts the
* waiting time and shows the cursor.
*/
void
gui_mch_start_blink(void)
{
if (blink_timer)
{
timeout_remove(blink_timer);
blink_timer = 0;
}
// Only switch blinking on if none of the times is zero
if (blink_waittime && blink_ontime && blink_offtime && gui.in_focus)
{
blink_timer = timeout_add(blink_waittime, blink_cb, NULL);
blink_state = BLINK_ON;
gui_update_cursor(TRUE, FALSE);
#if !GTK_CHECK_VERSION(3,0,0)
gui_mch_flush();
#endif
}
}