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
.event
.TKeypressEvent
;
37 import jexer
.event
.TMouseEvent
;
38 import jexer
.event
.TMenuEvent
;
39 import static jexer
.TKeypress
.*;
42 * TMenuItem implements a menu item.
44 public class TMenuItem
extends TWidget
{
46 // ------------------------------------------------------------------------
47 // Variables --------------------------------------------------------------
48 // ------------------------------------------------------------------------
51 * Label for this menu item.
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.
59 private int id
= TMenu
.MID_UNUSED
;
62 * When true, this item can be checked or unchecked.
64 private boolean checkable
= false;
67 * When true, this item is checked.
69 private boolean checked
= false;
72 * Global shortcut key.
74 private TKeypress key
;
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.
80 private MnemonicString mnemonic
;
82 // ------------------------------------------------------------------------
83 // Constructors -----------------------------------------------------------
84 // ------------------------------------------------------------------------
87 * Package private constructor.
89 * @param parent parent widget
91 * @param x column relative to parent
92 * @param y row relative to parent
93 * @param label menu item title
95 TMenuItem(final TMenu parent
, final int id
, final int x
, final int y
,
98 // Set parent and window
101 mnemonic
= new MnemonicString(label
);
106 this.label
= mnemonic
.getRawLabel();
107 setWidth(label
.length() + 4);
110 // Default state for some known menu items
119 case TMenu
.MID_PASTE
:
122 case TMenu
.MID_CLEAR
:
128 case TMenu
.MID_CASCADE
:
130 case TMenu
.MID_CLOSE_ALL
:
132 case TMenu
.MID_WINDOW_MOVE
:
134 case TMenu
.MID_WINDOW_ZOOM
:
136 case TMenu
.MID_WINDOW_NEXT
:
138 case TMenu
.MID_WINDOW_PREVIOUS
:
140 case TMenu
.MID_WINDOW_CLOSE
:
148 // ------------------------------------------------------------------------
149 // Event handlers ---------------------------------------------------------
150 // ------------------------------------------------------------------------
153 * Returns true if the mouse is currently on the menu item.
155 * @param mouse mouse event
156 * @return if true then the mouse is currently on this item
158 private boolean mouseOnMenuItem(final TMouseEvent mouse
) {
159 if ((mouse
.getY() == 0)
160 && (mouse
.getX() >= 0)
161 && (mouse
.getX() < getWidth())
169 * Handle mouse button releases.
171 * @param mouse mouse button release event
174 public void onMouseUp(final TMouseEvent mouse
) {
175 if ((mouseOnMenuItem(mouse
)) && (mouse
.isMouse1())) {
184 * @param keypress keystroke event
187 public void onKeypress(final TKeypressEvent keypress
) {
188 if (keypress
.equals(kbEnter
)) {
193 // Pass to parent for the things we don't care about.
194 super.onKeypress(keypress
);
197 // ------------------------------------------------------------------------
198 // TWidget ----------------------------------------------------------------
199 // ------------------------------------------------------------------------
202 * Draw a menu item with label.
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");
214 menuColor
= getTheme().getColor("tmenu");
215 menuMnemonicColor
= getTheme().getColor("tmenu.mnemonic");
217 menuColor
= getTheme().getColor("tmenu.disabled");
218 menuMnemonicColor
= getTheme().getColor("tmenu.disabled");
222 char cVSide
= GraphicsChars
.WINDOW_SIDE
;
223 vLineXY(0, 0, 1, cVSide
, background
);
224 vLineXY(getWidth() - 1, 0, 1, cVSide
, background
);
226 hLineXY(1, 0, getWidth() - 2, ' ', menuColor
);
227 putStringXY(2, 0, mnemonic
.getRawLabel(), menuColor
);
229 String keyLabel
= key
.toString();
230 putStringXY((getWidth() - keyLabel
.length() - 2), 0, keyLabel
,
233 if (mnemonic
.getShortcutIdx() >= 0) {
234 putCharXY(2 + mnemonic
.getShortcutIdx(), 0, mnemonic
.getShortcut(),
239 putCharXY(1, 0, GraphicsChars
.CHECK
, menuColor
);
244 // ------------------------------------------------------------------------
245 // TMenuItem --------------------------------------------------------------
246 // ------------------------------------------------------------------------
249 * Get the menu item ID.
253 public final int getId() {
258 * Set checkable flag.
260 * @param checkable if true, this menu item can be checked/unchecked
262 public final void setCheckable(final boolean checkable
) {
263 this.checkable
= checkable
;
267 * Get the mnemonic string for this menu item.
269 * @return mnemonic string
271 public final MnemonicString
getMnemonic() {
276 * Get a global accelerator key for this menu item.
278 * @return global keyboard accelerator, or null if no key is associated
281 public final TKeypress
getKey() {
286 * Set a global accelerator key for this menu item.
288 * @param key global keyboard accelerator
290 public final void setKey(final TKeypress key
) {
294 int newWidth
= (label
.length() + 4 + key
.toString().length() + 2);
295 if (newWidth
> getWidth()) {
302 * Dispatch event(s) due to selection or click.
304 public void dispatch() {
305 assert (isEnabled());
307 getApplication().postMenuEvent(new TMenuEvent(id
));