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 * Get the mnemonic string for this button.
57 * @return mnemonic string
59 public final MnemonicString
getMnemonic() {
64 * Remember mouse state.
66 private TMouseEvent mouse
;
69 * True when the button is being pressed and held down.
71 private boolean inButtonPress
= false;
74 * The action to perform when the button is clicked.
76 private TAction action
;
79 * Private constructor.
81 * @param parent parent widget
82 * @param text label on the button
83 * @param x column relative to parent
84 * @param y row relative to parent
86 private TButton(final TWidget parent
, final String text
,
87 final int x
, final int y
) {
89 // Set parent and window
92 mnemonic
= new MnemonicString(text
);
97 setWidth(mnemonic
.getRawLabel().length() + 3);
101 * Public constructor.
103 * @param parent parent widget
104 * @param text label on the button
105 * @param x column relative to parent
106 * @param y row relative to parent
107 * @param action to call when button is pressed
109 public TButton(final TWidget parent
, final String text
,
110 final int x
, final int y
, final TAction action
) {
112 this(parent
, text
, x
, y
);
113 this.action
= action
;
117 * Returns true if the mouse is currently on the button.
119 * @return if true the mouse is currently on the button
121 private boolean mouseOnButton() {
122 int rightEdge
= getWidth() - 1;
127 && (mouse
.getY() == 0)
128 && (mouse
.getX() >= 0)
129 && (mouse
.getX() < rightEdge
)
137 * Draw a button with a shadow.
141 CellAttributes buttonColor
;
142 CellAttributes menuMnemonicColor
;
143 CellAttributes shadowColor
= new CellAttributes();
144 shadowColor
.setTo(getWindow().getBackground());
145 shadowColor
.setForeColor(Color
.BLACK
);
146 shadowColor
.setBold(false);
149 buttonColor
= getTheme().getColor("tbutton.disabled");
150 menuMnemonicColor
= getTheme().getColor("tbutton.disabled");
151 } else if (isAbsoluteActive()) {
152 buttonColor
= getTheme().getColor("tbutton.active");
153 menuMnemonicColor
= getTheme().getColor("tbutton.mnemonic.highlighted");
155 buttonColor
= getTheme().getColor("tbutton.inactive");
156 menuMnemonicColor
= getTheme().getColor("tbutton.mnemonic");
160 getScreen().putCharXY(1, 0, ' ', buttonColor
);
161 getScreen().putStrXY(2, 0, mnemonic
.getRawLabel(), buttonColor
);
162 getScreen().putCharXY(getWidth() - 1, 0, ' ', buttonColor
);
164 getScreen().putCharXY(0, 0, ' ', buttonColor
);
165 getScreen().putStrXY(1, 0, mnemonic
.getRawLabel(), buttonColor
);
166 getScreen().putCharXY(getWidth() - 2, 0, ' ', buttonColor
);
168 getScreen().putCharXY(getWidth() - 1, 0,
169 GraphicsChars
.CP437
[0xDC], shadowColor
);
170 getScreen().hLineXY(1, 1, getWidth() - 1,
171 GraphicsChars
.CP437
[0xDF], shadowColor
);
173 if (mnemonic
.getShortcutIdx() >= 0) {
175 getScreen().putCharXY(2 + mnemonic
.getShortcutIdx(), 0,
176 mnemonic
.getShortcut(), menuMnemonicColor
);
178 getScreen().putCharXY(1 + mnemonic
.getShortcutIdx(), 0,
179 mnemonic
.getShortcut(), menuMnemonicColor
);
186 * Handle mouse button presses.
188 * @param mouse mouse button event
191 public void onMouseDown(final TMouseEvent mouse
) {
194 if ((mouseOnButton()) && (mouse
.isMouse1())) {
195 // Begin button press
196 inButtonPress
= true;
201 * Handle mouse button releases.
203 * @param mouse mouse button release event
206 public void onMouseUp(final TMouseEvent mouse
) {
209 if (inButtonPress
&& mouse
.isMouse1()) {
210 inButtonPress
= false;
211 // Dispatch the event
212 if (action
!= null) {
220 * Handle mouse movements.
222 * @param mouse mouse motion event
225 public void onMouseMotion(final TMouseEvent mouse
) {
228 if (!mouseOnButton()) {
229 inButtonPress
= false;
236 * @param keypress keystroke event
239 public void onKeypress(final TKeypressEvent keypress
) {
240 if (keypress
.equals(kbEnter
)
241 || keypress
.equals(kbSpace
)
244 if (action
!= null) {
250 // Pass to parent for the things we don't care about.
251 super.onKeypress(keypress
);