File: Menu.cpp

package info (click to toggle)
storm-lang 0.7.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 51,984 kB
  • sloc: ansic: 261,420; cpp: 140,270; sh: 14,877; perl: 9,846; python: 2,525; lisp: 2,504; asm: 860; makefile: 678; pascal: 70; java: 52; xml: 37; awk: 12
file content (545 lines) | stat: -rw-r--r-- 13,581 bytes parent folder | download | duplicates (3)
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
#include "stdafx.h"
#include "Menu.h"
#include "Exception.h"
#include "App.h"
#include "Frame.h"
#include "Accelerators.h"

namespace gui {

	Menu::Menu() : parent(null), items(new (engine()) Array<Item *>()) {}

	Menu::~Menu() {
		if (items) {
			// Note: This is slightly dangerous during shutdown...
			for (Nat i = 0; i < items->count(); i++) {
				items->at(i)->destroy();
			}
		}

#ifdef GUI_WIN32
		if (handle != Handle())
			DestroyMenu(handle.menu());
#endif
#ifdef GUI_GTK
		if (handle != Handle())
			g_object_unref(handle.widget());
#endif
		handle = Handle();
	}

	void Menu::push(Item *item) {
		Nat id = items->count();
		items->push(item);

		item->attached(this, id);

		repaint();
	}

	Menu &Menu::operator <<(Item *item) {
		push(item);
		return *this;
	}

	void Menu::repaint() {
		// Don't need to do anything here.
	}

	void Menu::onAddAccelerator(KeyChord, Fn<void> *, Handle) {}

	void Menu::onRemoveAccelerator(KeyChord) {}

	Menu *Menu::findMenu(Handle handle) {
		if (handle == this->handle)
			return this;

		for (Nat i = 0; i < items->count(); i++) {
			if (Menu *m = items->at(i)->findMenu(handle))
				return m;
		}

		return null;
	}

	Menu::Item *Menu::findMenuItem(Handle handle) {
		for (Nat i = 0; i < items->count(); i++) {
			if (Item *item = items->at(i)->findMenuItem(handle))
				return item;
		}
		return null;
	}

	void Menu::addAccelerators(Accelerators *to) {
		for (Nat i = 0; i < items->count(); i++)
			items->at(i)->addAccelerators(to);
	}

	void Menu::removeAccelerators(Accelerators *to) {
		for (Nat i = 0; i < items->count(); i++)
			items->at(i)->addAccelerators(to);
	}


	Menu::Item::Item() : owner(null), enable(true) {}

	void Menu::Item::attached(Menu *to, Nat id) {
		if (owner)
			throw new (this) GuiError(S("This menu item is already attached to another menu!"));

		this->owner = to;
		this->id = id;

		create();
	}

	void Menu::Item::destroy() {
		// No cleanup is needed on Win32.

#ifdef GUI_GTK
		if (handle != Handle())
			g_object_unref(handle.widget());
		handle = Handle();
#endif
	}

	void Menu::Item::clicked() {}

	void Menu::Item::onShortcut() {
		if (enable)
			clicked();
	}

	Menu *Menu::Item::findMenu(Handle handle) const {
		return null;
	}

	Menu::Item *Menu::Item::findMenuItem(Handle handle) {
		if (this->handle == handle)
			return this;
		return null;
	}

	void Menu::Item::addAccelerators(Accelerators *) {}

	void Menu::Item::removeAccelerators(Accelerators *) {}

	Menu::Separator::Separator() {}

	Menu::WithTitle::WithTitle(MnemonicStr title) : myTitle(title) {}

	Menu::WithTitle::WithTitle(MnemonicStr title, KeyChord shortcut) : myTitle(title), myShortcut(shortcut) {}

	Str *Menu::WithTitle::findTitle() {
		Str *t = myTitle.win32Mnemonic();
		if (myShortcut.any())
			t = TO_S(t, t << S("\t") << myShortcut);
		return t;
	}

	void Menu::WithTitle::shortcut(KeyChord s) {
		if (owner) {
			if (myShortcut.any())
				owner->onRemoveAccelerator(myShortcut);

			if (s.any())
				owner->onAddAccelerator(s, fnPtr<void, Menu::Item>(engine(), &Menu::Item::onShortcut, this), handle);
		}

		myShortcut = s;

#if defined(GUI_WIN32)
		title(myTitle);
#endif
	}

