2 * Jexer - Java Text User Interface
4 * The MIT License (MIT)
6 * Copyright (C) 2017 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
) {
166 for (TWidget widget
: getChildren()) {
167 if (widget
.mouseWouldHit(mouse
)) {
168 // Dispatch to this child, also activate it
171 // Set x and y relative to the child's coordinates
172 mouse
.setX(mouse
.getAbsoluteX() - widget
.getAbsoluteX());
173 mouse
.setY(mouse
.getAbsoluteY() - widget
.getAbsoluteY());
174 widget
.handleEvent(mouse
);
181 * Handle mouse button releases.
183 * @param mouse mouse button release event
186 public void onMouseUp(final TMouseEvent mouse
) {
190 for (TWidget widget
: getChildren()) {
191 if (widget
.mouseWouldHit(mouse
)) {
192 // Dispatch to this child, also activate it
195 // Set x and y relative to the child's coordinates
196 mouse
.setX(mouse
.getAbsoluteX() - widget
.getAbsoluteX());
197 mouse
.setY(mouse
.getAbsoluteY() - widget
.getAbsoluteY());
198 widget
.handleEvent(mouse
);
205 * Handle mouse movements.
207 * @param mouse mouse motion event
210 public void onMouseMotion(final TMouseEvent mouse
) {
213 // See if we should activate a different menu item
214 for (TWidget widget
: getChildren()) {
215 if ((mouse
.isMouse1())
216 && (widget
.mouseWouldHit(mouse
))
218 // Activate this menu item
220 if (widget
instanceof TSubMenu
) {
221 ((TSubMenu
) widget
).dispatch();
231 * @param keypress keystroke event
234 public void onKeypress(final TKeypressEvent keypress
) {
237 System.err.printf("keypress: %s active child: %s\n", keypress,
241 if (getActiveChild() != this) {
242 if ((getActiveChild() instanceof TSubMenu
)
243 || (getActiveChild() instanceof TMenu
)
245 getActiveChild().onKeypress(keypress
);
250 if (keypress
.equals(kbEsc
)) {
251 getApplication().closeMenu();
254 if (keypress
.equals(kbDown
)) {
258 if (keypress
.equals(kbUp
)) {
262 if (keypress
.equals(kbRight
)) {
263 getApplication().switchMenu(true);
266 if (keypress
.equals(kbLeft
)) {
268 getApplication().closeSubMenu();
270 getApplication().switchMenu(false);
275 // Switch to a menuItem if it has an mnemonic
276 if (!keypress
.getKey().isFnKey()
277 && !keypress
.getKey().isAlt()
278 && !keypress
.getKey().isCtrl()) {
279 for (TWidget widget
: getChildren()) {
280 TMenuItem item
= (TMenuItem
) widget
;
281 if ((item
.getMnemonic() != null)
282 && (Character
.toLowerCase(item
.getMnemonic().getShortcut())
283 == Character
.toLowerCase(keypress
.getKey().getChar()))
285 // Send an enter keystroke to it
287 item
.handleEvent(new TKeypressEvent(kbEnter
));
293 // Dispatch the keypress to an active widget
294 for (TWidget widget
: getChildren()) {
295 if (widget
.isActive()) {
296 widget
.handleEvent(keypress
);
302 // ------------------------------------------------------------------------
303 // TWindow ----------------------------------------------------------------
304 // ------------------------------------------------------------------------
307 * Draw a top-level menu with title and menu items.
311 CellAttributes background
= getTheme().getColor("tmenu");
313 assert (isAbsoluteActive());
315 // Fill in the interior background
316 for (int i
= 0; i
< getHeight(); i
++) {
317 hLineXY(0, i
, getWidth(), ' ', background
);
327 cTopLeft
= GraphicsChars
.ULCORNER
;
328 cTopRight
= GraphicsChars
.URCORNER
;
329 cBottomLeft
= GraphicsChars
.LLCORNER
;
330 cBottomRight
= GraphicsChars
.LRCORNER
;
331 cHSide
= GraphicsChars
.SINGLE_BAR
;
333 // Place the corner characters
334 putCharXY(1, 0, cTopLeft
, background
);
335 putCharXY(getWidth() - 2, 0, cTopRight
, background
);
336 putCharXY(1, getHeight() - 1, cBottomLeft
, background
);
337 putCharXY(getWidth() - 2, getHeight() - 1, cBottomRight
, background
);
339 // Draw the box lines
340 hLineXY(1 + 1, 0, getWidth() - 4, cHSide
, background
);
341 hLineXY(1 + 1, getHeight() - 1, getWidth() - 4, cHSide
, background
);
344 getScreen().drawBoxShadow(0, 0, getWidth(), getHeight());
347 // ------------------------------------------------------------------------
348 // TMenu ------------------------------------------------------------------
349 // ------------------------------------------------------------------------
352 * Set the menu title X position.
354 * @param titleX the position
356 public void setTitleX(final int titleX
) {
357 this.titleX
= titleX
;
361 * Get the menu title X position.
363 * @return the position
365 public int getTitleX() {
370 * Get the mnemonic string.
372 * @return the full mnemonic string
374 public MnemonicString
getMnemonic() {
379 * Convenience function to add a menu item.
381 * @param id menu item ID. Must be greater than 1024.
382 * @param label menu item label
383 * @return the new menu item
385 public TMenuItem
addItem(final int id
, final String label
) {
387 return addItemInternal(id
, label
, null);
391 * Convenience function to add a custom menu item.
393 * @param id menu item ID. Must be greater than 1024.
394 * @param label menu item label
395 * @param key global keyboard accelerator
396 * @return the new menu item
398 public TMenuItem
addItem(final int id
, final String label
,
399 final TKeypress key
) {
402 return addItemInternal(id
, label
, key
);
406 * Convenience function to add a custom menu item.
408 * @param id menu item ID. Must be greater than 1024.
409 * @param label menu item label
410 * @param key global keyboard accelerator
411 * @param enabled default state for enabled
412 * @return the new menu item
414 public TMenuItem
addItem(final int id
, final String label
,
415 final TKeypress key
, final boolean enabled
) {
417 TMenuItem item
= addItem(id
, label
, key
);
418 item
.setEnabled(enabled
);
423 * Convenience function to add a custom menu item.
425 * @param id menu item ID. Must be greater than 1024.
426 * @param label menu item label
427 * @param key global keyboard accelerator
428 * @return the new menu item
430 private TMenuItem
addItemInternal(final int id
, final String label
,
431 final TKeypress key
) {
433 return addItemInternal(id
, label
, key
, true);
437 * Convenience function to add a custom menu item.
439 * @param id menu item ID. Must be greater than 1024.
440 * @param label menu item label
441 * @param key global keyboard accelerator
442 * @param enabled default state for enabled
443 * @return the new menu item
445 private TMenuItem
addItemInternal(final int id
, final String label
,
446 final TKeypress key
, final boolean enabled
) {
448 int newY
= getChildren().size() + 1;
449 assert (newY
< getHeight());
451 TMenuItem menuItem
= new TMenuItem(this, id
, 1, newY
, label
);
452 menuItem
.setKey(key
);
453 menuItem
.setEnabled(enabled
);
454 setHeight(getHeight() + 1);
455 if (menuItem
.getWidth() + 2 > getWidth()) {
456 setWidth(menuItem
.getWidth() + 2);
458 for (TWidget widget
: getChildren()) {
459 widget
.setWidth(getWidth() - 2);
461 getApplication().addMenuItem(menuItem
);
462 getApplication().recomputeMenuX();
468 * Convenience function to add one of the default menu items.
470 * @param id menu item ID. Must be between 0 (inclusive) and 1023
472 * @return the new menu item
474 public TMenuItem
addDefaultItem(final int id
) {
475 return addDefaultItem(id
, true);
479 * Convenience function to add one of the default menu items.
481 * @param id menu item ID. Must be between 0 (inclusive) and 1023
483 * @param enabled default state for enabled
484 * @return the new menu item
486 public TMenuItem
addDefaultItem(final int id
, final boolean enabled
) {
491 TKeypress key
= null;
496 label
= i18n
.getString("menuExit");
501 label
= i18n
.getString("menuShell");
505 label
= i18n
.getString("menuOpen");
510 label
= i18n
.getString("menuCut");
514 label
= i18n
.getString("menuCopy");
518 label
= i18n
.getString("menuPaste");
522 label
= i18n
.getString("menuClear");
527 label
= i18n
.getString("menuFind");
530 label
= i18n
.getString("menuReplace");
532 case MID_SEARCH_AGAIN
:
533 label
= i18n
.getString("menuSearchAgain");
536 label
= i18n
.getString("menuGotoLine");
541 label
= i18n
.getString("menuWindowTile");
544 label
= i18n
.getString("menuWindowCascade");
547 label
= i18n
.getString("menuWindowCloseAll");
549 case MID_WINDOW_MOVE
:
550 label
= i18n
.getString("menuWindowMove");
553 case MID_WINDOW_ZOOM
:
554 label
= i18n
.getString("menuWindowZoom");
557 case MID_WINDOW_NEXT
:
558 label
= i18n
.getString("menuWindowNext");
561 case MID_WINDOW_PREVIOUS
:
562 label
= i18n
.getString("menuWindowPrevious");
565 case MID_WINDOW_CLOSE
:
566 label
= i18n
.getString("menuWindowClose");
570 case MID_HELP_CONTENTS
:
571 label
= i18n
.getString("menuHelpContents");
574 label
= i18n
.getString("menuHelpIndex");
577 case MID_HELP_SEARCH
:
578 label
= i18n
.getString("menuHelpSearch");
581 case MID_HELP_PREVIOUS
:
582 label
= i18n
.getString("menuHelpPrevious");
586 label
= i18n
.getString("menuHelpHelp");
588 case MID_HELP_ACTIVE_FILE
:
589 label
= i18n
.getString("menuHelpActive");
592 label
= i18n
.getString("menuHelpAbout");
596 label
= i18n
.getString("menuRepaintDesktop");
600 throw new IllegalArgumentException("Invalid menu ID: " + id
);
603 return addItemInternal(id
, label
, key
, enabled
);
607 * Convenience function to add a menu separator.
609 public void addSeparator() {
610 int newY
= getChildren().size() + 1;
611 assert (newY
< getHeight());
613 // We just have to construct it, don't need to hang onto what it
615 new TMenuSeparator(this, 1, newY
);
616 setHeight(getHeight() + 1);
620 * Convenience function to add a sub-menu.
622 * @param title menu title. Title must contain a keyboard shortcut,
623 * denoted by prefixing a letter with "&", e.g. "&File"
624 * @return the new sub-menu
626 public TSubMenu
addSubMenu(final String title
) {
627 int newY
= getChildren().size() + 1;
628 assert (newY
< getHeight());
630 TSubMenu subMenu
= new TSubMenu(this, title
, 1, newY
);
631 setHeight(getHeight() + 1);
632 if (subMenu
.getWidth() + 2 > getWidth()) {
633 setWidth(subMenu
.getWidth() + 2);
635 for (TWidget widget
: getChildren()) {
636 widget
.setWidth(getWidth() - 2);
638 getApplication().recomputeMenuX();
640 subMenu
.menu
.setX(getX() + getWidth() - 2);