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;
|
|
48 void collectFromPanel(Panel container) {
|
|
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;
|
|
57 } else if(auto c = cast(Panel)control)
|
|
58 collectFromPanel(c);
|
|
59 }
|
|
60 }
|
|
61 checkedIndex = -1;
|
|
62 collectFromPanel(topLevel.content);
|
|
63 return radios;
|
|
64 }
|
|
65 override void whenKeyDown(KeyEventArgs e) {
|
|
66 // TODO: when GetTopLevel() is changed to return NativeControl,
|
|
67 // update this
|
|
68 int index;
|
|
69 RadioButton[] radios = collectGroup(index);
|
|
70 if(radios is null)
|
|
71 return;
|
|
72 if(e.key == Key.Down || e.key == Key.Right) {
|
|
73 if(++index >= radios.length)
|
|
74 index = 0;
|
|
75 } else if(e.key == Key.Up || e.key == Key.Left) {
|
|
76 if(--index < 0)
|
|
77 index = radios.length-1;
|
|
78 }
|
|
79 radios[index].clicked(new EventArgs);
|
|
80 }
|
|
81 override void whenPainting(PaintingEventArgs e) {
|
|
82 Theme.current.RadioButton_paint(this, e.graphics);
|
|
83 }
|
|
84 override void whenClicked(EventArgs e) {
|
|
85 int index;
|
|
86 RadioButton[] radios = collectGroup(index);
|
|
87 foreach(r; radios)
|
|
88 r.checked = false;
|
|
89 checked = true;
|
|
90 focus();
|
|
91
|
|
92 }
|
|
93 public:
|
|
94 this() {
|
|
95 }
|
|
96 this(string text) {
|
|
97 this();
|
|
98 this.text = text;
|
|
99 }
|
|
100 override Size bestSize() {
|
|
101 return Size(70, 15);
|
|
102 }
|
|
103 /**
|
|
104 * Gets or sets what group this radio button is a part of. The default is 1.
|
|
105 */
|
|
106 int group() { return _group; }
|
|
107 /// ditto
|
|
108 void group(int i) { _group = i; }
|
|
109 }
|