	void Menu::WithTitle::setupShortcut() {
		if (owner && myShortcut.any())
			owner->onAddAccelerator(myShortcut, fnPtr<void, Menu::Item>(engine(), &Menu::Item::onShortcut, this), handle);
	}

	void Menu::WithTitle::addAccelerators(Accelerators *to) {
		if (myShortcut.any())
			to->add(myShortcut, fnPtr<void, Menu::Item>(engine(), &Menu::Item::onShortcut, this), handle);
	}

	void Menu::WithTitle::removeAccelerators(Accelerators *to) {
		if (myShortcut.any())
			to->remove(myShortcut);
	}

	Menu::Text::Text(MnemonicStr title) : WithTitle(title) {}

	Menu::Text::Text(MnemonicStr title, Fn<void> *callback) : WithTitle(title), onClick(callback) {}

	Menu::Text::Text(MnemonicStr title, KeyChord s) : WithTitle(title, s) {}

	Menu::Text::Text(MnemonicStr title, KeyChord s, Fn<void> *callback) : WithTitle(title, s), onClick(callback) {}

	void Menu::Text::clicked() {
		if (onClick)
			onClick->call();
	}

	Menu::Check::Check(MnemonicStr title) : WithTitle(title) {}

	Menu::Check::Check(MnemonicStr title, Fn<void, Bool> *callback) : WithTitle(title), onClick(callback) {}

	Menu::Check::Check(MnemonicStr title, Fn<void, Bool> *callback, Bool checked)
		: WithTitle(title), onClick(callback), myChecked(checked) {}

	Menu::Check::Check(MnemonicStr title, KeyChord s) : WithTitle(title, s) {}

	Menu::Check::Check(MnemonicStr title, KeyChord s, Fn<void, Bool> *callback)
		: WithTitle(title, s), onClick(callback) {}

	Menu::Check::Check(MnemonicStr title, KeyChord s, Fn<void, Bool> *callback, Bool checked)
		: WithTitle(title, s), onClick(callback), myChecked(checked) {}

	Menu::Submenu::Submenu(MnemonicStr title, PopupMenu *menu) : WithTitle(title), myMenu(menu) {
		if (menu->inside)
			throw new (this) GuiError(S("A popup menu may only be attached at a single place at a time."));
		menu->inside = this;
	}

	Menu *Menu::Submenu::findMenu(Handle handle) const {
		if (myMenu)
			return myMenu->findMenu(handle);
		return null;
	}

	Menu::Item *Menu::Submenu::findMenuItem(Handle handle) {
		if (Item *i = WithTitle::findMenuItem(handle))
			return i;
		if (myMenu)
			return myMenu->findMenuItem(handle);
		return null;
	}

	void Menu::Submenu::addAccelerators(Accelerators *to) {
		WithTitle::addAccelerators(to);
		if (myMenu)
			myMenu->addAccelerators(to);
	}

	void Menu::Submenu::removeAccelerators(Accelerators *to) {
		WithTitle::removeAccelerators(to);
		if (myMenu)
			myMenu->removeAccelerators(to);
	}

#ifdef GUI_WIN32

	void Menu::Item::enabled(Bool v) {
		enable = v;

		if (owner) {
			MENUITEMINFO info;
			info.cbSize = sizeof(info);
			info.fMask = MIIM_STATE;
			GetMenuItemInfo(owner->handle.menu(), id, TRUE, &info);
			if (enable)
				info.fState &= ~MF_DISABLED;
			else
				info.fState |= MF_DISABLED;
			SetMenuItemInfo(owner->handle.menu(), id, TRUE, &info);

			owner->repaint();
		}
	}

	void Menu::Separator::create() {
		AppendMenu(owner->handle.menu(), MF_SEPARATOR, 1, L"");
	}

	void Menu::WithTitle::title(MnemonicStr title) {
		myTitle = title;

		if (owner) {
			MENUITEMINFO info;
			info.cbSize = sizeof(info);
			info.fMask = MIIM_STRING;
			info.dwTypeData = (LPWSTR)findTitle()->c_str();
			SetMenuItemInfo(owner->handle.menu(), id, TRUE, &info);

			owner->repaint();
		}
	}

