Mercurial > projects > dynamin
annotate dynamin/gui/radio_button.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 | 7c5c50a06ffe |
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.radio_button; | |
27 | |
28 import dynamin.all_core; | |
29 import dynamin.all_gui; | |
30 import dynamin.all_painting; | |
31 import tango.io.Stdout; | |
32 | |
33 /** | |
34 * A control that can be checked only if other radio buttons are unchecked. | |
35 * | |
36 * The appearance of a radio button with Windows Classic: | |
37 * | |
38 * $(IMAGE ../web/example_radio_button.png) | |
39 */ | |
40 class RadioButton : CheckBox { | |
41 protected: | |
42 int _group = 1; | |
43 RadioButton[] collectGroup(ref int checkedIndex) { | |
44 Window topLevel = cast(Window)getTopLevel(); | |
45 if(!topLevel) | |
46 return null; | |
47 RadioButton[] radios; | |
43
6b264c92da34
Rename collectFromPanel() to collectFromContainer()
Jordan Miner <jminer7@gmail.com>
parents:
35
diff
changeset
|
48 void collectFromContainer(Container container) { |
0 | 49 foreach(control; container) { |
50 if(auto r = cast(RadioButton)control) { | |
51 if(r.group != group) | |
52 continue; | |
53 radios.length = radios.length + 1; | |
54 radios[$-1] = r; | |
55 if(r.checked) | |
56 checkedIndex = radios.length-1; | |
35
ee9a564d2814
Allow more use of Containers versus Panels.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
57 } else if(auto c = cast(Container)control) |
43
6b264c92da34
Rename collectFromPanel() to collectFromContainer()
Jordan Miner <jminer7@gmail.com>
parents:
35
diff
changeset
|
58 collectFromContainer(c); |
0 | 59 } |
60 } | |
61 checkedIndex = -1; | |
43
6b264c92da34
Rename collectFromPanel() to collectFromContainer()
Jordan Miner <jminer7@gmail.com>
parents:
35
diff
changeset
|
62 collectFromContainer(topLevel.content); |
0 | 63 return radios; |
64 } | |
65 override void whenKeyDown(KeyEventArgs e) { | |
53
6e33b00595e9
Fix capitalization in a couple comments.
Jordan Miner <jminer7@gmail.com>
parents:
48
diff
changeset
|
66 // TODO: when getTopLevel() is changed to return NativeControl, |
0 | 67 // update this |
68 int index; | |
69 if(e.key == Key.Down || e.key == Key.Right) { | |
48
41b141453b8e
Fix crash in RadioButton.whenKeyDown()
Jordan Miner <jminer7@gmail.com>
parents:
43
diff
changeset
|
70 RadioButton[] radios = collectGroup(index); |
41b141453b8e
Fix crash in RadioButton.whenKeyDown()
Jordan Miner <jminer7@gmail.com>
parents:
43
diff
changeset
|
71 if(radios is null) |
41b141453b8e
Fix crash in RadioButton.whenKeyDown()
Jordan Miner <jminer7@gmail.com>
parents:
43
diff
changeset
|
72 return; |
0 | 73 if(++index >= radios.length) |
74 index = 0; | |
48
41b141453b8e
Fix crash in RadioButton.whenKeyDown()
Jordan Miner <jminer7@gmail.com>
parents:
43
diff
changeset
|
75 radios[index].clicked(new EventArgs); |
41b141453b8e
Fix crash in RadioButton.whenKeyDown()
Jordan Miner <jminer7@gmail.com>
parents:
43
diff
changeset
|
76 e.stopped = true; |
0 | 77 } else if(e.key == Key.Up || e.key == Key.Left) { |
48
41b141453b8e
Fix crash in RadioButton.whenKeyDown()
Jordan Miner <jminer7@gmail.com>
parents:
43
diff
changeset
|
78 RadioButton[] radios = collectGroup(index); |
41b141453b8e
Fix crash in RadioButton.whenKeyDown()
Jordan Miner <jminer7@gmail.com>
parents:
43
diff
changeset
|
79 if(radios is null) |
41b141453b8e
Fix crash in RadioButton.whenKeyDown()
Jordan Miner <jminer7@gmail.com>
parents:
43
diff
changeset
|
80 return; |
0 | 81 if(--index < 0) |
82 index = radios.length-1; | |
48
41b141453b8e
Fix crash in RadioButton.whenKeyDown()
Jordan Miner <jminer7@gmail.com>
parents:
43
diff
changeset
|
83 radios[index].clicked(new EventArgs); |
41b141453b8e
Fix crash in RadioButton.whenKeyDown()
Jordan Miner <jminer7@gmail.com>
parents:
43
diff
changeset
|
84 e.stopped = true; |
0 | 85 } |
86 } | |
87 override void whenPainting(PaintingEventArgs e) { | |
88 Theme.current.RadioButton_paint(this, e.graphics); | |
89 } | |
90 override void whenClicked(EventArgs e) { | |
91 int index; | |
92 RadioButton[] radios = collectGroup(index); | |
93 foreach(r; radios) | |
94 r.checked = false; | |
95 checked = true; | |
96 focus(); | |
97 | |
98 } | |
99 public: | |
100 this() { | |
55
c138461bf845
Add focusing and other changes that are related
Jordan Miner <jminer7@gmail.com>
parents:
53
diff
changeset
|
101 _focusable = false; |
0 | 102 } |
103 this(string text) { | |
104 this(); | |
105 this.text = text; | |
106 } | |
107 override Size bestSize() { | |
108 return Size(70, 15); | |
109 } | |
55
c138461bf845
Add focusing and other changes that are related
Jordan Miner <jminer7@gmail.com>
parents:
53
diff
changeset
|
110 alias CheckBox.checked checked; |
c138461bf845
Add focusing and other changes that are related
Jordan Miner <jminer7@gmail.com>
parents:
53
diff
changeset
|
111 override void checked(bool b) { |
c138461bf845
Add focusing and other changes that are related
Jordan Miner <jminer7@gmail.com>
parents:
53
diff
changeset
|
112 focusable = b; |
c138461bf845
Add focusing and other changes that are related
Jordan Miner <jminer7@gmail.com>
parents:
53
diff
changeset
|
113 super.checked(b); |
c138461bf845
Add focusing and other changes that are related
Jordan Miner <jminer7@gmail.com>
parents:
53
diff
changeset
|
114 } |
0 | 115 /** |
116 * Gets or sets what group this radio button is a part of. The default is 1. | |
117 */ | |
118 int group() { return _group; } | |
119 /// ditto | |
120 void group(int i) { _group = i; } | |
121 } |