Commit | Line | Data |
---|---|---|
daa4106c | 1 | /* |
7272e49f KL |
2 | * Jexer - Java Text User Interface |
3 | * | |
e16dda65 | 4 | * The MIT License (MIT) |
7272e49f | 5 | * |
a2018e99 | 6 | * Copyright (C) 2017 Kevin Lamonte |
7272e49f | 7 | * |
e16dda65 KL |
8 | * Permission is hereby granted, free of charge, to any person obtaining a |
9 | * copy of this software and associated documentation files (the "Software"), | |
10 | * to deal in the Software without restriction, including without limitation | |
11 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, | |
12 | * and/or sell copies of the Software, and to permit persons to whom the | |
13 | * Software is furnished to do so, subject to the following conditions: | |
7272e49f | 14 | * |
e16dda65 KL |
15 | * The above copyright notice and this permission notice shall be included in |
16 | * all copies or substantial portions of the Software. | |
7272e49f | 17 | * |
e16dda65 KL |
18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL | |
21 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | |
23 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER | |
24 | * DEALINGS IN THE SOFTWARE. | |
7272e49f KL |
25 | * |
26 | * @author Kevin Lamonte [kevin.lamonte@gmail.com] | |
27 | * @version 1 | |
28 | */ | |
29 | package jexer; | |
30 | ||
7c870d89 | 31 | import static jexer.TKeypress.kbSpace; |
7272e49f KL |
32 | import jexer.bits.CellAttributes; |
33 | import jexer.bits.GraphicsChars; | |
34 | import jexer.event.TKeypressEvent; | |
35 | import jexer.event.TMouseEvent; | |
7272e49f KL |
36 | |
37 | /** | |
051e2913 | 38 | * TCheckBox implements an on/off checkbox. |
7272e49f | 39 | */ |
051e2913 | 40 | public class TCheckBox extends TWidget { |
7272e49f | 41 | |
d36057df KL |
42 | // ------------------------------------------------------------------------ |
43 | // Variables -------------------------------------------------------------- | |
44 | // ------------------------------------------------------------------------ | |
45 | ||
7272e49f KL |
46 | /** |
47 | * Checkbox state, true means checked. | |
48 | */ | |
49 | private boolean checked = false; | |
50 | ||
51 | /** | |
52 | * Label for this checkbox. | |
53 | */ | |
54 | private String label; | |
55 | ||
d36057df KL |
56 | // ------------------------------------------------------------------------ |
57 | // Constructors ----------------------------------------------------------- | |
58 | // ------------------------------------------------------------------------ | |
59 | ||
7272e49f KL |
60 | /** |
61 | * Public constructor. | |
62 | * | |
63 | * @param parent parent widget | |
64 | * @param x column relative to parent | |
65 | * @param y row relative to parent | |
66 | * @param label label to display next to (right of) the checkbox | |
67 | * @param checked initial check state | |
68 | */ | |
051e2913 | 69 | public TCheckBox(final TWidget parent, final int x, final int y, |
d36057df | 70 | final String label, final boolean checked) { |
7272e49f KL |
71 | |
72 | // Set parent and window | |
a83fea2b | 73 | super(parent, x, y, label.length() + 4, 1); |
7272e49f | 74 | |
7272e49f | 75 | this.label = label; |
7272e49f KL |
76 | this.checked = checked; |
77 | ||
7c870d89 | 78 | setCursorVisible(true); |
7272e49f KL |
79 | setCursorX(1); |
80 | } | |
81 | ||
d36057df KL |
82 | // ------------------------------------------------------------------------ |
83 | // Event handlers --------------------------------------------------------- | |
84 | // ------------------------------------------------------------------------ | |
85 | ||
7272e49f KL |
86 | /** |
87 | * Returns true if the mouse is currently on the checkbox. | |
88 | * | |
89 | * @param mouse mouse event | |
90 | * @return true if the mouse is currently on the checkbox | |
91 | */ | |
92 | private boolean mouseOnCheckbox(final TMouseEvent mouse) { | |
00d2622b | 93 | if ((mouse.getY() == 0) |
d36057df KL |
94 | && (mouse.getX() >= 0) |
95 | && (mouse.getX() <= 2) | |
96 | ) { | |
7272e49f KL |
97 | return true; |
98 | } | |
99 | return false; | |
100 | } | |
101 | ||
d36057df KL |
102 | /** |
103 | * Handle mouse checkbox presses. | |
104 | * | |
105 | * @param mouse mouse button down event | |
106 | */ | |
107 | @Override | |
108 | public void onMouseDown(final TMouseEvent mouse) { | |
109 | if ((mouseOnCheckbox(mouse)) && (mouse.isMouse1())) { | |
110 | // Switch state | |
111 | checked = !checked; | |
112 | } | |
113 | } | |
114 | ||
115 | /** | |
116 | * Handle keystrokes. | |
117 | * | |
118 | * @param keypress keystroke event | |
119 | */ | |
120 | @Override | |
121 | public void onKeypress(final TKeypressEvent keypress) { | |
122 | if (keypress.equals(kbSpace)) { | |
123 | checked = !checked; | |
124 | return; | |
125 | } | |
126 | ||
127 | // Pass to parent for the things we don't care about. | |
128 | super.onKeypress(keypress); | |
129 | } | |
130 | ||
131 | // ------------------------------------------------------------------------ | |
132 | // TWidget ---------------------------------------------------------------- | |
133 | // ------------------------------------------------------------------------ | |
134 | ||
7272e49f KL |
135 | /** |
136 | * Draw a checkbox with label. | |
137 | */ | |
138 | @Override | |
139 | public void draw() { | |
140 | CellAttributes checkboxColor; | |
141 | ||
7c870d89 | 142 | if (isAbsoluteActive()) { |
7272e49f KL |
143 | checkboxColor = getTheme().getColor("tcheckbox.active"); |
144 | } else { | |
145 | checkboxColor = getTheme().getColor("tcheckbox.inactive"); | |
146 | } | |
147 | ||
148 | getScreen().putCharXY(0, 0, '[', checkboxColor); | |
149 | if (checked) { | |
150 | getScreen().putCharXY(1, 0, GraphicsChars.CHECK, checkboxColor); | |
151 | } else { | |
152 | getScreen().putCharXY(1, 0, ' ', checkboxColor); | |
153 | } | |
154 | getScreen().putCharXY(2, 0, ']', checkboxColor); | |
0d47c546 | 155 | getScreen().putStringXY(4, 0, label, checkboxColor); |
7272e49f KL |
156 | } |
157 | ||
d36057df | 158 | // ------------------------------------------------------------------------ |
051e2913 | 159 | // TCheckBox -------------------------------------------------------------- |
d36057df KL |
160 | // ------------------------------------------------------------------------ |
161 | ||
7272e49f | 162 | /** |
d36057df | 163 | * Get checked value. |
7272e49f | 164 | * |
d36057df | 165 | * @return if true, this is checked |
7272e49f | 166 | */ |
d36057df KL |
167 | public boolean isChecked() { |
168 | return checked; | |
7272e49f KL |
169 | } |
170 | ||
171 | /** | |
d36057df | 172 | * Set checked value. |
7272e49f | 173 | * |
d36057df | 174 | * @param checked new checked value. |
7272e49f | 175 | */ |
d36057df KL |
176 | public void setChecked(final boolean checked) { |
177 | this.checked = checked; | |
7272e49f KL |
178 | } |
179 | ||
180 | } |