	void Menu::Text::create() {
		UINT flags = MF_STRING;
		if (!enable)
			flags |= MF_DISABLED;
		AppendMenu(owner->handle.menu(), flags, 1, findTitle()->c_str());
	}

	Bool Menu::Check::checked() {
		if (owner) {
			MENUITEMINFO info;
			info.cbSize = sizeof(info);
			info.fMask = MIIM_STATE;
			GetMenuItemInfo(owner->handle.menu(), id, TRUE, &info);

			myChecked = (info.fState & MF_CHECKED) != 0;
		}

		return myChecked;
	}

	void Menu::Check::checked(Bool v) {
		Bool changed = v != myChecked;
		myChecked = v;

		if (owner) {
			MENUITEMINFO info;
			info.cbSize = sizeof(info);
			info.fMask = MIIM_STATE;
			GetMenuItemInfo(owner->handle.menu(), id, TRUE, &info);
			UINT oldState = info.fState;
			if (v)
				info.fState |= MF_CHECKED;
			else
				info.fState &= ~MF_CHECKED;
			SetMenuItemInfo(owner->handle.menu(), id, TRUE, &info);

			if (info.fState != oldState) {
				owner->repaint();

				// We only get notifications when the checkbox is clicked, so we need to send a
				// notification ourselves now.
				if (onClick)
					onClick->call(v);
			}
		} else {
			if (changed && onClick)
				onClick->call(v);
		}
	}

	void Menu::Check::clicked() {
		Bool check = !checked();
		checked(check);
	}

	void Menu::Check::create() {
		UINT flags = MF_STRING;
		if (!enable)
			flags |= MF_DISABLED;
		if (myChecked)
			flags |= MF_CHECKED;
		AppendMenu(owner->handle.menu(), flags, 1, findTitle()->c_str());
	}

	void Menu::Submenu::create() {
		if (myMenu->parent)
			throw new (this) GuiError(S("This sub-menu is already used somewhere else!"));
		myMenu->parent = owner;

		UINT flags = MF_STRING | MF_POPUP;
		if (!enable)
			flags |= MF_DISABLED;
		AppendMenu(owner->handle.menu(), flags, (UINT_PTR)myMenu->handle.menu(), findTitle()->c_str());
	}

	void Menu::Submenu::destroy() {
		// Remove the submenu before we're destroyed to avoid us recursively destroying the menu.
		RemoveMenu(owner->handle.menu(), id, MF_BYPOSITION);

		WithTitle::destroy();
	}

	static void setupMenu(HMENU menu) {
		MENUINFO info;
		info.cbSize = sizeof(info);
		info.fMask = MIM_STYLE;
		// Note: We don't need to set MNS_NOTIFYBYPOS for sub-menus, but I don't think it hurts.
		// Note: Since there is a MNS_MODELESS flag, it appears that the message loop is blocked while
		// a menu is being interacted with. We might want to circumvent that...
		info.dwStyle = MNS_NOTIFYBYPOS;
		SetMenuInfo(menu, &info);
	}

	PopupMenu::PopupMenu() : inside(null) {
		handle = CreatePopupMenu();
		setupMenu(handle.menu());
	}

	MenuBar::MenuBar() {
		handle = CreateMenu();
		setupMenu(handle.menu());
	}

	void MenuBar::repaint() {
		if (attachedTo) {
			if (attachedTo->created()) {
				DrawMenuBar(attachedTo->handle().hwnd());
			}
		}
	}

#endif
#ifdef GUI_GTK

	static void menuCallback(GtkWidget *src, gpointer engine) {
		Engine *e = (Engine *)engine;
		App *app = gui::app(*e);

		Menu::Item *item = app->findMenuItem(src);
		if (!item) {
			// Note: This happens for example when a Menu::Check is toggled before it is shown
			// and/or properly added to a window.
			// WARNING(L"Unknown menu item!");
			return;
		}

		try {
			return item->clicked();
		} catch (const storm::Exception *e) {
			PLN(L"Unhandled exception in window thread:\n" << e->message());
		} catch (const ::Exception &e) {
			PLN(L"Unhandled exception in window thread:\n" << e);
		} catch (...) {
			PLN(L"Unhandled exception in window thread: <unknown>");
		}
	}

		// Connect to a signal associated with a Menu.
	static void connectMenu(GtkWidget *to, Engine &e) {
		g_signal_connect(to, "activate", (GCallback)&menuCallback, &e);
	}

