Prep for 2019 release
[fanfix.git] / src / jexer / TCheckBox.java
1 /*
2 * Jexer - Java Text User Interface
3 *
4 * The MIT License (MIT)
5 *
6 * Copyright (C) 2019 Kevin Lamonte
7 *
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:
14 *
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
17 *
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.
25 *
26 * @author Kevin Lamonte [kevin.lamonte@gmail.com]
27 * @version 1
28 */
29 package jexer;
30
31 import static jexer.TKeypress.kbSpace;
32 import jexer.bits.CellAttributes;
33 import jexer.bits.GraphicsChars;
34 import jexer.event.TKeypressEvent;
35 import jexer.event.TMouseEvent;
36
37 /**
38 * TCheckBox implements an on/off checkbox.
39 */
40 public class TCheckBox extends TWidget {
41
42 // ------------------------------------------------------------------------
43 // Variables --------------------------------------------------------------
44 // ------------------------------------------------------------------------
45
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
56 /**
57 * If true, use the window's background color.
58 */
59 private boolean useWindowBackground = false;
60
61 // ------------------------------------------------------------------------
62 // Constructors -----------------------------------------------------------
63 // ------------------------------------------------------------------------
64
65 /**
66 * Public constructor.
67 *
68 * @param parent parent widget
69 * @param x column relative to parent
70 * @param y row relative to parent
71 * @param label label to display next to (right of) the checkbox
72 * @param checked initial check state
73 */
74 public TCheckBox(final TWidget parent, final int x, final int y,
75 final String label, final boolean checked) {
76
77 // Set parent and window
78 super(parent, x, y, label.length() + 4, 1);
79
80 this.label = label;
81 this.checked = checked;
82
83 setCursorVisible(true);
84 setCursorX(1);
85 }
86
87 // ------------------------------------------------------------------------
88 // Event handlers ---------------------------------------------------------
89 // ------------------------------------------------------------------------
90
91 /**
92 * Returns true if the mouse is currently on the checkbox.
93 *
94 * @param mouse mouse event
95 * @return true if the mouse is currently on the checkbox
96 */
97 private boolean mouseOnCheckBox(final TMouseEvent mouse) {
98 if ((mouse.getY() == 0)
99 && (mouse.getX() >= 0)
100 && (mouse.getX() <= 2)
101 ) {
102 return true;
103 }
104 return false;
105 }
106
107 /**
108 * Handle mouse checkbox presses.
109 *
110 * @param mouse mouse button down event
111 */
112 @Override
113 public void onMouseDown(final TMouseEvent mouse) {
114 if ((mouseOnCheckBox(mouse)) && (mouse.isMouse1())) {
115 // Switch state
116 checked = !checked;
117 }
118 }
119
120 /**
121 * Handle keystrokes.
122 *
123 * @param keypress keystroke event
124 */
125 @Override
126 public void onKeypress(final TKeypressEvent keypress) {
127 if (keypress.equals(kbSpace)) {
128 checked = !checked;
129 return;
130 }
131
132 // Pass to parent for the things we don't care about.
133 super.onKeypress(keypress);
134 }
135
136 // ------------------------------------------------------------------------
137 // TWidget ----------------------------------------------------------------
138 // ------------------------------------------------------------------------
139
140 /**
141 * Draw a checkbox with label.
142 */
143 @Override
144 public void draw() {
145 CellAttributes checkboxColor;
146
147 if (isAbsoluteActive()) {
148 checkboxColor = getTheme().getColor("tcheckbox.active");
149 } else {
150 checkboxColor = getTheme().getColor("tcheckbox.inactive");
151 }
152 if (useWindowBackground) {
153 CellAttributes background = getWindow().getBackground();
154 checkboxColor.setBackColor(background.getBackColor());
155 }
156
157 putCharXY(0, 0, '[', checkboxColor);
158 if (checked) {
159 putCharXY(1, 0, GraphicsChars.CHECK, checkboxColor);
160 } else {
161 putCharXY(1, 0, ' ', checkboxColor);
162 }
163 putCharXY(2, 0, ']', checkboxColor);
164 putStringXY(4, 0, label, checkboxColor);
165 }
166
167 // ------------------------------------------------------------------------
168 // TCheckBox --------------------------------------------------------------
169 // ------------------------------------------------------------------------
170
171 /**
172 * Get checked value.
173 *
174 * @return if true, this is checked
175 */
176 public boolean isChecked() {
177 return checked;
178 }
179
180 /**
181 * Set checked value.
182 *
183 * @param checked new checked value.
184 */
185 public void setChecked(final boolean checked) {
186 this.checked = checked;
187 }
188
189 }