2 * Jexer - Java Text User Interface
4 * The MIT License (MIT)
6 * Copyright (C) 2019 Kevin Lamonte
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:
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
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.
26 * @author Kevin Lamonte [kevin.lamonte@gmail.com]
31 import jexer
.bits
.CellAttributes
;
32 import jexer
.bits
.Color
;
33 import jexer
.bits
.GraphicsChars
;
34 import jexer
.bits
.MnemonicString
;
35 import jexer
.bits
.StringUtils
;
36 import jexer
.event
.TKeypressEvent
;
37 import jexer
.event
.TMouseEvent
;
38 import static jexer
.TKeypress
.kbEnter
;
39 import static jexer
.TKeypress
.kbSpace
;
42 * TButton implements a simple button. To make the button do something, pass
43 * a TAction class to its constructor.
47 public class TButton
extends TWidget
{
49 // ------------------------------------------------------------------------
50 // Variables --------------------------------------------------------------
51 // ------------------------------------------------------------------------
54 * The shortcut and button text.
56 private MnemonicString mnemonic
;
59 * Remember mouse state.
61 private TMouseEvent mouse
;
64 * True when the button is being pressed and held down.
66 private boolean inButtonPress
= false;
69 * The action to perform when the button is clicked.
71 private TAction action
;
74 * The background color used for the button "shadow", or null for "no
77 private CellAttributes shadowColor
;
79 // ------------------------------------------------------------------------
80 // Constructors -----------------------------------------------------------
81 // ------------------------------------------------------------------------
84 * Private constructor.
86 * @param parent parent widget
87 * @param text label on the button
88 * @param x column relative to parent
89 * @param y row relative to parent
91 private TButton(final TWidget parent
, final String text
,
92 final int x
, final int y
) {
94 // Set parent and window
97 mnemonic
= new MnemonicString(text
);
102 setWidth(StringUtils
.width(mnemonic
.getRawLabel()) + 3);
104 shadowColor
= new CellAttributes();
105 shadowColor
.setTo(getWindow().getBackground());
106 shadowColor
.setForeColor(Color
.BLACK
);
107 shadowColor
.setBold(false);
111 * Public constructor.
113 * @param parent parent widget
114 * @param text label on the button
115 * @param x column relative to parent
116 * @param y row relative to parent
117 * @param action to call when button is pressed
119 public TButton(final TWidget parent
, final String text
,
120 final int x
, final int y
, final TAction action
) {
122 this(parent
, text
, x
, y
);
123 this.action
= action
;
126 // ------------------------------------------------------------------------
127 // Event handlers ---------------------------------------------------------
128 // ------------------------------------------------------------------------
131 * Returns true if the mouse is currently on the button.
133 * @return if true the mouse is currently on the button
135 private boolean mouseOnButton() {
136 int rightEdge
= getWidth() - 1;
141 && (mouse
.getY() == 0)
142 && (mouse
.getX() >= 0)
143 && (mouse
.getX() < rightEdge
)
151 * Handle mouse button presses.
153 * @param mouse mouse button event
156 public void onMouseDown(final TMouseEvent mouse
) {
159 if ((mouseOnButton()) && (mouse
.isMouse1())) {
160 // Begin button press
161 inButtonPress
= true;
166 * Handle mouse button releases.
168 * @param mouse mouse button release event
171 public void onMouseUp(final TMouseEvent mouse
) {
174 if (inButtonPress
&& mouse
.isMouse1()) {
175 // Dispatch the event
182 * Handle mouse movements.
184 * @param mouse mouse motion event
187 public void onMouseMotion(final TMouseEvent mouse
) {
190 if (!mouseOnButton()) {
191 inButtonPress
= false;
198 * @param keypress keystroke event
201 public void onKeypress(final TKeypressEvent keypress
) {
202 if (keypress
.equals(kbEnter
)
203 || keypress
.equals(kbSpace
)
210 // Pass to parent for the things we don't care about.
211 super.onKeypress(keypress
);
214 // ------------------------------------------------------------------------
215 // TWidget ----------------------------------------------------------------
216 // ------------------------------------------------------------------------
219 * Draw a button with a shadow.
223 CellAttributes buttonColor
;
224 CellAttributes menuMnemonicColor
;
227 buttonColor
= getTheme().getColor("tbutton.disabled");
228 menuMnemonicColor
= getTheme().getColor("tbutton.disabled");
229 } else if (isAbsoluteActive()) {
230 buttonColor
= getTheme().getColor("tbutton.active");
231 menuMnemonicColor
= getTheme().getColor("tbutton.mnemonic.highlighted");
233 buttonColor
= getTheme().getColor("tbutton.inactive");
234 menuMnemonicColor
= getTheme().getColor("tbutton.mnemonic");
238 putCharXY(1, 0, ' ', buttonColor
);
239 putStringXY(2, 0, mnemonic
.getRawLabel(), buttonColor
);
240 putCharXY(getWidth() - 1, 0, ' ', buttonColor
);
242 putCharXY(0, 0, ' ', buttonColor
);
243 putStringXY(1, 0, mnemonic
.getRawLabel(), buttonColor
);
244 putCharXY(getWidth() - 2, 0, ' ', buttonColor
);
246 if (shadowColor
!= null) {
247 putCharXY(getWidth() - 1, 0,
248 GraphicsChars
.CP437
[0xDC], shadowColor
);
249 hLineXY(1, 1, getWidth() - 1,
250 GraphicsChars
.CP437
[0xDF], shadowColor
);
253 if (mnemonic
.getShortcutIdx() >= 0) {
255 putCharXY(2 + mnemonic
.getShortcutIdx(), 0,
256 mnemonic
.getShortcut(), menuMnemonicColor
);
258 putCharXY(1 + mnemonic
.getShortcutIdx(), 0,
259 mnemonic
.getShortcut(), menuMnemonicColor
);
264 // ------------------------------------------------------------------------
265 // TButton ----------------------------------------------------------------
266 // ------------------------------------------------------------------------
269 * Get the mnemonic string for this button.
271 * @return mnemonic string
273 public MnemonicString
getMnemonic() {
278 * Act as though the button was pressed. This is useful for other UI
279 * elements to get the same action as if the user clicked the button.
281 public void dispatch() {
282 if (action
!= null) {
284 inButtonPress
= false;
289 * Set the background color used for the button "shadow". If null, no
290 * shadow will be drawn.
292 * @param color the new background color, or null for no shadow
294 public void setShadowColor(final CellAttributes color
) {
296 shadowColor
= new CellAttributes();
297 shadowColor
.setTo(color
);
298 shadowColor
.setForeColor(Color
.BLACK
);
299 shadowColor
.setBold(false);