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