oops
[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
e9bb3c1e
KL
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;
f528c340
KL
131 public static final int MID_TABLE_FILE_OPEN_CSV = 115;
132 public static final int MID_TABLE_FILE_SAVE_CSV = 116;
133 public static final int MID_TABLE_FILE_SAVE_TEXT = 117;
1dac6b8d 134
d36057df
KL
135 // ------------------------------------------------------------------------
136 // Variables --------------------------------------------------------------
137 // ------------------------------------------------------------------------
138
139 /**
140 * If true, this is a sub-menu. Note package private access.
141 */
142 boolean isSubMenu = false;
143
144 /**
145 * The X position of the menu's title.
146 */
147 private int titleX;
148
149 /**
150 * The shortcut and title.
151 */
152 private MnemonicString mnemonic;
153
154 // ------------------------------------------------------------------------
155 // Constructors -----------------------------------------------------------
156 // ------------------------------------------------------------------------
157
928811d8
KL
158 /**
159 * Public constructor.
160 *
161 * @param parent parent application
162 * @param x column relative to parent
163 * @param y row relative to parent
164 * @param label mnemonic menu title. Label must contain a keyboard
43ad7b6c
KL
165 * shortcut (mnemonic), denoted by prefixing a letter with "&",
166 * e.g. "&File"
928811d8
KL
167 */
168 public TMenu(final TApplication parent, final int x, final int y,
169 final String label) {
170
171 super(parent, label, x, y, parent.getScreen().getWidth(),
172 parent.getScreen().getHeight());
173
928811d8
KL
174 // Setup the menu shortcut
175 mnemonic = new MnemonicString(label);
176 setTitle(mnemonic.getRawLabel());
177 assert (mnemonic.getShortcutIdx() >= 0);
178
179 // Recompute width and height to reflect an empty menu
180 setWidth(getTitle().length() + 4);
181 setHeight(2);
182
183 setActive(false);
184 }
185
d36057df
KL
186 // ------------------------------------------------------------------------
187 // Event handlers ---------------------------------------------------------
188 // ------------------------------------------------------------------------
928811d8
KL
189
190 /**
191 * Handle mouse button presses.
192 *
193 * @param mouse mouse button event
194 */
195 @Override
196 public void onMouseDown(final TMouseEvent mouse) {
197 this.mouse = mouse;
a69ed767 198 super.onMouseDown(mouse);
928811d8
KL
199
200 // Pass to children
201 for (TWidget widget: getChildren()) {
202 if (widget.mouseWouldHit(mouse)) {
203 // Dispatch to this child, also activate it
204 activate(widget);
205
206 // Set x and y relative to the child's coordinates
207 mouse.setX(mouse.getAbsoluteX() - widget.getAbsoluteX());
208 mouse.setY(mouse.getAbsoluteY() - widget.getAbsoluteY());
209 widget.handleEvent(mouse);
210 return;
211 }
212 }
213 }
214
215 /**
216 * Handle mouse button releases.
217 *
218 * @param mouse mouse button release event
219 */
220 @Override
221 public void onMouseUp(final TMouseEvent mouse) {
222 this.mouse = mouse;
928811d8
KL
223
224 // Pass to children
225 for (TWidget widget: getChildren()) {
226 if (widget.mouseWouldHit(mouse)) {
227 // Dispatch to this child, also activate it
228 activate(widget);
229
230 // Set x and y relative to the child's coordinates
231 mouse.setX(mouse.getAbsoluteX() - widget.getAbsoluteX());
232 mouse.setY(mouse.getAbsoluteY() - widget.getAbsoluteY());
233 widget.handleEvent(mouse);
234 return;
235 }
236 }
237 }
238
239 /**
240 * Handle mouse movements.
241 *
242 * @param mouse mouse motion event
243 */
244 @Override
245 public void onMouseMotion(final TMouseEvent mouse) {
246 this.mouse = mouse;
928811d8
KL
247
248 // See if we should activate a different menu item
249 for (TWidget widget: getChildren()) {
7c870d89 250 if ((mouse.isMouse1())
928811d8
KL
251 && (widget.mouseWouldHit(mouse))
252 ) {
253 // Activate this menu item
254 activate(widget);
255 if (widget instanceof TSubMenu) {
256 ((TSubMenu) widget).dispatch();
257 }
258 return;
259 }
260 }
261 }
262
263 /**
264 * Handle keystrokes.
265 *
266 * @param keypress keystroke event
267 */
268 @Override
269 public void onKeypress(final TKeypressEvent keypress) {
91c9a837
KL
270
271 /*
272 System.err.printf("keypress: %s active child: %s\n", keypress,
273 getActiveChild());
382bc294 274 */
91c9a837
KL
275
276 if (getActiveChild() != this) {
382bc294 277 if (getActiveChild() instanceof TMenu) {
928811d8
KL
278 getActiveChild().onKeypress(keypress);
279 return;
280 }
382bc294
KL
281
282 if (getActiveChild() instanceof TSubMenu) {
283 TSubMenu subMenu = (TSubMenu) getActiveChild();
284 if (subMenu.menu.isActive()) {
285 subMenu.onKeypress(keypress);
286 return;
287 }
288 }
928811d8
KL
289 }
290
291 if (keypress.equals(kbEsc)) {
292 getApplication().closeMenu();
293 return;
294 }
295 if (keypress.equals(kbDown)) {
296 switchWidget(true);
297 return;
298 }
299 if (keypress.equals(kbUp)) {
300 switchWidget(false);
301 return;
302 }
303 if (keypress.equals(kbRight)) {
91c9a837 304 getApplication().switchMenu(true);
928811d8
KL
305 return;
306 }
307 if (keypress.equals(kbLeft)) {
308 if (isSubMenu) {
309 getApplication().closeSubMenu();
310 } else {
311 getApplication().switchMenu(false);
312 }
313 return;
314 }
315
316 // Switch to a menuItem if it has an mnemonic
7c870d89
KL
317 if (!keypress.getKey().isFnKey()
318 && !keypress.getKey().isAlt()
319 && !keypress.getKey().isCtrl()) {
382bc294
KL
320
321 // System.err.println("Checking children for mnemonic...");
322
928811d8
KL
323 for (TWidget widget: getChildren()) {
324 TMenuItem item = (TMenuItem) widget;
382bc294
KL
325 if ((item.isEnabled() == true)
326 && (item.getMnemonic() != null)
928811d8 327 && (Character.toLowerCase(item.getMnemonic().getShortcut())
7c870d89 328 == Character.toLowerCase(keypress.getKey().getChar()))
928811d8 329 ) {
382bc294
KL
330 // System.err.println("activate: " + item);
331
928811d8
KL
332 // Send an enter keystroke to it
333 activate(item);
334 item.handleEvent(new TKeypressEvent(kbEnter));
335 return;
336 }
337 }
338 }
339
340 // Dispatch the keypress to an active widget
341 for (TWidget widget: getChildren()) {
7c870d89 342 if (widget.isActive()) {
928811d8
KL
343 widget.handleEvent(keypress);
344 return;
345 }
346 }
347 }
348
d36057df
KL
349 // ------------------------------------------------------------------------
350 // TWindow ----------------------------------------------------------------
351 // ------------------------------------------------------------------------
352
353 /**
354 * Draw a top-level menu with title and menu items.
355 */
356 @Override
357 public void draw() {
358 CellAttributes background = getTheme().getColor("tmenu");
359
360 assert (isAbsoluteActive());
361
362 // Fill in the interior background
363 for (int i = 0; i < getHeight(); i++) {
364 hLineXY(0, i, getWidth(), ' ', background);
365 }
366
367 // Draw the box
368 char cTopLeft;
369 char cTopRight;
370 char cBottomLeft;
371 char cBottomRight;
372 char cHSide;
373
374 cTopLeft = GraphicsChars.ULCORNER;
375 cTopRight = GraphicsChars.URCORNER;
376 cBottomLeft = GraphicsChars.LLCORNER;
377 cBottomRight = GraphicsChars.LRCORNER;
378 cHSide = GraphicsChars.SINGLE_BAR;
379
380 // Place the corner characters
381 putCharXY(1, 0, cTopLeft, background);
382 putCharXY(getWidth() - 2, 0, cTopRight, background);
383 putCharXY(1, getHeight() - 1, cBottomLeft, background);
384 putCharXY(getWidth() - 2, getHeight() - 1, cBottomRight, background);
385
386 // Draw the box lines
387 hLineXY(1 + 1, 0, getWidth() - 4, cHSide, background);
388 hLineXY(1 + 1, getHeight() - 1, getWidth() - 4, cHSide, background);
389
390 // Draw a shadow
a69ed767 391 drawBoxShadow(0, 0, getWidth(), getHeight());
d36057df
KL
392 }
393
394 // ------------------------------------------------------------------------
395 // TMenu ------------------------------------------------------------------
396 // ------------------------------------------------------------------------
397
398 /**
399 * Set the menu title X position.
400 *
401 * @param titleX the position
402 */
403 public void setTitleX(final int titleX) {
404 this.titleX = titleX;
405 }
406
407 /**
408 * Get the menu title X position.
409 *
410 * @return the position
411 */
412 public int getTitleX() {
413 return titleX;
414 }
415
416 /**
417 * Get the mnemonic string.
418 *
419 * @return the full mnemonic string
420 */
421 public MnemonicString getMnemonic() {
422 return mnemonic;
423 }
424
928811d8 425 /**
efb7af1f 426 * Convenience function to add a menu item.
928811d8
KL
427 *
428 * @param id menu item ID. Must be greater than 1024.
429 * @param label menu item label
928811d8
KL
430 * @return the new menu item
431 */
329fd62e 432 public TMenuItem addItem(final int id, final String label) {
928811d8 433 assert (id >= 1024);
efb7af1f 434 return addItemInternal(id, label, null);
928811d8
KL
435 }
436
a69ed767
KL
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
928811d8
KL
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 */
329fd62e 460 public TMenuItem addItem(final int id, final String label,
928811d8
KL
461 final TKeypress key) {
462
efb7af1f
KL
463 assert (id >= 1024);
464 return addItemInternal(id, label, key);
928811d8
KL
465 }
466
68c5cd6b
KL
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
928811d8 484 /**
efb7af1f 485 * Convenience function to add a custom menu item.
928811d8
KL
486 *
487 * @param id menu item ID. Must be greater than 1024.
488 * @param label menu item label
efb7af1f 489 * @param key global keyboard accelerator
928811d8
KL
490 * @return the new menu item
491 */
efb7af1f
KL
492 private TMenuItem addItemInternal(final int id, final String label,
493 final TKeypress key) {
928811d8 494
d36057df
KL
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
928811d8
KL
510 int newY = getChildren().size() + 1;
511 assert (newY < getHeight());
512
513 TMenuItem menuItem = new TMenuItem(this, id, 1, newY, label);
efb7af1f 514 menuItem.setKey(key);
d36057df 515 menuItem.setEnabled(enabled);
928811d8
KL
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 }
efb7af1f 523 getApplication().addMenuItem(menuItem);
928811d8
KL
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 */
329fd62e 536 public TMenuItem addDefaultItem(final int id) {
d36057df
KL
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) {
928811d8
KL
549 assert (id >= 0);
550 assert (id < 1024);
551
552 String label;
553 TKeypress key = null;
77961919
KL
554 boolean checkable = false;
555 boolean checked = false;
928811d8
KL
556
557 switch (id) {
558
e23ea538
KL
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_CHANGE_FONT:
568 label = i18n.getString("menuChangeFont");
569 break;
570
571 case MID_NEW:
572 label = i18n.getString("menuNew");
573 break;
574
928811d8 575 case MID_EXIT:
339652cc 576 label = i18n.getString("menuExit");
928811d8
KL
577 key = kbAltX;
578 break;
579
580 case MID_SHELL:
339652cc 581 label = i18n.getString("menuShell");
928811d8
KL
582 break;
583
584 case MID_OPEN_FILE:
339652cc 585 label = i18n.getString("menuOpen");
b2d49e0f 586 key = kbF3;
928811d8
KL
587 break;
588
589 case MID_CUT:
339652cc 590 label = i18n.getString("menuCut");
928811d8
KL
591 key = kbCtrlX;
592 break;
593 case MID_COPY:
339652cc 594 label = i18n.getString("menuCopy");
928811d8
KL
595 key = kbCtrlC;
596 break;
597 case MID_PASTE:
339652cc 598 label = i18n.getString("menuPaste");
928811d8
KL
599 key = kbCtrlV;
600 break;
601 case MID_CLEAR:
339652cc 602 label = i18n.getString("menuClear");
30bd4abd 603 // key = kbDel;
928811d8
KL
604 break;
605
d36057df
KL
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");
978a5d8f 614 key = kbCtrlL;
d36057df
KL
615 break;
616 case MID_GOTO_LINE:
617 label = i18n.getString("menuGotoLine");
d36057df
KL
618 break;
619
928811d8 620 case MID_TILE:
339652cc 621 label = i18n.getString("menuWindowTile");
928811d8
KL
622 break;
623 case MID_CASCADE:
339652cc 624 label = i18n.getString("menuWindowCascade");
928811d8
KL
625 break;
626 case MID_CLOSE_ALL:
339652cc 627 label = i18n.getString("menuWindowCloseAll");
928811d8
KL
628 break;
629 case MID_WINDOW_MOVE:
339652cc 630 label = i18n.getString("menuWindowMove");
928811d8
KL
631 key = kbCtrlF5;
632 break;
633 case MID_WINDOW_ZOOM:
339652cc 634 label = i18n.getString("menuWindowZoom");
928811d8
KL
635 key = kbF5;
636 break;
637 case MID_WINDOW_NEXT:
339652cc 638 label = i18n.getString("menuWindowNext");
928811d8
KL
639 key = kbF6;
640 break;
641 case MID_WINDOW_PREVIOUS:
339652cc 642 label = i18n.getString("menuWindowPrevious");
928811d8
KL
643 key = kbShiftF6;
644 break;
645 case MID_WINDOW_CLOSE:
339652cc 646 label = i18n.getString("menuWindowClose");
5dfd1c11 647 key = kbCtrlW;
928811d8
KL
648 break;
649
55d2b2c2 650 case MID_HELP_CONTENTS:
339652cc 651 label = i18n.getString("menuHelpContents");
55d2b2c2
KL
652 break;
653 case MID_HELP_INDEX:
339652cc 654 label = i18n.getString("menuHelpIndex");
55d2b2c2
KL
655 key = kbShiftF1;
656 break;
657 case MID_HELP_SEARCH:
339652cc 658 label = i18n.getString("menuHelpSearch");
55d2b2c2
KL
659 key = kbCtrlF1;
660 break;
661 case MID_HELP_PREVIOUS:
339652cc 662 label = i18n.getString("menuHelpPrevious");
55d2b2c2
KL
663 key = kbAltF1;
664 break;
665 case MID_HELP_HELP:
339652cc 666 label = i18n.getString("menuHelpHelp");
55d2b2c2
KL
667 break;
668 case MID_HELP_ACTIVE_FILE:
339652cc 669 label = i18n.getString("menuHelpActive");
55d2b2c2
KL
670 break;
671 case MID_ABOUT:
339652cc 672 label = i18n.getString("menuHelpAbout");
55d2b2c2
KL
673 break;
674
2b427404
KL
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;
77961919
KL
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
1dac6b8d
KL
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;
e9bb3c1e
KL
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;
1dac6b8d
KL
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;
f528c340
KL
764 case MID_TABLE_FILE_OPEN_CSV:
765 label = i18n.getString("menuTableFileOpenCsv");
766 break;
1dac6b8d
KL
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
928811d8
KL
774 default:
775 throw new IllegalArgumentException("Invalid menu ID: " + id);
776 }
777
77961919
KL
778 TMenuItem item = addItemInternal(id, label, key, enabled);
779 item.setCheckable(checkable);
780 return item;
928811d8
KL
781 }
782
783 /**
784 * Convenience function to add a menu separator.
785 */
329fd62e 786 public void addSeparator() {
928811d8
KL
787 int newY = getChildren().size() + 1;
788 assert (newY < getHeight());
789
cf9af8df
KL
790 // We just have to construct it, don't need to hang onto what it
791 // makes.
792 new TMenuSeparator(this, 1, newY);
928811d8
KL
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,
43ad7b6c 800 * denoted by prefixing a letter with "&amp;", e.g. "&amp;File"
928811d8
KL
801 * @return the new sub-menu
802 */
329fd62e 803 public TSubMenu addSubMenu(final String title) {
928811d8
KL
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}