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