File: Frame.cpp

package info (click to toggle)
storm-lang 0.7.5-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 52,028 kB
  • sloc: ansic: 261,471; cpp: 140,432; sh: 14,891; perl: 9,846; python: 2,525; lisp: 2,504; asm: 860; makefile: 678; pascal: 70; java: 52; xml: 37; awk: 12
file content (752 lines) | stat: -rw-r--r-- 19,785 bytes parent folder | download
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
#include "stdafx.h"
#include "Frame.h"
#include "App.h"
#include "Win32Dpi.h"
#include "GtkSignal.h"
#include "Exception.h"

#ifdef DEBUG
// #define WIN32_TESTDPI
#endif

namespace gui {

	static void goFull(Handle wnd, Nat &oldStyle, Rect &oldPos);
	static void goBack(Handle wnd, Nat &oldStyle, Rect &oldPos);


	Frame::Frame(Str *title) : myMenu(null), flags(showCursor) {
		dpi = defaultDpi;
		onClose = new (this) Event();
		myAccelerators = new (this) Accelerators();
		attachParent(this);
		text(title);
		pos(Rect(-10000, -10000, -10002, -10002));
		clearFlag(positionAltered);
	}

	Frame::Frame(Str *title, Size size) : myMenu(null), flags(showCursor) {
		dpi = defaultDpi;
		onClose = new (this) Event();
		myAccelerators = new (this) Accelerators();
		attachParent(this);
		text(title);
		pos(Rect(Point(-10000, -10000), size));
		clearFlag(positionAltered);
	}

	void Frame::create() {
		if (handle() != invalid)
			return;

		createWindow(true, null);
	}

	void Frame::close() {
		if (handle() != invalid) {
			handle(invalid);
		}

		onClose->set();
	}

	void Frame::waitForClose() {
		App *app = gui::app(engine());
		app->waitForEvent(this, onClose);
	}

	void Frame::size(Size s) {
		if (!hasFlag(isFullscreen)) {
			Bool oldSet = hasFlag(positionAltered);
			Rect p = pos();
			p.size(s);
			pos(p);
			setFlag(positionAltered, oldSet);
		}
	}

	Bool Frame::cursorVisible() {
		return hasFlag(showCursor);
	}

	void Frame::fullscreen(Bool v) {
		if (created() && hasFlag(isFullscreen) != v) {
			if (v) {
				goFull(handle(), windowedStyle, windowedRect);
			} else {
				goBack(handle(), windowedStyle, windowedRect);
			}
		}

		setFlag(isFullscreen, v);
	}

	Bool Frame::fullscreen() {
		return hasFlag(isFullscreen);
	}

	void Frame::menu(MenuBar *menu) {
		if (menu && menu->attachedTo && menu != myMenu)
			throw new (this) GuiError(S("A menu can only be attached to one frame at a time."));

		MenuBar *old = myMenu;
		if (old) {
			old->removeAccelerators(myAccelerators);
			old->attachedTo = null;
		}

		myMenu = menu;
		if (menu) {
			menu->attachedTo = this;
			menu->addAccelerators(myAccelerators);
		}

		if (created()) {
			setMenu(old);
		}
	}

	MenuBar *Frame::menu() {
		return myMenu;
	}

	Menu::Item *Frame::findMenuItem(Handle h) {
		if (myMenu) {
			if (Menu::Item *found = myMenu->findMenuItem(h)) {
				return found;
			}
		}

		if (myPopup) {
			if (Menu::Item *found = myPopup->findMenuItem(h)) {
				// This will not cover the case when a menu is closed without any selection, but it
				// seems it is the best we can do.
				myPopup = null;
				return found;
			}
		}

		return null;
	}

	void Frame::focus(Window *to) {
		setFocus = to;
	}

	Bool Frame::inhibitScreenSaver() {
		return hasFlag(noScreenSaver);
	}

	void Frame::inhibitScreenSaver(Bool inhibit) {
		setFlag(noScreenSaver, inhibit);

		if (created() && !hasFlag(isMinimized)) {
			app(engine())->inhibitScreenSaver(this, inhibit);
		}
	}

	MAYBE(Icon *) Frame::icon() {
		return myIcon;
	}

	void Frame::icon(ImageSet *images) {
		icon(new (this) Icon(images));
	}

