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 jexer
.TKeypress
;
33 import jexer
.bits
.CellAttributes
;
34 import jexer
.bits
.GraphicsChars
;
35 import jexer
.bits
.MnemonicString
;
36 import jexer
.bits
.StringUtils
;
37 import jexer
.event
.TKeypressEvent
;
38 import jexer
.event
.TMouseEvent
;
39 import jexer
.event
.TMenuEvent
;
40 import static jexer
.TKeypress
.*;
43 * TMenuItem implements a menu item.
45 public class TMenuItem
extends TWidget
{
47 // ------------------------------------------------------------------------
48 // Variables --------------------------------------------------------------
49 // ------------------------------------------------------------------------
52 * Label for this menu item.
57 * Menu ID. IDs less than 1024 are reserved for common system
58 * functions. Existing ones are defined in TMenu, i.e. TMenu.MID_EXIT.
60 private int id
= TMenu
.MID_UNUSED
;
63 * When true, this item can be checked or unchecked.
65 private boolean checkable
= false;
68 * When true, this item is checked.
70 private boolean checked
= false;
73 * Global shortcut key.
75 private TKeypress key
;
78 * The title string. Use '&' to specify a mnemonic, i.e. "&File" will
79 * highlight the 'F' and allow 'f' or 'F' to select it.
81 private MnemonicString mnemonic
;
84 * An optional 2-cell-wide picture/icon for this item.
86 private int icon
= -1;
88 // ------------------------------------------------------------------------
89 // Constructors -----------------------------------------------------------
90 // ------------------------------------------------------------------------
93 * Package private constructor.
95 * @param parent parent widget
97 * @param x column relative to parent
98 * @param y row relative to parent
99 * @param label menu item title
101 TMenuItem(final TMenu parent
, final int id
, final int x
, final int y
,
102 final String label
) {
104 this(parent
, id
, x
, y
, label
, -1);
108 * Package private constructor.
110 * @param parent parent widget
112 * @param x column relative to parent
113 * @param y row relative to parent
114 * @param label menu item title
115 * @param icon icon picture/emoji
117 TMenuItem(final TMenu parent
, final int id
, final int x
, final int y
,
118 final String label
, final int icon
) {
120 // Set parent and window
123 mnemonic
= new MnemonicString(label
);
128 this.label
= mnemonic
.getRawLabel();
129 if (parent
.useIcons
) {
130 setWidth(StringUtils
.width(label
) + 6);
132 setWidth(StringUtils
.width(label
) + 4);
137 // Default state for some known menu items
146 case TMenu
.MID_PASTE
:
149 case TMenu
.MID_CLEAR
:
155 case TMenu
.MID_CASCADE
:
157 case TMenu
.MID_CLOSE_ALL
:
159 case TMenu
.MID_WINDOW_MOVE
:
161 case TMenu
.MID_WINDOW_ZOOM
:
163 case TMenu
.MID_WINDOW_NEXT
:
165 case TMenu
.MID_WINDOW_PREVIOUS
:
167 case TMenu
.MID_WINDOW_CLOSE
:
175 // ------------------------------------------------------------------------
176 // Event handlers ---------------------------------------------------------
177 // ------------------------------------------------------------------------
180 * Returns true if the mouse is currently on the menu item.
182 * @param mouse mouse event
183 * @return if true then the mouse is currently on this item
185 private boolean mouseOnMenuItem(final TMouseEvent mouse
) {
186 if ((mouse
.getY() == 0)
187 && (mouse
.getX() >= 0)
188 && (mouse
.getX() < getWidth())
196 * Handle mouse button releases.
198 * @param mouse mouse button release event
201 public void onMouseUp(final TMouseEvent mouse
) {
202 if ((mouseOnMenuItem(mouse
)) && (mouse
.isMouse1())) {
211 * @param keypress keystroke event
214 public void onKeypress(final TKeypressEvent keypress
) {
215 if (keypress
.equals(kbEnter
)) {
220 // Pass to parent for the things we don't care about.
221 super.onKeypress(keypress
);
224 // ------------------------------------------------------------------------
225 // TWidget ----------------------------------------------------------------
226 // ------------------------------------------------------------------------
229 * Draw a menu item with label.
233 CellAttributes background
= getTheme().getColor("tmenu");
234 CellAttributes menuColor
;
235 CellAttributes menuMnemonicColor
;
236 if (isAbsoluteActive()) {
237 menuColor
= getTheme().getColor("tmenu.highlighted");
238 menuMnemonicColor
= getTheme().getColor("tmenu.mnemonic.highlighted");
241 menuColor
= getTheme().getColor("tmenu");
242 menuMnemonicColor
= getTheme().getColor("tmenu.mnemonic");
244 menuColor
= getTheme().getColor("tmenu.disabled");
245 menuMnemonicColor
= getTheme().getColor("tmenu.disabled");
249 boolean useIcons
= ((TMenu
) getParent()).useIcons
;
251 char cVSide
= GraphicsChars
.WINDOW_SIDE
;
252 vLineXY(0, 0, 1, cVSide
, background
);
253 vLineXY(getWidth() - 1, 0, 1, cVSide
, background
);
255 hLineXY(1, 0, getWidth() - 2, ' ', menuColor
);
256 putStringXY(2 + (useIcons ?
2 : 0), 0, mnemonic
.getRawLabel(),
259 String keyLabel
= key
.toString();
260 putStringXY((getWidth() - StringUtils
.width(keyLabel
) - 2), 0,
261 keyLabel
, menuColor
);
263 if (mnemonic
.getScreenShortcutIdx() >= 0) {
264 putCharXY(2 + (useIcons ?
2 : 0) + mnemonic
.getScreenShortcutIdx(),
265 0, mnemonic
.getShortcut(), menuMnemonicColor
);
269 putCharXY(1, 0, GraphicsChars
.CHECK
, menuColor
);
271 if ((useIcons
== true) && (icon
!= -1)) {
272 putCharXY(2, 0, icon
, menuColor
);
276 // ------------------------------------------------------------------------
277 // TMenuItem --------------------------------------------------------------
278 // ------------------------------------------------------------------------
281 * Get the menu item ID.
285 public final int getId() {
290 * Set checkable flag.
292 * @param checkable if true, this menu item can be checked/unchecked
294 public final void setCheckable(final boolean checkable
) {
295 this.checkable
= checkable
;
299 * Get checkable flag.
301 * @return true if this menu item is both checkable and checked
303 public final boolean getChecked() {
304 return ((checkable
== true) && (checked
== true));
308 * Set checked flag. Note that setting checked on an item checkable will
311 * @param checked if true, and if this menu item is checkable, then
312 * getChecked() will return true
314 public final void setChecked(final boolean checked
) {
316 this.checked
= checked
;
318 this.checked
= false;
323 * Get the mnemonic string for this menu item.
325 * @return mnemonic string
327 public final MnemonicString
getMnemonic() {
332 * Get a global accelerator key for this menu item.
334 * @return global keyboard accelerator, or null if no key is associated
337 public final TKeypress
getKey() {
342 * Set a global accelerator key for this menu item.
344 * @param key global keyboard accelerator
346 public final void setKey(final TKeypress key
) {
350 int newWidth
= (StringUtils
.width(label
) + 4 +
351 StringUtils
.width(key
.toString()) + 2);
352 if (((TMenu
) getParent()).useIcons
) {
355 if (newWidth
> getWidth()) {
362 * Get a picture/emoji icon for this menu item.
364 * @return the codepoint, or -1 if no icon is specified for this menu
367 public final int getIcon() {
372 * Set a picture/emoji icon for this menu item.
374 * @param icon a codepoint, or -1 to unset the icon
376 public final void setIcon(final int icon
) {
381 * Dispatch event(s) due to selection or click.
383 public void dispatch() {
384 assert (isEnabled());
386 getApplication().postMenuEvent(new TMenuEvent(id
));