2 * Jexer - Java Text User Interface
4 * The MIT License (MIT)
6 * Copyright (C) 2016 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 jexer
.TApplication
;
32 import jexer
.TKeypress
;
35 import jexer
.bits
.CellAttributes
;
36 import jexer
.bits
.GraphicsChars
;
37 import jexer
.bits
.MnemonicString
;
38 import jexer
.event
.TKeypressEvent
;
39 import jexer
.event
.TMouseEvent
;
40 import static jexer
.TKeypress
.*;
43 * TMenu is a top-level collection of TMenuItems.
45 public final class TMenu
extends TWindow
{
48 * If true, this is a sub-menu. Note package private access.
50 boolean isSubMenu
= false;
53 * The shortcut and title.
55 private MnemonicString mnemonic
;
58 * Get the mnemonic string.
60 * @return the full mnemonic string
62 public MnemonicString
getMnemonic() {
66 // Reserved menu item IDs
67 public static final int MID_UNUSED
= -1;
70 public static final int MID_EXIT
= 1;
71 public static final int MID_QUIT
= MID_EXIT
;
72 public static final int MID_OPEN_FILE
= 2;
73 public static final int MID_SHELL
= 3;
76 public static final int MID_CUT
= 10;
77 public static final int MID_COPY
= 11;
78 public static final int MID_PASTE
= 12;
79 public static final int MID_CLEAR
= 13;
82 public static final int MID_TILE
= 20;
83 public static final int MID_CASCADE
= 21;
84 public static final int MID_CLOSE_ALL
= 22;
85 public static final int MID_WINDOW_MOVE
= 23;
86 public static final int MID_WINDOW_ZOOM
= 24;
87 public static final int MID_WINDOW_NEXT
= 25;
88 public static final int MID_WINDOW_PREVIOUS
= 26;
89 public static final int MID_WINDOW_CLOSE
= 27;
92 public static final int MID_HELP_CONTENTS
= 40;
93 public static final int MID_HELP_INDEX
= 41;
94 public static final int MID_HELP_SEARCH
= 42;
95 public static final int MID_HELP_PREVIOUS
= 43;
96 public static final int MID_HELP_HELP
= 44;
97 public static final int MID_HELP_ACTIVE_FILE
= 45;
98 public static final int MID_ABOUT
= 46;
101 * Public constructor.
103 * @param parent parent application
104 * @param x column relative to parent
105 * @param y row relative to parent
106 * @param label mnemonic menu title. Label must contain a keyboard
107 * shortcut (mnemonic), denoted by prefixing a letter with "&",
110 public TMenu(final TApplication parent
, final int x
, final int y
,
111 final String label
) {
113 super(parent
, label
, x
, y
, parent
.getScreen().getWidth(),
114 parent
.getScreen().getHeight());
116 // Setup the menu shortcut
117 mnemonic
= new MnemonicString(label
);
118 setTitle(mnemonic
.getRawLabel());
119 assert (mnemonic
.getShortcutIdx() >= 0);
121 // Recompute width and height to reflect an empty menu
122 setWidth(getTitle().length() + 4);
129 * Draw a top-level menu with title and menu items.
133 CellAttributes background
= getTheme().getColor("tmenu");
135 assert (isAbsoluteActive());
137 // Fill in the interior background
138 for (int i
= 0; i
< getHeight(); i
++) {
139 hLineXY(0, i
, getWidth(), ' ', background
);
149 cTopLeft
= GraphicsChars
.ULCORNER
;
150 cTopRight
= GraphicsChars
.URCORNER
;
151 cBottomLeft
= GraphicsChars
.LLCORNER
;
152 cBottomRight
= GraphicsChars
.LRCORNER
;
153 cHSide
= GraphicsChars
.SINGLE_BAR
;
155 // Place the corner characters
156 putCharXY(1, 0, cTopLeft
, background
);
157 putCharXY(getWidth() - 2, 0, cTopRight
, background
);
158 putCharXY(1, getHeight() - 1, cBottomLeft
, background
);
159 putCharXY(getWidth() - 2, getHeight() - 1, cBottomRight
, background
);
161 // Draw the box lines
162 hLineXY(1 + 1, 0, getWidth() - 4, cHSide
, background
);
163 hLineXY(1 + 1, getHeight() - 1, getWidth() - 4, cHSide
, background
);
166 getScreen().drawBoxShadow(0, 0, getWidth(), getHeight());
170 * Handle mouse button presses.
172 * @param mouse mouse button event
175 public void onMouseDown(final TMouseEvent mouse
) {
179 for (TWidget widget
: getChildren()) {
180 if (widget
.mouseWouldHit(mouse
)) {
181 // Dispatch to this child, also activate it
184 // Set x and y relative to the child's coordinates
185 mouse
.setX(mouse
.getAbsoluteX() - widget
.getAbsoluteX());
186 mouse
.setY(mouse
.getAbsoluteY() - widget
.getAbsoluteY());
187 widget
.handleEvent(mouse
);
194 * Handle mouse button releases.
196 * @param mouse mouse button release event
199 public void onMouseUp(final TMouseEvent mouse
) {
203 for (TWidget widget
: getChildren()) {
204 if (widget
.mouseWouldHit(mouse
)) {
205 // Dispatch to this child, also activate it
208 // Set x and y relative to the child's coordinates
209 mouse
.setX(mouse
.getAbsoluteX() - widget
.getAbsoluteX());
210 mouse
.setY(mouse
.getAbsoluteY() - widget
.getAbsoluteY());
211 widget
.handleEvent(mouse
);
218 * Handle mouse movements.
220 * @param mouse mouse motion event
223 public void onMouseMotion(final TMouseEvent mouse
) {
226 // See if we should activate a different menu item
227 for (TWidget widget
: getChildren()) {
228 if ((mouse
.isMouse1())
229 && (widget
.mouseWouldHit(mouse
))
231 // Activate this menu item
233 if (widget
instanceof TSubMenu
) {
234 ((TSubMenu
) widget
).dispatch();
244 * @param keypress keystroke event
247 public void onKeypress(final TKeypressEvent keypress
) {
250 System.err.printf("keypress: %s active child: %s\n", keypress,
254 if (getActiveChild() != this) {
255 if ((getActiveChild() instanceof TSubMenu
)
256 || (getActiveChild() instanceof TMenu
)
258 getActiveChild().onKeypress(keypress
);
263 if (keypress
.equals(kbEsc
)) {
264 getApplication().closeMenu();
267 if (keypress
.equals(kbDown
)) {
271 if (keypress
.equals(kbUp
)) {
275 if (keypress
.equals(kbRight
)) {
276 getApplication().switchMenu(true);
279 if (keypress
.equals(kbLeft
)) {
281 getApplication().closeSubMenu();
283 getApplication().switchMenu(false);
288 // Switch to a menuItem if it has an mnemonic
289 if (!keypress
.getKey().isFnKey()
290 && !keypress
.getKey().isAlt()
291 && !keypress
.getKey().isCtrl()) {
292 for (TWidget widget
: getChildren()) {
293 TMenuItem item
= (TMenuItem
) widget
;
294 if ((item
.getMnemonic() != null)
295 && (Character
.toLowerCase(item
.getMnemonic().getShortcut())
296 == Character
.toLowerCase(keypress
.getKey().getChar()))
298 // Send an enter keystroke to it
300 item
.handleEvent(new TKeypressEvent(kbEnter
));
306 // Dispatch the keypress to an active widget
307 for (TWidget widget
: getChildren()) {
308 if (widget
.isActive()) {
309 widget
.handleEvent(keypress
);
316 * Convenience function to add a menu item.
318 * @param id menu item ID. Must be greater than 1024.
319 * @param label menu item label
320 * @return the new menu item
322 public TMenuItem
addItem(final int id
, final String label
) {
324 return addItemInternal(id
, label
, null);
328 * Convenience function to add a custom menu item.
330 * @param id menu item ID. Must be greater than 1024.
331 * @param label menu item label
332 * @param key global keyboard accelerator
333 * @return the new menu item
335 public TMenuItem
addItem(final int id
, final String label
,
336 final TKeypress key
) {
339 return addItemInternal(id
, label
, key
);
343 * Convenience function to add a custom menu item.
345 * @param id menu item ID. Must be greater than 1024.
346 * @param label menu item label
347 * @param key global keyboard accelerator
348 * @return the new menu item
350 private TMenuItem
addItemInternal(final int id
, final String label
,
351 final TKeypress key
) {
353 int newY
= getChildren().size() + 1;
354 assert (newY
< getHeight());
356 TMenuItem menuItem
= new TMenuItem(this, id
, 1, newY
, label
);
357 menuItem
.setKey(key
);
358 setHeight(getHeight() + 1);
359 if (menuItem
.getWidth() + 2 > getWidth()) {
360 setWidth(menuItem
.getWidth() + 2);
362 for (TWidget widget
: getChildren()) {
363 widget
.setWidth(getWidth() - 2);
365 getApplication().addMenuItem(menuItem
);
366 getApplication().recomputeMenuX();
372 * Convenience function to add one of the default menu items.
374 * @param id menu item ID. Must be between 0 (inclusive) and 1023
376 * @return the new menu item
378 public TMenuItem
addDefaultItem(final int id
) {
383 TKeypress key
= null;
425 label
= "Cl&ose All";
427 case MID_WINDOW_MOVE
:
428 label
= "&Size/Move";
431 case MID_WINDOW_ZOOM
:
435 case MID_WINDOW_NEXT
:
439 case MID_WINDOW_PREVIOUS
:
443 case MID_WINDOW_CLOSE
:
448 case MID_HELP_CONTENTS
:
455 case MID_HELP_SEARCH
:
456 label
= "&Topic search";
459 case MID_HELP_PREVIOUS
:
460 label
= "&Previous topic";
464 label
= "&Help on help";
466 case MID_HELP_ACTIVE_FILE
:
467 label
= "Active &file...";
474 throw new IllegalArgumentException("Invalid menu ID: " + id
);
477 return addItemInternal(id
, label
, key
);
481 * Convenience function to add a menu separator.
483 public void addSeparator() {
484 int newY
= getChildren().size() + 1;
485 assert (newY
< getHeight());
487 // We just have to construct it, don't need to hang onto what it
489 new TMenuSeparator(this, 1, newY
);
490 setHeight(getHeight() + 1);
494 * Convenience function to add a sub-menu.
496 * @param title menu title. Title must contain a keyboard shortcut,
497 * denoted by prefixing a letter with "&", e.g. "&File"
498 * @return the new sub-menu
500 public TSubMenu
addSubMenu(final String title
) {
501 int newY
= getChildren().size() + 1;
502 assert (newY
< getHeight());
504 TSubMenu subMenu
= new TSubMenu(this, title
, 1, newY
);
505 setHeight(getHeight() + 1);
506 if (subMenu
.getWidth() + 2 > getWidth()) {
507 setWidth(subMenu
.getWidth() + 2);
509 for (TWidget widget
: getChildren()) {
510 widget
.setWidth(getWidth() - 2);
512 getApplication().recomputeMenuX();
514 subMenu
.menu
.setX(getX() + getWidth() - 2);