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 java
.util
.ResourceBundle
;
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 class TMenu
extends TWindow
{
52 private static final ResourceBundle i18n
= ResourceBundle
.getBundle(TMenu
.class.getName());
54 // ------------------------------------------------------------------------
55 // Constants --------------------------------------------------------------
56 // ------------------------------------------------------------------------
58 // Reserved menu item IDs
59 public static final int MID_UNUSED
= -1;
62 public static final int MID_REPAINT
= 1;
63 public static final int MID_VIEW_IMAGE
= 2;
64 public static final int MID_CHANGE_FONT
= 3;
67 public static final int MID_NEW
= 10;
68 public static final int MID_EXIT
= 11;
69 public static final int MID_QUIT
= MID_EXIT
;
70 public static final int MID_OPEN_FILE
= 12;
71 public static final int MID_SHELL
= 13;
74 public static final int MID_CUT
= 20;
75 public static final int MID_COPY
= 21;
76 public static final int MID_PASTE
= 22;
77 public static final int MID_CLEAR
= 23;
80 public static final int MID_FIND
= 30;
81 public static final int MID_REPLACE
= 31;
82 public static final int MID_SEARCH_AGAIN
= 32;
83 public static final int MID_GOTO_LINE
= 33;
86 public static final int MID_TILE
= 40;
87 public static final int MID_CASCADE
= 41;
88 public static final int MID_CLOSE_ALL
= 42;
89 public static final int MID_WINDOW_MOVE
= 43;
90 public static final int MID_WINDOW_ZOOM
= 44;
91 public static final int MID_WINDOW_NEXT
= 45;
92 public static final int MID_WINDOW_PREVIOUS
= 46;
93 public static final int MID_WINDOW_CLOSE
= 47;
96 public static final int MID_HELP_CONTENTS
= 50;
97 public static final int MID_HELP_INDEX
= 51;
98 public static final int MID_HELP_SEARCH
= 52;
99 public static final int MID_HELP_PREVIOUS
= 53;
100 public static final int MID_HELP_HELP
= 54;
101 public static final int MID_HELP_ACTIVE_FILE
= 55;
102 public static final int MID_ABOUT
= 56;
104 // ------------------------------------------------------------------------
105 // Variables --------------------------------------------------------------
106 // ------------------------------------------------------------------------
109 * If true, this is a sub-menu. Note package private access.
111 boolean isSubMenu
= false;
114 * The X position of the menu's title.
119 * The shortcut and title.
121 private MnemonicString mnemonic
;
123 // ------------------------------------------------------------------------
124 // Constructors -----------------------------------------------------------
125 // ------------------------------------------------------------------------
128 * Public constructor.
130 * @param parent parent application
131 * @param x column relative to parent
132 * @param y row relative to parent
133 * @param label mnemonic menu title. Label must contain a keyboard
134 * shortcut (mnemonic), denoted by prefixing a letter with "&",
137 public TMenu(final TApplication parent
, final int x
, final int y
,
138 final String label
) {
140 super(parent
, label
, x
, y
, parent
.getScreen().getWidth(),
141 parent
.getScreen().getHeight());
143 // Setup the menu shortcut
144 mnemonic
= new MnemonicString(label
);
145 setTitle(mnemonic
.getRawLabel());
146 assert (mnemonic
.getShortcutIdx() >= 0);
148 // Recompute width and height to reflect an empty menu
149 setWidth(getTitle().length() + 4);
155 // ------------------------------------------------------------------------
156 // Event handlers ---------------------------------------------------------
157 // ------------------------------------------------------------------------
160 * Handle mouse button presses.
162 * @param mouse mouse button event
165 public void onMouseDown(final TMouseEvent mouse
) {
167 super.onMouseDown(mouse
);
170 for (TWidget widget
: getChildren()) {
171 if (widget
.mouseWouldHit(mouse
)) {
172 // Dispatch to this child, also activate it
175 // Set x and y relative to the child's coordinates
176 mouse
.setX(mouse
.getAbsoluteX() - widget
.getAbsoluteX());
177 mouse
.setY(mouse
.getAbsoluteY() - widget
.getAbsoluteY());
178 widget
.handleEvent(mouse
);
185 * Handle mouse button releases.
187 * @param mouse mouse button release event
190 public void onMouseUp(final TMouseEvent mouse
) {
194 for (TWidget widget
: getChildren()) {
195 if (widget
.mouseWouldHit(mouse
)) {
196 // Dispatch to this child, also activate it
199 // Set x and y relative to the child's coordinates
200 mouse
.setX(mouse
.getAbsoluteX() - widget
.getAbsoluteX());
201 mouse
.setY(mouse
.getAbsoluteY() - widget
.getAbsoluteY());
202 widget
.handleEvent(mouse
);
209 * Handle mouse movements.
211 * @param mouse mouse motion event
214 public void onMouseMotion(final TMouseEvent mouse
) {
217 // See if we should activate a different menu item
218 for (TWidget widget
: getChildren()) {
219 if ((mouse
.isMouse1())
220 && (widget
.mouseWouldHit(mouse
))
222 // Activate this menu item
224 if (widget
instanceof TSubMenu
) {
225 ((TSubMenu
) widget
).dispatch();
235 * @param keypress keystroke event
238 public void onKeypress(final TKeypressEvent keypress
) {
241 System.err.printf("keypress: %s active child: %s\n", keypress,
245 if (getActiveChild() != this) {
246 if ((getActiveChild() instanceof TSubMenu
)
247 || (getActiveChild() instanceof TMenu
)
249 getActiveChild().onKeypress(keypress
);
254 if (keypress
.equals(kbEsc
)) {
255 getApplication().closeMenu();
258 if (keypress
.equals(kbDown
)) {
262 if (keypress
.equals(kbUp
)) {
266 if (keypress
.equals(kbRight
)) {
267 getApplication().switchMenu(true);
270 if (keypress
.equals(kbLeft
)) {
272 getApplication().closeSubMenu();
274 getApplication().switchMenu(false);
279 // Switch to a menuItem if it has an mnemonic
280 if (!keypress
.getKey().isFnKey()
281 && !keypress
.getKey().isAlt()
282 && !keypress
.getKey().isCtrl()) {
283 for (TWidget widget
: getChildren()) {
284 TMenuItem item
= (TMenuItem
) widget
;
285 if ((item
.getMnemonic() != null)
286 && (Character
.toLowerCase(item
.getMnemonic().getShortcut())
287 == Character
.toLowerCase(keypress
.getKey().getChar()))
289 // Send an enter keystroke to it
291 item
.handleEvent(new TKeypressEvent(kbEnter
));
297 // Dispatch the keypress to an active widget
298 for (TWidget widget
: getChildren()) {
299 if (widget
.isActive()) {
300 widget
.handleEvent(keypress
);
306 // ------------------------------------------------------------------------
307 // TWindow ----------------------------------------------------------------
308 // ------------------------------------------------------------------------
311 * Draw a top-level menu with title and menu items.
315 CellAttributes background
= getTheme().getColor("tmenu");
317 assert (isAbsoluteActive());
319 // Fill in the interior background
320 for (int i
= 0; i
< getHeight(); i
++) {
321 hLineXY(0, i
, getWidth(), ' ', background
);
331 cTopLeft
= GraphicsChars
.ULCORNER
;
332 cTopRight
= GraphicsChars
.URCORNER
;
333 cBottomLeft
= GraphicsChars
.LLCORNER
;
334 cBottomRight
= GraphicsChars
.LRCORNER
;
335 cHSide
= GraphicsChars
.SINGLE_BAR
;
337 // Place the corner characters
338 putCharXY(1, 0, cTopLeft
, background
);
339 putCharXY(getWidth() - 2, 0, cTopRight
, background
);
340 putCharXY(1, getHeight() - 1, cBottomLeft
, background
);
341 putCharXY(getWidth() - 2, getHeight() - 1, cBottomRight
, background
);
343 // Draw the box lines
344 hLineXY(1 + 1, 0, getWidth() - 4, cHSide
, background
);
345 hLineXY(1 + 1, getHeight() - 1, getWidth() - 4, cHSide
, background
);
348 drawBoxShadow(0, 0, getWidth(), getHeight());
351 // ------------------------------------------------------------------------
352 // TMenu ------------------------------------------------------------------
353 // ------------------------------------------------------------------------
356 * Set the menu title X position.
358 * @param titleX the position
360 public void setTitleX(final int titleX
) {
361 this.titleX
= titleX
;
365 * Get the menu title X position.
367 * @return the position
369 public int getTitleX() {
374 * Get the mnemonic string.
376 * @return the full mnemonic string
378 public MnemonicString
getMnemonic() {
383 * Convenience function to add a menu item.
385 * @param id menu item ID. Must be greater than 1024.
386 * @param label menu item label
387 * @return the new menu item
389 public TMenuItem
addItem(final int id
, final String label
) {
391 return addItemInternal(id
, label
, null);
395 * Convenience function to add a menu item.
397 * @param id menu item ID. Must be greater than 1024.
398 * @param label menu item label
399 * @param enabled default state for enabled
400 * @return the new menu item
402 public TMenuItem
addItem(final int id
, final String label
,
403 final boolean enabled
) {
406 return addItemInternal(id
, label
, null, enabled
);
410 * Convenience function to add a custom menu item.
412 * @param id menu item ID. Must be greater than 1024.
413 * @param label menu item label
414 * @param key global keyboard accelerator
415 * @return the new menu item
417 public TMenuItem
addItem(final int id
, final String label
,
418 final TKeypress key
) {
421 return addItemInternal(id
, label
, key
);
425 * Convenience function to add a custom menu item.
427 * @param id menu item ID. Must be greater than 1024.
428 * @param label menu item label
429 * @param key global keyboard accelerator
430 * @param enabled default state for enabled
431 * @return the new menu item
433 public TMenuItem
addItem(final int id
, final String label
,
434 final TKeypress key
, final boolean enabled
) {
436 TMenuItem item
= addItem(id
, label
, key
);
437 item
.setEnabled(enabled
);
442 * Convenience function to add a custom menu item.
444 * @param id menu item ID. Must be greater than 1024.
445 * @param label menu item label
446 * @param key global keyboard accelerator
447 * @return the new menu item
449 private TMenuItem
addItemInternal(final int id
, final String label
,
450 final TKeypress key
) {
452 return addItemInternal(id
, label
, key
, true);
456 * Convenience function to add a custom menu item.
458 * @param id menu item ID. Must be greater than 1024.
459 * @param label menu item label
460 * @param key global keyboard accelerator
461 * @param enabled default state for enabled
462 * @return the new menu item
464 private TMenuItem
addItemInternal(final int id
, final String label
,
465 final TKeypress key
, final boolean enabled
) {
467 int newY
= getChildren().size() + 1;
468 assert (newY
< getHeight());
470 TMenuItem menuItem
= new TMenuItem(this, id
, 1, newY
, label
);
471 menuItem
.setKey(key
);
472 menuItem
.setEnabled(enabled
);
473 setHeight(getHeight() + 1);
474 if (menuItem
.getWidth() + 2 > getWidth()) {
475 setWidth(menuItem
.getWidth() + 2);
477 for (TWidget widget
: getChildren()) {
478 widget
.setWidth(getWidth() - 2);
480 getApplication().addMenuItem(menuItem
);
481 getApplication().recomputeMenuX();
487 * Convenience function to add one of the default menu items.
489 * @param id menu item ID. Must be between 0 (inclusive) and 1023
491 * @return the new menu item
493 public TMenuItem
addDefaultItem(final int id
) {
494 return addDefaultItem(id
, true);
498 * Convenience function to add one of the default menu items.
500 * @param id menu item ID. Must be between 0 (inclusive) and 1023
502 * @param enabled default state for enabled
503 * @return the new menu item
505 public TMenuItem
addDefaultItem(final int id
, final boolean enabled
) {
510 TKeypress key
= null;
515 label
= i18n
.getString("menuRepaintDesktop");
519 label
= i18n
.getString("menuViewImage");
522 case MID_CHANGE_FONT
:
523 label
= i18n
.getString("menuChangeFont");
527 label
= i18n
.getString("menuNew");
531 label
= i18n
.getString("menuExit");
536 label
= i18n
.getString("menuShell");
540 label
= i18n
.getString("menuOpen");
545 label
= i18n
.getString("menuCut");
549 label
= i18n
.getString("menuCopy");
553 label
= i18n
.getString("menuPaste");
557 label
= i18n
.getString("menuClear");
562 label
= i18n
.getString("menuFind");
565 label
= i18n
.getString("menuReplace");
567 case MID_SEARCH_AGAIN
:
568 label
= i18n
.getString("menuSearchAgain");
572 label
= i18n
.getString("menuGotoLine");
576 label
= i18n
.getString("menuWindowTile");
579 label
= i18n
.getString("menuWindowCascade");
582 label
= i18n
.getString("menuWindowCloseAll");
584 case MID_WINDOW_MOVE
:
585 label
= i18n
.getString("menuWindowMove");
588 case MID_WINDOW_ZOOM
:
589 label
= i18n
.getString("menuWindowZoom");
592 case MID_WINDOW_NEXT
:
593 label
= i18n
.getString("menuWindowNext");
596 case MID_WINDOW_PREVIOUS
:
597 label
= i18n
.getString("menuWindowPrevious");
600 case MID_WINDOW_CLOSE
:
601 label
= i18n
.getString("menuWindowClose");
605 case MID_HELP_CONTENTS
:
606 label
= i18n
.getString("menuHelpContents");
609 label
= i18n
.getString("menuHelpIndex");
612 case MID_HELP_SEARCH
:
613 label
= i18n
.getString("menuHelpSearch");
616 case MID_HELP_PREVIOUS
:
617 label
= i18n
.getString("menuHelpPrevious");
621 label
= i18n
.getString("menuHelpHelp");
623 case MID_HELP_ACTIVE_FILE
:
624 label
= i18n
.getString("menuHelpActive");
627 label
= i18n
.getString("menuHelpAbout");
631 throw new IllegalArgumentException("Invalid menu ID: " + id
);
634 return addItemInternal(id
, label
, key
, enabled
);
638 * Convenience function to add a menu separator.
640 public void addSeparator() {
641 int newY
= getChildren().size() + 1;
642 assert (newY
< getHeight());
644 // We just have to construct it, don't need to hang onto what it
646 new TMenuSeparator(this, 1, newY
);
647 setHeight(getHeight() + 1);
651 * Convenience function to add a sub-menu.
653 * @param title menu title. Title must contain a keyboard shortcut,
654 * denoted by prefixing a letter with "&", e.g. "&File"
655 * @return the new sub-menu
657 public TSubMenu
addSubMenu(final String title
) {
658 int newY
= getChildren().size() + 1;
659 assert (newY
< getHeight());
661 TSubMenu subMenu
= new TSubMenu(this, title
, 1, newY
);
662 setHeight(getHeight() + 1);
663 if (subMenu
.getWidth() + 2 > getWidth()) {
664 setWidth(subMenu
.getWidth() + 2);
666 for (TWidget widget
: getChildren()) {
667 widget
.setWidth(getWidth() - 2);
669 getApplication().recomputeMenuX();
671 subMenu
.menu
.setX(getX() + getWidth() - 2);