Mercurial > projects > dynamin
annotate dynamin/gui/radio_button.d @ 113:4636a64afabc default tip
Add reverse() function.
author | Jordan Miner <jminer7@gmail.com> |
---|---|
date | Sat, 19 Jan 2013 21:08:52 -0600 |
parents | 73060bc3f004 |
children |
rev | line source |
---|---|
0 | 1 |
2 /* | |
103
73060bc3f004
Change license to Boost 1.0 and MPL 2.0.
Jordan Miner <jminer7@gmail.com>
parents:
65
diff
changeset
|
3 * Copyright Jordan Miner |
0 | 4 * |
103
73060bc3f004
Change license to Boost 1.0 and MPL 2.0.
Jordan Miner <jminer7@gmail.com>
parents:
65
diff
changeset
|
5 * This Source Code Form is subject to the terms of the Mozilla Public |
73060bc3f004
Change license to Boost 1.0 and MPL 2.0.
Jordan Miner <jminer7@gmail.com>
parents:
65
diff
changeset
|
6 * License, v. 2.0. If a copy of the MPL was not distributed with this |
73060bc3f004
Change license to Boost 1.0 and MPL 2.0.
Jordan Miner <jminer7@gmail.com>
parents:
65
diff
changeset
|
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/. |
0 | 8 * |
9 */ | |
10 | |
11 module dynamin.gui.radio_button; | |
12 | |
13 import dynamin.all_core; | |
14 import dynamin.all_gui; | |
15 import dynamin.all_painting; | |
16 import tango.io.Stdout; | |
17 | |
18 /** | |
19 * A control that can be checked only if other radio buttons are unchecked. | |
20 * | |
21 * The appearance of a radio button with Windows Classic: | |
22 * | |
23 * $(IMAGE ../web/example_radio_button.png) | |
24 */ | |
25 class RadioButton : CheckBox { | |
26 protected: | |
27 int _group = 1; | |
28 RadioButton[] collectGroup(ref int checkedIndex) { | |
29 Window topLevel = cast(Window)getTopLevel(); | |
30 if(!topLevel) | |
31 return null; | |
32 RadioButton[] radios; | |
43
6b264c92da34
Rename collectFromPanel() to collectFromContainer()
Jordan Miner <jminer7@gmail.com>
parents:
35
diff
changeset
|
33 void collectFromContainer(Container container) { |
0 | 34 foreach(control; container) { |
35 if(auto r = cast(RadioButton)control) { | |
36 if(r.group != group) | |
37 continue; | |
38 radios.length = radios.length + 1; | |
39 radios[$-1] = r; | |
40 if(r.checked) | |
41 checkedIndex = radios.length-1; | |
35
ee9a564d2814
Allow more use of Containers versus Panels.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
42 } else if(auto c = cast(Container)control) |
43
6b264c92da34
Rename collectFromPanel() to collectFromContainer()
Jordan Miner <jminer7@gmail.com>
parents:
35
diff
changeset
|
43 collectFromContainer(c); |
0 | 44 } |
45 } | |
46 checkedIndex = -1; | |
43
6b264c92da34
Rename collectFromPanel() to collectFromContainer()
Jordan Miner <jminer7@gmail.com>
parents:
35
diff
changeset
|
47 collectFromContainer(topLevel.content); |
0 | 48 return radios; |
49 } | |
50 override void whenKeyDown(KeyEventArgs e) { | |
53
6e33b00595e9
Fix capitalization in a couple comments.
Jordan Miner <jminer7@gmail.com>
parents:
48
diff
changeset
|
51 // TODO: when getTopLevel() is changed to return NativeControl, |
0 | 52 // update this |
53 int index; | |
54 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
|
55 RadioButton[] radios = collectGroup(index); |
41b141453b8e
Fix crash in RadioButton.whenKeyDown()
Jordan Miner <jminer7@gmail.com>
parents:
43
diff
changeset
|
56 if(radios is null) |
41b141453b8e
Fix crash in RadioButton.whenKeyDown()
Jordan Miner <jminer7@gmail.com>
parents:
43
diff
changeset
|
57 return; |
0 | 58 if(++index >= radios.length) |
59 index = 0; | |
48
41b141453b8e
Fix crash in RadioButton.whenKeyDown()
Jordan Miner <jminer7@gmail.com>
parents:
43
diff
changeset
|
60 radios[index].clicked(new EventArgs); |
41b141453b8e
Fix crash in RadioButton.whenKeyDown()
Jordan Miner <jminer7@gmail.com>
parents:
43
diff
changeset
|
61 e.stopped = true; |
0 | 62 } 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
|
63 RadioButton[] radios = collectGroup(index); |
41b141453b8e
Fix crash in RadioButton.whenKeyDown()
Jordan Miner <jminer7@gmail.com>
parents:
43
diff
changeset
|
64 if(radios is null) |
41b141453b8e
Fix crash in RadioButton.whenKeyDown()
Jordan Miner <jminer7@gmail.com>
parents:
43
diff
changeset
|
65 return; |
0 | 66 if(--index < 0) |
67 index = radios.length-1; | |
48
41b141453b8e
Fix crash in RadioButton.whenKeyDown()
Jordan Miner <jminer7@gmail.com>
parents:
43
diff
changeset
|
68 radios[index].clicked(new EventArgs); |
41b141453b8e
Fix crash in RadioButton.whenKeyDown()
Jordan Miner <jminer7@gmail.com>
parents:
43
diff
changeset
|
69 e.stopped = true; |
0 | 70 } |
71 } | |
72 override void whenPainting(PaintingEventArgs e) { | |
73 Theme.current.RadioButton_paint(this, e.graphics); | |
74 } | |
75 override void whenClicked(EventArgs e) { | |
76 int index; | |
77 RadioButton[] radios = collectGroup(index); | |
78 foreach(r; radios) | |
79 r.checked = false; | |
80 checked = true; | |
81 focus(); | |
82 | |
83 } | |
84 public: | |
85 this() { | |
55
c138461bf845
Add focusing and other changes that are related
Jordan Miner <jminer7@gmail.com>
parents:
53
diff
changeset
|
86 _focusable = false; |
0 | 87 } |
88 this(string text) { | |
89 this(); | |
90 this.text = text; | |
91 } | |
92 override Size bestSize() { | |
93 return Size(70, 15); | |
94 } | |
55
c138461bf845
Add focusing and other changes that are related
Jordan Miner <jminer7@gmail.com>
parents:
53
diff
changeset
|
95 alias CheckBox.checked checked; |
c138461bf845
Add focusing and other changes that are related
Jordan Miner <jminer7@gmail.com>
parents:
53
diff
changeset
|
96 override void checked(bool b) { |
c138461bf845
Add focusing and other changes that are related
Jordan Miner <jminer7@gmail.com>
parents:
53
diff
changeset
|
97 focusable = b; |
c138461bf845
Add focusing and other changes that are related
Jordan Miner <jminer7@gmail.com>
parents:
53
diff
changeset
|
98 super.checked(b); |
c138461bf845
Add focusing and other changes that are related
Jordan Miner <jminer7@gmail.com>
parents:
53
diff
changeset
|
99 } |
0 | 100 /** |
101 * Gets or sets what group this radio button is a part of. The default is 1. | |
102 */ | |
103 int group() { return _group; } | |
104 /// ditto | |
105 void group(int i) { _group = i; } | |
106 } | |
65
7c5c50a06ffe
Add setGroup() for RadioButtons
Jordan Miner <jminer7@gmail.com>
parents:
55
diff
changeset
|
107 |
7c5c50a06ffe
Add setGroup() for RadioButtons
Jordan Miner <jminer7@gmail.com>
parents:
55
diff
changeset
|
108 /** |
7c5c50a06ffe
Add setGroup() for RadioButtons
Jordan Miner <jminer7@gmail.com>
parents:
55
diff
changeset
|
109 * Simply sets every radio button to have the specified group. |
7c5c50a06ffe
Add setGroup() for RadioButtons
Jordan Miner <jminer7@gmail.com>
parents:
55
diff
changeset
|
110 * |
7c5c50a06ffe
Add setGroup() for RadioButtons
Jordan Miner <jminer7@gmail.com>
parents:
55
diff
changeset
|
111 * Example: |
7c5c50a06ffe
Add setGroup() for RadioButtons
Jordan Miner <jminer7@gmail.com>
parents:
55
diff
changeset
|
112 * ----- |
7c5c50a06ffe
Add setGroup() for RadioButtons
Jordan Miner <jminer7@gmail.com>
parents:
55
diff
changeset
|
113 * auto radioButton1 = new RadioButton; |
7c5c50a06ffe
Add setGroup() for RadioButtons
Jordan Miner <jminer7@gmail.com>
parents:
55
diff
changeset
|
114 * auto radioButton2 = new RadioButton; |
7c5c50a06ffe
Add setGroup() for RadioButtons
Jordan Miner <jminer7@gmail.com>
parents:
55
diff
changeset
|
115 * setGroup(7, radioButton1, radioButton2); |
7c5c50a06ffe
Add setGroup() for RadioButtons
Jordan Miner <jminer7@gmail.com>
parents:
55
diff
changeset
|
116 * ----- |
7c5c50a06ffe
Add setGroup() for RadioButtons
Jordan Miner <jminer7@gmail.com>
parents:
55
diff
changeset
|
117 */ |
7c5c50a06ffe
Add setGroup() for RadioButtons
Jordan Miner <jminer7@gmail.com>
parents:
55
diff
changeset
|
118 void setGroup(int group, RadioButton[] radios...) { |
7c5c50a06ffe
Add setGroup() for RadioButtons
Jordan Miner <jminer7@gmail.com>
parents:
55
diff
changeset
|
119 foreach(r; radios) |
7c5c50a06ffe
Add setGroup() for RadioButtons
Jordan Miner <jminer7@gmail.com>
parents:
55
diff
changeset
|
120 r.group = group; |
7c5c50a06ffe
Add setGroup() for RadioButtons
Jordan Miner <jminer7@gmail.com>
parents:
55
diff
changeset
|
121 } |
7c5c50a06ffe
Add setGroup() for RadioButtons
Jordan Miner <jminer7@gmail.com>
parents:
55
diff
changeset
|
122 |