Commit | Line | Data |
---|---|---|
daa4106c | 1 | /* |
00d2622b KL |
2 | * Jexer - Java Text User Interface |
3 | * | |
e16dda65 | 4 | * The MIT License (MIT) |
00d2622b | 5 | * |
a69ed767 | 6 | * Copyright (C) 2019 Kevin Lamonte |
00d2622b | 7 | * |
e16dda65 KL |
8 | * Permission is hereby granted, free of charge, to any person obtaining a |
9 | * copy of this software and associated documentation files (the "Software"), | |
10 | * to deal in the Software without restriction, including without limitation | |
11 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, | |
12 | * and/or sell copies of the Software, and to permit persons to whom the | |
13 | * Software is furnished to do so, subject to the following conditions: | |
00d2622b | 14 | * |
e16dda65 KL |
15 | * The above copyright notice and this permission notice shall be included in |
16 | * all copies or substantial portions of the Software. | |
00d2622b | 17 | * |
e16dda65 KL |
18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL | |
21 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | |
23 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER | |
24 | * DEALINGS IN THE SOFTWARE. | |
00d2622b KL |
25 | * |
26 | * @author Kevin Lamonte [kevin.lamonte@gmail.com] | |
27 | * @version 1 | |
28 | */ | |
29 | package jexer; | |
30 | ||
31 | import jexer.bits.CellAttributes; | |
32 | import jexer.bits.GraphicsChars; | |
00691e80 | 33 | import jexer.bits.MnemonicString; |
9f613a0c | 34 | import jexer.bits.StringUtils; |
00d2622b KL |
35 | import jexer.event.TKeypressEvent; |
36 | import jexer.event.TMouseEvent; | |
37 | import static jexer.TKeypress.*; | |
38 | ||
39 | /** | |
40 | * TRadioButton implements a selectable radio button. | |
00691e80 KL |
41 | * |
42 | * If the user clicks or presses space on this button, it is selected. | |
43 | * | |
44 | * If the user presses escape on this button, it is unselected. | |
00d2622b | 45 | */ |
051e2913 | 46 | public class TRadioButton extends TWidget { |
00d2622b | 47 | |
d36057df KL |
48 | // ------------------------------------------------------------------------ |
49 | // Variables -------------------------------------------------------------- | |
50 | // ------------------------------------------------------------------------ | |
51 | ||
00d2622b KL |
52 | /** |
53 | * RadioButton state, true means selected. | |
54 | */ | |
55 | private boolean selected = false; | |
56 | ||
00d2622b | 57 | /** |
00691e80 | 58 | * The shortcut and radio button label. |
00d2622b | 59 | */ |
00691e80 | 60 | private MnemonicString mnemonic; |
00d2622b KL |
61 | |
62 | /** | |
63 | * ID for this radio button. Buttons start counting at 1 in the | |
64 | * RadioGroup. | |
65 | */ | |
66 | private int id; | |
67 | ||
d36057df KL |
68 | // ------------------------------------------------------------------------ |
69 | // Constructors ----------------------------------------------------------- | |
70 | // ------------------------------------------------------------------------ | |
00d2622b KL |
71 | |
72 | /** | |
73 | * Public constructor. | |
74 | * | |
75 | * @param parent parent widget | |
76 | * @param x column relative to parent | |
77 | * @param y row relative to parent | |
78 | * @param label label to display next to (right of) the radiobutton | |
79 | * @param id ID for this radio button | |
80 | */ | |
81 | public TRadioButton(final TRadioGroup parent, final int x, final int y, | |
82 | final String label, final int id) { | |
83 | ||
84 | // Set parent and window | |
9f613a0c | 85 | super(parent, x, y, StringUtils.width(label) + 4, 1); |
00d2622b | 86 | |
00691e80 | 87 | mnemonic = new MnemonicString(label); |
00d2622b KL |
88 | this.id = id; |
89 | ||
7c870d89 | 90 | setCursorVisible(true); |
00d2622b KL |
91 | setCursorX(1); |
92 | } | |
93 | ||
d36057df KL |
94 | // ------------------------------------------------------------------------ |
95 | // Event handlers --------------------------------------------------------- | |
96 | // ------------------------------------------------------------------------ | |
97 | ||
00d2622b KL |
98 | /** |
99 | * Returns true if the mouse is currently on the radio button. | |
100 | * | |
101 | * @param mouse mouse event | |
102 | * @return if true the mouse is currently on the radio button | |
103 | */ | |
104 | private boolean mouseOnRadioButton(final TMouseEvent mouse) { | |
105 | if ((mouse.getY() == 0) | |
106 | && (mouse.getX() >= 0) | |
107 | && (mouse.getX() <= 2) | |
108 | ) { | |
109 | return true; | |
110 | } | |
111 | return false; | |
112 | } | |
113 | ||
00d2622b KL |
114 | /** |
115 | * Handle mouse button presses. | |
116 | * | |
117 | * @param mouse mouse button press event | |
118 | */ | |
119 | @Override | |
120 | public void onMouseDown(final TMouseEvent mouse) { | |
7c870d89 | 121 | if ((mouseOnRadioButton(mouse)) && (mouse.isMouse1())) { |
00d2622b | 122 | // Switch state |
00691e80 KL |
123 | selected = true; |
124 | ((TRadioGroup) getParent()).setSelected(this); | |
00d2622b KL |
125 | } |
126 | } | |
127 | ||
128 | /** | |
129 | * Handle keystrokes. | |
130 | * | |
131 | * @param keypress keystroke event | |
132 | */ | |
133 | @Override | |
134 | public void onKeypress(final TKeypressEvent keypress) { | |
135 | ||
136 | if (keypress.equals(kbSpace)) { | |
00691e80 KL |
137 | selected = true; |
138 | ((TRadioGroup) getParent()).setSelected(this); | |
139 | return; | |
140 | } | |
141 | ||
142 | if (keypress.equals(kbEsc)) { | |
143 | TRadioGroup parent = (TRadioGroup) getParent(); | |
144 | if (parent.requiresSelection == false) { | |
145 | selected = false; | |
146 | parent.setSelected(0); | |
00d2622b KL |
147 | } |
148 | return; | |
149 | } | |
150 | ||
151 | // Pass to parent for the things we don't care about. | |
152 | super.onKeypress(keypress); | |
153 | } | |
154 | ||
d36057df KL |
155 | // ------------------------------------------------------------------------ |
156 | // TWidget ---------------------------------------------------------------- | |
157 | // ------------------------------------------------------------------------ | |
158 | ||
159 | /** | |
160 | * Draw a radio button with label. | |
161 | */ | |
162 | @Override | |
163 | public void draw() { | |
164 | CellAttributes radioButtonColor; | |
00691e80 | 165 | CellAttributes mnemonicColor; |
d36057df KL |
166 | |
167 | if (isAbsoluteActive()) { | |
168 | radioButtonColor = getTheme().getColor("tradiobutton.active"); | |
00691e80 | 169 | mnemonicColor = getTheme().getColor("tradiobutton.mnemonic.highlighted"); |
d36057df KL |
170 | } else { |
171 | radioButtonColor = getTheme().getColor("tradiobutton.inactive"); | |
00691e80 | 172 | mnemonicColor = getTheme().getColor("tradiobutton.mnemonic"); |
d36057df KL |
173 | } |
174 | ||
a69ed767 | 175 | putCharXY(0, 0, '(', radioButtonColor); |
d36057df | 176 | if (selected) { |
a69ed767 | 177 | putCharXY(1, 0, GraphicsChars.CP437[0x07], radioButtonColor); |
d36057df | 178 | } else { |
a69ed767 | 179 | putCharXY(1, 0, ' ', radioButtonColor); |
d36057df | 180 | } |
a69ed767 | 181 | putCharXY(2, 0, ')', radioButtonColor); |
00691e80 | 182 | putStringXY(4, 0, mnemonic.getRawLabel(), radioButtonColor); |
9f613a0c KL |
183 | if (mnemonic.getScreenShortcutIdx() >= 0) { |
184 | putCharXY(4 + mnemonic.getScreenShortcutIdx(), 0, | |
00691e80 KL |
185 | mnemonic.getShortcut(), mnemonicColor); |
186 | } | |
d36057df KL |
187 | } |
188 | ||
189 | // ------------------------------------------------------------------------ | |
190 | // TRadioButton ----------------------------------------------------------- | |
191 | // ------------------------------------------------------------------------ | |
192 | ||
193 | /** | |
194 | * Get RadioButton state, true means selected. | |
195 | * | |
196 | * @return if true then this is the one button in the group that is | |
197 | * selected | |
198 | */ | |
199 | public boolean isSelected() { | |
200 | return selected; | |
201 | } | |
202 | ||
203 | /** | |
204 | * Set RadioButton state, true means selected. Note package private | |
205 | * access. | |
206 | * | |
207 | * @param selected if true then this is the one button in the group that | |
208 | * is selected | |
209 | */ | |
210 | void setSelected(final boolean selected) { | |
211 | this.selected = selected; | |
212 | } | |
213 | ||
214 | /** | |
215 | * Get ID for this radio button. Buttons start counting at 1 in the | |
216 | * RadioGroup. | |
217 | * | |
218 | * @return the ID | |
219 | */ | |
220 | public int getId() { | |
221 | return id; | |
222 | } | |
223 | ||
00691e80 KL |
224 | /** |
225 | * Get the mnemonic string for this button. | |
226 | * | |
227 | * @return mnemonic string | |
228 | */ | |
229 | public MnemonicString getMnemonic() { | |
230 | return mnemonic; | |
231 | } | |
232 | ||
00d2622b | 233 | } |