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