| 1 | /** |
| 2 | * Jexer - Java Text User Interface |
| 3 | * |
| 4 | * License: LGPLv3 or later |
| 5 | * |
| 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. |
| 9 | * |
| 10 | * Copyright (C) 2015 Kevin Lamonte |
| 11 | * |
| 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. |
| 16 | * |
| 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. |
| 21 | * |
| 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 |
| 26 | * 02110-1301 USA |
| 27 | * |
| 28 | * @author Kevin Lamonte [kevin.lamonte@gmail.com] |
| 29 | * @version 1 |
| 30 | */ |
| 31 | package jexer; |
| 32 | |
| 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.*; |
| 40 | |
| 41 | /** |
| 42 | * TButton implements a simple button. To make the button do something, pass |
| 43 | * a TAction class to its constructor. |
| 44 | * |
| 45 | * @see TAction#DO() |
| 46 | */ |
| 47 | public final class TButton extends TWidget { |
| 48 | |
| 49 | /** |
| 50 | * The shortcut and button text. |
| 51 | */ |
| 52 | private MnemonicString mnemonic; |
| 53 | |
| 54 | /** |
| 55 | * Remember mouse state. |
| 56 | */ |
| 57 | private TMouseEvent mouse; |
| 58 | |
| 59 | /** |
| 60 | * True when the button is being pressed and held down. |
| 61 | */ |
| 62 | private boolean inButtonPress = false; |
| 63 | |
| 64 | /** |
| 65 | * The action to perform when the button is clicked. |
| 66 | */ |
| 67 | private TAction action; |
| 68 | |
| 69 | /** |
| 70 | * Private constructor. |
| 71 | * |
| 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 |
| 76 | */ |
| 77 | private TButton(final TWidget parent, final String text, |
| 78 | final int x, final int y) { |
| 79 | |
| 80 | // Set parent and window |
| 81 | super(parent); |
| 82 | |
| 83 | mnemonic = new MnemonicString(text); |
| 84 | |
| 85 | setX(x); |
| 86 | setY(y); |
| 87 | setHeight(2); |
| 88 | setWidth(mnemonic.getRawLabel().length() + 3); |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * Public constructor. |
| 93 | * |
| 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 |
| 99 | */ |
| 100 | public TButton(final TWidget parent, final String text, |
| 101 | final int x, final int y, final TAction action) { |
| 102 | |
| 103 | this(parent, text, x, y); |
| 104 | this.action = action; |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * Returns true if the mouse is currently on the button. |
| 109 | * |
| 110 | * @return if true the mouse is currently on the button |
| 111 | */ |
| 112 | private boolean mouseOnButton() { |
| 113 | int rightEdge = getWidth() - 1; |
| 114 | if (inButtonPress) { |
| 115 | rightEdge++; |
| 116 | } |
| 117 | if ((mouse != null) |
| 118 | && (mouse.getY() == 0) |
| 119 | && (mouse.getX() >= 0) |
| 120 | && (mouse.getX() < rightEdge) |
| 121 | ) { |
| 122 | return true; |
| 123 | } |
| 124 | return false; |
| 125 | } |
| 126 | |
| 127 | /** |
| 128 | * Draw a button with a shadow. |
| 129 | */ |
| 130 | @Override |
| 131 | public void draw() { |
| 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); |
| 138 | |
| 139 | if (!getEnabled()) { |
| 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"); |
| 145 | } else { |
| 146 | buttonColor = getTheme().getColor("tbutton.inactive"); |
| 147 | menuMnemonicColor = getTheme().getColor("tbutton.mnemonic"); |
| 148 | } |
| 149 | |
| 150 | if (inButtonPress) { |
| 151 | getScreen().putCharXY(1, 0, ' ', buttonColor); |
| 152 | getScreen().putStrXY(2, 0, mnemonic.getRawLabel(), buttonColor); |
| 153 | getScreen().putCharXY(getWidth() - 1, 0, ' ', buttonColor); |
| 154 | } else { |
| 155 | getScreen().putCharXY(0, 0, ' ', buttonColor); |
| 156 | getScreen().putStrXY(1, 0, mnemonic.getRawLabel(), buttonColor); |
| 157 | getScreen().putCharXY(getWidth() - 2, 0, ' ', buttonColor); |
| 158 | |
| 159 | getScreen().putCharXY(getWidth() - 1, 0, |
| 160 | GraphicsChars.CP437[0xDC], shadowColor); |
| 161 | getScreen().hLineXY(1, 1, getWidth() - 1, |
| 162 | GraphicsChars.CP437[0xDF], shadowColor); |
| 163 | } |
| 164 | if (mnemonic.getShortcutIdx() >= 0) { |
| 165 | if (inButtonPress) { |
| 166 | getScreen().putCharXY(2 + mnemonic.getShortcutIdx(), 0, |
| 167 | mnemonic.getShortcut(), menuMnemonicColor); |
| 168 | } else { |
| 169 | getScreen().putCharXY(1 + mnemonic.getShortcutIdx(), 0, |
| 170 | mnemonic.getShortcut(), menuMnemonicColor); |
| 171 | } |
| 172 | |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | /** |
| 177 | * Handle mouse button presses. |
| 178 | * |
| 179 | * @param mouse mouse button event |
| 180 | */ |
| 181 | @Override |
| 182 | public void onMouseDown(final TMouseEvent mouse) { |
| 183 | this.mouse = mouse; |
| 184 | |
| 185 | if ((mouseOnButton()) && (mouse.getMouse1())) { |
| 186 | // Begin button press |
| 187 | inButtonPress = true; |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | /** |
| 192 | * Handle mouse button releases. |
| 193 | * |
| 194 | * @param mouse mouse button release event |
| 195 | */ |
| 196 | @Override |
| 197 | public void onMouseUp(final TMouseEvent mouse) { |
| 198 | this.mouse = mouse; |
| 199 | |
| 200 | if (inButtonPress && mouse.getMouse1()) { |
| 201 | inButtonPress = false; |
| 202 | // Dispatch the event |
| 203 | if (action != null) { |
| 204 | action.DO(); |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | } |
| 209 | |
| 210 | /** |
| 211 | * Handle mouse movements. |
| 212 | * |
| 213 | * @param mouse mouse motion event |
| 214 | */ |
| 215 | @Override |
| 216 | public void onMouseMotion(final TMouseEvent mouse) { |
| 217 | this.mouse = mouse; |
| 218 | |
| 219 | if (!mouseOnButton()) { |
| 220 | inButtonPress = false; |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | /** |
| 225 | * Handle keystrokes. |
| 226 | * |
| 227 | * @param keypress keystroke event |
| 228 | */ |
| 229 | @Override |
| 230 | public void onKeypress(final TKeypressEvent keypress) { |
| 231 | if (keypress.equals(kbEnter) |
| 232 | || keypress.equals(kbSpace) |
| 233 | ) { |
| 234 | // Dispatch |
| 235 | if (action != null) { |
| 236 | action.DO(); |
| 237 | } |
| 238 | return; |
| 239 | } |
| 240 | |
| 241 | // Pass to parent for the things we don't care about. |
| 242 | super.onKeypress(keypress); |
| 243 | } |
| 244 | |
| 245 | } |