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