	void Frame::updateDefaultIcon() {
		if (!myIcon)
			icon((Icon *)null);
	}

	Bool Frame::minimized() {
		return hasFlag(isMinimized);
	}

	void Frame::onResize(Size size) {
		updateMinSize();
		Window::onResize(size);
	}

	void Frame::destroyWindow(Handle handle) {
		// Remove the menu first, so we don't destroy that as well (only an issue on Windows).
		MenuBar *old = myMenu;
		myMenu = null;
		setMenu(old);
		// Restore, so we don't alter the visible state.
		myMenu = old;

		Window::destroyWindow(handle);
	}


#ifdef GUI_WIN32

	Rect Frame::pos() {
		if (created()) {
			RECT r;
			HWND h = handle().hwnd();
			GetClientRect(h, &r);
			POINT a = { r.left, r.top };
			ClientToScreen(h, &a);
			POINT b = { r.right, r. bottom };
			ClientToScreen(h, &b);

			if (parent() != this) {
				ScreenToClient(parent()->handle().hwnd(), &a);
				ScreenToClient(parent()->handle().hwnd(), &b);
			}

			myPos = dpiFromPx(currentDpi(), convert(a, b));
		}
		return myPos;
	}

	void Frame::pos(Rect r) {
		if (hasFlag(isFullscreen))
			return;

		setFlag(positionAltered);

		// We need to do this to take the menu into account.
		myPos = r;
		if (created()) {
			RECT z = convert(r);
			HWND h = handle().hwnd();
			DWORD style = GetWindowLong(h, GWL_STYLE);
			DWORD exStyle = GetWindowLong(h, GWL_EXSTYLE);
			dpiAdjustWindowRectEx(&z, style, myMenu ? TRUE : FALSE, exStyle, dpi);
			// Todo: keep track if we need to repaint.
			MoveWindow(h, z.left, z.top, z.right - z.left, z.bottom - z.top, TRUE);
		}
	}

	// Convert a client size to the actual window size.
	static Size clientToWindow(Size s, DWORD windowStyles, DWORD windowExStyles, bool menu, Nat dpi) {
		RECT r = { 0, 0, (LONG)s.w, (LONG)s.h };
		BOOL m = menu ? TRUE : FALSE;
		dpiAdjustWindowRectEx(&r, windowStyles & ~WS_OVERLAPPED, menu, windowExStyles, dpi);
		return Size(Float(r.right - r.left), Float(r.bottom - r.top));
	}

	// Make 'wnd' go fullscreen.
	static void goFull(Handle wnd, Nat &oldStyle, Rect &oldPos) {
		oldStyle = GetWindowLong(wnd.hwnd(), GWL_STYLE);
		RECT oldRect;
		GetWindowRect(wnd.hwnd(), &oldRect);
		oldPos = convert(oldRect);

		HMONITOR m = MonitorFromWindow(wnd.hwnd(), MONITOR_DEFAULTTONEAREST);
		MONITORINFO mi = { sizeof(mi) };
		GetMonitorInfo(m, &mi);

		SetWindowLong(wnd.hwnd(), GWL_STYLE, oldStyle & ~(WS_CAPTION | WS_THICKFRAME));

		LONG l = mi.rcMonitor.left;
		LONG r = mi.rcMonitor.right;
		LONG t = mi.rcMonitor.top;
		LONG b = mi.rcMonitor.bottom;
		SetWindowPos(wnd.hwnd(), NULL, l, t, r - l, b - t, SWP_NOZORDER | SWP_NOACTIVATE | SWP_FRAMECHANGED);
	}

	static void goBack(Handle wnd, Nat &style, Rect &rect) {
		LONG l = LONG(rect.p0.x);
		LONG r = LONG(rect.p1.x);
		LONG t = LONG(rect.p0.y);
		LONG b = LONG(rect.p1.y);

		SetWindowLong(wnd.hwnd(), GWL_STYLE, LONG(style));
		SetWindowPos(wnd.hwnd(), NULL, l, t, r - l, b - t, SWP_NOZORDER | SWP_NOACTIVATE | SWP_FRAMECHANGED);
	}

