Commit | Line | Data |
---|---|---|
daa4106c | 1 | /* |
928811d8 KL |
2 | * Jexer - Java Text User Interface |
3 | * | |
e16dda65 | 4 | * The MIT License (MIT) |
928811d8 | 5 | * |
a69ed767 | 6 | * Copyright (C) 2019 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; | |
a69ed767 KL |
223 | vLineXY(0, 0, 1, cVSide, background); |
224 | vLineXY(getWidth() - 1, 0, 1, cVSide, background); | |
928811d8 | 225 | |
a69ed767 KL |
226 | hLineXY(1, 0, getWidth() - 2, ' ', menuColor); |
227 | putStringXY(2, 0, mnemonic.getRawLabel(), menuColor); | |
efb7af1f | 228 | if (key != null) { |
928811d8 | 229 | String keyLabel = key.toString(); |
a69ed767 KL |
230 | putStringXY((getWidth() - keyLabel.length() - 2), 0, keyLabel, |
231 | menuColor); | |
928811d8 KL |
232 | } |
233 | if (mnemonic.getShortcutIdx() >= 0) { | |
a69ed767 KL |
234 | putCharXY(2 + mnemonic.getShortcutIdx(), 0, mnemonic.getShortcut(), |
235 | menuMnemonicColor); | |
928811d8 KL |
236 | } |
237 | if (checked) { | |
238 | assert (checkable); | |
a69ed767 | 239 | putCharXY(1, 0, GraphicsChars.CHECK, menuColor); |
928811d8 KL |
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 | ||
77961919 KL |
266 | /** |
267 | * Get checkable flag. | |
268 | * | |
269 | * @return true if this menu item is both checkable and checked | |
270 | */ | |
271 | public final boolean getChecked() { | |
272 | return ((checkable == true) && (checked == true)); | |
273 | } | |
274 | ||
275 | /** | |
276 | * Set checked flag. Note that setting checked on an item checkable will | |
277 | * do nothing. | |
278 | * | |
279 | * @param checked if true, and if this menu item is checkable, then | |
280 | * getChecked() will return true | |
281 | */ | |
282 | public final void setChecked(final boolean checked) { | |
283 | if (checkable) { | |
284 | this.checked = checked; | |
285 | } else { | |
286 | this.checked = false; | |
287 | } | |
288 | } | |
289 | ||
928811d8 | 290 | /** |
d36057df | 291 | * Get the mnemonic string for this menu item. |
928811d8 | 292 | * |
d36057df | 293 | * @return mnemonic string |
928811d8 | 294 | */ |
d36057df KL |
295 | public final MnemonicString getMnemonic() { |
296 | return mnemonic; | |
928811d8 KL |
297 | } |
298 | ||
299 | /** | |
d36057df | 300 | * Get a global accelerator key for this menu item. |
928811d8 | 301 | * |
d36057df KL |
302 | * @return global keyboard accelerator, or null if no key is associated |
303 | * with this item | |
928811d8 | 304 | */ |
d36057df KL |
305 | public final TKeypress getKey() { |
306 | return key; | |
307 | } | |
308 | ||
309 | /** | |
310 | * Set a global accelerator key for this menu item. | |
311 | * | |
312 | * @param key global keyboard accelerator | |
313 | */ | |
314 | public final void setKey(final TKeypress key) { | |
315 | this.key = key; | |
316 | ||
317 | if (key != null) { | |
318 | int newWidth = (label.length() + 4 + key.toString().length() + 2); | |
319 | if (newWidth > getWidth()) { | |
320 | setWidth(newWidth); | |
321 | } | |
928811d8 | 322 | } |
d36057df | 323 | } |
928811d8 | 324 | |
d36057df KL |
325 | /** |
326 | * Dispatch event(s) due to selection or click. | |
327 | */ | |
328 | public void dispatch() { | |
329 | assert (isEnabled()); | |
330 | ||
331 | getApplication().postMenuEvent(new TMenuEvent(id)); | |
332 | if (checkable) { | |
333 | checked = !checked; | |
334 | } | |
928811d8 | 335 | } |
d36057df | 336 | |
928811d8 | 337 | } |