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
.event
.TKeypressEvent
;
36 import jexer
.event
.TMouseEvent
;
37 import static jexer
.TKeypress
.*;
40 * TButton implements a simple button. To make the button do something, pass
41 * a TAction class to its constructor.
45 public class TButton
extends TWidget
{
47 // ------------------------------------------------------------------------
48 // Variables --------------------------------------------------------------
49 // ------------------------------------------------------------------------
52 * The shortcut and button text.
54 private MnemonicString mnemonic
;
57 * Remember mouse state.
59 private TMouseEvent mouse
;
62 * True when the button is being pressed and held down.
64 private boolean inButtonPress
= false;
67 * The action to perform when the button is clicked.
69 private TAction action
;
72 * The background color used for the button "shadow".
74 private CellAttributes shadowColor
;
76 // ------------------------------------------------------------------------
77 // Constructors -----------------------------------------------------------
78 // ------------------------------------------------------------------------
81 * Private constructor.
83 * @param parent parent widget
84 * @param text label on the button
85 * @param x column relative to parent
86 * @param y row relative to parent
88 private TButton(final TWidget parent
, final String text
,
89 final int x
, final int y
) {
91 // Set parent and window
94 mnemonic
= new MnemonicString(text
);
99 setWidth(mnemonic
.getRawLabel().length() + 3);
101 shadowColor
= new CellAttributes();
102 shadowColor
.setTo(getWindow().getBackground());
103 shadowColor
.setForeColor(Color
.BLACK
);
104 shadowColor
.setBold(false);
108 * Public constructor.
110 * @param parent parent widget
111 * @param text label on the button
112 * @param x column relative to parent
113 * @param y row relative to parent
114 * @param action to call when button is pressed
116 public TButton(final TWidget parent
, final String text
,
117 final int x
, final int y
, final TAction action
) {
119 this(parent
, text
, x
, y
);
120 this.action
= action
;
123 // ------------------------------------------------------------------------
124 // Event handlers ---------------------------------------------------------
125 // ------------------------------------------------------------------------
128 * Returns true if the mouse is currently on the button.
130 * @return if true the mouse is currently on the button
132 private boolean mouseOnButton() {
133 int rightEdge
= getWidth() - 1;
138 && (mouse
.getY() == 0)
139 && (mouse
.getX() >= 0)
140 && (mouse
.getX() < rightEdge
)
148 * Handle mouse button presses.
150 * @param mouse mouse button event
153 public void onMouseDown(final TMouseEvent mouse
) {
156 if ((mouseOnButton()) && (mouse
.isMouse1())) {
157 // Begin button press
158 inButtonPress
= true;
163 * Handle mouse button releases.
165 * @param mouse mouse button release event
168 public void onMouseUp(final TMouseEvent mouse
) {
171 if (inButtonPress
&& mouse
.isMouse1()) {
172 // Dispatch the event
179 * Handle mouse movements.
181 * @param mouse mouse motion event
184 public void onMouseMotion(final TMouseEvent mouse
) {
187 if (!mouseOnButton()) {
188 inButtonPress
= false;
195 * @param keypress keystroke event
198 public void onKeypress(final TKeypressEvent keypress
) {
199 if (keypress
.equals(kbEnter
)
200 || keypress
.equals(kbSpace
)
207 // Pass to parent for the things we don't care about.
208 super.onKeypress(keypress
);
211 // ------------------------------------------------------------------------
212 // TWidget ----------------------------------------------------------------
213 // ------------------------------------------------------------------------
216 * Draw a button with a shadow.
220 CellAttributes buttonColor
;
221 CellAttributes menuMnemonicColor
;
224 buttonColor
= getTheme().getColor("tbutton.disabled");
225 menuMnemonicColor
= getTheme().getColor("tbutton.disabled");
226 } else if (isAbsoluteActive()) {
227 buttonColor
= getTheme().getColor("tbutton.active");
228 menuMnemonicColor
= getTheme().getColor("tbutton.mnemonic.highlighted");
230 buttonColor
= getTheme().getColor("tbutton.inactive");
231 menuMnemonicColor
= getTheme().getColor("tbutton.mnemonic");
235 putCharXY(1, 0, ' ', buttonColor
);
236 putStringXY(2, 0, mnemonic
.getRawLabel(), buttonColor
);
237 putCharXY(getWidth() - 1, 0, ' ', buttonColor
);
239 putCharXY(0, 0, ' ', buttonColor
);
240 putStringXY(1, 0, mnemonic
.getRawLabel(), buttonColor
);
241 putCharXY(getWidth() - 2, 0, ' ', buttonColor
);
243 putCharXY(getWidth() - 1, 0,
244 GraphicsChars
.CP437
[0xDC], shadowColor
);
245 hLineXY(1, 1, getWidth() - 1,
246 GraphicsChars
.CP437
[0xDF], shadowColor
);
248 if (mnemonic
.getShortcutIdx() >= 0) {
250 putCharXY(2 + mnemonic
.getShortcutIdx(), 0,
251 mnemonic
.getShortcut(), menuMnemonicColor
);
253 putCharXY(1 + mnemonic
.getShortcutIdx(), 0,
254 mnemonic
.getShortcut(), menuMnemonicColor
);
259 // ------------------------------------------------------------------------
260 // TButton ----------------------------------------------------------------
261 // ------------------------------------------------------------------------
264 * Get the mnemonic string for this button.
266 * @return mnemonic string
268 public MnemonicString
getMnemonic() {
273 * Act as though the button was pressed. This is useful for other UI
274 * elements to get the same action as if the user clicked the button.
276 public void dispatch() {
277 if (action
!= null) {
279 inButtonPress
= false;
284 * Set the background color used for the button "shadow".
286 * @param color the new background color
288 public void setShadowColor(final CellAttributes color
) {
289 shadowColor
= new CellAttributes();
290 shadowColor
.setTo(color
);
291 shadowColor
.setForeColor(Color
.BLACK
);
292 shadowColor
.setBold(false);