	// Helper to create the actual handle.
	bool Frame::createWindow(bool sizeable, MAYBE(Frame *) parent) {
		DWORD exStyles = WS_EX_CONTROLPARENT;
		DWORD styles = 0;
		if (sizeable)
			styles = WS_OVERLAPPEDWINDOW;
		else
			styles = WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX;

		styles |= WS_CLIPCHILDREN;

		// Respect the minimum size.
		updateMinSize();
		Size sz = myPos.size();
		sz.w = max(sz.w, lastMinSize.w);
		sz.h = max(sz.h, lastMinSize.h);
		myPos.size(sz);

		HWND hParent = NULL;
		if (parent)
			hParent = parent->handle().hwnd();
		CreateFlags cFlags = cManualVisibility;
		if (!hasFlag(positionAltered)) {
			cFlags |= cAutoPos;
		}
		if (!createEx(NULL, styles, exStyles, hParent, 0, cFlags))
			return false;

		dpi = windowDpi(handle().hwnd());

		// Create child windows (a bit of a hack, sorry!)
		parentCreated(0);

		// Set icons.
		icon(myIcon);

		if (visible()) {
			// At some points, it seems we need to hide the window first in order for it to be
			// visible later. This happens when running Storm as a background process to Emacs for
			// example.
			ShowWindow(handle().hwnd(), FALSE);
			ShowWindow(handle().hwnd(), TRUE);
			update();
		}

		if (hasFlag(isFullscreen)) {
			goFull(handle(), windowedStyle, windowedRect);
		}

		if (myMenu) {
			setMenu(null);
		}

		// Set focus as requested.
		if (setFocus) {
			setFocus->focus();
		}

		// Update inhibit screensaver state.
		if (hasFlag(noScreenSaver))
			app(engine())->inhibitScreenSaver(this, true);

		return true;
	}

	static void fill(MINMAXINFO *info, HWND hWnd, Size size, bool menu, Nat dpi) {
		// Need to adjust 'size' since it is measured in client size, while Win32 expects actual window sizes.
		LONG styles = GetWindowLong(hWnd, GWL_STYLE);
		LONG exStyles = GetWindowLong(hWnd, GWL_EXSTYLE);
		size = clientToWindow(size, styles, exStyles, menu, dpi);

		info->ptMinTrackSize.x = (LONG)size.w;
		info->ptMinTrackSize.y = (LONG)size.h;
	}

	MsgResult Frame::onMessage(const Message &msg) {
		switch (msg.msg) {
		case WM_CLOSE:
			close();
			return msgResult(0);
#ifdef WIN32_TESTDPI
		case WM_LBUTTONDBLCLK: {
			// FOR TESTING:
			RECT rect;
			GetClientRect(handle().hwnd(), &rect);
			PVAR(rect.right); PVAR(rect.bottom);
			if (dpi == 96) {
				dpi += dpi / 2;
				rect.right += rect.right / 2;
				rect.bottom += rect.bottom / 2;
			} else {
				dpi = 96;
				rect.right -= rect.right / 3;
				rect.bottom -= rect.bottom / 3;
			}
			PVAR(rect.right); PVAR(rect.bottom);
			LONG styles = GetWindowLong(handle().hwnd(), GWL_STYLE);
			LONG exStyles = GetWindowLong(handle().hwnd(), GWL_EXSTYLE);
			dpiAdjustWindowRectEx(&rect, styles, myMenu != null, exStyles, 96);
			rect.right -= rect.left;
			rect.bottom -= rect.top;
			updateDpi(false);
			SetWindowPos(handle().hwnd(), NULL, 0, 0, rect.right, rect.bottom, SWP_NOACTIVATE | SWP_NOZORDER | SWP_NOMOVE);
			break;
		}
#endif
		case WM_DPICHANGED: {
			dpi = HIWORD(msg.wParam);
			// Don't move us automatically, we want to respect the rectangle Windows suggest.
			updateDpi(false);
			RECT *newPos = (RECT *)msg.lParam;
			SetWindowPos(handle().hwnd(), NULL,
						newPos->left, newPos->top,
						newPos->right - newPos->left, newPos->bottom - newPos->top,
						SWP_NOACTIVATE | SWP_NOZORDER);
			return msgResult(0);
		}
		case WM_SETCURSOR:
			if (LOWORD(msg.lParam) == HTCLIENT && !hasFlag(showCursor)) {
				SetCursor(NULL);
				return msgResult(TRUE);
			}
			break;
		case WM_GETMINMAXINFO:
			fill((MINMAXINFO *)msg.lParam, handle().hwnd(), dpiToPx(currentDpi(), lastMinSize), myMenu != null, dpi);
			return msgResult(0);
		case WM_MENUCOMMAND:
			menuClicked((HMENU)msg.lParam, (Nat)msg.wParam);
			return msgResult(TRUE);
		case WM_SIZE:
			if (msg.wParam == SIZE_MINIMIZED) {
				if (!hasFlag(isMinimized) && hasFlag(noScreenSaver)) {
					app(engine())->inhibitScreenSaver(this, false);
				}
				setFlag(isMinimized);
			} else if (msg.wParam == SIZE_RESTORED) {
				if (hasFlag(isMinimized) && hasFlag(noScreenSaver)) {
					app(engine())->inhibitScreenSaver(this, true);
				}
				clearFlag(isMinimized);
			}
			// Note: Intentional to not stop message propagation - we only wish to detect what is happening!
			break;
		}

		return Container::onMessage(msg);
	}

