Mercurial > projects > dynamin
annotate dynamin/gui/window.d @ 5:4029d5af7542
Add blank lines and rewrap some comments.
author | Jordan Miner <jminer7@gmail.com> |
---|---|
date | Sat, 20 Jun 2009 12:26:40 -0500 |
parents | 3ab1e9bfb88c |
children | 7a7e5f9bd1ae |
rev | line source |
---|---|
0 | 1 // Written in the D programming language |
2 // www.digitalmars.com/d/ | |
3 | |
4 /* | |
5 * The contents of this file are subject to the Mozilla Public License Version | |
6 * 1.1 (the "License"); you may not use this file except in compliance with | |
7 * the License. You may obtain a copy of the License at | |
8 * http://www.mozilla.org/MPL/ | |
9 * | |
10 * Software distributed under the License is distributed on an "AS IS" basis, | |
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License | |
12 * for the specific language governing rights and limitations under the | |
13 * License. | |
14 * | |
15 * The Original Code is the Dynamin library. | |
16 * | |
17 * The Initial Developer of the Original Code is Jordan Miner. | |
18 * Portions created by the Initial Developer are Copyright (C) 2006-2009 | |
19 * the Initial Developer. All Rights Reserved. | |
20 * | |
21 * Contributor(s): | |
22 * Jordan Miner <jminer7@gmail.com> | |
23 * | |
24 */ | |
25 | |
26 module dynamin.gui.window; | |
27 | |
28 import dynamin.c.cairo; | |
29 import dynamin.all_core; | |
30 import dynamin.all_painting; | |
31 import dynamin.all_gui; | |
32 import dynamin.gui.control; | |
1
3ab1e9bfb88c
Fix a rename of gui.backend -> gui_backend I missed.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
33 import dynamin.gui_backend; |
0 | 34 import dynamin.gui.container; |
35 import dynamin.gui.events; | |
36 import tango.io.Stdout; | |
37 import tango.core.Exception; | |
38 import tango.core.Thread; | |
39 | |
40 static class Application { | |
41 static: | |
42 mixin ApplicationBackend; | |
43 package Thread eventThread; | |
44 void run(Window w = null) { | |
45 Window.hasProcessedEvents = true; | |
46 | |
47 auto thread = Thread.getThis(); | |
48 assert(eventThread is null || eventThread is thread, | |
49 "Application.run called from two different threads"); | |
50 eventThread = thread; | |
51 | |
52 backend_run(w); | |
53 } | |
54 //void invoke(void delegate() dg) { | |
55 // | |
56 //} | |
57 //void invokeNow(void delegate() dg) | |
58 } | |
59 | |
60 enum DialogResult { | |
61 /// | |
62 OK, | |
63 /// | |
64 Yes, | |
65 /// | |
66 No, | |
67 /// | |
68 Cancel, | |
69 /// | |
70 Custom | |
71 } | |
72 | |
73 /// | |
74 enum Position { | |
75 /// Specifies being at the top-left corner. | |
76 TopLeft, | |
77 /// Specifies being centered between the top-left corner and the top-right corner. | |
78 Top, | |
79 /// Specifies being at the top-right corner. | |
80 TopRight, | |
81 /// Specifies being centered between the top-left corner and the bottom-left corner. | |
82 Left, | |
83 /// Specifies being centered between all corners. | |
84 Center, | |
85 /// Specifies being centered between the top-right corner and the bottom-right corner. | |
86 Right, | |
87 /// Specifies being at the bottom-left corner. | |
88 BottomLeft, | |
89 /// Specifies being centered between the bottom-left corner and the bottom-right corner. | |
90 Bottom, | |
91 /// Specifies being at the bottom-right corner. | |
92 BottomRight | |
93 } | |
94 | |
95 /** | |
96 * The different types of borders that a window may have. | |
97 * These do not affect whether the window is resizable-- | |
98 * use Window.Resizable for that. | |
99 */ | |
100 enum WindowBorderStyle { | |
101 /** Specifies that the window has no border around the content area. */ | |
102 None, | |
103 /** | |
104 * Specifies that the window has a normal border with a title bar, icon, | |
105 * and minimize button. | |
106 */ | |
107 Normal, | |
108 /** Specifies that the window has a normal border without a minimize button. */ | |
109 Dialog, | |
110 /** Specifies that the window has the border of a floating tool box. */ | |
111 Tool | |
112 } | |
113 | |
114 alias List!(Control) ControlList; | |
115 //Frames and Dialogs are identical except that Dialogs do not have minimize and | |
116 //maximize buttons, are not shown on the taskbar, and can be modal. | |
117 /** | |
118 * A window is a top level control that has no parent. Its location is relative | |
119 * to the top-left corner of the screen. | |
120 * A window can have no border, the border of a normal window, or the border | |
121 * of a tool window. | |
122 * | |
123 * The appearance of a window with Windows Classic: | |
124 * | |
125 * $(IMAGE ../web/example_window.png) | |
126 */ | |
127 class Window : Container { | |
128 private static hasProcessedEvents = false; | |
129 ~this() { // this should be a static ~this, but I get a circular dep error | |
130 if(!hasProcessedEvents) { | |
131 Stdout("Warning: a window was created, but Application.run"); | |
132 Stdout(" was not called to process events").newline; | |
133 } | |
134 } | |
135 protected: | |
136 mixin WindowBackend; | |
137 BorderSize _borderSize; | |
138 Window _owner; | |
139 WindowBorderStyle _borderStyle; | |
140 bool _resizable = true; | |
141 Panel _content; | |
142 Control _focusedControl; | |
143 package Control focusedControl() { return _focusedControl; } | |
144 package void focusedControl(Control c) { | |
145 _focusedControl = c; | |
146 } | |
147 override void dispatchPainting(PaintingEventArgs e) { | |
148 Theme.current.Window_paint(this, e.graphics); | |
149 super.dispatchPainting(e); | |
150 } | |
151 public: | |
152 this() { | |
153 _children = new ControlList(); | |
154 content = new Panel; | |
155 | |
156 _visible = false; | |
157 _minSize = Size(0, 0); | |
158 _maxSize = Size(0, 0); | |
159 _borderStyle = WindowBorderStyle.Normal; | |
160 recreateHandle(); | |
161 } | |
162 this(string text) { | |
163 this(); | |
164 this.text = text; | |
165 } | |
166 | |
167 void content(Panel panel) { | |
168 if(panel is null) | |
169 throw new IllegalArgumentException("content must not be null"); | |
170 // TODO: remove handlers | |
171 super.remove(panel); | |
172 super.add(_content = panel); | |
173 _content.resized += &whenContentResized; | |
174 _content.minSizeChanged += &whenContentMinSizeChanged; | |
175 _content.maxSizeChanged += &whenContentMaxSizeChanged; | |
176 | |
177 auto best = _content.bestSize; | |
178 _content.minSize = best; | |
179 _content.maxSize = Size(_content.elasticX ? 0 : best.width, | |
180 _content.elasticY ? 0 : best.height); | |
181 resizable = _content.maxSize != best; // avoid calling elasticX/Y again | |
182 _content.size = best; | |
183 | |
184 } | |
185 bool ignoreResize; | |
186 void whenContentResized(EventArgs e) { | |
187 if(ignoreResize) | |
188 return; | |
189 ignoreResize = true; | |
190 size = _content.size + _borderSize; | |
191 ignoreResize = false; | |
192 } | |
193 void whenContentMinSizeChanged(EventArgs e) { | |
194 if(!handleCreated) | |
195 return; | |
196 backend_contentMinSizeChanged; | |
197 } | |
198 void whenContentMaxSizeChanged(EventArgs e) { | |
199 if(!handleCreated) | |
200 return; | |
201 backend_contentMaxSizeChanged; | |
202 } | |
203 override void whenResized(EventArgs e) { | |
204 if(ignoreResize) | |
205 return; | |
206 _content._location = Point(_borderSize.left, _borderSize.top); | |
207 ignoreResize = true; | |
208 _content.size = _size-_borderSize; | |
209 ignoreResize = false; | |
210 } | |
211 Panel content() { | |
212 return _content; | |
213 } | |
5
4029d5af7542
Add blank lines and rewrap some comments.
Jordan Miner <jminer7@gmail.com>
parents:
1
diff
changeset
|
214 |
0 | 215 /** |
216 * If the handle has not yet been created, calling this will cause it to be. | |
217 * Under the Windows backend, returns a HWND. | |
218 * Under the X backend, returns a Window. | |
219 * Returns: the backend specific native handle. | |
220 */ | |
221 typeof(_handle) handle() { | |
222 if(!handleCreated) | |
223 recreateHandle(); | |
224 assert(Thread.getThis() is Application.eventThread || | |
225 Application.eventThread is null, | |
226 "controls must be accessed and changed only on the event thread"); | |
227 return _handle; | |
228 } | |
5
4029d5af7542
Add blank lines and rewrap some comments.
Jordan Miner <jminer7@gmail.com>
parents:
1
diff
changeset
|
229 |
0 | 230 bool handleCreated() { return backend_handleCreated; } |
5
4029d5af7542
Add blank lines and rewrap some comments.
Jordan Miner <jminer7@gmail.com>
parents:
1
diff
changeset
|
231 |
0 | 232 void recreateHandle() { |
233 backend_recreateHandle(); | |
234 } | |
5
4029d5af7542
Add blank lines and rewrap some comments.
Jordan Miner <jminer7@gmail.com>
parents:
1
diff
changeset
|
235 |
0 | 236 override protected Graphics quickCreateGraphics() { |
237 if(!handleCreated) | |
238 return null; | |
239 return backend_quickCreateGraphics(); | |
240 } | |
241 override bool onScreen() { | |
242 return true; | |
243 } | |
244 override Point screenLocation() { | |
245 return location; | |
246 } | |
247 override Point contentToScreen(Point pt) { | |
248 return pt + location; | |
249 } | |
250 override Point screenToContent(Point pt) { | |
251 return pt - location; | |
252 } | |
253 override bool topLevel() { return true; } | |
254 override Container parent() { return null; } | |
5
4029d5af7542
Add blank lines and rewrap some comments.
Jordan Miner <jminer7@gmail.com>
parents:
1
diff
changeset
|
255 // TODO: because you should always be able to click a window from |
4029d5af7542
Add blank lines and rewrap some comments.
Jordan Miner <jminer7@gmail.com>
parents:
1
diff
changeset
|
256 // the taskbar, then show it on taskbar if window has an owner, |
4029d5af7542
Add blank lines and rewrap some comments.
Jordan Miner <jminer7@gmail.com>
parents:
1
diff
changeset
|
257 // but don't if it does not |
0 | 258 void owner(Window w) { |
259 _owner = w; | |
260 if(!handleCreated) | |
261 return; | |
262 recreateHandle(); | |
263 } | |
264 Window owner() { return _owner; } | |
265 alias Control.visible visible; | |
266 void visible(bool b) { | |
267 _visible = b; | |
268 backend_visible = b; | |
269 } | |
5
4029d5af7542
Add blank lines and rewrap some comments.
Jordan Miner <jminer7@gmail.com>
parents:
1
diff
changeset
|
270 |
0 | 271 /** |
272 * Gets or sets what border this window will have around its contents. | |
273 * The default is WindowBorderStyle.Normal. | |
274 */ | |
275 WindowBorderStyle borderStyle() { return _borderStyle; } | |
276 /// ditto | |
277 void borderStyle(WindowBorderStyle border) { | |
278 if(border > WindowBorderStyle.Tool) | |
279 throw new IllegalArgumentException("Window.borderStyle(): invalid border style"); | |
280 _borderStyle = border; | |
281 backend_borderStyle = border; | |
282 } | |
5
4029d5af7542
Add blank lines and rewrap some comments.
Jordan Miner <jminer7@gmail.com>
parents:
1
diff
changeset
|
283 |
0 | 284 alias Control.repaint repaint; |
285 void repaint(Rect rect) { | |
286 if(!handleCreated) | |
287 return; | |
288 backend_repaint(rect); | |
289 } | |
5
4029d5af7542
Add blank lines and rewrap some comments.
Jordan Miner <jminer7@gmail.com>
parents:
1
diff
changeset
|
290 |
0 | 291 /** |
292 * An array of rectangles in screen coordinates that the window will be | |
293 * snapped to. | |
294 */ | |
295 Rect[] snapRects = null; | |
296 /** | |
297 * Convenience method that sets SnapRects to an array | |
298 * with just the specified Rect. | |
299 */ | |
300 void snapRect(Rect rect) { | |
301 snapRects = [rect]; | |
302 } | |
5
4029d5af7542
Add blank lines and rewrap some comments.
Jordan Miner <jminer7@gmail.com>
parents:
1
diff
changeset
|
303 |
0 | 304 /** |
305 * The SnapDistance specifies how close a window has to be to a | |
306 * snap rectangle for the window to snap to it. The default is 10 pixels. | |
307 */ | |
308 uint snapDistance = 10; | |
5
4029d5af7542
Add blank lines and rewrap some comments.
Jordan Miner <jminer7@gmail.com>
parents:
1
diff
changeset
|
309 |
0 | 310 /** |
311 * Gets or sets whether this window can be resized by the user. | |
312 * The default is true. | |
313 */ | |
314 bool resizable() { return _resizable; } | |
315 /// ditto | |
316 void resizable(bool b) { // TODO: set based upon whether content is elastic? | |
317 _resizable = b; | |
318 if(!handleCreated) | |
319 return; | |
320 backend_resizable = b; | |
321 } | |
5
4029d5af7542
Add blank lines and rewrap some comments.
Jordan Miner <jminer7@gmail.com>
parents:
1
diff
changeset
|
322 |
0 | 323 // TODO: 1.0 MinSize -> contentMinSize MaxSize -> contentMaxSize |
324 alias Control.location location; | |
325 void location(Point pt) { | |
326 super.location(pt); | |
327 if(!handleCreated) | |
328 return; | |
329 backend_location = pt; | |
330 } | |
331 alias Control.size size; | |
332 void size(Size size) { | |
333 super.size(size); | |
334 _content.size = size - _borderSize; | |
335 if(!handleCreated) | |
336 return; | |
337 backend_size = size; | |
338 } | |
339 /** | |
340 * Gets the size of the border/frame around this window. | |
341 */ | |
342 BorderSize borderSize() { | |
343 return _borderSize; | |
344 } | |
345 alias Control.text text; | |
346 void text(string str) { | |
347 super.text(str); | |
348 if(!handleCreated) | |
349 return; | |
350 backend_text = str; | |
351 } | |
5
4029d5af7542
Add blank lines and rewrap some comments.
Jordan Miner <jminer7@gmail.com>
parents:
1
diff
changeset
|
352 |
0 | 353 /** |
354 * Moves this window to the specified position relative to | |
355 * the specified control. If no control is specified, the | |
356 * window is positioned relative to the screen. | |
357 */ | |
358 void position(Position pos, Control c = null) { | |
359 Rect rect; | |
360 if(c && c.onScreen) { | |
361 rect = c.screenLocation + c.size; | |
362 } else { | |
363 rect = desktopRect; | |
364 } | |
365 Point newLoc = Point(); | |
366 switch(pos) { | |
367 case Position.TopLeft: | |
368 case Position.Left: | |
369 case Position.BottomLeft: | |
370 newLoc.x = rect.x; | |
371 break; | |
372 case Position.Top: | |
373 case Position.Center: | |
374 case Position.Bottom: | |
375 newLoc.x = rect.x + (rect.width - width)/2; | |
376 break; | |
377 case Position.TopRight: | |
378 case Position.Right: | |
379 case Position.BottomRight: | |
380 newLoc.x = rect.x + rect.width - width; | |
381 break; | |
382 } | |
383 switch(pos) { | |
384 case Position.TopLeft: | |
385 case Position.Top: | |
386 case Position.TopRight: | |
387 newLoc.y = rect.y; | |
388 break; | |
389 case Position.Left: | |
390 case Position.Center: | |
391 case Position.Right: | |
392 newLoc.y = rect.y + (rect.height - height)/2; | |
393 break; | |
394 case Position.BottomLeft: | |
395 case Position.Bottom: | |
396 case Position.BottomRight: | |
397 newLoc.y = rect.y + rect.height - height; | |
398 break; | |
399 } | |
400 location = newLoc; | |
401 } | |
402 } | |
403 |