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 | ||
339652cc KL |
31 | import java.util.ResourceBundle; |
32 | ||
928811d8 KL |
33 | import jexer.TApplication; |
34 | import jexer.TKeypress; | |
35 | import jexer.TWidget; | |
36 | import jexer.TWindow; | |
37 | import jexer.bits.CellAttributes; | |
38 | import jexer.bits.GraphicsChars; | |
39 | import jexer.bits.MnemonicString; | |
9f613a0c | 40 | import jexer.bits.StringUtils; |
928811d8 KL |
41 | import jexer.event.TKeypressEvent; |
42 | import jexer.event.TMouseEvent; | |
43 | import static jexer.TKeypress.*; | |
44 | ||
45 | /** | |
46 | * TMenu is a top-level collection of TMenuItems. | |
47 | */ | |
051e2913 | 48 | public class TMenu extends TWindow { |
928811d8 | 49 | |
339652cc KL |
50 | /** |
51 | * Translated strings. | |
52 | */ | |
53 | private static final ResourceBundle i18n = ResourceBundle.getBundle(TMenu.class.getName()); | |
54 | ||
d36057df KL |
55 | // ------------------------------------------------------------------------ |
56 | // Constants -------------------------------------------------------------- | |
57 | // ------------------------------------------------------------------------ | |
928811d8 KL |
58 | |
59 | // Reserved menu item IDs | |
60 | public static final int MID_UNUSED = -1; | |
61 | ||
e23ea538 KL |
62 | // Tools menu |
63 | public static final int MID_REPAINT = 1; | |
64 | public static final int MID_VIEW_IMAGE = 2; | |
a75902fa | 65 | public static final int MID_SCREEN_OPTIONS = 3; |
e23ea538 | 66 | |
928811d8 | 67 | // File menu |
e23ea538 KL |
68 | public static final int MID_NEW = 10; |
69 | public static final int MID_EXIT = 11; | |
928811d8 | 70 | public static final int MID_QUIT = MID_EXIT; |
e23ea538 KL |
71 | public static final int MID_OPEN_FILE = 12; |
72 | public static final int MID_SHELL = 13; | |
928811d8 KL |
73 | |
74 | // Edit menu | |
e23ea538 KL |
75 | public static final int MID_CUT = 20; |
76 | public static final int MID_COPY = 21; | |
77 | public static final int MID_PASTE = 22; | |
78 | public static final int MID_CLEAR = 23; | |
928811d8 | 79 | |
d36057df | 80 | // Search menu |
e23ea538 KL |
81 | public static final int MID_FIND = 30; |
82 | public static final int MID_REPLACE = 31; | |
83 | public static final int MID_SEARCH_AGAIN = 32; | |
84 | public static final int MID_GOTO_LINE = 33; | |
d36057df | 85 | |
928811d8 | 86 | // Window menu |
e23ea538 KL |
87 | public static final int MID_TILE = 40; |
88 | public static final int MID_CASCADE = 41; | |
89 | public static final int MID_CLOSE_ALL = 42; | |
90 | public static final int MID_WINDOW_MOVE = 43; | |
91 | public static final int MID_WINDOW_ZOOM = 44; | |
92 | public static final int MID_WINDOW_NEXT = 45; | |
93 | public static final int MID_WINDOW_PREVIOUS = 46; | |
94 | public static final int MID_WINDOW_CLOSE = 47; | |
928811d8 | 95 | |
55d2b2c2 | 96 | // Help menu |
e23ea538 KL |
97 | public static final int MID_HELP_CONTENTS = 50; |
98 | public static final int MID_HELP_INDEX = 51; | |
99 | public static final int MID_HELP_SEARCH = 52; | |
100 | public static final int MID_HELP_PREVIOUS = 53; | |
101 | public static final int MID_HELP_HELP = 54; | |
102 | public static final int MID_HELP_ACTIVE_FILE = 55; | |
103 | public static final int MID_ABOUT = 56; | |
1c19fdea | 104 | |
1dac6b8d | 105 | // Table menu |
e9bb3c1e KL |
106 | public static final int MID_TABLE_RENAME_ROW = 60; |
107 | public static final int MID_TABLE_RENAME_COLUMN = 61; | |
108 | public static final int MID_TABLE_VIEW_ROW_LABELS = 70; | |
109 | public static final int MID_TABLE_VIEW_COLUMN_LABELS = 71; | |
110 | public static final int MID_TABLE_VIEW_HIGHLIGHT_ROW = 72; | |
111 | public static final int MID_TABLE_VIEW_HIGHLIGHT_COLUMN = 73; | |
112 | public static final int MID_TABLE_BORDER_NONE = 80; | |
113 | public static final int MID_TABLE_BORDER_ALL = 81; | |
114 | public static final int MID_TABLE_BORDER_CELL_NONE = 82; | |
115 | public static final int MID_TABLE_BORDER_CELL_ALL = 83; | |
116 | public static final int MID_TABLE_BORDER_RIGHT = 84; | |
117 | public static final int MID_TABLE_BORDER_LEFT = 85; | |
118 | public static final int MID_TABLE_BORDER_TOP = 86; | |
119 | public static final int MID_TABLE_BORDER_BOTTOM = 87; | |
120 | public static final int MID_TABLE_BORDER_DOUBLE_BOTTOM = 88; | |
121 | public static final int MID_TABLE_BORDER_THICK_BOTTOM = 89; | |
122 | public static final int MID_TABLE_DELETE_LEFT = 100; | |
123 | public static final int MID_TABLE_DELETE_UP = 101; | |
124 | public static final int MID_TABLE_DELETE_ROW = 102; | |
125 | public static final int MID_TABLE_DELETE_COLUMN = 103; | |
126 | public static final int MID_TABLE_INSERT_LEFT = 104; | |
127 | public static final int MID_TABLE_INSERT_RIGHT = 105; | |
128 | public static final int MID_TABLE_INSERT_ABOVE = 106; | |
129 | public static final int MID_TABLE_INSERT_BELOW = 107; | |
130 | public static final int MID_TABLE_COLUMN_NARROW = 110; | |
131 | public static final int MID_TABLE_COLUMN_WIDEN = 111; | |
f528c340 KL |
132 | public static final int MID_TABLE_FILE_OPEN_CSV = 115; |
133 | public static final int MID_TABLE_FILE_SAVE_CSV = 116; | |
134 | public static final int MID_TABLE_FILE_SAVE_TEXT = 117; | |
1dac6b8d | 135 | |
d36057df KL |
136 | // ------------------------------------------------------------------------ |
137 | // Variables -------------------------------------------------------------- | |
138 | // ------------------------------------------------------------------------ | |
139 | ||
140 | /** | |
141 | * If true, this is a sub-menu. Note package private access. | |
142 | */ | |
143 | boolean isSubMenu = false; | |
144 | ||
145 | /** | |
146 | * The X position of the menu's title. | |
147 | */ | |
148 | private int titleX; | |
149 | ||
150 | /** | |
151 | * The shortcut and title. | |
152 | */ | |
153 | private MnemonicString mnemonic; | |
154 | ||
84164e2c KL |
155 | /** |
156 | * If true, draw icons with menu items. Note package private access. | |
157 | */ | |
158 | boolean useIcons = false; | |
159 | ||
d36057df KL |
160 | // ------------------------------------------------------------------------ |
161 | // Constructors ----------------------------------------------------------- | |
162 | // ------------------------------------------------------------------------ | |
163 | ||
928811d8 KL |
164 | /** |
165 | * Public constructor. | |
166 | * | |
167 | * @param parent parent application | |
168 | * @param x column relative to parent | |
169 | * @param y row relative to parent | |
170 | * @param label mnemonic menu title. Label must contain a keyboard | |
43ad7b6c KL |
171 | * shortcut (mnemonic), denoted by prefixing a letter with "&", |
172 | * e.g. "&File" | |
928811d8 KL |
173 | */ |
174 | public TMenu(final TApplication parent, final int x, final int y, | |
175 | final String label) { | |
176 | ||
177 | super(parent, label, x, y, parent.getScreen().getWidth(), | |
178 | parent.getScreen().getHeight()); | |
179 | ||
928811d8 KL |
180 | // Setup the menu shortcut |
181 | mnemonic = new MnemonicString(label); | |
182 | setTitle(mnemonic.getRawLabel()); | |
183 | assert (mnemonic.getShortcutIdx() >= 0); | |
184 | ||
185 | // Recompute width and height to reflect an empty menu | |
9f613a0c | 186 | setWidth(StringUtils.width(getTitle()) + 4); |
928811d8 KL |
187 | setHeight(2); |
188 | ||
189 | setActive(false); | |
84164e2c KL |
190 | |
191 | if (System.getProperty("jexer.menuIcons", "false").equals("true")) { | |
192 | useIcons = true; | |
193 | } | |
194 | ||
928811d8 KL |
195 | } |
196 | ||
d36057df KL |
197 | // ------------------------------------------------------------------------ |
198 | // Event handlers --------------------------------------------------------- | |
199 | // ------------------------------------------------------------------------ | |
928811d8 KL |
200 | |
201 | /** | |
202 | * Handle mouse button presses. | |
203 | * | |
204 | * @param mouse mouse button event | |
205 | */ | |
206 | @Override | |
207 | public void onMouseDown(final TMouseEvent mouse) { | |
208 | this.mouse = mouse; | |
a69ed767 | 209 | super.onMouseDown(mouse); |
928811d8 KL |
210 | |
211 | // Pass to children | |
212 | for (TWidget widget: getChildren()) { | |
213 | if (widget.mouseWouldHit(mouse)) { | |
214 | // Dispatch to this child, also activate it | |
215 | activate(widget); | |
216 | ||
217 | // Set x and y relative to the child's coordinates | |
218 | mouse.setX(mouse.getAbsoluteX() - widget.getAbsoluteX()); | |
219 | mouse.setY(mouse.getAbsoluteY() - widget.getAbsoluteY()); | |
220 | widget.handleEvent(mouse); | |
221 | return; | |
222 | } | |
223 | } | |
224 | } | |
225 | ||
226 | /** | |
227 | * Handle mouse button releases. | |
228 | * | |
229 | * @param mouse mouse button release event | |
230 | */ | |
231 | @Override | |
232 | public void onMouseUp(final TMouseEvent mouse) { | |
233 | this.mouse = mouse; | |
928811d8 KL |
234 | |
235 | // Pass to children | |
236 | for (TWidget widget: getChildren()) { | |
237 | if (widget.mouseWouldHit(mouse)) { | |
238 | // Dispatch to this child, also activate it | |
239 | activate(widget); | |
240 | ||
241 | // Set x and y relative to the child's coordinates | |
242 | mouse.setX(mouse.getAbsoluteX() - widget.getAbsoluteX()); | |
243 | mouse.setY(mouse.getAbsoluteY() - widget.getAbsoluteY()); | |
244 | widget.handleEvent(mouse); | |
245 | return; | |
246 | } | |
247 | } | |
248 | } | |
249 | ||
250 | /** | |
251 | * Handle mouse movements. | |
252 | * | |
253 | * @param mouse mouse motion event | |
254 | */ | |
255 | @Override | |
256 | public void onMouseMotion(final TMouseEvent mouse) { | |
257 | this.mouse = mouse; | |
928811d8 KL |
258 | |
259 | // See if we should activate a different menu item | |
260 | for (TWidget widget: getChildren()) { | |
7c870d89 | 261 | if ((mouse.isMouse1()) |
928811d8 KL |
262 | && (widget.mouseWouldHit(mouse)) |
263 | ) { | |
264 | // Activate this menu item | |
265 | activate(widget); | |
266 | if (widget instanceof TSubMenu) { | |
267 | ((TSubMenu) widget).dispatch(); | |
268 | } | |
269 | return; | |
270 | } | |
271 | } | |
272 | } | |
273 | ||
274 | /** | |
275 | * Handle keystrokes. | |
276 | * | |
277 | * @param keypress keystroke event | |
278 | */ | |
279 | @Override | |
280 | public void onKeypress(final TKeypressEvent keypress) { | |
91c9a837 KL |
281 | |
282 | /* | |
283 | System.err.printf("keypress: %s active child: %s\n", keypress, | |
284 | getActiveChild()); | |
382bc294 | 285 | */ |
91c9a837 KL |
286 | |
287 | if (getActiveChild() != this) { | |
382bc294 | 288 | if (getActiveChild() instanceof TMenu) { |
928811d8 KL |
289 | getActiveChild().onKeypress(keypress); |
290 | return; | |
291 | } | |
382bc294 KL |
292 | |
293 | if (getActiveChild() instanceof TSubMenu) { | |
294 | TSubMenu subMenu = (TSubMenu) getActiveChild(); | |
295 | if (subMenu.menu.isActive()) { | |
296 | subMenu.onKeypress(keypress); | |
297 | return; | |
298 | } | |
299 | } | |
928811d8 KL |
300 | } |
301 | ||
302 | if (keypress.equals(kbEsc)) { | |
303 | getApplication().closeMenu(); | |
304 | return; | |
305 | } | |
306 | if (keypress.equals(kbDown)) { | |
307 | switchWidget(true); | |
308 | return; | |
309 | } | |
310 | if (keypress.equals(kbUp)) { | |
311 | switchWidget(false); | |
312 | return; | |
313 | } | |
314 | if (keypress.equals(kbRight)) { | |
91c9a837 | 315 | getApplication().switchMenu(true); |
928811d8 KL |
316 | return; |
317 | } | |
318 | if (keypress.equals(kbLeft)) { | |
319 | if (isSubMenu) { | |
320 | getApplication().closeSubMenu(); | |
321 | } else { | |
322 | getApplication().switchMenu(false); | |
323 | } | |
324 | return; | |
325 | } | |
326 | ||
327 | // Switch to a menuItem if it has an mnemonic | |
7c870d89 KL |
328 | if (!keypress.getKey().isFnKey() |
329 | && !keypress.getKey().isAlt() | |
330 | && !keypress.getKey().isCtrl()) { | |
382bc294 KL |
331 | |
332 | // System.err.println("Checking children for mnemonic..."); | |
333 | ||
928811d8 KL |
334 | for (TWidget widget: getChildren()) { |
335 | TMenuItem item = (TMenuItem) widget; | |
382bc294 KL |
336 | if ((item.isEnabled() == true) |
337 | && (item.getMnemonic() != null) | |
928811d8 | 338 | && (Character.toLowerCase(item.getMnemonic().getShortcut()) |
7c870d89 | 339 | == Character.toLowerCase(keypress.getKey().getChar())) |
928811d8 | 340 | ) { |
382bc294 KL |
341 | // System.err.println("activate: " + item); |
342 | ||
928811d8 KL |
343 | // Send an enter keystroke to it |
344 | activate(item); | |
345 | item.handleEvent(new TKeypressEvent(kbEnter)); | |
346 | return; | |
347 | } | |
348 | } | |
349 | } | |
350 | ||
351 | // Dispatch the keypress to an active widget | |
352 | for (TWidget widget: getChildren()) { | |
7c870d89 | 353 | if (widget.isActive()) { |
928811d8 KL |
354 | widget.handleEvent(keypress); |
355 | return; | |
356 | } | |
357 | } | |
358 | } | |
359 | ||
d36057df KL |
360 | // ------------------------------------------------------------------------ |
361 | // TWindow ---------------------------------------------------------------- | |
362 | // ------------------------------------------------------------------------ | |
363 | ||
364 | /** | |
365 | * Draw a top-level menu with title and menu items. | |
366 | */ | |
367 | @Override | |
368 | public void draw() { | |
369 | CellAttributes background = getTheme().getColor("tmenu"); | |
370 | ||
371 | assert (isAbsoluteActive()); | |
372 | ||
373 | // Fill in the interior background | |
374 | for (int i = 0; i < getHeight(); i++) { | |
375 | hLineXY(0, i, getWidth(), ' ', background); | |
376 | } | |
377 | ||
378 | // Draw the box | |
379 | char cTopLeft; | |
380 | char cTopRight; | |
381 | char cBottomLeft; | |
382 | char cBottomRight; | |
383 | char cHSide; | |
384 | ||
385 | cTopLeft = GraphicsChars.ULCORNER; | |
386 | cTopRight = GraphicsChars.URCORNER; | |
387 | cBottomLeft = GraphicsChars.LLCORNER; | |
388 | cBottomRight = GraphicsChars.LRCORNER; | |
389 | cHSide = GraphicsChars.SINGLE_BAR; | |
390 | ||
391 | // Place the corner characters | |
392 | putCharXY(1, 0, cTopLeft, background); | |
393 | putCharXY(getWidth() - 2, 0, cTopRight, background); | |
394 | putCharXY(1, getHeight() - 1, cBottomLeft, background); | |
395 | putCharXY(getWidth() - 2, getHeight() - 1, cBottomRight, background); | |
396 | ||
397 | // Draw the box lines | |
398 | hLineXY(1 + 1, 0, getWidth() - 4, cHSide, background); | |
399 | hLineXY(1 + 1, getHeight() - 1, getWidth() - 4, cHSide, background); | |
400 | ||
401 | // Draw a shadow | |
a69ed767 | 402 | drawBoxShadow(0, 0, getWidth(), getHeight()); |
d36057df KL |
403 | } |
404 | ||
405 | // ------------------------------------------------------------------------ | |
406 | // TMenu ------------------------------------------------------------------ | |
407 | // ------------------------------------------------------------------------ | |
408 | ||
409 | /** | |
410 | * Set the menu title X position. | |
411 | * | |
412 | * @param titleX the position | |
413 | */ | |
414 | public void setTitleX(final int titleX) { | |
415 | this.titleX = titleX; | |
416 | } | |
417 | ||
418 | /** | |
419 | * Get the menu title X position. | |
420 | * | |
421 | * @return the position | |
422 | */ | |
423 | public int getTitleX() { | |
424 | return titleX; | |
425 | } | |
426 | ||
427 | /** | |
428 | * Get the mnemonic string. | |
429 | * | |
430 | * @return the full mnemonic string | |
431 | */ | |
432 | public MnemonicString getMnemonic() { | |
433 | return mnemonic; | |
434 | } | |
435 | ||
928811d8 | 436 | /** |
efb7af1f | 437 | * Convenience function to add a menu item. |
928811d8 KL |
438 | * |
439 | * @param id menu item ID. Must be greater than 1024. | |
440 | * @param label menu item label | |
928811d8 KL |
441 | * @return the new menu item |
442 | */ | |
329fd62e | 443 | public TMenuItem addItem(final int id, final String label) { |
efb7af1f | 444 | return addItemInternal(id, label, null); |
928811d8 KL |
445 | } |
446 | ||
a69ed767 KL |
447 | /** |
448 | * Convenience function to add a menu item. | |
449 | * | |
450 | * @param id menu item ID. Must be greater than 1024. | |
451 | * @param label menu item label | |
452 | * @param enabled default state for enabled | |
453 | * @return the new menu item | |
454 | */ | |
455 | public TMenuItem addItem(final int id, final String label, | |
456 | final boolean enabled) { | |
457 | ||
458 | assert (id >= 1024); | |
84164e2c | 459 | return addItemInternal(id, label, null, enabled, -1); |
a69ed767 KL |
460 | } |
461 | ||
928811d8 KL |
462 | /** |
463 | * Convenience function to add a custom menu item. | |
464 | * | |
465 | * @param id menu item ID. Must be greater than 1024. | |
466 | * @param label menu item label | |
467 | * @param key global keyboard accelerator | |
468 | * @return the new menu item | |
469 | */ | |
329fd62e | 470 | public TMenuItem addItem(final int id, final String label, |
928811d8 KL |
471 | final TKeypress key) { |
472 | ||
efb7af1f KL |
473 | assert (id >= 1024); |
474 | return addItemInternal(id, label, key); | |
928811d8 KL |
475 | } |
476 | ||
68c5cd6b KL |
477 | /** |
478 | * Convenience function to add a custom menu item. | |
479 | * | |
480 | * @param id menu item ID. Must be greater than 1024. | |
481 | * @param label menu item label | |
482 | * @param key global keyboard accelerator | |
483 | * @param enabled default state for enabled | |
484 | * @return the new menu item | |
485 | */ | |
486 | public TMenuItem addItem(final int id, final String label, | |
487 | final TKeypress key, final boolean enabled) { | |
488 | ||
489 | TMenuItem item = addItem(id, label, key); | |
490 | item.setEnabled(enabled); | |
491 | return item; | |
492 | } | |
493 | ||
928811d8 | 494 | /** |
efb7af1f | 495 | * Convenience function to add a custom menu item. |
928811d8 KL |
496 | * |
497 | * @param id menu item ID. Must be greater than 1024. | |
498 | * @param label menu item label | |
efb7af1f | 499 | * @param key global keyboard accelerator |
928811d8 KL |
500 | * @return the new menu item |
501 | */ | |
efb7af1f KL |
502 | private TMenuItem addItemInternal(final int id, final String label, |
503 | final TKeypress key) { | |
928811d8 | 504 | |
84164e2c | 505 | return addItemInternal(id, label, key, true, -1); |
d36057df KL |
506 | } |
507 | ||
508 | /** | |
509 | * Convenience function to add a custom menu item. | |
510 | * | |
511 | * @param id menu item ID. Must be greater than 1024. | |
512 | * @param label menu item label | |
513 | * @param key global keyboard accelerator | |
514 | * @param enabled default state for enabled | |
84164e2c | 515 | * @param icon icon picture/emoji |
d36057df KL |
516 | * @return the new menu item |
517 | */ | |
518 | private TMenuItem addItemInternal(final int id, final String label, | |
84164e2c | 519 | final TKeypress key, final boolean enabled, final int icon) { |
d36057df | 520 | |
928811d8 KL |
521 | int newY = getChildren().size() + 1; |
522 | assert (newY < getHeight()); | |
523 | ||
84164e2c | 524 | TMenuItem menuItem = new TMenuItem(this, id, 1, newY, label, icon); |
efb7af1f | 525 | menuItem.setKey(key); |
d36057df | 526 | menuItem.setEnabled(enabled); |
928811d8 KL |
527 | setHeight(getHeight() + 1); |
528 | if (menuItem.getWidth() + 2 > getWidth()) { | |
529 | setWidth(menuItem.getWidth() + 2); | |
530 | } | |
531 | for (TWidget widget: getChildren()) { | |
532 | widget.setWidth(getWidth() - 2); | |
533 | } | |
efb7af1f | 534 | getApplication().addMenuItem(menuItem); |
928811d8 KL |
535 | getApplication().recomputeMenuX(); |
536 | activate(0); | |
537 | return menuItem; | |
538 | } | |
539 | ||
540 | /** | |
541 | * Convenience function to add one of the default menu items. | |
542 | * | |
543 | * @param id menu item ID. Must be between 0 (inclusive) and 1023 | |
544 | * (inclusive). | |
545 | * @return the new menu item | |
546 | */ | |
329fd62e | 547 | public TMenuItem addDefaultItem(final int id) { |
d36057df KL |
548 | return addDefaultItem(id, true); |
549 | } | |
550 | ||
551 | /** | |
552 | * Convenience function to add one of the default menu items. | |
553 | * | |
554 | * @param id menu item ID. Must be between 0 (inclusive) and 1023 | |
555 | * (inclusive). | |
556 | * @param enabled default state for enabled | |
557 | * @return the new menu item | |
558 | */ | |
559 | public TMenuItem addDefaultItem(final int id, final boolean enabled) { | |
928811d8 KL |
560 | assert (id >= 0); |
561 | assert (id < 1024); | |
562 | ||
563 | String label; | |
564 | TKeypress key = null; | |
84164e2c | 565 | int icon = -1; |
77961919 KL |
566 | boolean checkable = false; |
567 | boolean checked = false; | |
928811d8 KL |
568 | |
569 | switch (id) { | |
570 | ||
e23ea538 KL |
571 | case MID_REPAINT: |
572 | label = i18n.getString("menuRepaintDesktop"); | |
84164e2c | 573 | icon = 0x1F3A8; |
e23ea538 KL |
574 | break; |
575 | ||
576 | case MID_VIEW_IMAGE: | |
577 | label = i18n.getString("menuViewImage"); | |
578 | break; | |
579 | ||
a75902fa KL |
580 | case MID_SCREEN_OPTIONS: |
581 | label = i18n.getString("menuScreenOptions"); | |
e23ea538 KL |
582 | break; |
583 | ||
584 | case MID_NEW: | |
585 | label = i18n.getString("menuNew"); | |
84164e2c | 586 | icon = 0x1F5CE; |
e23ea538 KL |
587 | break; |
588 | ||
928811d8 | 589 | case MID_EXIT: |
339652cc | 590 | label = i18n.getString("menuExit"); |
928811d8 | 591 | key = kbAltX; |
84164e2c | 592 | icon = 0x1F5D9; |
928811d8 KL |
593 | break; |
594 | ||
595 | case MID_SHELL: | |
339652cc | 596 | label = i18n.getString("menuShell"); |
84164e2c | 597 | icon = 0x1F5AE; |
928811d8 KL |
598 | break; |
599 | ||
600 | case MID_OPEN_FILE: | |
339652cc | 601 | label = i18n.getString("menuOpen"); |
b2d49e0f | 602 | key = kbF3; |
84164e2c | 603 | icon = 0x1F5C1; |
928811d8 KL |
604 | break; |
605 | ||
606 | case MID_CUT: | |
339652cc | 607 | label = i18n.getString("menuCut"); |
928811d8 | 608 | key = kbCtrlX; |
84164e2c | 609 | icon = 0x1F5F6; |
928811d8 KL |
610 | break; |
611 | case MID_COPY: | |
339652cc | 612 | label = i18n.getString("menuCopy"); |
928811d8 | 613 | key = kbCtrlC; |
84164e2c | 614 | icon = 0x1F5D0; |
928811d8 KL |
615 | break; |
616 | case MID_PASTE: | |
339652cc | 617 | label = i18n.getString("menuPaste"); |
928811d8 | 618 | key = kbCtrlV; |
84164e2c | 619 | icon = 0x1F4CB; |
928811d8 KL |
620 | break; |
621 | case MID_CLEAR: | |
339652cc | 622 | label = i18n.getString("menuClear"); |
928811d8 KL |
623 | break; |
624 | ||
d36057df KL |
625 | case MID_FIND: |
626 | label = i18n.getString("menuFind"); | |
84164e2c | 627 | icon = 0x1F50D; |
d36057df KL |
628 | break; |
629 | case MID_REPLACE: | |
630 | label = i18n.getString("menuReplace"); | |
631 | break; | |
632 | case MID_SEARCH_AGAIN: | |
633 | label = i18n.getString("menuSearchAgain"); | |
978a5d8f | 634 | key = kbCtrlL; |
d36057df KL |
635 | break; |
636 | case MID_GOTO_LINE: | |
637 | label = i18n.getString("menuGotoLine"); | |
d36057df KL |
638 | break; |
639 | ||
928811d8 | 640 | case MID_TILE: |
339652cc | 641 | label = i18n.getString("menuWindowTile"); |
928811d8 KL |
642 | break; |
643 | case MID_CASCADE: | |
339652cc | 644 | label = i18n.getString("menuWindowCascade"); |
84164e2c | 645 | icon = 0x1F5D7; |
928811d8 KL |
646 | break; |
647 | case MID_CLOSE_ALL: | |
339652cc | 648 | label = i18n.getString("menuWindowCloseAll"); |
928811d8 KL |
649 | break; |
650 | case MID_WINDOW_MOVE: | |
339652cc | 651 | label = i18n.getString("menuWindowMove"); |
928811d8 | 652 | key = kbCtrlF5; |
84164e2c | 653 | icon = 0x263C; |
928811d8 KL |
654 | break; |
655 | case MID_WINDOW_ZOOM: | |
339652cc | 656 | label = i18n.getString("menuWindowZoom"); |
928811d8 | 657 | key = kbF5; |
84164e2c | 658 | icon = 0x2195; |
928811d8 KL |
659 | break; |
660 | case MID_WINDOW_NEXT: | |
339652cc | 661 | label = i18n.getString("menuWindowNext"); |
928811d8 | 662 | key = kbF6; |
84164e2c | 663 | icon = 0x2192; |
928811d8 KL |
664 | break; |
665 | case MID_WINDOW_PREVIOUS: | |
339652cc | 666 | label = i18n.getString("menuWindowPrevious"); |
928811d8 | 667 | key = kbShiftF6; |
84164e2c | 668 | icon = 0x2190; |
928811d8 KL |
669 | break; |
670 | case MID_WINDOW_CLOSE: | |
339652cc | 671 | label = i18n.getString("menuWindowClose"); |
5dfd1c11 | 672 | key = kbCtrlW; |
928811d8 KL |
673 | break; |
674 | ||
55d2b2c2 | 675 | case MID_HELP_CONTENTS: |
339652cc | 676 | label = i18n.getString("menuHelpContents"); |
55d2b2c2 KL |
677 | break; |
678 | case MID_HELP_INDEX: | |
339652cc | 679 | label = i18n.getString("menuHelpIndex"); |
55d2b2c2 KL |
680 | key = kbShiftF1; |
681 | break; | |
682 | case MID_HELP_SEARCH: | |
339652cc | 683 | label = i18n.getString("menuHelpSearch"); |
55d2b2c2 KL |
684 | key = kbCtrlF1; |
685 | break; | |
686 | case MID_HELP_PREVIOUS: | |
339652cc | 687 | label = i18n.getString("menuHelpPrevious"); |
55d2b2c2 KL |
688 | key = kbAltF1; |
689 | break; | |
690 | case MID_HELP_HELP: | |
339652cc | 691 | label = i18n.getString("menuHelpHelp"); |
55d2b2c2 KL |
692 | break; |
693 | case MID_HELP_ACTIVE_FILE: | |
339652cc | 694 | label = i18n.getString("menuHelpActive"); |
55d2b2c2 KL |
695 | break; |
696 | case MID_ABOUT: | |
339652cc | 697 | label = i18n.getString("menuHelpAbout"); |
55d2b2c2 KL |
698 | break; |
699 | ||
2b427404 KL |
700 | case MID_TABLE_RENAME_COLUMN: |
701 | label = i18n.getString("menuTableRenameColumn"); | |
702 | break; | |
703 | case MID_TABLE_RENAME_ROW: | |
704 | label = i18n.getString("menuTableRenameRow"); | |
705 | break; | |
77961919 KL |
706 | case MID_TABLE_VIEW_ROW_LABELS: |
707 | label = i18n.getString("menuTableViewRowLabels"); | |
708 | checkable = true; | |
709 | checked = true; | |
710 | break; | |
711 | case MID_TABLE_VIEW_COLUMN_LABELS: | |
712 | label = i18n.getString("menuTableViewColumnLabels"); | |
713 | checkable = true; | |
714 | checked = true; | |
715 | break; | |
716 | case MID_TABLE_VIEW_HIGHLIGHT_ROW: | |
717 | label = i18n.getString("menuTableViewHighlightRow"); | |
718 | checkable = true; | |
719 | checked = true; | |
720 | break; | |
721 | case MID_TABLE_VIEW_HIGHLIGHT_COLUMN: | |
722 | label = i18n.getString("menuTableViewHighlightColumn"); | |
723 | checkable = true; | |
724 | checked = true; | |
725 | break; | |
726 | ||
1dac6b8d KL |
727 | case MID_TABLE_BORDER_NONE: |
728 | label = i18n.getString("menuTableBorderNone"); | |
729 | break; | |
730 | case MID_TABLE_BORDER_ALL: | |
731 | label = i18n.getString("menuTableBorderAll"); | |
732 | break; | |
e9bb3c1e KL |
733 | case MID_TABLE_BORDER_CELL_NONE: |
734 | label = i18n.getString("menuTableBorderCellNone"); | |
735 | break; | |
736 | case MID_TABLE_BORDER_CELL_ALL: | |
737 | label = i18n.getString("menuTableBorderCellAll"); | |
738 | break; | |
1dac6b8d KL |
739 | case MID_TABLE_BORDER_RIGHT: |
740 | label = i18n.getString("menuTableBorderRight"); | |
741 | break; | |
742 | case MID_TABLE_BORDER_LEFT: | |
743 | label = i18n.getString("menuTableBorderLeft"); | |
744 | break; | |
745 | case MID_TABLE_BORDER_TOP: | |
746 | label = i18n.getString("menuTableBorderTop"); | |
747 | break; | |
748 | case MID_TABLE_BORDER_BOTTOM: | |
749 | label = i18n.getString("menuTableBorderBottom"); | |
750 | break; | |
751 | case MID_TABLE_BORDER_DOUBLE_BOTTOM: | |
752 | label = i18n.getString("menuTableBorderDoubleBottom"); | |
753 | break; | |
754 | case MID_TABLE_BORDER_THICK_BOTTOM: | |
755 | label = i18n.getString("menuTableBorderThickBottom"); | |
756 | break; | |
757 | case MID_TABLE_DELETE_LEFT: | |
758 | label = i18n.getString("menuTableDeleteLeft"); | |
759 | break; | |
760 | case MID_TABLE_DELETE_UP: | |
761 | label = i18n.getString("menuTableDeleteUp"); | |
762 | break; | |
763 | case MID_TABLE_DELETE_ROW: | |
764 | label = i18n.getString("menuTableDeleteRow"); | |
765 | break; | |
766 | case MID_TABLE_DELETE_COLUMN: | |
767 | label = i18n.getString("menuTableDeleteColumn"); | |
768 | break; | |
769 | case MID_TABLE_INSERT_LEFT: | |
770 | label = i18n.getString("menuTableInsertLeft"); | |
771 | break; | |
772 | case MID_TABLE_INSERT_RIGHT: | |
773 | label = i18n.getString("menuTableInsertRight"); | |
774 | break; | |
775 | case MID_TABLE_INSERT_ABOVE: | |
776 | label = i18n.getString("menuTableInsertAbove"); | |
777 | break; | |
778 | case MID_TABLE_INSERT_BELOW: | |
779 | label = i18n.getString("menuTableInsertBelow"); | |
780 | break; | |
781 | case MID_TABLE_COLUMN_NARROW: | |
782 | label = i18n.getString("menuTableColumnNarrow"); | |
783 | key = kbShiftLeft; | |
784 | break; | |
785 | case MID_TABLE_COLUMN_WIDEN: | |
786 | label = i18n.getString("menuTableColumnWiden"); | |
787 | key = kbShiftRight; | |
788 | break; | |
f528c340 KL |
789 | case MID_TABLE_FILE_OPEN_CSV: |
790 | label = i18n.getString("menuTableFileOpenCsv"); | |
791 | break; | |
1dac6b8d KL |
792 | case MID_TABLE_FILE_SAVE_CSV: |
793 | label = i18n.getString("menuTableFileSaveCsv"); | |
794 | break; | |
795 | case MID_TABLE_FILE_SAVE_TEXT: | |
796 | label = i18n.getString("menuTableFileSaveText"); | |
797 | break; | |
798 | ||
928811d8 KL |
799 | default: |
800 | throw new IllegalArgumentException("Invalid menu ID: " + id); | |
801 | } | |
802 | ||
84164e2c | 803 | TMenuItem item = addItemInternal(id, label, key, enabled, icon); |
77961919 KL |
804 | item.setCheckable(checkable); |
805 | return item; | |
928811d8 KL |
806 | } |
807 | ||
808 | /** | |
809 | * Convenience function to add a menu separator. | |
810 | */ | |
329fd62e | 811 | public void addSeparator() { |
928811d8 KL |
812 | int newY = getChildren().size() + 1; |
813 | assert (newY < getHeight()); | |
814 | ||
cf9af8df KL |
815 | // We just have to construct it, don't need to hang onto what it |
816 | // makes. | |
817 | new TMenuSeparator(this, 1, newY); | |
928811d8 KL |
818 | setHeight(getHeight() + 1); |
819 | } | |
820 | ||
821 | /** | |
822 | * Convenience function to add a sub-menu. | |
823 | * | |
824 | * @param title menu title. Title must contain a keyboard shortcut, | |
43ad7b6c | 825 | * denoted by prefixing a letter with "&", e.g. "&File" |
928811d8 KL |
826 | * @return the new sub-menu |
827 | */ | |
329fd62e | 828 | public TSubMenu addSubMenu(final String title) { |
928811d8 KL |
829 | int newY = getChildren().size() + 1; |
830 | assert (newY < getHeight()); | |
831 | ||
832 | TSubMenu subMenu = new TSubMenu(this, title, 1, newY); | |
833 | setHeight(getHeight() + 1); | |
834 | if (subMenu.getWidth() + 2 > getWidth()) { | |
835 | setWidth(subMenu.getWidth() + 2); | |
836 | } | |
837 | for (TWidget widget: getChildren()) { | |
838 | widget.setWidth(getWidth() - 2); | |
839 | } | |
840 | getApplication().recomputeMenuX(); | |
841 | activate(0); | |
842 | subMenu.menu.setX(getX() + getWidth() - 2); | |
843 | ||
844 | return subMenu; | |
845 | } | |
846 | ||
847 | } |