ttable navigation minimally working
[fanfix.git] / src / jexer / menu / TMenuItem.java
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.menu;
30
31 import jexer.TKeypress;
32 import jexer.TWidget;
33 import jexer.bits.CellAttributes;
34 import jexer.bits.GraphicsChars;
35 import jexer.bits.MnemonicString;
36 import jexer.event.TKeypressEvent;
37 import jexer.event.TMouseEvent;
38 import jexer.event.TMenuEvent;
39 import static jexer.TKeypress.*;
40
41 /**
42 * TMenuItem implements a menu item.
43 */
44 public class TMenuItem extends TWidget {
45
46 // ------------------------------------------------------------------------
47 // Variables --------------------------------------------------------------
48 // ------------------------------------------------------------------------
49
50 /**
51 * Label for this menu item.
52 */
53 private String label;
54
55 /**
56 * Menu ID. IDs less than 1024 are reserved for common system
57 * functions. Existing ones are defined in TMenu, i.e. TMenu.MID_EXIT.
58 */
59 private int id = TMenu.MID_UNUSED;
60
61 /**
62 * When true, this item can be checked or unchecked.
63 */
64 private boolean checkable = false;
65
66 /**
67 * When true, this item is checked.
68 */
69 private boolean checked = false;
70
71 /**
72 * Global shortcut key.
73 */
74 private TKeypress key;
75
76 /**
77 * The title string. Use '&' to specify a mnemonic, i.e. "&File" will
78 * highlight the 'F' and allow 'f' or 'F' to select it.
79 */
80 private MnemonicString mnemonic;
81
82 // ------------------------------------------------------------------------
83 // Constructors -----------------------------------------------------------
84 // ------------------------------------------------------------------------
85
86 /**
87 * Package private constructor.
88 *
89 * @param parent parent widget
90 * @param id menu id
91 * @param x column relative to parent
92 * @param y row relative to parent
93 * @param label menu item title
94 */
95 TMenuItem(final TMenu parent, final int id, final int x, final int y,
96 final String label) {
97
98 // Set parent and window
99 super(parent);
100
101 mnemonic = new MnemonicString(label);
102
103 setX(x);
104 setY(y);
105 setHeight(1);
106 this.label = mnemonic.getRawLabel();
107 setWidth(label.length() + 4);
108 this.id = id;
109
110 // Default state for some known menu items
111 switch (id) {
112
113 case TMenu.MID_CUT:
114 setEnabled(false);
115 break;
116 case TMenu.MID_COPY:
117 setEnabled(false);
118 break;
119 case TMenu.MID_PASTE:
120 setEnabled(false);
121 break;
122 case TMenu.MID_CLEAR:
123 setEnabled(false);
124 break;
125
126 case TMenu.MID_TILE:
127 break;
128 case TMenu.MID_CASCADE:
129 break;
130 case TMenu.MID_CLOSE_ALL:
131 break;
132 case TMenu.MID_WINDOW_MOVE:
133 break;
134 case TMenu.MID_WINDOW_ZOOM:
135 break;
136 case TMenu.MID_WINDOW_NEXT:
137 break;
138 case TMenu.MID_WINDOW_PREVIOUS:
139 break;
140 case TMenu.MID_WINDOW_CLOSE:
141 break;
142 default:
143 break;
144 }
145
146 }
147
148 // ------------------------------------------------------------------------
149 // Event handlers ---------------------------------------------------------
150 // ------------------------------------------------------------------------
151
152 /**
153 * Returns true if the mouse is currently on the menu item.
154 *
155 * @param mouse mouse event
156 * @return if true then the mouse is currently on this item
157 */
158 private boolean mouseOnMenuItem(final TMouseEvent mouse) {
159 if ((mouse.getY() == 0)
160 && (mouse.getX() >= 0)
161 && (mouse.getX() < getWidth())
162 ) {
163 return true;
164 }
165 return false;
166 }
167
168 /**
169 * Handle mouse button releases.
170 *
171 * @param mouse mouse button release event
172 */
173 @Override
174 public void onMouseUp(final TMouseEvent mouse) {
175 if ((mouseOnMenuItem(mouse)) && (mouse.isMouse1())) {
176 dispatch();
177 return;
178 }
179 }
180
181 /**
182 * Handle keystrokes.
183 *
184 * @param keypress keystroke event
185 */
186 @Override
187 public void onKeypress(final TKeypressEvent keypress) {
188 if (keypress.equals(kbEnter)) {
189 dispatch();
190 return;
191 }
192
193 // Pass to parent for the things we don't care about.
194 super.onKeypress(keypress);
195 }
196
197 // ------------------------------------------------------------------------
198 // TWidget ----------------------------------------------------------------
199 // ------------------------------------------------------------------------
200
201 /**
202 * Draw a menu item with label.
203 */
204 @Override
205 public void draw() {
206 CellAttributes background = getTheme().getColor("tmenu");
207 CellAttributes menuColor;
208 CellAttributes menuMnemonicColor;
209 if (isAbsoluteActive()) {
210 menuColor = getTheme().getColor("tmenu.highlighted");
211 menuMnemonicColor = getTheme().getColor("tmenu.mnemonic.highlighted");
212 } else {
213 if (isEnabled()) {
214 menuColor = getTheme().getColor("tmenu");
215 menuMnemonicColor = getTheme().getColor("tmenu.mnemonic");
216 } else {
217 menuColor = getTheme().getColor("tmenu.disabled");
218 menuMnemonicColor = getTheme().getColor("tmenu.disabled");
219 }
220 }
221
222 char cVSide = GraphicsChars.WINDOW_SIDE;
223 vLineXY(0, 0, 1, cVSide, background);
224 vLineXY(getWidth() - 1, 0, 1, cVSide, background);
225
226 hLineXY(1, 0, getWidth() - 2, ' ', menuColor);
227 putStringXY(2, 0, mnemonic.getRawLabel(), menuColor);
228 if (key != null) {
229 String keyLabel = key.toString();
230 putStringXY((getWidth() - keyLabel.length() - 2), 0, keyLabel,
231 menuColor);
232 }
233 if (mnemonic.getShortcutIdx() >= 0) {
234 putCharXY(2 + mnemonic.getShortcutIdx(), 0, mnemonic.getShortcut(),
235 menuMnemonicColor);
236 }
237 if (checked) {
238 assert (checkable);
239 putCharXY(1, 0, GraphicsChars.CHECK, menuColor);
240 }
241
242 }
243
244 // ------------------------------------------------------------------------
245 // TMenuItem --------------------------------------------------------------
246 // ------------------------------------------------------------------------
247
248 /**
249 * Get the menu item ID.
250 *
251 * @return the id
252 */
253 public final int getId() {
254 return id;
255 }
256
257 /**
258 * Set checkable flag.
259 *
260 * @param checkable if true, this menu item can be checked/unchecked
261 */
262 public final void setCheckable(final boolean checkable) {
263 this.checkable = checkable;
264 }
265
266 /**
267 * Get checkable flag.
268 *
269 * @return true if this menu item is both checkable and checked
270 */
271 public final boolean getChecked() {
272 return ((checkable == true) && (checked == true));
273 }
274
275 /**
276 * Set checked flag. Note that setting checked on an item checkable will
277 * do nothing.
278 *
279 * @param checked if true, and if this menu item is checkable, then
280 * getChecked() will return true
281 */
282 public final void setChecked(final boolean checked) {
283 if (checkable) {
284 this.checked = checked;
285 } else {
286 this.checked = false;
287 }
288 }
289
290 /**
291 * Get the mnemonic string for this menu item.
292 *
293 * @return mnemonic string
294 */
295 public final MnemonicString getMnemonic() {
296 return mnemonic;
297 }
298
299 /**
300 * Get a global accelerator key for this menu item.
301 *
302 * @return global keyboard accelerator, or null if no key is associated
303 * with this item
304 */
305 public final TKeypress getKey() {
306 return key;
307 }
308
309 /**
310 * Set a global accelerator key for this menu item.
311 *
312 * @param key global keyboard accelerator
313 */
314 public final void setKey(final TKeypress key) {
315 this.key = key;
316
317 if (key != null) {
318 int newWidth = (label.length() + 4 + key.toString().length() + 2);
319 if (newWidth > getWidth()) {
320 setWidth(newWidth);
321 }
322 }
323 }
324
325 /**
326 * Dispatch event(s) due to selection or click.
327 */
328 public void dispatch() {
329 assert (isEnabled());
330
331 getApplication().postMenuEvent(new TMenuEvent(id));
332 if (checkable) {
333 checked = !checked;
334 }
335 }
336
337 }