	void Menu::Item::enabled(Bool v) {
		enable = v;

		if (handle != Handle()) {
			gtk_widget_set_sensitive(handle.widget(), v ? TRUE : FALSE);
		}
	}

	void Menu::Separator::create() {
		GtkWidget *created = gtk_separator_menu_item_new();
		g_object_ref_sink(created);
		handle = created;
		gtk_menu_shell_append(GTK_MENU_SHELL(owner->handle.widget()), created);
	}

	void Menu::WithTitle::title(MnemonicStr title) {
		myTitle = title;

		if (handle != Handle())
			gtk_menu_item_set_label(GTK_MENU_ITEM(handle.widget()), title.mnemonic()->utf8_str());
	}

	void Menu::Text::create() {
		GtkWidget *created = gtk_menu_item_new_with_mnemonic(myTitle.mnemonic()->utf8_str());
		g_object_ref_sink(created);
		gtk_widget_set_sensitive(created, enable ? TRUE : FALSE);
		handle = created;
		gtk_menu_shell_append(GTK_MENU_SHELL(owner->handle.widget()), created);
		connectMenu(created, engine());
	}

	Bool Menu::Check::checked() {
		if (handle != Handle()) {
			myChecked = gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(handle.widget()));
		}

		return myChecked;
	}

	void Menu::Check::checked(Bool v) {
		Bool changed = v != myChecked;
		myChecked = v;

		if (handle != Handle()) {
			gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(handle.widget()), v ? TRUE : FALSE);
		} else {
			// If it is not created, we need to send the notification ourselves.
			if (changed && onClick)
				onClick->call(v);
		}
	}

	void Menu::Check::clicked() {
		Bool check = checked();
		// checked(check);

		if (onClick)
			onClick->call(check);
	}

	void Menu::Check::create() {
		GtkWidget *created = gtk_check_menu_item_new_with_mnemonic(myTitle.mnemonic()->utf8_str());
		g_object_ref_sink(created);
		gtk_widget_set_sensitive(created, enable ? TRUE : FALSE);
		gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(created), myChecked ? TRUE : FALSE);
		handle = created;
		gtk_menu_shell_append(GTK_MENU_SHELL(owner->handle.widget()), created);
		connectMenu(created, engine());
	}

	void Menu::Submenu::create() {
		GtkWidget *created = gtk_menu_item_new_with_mnemonic(myTitle.mnemonic()->utf8_str());
		g_object_ref_sink(created);
		gtk_widget_set_sensitive(created, enable ? TRUE : FALSE);
		handle = created;
		gtk_menu_shell_append(GTK_MENU_SHELL(owner->handle.widget()), created);
		connectMenu(created, engine());

		if (myMenu->parent)
			throw new (this) GuiError(S("This sub-menu is already used somewhere else!"));
		myMenu->parent = owner;
		gtk_menu_item_set_submenu(GTK_MENU_ITEM(handle.widget()), myMenu->handle.widget());
	}

	void Menu::Submenu::destroy() {
		WithTitle::destroy();
	}


	PopupMenu::PopupMenu() : inside(null) {
		GtkWidget *created = gtk_menu_new();
		g_object_ref_sink(created);
		handle = created;
	}

	MenuBar::MenuBar() {
		GtkWidget *created = gtk_menu_bar_new();
		g_object_ref_sink(created);
		handle = created;
	}

	void MenuBar::repaint() {
		if (attachedTo) {
			gtk_widget_show_all(handle.widget());
		}
	}

#endif

	void PopupMenu::onAddAccelerator(KeyChord chord, Fn<void> *action, Handle handle) {
		if (inside && inside->owner) {
			inside->owner->onAddAccelerator(chord, action, handle);
		}
	}

	void PopupMenu::onRemoveAccelerator(KeyChord chord) {
		if (inside && inside->owner) {
			inside->owner->onRemoveAccelerator(chord);
		}
	}

	void MenuBar::onAddAccelerator(KeyChord chord, Fn<void> *action, Handle handle) {
		if (attachedTo) {
			attachedTo->accelerators()->add(chord, action, handle);
		}
	}

	void MenuBar::onRemoveAccelerator(KeyChord chord) {
		if (attachedTo) {
			attachedTo->accelerators()->remove(chord);
		}
	}


}