Mercurial > projects > dynamin
comparison dynamin/gui/control.d @ 55:c138461bf845
Add focusing and other changes that are related
like descendantAdded/Removed events, Window.activated event, and updating List.
Window.state was also added, even though focusing does not depend on it.
author | Jordan Miner <jminer7@gmail.com> |
---|---|
date | Sat, 08 Aug 2009 15:42:27 -0500 |
parents | 6e33b00595e9 |
children | c2566ab82535 |
comparison
equal
deleted
inserted
replaced
54:3738a2d0bac3 | 55:c138461bf845 |
---|---|
31 import dynamin.gui.events; | 31 import dynamin.gui.events; |
32 import dynamin.gui.window; | 32 import dynamin.gui.window; |
33 import dynamin.gui.cursor; | 33 import dynamin.gui.cursor; |
34 import tango.io.Stdout; | 34 import tango.io.Stdout; |
35 | 35 |
36 //{{{ hotControl | |
36 Control hotControl; | 37 Control hotControl; |
37 // the hot control is the one the mouse is over | 38 // the hot control is the one the mouse is over |
38 package void setHotControl(Control c) { | 39 package void setHotControl(Control c) { |
39 if(c !is hotControl) { | 40 if(c !is hotControl) { |
40 if(hotControl) | 41 if(hotControl) |
43 if(c) | 44 if(c) |
44 c.mouseEntered(new EventArgs); | 45 c.mouseEntered(new EventArgs); |
45 } | 46 } |
46 } | 47 } |
47 package Control getHotControl() { return hotControl; } | 48 package Control getHotControl() { return hotControl; } |
49 //}}} | |
50 | |
51 //{{{ captorControl | |
48 Control captorControl; | 52 Control captorControl; |
49 package void setCaptorControl(Control c) { | 53 package void setCaptorControl(Control c) { |
50 captorControl = c; | 54 captorControl = c; |
51 } | 55 } |
52 package Control getCaptorControl() { return captorControl; } | 56 package Control getCaptorControl() { return captorControl; } |
57 //}}} | |
58 | |
59 //{{{ focusedControl | |
60 Control focusedControl; | |
61 package void setFocusedControl(Control c) { | |
62 if(focusedControl is c) | |
63 return; | |
64 scope e = new EventArgs; | |
65 if(focusedControl) | |
66 focusedControl.focusLost(e); | |
67 focusedControl = c; | |
68 if(focusedControl) | |
69 focusedControl.focusGained(e); | |
70 } | |
71 package Control getFocusedControl() { return focusedControl; } | |
72 //}}} | |
53 | 73 |
54 /** | 74 /** |
55 * The painting event is an exception to the rule that added handlers are called | 75 * The painting event is an exception to the rule that added handlers are called |
56 * before whenPainting. The painting event is far more useful since | 76 * before whenPainting. The painting event is far more useful since |
57 * added handles are called after the control's whenPainting has finished. | 77 * added handles are called after the control's whenPainting has finished. |
203 e.graphics.paint(); | 223 e.graphics.paint(); |
204 } | 224 } |
205 /// This event occurs when the control needs to be painted. | 225 /// This event occurs when the control needs to be painted. |
206 Event!(whenPainting) painting; | 226 Event!(whenPainting) painting; |
207 | 227 |
228 /// Override this method in a subclass to handle the focusGained event. | |
229 protected void whenFocusGained(EventArgs e) { | |
230 repaint(); | |
231 } | |
232 /// This event occurs after this control is focused. | |
233 Event!(whenFocusGained) focusGained; | |
234 | |
235 /// Override this method in a subclass to handle the focusLost event. | |
236 protected void whenFocusLost(EventArgs e) { | |
237 repaint(); | |
238 } | |
239 /// This event occurs after this control loses focus. | |
240 Event!(whenFocusLost) focusLost; | |
241 | |
208 this() { | 242 this() { |
209 moved.mainHandler = &whenMoved; | 243 moved.mainHandler = &whenMoved; |
210 resized.mainHandler = &whenResized; | 244 resized.mainHandler = &whenResized; |
211 mouseEntered.mainHandler = &whenMouseEntered; | 245 mouseEntered.mainHandler = &whenMouseEntered; |
212 mouseEntered.dispatcher = &dispatchMouseEntered; | 246 mouseEntered.dispatcher = &dispatchMouseEntered; |
227 keyTyped.dispatcher = &dispatchKeyTyped; | 261 keyTyped.dispatcher = &dispatchKeyTyped; |
228 keyUp.mainHandler = &whenKeyUp; | 262 keyUp.mainHandler = &whenKeyUp; |
229 keyUp.dispatcher = &dispatchKeyUp; | 263 keyUp.dispatcher = &dispatchKeyUp; |
230 painting.mainHandler = &whenPainting; | 264 painting.mainHandler = &whenPainting; |
231 painting.dispatcher = &dispatchPainting; | 265 painting.dispatcher = &dispatchPainting; |
266 focusGained.mainHandler = &whenFocusGained; | |
267 focusLost.mainHandler = &whenFocusLost; | |
232 | 268 |
233 _location = Point(0, 0); | 269 _location = Point(0, 0); |
234 _size = Size(100, 100); | 270 _size = Size(100, 100); |
235 _text = ""; | 271 _text = ""; |
236 _focusable = false; | 272 _focusable = false; |
275 dg(g); | 311 dg(g); |
276 delete g; | 312 delete g; |
277 } | 313 } |
278 | 314 |
279 /** | 315 /** |
316 * Returns whether or not this control can receive focus. | |
317 */ | |
318 bool focusable() { | |
319 return _focusable; | |
320 } | |
321 void focusable(bool f) { | |
322 _focusable = f; | |
323 // TODO: | |
324 } | |
325 | |
326 /** | |
327 * Returns whether this control currently has focus. A control with focus | |
328 * receives keyboard events. | |
329 */ | |
330 bool focused() { | |
331 return getFocusedControl() is this; | |
332 } | |
333 | |
334 /** | |
335 * Returns true if this control should visually show when it has focus | |
336 * and returns false if not. Focus is usually hidden until the | |
337 * user uses the keyboard to navigate. | |
280 * | 338 * |
281 */ | 339 * A text box is one control that shows when it is |
282 bool focused() { | 340 * focused (by its caret), regardless of this value. (Because showing |
283 return _focused; | 341 * focus isn't the sole purpose of a caret.) |
342 */ | |
343 bool showFocus() { | |
344 auto top = getTopLevel(); | |
345 return top && (cast(Window)top).showFocus; | |
284 } | 346 } |
285 | 347 |
286 /** | 348 /** |
287 * Gets or sets this control's tab index. The tab index of controls | 349 * Gets or sets this control's tab index. The tab index of controls |
288 * decides in what order they are focused when the tab key is pressed. | 350 * decides in what order they are focused when the tab key is pressed. |
305 if(!_focusable) | 367 if(!_focusable) |
306 return; | 368 return; |
307 auto top = getTopLevel(); | 369 auto top = getTopLevel(); |
308 if(!top) | 370 if(!top) |
309 return; | 371 return; |
310 if(auto win = cast(Window)top) { | 372 if(auto win = cast(Window)top) |
311 if(win.focusedControl) { | |
312 win.focusedControl._focused = false; | |
313 win.focusedControl.repaint(); | |
314 } | |
315 win.focusedControl = this; | 373 win.focusedControl = this; |
316 _focused = true; | |
317 repaint(); | |
318 } | |
319 } | 374 } |
320 | 375 |
321 /** | 376 /** |
322 * Returns whether this control is on the screen. A control is | 377 * Returns whether this control is on the screen. A control is |
323 * on screen if one of its ancestors is a top level window. Whether or | 378 * on screen if one of its ancestors is a top level window. Whether or |