	MsgResult Frame::beforeMessage(const Message &msg) {
		switch (msg.msg) {
		case WM_KEYDOWN:
			// Note "modifiers()" give modifiers as they where when the message was queued, which is what we want.
			if (myAccelerators->dispatch(KeyChord(keycode(msg.wParam), modifiers()))) {
				return msgResult(0);
			}
			break;
		}
		return Container::beforeMessage(msg);
	}

	Nat Frame::currentDpi() {
		return dpi;
	}

	void Frame::windowBackground(HBRUSH &brush, COLORREF &color) {
		color = GetSysColor(gui::windowBackground);
		brush = GetSysColorBrush(gui::windowBackground);
	}

	void Frame::cursorVisible(Bool v) {
		// Will be altered soon anyway, we do not need to do anything here.
		setFlag(showCursor, v);
	}

	void Frame::updateMinSize() {
		lastMinSize = minSize();
	}

	void Frame::setMenu(MenuBar *) {
		// Check the size of the window now.
		RECT original;
		GetClientRect(handle().hwnd(), &original);

		// Note: SetMenu does not remove the old menu. That is exactly what we want.
		if (myMenu) {
			SetMenu(handle().hwnd(), myMenu->handle.menu());
		} else {
			SetMenu(handle().hwnd(), NULL);
		}

		// Check the new size.
		RECT changed;
		GetClientRect(handle().hwnd(), &changed);

		// Alter our height if needed.
		int delta = original.bottom - changed.bottom;
		if (delta && !hasFlag(isFullscreen)) {
			GetWindowRect(handle().hwnd(), &original);
			original.bottom += delta;
			int width = original.right - original.left;
			int height = original.bottom - original.top;
			SetWindowPos(handle().hwnd(), HWND_TOP, 0, 0, width, height, SWP_NOACTIVATE | SWP_NOMOVE | SWP_NOZORDER);
		}
	}

	void Frame::menuClicked(HMENU menu, Nat id) {
		if (!myMenu)
			return;

		if (Menu *m = myMenu->findMenu(menu)) {
			if (id >= m->count())
				return;

			(*m)[id]->clicked();
		}
	}

	void Frame::popupMenu(PopupMenu *menu) {
		if (!created())
			throw new (this) GuiError(S("Can not show a popup menu from a window that is not created."));

		POINT pt;
		GetCursorPos(&pt);
		RECT limits;
		GetWindowRect(handle().hwnd(), &limits);

		if (pt.x < limits.left)
			pt.x = limits.left;
		if (pt.x > limits.right - 1)
			pt.x = limits.right - 1;
		if (pt.y < limits.top)
			pt.y = limits.top;
		if (pt.y > limits.bottom - 1)
			pt.y = limits.bottom - 1;

		TrackPopupMenuEx(menu->handle.menu(), TPM_LEFTALIGN | TPM_TOPALIGN, pt.x, pt.y, handle().hwnd(), NULL);
	}

	void Frame::icon(MAYBE(Icon *) icon) {
		myIcon = icon;

		if (created()) {
			Icon *toSet = myIcon;
			if (!toSet)
				toSet = app(engine())->icon();

			HWND hWnd = handle().hwnd();

			SendMessage(hWnd, WM_SETICON, ICON_SMALL, LPARAM(toSet ? toSet->win32Small() : NULL));
			SendMessage(hWnd, WM_SETICON, ICON_BIG, LPARAM(toSet ? toSet->win32Large() : NULL));
		}
	}

#endif
#ifdef GUI_GTK

