-
-
Notifications
You must be signed in to change notification settings - Fork 695
Expand file tree
/
Copy pathgui_gtk4.c
More file actions
5917 lines (5075 loc) · 145 KB
/
Copy pathgui_gtk4.c
File metadata and controls
5917 lines (5075 loc) · 145 KB
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.
*
* GTK4 GUI implementation: main window, events, drawing, menus,
* scrollbars, dialogs, and toolbar. This is a clean implementation for
* GTK4, separate from gui_gtk_x11.c which handles GTK2/GTK3.
*
* GTK4 differences from GTK3:
* - No GdkWindow (use GdkSurface for top-level only)
* - No GtkContainer (use gtk_widget_set_parent/gtk_box_append)
* - Events via GtkEventController, not signal+mask
* - Drawing via GtkSnapshot or gtk_drawing_area_set_draw_func
* - No gtk_dialog_run (async dialogs)
* - No GdkAtom (string-based content types)
* - No GtkSocket/GtkPlug
* - gtk_window_new() takes no arguments
*/
#include "vim.h"
#ifdef FEAT_GUI_GTK
#include <gdk/gdkkeysyms.h>
#include <gdk/gdk.h>
#include <gtk/gtk.h>
#include "gui_gtk4_f.h"
#include "gui_gtk4_cb.h"
#ifdef USE_GTK4_SNAPSHOT
# include "gui_gtk4_da.h"
#endif
#ifdef FEAT_TOOLBAR
# include "gui_gtk4_tb.h"
#endif
/*
* Geometry string parser, replacing XParseGeometry to remove X11 dependency.
* Format: [WIDTHxHEIGHT][{+-}XOFF{+-}YOFF]
*/
#define NoValue 0x0000
#define XValue 0x0001
#define YValue 0x0002
#define WidthValue 0x0004
#define HeightValue 0x0008
#define XNegative 0x0010
#define YNegative 0x0020
static int
vim_parse_geometry(const char *str, int *x, int *y,
unsigned int *width, unsigned int *height)
{
int mask = NoValue;
char *end;
long val;
if (str == NULL || *str == NUL)
return mask;
// Parse width
if (*str != '+' && *str != '-')
{
val = strtol(str, &end, 10);
if (end != str)
{
*width = (unsigned int)val;
mask |= WidthValue;
str = end;
}
}
// Parse 'x' or 'X' separator and height
if (*str == 'x' || *str == 'X')
{
str++;
val = strtol(str, &end, 10);
if (end != str)
{
*height = (unsigned int)val;
mask |= HeightValue;
str = end;
}
}
// Parse x offset
if (*str == '+' || *str == '-')
{
int negative = (*str == '-');
str++;
val = strtol(str, &end, 10);
if (end != str)
{
*x = negative ? -(int)val : (int)val;
mask |= XValue;
if (negative)
mask |= XNegative;
str = end;
}
}
// Parse y offset
if (*str == '+' || *str == '-')
{
int negative = (*str == '-');
str++;
val = strtol(str, &end, 10);
if (end != str)
{
*y = negative ? -(int)val : (int)val;
mask |= YValue;
if (negative)
mask |= YNegative;
}
}
return mask;
}
#if defined(FEAT_MOUSESHAPE)
// Last set mouse pointer shape
static int last_shape = 0;
#endif
#define DEFAULT_FONT "Monospace 10"
// Menu action group for GMenu-based menus
static GSimpleActionGroup *menu_action_group = NULL;
// Cursor blinking state
static enum {
BLINK_NONE,
BLINK_OFF,
BLINK_ON
} blink_state = BLINK_NONE;
// GTK4 main loop compatibility
static int gtk4_main_loop_level = 0;
static int gtk4_main_loop_quit = FALSE;
#ifdef USE_GRESOURCE
# include "auto/gui_gtk_gresources.h"
#endif
typedef gboolean timeout_cb_type;
/*
* Table of special key mappings.
*/
static struct special_key
{
guint key_sym;
char_u code0;
char_u code1;
}
const special_keys[] =
{
{GDK_KEY_Up, 'k', 'u'},
{GDK_KEY_Down, 'k', 'd'},
{GDK_KEY_Left, 'k', 'l'},
{GDK_KEY_Right, 'k', 'r'},
{GDK_KEY_F1, 'k', '1'},
{GDK_KEY_F2, 'k', '2'},
{GDK_KEY_F3, 'k', '3'},
{GDK_KEY_F4, 'k', '4'},
{GDK_KEY_F5, 'k', '5'},
{GDK_KEY_F6, 'k', '6'},
{GDK_KEY_F7, 'k', '7'},
{GDK_KEY_F8, 'k', '8'},
{GDK_KEY_F9, 'k', '9'},
{GDK_KEY_F10, 'k', ';'},
{GDK_KEY_F11, 'F', '1'},
{GDK_KEY_F12, 'F', '2'},
{GDK_KEY_Help, '%', '1'},
{GDK_KEY_Undo, '&', '8'},
{GDK_KEY_BackSpace, 'k', 'b'},
{GDK_KEY_Insert, 'k', 'I'},
{GDK_KEY_Delete, 'k', 'D'},
{GDK_KEY_Home, 'k', 'h'},
{GDK_KEY_End, '@', '7'},
{GDK_KEY_Prior, 'k', 'P'},
{GDK_KEY_Next, 'k', 'N'},
{GDK_KEY_Print, '%', '9'},
{GDK_KEY_KP_Left, 'k', 'l'},
{GDK_KEY_KP_Right, 'k', 'r'},
{GDK_KEY_KP_Up, 'k', 'u'},
{GDK_KEY_KP_Down, 'k', 'd'},
{GDK_KEY_KP_Insert, KS_EXTRA, (char_u)KE_KINS},
{GDK_KEY_KP_Delete, KS_EXTRA, (char_u)KE_KDEL},
{GDK_KEY_KP_Home, 'K', '1'},
{GDK_KEY_KP_End, 'K', '4'},
{GDK_KEY_KP_Prior, 'K', '3'},
{GDK_KEY_KP_Next, 'K', '5'},
{GDK_KEY_KP_Add, 'K', '6'},
{GDK_KEY_KP_Subtract, 'K', '7'},
{GDK_KEY_KP_Divide, 'K', '8'},
{GDK_KEY_KP_Multiply, 'K', '9'},
{GDK_KEY_KP_Enter, 'K', 'A'},
{GDK_KEY_KP_Decimal, 'K', 'B'},
{GDK_KEY_KP_0, 'K', 'C'},
{GDK_KEY_KP_1, 'K', 'D'},
{GDK_KEY_KP_2, 'K', 'E'},
{GDK_KEY_KP_3, 'K', 'F'},
{GDK_KEY_KP_4, 'K', 'G'},
{GDK_KEY_KP_5, 'K', 'H'},
{GDK_KEY_KP_6, 'K', 'I'},
{GDK_KEY_KP_7, 'K', 'J'},
{GDK_KEY_KP_8, 'K', 'K'},
{GDK_KEY_KP_9, 'K', 'L'},
{0, 0, 0}
};
static int
keyval_to_string(unsigned int keyval, char_u *string)
{
int len;
guint32 uc;
uc = gdk_keyval_to_unicode(keyval);
if (uc != 0)
{
len = utf_char2bytes((int)uc, string);
}
else
{
len = 1;
switch (keyval)
{
case GDK_KEY_Tab: case GDK_KEY_KP_Tab: case GDK_KEY_ISO_Left_Tab:
string[0] = TAB;
break;
case GDK_KEY_Linefeed:
string[0] = NL;
break;
case GDK_KEY_Return: case GDK_KEY_ISO_Enter: case GDK_KEY_3270_Enter:
string[0] = CAR;
break;
case GDK_KEY_Escape:
string[0] = ESC;
break;
default:
len = 0;
break;
}
}
string[len] = NUL;
return len;
}
static int
modifiers_gdk2vim(guint state)
{
int modifiers = 0;
if (state & GDK_SHIFT_MASK)
modifiers |= MOD_MASK_SHIFT;
if (state & GDK_CONTROL_MASK)
modifiers |= MOD_MASK_CTRL;
if (state & GDK_ALT_MASK)
modifiers |= MOD_MASK_ALT;
if (state & GDK_META_MASK)
modifiers |= MOD_MASK_META;
if (state & GDK_SUPER_MASK)
modifiers |= MOD_MASK_CMD;
return modifiers;
}
static GtkWidget *vbox; // the main vertical box
// Forward declarations for event callbacks
#ifndef USE_GTK4_SNAPSHOT
static void draw_event(GtkDrawingArea *area, cairo_t *cr, int width, int height, gpointer data);
#endif
static gboolean key_press_event(GtkEventControllerKey *controller, guint keyval, guint keycode, GdkModifierType state, gpointer data);
static void key_release_event(GtkEventControllerKey *controller, guint keyval, guint keycode, GdkModifierType state, gpointer data);
static void button_press_event(GtkGestureClick *gesture, int n_press, double x, double y, gpointer data);
static void button_release_event(GtkGestureClick *gesture, int n_press, double x, double y, gpointer data);
static void motion_notify_event(GtkEventControllerMotion *controller, double x, double y, gpointer data);
static void enter_notify_event(GtkEventControllerMotion *controller, double x, double y, gpointer data);
static gboolean scroll_event(GtkEventControllerScroll *controller, double dx, double dy, gpointer data);
static void focus_in_event(GtkEventControllerFocus *controller, gpointer data);
static void focus_out_event(GtkEventControllerFocus *controller, gpointer data);
#ifdef FEAT_MENU
static gboolean menubar_popover_closed_hook(GSignalInvocationHint *ihint, guint n_param_values, const GValue *param_values, gpointer data);
#endif
#ifdef FEAT_DND
static gboolean drop_cb(GtkDropTarget *target, const GValue *value, double x, double y, gpointer data);
#endif
#ifdef FEAT_GUI_TABLINE
static void tabline_enter_cb(GtkEventController *controller, double x, double y, void *udata);
static void on_select_tab(GtkNotebook *notebook, gpointer *page, gint idx, gpointer data);
static void on_tab_reordered(GtkNotebook *notebook, gpointer *page, gint idx, gpointer data);
static GMenu *create_tabline_popup_menu(GActionGroup **agroup_store);
static void tabline_menu_press_event(GtkGestureClick *gesture, int n_press, double x, double y, GtkWidget *popover);
#endif
static void mainwin_destroy_cb(GObject *object, gpointer data);
static gboolean delete_event_cb(GtkWindow *window, gpointer data);
static void mainwin_fullscreened_cb(GObject *obj, GParamSpec *pspec, gpointer user_data);
#ifndef USE_GTK4_SNAPSHOT
static void drawarea_realize_cb(GtkWidget *widget, gpointer data);
#endif
static void drawarea_unrealize_cb(GtkWidget *widget, gpointer data);
#ifndef USE_GTK4_SNAPSHOT
static void drawarea_resize_cb(GtkDrawingArea *area, int width, int height, gpointer data);
static void drawarea_scale_factor_cb(GObject *object, GParamSpec *pspec, gpointer data);
static cairo_surface_t *create_backing_surface(int width, int height);
#endif
static void clipboard_changed_cb(GdkClipboard *clipboard, gpointer user_data);
#ifdef FEAT_MENU
static void show_menubar_popover(void);
#endif
/*
* 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)
{
// Don't call gtk_init() here. It will be called in
// gui_mch_init_check() after the fork. Calling it before fork
// breaks the display connection in the child process, causing gvim
// to fail to start without --nofork.
}
/*
* Free all GUI related resources.
*/
void
gui_mch_free_all(void)
{
}
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);
}
static long_u blink_waittime = 700;
static long_u blink_ontime = 400;
static long_u blink_offtime = 250;
static guint blink_timer = 0;
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);
}
return FALSE;
}
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;
}
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);
blink_state = BLINK_NONE;
}
void
gui_mch_start_blink(void)
{
if (blink_timer)
{
timeout_remove(blink_timer);
blink_timer = 0;
}
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);
}
}
int
gui_mch_early_init_check(int give_message UNUSED)
{
return OK;
}
int
gui_mch_init_check(void)
{
// This defaults to argv[0], but we want it to match the name of the
// shipped gvim.desktop so that Vim's windows can be associated with this
// file. Also sets WM_CLASS on X11.
g_set_prgname("gvim");
// Suppress noisy EGL warnings when GL is not available. Only set
// this when actually starting the GUI, so non-GUI invocations are
// not affected.
if (g_getenv("EGL_LOG_LEVEL") == NULL)
setenv("EGL_LOG_LEVEL", "fatal", 0);
// Call gtk_init() here after fork(). Calling it before fork() breaks
// the display connection in the child process.
gtk_init();
return OK;
}
/*
* Initialise the GUI. Create all the windows, set up all the callbacks etc.
* Returns OK for success, FAIL when the GUI can't be started.
*/
int
gui_mch_init(void)
{
// Allocate GdkRGBA color structs.
gui.fgcolor = g_new0(GdkRGBA, 1);
gui.bgcolor = g_new0(GdkRGBA, 1);
gui.spcolor = g_new0(GdkRGBA, 1);
gui.def_norm_pixel = 0x00000000; // black
gui.def_back_pixel = 0x00ffffff; // white
gui.norm_pixel = gui.def_norm_pixel;
gui.back_pixel = gui.def_back_pixel;
gui.scrollbar_width = SB_DEFAULT_WIDTH;
gui.scrollbar_height = SB_DEFAULT_WIDTH;
// Create the main window.
gui.mainwin = gtk_window_new();
gtk_widget_set_name(gui.mainwin, "vim-main-window");
// Create the PangoContext used for drawing all text.
gui.text_context = gtk_widget_create_pango_context(gui.mainwin);
pango_context_set_base_dir(gui.text_context, PANGO_DIRECTION_LTR);
g_signal_connect(G_OBJECT(gui.mainwin), "close-request",
G_CALLBACK(delete_event_cb), NULL);
g_signal_connect(G_OBJECT(gui.mainwin), "notify::fullscreened",
G_CALLBACK(mainwin_fullscreened_cb), NULL);
// A vertical box holds the menubar, toolbar and main text window.
vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
gtk_box_set_homogeneous(GTK_BOX(vbox), FALSE);
gtk_window_set_child(GTK_WINDOW(gui.mainwin), vbox);
#ifdef FEAT_MENU
{
GMenu *gmenu = g_menu_new();
gui.menubar = gtk_popover_menu_bar_new_from_model(
G_MENU_MODEL(gmenu));
g_object_set_data_full(G_OBJECT(gui.menubar), "vim-gmenu",
gmenu, g_object_unref);
gtk_widget_set_name(gui.menubar, "vim-menubar");
gtk_widget_set_visible(gui.menubar, FALSE);
gtk_box_append(GTK_BOX(vbox), gui.menubar);
}
// Return keyboard focus to the drawing area when a menubar popover
// closes (issue #20274). GtkPopoverMenuBar owns its popovers
// privately, so attach via an emission hook on GtkPopover::closed
// and filter for popovers under our menubar inside the callback.
{
GTypeClass *cls = g_type_class_ref(GTK_TYPE_POPOVER);
guint sig_id = g_signal_lookup("closed", GTK_TYPE_POPOVER);
if (sig_id != 0)
g_signal_add_emission_hook(sig_id, 0,
menubar_popover_closed_hook, NULL, NULL);
if (cls != NULL)
g_type_class_unref(cls);
}
#endif
#ifdef FEAT_TOOLBAR
gui.toolbar = vim_toolbar_new();
gtk_widget_set_name(gui.toolbar, "vim-toolbar");
gtk_widget_set_visible(gui.toolbar, FALSE);
gtk_box_append(GTK_BOX(vbox), gui.toolbar);
vim_toolbar_set_style(VIM_TOOLBAR(gui.toolbar), toolbar_flags, tbis_flags);
#endif
#ifdef FEAT_GUI_TABLINE
gui.tabline = gtk_notebook_new();
gtk_notebook_set_show_border(GTK_NOTEBOOK(gui.tabline), FALSE);
gtk_notebook_set_show_tabs(GTK_NOTEBOOK(gui.tabline), TRUE);
gtk_notebook_set_scrollable(GTK_NOTEBOOK(gui.tabline), TRUE);
gtk_widget_set_visible(gui.tabline, FALSE);
gtk_box_append(GTK_BOX(vbox), gui.tabline);
g_signal_connect(G_OBJECT(gui.tabline), "switch-page",
G_CALLBACK(on_select_tab), NULL);
g_signal_connect(G_OBJECT(gui.tabline), "page-reordered",
G_CALLBACK(on_tab_reordered), NULL);
{
GtkEventController *mcontroller = gtk_event_controller_motion_new();
g_signal_connect(mcontroller, "enter",
G_CALLBACK(tabline_enter_cb), NULL);
// Make sure that tabline_enter_cb() is always called before
// tabpage_enter_cb().
gtk_event_controller_set_propagation_phase(
mcontroller, GTK_PHASE_CAPTURE);
gtk_widget_add_controller(gui.tabline, mcontroller);
}
// Create right click popup menu for tabline
{
GtkGesture *click;
GActionGroup *agroup;
GMenu *menu;
GtkWidget *popover;
click = gtk_gesture_click_new();
menu = create_tabline_popup_menu(&agroup);
popover = gtk_popover_menu_new_from_model(G_MENU_MODEL(menu));
g_object_unref(menu);
gtk_widget_set_parent(popover, gui.tabline);
g_object_set_data(G_OBJECT(gui.tabline), "menu", popover);
gtk_widget_insert_action_group(gui.tabline, "tabline", agroup);
g_object_unref(agroup);
gtk_popover_set_has_arrow(GTK_POPOVER(popover), FALSE);
gtk_popover_set_position(GTK_POPOVER(popover), GTK_POS_BOTTOM);
// Listen for anny mouse button
gtk_gesture_single_set_button(GTK_GESTURE_SINGLE(click), 0);
g_signal_connect_object(click, "pressed",
G_CALLBACK(tabline_menu_press_event), popover, G_CONNECT_DEFAULT);
gtk_widget_add_controller(gui.tabline, GTK_EVENT_CONTROLLER(click));
}
#endif
// The form widget manages absolute positioning of scrollbars and the draw
// area.
gui.formwin = vim_form_new();
gtk_widget_set_name(gui.formwin, "vim-gtk-form");
gtk_widget_set_vexpand(gui.formwin, TRUE);
gtk_widget_set_hexpand(gui.formwin, TRUE);
gtk_box_append(GTK_BOX(vbox), gui.formwin);
// The drawing area for the editor content.
#ifdef USE_GTK4_SNAPSHOT
gui.drawarea = vim_draw_area_new();
#else
gui.drawarea = gtk_drawing_area_new();
gui.surface = NULL;
#endif
gtk_widget_set_focusable(gui.drawarea, TRUE);
gtk_widget_set_vexpand(gui.drawarea, TRUE);
gtk_widget_set_hexpand(gui.drawarea, TRUE);
vim_form_put(VIM_FORM(gui.formwin), gui.drawarea, 0, 0);
#ifndef USE_GTK4_SNAPSHOT
// Set up drawing.
gtk_drawing_area_set_draw_func(GTK_DRAWING_AREA(gui.drawarea),
(GtkDrawingAreaDrawFunc)draw_event, NULL, NULL);
g_signal_connect(G_OBJECT(gui.drawarea), "resize",
G_CALLBACK(drawarea_resize_cb), NULL);
g_signal_connect(G_OBJECT(gui.drawarea), "notify::scale-factor",
G_CALLBACK(drawarea_scale_factor_cb), NULL);
g_signal_connect(G_OBJECT(gui.drawarea), "realize",
G_CALLBACK(drawarea_realize_cb), NULL);
#endif
g_signal_connect(G_OBJECT(gui.drawarea), "unrealize",
G_CALLBACK(drawarea_unrealize_cb), NULL);
// Set up event controllers.
{
GtkEventController *key_ctrl = gtk_event_controller_key_new();
g_signal_connect(key_ctrl, "key-pressed",
G_CALLBACK(key_press_event), NULL);
g_signal_connect(key_ctrl, "key-released",
G_CALLBACK(key_release_event), NULL);
gtk_widget_add_controller(gui.drawarea, key_ctrl);
#ifdef FEAT_XIM
xim_init();
#endif
}
{
GtkGesture *click = gtk_gesture_click_new();
gtk_gesture_single_set_button(GTK_GESTURE_SINGLE(click), 0);
g_signal_connect(click, "pressed",
G_CALLBACK(button_press_event), NULL);
g_signal_connect(click, "released",
G_CALLBACK(button_release_event), NULL);
gtk_widget_add_controller(gui.drawarea, GTK_EVENT_CONTROLLER(click));
}
{
GtkEventController *motion = gtk_event_controller_motion_new();
g_signal_connect(motion, "motion",
G_CALLBACK(motion_notify_event), NULL);
g_signal_connect(motion, "enter",
G_CALLBACK(enter_notify_event), NULL);
gtk_widget_add_controller(gui.drawarea, motion);
}
{
GtkEventController *scroll = gtk_event_controller_scroll_new(
GTK_EVENT_CONTROLLER_SCROLL_BOTH_AXES
| GTK_EVENT_CONTROLLER_SCROLL_DISCRETE);
g_signal_connect(scroll, "scroll",
G_CALLBACK(scroll_event), NULL);
gtk_widget_add_controller(gui.drawarea, scroll);
}
{
GtkEventController *focus = gtk_event_controller_focus_new();
g_signal_connect(focus, "enter",
G_CALLBACK(focus_in_event), NULL);
g_signal_connect(focus, "leave",
G_CALLBACK(focus_out_event), NULL);
gtk_widget_add_controller(gui.drawarea, focus);
}
#ifdef FEAT_DND
// Set up drag-and-drop target for files and text.
{
GtkDropTarget *drop = gtk_drop_target_new(G_TYPE_INVALID, GDK_ACTION_COPY);
GType types[] = { GDK_TYPE_FILE_LIST, G_TYPE_STRING };
gtk_drop_target_set_gtypes(drop, types, 2);
g_signal_connect(drop, "drop",
G_CALLBACK(drop_cb), NULL);
gtk_widget_add_controller(gui.drawarea, GTK_EVENT_CONTROLLER(drop));
}
#endif
gui.border_offset = gui.border_width;
// Create a blank (invisible) cursor for hiding the mouse pointer.
gui.blank_pointer = gdk_cursor_new_from_name("none", NULL);
{
GdkDisplay *display = gtk_widget_get_display(gui.mainwin);
GdkClipboard *primary = gdk_display_get_primary_clipboard(display);
GdkClipboard *board = gdk_display_get_clipboard(display);
if (primary != NULL)
g_signal_connect(primary, "changed",
G_CALLBACK(clipboard_changed_cb), &clip_star);
if (board != NULL)
g_signal_connect(board, "changed",
G_CALLBACK(clipboard_changed_cb), &clip_plus);
}
gui.regular_provider = vim_content_provider_new(&clip_plus);
gui.primary_provider = vim_content_provider_new(&clip_star);
return OK;
}
#ifndef USE_GTK4_SNAPSHOT
/*
* Called when the foreground or background color has been changed.
*/
static void
surface_fill_bg(void)
{
if (gui.surface != NULL)
{
cairo_t *cr = cairo_create(gui.surface);
cairo_set_source_rgba(cr,
gui.bgcolor->red, gui.bgcolor->green,
gui.bgcolor->blue, gui.bgcolor->alpha);
cairo_paint(cr);
cairo_destroy(cr);
}
}
#endif
void
gui_mch_new_colors(void)
{
#ifndef USE_GTK4_SNAPSHOT
surface_fill_bg();
#endif
if (gui.drawarea != NULL && gtk_widget_get_realized(gui.drawarea))
gtk_widget_queue_draw(gui.drawarea);
}
/*
* Open the GUI window which was created by a call to gui_mch_init().
*/
int
gui_mch_open(void)
{
guicolor_T fg_pixel = INVALCOLOR;
guicolor_T bg_pixel = INVALCOLOR;
guint pixel_width;
guint pixel_height;
if (gui.geom != NULL)
{
int mask;
unsigned int w, h;
int x = 0;
int y = 0;
mask = vim_parse_geometry((char *)gui.geom, &x, &y, &w, &h);
if (mask & WidthValue)
Columns = w;
if (mask & HeightValue)
{
if (p_window > (long)h - 1 || !option_was_set((char_u *)"window"))
p_window = h - 1;
Rows = h;
}
limit_screen_size();
VIM_CLEAR(gui.geom);
}
// Use 80x24 as the default GUI size, unless geometry was specified.
if (Columns > 80 && gui.geom == NULL)
Columns = 80;
if (Rows > 24 && gui.geom == NULL)
Rows = 24;
pixel_width = (guint)(gui_get_base_width() + Columns * gui.char_width);
pixel_height = (guint)(gui_get_base_height() + Rows * gui.char_height);
gtk_window_set_default_size(GTK_WINDOW(gui.mainwin),
pixel_width, pixel_height);
if (foreground_argument != NULL)
fg_pixel = gui_get_color((char_u *)foreground_argument);
if (fg_pixel == INVALCOLOR)
fg_pixel = gui_get_color((char_u *)"Black");
if (background_argument != NULL)
bg_pixel = gui_get_color((char_u *)background_argument);
if (bg_pixel == INVALCOLOR)
bg_pixel = gui_get_color((char_u *)"White");
if (found_reverse_arg)
{
gui.def_norm_pixel = bg_pixel;
gui.def_back_pixel = fg_pixel;
}
else
{
gui.def_norm_pixel = fg_pixel;
gui.def_back_pixel = bg_pixel;
}
set_normal_colors();
gui_check_colors();
highlight_gui_started();
g_signal_connect(G_OBJECT(gui.mainwin), "destroy",
G_CALLBACK(mainwin_destroy_cb), NULL);
// Resize is handled by GtkForm's size_allocate callback.
gtk_widget_set_visible(gui.mainwin, TRUE);
// Make sure the drawing area gets keyboard focus.
gtk_widget_grab_focus(gui.drawarea);
gui_focus_change(TRUE);
return OK;
}
void
gui_mch_exit(int rc UNUSED)
{
if (gui.mainwin != NULL)
{
#ifdef FEAT_GUI_TABLINE
// Must unparent popover menu for tabline or we will get warning
gtk_widget_unparent(g_object_get_data(G_OBJECT(gui.tabline), "menu"));
#endif
#ifdef FEAT_BEVAL_GUI
// Make sure to destroy popover used for balloon eval, or we will get a
// warning from GTK that the draw area still has children left.
gui_mch_destroy_beval_area(balloonEval);
#endif
gtk_window_destroy(GTK_WINDOW(gui.mainwin));
}
}
int
gui_mch_get_winpos(int *x, int *y)
{
// GTK4 does not provide a window position API.
*x = 0;
*y = 0;
return FAIL;
}
void
gui_mch_set_winpos(int x UNUSED, int y UNUSED)
{
// GTK4/Wayland: window positioning not available
}
int
gui_mch_maximized(void)
{
return gtk_window_is_maximized(GTK_WINDOW(gui.mainwin));
}
void
gui_mch_unmaximize(void)
{
if (gui.mainwin != NULL)
gtk_window_unmaximize(GTK_WINDOW(gui.mainwin));
}
void
gui_mch_set_fullscreen(int flag)
{
if (gui.mainwin == NULL)
return;
if (flag)
gtk_window_fullscreen(GTK_WINDOW(gui.mainwin));
else
gtk_window_unfullscreen(GTK_WINDOW(gui.mainwin));
}
static void
mainwin_fullscreened_cb(GObject *obj,
GParamSpec *pspec UNUSED, gpointer user_data UNUSED)
{
// Force a redraw of the drawing area when entering fullscreen mode.
if (gtk_window_is_fullscreen(GTK_WINDOW(obj)))
gui_focus_change(TRUE);
}
/*
* Called when the font changed while the window is maximized or GO_KEEPWINSIZE
* is set. Recalculate Rows and Columns based on the current window size.
*
* NOTE: gui_set_shellsize() calls this when GO_KEEPWINSIZE ('k') is in
* 'guioptions', even when the font hasn't actually changed (e.g. just setting
* "guioptions=k" triggers it via gui_init_which_components()). This is
* arguably a design problem in the common code, but we must not call
* gui_set_shellsize() back from here or it will cause infinite recursion and
* crash. Use gui_resize_shell() to recalculate Rows/Columns from the current
* window size instead.
*/
void
gui_mch_newfont(void)
{
int w, h;
w = gtk_widget_get_width(gui.formwin);
h = gtk_widget_get_height(gui.formwin);
w -= get_menu_tool_width();
h -= get_menu_tool_height();
gui_resize_shell(w, h);
}
void
gui_mch_settitle(char_u *title, char_u *icon UNUSED)
{
if (title != NULL && gui.mainwin != NULL)
gtk_window_set_title(GTK_WINDOW(gui.mainwin), (const char *)title);
}
/*
* Get height of window decorations, that we cannot determine directly. For
* example, the GtkHeaderBar widget. This is called in gui_resize_shell(), we
* cannot call it in gui_set_shellsize(), because that may be called before the
* drawarea/formwin is resized, which may cause the drawarea to be bigger than
* it actually is (while the window size is up to date), causing a negative
* "decor_height".
*/
void
gui_gtk_init_decor_height(void)
{
int h = gtk_widget_get_height(gui.mainwin);
if (h == 0)
return;
h -= get_menu_tool_height();
h -= gtk_widget_get_height(gui.formwin);
gui.decor_height = h;
}
void
gui_mch_set_shellsize(int width, int height,
int min_width UNUSED, int min_height UNUSED,
int base_width UNUSED, int base_height UNUSED,
int direction UNUSED)
{
width += get_menu_tool_width();
height += get_menu_tool_height();
// GtkWindow default size also includes client side decorations, so must
// include it also.
height += gui.decor_height;
gtk_window_set_default_size(GTK_WINDOW(gui.mainwin), width, height);
}
void
gui_mch_get_screen_dimensions(int *screen_w, int *screen_h)
{
GdkDisplay *display = gtk_widget_get_display(gui.mainwin);
GdkSurface *surface = gtk_native_get_surface(GTK_NATIVE(gui.mainwin));
if (surface != NULL)
{
GdkMonitor *monitor = gdk_display_get_monitor_at_surface(display,
surface);
if (monitor != NULL)
{
GdkRectangle geom;
gdk_monitor_get_geometry(monitor, &geom);
*screen_w = geom.width;
*screen_h = geom.height;
return;
}
}
*screen_w = 800;
*screen_h = 600;
}
#ifdef FEAT_MENU
void
gui_mch_enable_menu(int showit)
{
if (gui.menubar != NULL)
gtk_widget_set_visible(gui.menubar, showit);
}
#endif
#ifdef FEAT_TOOLBAR
void
gui_mch_show_toolbar(int showit)
{
if (gui.toolbar != NULL)
{
gtk_widget_set_visible(gui.toolbar, showit);
if (showit)
vim_toolbar_set_style(VIM_TOOLBAR(gui.toolbar),
toolbar_flags, tbis_flags);
}
}
#endif
void
gui_mch_set_dark_theme(int dark)
{
// GTK4: use GtkSettings
GtkSettings *settings = gtk_settings_get_default();
if (settings != NULL)
g_object_set(settings, "gtk-application-prefer-dark-theme",
(gboolean)dark, NULL);
}
/*
* ============================================================