2 * Jexer - Java Text User Interface
4 * The MIT License (MIT)
6 * Copyright (C) 2019 Kevin Lamonte
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:
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
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.
26 * @author Kevin Lamonte [kevin.lamonte@gmail.com]
31 import static jexer
.TKeypress
.kbEnter
;
32 import static jexer
.TKeypress
.kbEsc
;
33 import static jexer
.TKeypress
.kbSpace
;
34 import jexer
.bits
.CellAttributes
;
35 import jexer
.bits
.GraphicsChars
;
36 import jexer
.bits
.MnemonicString
;
37 import jexer
.event
.TKeypressEvent
;
38 import jexer
.event
.TMouseEvent
;
41 * TCheckBox implements an on/off checkbox.
43 public class TCheckBox
extends TWidget
{
45 // ------------------------------------------------------------------------
46 // Variables --------------------------------------------------------------
47 // ------------------------------------------------------------------------
50 * CheckBox state, true means checked.
52 private boolean checked
= false;
55 * The shortcut and checkbox label.
57 private MnemonicString mnemonic
;
60 * If true, use the window's background color.
62 private boolean useWindowBackground
= false;
64 // ------------------------------------------------------------------------
65 // Constructors -----------------------------------------------------------
66 // ------------------------------------------------------------------------
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
77 public TCheckBox(final TWidget parent
, final int x
, final int y
,
78 final String label
, final boolean checked
) {
80 // Set parent and window
81 super(parent
, x
, y
, label
.length() + 4, 1);
83 mnemonic
= new MnemonicString(label
);
84 this.checked
= checked
;
86 setCursorVisible(true);
90 // ------------------------------------------------------------------------
91 // Event handlers ---------------------------------------------------------
92 // ------------------------------------------------------------------------
95 * Returns true if the mouse is currently on the checkbox.
97 * @param mouse mouse event
98 * @return true if the mouse is currently on the checkbox
100 private boolean mouseOnCheckBox(final TMouseEvent mouse
) {
101 if ((mouse
.getY() == 0)
102 && (mouse
.getX() >= 0)
103 && (mouse
.getX() <= 2)
111 * Handle mouse checkbox presses.
113 * @param mouse mouse button down event
116 public void onMouseDown(final TMouseEvent mouse
) {
117 if ((mouseOnCheckBox(mouse
)) && (mouse
.isMouse1())) {
126 * @param keypress keystroke event
129 public void onKeypress(final TKeypressEvent keypress
) {
130 if (keypress
.equals(kbSpace
)
131 || keypress
.equals(kbEnter
)
137 if (keypress
.equals(kbEsc
)) {
142 // Pass to parent for the things we don't care about.
143 super.onKeypress(keypress
);
146 // ------------------------------------------------------------------------
147 // TWidget ----------------------------------------------------------------
148 // ------------------------------------------------------------------------
151 * Draw a checkbox with label.
155 CellAttributes checkboxColor
;
156 CellAttributes mnemonicColor
;
158 if (isAbsoluteActive()) {
159 checkboxColor
= getTheme().getColor("tcheckbox.active");
160 mnemonicColor
= getTheme().getColor("tcheckbox.mnemonic.highlighted");
162 checkboxColor
= getTheme().getColor("tcheckbox.inactive");
163 mnemonicColor
= getTheme().getColor("tcheckbox.mnemonic");
165 if (useWindowBackground
) {
166 CellAttributes background
= getWindow().getBackground();
167 checkboxColor
.setBackColor(background
.getBackColor());
170 putCharXY(0, 0, '[', checkboxColor
);
172 putCharXY(1, 0, GraphicsChars
.CHECK
, checkboxColor
);
174 putCharXY(1, 0, ' ', checkboxColor
);
176 putCharXY(2, 0, ']', checkboxColor
);
177 putStringXY(4, 0, mnemonic
.getRawLabel(), checkboxColor
);
178 if (mnemonic
.getShortcutIdx() >= 0) {
179 putCharXY(4 + mnemonic
.getShortcutIdx(), 0,
180 mnemonic
.getShortcut(), mnemonicColor
);
184 // ------------------------------------------------------------------------
185 // TCheckBox --------------------------------------------------------------
186 // ------------------------------------------------------------------------
191 * @return if true, this is checked
193 public boolean isChecked() {
200 * @param checked new checked value.
202 public void setChecked(final boolean checked
) {
203 this.checked
= checked
;
207 * Get the mnemonic string for this checkbox.
209 * @return mnemonic string
211 public MnemonicString
getMnemonic() {