9d415d5559322d5871bc9d4e6445eb7177efd190
[nikiroo-utils.git] / src / jexer / menu / TSubMenu.java
1 /*
2 * Jexer - Java Text User Interface
3 *
4 * License: LGPLv3 or later
5 *
6 * This module is licensed under the GNU Lesser General Public License
7 * Version 3. Please see the file "COPYING" in this directory for more
8 * information about the GNU Lesser General Public License Version 3.
9 *
10 * Copyright (C) 2015 Kevin Lamonte
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU Lesser General Public License
14 * as published by the Free Software Foundation; either version 3 of
15 * the License, or (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
21 *
22 * You should have received a copy of the GNU Lesser General Public
23 * License along with this program; if not, see
24 * http://www.gnu.org/licenses/, or write to the Free Software
25 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
26 * 02110-1301 USA
27 *
28 * @author Kevin Lamonte [kevin.lamonte@gmail.com]
29 * @version 1
30 */
31 package jexer.menu;
32
33 import jexer.TKeypress;
34 import jexer.TWidget;
35 import jexer.bits.CellAttributes;
36 import jexer.bits.GraphicsChars;
37 import jexer.event.TKeypressEvent;
38 import static jexer.TKeypress.*;
39
40 /**
41 * TSubMenu is a special case menu item that wraps another TMenu.
42 */
43 public final class TSubMenu extends TMenuItem {
44
45 /**
46 * The menu window. Note package private access.
47 */
48 TMenu menu;
49
50 /**
51 * Package private constructor.
52 *
53 * @param parent parent widget
54 * @param title menu title. Title must contain a keyboard shortcut,
55 * denoted by prefixing a letter with "&", e.g. "&File"
56 * @param x column relative to parent
57 * @param y row relative to parent
58 */
59 TSubMenu(final TMenu parent, final String title, final int x, final int y) {
60 super(parent, TMenu.MID_UNUSED, x, y, title);
61
62 setActive(false);
63 setEnabled(true);
64
65 this.menu = new TMenu(parent.getApplication(), x, getAbsoluteY() - 1,
66 title);
67 setWidth(menu.getWidth() + 2);
68
69 this.menu.isSubMenu = true;
70 }
71
72 /**
73 * Draw the menu title.
74 */
75 @Override
76 public void draw() {
77 super.draw();
78
79 CellAttributes menuColor;
80 if (isAbsoluteActive()) {
81 menuColor = getTheme().getColor("tmenu.highlighted");
82 } else {
83 if (isEnabled()) {
84 menuColor = getTheme().getColor("tmenu");
85 } else {
86 menuColor = getTheme().getColor("tmenu.disabled");
87 }
88 }
89
90 // Add the arrow
91 getScreen().putCharXY(getWidth() - 2, 0, GraphicsChars.CP437[0x10],
92 menuColor);
93 }
94
95 /**
96 * Handle keystrokes.
97 *
98 * @param keypress keystroke event
99 */
100 @Override
101 public void onKeypress(final TKeypressEvent keypress) {
102
103 // Open me if they hit my mnemonic.
104 if (!keypress.getKey().isFnKey()
105 && !keypress.getKey().isAlt()
106 && !keypress.getKey().isCtrl()
107 && (getMnemonic() != null)
108 && (Character.toLowerCase(getMnemonic().getShortcut())
109 == Character.toLowerCase(keypress.getKey().getChar()))
110 ) {
111 dispatch();
112 return;
113 }
114
115 if (menu.isActive()) {
116 menu.onKeypress(keypress);
117 return;
118 }
119
120 if (keypress.equals(kbEnter)) {
121 dispatch();
122 return;
123 }
124
125 if (keypress.equals(kbRight)) {
126 dispatch();
127 return;
128 }
129
130 if (keypress.equals(kbDown)) {
131 getParent().switchWidget(true);
132 return;
133 }
134
135 if (keypress.equals(kbUp)) {
136 getParent().switchWidget(false);
137 return;
138 }
139
140 if (keypress.equals(kbLeft)) {
141 TMenu parentMenu = (TMenu) getParent();
142 if (parentMenu.isSubMenu) {
143 getApplication().closeSubMenu();
144 } else {
145 getApplication().switchMenu(false);
146 }
147 return;
148 }
149
150 if (keypress.equals(kbEsc)) {
151 getApplication().closeMenu();
152 return;
153 }
154 }
155
156 /**
157 * Override dispatch() to do nothing.
158 */
159 @Override
160 public void dispatch() {
161 assert (isEnabled());
162 if (isAbsoluteActive()) {
163 if (!menu.isActive()) {
164 getApplication().addSubMenu(menu);
165 menu.setActive(true);
166 }
167 }
168 }
169
170 /**
171 * Returns my active widget.
172 *
173 * @return widget that is active, or this if no children
174 */
175 @Override
176 public TWidget getActiveChild() {
177 if (menu.isActive()) {
178 return menu;
179 }
180 // Menu not active, return me
181 return this;
182 }
183
184 /**
185 * Convenience function to add a custom menu item.
186 *
187 * @param id menu item ID. Must be greater than 1024.
188 * @param label menu item label
189 * @param key global keyboard accelerator
190 * @return the new menu item
191 */
192 public TMenuItem addItem(final int id, final String label,
193 final TKeypress key) {
194
195 return menu.addItem(id, label, key);
196 }
197
198 /**
199 * Convenience function to add a menu item.
200 *
201 * @param id menu item ID. Must be greater than 1024.
202 * @param label menu item label
203 * @return the new menu item
204 */
205 public TMenuItem addItem(final int id, final String label) {
206 return menu.addItem(id, label);
207 }
208
209 /**
210 * Convenience function to add one of the default menu items.
211 *
212 * @param id menu item ID. Must be between 0 (inclusive) and 1023
213 * (inclusive).
214 * @return the new menu item
215 */
216 public TMenuItem addDefaultItem(final int id) {
217 return menu.addDefaultItem(id);
218 }
219
220 /**
221 * Convenience function to add a menu separator.
222 */
223 public void addSeparator() {
224 menu.addSeparator();
225 }
226
227 /**
228 * Convenience function to add a sub-menu.
229 *
230 * @param title menu title. Title must contain a keyboard shortcut,
231 * denoted by prefixing a letter with "&", e.g. "&File"
232 * @return the new sub-menu
233 */
234 public TSubMenu addSubMenu(final String title) {
235 return menu.addSubMenu(title);
236 }
237
238
239 }