aboutsummaryrefslogtreecommitdiff
path: root/src/libui_sdl/libui/unix/menu.c
blob: d6aa398d8b6140c5ea415f1b8adaf21fefb2fd6d (plain)
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
// 23 april 2015
#include "uipriv_unix.h"

static GArray *menus = NULL;
static guint nmenus = 0;
static gboolean menusFinalized = FALSE;
static gboolean hasQuit = FALSE;
static gboolean hasPreferences = FALSE;
static gboolean hasAbout = FALSE;

struct uiMenu {
	char *name;
	GArray *items;					// []*uiMenuItem
	gboolean ischild;
	guint id;
};

struct uiMenuItem {
	char *name;
	int type;
	void (*onClicked)(uiMenuItem *, uiWindow *, void *);
	void *onClickedData;
	GType gtype;					// template for new instances; kept in sync with everything else
	gboolean disabled;
	gboolean checked;
	GHashTable *windows;			// map[GtkMenuItem]*menuItemWindow
	uiMenu *popupchild;
};

struct menuItemWindow {
	uiWindow *w;
	gulong signal;
};

enum {
	typeRegular,
	typeCheckbox,
	typeQuit,
	typePreferences,
	typeAbout,
	typeSeparator,
	typeSubmenu,
};

// we do NOT want programmatic updates to raise an ::activated signal
static void singleSetChecked(GtkCheckMenuItem *menuitem, gboolean checked, gulong signal)
{
	g_signal_handler_block(menuitem, signal);
	gtk_check_menu_item_set_active(menuitem, checked);
	g_signal_handler_unblock(menuitem, signal);
}

static void setChecked(uiMenuItem *item, gboolean checked)
{
	GHashTableIter iter;
	gpointer widget;
	gpointer ww;
	struct menuItemWindow *w;

	item->checked = checked;
	g_hash_table_iter_init(&iter, item->windows);
	while (g_hash_table_iter_next(&iter, &widget, &ww)) {
		w = (struct menuItemWindow *) ww;
		singleSetChecked(GTK_CHECK_MENU_ITEM(widget), item->checked, w->signal);
	}
}

static void onClicked(GtkMenuItem *menuitem, gpointer data)
{
	uiMenuItem *item = uiMenuItem(data);
	struct menuItemWindow *w;

	// we need to manually update the checked states of all menu items if one changes
	// notice that this is getting the checked state of the menu item that this signal is sent from
	if (item->type == typeCheckbox)
		setChecked(item, gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(menuitem)));

	w = (struct menuItemWindow *) g_hash_table_lookup(item->windows, menuitem);
	(*(item->onClicked))(item, w->w, item->onClickedData);
}

static void defaultOnClicked(uiMenuItem *item, uiWindow *w, void *data)
{
	// do nothing
}

static void onQuitClicked(uiMenuItem *item, uiWindow *w, void *data)
{
	if (shouldQuit())
		uiQuit();
}

static void menuItemEnableDisable(uiMenuItem *item, gboolean enabled)
{
	GHashTableIter iter;
	gpointer widget;

	item->disabled = !enabled;
	g_hash_table_iter_init(&iter, item->windows);
	while (g_hash_table_iter_next(&iter, &widget, NULL))
		gtk_widget_set_sensitive(GTK_WIDGET(widget), enabled);
}

void uiMenuItemEnable(uiMenuItem *item)
{
	menuItemEnableDisable(item, TRUE);
}

void uiMenuItemDisable(uiMenuItem *item)
{
	menuItemEnableDisable(item, FALSE);
}

void uiMenuItemOnClicked(uiMenuItem *item, void (*f)(uiMenuItem *, uiWindow *, void *), void *data)
{
	if (item->type == typeQuit)
		userbug("You cannot call uiMenuItemOnClicked() on a Quit item; use uiOnShouldQuit() instead.");
	item->onClicked = f;
	item->onClickedData = data;
}

int uiMenuItemChecked(uiMenuItem *item)
{
	return item->checked != FALSE;
}

void uiMenuItemSetChecked(uiMenuItem *item, int checked)
{
	gboolean c;

	// use explicit values
	c = FALSE;
	if (checked)
		c = TRUE;
	setChecked(item, c);
}

