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 * When true, this item can be checked or unchecked.
62 private boolean checkable
= false;
67 * @param checkable if true, this menu item can be checked/unchecked
69 public final void setCheckable(final boolean checkable
) {
70 this.checkable
= checkable
;
74 * When true, this item is checked.
76 private boolean checked
= false;
79 * Global shortcut key.
81 private TKeypress key
;
84 * When true, a global accelerator can be used to select this item.
86 private boolean hasKey
= false;
89 * The title string. Use '&' to specify a mnemonic, i.e. "&File" will
90 * highlight the 'F' and allow 'f' or 'F' to select it.
92 private MnemonicString mnemonic
;
95 * Get the mnemonic string for this menu item.
97 * @return mnemonic string
99 public final MnemonicString
getMnemonic() {
104 * Set a global accelerator key for this menu item.
106 * @param key global keyboard accelerator
108 public final void setKey(final TKeypress key
) {
112 int newWidth
= (label
.length() + 4 + key
.toString().length() + 2);
113 if (newWidth
> getWidth()) {
119 * Package private constructor.
121 * @param parent parent widget
123 * @param x column relative to parent
124 * @param y row relative to parent
125 * @param label menu item title
127 TMenuItem(final TMenu parent
, final int id
, final int x
, final int y
,
128 final String label
) {
130 // Set parent and window
133 mnemonic
= new MnemonicString(label
);
138 this.label
= mnemonic
.getRawLabel();
139 setWidth(label
.length() + 4);
142 // Default state for some known menu items
151 case TMenu
.MID_PASTE
:
154 case TMenu
.MID_CLEAR
:
160 case TMenu
.MID_CASCADE
:
162 case TMenu
.MID_CLOSE_ALL
:
164 case TMenu
.MID_WINDOW_MOVE
:
166 case TMenu
.MID_WINDOW_ZOOM
:
168 case TMenu
.MID_WINDOW_NEXT
:
170 case TMenu
.MID_WINDOW_PREVIOUS
:
172 case TMenu
.MID_WINDOW_CLOSE
:
181 * Returns true if the mouse is currently on the menu item.
183 * @param mouse mouse event
185 private boolean mouseOnMenuItem(final TMouseEvent mouse
) {
186 if ((mouse
.getY() == 0)
187 && (mouse
.getX() >= 0)
188 && (mouse
.getX() < getWidth())
196 * Draw a menu item with label.
200 CellAttributes background
= getTheme().getColor("tmenu");
201 CellAttributes menuColor
;
202 CellAttributes menuMnemonicColor
;
203 if (getAbsoluteActive()) {
204 menuColor
= getTheme().getColor("tmenu.highlighted");
205 menuMnemonicColor
= getTheme().getColor("tmenu.mnemonic.highlighted");
208 menuColor
= getTheme().getColor("tmenu");
209 menuMnemonicColor
= getTheme().getColor("tmenu.mnemonic");
211 menuColor
= getTheme().getColor("tmenu.disabled");
212 menuMnemonicColor
= getTheme().getColor("tmenu.disabled");
216 char cVSide
= GraphicsChars
.WINDOW_SIDE
;
217 getScreen().vLineXY(0, 0, 1, cVSide
, background
);
218 getScreen().vLineXY(getWidth() - 1, 0, 1, cVSide
, background
);
220 getScreen().hLineXY(1, 0, getWidth() - 2, ' ', menuColor
);
221 getScreen().putStrXY(2, 0, mnemonic
.getRawLabel(), menuColor
);
223 String keyLabel
= key
.toString();
224 getScreen().putStrXY((getWidth() - keyLabel
.length() - 2), 0,
225 keyLabel
, menuColor
);
227 if (mnemonic
.getShortcutIdx() >= 0) {
228 getScreen().putCharXY(2 + mnemonic
.getShortcutIdx(), 0,
229 mnemonic
.getShortcut(), menuMnemonicColor
);
233 getScreen().putCharXY(1, 0, GraphicsChars
.CHECK
, menuColor
);
239 * Dispatch event(s) due to selection or click.
241 public void dispatch() {
242 assert (getEnabled());
244 getApplication().addMenuEvent(new TMenuEvent(id
));
251 * Handle mouse button presses.
253 * @param event mouse button press event
255 /* TODO: this was commented out in d-tui, why?
257 public void onMouseDown(final TMouseEvent event) {
258 if ((mouseOnMenuItem(event)) && (event.mouse1)) {
266 * Handle mouse button releases.
268 * @param mouse mouse button release event
271 public void onMouseUp(final TMouseEvent mouse
) {
272 if ((mouseOnMenuItem(mouse
)) && (mouse
.getMouse1())) {
281 * @param keypress keystroke event
284 public void onKeypress(final TKeypressEvent keypress
) {
285 if (keypress
.equals(kbEnter
)) {
290 // Pass to parent for the things we don't care about.
291 super.onKeypress(keypress
);