Widgets: fix Z-Order on up/down mouse events
[fanfix.git] / src / jexer / TRadioButton.java
... / ...
CommitLineData
1/*
2 * Jexer - Java Text User Interface
3 *
4 * The MIT License (MIT)
5 *
6 * Copyright (C) 2017 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 */
29package jexer;
30
31import jexer.bits.CellAttributes;
32import jexer.bits.GraphicsChars;
33import jexer.event.TKeypressEvent;
34import jexer.event.TMouseEvent;
35import static jexer.TKeypress.*;
36
37/**
38 * TRadioButton implements a selectable radio button.
39 */
40public final class TRadioButton extends TWidget {
41
42 /**
43 * RadioButton state, true means selected.
44 */
45 private boolean selected = false;
46
47 /**
48 * Get RadioButton state, true means selected.
49 *
50 * @return if true then this is the one button in the group that is
51 * selected
52 */
53 public boolean isSelected() {
54 return selected;
55 }
56
57 /**
58 * Set RadioButton state, true means selected. Note package private
59 * access.
60 *
61 * @param selected if true then this is the one button in the group that
62 * is selected
63 */
64 void setSelected(final boolean selected) {
65 this.selected = selected;
66 }
67
68 /**
69 * Label for this radio button.
70 */
71 private String label;
72
73 /**
74 * ID for this radio button. Buttons start counting at 1 in the
75 * RadioGroup.
76 */
77 private int id;
78
79 /**
80 * Get ID for this radio button. Buttons start counting at 1 in the
81 * RadioGroup.
82 *
83 * @return the ID
84 */
85 public int getId() {
86 return id;
87 }
88
89 /**
90 * Public constructor.
91 *
92 * @param parent parent widget
93 * @param x column relative to parent
94 * @param y row relative to parent
95 * @param label label to display next to (right of) the radiobutton
96 * @param id ID for this radio button
97 */
98 public TRadioButton(final TRadioGroup parent, final int x, final int y,
99 final String label, final int id) {
100
101 // Set parent and window
102 super(parent, x, y, label.length() + 4, 1);
103
104 this.label = label;
105 this.id = id;
106
107 setCursorVisible(true);
108 setCursorX(1);
109 }
110
111 /**
112 * Returns true if the mouse is currently on the radio button.
113 *
114 * @param mouse mouse event
115 * @return if true the mouse is currently on the radio button
116 */
117 private boolean mouseOnRadioButton(final TMouseEvent mouse) {
118 if ((mouse.getY() == 0)
119 && (mouse.getX() >= 0)
120 && (mouse.getX() <= 2)
121 ) {
122 return true;
123 }
124 return false;
125 }
126
127 /**
128 * Draw a radio button with label.
129 */
130 @Override
131 public void draw() {
132 CellAttributes radioButtonColor;
133
134 if (isAbsoluteActive()) {
135 radioButtonColor = getTheme().getColor("tradiobutton.active");
136 } else {
137 radioButtonColor = getTheme().getColor("tradiobutton.inactive");
138 }
139
140 getScreen().putCharXY(0, 0, '(', radioButtonColor);
141 if (selected) {
142 getScreen().putCharXY(1, 0, GraphicsChars.CP437[0x07],
143 radioButtonColor);
144 } else {
145 getScreen().putCharXY(1, 0, ' ', radioButtonColor);
146 }
147 getScreen().putCharXY(2, 0, ')', radioButtonColor);
148 getScreen().putStringXY(4, 0, label, radioButtonColor);
149 }
150
151 /**
152 * Handle mouse button presses.
153 *
154 * @param mouse mouse button press event
155 */
156 @Override
157 public void onMouseDown(final TMouseEvent mouse) {
158 if ((mouseOnRadioButton(mouse)) && (mouse.isMouse1())) {
159 // Switch state
160 selected = !selected;
161 if (selected) {
162 ((TRadioGroup) getParent()).setSelected(this);
163 }
164 }
165 }
166
167 /**
168 * Handle keystrokes.
169 *
170 * @param keypress keystroke event
171 */
172 @Override
173 public void onKeypress(final TKeypressEvent keypress) {
174
175 if (keypress.equals(kbSpace)) {
176 selected = !selected;
177 if (selected) {
178 ((TRadioGroup) getParent()).setSelected(this);
179 }
180 return;
181 }
182
183 // Pass to parent for the things we don't care about.
184 super.onKeypress(keypress);
185 }
186
187}