static uiMenuItem *newItem(uiMenu *m, int type, const char *name)
{
	uiMenuItem *item;

	if (menusFinalized)
		userbug("You cannot create a new menu item after menus have been finalized.");

	item = uiNew(uiMenuItem);

	g_array_append_val(m->items, item);

	item->type = type;
	switch (item->type) {
	case typeQuit:
		item->name = g_strdup("Quit");
		break;
	case typePreferences:
		item->name = g_strdup("Preferences...");
		break;
	case typeAbout:
		item->name = g_strdup("About");
		break;
	case typeSeparator:
		break;
	default:
		item->name = g_strdup(name);
		break;
	}

	if (item->type == typeQuit) {
		// can't call uiMenuItemOnClicked() here
		item->onClicked = onQuitClicked;
		item->onClickedData = NULL;
	} else
		uiMenuItemOnClicked(item, defaultOnClicked, NULL);

	switch (item->type) {
	case typeCheckbox:
		item->gtype = GTK_TYPE_CHECK_MENU_ITEM;
		break;
	case typeSeparator:
		item->gtype = GTK_TYPE_SEPARATOR_MENU_ITEM;
		break;
	default:
		item->gtype = GTK_TYPE_MENU_ITEM;
		break;
	}

	item->windows = g_hash_table_new(g_direct_hash, g_direct_equal);
	item->popupchild = NULL;

	return item;
}

uiMenuItem *uiMenuAppendSubmenu(uiMenu *m, uiMenu* child)
{
	uiMenuItem *item;

	if (menusFinalized)
		userbug("You cannot create a new menu item after menus have been finalized.");

	item = uiNew(uiMenuItem);

	g_array_append_val(m->items, item);

	item->type = typeSubmenu;
	item->name = child->name;

	uiMenuItemOnClicked(item, defaultOnClicked, NULL);

    // checkme
	item->gtype = GTK_TYPE_MENU_ITEM;

	item->windows = g_hash_table_new(g_direct_hash, g_direct_equal);
	item->popupchild = child;
	child->ischild = TRUE;

	return item;
}

uiMenuItem *uiMenuAppendItem(uiMenu *m, const char *name)
{
	return newItem(m, typeRegular, name);
}

uiMenuItem *uiMenuAppendCheckItem(uiMenu *m, const char *name)
{
	return newItem(m, typeCheckbox, name);
}

uiMenuItem *uiMenuAppendQuitItem(uiMenu *m)
{
	if (hasQuit)
		userbug("You cannot have multiple Quit menu items in the same program.");
	hasQuit = TRUE;
	newItem(m, typeSeparator, NULL);
	return newItem(m, typeQuit, NULL);
}

uiMenuItem *uiMenuAppendPreferencesItem(uiMenu *m)
{
	if (hasPreferences)
		userbug("You cannot have multiple Preferences menu items in the same program.");
	hasPreferences = TRUE;
	newItem(m, typeSeparator, NULL);
	return newItem(m, typePreferences, NULL);
}

uiMenuItem *uiMenuAppendAboutItem(uiMenu *m)
{
	if (hasAbout)
		userbug("You cannot have multiple About menu items in the same program.");
	hasAbout = TRUE;
	newItem(m, typeSeparator, NULL);
	return newItem(m, typeAbout, NULL);
}

void uiMenuAppendSeparator(uiMenu *m)
{
	newItem(m, typeSeparator, NULL);
}

uiMenu *uiNewMenu(const char *name)
{
	uiMenu *m;

	if (menusFinalized)
		userbug("You cannot create a new menu after menus have been finalized.");
	if (menus == NULL)
		menus = g_array_new(FALSE, TRUE, sizeof (uiMenu *));

	m = uiNew(uiMenu);

	g_array_append_val(menus, m);
	m->id = nmenus;
	nmenus++;

	m->name = g_strdup(name);
	m->items = g_array_new(FALSE, TRUE, sizeof (uiMenuItem *));
	m->ischild = FALSE;

	return m;
}

static void appendMenuItem(GtkMenuShell *submenu, uiMenuItem *item, uiWindow *w)
{
	GtkWidget *menuitem;
	gulong signal;
	struct menuItemWindow *ww;

	menuitem = g_object_new(item->gtype, NULL);
	if (item->name != NULL)
		gtk_menu_item_set_label(GTK_MENU_ITEM(menuitem), item->name);
	if (item->type != typeSeparator) {
		signal = g_signal_connect(menuitem, "activate", G_CALLBACK(onClicked), item);
		gtk_widget_set_sensitive(menuitem, !item->disabled);
		if (item->type == typeCheckbox)
			singleSetChecked(GTK_CHECK_MENU_ITEM(menuitem), item->checked, signal);
	}
	gtk_menu_shell_append(submenu, menuitem);
	
	ww = uiNew(struct menuItemWindow);
	ww->w = w;
	ww->signal = signal;
	g_hash_table_insert(item->windows, menuitem, ww);
	
	if (item->popupchild != NULL)
	{
	    int j;
	    uiMenu* m;
	    GtkWidget *submenu;
	    
	    m = item->popupchild;
		submenu = gtk_menu_new();
		gtk_menu_item_set_submenu(GTK_MENU_ITEM(menuitem), submenu);
		for (j = 0; j < m->items->len; j++)
			appendMenuItem(GTK_MENU_SHELL(submenu), g_array_index(m->items, uiMenuItem *, j), w);
	}
}

