Commit | Line | Data |
---|---|---|
daa4106c | 1 | /* |
30d336cc KL |
2 | * Jexer - Java Text User Interface |
3 | * | |
e16dda65 | 4 | * The MIT License (MIT) |
30d336cc | 5 | * |
a69ed767 | 6 | * Copyright (C) 2019 Kevin Lamonte |
30d336cc | 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: | |
30d336cc | 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. | |
30d336cc | 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. | |
30d336cc 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.Color; | |
33 | import jexer.bits.GraphicsChars; | |
34 | import jexer.bits.MnemonicString; | |
35 | import jexer.event.TKeypressEvent; | |
36 | import jexer.event.TMouseEvent; | |
a39c5665 KL |
37 | import static jexer.TKeypress.kbEnter; |
38 | import static jexer.TKeypress.kbSpace; | |
30d336cc KL |
39 | |
40 | /** | |
41 | * TButton implements a simple button. To make the button do something, pass | |
42 | * a TAction class to its constructor. | |
43 | * | |
44 | * @see TAction#DO() | |
45 | */ | |
051e2913 | 46 | public class TButton extends TWidget { |
30d336cc | 47 | |
d36057df KL |
48 | // ------------------------------------------------------------------------ |
49 | // Variables -------------------------------------------------------------- | |
50 | // ------------------------------------------------------------------------ | |
51 | ||
30d336cc KL |
52 | /** |
53 | * The shortcut and button text. | |
54 | */ | |
55 | private MnemonicString mnemonic; | |
56 | ||
57 | /** | |
58 | * Remember mouse state. | |
59 | */ | |
60 | private TMouseEvent mouse; | |
61 | ||
62 | /** | |
63 | * True when the button is being pressed and held down. | |
64 | */ | |
65 | private boolean inButtonPress = false; | |
66 | ||
67 | /** | |
68 | * The action to perform when the button is clicked. | |
69 | */ | |
70 | private TAction action; | |
71 | ||
e23ea538 | 72 | /** |
a39c5665 KL |
73 | * The background color used for the button "shadow", or null for "no |
74 | * shadow". | |
e23ea538 KL |
75 | */ |
76 | private CellAttributes shadowColor; | |
a39c5665 | 77 | |
d36057df KL |
78 | // ------------------------------------------------------------------------ |
79 | // Constructors ----------------------------------------------------------- | |
80 | // ------------------------------------------------------------------------ | |
91c9a837 | 81 | |
30d336cc KL |
82 | /** |
83 | * Private constructor. | |
84 | * | |
85 | * @param parent parent widget | |
86 | * @param text label on the button | |
87 | * @param x column relative to parent | |
88 | * @param y row relative to parent | |
89 | */ | |
90 | private TButton(final TWidget parent, final String text, | |
91 | final int x, final int y) { | |
92 | ||
93 | // Set parent and window | |
94 | super(parent); | |
95 | ||
96 | mnemonic = new MnemonicString(text); | |
97 | ||
98 | setX(x); | |
99 | setY(y); | |
100 | setHeight(2); | |
101 | setWidth(mnemonic.getRawLabel().length() + 3); | |
e23ea538 KL |
102 | |
103 | shadowColor = new CellAttributes(); | |
104 | shadowColor.setTo(getWindow().getBackground()); | |
105 | shadowColor.setForeColor(Color.BLACK); | |
106 | shadowColor.setBold(false); | |
30d336cc KL |
107 | } |
108 | ||
109 | /** | |
110 | * Public constructor. | |
111 | * | |
112 | * @param parent parent widget | |
113 | * @param text label on the button | |
114 | * @param x column relative to parent | |
115 | * @param y row relative to parent | |
116 | * @param action to call when button is pressed | |
117 | */ | |
118 | public TButton(final TWidget parent, final String text, | |
119 | final int x, final int y, final TAction action) { | |
120 | ||
121 | this(parent, text, x, y); | |
122 | this.action = action; | |
123 | } | |
124 | ||
d36057df KL |
125 | // ------------------------------------------------------------------------ |
126 | // Event handlers --------------------------------------------------------- | |
127 | // ------------------------------------------------------------------------ | |
128 | ||
30d336cc KL |
129 | /** |
130 | * Returns true if the mouse is currently on the button. | |
131 | * | |
132 | * @return if true the mouse is currently on the button | |
133 | */ | |
134 | private boolean mouseOnButton() { | |
135 | int rightEdge = getWidth() - 1; | |
136 | if (inButtonPress) { | |
137 | rightEdge++; | |
138 | } | |
139 | if ((mouse != null) | |
140 | && (mouse.getY() == 0) | |
141 | && (mouse.getX() >= 0) | |
142 | && (mouse.getX() < rightEdge) | |
143 | ) { | |
144 | return true; | |
145 | } | |
146 | return false; | |
147 | } | |
148 | ||
30d336cc KL |
149 | /** |
150 | * Handle mouse button presses. | |
151 | * | |
152 | * @param mouse mouse button event | |
153 | */ | |
154 | @Override | |
155 | public void onMouseDown(final TMouseEvent mouse) { | |
156 | this.mouse = mouse; | |
157 | ||
7c870d89 | 158 | if ((mouseOnButton()) && (mouse.isMouse1())) { |
30d336cc KL |
159 | // Begin button press |
160 | inButtonPress = true; | |
161 | } | |
162 | } | |
163 | ||
164 | /** | |
165 | * Handle mouse button releases. | |
166 | * | |
167 | * @param mouse mouse button release event | |
168 | */ | |
169 | @Override | |
170 | public void onMouseUp(final TMouseEvent mouse) { | |
171 | this.mouse = mouse; | |
172 | ||
7c870d89 | 173 | if (inButtonPress && mouse.isMouse1()) { |
30d336cc | 174 | // Dispatch the event |
a325f111 | 175 | dispatch(); |
30d336cc KL |
176 | } |
177 | ||
178 | } | |
179 | ||
180 | /** | |
181 | * Handle mouse movements. | |
182 | * | |
183 | * @param mouse mouse motion event | |
184 | */ | |
185 | @Override | |
186 | public void onMouseMotion(final TMouseEvent mouse) { | |
187 | this.mouse = mouse; | |
188 | ||
189 | if (!mouseOnButton()) { | |
190 | inButtonPress = false; | |
191 | } | |
192 | } | |
193 | ||
194 | /** | |
195 | * Handle keystrokes. | |
196 | * | |
197 | * @param keypress keystroke event | |
198 | */ | |
199 | @Override | |
200 | public void onKeypress(final TKeypressEvent keypress) { | |
201 | if (keypress.equals(kbEnter) | |
202 | || keypress.equals(kbSpace) | |
203 | ) { | |
204 | // Dispatch | |
a325f111 | 205 | dispatch(); |
30d336cc KL |
206 | return; |
207 | } | |
208 | ||
209 | // Pass to parent for the things we don't care about. | |
210 | super.onKeypress(keypress); | |
211 | } | |
212 | ||
d36057df KL |
213 | // ------------------------------------------------------------------------ |
214 | // TWidget ---------------------------------------------------------------- | |
215 | // ------------------------------------------------------------------------ | |
216 | ||
217 | /** | |
218 | * Draw a button with a shadow. | |
219 | */ | |
220 | @Override | |
221 | public void draw() { | |
222 | CellAttributes buttonColor; | |
223 | CellAttributes menuMnemonicColor; | |
d36057df KL |
224 | |
225 | if (!isEnabled()) { | |
226 | buttonColor = getTheme().getColor("tbutton.disabled"); | |
227 | menuMnemonicColor = getTheme().getColor("tbutton.disabled"); | |
228 | } else if (isAbsoluteActive()) { | |
229 | buttonColor = getTheme().getColor("tbutton.active"); | |
230 | menuMnemonicColor = getTheme().getColor("tbutton.mnemonic.highlighted"); | |
231 | } else { | |
232 | buttonColor = getTheme().getColor("tbutton.inactive"); | |
233 | menuMnemonicColor = getTheme().getColor("tbutton.mnemonic"); | |
234 | } | |
235 | ||
236 | if (inButtonPress) { | |
a39c5665 KL |
237 | putCharXY(1, 0, ' ', buttonColor); |
238 | putStringXY(2, 0, mnemonic.getRawLabel(), buttonColor); | |
a69ed767 | 239 | putCharXY(getWidth() - 1, 0, ' ', buttonColor); |
d36057df | 240 | } else { |
a69ed767 KL |
241 | putCharXY(0, 0, ' ', buttonColor); |
242 | putStringXY(1, 0, mnemonic.getRawLabel(), buttonColor); | |
243 | putCharXY(getWidth() - 2, 0, ' ', buttonColor); | |
a39c5665 | 244 | |
255967a8 | 245 | if (shadowColor != null) { |
a39c5665 KL |
246 | putCharXY(getWidth() - 1, 0, |
247 | GraphicsChars.CP437[0xDC], shadowColor); | |
248 | hLineXY(1, 1, getWidth() - 1, | |
249 | GraphicsChars.CP437[0xDF], shadowColor); | |
255967a8 | 250 | } |
d36057df KL |
251 | } |
252 | if (mnemonic.getShortcutIdx() >= 0) { | |
253 | if (inButtonPress) { | |
a69ed767 | 254 | putCharXY(2 + mnemonic.getShortcutIdx(), 0, |
d36057df KL |
255 | mnemonic.getShortcut(), menuMnemonicColor); |
256 | } else { | |
a69ed767 | 257 | putCharXY(1 + mnemonic.getShortcutIdx(), 0, |
d36057df KL |
258 | mnemonic.getShortcut(), menuMnemonicColor); |
259 | } | |
d36057df KL |
260 | } |
261 | } | |
262 | ||
263 | // ------------------------------------------------------------------------ | |
264 | // TButton ---------------------------------------------------------------- | |
265 | // ------------------------------------------------------------------------ | |
266 | ||
267 | /** | |
268 | * Get the mnemonic string for this button. | |
269 | * | |
270 | * @return mnemonic string | |
271 | */ | |
272 | public MnemonicString getMnemonic() { | |
273 | return mnemonic; | |
274 | } | |
275 | ||
276 | /** | |
277 | * Act as though the button was pressed. This is useful for other UI | |
278 | * elements to get the same action as if the user clicked the button. | |
279 | */ | |
280 | public void dispatch() { | |
281 | if (action != null) { | |
282 | action.DO(); | |
283 | inButtonPress = false; | |
284 | } | |
285 | } | |
286 | ||
e23ea538 | 287 | /** |
a39c5665 KL |
288 | * Set the background color used for the button "shadow". If null, no |
289 | * shadow will be drawn. | |
e23ea538 | 290 | * |
a39c5665 | 291 | * @param color the new background color, or null for no shadow |
e23ea538 KL |
292 | */ |
293 | public void setShadowColor(final CellAttributes color) { | |
a39c5665 KL |
294 | if (color != null) { |
295 | shadowColor = new CellAttributes(); | |
296 | shadowColor.setTo(color); | |
297 | shadowColor.setForeColor(Color.BLACK); | |
298 | shadowColor.setBold(false); | |
299 | } else { | |
300 | shadowColor = null; | |
301 | } | |
e23ea538 KL |
302 | } |
303 | ||
30d336cc | 304 | } |