Commit | Line | Data |
---|---|---|
928811d8 KL |
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.TWidget; | |
34 | import jexer.bits.CellAttributes; | |
35 | import jexer.bits.GraphicsChars; | |
36 | import jexer.event.TKeypressEvent; | |
37 | import static jexer.TKeypress.*; | |
38 | ||
39 | /** | |
40 | * TSubMenu is a special case menu item that wraps another TMenu. | |
41 | */ | |
42 | public class TSubMenu extends TMenuItem { | |
43 | ||
44 | /** | |
45 | * The menu window. Note package private access. | |
46 | */ | |
47 | TMenu menu; | |
48 | ||
49 | /** | |
50 | * Package private constructor. | |
51 | * | |
52 | * @param parent parent widget | |
53 | * @param title menu title. Title must contain a keyboard shortcut, | |
54 | * denoted by prefixing a letter with "&", e.g. "&File" | |
55 | * @param x column relative to parent | |
56 | * @param y row relative to parent | |
57 | */ | |
58 | TSubMenu(final TMenu parent, final String title, final int x, final int y) { | |
59 | super(parent, TMenu.MID_UNUSED, x, y, title); | |
60 | ||
61 | setActive(false); | |
62 | setEnabled(true); | |
63 | ||
64 | this.menu = new TMenu(parent.getApplication(), x, getAbsoluteY() - 1, | |
65 | title); | |
66 | setWidth(menu.getWidth() + 2); | |
67 | ||
68 | this.menu.isSubMenu = true; | |
69 | } | |
70 | ||
71 | /** | |
72 | * Draw the menu title. | |
73 | */ | |
74 | @Override | |
75 | public void draw() { | |
76 | super.draw(); | |
77 | ||
78 | CellAttributes background = getTheme().getColor("tmenu"); | |
79 | CellAttributes menuColor; | |
80 | CellAttributes menuMnemonicColor; | |
81 | if (getAbsoluteActive()) { | |
82 | menuColor = getTheme().getColor("tmenu.highlighted"); | |
83 | menuMnemonicColor = getTheme().getColor("tmenu.mnemonic.highlighted"); | |
84 | } else { | |
85 | if (getEnabled()) { | |
86 | menuColor = getTheme().getColor("tmenu"); | |
87 | menuMnemonicColor = getTheme().getColor("tmenu.mnemonic"); | |
88 | } else { | |
89 | menuColor = getTheme().getColor("tmenu.disabled"); | |
90 | menuMnemonicColor = getTheme().getColor("tmenu.disabled"); | |
91 | } | |
92 | } | |
93 | ||
94 | // Add the arrow | |
95 | getScreen().putCharXY(getWidth() - 2, 0, GraphicsChars.CP437[0x10], | |
96 | menuColor); | |
97 | } | |
98 | ||
99 | /** | |
100 | * Handle keystrokes. | |
101 | * | |
102 | * @param keypress keystroke event | |
103 | */ | |
104 | @Override | |
105 | public void onKeypress(final TKeypressEvent keypress) { | |
106 | ||
107 | if (menu.getActive()) { | |
108 | menu.onKeypress(keypress); | |
109 | return; | |
110 | } | |
111 | ||
112 | if (keypress.equals(kbEnter)) { | |
113 | dispatch(); | |
114 | return; | |
115 | } | |
116 | ||
117 | if (keypress.equals(kbRight)) { | |
118 | dispatch(); | |
119 | return; | |
120 | } | |
121 | ||
122 | if (keypress.equals(kbDown)) { | |
123 | getParent().switchWidget(true); | |
124 | return; | |
125 | } | |
126 | ||
127 | if (keypress.equals(kbUp)) { | |
128 | getParent().switchWidget(false); | |
129 | return; | |
130 | } | |
131 | ||
132 | if (keypress.equals(kbLeft)) { | |
133 | TMenu parentMenu = (TMenu) getParent(); | |
134 | if (parentMenu.isSubMenu) { | |
135 | getApplication().closeSubMenu(); | |
136 | } else { | |
137 | getApplication().switchMenu(false); | |
138 | } | |
139 | return; | |
140 | } | |
141 | ||
142 | if (keypress.equals(kbEsc)) { | |
143 | getApplication().closeMenu(); | |
144 | return; | |
145 | } | |
146 | } | |
147 | ||
148 | /** | |
149 | * Override dispatch() to do nothing. | |
150 | */ | |
151 | @Override | |
152 | public void dispatch() { | |
153 | assert (getEnabled()); | |
154 | if (getAbsoluteActive()) { | |
155 | if (!menu.getActive()) { | |
156 | getApplication().addSubMenu(menu); | |
157 | menu.setActive(true); | |
158 | } | |
159 | } | |
160 | } | |
161 | ||
162 | /** | |
163 | * Returns my active widget. | |
164 | * | |
165 | * @return widget that is active, or this if no children | |
166 | */ | |
167 | @Override | |
168 | public TWidget getActiveChild() { | |
169 | if (menu.getActive()) { | |
170 | return menu; | |
171 | } | |
172 | // Menu not active, return me | |
173 | return this; | |
174 | } | |
175 | ||
176 | } |