GtkWidget *makeMenubar(uiWindow *w)
{
	GtkWidget *menubar;
	guint i, j;
	uiMenu *m;
	GtkWidget *menuitem;
	GtkWidget *submenu;

	menusFinalized = TRUE;

	menubar = gtk_menu_bar_new();

	if (menus != NULL)
		for (i = 0; i < menus->len; i++) {
			m = g_array_index(menus, uiMenu *, i);
			if (m->ischild) continue;
			menuitem = gtk_menu_item_new_with_label(m->name);
			submenu = gtk_menu_new();
			gtk_menu_item_set_submenu(GTK_MENU_ITEM(menuitem), submenu);
			for (j = 0; j < m->items->len; j++)
				appendMenuItem(GTK_MENU_SHELL(submenu), g_array_index(m->items, uiMenuItem *, j), w);
			gtk_menu_shell_append(GTK_MENU_SHELL(menubar), menuitem);
		}

	gtk_widget_set_hexpand(menubar, TRUE);
	gtk_widget_set_halign(menubar, GTK_ALIGN_FILL);
	return menubar;
}

struct freeMenuItemData {
	GArray *items;
	guint i;
};

static void freeMenu(GtkWidget *widget, gpointer data);

static void freeMenuItem(GtkWidget *widget, gpointer data)
{
	struct freeMenuItemData *fmi = (struct freeMenuItemData *) data;
	uiMenuItem *item;
	struct menuItemWindow *w;

	item = g_array_index(fmi->items, uiMenuItem *, fmi->i);
	if (item->popupchild != NULL)
	    freeMenu(widget, &item->popupchild->id);
	w = (struct menuItemWindow *) g_hash_table_lookup(item->windows, widget);
	if (g_hash_table_remove(item->windows, widget) == FALSE)
		implbug("GtkMenuItem %p not in menu item's item/window map", widget);
	uiFree(w);
	fmi->i++;
}

static void freeMenu(GtkWidget *widget, gpointer data)
{
	guint *i = (guint *) data;
	uiMenu *m;
	GtkMenuItem *item;
	GtkWidget *submenu;
	struct freeMenuItemData fmi;

	m = g_array_index(menus, uiMenu *, *i);
	item = GTK_MENU_ITEM(widget);
	submenu = gtk_menu_item_get_submenu(item);
	fmi.items = m->items;
	fmi.i = 0;
	gtk_container_foreach(GTK_CONTAINER(submenu), freeMenuItem, &fmi);
	(*i)++;
}

void freeMenubar(GtkWidget *mb)
{
	guint i;

	i = 0;
	gtk_container_foreach(GTK_CONTAINER(mb), freeMenu, &i);
	// no need to worry about destroying any widgets; destruction of the window they're in will do it for us
}

void _freeMenu(uiMenu* m)
{
    uiMenuItem *item;
    guint j;

    g_free(m->name);
	for (j = 0; j < m->items->len; j++) {
		item = g_array_index(m->items, uiMenuItem *, j);
		if (item->popupchild != NULL) _freeMenu(item->popupchild);
		if (g_hash_table_size(item->windows) != 0)
			// TODO is this really a userbug()?
			implbug("menu item %p (%s) still has uiWindows attached; did you forget to destroy some windows?", item, item->name);
		if (item->type != typeSubmenu) g_free(item->name);
		g_hash_table_destroy(item->windows);
		uiFree(item);
	}
	g_array_free(m->items, TRUE);
	uiFree(m);
}

void uninitMenus(void)
{
	uiMenu *m;
	guint i;

	if (menus == NULL)
		return;
	for (i = 0; i < menus->len; i++) {
		m = g_array_index(menus, uiMenu *, i);
		if (m->ischild) continue;
		_freeMenu(m);
	}
	g_array_free(menus, TRUE);
}