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
.bits
.StringUtils
;
41 import jexer
.event
.TKeypressEvent
;
42 import jexer
.event
.TMouseEvent
;
43 import static jexer
.TKeypress
.*;
46 * TMenu is a top-level collection of TMenuItems.
48 public class TMenu
extends TWindow
{
53 private static final ResourceBundle i18n
= ResourceBundle
.getBundle(TMenu
.class.getName());
55 // ------------------------------------------------------------------------
56 // Constants --------------------------------------------------------------
57 // ------------------------------------------------------------------------
59 // Reserved menu item IDs
60 public static final int MID_UNUSED
= -1;
63 public static final int MID_REPAINT
= 1;
64 public static final int MID_VIEW_IMAGE
= 2;
65 public static final int MID_SCREEN_OPTIONS
= 3;
68 public static final int MID_NEW
= 10;
69 public static final int MID_EXIT
= 11;
70 public static final int MID_QUIT
= MID_EXIT
;
71 public static final int MID_OPEN_FILE
= 12;
72 public static final int MID_SHELL
= 13;
75 public static final int MID_CUT
= 20;
76 public static final int MID_COPY
= 21;
77 public static final int MID_PASTE
= 22;
78 public static final int MID_CLEAR
= 23;
81 public static final int MID_FIND
= 30;
82 public static final int MID_REPLACE
= 31;
83 public static final int MID_SEARCH_AGAIN
= 32;
84 public static final int MID_GOTO_LINE
= 33;
87 public static final int MID_TILE
= 40;
88 public static final int MID_CASCADE
= 41;
89 public static final int MID_CLOSE_ALL
= 42;
90 public static final int MID_WINDOW_MOVE
= 43;
91 public static final int MID_WINDOW_ZOOM
= 44;
92 public static final int MID_WINDOW_NEXT
= 45;
93 public static final int MID_WINDOW_PREVIOUS
= 46;
94 public static final int MID_WINDOW_CLOSE
= 47;
97 public static final int MID_HELP_CONTENTS
= 50;
98 public static final int MID_HELP_INDEX
= 51;
99 public static final int MID_HELP_SEARCH
= 52;
100 public static final int MID_HELP_PREVIOUS
= 53;
101 public static final int MID_HELP_HELP
= 54;
102 public static final int MID_HELP_ACTIVE_FILE
= 55;
103 public static final int MID_ABOUT
= 56;
106 public static final int MID_TABLE_RENAME_ROW
= 60;
107 public static final int MID_TABLE_RENAME_COLUMN
= 61;
108 public static final int MID_TABLE_VIEW_ROW_LABELS
= 70;
109 public static final int MID_TABLE_VIEW_COLUMN_LABELS
= 71;
110 public static final int MID_TABLE_VIEW_HIGHLIGHT_ROW
= 72;
111 public static final int MID_TABLE_VIEW_HIGHLIGHT_COLUMN
= 73;
112 public static final int MID_TABLE_BORDER_NONE
= 80;
113 public static final int MID_TABLE_BORDER_ALL
= 81;
114 public static final int MID_TABLE_BORDER_CELL_NONE
= 82;
115 public static final int MID_TABLE_BORDER_CELL_ALL
= 83;
116 public static final int MID_TABLE_BORDER_RIGHT
= 84;
117 public static final int MID_TABLE_BORDER_LEFT
= 85;
118 public static final int MID_TABLE_BORDER_TOP
= 86;
119 public static final int MID_TABLE_BORDER_BOTTOM
= 87;
120 public static final int MID_TABLE_BORDER_DOUBLE_BOTTOM
= 88;
121 public static final int MID_TABLE_BORDER_THICK_BOTTOM
= 89;
122 public static final int MID_TABLE_DELETE_LEFT
= 100;
123 public static final int MID_TABLE_DELETE_UP
= 101;
124 public static final int MID_TABLE_DELETE_ROW
= 102;
125 public static final int MID_TABLE_DELETE_COLUMN
= 103;
126 public static final int MID_TABLE_INSERT_LEFT
= 104;
127 public static final int MID_TABLE_INSERT_RIGHT
= 105;
128 public static final int MID_TABLE_INSERT_ABOVE
= 106;
129 public static final int MID_TABLE_INSERT_BELOW
= 107;
130 public static final int MID_TABLE_COLUMN_NARROW
= 110;
131 public static final int MID_TABLE_COLUMN_WIDEN
= 111;
132 public static final int MID_TABLE_FILE_OPEN_CSV
= 115;
133 public static final int MID_TABLE_FILE_SAVE_CSV
= 116;
134 public static final int MID_TABLE_FILE_SAVE_TEXT
= 117;
136 // ------------------------------------------------------------------------
137 // Variables --------------------------------------------------------------
138 // ------------------------------------------------------------------------
141 * If true, this is a sub-menu. Note package private access.
143 boolean isSubMenu
= false;
146 * The X position of the menu's title.
151 * The shortcut and title.
153 private MnemonicString mnemonic
;
155 // ------------------------------------------------------------------------
156 // Constructors -----------------------------------------------------------
157 // ------------------------------------------------------------------------
160 * Public constructor.
162 * @param parent parent application
163 * @param x column relative to parent
164 * @param y row relative to parent
165 * @param label mnemonic menu title. Label must contain a keyboard
166 * shortcut (mnemonic), denoted by prefixing a letter with "&",
169 public TMenu(final TApplication parent
, final int x
, final int y
,
170 final String label
) {
172 super(parent
, label
, x
, y
, parent
.getScreen().getWidth(),
173 parent
.getScreen().getHeight());
175 // Setup the menu shortcut
176 mnemonic
= new MnemonicString(label
);
177 setTitle(mnemonic
.getRawLabel());
178 assert (mnemonic
.getShortcutIdx() >= 0);
180 // Recompute width and height to reflect an empty menu
181 setWidth(StringUtils
.width(getTitle()) + 4);
187 // ------------------------------------------------------------------------
188 // Event handlers ---------------------------------------------------------
189 // ------------------------------------------------------------------------
192 * Handle mouse button presses.
194 * @param mouse mouse button event
197 public void onMouseDown(final TMouseEvent mouse
) {
199 super.onMouseDown(mouse
);
202 for (TWidget widget
: getChildren()) {
203 if (widget
.mouseWouldHit(mouse
)) {
204 // Dispatch to this child, also activate it
207 // Set x and y relative to the child's coordinates
208 mouse
.setX(mouse
.getAbsoluteX() - widget
.getAbsoluteX());
209 mouse
.setY(mouse
.getAbsoluteY() - widget
.getAbsoluteY());
210 widget
.handleEvent(mouse
);
217 * Handle mouse button releases.
219 * @param mouse mouse button release event
222 public void onMouseUp(final TMouseEvent mouse
) {
226 for (TWidget widget
: getChildren()) {
227 if (widget
.mouseWouldHit(mouse
)) {
228 // Dispatch to this child, also activate it
231 // Set x and y relative to the child's coordinates
232 mouse
.setX(mouse
.getAbsoluteX() - widget
.getAbsoluteX());
233 mouse
.setY(mouse
.getAbsoluteY() - widget
.getAbsoluteY());
234 widget
.handleEvent(mouse
);
241 * Handle mouse movements.
243 * @param mouse mouse motion event
246 public void onMouseMotion(final TMouseEvent mouse
) {
249 // See if we should activate a different menu item
250 for (TWidget widget
: getChildren()) {
251 if ((mouse
.isMouse1())
252 && (widget
.mouseWouldHit(mouse
))
254 // Activate this menu item
256 if (widget
instanceof TSubMenu
) {
257 ((TSubMenu
) widget
).dispatch();
267 * @param keypress keystroke event
270 public void onKeypress(final TKeypressEvent keypress
) {
273 System.err.printf("keypress: %s active child: %s\n", keypress,
277 if (getActiveChild() != this) {
278 if (getActiveChild() instanceof TMenu
) {
279 getActiveChild().onKeypress(keypress
);
283 if (getActiveChild() instanceof TSubMenu
) {
284 TSubMenu subMenu
= (TSubMenu
) getActiveChild();
285 if (subMenu
.menu
.isActive()) {
286 subMenu
.onKeypress(keypress
);
292 if (keypress
.equals(kbEsc
)) {
293 getApplication().closeMenu();
296 if (keypress
.equals(kbDown
)) {
300 if (keypress
.equals(kbUp
)) {
304 if (keypress
.equals(kbRight
)) {
305 getApplication().switchMenu(true);
308 if (keypress
.equals(kbLeft
)) {
310 getApplication().closeSubMenu();
312 getApplication().switchMenu(false);
317 // Switch to a menuItem if it has an mnemonic
318 if (!keypress
.getKey().isFnKey()
319 && !keypress
.getKey().isAlt()
320 && !keypress
.getKey().isCtrl()) {
322 // System.err.println("Checking children for mnemonic...");
324 for (TWidget widget
: getChildren()) {
325 TMenuItem item
= (TMenuItem
) widget
;
326 if ((item
.isEnabled() == true)
327 && (item
.getMnemonic() != null)
328 && (Character
.toLowerCase(item
.getMnemonic().getShortcut())
329 == Character
.toLowerCase(keypress
.getKey().getChar()))
331 // System.err.println("activate: " + item);
333 // Send an enter keystroke to it
335 item
.handleEvent(new TKeypressEvent(kbEnter
));
341 // Dispatch the keypress to an active widget
342 for (TWidget widget
: getChildren()) {
343 if (widget
.isActive()) {
344 widget
.handleEvent(keypress
);
350 // ------------------------------------------------------------------------
351 // TWindow ----------------------------------------------------------------
352 // ------------------------------------------------------------------------
355 * Draw a top-level menu with title and menu items.
359 CellAttributes background
= getTheme().getColor("tmenu");
361 assert (isAbsoluteActive());
363 // Fill in the interior background
364 for (int i
= 0; i
< getHeight(); i
++) {
365 hLineXY(0, i
, getWidth(), ' ', background
);
375 cTopLeft
= GraphicsChars
.ULCORNER
;
376 cTopRight
= GraphicsChars
.URCORNER
;
377 cBottomLeft
= GraphicsChars
.LLCORNER
;
378 cBottomRight
= GraphicsChars
.LRCORNER
;
379 cHSide
= GraphicsChars
.SINGLE_BAR
;
381 // Place the corner characters
382 putCharXY(1, 0, cTopLeft
, background
);
383 putCharXY(getWidth() - 2, 0, cTopRight
, background
);
384 putCharXY(1, getHeight() - 1, cBottomLeft
, background
);
385 putCharXY(getWidth() - 2, getHeight() - 1, cBottomRight
, background
);
387 // Draw the box lines
388 hLineXY(1 + 1, 0, getWidth() - 4, cHSide
, background
);
389 hLineXY(1 + 1, getHeight() - 1, getWidth() - 4, cHSide
, background
);
392 drawBoxShadow(0, 0, getWidth(), getHeight());
395 // ------------------------------------------------------------------------
396 // TMenu ------------------------------------------------------------------
397 // ------------------------------------------------------------------------
400 * Set the menu title X position.
402 * @param titleX the position
404 public void setTitleX(final int titleX
) {
405 this.titleX
= titleX
;
409 * Get the menu title X position.
411 * @return the position
413 public int getTitleX() {
418 * Get the mnemonic string.
420 * @return the full mnemonic string
422 public MnemonicString
getMnemonic() {
427 * Convenience function to add a menu item.
429 * @param id menu item ID. Must be greater than 1024.
430 * @param label menu item label
431 * @return the new menu item
433 public TMenuItem
addItem(final int id
, final String label
) {
435 return addItemInternal(id
, label
, null);
439 * Convenience function to add a menu item.
441 * @param id menu item ID. Must be greater than 1024.
442 * @param label menu item label
443 * @param enabled default state for enabled
444 * @return the new menu item
446 public TMenuItem
addItem(final int id
, final String label
,
447 final boolean enabled
) {
450 return addItemInternal(id
, label
, null, enabled
);
454 * Convenience function to add a custom menu item.
456 * @param id menu item ID. Must be greater than 1024.
457 * @param label menu item label
458 * @param key global keyboard accelerator
459 * @return the new menu item
461 public TMenuItem
addItem(final int id
, final String label
,
462 final TKeypress key
) {
465 return addItemInternal(id
, label
, key
);
469 * Convenience function to add a custom menu item.
471 * @param id menu item ID. Must be greater than 1024.
472 * @param label menu item label
473 * @param key global keyboard accelerator
474 * @param enabled default state for enabled
475 * @return the new menu item
477 public TMenuItem
addItem(final int id
, final String label
,
478 final TKeypress key
, final boolean enabled
) {
480 TMenuItem item
= addItem(id
, label
, key
);
481 item
.setEnabled(enabled
);
486 * Convenience function to add a custom menu item.
488 * @param id menu item ID. Must be greater than 1024.
489 * @param label menu item label
490 * @param key global keyboard accelerator
491 * @return the new menu item
493 private TMenuItem
addItemInternal(final int id
, final String label
,
494 final TKeypress key
) {
496 return addItemInternal(id
, label
, key
, true);
500 * Convenience function to add a custom menu item.
502 * @param id menu item ID. Must be greater than 1024.
503 * @param label menu item label
504 * @param key global keyboard accelerator
505 * @param enabled default state for enabled
506 * @return the new menu item
508 private TMenuItem
addItemInternal(final int id
, final String label
,
509 final TKeypress key
, final boolean enabled
) {
511 int newY
= getChildren().size() + 1;
512 assert (newY
< getHeight());
514 TMenuItem menuItem
= new TMenuItem(this, id
, 1, newY
, label
);
515 menuItem
.setKey(key
);
516 menuItem
.setEnabled(enabled
);
517 setHeight(getHeight() + 1);
518 if (menuItem
.getWidth() + 2 > getWidth()) {
519 setWidth(menuItem
.getWidth() + 2);
521 for (TWidget widget
: getChildren()) {
522 widget
.setWidth(getWidth() - 2);
524 getApplication().addMenuItem(menuItem
);
525 getApplication().recomputeMenuX();
531 * Convenience function to add one of the default menu items.
533 * @param id menu item ID. Must be between 0 (inclusive) and 1023
535 * @return the new menu item
537 public TMenuItem
addDefaultItem(final int id
) {
538 return addDefaultItem(id
, true);
542 * Convenience function to add one of the default menu items.
544 * @param id menu item ID. Must be between 0 (inclusive) and 1023
546 * @param enabled default state for enabled
547 * @return the new menu item
549 public TMenuItem
addDefaultItem(final int id
, final boolean enabled
) {
554 TKeypress key
= null;
555 boolean checkable
= false;
556 boolean checked
= false;
561 label
= i18n
.getString("menuRepaintDesktop");
565 label
= i18n
.getString("menuViewImage");
568 case MID_SCREEN_OPTIONS
:
569 label
= i18n
.getString("menuScreenOptions");
573 label
= i18n
.getString("menuNew");
577 label
= i18n
.getString("menuExit");
582 label
= i18n
.getString("menuShell");
586 label
= i18n
.getString("menuOpen");
591 label
= i18n
.getString("menuCut");
595 label
= i18n
.getString("menuCopy");
599 label
= i18n
.getString("menuPaste");
603 label
= i18n
.getString("menuClear");
608 label
= i18n
.getString("menuFind");
611 label
= i18n
.getString("menuReplace");
613 case MID_SEARCH_AGAIN
:
614 label
= i18n
.getString("menuSearchAgain");
618 label
= i18n
.getString("menuGotoLine");
622 label
= i18n
.getString("menuWindowTile");
625 label
= i18n
.getString("menuWindowCascade");
628 label
= i18n
.getString("menuWindowCloseAll");
630 case MID_WINDOW_MOVE
:
631 label
= i18n
.getString("menuWindowMove");
634 case MID_WINDOW_ZOOM
:
635 label
= i18n
.getString("menuWindowZoom");
638 case MID_WINDOW_NEXT
:
639 label
= i18n
.getString("menuWindowNext");
642 case MID_WINDOW_PREVIOUS
:
643 label
= i18n
.getString("menuWindowPrevious");
646 case MID_WINDOW_CLOSE
:
647 label
= i18n
.getString("menuWindowClose");
651 case MID_HELP_CONTENTS
:
652 label
= i18n
.getString("menuHelpContents");
655 label
= i18n
.getString("menuHelpIndex");
658 case MID_HELP_SEARCH
:
659 label
= i18n
.getString("menuHelpSearch");
662 case MID_HELP_PREVIOUS
:
663 label
= i18n
.getString("menuHelpPrevious");
667 label
= i18n
.getString("menuHelpHelp");
669 case MID_HELP_ACTIVE_FILE
:
670 label
= i18n
.getString("menuHelpActive");
673 label
= i18n
.getString("menuHelpAbout");
676 case MID_TABLE_RENAME_COLUMN
:
677 label
= i18n
.getString("menuTableRenameColumn");
679 case MID_TABLE_RENAME_ROW
:
680 label
= i18n
.getString("menuTableRenameRow");
682 case MID_TABLE_VIEW_ROW_LABELS
:
683 label
= i18n
.getString("menuTableViewRowLabels");
687 case MID_TABLE_VIEW_COLUMN_LABELS
:
688 label
= i18n
.getString("menuTableViewColumnLabels");
692 case MID_TABLE_VIEW_HIGHLIGHT_ROW
:
693 label
= i18n
.getString("menuTableViewHighlightRow");
697 case MID_TABLE_VIEW_HIGHLIGHT_COLUMN
:
698 label
= i18n
.getString("menuTableViewHighlightColumn");
703 case MID_TABLE_BORDER_NONE
:
704 label
= i18n
.getString("menuTableBorderNone");
706 case MID_TABLE_BORDER_ALL
:
707 label
= i18n
.getString("menuTableBorderAll");
709 case MID_TABLE_BORDER_CELL_NONE
:
710 label
= i18n
.getString("menuTableBorderCellNone");
712 case MID_TABLE_BORDER_CELL_ALL
:
713 label
= i18n
.getString("menuTableBorderCellAll");
715 case MID_TABLE_BORDER_RIGHT
:
716 label
= i18n
.getString("menuTableBorderRight");
718 case MID_TABLE_BORDER_LEFT
:
719 label
= i18n
.getString("menuTableBorderLeft");
721 case MID_TABLE_BORDER_TOP
:
722 label
= i18n
.getString("menuTableBorderTop");
724 case MID_TABLE_BORDER_BOTTOM
:
725 label
= i18n
.getString("menuTableBorderBottom");
727 case MID_TABLE_BORDER_DOUBLE_BOTTOM
:
728 label
= i18n
.getString("menuTableBorderDoubleBottom");
730 case MID_TABLE_BORDER_THICK_BOTTOM
:
731 label
= i18n
.getString("menuTableBorderThickBottom");
733 case MID_TABLE_DELETE_LEFT
:
734 label
= i18n
.getString("menuTableDeleteLeft");
736 case MID_TABLE_DELETE_UP
:
737 label
= i18n
.getString("menuTableDeleteUp");
739 case MID_TABLE_DELETE_ROW
:
740 label
= i18n
.getString("menuTableDeleteRow");
742 case MID_TABLE_DELETE_COLUMN
:
743 label
= i18n
.getString("menuTableDeleteColumn");
745 case MID_TABLE_INSERT_LEFT
:
746 label
= i18n
.getString("menuTableInsertLeft");
748 case MID_TABLE_INSERT_RIGHT
:
749 label
= i18n
.getString("menuTableInsertRight");
751 case MID_TABLE_INSERT_ABOVE
:
752 label
= i18n
.getString("menuTableInsertAbove");
754 case MID_TABLE_INSERT_BELOW
:
755 label
= i18n
.getString("menuTableInsertBelow");
757 case MID_TABLE_COLUMN_NARROW
:
758 label
= i18n
.getString("menuTableColumnNarrow");
761 case MID_TABLE_COLUMN_WIDEN
:
762 label
= i18n
.getString("menuTableColumnWiden");
765 case MID_TABLE_FILE_OPEN_CSV
:
766 label
= i18n
.getString("menuTableFileOpenCsv");
768 case MID_TABLE_FILE_SAVE_CSV
:
769 label
= i18n
.getString("menuTableFileSaveCsv");
771 case MID_TABLE_FILE_SAVE_TEXT
:
772 label
= i18n
.getString("menuTableFileSaveText");
776 throw new IllegalArgumentException("Invalid menu ID: " + id
);
779 TMenuItem item
= addItemInternal(id
, label
, key
, enabled
);
780 item
.setCheckable(checkable
);
785 * Convenience function to add a menu separator.
787 public void addSeparator() {
788 int newY
= getChildren().size() + 1;
789 assert (newY
< getHeight());
791 // We just have to construct it, don't need to hang onto what it
793 new TMenuSeparator(this, 1, newY
);
794 setHeight(getHeight() + 1);
798 * Convenience function to add a sub-menu.
800 * @param title menu title. Title must contain a keyboard shortcut,
801 * denoted by prefixing a letter with "&", e.g. "&File"
802 * @return the new sub-menu
804 public TSubMenu
addSubMenu(final String title
) {
805 int newY
= getChildren().size() + 1;
806 assert (newY
< getHeight());
808 TSubMenu subMenu
= new TSubMenu(this, title
, 1, newY
);
809 setHeight(getHeight() + 1);
810 if (subMenu
.getWidth() + 2 > getWidth()) {
811 setWidth(subMenu
.getWidth() + 2);
813 for (TWidget widget
: getChildren()) {
814 widget
.setWidth(getWidth() - 2);
816 getApplication().recomputeMenuX();
818 subMenu
.menu
.setX(getX() + getWidth() - 2);