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 TSubMenu
)
277 || (getActiveChild() instanceof TMenu
)
279 getActiveChild().onKeypress(keypress
);
284 if (keypress
.equals(kbEsc
)) {
285 getApplication().closeMenu();
288 if (keypress
.equals(kbDown
)) {
292 if (keypress
.equals(kbUp
)) {
296 if (keypress
.equals(kbRight
)) {
297 getApplication().switchMenu(true);
300 if (keypress
.equals(kbLeft
)) {
302 getApplication().closeSubMenu();
304 getApplication().switchMenu(false);
309 // Switch to a menuItem if it has an mnemonic
310 if (!keypress
.getKey().isFnKey()
311 && !keypress
.getKey().isAlt()
312 && !keypress
.getKey().isCtrl()) {
313 for (TWidget widget
: getChildren()) {
314 TMenuItem item
= (TMenuItem
) widget
;
315 if ((item
.getMnemonic() != null)
316 && (Character
.toLowerCase(item
.getMnemonic().getShortcut())
317 == Character
.toLowerCase(keypress
.getKey().getChar()))
319 // Send an enter keystroke to it
321 item
.handleEvent(new TKeypressEvent(kbEnter
));
327 // Dispatch the keypress to an active widget
328 for (TWidget widget
: getChildren()) {
329 if (widget
.isActive()) {
330 widget
.handleEvent(keypress
);
336 // ------------------------------------------------------------------------
337 // TWindow ----------------------------------------------------------------
338 // ------------------------------------------------------------------------
341 * Draw a top-level menu with title and menu items.
345 CellAttributes background
= getTheme().getColor("tmenu");
347 assert (isAbsoluteActive());
349 // Fill in the interior background
350 for (int i
= 0; i
< getHeight(); i
++) {
351 hLineXY(0, i
, getWidth(), ' ', background
);
361 cTopLeft
= GraphicsChars
.ULCORNER
;
362 cTopRight
= GraphicsChars
.URCORNER
;
363 cBottomLeft
= GraphicsChars
.LLCORNER
;
364 cBottomRight
= GraphicsChars
.LRCORNER
;
365 cHSide
= GraphicsChars
.SINGLE_BAR
;
367 // Place the corner characters
368 putCharXY(1, 0, cTopLeft
, background
);
369 putCharXY(getWidth() - 2, 0, cTopRight
, background
);
370 putCharXY(1, getHeight() - 1, cBottomLeft
, background
);
371 putCharXY(getWidth() - 2, getHeight() - 1, cBottomRight
, background
);
373 // Draw the box lines
374 hLineXY(1 + 1, 0, getWidth() - 4, cHSide
, background
);
375 hLineXY(1 + 1, getHeight() - 1, getWidth() - 4, cHSide
, background
);
378 drawBoxShadow(0, 0, getWidth(), getHeight());
381 // ------------------------------------------------------------------------
382 // TMenu ------------------------------------------------------------------
383 // ------------------------------------------------------------------------
386 * Set the menu title X position.
388 * @param titleX the position
390 public void setTitleX(final int titleX
) {
391 this.titleX
= titleX
;
395 * Get the menu title X position.
397 * @return the position
399 public int getTitleX() {
404 * Get the mnemonic string.
406 * @return the full mnemonic string
408 public MnemonicString
getMnemonic() {
413 * Convenience function to add a menu item.
415 * @param id menu item ID. Must be greater than 1024.
416 * @param label menu item label
417 * @return the new menu item
419 public TMenuItem
addItem(final int id
, final String label
) {
421 return addItemInternal(id
, label
, null);
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 * @param enabled default state for enabled
430 * @return the new menu item
432 public TMenuItem
addItem(final int id
, final String label
,
433 final boolean enabled
) {
436 return addItemInternal(id
, label
, null, enabled
);
440 * Convenience function to add a custom menu item.
442 * @param id menu item ID. Must be greater than 1024.
443 * @param label menu item label
444 * @param key global keyboard accelerator
445 * @return the new menu item
447 public TMenuItem
addItem(final int id
, final String label
,
448 final TKeypress key
) {
451 return addItemInternal(id
, label
, key
);
455 * Convenience function to add a custom menu item.
457 * @param id menu item ID. Must be greater than 1024.
458 * @param label menu item label
459 * @param key global keyboard accelerator
460 * @param enabled default state for enabled
461 * @return the new menu item
463 public TMenuItem
addItem(final int id
, final String label
,
464 final TKeypress key
, final boolean enabled
) {
466 TMenuItem item
= addItem(id
, label
, key
);
467 item
.setEnabled(enabled
);
472 * Convenience function to add a custom menu item.
474 * @param id menu item ID. Must be greater than 1024.
475 * @param label menu item label
476 * @param key global keyboard accelerator
477 * @return the new menu item
479 private TMenuItem
addItemInternal(final int id
, final String label
,
480 final TKeypress key
) {
482 return addItemInternal(id
, label
, key
, true);
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 * @param enabled default state for enabled
492 * @return the new menu item
494 private TMenuItem
addItemInternal(final int id
, final String label
,
495 final TKeypress key
, final boolean enabled
) {
497 int newY
= getChildren().size() + 1;
498 assert (newY
< getHeight());
500 TMenuItem menuItem
= new TMenuItem(this, id
, 1, newY
, label
);
501 menuItem
.setKey(key
);
502 menuItem
.setEnabled(enabled
);
503 setHeight(getHeight() + 1);
504 if (menuItem
.getWidth() + 2 > getWidth()) {
505 setWidth(menuItem
.getWidth() + 2);
507 for (TWidget widget
: getChildren()) {
508 widget
.setWidth(getWidth() - 2);
510 getApplication().addMenuItem(menuItem
);
511 getApplication().recomputeMenuX();
517 * Convenience function to add one of the default menu items.
519 * @param id menu item ID. Must be between 0 (inclusive) and 1023
521 * @return the new menu item
523 public TMenuItem
addDefaultItem(final int id
) {
524 return addDefaultItem(id
, true);
528 * Convenience function to add one of the default menu items.
530 * @param id menu item ID. Must be between 0 (inclusive) and 1023
532 * @param enabled default state for enabled
533 * @return the new menu item
535 public TMenuItem
addDefaultItem(final int id
, final boolean enabled
) {
540 TKeypress key
= null;
541 boolean checkable
= false;
542 boolean checked
= false;
547 label
= i18n
.getString("menuRepaintDesktop");
551 label
= i18n
.getString("menuViewImage");
554 case MID_CHANGE_FONT
:
555 label
= i18n
.getString("menuChangeFont");
559 label
= i18n
.getString("menuNew");
563 label
= i18n
.getString("menuExit");
568 label
= i18n
.getString("menuShell");
572 label
= i18n
.getString("menuOpen");
577 label
= i18n
.getString("menuCut");
581 label
= i18n
.getString("menuCopy");
585 label
= i18n
.getString("menuPaste");
589 label
= i18n
.getString("menuClear");
594 label
= i18n
.getString("menuFind");
597 label
= i18n
.getString("menuReplace");
599 case MID_SEARCH_AGAIN
:
600 label
= i18n
.getString("menuSearchAgain");
604 label
= i18n
.getString("menuGotoLine");
608 label
= i18n
.getString("menuWindowTile");
611 label
= i18n
.getString("menuWindowCascade");
614 label
= i18n
.getString("menuWindowCloseAll");
616 case MID_WINDOW_MOVE
:
617 label
= i18n
.getString("menuWindowMove");
620 case MID_WINDOW_ZOOM
:
621 label
= i18n
.getString("menuWindowZoom");
624 case MID_WINDOW_NEXT
:
625 label
= i18n
.getString("menuWindowNext");
628 case MID_WINDOW_PREVIOUS
:
629 label
= i18n
.getString("menuWindowPrevious");
632 case MID_WINDOW_CLOSE
:
633 label
= i18n
.getString("menuWindowClose");
637 case MID_HELP_CONTENTS
:
638 label
= i18n
.getString("menuHelpContents");
641 label
= i18n
.getString("menuHelpIndex");
644 case MID_HELP_SEARCH
:
645 label
= i18n
.getString("menuHelpSearch");
648 case MID_HELP_PREVIOUS
:
649 label
= i18n
.getString("menuHelpPrevious");
653 label
= i18n
.getString("menuHelpHelp");
655 case MID_HELP_ACTIVE_FILE
:
656 label
= i18n
.getString("menuHelpActive");
659 label
= i18n
.getString("menuHelpAbout");
662 case MID_TABLE_VIEW_ROW_LABELS
:
663 label
= i18n
.getString("menuTableViewRowLabels");
667 case MID_TABLE_VIEW_COLUMN_LABELS
:
668 label
= i18n
.getString("menuTableViewColumnLabels");
672 case MID_TABLE_VIEW_HIGHLIGHT_ROW
:
673 label
= i18n
.getString("menuTableViewHighlightRow");
677 case MID_TABLE_VIEW_HIGHLIGHT_COLUMN
:
678 label
= i18n
.getString("menuTableViewHighlightColumn");
683 case MID_TABLE_BORDER_NONE
:
684 label
= i18n
.getString("menuTableBorderNone");
686 case MID_TABLE_BORDER_ALL
:
687 label
= i18n
.getString("menuTableBorderAll");
689 case MID_TABLE_BORDER_CELL_NONE
:
690 label
= i18n
.getString("menuTableBorderCellNone");
692 case MID_TABLE_BORDER_CELL_ALL
:
693 label
= i18n
.getString("menuTableBorderCellAll");
695 case MID_TABLE_BORDER_RIGHT
:
696 label
= i18n
.getString("menuTableBorderRight");
698 case MID_TABLE_BORDER_LEFT
:
699 label
= i18n
.getString("menuTableBorderLeft");
701 case MID_TABLE_BORDER_TOP
:
702 label
= i18n
.getString("menuTableBorderTop");
704 case MID_TABLE_BORDER_BOTTOM
:
705 label
= i18n
.getString("menuTableBorderBottom");
707 case MID_TABLE_BORDER_DOUBLE_BOTTOM
:
708 label
= i18n
.getString("menuTableBorderDoubleBottom");
710 case MID_TABLE_BORDER_THICK_BOTTOM
:
711 label
= i18n
.getString("menuTableBorderThickBottom");
713 case MID_TABLE_DELETE_LEFT
:
714 label
= i18n
.getString("menuTableDeleteLeft");
716 case MID_TABLE_DELETE_UP
:
717 label
= i18n
.getString("menuTableDeleteUp");
719 case MID_TABLE_DELETE_ROW
:
720 label
= i18n
.getString("menuTableDeleteRow");
722 case MID_TABLE_DELETE_COLUMN
:
723 label
= i18n
.getString("menuTableDeleteColumn");
725 case MID_TABLE_INSERT_LEFT
:
726 label
= i18n
.getString("menuTableInsertLeft");
728 case MID_TABLE_INSERT_RIGHT
:
729 label
= i18n
.getString("menuTableInsertRight");
731 case MID_TABLE_INSERT_ABOVE
:
732 label
= i18n
.getString("menuTableInsertAbove");
734 case MID_TABLE_INSERT_BELOW
:
735 label
= i18n
.getString("menuTableInsertBelow");
737 case MID_TABLE_COLUMN_NARROW
:
738 label
= i18n
.getString("menuTableColumnNarrow");
741 case MID_TABLE_COLUMN_WIDEN
:
742 label
= i18n
.getString("menuTableColumnWiden");
745 case MID_TABLE_FILE_SAVE_CSV
:
746 label
= i18n
.getString("menuTableFileSaveCsv");
748 case MID_TABLE_FILE_SAVE_TEXT
:
749 label
= i18n
.getString("menuTableFileSaveText");
753 throw new IllegalArgumentException("Invalid menu ID: " + id
);
756 TMenuItem item
= addItemInternal(id
, label
, key
, enabled
);
757 item
.setCheckable(checkable
);
762 * Convenience function to add a menu separator.
764 public void addSeparator() {
765 int newY
= getChildren().size() + 1;
766 assert (newY
< getHeight());
768 // We just have to construct it, don't need to hang onto what it
770 new TMenuSeparator(this, 1, newY
);
771 setHeight(getHeight() + 1);
775 * Convenience function to add a sub-menu.
777 * @param title menu title. Title must contain a keyboard shortcut,
778 * denoted by prefixing a letter with "&", e.g. "&File"
779 * @return the new sub-menu
781 public TSubMenu
addSubMenu(final String title
) {
782 int newY
= getChildren().size() + 1;
783 assert (newY
< getHeight());
785 TSubMenu subMenu
= new TSubMenu(this, title
, 1, newY
);
786 setHeight(getHeight() + 1);
787 if (subMenu
.getWidth() + 2 > getWidth()) {
788 setWidth(subMenu
.getWidth() + 2);
790 for (TWidget widget
: getChildren()) {
791 widget
.setWidth(getWidth() - 2);
793 getApplication().recomputeMenuX();
795 subMenu
.menu
.setX(getX() + getWidth() - 2);