	Rect Frame::pos() {
		return Window::pos();
	}

	void Frame::pos(Rect r) {
		myPos = r;
		setFlag(positionAltered);
		if (!hasFlag(isFullscreen) && handle() != Handle()) {
			gtk_window_resize(GTK_WINDOW(handle().widget()), r.size().w, r.size().h);
		}
	}

	static void goFull(Handle wnd, Nat &, Rect &) {
		GdkWindow *window = gtk_widget_get_window(wnd.widget());
		gdk_window_set_fullscreen_mode(window, GDK_FULLSCREEN_ON_CURRENT_MONITOR);
		gdk_window_fullscreen(window);
	}

	static void goBack(Handle wnd, Nat &, Rect &) {
		GdkWindow *window = gtk_widget_get_window(wnd.widget());
		gdk_window_unfullscreen(window);
	}

	bool Frame::createWindow(bool sizeable, MAYBE(Frame *) parent) {
		GtkWidget *frame = null;
		// If "parent" is set, we want to create a dialog instead.
		if (parent) {
			// We use "with buttons" so that we may specify flags and parent!
			const char *title = text()->utf8_str();
			GtkDialogFlags flags = GTK_DIALOG_MODAL;
			GtkWindow *parentWindow = GTK_WINDOW(parent->handle().widget());
			// The last NULL is not needed, but GCC complains if we omit it.
			frame = gtk_dialog_new_with_buttons(title, parentWindow, flags, NULL, NULL);

			// A dialog already contains a Bin container. So we remove that...
			GtkWidget *contained = gtk_bin_get_child(GTK_BIN(frame));
			gtk_container_remove(GTK_CONTAINER(frame), contained);
		} else {
			frame = gtk_window_new(GTK_WINDOW_TOPLEVEL);
			gtk_window_set_title((GtkWindow *)frame, text()->utf8_str());
		}
		gtk_window_set_resizable((GtkWindow *)frame, sizeable);

		// Add a VBox to the frame, so we can place the menu appropriately.
		GtkWidget *vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
		gtk_container_add(GTK_CONTAINER(frame), vbox);
		gtk_widget_show(vbox);

		// Place a BASIC container inside, so that we can control layout of child widgets as we want.
		GtkWidget *container = basic_new();
		gtk_box_pack_end(GTK_BOX(vbox), container, true, true, 0);
		gtk_widget_show(container);

		Signal<void, Frame>::Connect<&Frame::close>::to(frame, "destroy", engine());
		Signal<gboolean, Frame, GdkEventWindowState *>::Connect<&Frame::windowState>::to(frame, "window-state-event", engine());

		Size sz = pos().size();
		if (sz.w > 0 && sz.h > 0)
			gtk_window_set_default_size((GtkWindow *)frame, sz.w, sz.h);

		handle(frame);
		initSignals(frame, container);

		// Set icon if applicable.
		icon(myIcon);

		// Attach accelerators.
		myAccelerators->attach(handle());

		// Create child windows (if any).
		parentCreated(0);

		updateMinSize();

		// Set visibility and full screen attributes.
		if (visible()) {
			gtk_widget_show(frame);
		}

		// Update cursor visibility.
		cursorVisible(hasFlag(showCursor));

		if (hasFlag(isFullscreen)) {
			goFull(handle(), windowedStyle, windowedRect);
		}

		if (myMenu) {
			setMenu(null);
		}

		// Set focus as requested.
		if (setFocus) {
			setFocus->focus();
		}

		// Update inhibit screensaver state.
		if (hasFlag(noScreenSaver))
			app(engine())->inhibitScreenSaver(this, true);

		return true;
	}

	void Frame::text(Str *str) {
		if (created()) {
			gtk_window_set_title((GtkWindow *)handle().widget(), str->utf8_str());
		}
		Window::text(str);
	}

	static void getLastCB(GtkWidget *widget, gpointer data) {
		GtkWidget **store = (GtkWidget **)data;
		*store = widget;
	}

