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_EXIT
= 1;
63 public static final int MID_QUIT
= MID_EXIT
;
64 public static final int MID_OPEN_FILE
= 2;
65 public static final int MID_SHELL
= 3;
68 public static final int MID_CUT
= 10;
69 public static final int MID_COPY
= 11;
70 public static final int MID_PASTE
= 12;
71 public static final int MID_CLEAR
= 13;
74 public static final int MID_FIND
= 20;
75 public static final int MID_REPLACE
= 21;
76 public static final int MID_SEARCH_AGAIN
= 22;
77 public static final int MID_GOTO_LINE
= 23;
80 public static final int MID_TILE
= 30;
81 public static final int MID_CASCADE
= 31;
82 public static final int MID_CLOSE_ALL
= 32;
83 public static final int MID_WINDOW_MOVE
= 33;
84 public static final int MID_WINDOW_ZOOM
= 34;
85 public static final int MID_WINDOW_NEXT
= 35;
86 public static final int MID_WINDOW_PREVIOUS
= 36;
87 public static final int MID_WINDOW_CLOSE
= 37;
90 public static final int MID_HELP_CONTENTS
= 40;
91 public static final int MID_HELP_INDEX
= 41;
92 public static final int MID_HELP_SEARCH
= 42;
93 public static final int MID_HELP_PREVIOUS
= 43;
94 public static final int MID_HELP_HELP
= 44;
95 public static final int MID_HELP_ACTIVE_FILE
= 45;
96 public static final int MID_ABOUT
= 46;
99 public static final int MID_REPAINT
= 50;
101 // ------------------------------------------------------------------------
102 // Variables --------------------------------------------------------------
103 // ------------------------------------------------------------------------
106 * If true, this is a sub-menu. Note package private access.
108 boolean isSubMenu
= false;
111 * The X position of the menu's title.
116 * The shortcut and title.
118 private MnemonicString mnemonic
;
120 // ------------------------------------------------------------------------
121 // Constructors -----------------------------------------------------------
122 // ------------------------------------------------------------------------
125 * Public constructor.
127 * @param parent parent application
128 * @param x column relative to parent
129 * @param y row relative to parent
130 * @param label mnemonic menu title. Label must contain a keyboard
131 * shortcut (mnemonic), denoted by prefixing a letter with "&",
134 public TMenu(final TApplication parent
, final int x
, final int y
,
135 final String label
) {
137 super(parent
, label
, x
, y
, parent
.getScreen().getWidth(),
138 parent
.getScreen().getHeight());
140 // Setup the menu shortcut
141 mnemonic
= new MnemonicString(label
);
142 setTitle(mnemonic
.getRawLabel());
143 assert (mnemonic
.getShortcutIdx() >= 0);
145 // Recompute width and height to reflect an empty menu
146 setWidth(getTitle().length() + 4);
152 // ------------------------------------------------------------------------
153 // Event handlers ---------------------------------------------------------
154 // ------------------------------------------------------------------------
157 * Handle mouse button presses.
159 * @param mouse mouse button event
162 public void onMouseDown(final TMouseEvent mouse
) {
164 super.onMouseDown(mouse
);
167 for (TWidget widget
: getChildren()) {
168 if (widget
.mouseWouldHit(mouse
)) {
169 // Dispatch to this child, also activate it
172 // Set x and y relative to the child's coordinates
173 mouse
.setX(mouse
.getAbsoluteX() - widget
.getAbsoluteX());
174 mouse
.setY(mouse
.getAbsoluteY() - widget
.getAbsoluteY());
175 widget
.handleEvent(mouse
);
182 * Handle mouse button releases.
184 * @param mouse mouse button release event
187 public void onMouseUp(final TMouseEvent mouse
) {
191 for (TWidget widget
: getChildren()) {
192 if (widget
.mouseWouldHit(mouse
)) {
193 // Dispatch to this child, also activate it
196 // Set x and y relative to the child's coordinates
197 mouse
.setX(mouse
.getAbsoluteX() - widget
.getAbsoluteX());
198 mouse
.setY(mouse
.getAbsoluteY() - widget
.getAbsoluteY());
199 widget
.handleEvent(mouse
);
206 * Handle mouse movements.
208 * @param mouse mouse motion event
211 public void onMouseMotion(final TMouseEvent mouse
) {
214 // See if we should activate a different menu item
215 for (TWidget widget
: getChildren()) {
216 if ((mouse
.isMouse1())
217 && (widget
.mouseWouldHit(mouse
))
219 // Activate this menu item
221 if (widget
instanceof TSubMenu
) {
222 ((TSubMenu
) widget
).dispatch();
232 * @param keypress keystroke event
235 public void onKeypress(final TKeypressEvent keypress
) {
238 System.err.printf("keypress: %s active child: %s\n", keypress,
242 if (getActiveChild() != this) {
243 if ((getActiveChild() instanceof TSubMenu
)
244 || (getActiveChild() instanceof TMenu
)
246 getActiveChild().onKeypress(keypress
);
251 if (keypress
.equals(kbEsc
)) {
252 getApplication().closeMenu();
255 if (keypress
.equals(kbDown
)) {
259 if (keypress
.equals(kbUp
)) {
263 if (keypress
.equals(kbRight
)) {
264 getApplication().switchMenu(true);
267 if (keypress
.equals(kbLeft
)) {
269 getApplication().closeSubMenu();
271 getApplication().switchMenu(false);
276 // Switch to a menuItem if it has an mnemonic
277 if (!keypress
.getKey().isFnKey()
278 && !keypress
.getKey().isAlt()
279 && !keypress
.getKey().isCtrl()) {
280 for (TWidget widget
: getChildren()) {
281 TMenuItem item
= (TMenuItem
) widget
;
282 if ((item
.getMnemonic() != null)
283 && (Character
.toLowerCase(item
.getMnemonic().getShortcut())
284 == Character
.toLowerCase(keypress
.getKey().getChar()))
286 // Send an enter keystroke to it
288 item
.handleEvent(new TKeypressEvent(kbEnter
));
294 // Dispatch the keypress to an active widget
295 for (TWidget widget
: getChildren()) {
296 if (widget
.isActive()) {
297 widget
.handleEvent(keypress
);
303 // ------------------------------------------------------------------------
304 // TWindow ----------------------------------------------------------------
305 // ------------------------------------------------------------------------
308 * Draw a top-level menu with title and menu items.
312 CellAttributes background
= getTheme().getColor("tmenu");
314 assert (isAbsoluteActive());
316 // Fill in the interior background
317 for (int i
= 0; i
< getHeight(); i
++) {
318 hLineXY(0, i
, getWidth(), ' ', background
);
328 cTopLeft
= GraphicsChars
.ULCORNER
;
329 cTopRight
= GraphicsChars
.URCORNER
;
330 cBottomLeft
= GraphicsChars
.LLCORNER
;
331 cBottomRight
= GraphicsChars
.LRCORNER
;
332 cHSide
= GraphicsChars
.SINGLE_BAR
;
334 // Place the corner characters
335 putCharXY(1, 0, cTopLeft
, background
);
336 putCharXY(getWidth() - 2, 0, cTopRight
, background
);
337 putCharXY(1, getHeight() - 1, cBottomLeft
, background
);
338 putCharXY(getWidth() - 2, getHeight() - 1, cBottomRight
, background
);
340 // Draw the box lines
341 hLineXY(1 + 1, 0, getWidth() - 4, cHSide
, background
);
342 hLineXY(1 + 1, getHeight() - 1, getWidth() - 4, cHSide
, background
);
345 drawBoxShadow(0, 0, getWidth(), getHeight());
348 // ------------------------------------------------------------------------
349 // TMenu ------------------------------------------------------------------
350 // ------------------------------------------------------------------------
353 * Set the menu title X position.
355 * @param titleX the position
357 public void setTitleX(final int titleX
) {
358 this.titleX
= titleX
;
362 * Get the menu title X position.
364 * @return the position
366 public int getTitleX() {
371 * Get the mnemonic string.
373 * @return the full mnemonic string
375 public MnemonicString
getMnemonic() {
380 * Convenience function to add a menu item.
382 * @param id menu item ID. Must be greater than 1024.
383 * @param label menu item label
384 * @return the new menu item
386 public TMenuItem
addItem(final int id
, final String label
) {
388 return addItemInternal(id
, label
, null);
392 * Convenience function to add a menu item.
394 * @param id menu item ID. Must be greater than 1024.
395 * @param label menu item label
396 * @param enabled default state for enabled
397 * @return the new menu item
399 public TMenuItem
addItem(final int id
, final String label
,
400 final boolean enabled
) {
403 return addItemInternal(id
, label
, null, enabled
);
407 * Convenience function to add a custom menu item.
409 * @param id menu item ID. Must be greater than 1024.
410 * @param label menu item label
411 * @param key global keyboard accelerator
412 * @return the new menu item
414 public TMenuItem
addItem(final int id
, final String label
,
415 final TKeypress key
) {
418 return addItemInternal(id
, label
, key
);
422 * Convenience function to add a custom menu item.
424 * @param id menu item ID. Must be greater than 1024.
425 * @param label menu item label
426 * @param key global keyboard accelerator
427 * @param enabled default state for enabled
428 * @return the new menu item
430 public TMenuItem
addItem(final int id
, final String label
,
431 final TKeypress key
, final boolean enabled
) {
433 TMenuItem item
= addItem(id
, label
, key
);
434 item
.setEnabled(enabled
);
439 * Convenience function to add a custom menu item.
441 * @param id menu item ID. Must be greater than 1024.
442 * @param label menu item label
443 * @param key global keyboard accelerator
444 * @return the new menu item
446 private TMenuItem
addItemInternal(final int id
, final String label
,
447 final TKeypress key
) {
449 return addItemInternal(id
, label
, key
, true);
453 * Convenience function to add a custom menu item.
455 * @param id menu item ID. Must be greater than 1024.
456 * @param label menu item label
457 * @param key global keyboard accelerator
458 * @param enabled default state for enabled
459 * @return the new menu item
461 private TMenuItem
addItemInternal(final int id
, final String label
,
462 final TKeypress key
, final boolean enabled
) {
464 int newY
= getChildren().size() + 1;
465 assert (newY
< getHeight());
467 TMenuItem menuItem
= new TMenuItem(this, id
, 1, newY
, label
);
468 menuItem
.setKey(key
);
469 menuItem
.setEnabled(enabled
);
470 setHeight(getHeight() + 1);
471 if (menuItem
.getWidth() + 2 > getWidth()) {
472 setWidth(menuItem
.getWidth() + 2);
474 for (TWidget widget
: getChildren()) {
475 widget
.setWidth(getWidth() - 2);
477 getApplication().addMenuItem(menuItem
);
478 getApplication().recomputeMenuX();
484 * Convenience function to add one of the default menu items.
486 * @param id menu item ID. Must be between 0 (inclusive) and 1023
488 * @return the new menu item
490 public TMenuItem
addDefaultItem(final int id
) {
491 return addDefaultItem(id
, true);
495 * Convenience function to add one of the default menu items.
497 * @param id menu item ID. Must be between 0 (inclusive) and 1023
499 * @param enabled default state for enabled
500 * @return the new menu item
502 public TMenuItem
addDefaultItem(final int id
, final boolean enabled
) {
507 TKeypress key
= null;
512 label
= i18n
.getString("menuExit");
517 label
= i18n
.getString("menuShell");
521 label
= i18n
.getString("menuOpen");
526 label
= i18n
.getString("menuCut");
530 label
= i18n
.getString("menuCopy");
534 label
= i18n
.getString("menuPaste");
538 label
= i18n
.getString("menuClear");
543 label
= i18n
.getString("menuFind");
546 label
= i18n
.getString("menuReplace");
548 case MID_SEARCH_AGAIN
:
549 label
= i18n
.getString("menuSearchAgain");
552 label
= i18n
.getString("menuGotoLine");
557 label
= i18n
.getString("menuWindowTile");
560 label
= i18n
.getString("menuWindowCascade");
563 label
= i18n
.getString("menuWindowCloseAll");
565 case MID_WINDOW_MOVE
:
566 label
= i18n
.getString("menuWindowMove");
569 case MID_WINDOW_ZOOM
:
570 label
= i18n
.getString("menuWindowZoom");
573 case MID_WINDOW_NEXT
:
574 label
= i18n
.getString("menuWindowNext");
577 case MID_WINDOW_PREVIOUS
:
578 label
= i18n
.getString("menuWindowPrevious");
581 case MID_WINDOW_CLOSE
:
582 label
= i18n
.getString("menuWindowClose");
586 case MID_HELP_CONTENTS
:
587 label
= i18n
.getString("menuHelpContents");
590 label
= i18n
.getString("menuHelpIndex");
593 case MID_HELP_SEARCH
:
594 label
= i18n
.getString("menuHelpSearch");
597 case MID_HELP_PREVIOUS
:
598 label
= i18n
.getString("menuHelpPrevious");
602 label
= i18n
.getString("menuHelpHelp");
604 case MID_HELP_ACTIVE_FILE
:
605 label
= i18n
.getString("menuHelpActive");
608 label
= i18n
.getString("menuHelpAbout");
612 label
= i18n
.getString("menuRepaintDesktop");
616 throw new IllegalArgumentException("Invalid menu ID: " + id
);
619 return addItemInternal(id
, label
, key
, enabled
);
623 * Convenience function to add a menu separator.
625 public void addSeparator() {
626 int newY
= getChildren().size() + 1;
627 assert (newY
< getHeight());
629 // We just have to construct it, don't need to hang onto what it
631 new TMenuSeparator(this, 1, newY
);
632 setHeight(getHeight() + 1);
636 * Convenience function to add a sub-menu.
638 * @param title menu title. Title must contain a keyboard shortcut,
639 * denoted by prefixing a letter with "&", e.g. "&File"
640 * @return the new sub-menu
642 public TSubMenu
addSubMenu(final String title
) {
643 int newY
= getChildren().size() + 1;
644 assert (newY
< getHeight());
646 TSubMenu subMenu
= new TSubMenu(this, title
, 1, newY
);
647 setHeight(getHeight() + 1);
648 if (subMenu
.getWidth() + 2 > getWidth()) {
649 setWidth(subMenu
.getWidth() + 2);
651 for (TWidget widget
: getChildren()) {
652 widget
.setWidth(getWidth() - 2);
654 getApplication().recomputeMenuX();
656 subMenu
.menu
.setX(getX() + getWidth() - 2);