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
.TApplication
;
34 import jexer
.TKeypress
;
37 import jexer
.bits
.CellAttributes
;
38 import jexer
.bits
.GraphicsChars
;
39 import jexer
.bits
.MnemonicString
;
40 import jexer
.event
.TKeypressEvent
;
41 import jexer
.event
.TMouseEvent
;
42 import static jexer
.TKeypress
.*;
45 * TMenu is a top-level collection of TMenuItems.
47 public final class TMenu
extends TWindow
{
50 * If true, this is a sub-menu. Note package private access.
52 boolean isSubMenu
= false;
55 * The shortcut and title.
57 private MnemonicString mnemonic
;
60 * Get the mnemonic string.
62 * @return the full mnemonic string
64 public MnemonicString
getMnemonic() {
68 // Reserved menu item IDs
69 public static final int MID_UNUSED
= -1;
72 public static final int MID_EXIT
= 1;
73 public static final int MID_QUIT
= MID_EXIT
;
74 public static final int MID_OPEN_FILE
= 2;
75 public static final int MID_SHELL
= 3;
78 public static final int MID_CUT
= 10;
79 public static final int MID_COPY
= 11;
80 public static final int MID_PASTE
= 12;
81 public static final int MID_CLEAR
= 13;
84 public static final int MID_TILE
= 20;
85 public static final int MID_CASCADE
= 21;
86 public static final int MID_CLOSE_ALL
= 22;
87 public static final int MID_WINDOW_MOVE
= 23;
88 public static final int MID_WINDOW_ZOOM
= 24;
89 public static final int MID_WINDOW_NEXT
= 25;
90 public static final int MID_WINDOW_PREVIOUS
= 26;
91 public static final int MID_WINDOW_CLOSE
= 27;
96 * @param parent parent application
97 * @param x column relative to parent
98 * @param y row relative to parent
99 * @param label mnemonic menu title. Label must contain a keyboard
100 * shortcut (mnemonic), denoted by prefixing a letter with "&",
103 public TMenu(final TApplication parent
, final int x
, final int y
,
104 final String label
) {
106 super(parent
, label
, x
, y
, parent
.getScreen().getWidth(),
107 parent
.getScreen().getHeight());
109 // My parent constructor added me as a window, get rid of that
110 parent
.closeWindow(this);
112 // Setup the menu shortcut
113 mnemonic
= new MnemonicString(label
);
114 setTitle(mnemonic
.getRawLabel());
115 assert (mnemonic
.getShortcutIdx() >= 0);
117 // Recompute width and height to reflect an empty menu
118 setWidth(getTitle().length() + 4);
125 * Draw a top-level menu with title and menu items.
129 CellAttributes background
= getTheme().getColor("tmenu");
131 assert (isAbsoluteActive());
133 // Fill in the interior background
134 for (int i
= 0; i
< getHeight(); i
++) {
135 hLineXY(0, i
, getWidth(), ' ', background
);
145 cTopLeft
= GraphicsChars
.ULCORNER
;
146 cTopRight
= GraphicsChars
.URCORNER
;
147 cBottomLeft
= GraphicsChars
.LLCORNER
;
148 cBottomRight
= GraphicsChars
.LRCORNER
;
149 cHSide
= GraphicsChars
.SINGLE_BAR
;
151 // Place the corner characters
152 putCharXY(1, 0, cTopLeft
, background
);
153 putCharXY(getWidth() - 2, 0, cTopRight
, background
);
154 putCharXY(1, getHeight() - 1, cBottomLeft
, background
);
155 putCharXY(getWidth() - 2, getHeight() - 1, cBottomRight
, background
);
157 // Draw the box lines
158 hLineXY(1 + 1, 0, getWidth() - 4, cHSide
, background
);
159 hLineXY(1 + 1, getHeight() - 1, getWidth() - 4, cHSide
, background
);
162 getScreen().drawBoxShadow(0, 0, getWidth(), getHeight());
166 * Handle mouse button presses.
168 * @param mouse mouse button event
171 public void onMouseDown(final TMouseEvent mouse
) {
175 for (TWidget widget
: getChildren()) {
176 if (widget
.mouseWouldHit(mouse
)) {
177 // Dispatch to this child, also activate it
180 // Set x and y relative to the child's coordinates
181 mouse
.setX(mouse
.getAbsoluteX() - widget
.getAbsoluteX());
182 mouse
.setY(mouse
.getAbsoluteY() - widget
.getAbsoluteY());
183 widget
.handleEvent(mouse
);
190 * Handle mouse button releases.
192 * @param mouse mouse button release event
195 public void onMouseUp(final TMouseEvent mouse
) {
199 for (TWidget widget
: getChildren()) {
200 if (widget
.mouseWouldHit(mouse
)) {
201 // Dispatch to this child, also activate it
204 // Set x and y relative to the child's coordinates
205 mouse
.setX(mouse
.getAbsoluteX() - widget
.getAbsoluteX());
206 mouse
.setY(mouse
.getAbsoluteY() - widget
.getAbsoluteY());
207 widget
.handleEvent(mouse
);
214 * Handle mouse movements.
216 * @param mouse mouse motion event
219 public void onMouseMotion(final TMouseEvent mouse
) {
222 // See if we should activate a different menu item
223 for (TWidget widget
: getChildren()) {
224 if ((mouse
.isMouse1())
225 && (widget
.mouseWouldHit(mouse
))
227 // Activate this menu item
229 if (widget
instanceof TSubMenu
) {
230 ((TSubMenu
) widget
).dispatch();
240 * @param keypress keystroke event
243 public void onKeypress(final TKeypressEvent keypress
) {
246 System.err.printf("keypress: %s active child: %s\n", keypress,
250 if (getActiveChild() != this) {
251 if ((getActiveChild() instanceof TSubMenu
)
252 || (getActiveChild() instanceof TMenu
)
254 getActiveChild().onKeypress(keypress
);
259 if (keypress
.equals(kbEsc
)) {
260 getApplication().closeMenu();
263 if (keypress
.equals(kbDown
)) {
267 if (keypress
.equals(kbUp
)) {
271 if (keypress
.equals(kbRight
)) {
272 getApplication().switchMenu(true);
275 if (keypress
.equals(kbLeft
)) {
277 getApplication().closeSubMenu();
279 getApplication().switchMenu(false);
284 // Switch to a menuItem if it has an mnemonic
285 if (!keypress
.getKey().isFnKey()
286 && !keypress
.getKey().isAlt()
287 && !keypress
.getKey().isCtrl()) {
288 for (TWidget widget
: getChildren()) {
289 TMenuItem item
= (TMenuItem
) widget
;
290 if ((item
.getMnemonic() != null)
291 && (Character
.toLowerCase(item
.getMnemonic().getShortcut())
292 == Character
.toLowerCase(keypress
.getKey().getChar()))
294 // Send an enter keystroke to it
296 item
.handleEvent(new TKeypressEvent(kbEnter
));
302 // Dispatch the keypress to an active widget
303 for (TWidget widget
: getChildren()) {
304 if (widget
.isActive()) {
305 widget
.handleEvent(keypress
);
312 * Convenience function to add a menu item.
314 * @param id menu item ID. Must be greater than 1024.
315 * @param label menu item label
316 * @return the new menu item
318 public final TMenuItem
addItem(final int id
, final String label
) {
320 return addItemInternal(id
, label
, null);
324 * Convenience function to add a custom menu item.
326 * @param id menu item ID. Must be greater than 1024.
327 * @param label menu item label
328 * @param key global keyboard accelerator
329 * @return the new menu item
331 public final TMenuItem
addItem(final int id
, final String label
,
332 final TKeypress key
) {
335 return addItemInternal(id
, label
, key
);
339 * Convenience function to add a custom menu item.
341 * @param id menu item ID. Must be greater than 1024.
342 * @param label menu item label
343 * @param key global keyboard accelerator
344 * @return the new menu item
346 private TMenuItem
addItemInternal(final int id
, final String label
,
347 final TKeypress key
) {
349 int newY
= getChildren().size() + 1;
350 assert (newY
< getHeight());
352 TMenuItem menuItem
= new TMenuItem(this, id
, 1, newY
, label
);
353 menuItem
.setKey(key
);
354 setHeight(getHeight() + 1);
355 if (menuItem
.getWidth() + 2 > getWidth()) {
356 setWidth(menuItem
.getWidth() + 2);
358 for (TWidget widget
: getChildren()) {
359 widget
.setWidth(getWidth() - 2);
361 getApplication().addMenuItem(menuItem
);
362 getApplication().recomputeMenuX();
368 * Convenience function to add one of the default menu items.
370 * @param id menu item ID. Must be between 0 (inclusive) and 1023
372 * @return the new menu item
374 public final TMenuItem
addDefaultItem(final int id
) {
379 TKeypress key
= null;
421 label
= "Cl&ose All";
423 case MID_WINDOW_MOVE
:
424 label
= "&Size/Move";
427 case MID_WINDOW_ZOOM
:
431 case MID_WINDOW_NEXT
:
435 case MID_WINDOW_PREVIOUS
:
439 case MID_WINDOW_CLOSE
:
445 throw new IllegalArgumentException("Invalid menu ID: " + id
);
448 return addItemInternal(id
, label
, key
);
452 * Convenience function to add a menu separator.
454 public final void addSeparator() {
455 int newY
= getChildren().size() + 1;
456 assert (newY
< getHeight());
458 // We just have to construct it, don't need to hang onto what it
460 new TMenuSeparator(this, 1, newY
);
461 setHeight(getHeight() + 1);
465 * Convenience function to add a sub-menu.
467 * @param title menu title. Title must contain a keyboard shortcut,
468 * denoted by prefixing a letter with "&", e.g. "&File"
469 * @return the new sub-menu
471 public final TSubMenu
addSubMenu(final String title
) {
472 int newY
= getChildren().size() + 1;
473 assert (newY
< getHeight());
475 TSubMenu subMenu
= new TSubMenu(this, title
, 1, newY
);
476 setHeight(getHeight() + 1);
477 if (subMenu
.getWidth() + 2 > getWidth()) {
478 setWidth(subMenu
.getWidth() + 2);
480 for (TWidget widget
: getChildren()) {
481 widget
.setWidth(getWidth() - 2);
483 getApplication().recomputeMenuX();
485 subMenu
.menu
.setX(getX() + getWidth() - 2);