2 * Jexer - Java Text User Interface
4 * The MIT License (MIT)
6 * Copyright (C) 2016 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
.TApplication
;
32 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 static jexer
.TKeypress
.*;
43 * TMenu is a top-level collection of TMenuItems.
45 public final class TMenu
extends TWindow
{
48 * If true, this is a sub-menu. Note package private access.
50 boolean isSubMenu
= false;
53 * The shortcut and title.
55 private MnemonicString mnemonic
;
58 * Get the mnemonic string.
60 * @return the full mnemonic string
62 public MnemonicString
getMnemonic() {
66 // Reserved menu item IDs
67 public static final int MID_UNUSED
= -1;
70 public static final int MID_EXIT
= 1;
71 public static final int MID_QUIT
= MID_EXIT
;
72 public static final int MID_OPEN_FILE
= 2;
73 public static final int MID_SHELL
= 3;
76 public static final int MID_CUT
= 10;
77 public static final int MID_COPY
= 11;
78 public static final int MID_PASTE
= 12;
79 public static final int MID_CLEAR
= 13;
82 public static final int MID_TILE
= 20;
83 public static final int MID_CASCADE
= 21;
84 public static final int MID_CLOSE_ALL
= 22;
85 public static final int MID_WINDOW_MOVE
= 23;
86 public static final int MID_WINDOW_ZOOM
= 24;
87 public static final int MID_WINDOW_NEXT
= 25;
88 public static final int MID_WINDOW_PREVIOUS
= 26;
89 public static final int MID_WINDOW_CLOSE
= 27;
94 * @param parent parent application
95 * @param x column relative to parent
96 * @param y row relative to parent
97 * @param label mnemonic menu title. Label must contain a keyboard
98 * shortcut (mnemonic), denoted by prefixing a letter with "&",
101 public TMenu(final TApplication parent
, final int x
, final int y
,
102 final String label
) {
104 super(parent
, label
, x
, y
, parent
.getScreen().getWidth(),
105 parent
.getScreen().getHeight());
107 // My parent constructor added me as a window, get rid of that
108 parent
.closeWindow(this);
110 // Setup the menu shortcut
111 mnemonic
= new MnemonicString(label
);
112 setTitle(mnemonic
.getRawLabel());
113 assert (mnemonic
.getShortcutIdx() >= 0);
115 // Recompute width and height to reflect an empty menu
116 setWidth(getTitle().length() + 4);
123 * Draw a top-level menu with title and menu items.
127 CellAttributes background
= getTheme().getColor("tmenu");
129 assert (isAbsoluteActive());
131 // Fill in the interior background
132 for (int i
= 0; i
< getHeight(); i
++) {
133 hLineXY(0, i
, getWidth(), ' ', background
);
143 cTopLeft
= GraphicsChars
.ULCORNER
;
144 cTopRight
= GraphicsChars
.URCORNER
;
145 cBottomLeft
= GraphicsChars
.LLCORNER
;
146 cBottomRight
= GraphicsChars
.LRCORNER
;
147 cHSide
= GraphicsChars
.SINGLE_BAR
;
149 // Place the corner characters
150 putCharXY(1, 0, cTopLeft
, background
);
151 putCharXY(getWidth() - 2, 0, cTopRight
, background
);
152 putCharXY(1, getHeight() - 1, cBottomLeft
, background
);
153 putCharXY(getWidth() - 2, getHeight() - 1, cBottomRight
, background
);
155 // Draw the box lines
156 hLineXY(1 + 1, 0, getWidth() - 4, cHSide
, background
);
157 hLineXY(1 + 1, getHeight() - 1, getWidth() - 4, cHSide
, background
);
160 getScreen().drawBoxShadow(0, 0, getWidth(), getHeight());
164 * Handle mouse button presses.
166 * @param mouse mouse button event
169 public void onMouseDown(final TMouseEvent mouse
) {
173 for (TWidget widget
: getChildren()) {
174 if (widget
.mouseWouldHit(mouse
)) {
175 // Dispatch to this child, also activate it
178 // Set x and y relative to the child's coordinates
179 mouse
.setX(mouse
.getAbsoluteX() - widget
.getAbsoluteX());
180 mouse
.setY(mouse
.getAbsoluteY() - widget
.getAbsoluteY());
181 widget
.handleEvent(mouse
);
188 * Handle mouse button releases.
190 * @param mouse mouse button release event
193 public void onMouseUp(final TMouseEvent mouse
) {
197 for (TWidget widget
: getChildren()) {
198 if (widget
.mouseWouldHit(mouse
)) {
199 // Dispatch to this child, also activate it
202 // Set x and y relative to the child's coordinates
203 mouse
.setX(mouse
.getAbsoluteX() - widget
.getAbsoluteX());
204 mouse
.setY(mouse
.getAbsoluteY() - widget
.getAbsoluteY());
205 widget
.handleEvent(mouse
);
212 * Handle mouse movements.
214 * @param mouse mouse motion event
217 public void onMouseMotion(final TMouseEvent mouse
) {
220 // See if we should activate a different menu item
221 for (TWidget widget
: getChildren()) {
222 if ((mouse
.isMouse1())
223 && (widget
.mouseWouldHit(mouse
))
225 // Activate this menu item
227 if (widget
instanceof TSubMenu
) {
228 ((TSubMenu
) widget
).dispatch();
238 * @param keypress keystroke event
241 public void onKeypress(final TKeypressEvent keypress
) {
244 System.err.printf("keypress: %s active child: %s\n", keypress,
248 if (getActiveChild() != this) {
249 if ((getActiveChild() instanceof TSubMenu
)
250 || (getActiveChild() instanceof TMenu
)
252 getActiveChild().onKeypress(keypress
);
257 if (keypress
.equals(kbEsc
)) {
258 getApplication().closeMenu();
261 if (keypress
.equals(kbDown
)) {
265 if (keypress
.equals(kbUp
)) {
269 if (keypress
.equals(kbRight
)) {
270 getApplication().switchMenu(true);
273 if (keypress
.equals(kbLeft
)) {
275 getApplication().closeSubMenu();
277 getApplication().switchMenu(false);
282 // Switch to a menuItem if it has an mnemonic
283 if (!keypress
.getKey().isFnKey()
284 && !keypress
.getKey().isAlt()
285 && !keypress
.getKey().isCtrl()) {
286 for (TWidget widget
: getChildren()) {
287 TMenuItem item
= (TMenuItem
) widget
;
288 if ((item
.getMnemonic() != null)
289 && (Character
.toLowerCase(item
.getMnemonic().getShortcut())
290 == Character
.toLowerCase(keypress
.getKey().getChar()))
292 // Send an enter keystroke to it
294 item
.handleEvent(new TKeypressEvent(kbEnter
));
300 // Dispatch the keypress to an active widget
301 for (TWidget widget
: getChildren()) {
302 if (widget
.isActive()) {
303 widget
.handleEvent(keypress
);
310 * Convenience function to add a menu item.
312 * @param id menu item ID. Must be greater than 1024.
313 * @param label menu item label
314 * @return the new menu item
316 public TMenuItem
addItem(final int id
, final String label
) {
318 return addItemInternal(id
, label
, null);
322 * Convenience function to add a custom menu item.
324 * @param id menu item ID. Must be greater than 1024.
325 * @param label menu item label
326 * @param key global keyboard accelerator
327 * @return the new menu item
329 public TMenuItem
addItem(final int id
, final String label
,
330 final TKeypress key
) {
333 return addItemInternal(id
, label
, key
);
337 * Convenience function to add a custom menu item.
339 * @param id menu item ID. Must be greater than 1024.
340 * @param label menu item label
341 * @param key global keyboard accelerator
342 * @return the new menu item
344 private TMenuItem
addItemInternal(final int id
, final String label
,
345 final TKeypress key
) {
347 int newY
= getChildren().size() + 1;
348 assert (newY
< getHeight());
350 TMenuItem menuItem
= new TMenuItem(this, id
, 1, newY
, label
);
351 menuItem
.setKey(key
);
352 setHeight(getHeight() + 1);
353 if (menuItem
.getWidth() + 2 > getWidth()) {
354 setWidth(menuItem
.getWidth() + 2);
356 for (TWidget widget
: getChildren()) {
357 widget
.setWidth(getWidth() - 2);
359 getApplication().addMenuItem(menuItem
);
360 getApplication().recomputeMenuX();
366 * Convenience function to add one of the default menu items.
368 * @param id menu item ID. Must be between 0 (inclusive) and 1023
370 * @return the new menu item
372 public TMenuItem
addDefaultItem(final int id
) {
377 TKeypress key
= null;
419 label
= "Cl&ose All";
421 case MID_WINDOW_MOVE
:
422 label
= "&Size/Move";
425 case MID_WINDOW_ZOOM
:
429 case MID_WINDOW_NEXT
:
433 case MID_WINDOW_PREVIOUS
:
437 case MID_WINDOW_CLOSE
:
443 throw new IllegalArgumentException("Invalid menu ID: " + id
);
446 return addItemInternal(id
, label
, key
);
450 * Convenience function to add a menu separator.
452 public void addSeparator() {
453 int newY
= getChildren().size() + 1;
454 assert (newY
< getHeight());
456 // We just have to construct it, don't need to hang onto what it
458 new TMenuSeparator(this, 1, newY
);
459 setHeight(getHeight() + 1);
463 * Convenience function to add a sub-menu.
465 * @param title menu title. Title must contain a keyboard shortcut,
466 * denoted by prefixing a letter with "&", e.g. "&File"
467 * @return the new sub-menu
469 public TSubMenu
addSubMenu(final String title
) {
470 int newY
= getChildren().size() + 1;
471 assert (newY
< getHeight());
473 TSubMenu subMenu
= new TSubMenu(this, title
, 1, newY
);
474 setHeight(getHeight() + 1);
475 if (subMenu
.getWidth() + 2 > getWidth()) {
476 setWidth(subMenu
.getWidth() + 2);
478 for (TWidget widget
: getChildren()) {
479 widget
.setWidth(getWidth() - 2);
481 getApplication().recomputeMenuX();
483 subMenu
.menu
.setX(getX() + getWidth() - 2);