PageRenderTime 24ms CodeModel.GetById 16ms app.highlight 6ms RepoModel.GetById 1ms app.codeStats 0ms

/gui/application.d

http://github.com/wilkie/djehuty
D | 156 lines | 94 code | 42 blank | 20 comment | 7 complexity | ffd4eac79541c8fb7a89ba10dfb43ebd MD5 | raw file
  1/*
  2 * application.d
  3 *
  4 * This module implements the GUI application class that will be the starting
  5 * point for a GUI application. From this, Window classes with Widgets can be
  6 * created and pushed.
  7 *
  8 * Author: Dave Wilkinson
  9 * Originated: June 24th, 2009
 10 *
 11 */
 12
 13module gui.application;
 14
 15import core.application;
 16import core.event;
 17
 18import gui.widget;
 19import gui.window;
 20
 21// Platform Specific application entry
 22import gui.apploop;
 23
 24import platform.vars.window;
 25
 26import scaffold.window;
 27
 28class GuiApplication : Application {
 29public:
 30
 31	// Constructors
 32
 33	this() {
 34		_appController = new GuiApplicationController();
 35		super();
 36	}
 37
 38	override void push(Dispatcher dsp) {
 39		if (cast(Window)dsp !is null) {
 40			addWindow(cast(Window)dsp);
 41		}
 42
 43		super.push(dsp);
 44	}
 45
 46	// Properties
 47
 48	int numWindows() {
 49		return _windowCount;
 50	}
 51
 52	int numVisible() {
 53		return _windowVisibleCount;
 54	}
 55
 56	// Methods
 57
 58	override bool isZombie() {
 59		return _windowVisibleCount == 0;
 60	}
 61
 62	Window firstWindow() {
 63		return _windowListHead;
 64	}
 65
 66protected:
 67	package Window _windowListHead = null;
 68	package Window _windowListTail = null;
 69
 70	package int _windowCount;
 71	package int _windowVisibleCount;
 72
 73	override void start() {
 74		_appController.start();
 75	}
 76
 77	override void end(uint exitCode) {
 78		_appController.end(exitCode);
 79	}
 80
 81private:
 82
 83	GuiApplicationController _appController;
 84
 85	// Description: Will add and create the window (as long as it hasn't been already) and add it to the root window hierarchy.
 86	// window: An instance of a Window class, or any the inherit from Window.
 87	void addWindow(Window window) {
 88		WindowPlatformVars* wpv = &window._pfvars;
 89
 90		synchronized {
 91			// update the window linked list
 92			updateWindowList(window);
 93
 94			// increase global window count
 95			_windowCount++;
 96
 97			// create the window through platform calls
 98			WindowCreate(window, wpv);
 99		}
100
101		if (window.visible) {
102			WindowSetVisible(window, wpv, true);
103		}
104	}
105
106	void updateWindowList(Window window) {
107		window._inited = true;
108
109		if (_windowListHead is null)
110		{
111			_windowListHead = window;
112			_windowListTail = window;
113
114			window._nextWindow = window;
115			window._prevWindow = window;
116		}
117		else
118		{
119			window._nextWindow = _windowListHead;
120			window._prevWindow = _windowListTail;
121
122			_windowListHead._prevWindow = window;
123			_windowListTail._nextWindow = window;
124
125			_windowListHead = window;
126		}
127
128		if (window._visible)
129		{
130			_windowVisibleCount++;
131		}
132	}
133
134	package void destroyAllWindows()
135	{
136		Window w = _windowListHead;
137
138		if (w is null) { return; }
139
140		Window tmp = w;
141
142		_windowListHead = null;
143		_windowListTail = null;
144
145		do
146		{
147			w.remove();
148
149			w = w._nextWindow;
150
151		} while (w !is tmp)
152
153		_windowCount = 0;
154		_windowVisibleCount = 0;
155	}
156}