2 * Jexer - Java Text User Interface
4 * License: LGPLv3 or later
6 * This module is licensed under the GNU Lesser General Public License
7 * Version 3. Please see the file "COPYING" in this directory for more
8 * information about the GNU Lesser General Public License Version 3.
10 * Copyright (C) 2015 Kevin Lamonte
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU Lesser General Public License
14 * as published by the Free Software Foundation; either version 3 of
15 * the License, or (at your option) any later version.
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
22 * You should have received a copy of the GNU Lesser General Public
23 * License along with this program; if not, see
24 * http://www.gnu.org/licenses/, or write to the Free Software
25 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
28 * @author Kevin Lamonte [kevin.lamonte@gmail.com]
33 import jexer
.bits
.CellAttributes
;
34 import jexer
.bits
.Color
;
35 import jexer
.bits
.GraphicsChars
;
36 import jexer
.bits
.MnemonicString
;
37 import jexer
.event
.TKeypressEvent
;
38 import jexer
.event
.TMouseEvent
;
39 import static jexer
.TKeypress
.*;
42 * TButton implements a simple button. To make the button do something, pass
43 * a TAction class to its constructor.
47 public final class TButton
extends TWidget
{
50 * The shortcut and button text.
52 private MnemonicString mnemonic
;
55 * Remember mouse state.
57 private TMouseEvent mouse
;
60 * True when the button is being pressed and held down.
62 private boolean inButtonPress
= false;
65 * The action to perform when the button is clicked.
67 private TAction action
;
70 * Private constructor.
72 * @param parent parent widget
73 * @param text label on the button
74 * @param x column relative to parent
75 * @param y row relative to parent
77 private TButton(final TWidget parent
, final String text
,
78 final int x
, final int y
) {
80 // Set parent and window
83 mnemonic
= new MnemonicString(text
);
88 setWidth(mnemonic
.getRawLabel().length() + 3);
94 * @param parent parent widget
95 * @param text label on the button
96 * @param x column relative to parent
97 * @param y row relative to parent
98 * @param action to call when button is pressed
100 public TButton(final TWidget parent
, final String text
,
101 final int x
, final int y
, final TAction action
) {
103 this(parent
, text
, x
, y
);
104 this.action
= action
;
108 * Returns true if the mouse is currently on the button.
110 * @return if true the mouse is currently on the button
112 private boolean mouseOnButton() {
113 int rightEdge
= getWidth() - 1;
118 && (mouse
.getY() == 0)
119 && (mouse
.getX() >= 0)
120 && (mouse
.getX() < rightEdge
)
128 * Draw a button with a shadow.
132 CellAttributes buttonColor
;
133 CellAttributes menuMnemonicColor
;
134 CellAttributes shadowColor
= new CellAttributes();
135 shadowColor
.setTo(getWindow().getBackground());
136 shadowColor
.setForeColor(Color
.BLACK
);
137 shadowColor
.setBold(false);
140 buttonColor
= getTheme().getColor("tbutton.disabled");
141 menuMnemonicColor
= getTheme().getColor("tbutton.disabled");
142 } else if (getAbsoluteActive()) {
143 buttonColor
= getTheme().getColor("tbutton.active");
144 menuMnemonicColor
= getTheme().getColor("tbutton.mnemonic.highlighted");
146 buttonColor
= getTheme().getColor("tbutton.inactive");
147 menuMnemonicColor
= getTheme().getColor("tbutton.mnemonic");
151 getScreen().putCharXY(1, 0, ' ', buttonColor
);
152 getScreen().putStrXY(2, 0, mnemonic
.getRawLabel(), buttonColor
);
153 getScreen().putCharXY(getWidth() - 1, 0, ' ', buttonColor
);
155 getScreen().putCharXY(0, 0, ' ', buttonColor
);
156 getScreen().putStrXY(1, 0, mnemonic
.getRawLabel(), buttonColor
);
157 getScreen().putCharXY(getWidth() - 2, 0, ' ', buttonColor
);
159 getScreen().putCharXY(getWidth() - 1, 0,
160 GraphicsChars
.CP437
[0xDC], shadowColor
);
161 getScreen().hLineXY(1, 1, getWidth() - 1,
162 GraphicsChars
.CP437
[0xDF], shadowColor
);
164 if (mnemonic
.getShortcutIdx() >= 0) {
166 getScreen().putCharXY(2 + mnemonic
.getShortcutIdx(), 0,
167 mnemonic
.getShortcut(), menuMnemonicColor
);
169 getScreen().putCharXY(1 + mnemonic
.getShortcutIdx(), 0,
170 mnemonic
.getShortcut(), menuMnemonicColor
);
177 * Handle mouse button presses.
179 * @param mouse mouse button event
182 public void onMouseDown(final TMouseEvent mouse
) {
185 if ((mouseOnButton()) && (mouse
.getMouse1())) {
186 // Begin button press
187 inButtonPress
= true;
192 * Handle mouse button releases.
194 * @param mouse mouse button release event
197 public void onMouseUp(final TMouseEvent mouse
) {
200 if (inButtonPress
&& mouse
.getMouse1()) {
201 inButtonPress
= false;
202 // Dispatch the event
203 if (action
!= null) {
211 * Handle mouse movements.
213 * @param mouse mouse motion event
216 public void onMouseMotion(final TMouseEvent mouse
) {
219 if (!mouseOnButton()) {
220 inButtonPress
= false;
227 * @param keypress keystroke event
230 public void onKeypress(final TKeypressEvent keypress
) {
231 if (keypress
.equals(kbEnter
)
232 || keypress
.equals(kbSpace
)
235 if (action
!= null) {
241 // Pass to parent for the things we don't care about.
242 super.onKeypress(keypress
);