Mercurial > projects > dynamin
annotate dynamin/gui/check_box.d @ 78:651082a9b364
Add Event.setUp() and use in place of mainHandler and dispatcher.
author | Jordan Miner <jminer7@gmail.com> |
---|---|
date | Sat, 06 Feb 2010 15:12:52 -0600 |
parents | 6580fabb7dce |
children | 73060bc3f004 |
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) 2007-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.check_box; | |
27 | |
28 import dynamin.all_core; | |
29 import dynamin.all_gui; | |
30 import dynamin.all_painting; | |
31 import tango.io.Stdout; | |
32 | |
33 enum CheckState { | |
34 /// | |
35 Unchecked, | |
36 /// | |
37 Checked, | |
38 /// | |
39 Both | |
40 } | |
41 | |
42 /** | |
43 * A control that can be checked or unchecked independent of other controls. | |
44 * | |
45 * The appearance of a check box with Windows Classic: | |
46 * | |
47 * $(IMAGE ../web/example_check_box.png) | |
48 */ | |
49 class CheckBox : Button { | |
50 protected: | |
51 CheckState _checkState = CheckState.Unchecked; | |
52 override void whenClicked(EventArgs e) { | |
53 checked = !checked; | |
54 focus(); | |
55 } | |
56 override void whenPainting(PaintingEventArgs e) { | |
57 Theme.current.CheckBox_paint(this, e.graphics); | |
58 } | |
59 | |
60 public: | |
10
ccc108b25a0a
Convert to using a struct for events. Fix some comments too.
Jordan Miner <jminer7@gmail.com>
parents:
8
diff
changeset
|
61 /// Override this method in a subclass to handle the checkedChanged event. |
8
b621b528823d
whenXX methods have to come before the event if switched to template mixins.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
62 protected void whenCheckedChanged(EventArgs e) { } |
0 | 63 /// This event occurs after . |
10
ccc108b25a0a
Convert to using a struct for events. Fix some comments too.
Jordan Miner <jminer7@gmail.com>
parents:
8
diff
changeset
|
64 Event!(whenCheckedChanged) checkedChanged; |
0 | 65 this() { |
78
651082a9b364
Add Event.setUp() and use in place of mainHandler and dispatcher.
Jordan Miner <jminer7@gmail.com>
parents:
68
diff
changeset
|
66 checkedChanged.setUp(&whenCheckedChanged); |
0 | 67 _focusable = true; |
68 } | |
69 this(string text) { | |
70 this(); | |
71 this.text = text; | |
72 } | |
51 | 73 /// Gets or sets whether this check box is checked. |
0 | 74 bool checked() { |
75 return _checkState == CheckState.Checked; | |
76 } | |
51 | 77 /// ditto |
0 | 78 void checked(bool b) { |
68
6580fabb7dce
Setting a property to the value it already is shouldn't do anything
Jordan Miner <jminer7@gmail.com>
parents:
51
diff
changeset
|
79 auto old = _checkState; |
0 | 80 _checkState = b ? CheckState.Checked : CheckState.Unchecked; |
68
6580fabb7dce
Setting a property to the value it already is shouldn't do anything
Jordan Miner <jminer7@gmail.com>
parents:
51
diff
changeset
|
81 if(_checkState == old) |
6580fabb7dce
Setting a property to the value it already is shouldn't do anything
Jordan Miner <jminer7@gmail.com>
parents:
51
diff
changeset
|
82 return; |
0 | 83 repaint(); |
84 checkedChanged(new EventArgs); | |
85 } | |
86 override Size bestSize() { | |
87 return Size(70, 15); | |
88 } | |
89 override void paintFore(Graphics g) { | |
90 g.drawText(text, 0, (height-g.getTextExtents(text).height)/2); | |
91 } | |
92 } | |
93 |