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