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 super.setWidth(StringUtils
.width(mnemonic
.getRawLabel()) + 3);
104 shadowColor
= new CellAttributes();
105 shadowColor
.setTo(getWindow().getBackground());
106 shadowColor
.setForeColor(Color
.BLACK
);
107 shadowColor
.setBold(false);
109 // Since we set dimensions after TWidget's constructor, we need to
110 // update the layout manager.
111 if (getParent().getLayoutManager() != null) {
112 getParent().getLayoutManager().remove(this);
113 getParent().getLayoutManager().add(this);
118 * Public constructor.
120 * @param parent parent widget
121 * @param text label on the button
122 * @param x column relative to parent
123 * @param y row relative to parent
124 * @param action to call when button is pressed
126 public TButton(final TWidget parent
, final String text
,
127 final int x
, final int y
, final TAction action
) {
129 this(parent
, text
, x
, y
);
130 this.action
= action
;
133 // ------------------------------------------------------------------------
134 // Event handlers ---------------------------------------------------------
135 // ------------------------------------------------------------------------
138 * Returns true if the mouse is currently on the button.
140 * @return if true the mouse is currently on the button
142 private boolean mouseOnButton() {
143 int rightEdge
= getWidth() - 1;
148 && (mouse
.getY() == 0)
149 && (mouse
.getX() >= 0)
150 && (mouse
.getX() < rightEdge
)
158 * Handle mouse button presses.
160 * @param mouse mouse button event
163 public void onMouseDown(final TMouseEvent mouse
) {
166 if ((mouseOnButton()) && (mouse
.isMouse1())) {
167 // Begin button press
168 inButtonPress
= true;
173 * Handle mouse button releases.
175 * @param mouse mouse button release event
178 public void onMouseUp(final TMouseEvent mouse
) {
181 if (inButtonPress
&& mouse
.isMouse1()) {
182 // Dispatch the event
189 * Handle mouse movements.
191 * @param mouse mouse motion event
194 public void onMouseMotion(final TMouseEvent mouse
) {
197 if (!mouseOnButton()) {
198 inButtonPress
= false;
205 * @param keypress keystroke event
208 public void onKeypress(final TKeypressEvent keypress
) {
209 if (keypress
.equals(kbEnter
)
210 || keypress
.equals(kbSpace
)
217 // Pass to parent for the things we don't care about.
218 super.onKeypress(keypress
);
221 // ------------------------------------------------------------------------
222 // TWidget ----------------------------------------------------------------
223 // ------------------------------------------------------------------------
226 * Override TWidget's width: we can only set width at construction time.
228 * @param width new widget width (ignored)
231 public void setWidth(final int width
) {
236 * Override TWidget's height: we can only set height at construction
239 * @param height new widget height (ignored)
242 public void setHeight(final int height
) {
247 * Draw a button with a shadow.
251 CellAttributes buttonColor
;
252 CellAttributes menuMnemonicColor
;
255 buttonColor
= getTheme().getColor("tbutton.disabled");
256 menuMnemonicColor
= getTheme().getColor("tbutton.disabled");
257 } else if (isAbsoluteActive()) {
258 buttonColor
= getTheme().getColor("tbutton.active");
259 menuMnemonicColor
= getTheme().getColor("tbutton.mnemonic.highlighted");
261 buttonColor
= getTheme().getColor("tbutton.inactive");
262 menuMnemonicColor
= getTheme().getColor("tbutton.mnemonic");
266 putCharXY(1, 0, ' ', buttonColor
);
267 putStringXY(2, 0, mnemonic
.getRawLabel(), buttonColor
);
268 putCharXY(getWidth() - 1, 0, ' ', buttonColor
);
270 putCharXY(0, 0, ' ', buttonColor
);
271 putStringXY(1, 0, mnemonic
.getRawLabel(), buttonColor
);
272 putCharXY(getWidth() - 2, 0, ' ', buttonColor
);
274 if (shadowColor
!= null) {
275 putCharXY(getWidth() - 1, 0,
276 GraphicsChars
.CP437
[0xDC], shadowColor
);
277 hLineXY(1, 1, getWidth() - 1,
278 GraphicsChars
.CP437
[0xDF], shadowColor
);
281 if (mnemonic
.getScreenShortcutIdx() >= 0) {
283 putCharXY(2 + mnemonic
.getScreenShortcutIdx(), 0,
284 mnemonic
.getShortcut(), menuMnemonicColor
);
286 putCharXY(1 + mnemonic
.getScreenShortcutIdx(), 0,
287 mnemonic
.getShortcut(), menuMnemonicColor
);
292 // ------------------------------------------------------------------------
293 // TButton ----------------------------------------------------------------
294 // ------------------------------------------------------------------------
297 * Get the mnemonic string for this button.
299 * @return mnemonic string
301 public MnemonicString
getMnemonic() {
306 * Act as though the button was pressed. This is useful for other UI
307 * elements to get the same action as if the user clicked the button.
309 public void dispatch() {
310 if (action
!= null) {
312 inButtonPress
= false;
317 * Set the background color used for the button "shadow". If null, no
318 * shadow will be drawn.
320 * @param color the new background color, or null for no shadow
322 public void setShadowColor(final CellAttributes color
) {
324 shadowColor
= new CellAttributes();
325 shadowColor
.setTo(color
);
326 shadowColor
.setForeColor(Color
.BLACK
);
327 shadowColor
.setBold(false);