| 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.GraphicsChars; |
| 35 | import jexer.event.TKeypressEvent; |
| 36 | import jexer.event.TMouseEvent; |
| 37 | import static jexer.TKeypress.*; |
| 38 | |
| 39 | /** |
| 40 | * TCheckbox implements an on/off checkbox. |
| 41 | */ |
| 42 | public final class TCheckbox extends TWidget { |
| 43 | |
| 44 | /** |
| 45 | * Checkbox state, true means checked. |
| 46 | */ |
| 47 | private boolean checked = false; |
| 48 | |
| 49 | /** |
| 50 | * Label for this checkbox. |
| 51 | */ |
| 52 | private String label; |
| 53 | |
| 54 | /** |
| 55 | * Public constructor. |
| 56 | * |
| 57 | * @param parent parent widget |
| 58 | * @param x column relative to parent |
| 59 | * @param y row relative to parent |
| 60 | * @param label label to display next to (right of) the checkbox |
| 61 | * @param checked initial check state |
| 62 | */ |
| 63 | public TCheckbox(final TWidget parent, final int x, final int y, |
| 64 | final String label, final boolean checked) { |
| 65 | |
| 66 | // Set parent and window |
| 67 | super(parent); |
| 68 | |
| 69 | setX(x); |
| 70 | setY(y); |
| 71 | setHeight(1); |
| 72 | this.label = label; |
| 73 | setWidth(label.length() + 4); |
| 74 | this.checked = checked; |
| 75 | |
| 76 | setHasCursor(true); |
| 77 | setCursorX(1); |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * Returns true if the mouse is currently on the checkbox. |
| 82 | * |
| 83 | * @param mouse mouse event |
| 84 | * @return true if the mouse is currently on the checkbox |
| 85 | */ |
| 86 | private boolean mouseOnCheckbox(final TMouseEvent mouse) { |
| 87 | if ((mouse.getY() == 0) |
| 88 | && (mouse.getX() >= 0) |
| 89 | && (mouse.getX() <= 2) |
| 90 | ) { |
| 91 | return true; |
| 92 | } |
| 93 | return false; |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * Draw a checkbox with label. |
| 98 | */ |
| 99 | @Override |
| 100 | public void draw() { |
| 101 | CellAttributes checkboxColor; |
| 102 | |
| 103 | if (getAbsoluteActive()) { |
| 104 | checkboxColor = getTheme().getColor("tcheckbox.active"); |
| 105 | } else { |
| 106 | checkboxColor = getTheme().getColor("tcheckbox.inactive"); |
| 107 | } |
| 108 | |
| 109 | getScreen().putCharXY(0, 0, '[', checkboxColor); |
| 110 | if (checked) { |
| 111 | getScreen().putCharXY(1, 0, GraphicsChars.CHECK, checkboxColor); |
| 112 | } else { |
| 113 | getScreen().putCharXY(1, 0, ' ', checkboxColor); |
| 114 | } |
| 115 | getScreen().putCharXY(2, 0, ']', checkboxColor); |
| 116 | getScreen().putStrXY(4, 0, label, checkboxColor); |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * Handle mouse checkbox presses. |
| 121 | * |
| 122 | * @param mouse mouse button down event |
| 123 | */ |
| 124 | @Override |
| 125 | public void onMouseDown(final TMouseEvent mouse) { |
| 126 | if ((mouseOnCheckbox(mouse)) && (mouse.getMouse1())) { |
| 127 | // Switch state |
| 128 | checked = !checked; |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | /** |
| 133 | * Handle keystrokes. |
| 134 | * |
| 135 | * @param keypress keystroke event |
| 136 | */ |
| 137 | @Override |
| 138 | public void onKeypress(final TKeypressEvent keypress) { |
| 139 | if (keypress.equals(kbSpace)) { |
| 140 | checked = !checked; |
| 141 | return; |
| 142 | } |
| 143 | |
| 144 | // Pass to parent for the things we don't care about. |
| 145 | super.onKeypress(keypress); |
| 146 | } |
| 147 | |
| 148 | } |