Add 'src/jexer/' from commit 'cf01c92f5809a0732409e280fb0f32f27393618d'
[fanfix.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.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_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;
79
80 // Search menu
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;
85
86 // Window menu
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;
95
96 // Help menu
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;
104
105 // Table menu
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;
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;
135
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
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
166 * shortcut (mnemonic), denoted by prefixing a letter with "&",
167 * e.g. "&File"
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
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
181 setWidth(StringUtils.width(getTitle()) + 4);
182 setHeight(2);
183
184 setActive(false);
185 }
186
187 // ------------------------------------------------------------------------
188 // Event handlers ---------------------------------------------------------
189 // ------------------------------------------------------------------------
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;
199 super.onMouseDown(mouse);
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;
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;
248
249 // See if we should activate a different menu item
250 for (TWidget widget: getChildren()) {
251 if ((mouse.isMouse1())
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) {
271
272 /*
273 System.err.printf("keypress: %s active child: %s\n", keypress,
274 getActiveChild());
275 */
276
277 if (getActiveChild() != this) {
278 if (getActiveChild() instanceof TMenu) {
279 getActiveChild().onKeypress(keypress);
280 return;
281 }
282
283 if (getActiveChild() instanceof TSubMenu) {
284 TSubMenu subMenu = (TSubMenu) getActiveChild();
285 if (subMenu.menu.isActive()) {
286 subMenu.onKeypress(keypress);
287 return;
288 }
289 }
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)) {
305 getApplication().switchMenu(true);
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
318 if (!keypress.getKey().isFnKey()
319 && !keypress.getKey().isAlt()
320 && !keypress.getKey().isCtrl()) {
321
322 // System.err.println("Checking children for mnemonic...");
323
324 for (TWidget widget: getChildren()) {
325 TMenuItem item = (TMenuItem) widget;
326 if ((item.isEnabled() == true)
327 && (item.getMnemonic() != null)
328 && (Character.toLowerCase(item.getMnemonic().getShortcut())
329 == Character.toLowerCase(keypress.getKey().getChar()))
330 ) {
331 // System.err.println("activate: " + item);
332
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()) {
343 if (widget.isActive()) {
344 widget.handleEvent(keypress);
345 return;
346 }
347 }
348 }
349
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
392 drawBoxShadow(0, 0, getWidth(), getHeight());
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
426 /**
427 * Convenience function to add a menu item.
428 *
429 * @param id menu item ID. Must be greater than 1024.
430 * @param label menu item label
431 * @return the new menu item
432 */
433 public TMenuItem addItem(final int id, final String label) {
434 return addItemInternal(id, label, null);
435 }
436
437 /**
438 * Convenience function to add a menu item.
439 *
440 * @param id menu item ID. Must be greater than 1024.
441 * @param label menu item label
442 * @param enabled default state for enabled
443 * @return the new menu item
444 */
445 public TMenuItem addItem(final int id, final String label,
446 final boolean enabled) {
447
448 assert (id >= 1024);
449 return addItemInternal(id, label, null, enabled);
450 }
451
452 /**
453 * Convenience function to add a custom menu item.
454 *
455 * @param id menu item ID. Must be greater than 1024.
456 * @param label menu item label
457 * @param key global keyboard accelerator
458 * @return the new menu item
459 */
460 public TMenuItem addItem(final int id, final String label,
461 final TKeypress key) {
462
463 assert (id >= 1024);
464 return addItemInternal(id, label, key);
465 }
466
467 /**
468 * Convenience function to add a custom menu item.
469 *
470 * @param id menu item ID. Must be greater than 1024.
471 * @param label menu item label
472 * @param key global keyboard accelerator
473 * @param enabled default state for enabled
474 * @return the new menu item
475 */
476 public TMenuItem addItem(final int id, final String label,
477 final TKeypress key, final boolean enabled) {
478
479 TMenuItem item = addItem(id, label, key);
480 item.setEnabled(enabled);
481 return item;
482 }
483
484 /**
485 * Convenience function to add a custom menu item.
486 *
487 * @param id menu item ID. Must be greater than 1024.
488 * @param label menu item label
489 * @param key global keyboard accelerator
490 * @return the new menu item
491 */
492 private TMenuItem addItemInternal(final int id, final String label,
493 final TKeypress key) {
494
495 return addItemInternal(id, label, key, true);
496 }
497
498 /**
499 * Convenience function to add a custom menu item.
500 *
501 * @param id menu item ID. Must be greater than 1024.
502 * @param label menu item label
503 * @param key global keyboard accelerator
504 * @param enabled default state for enabled
505 * @return the new menu item
506 */
507 private TMenuItem addItemInternal(final int id, final String label,
508 final TKeypress key, final boolean enabled) {
509
510 int newY = getChildren().size() + 1;
511 assert (newY < getHeight());
512
513 TMenuItem menuItem = new TMenuItem(this, id, 1, newY, label);
514 menuItem.setKey(key);
515 menuItem.setEnabled(enabled);
516 setHeight(getHeight() + 1);
517 if (menuItem.getWidth() + 2 > getWidth()) {
518 setWidth(menuItem.getWidth() + 2);
519 }
520 for (TWidget widget: getChildren()) {
521 widget.setWidth(getWidth() - 2);
522 }
523 getApplication().addMenuItem(menuItem);
524 getApplication().recomputeMenuX();
525 activate(0);
526 return menuItem;
527 }
528
529 /**
530 * Convenience function to add one of the default menu items.
531 *
532 * @param id menu item ID. Must be between 0 (inclusive) and 1023
533 * (inclusive).
534 * @return the new menu item
535 */
536 public TMenuItem addDefaultItem(final int id) {
537 return addDefaultItem(id, true);
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 * @param enabled default state for enabled
546 * @return the new menu item
547 */
548 public TMenuItem addDefaultItem(final int id, final boolean enabled) {
549 assert (id >= 0);
550 assert (id < 1024);
551
552 String label;
553 TKeypress key = null;
554 boolean checkable = false;
555 boolean checked = false;
556
557 switch (id) {
558
559 case MID_REPAINT:
560 label = i18n.getString("menuRepaintDesktop");
561 break;
562
563 case MID_VIEW_IMAGE:
564 label = i18n.getString("menuViewImage");
565 break;
566
567 case MID_SCREEN_OPTIONS:
568 label = i18n.getString("menuScreenOptions");
569 break;
570
571 case MID_NEW:
572 label = i18n.getString("menuNew");
573 break;
574
575 case MID_EXIT:
576 label = i18n.getString("menuExit");
577 key = kbAltX;
578 break;
579
580 case MID_SHELL:
581 label = i18n.getString("menuShell");
582 break;
583
584 case MID_OPEN_FILE:
585 label = i18n.getString("menuOpen");
586 key = kbF3;
587 break;
588
589 case MID_CUT:
590 label = i18n.getString("menuCut");
591 key = kbCtrlX;
592 break;
593 case MID_COPY:
594 label = i18n.getString("menuCopy");
595 key = kbCtrlC;
596 break;
597 case MID_PASTE:
598 label = i18n.getString("menuPaste");
599 key = kbCtrlV;
600 break;
601 case MID_CLEAR:
602 label = i18n.getString("menuClear");
603 // key = kbDel;
604 break;
605
606 case MID_FIND:
607 label = i18n.getString("menuFind");
608 break;
609 case MID_REPLACE:
610 label = i18n.getString("menuReplace");
611 break;
612 case MID_SEARCH_AGAIN:
613 label = i18n.getString("menuSearchAgain");
614 key = kbCtrlL;
615 break;
616 case MID_GOTO_LINE:
617 label = i18n.getString("menuGotoLine");
618 break;
619
620 case MID_TILE:
621 label = i18n.getString("menuWindowTile");
622 break;
623 case MID_CASCADE:
624 label = i18n.getString("menuWindowCascade");
625 break;
626 case MID_CLOSE_ALL:
627 label = i18n.getString("menuWindowCloseAll");
628 break;
629 case MID_WINDOW_MOVE:
630 label = i18n.getString("menuWindowMove");
631 key = kbCtrlF5;
632 break;
633 case MID_WINDOW_ZOOM:
634 label = i18n.getString("menuWindowZoom");
635 key = kbF5;
636 break;
637 case MID_WINDOW_NEXT:
638 label = i18n.getString("menuWindowNext");
639 key = kbF6;
640 break;
641 case MID_WINDOW_PREVIOUS:
642 label = i18n.getString("menuWindowPrevious");
643 key = kbShiftF6;
644 break;
645 case MID_WINDOW_CLOSE:
646 label = i18n.getString("menuWindowClose");
647 key = kbCtrlW;
648 break;
649
650 case MID_HELP_CONTENTS:
651 label = i18n.getString("menuHelpContents");
652 break;
653 case MID_HELP_INDEX:
654 label = i18n.getString("menuHelpIndex");
655 key = kbShiftF1;
656 break;
657 case MID_HELP_SEARCH:
658 label = i18n.getString("menuHelpSearch");
659 key = kbCtrlF1;
660 break;
661 case MID_HELP_PREVIOUS:
662 label = i18n.getString("menuHelpPrevious");
663 key = kbAltF1;
664 break;
665 case MID_HELP_HELP:
666 label = i18n.getString("menuHelpHelp");
667 break;
668 case MID_HELP_ACTIVE_FILE:
669 label = i18n.getString("menuHelpActive");
670 break;
671 case MID_ABOUT:
672 label = i18n.getString("menuHelpAbout");
673 break;
674
675 case MID_TABLE_RENAME_COLUMN:
676 label = i18n.getString("menuTableRenameColumn");
677 break;
678 case MID_TABLE_RENAME_ROW:
679 label = i18n.getString("menuTableRenameRow");
680 break;
681 case MID_TABLE_VIEW_ROW_LABELS:
682 label = i18n.getString("menuTableViewRowLabels");
683 checkable = true;
684 checked = true;
685 break;
686 case MID_TABLE_VIEW_COLUMN_LABELS:
687 label = i18n.getString("menuTableViewColumnLabels");
688 checkable = true;
689 checked = true;
690 break;
691 case MID_TABLE_VIEW_HIGHLIGHT_ROW:
692 label = i18n.getString("menuTableViewHighlightRow");
693 checkable = true;
694 checked = true;
695 break;
696 case MID_TABLE_VIEW_HIGHLIGHT_COLUMN:
697 label = i18n.getString("menuTableViewHighlightColumn");
698 checkable = true;
699 checked = true;
700 break;
701
702 case MID_TABLE_BORDER_NONE:
703 label = i18n.getString("menuTableBorderNone");
704 break;
705 case MID_TABLE_BORDER_ALL:
706 label = i18n.getString("menuTableBorderAll");
707 break;
708 case MID_TABLE_BORDER_CELL_NONE:
709 label = i18n.getString("menuTableBorderCellNone");
710 break;
711 case MID_TABLE_BORDER_CELL_ALL:
712 label = i18n.getString("menuTableBorderCellAll");
713 break;
714 case MID_TABLE_BORDER_RIGHT:
715 label = i18n.getString("menuTableBorderRight");
716 break;
717 case MID_TABLE_BORDER_LEFT:
718 label = i18n.getString("menuTableBorderLeft");
719 break;
720 case MID_TABLE_BORDER_TOP:
721 label = i18n.getString("menuTableBorderTop");
722 break;
723 case MID_TABLE_BORDER_BOTTOM:
724 label = i18n.getString("menuTableBorderBottom");
725 break;
726 case MID_TABLE_BORDER_DOUBLE_BOTTOM:
727 label = i18n.getString("menuTableBorderDoubleBottom");
728 break;
729 case MID_TABLE_BORDER_THICK_BOTTOM:
730 label = i18n.getString("menuTableBorderThickBottom");
731 break;
732 case MID_TABLE_DELETE_LEFT:
733 label = i18n.getString("menuTableDeleteLeft");
734 break;
735 case MID_TABLE_DELETE_UP:
736 label = i18n.getString("menuTableDeleteUp");
737 break;
738 case MID_TABLE_DELETE_ROW:
739 label = i18n.getString("menuTableDeleteRow");
740 break;
741 case MID_TABLE_DELETE_COLUMN:
742 label = i18n.getString("menuTableDeleteColumn");
743 break;
744 case MID_TABLE_INSERT_LEFT:
745 label = i18n.getString("menuTableInsertLeft");
746 break;
747 case MID_TABLE_INSERT_RIGHT:
748 label = i18n.getString("menuTableInsertRight");
749 break;
750 case MID_TABLE_INSERT_ABOVE:
751 label = i18n.getString("menuTableInsertAbove");
752 break;
753 case MID_TABLE_INSERT_BELOW:
754 label = i18n.getString("menuTableInsertBelow");
755 break;
756 case MID_TABLE_COLUMN_NARROW:
757 label = i18n.getString("menuTableColumnNarrow");
758 key = kbShiftLeft;
759 break;
760 case MID_TABLE_COLUMN_WIDEN:
761 label = i18n.getString("menuTableColumnWiden");
762 key = kbShiftRight;
763 break;
764 case MID_TABLE_FILE_OPEN_CSV:
765 label = i18n.getString("menuTableFileOpenCsv");
766 break;
767 case MID_TABLE_FILE_SAVE_CSV:
768 label = i18n.getString("menuTableFileSaveCsv");
769 break;
770 case MID_TABLE_FILE_SAVE_TEXT:
771 label = i18n.getString("menuTableFileSaveText");
772 break;
773
774 default:
775 throw new IllegalArgumentException("Invalid menu ID: " + id);
776 }
777
778 TMenuItem item = addItemInternal(id, label, key, enabled);
779 item.setCheckable(checkable);
780 return item;
781 }
782
783 /**
784 * Convenience function to add a menu separator.
785 */
786 public void addSeparator() {
787 int newY = getChildren().size() + 1;
788 assert (newY < getHeight());
789
790 // We just have to construct it, don't need to hang onto what it
791 // makes.
792 new TMenuSeparator(this, 1, newY);
793 setHeight(getHeight() + 1);
794 }
795
796 /**
797 * Convenience function to add a sub-menu.
798 *
799 * @param title menu title. Title must contain a keyboard shortcut,
800 * denoted by prefixing a letter with "&amp;", e.g. "&amp;File"
801 * @return the new sub-menu
802 */
803 public TSubMenu addSubMenu(final String title) {
804 int newY = getChildren().size() + 1;
805 assert (newY < getHeight());
806
807 TSubMenu subMenu = new TSubMenu(this, title, 1, newY);
808 setHeight(getHeight() + 1);
809 if (subMenu.getWidth() + 2 > getWidth()) {
810 setWidth(subMenu.getWidth() + 2);
811 }
812 for (TWidget widget: getChildren()) {
813 widget.setWidth(getWidth() - 2);
814 }
815 getApplication().recomputeMenuX();
816 activate(0);
817 subMenu.menu.setX(getX() + getWidth() - 2);
818
819 return subMenu;
820 }
821
822 }