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
;
83 // ------------------------------------------------------------------------
84 // Constructors -----------------------------------------------------------
85 // ------------------------------------------------------------------------
88 * Package private constructor.
90 * @param parent parent widget
92 * @param x column relative to parent
93 * @param y row relative to parent
94 * @param label menu item title
96 TMenuItem(final TMenu parent
, final int id
, final int x
, final int y
,
99 // Set parent and window
102 mnemonic
= new MnemonicString(label
);
107 this.label
= mnemonic
.getRawLabel();
108 setWidth(StringUtils
.width(label
) + 4);
111 // Default state for some known menu items
120 case TMenu
.MID_PASTE
:
123 case TMenu
.MID_CLEAR
:
129 case TMenu
.MID_CASCADE
:
131 case TMenu
.MID_CLOSE_ALL
:
133 case TMenu
.MID_WINDOW_MOVE
:
135 case TMenu
.MID_WINDOW_ZOOM
:
137 case TMenu
.MID_WINDOW_NEXT
:
139 case TMenu
.MID_WINDOW_PREVIOUS
:
141 case TMenu
.MID_WINDOW_CLOSE
:
149 // ------------------------------------------------------------------------
150 // Event handlers ---------------------------------------------------------
151 // ------------------------------------------------------------------------
154 * Returns true if the mouse is currently on the menu item.
156 * @param mouse mouse event
157 * @return if true then the mouse is currently on this item
159 private boolean mouseOnMenuItem(final TMouseEvent mouse
) {
160 if ((mouse
.getY() == 0)
161 && (mouse
.getX() >= 0)
162 && (mouse
.getX() < getWidth())
170 * Handle mouse button releases.
172 * @param mouse mouse button release event
175 public void onMouseUp(final TMouseEvent mouse
) {
176 if ((mouseOnMenuItem(mouse
)) && (mouse
.isMouse1())) {
185 * @param keypress keystroke event
188 public void onKeypress(final TKeypressEvent keypress
) {
189 if (keypress
.equals(kbEnter
)) {
194 // Pass to parent for the things we don't care about.
195 super.onKeypress(keypress
);
198 // ------------------------------------------------------------------------
199 // TWidget ----------------------------------------------------------------
200 // ------------------------------------------------------------------------
203 * Draw a menu item with label.
207 CellAttributes background
= getTheme().getColor("tmenu");
208 CellAttributes menuColor
;
209 CellAttributes menuMnemonicColor
;
210 if (isAbsoluteActive()) {
211 menuColor
= getTheme().getColor("tmenu.highlighted");
212 menuMnemonicColor
= getTheme().getColor("tmenu.mnemonic.highlighted");
215 menuColor
= getTheme().getColor("tmenu");
216 menuMnemonicColor
= getTheme().getColor("tmenu.mnemonic");
218 menuColor
= getTheme().getColor("tmenu.disabled");
219 menuMnemonicColor
= getTheme().getColor("tmenu.disabled");
223 char cVSide
= GraphicsChars
.WINDOW_SIDE
;
224 vLineXY(0, 0, 1, cVSide
, background
);
225 vLineXY(getWidth() - 1, 0, 1, cVSide
, background
);
227 hLineXY(1, 0, getWidth() - 2, ' ', menuColor
);
228 putStringXY(2, 0, mnemonic
.getRawLabel(), menuColor
);
230 String keyLabel
= key
.toString();
231 putStringXY((getWidth() - StringUtils
.width(keyLabel
) - 2), 0,
232 keyLabel
, menuColor
);
234 if (mnemonic
.getScreenShortcutIdx() >= 0) {
235 putCharXY(2 + mnemonic
.getScreenShortcutIdx(), 0,
236 mnemonic
.getShortcut(), menuMnemonicColor
);
240 putCharXY(1, 0, GraphicsChars
.CHECK
, menuColor
);
245 // ------------------------------------------------------------------------
246 // TMenuItem --------------------------------------------------------------
247 // ------------------------------------------------------------------------
250 * Get the menu item ID.
254 public final int getId() {
259 * Set checkable flag.
261 * @param checkable if true, this menu item can be checked/unchecked
263 public final void setCheckable(final boolean checkable
) {
264 this.checkable
= checkable
;
268 * Get checkable flag.
270 * @return true if this menu item is both checkable and checked
272 public final boolean getChecked() {
273 return ((checkable
== true) && (checked
== true));
277 * Set checked flag. Note that setting checked on an item checkable will
280 * @param checked if true, and if this menu item is checkable, then
281 * getChecked() will return true
283 public final void setChecked(final boolean checked
) {
285 this.checked
= checked
;
287 this.checked
= false;
292 * Get the mnemonic string for this menu item.
294 * @return mnemonic string
296 public final MnemonicString
getMnemonic() {
301 * Get a global accelerator key for this menu item.
303 * @return global keyboard accelerator, or null if no key is associated
306 public final TKeypress
getKey() {
311 * Set a global accelerator key for this menu item.
313 * @param key global keyboard accelerator
315 public final void setKey(final TKeypress key
) {
319 int newWidth
= (StringUtils
.width(label
) + 4 +
320 StringUtils
.width(key
.toString()) + 2);
321 if (newWidth
> getWidth()) {
328 * Dispatch event(s) due to selection or click.
330 public void dispatch() {
331 assert (isEnabled());
333 getApplication().postMenuEvent(new TMenuEvent(id
));