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;
105 public static final int MID_TABLE_BORDER_NONE
= 60;
106 public static final int MID_TABLE_BORDER_ALL
= 61;
107 public static final int MID_TABLE_BORDER_RIGHT
= 62;
108 public static final int MID_TABLE_BORDER_LEFT
= 63;
109 public static final int MID_TABLE_BORDER_TOP
= 64;
110 public static final int MID_TABLE_BORDER_BOTTOM
= 65;
111 public static final int MID_TABLE_BORDER_DOUBLE_BOTTOM
= 66;
112 public static final int MID_TABLE_BORDER_THICK_BOTTOM
= 67;
113 public static final int MID_TABLE_DELETE_LEFT
= 68;
114 public static final int MID_TABLE_DELETE_UP
= 69;
115 public static final int MID_TABLE_DELETE_ROW
= 70;
116 public static final int MID_TABLE_DELETE_COLUMN
= 71;
117 public static final int MID_TABLE_INSERT_LEFT
= 72;
118 public static final int MID_TABLE_INSERT_RIGHT
= 73;
119 public static final int MID_TABLE_INSERT_ABOVE
= 74;
120 public static final int MID_TABLE_INSERT_BELOW
= 75;
121 public static final int MID_TABLE_COLUMN_NARROW
= 76;
122 public static final int MID_TABLE_COLUMN_WIDEN
= 77;
123 public static final int MID_TABLE_FILE_SAVE_CSV
= 78;
124 public static final int MID_TABLE_FILE_SAVE_TEXT
= 79;
126 // ------------------------------------------------------------------------
127 // Variables --------------------------------------------------------------
128 // ------------------------------------------------------------------------
131 * If true, this is a sub-menu. Note package private access.
133 boolean isSubMenu
= false;
136 * The X position of the menu's title.
141 * The shortcut and title.
143 private MnemonicString mnemonic
;
145 // ------------------------------------------------------------------------
146 // Constructors -----------------------------------------------------------
147 // ------------------------------------------------------------------------
150 * Public constructor.
152 * @param parent parent application
153 * @param x column relative to parent
154 * @param y row relative to parent
155 * @param label mnemonic menu title. Label must contain a keyboard
156 * shortcut (mnemonic), denoted by prefixing a letter with "&",
159 public TMenu(final TApplication parent
, final int x
, final int y
,
160 final String label
) {
162 super(parent
, label
, x
, y
, parent
.getScreen().getWidth(),
163 parent
.getScreen().getHeight());
165 // Setup the menu shortcut
166 mnemonic
= new MnemonicString(label
);
167 setTitle(mnemonic
.getRawLabel());
168 assert (mnemonic
.getShortcutIdx() >= 0);
170 // Recompute width and height to reflect an empty menu
171 setWidth(getTitle().length() + 4);
177 // ------------------------------------------------------------------------
178 // Event handlers ---------------------------------------------------------
179 // ------------------------------------------------------------------------
182 * Handle mouse button presses.
184 * @param mouse mouse button event
187 public void onMouseDown(final TMouseEvent mouse
) {
189 super.onMouseDown(mouse
);
192 for (TWidget widget
: getChildren()) {
193 if (widget
.mouseWouldHit(mouse
)) {
194 // Dispatch to this child, also activate it
197 // Set x and y relative to the child's coordinates
198 mouse
.setX(mouse
.getAbsoluteX() - widget
.getAbsoluteX());
199 mouse
.setY(mouse
.getAbsoluteY() - widget
.getAbsoluteY());
200 widget
.handleEvent(mouse
);
207 * Handle mouse button releases.
209 * @param mouse mouse button release event
212 public void onMouseUp(final TMouseEvent mouse
) {
216 for (TWidget widget
: getChildren()) {
217 if (widget
.mouseWouldHit(mouse
)) {
218 // Dispatch to this child, also activate it
221 // Set x and y relative to the child's coordinates
222 mouse
.setX(mouse
.getAbsoluteX() - widget
.getAbsoluteX());
223 mouse
.setY(mouse
.getAbsoluteY() - widget
.getAbsoluteY());
224 widget
.handleEvent(mouse
);
231 * Handle mouse movements.
233 * @param mouse mouse motion event
236 public void onMouseMotion(final TMouseEvent mouse
) {
239 // See if we should activate a different menu item
240 for (TWidget widget
: getChildren()) {
241 if ((mouse
.isMouse1())
242 && (widget
.mouseWouldHit(mouse
))
244 // Activate this menu item
246 if (widget
instanceof TSubMenu
) {
247 ((TSubMenu
) widget
).dispatch();
257 * @param keypress keystroke event
260 public void onKeypress(final TKeypressEvent keypress
) {
263 System.err.printf("keypress: %s active child: %s\n", keypress,
267 if (getActiveChild() != this) {
268 if ((getActiveChild() instanceof TSubMenu
)
269 || (getActiveChild() instanceof TMenu
)
271 getActiveChild().onKeypress(keypress
);
276 if (keypress
.equals(kbEsc
)) {
277 getApplication().closeMenu();
280 if (keypress
.equals(kbDown
)) {
284 if (keypress
.equals(kbUp
)) {
288 if (keypress
.equals(kbRight
)) {
289 getApplication().switchMenu(true);
292 if (keypress
.equals(kbLeft
)) {
294 getApplication().closeSubMenu();
296 getApplication().switchMenu(false);
301 // Switch to a menuItem if it has an mnemonic
302 if (!keypress
.getKey().isFnKey()
303 && !keypress
.getKey().isAlt()
304 && !keypress
.getKey().isCtrl()) {
305 for (TWidget widget
: getChildren()) {
306 TMenuItem item
= (TMenuItem
) widget
;
307 if ((item
.getMnemonic() != null)
308 && (Character
.toLowerCase(item
.getMnemonic().getShortcut())
309 == Character
.toLowerCase(keypress
.getKey().getChar()))
311 // Send an enter keystroke to it
313 item
.handleEvent(new TKeypressEvent(kbEnter
));
319 // Dispatch the keypress to an active widget
320 for (TWidget widget
: getChildren()) {
321 if (widget
.isActive()) {
322 widget
.handleEvent(keypress
);
328 // ------------------------------------------------------------------------
329 // TWindow ----------------------------------------------------------------
330 // ------------------------------------------------------------------------
333 * Draw a top-level menu with title and menu items.
337 CellAttributes background
= getTheme().getColor("tmenu");
339 assert (isAbsoluteActive());
341 // Fill in the interior background
342 for (int i
= 0; i
< getHeight(); i
++) {
343 hLineXY(0, i
, getWidth(), ' ', background
);
353 cTopLeft
= GraphicsChars
.ULCORNER
;
354 cTopRight
= GraphicsChars
.URCORNER
;
355 cBottomLeft
= GraphicsChars
.LLCORNER
;
356 cBottomRight
= GraphicsChars
.LRCORNER
;
357 cHSide
= GraphicsChars
.SINGLE_BAR
;
359 // Place the corner characters
360 putCharXY(1, 0, cTopLeft
, background
);
361 putCharXY(getWidth() - 2, 0, cTopRight
, background
);
362 putCharXY(1, getHeight() - 1, cBottomLeft
, background
);
363 putCharXY(getWidth() - 2, getHeight() - 1, cBottomRight
, background
);
365 // Draw the box lines
366 hLineXY(1 + 1, 0, getWidth() - 4, cHSide
, background
);
367 hLineXY(1 + 1, getHeight() - 1, getWidth() - 4, cHSide
, background
);
370 drawBoxShadow(0, 0, getWidth(), getHeight());
373 // ------------------------------------------------------------------------
374 // TMenu ------------------------------------------------------------------
375 // ------------------------------------------------------------------------
378 * Set the menu title X position.
380 * @param titleX the position
382 public void setTitleX(final int titleX
) {
383 this.titleX
= titleX
;
387 * Get the menu title X position.
389 * @return the position
391 public int getTitleX() {
396 * Get the mnemonic string.
398 * @return the full mnemonic string
400 public MnemonicString
getMnemonic() {
405 * Convenience function to add a menu item.
407 * @param id menu item ID. Must be greater than 1024.
408 * @param label menu item label
409 * @return the new menu item
411 public TMenuItem
addItem(final int id
, final String label
) {
413 return addItemInternal(id
, label
, null);
417 * Convenience function to add a menu item.
419 * @param id menu item ID. Must be greater than 1024.
420 * @param label menu item label
421 * @param enabled default state for enabled
422 * @return the new menu item
424 public TMenuItem
addItem(final int id
, final String label
,
425 final boolean enabled
) {
428 return addItemInternal(id
, label
, null, enabled
);
432 * Convenience function to add a custom menu item.
434 * @param id menu item ID. Must be greater than 1024.
435 * @param label menu item label
436 * @param key global keyboard accelerator
437 * @return the new menu item
439 public TMenuItem
addItem(final int id
, final String label
,
440 final TKeypress key
) {
443 return addItemInternal(id
, label
, key
);
447 * Convenience function to add a custom menu item.
449 * @param id menu item ID. Must be greater than 1024.
450 * @param label menu item label
451 * @param key global keyboard accelerator
452 * @param enabled default state for enabled
453 * @return the new menu item
455 public TMenuItem
addItem(final int id
, final String label
,
456 final TKeypress key
, final boolean enabled
) {
458 TMenuItem item
= addItem(id
, label
, key
);
459 item
.setEnabled(enabled
);
464 * Convenience function to add a custom menu item.
466 * @param id menu item ID. Must be greater than 1024.
467 * @param label menu item label
468 * @param key global keyboard accelerator
469 * @return the new menu item
471 private TMenuItem
addItemInternal(final int id
, final String label
,
472 final TKeypress key
) {
474 return addItemInternal(id
, label
, key
, true);
478 * Convenience function to add a custom menu item.
480 * @param id menu item ID. Must be greater than 1024.
481 * @param label menu item label
482 * @param key global keyboard accelerator
483 * @param enabled default state for enabled
484 * @return the new menu item
486 private TMenuItem
addItemInternal(final int id
, final String label
,
487 final TKeypress key
, final boolean enabled
) {
489 int newY
= getChildren().size() + 1;
490 assert (newY
< getHeight());
492 TMenuItem menuItem
= new TMenuItem(this, id
, 1, newY
, label
);
493 menuItem
.setKey(key
);
494 menuItem
.setEnabled(enabled
);
495 setHeight(getHeight() + 1);
496 if (menuItem
.getWidth() + 2 > getWidth()) {
497 setWidth(menuItem
.getWidth() + 2);
499 for (TWidget widget
: getChildren()) {
500 widget
.setWidth(getWidth() - 2);
502 getApplication().addMenuItem(menuItem
);
503 getApplication().recomputeMenuX();
509 * Convenience function to add one of the default menu items.
511 * @param id menu item ID. Must be between 0 (inclusive) and 1023
513 * @return the new menu item
515 public TMenuItem
addDefaultItem(final int id
) {
516 return addDefaultItem(id
, true);
520 * Convenience function to add one of the default menu items.
522 * @param id menu item ID. Must be between 0 (inclusive) and 1023
524 * @param enabled default state for enabled
525 * @return the new menu item
527 public TMenuItem
addDefaultItem(final int id
, final boolean enabled
) {
532 TKeypress key
= null;
537 label
= i18n
.getString("menuRepaintDesktop");
541 label
= i18n
.getString("menuViewImage");
544 case MID_CHANGE_FONT
:
545 label
= i18n
.getString("menuChangeFont");
549 label
= i18n
.getString("menuNew");
553 label
= i18n
.getString("menuExit");
558 label
= i18n
.getString("menuShell");
562 label
= i18n
.getString("menuOpen");
567 label
= i18n
.getString("menuCut");
571 label
= i18n
.getString("menuCopy");
575 label
= i18n
.getString("menuPaste");
579 label
= i18n
.getString("menuClear");
584 label
= i18n
.getString("menuFind");
587 label
= i18n
.getString("menuReplace");
589 case MID_SEARCH_AGAIN
:
590 label
= i18n
.getString("menuSearchAgain");
594 label
= i18n
.getString("menuGotoLine");
598 label
= i18n
.getString("menuWindowTile");
601 label
= i18n
.getString("menuWindowCascade");
604 label
= i18n
.getString("menuWindowCloseAll");
606 case MID_WINDOW_MOVE
:
607 label
= i18n
.getString("menuWindowMove");
610 case MID_WINDOW_ZOOM
:
611 label
= i18n
.getString("menuWindowZoom");
614 case MID_WINDOW_NEXT
:
615 label
= i18n
.getString("menuWindowNext");
618 case MID_WINDOW_PREVIOUS
:
619 label
= i18n
.getString("menuWindowPrevious");
622 case MID_WINDOW_CLOSE
:
623 label
= i18n
.getString("menuWindowClose");
627 case MID_HELP_CONTENTS
:
628 label
= i18n
.getString("menuHelpContents");
631 label
= i18n
.getString("menuHelpIndex");
634 case MID_HELP_SEARCH
:
635 label
= i18n
.getString("menuHelpSearch");
638 case MID_HELP_PREVIOUS
:
639 label
= i18n
.getString("menuHelpPrevious");
643 label
= i18n
.getString("menuHelpHelp");
645 case MID_HELP_ACTIVE_FILE
:
646 label
= i18n
.getString("menuHelpActive");
649 label
= i18n
.getString("menuHelpAbout");
652 case MID_TABLE_BORDER_NONE
:
653 label
= i18n
.getString("menuTableBorderNone");
655 case MID_TABLE_BORDER_ALL
:
656 label
= i18n
.getString("menuTableBorderAll");
658 case MID_TABLE_BORDER_RIGHT
:
659 label
= i18n
.getString("menuTableBorderRight");
661 case MID_TABLE_BORDER_LEFT
:
662 label
= i18n
.getString("menuTableBorderLeft");
664 case MID_TABLE_BORDER_TOP
:
665 label
= i18n
.getString("menuTableBorderTop");
667 case MID_TABLE_BORDER_BOTTOM
:
668 label
= i18n
.getString("menuTableBorderBottom");
670 case MID_TABLE_BORDER_DOUBLE_BOTTOM
:
671 label
= i18n
.getString("menuTableBorderDoubleBottom");
673 case MID_TABLE_BORDER_THICK_BOTTOM
:
674 label
= i18n
.getString("menuTableBorderThickBottom");
676 case MID_TABLE_DELETE_LEFT
:
677 label
= i18n
.getString("menuTableDeleteLeft");
679 case MID_TABLE_DELETE_UP
:
680 label
= i18n
.getString("menuTableDeleteUp");
682 case MID_TABLE_DELETE_ROW
:
683 label
= i18n
.getString("menuTableDeleteRow");
685 case MID_TABLE_DELETE_COLUMN
:
686 label
= i18n
.getString("menuTableDeleteColumn");
688 case MID_TABLE_INSERT_LEFT
:
689 label
= i18n
.getString("menuTableInsertLeft");
691 case MID_TABLE_INSERT_RIGHT
:
692 label
= i18n
.getString("menuTableInsertRight");
694 case MID_TABLE_INSERT_ABOVE
:
695 label
= i18n
.getString("menuTableInsertAbove");
697 case MID_TABLE_INSERT_BELOW
:
698 label
= i18n
.getString("menuTableInsertBelow");
700 case MID_TABLE_COLUMN_NARROW
:
701 label
= i18n
.getString("menuTableColumnNarrow");
704 case MID_TABLE_COLUMN_WIDEN
:
705 label
= i18n
.getString("menuTableColumnWiden");
708 case MID_TABLE_FILE_SAVE_CSV
:
709 label
= i18n
.getString("menuTableFileSaveCsv");
711 case MID_TABLE_FILE_SAVE_TEXT
:
712 label
= i18n
.getString("menuTableFileSaveText");
716 throw new IllegalArgumentException("Invalid menu ID: " + id
);
719 return addItemInternal(id
, label
, key
, enabled
);
723 * Convenience function to add a menu separator.
725 public void addSeparator() {
726 int newY
= getChildren().size() + 1;
727 assert (newY
< getHeight());
729 // We just have to construct it, don't need to hang onto what it
731 new TMenuSeparator(this, 1, newY
);
732 setHeight(getHeight() + 1);
736 * Convenience function to add a sub-menu.
738 * @param title menu title. Title must contain a keyboard shortcut,
739 * denoted by prefixing a letter with "&", e.g. "&File"
740 * @return the new sub-menu
742 public TSubMenu
addSubMenu(final String title
) {
743 int newY
= getChildren().size() + 1;
744 assert (newY
< getHeight());
746 TSubMenu subMenu
= new TSubMenu(this, title
, 1, newY
);
747 setHeight(getHeight() + 1);
748 if (subMenu
.getWidth() + 2 > getWidth()) {
749 setWidth(subMenu
.getWidth() + 2);
751 for (TWidget widget
: getChildren()) {
752 widget
.setWidth(getWidth() - 2);
754 getApplication().recomputeMenuX();
756 subMenu
.menu
.setX(getX() + getWidth() - 2);