| 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 jexer.bits.CellAttributes; |
| 32 | import jexer.bits.GraphicsChars; |
| 33 | import jexer.bits.MnemonicString; |
| 34 | import jexer.event.TKeypressEvent; |
| 35 | import jexer.event.TMouseEvent; |
| 36 | import static jexer.TKeypress.*; |
| 37 | |
| 38 | /** |
| 39 | * TRadioButton implements a selectable radio button. |
| 40 | * |
| 41 | * If the user clicks or presses space on this button, it is selected. |
| 42 | * |
| 43 | * If the user presses escape on this button, it is unselected. |
| 44 | */ |
| 45 | public class TRadioButton extends TWidget { |
| 46 | |
| 47 | // ------------------------------------------------------------------------ |
| 48 | // Variables -------------------------------------------------------------- |
| 49 | // ------------------------------------------------------------------------ |
| 50 | |
| 51 | /** |
| 52 | * RadioButton state, true means selected. |
| 53 | */ |
| 54 | private boolean selected = false; |
| 55 | |
| 56 | /** |
| 57 | * The shortcut and radio button label. |
| 58 | */ |
| 59 | private MnemonicString mnemonic; |
| 60 | |
| 61 | /** |
| 62 | * ID for this radio button. Buttons start counting at 1 in the |
| 63 | * RadioGroup. |
| 64 | */ |
| 65 | private int id; |
| 66 | |
| 67 | // ------------------------------------------------------------------------ |
| 68 | // Constructors ----------------------------------------------------------- |
| 69 | // ------------------------------------------------------------------------ |
| 70 | |
| 71 | /** |
| 72 | * Public constructor. |
| 73 | * |
| 74 | * @param parent parent widget |
| 75 | * @param x column relative to parent |
| 76 | * @param y row relative to parent |
| 77 | * @param label label to display next to (right of) the radiobutton |
| 78 | * @param id ID for this radio button |
| 79 | */ |
| 80 | public TRadioButton(final TRadioGroup parent, final int x, final int y, |
| 81 | final String label, final int id) { |
| 82 | |
| 83 | // Set parent and window |
| 84 | super(parent, x, y, label.length() + 4, 1); |
| 85 | |
| 86 | mnemonic = new MnemonicString(label); |
| 87 | this.id = id; |
| 88 | |
| 89 | setCursorVisible(true); |
| 90 | setCursorX(1); |
| 91 | } |
| 92 | |
| 93 | // ------------------------------------------------------------------------ |
| 94 | // Event handlers --------------------------------------------------------- |
| 95 | // ------------------------------------------------------------------------ |
| 96 | |
| 97 | /** |
| 98 | * Returns true if the mouse is currently on the radio button. |
| 99 | * |
| 100 | * @param mouse mouse event |
| 101 | * @return if true the mouse is currently on the radio button |
| 102 | */ |
| 103 | private boolean mouseOnRadioButton(final TMouseEvent mouse) { |
| 104 | if ((mouse.getY() == 0) |
| 105 | && (mouse.getX() >= 0) |
| 106 | && (mouse.getX() <= 2) |
| 107 | ) { |
| 108 | return true; |
| 109 | } |
| 110 | return false; |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * Handle mouse button presses. |
| 115 | * |
| 116 | * @param mouse mouse button press event |
| 117 | */ |
| 118 | @Override |
| 119 | public void onMouseDown(final TMouseEvent mouse) { |
| 120 | if ((mouseOnRadioButton(mouse)) && (mouse.isMouse1())) { |
| 121 | // Switch state |
| 122 | selected = true; |
| 123 | ((TRadioGroup) getParent()).setSelected(this); |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | /** |
| 128 | * Handle keystrokes. |
| 129 | * |
| 130 | * @param keypress keystroke event |
| 131 | */ |
| 132 | @Override |
| 133 | public void onKeypress(final TKeypressEvent keypress) { |
| 134 | |
| 135 | if (keypress.equals(kbSpace)) { |
| 136 | selected = true; |
| 137 | ((TRadioGroup) getParent()).setSelected(this); |
| 138 | return; |
| 139 | } |
| 140 | |
| 141 | if (keypress.equals(kbEsc)) { |
| 142 | TRadioGroup parent = (TRadioGroup) getParent(); |
| 143 | if (parent.requiresSelection == false) { |
| 144 | selected = false; |
| 145 | parent.setSelected(0); |
| 146 | } |
| 147 | return; |
| 148 | } |
| 149 | |
| 150 | // Pass to parent for the things we don't care about. |
| 151 | super.onKeypress(keypress); |
| 152 | } |
| 153 | |
| 154 | // ------------------------------------------------------------------------ |
| 155 | // TWidget ---------------------------------------------------------------- |
| 156 | // ------------------------------------------------------------------------ |
| 157 | |
| 158 | /** |
| 159 | * Draw a radio button with label. |
| 160 | */ |
| 161 | @Override |
| 162 | public void draw() { |
| 163 | CellAttributes radioButtonColor; |
| 164 | CellAttributes mnemonicColor; |
| 165 | |
| 166 | if (isAbsoluteActive()) { |
| 167 | radioButtonColor = getTheme().getColor("tradiobutton.active"); |
| 168 | mnemonicColor = getTheme().getColor("tradiobutton.mnemonic.highlighted"); |
| 169 | } else { |
| 170 | radioButtonColor = getTheme().getColor("tradiobutton.inactive"); |
| 171 | mnemonicColor = getTheme().getColor("tradiobutton.mnemonic"); |
| 172 | } |
| 173 | |
| 174 | putCharXY(0, 0, '(', radioButtonColor); |
| 175 | if (selected) { |
| 176 | putCharXY(1, 0, GraphicsChars.CP437[0x07], radioButtonColor); |
| 177 | } else { |
| 178 | putCharXY(1, 0, ' ', radioButtonColor); |
| 179 | } |
| 180 | putCharXY(2, 0, ')', radioButtonColor); |
| 181 | putStringXY(4, 0, mnemonic.getRawLabel(), radioButtonColor); |
| 182 | if (mnemonic.getShortcutIdx() >= 0) { |
| 183 | putCharXY(4 + mnemonic.getShortcutIdx(), 0, |
| 184 | mnemonic.getShortcut(), mnemonicColor); |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | // ------------------------------------------------------------------------ |
| 189 | // TRadioButton ----------------------------------------------------------- |
| 190 | // ------------------------------------------------------------------------ |
| 191 | |
| 192 | /** |
| 193 | * Get RadioButton state, true means selected. |
| 194 | * |
| 195 | * @return if true then this is the one button in the group that is |
| 196 | * selected |
| 197 | */ |
| 198 | public boolean isSelected() { |
| 199 | return selected; |
| 200 | } |
| 201 | |
| 202 | /** |
| 203 | * Set RadioButton state, true means selected. Note package private |
| 204 | * access. |
| 205 | * |
| 206 | * @param selected if true then this is the one button in the group that |
| 207 | * is selected |
| 208 | */ |
| 209 | void setSelected(final boolean selected) { |
| 210 | this.selected = selected; |
| 211 | } |
| 212 | |
| 213 | /** |
| 214 | * Get ID for this radio button. Buttons start counting at 1 in the |
| 215 | * RadioGroup. |
| 216 | * |
| 217 | * @return the ID |
| 218 | */ |
| 219 | public int getId() { |
| 220 | return id; |
| 221 | } |
| 222 | |
| 223 | /** |
| 224 | * Get the mnemonic string for this button. |
| 225 | * |
| 226 | * @return mnemonic string |
| 227 | */ |
| 228 | public MnemonicString getMnemonic() { |
| 229 | return mnemonic; |
| 230 | } |
| 231 | |
| 232 | } |