Commit | Line | Data |
---|---|---|
daa4106c | 1 | /* |
7272e49f KL |
2 | * Jexer - Java Text User Interface |
3 | * | |
e16dda65 | 4 | * The MIT License (MIT) |
7272e49f | 5 | * |
e16dda65 | 6 | * Copyright (C) 2016 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 | /** | |
38 | * TCheckbox implements an on/off checkbox. | |
39 | */ | |
40 | public final class TCheckbox extends TWidget { | |
41 | ||
42 | /** | |
43 | * Checkbox state, true means checked. | |
44 | */ | |
45 | private boolean checked = false; | |
46 | ||
7c870d89 KL |
47 | /** |
48 | * Get checked value. | |
49 | * | |
50 | * @return if true, this is checked | |
51 | */ | |
329fd62e | 52 | public boolean isChecked() { |
7c870d89 KL |
53 | return checked; |
54 | } | |
55 | ||
56 | /** | |
57 | * Set checked value. | |
58 | * | |
59 | * @param checked new checked value. | |
60 | */ | |
329fd62e | 61 | public void setChecked(final boolean checked) { |
7c870d89 KL |
62 | this.checked = checked; |
63 | } | |
64 | ||
7272e49f KL |
65 | /** |
66 | * Label for this checkbox. | |
67 | */ | |
68 | private String label; | |
69 | ||
70 | /** | |
71 | * Public constructor. | |
72 | * | |
73 | * @param parent parent widget | |
74 | * @param x column relative to parent | |
75 | * @param y row relative to parent | |
76 | * @param label label to display next to (right of) the checkbox | |
77 | * @param checked initial check state | |
78 | */ | |
79 | public TCheckbox(final TWidget parent, final int x, final int y, | |
7c870d89 | 80 | final String label, final boolean checked) { |
7272e49f KL |
81 | |
82 | // Set parent and window | |
a83fea2b | 83 | super(parent, x, y, label.length() + 4, 1); |
7272e49f | 84 | |
7272e49f | 85 | this.label = label; |
7272e49f KL |
86 | this.checked = checked; |
87 | ||
7c870d89 | 88 | setCursorVisible(true); |
7272e49f KL |
89 | setCursorX(1); |
90 | } | |
91 | ||
92 | /** | |
93 | * Returns true if the mouse is currently on the checkbox. | |
94 | * | |
95 | * @param mouse mouse event | |
96 | * @return true if the mouse is currently on the checkbox | |
97 | */ | |
98 | private boolean mouseOnCheckbox(final TMouseEvent mouse) { | |
00d2622b | 99 | if ((mouse.getY() == 0) |
7c870d89 KL |
100 | && (mouse.getX() >= 0) |
101 | && (mouse.getX() <= 2) | |
102 | ) { | |
7272e49f KL |
103 | return true; |
104 | } | |
105 | return false; | |
106 | } | |
107 | ||
108 | /** | |
109 | * Draw a checkbox with label. | |
110 | */ | |
111 | @Override | |
112 | public void draw() { | |
113 | CellAttributes checkboxColor; | |
114 | ||
7c870d89 | 115 | if (isAbsoluteActive()) { |
7272e49f KL |
116 | checkboxColor = getTheme().getColor("tcheckbox.active"); |
117 | } else { | |
118 | checkboxColor = getTheme().getColor("tcheckbox.inactive"); | |
119 | } | |
120 | ||
121 | getScreen().putCharXY(0, 0, '[', checkboxColor); | |
122 | if (checked) { | |
123 | getScreen().putCharXY(1, 0, GraphicsChars.CHECK, checkboxColor); | |
124 | } else { | |
125 | getScreen().putCharXY(1, 0, ' ', checkboxColor); | |
126 | } | |
127 | getScreen().putCharXY(2, 0, ']', checkboxColor); | |
0d47c546 | 128 | getScreen().putStringXY(4, 0, label, checkboxColor); |
7272e49f KL |
129 | } |
130 | ||
131 | /** | |
132 | * Handle mouse checkbox presses. | |
133 | * | |
134 | * @param mouse mouse button down event | |
135 | */ | |
136 | @Override | |
137 | public void onMouseDown(final TMouseEvent mouse) { | |
7c870d89 | 138 | if ((mouseOnCheckbox(mouse)) && (mouse.isMouse1())) { |
7272e49f KL |
139 | // Switch state |
140 | checked = !checked; | |
141 | } | |
142 | } | |
143 | ||
144 | /** | |
145 | * Handle keystrokes. | |
146 | * | |
147 | * @param keypress keystroke event | |
148 | */ | |
149 | @Override | |
150 | public void onKeypress(final TKeypressEvent keypress) { | |
151 | if (keypress.equals(kbSpace)) { | |
152 | checked = !checked; | |
153 | return; | |
154 | } | |
155 | ||
156 | // Pass to parent for the things we don't care about. | |
157 | super.onKeypress(keypress); | |
158 | } | |
159 | ||
160 | } |