Commit | Line | Data |
---|---|---|
daa4106c | 1 | /* |
928811d8 KL |
2 | * Jexer - Java Text User Interface |
3 | * | |
e16dda65 | 4 | * The MIT License (MIT) |
928811d8 | 5 | * |
a2018e99 | 6 | * Copyright (C) 2017 Kevin Lamonte |
928811d8 | 7 | * |
e16dda65 KL |
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: | |
928811d8 | 14 | * |
e16dda65 KL |
15 | * The above copyright notice and this permission notice shall be included in |
16 | * all copies or substantial portions of the Software. | |
928811d8 | 17 | * |
e16dda65 KL |
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. | |
928811d8 KL |
25 | * |
26 | * @author Kevin Lamonte [kevin.lamonte@gmail.com] | |
27 | * @version 1 | |
28 | */ | |
29 | package jexer.menu; | |
30 | ||
31 | import jexer.TKeypress; | |
32 | import jexer.TWidget; | |
33 | import jexer.bits.CellAttributes; | |
34 | import jexer.bits.GraphicsChars; | |
35 | import jexer.bits.MnemonicString; | |
36 | import jexer.event.TKeypressEvent; | |
37 | import jexer.event.TMouseEvent; | |
38 | import jexer.event.TMenuEvent; | |
39 | import static jexer.TKeypress.*; | |
40 | ||
41 | /** | |
42 | * TMenuItem implements a menu item. | |
43 | */ | |
44 | public class TMenuItem extends TWidget { | |
45 | ||
d36057df KL |
46 | // ------------------------------------------------------------------------ |
47 | // Variables -------------------------------------------------------------- | |
48 | // ------------------------------------------------------------------------ | |
49 | ||
928811d8 KL |
50 | /** |
51 | * Label for this menu item. | |
52 | */ | |
53 | private String label; | |
54 | ||
55 | /** | |
56 | * Menu ID. IDs less than 1024 are reserved for common system | |
57 | * functions. Existing ones are defined in TMenu, i.e. TMenu.MID_EXIT. | |
58 | */ | |
59 | private int id = TMenu.MID_UNUSED; | |
60 | ||
61 | /** | |
62 | * When true, this item can be checked or unchecked. | |
63 | */ | |
64 | private boolean checkable = false; | |
65 | ||
66 | /** | |
67 | * When true, this item is checked. | |
68 | */ | |
69 | private boolean checked = false; | |
70 | ||
71 | /** | |
72 | * Global shortcut key. | |
73 | */ | |
74 | private TKeypress key; | |
75 | ||
928811d8 KL |
76 | /** |
77 | * The title string. Use '&' to specify a mnemonic, i.e. "&File" will | |
78 | * highlight the 'F' and allow 'f' or 'F' to select it. | |
79 | */ | |
80 | private MnemonicString mnemonic; | |
81 | ||
d36057df KL |
82 | // ------------------------------------------------------------------------ |
83 | // Constructors ----------------------------------------------------------- | |
84 | // ------------------------------------------------------------------------ | |
928811d8 KL |
85 | |
86 | /** | |
87 | * Package private constructor. | |
88 | * | |
89 | * @param parent parent widget | |
90 | * @param id menu id | |
91 | * @param x column relative to parent | |
92 | * @param y row relative to parent | |
93 | * @param label menu item title | |
94 | */ | |
95 | TMenuItem(final TMenu parent, final int id, final int x, final int y, | |
96 | final String label) { | |
97 | ||
98 | // Set parent and window | |
99 | super(parent); | |
100 | ||
101 | mnemonic = new MnemonicString(label); | |
102 | ||
103 | setX(x); | |
104 | setY(y); | |
105 | setHeight(1); | |
106 | this.label = mnemonic.getRawLabel(); | |
107 | setWidth(label.length() + 4); | |
108 | this.id = id; | |
109 | ||
110 | // Default state for some known menu items | |
111 | switch (id) { | |
112 | ||
113 | case TMenu.MID_CUT: | |
114 | setEnabled(false); | |
115 | break; | |
116 | case TMenu.MID_COPY: | |
117 | setEnabled(false); | |
118 | break; | |
119 | case TMenu.MID_PASTE: | |
120 | setEnabled(false); | |
121 | break; | |
122 | case TMenu.MID_CLEAR: | |
123 | setEnabled(false); | |
124 | break; | |
125 | ||
126 | case TMenu.MID_TILE: | |
127 | break; | |
128 | case TMenu.MID_CASCADE: | |
129 | break; | |
130 | case TMenu.MID_CLOSE_ALL: | |
131 | break; | |
132 | case TMenu.MID_WINDOW_MOVE: | |
133 | break; | |
134 | case TMenu.MID_WINDOW_ZOOM: | |
135 | break; | |
136 | case TMenu.MID_WINDOW_NEXT: | |
137 | break; | |
138 | case TMenu.MID_WINDOW_PREVIOUS: | |
139 | break; | |
140 | case TMenu.MID_WINDOW_CLOSE: | |
141 | break; | |
142 | default: | |
143 | break; | |
144 | } | |
145 | ||
146 | } | |
147 | ||
d36057df KL |
148 | // ------------------------------------------------------------------------ |
149 | // Event handlers --------------------------------------------------------- | |
150 | // ------------------------------------------------------------------------ | |
151 | ||
928811d8 KL |
152 | /** |
153 | * Returns true if the mouse is currently on the menu item. | |
154 | * | |
155 | * @param mouse mouse event | |
329fd62e | 156 | * @return if true then the mouse is currently on this item |
928811d8 KL |
157 | */ |
158 | private boolean mouseOnMenuItem(final TMouseEvent mouse) { | |
159 | if ((mouse.getY() == 0) | |
160 | && (mouse.getX() >= 0) | |
161 | && (mouse.getX() < getWidth()) | |
162 | ) { | |
163 | return true; | |
164 | } | |
165 | return false; | |
166 | } | |
167 | ||
d36057df KL |
168 | /** |
169 | * Handle mouse button releases. | |
170 | * | |
171 | * @param mouse mouse button release event | |
172 | */ | |
173 | @Override | |
174 | public void onMouseUp(final TMouseEvent mouse) { | |
175 | if ((mouseOnMenuItem(mouse)) && (mouse.isMouse1())) { | |
176 | dispatch(); | |
177 | return; | |
178 | } | |
179 | } | |
180 | ||
181 | /** | |
182 | * Handle keystrokes. | |
183 | * | |
184 | * @param keypress keystroke event | |
185 | */ | |
186 | @Override | |
187 | public void onKeypress(final TKeypressEvent keypress) { | |
188 | if (keypress.equals(kbEnter)) { | |
189 | dispatch(); | |
190 | return; | |
191 | } | |
192 | ||
193 | // Pass to parent for the things we don't care about. | |
194 | super.onKeypress(keypress); | |
195 | } | |
196 | ||
197 | // ------------------------------------------------------------------------ | |
198 | // TWidget ---------------------------------------------------------------- | |
199 | // ------------------------------------------------------------------------ | |
200 | ||
928811d8 KL |
201 | /** |
202 | * Draw a menu item with label. | |
203 | */ | |
204 | @Override | |
205 | public void draw() { | |
206 | CellAttributes background = getTheme().getColor("tmenu"); | |
207 | CellAttributes menuColor; | |
208 | CellAttributes menuMnemonicColor; | |
7c870d89 | 209 | if (isAbsoluteActive()) { |
928811d8 KL |
210 | menuColor = getTheme().getColor("tmenu.highlighted"); |
211 | menuMnemonicColor = getTheme().getColor("tmenu.mnemonic.highlighted"); | |
212 | } else { | |
7c870d89 | 213 | if (isEnabled()) { |
928811d8 KL |
214 | menuColor = getTheme().getColor("tmenu"); |
215 | menuMnemonicColor = getTheme().getColor("tmenu.mnemonic"); | |
216 | } else { | |
217 | menuColor = getTheme().getColor("tmenu.disabled"); | |
218 | menuMnemonicColor = getTheme().getColor("tmenu.disabled"); | |
219 | } | |
220 | } | |
221 | ||
222 | char cVSide = GraphicsChars.WINDOW_SIDE; | |
223 | getScreen().vLineXY(0, 0, 1, cVSide, background); | |
224 | getScreen().vLineXY(getWidth() - 1, 0, 1, cVSide, background); | |
225 | ||
226 | getScreen().hLineXY(1, 0, getWidth() - 2, ' ', menuColor); | |
0d47c546 | 227 | getScreen().putStringXY(2, 0, mnemonic.getRawLabel(), menuColor); |
efb7af1f | 228 | if (key != null) { |
928811d8 | 229 | String keyLabel = key.toString(); |
0d47c546 | 230 | getScreen().putStringXY((getWidth() - keyLabel.length() - 2), 0, |
928811d8 KL |
231 | keyLabel, menuColor); |
232 | } | |
233 | if (mnemonic.getShortcutIdx() >= 0) { | |
234 | getScreen().putCharXY(2 + mnemonic.getShortcutIdx(), 0, | |
235 | mnemonic.getShortcut(), menuMnemonicColor); | |
236 | } | |
237 | if (checked) { | |
238 | assert (checkable); | |
239 | getScreen().putCharXY(1, 0, GraphicsChars.CHECK, menuColor); | |
240 | } | |
241 | ||
242 | } | |
243 | ||
d36057df KL |
244 | // ------------------------------------------------------------------------ |
245 | // TMenuItem -------------------------------------------------------------- | |
246 | // ------------------------------------------------------------------------ | |
247 | ||
928811d8 | 248 | /** |
d36057df KL |
249 | * Get the menu item ID. |
250 | * | |
251 | * @return the id | |
928811d8 | 252 | */ |
d36057df KL |
253 | public final int getId() { |
254 | return id; | |
255 | } | |
928811d8 | 256 | |
d36057df KL |
257 | /** |
258 | * Set checkable flag. | |
259 | * | |
260 | * @param checkable if true, this menu item can be checked/unchecked | |
261 | */ | |
262 | public final void setCheckable(final boolean checkable) { | |
263 | this.checkable = checkable; | |
928811d8 KL |
264 | } |
265 | ||
928811d8 | 266 | /** |
d36057df | 267 | * Get the mnemonic string for this menu item. |
928811d8 | 268 | * |
d36057df | 269 | * @return mnemonic string |
928811d8 | 270 | */ |
d36057df KL |
271 | public final MnemonicString getMnemonic() { |
272 | return mnemonic; | |
928811d8 KL |
273 | } |
274 | ||
275 | /** | |
d36057df | 276 | * Get a global accelerator key for this menu item. |
928811d8 | 277 | * |
d36057df KL |
278 | * @return global keyboard accelerator, or null if no key is associated |
279 | * with this item | |
928811d8 | 280 | */ |
d36057df KL |
281 | public final TKeypress getKey() { |
282 | return key; | |
283 | } | |
284 | ||
285 | /** | |
286 | * Set a global accelerator key for this menu item. | |
287 | * | |
288 | * @param key global keyboard accelerator | |
289 | */ | |
290 | public final void setKey(final TKeypress key) { | |
291 | this.key = key; | |
292 | ||
293 | if (key != null) { | |
294 | int newWidth = (label.length() + 4 + key.toString().length() + 2); | |
295 | if (newWidth > getWidth()) { | |
296 | setWidth(newWidth); | |
297 | } | |
928811d8 | 298 | } |
d36057df | 299 | } |
928811d8 | 300 | |
d36057df KL |
301 | /** |
302 | * Dispatch event(s) due to selection or click. | |
303 | */ | |
304 | public void dispatch() { | |
305 | assert (isEnabled()); | |
306 | ||
307 | getApplication().postMenuEvent(new TMenuEvent(id)); | |
308 | if (checkable) { | |
309 | checked = !checked; | |
310 | } | |
928811d8 | 311 | } |
d36057df | 312 | |
928811d8 | 313 | } |