retrofit from gjexer
[nikiroo-utils.git] / src / jexer / TCheckBox.java
CommitLineData
daa4106c 1/*
7272e49f
KL
2 * Jexer - Java Text User Interface
3 *
e16dda65 4 * The MIT License (MIT)
7272e49f 5 *
a69ed767 6 * Copyright (C) 2019 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 */
29package jexer;
30
00691e80
KL
31import static jexer.TKeypress.kbEnter;
32import static jexer.TKeypress.kbEsc;
7c870d89 33import static jexer.TKeypress.kbSpace;
7272e49f
KL
34import jexer.bits.CellAttributes;
35import jexer.bits.GraphicsChars;
00691e80 36import jexer.bits.MnemonicString;
7272e49f
KL
37import jexer.event.TKeypressEvent;
38import jexer.event.TMouseEvent;
7272e49f
KL
39
40/**
051e2913 41 * TCheckBox implements an on/off checkbox.
7272e49f 42 */
051e2913 43public class TCheckBox extends TWidget {
7272e49f 44
d36057df
KL
45 // ------------------------------------------------------------------------
46 // Variables --------------------------------------------------------------
47 // ------------------------------------------------------------------------
48
7272e49f 49 /**
615a0d99 50 * CheckBox state, true means checked.
7272e49f
KL
51 */
52 private boolean checked = false;
53
54 /**
00691e80 55 * The shortcut and checkbox label.
7272e49f 56 */
00691e80 57 private MnemonicString mnemonic;
7272e49f 58
a69ed767
KL
59 /**
60 * If true, use the window's background color.
61 */
62 private boolean useWindowBackground = false;
63
d36057df
KL
64 // ------------------------------------------------------------------------
65 // Constructors -----------------------------------------------------------
66 // ------------------------------------------------------------------------
67
7272e49f
KL
68 /**
69 * Public constructor.
70 *
71 * @param parent parent widget
72 * @param x column relative to parent
73 * @param y row relative to parent
74 * @param label label to display next to (right of) the checkbox
75 * @param checked initial check state
76 */
051e2913 77 public TCheckBox(final TWidget parent, final int x, final int y,
d36057df 78 final String label, final boolean checked) {
7272e49f
KL
79
80 // Set parent and window
a83fea2b 81 super(parent, x, y, label.length() + 4, 1);
7272e49f 82
00691e80 83 mnemonic = new MnemonicString(label);
7272e49f
KL
84 this.checked = checked;
85
7c870d89 86 setCursorVisible(true);
7272e49f
KL
87 setCursorX(1);
88 }
89
d36057df
KL
90 // ------------------------------------------------------------------------
91 // Event handlers ---------------------------------------------------------
92 // ------------------------------------------------------------------------
93
7272e49f
KL
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 */
615a0d99 100 private boolean mouseOnCheckBox(final TMouseEvent mouse) {
00d2622b 101 if ((mouse.getY() == 0)
d36057df
KL
102 && (mouse.getX() >= 0)
103 && (mouse.getX() <= 2)
104 ) {
7272e49f
KL
105 return true;
106 }
107 return false;
108 }
109
d36057df
KL
110 /**
111 * Handle mouse checkbox presses.
112 *
113 * @param mouse mouse button down event
114 */
115 @Override
116 public void onMouseDown(final TMouseEvent mouse) {
615a0d99 117 if ((mouseOnCheckBox(mouse)) && (mouse.isMouse1())) {
d36057df
KL
118 // Switch state
119 checked = !checked;
120 }
121 }
122
123 /**
124 * Handle keystrokes.
125 *
126 * @param keypress keystroke event
127 */
128 @Override
129 public void onKeypress(final TKeypressEvent keypress) {
00691e80
KL
130 if (keypress.equals(kbSpace)
131 || keypress.equals(kbEnter)
132 ) {
d36057df
KL
133 checked = !checked;
134 return;
135 }
136
00691e80
KL
137 if (keypress.equals(kbEsc)) {
138 checked = false;
139 return;
140 }
141
d36057df
KL
142 // Pass to parent for the things we don't care about.
143 super.onKeypress(keypress);
144 }
145
146 // ------------------------------------------------------------------------
147 // TWidget ----------------------------------------------------------------
148 // ------------------------------------------------------------------------
149
7272e49f
KL
150 /**
151 * Draw a checkbox with label.
152 */
153 @Override
154 public void draw() {
155 CellAttributes checkboxColor;
00691e80 156 CellAttributes mnemonicColor;
7272e49f 157
7c870d89 158 if (isAbsoluteActive()) {
7272e49f 159 checkboxColor = getTheme().getColor("tcheckbox.active");
00691e80 160 mnemonicColor = getTheme().getColor("tcheckbox.mnemonic.highlighted");
7272e49f
KL
161 } else {
162 checkboxColor = getTheme().getColor("tcheckbox.inactive");
00691e80 163 mnemonicColor = getTheme().getColor("tcheckbox.mnemonic");
7272e49f 164 }
a69ed767
KL
165 if (useWindowBackground) {
166 CellAttributes background = getWindow().getBackground();
167 checkboxColor.setBackColor(background.getBackColor());
168 }
7272e49f 169
a69ed767 170 putCharXY(0, 0, '[', checkboxColor);
7272e49f 171 if (checked) {
a69ed767 172 putCharXY(1, 0, GraphicsChars.CHECK, checkboxColor);
7272e49f 173 } else {
a69ed767 174 putCharXY(1, 0, ' ', checkboxColor);
7272e49f 175 }
a69ed767 176 putCharXY(2, 0, ']', checkboxColor);
00691e80
KL
177 putStringXY(4, 0, mnemonic.getRawLabel(), checkboxColor);
178 if (mnemonic.getShortcutIdx() >= 0) {
179 putCharXY(4 + mnemonic.getShortcutIdx(), 0,
180 mnemonic.getShortcut(), mnemonicColor);
181 }
7272e49f
KL
182 }
183
d36057df 184 // ------------------------------------------------------------------------
051e2913 185 // TCheckBox --------------------------------------------------------------
d36057df
KL
186 // ------------------------------------------------------------------------
187
7272e49f 188 /**
d36057df 189 * Get checked value.
7272e49f 190 *
d36057df 191 * @return if true, this is checked
7272e49f 192 */
d36057df
KL
193 public boolean isChecked() {
194 return checked;
7272e49f
KL
195 }
196
197 /**
d36057df 198 * Set checked value.
7272e49f 199 *
d36057df 200 * @param checked new checked value.
7272e49f 201 */
d36057df
KL
202 public void setChecked(final boolean checked) {
203 this.checked = checked;
7272e49f
KL
204 }
205
00691e80
KL
206 /**
207 * Get the mnemonic string for this checkbox.
208 *
209 * @return mnemonic string
210 */
211 public MnemonicString getMnemonic() {
212 return mnemonic;
213 }
214
7272e49f 215}