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 X position of the menu's title.
65 * Set the menu title X position.
67 * @param titleX the position
69 public void setTitleX(final int titleX
) {
74 * Get the menu title X position.
76 * @return the position
78 public int getTitleX() {
83 * The shortcut and title.
85 private MnemonicString mnemonic
;
88 * Get the mnemonic string.
90 * @return the full mnemonic string
92 public MnemonicString
getMnemonic() {
96 // Reserved menu item IDs
97 public static final int MID_UNUSED
= -1;
100 public static final int MID_EXIT
= 1;
101 public static final int MID_QUIT
= MID_EXIT
;
102 public static final int MID_OPEN_FILE
= 2;
103 public static final int MID_SHELL
= 3;
106 public static final int MID_CUT
= 10;
107 public static final int MID_COPY
= 11;
108 public static final int MID_PASTE
= 12;
109 public static final int MID_CLEAR
= 13;
112 public static final int MID_TILE
= 20;
113 public static final int MID_CASCADE
= 21;
114 public static final int MID_CLOSE_ALL
= 22;
115 public static final int MID_WINDOW_MOVE
= 23;
116 public static final int MID_WINDOW_ZOOM
= 24;
117 public static final int MID_WINDOW_NEXT
= 25;
118 public static final int MID_WINDOW_PREVIOUS
= 26;
119 public static final int MID_WINDOW_CLOSE
= 27;
122 public static final int MID_HELP_CONTENTS
= 40;
123 public static final int MID_HELP_INDEX
= 41;
124 public static final int MID_HELP_SEARCH
= 42;
125 public static final int MID_HELP_PREVIOUS
= 43;
126 public static final int MID_HELP_HELP
= 44;
127 public static final int MID_HELP_ACTIVE_FILE
= 45;
128 public static final int MID_ABOUT
= 46;
131 public static final int MID_REPAINT
= 50;
134 * Public constructor.
136 * @param parent parent application
137 * @param x column relative to parent
138 * @param y row relative to parent
139 * @param label mnemonic menu title. Label must contain a keyboard
140 * shortcut (mnemonic), denoted by prefixing a letter with "&",
143 public TMenu(final TApplication parent
, final int x
, final int y
,
144 final String label
) {
146 super(parent
, label
, x
, y
, parent
.getScreen().getWidth(),
147 parent
.getScreen().getHeight());
149 // Setup the menu shortcut
150 mnemonic
= new MnemonicString(label
);
151 setTitle(mnemonic
.getRawLabel());
152 assert (mnemonic
.getShortcutIdx() >= 0);
154 // Recompute width and height to reflect an empty menu
155 setWidth(getTitle().length() + 4);
162 * Draw a top-level menu with title and menu items.
166 CellAttributes background
= getTheme().getColor("tmenu");
168 assert (isAbsoluteActive());
170 // Fill in the interior background
171 for (int i
= 0; i
< getHeight(); i
++) {
172 hLineXY(0, i
, getWidth(), ' ', background
);
182 cTopLeft
= GraphicsChars
.ULCORNER
;
183 cTopRight
= GraphicsChars
.URCORNER
;
184 cBottomLeft
= GraphicsChars
.LLCORNER
;
185 cBottomRight
= GraphicsChars
.LRCORNER
;
186 cHSide
= GraphicsChars
.SINGLE_BAR
;
188 // Place the corner characters
189 putCharXY(1, 0, cTopLeft
, background
);
190 putCharXY(getWidth() - 2, 0, cTopRight
, background
);
191 putCharXY(1, getHeight() - 1, cBottomLeft
, background
);
192 putCharXY(getWidth() - 2, getHeight() - 1, cBottomRight
, background
);
194 // Draw the box lines
195 hLineXY(1 + 1, 0, getWidth() - 4, cHSide
, background
);
196 hLineXY(1 + 1, getHeight() - 1, getWidth() - 4, cHSide
, background
);
199 getScreen().drawBoxShadow(0, 0, getWidth(), getHeight());
203 * Handle mouse button presses.
205 * @param mouse mouse button event
208 public void onMouseDown(final TMouseEvent mouse
) {
212 for (TWidget widget
: getChildren()) {
213 if (widget
.mouseWouldHit(mouse
)) {
214 // Dispatch to this child, also activate it
217 // Set x and y relative to the child's coordinates
218 mouse
.setX(mouse
.getAbsoluteX() - widget
.getAbsoluteX());
219 mouse
.setY(mouse
.getAbsoluteY() - widget
.getAbsoluteY());
220 widget
.handleEvent(mouse
);
227 * Handle mouse button releases.
229 * @param mouse mouse button release event
232 public void onMouseUp(final TMouseEvent mouse
) {
236 for (TWidget widget
: getChildren()) {
237 if (widget
.mouseWouldHit(mouse
)) {
238 // Dispatch to this child, also activate it
241 // Set x and y relative to the child's coordinates
242 mouse
.setX(mouse
.getAbsoluteX() - widget
.getAbsoluteX());
243 mouse
.setY(mouse
.getAbsoluteY() - widget
.getAbsoluteY());
244 widget
.handleEvent(mouse
);
251 * Handle mouse movements.
253 * @param mouse mouse motion event
256 public void onMouseMotion(final TMouseEvent mouse
) {
259 // See if we should activate a different menu item
260 for (TWidget widget
: getChildren()) {
261 if ((mouse
.isMouse1())
262 && (widget
.mouseWouldHit(mouse
))
264 // Activate this menu item
266 if (widget
instanceof TSubMenu
) {
267 ((TSubMenu
) widget
).dispatch();
277 * @param keypress keystroke event
280 public void onKeypress(final TKeypressEvent keypress
) {
283 System.err.printf("keypress: %s active child: %s\n", keypress,
287 if (getActiveChild() != this) {
288 if ((getActiveChild() instanceof TSubMenu
)
289 || (getActiveChild() instanceof TMenu
)
291 getActiveChild().onKeypress(keypress
);
296 if (keypress
.equals(kbEsc
)) {
297 getApplication().closeMenu();
300 if (keypress
.equals(kbDown
)) {
304 if (keypress
.equals(kbUp
)) {
308 if (keypress
.equals(kbRight
)) {
309 getApplication().switchMenu(true);
312 if (keypress
.equals(kbLeft
)) {
314 getApplication().closeSubMenu();
316 getApplication().switchMenu(false);
321 // Switch to a menuItem if it has an mnemonic
322 if (!keypress
.getKey().isFnKey()
323 && !keypress
.getKey().isAlt()
324 && !keypress
.getKey().isCtrl()) {
325 for (TWidget widget
: getChildren()) {
326 TMenuItem item
= (TMenuItem
) widget
;
327 if ((item
.getMnemonic() != null)
328 && (Character
.toLowerCase(item
.getMnemonic().getShortcut())
329 == Character
.toLowerCase(keypress
.getKey().getChar()))
331 // Send an enter keystroke to it
333 item
.handleEvent(new TKeypressEvent(kbEnter
));
339 // Dispatch the keypress to an active widget
340 for (TWidget widget
: getChildren()) {
341 if (widget
.isActive()) {
342 widget
.handleEvent(keypress
);
349 * Convenience function to add a menu item.
351 * @param id menu item ID. Must be greater than 1024.
352 * @param label menu item label
353 * @return the new menu item
355 public TMenuItem
addItem(final int id
, final String label
) {
357 return addItemInternal(id
, label
, null);
361 * Convenience function to add a custom menu item.
363 * @param id menu item ID. Must be greater than 1024.
364 * @param label menu item label
365 * @param key global keyboard accelerator
366 * @return the new menu item
368 public TMenuItem
addItem(final int id
, final String label
,
369 final TKeypress key
) {
372 return addItemInternal(id
, label
, key
);
376 * Convenience function to add a custom menu item.
378 * @param id menu item ID. Must be greater than 1024.
379 * @param label menu item label
380 * @param key global keyboard accelerator
381 * @param enabled default state for enabled
382 * @return the new menu item
384 public TMenuItem
addItem(final int id
, final String label
,
385 final TKeypress key
, final boolean enabled
) {
387 TMenuItem item
= addItem(id
, label
, key
);
388 item
.setEnabled(enabled
);
393 * Convenience function to add a custom menu item.
395 * @param id menu item ID. Must be greater than 1024.
396 * @param label menu item label
397 * @param key global keyboard accelerator
398 * @return the new menu item
400 private TMenuItem
addItemInternal(final int id
, final String label
,
401 final TKeypress key
) {
403 int newY
= getChildren().size() + 1;
404 assert (newY
< getHeight());
406 TMenuItem menuItem
= new TMenuItem(this, id
, 1, newY
, label
);
407 menuItem
.setKey(key
);
408 setHeight(getHeight() + 1);
409 if (menuItem
.getWidth() + 2 > getWidth()) {
410 setWidth(menuItem
.getWidth() + 2);
412 for (TWidget widget
: getChildren()) {
413 widget
.setWidth(getWidth() - 2);
415 getApplication().addMenuItem(menuItem
);
416 getApplication().recomputeMenuX();
422 * Convenience function to add one of the default menu items.
424 * @param id menu item ID. Must be between 0 (inclusive) and 1023
426 * @return the new menu item
428 public TMenuItem
addDefaultItem(final int id
) {
433 TKeypress key
= null;
438 label
= i18n
.getString("menuExit");
443 label
= i18n
.getString("menuShell");
447 label
= i18n
.getString("menuOpen");
452 label
= i18n
.getString("menuCut");
456 label
= i18n
.getString("menuCopy");
460 label
= i18n
.getString("menuPaste");
464 label
= i18n
.getString("menuClear");
469 label
= i18n
.getString("menuWindowTile");
472 label
= i18n
.getString("menuWindowCascade");
475 label
= i18n
.getString("menuWindowCloseAll");
477 case MID_WINDOW_MOVE
:
478 label
= i18n
.getString("menuWindowMove");
481 case MID_WINDOW_ZOOM
:
482 label
= i18n
.getString("menuWindowZoom");
485 case MID_WINDOW_NEXT
:
486 label
= i18n
.getString("menuWindowNext");
489 case MID_WINDOW_PREVIOUS
:
490 label
= i18n
.getString("menuWindowPrevious");
493 case MID_WINDOW_CLOSE
:
494 label
= i18n
.getString("menuWindowClose");
498 case MID_HELP_CONTENTS
:
499 label
= i18n
.getString("menuHelpContents");
502 label
= i18n
.getString("menuHelpIndex");
505 case MID_HELP_SEARCH
:
506 label
= i18n
.getString("menuHelpSearch");
509 case MID_HELP_PREVIOUS
:
510 label
= i18n
.getString("menuHelpPrevious");
514 label
= i18n
.getString("menuHelpHelp");
516 case MID_HELP_ACTIVE_FILE
:
517 label
= i18n
.getString("menuHelpActive");
520 label
= i18n
.getString("menuHelpAbout");
524 label
= i18n
.getString("menuRepaintDesktop");
528 throw new IllegalArgumentException("Invalid menu ID: " + id
);
531 return addItemInternal(id
, label
, key
);
535 * Convenience function to add a menu separator.
537 public void addSeparator() {
538 int newY
= getChildren().size() + 1;
539 assert (newY
< getHeight());
541 // We just have to construct it, don't need to hang onto what it
543 new TMenuSeparator(this, 1, newY
);
544 setHeight(getHeight() + 1);
548 * Convenience function to add a sub-menu.
550 * @param title menu title. Title must contain a keyboard shortcut,
551 * denoted by prefixing a letter with "&", e.g. "&File"
552 * @return the new sub-menu
554 public TSubMenu
addSubMenu(final String title
) {
555 int newY
= getChildren().size() + 1;
556 assert (newY
< getHeight());
558 TSubMenu subMenu
= new TSubMenu(this, title
, 1, newY
);
559 setHeight(getHeight() + 1);
560 if (subMenu
.getWidth() + 2 > getWidth()) {
561 setWidth(subMenu
.getWidth() + 2);
563 for (TWidget widget
: getChildren()) {
564 widget
.setWidth(getWidth() - 2);
566 getApplication().recomputeMenuX();
568 subMenu
.menu
.setX(getX() + getWidth() - 2);