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_VIEW_ROW_LABELS
= 60;
106 public static final int MID_TABLE_VIEW_COLUMN_LABELS
= 61;
107 public static final int MID_TABLE_VIEW_HIGHLIGHT_ROW
= 62;
108 public static final int MID_TABLE_VIEW_HIGHLIGHT_COLUMN
= 63;
109 public static final int MID_TABLE_BORDER_NONE
= 64;
110 public static final int MID_TABLE_BORDER_ALL
= 65;
111 public static final int MID_TABLE_BORDER_RIGHT
= 66;
112 public static final int MID_TABLE_BORDER_LEFT
= 67;
113 public static final int MID_TABLE_BORDER_TOP
= 68;
114 public static final int MID_TABLE_BORDER_BOTTOM
= 69;
115 public static final int MID_TABLE_BORDER_DOUBLE_BOTTOM
= 70;
116 public static final int MID_TABLE_BORDER_THICK_BOTTOM
= 71;
117 public static final int MID_TABLE_DELETE_LEFT
= 72;
118 public static final int MID_TABLE_DELETE_UP
= 73;
119 public static final int MID_TABLE_DELETE_ROW
= 74;
120 public static final int MID_TABLE_DELETE_COLUMN
= 75;
121 public static final int MID_TABLE_INSERT_LEFT
= 76;
122 public static final int MID_TABLE_INSERT_RIGHT
= 77;
123 public static final int MID_TABLE_INSERT_ABOVE
= 78;
124 public static final int MID_TABLE_INSERT_BELOW
= 79;
125 public static final int MID_TABLE_COLUMN_NARROW
= 80;
126 public static final int MID_TABLE_COLUMN_WIDEN
= 81;
127 public static final int MID_TABLE_FILE_SAVE_CSV
= 82;
128 public static final int MID_TABLE_FILE_SAVE_TEXT
= 83;
130 // ------------------------------------------------------------------------
131 // Variables --------------------------------------------------------------
132 // ------------------------------------------------------------------------
135 * If true, this is a sub-menu. Note package private access.
137 boolean isSubMenu
= false;
140 * The X position of the menu's title.
145 * The shortcut and title.
147 private MnemonicString mnemonic
;
149 // ------------------------------------------------------------------------
150 // Constructors -----------------------------------------------------------
151 // ------------------------------------------------------------------------
154 * Public constructor.
156 * @param parent parent application
157 * @param x column relative to parent
158 * @param y row relative to parent
159 * @param label mnemonic menu title. Label must contain a keyboard
160 * shortcut (mnemonic), denoted by prefixing a letter with "&",
163 public TMenu(final TApplication parent
, final int x
, final int y
,
164 final String label
) {
166 super(parent
, label
, x
, y
, parent
.getScreen().getWidth(),
167 parent
.getScreen().getHeight());
169 // Setup the menu shortcut
170 mnemonic
= new MnemonicString(label
);
171 setTitle(mnemonic
.getRawLabel());
172 assert (mnemonic
.getShortcutIdx() >= 0);
174 // Recompute width and height to reflect an empty menu
175 setWidth(getTitle().length() + 4);
181 // ------------------------------------------------------------------------
182 // Event handlers ---------------------------------------------------------
183 // ------------------------------------------------------------------------
186 * Handle mouse button presses.
188 * @param mouse mouse button event
191 public void onMouseDown(final TMouseEvent mouse
) {
193 super.onMouseDown(mouse
);
196 for (TWidget widget
: getChildren()) {
197 if (widget
.mouseWouldHit(mouse
)) {
198 // Dispatch to this child, also activate it
201 // Set x and y relative to the child's coordinates
202 mouse
.setX(mouse
.getAbsoluteX() - widget
.getAbsoluteX());
203 mouse
.setY(mouse
.getAbsoluteY() - widget
.getAbsoluteY());
204 widget
.handleEvent(mouse
);
211 * Handle mouse button releases.
213 * @param mouse mouse button release event
216 public void onMouseUp(final TMouseEvent mouse
) {
220 for (TWidget widget
: getChildren()) {
221 if (widget
.mouseWouldHit(mouse
)) {
222 // Dispatch to this child, also activate it
225 // Set x and y relative to the child's coordinates
226 mouse
.setX(mouse
.getAbsoluteX() - widget
.getAbsoluteX());
227 mouse
.setY(mouse
.getAbsoluteY() - widget
.getAbsoluteY());
228 widget
.handleEvent(mouse
);
235 * Handle mouse movements.
237 * @param mouse mouse motion event
240 public void onMouseMotion(final TMouseEvent mouse
) {
243 // See if we should activate a different menu item
244 for (TWidget widget
: getChildren()) {
245 if ((mouse
.isMouse1())
246 && (widget
.mouseWouldHit(mouse
))
248 // Activate this menu item
250 if (widget
instanceof TSubMenu
) {
251 ((TSubMenu
) widget
).dispatch();
261 * @param keypress keystroke event
264 public void onKeypress(final TKeypressEvent keypress
) {
267 System.err.printf("keypress: %s active child: %s\n", keypress,
271 if (getActiveChild() != this) {
272 if ((getActiveChild() instanceof TSubMenu
)
273 || (getActiveChild() instanceof TMenu
)
275 getActiveChild().onKeypress(keypress
);
280 if (keypress
.equals(kbEsc
)) {
281 getApplication().closeMenu();
284 if (keypress
.equals(kbDown
)) {
288 if (keypress
.equals(kbUp
)) {
292 if (keypress
.equals(kbRight
)) {
293 getApplication().switchMenu(true);
296 if (keypress
.equals(kbLeft
)) {
298 getApplication().closeSubMenu();
300 getApplication().switchMenu(false);
305 // Switch to a menuItem if it has an mnemonic
306 if (!keypress
.getKey().isFnKey()
307 && !keypress
.getKey().isAlt()
308 && !keypress
.getKey().isCtrl()) {
309 for (TWidget widget
: getChildren()) {
310 TMenuItem item
= (TMenuItem
) widget
;
311 if ((item
.getMnemonic() != null)
312 && (Character
.toLowerCase(item
.getMnemonic().getShortcut())
313 == Character
.toLowerCase(keypress
.getKey().getChar()))
315 // Send an enter keystroke to it
317 item
.handleEvent(new TKeypressEvent(kbEnter
));
323 // Dispatch the keypress to an active widget
324 for (TWidget widget
: getChildren()) {
325 if (widget
.isActive()) {
326 widget
.handleEvent(keypress
);
332 // ------------------------------------------------------------------------
333 // TWindow ----------------------------------------------------------------
334 // ------------------------------------------------------------------------
337 * Draw a top-level menu with title and menu items.
341 CellAttributes background
= getTheme().getColor("tmenu");
343 assert (isAbsoluteActive());
345 // Fill in the interior background
346 for (int i
= 0; i
< getHeight(); i
++) {
347 hLineXY(0, i
, getWidth(), ' ', background
);
357 cTopLeft
= GraphicsChars
.ULCORNER
;
358 cTopRight
= GraphicsChars
.URCORNER
;
359 cBottomLeft
= GraphicsChars
.LLCORNER
;
360 cBottomRight
= GraphicsChars
.LRCORNER
;
361 cHSide
= GraphicsChars
.SINGLE_BAR
;
363 // Place the corner characters
364 putCharXY(1, 0, cTopLeft
, background
);
365 putCharXY(getWidth() - 2, 0, cTopRight
, background
);
366 putCharXY(1, getHeight() - 1, cBottomLeft
, background
);
367 putCharXY(getWidth() - 2, getHeight() - 1, cBottomRight
, background
);
369 // Draw the box lines
370 hLineXY(1 + 1, 0, getWidth() - 4, cHSide
, background
);
371 hLineXY(1 + 1, getHeight() - 1, getWidth() - 4, cHSide
, background
);
374 drawBoxShadow(0, 0, getWidth(), getHeight());
377 // ------------------------------------------------------------------------
378 // TMenu ------------------------------------------------------------------
379 // ------------------------------------------------------------------------
382 * Set the menu title X position.
384 * @param titleX the position
386 public void setTitleX(final int titleX
) {
387 this.titleX
= titleX
;
391 * Get the menu title X position.
393 * @return the position
395 public int getTitleX() {
400 * Get the mnemonic string.
402 * @return the full mnemonic string
404 public MnemonicString
getMnemonic() {
409 * Convenience function to add a menu item.
411 * @param id menu item ID. Must be greater than 1024.
412 * @param label menu item label
413 * @return the new menu item
415 public TMenuItem
addItem(final int id
, final String label
) {
417 return addItemInternal(id
, label
, null);
421 * Convenience function to add a menu item.
423 * @param id menu item ID. Must be greater than 1024.
424 * @param label menu item label
425 * @param enabled default state for enabled
426 * @return the new menu item
428 public TMenuItem
addItem(final int id
, final String label
,
429 final boolean enabled
) {
432 return addItemInternal(id
, label
, null, enabled
);
436 * Convenience function to add a custom menu item.
438 * @param id menu item ID. Must be greater than 1024.
439 * @param label menu item label
440 * @param key global keyboard accelerator
441 * @return the new menu item
443 public TMenuItem
addItem(final int id
, final String label
,
444 final TKeypress key
) {
447 return addItemInternal(id
, label
, key
);
451 * Convenience function to add a custom menu item.
453 * @param id menu item ID. Must be greater than 1024.
454 * @param label menu item label
455 * @param key global keyboard accelerator
456 * @param enabled default state for enabled
457 * @return the new menu item
459 public TMenuItem
addItem(final int id
, final String label
,
460 final TKeypress key
, final boolean enabled
) {
462 TMenuItem item
= addItem(id
, label
, key
);
463 item
.setEnabled(enabled
);
468 * Convenience function to add a custom menu item.
470 * @param id menu item ID. Must be greater than 1024.
471 * @param label menu item label
472 * @param key global keyboard accelerator
473 * @return the new menu item
475 private TMenuItem
addItemInternal(final int id
, final String label
,
476 final TKeypress key
) {
478 return addItemInternal(id
, label
, key
, true);
482 * Convenience function to add a custom menu item.
484 * @param id menu item ID. Must be greater than 1024.
485 * @param label menu item label
486 * @param key global keyboard accelerator
487 * @param enabled default state for enabled
488 * @return the new menu item
490 private TMenuItem
addItemInternal(final int id
, final String label
,
491 final TKeypress key
, final boolean enabled
) {
493 int newY
= getChildren().size() + 1;
494 assert (newY
< getHeight());
496 TMenuItem menuItem
= new TMenuItem(this, id
, 1, newY
, label
);
497 menuItem
.setKey(key
);
498 menuItem
.setEnabled(enabled
);
499 setHeight(getHeight() + 1);
500 if (menuItem
.getWidth() + 2 > getWidth()) {
501 setWidth(menuItem
.getWidth() + 2);
503 for (TWidget widget
: getChildren()) {
504 widget
.setWidth(getWidth() - 2);
506 getApplication().addMenuItem(menuItem
);
507 getApplication().recomputeMenuX();
513 * Convenience function to add one of the default menu items.
515 * @param id menu item ID. Must be between 0 (inclusive) and 1023
517 * @return the new menu item
519 public TMenuItem
addDefaultItem(final int id
) {
520 return addDefaultItem(id
, true);
524 * Convenience function to add one of the default menu items.
526 * @param id menu item ID. Must be between 0 (inclusive) and 1023
528 * @param enabled default state for enabled
529 * @return the new menu item
531 public TMenuItem
addDefaultItem(final int id
, final boolean enabled
) {
536 TKeypress key
= null;
537 boolean checkable
= false;
538 boolean checked
= false;
543 label
= i18n
.getString("menuRepaintDesktop");
547 label
= i18n
.getString("menuViewImage");
550 case MID_CHANGE_FONT
:
551 label
= i18n
.getString("menuChangeFont");
555 label
= i18n
.getString("menuNew");
559 label
= i18n
.getString("menuExit");
564 label
= i18n
.getString("menuShell");
568 label
= i18n
.getString("menuOpen");
573 label
= i18n
.getString("menuCut");
577 label
= i18n
.getString("menuCopy");
581 label
= i18n
.getString("menuPaste");
585 label
= i18n
.getString("menuClear");
590 label
= i18n
.getString("menuFind");
593 label
= i18n
.getString("menuReplace");
595 case MID_SEARCH_AGAIN
:
596 label
= i18n
.getString("menuSearchAgain");
600 label
= i18n
.getString("menuGotoLine");
604 label
= i18n
.getString("menuWindowTile");
607 label
= i18n
.getString("menuWindowCascade");
610 label
= i18n
.getString("menuWindowCloseAll");
612 case MID_WINDOW_MOVE
:
613 label
= i18n
.getString("menuWindowMove");
616 case MID_WINDOW_ZOOM
:
617 label
= i18n
.getString("menuWindowZoom");
620 case MID_WINDOW_NEXT
:
621 label
= i18n
.getString("menuWindowNext");
624 case MID_WINDOW_PREVIOUS
:
625 label
= i18n
.getString("menuWindowPrevious");
628 case MID_WINDOW_CLOSE
:
629 label
= i18n
.getString("menuWindowClose");
633 case MID_HELP_CONTENTS
:
634 label
= i18n
.getString("menuHelpContents");
637 label
= i18n
.getString("menuHelpIndex");
640 case MID_HELP_SEARCH
:
641 label
= i18n
.getString("menuHelpSearch");
644 case MID_HELP_PREVIOUS
:
645 label
= i18n
.getString("menuHelpPrevious");
649 label
= i18n
.getString("menuHelpHelp");
651 case MID_HELP_ACTIVE_FILE
:
652 label
= i18n
.getString("menuHelpActive");
655 label
= i18n
.getString("menuHelpAbout");
658 case MID_TABLE_VIEW_ROW_LABELS
:
659 label
= i18n
.getString("menuTableViewRowLabels");
663 case MID_TABLE_VIEW_COLUMN_LABELS
:
664 label
= i18n
.getString("menuTableViewColumnLabels");
668 case MID_TABLE_VIEW_HIGHLIGHT_ROW
:
669 label
= i18n
.getString("menuTableViewHighlightRow");
673 case MID_TABLE_VIEW_HIGHLIGHT_COLUMN
:
674 label
= i18n
.getString("menuTableViewHighlightColumn");
679 case MID_TABLE_BORDER_NONE
:
680 label
= i18n
.getString("menuTableBorderNone");
682 case MID_TABLE_BORDER_ALL
:
683 label
= i18n
.getString("menuTableBorderAll");
685 case MID_TABLE_BORDER_RIGHT
:
686 label
= i18n
.getString("menuTableBorderRight");
688 case MID_TABLE_BORDER_LEFT
:
689 label
= i18n
.getString("menuTableBorderLeft");
691 case MID_TABLE_BORDER_TOP
:
692 label
= i18n
.getString("menuTableBorderTop");
694 case MID_TABLE_BORDER_BOTTOM
:
695 label
= i18n
.getString("menuTableBorderBottom");
697 case MID_TABLE_BORDER_DOUBLE_BOTTOM
:
698 label
= i18n
.getString("menuTableBorderDoubleBottom");
700 case MID_TABLE_BORDER_THICK_BOTTOM
:
701 label
= i18n
.getString("menuTableBorderThickBottom");
703 case MID_TABLE_DELETE_LEFT
:
704 label
= i18n
.getString("menuTableDeleteLeft");
706 case MID_TABLE_DELETE_UP
:
707 label
= i18n
.getString("menuTableDeleteUp");
709 case MID_TABLE_DELETE_ROW
:
710 label
= i18n
.getString("menuTableDeleteRow");
712 case MID_TABLE_DELETE_COLUMN
:
713 label
= i18n
.getString("menuTableDeleteColumn");
715 case MID_TABLE_INSERT_LEFT
:
716 label
= i18n
.getString("menuTableInsertLeft");
718 case MID_TABLE_INSERT_RIGHT
:
719 label
= i18n
.getString("menuTableInsertRight");
721 case MID_TABLE_INSERT_ABOVE
:
722 label
= i18n
.getString("menuTableInsertAbove");
724 case MID_TABLE_INSERT_BELOW
:
725 label
= i18n
.getString("menuTableInsertBelow");
727 case MID_TABLE_COLUMN_NARROW
:
728 label
= i18n
.getString("menuTableColumnNarrow");
731 case MID_TABLE_COLUMN_WIDEN
:
732 label
= i18n
.getString("menuTableColumnWiden");
735 case MID_TABLE_FILE_SAVE_CSV
:
736 label
= i18n
.getString("menuTableFileSaveCsv");
738 case MID_TABLE_FILE_SAVE_TEXT
:
739 label
= i18n
.getString("menuTableFileSaveText");
743 throw new IllegalArgumentException("Invalid menu ID: " + id
);
746 TMenuItem item
= addItemInternal(id
, label
, key
, enabled
);
747 item
.setCheckable(checkable
);
752 * Convenience function to add a menu separator.
754 public void addSeparator() {
755 int newY
= getChildren().size() + 1;
756 assert (newY
< getHeight());
758 // We just have to construct it, don't need to hang onto what it
760 new TMenuSeparator(this, 1, newY
);
761 setHeight(getHeight() + 1);
765 * Convenience function to add a sub-menu.
767 * @param title menu title. Title must contain a keyboard shortcut,
768 * denoted by prefixing a letter with "&", e.g. "&File"
769 * @return the new sub-menu
771 public TSubMenu
addSubMenu(final String title
) {
772 int newY
= getChildren().size() + 1;
773 assert (newY
< getHeight());
775 TSubMenu subMenu
= new TSubMenu(this, title
, 1, newY
);
776 setHeight(getHeight() + 1);
777 if (subMenu
.getWidth() + 2 > getWidth()) {
778 setWidth(subMenu
.getWidth() + 2);
780 for (TWidget widget
: getChildren()) {
781 widget
.setWidth(getWidth() - 2);
783 getApplication().recomputeMenuX();
785 subMenu
.menu
.setX(getX() + getWidth() - 2);