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_RENAME_ROW
= 60;
106 public static final int MID_TABLE_RENAME_COLUMN
= 61;
107 public static final int MID_TABLE_VIEW_ROW_LABELS
= 70;
108 public static final int MID_TABLE_VIEW_COLUMN_LABELS
= 71;
109 public static final int MID_TABLE_VIEW_HIGHLIGHT_ROW
= 72;
110 public static final int MID_TABLE_VIEW_HIGHLIGHT_COLUMN
= 73;
111 public static final int MID_TABLE_BORDER_NONE
= 80;
112 public static final int MID_TABLE_BORDER_ALL
= 81;
113 public static final int MID_TABLE_BORDER_CELL_NONE
= 82;
114 public static final int MID_TABLE_BORDER_CELL_ALL
= 83;
115 public static final int MID_TABLE_BORDER_RIGHT
= 84;
116 public static final int MID_TABLE_BORDER_LEFT
= 85;
117 public static final int MID_TABLE_BORDER_TOP
= 86;
118 public static final int MID_TABLE_BORDER_BOTTOM
= 87;
119 public static final int MID_TABLE_BORDER_DOUBLE_BOTTOM
= 88;
120 public static final int MID_TABLE_BORDER_THICK_BOTTOM
= 89;
121 public static final int MID_TABLE_DELETE_LEFT
= 100;
122 public static final int MID_TABLE_DELETE_UP
= 101;
123 public static final int MID_TABLE_DELETE_ROW
= 102;
124 public static final int MID_TABLE_DELETE_COLUMN
= 103;
125 public static final int MID_TABLE_INSERT_LEFT
= 104;
126 public static final int MID_TABLE_INSERT_RIGHT
= 105;
127 public static final int MID_TABLE_INSERT_ABOVE
= 106;
128 public static final int MID_TABLE_INSERT_BELOW
= 107;
129 public static final int MID_TABLE_COLUMN_NARROW
= 110;
130 public static final int MID_TABLE_COLUMN_WIDEN
= 111;
131 public static final int MID_TABLE_FILE_SAVE_CSV
= 115;
132 public static final int MID_TABLE_FILE_SAVE_TEXT
= 116;
134 // ------------------------------------------------------------------------
135 // Variables --------------------------------------------------------------
136 // ------------------------------------------------------------------------
139 * If true, this is a sub-menu. Note package private access.
141 boolean isSubMenu
= false;
144 * The X position of the menu's title.
149 * The shortcut and title.
151 private MnemonicString mnemonic
;
153 // ------------------------------------------------------------------------
154 // Constructors -----------------------------------------------------------
155 // ------------------------------------------------------------------------
158 * Public constructor.
160 * @param parent parent application
161 * @param x column relative to parent
162 * @param y row relative to parent
163 * @param label mnemonic menu title. Label must contain a keyboard
164 * shortcut (mnemonic), denoted by prefixing a letter with "&",
167 public TMenu(final TApplication parent
, final int x
, final int y
,
168 final String label
) {
170 super(parent
, label
, x
, y
, parent
.getScreen().getWidth(),
171 parent
.getScreen().getHeight());
173 // Setup the menu shortcut
174 mnemonic
= new MnemonicString(label
);
175 setTitle(mnemonic
.getRawLabel());
176 assert (mnemonic
.getShortcutIdx() >= 0);
178 // Recompute width and height to reflect an empty menu
179 setWidth(getTitle().length() + 4);
185 // ------------------------------------------------------------------------
186 // Event handlers ---------------------------------------------------------
187 // ------------------------------------------------------------------------
190 * Handle mouse button presses.
192 * @param mouse mouse button event
195 public void onMouseDown(final TMouseEvent mouse
) {
197 super.onMouseDown(mouse
);
200 for (TWidget widget
: getChildren()) {
201 if (widget
.mouseWouldHit(mouse
)) {
202 // Dispatch to this child, also activate it
205 // Set x and y relative to the child's coordinates
206 mouse
.setX(mouse
.getAbsoluteX() - widget
.getAbsoluteX());
207 mouse
.setY(mouse
.getAbsoluteY() - widget
.getAbsoluteY());
208 widget
.handleEvent(mouse
);
215 * Handle mouse button releases.
217 * @param mouse mouse button release event
220 public void onMouseUp(final TMouseEvent mouse
) {
224 for (TWidget widget
: getChildren()) {
225 if (widget
.mouseWouldHit(mouse
)) {
226 // Dispatch to this child, also activate it
229 // Set x and y relative to the child's coordinates
230 mouse
.setX(mouse
.getAbsoluteX() - widget
.getAbsoluteX());
231 mouse
.setY(mouse
.getAbsoluteY() - widget
.getAbsoluteY());
232 widget
.handleEvent(mouse
);
239 * Handle mouse movements.
241 * @param mouse mouse motion event
244 public void onMouseMotion(final TMouseEvent mouse
) {
247 // See if we should activate a different menu item
248 for (TWidget widget
: getChildren()) {
249 if ((mouse
.isMouse1())
250 && (widget
.mouseWouldHit(mouse
))
252 // Activate this menu item
254 if (widget
instanceof TSubMenu
) {
255 ((TSubMenu
) widget
).dispatch();
265 * @param keypress keystroke event
268 public void onKeypress(final TKeypressEvent keypress
) {
271 System.err.printf("keypress: %s active child: %s\n", keypress,
275 if (getActiveChild() != this) {
276 if (getActiveChild() instanceof TMenu
) {
277 getActiveChild().onKeypress(keypress
);
281 if (getActiveChild() instanceof TSubMenu
) {
282 TSubMenu subMenu
= (TSubMenu
) getActiveChild();
283 if (subMenu
.menu
.isActive()) {
284 subMenu
.onKeypress(keypress
);
290 if (keypress
.equals(kbEsc
)) {
291 getApplication().closeMenu();
294 if (keypress
.equals(kbDown
)) {
298 if (keypress
.equals(kbUp
)) {
302 if (keypress
.equals(kbRight
)) {
303 getApplication().switchMenu(true);
306 if (keypress
.equals(kbLeft
)) {
308 getApplication().closeSubMenu();
310 getApplication().switchMenu(false);
315 // Switch to a menuItem if it has an mnemonic
316 if (!keypress
.getKey().isFnKey()
317 && !keypress
.getKey().isAlt()
318 && !keypress
.getKey().isCtrl()) {
320 // System.err.println("Checking children for mnemonic...");
322 for (TWidget widget
: getChildren()) {
323 TMenuItem item
= (TMenuItem
) widget
;
324 if ((item
.isEnabled() == true)
325 && (item
.getMnemonic() != null)
326 && (Character
.toLowerCase(item
.getMnemonic().getShortcut())
327 == Character
.toLowerCase(keypress
.getKey().getChar()))
329 // System.err.println("activate: " + item);
331 // Send an enter keystroke to it
333 item
.handleEvent(new TKeypressEvent(kbEnter
));
339 // Dispatch the keypress to an active widget
340 for (TWidget widget
: getChildren()) {
341 if (widget
.isActive()) {
342 widget
.handleEvent(keypress
);
348 // ------------------------------------------------------------------------
349 // TWindow ----------------------------------------------------------------
350 // ------------------------------------------------------------------------
353 * Draw a top-level menu with title and menu items.
357 CellAttributes background
= getTheme().getColor("tmenu");
359 assert (isAbsoluteActive());
361 // Fill in the interior background
362 for (int i
= 0; i
< getHeight(); i
++) {
363 hLineXY(0, i
, getWidth(), ' ', background
);
373 cTopLeft
= GraphicsChars
.ULCORNER
;
374 cTopRight
= GraphicsChars
.URCORNER
;
375 cBottomLeft
= GraphicsChars
.LLCORNER
;
376 cBottomRight
= GraphicsChars
.LRCORNER
;
377 cHSide
= GraphicsChars
.SINGLE_BAR
;
379 // Place the corner characters
380 putCharXY(1, 0, cTopLeft
, background
);
381 putCharXY(getWidth() - 2, 0, cTopRight
, background
);
382 putCharXY(1, getHeight() - 1, cBottomLeft
, background
);
383 putCharXY(getWidth() - 2, getHeight() - 1, cBottomRight
, background
);
385 // Draw the box lines
386 hLineXY(1 + 1, 0, getWidth() - 4, cHSide
, background
);
387 hLineXY(1 + 1, getHeight() - 1, getWidth() - 4, cHSide
, background
);
390 drawBoxShadow(0, 0, getWidth(), getHeight());
393 // ------------------------------------------------------------------------
394 // TMenu ------------------------------------------------------------------
395 // ------------------------------------------------------------------------
398 * Set the menu title X position.
400 * @param titleX the position
402 public void setTitleX(final int titleX
) {
403 this.titleX
= titleX
;
407 * Get the menu title X position.
409 * @return the position
411 public int getTitleX() {
416 * Get the mnemonic string.
418 * @return the full mnemonic string
420 public MnemonicString
getMnemonic() {
425 * Convenience function to add a menu item.
427 * @param id menu item ID. Must be greater than 1024.
428 * @param label menu item label
429 * @return the new menu item
431 public TMenuItem
addItem(final int id
, final String label
) {
433 return addItemInternal(id
, label
, null);
437 * Convenience function to add a menu item.
439 * @param id menu item ID. Must be greater than 1024.
440 * @param label menu item label
441 * @param enabled default state for enabled
442 * @return the new menu item
444 public TMenuItem
addItem(final int id
, final String label
,
445 final boolean enabled
) {
448 return addItemInternal(id
, label
, null, enabled
);
452 * Convenience function to add a custom menu item.
454 * @param id menu item ID. Must be greater than 1024.
455 * @param label menu item label
456 * @param key global keyboard accelerator
457 * @return the new menu item
459 public TMenuItem
addItem(final int id
, final String label
,
460 final TKeypress key
) {
463 return addItemInternal(id
, label
, key
);
467 * Convenience function to add a custom menu item.
469 * @param id menu item ID. Must be greater than 1024.
470 * @param label menu item label
471 * @param key global keyboard accelerator
472 * @param enabled default state for enabled
473 * @return the new menu item
475 public TMenuItem
addItem(final int id
, final String label
,
476 final TKeypress key
, final boolean enabled
) {
478 TMenuItem item
= addItem(id
, label
, key
);
479 item
.setEnabled(enabled
);
484 * Convenience function to add a custom menu item.
486 * @param id menu item ID. Must be greater than 1024.
487 * @param label menu item label
488 * @param key global keyboard accelerator
489 * @return the new menu item
491 private TMenuItem
addItemInternal(final int id
, final String label
,
492 final TKeypress key
) {
494 return addItemInternal(id
, label
, key
, true);
498 * Convenience function to add a custom menu item.
500 * @param id menu item ID. Must be greater than 1024.
501 * @param label menu item label
502 * @param key global keyboard accelerator
503 * @param enabled default state for enabled
504 * @return the new menu item
506 private TMenuItem
addItemInternal(final int id
, final String label
,
507 final TKeypress key
, final boolean enabled
) {
509 int newY
= getChildren().size() + 1;
510 assert (newY
< getHeight());
512 TMenuItem menuItem
= new TMenuItem(this, id
, 1, newY
, label
);
513 menuItem
.setKey(key
);
514 menuItem
.setEnabled(enabled
);
515 setHeight(getHeight() + 1);
516 if (menuItem
.getWidth() + 2 > getWidth()) {
517 setWidth(menuItem
.getWidth() + 2);
519 for (TWidget widget
: getChildren()) {
520 widget
.setWidth(getWidth() - 2);
522 getApplication().addMenuItem(menuItem
);
523 getApplication().recomputeMenuX();
529 * Convenience function to add one of the default menu items.
531 * @param id menu item ID. Must be between 0 (inclusive) and 1023
533 * @return the new menu item
535 public TMenuItem
addDefaultItem(final int id
) {
536 return addDefaultItem(id
, true);
540 * Convenience function to add one of the default menu items.
542 * @param id menu item ID. Must be between 0 (inclusive) and 1023
544 * @param enabled default state for enabled
545 * @return the new menu item
547 public TMenuItem
addDefaultItem(final int id
, final boolean enabled
) {
552 TKeypress key
= null;
553 boolean checkable
= false;
554 boolean checked
= false;
559 label
= i18n
.getString("menuRepaintDesktop");
563 label
= i18n
.getString("menuViewImage");
566 case MID_CHANGE_FONT
:
567 label
= i18n
.getString("menuChangeFont");
571 label
= i18n
.getString("menuNew");
575 label
= i18n
.getString("menuExit");
580 label
= i18n
.getString("menuShell");
584 label
= i18n
.getString("menuOpen");
589 label
= i18n
.getString("menuCut");
593 label
= i18n
.getString("menuCopy");
597 label
= i18n
.getString("menuPaste");
601 label
= i18n
.getString("menuClear");
606 label
= i18n
.getString("menuFind");
609 label
= i18n
.getString("menuReplace");
611 case MID_SEARCH_AGAIN
:
612 label
= i18n
.getString("menuSearchAgain");
616 label
= i18n
.getString("menuGotoLine");
620 label
= i18n
.getString("menuWindowTile");
623 label
= i18n
.getString("menuWindowCascade");
626 label
= i18n
.getString("menuWindowCloseAll");
628 case MID_WINDOW_MOVE
:
629 label
= i18n
.getString("menuWindowMove");
632 case MID_WINDOW_ZOOM
:
633 label
= i18n
.getString("menuWindowZoom");
636 case MID_WINDOW_NEXT
:
637 label
= i18n
.getString("menuWindowNext");
640 case MID_WINDOW_PREVIOUS
:
641 label
= i18n
.getString("menuWindowPrevious");
644 case MID_WINDOW_CLOSE
:
645 label
= i18n
.getString("menuWindowClose");
649 case MID_HELP_CONTENTS
:
650 label
= i18n
.getString("menuHelpContents");
653 label
= i18n
.getString("menuHelpIndex");
656 case MID_HELP_SEARCH
:
657 label
= i18n
.getString("menuHelpSearch");
660 case MID_HELP_PREVIOUS
:
661 label
= i18n
.getString("menuHelpPrevious");
665 label
= i18n
.getString("menuHelpHelp");
667 case MID_HELP_ACTIVE_FILE
:
668 label
= i18n
.getString("menuHelpActive");
671 label
= i18n
.getString("menuHelpAbout");
674 case MID_TABLE_RENAME_COLUMN
:
675 label
= i18n
.getString("menuTableRenameColumn");
677 case MID_TABLE_RENAME_ROW
:
678 label
= i18n
.getString("menuTableRenameRow");
680 case MID_TABLE_VIEW_ROW_LABELS
:
681 label
= i18n
.getString("menuTableViewRowLabels");
685 case MID_TABLE_VIEW_COLUMN_LABELS
:
686 label
= i18n
.getString("menuTableViewColumnLabels");
690 case MID_TABLE_VIEW_HIGHLIGHT_ROW
:
691 label
= i18n
.getString("menuTableViewHighlightRow");
695 case MID_TABLE_VIEW_HIGHLIGHT_COLUMN
:
696 label
= i18n
.getString("menuTableViewHighlightColumn");
701 case MID_TABLE_BORDER_NONE
:
702 label
= i18n
.getString("menuTableBorderNone");
704 case MID_TABLE_BORDER_ALL
:
705 label
= i18n
.getString("menuTableBorderAll");
707 case MID_TABLE_BORDER_CELL_NONE
:
708 label
= i18n
.getString("menuTableBorderCellNone");
710 case MID_TABLE_BORDER_CELL_ALL
:
711 label
= i18n
.getString("menuTableBorderCellAll");
713 case MID_TABLE_BORDER_RIGHT
:
714 label
= i18n
.getString("menuTableBorderRight");
716 case MID_TABLE_BORDER_LEFT
:
717 label
= i18n
.getString("menuTableBorderLeft");
719 case MID_TABLE_BORDER_TOP
:
720 label
= i18n
.getString("menuTableBorderTop");
722 case MID_TABLE_BORDER_BOTTOM
:
723 label
= i18n
.getString("menuTableBorderBottom");
725 case MID_TABLE_BORDER_DOUBLE_BOTTOM
:
726 label
= i18n
.getString("menuTableBorderDoubleBottom");
728 case MID_TABLE_BORDER_THICK_BOTTOM
:
729 label
= i18n
.getString("menuTableBorderThickBottom");
731 case MID_TABLE_DELETE_LEFT
:
732 label
= i18n
.getString("menuTableDeleteLeft");
734 case MID_TABLE_DELETE_UP
:
735 label
= i18n
.getString("menuTableDeleteUp");
737 case MID_TABLE_DELETE_ROW
:
738 label
= i18n
.getString("menuTableDeleteRow");
740 case MID_TABLE_DELETE_COLUMN
:
741 label
= i18n
.getString("menuTableDeleteColumn");
743 case MID_TABLE_INSERT_LEFT
:
744 label
= i18n
.getString("menuTableInsertLeft");
746 case MID_TABLE_INSERT_RIGHT
:
747 label
= i18n
.getString("menuTableInsertRight");
749 case MID_TABLE_INSERT_ABOVE
:
750 label
= i18n
.getString("menuTableInsertAbove");
752 case MID_TABLE_INSERT_BELOW
:
753 label
= i18n
.getString("menuTableInsertBelow");
755 case MID_TABLE_COLUMN_NARROW
:
756 label
= i18n
.getString("menuTableColumnNarrow");
759 case MID_TABLE_COLUMN_WIDEN
:
760 label
= i18n
.getString("menuTableColumnWiden");
763 case MID_TABLE_FILE_SAVE_CSV
:
764 label
= i18n
.getString("menuTableFileSaveCsv");
766 case MID_TABLE_FILE_SAVE_TEXT
:
767 label
= i18n
.getString("menuTableFileSaveText");
771 throw new IllegalArgumentException("Invalid menu ID: " + id
);
774 TMenuItem item
= addItemInternal(id
, label
, key
, enabled
);
775 item
.setCheckable(checkable
);
780 * Convenience function to add a menu separator.
782 public void addSeparator() {
783 int newY
= getChildren().size() + 1;
784 assert (newY
< getHeight());
786 // We just have to construct it, don't need to hang onto what it
788 new TMenuSeparator(this, 1, newY
);
789 setHeight(getHeight() + 1);
793 * Convenience function to add a sub-menu.
795 * @param title menu title. Title must contain a keyboard shortcut,
796 * denoted by prefixing a letter with "&", e.g. "&File"
797 * @return the new sub-menu
799 public TSubMenu
addSubMenu(final String title
) {
800 int newY
= getChildren().size() + 1;
801 assert (newY
< getHeight());
803 TSubMenu subMenu
= new TSubMenu(this, title
, 1, newY
);
804 setHeight(getHeight() + 1);
805 if (subMenu
.getWidth() + 2 > getWidth()) {
806 setWidth(subMenu
.getWidth() + 2);
808 for (TWidget widget
: getChildren()) {
809 widget
.setWidth(getWidth() - 2);
811 getApplication().recomputeMenuX();
813 subMenu
.menu
.setX(getX() + getWidth() - 2);