Commit | Line | Data |
---|---|---|
7272e49f KL |
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 | ||
7c870d89 | 33 | import static jexer.TKeypress.kbSpace; |
7272e49f KL |
34 | import jexer.bits.CellAttributes; |
35 | import jexer.bits.GraphicsChars; | |
36 | import jexer.event.TKeypressEvent; | |
37 | import jexer.event.TMouseEvent; | |
7272e49f KL |
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 | ||
7c870d89 KL |
49 | /** |
50 | * Get checked value. | |
51 | * | |
52 | * @return if true, this is checked | |
53 | */ | |
54 | public final boolean isChecked() { | |
55 | return checked; | |
56 | } | |
57 | ||
58 | /** | |
59 | * Set checked value. | |
60 | * | |
61 | * @param checked new checked value. | |
62 | */ | |
63 | public final void setChecked(final boolean checked) { | |
64 | this.checked = checked; | |
65 | } | |
66 | ||
7272e49f KL |
67 | /** |
68 | * Label for this checkbox. | |
69 | */ | |
70 | private String label; | |
71 | ||
72 | /** | |
73 | * Public constructor. | |
74 | * | |
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 | |
80 | */ | |
81 | public TCheckbox(final TWidget parent, final int x, final int y, | |
7c870d89 | 82 | final String label, final boolean checked) { |
7272e49f KL |
83 | |
84 | // Set parent and window | |
a83fea2b | 85 | super(parent, x, y, label.length() + 4, 1); |
7272e49f | 86 | |
7272e49f | 87 | this.label = label; |
7272e49f KL |
88 | this.checked = checked; |
89 | ||
7c870d89 | 90 | setCursorVisible(true); |
7272e49f KL |
91 | setCursorX(1); |
92 | } | |
93 | ||
94 | /** | |
95 | * Returns true if the mouse is currently on the checkbox. | |
96 | * | |
97 | * @param mouse mouse event | |
98 | * @return true if the mouse is currently on the checkbox | |
99 | */ | |
100 | private boolean mouseOnCheckbox(final TMouseEvent mouse) { | |
00d2622b | 101 | if ((mouse.getY() == 0) |
7c870d89 KL |
102 | && (mouse.getX() >= 0) |
103 | && (mouse.getX() <= 2) | |
104 | ) { | |
7272e49f KL |
105 | return true; |
106 | } | |
107 | return false; | |
108 | } | |
109 | ||
110 | /** | |
111 | * Draw a checkbox with label. | |
112 | */ | |
113 | @Override | |
114 | public void draw() { | |
115 | CellAttributes checkboxColor; | |
116 | ||
7c870d89 | 117 | if (isAbsoluteActive()) { |
7272e49f KL |
118 | checkboxColor = getTheme().getColor("tcheckbox.active"); |
119 | } else { | |
120 | checkboxColor = getTheme().getColor("tcheckbox.inactive"); | |
121 | } | |
122 | ||
123 | getScreen().putCharXY(0, 0, '[', checkboxColor); | |
124 | if (checked) { | |
125 | getScreen().putCharXY(1, 0, GraphicsChars.CHECK, checkboxColor); | |
126 | } else { | |
127 | getScreen().putCharXY(1, 0, ' ', checkboxColor); | |
128 | } | |
129 | getScreen().putCharXY(2, 0, ']', checkboxColor); | |
130 | getScreen().putStrXY(4, 0, label, checkboxColor); | |
131 | } | |
132 | ||
133 | /** | |
134 | * Handle mouse checkbox presses. | |
135 | * | |
136 | * @param mouse mouse button down event | |
137 | */ | |
138 | @Override | |
139 | public void onMouseDown(final TMouseEvent mouse) { | |
7c870d89 | 140 | if ((mouseOnCheckbox(mouse)) && (mouse.isMouse1())) { |
7272e49f KL |
141 | // Switch state |
142 | checked = !checked; | |
143 | } | |
144 | } | |
145 | ||
146 | /** | |
147 | * Handle keystrokes. | |
148 | * | |
149 | * @param keypress keystroke event | |
150 | */ | |
151 | @Override | |
152 | public void onKeypress(final TKeypressEvent keypress) { | |
153 | if (keypress.equals(kbSpace)) { | |
154 | checked = !checked; | |
155 | return; | |
156 | } | |
157 | ||
158 | // Pass to parent for the things we don't care about. | |
159 | super.onKeypress(keypress); | |
160 | } | |
161 | ||
162 | } |