-
Notifications
You must be signed in to change notification settings - Fork 15
/
menus.c
359 lines (285 loc) · 11.8 KB
/
menus.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
#include "zterm.h"
#include <alloca.h>
void
do_reload_config (GSimpleAction *self, GVariant *parameter, gpointer user_data)
{
int old_n_active = terms.n_active;
temu_parse_config();
if (!terms.n_active) {
errorf("Unable to read config file, or no terminals defined.");
return;
}
// We actively don't want to worry about the number shrinking. That just makes too much pain.
// But we absolutely have to handle it growing.
if (terms.n_active > old_n_active) {
debugf("old_n_active: %d, terms.n_active: %d", old_n_active, terms.n_active);
terms.active = realloc(terms.active, terms.n_active * sizeof(*terms.active));
memset(&terms.active[old_n_active], 0, (terms.n_active - old_n_active) * sizeof(*terms.active));
}
for (int i = 0; i < terms.n_active; i++) {
if (terms.active[i].term) {
term_config(terms.active[i].term, terms.active[i].window);
//vte_terminal_set_geometry_hints_for_window(VTE_TERMINAL (terms.active[i].term), GTK_WINDOW (windows[terms.active[i].window].window));
}
}
rebuild_menus();
}
void
do_copy (GSimpleAction *self, GVariant *parameter, gpointer data)
{
long int i = (long int) data;
debugf("parameter: %p, user_data: %p", parameter, data);
GtkWidget *widget = gtk_notebook_get_nth_page(windows[i].notebook, gtk_notebook_get_current_page(windows[i].notebook));
vte_terminal_copy_clipboard_format (VTE_TERMINAL(widget), VTE_FORMAT_TEXT);
}
void
do_copy_uri (GSimpleAction *self, GVariant *parameter, gpointer data)
{
long int window_i = (long int) data;
int n;
debugf("parameter: %p, user_data: %p", parameter, data);
window_t *window = &windows[window_i];
GtkWidget *widget = gtk_notebook_get_nth_page(window->notebook, gtk_notebook_get_current_page(window->notebook));
if (term_find(widget, &n)) {
process_uri(n, window, BIND_ACT_CUT_URI, window->menu_x, window->menu_y, true);
}
}
void
do_open_uri (GSimpleAction *self, GVariant *parameter, gpointer data)
{
long int window_i = (long int) data;
int n;
debugf("parameter: %p, user_data: %p", parameter, data);
window_t *window = &windows[window_i];
GtkWidget *widget = gtk_notebook_get_nth_page(window->notebook, gtk_notebook_get_current_page(window->notebook));
if (term_find(widget, &n)) {
process_uri(n, window, BIND_ACT_OPEN_URI, window->menu_x, window->menu_y, true);
}
}
void
do_paste (GSimpleAction *self, GVariant *parameter, gpointer data)
{
long int i = (long int) data;
debugf("parameter: %p, user_data: %p", parameter, data);
GtkWidget *widget = gtk_notebook_get_nth_page(windows[i].notebook, gtk_notebook_get_current_page(windows[i].notebook));
vte_terminal_paste_clipboard (VTE_TERMINAL(widget));
}
void
do_t_decorate (GSimpleAction *self, GVariant *parameter, gpointer data)
{
long int i = (long int) data;
gboolean decorated = gtk_window_get_decorated (GTK_WINDOW(windows[i].window));
debugf("setting decorated: %d", !decorated);
gtk_window_set_decorated(GTK_WINDOW(windows[i].window), !decorated);
}
void
do_t_fullscreen (GSimpleAction *self, GVariant *parameter, gpointer data)
{
long int i = (long int) data;
debugf("");
if (gtk_window_is_fullscreen(GTK_WINDOW(windows[i].window))) {
gtk_window_unfullscreen (GTK_WINDOW (windows[i].window));
} else {
gtk_window_fullscreen (GTK_WINDOW (windows[i].window));
}
}
void
do_t_tabbar (GSimpleAction *self, GVariant *parameter, gpointer data)
{
long int i = (long int) data;
debugf("");
gboolean show_tabs = gtk_notebook_get_show_tabs (GTK_NOTEBOOK(windows[i].notebook));
gtk_notebook_set_show_tabs(GTK_NOTEBOOK(windows[i].notebook), !show_tabs);
}
void
do_show_terms (GSimpleAction *self, GVariant *parameter, gpointer data)
{
long int window_i = (long int) data;
gboolean ret;
ret = gtk_widget_activate_action_variant(GTK_WIDGET(windows[window_i].notebook), "menu.popup", NULL);
debugf("gtk_widget_activate_action_variant: %d", ret);
}
void
do_next_term (GSimpleAction *self, GVariant *parameter, gpointer data)
{
long int i = (long int) data;
debugf("");
gtk_notebook_next_page(GTK_NOTEBOOK(windows[i].notebook));
}
void
do_prev_term (GSimpleAction *self, GVariant *parameter, gpointer data)
{
long int i = (long int) data;
debugf("");
gtk_notebook_prev_page(GTK_NOTEBOOK(windows[i].notebook));
}
void
do_move_to_window (GSimpleAction *self, GVariant *parameter, gpointer data)
{
long int window_i = ((long int) data) & ((1<<8)-1);
long int new_window_i = (long int) data >> 8;
int i;
GtkWidget *widget;
debugf("window_i: %d, new_window_i: %d, data: 0x%x", window_i, new_window_i, data);
widget = gtk_notebook_get_nth_page(windows[window_i].notebook, gtk_notebook_get_current_page(windows[window_i].notebook));
if (term_find(widget, &i)) {
term_set_window (i, new_window_i);
term_switch (i, NULL, NULL, NULL, window_i);
}
}
void
do_set_window_color_scheme (GSimpleAction *self, GVariant *parameter, gpointer data)
{
long int color_scheme = ((long int) data) >> 8;
long int window_i = ((long int) data) & ((1 << 8) - 1);
int i;
debugf("");
windows[window_i].color_scheme = color_scheme;
for (i = 0; i < terms.n_active; i++) {
if (terms.active[i].term && terms.active[i].window == window_i) {
vte_terminal_set_colors (VTE_TERMINAL (terms.active[i].term), &terms.color_schemes[windows[window_i].color_scheme].foreground, &terms.color_schemes[windows[window_i].color_scheme].background, &colors[0], MIN(256, sizeof (colors) / sizeof(colors[0])));
}
}
}
/*
* Ideally, we would free menu_hyperlink_uri here.
*
* Sadly, if someone opens the menu, and then selects something like 'Copy
* URI', we get the closed signal before we get told that a menu item was
* selected.
*
* As such, this is far less useful at the moment.
*/
static void
menu_closed(GtkPopover *menu, gpointer user_data)
{
long int i = (long int) user_data;
debugf("window %d", i);
}
// This is really strdupa.
// It really must be a macro, not a function, so that alloca gets called with
// the caller function's stack.
// And we're using the gcc statement expression syntax, because nothing else
// gives us the ability to have temporary variables, return values, and the
// ability to have a return value.
#define dupstr(str) ({ \
char *in = str; \
char *new = alloca(strlen(in) + 1); \
char *ret = strcpy(new, in); \
ret; \
})
typedef struct {
GActionEntry entry;
gpointer user_data;
} ZActionEntry;
static void
z_menu_append(GMenu *menu, ZActionEntry *actions, int *n_actions, char *prefix, char *label, char *_name, void (*function)(GSimpleAction *, GVariant *, gpointer), long int _user_data)
{
char buf[70] = { 0 };
snprintf(buf, sizeof(buf) - 1, "%s%s", prefix, _name);
g_menu_append(menu, label, buf);
debugf("label: %s, action: %s, name: %s", label, buf, _name);
ZActionEntry tmp = { .entry = { _name, function }, .user_data = (gpointer) _user_data };
actions[(*n_actions)++] = tmp;
debugf("action: %d, name: %s", *n_actions - 1, actions[*n_actions - 1].entry.name);
}
static void
rebuild_window_menu(long int window_n)
{
ZActionEntry add_actions[64];
int n_add_actions = 0;
debugf("windows[%d].menu: %p", window_n, windows[window_n].menu);
if (windows[window_n].menu != NULL) {
gtk_widget_unparent(GTK_WIDGET(windows[window_n].menu));
windows[window_n].menu = NULL;
}
GMenu *main = g_menu_new();
GMenu *actions = g_menu_new();
z_menu_append(actions, add_actions, &n_add_actions, "menu.", "_Copy", "copy", do_copy, window_n);
z_menu_append(actions, add_actions, &n_add_actions, "menu.", "_Paste", "paste", do_paste, window_n);
z_menu_append(actions, add_actions, &n_add_actions, "menu.", "Copy _URI", "copy_uri", do_copy_uri, window_n);
z_menu_append(actions, add_actions, &n_add_actions, "menu.", "_Open URI", "open_uri", do_open_uri, window_n);
g_menu_append_section(main, "Actions", G_MENU_MODEL(actions));
GMenu *terminals = g_menu_new();
z_menu_append(terminals, add_actions, &n_add_actions, "menu.", "_Show Terminals", "show_terminals", do_show_terms, window_n);
z_menu_append(terminals, add_actions, &n_add_actions, "menu.", "_Previous Terminal", "prev_terminal", do_prev_term, window_n);
z_menu_append(terminals, add_actions, &n_add_actions, "menu.", "_Next Terminal", "next_terminal", do_next_term, window_n);
g_menu_append_section(main, "Terminals", G_MENU_MODEL(terminals));
GMenu *config = g_menu_new();
z_menu_append(config, add_actions, &n_add_actions, "menu.", "_Decorations", "decorations", do_t_decorate, window_n);
z_menu_append(config, add_actions, &n_add_actions, "menu.", "_Fullscreen", "fullscreen", do_t_fullscreen, window_n);
z_menu_append(config, add_actions, &n_add_actions, "menu.", "_Tab bar", "tab_bar", do_t_tabbar, window_n);
z_menu_append(config, add_actions, &n_add_actions, "menu.", "_Reload config file", "reload_config", do_reload_config, window_n);
g_menu_append_section(main, "Config", G_MENU_MODEL(config));
/*
z_menu_append(config, add_actions, &n_add_actions, "menu.", "_", "", do_, window_n);
z_menu_append(config, add_actions, &n_add_actions, "menu.", "_", "", do_, window_n);
z_menu_append(config, add_actions, &n_add_actions, "menu.", "_", "", do_, window_n);
*/
GMenu *schemes = g_menu_new();
for (long int j = 0; j < MAX_COLOR_SCHEMES && terms.color_schemes[j].name[0]; j++) {
debugf("name: %s, action: %s", terms.color_schemes[j].name, terms.color_schemes[j].action);
z_menu_append(schemes, add_actions, &n_add_actions, "menu.", terms.color_schemes[j].name, terms.color_schemes[j].action, do_set_window_color_scheme, ((j << 8) + window_n));
}
g_menu_append_section(main, "Color schemes", G_MENU_MODEL(schemes));
GMenu *window = g_menu_new();
long first_empty = -1;
for (long n = 0; n < MAX_WINDOWS; n++) {
if (n == window_n) { // Don't offer to move to the same window, that's just weird.
continue;
} else if (windows[n].window) {
char action[64] = { 0 };
char title[64] = { 0 };
snprintf(title, sizeof (title), "Move to window _%ld", n);
snprintf(action, sizeof(action), "window.move_%ld", n);
char *_title = dupstr(title);
char *_action = dupstr(action);
z_menu_append(window, add_actions, &n_add_actions, "menu.", _title, _action, do_move_to_window, ((n << 8) + window_n));
} else if (first_empty == -1) {
first_empty = n;
}
}
if (first_empty != -1) {
char action[64] = { 0 };
char title[64] = { 0 };
snprintf(title, sizeof (title), "Move to _new window %ld", first_empty);
snprintf(action, sizeof(action), "window.move_%ld", first_empty);
char *_title = dupstr(title);
char *_action = dupstr(action);
z_menu_append(window, add_actions, &n_add_actions, "menu.", _title, _action, do_move_to_window, ((first_empty << 8) + window_n));
} else {
debugf("first_empty: %d", first_empty);
for (long n = 0; n < MAX_WINDOWS; n++) {
debugf("windows[%d].window: %p", n, windows[n].window);
}
}
g_menu_append_section(main, "Window", G_MENU_MODEL(window));
windows[window_n].menu_model = G_MENU_MODEL(main);
GtkWidget *menu = gtk_popover_menu_new_from_model(G_MENU_MODEL(main));
gtk_popover_set_autohide (GTK_POPOVER(menu), TRUE);
gtk_popover_set_has_arrow (GTK_POPOVER(menu), FALSE);
gtk_popover_set_position(GTK_POPOVER(menu), GTK_POS_BOTTOM);
gtk_widget_set_halign(menu, GTK_ALIGN_START);
gtk_widget_set_valign(menu, GTK_ALIGN_END);
GSimpleActionGroup *group = g_simple_action_group_new();
for (int i = 0; i < n_add_actions; i++) {
g_action_map_add_action_entries (G_ACTION_MAP(group), &add_actions[i].entry, 1, add_actions[i].user_data);
debugf("action: %d, name: %s", i, add_actions[i].entry.name);
}
gtk_widget_insert_action_group(windows[window_n].window, "menu", G_ACTION_GROUP(group));
windows[window_n].menu = menu;
gtk_widget_set_parent(menu, windows[window_n].window);
debugf("windows[%d].menu: %p", window_n, windows[window_n].menu);
g_signal_connect_after(menu, "closed", G_CALLBACK(menu_closed), (void *) window_n);
return;
}
void
rebuild_menus(void)
{
for (int i = 0; i < MAX_WINDOWS; i++) {
if (windows[i].window) {
rebuild_window_menu(i);
}
}
}
// vim: set ts=4 sw=4 noexpandtab :