	static Basic *getContainer(GtkWidget *root) {
		GtkWidget *found = null;
		GtkWidget *vbox = gtk_bin_get_child(GTK_BIN(root));
		gtk_container_foreach(GTK_CONTAINER(vbox), &getLastCB, &found);
		// There is a BASIC layout at the last position.
		return BASIC(found);
	}

	void Frame::addChild(GtkWidget *child, Rect pos) {
		Basic *basic = getContainer(handle().widget());
		Size s = pos.size();
		basic_put(basic, child, pos.p0.x, pos.p0.y, s.w, s.h);
	}

	void Frame::moveChild(GtkWidget *child, Rect pos) {
		Basic *basic = getContainer(handle().widget());
		Size s = pos.size();
		basic_move(basic, child, pos.p0.x, pos.p0.y, s.w, s.h);
	}

	GtkWidget *Frame::drawWidget() {
		GtkWidget *found = null;
		GtkWidget *vbox = gtk_bin_get_child(GTK_BIN(handle().widget()));
		gtk_container_foreach(GTK_CONTAINER(vbox), &getLastCB, &found);
		return found;
	}

	void Frame::cursorVisible(Bool v) {
		setFlag(showCursor, v);
		if (created()) {
			GtkWidget *widget = handle().widget();
			GdkCursor *cursor = null;
			if (hasFlag(showCursor))
				cursor = gdk_cursor_new_from_name(gtk_widget_get_display(widget), "default");
			else
				cursor = gdk_cursor_new_for_display(gtk_widget_get_display(widget), GDK_BLANK_CURSOR);

			GdkWindow *window = gtk_widget_get_window(widget);

			gdk_window_set_cursor(window, cursor);
		}
	}

	void Frame::updateMinSize() {
		lastMinSize = minSize();

		if (handle() != Handle()) {
			GtkWidget *widget = drawWidget();
			gtk_widget_set_size_request(widget, (gint)lastMinSize.w, (gint)lastMinSize.h);
		}
	}

	void Frame::setMenu(MenuBar *old) {
		GtkWidget *vbox = gtk_bin_get_child(GTK_BIN(handle().widget()));
		gint heightDiff = 0;
		gint oldWidth, oldHeight;
		gtk_window_get_size(GTK_WINDOW(handle().widget()), &oldWidth, &oldHeight);

		if (old) {
			gint natural;
			gtk_widget_get_preferred_height(old->handle.widget(), NULL, &natural);
			gtk_container_remove(GTK_CONTAINER(vbox), old->handle.widget());

			heightDiff -= natural;
		}

		if (myMenu) {
			// Place the menu first.
			GtkWidget *m = myMenu->handle.widget();
			gtk_box_pack_start(GTK_BOX(vbox), m, false, false, 0);

			gtk_widget_show_all(m);

			gint natural;
			gtk_widget_get_preferred_height(m, NULL, &natural);

			heightDiff += natural;
		}

		if (!hasFlag(isFullscreen))
			gtk_window_resize(GTK_WINDOW(handle().widget()), oldWidth, oldHeight + heightDiff);

	}

	void Frame::popupMenu(PopupMenu *menu) {
		if (!created())
			throw new (this) GuiError(S("Can not show a popup menu from a window that is not created."));

		// Note: We will keep the popup alive a bit too long here, as it is difficult to get a
		// definitive answer as to when the popup menu is closed.
		myPopup = menu;
		gtk_menu_popup_at_pointer(GTK_MENU(menu->handle.widget()), NULL);
	}

	gboolean Frame::windowState(GdkEventWindowState *ws) {
		Bool minimized = (ws->new_window_state & GDK_WINDOW_STATE_ICONIFIED) != 0;
		if (minimized != hasFlag(isMinimized)) {
			setFlag(isMinimized, minimized);
			if (hasFlag(noScreenSaver)) {
				app(engine())->inhibitScreenSaver(this, !minimized);
			}
		}
		return TRUE;
	}

	void Frame::icon(MAYBE(Icon *) icon) {
		myIcon = icon;

		if (created()) {
			Icon *toSet = myIcon;
			if (!toSet)
				toSet = app(engine())->icon();

			// Note: We can't really ask Gtk to "remove" the icon if we ever set one.
			if (toSet)
				gtk_window_set_icon_list(GTK_WINDOW(handle().widget()), toSet->gtkList());
		}
	}

#endif

}