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 static jexer
.TKeypress
.kbSpace
;
34 import jexer
.bits
.CellAttributes
;
35 import jexer
.bits
.GraphicsChars
;
36 import jexer
.event
.TKeypressEvent
;
37 import jexer
.event
.TMouseEvent
;
40 * TCheckbox implements an on/off checkbox.
42 public final class TCheckbox
extends TWidget
{
45 * Checkbox state, true means checked.
47 private boolean checked
= false;
52 * @return if true, this is checked
54 public final boolean isChecked() {
61 * @param checked new checked value.
63 public final void setChecked(final boolean checked
) {
64 this.checked
= checked
;
68 * Label for this checkbox.
75 * @param parent parent widget
76 * @param x column relative to parent
77 * @param y row relative to parent
78 * @param label label to display next to (right of) the checkbox
79 * @param checked initial check state
81 public TCheckbox(final TWidget parent
, final int x
, final int y
,
82 final String label
, final boolean checked
) {
84 // Set parent and window
85 super(parent
, x
, y
, label
.length() + 4, 1);
88 this.checked
= checked
;
90 setCursorVisible(true);
95 * Returns true if the mouse is currently on the checkbox.
97 * @param mouse mouse event
98 * @return true if the mouse is currently on the checkbox
100 private boolean mouseOnCheckbox(final TMouseEvent mouse
) {
101 if ((mouse
.getY() == 0)
102 && (mouse
.getX() >= 0)
103 && (mouse
.getX() <= 2)
111 * Draw a checkbox with label.
115 CellAttributes checkboxColor
;
117 if (isAbsoluteActive()) {
118 checkboxColor
= getTheme().getColor("tcheckbox.active");
120 checkboxColor
= getTheme().getColor("tcheckbox.inactive");
123 getScreen().putCharXY(0, 0, '[', checkboxColor
);
125 getScreen().putCharXY(1, 0, GraphicsChars
.CHECK
, checkboxColor
);
127 getScreen().putCharXY(1, 0, ' ', checkboxColor
);
129 getScreen().putCharXY(2, 0, ']', checkboxColor
);
130 getScreen().putStringXY(4, 0, label
, checkboxColor
);
134 * Handle mouse checkbox presses.
136 * @param mouse mouse button down event
139 public void onMouseDown(final TMouseEvent mouse
) {
140 if ((mouseOnCheckbox(mouse
)) && (mouse
.isMouse1())) {
149 * @param keypress keystroke event
152 public void onKeypress(final TKeypressEvent keypress
) {
153 if (keypress
.equals(kbSpace
)) {
158 // Pass to parent for the things we don't care about.
159 super.onKeypress(keypress
);