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