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_UNDO
= 20;
76 public static final int MID_REDO
= 21;
77 public static final int MID_CUT
= 22;
78 public static final int MID_COPY
= 23;
79 public static final int MID_PASTE
= 24;
80 public static final int MID_CLEAR
= 25;
83 public static final int MID_FIND
= 30;
84 public static final int MID_REPLACE
= 31;
85 public static final int MID_SEARCH_AGAIN
= 32;
86 public static final int MID_GOTO_LINE
= 33;
89 public static final int MID_TILE
= 40;
90 public static final int MID_CASCADE
= 41;
91 public static final int MID_CLOSE_ALL
= 42;
92 public static final int MID_WINDOW_MOVE
= 43;
93 public static final int MID_WINDOW_ZOOM
= 44;
94 public static final int MID_WINDOW_NEXT
= 45;
95 public static final int MID_WINDOW_PREVIOUS
= 46;
96 public static final int MID_WINDOW_CLOSE
= 47;
99 public static final int MID_HELP_CONTENTS
= 50;
100 public static final int MID_HELP_INDEX
= 51;
101 public static final int MID_HELP_SEARCH
= 52;
102 public static final int MID_HELP_PREVIOUS
= 53;
103 public static final int MID_HELP_HELP
= 54;
104 public static final int MID_HELP_ACTIVE_FILE
= 55;
105 public static final int MID_ABOUT
= 56;
108 public static final int MID_TABLE_RENAME_ROW
= 60;
109 public static final int MID_TABLE_RENAME_COLUMN
= 61;
110 public static final int MID_TABLE_VIEW_ROW_LABELS
= 70;
111 public static final int MID_TABLE_VIEW_COLUMN_LABELS
= 71;
112 public static final int MID_TABLE_VIEW_HIGHLIGHT_ROW
= 72;
113 public static final int MID_TABLE_VIEW_HIGHLIGHT_COLUMN
= 73;
114 public static final int MID_TABLE_BORDER_NONE
= 80;
115 public static final int MID_TABLE_BORDER_ALL
= 81;
116 public static final int MID_TABLE_BORDER_CELL_NONE
= 82;
117 public static final int MID_TABLE_BORDER_CELL_ALL
= 83;
118 public static final int MID_TABLE_BORDER_RIGHT
= 84;
119 public static final int MID_TABLE_BORDER_LEFT
= 85;
120 public static final int MID_TABLE_BORDER_TOP
= 86;
121 public static final int MID_TABLE_BORDER_BOTTOM
= 87;
122 public static final int MID_TABLE_BORDER_DOUBLE_BOTTOM
= 88;
123 public static final int MID_TABLE_BORDER_THICK_BOTTOM
= 89;
124 public static final int MID_TABLE_DELETE_LEFT
= 100;
125 public static final int MID_TABLE_DELETE_UP
= 101;
126 public static final int MID_TABLE_DELETE_ROW
= 102;
127 public static final int MID_TABLE_DELETE_COLUMN
= 103;
128 public static final int MID_TABLE_INSERT_LEFT
= 104;
129 public static final int MID_TABLE_INSERT_RIGHT
= 105;
130 public static final int MID_TABLE_INSERT_ABOVE
= 106;
131 public static final int MID_TABLE_INSERT_BELOW
= 107;
132 public static final int MID_TABLE_COLUMN_NARROW
= 110;
133 public static final int MID_TABLE_COLUMN_WIDEN
= 111;
134 public static final int MID_TABLE_FILE_OPEN_CSV
= 115;
135 public static final int MID_TABLE_FILE_SAVE_CSV
= 116;
136 public static final int MID_TABLE_FILE_SAVE_TEXT
= 117;
138 // ------------------------------------------------------------------------
139 // Variables --------------------------------------------------------------
140 // ------------------------------------------------------------------------
143 * If true, this is a sub-menu. Note package private access.
145 boolean isSubMenu
= false;
148 * The X position of the menu's title.
153 * The shortcut and title.
155 private MnemonicString mnemonic
;
158 * If true, draw icons with menu items. Note package private access.
160 boolean useIcons
= false;
162 // ------------------------------------------------------------------------
163 // Constructors -----------------------------------------------------------
164 // ------------------------------------------------------------------------
167 * Public constructor.
169 * @param parent parent application
170 * @param x column relative to parent
171 * @param y row relative to parent
172 * @param label mnemonic menu title. Label must contain a keyboard
173 * shortcut (mnemonic), denoted by prefixing a letter with "&",
176 public TMenu(final TApplication parent
, final int x
, final int y
,
177 final String label
) {
179 super(parent
, label
, x
, y
, parent
.getScreen().getWidth(),
180 parent
.getScreen().getHeight());
182 // Setup the menu shortcut
183 mnemonic
= new MnemonicString(label
);
184 setTitle(mnemonic
.getRawLabel());
185 assert (mnemonic
.getShortcutIdx() >= 0);
187 // Recompute width and height to reflect an empty menu
188 setWidth(StringUtils
.width(getTitle()) + 4);
193 if (System
.getProperty("jexer.menuIcons", "false").equals("true")) {
199 // ------------------------------------------------------------------------
200 // Event handlers ---------------------------------------------------------
201 // ------------------------------------------------------------------------
204 * Handle mouse button presses.
206 * @param mouse mouse button event
209 public void onMouseDown(final TMouseEvent mouse
) {
211 super.onMouseDown(mouse
);
214 for (TWidget widget
: getChildren()) {
215 if (widget
.mouseWouldHit(mouse
)) {
216 // Dispatch to this child, also activate it
219 // Set x and y relative to the child's coordinates
220 mouse
.setX(mouse
.getAbsoluteX() - widget
.getAbsoluteX());
221 mouse
.setY(mouse
.getAbsoluteY() - widget
.getAbsoluteY());
222 widget
.handleEvent(mouse
);
229 * Handle mouse button releases.
231 * @param mouse mouse button release event
234 public void onMouseUp(final TMouseEvent mouse
) {
238 for (TWidget widget
: getChildren()) {
239 if (widget
.mouseWouldHit(mouse
)) {
240 // Dispatch to this child, also activate it
243 // Set x and y relative to the child's coordinates
244 mouse
.setX(mouse
.getAbsoluteX() - widget
.getAbsoluteX());
245 mouse
.setY(mouse
.getAbsoluteY() - widget
.getAbsoluteY());
246 widget
.handleEvent(mouse
);
253 * Handle mouse movements.
255 * @param mouse mouse motion event
258 public void onMouseMotion(final TMouseEvent mouse
) {
261 // See if we should activate a different menu item
262 for (TWidget widget
: getChildren()) {
263 if ((mouse
.isMouse1())
264 && (widget
.mouseWouldHit(mouse
))
266 // Activate this menu item
268 if (widget
instanceof TSubMenu
) {
269 ((TSubMenu
) widget
).dispatch();
279 * @param keypress keystroke event
282 public void onKeypress(final TKeypressEvent keypress
) {
285 System.err.printf("keypress: %s active child: %s\n", keypress,
289 if (getActiveChild() != this) {
290 if (getActiveChild() instanceof TMenu
) {
291 getActiveChild().onKeypress(keypress
);
295 if (getActiveChild() instanceof TSubMenu
) {
296 TSubMenu subMenu
= (TSubMenu
) getActiveChild();
297 if (subMenu
.menu
.isActive()) {
298 subMenu
.onKeypress(keypress
);
304 if (keypress
.equals(kbEsc
)) {
305 getApplication().closeMenu();
308 if (keypress
.equals(kbDown
)) {
312 if (keypress
.equals(kbUp
)) {
316 if (keypress
.equals(kbRight
)) {
317 getApplication().switchMenu(true);
320 if (keypress
.equals(kbLeft
)) {
322 getApplication().closeSubMenu();
324 getApplication().switchMenu(false);
329 // Switch to a menuItem if it has an mnemonic
330 if (!keypress
.getKey().isFnKey()
331 && !keypress
.getKey().isAlt()
332 && !keypress
.getKey().isCtrl()) {
334 // System.err.println("Checking children for mnemonic...");
336 for (TWidget widget
: getChildren()) {
337 TMenuItem item
= (TMenuItem
) widget
;
338 if ((item
.isEnabled() == true)
339 && (item
.getMnemonic() != null)
340 && (Character
.toLowerCase(item
.getMnemonic().getShortcut())
341 == Character
.toLowerCase(keypress
.getKey().getChar()))
343 // System.err.println("activate: " + item);
345 // Send an enter keystroke to it
347 item
.handleEvent(new TKeypressEvent(kbEnter
));
353 // Dispatch the keypress to an active widget
354 for (TWidget widget
: getChildren()) {
355 if (widget
.isActive()) {
356 widget
.handleEvent(keypress
);
362 // ------------------------------------------------------------------------
363 // TWindow ----------------------------------------------------------------
364 // ------------------------------------------------------------------------
367 * Draw a top-level menu with title and menu items.
371 CellAttributes background
= getTheme().getColor("tmenu");
373 assert (isAbsoluteActive());
375 // Fill in the interior background
376 for (int i
= 0; i
< getHeight(); i
++) {
377 hLineXY(0, i
, getWidth(), ' ', background
);
387 cTopLeft
= GraphicsChars
.ULCORNER
;
388 cTopRight
= GraphicsChars
.URCORNER
;
389 cBottomLeft
= GraphicsChars
.LLCORNER
;
390 cBottomRight
= GraphicsChars
.LRCORNER
;
391 cHSide
= GraphicsChars
.SINGLE_BAR
;
393 // Place the corner characters
394 putCharXY(1, 0, cTopLeft
, background
);
395 putCharXY(getWidth() - 2, 0, cTopRight
, background
);
396 putCharXY(1, getHeight() - 1, cBottomLeft
, background
);
397 putCharXY(getWidth() - 2, getHeight() - 1, cBottomRight
, background
);
399 // Draw the box lines
400 hLineXY(1 + 1, 0, getWidth() - 4, cHSide
, background
);
401 hLineXY(1 + 1, getHeight() - 1, getWidth() - 4, cHSide
, background
);
404 drawBoxShadow(0, 0, getWidth(), getHeight());
407 // ------------------------------------------------------------------------
408 // TMenu ------------------------------------------------------------------
409 // ------------------------------------------------------------------------
412 * Set the menu title X position.
414 * @param titleX the position
416 public void setTitleX(final int titleX
) {
417 this.titleX
= titleX
;
421 * Get the menu title X position.
423 * @return the position
425 public int getTitleX() {
430 * Get the mnemonic string.
432 * @return the full mnemonic string
434 public MnemonicString
getMnemonic() {
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 * @return the new menu item
445 public TMenuItem
addItem(final int id
, final String label
) {
446 return addItemInternal(id
, label
, null);
450 * Convenience function to add a menu item.
452 * @param id menu item ID. Must be greater than 1024.
453 * @param label menu item label
454 * @param enabled default state for enabled
455 * @return the new menu item
457 public TMenuItem
addItem(final int id
, final String label
,
458 final boolean enabled
) {
461 return addItemInternal(id
, label
, null, enabled
, -1);
465 * Convenience function to add a custom menu item.
467 * @param id menu item ID. Must be greater than 1024.
468 * @param label menu item label
469 * @param key global keyboard accelerator
470 * @return the new menu item
472 public TMenuItem
addItem(final int id
, final String label
,
473 final TKeypress key
) {
476 return addItemInternal(id
, label
, key
);
480 * Convenience function to add a custom menu item.
482 * @param id menu item ID. Must be greater than 1024.
483 * @param label menu item label
484 * @param key global keyboard accelerator
485 * @param enabled default state for enabled
486 * @return the new menu item
488 public TMenuItem
addItem(final int id
, final String label
,
489 final TKeypress key
, final boolean enabled
) {
491 TMenuItem item
= addItem(id
, label
, key
);
492 item
.setEnabled(enabled
);
497 * Convenience function to add a custom menu item.
499 * @param id menu item ID. Must be greater than 1024.
500 * @param label menu item label
501 * @param key global keyboard accelerator
502 * @return the new menu item
504 private TMenuItem
addItemInternal(final int id
, final String label
,
505 final TKeypress key
) {
507 return addItemInternal(id
, label
, key
, true, -1);
511 * Convenience function to add a custom menu item.
513 * @param id menu item ID. Must be greater than 1024.
514 * @param label menu item label
515 * @param key global keyboard accelerator
516 * @param enabled default state for enabled
517 * @param icon icon picture/emoji
518 * @return the new menu item
520 private TMenuItem
addItemInternal(final int id
, final String label
,
521 final TKeypress key
, final boolean enabled
, final int icon
) {
523 int newY
= getChildren().size() + 1;
524 assert (newY
< getHeight());
526 TMenuItem menuItem
= new TMenuItem(this, id
, 1, newY
, label
, icon
);
527 menuItem
.setKey(key
);
528 menuItem
.setEnabled(enabled
);
529 setHeight(getHeight() + 1);
530 if (menuItem
.getWidth() + 2 > getWidth()) {
531 setWidth(menuItem
.getWidth() + 2);
533 for (TWidget widget
: getChildren()) {
534 widget
.setWidth(getWidth() - 2);
536 getApplication().addMenuItem(menuItem
);
537 getApplication().recomputeMenuX();
543 * Convenience function to add one of the default menu items.
545 * @param id menu item ID. Must be between 0 (inclusive) and 1023
547 * @return the new menu item
549 public TMenuItem
addDefaultItem(final int id
) {
550 return addDefaultItem(id
, true);
554 * Convenience function to add one of the default menu items.
556 * @param id menu item ID. Must be between 0 (inclusive) and 1023
558 * @param enabled default state for enabled
559 * @return the new menu item
561 public TMenuItem
addDefaultItem(final int id
, final boolean enabled
) {
566 TKeypress key
= null;
568 boolean checkable
= false;
569 boolean checked
= false;
574 label
= i18n
.getString("menuRepaintDesktop");
579 label
= i18n
.getString("menuViewImage");
582 case MID_SCREEN_OPTIONS
:
583 label
= i18n
.getString("menuScreenOptions");
587 label
= i18n
.getString("menuNew");
592 label
= i18n
.getString("menuExit");
598 label
= i18n
.getString("menuShell");
603 label
= i18n
.getString("menuOpen");
609 label
= i18n
.getString("menuUndo");
613 label
= i18n
.getString("menuRedo");
617 label
= i18n
.getString("menuCut");
622 label
= i18n
.getString("menuCopy");
627 label
= i18n
.getString("menuPaste");
632 label
= i18n
.getString("menuClear");
636 label
= i18n
.getString("menuFind");
640 label
= i18n
.getString("menuReplace");
642 case MID_SEARCH_AGAIN
:
643 label
= i18n
.getString("menuSearchAgain");
647 label
= i18n
.getString("menuGotoLine");
651 label
= i18n
.getString("menuWindowTile");
654 label
= i18n
.getString("menuWindowCascade");
658 label
= i18n
.getString("menuWindowCloseAll");
660 case MID_WINDOW_MOVE
:
661 label
= i18n
.getString("menuWindowMove");
665 case MID_WINDOW_ZOOM
:
666 label
= i18n
.getString("menuWindowZoom");
670 case MID_WINDOW_NEXT
:
671 label
= i18n
.getString("menuWindowNext");
675 case MID_WINDOW_PREVIOUS
:
676 label
= i18n
.getString("menuWindowPrevious");
680 case MID_WINDOW_CLOSE
:
681 label
= i18n
.getString("menuWindowClose");
685 case MID_HELP_CONTENTS
:
686 label
= i18n
.getString("menuHelpContents");
689 label
= i18n
.getString("menuHelpIndex");
692 case MID_HELP_SEARCH
:
693 label
= i18n
.getString("menuHelpSearch");
696 case MID_HELP_PREVIOUS
:
697 label
= i18n
.getString("menuHelpPrevious");
701 label
= i18n
.getString("menuHelpHelp");
703 case MID_HELP_ACTIVE_FILE
:
704 label
= i18n
.getString("menuHelpActive");
707 label
= i18n
.getString("menuHelpAbout");
710 case MID_TABLE_RENAME_COLUMN
:
711 label
= i18n
.getString("menuTableRenameColumn");
713 case MID_TABLE_RENAME_ROW
:
714 label
= i18n
.getString("menuTableRenameRow");
716 case MID_TABLE_VIEW_ROW_LABELS
:
717 label
= i18n
.getString("menuTableViewRowLabels");
721 case MID_TABLE_VIEW_COLUMN_LABELS
:
722 label
= i18n
.getString("menuTableViewColumnLabels");
726 case MID_TABLE_VIEW_HIGHLIGHT_ROW
:
727 label
= i18n
.getString("menuTableViewHighlightRow");
731 case MID_TABLE_VIEW_HIGHLIGHT_COLUMN
:
732 label
= i18n
.getString("menuTableViewHighlightColumn");
737 case MID_TABLE_BORDER_NONE
:
738 label
= i18n
.getString("menuTableBorderNone");
740 case MID_TABLE_BORDER_ALL
:
741 label
= i18n
.getString("menuTableBorderAll");
743 case MID_TABLE_BORDER_CELL_NONE
:
744 label
= i18n
.getString("menuTableBorderCellNone");
746 case MID_TABLE_BORDER_CELL_ALL
:
747 label
= i18n
.getString("menuTableBorderCellAll");
749 case MID_TABLE_BORDER_RIGHT
:
750 label
= i18n
.getString("menuTableBorderRight");
752 case MID_TABLE_BORDER_LEFT
:
753 label
= i18n
.getString("menuTableBorderLeft");
755 case MID_TABLE_BORDER_TOP
:
756 label
= i18n
.getString("menuTableBorderTop");
758 case MID_TABLE_BORDER_BOTTOM
:
759 label
= i18n
.getString("menuTableBorderBottom");
761 case MID_TABLE_BORDER_DOUBLE_BOTTOM
:
762 label
= i18n
.getString("menuTableBorderDoubleBottom");
764 case MID_TABLE_BORDER_THICK_BOTTOM
:
765 label
= i18n
.getString("menuTableBorderThickBottom");
767 case MID_TABLE_DELETE_LEFT
:
768 label
= i18n
.getString("menuTableDeleteLeft");
770 case MID_TABLE_DELETE_UP
:
771 label
= i18n
.getString("menuTableDeleteUp");
773 case MID_TABLE_DELETE_ROW
:
774 label
= i18n
.getString("menuTableDeleteRow");
776 case MID_TABLE_DELETE_COLUMN
:
777 label
= i18n
.getString("menuTableDeleteColumn");
779 case MID_TABLE_INSERT_LEFT
:
780 label
= i18n
.getString("menuTableInsertLeft");
782 case MID_TABLE_INSERT_RIGHT
:
783 label
= i18n
.getString("menuTableInsertRight");
785 case MID_TABLE_INSERT_ABOVE
:
786 label
= i18n
.getString("menuTableInsertAbove");
788 case MID_TABLE_INSERT_BELOW
:
789 label
= i18n
.getString("menuTableInsertBelow");
791 case MID_TABLE_COLUMN_NARROW
:
792 label
= i18n
.getString("menuTableColumnNarrow");
795 case MID_TABLE_COLUMN_WIDEN
:
796 label
= i18n
.getString("menuTableColumnWiden");
799 case MID_TABLE_FILE_OPEN_CSV
:
800 label
= i18n
.getString("menuTableFileOpenCsv");
802 case MID_TABLE_FILE_SAVE_CSV
:
803 label
= i18n
.getString("menuTableFileSaveCsv");
805 case MID_TABLE_FILE_SAVE_TEXT
:
806 label
= i18n
.getString("menuTableFileSaveText");
810 throw new IllegalArgumentException("Invalid menu ID: " + id
);
813 TMenuItem item
= addItemInternal(id
, label
, key
, enabled
, icon
);
814 item
.setCheckable(checkable
);
819 * Convenience function to add a menu separator.
821 public void addSeparator() {
822 int newY
= getChildren().size() + 1;
823 assert (newY
< getHeight());
825 // We just have to construct it, don't need to hang onto what it
827 new TMenuSeparator(this, 1, newY
);
828 setHeight(getHeight() + 1);
832 * Convenience function to add a sub-menu.
834 * @param title menu title. Title must contain a keyboard shortcut,
835 * denoted by prefixing a letter with "&", e.g. "&File"
836 * @return the new sub-menu
838 public TSubMenu
addSubMenu(final String title
) {
839 int newY
= getChildren().size() + 1;
840 assert (newY
< getHeight());
842 TSubMenu subMenu
= new TSubMenu(this, title
, 1, newY
);
843 setHeight(getHeight() + 1);
844 if (subMenu
.getWidth() + 2 > getWidth()) {
845 setWidth(subMenu
.getWidth() + 2);
847 for (TWidget widget
: getChildren()) {
848 widget
.setWidth(getWidth() - 2);
850 getApplication().recomputeMenuX();
852 subMenu
.menu
.setX(getX() + getWidth() - 2);