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