2 * Jexer - Java Text User Interface
4 * The MIT License (MIT)
6 * Copyright (C) 2017 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
;
71 // ------------------------------------------------------------------------
72 // Constructors -----------------------------------------------------------
73 // ------------------------------------------------------------------------
76 * Private constructor.
78 * @param parent parent widget
79 * @param text label on the button
80 * @param x column relative to parent
81 * @param y row relative to parent
83 private TButton(final TWidget parent
, final String text
,
84 final int x
, final int y
) {
86 // Set parent and window
89 mnemonic
= new MnemonicString(text
);
94 setWidth(mnemonic
.getRawLabel().length() + 3);
100 * @param parent parent widget
101 * @param text label on the button
102 * @param x column relative to parent
103 * @param y row relative to parent
104 * @param action to call when button is pressed
106 public TButton(final TWidget parent
, final String text
,
107 final int x
, final int y
, final TAction action
) {
109 this(parent
, text
, x
, y
);
110 this.action
= action
;
113 // ------------------------------------------------------------------------
114 // Event handlers ---------------------------------------------------------
115 // ------------------------------------------------------------------------
118 * Returns true if the mouse is currently on the button.
120 * @return if true the mouse is currently on the button
122 private boolean mouseOnButton() {
123 int rightEdge
= getWidth() - 1;
128 && (mouse
.getY() == 0)
129 && (mouse
.getX() >= 0)
130 && (mouse
.getX() < rightEdge
)
138 * Handle mouse button presses.
140 * @param mouse mouse button event
143 public void onMouseDown(final TMouseEvent mouse
) {
146 if ((mouseOnButton()) && (mouse
.isMouse1())) {
147 // Begin button press
148 inButtonPress
= true;
153 * Handle mouse button releases.
155 * @param mouse mouse button release event
158 public void onMouseUp(final TMouseEvent mouse
) {
161 if (inButtonPress
&& mouse
.isMouse1()) {
162 // Dispatch the event
169 * Handle mouse movements.
171 * @param mouse mouse motion event
174 public void onMouseMotion(final TMouseEvent mouse
) {
177 if (!mouseOnButton()) {
178 inButtonPress
= false;
185 * @param keypress keystroke event
188 public void onKeypress(final TKeypressEvent keypress
) {
189 if (keypress
.equals(kbEnter
)
190 || keypress
.equals(kbSpace
)
197 // Pass to parent for the things we don't care about.
198 super.onKeypress(keypress
);
201 // ------------------------------------------------------------------------
202 // TWidget ----------------------------------------------------------------
203 // ------------------------------------------------------------------------
206 * Draw a button with a shadow.
210 CellAttributes buttonColor
;
211 CellAttributes menuMnemonicColor
;
212 CellAttributes shadowColor
= new CellAttributes();
213 shadowColor
.setTo(getWindow().getBackground());
214 shadowColor
.setForeColor(Color
.BLACK
);
215 shadowColor
.setBold(false);
218 buttonColor
= getTheme().getColor("tbutton.disabled");
219 menuMnemonicColor
= getTheme().getColor("tbutton.disabled");
220 } else if (isAbsoluteActive()) {
221 buttonColor
= getTheme().getColor("tbutton.active");
222 menuMnemonicColor
= getTheme().getColor("tbutton.mnemonic.highlighted");
224 buttonColor
= getTheme().getColor("tbutton.inactive");
225 menuMnemonicColor
= getTheme().getColor("tbutton.mnemonic");
229 getScreen().putCharXY(1, 0, ' ', buttonColor
);
230 getScreen().putStringXY(2, 0, mnemonic
.getRawLabel(), buttonColor
);
231 getScreen().putCharXY(getWidth() - 1, 0, ' ', buttonColor
);
233 getScreen().putCharXY(0, 0, ' ', buttonColor
);
234 getScreen().putStringXY(1, 0, mnemonic
.getRawLabel(), buttonColor
);
235 getScreen().putCharXY(getWidth() - 2, 0, ' ', buttonColor
);
237 getScreen().putCharXY(getWidth() - 1, 0,
238 GraphicsChars
.CP437
[0xDC], shadowColor
);
239 getScreen().hLineXY(1, 1, getWidth() - 1,
240 GraphicsChars
.CP437
[0xDF], shadowColor
);
242 if (mnemonic
.getShortcutIdx() >= 0) {
244 getScreen().putCharXY(2 + mnemonic
.getShortcutIdx(), 0,
245 mnemonic
.getShortcut(), menuMnemonicColor
);
247 getScreen().putCharXY(1 + mnemonic
.getShortcutIdx(), 0,
248 mnemonic
.getShortcut(), menuMnemonicColor
);
254 // ------------------------------------------------------------------------
255 // TButton ----------------------------------------------------------------
256 // ------------------------------------------------------------------------
259 * Get the mnemonic string for this button.
261 * @return mnemonic string
263 public MnemonicString
getMnemonic() {
268 * Act as though the button was pressed. This is useful for other UI
269 * elements to get the same action as if the user clicked the button.
271 public void dispatch() {
272 if (action
!= null) {
274 inButtonPress
= false;