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 final class TButton
extends TWidget
{
48 * The shortcut and button text.
50 private MnemonicString mnemonic
;
53 * Get the mnemonic string for this button.
55 * @return mnemonic string
57 public MnemonicString
getMnemonic() {
62 * Remember mouse state.
64 private TMouseEvent mouse
;
67 * True when the button is being pressed and held down.
69 private boolean inButtonPress
= false;
72 * The action to perform when the button is clicked.
74 private TAction action
;
77 * Act as though the button was pressed. This is useful for other UI
78 * elements to get the same action as if the user clicked the button.
80 public void dispatch() {
87 * Private constructor.
89 * @param parent parent widget
90 * @param text label on the button
91 * @param x column relative to parent
92 * @param y row relative to parent
94 private TButton(final TWidget parent
, final String text
,
95 final int x
, final int y
) {
97 // Set parent and window
100 mnemonic
= new MnemonicString(text
);
105 setWidth(mnemonic
.getRawLabel().length() + 3);
109 * Public constructor.
111 * @param parent parent widget
112 * @param text label on the button
113 * @param x column relative to parent
114 * @param y row relative to parent
115 * @param action to call when button is pressed
117 public TButton(final TWidget parent
, final String text
,
118 final int x
, final int y
, final TAction action
) {
120 this(parent
, text
, x
, y
);
121 this.action
= action
;
125 * Returns true if the mouse is currently on the button.
127 * @return if true the mouse is currently on the button
129 private boolean mouseOnButton() {
130 int rightEdge
= getWidth() - 1;
135 && (mouse
.getY() == 0)
136 && (mouse
.getX() >= 0)
137 && (mouse
.getX() < rightEdge
)
145 * Draw a button with a shadow.
149 CellAttributes buttonColor
;
150 CellAttributes menuMnemonicColor
;
151 CellAttributes shadowColor
= new CellAttributes();
152 shadowColor
.setTo(getWindow().getBackground());
153 shadowColor
.setForeColor(Color
.BLACK
);
154 shadowColor
.setBold(false);
157 buttonColor
= getTheme().getColor("tbutton.disabled");
158 menuMnemonicColor
= getTheme().getColor("tbutton.disabled");
159 } else if (isAbsoluteActive()) {
160 buttonColor
= getTheme().getColor("tbutton.active");
161 menuMnemonicColor
= getTheme().getColor("tbutton.mnemonic.highlighted");
163 buttonColor
= getTheme().getColor("tbutton.inactive");
164 menuMnemonicColor
= getTheme().getColor("tbutton.mnemonic");
168 getScreen().putCharXY(1, 0, ' ', buttonColor
);
169 getScreen().putStringXY(2, 0, mnemonic
.getRawLabel(), buttonColor
);
170 getScreen().putCharXY(getWidth() - 1, 0, ' ', buttonColor
);
172 getScreen().putCharXY(0, 0, ' ', buttonColor
);
173 getScreen().putStringXY(1, 0, mnemonic
.getRawLabel(), buttonColor
);
174 getScreen().putCharXY(getWidth() - 2, 0, ' ', buttonColor
);
176 getScreen().putCharXY(getWidth() - 1, 0,
177 GraphicsChars
.CP437
[0xDC], shadowColor
);
178 getScreen().hLineXY(1, 1, getWidth() - 1,
179 GraphicsChars
.CP437
[0xDF], shadowColor
);
181 if (mnemonic
.getShortcutIdx() >= 0) {
183 getScreen().putCharXY(2 + mnemonic
.getShortcutIdx(), 0,
184 mnemonic
.getShortcut(), menuMnemonicColor
);
186 getScreen().putCharXY(1 + mnemonic
.getShortcutIdx(), 0,
187 mnemonic
.getShortcut(), menuMnemonicColor
);
194 * Handle mouse button presses.
196 * @param mouse mouse button event
199 public void onMouseDown(final TMouseEvent mouse
) {
202 if ((mouseOnButton()) && (mouse
.isMouse1())) {
203 // Begin button press
204 inButtonPress
= true;
209 * Handle mouse button releases.
211 * @param mouse mouse button release event
214 public void onMouseUp(final TMouseEvent mouse
) {
217 if (inButtonPress
&& mouse
.isMouse1()) {
218 inButtonPress
= false;
219 // Dispatch the event
220 if (action
!= null) {
228 * Handle mouse movements.
230 * @param mouse mouse motion event
233 public void onMouseMotion(final TMouseEvent mouse
) {
236 if (!mouseOnButton()) {
237 inButtonPress
= false;
244 * @param keypress keystroke event
247 public void onKeypress(final TKeypressEvent keypress
) {
248 if (keypress
.equals(kbEnter
)
249 || keypress
.equals(kbSpace
)
252 if (action
!= null) {
258 // Pass to parent for the things we don't care about.
259 super.onKeypress(keypress
);