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