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 final class TMenu
extends TWindow
{
52 private static final ResourceBundle i18n
= ResourceBundle
.getBundle(TMenu
.class.getName());
55 * If true, this is a sub-menu. Note package private access.
57 boolean isSubMenu
= false;
60 * The shortcut and title.
62 private MnemonicString mnemonic
;
65 * Get the mnemonic string.
67 * @return the full mnemonic string
69 public MnemonicString
getMnemonic() {
73 // Reserved menu item IDs
74 public static final int MID_UNUSED
= -1;
77 public static final int MID_EXIT
= 1;
78 public static final int MID_QUIT
= MID_EXIT
;
79 public static final int MID_OPEN_FILE
= 2;
80 public static final int MID_SHELL
= 3;
83 public static final int MID_CUT
= 10;
84 public static final int MID_COPY
= 11;
85 public static final int MID_PASTE
= 12;
86 public static final int MID_CLEAR
= 13;
89 public static final int MID_TILE
= 20;
90 public static final int MID_CASCADE
= 21;
91 public static final int MID_CLOSE_ALL
= 22;
92 public static final int MID_WINDOW_MOVE
= 23;
93 public static final int MID_WINDOW_ZOOM
= 24;
94 public static final int MID_WINDOW_NEXT
= 25;
95 public static final int MID_WINDOW_PREVIOUS
= 26;
96 public static final int MID_WINDOW_CLOSE
= 27;
99 public static final int MID_HELP_CONTENTS
= 40;
100 public static final int MID_HELP_INDEX
= 41;
101 public static final int MID_HELP_SEARCH
= 42;
102 public static final int MID_HELP_PREVIOUS
= 43;
103 public static final int MID_HELP_HELP
= 44;
104 public static final int MID_HELP_ACTIVE_FILE
= 45;
105 public static final int MID_ABOUT
= 46;
108 public static final int MID_REPAINT
= 50;
111 * Public constructor.
113 * @param parent parent application
114 * @param x column relative to parent
115 * @param y row relative to parent
116 * @param label mnemonic menu title. Label must contain a keyboard
117 * shortcut (mnemonic), denoted by prefixing a letter with "&",
120 public TMenu(final TApplication parent
, final int x
, final int y
,
121 final String label
) {
123 super(parent
, label
, x
, y
, parent
.getScreen().getWidth(),
124 parent
.getScreen().getHeight());
126 // Setup the menu shortcut
127 mnemonic
= new MnemonicString(label
);
128 setTitle(mnemonic
.getRawLabel());
129 assert (mnemonic
.getShortcutIdx() >= 0);
131 // Recompute width and height to reflect an empty menu
132 setWidth(getTitle().length() + 4);
139 * Draw a top-level menu with title and menu items.
143 CellAttributes background
= getTheme().getColor("tmenu");
145 assert (isAbsoluteActive());
147 // Fill in the interior background
148 for (int i
= 0; i
< getHeight(); i
++) {
149 hLineXY(0, i
, getWidth(), ' ', background
);
159 cTopLeft
= GraphicsChars
.ULCORNER
;
160 cTopRight
= GraphicsChars
.URCORNER
;
161 cBottomLeft
= GraphicsChars
.LLCORNER
;
162 cBottomRight
= GraphicsChars
.LRCORNER
;
163 cHSide
= GraphicsChars
.SINGLE_BAR
;
165 // Place the corner characters
166 putCharXY(1, 0, cTopLeft
, background
);
167 putCharXY(getWidth() - 2, 0, cTopRight
, background
);
168 putCharXY(1, getHeight() - 1, cBottomLeft
, background
);
169 putCharXY(getWidth() - 2, getHeight() - 1, cBottomRight
, background
);
171 // Draw the box lines
172 hLineXY(1 + 1, 0, getWidth() - 4, cHSide
, background
);
173 hLineXY(1 + 1, getHeight() - 1, getWidth() - 4, cHSide
, background
);
176 getScreen().drawBoxShadow(0, 0, getWidth(), getHeight());
180 * Handle mouse button presses.
182 * @param mouse mouse button event
185 public void onMouseDown(final TMouseEvent mouse
) {
189 for (TWidget widget
: getChildren()) {
190 if (widget
.mouseWouldHit(mouse
)) {
191 // Dispatch to this child, also activate it
194 // Set x and y relative to the child's coordinates
195 mouse
.setX(mouse
.getAbsoluteX() - widget
.getAbsoluteX());
196 mouse
.setY(mouse
.getAbsoluteY() - widget
.getAbsoluteY());
197 widget
.handleEvent(mouse
);
204 * Handle mouse button releases.
206 * @param mouse mouse button release event
209 public void onMouseUp(final TMouseEvent mouse
) {
213 for (TWidget widget
: getChildren()) {
214 if (widget
.mouseWouldHit(mouse
)) {
215 // Dispatch to this child, also activate it
218 // Set x and y relative to the child's coordinates
219 mouse
.setX(mouse
.getAbsoluteX() - widget
.getAbsoluteX());
220 mouse
.setY(mouse
.getAbsoluteY() - widget
.getAbsoluteY());
221 widget
.handleEvent(mouse
);
228 * Handle mouse movements.
230 * @param mouse mouse motion event
233 public void onMouseMotion(final TMouseEvent mouse
) {
236 // See if we should activate a different menu item
237 for (TWidget widget
: getChildren()) {
238 if ((mouse
.isMouse1())
239 && (widget
.mouseWouldHit(mouse
))
241 // Activate this menu item
243 if (widget
instanceof TSubMenu
) {
244 ((TSubMenu
) widget
).dispatch();
254 * @param keypress keystroke event
257 public void onKeypress(final TKeypressEvent keypress
) {
260 System.err.printf("keypress: %s active child: %s\n", keypress,
264 if (getActiveChild() != this) {
265 if ((getActiveChild() instanceof TSubMenu
)
266 || (getActiveChild() instanceof TMenu
)
268 getActiveChild().onKeypress(keypress
);
273 if (keypress
.equals(kbEsc
)) {
274 getApplication().closeMenu();
277 if (keypress
.equals(kbDown
)) {
281 if (keypress
.equals(kbUp
)) {
285 if (keypress
.equals(kbRight
)) {
286 getApplication().switchMenu(true);
289 if (keypress
.equals(kbLeft
)) {
291 getApplication().closeSubMenu();
293 getApplication().switchMenu(false);
298 // Switch to a menuItem if it has an mnemonic
299 if (!keypress
.getKey().isFnKey()
300 && !keypress
.getKey().isAlt()
301 && !keypress
.getKey().isCtrl()) {
302 for (TWidget widget
: getChildren()) {
303 TMenuItem item
= (TMenuItem
) widget
;
304 if ((item
.getMnemonic() != null)
305 && (Character
.toLowerCase(item
.getMnemonic().getShortcut())
306 == Character
.toLowerCase(keypress
.getKey().getChar()))
308 // Send an enter keystroke to it
310 item
.handleEvent(new TKeypressEvent(kbEnter
));
316 // Dispatch the keypress to an active widget
317 for (TWidget widget
: getChildren()) {
318 if (widget
.isActive()) {
319 widget
.handleEvent(keypress
);
326 * Convenience function to add a menu item.
328 * @param id menu item ID. Must be greater than 1024.
329 * @param label menu item label
330 * @return the new menu item
332 public TMenuItem
addItem(final int id
, final String label
) {
334 return addItemInternal(id
, label
, null);
338 * Convenience function to add a custom menu item.
340 * @param id menu item ID. Must be greater than 1024.
341 * @param label menu item label
342 * @param key global keyboard accelerator
343 * @return the new menu item
345 public TMenuItem
addItem(final int id
, final String label
,
346 final TKeypress key
) {
349 return addItemInternal(id
, label
, key
);
353 * Convenience function to add a custom menu item.
355 * @param id menu item ID. Must be greater than 1024.
356 * @param label menu item label
357 * @param key global keyboard accelerator
358 * @param enabled default state for enabled
359 * @return the new menu item
361 public TMenuItem
addItem(final int id
, final String label
,
362 final TKeypress key
, final boolean enabled
) {
364 TMenuItem item
= addItem(id
, label
, key
);
365 item
.setEnabled(enabled
);
370 * Convenience function to add a custom menu item.
372 * @param id menu item ID. Must be greater than 1024.
373 * @param label menu item label
374 * @param key global keyboard accelerator
375 * @return the new menu item
377 private TMenuItem
addItemInternal(final int id
, final String label
,
378 final TKeypress key
) {
380 int newY
= getChildren().size() + 1;
381 assert (newY
< getHeight());
383 TMenuItem menuItem
= new TMenuItem(this, id
, 1, newY
, label
);
384 menuItem
.setKey(key
);
385 setHeight(getHeight() + 1);
386 if (menuItem
.getWidth() + 2 > getWidth()) {
387 setWidth(menuItem
.getWidth() + 2);
389 for (TWidget widget
: getChildren()) {
390 widget
.setWidth(getWidth() - 2);
392 getApplication().addMenuItem(menuItem
);
393 getApplication().recomputeMenuX();
399 * Convenience function to add one of the default menu items.
401 * @param id menu item ID. Must be between 0 (inclusive) and 1023
403 * @return the new menu item
405 public TMenuItem
addDefaultItem(final int id
) {
410 TKeypress key
= null;
415 label
= i18n
.getString("menuExit");
420 label
= i18n
.getString("menuShell");
424 label
= i18n
.getString("menuOpen");
429 label
= i18n
.getString("menuCut");
433 label
= i18n
.getString("menuCopy");
437 label
= i18n
.getString("menuPaste");
441 label
= i18n
.getString("menuClear");
446 label
= i18n
.getString("menuWindowTile");
449 label
= i18n
.getString("menuWindowCascade");
452 label
= i18n
.getString("menuWindowCloseAll");
454 case MID_WINDOW_MOVE
:
455 label
= i18n
.getString("menuWindowMove");
458 case MID_WINDOW_ZOOM
:
459 label
= i18n
.getString("menuWindowZoom");
462 case MID_WINDOW_NEXT
:
463 label
= i18n
.getString("menuWindowNext");
466 case MID_WINDOW_PREVIOUS
:
467 label
= i18n
.getString("menuWindowPrevious");
470 case MID_WINDOW_CLOSE
:
471 label
= i18n
.getString("menuWindowClose");
475 case MID_HELP_CONTENTS
:
476 label
= i18n
.getString("menuHelpContents");
479 label
= i18n
.getString("menuHelpIndex");
482 case MID_HELP_SEARCH
:
483 label
= i18n
.getString("menuHelpSearch");
486 case MID_HELP_PREVIOUS
:
487 label
= i18n
.getString("menuHelpPrevious");
491 label
= i18n
.getString("menuHelpHelp");
493 case MID_HELP_ACTIVE_FILE
:
494 label
= i18n
.getString("menuHelpActive");
497 label
= i18n
.getString("menuHelpAbout");
501 label
= i18n
.getString("menuRepaintDesktop");
505 throw new IllegalArgumentException("Invalid menu ID: " + id
);
508 return addItemInternal(id
, label
, key
);
512 * Convenience function to add a menu separator.
514 public void addSeparator() {
515 int newY
= getChildren().size() + 1;
516 assert (newY
< getHeight());
518 // We just have to construct it, don't need to hang onto what it
520 new TMenuSeparator(this, 1, newY
);
521 setHeight(getHeight() + 1);
525 * Convenience function to add a sub-menu.
527 * @param title menu title. Title must contain a keyboard shortcut,
528 * denoted by prefixing a letter with "&", e.g. "&File"
529 * @return the new sub-menu
531 public TSubMenu
addSubMenu(final String title
) {
532 int newY
= getChildren().size() + 1;
533 assert (newY
< getHeight());
535 TSubMenu subMenu
= new TSubMenu(this, title
, 1, newY
);
536 setHeight(getHeight() + 1);
537 if (subMenu
.getWidth() + 2 > getWidth()) {
538 setWidth(subMenu
.getWidth() + 2);
540 for (TWidget widget
: getChildren()) {
541 widget
.setWidth(getWidth() - 2);
543 getApplication().recomputeMenuX();
545 subMenu
.menu
.setX(getX() + getWidth() - 2);