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