Commit | Line | Data |
---|---|---|
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 | */ | |
29 | package jexer.menu; | |
30 | ||
339652cc KL |
31 | import java.util.ResourceBundle; |
32 | ||
928811d8 KL |
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; | |
9f613a0c | 40 | import jexer.bits.StringUtils; |
928811d8 KL |
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 | */ | |
051e2913 | 48 | public class TMenu extends TWindow { |
928811d8 | 49 | |
339652cc KL |
50 | /** |
51 | * Translated strings. | |
52 | */ | |
53 | private static final ResourceBundle i18n = ResourceBundle.getBundle(TMenu.class.getName()); | |
54 | ||
d36057df KL |
55 | // ------------------------------------------------------------------------ |
56 | // Constants -------------------------------------------------------------- | |
57 | // ------------------------------------------------------------------------ | |
928811d8 KL |
58 | |
59 | // Reserved menu item IDs | |
60 | public static final int MID_UNUSED = -1; | |
61 | ||
e23ea538 KL |
62 | // Tools menu |
63 | public static final int MID_REPAINT = 1; | |
64 | public static final int MID_VIEW_IMAGE = 2; | |
a75902fa | 65 | public static final int MID_SCREEN_OPTIONS = 3; |
e23ea538 | 66 | |
928811d8 | 67 | // File menu |
e23ea538 KL |
68 | public static final int MID_NEW = 10; |
69 | public static final int MID_EXIT = 11; | |
928811d8 | 70 | public static final int MID_QUIT = MID_EXIT; |
e23ea538 KL |
71 | public static final int MID_OPEN_FILE = 12; |
72 | public static final int MID_SHELL = 13; | |
928811d8 KL |
73 | |
74 | // Edit menu | |
e23ea538 KL |
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; | |
928811d8 | 79 | |
d36057df | 80 | // Search menu |
e23ea538 KL |
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; | |
d36057df | 85 | |
928811d8 | 86 | // Window menu |
e23ea538 KL |
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; | |
928811d8 | 95 | |
55d2b2c2 | 96 | // Help menu |
e23ea538 KL |
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; | |
1c19fdea | 104 | |
1dac6b8d | 105 | // Table menu |
e9bb3c1e KL |
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; | |
f528c340 KL |
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; | |
1dac6b8d | 135 | |
5ffeabcc KL |
136 | // Miscellaneous |
137 | public static final int MID_SPLIT_VERTICAL = 200; | |
138 | public static final int MID_SPLIT_HORIZONTAL = 201; | |
139 | ||
d36057df KL |
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 | ||
928811d8 KL |
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 | |
43ad7b6c KL |
170 | * shortcut (mnemonic), denoted by prefixing a letter with "&", |
171 | * e.g. "&File" | |
928811d8 KL |
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 | ||
928811d8 KL |
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 | |
9f613a0c | 185 | setWidth(StringUtils.width(getTitle()) + 4); |
928811d8 KL |
186 | setHeight(2); |
187 | ||
188 | setActive(false); | |
189 | } | |
190 | ||
d36057df KL |
191 | // ------------------------------------------------------------------------ |
192 | // Event handlers --------------------------------------------------------- | |
193 | // ------------------------------------------------------------------------ | |
928811d8 KL |
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; | |
a69ed767 | 203 | super.onMouseDown(mouse); |
928811d8 KL |
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; | |
928811d8 KL |
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; | |
928811d8 KL |
252 | |
253 | // See if we should activate a different menu item | |
254 | for (TWidget widget: getChildren()) { | |
7c870d89 | 255 | if ((mouse.isMouse1()) |
928811d8 KL |
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) { | |
91c9a837 KL |
275 | |
276 | /* | |
277 | System.err.printf("keypress: %s active child: %s\n", keypress, | |
278 | getActiveChild()); | |
382bc294 | 279 | */ |
91c9a837 KL |
280 | |
281 | if (getActiveChild() != this) { | |
382bc294 | 282 | if (getActiveChild() instanceof TMenu) { |
928811d8 KL |
283 | getActiveChild().onKeypress(keypress); |
284 | return; | |
285 | } | |
382bc294 KL |
286 | |
287 | if (getActiveChild() instanceof TSubMenu) { | |
288 | TSubMenu subMenu = (TSubMenu) getActiveChild(); | |
289 | if (subMenu.menu.isActive()) { | |
290 | subMenu.onKeypress(keypress); | |
291 | return; | |
292 | } | |
293 | } | |
928811d8 KL |
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)) { | |
91c9a837 | 309 | getApplication().switchMenu(true); |
928811d8 KL |
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 | |
7c870d89 KL |
322 | if (!keypress.getKey().isFnKey() |
323 | && !keypress.getKey().isAlt() | |
324 | && !keypress.getKey().isCtrl()) { | |
382bc294 KL |
325 | |
326 | // System.err.println("Checking children for mnemonic..."); | |
327 | ||
928811d8 KL |
328 | for (TWidget widget: getChildren()) { |
329 | TMenuItem item = (TMenuItem) widget; | |
382bc294 KL |
330 | if ((item.isEnabled() == true) |
331 | && (item.getMnemonic() != null) | |
928811d8 | 332 | && (Character.toLowerCase(item.getMnemonic().getShortcut()) |
7c870d89 | 333 | == Character.toLowerCase(keypress.getKey().getChar())) |
928811d8 | 334 | ) { |
382bc294 KL |
335 | // System.err.println("activate: " + item); |
336 | ||
928811d8 KL |
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()) { | |
7c870d89 | 347 | if (widget.isActive()) { |
928811d8 KL |
348 | widget.handleEvent(keypress); |
349 | return; | |
350 | } | |
351 | } | |
352 | } | |
353 | ||
d36057df KL |
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 | |
a69ed767 | 396 | drawBoxShadow(0, 0, getWidth(), getHeight()); |
d36057df KL |
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 | ||
928811d8 | 430 | /** |
efb7af1f | 431 | * Convenience function to add a menu item. |
928811d8 KL |
432 | * |
433 | * @param id menu item ID. Must be greater than 1024. | |
434 | * @param label menu item label | |
928811d8 KL |
435 | * @return the new menu item |
436 | */ | |
329fd62e | 437 | public TMenuItem addItem(final int id, final String label) { |
efb7af1f | 438 | return addItemInternal(id, label, null); |
928811d8 KL |
439 | } |
440 | ||
a69ed767 KL |
441 | /** |
442 | * Convenience function to add a menu item. | |
443 | * | |
444 | * @param id menu item ID. Must be greater than 1024. | |
445 | * @param label menu item label | |
446 | * @param enabled default state for enabled | |
447 | * @return the new menu item | |
448 | */ | |
449 | public TMenuItem addItem(final int id, final String label, | |
450 | final boolean enabled) { | |
451 | ||
452 | assert (id >= 1024); | |
453 | return addItemInternal(id, label, null, enabled); | |
454 | } | |
455 | ||
928811d8 KL |
456 | /** |
457 | * Convenience function to add a custom menu item. | |
458 | * | |
459 | * @param id menu item ID. Must be greater than 1024. | |
460 | * @param label menu item label | |
461 | * @param key global keyboard accelerator | |
462 | * @return the new menu item | |
463 | */ | |
329fd62e | 464 | public TMenuItem addItem(final int id, final String label, |
928811d8 KL |
465 | final TKeypress key) { |
466 | ||
efb7af1f KL |
467 | assert (id >= 1024); |
468 | return addItemInternal(id, label, key); | |
928811d8 KL |
469 | } |
470 | ||
68c5cd6b KL |
471 | /** |
472 | * Convenience function to add a custom menu item. | |
473 | * | |
474 | * @param id menu item ID. Must be greater than 1024. | |
475 | * @param label menu item label | |
476 | * @param key global keyboard accelerator | |
477 | * @param enabled default state for enabled | |
478 | * @return the new menu item | |
479 | */ | |
480 | public TMenuItem addItem(final int id, final String label, | |
481 | final TKeypress key, final boolean enabled) { | |
482 | ||
483 | TMenuItem item = addItem(id, label, key); | |
484 | item.setEnabled(enabled); | |
485 | return item; | |
486 | } | |
487 | ||
928811d8 | 488 | /** |
efb7af1f | 489 | * Convenience function to add a custom menu item. |
928811d8 KL |
490 | * |
491 | * @param id menu item ID. Must be greater than 1024. | |
492 | * @param label menu item label | |
efb7af1f | 493 | * @param key global keyboard accelerator |
928811d8 KL |
494 | * @return the new menu item |
495 | */ | |
efb7af1f KL |
496 | private TMenuItem addItemInternal(final int id, final String label, |
497 | final TKeypress key) { | |
928811d8 | 498 | |
d36057df KL |
499 | return addItemInternal(id, label, key, true); |
500 | } | |
501 | ||
502 | /** | |
503 | * Convenience function to add a custom menu item. | |
504 | * | |
505 | * @param id menu item ID. Must be greater than 1024. | |
506 | * @param label menu item label | |
507 | * @param key global keyboard accelerator | |
508 | * @param enabled default state for enabled | |
509 | * @return the new menu item | |
510 | */ | |
511 | private TMenuItem addItemInternal(final int id, final String label, | |
512 | final TKeypress key, final boolean enabled) { | |
513 | ||
928811d8 KL |
514 | int newY = getChildren().size() + 1; |
515 | assert (newY < getHeight()); | |
516 | ||
517 | TMenuItem menuItem = new TMenuItem(this, id, 1, newY, label); | |
efb7af1f | 518 | menuItem.setKey(key); |
d36057df | 519 | menuItem.setEnabled(enabled); |
928811d8 KL |
520 | setHeight(getHeight() + 1); |
521 | if (menuItem.getWidth() + 2 > getWidth()) { | |
522 | setWidth(menuItem.getWidth() + 2); | |
523 | } | |
524 | for (TWidget widget: getChildren()) { | |
525 | widget.setWidth(getWidth() - 2); | |
526 | } | |
efb7af1f | 527 | getApplication().addMenuItem(menuItem); |
928811d8 KL |
528 | getApplication().recomputeMenuX(); |
529 | activate(0); | |
530 | return menuItem; | |
531 | } | |
532 | ||
533 | /** | |
534 | * Convenience function to add one of the default menu items. | |
535 | * | |
536 | * @param id menu item ID. Must be between 0 (inclusive) and 1023 | |
537 | * (inclusive). | |
538 | * @return the new menu item | |
539 | */ | |
329fd62e | 540 | public TMenuItem addDefaultItem(final int id) { |
d36057df KL |
541 | return addDefaultItem(id, true); |
542 | } | |
543 | ||
544 | /** | |
545 | * Convenience function to add one of the default menu items. | |
546 | * | |
547 | * @param id menu item ID. Must be between 0 (inclusive) and 1023 | |
548 | * (inclusive). | |
549 | * @param enabled default state for enabled | |
550 | * @return the new menu item | |
551 | */ | |
552 | public TMenuItem addDefaultItem(final int id, final boolean enabled) { | |
928811d8 KL |
553 | assert (id >= 0); |
554 | assert (id < 1024); | |
555 | ||
556 | String label; | |
557 | TKeypress key = null; | |
77961919 KL |
558 | boolean checkable = false; |
559 | boolean checked = false; | |
928811d8 KL |
560 | |
561 | switch (id) { | |
562 | ||
e23ea538 KL |
563 | case MID_REPAINT: |
564 | label = i18n.getString("menuRepaintDesktop"); | |
565 | break; | |
566 | ||
567 | case MID_VIEW_IMAGE: | |
568 | label = i18n.getString("menuViewImage"); | |
569 | break; | |
570 | ||
a75902fa KL |
571 | case MID_SCREEN_OPTIONS: |
572 | label = i18n.getString("menuScreenOptions"); | |
e23ea538 KL |
573 | break; |
574 | ||
575 | case MID_NEW: | |
576 | label = i18n.getString("menuNew"); | |
577 | break; | |
578 | ||
928811d8 | 579 | case MID_EXIT: |
339652cc | 580 | label = i18n.getString("menuExit"); |
928811d8 KL |
581 | key = kbAltX; |
582 | break; | |
583 | ||
584 | case MID_SHELL: | |
339652cc | 585 | label = i18n.getString("menuShell"); |
928811d8 KL |
586 | break; |
587 | ||
588 | case MID_OPEN_FILE: | |
339652cc | 589 | label = i18n.getString("menuOpen"); |
b2d49e0f | 590 | key = kbF3; |
928811d8 KL |
591 | break; |
592 | ||
593 | case MID_CUT: | |
339652cc | 594 | label = i18n.getString("menuCut"); |
928811d8 KL |
595 | key = kbCtrlX; |
596 | break; | |
597 | case MID_COPY: | |
339652cc | 598 | label = i18n.getString("menuCopy"); |
928811d8 KL |
599 | key = kbCtrlC; |
600 | break; | |
601 | case MID_PASTE: | |
339652cc | 602 | label = i18n.getString("menuPaste"); |
928811d8 KL |
603 | key = kbCtrlV; |
604 | break; | |
605 | case MID_CLEAR: | |
339652cc | 606 | label = i18n.getString("menuClear"); |
30bd4abd | 607 | // key = kbDel; |
928811d8 KL |
608 | break; |
609 | ||
d36057df KL |
610 | case MID_FIND: |
611 | label = i18n.getString("menuFind"); | |
612 | break; | |
613 | case MID_REPLACE: | |
614 | label = i18n.getString("menuReplace"); | |
615 | break; | |
616 | case MID_SEARCH_AGAIN: | |
617 | label = i18n.getString("menuSearchAgain"); | |
978a5d8f | 618 | key = kbCtrlL; |
d36057df KL |
619 | break; |
620 | case MID_GOTO_LINE: | |
621 | label = i18n.getString("menuGotoLine"); | |
d36057df KL |
622 | break; |
623 | ||
928811d8 | 624 | case MID_TILE: |
339652cc | 625 | label = i18n.getString("menuWindowTile"); |
928811d8 KL |
626 | break; |
627 | case MID_CASCADE: | |
339652cc | 628 | label = i18n.getString("menuWindowCascade"); |
928811d8 KL |
629 | break; |
630 | case MID_CLOSE_ALL: | |
339652cc | 631 | label = i18n.getString("menuWindowCloseAll"); |
928811d8 KL |
632 | break; |
633 | case MID_WINDOW_MOVE: | |
339652cc | 634 | label = i18n.getString("menuWindowMove"); |
928811d8 KL |
635 | key = kbCtrlF5; |
636 | break; | |
637 | case MID_WINDOW_ZOOM: | |
339652cc | 638 | label = i18n.getString("menuWindowZoom"); |
928811d8 KL |
639 | key = kbF5; |
640 | break; | |
641 | case MID_WINDOW_NEXT: | |
339652cc | 642 | label = i18n.getString("menuWindowNext"); |
928811d8 KL |
643 | key = kbF6; |
644 | break; | |
645 | case MID_WINDOW_PREVIOUS: | |
339652cc | 646 | label = i18n.getString("menuWindowPrevious"); |
928811d8 KL |
647 | key = kbShiftF6; |
648 | break; | |
649 | case MID_WINDOW_CLOSE: | |
339652cc | 650 | label = i18n.getString("menuWindowClose"); |
5dfd1c11 | 651 | key = kbCtrlW; |
928811d8 KL |
652 | break; |
653 | ||
55d2b2c2 | 654 | case MID_HELP_CONTENTS: |
339652cc | 655 | label = i18n.getString("menuHelpContents"); |
55d2b2c2 KL |
656 | break; |
657 | case MID_HELP_INDEX: | |
339652cc | 658 | label = i18n.getString("menuHelpIndex"); |
55d2b2c2 KL |
659 | key = kbShiftF1; |
660 | break; | |
661 | case MID_HELP_SEARCH: | |
339652cc | 662 | label = i18n.getString("menuHelpSearch"); |
55d2b2c2 KL |
663 | key = kbCtrlF1; |
664 | break; | |
665 | case MID_HELP_PREVIOUS: | |
339652cc | 666 | label = i18n.getString("menuHelpPrevious"); |
55d2b2c2 KL |
667 | key = kbAltF1; |
668 | break; | |
669 | case MID_HELP_HELP: | |
339652cc | 670 | label = i18n.getString("menuHelpHelp"); |
55d2b2c2 KL |
671 | break; |
672 | case MID_HELP_ACTIVE_FILE: | |
339652cc | 673 | label = i18n.getString("menuHelpActive"); |
55d2b2c2 KL |
674 | break; |
675 | case MID_ABOUT: | |
339652cc | 676 | label = i18n.getString("menuHelpAbout"); |
55d2b2c2 KL |
677 | break; |
678 | ||
2b427404 KL |
679 | case MID_TABLE_RENAME_COLUMN: |
680 | label = i18n.getString("menuTableRenameColumn"); | |
681 | break; | |
682 | case MID_TABLE_RENAME_ROW: | |
683 | label = i18n.getString("menuTableRenameRow"); | |
684 | break; | |
77961919 KL |
685 | case MID_TABLE_VIEW_ROW_LABELS: |
686 | label = i18n.getString("menuTableViewRowLabels"); | |
687 | checkable = true; | |
688 | checked = true; | |
689 | break; | |
690 | case MID_TABLE_VIEW_COLUMN_LABELS: | |
691 | label = i18n.getString("menuTableViewColumnLabels"); | |
692 | checkable = true; | |
693 | checked = true; | |
694 | break; | |
695 | case MID_TABLE_VIEW_HIGHLIGHT_ROW: | |
696 | label = i18n.getString("menuTableViewHighlightRow"); | |
697 | checkable = true; | |
698 | checked = true; | |
699 | break; | |
700 | case MID_TABLE_VIEW_HIGHLIGHT_COLUMN: | |
701 | label = i18n.getString("menuTableViewHighlightColumn"); | |
702 | checkable = true; | |
703 | checked = true; | |
704 | break; | |
705 | ||
1dac6b8d KL |
706 | case MID_TABLE_BORDER_NONE: |
707 | label = i18n.getString("menuTableBorderNone"); | |
708 | break; | |
709 | case MID_TABLE_BORDER_ALL: | |
710 | label = i18n.getString("menuTableBorderAll"); | |
711 | break; | |
e9bb3c1e KL |
712 | case MID_TABLE_BORDER_CELL_NONE: |
713 | label = i18n.getString("menuTableBorderCellNone"); | |
714 | break; | |
715 | case MID_TABLE_BORDER_CELL_ALL: | |
716 | label = i18n.getString("menuTableBorderCellAll"); | |
717 | break; | |
1dac6b8d KL |
718 | case MID_TABLE_BORDER_RIGHT: |
719 | label = i18n.getString("menuTableBorderRight"); | |
720 | break; | |
721 | case MID_TABLE_BORDER_LEFT: | |
722 | label = i18n.getString("menuTableBorderLeft"); | |
723 | break; | |
724 | case MID_TABLE_BORDER_TOP: | |
725 | label = i18n.getString("menuTableBorderTop"); | |
726 | break; | |
727 | case MID_TABLE_BORDER_BOTTOM: | |
728 | label = i18n.getString("menuTableBorderBottom"); | |
729 | break; | |
730 | case MID_TABLE_BORDER_DOUBLE_BOTTOM: | |
731 | label = i18n.getString("menuTableBorderDoubleBottom"); | |
732 | break; | |
733 | case MID_TABLE_BORDER_THICK_BOTTOM: | |
734 | label = i18n.getString("menuTableBorderThickBottom"); | |
735 | break; | |
736 | case MID_TABLE_DELETE_LEFT: | |
737 | label = i18n.getString("menuTableDeleteLeft"); | |
738 | break; | |
739 | case MID_TABLE_DELETE_UP: | |
740 | label = i18n.getString("menuTableDeleteUp"); | |
741 | break; | |
742 | case MID_TABLE_DELETE_ROW: | |
743 | label = i18n.getString("menuTableDeleteRow"); | |
744 | break; | |
745 | case MID_TABLE_DELETE_COLUMN: | |
746 | label = i18n.getString("menuTableDeleteColumn"); | |
747 | break; | |
748 | case MID_TABLE_INSERT_LEFT: | |
749 | label = i18n.getString("menuTableInsertLeft"); | |
750 | break; | |
751 | case MID_TABLE_INSERT_RIGHT: | |
752 | label = i18n.getString("menuTableInsertRight"); | |
753 | break; | |
754 | case MID_TABLE_INSERT_ABOVE: | |
755 | label = i18n.getString("menuTableInsertAbove"); | |
756 | break; | |
757 | case MID_TABLE_INSERT_BELOW: | |
758 | label = i18n.getString("menuTableInsertBelow"); | |
759 | break; | |
760 | case MID_TABLE_COLUMN_NARROW: | |
761 | label = i18n.getString("menuTableColumnNarrow"); | |
762 | key = kbShiftLeft; | |
763 | break; | |
764 | case MID_TABLE_COLUMN_WIDEN: | |
765 | label = i18n.getString("menuTableColumnWiden"); | |
766 | key = kbShiftRight; | |
767 | break; | |
f528c340 KL |
768 | case MID_TABLE_FILE_OPEN_CSV: |
769 | label = i18n.getString("menuTableFileOpenCsv"); | |
770 | break; | |
1dac6b8d KL |
771 | case MID_TABLE_FILE_SAVE_CSV: |
772 | label = i18n.getString("menuTableFileSaveCsv"); | |
773 | break; | |
774 | case MID_TABLE_FILE_SAVE_TEXT: | |
775 | label = i18n.getString("menuTableFileSaveText"); | |
776 | break; | |
777 | ||
5ffeabcc KL |
778 | case MID_SPLIT_VERTICAL: |
779 | label = i18n.getString("menuSplitVertical"); | |
780 | break; | |
781 | case MID_SPLIT_HORIZONTAL: | |
782 | label = i18n.getString("menuSplitHorizontal"); | |
783 | break; | |
784 | ||
928811d8 KL |
785 | default: |
786 | throw new IllegalArgumentException("Invalid menu ID: " + id); | |
787 | } | |
788 | ||
77961919 KL |
789 | TMenuItem item = addItemInternal(id, label, key, enabled); |
790 | item.setCheckable(checkable); | |
791 | return item; | |
928811d8 KL |
792 | } |
793 | ||
794 | /** | |
795 | * Convenience function to add a menu separator. | |
796 | */ | |
329fd62e | 797 | public void addSeparator() { |
928811d8 KL |
798 | int newY = getChildren().size() + 1; |
799 | assert (newY < getHeight()); | |
800 | ||
cf9af8df KL |
801 | // We just have to construct it, don't need to hang onto what it |
802 | // makes. | |
803 | new TMenuSeparator(this, 1, newY); | |
928811d8 KL |
804 | setHeight(getHeight() + 1); |
805 | } | |
806 | ||
807 | /** | |
808 | * Convenience function to add a sub-menu. | |
809 | * | |
810 | * @param title menu title. Title must contain a keyboard shortcut, | |
43ad7b6c | 811 | * denoted by prefixing a letter with "&", e.g. "&File" |
928811d8 KL |
812 | * @return the new sub-menu |
813 | */ | |
329fd62e | 814 | public TSubMenu addSubMenu(final String title) { |
928811d8 KL |
815 | int newY = getChildren().size() + 1; |
816 | assert (newY < getHeight()); | |
817 | ||
818 | TSubMenu subMenu = new TSubMenu(this, title, 1, newY); | |
819 | setHeight(getHeight() + 1); | |
820 | if (subMenu.getWidth() + 2 > getWidth()) { | |
821 | setWidth(subMenu.getWidth() + 2); | |
822 | } | |
823 | for (TWidget widget: getChildren()) { | |
824 | widget.setWidth(getWidth() - 2); | |
825 | } | |
826 | getApplication().recomputeMenuX(); | |
827 | activate(0); | |
828 | subMenu.menu.setX(getX() + getWidth() - 2); | |
829 | ||
830 | return subMenu; | |
831 | } | |
832 | ||
833 | } |