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 // My parent constructor added me as a window, get rid of that
117 parent
.closeWindow(this);
119 // Setup the menu shortcut
120 mnemonic
= new MnemonicString(label
);
121 setTitle(mnemonic
.getRawLabel());
122 assert (mnemonic
.getShortcutIdx() >= 0);
124 // Recompute width and height to reflect an empty menu
125 setWidth(getTitle().length() + 4);
132 * Draw a top-level menu with title and menu items.
136 CellAttributes background
= getTheme().getColor("tmenu");
138 assert (isAbsoluteActive());
140 // Fill in the interior background
141 for (int i
= 0; i
< getHeight(); i
++) {
142 hLineXY(0, i
, getWidth(), ' ', background
);
152 cTopLeft
= GraphicsChars
.ULCORNER
;
153 cTopRight
= GraphicsChars
.URCORNER
;
154 cBottomLeft
= GraphicsChars
.LLCORNER
;
155 cBottomRight
= GraphicsChars
.LRCORNER
;
156 cHSide
= GraphicsChars
.SINGLE_BAR
;
158 // Place the corner characters
159 putCharXY(1, 0, cTopLeft
, background
);
160 putCharXY(getWidth() - 2, 0, cTopRight
, background
);
161 putCharXY(1, getHeight() - 1, cBottomLeft
, background
);
162 putCharXY(getWidth() - 2, getHeight() - 1, cBottomRight
, background
);
164 // Draw the box lines
165 hLineXY(1 + 1, 0, getWidth() - 4, cHSide
, background
);
166 hLineXY(1 + 1, getHeight() - 1, getWidth() - 4, cHSide
, background
);
169 getScreen().drawBoxShadow(0, 0, getWidth(), getHeight());
173 * Handle mouse button presses.
175 * @param mouse mouse button event
178 public void onMouseDown(final TMouseEvent mouse
) {
182 for (TWidget widget
: getChildren()) {
183 if (widget
.mouseWouldHit(mouse
)) {
184 // Dispatch to this child, also activate it
187 // Set x and y relative to the child's coordinates
188 mouse
.setX(mouse
.getAbsoluteX() - widget
.getAbsoluteX());
189 mouse
.setY(mouse
.getAbsoluteY() - widget
.getAbsoluteY());
190 widget
.handleEvent(mouse
);
197 * Handle mouse button releases.
199 * @param mouse mouse button release event
202 public void onMouseUp(final TMouseEvent mouse
) {
206 for (TWidget widget
: getChildren()) {
207 if (widget
.mouseWouldHit(mouse
)) {
208 // Dispatch to this child, also activate it
211 // Set x and y relative to the child's coordinates
212 mouse
.setX(mouse
.getAbsoluteX() - widget
.getAbsoluteX());
213 mouse
.setY(mouse
.getAbsoluteY() - widget
.getAbsoluteY());
214 widget
.handleEvent(mouse
);
221 * Handle mouse movements.
223 * @param mouse mouse motion event
226 public void onMouseMotion(final TMouseEvent mouse
) {
229 // See if we should activate a different menu item
230 for (TWidget widget
: getChildren()) {
231 if ((mouse
.isMouse1())
232 && (widget
.mouseWouldHit(mouse
))
234 // Activate this menu item
236 if (widget
instanceof TSubMenu
) {
237 ((TSubMenu
) widget
).dispatch();
247 * @param keypress keystroke event
250 public void onKeypress(final TKeypressEvent keypress
) {
253 System.err.printf("keypress: %s active child: %s\n", keypress,
257 if (getActiveChild() != this) {
258 if ((getActiveChild() instanceof TSubMenu
)
259 || (getActiveChild() instanceof TMenu
)
261 getActiveChild().onKeypress(keypress
);
266 if (keypress
.equals(kbEsc
)) {
267 getApplication().closeMenu();
270 if (keypress
.equals(kbDown
)) {
274 if (keypress
.equals(kbUp
)) {
278 if (keypress
.equals(kbRight
)) {
279 getApplication().switchMenu(true);
282 if (keypress
.equals(kbLeft
)) {
284 getApplication().closeSubMenu();
286 getApplication().switchMenu(false);
291 // Switch to a menuItem if it has an mnemonic
292 if (!keypress
.getKey().isFnKey()
293 && !keypress
.getKey().isAlt()
294 && !keypress
.getKey().isCtrl()) {
295 for (TWidget widget
: getChildren()) {
296 TMenuItem item
= (TMenuItem
) widget
;
297 if ((item
.getMnemonic() != null)
298 && (Character
.toLowerCase(item
.getMnemonic().getShortcut())
299 == Character
.toLowerCase(keypress
.getKey().getChar()))
301 // Send an enter keystroke to it
303 item
.handleEvent(new TKeypressEvent(kbEnter
));
309 // Dispatch the keypress to an active widget
310 for (TWidget widget
: getChildren()) {
311 if (widget
.isActive()) {
312 widget
.handleEvent(keypress
);
319 * Convenience function to add a menu item.
321 * @param id menu item ID. Must be greater than 1024.
322 * @param label menu item label
323 * @return the new menu item
325 public TMenuItem
addItem(final int id
, final String label
) {
327 return addItemInternal(id
, label
, null);
331 * Convenience function to add a custom menu item.
333 * @param id menu item ID. Must be greater than 1024.
334 * @param label menu item label
335 * @param key global keyboard accelerator
336 * @return the new menu item
338 public TMenuItem
addItem(final int id
, final String label
,
339 final TKeypress key
) {
342 return addItemInternal(id
, label
, key
);
346 * Convenience function to add a custom menu item.
348 * @param id menu item ID. Must be greater than 1024.
349 * @param label menu item label
350 * @param key global keyboard accelerator
351 * @return the new menu item
353 private TMenuItem
addItemInternal(final int id
, final String label
,
354 final TKeypress key
) {
356 int newY
= getChildren().size() + 1;
357 assert (newY
< getHeight());
359 TMenuItem menuItem
= new TMenuItem(this, id
, 1, newY
, label
);
360 menuItem
.setKey(key
);
361 setHeight(getHeight() + 1);
362 if (menuItem
.getWidth() + 2 > getWidth()) {
363 setWidth(menuItem
.getWidth() + 2);
365 for (TWidget widget
: getChildren()) {
366 widget
.setWidth(getWidth() - 2);
368 getApplication().addMenuItem(menuItem
);
369 getApplication().recomputeMenuX();
375 * Convenience function to add one of the default menu items.
377 * @param id menu item ID. Must be between 0 (inclusive) and 1023
379 * @return the new menu item
381 public TMenuItem
addDefaultItem(final int id
) {
386 TKeypress key
= null;
428 label
= "Cl&ose All";
430 case MID_WINDOW_MOVE
:
431 label
= "&Size/Move";
434 case MID_WINDOW_ZOOM
:
438 case MID_WINDOW_NEXT
:
442 case MID_WINDOW_PREVIOUS
:
446 case MID_WINDOW_CLOSE
:
451 case MID_HELP_CONTENTS
:
458 case MID_HELP_SEARCH
:
459 label
= "&Topic search";
462 case MID_HELP_PREVIOUS
:
463 label
= "&Previous topic";
467 label
= "&Help on help";
469 case MID_HELP_ACTIVE_FILE
:
470 label
= "Active &file...";
477 throw new IllegalArgumentException("Invalid menu ID: " + id
);
480 return addItemInternal(id
, label
, key
);
484 * Convenience function to add a menu separator.
486 public void addSeparator() {
487 int newY
= getChildren().size() + 1;
488 assert (newY
< getHeight());
490 // We just have to construct it, don't need to hang onto what it
492 new TMenuSeparator(this, 1, newY
);
493 setHeight(getHeight() + 1);
497 * Convenience function to add a sub-menu.
499 * @param title menu title. Title must contain a keyboard shortcut,
500 * denoted by prefixing a letter with "&", e.g. "&File"
501 * @return the new sub-menu
503 public TSubMenu
addSubMenu(final String title
) {
504 int newY
= getChildren().size() + 1;
505 assert (newY
< getHeight());
507 TSubMenu subMenu
= new TSubMenu(this, title
, 1, newY
);
508 setHeight(getHeight() + 1);
509 if (subMenu
.getWidth() + 2 > getWidth()) {
510 setWidth(subMenu
.getWidth() + 2);
512 for (TWidget widget
: getChildren()) {
513 widget
.setWidth(getWidth() - 2);
515 getApplication().recomputeMenuX();
517 subMenu
.menu
.setX(getX() + getWidth() - 2);