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