2 * Jexer - Java Text User Interface
4 * License: LGPLv3 or later
6 * This module is licensed under the GNU Lesser General Public License
7 * Version 3. Please see the file "COPYING" in this directory for more
8 * information about the GNU Lesser General Public License Version 3.
10 * Copyright (C) 2015 Kevin Lamonte
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU Lesser General Public License
14 * as published by the Free Software Foundation; either version 3 of
15 * the License, or (at your option) any later version.
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
22 * You should have received a copy of the GNU Lesser General Public
23 * License along with this program; if not, see
24 * http://www.gnu.org/licenses/, or write to the Free Software
25 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
28 * @author Kevin Lamonte [kevin.lamonte@gmail.com]
33 import jexer
.TKeypress
;
35 import jexer
.bits
.CellAttributes
;
36 import jexer
.bits
.GraphicsChars
;
37 import jexer
.bits
.MnemonicString
;
38 import jexer
.event
.TKeypressEvent
;
39 import jexer
.event
.TMouseEvent
;
40 import jexer
.event
.TMenuEvent
;
41 import static jexer
.TKeypress
.*;
44 * TMenuItem implements a menu item.
46 public class TMenuItem
extends TWidget
{
49 * Label for this menu item.
54 * Menu ID. IDs less than 1024 are reserved for common system
55 * functions. Existing ones are defined in TMenu, i.e. TMenu.MID_EXIT.
57 private int id
= TMenu
.MID_UNUSED
;
60 * Get the menu item ID.
64 public final int getId() {
69 * When true, this item can be checked or unchecked.
71 private boolean checkable
= false;
76 * @param checkable if true, this menu item can be checked/unchecked
78 public final void setCheckable(final boolean checkable
) {
79 this.checkable
= checkable
;
83 * When true, this item is checked.
85 private boolean checked
= false;
88 * Global shortcut key.
90 private TKeypress key
;
93 * The title string. Use '&' to specify a mnemonic, i.e. "&File" will
94 * highlight the 'F' and allow 'f' or 'F' to select it.
96 private MnemonicString mnemonic
;
99 * Get the mnemonic string for this menu item.
101 * @return mnemonic string
103 public final MnemonicString
getMnemonic() {
108 * Get a global accelerator key for this menu item.
110 * @return global keyboard accelerator, or null if no key is associated
113 public final TKeypress
getKey() {
118 * Set a global accelerator key for this menu item.
120 * @param key global keyboard accelerator
122 public final void setKey(final TKeypress key
) {
126 int newWidth
= (label
.length() + 4 + key
.toString().length() + 2);
127 if (newWidth
> getWidth()) {
134 * Package private constructor.
136 * @param parent parent widget
138 * @param x column relative to parent
139 * @param y row relative to parent
140 * @param label menu item title
142 TMenuItem(final TMenu parent
, final int id
, final int x
, final int y
,
143 final String label
) {
145 // Set parent and window
148 mnemonic
= new MnemonicString(label
);
153 this.label
= mnemonic
.getRawLabel();
154 setWidth(label
.length() + 4);
157 // Default state for some known menu items
166 case TMenu
.MID_PASTE
:
169 case TMenu
.MID_CLEAR
:
175 case TMenu
.MID_CASCADE
:
177 case TMenu
.MID_CLOSE_ALL
:
179 case TMenu
.MID_WINDOW_MOVE
:
181 case TMenu
.MID_WINDOW_ZOOM
:
183 case TMenu
.MID_WINDOW_NEXT
:
185 case TMenu
.MID_WINDOW_PREVIOUS
:
187 case TMenu
.MID_WINDOW_CLOSE
:
196 * Returns true if the mouse is currently on the menu item.
198 * @param mouse mouse event
200 private boolean mouseOnMenuItem(final TMouseEvent mouse
) {
201 if ((mouse
.getY() == 0)
202 && (mouse
.getX() >= 0)
203 && (mouse
.getX() < getWidth())
211 * Draw a menu item with label.
215 CellAttributes background
= getTheme().getColor("tmenu");
216 CellAttributes menuColor
;
217 CellAttributes menuMnemonicColor
;
218 if (isAbsoluteActive()) {
219 menuColor
= getTheme().getColor("tmenu.highlighted");
220 menuMnemonicColor
= getTheme().getColor("tmenu.mnemonic.highlighted");
223 menuColor
= getTheme().getColor("tmenu");
224 menuMnemonicColor
= getTheme().getColor("tmenu.mnemonic");
226 menuColor
= getTheme().getColor("tmenu.disabled");
227 menuMnemonicColor
= getTheme().getColor("tmenu.disabled");
231 char cVSide
= GraphicsChars
.WINDOW_SIDE
;
232 getScreen().vLineXY(0, 0, 1, cVSide
, background
);
233 getScreen().vLineXY(getWidth() - 1, 0, 1, cVSide
, background
);
235 getScreen().hLineXY(1, 0, getWidth() - 2, ' ', menuColor
);
236 getScreen().putStringXY(2, 0, mnemonic
.getRawLabel(), menuColor
);
238 String keyLabel
= key
.toString();
239 getScreen().putStringXY((getWidth() - keyLabel
.length() - 2), 0,
240 keyLabel
, menuColor
);
242 if (mnemonic
.getShortcutIdx() >= 0) {
243 getScreen().putCharXY(2 + mnemonic
.getShortcutIdx(), 0,
244 mnemonic
.getShortcut(), menuMnemonicColor
);
248 getScreen().putCharXY(1, 0, GraphicsChars
.CHECK
, menuColor
);
254 * Dispatch event(s) due to selection or click.
256 public void dispatch() {
257 assert (isEnabled());
259 getApplication().addMenuEvent(new TMenuEvent(id
));
266 * Handle mouse button presses.
268 * @param event mouse button press event
270 /* TODO: this was commented out in d-tui, why?
272 public void onMouseDown(final TMouseEvent event) {
273 if ((mouseOnMenuItem(event)) && (event.mouse1)) {
281 * Handle mouse button releases.
283 * @param mouse mouse button release event
286 public void onMouseUp(final TMouseEvent mouse
) {
287 if ((mouseOnMenuItem(mouse
)) && (mouse
.isMouse1())) {
296 * @param keypress keystroke event
299 public void onKeypress(final TKeypressEvent keypress
) {
300 if (keypress
.equals(kbEnter
)) {
305 // Pass to parent for the things we don't care about.
306 super.onKeypress(keypress
);