Commit | Line | Data |
---|---|---|
daa4106c | 1 | /* |
7b5261bc | 2 | * Jexer - Java Text User Interface |
7d4115a5 | 3 | * |
e16dda65 | 4 | * The MIT License (MIT) |
7d4115a5 | 5 | * |
a2018e99 | 6 | * Copyright (C) 2017 Kevin Lamonte |
7d4115a5 | 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: | |
7d4115a5 | 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. | |
7d4115a5 | 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. | |
7b5261bc KL |
25 | * |
26 | * @author Kevin Lamonte [kevin.lamonte@gmail.com] | |
27 | * @version 1 | |
7d4115a5 KL |
28 | */ |
29 | package jexer; | |
30 | ||
1978ad50 | 31 | import java.io.File; |
4328bb42 | 32 | import java.io.InputStream; |
0d47c546 | 33 | import java.io.IOException; |
4328bb42 | 34 | import java.io.OutputStream; |
6985c572 KL |
35 | import java.io.PrintWriter; |
36 | import java.io.Reader; | |
4328bb42 | 37 | import java.io.UnsupportedEncodingException; |
339652cc | 38 | import java.text.MessageFormat; |
a06459bd | 39 | import java.util.Collections; |
d502a0e9 | 40 | import java.util.Date; |
e826b451 | 41 | import java.util.HashMap; |
c6940ed9 | 42 | import java.util.ArrayList; |
4328bb42 KL |
43 | import java.util.LinkedList; |
44 | import java.util.List; | |
e826b451 | 45 | import java.util.Map; |
339652cc | 46 | import java.util.ResourceBundle; |
4328bb42 KL |
47 | |
48 | import jexer.bits.CellAttributes; | |
49 | import jexer.bits.ColorTheme; | |
4328bb42 KL |
50 | import jexer.event.TCommandEvent; |
51 | import jexer.event.TInputEvent; | |
52 | import jexer.event.TKeypressEvent; | |
fca67db0 | 53 | import jexer.event.TMenuEvent; |
4328bb42 KL |
54 | import jexer.event.TMouseEvent; |
55 | import jexer.event.TResizeEvent; | |
56 | import jexer.backend.Backend; | |
42873e30 | 57 | import jexer.backend.Screen; |
be72cb5c | 58 | import jexer.backend.MultiBackend; |
a4406f4e | 59 | import jexer.backend.SwingBackend; |
4328bb42 | 60 | import jexer.backend.ECMA48Backend; |
3e074355 | 61 | import jexer.backend.TWindowBackend; |
928811d8 KL |
62 | import jexer.menu.TMenu; |
63 | import jexer.menu.TMenuItem; | |
4328bb42 | 64 | import static jexer.TCommand.*; |
2ce6dab2 | 65 | import static jexer.TKeypress.*; |
4328bb42 | 66 | |
7d4115a5 | 67 | /** |
42873e30 KL |
68 | * TApplication is the main driver class for a full Text User Interface |
69 | * application. It manages windows, provides a menu bar and status bar, and | |
70 | * processes events received from the user. | |
7d4115a5 | 71 | */ |
a4406f4e | 72 | public class TApplication implements Runnable { |
7d4115a5 | 73 | |
339652cc KL |
74 | /** |
75 | * Translated strings. | |
76 | */ | |
77 | private static final ResourceBundle i18n = ResourceBundle.getBundle(TApplication.class.getName()); | |
78 | ||
2ce6dab2 KL |
79 | // ------------------------------------------------------------------------ |
80 | // Public constants ------------------------------------------------------- | |
81 | // ------------------------------------------------------------------------ | |
82 | ||
99144c71 KL |
83 | /** |
84 | * If true, emit thread stuff to System.err. | |
85 | */ | |
86 | private static final boolean debugThreads = false; | |
87 | ||
a83fea2b KL |
88 | /** |
89 | * If true, emit events being processed to System.err. | |
90 | */ | |
91 | private static final boolean debugEvents = false; | |
92 | ||
a7986f7b KL |
93 | /** |
94 | * If true, do "smart placement" on new windows that are not specified to | |
95 | * be centered. | |
96 | */ | |
97 | private static final boolean smartWindowPlacement = true; | |
98 | ||
a4406f4e KL |
99 | /** |
100 | * Two backend types are available. | |
101 | */ | |
102 | public static enum BackendType { | |
103 | /** | |
104 | * A Swing JFrame. | |
105 | */ | |
106 | SWING, | |
107 | ||
108 | /** | |
109 | * An ECMA48 / ANSI X3.64 / XTERM style terminal. | |
110 | */ | |
111 | ECMA48, | |
112 | ||
113 | /** | |
329fd62e | 114 | * Synonym for ECMA48. |
a4406f4e KL |
115 | */ |
116 | XTERM | |
117 | } | |
118 | ||
2ce6dab2 KL |
119 | // ------------------------------------------------------------------------ |
120 | // Primary/secondary event handlers --------------------------------------- | |
121 | // ------------------------------------------------------------------------ | |
122 | ||
c6940ed9 KL |
123 | /** |
124 | * WidgetEventHandler is the main event consumer loop. There are at most | |
125 | * two such threads in existence: the primary for normal case and a | |
126 | * secondary that is used for TMessageBox, TInputBox, and similar. | |
127 | */ | |
128 | private class WidgetEventHandler implements Runnable { | |
129 | /** | |
130 | * The main application. | |
131 | */ | |
132 | private TApplication application; | |
133 | ||
134 | /** | |
135 | * Whether or not this WidgetEventHandler is the primary or secondary | |
136 | * thread. | |
137 | */ | |
138 | private boolean primary = true; | |
139 | ||
140 | /** | |
141 | * Public constructor. | |
142 | * | |
143 | * @param application the main application | |
144 | * @param primary if true, this is the primary event handler thread | |
145 | */ | |
146 | public WidgetEventHandler(final TApplication application, | |
147 | final boolean primary) { | |
148 | ||
149 | this.application = application; | |
150 | this.primary = primary; | |
151 | } | |
152 | ||
153 | /** | |
154 | * The consumer loop. | |
155 | */ | |
156 | public void run() { | |
be72cb5c | 157 | boolean first = true; |
c6940ed9 KL |
158 | |
159 | // Loop forever | |
160 | while (!application.quit) { | |
161 | ||
162 | // Wait until application notifies me | |
163 | while (!application.quit) { | |
164 | try { | |
165 | synchronized (application.drainEventQueue) { | |
166 | if (application.drainEventQueue.size() > 0) { | |
167 | break; | |
168 | } | |
169 | } | |
92554d64 | 170 | |
be72cb5c KL |
171 | long timeout = 0; |
172 | if (first) { | |
173 | first = false; | |
174 | } else { | |
175 | timeout = application.getSleepTime(1000); | |
176 | } | |
177 | ||
178 | if (timeout == 0) { | |
179 | // A timer needs to fire, break out. | |
180 | break; | |
181 | } | |
92554d64 | 182 | |
be72cb5c KL |
183 | if (debugThreads) { |
184 | System.err.printf("%d %s %s sleep %d millis\n", | |
185 | System.currentTimeMillis(), this, | |
186 | primary ? "primary" : "secondary", timeout); | |
187 | } | |
92554d64 | 188 | |
be72cb5c KL |
189 | synchronized (this) { |
190 | this.wait(timeout); | |
191 | } | |
92554d64 | 192 | |
be72cb5c KL |
193 | if (debugThreads) { |
194 | System.err.printf("%d %s %s AWAKE\n", | |
195 | System.currentTimeMillis(), this, | |
196 | primary ? "primary" : "secondary"); | |
197 | } | |
198 | ||
199 | if ((!primary) | |
200 | && (application.secondaryEventReceiver == null) | |
201 | ) { | |
202 | // Secondary thread, emergency exit. If we got | |
203 | // here then something went wrong with the | |
204 | // handoff between yield() and closeWindow(). | |
205 | synchronized (application.primaryEventHandler) { | |
206 | application.primaryEventHandler.notify(); | |
c6940ed9 | 207 | } |
be72cb5c KL |
208 | application.secondaryEventHandler = null; |
209 | throw new RuntimeException("secondary exited " + | |
210 | "at wrong time"); | |
c6940ed9 | 211 | } |
be72cb5c | 212 | break; |
c6940ed9 KL |
213 | } catch (InterruptedException e) { |
214 | // SQUASH | |
215 | } | |
be72cb5c | 216 | } // while (!application.quit) |
ef368bd0 | 217 | |
c6940ed9 KL |
218 | // Pull all events off the queue |
219 | for (;;) { | |
220 | TInputEvent event = null; | |
221 | synchronized (application.drainEventQueue) { | |
222 | if (application.drainEventQueue.size() == 0) { | |
223 | break; | |
224 | } | |
225 | event = application.drainEventQueue.remove(0); | |
226 | } | |
be72cb5c KL |
227 | |
228 | // We will have an event to process, so repaint the | |
229 | // screen at the end. | |
bd8d51fa | 230 | application.repaint = true; |
be72cb5c | 231 | |
c6940ed9 KL |
232 | if (primary) { |
233 | primaryHandleEvent(event); | |
234 | } else { | |
235 | secondaryHandleEvent(event); | |
236 | } | |
237 | if ((!primary) | |
238 | && (application.secondaryEventReceiver == null) | |
239 | ) { | |
99144c71 KL |
240 | // Secondary thread, time to exit. |
241 | ||
242 | // DO NOT UNLOCK. Primary thread just came back from | |
243 | // primaryHandleEvent() and will unlock in the else | |
92554d64 KL |
244 | // block below. Just wake it up. |
245 | synchronized (application.primaryEventHandler) { | |
246 | application.primaryEventHandler.notify(); | |
247 | } | |
248 | // Now eliminate my reference so that | |
249 | // wakeEventHandler() resumes working on the primary. | |
250 | application.secondaryEventHandler = null; | |
251 | ||
252 | // All done! | |
c6940ed9 KL |
253 | return; |
254 | } | |
92554d64 | 255 | |
be72cb5c | 256 | } // for (;;) |
ef368bd0 | 257 | |
be72cb5c KL |
258 | // Fire timers, update screen. |
259 | if (!quit) { | |
260 | application.finishEventProcessing(); | |
c6940ed9 | 261 | } |
92554d64 | 262 | |
c6940ed9 KL |
263 | } // while (true) (main runnable loop) |
264 | } | |
265 | } | |
266 | ||
267 | /** | |
268 | * The primary event handler thread. | |
269 | */ | |
92554d64 | 270 | private volatile WidgetEventHandler primaryEventHandler; |
c6940ed9 KL |
271 | |
272 | /** | |
273 | * The secondary event handler thread. | |
274 | */ | |
92554d64 | 275 | private volatile WidgetEventHandler secondaryEventHandler; |
c6940ed9 KL |
276 | |
277 | /** | |
278 | * The widget receiving events from the secondary event handler thread. | |
279 | */ | |
92554d64 | 280 | private volatile TWidget secondaryEventReceiver; |
c6940ed9 | 281 | |
92554d64 KL |
282 | /** |
283 | * Wake the sleeping active event handler. | |
284 | */ | |
285 | private void wakeEventHandler() { | |
b2d49e0f KL |
286 | if (!started) { |
287 | return; | |
288 | } | |
289 | ||
92554d64 KL |
290 | if (secondaryEventHandler != null) { |
291 | synchronized (secondaryEventHandler) { | |
292 | secondaryEventHandler.notify(); | |
293 | } | |
294 | } else { | |
295 | assert (primaryEventHandler != null); | |
296 | synchronized (primaryEventHandler) { | |
297 | primaryEventHandler.notify(); | |
298 | } | |
299 | } | |
300 | } | |
301 | ||
2ce6dab2 KL |
302 | // ------------------------------------------------------------------------ |
303 | // TApplication attributes ------------------------------------------------ | |
304 | // ------------------------------------------------------------------------ | |
305 | ||
7d4115a5 | 306 | /** |
4328bb42 KL |
307 | * Access to the physical screen, keyboard, and mouse. |
308 | */ | |
7b5261bc | 309 | private Backend backend; |
4328bb42 | 310 | |
55d2b2c2 KL |
311 | /** |
312 | * Get the Backend. | |
313 | * | |
314 | * @return the Backend | |
315 | */ | |
316 | public final Backend getBackend() { | |
317 | return backend; | |
318 | } | |
319 | ||
48e27807 KL |
320 | /** |
321 | * Get the Screen. | |
322 | * | |
323 | * @return the Screen | |
324 | */ | |
325 | public final Screen getScreen() { | |
3e074355 KL |
326 | if (backend instanceof TWindowBackend) { |
327 | // We are being rendered to a TWindow. We can't use its | |
328 | // getScreen() method because that is how it is rendering to a | |
329 | // hardware backend somewhere. Instead use its getOtherScreen() | |
330 | // method. | |
331 | return ((TWindowBackend) backend).getOtherScreen(); | |
332 | } else { | |
333 | return backend.getScreen(); | |
334 | } | |
48e27807 KL |
335 | } |
336 | ||
4328bb42 | 337 | /** |
7b5261bc | 338 | * Actual mouse coordinate X. |
4328bb42 KL |
339 | */ |
340 | private int mouseX; | |
341 | ||
342 | /** | |
7b5261bc | 343 | * Actual mouse coordinate Y. |
4328bb42 KL |
344 | */ |
345 | private int mouseY; | |
346 | ||
bd8d51fa KL |
347 | /** |
348 | * Old version of mouse coordinate X. | |
349 | */ | |
350 | private int oldMouseX; | |
351 | ||
352 | /** | |
353 | * Old version mouse coordinate Y. | |
354 | */ | |
355 | private int oldMouseY; | |
356 | ||
b6faeac0 KL |
357 | /** |
358 | * The last mouse up click time, used to determine if this is a mouse | |
359 | * double-click. | |
360 | */ | |
361 | private long lastMouseUpTime; | |
362 | ||
363 | /** | |
364 | * The amount of millis between mouse up events to assume a double-click. | |
365 | */ | |
366 | private long doubleClickTime = 250; | |
367 | ||
4328bb42 | 368 | /** |
8e688b92 | 369 | * Event queue that is filled by run(). |
4328bb42 | 370 | */ |
8e688b92 KL |
371 | private List<TInputEvent> fillEventQueue; |
372 | ||
373 | /** | |
374 | * Event queue that will be drained by either primary or secondary | |
375 | * Thread. | |
376 | */ | |
377 | private List<TInputEvent> drainEventQueue; | |
4328bb42 | 378 | |
fca67db0 KL |
379 | /** |
380 | * Top-level menus in this application. | |
381 | */ | |
382 | private List<TMenu> menus; | |
383 | ||
384 | /** | |
385 | * Stack of activated sub-menus in this application. | |
386 | */ | |
387 | private List<TMenu> subMenus; | |
388 | ||
389 | /** | |
92453213 | 390 | * The currently active menu. |
fca67db0 KL |
391 | */ |
392 | private TMenu activeMenu = null; | |
393 | ||
e826b451 KL |
394 | /** |
395 | * Active keyboard accelerators. | |
396 | */ | |
397 | private Map<TKeypress, TMenuItem> accelerators; | |
398 | ||
efb7af1f KL |
399 | /** |
400 | * All menu items. | |
401 | */ | |
402 | private List<TMenuItem> menuItems; | |
403 | ||
4328bb42 KL |
404 | /** |
405 | * Windows and widgets pull colors from this ColorTheme. | |
406 | */ | |
7b5261bc KL |
407 | private ColorTheme theme; |
408 | ||
409 | /** | |
410 | * Get the color theme. | |
411 | * | |
412 | * @return the theme | |
413 | */ | |
414 | public final ColorTheme getTheme() { | |
415 | return theme; | |
416 | } | |
4328bb42 | 417 | |
a06459bd KL |
418 | /** |
419 | * The top-level windows (but not menus). | |
420 | */ | |
fca67db0 | 421 | private List<TWindow> windows; |
a06459bd | 422 | |
92453213 KL |
423 | /** |
424 | * The currently acive window. | |
425 | */ | |
426 | private TWindow activeWindow = null; | |
427 | ||
d502a0e9 KL |
428 | /** |
429 | * Timers that are being ticked. | |
430 | */ | |
431 | private List<TTimer> timers; | |
432 | ||
b2d49e0f KL |
433 | /** |
434 | * When true, the application has been started. | |
435 | */ | |
436 | private volatile boolean started = false; | |
437 | ||
4328bb42 KL |
438 | /** |
439 | * When true, exit the application. | |
440 | */ | |
92554d64 | 441 | private volatile boolean quit = false; |
4328bb42 KL |
442 | |
443 | /** | |
444 | * When true, repaint the entire screen. | |
445 | */ | |
92554d64 | 446 | private volatile boolean repaint = true; |
4328bb42 | 447 | |
be72cb5c KL |
448 | /** |
449 | * Repaint the screen on the next update. | |
450 | */ | |
451 | public void doRepaint() { | |
452 | repaint = true; | |
453 | wakeEventHandler(); | |
454 | } | |
455 | ||
4328bb42 | 456 | /** |
7b5261bc KL |
457 | * Y coordinate of the top edge of the desktop. For now this is a |
458 | * constant. Someday it would be nice to have a multi-line menu or | |
459 | * toolbars. | |
4328bb42 | 460 | */ |
48e27807 KL |
461 | private static final int desktopTop = 1; |
462 | ||
463 | /** | |
464 | * Get Y coordinate of the top edge of the desktop. | |
465 | * | |
466 | * @return Y coordinate of the top edge of the desktop | |
467 | */ | |
468 | public final int getDesktopTop() { | |
469 | return desktopTop; | |
470 | } | |
4328bb42 KL |
471 | |
472 | /** | |
473 | * Y coordinate of the bottom edge of the desktop. | |
474 | */ | |
48e27807 KL |
475 | private int desktopBottom; |
476 | ||
477 | /** | |
478 | * Get Y coordinate of the bottom edge of the desktop. | |
479 | * | |
480 | * @return Y coordinate of the bottom edge of the desktop | |
481 | */ | |
482 | public final int getDesktopBottom() { | |
483 | return desktopBottom; | |
484 | } | |
4328bb42 | 485 | |
0ee88b6d KL |
486 | /** |
487 | * An optional TDesktop background window that is drawn underneath | |
488 | * everything else. | |
489 | */ | |
490 | private TDesktop desktop; | |
491 | ||
492 | /** | |
493 | * Set the TDesktop instance. | |
494 | * | |
495 | * @param desktop a TDesktop instance, or null to remove the one that is | |
496 | * set | |
497 | */ | |
498 | public final void setDesktop(final TDesktop desktop) { | |
499 | if (this.desktop != null) { | |
500 | this.desktop.onClose(); | |
501 | } | |
502 | this.desktop = desktop; | |
503 | } | |
504 | ||
505 | /** | |
506 | * Get the TDesktop instance. | |
507 | * | |
508 | * @return the desktop, or null if it is not set | |
509 | */ | |
510 | public final TDesktop getDesktop() { | |
511 | return desktop; | |
512 | } | |
513 | ||
92453213 KL |
514 | /** |
515 | * Get the current active window. | |
516 | * | |
517 | * @return the active window, or null if it is not set | |
518 | */ | |
519 | public final TWindow getActiveWindow() { | |
520 | return activeWindow; | |
521 | } | |
522 | ||
523 | /** | |
e8a11f98 | 524 | * Get a (shallow) copy of the window list. |
92453213 KL |
525 | * |
526 | * @return a copy of the list of windows for this application | |
527 | */ | |
528 | public final List<TWindow> getAllWindows() { | |
529 | List<TWindow> result = new LinkedList<TWindow>(); | |
530 | result.addAll(windows); | |
531 | return result; | |
532 | } | |
533 | ||
72fca17b KL |
534 | /** |
535 | * If true, focus follows mouse: windows automatically raised if the | |
536 | * mouse passes over them. | |
537 | */ | |
538 | private boolean focusFollowsMouse = false; | |
539 | ||
540 | /** | |
541 | * Get focusFollowsMouse flag. | |
542 | * | |
543 | * @return true if focus follows mouse: windows automatically raised if | |
544 | * the mouse passes over them | |
545 | */ | |
546 | public boolean getFocusFollowsMouse() { | |
547 | return focusFollowsMouse; | |
548 | } | |
549 | ||
550 | /** | |
551 | * Set focusFollowsMouse flag. | |
552 | * | |
553 | * @param focusFollowsMouse if true, focus follows mouse: windows | |
554 | * automatically raised if the mouse passes over them | |
555 | */ | |
556 | public void setFocusFollowsMouse(final boolean focusFollowsMouse) { | |
557 | this.focusFollowsMouse = focusFollowsMouse; | |
558 | } | |
559 | ||
2ce6dab2 KL |
560 | // ------------------------------------------------------------------------ |
561 | // General behavior ------------------------------------------------------- | |
562 | // ------------------------------------------------------------------------ | |
563 | ||
564 | /** | |
565 | * Display the about dialog. | |
566 | */ | |
567 | protected void showAboutDialog() { | |
339652cc KL |
568 | messageBox(i18n.getString("aboutDialogTitle"), |
569 | MessageFormat.format(i18n.getString("aboutDialogText"), | |
570 | this.getClass().getPackage().getImplementationVersion()), | |
2ce6dab2 KL |
571 | TMessageBox.Type.OK); |
572 | } | |
573 | ||
574 | // ------------------------------------------------------------------------ | |
575 | // Constructors ----------------------------------------------------------- | |
576 | // ------------------------------------------------------------------------ | |
577 | ||
eb29bbb5 KL |
578 | /** |
579 | * Public constructor. | |
580 | * | |
581 | * @param backendType BackendType.XTERM, BackendType.ECMA48 or | |
582 | * BackendType.SWING | |
583 | * @param windowWidth the number of text columns to start with | |
584 | * @param windowHeight the number of text rows to start with | |
585 | * @param fontSize the size in points | |
586 | * @throws UnsupportedEncodingException if an exception is thrown when | |
587 | * creating the InputStreamReader | |
588 | */ | |
589 | public TApplication(final BackendType backendType, final int windowWidth, | |
590 | final int windowHeight, final int fontSize) | |
591 | throws UnsupportedEncodingException { | |
592 | ||
593 | switch (backendType) { | |
594 | case SWING: | |
595 | backend = new SwingBackend(this, windowWidth, windowHeight, | |
596 | fontSize); | |
597 | break; | |
598 | case XTERM: | |
599 | // Fall through... | |
600 | case ECMA48: | |
601 | backend = new ECMA48Backend(this, null, null, windowWidth, | |
602 | windowHeight, fontSize); | |
603 | break; | |
604 | default: | |
605 | throw new IllegalArgumentException("Invalid backend type: " | |
606 | + backendType); | |
607 | } | |
608 | TApplicationImpl(); | |
609 | } | |
610 | ||
4328bb42 KL |
611 | /** |
612 | * Public constructor. | |
613 | * | |
a4406f4e KL |
614 | * @param backendType BackendType.XTERM, BackendType.ECMA48 or |
615 | * BackendType.SWING | |
616 | * @throws UnsupportedEncodingException if an exception is thrown when | |
617 | * creating the InputStreamReader | |
618 | */ | |
619 | public TApplication(final BackendType backendType) | |
620 | throws UnsupportedEncodingException { | |
621 | ||
622 | switch (backendType) { | |
623 | case SWING: | |
c447c6e5 KL |
624 | // The default SwingBackend is 80x25, 20 pt font. If you want to |
625 | // change that, you can pass the extra arguments to the | |
626 | // SwingBackend constructor here. For example, if you wanted | |
627 | // 90x30, 16 pt font: | |
628 | // | |
629 | // backend = new SwingBackend(this, 90, 30, 16); | |
a4406f4e KL |
630 | backend = new SwingBackend(this); |
631 | break; | |
632 | case XTERM: | |
633 | // Fall through... | |
634 | case ECMA48: | |
635 | backend = new ECMA48Backend(this, null, null); | |
329fd62e KL |
636 | break; |
637 | default: | |
638 | throw new IllegalArgumentException("Invalid backend type: " | |
639 | + backendType); | |
a4406f4e KL |
640 | } |
641 | TApplicationImpl(); | |
642 | } | |
643 | ||
644 | /** | |
645 | * Public constructor. The backend type will be BackendType.ECMA48. | |
646 | * | |
4328bb42 KL |
647 | * @param input an InputStream connected to the remote user, or null for |
648 | * System.in. If System.in is used, then on non-Windows systems it will | |
649 | * be put in raw mode; shutdown() will (blindly!) put System.in in cooked | |
650 | * mode. input is always converted to a Reader with UTF-8 encoding. | |
651 | * @param output an OutputStream connected to the remote user, or null | |
652 | * for System.out. output is always converted to a Writer with UTF-8 | |
653 | * encoding. | |
7b5261bc KL |
654 | * @throws UnsupportedEncodingException if an exception is thrown when |
655 | * creating the InputStreamReader | |
4328bb42 | 656 | */ |
7b5261bc KL |
657 | public TApplication(final InputStream input, |
658 | final OutputStream output) throws UnsupportedEncodingException { | |
4328bb42 | 659 | |
a4406f4e KL |
660 | backend = new ECMA48Backend(this, input, output); |
661 | TApplicationImpl(); | |
662 | } | |
30bd4abd | 663 | |
6985c572 KL |
664 | /** |
665 | * Public constructor. The backend type will be BackendType.ECMA48. | |
666 | * | |
667 | * @param input the InputStream underlying 'reader'. Its available() | |
668 | * method is used to determine if reader.read() will block or not. | |
669 | * @param reader a Reader connected to the remote user. | |
670 | * @param writer a PrintWriter connected to the remote user. | |
671 | * @param setRawMode if true, set System.in into raw mode with stty. | |
672 | * This should in general not be used. It is here solely for Demo3, | |
673 | * which uses System.in. | |
674 | * @throws IllegalArgumentException if input, reader, or writer are null. | |
675 | */ | |
676 | public TApplication(final InputStream input, final Reader reader, | |
677 | final PrintWriter writer, final boolean setRawMode) { | |
678 | ||
679 | backend = new ECMA48Backend(this, input, reader, writer, setRawMode); | |
680 | TApplicationImpl(); | |
681 | } | |
682 | ||
683 | /** | |
684 | * Public constructor. The backend type will be BackendType.ECMA48. | |
685 | * | |
686 | * @param input the InputStream underlying 'reader'. Its available() | |
687 | * method is used to determine if reader.read() will block or not. | |
688 | * @param reader a Reader connected to the remote user. | |
689 | * @param writer a PrintWriter connected to the remote user. | |
690 | * @throws IllegalArgumentException if input, reader, or writer are null. | |
691 | */ | |
692 | public TApplication(final InputStream input, final Reader reader, | |
693 | final PrintWriter writer) { | |
694 | ||
695 | this(input, reader, writer, false); | |
696 | } | |
697 | ||
a4406f4e KL |
698 | /** |
699 | * Public constructor. This hook enables use with new non-Jexer | |
700 | * backends. | |
701 | * | |
702 | * @param backend a Backend that is already ready to go. | |
703 | */ | |
704 | public TApplication(final Backend backend) { | |
705 | this.backend = backend; | |
3e074355 | 706 | backend.setListener(this); |
a4406f4e KL |
707 | TApplicationImpl(); |
708 | } | |
30bd4abd | 709 | |
a4406f4e KL |
710 | /** |
711 | * Finish construction once the backend is set. | |
712 | */ | |
713 | private void TApplicationImpl() { | |
8e688b92 KL |
714 | theme = new ColorTheme(); |
715 | desktopBottom = getScreen().getHeight() - 1; | |
c6940ed9 KL |
716 | fillEventQueue = new ArrayList<TInputEvent>(); |
717 | drainEventQueue = new ArrayList<TInputEvent>(); | |
8e688b92 KL |
718 | windows = new LinkedList<TWindow>(); |
719 | menus = new LinkedList<TMenu>(); | |
720 | subMenus = new LinkedList<TMenu>(); | |
d502a0e9 | 721 | timers = new LinkedList<TTimer>(); |
e826b451 | 722 | accelerators = new HashMap<TKeypress, TMenuItem>(); |
efb7af1f | 723 | menuItems = new ArrayList<TMenuItem>(); |
0ee88b6d | 724 | desktop = new TDesktop(this); |
c6940ed9 | 725 | |
be72cb5c KL |
726 | // Special case: the Swing backend needs to have a timer to drive its |
727 | // blink state. | |
728 | if ((backend instanceof SwingBackend) | |
729 | || (backend instanceof MultiBackend) | |
730 | ) { | |
731 | // Default to 500 millis, unless a SwingBackend has its own | |
732 | // value. | |
733 | long millis = 500; | |
734 | if (backend instanceof SwingBackend) { | |
735 | millis = ((SwingBackend) backend).getBlinkMillis(); | |
736 | } | |
737 | if (millis > 0) { | |
738 | addTimer(millis, true, | |
739 | new TAction() { | |
740 | public void DO() { | |
741 | TApplication.this.doRepaint(); | |
742 | } | |
743 | } | |
744 | ); | |
745 | } | |
746 | } | |
4328bb42 KL |
747 | } |
748 | ||
2ce6dab2 KL |
749 | // ------------------------------------------------------------------------ |
750 | // Screen refresh loop ---------------------------------------------------- | |
751 | // ------------------------------------------------------------------------ | |
752 | ||
be72cb5c KL |
753 | /** |
754 | * Process background events, and update the screen. | |
755 | */ | |
756 | private void finishEventProcessing() { | |
757 | if (debugThreads) { | |
758 | System.err.printf(System.currentTimeMillis() + " " + | |
759 | Thread.currentThread() + " finishEventProcessing()\n"); | |
760 | } | |
761 | ||
762 | // Process timers and call doIdle()'s | |
763 | doIdle(); | |
764 | ||
765 | // Update the screen | |
766 | synchronized (getScreen()) { | |
767 | drawAll(); | |
768 | } | |
769 | ||
770 | if (debugThreads) { | |
771 | System.err.printf(System.currentTimeMillis() + " " + | |
772 | Thread.currentThread() + " finishEventProcessing() END\n"); | |
773 | } | |
774 | } | |
775 | ||
4328bb42 | 776 | /** |
bd8d51fa KL |
777 | * Invert the cell color at a position. This is used to track the mouse. |
778 | * | |
779 | * @param x column position | |
780 | * @param y row position | |
4328bb42 | 781 | */ |
bd8d51fa | 782 | private void invertCell(final int x, final int y) { |
1d14ffab | 783 | if (debugThreads) { |
be72cb5c KL |
784 | System.err.printf("%d %s invertCell() %d %d\n", |
785 | System.currentTimeMillis(), Thread.currentThread(), x, y); | |
7b5261bc | 786 | } |
1d14ffab KL |
787 | CellAttributes attr = getScreen().getAttrXY(x, y); |
788 | attr.setForeColor(attr.getForeColor().invert()); | |
789 | attr.setBackColor(attr.getBackColor().invert()); | |
790 | getScreen().putAttrXY(x, y, attr, false); | |
4328bb42 KL |
791 | } |
792 | ||
793 | /** | |
794 | * Draw everything. | |
795 | */ | |
7c870d89 | 796 | private void drawAll() { |
339652cc KL |
797 | boolean menuIsActive = false; |
798 | ||
99144c71 | 799 | if (debugThreads) { |
be72cb5c KL |
800 | System.err.printf("%d %s drawAll() enter\n", |
801 | System.currentTimeMillis(), Thread.currentThread()); | |
99144c71 KL |
802 | } |
803 | ||
bd8d51fa | 804 | if (!repaint) { |
1d14ffab | 805 | if (debugThreads) { |
be72cb5c KL |
806 | System.err.printf("%d %s drawAll() !repaint\n", |
807 | System.currentTimeMillis(), Thread.currentThread()); | |
bd8d51fa | 808 | } |
1d14ffab KL |
809 | synchronized (getScreen()) { |
810 | if ((oldMouseX != mouseX) || (oldMouseY != mouseY)) { | |
811 | // The only thing that has happened is the mouse moved. | |
812 | // Clear the old position and draw the new position. | |
813 | invertCell(oldMouseX, oldMouseY); | |
814 | invertCell(mouseX, mouseY); | |
815 | oldMouseX = mouseX; | |
816 | oldMouseY = mouseY; | |
817 | } | |
818 | if (getScreen().isDirty()) { | |
819 | backend.flushScreen(); | |
820 | } | |
821 | return; | |
bd8d51fa | 822 | } |
7b5261bc KL |
823 | } |
824 | ||
99144c71 | 825 | if (debugThreads) { |
be72cb5c KL |
826 | System.err.printf("%d %s drawAll() REDRAW\n", |
827 | System.currentTimeMillis(), Thread.currentThread()); | |
99144c71 KL |
828 | } |
829 | ||
7b5261bc KL |
830 | // If true, the cursor is not visible |
831 | boolean cursor = false; | |
832 | ||
833 | // Start with a clean screen | |
a06459bd | 834 | getScreen().clear(); |
7b5261bc | 835 | |
0ee88b6d KL |
836 | // Draw the desktop |
837 | if (desktop != null) { | |
838 | desktop.drawChildren(); | |
839 | } | |
7b5261bc | 840 | |
7b5261bc | 841 | // Draw each window in reverse Z order |
a06459bd KL |
842 | List<TWindow> sorted = new LinkedList<TWindow>(windows); |
843 | Collections.sort(sorted); | |
e685a47d KL |
844 | TWindow topLevel = null; |
845 | if (sorted.size() > 0) { | |
846 | topLevel = sorted.get(0); | |
847 | } | |
a06459bd KL |
848 | Collections.reverse(sorted); |
849 | for (TWindow window: sorted) { | |
92453213 KL |
850 | if (window.isShown()) { |
851 | window.drawChildren(); | |
852 | } | |
7b5261bc KL |
853 | } |
854 | ||
855 | // Draw the blank menubar line - reset the screen clipping first so | |
856 | // it won't trim it out. | |
a06459bd KL |
857 | getScreen().resetClipping(); |
858 | getScreen().hLineXY(0, 0, getScreen().getWidth(), ' ', | |
7b5261bc KL |
859 | theme.getColor("tmenu")); |
860 | // Now draw the menus. | |
861 | int x = 1; | |
fca67db0 | 862 | for (TMenu menu: menus) { |
7b5261bc KL |
863 | CellAttributes menuColor; |
864 | CellAttributes menuMnemonicColor; | |
7c870d89 | 865 | if (menu.isActive()) { |
339652cc | 866 | menuIsActive = true; |
7b5261bc KL |
867 | menuColor = theme.getColor("tmenu.highlighted"); |
868 | menuMnemonicColor = theme.getColor("tmenu.mnemonic.highlighted"); | |
2ce6dab2 | 869 | topLevel = menu; |
7b5261bc KL |
870 | } else { |
871 | menuColor = theme.getColor("tmenu"); | |
872 | menuMnemonicColor = theme.getColor("tmenu.mnemonic"); | |
873 | } | |
874 | // Draw the menu title | |
fca67db0 | 875 | getScreen().hLineXY(x, 0, menu.getTitle().length() + 2, ' ', |
7b5261bc | 876 | menuColor); |
0d47c546 | 877 | getScreen().putStringXY(x + 1, 0, menu.getTitle(), menuColor); |
7b5261bc | 878 | // Draw the highlight character |
fca67db0 KL |
879 | getScreen().putCharXY(x + 1 + menu.getMnemonic().getShortcutIdx(), |
880 | 0, menu.getMnemonic().getShortcut(), menuMnemonicColor); | |
7b5261bc | 881 | |
7c870d89 | 882 | if (menu.isActive()) { |
a06459bd | 883 | menu.drawChildren(); |
7b5261bc | 884 | // Reset the screen clipping so we can draw the next title. |
a06459bd | 885 | getScreen().resetClipping(); |
7b5261bc | 886 | } |
fca67db0 | 887 | x += menu.getTitle().length() + 2; |
7b5261bc KL |
888 | } |
889 | ||
a06459bd | 890 | for (TMenu menu: subMenus) { |
7b5261bc | 891 | // Reset the screen clipping so we can draw the next sub-menu. |
a06459bd KL |
892 | getScreen().resetClipping(); |
893 | menu.drawChildren(); | |
7b5261bc | 894 | } |
7b5261bc | 895 | |
2ce6dab2 | 896 | // Draw the status bar of the top-level window |
e685a47d KL |
897 | TStatusBar statusBar = null; |
898 | if (topLevel != null) { | |
899 | statusBar = topLevel.getStatusBar(); | |
900 | } | |
2ce6dab2 KL |
901 | if (statusBar != null) { |
902 | getScreen().resetClipping(); | |
903 | statusBar.setWidth(getScreen().getWidth()); | |
904 | statusBar.setY(getScreen().getHeight() - topLevel.getY()); | |
905 | statusBar.draw(); | |
906 | } else { | |
907 | CellAttributes barColor = new CellAttributes(); | |
908 | barColor.setTo(getTheme().getColor("tstatusbar.text")); | |
909 | getScreen().hLineXY(0, desktopBottom, getScreen().getWidth(), ' ', | |
910 | barColor); | |
911 | } | |
912 | ||
7b5261bc | 913 | // Draw the mouse pointer |
bd8d51fa | 914 | invertCell(mouseX, mouseY); |
1d14ffab KL |
915 | oldMouseX = mouseX; |
916 | oldMouseY = mouseY; | |
7b5261bc | 917 | |
7b5261bc | 918 | // Place the cursor if it is visible |
339652cc KL |
919 | if (!menuIsActive) { |
920 | TWidget activeWidget = null; | |
921 | if (sorted.size() > 0) { | |
922 | activeWidget = sorted.get(sorted.size() - 1).getActiveChild(); | |
923 | if (activeWidget.isCursorVisible()) { | |
924 | if ((activeWidget.getCursorAbsoluteY() < desktopBottom) | |
925 | && (activeWidget.getCursorAbsoluteY() > desktopTop) | |
926 | ) { | |
927 | getScreen().putCursor(true, | |
928 | activeWidget.getCursorAbsoluteX(), | |
929 | activeWidget.getCursorAbsoluteY()); | |
930 | cursor = true; | |
931 | } else { | |
932 | getScreen().putCursor(false, | |
933 | activeWidget.getCursorAbsoluteX(), | |
934 | activeWidget.getCursorAbsoluteY()); | |
935 | cursor = false; | |
936 | } | |
be72cb5c | 937 | } |
7b5261bc KL |
938 | } |
939 | } | |
940 | ||
941 | // Kill the cursor | |
fca67db0 | 942 | if (!cursor) { |
a06459bd | 943 | getScreen().hideCursor(); |
7b5261bc | 944 | } |
7b5261bc KL |
945 | |
946 | // Flush the screen contents | |
1d14ffab KL |
947 | if (getScreen().isDirty()) { |
948 | backend.flushScreen(); | |
949 | } | |
7b5261bc KL |
950 | |
951 | repaint = false; | |
4328bb42 KL |
952 | } |
953 | ||
2ce6dab2 KL |
954 | // ------------------------------------------------------------------------ |
955 | // Main loop -------------------------------------------------------------- | |
956 | // ------------------------------------------------------------------------ | |
957 | ||
42873e30 KL |
958 | /** |
959 | * Force this application to exit. | |
960 | */ | |
961 | public void exit() { | |
962 | quit = true; | |
be72cb5c KL |
963 | synchronized (this) { |
964 | this.notify(); | |
965 | } | |
42873e30 KL |
966 | } |
967 | ||
4328bb42 | 968 | /** |
7b5261bc | 969 | * Run this application until it exits. |
4328bb42 | 970 | */ |
a4406f4e | 971 | public void run() { |
be72cb5c KL |
972 | // Start the main consumer thread |
973 | primaryEventHandler = new WidgetEventHandler(this, true); | |
974 | (new Thread(primaryEventHandler)).start(); | |
2027327c | 975 | |
b2d49e0f KL |
976 | started = true; |
977 | ||
7b5261bc | 978 | while (!quit) { |
be72cb5c KL |
979 | synchronized (this) { |
980 | boolean doWait = false; | |
981 | ||
982 | synchronized (fillEventQueue) { | |
983 | if (fillEventQueue.size() == 0) { | |
984 | doWait = true; | |
6358f6e5 | 985 | } |
be72cb5c KL |
986 | } |
987 | ||
988 | if (doWait) { | |
989 | // No I/O to dispatch, so wait until the backend | |
990 | // provides new I/O. | |
991 | try { | |
992 | if (debugThreads) { | |
993 | System.err.println(System.currentTimeMillis() + | |
994 | " MAIN sleep"); | |
995 | } | |
996 | ||
997 | this.wait(); | |
998 | ||
999 | if (debugThreads) { | |
1000 | System.err.println(System.currentTimeMillis() + | |
1001 | " MAIN AWAKE"); | |
1002 | } | |
1003 | } catch (InterruptedException e) { | |
1004 | // I'm awake and don't care why, let's see what's | |
1005 | // going on out there. | |
92554d64 | 1006 | } |
8e688b92 | 1007 | } |
7b5261bc | 1008 | |
be72cb5c | 1009 | } // synchronized (this) |
ef368bd0 | 1010 | |
be72cb5c KL |
1011 | synchronized (fillEventQueue) { |
1012 | // Pull any pending I/O events | |
1013 | backend.getEvents(fillEventQueue); | |
8e688b92 | 1014 | |
be72cb5c KL |
1015 | // Dispatch each event to the appropriate handler, one at a |
1016 | // time. | |
1017 | for (;;) { | |
1018 | TInputEvent event = null; | |
1019 | if (fillEventQueue.size() == 0) { | |
1020 | break; | |
1021 | } | |
1022 | event = fillEventQueue.remove(0); | |
1023 | metaHandleEvent(event); | |
8e688b92 | 1024 | } |
8e688b92 | 1025 | } |
7b5261bc | 1026 | |
92554d64 | 1027 | // Wake a consumer thread if we have any pending events. |
ef368bd0 KL |
1028 | if (drainEventQueue.size() > 0) { |
1029 | wakeEventHandler(); | |
92554d64 KL |
1030 | } |
1031 | ||
92554d64 KL |
1032 | } // while (!quit) |
1033 | ||
1034 | // Shutdown the event consumer threads | |
1035 | if (secondaryEventHandler != null) { | |
1036 | synchronized (secondaryEventHandler) { | |
1037 | secondaryEventHandler.notify(); | |
1038 | } | |
1039 | } | |
1040 | if (primaryEventHandler != null) { | |
1041 | synchronized (primaryEventHandler) { | |
1042 | primaryEventHandler.notify(); | |
1043 | } | |
7b5261bc KL |
1044 | } |
1045 | ||
92554d64 | 1046 | // Shutdown the user I/O thread(s) |
7b5261bc | 1047 | backend.shutdown(); |
92554d64 KL |
1048 | |
1049 | // Close all the windows. This gives them an opportunity to release | |
1050 | // resources. | |
1051 | closeAllWindows(); | |
1052 | ||
4328bb42 KL |
1053 | } |
1054 | ||
1055 | /** | |
1056 | * Peek at certain application-level events, add to eventQueue, and wake | |
8e688b92 | 1057 | * up the consuming Thread. |
4328bb42 | 1058 | * |
8e688b92 | 1059 | * @param event the input event to consume |
4328bb42 | 1060 | */ |
8e688b92 | 1061 | private void metaHandleEvent(final TInputEvent event) { |
7b5261bc | 1062 | |
a83fea2b KL |
1063 | if (debugEvents) { |
1064 | System.err.printf(String.format("metaHandleEvents event: %s\n", | |
1065 | event)); System.err.flush(); | |
1066 | } | |
7b5261bc | 1067 | |
8e688b92 KL |
1068 | if (quit) { |
1069 | // Do no more processing if the application is already trying | |
1070 | // to exit. | |
1071 | return; | |
1072 | } | |
7b5261bc | 1073 | |
8e688b92 | 1074 | // Special application-wide events ------------------------------- |
7b5261bc | 1075 | |
8e688b92 KL |
1076 | // Abort everything |
1077 | if (event instanceof TCommandEvent) { | |
1078 | TCommandEvent command = (TCommandEvent) event; | |
1079 | if (command.getCmd().equals(cmAbort)) { | |
be72cb5c | 1080 | exit(); |
8e688b92 | 1081 | return; |
7b5261bc | 1082 | } |
8e688b92 | 1083 | } |
7b5261bc | 1084 | |
be72cb5c KL |
1085 | synchronized (drainEventQueue) { |
1086 | // Screen resize | |
1087 | if (event instanceof TResizeEvent) { | |
1088 | TResizeEvent resize = (TResizeEvent) event; | |
1089 | synchronized (getScreen()) { | |
1090 | getScreen().setDimensions(resize.getWidth(), | |
1091 | resize.getHeight()); | |
1092 | desktopBottom = getScreen().getHeight() - 1; | |
1093 | mouseX = 0; | |
1094 | mouseY = 0; | |
1095 | oldMouseX = 0; | |
1096 | oldMouseY = 0; | |
1097 | } | |
1098 | if (desktop != null) { | |
1099 | desktop.setDimensions(0, 0, resize.getWidth(), | |
1100 | resize.getHeight() - 1); | |
1101 | } | |
68c5cd6b KL |
1102 | |
1103 | // Change menu edges if needed. | |
1104 | recomputeMenuX(); | |
1105 | ||
85c07c5e KL |
1106 | // We are dirty, redraw the screen. |
1107 | doRepaint(); | |
be72cb5c | 1108 | return; |
0ee88b6d | 1109 | } |
7b5261bc | 1110 | |
be72cb5c KL |
1111 | // Put into the main queue |
1112 | drainEventQueue.add(event); | |
1113 | } | |
4328bb42 KL |
1114 | } |
1115 | ||
a06459bd KL |
1116 | /** |
1117 | * Dispatch one event to the appropriate widget or application-level | |
fca67db0 KL |
1118 | * event handler. This is the primary event handler, it has the normal |
1119 | * application-wide event handling. | |
a06459bd KL |
1120 | * |
1121 | * @param event the input event to consume | |
fca67db0 | 1122 | * @see #secondaryHandleEvent(TInputEvent event) |
a06459bd | 1123 | */ |
fca67db0 KL |
1124 | private void primaryHandleEvent(final TInputEvent event) { |
1125 | ||
a83fea2b KL |
1126 | if (debugEvents) { |
1127 | System.err.printf("Handle event: %s\n", event); | |
1128 | } | |
b6faeac0 | 1129 | TMouseEvent doubleClick = null; |
fca67db0 KL |
1130 | |
1131 | // Special application-wide events ----------------------------------- | |
1132 | ||
1133 | // Peek at the mouse position | |
1134 | if (event instanceof TMouseEvent) { | |
e8a11f98 KL |
1135 | TMouseEvent mouse = (TMouseEvent) event; |
1136 | if ((mouseX != mouse.getX()) || (mouseY != mouse.getY())) { | |
1137 | oldMouseX = mouseX; | |
1138 | oldMouseY = mouseY; | |
1139 | mouseX = mouse.getX(); | |
1140 | mouseY = mouse.getY(); | |
b6faeac0 KL |
1141 | } else { |
1142 | if (mouse.getType() == TMouseEvent.Type.MOUSE_UP) { | |
1143 | if ((mouse.getTime().getTime() - lastMouseUpTime) < | |
1144 | doubleClickTime) { | |
1145 | ||
1146 | // This is a double-click. | |
1147 | doubleClick = new TMouseEvent(TMouseEvent.Type. | |
1148 | MOUSE_DOUBLE_CLICK, | |
1149 | mouse.getX(), mouse.getY(), | |
1150 | mouse.getAbsoluteX(), mouse.getAbsoluteY(), | |
1151 | mouse.isMouse1(), mouse.isMouse2(), | |
1152 | mouse.isMouse3(), | |
1153 | mouse.isMouseWheelUp(), mouse.isMouseWheelDown()); | |
1154 | ||
1155 | } else { | |
1156 | // The first click of a potential double-click. | |
1157 | lastMouseUpTime = mouse.getTime().getTime(); | |
1158 | } | |
1159 | } | |
e8a11f98 KL |
1160 | } |
1161 | ||
fca67db0 KL |
1162 | // See if we need to switch focus to another window or the menu |
1163 | checkSwitchFocus((TMouseEvent) event); | |
1164 | } | |
1165 | ||
1166 | // Handle menu events | |
1167 | if ((activeMenu != null) && !(event instanceof TCommandEvent)) { | |
1168 | TMenu menu = activeMenu; | |
1169 | ||
1170 | if (event instanceof TMouseEvent) { | |
1171 | TMouseEvent mouse = (TMouseEvent) event; | |
1172 | ||
1173 | while (subMenus.size() > 0) { | |
1174 | TMenu subMenu = subMenus.get(subMenus.size() - 1); | |
1175 | if (subMenu.mouseWouldHit(mouse)) { | |
1176 | break; | |
1177 | } | |
1178 | if ((mouse.getType() == TMouseEvent.Type.MOUSE_MOTION) | |
7c870d89 KL |
1179 | && (!mouse.isMouse1()) |
1180 | && (!mouse.isMouse2()) | |
1181 | && (!mouse.isMouse3()) | |
1182 | && (!mouse.isMouseWheelUp()) | |
1183 | && (!mouse.isMouseWheelDown()) | |
fca67db0 KL |
1184 | ) { |
1185 | break; | |
1186 | } | |
1187 | // We navigated away from a sub-menu, so close it | |
1188 | closeSubMenu(); | |
1189 | } | |
1190 | ||
1191 | // Convert the mouse relative x/y to menu coordinates | |
1192 | assert (mouse.getX() == mouse.getAbsoluteX()); | |
1193 | assert (mouse.getY() == mouse.getAbsoluteY()); | |
1194 | if (subMenus.size() > 0) { | |
1195 | menu = subMenus.get(subMenus.size() - 1); | |
1196 | } | |
1197 | mouse.setX(mouse.getX() - menu.getX()); | |
1198 | mouse.setY(mouse.getY() - menu.getY()); | |
1199 | } | |
1200 | menu.handleEvent(event); | |
1201 | return; | |
1202 | } | |
a06459bd | 1203 | |
fca67db0 KL |
1204 | if (event instanceof TKeypressEvent) { |
1205 | TKeypressEvent keypress = (TKeypressEvent) event; | |
e826b451 | 1206 | |
5dfd1c11 KL |
1207 | // See if this key matches an accelerator, and is not being |
1208 | // shortcutted by the active window, and if so dispatch the menu | |
1209 | // event. | |
1210 | boolean windowWillShortcut = false; | |
92453213 KL |
1211 | if (activeWindow != null) { |
1212 | assert (activeWindow.isShown()); | |
1213 | if (activeWindow.isShortcutKeypress(keypress.getKey())) { | |
1214 | // We do not process this key, it will be passed to the | |
1215 | // window instead. | |
1216 | windowWillShortcut = true; | |
5dfd1c11 | 1217 | } |
e826b451 | 1218 | } |
5dfd1c11 | 1219 | |
2ce6dab2 | 1220 | if (!windowWillShortcut && !modalWindowActive()) { |
5dfd1c11 KL |
1221 | TKeypress keypressLowercase = keypress.getKey().toLowerCase(); |
1222 | TMenuItem item = null; | |
1223 | synchronized (accelerators) { | |
1224 | item = accelerators.get(keypressLowercase); | |
1225 | } | |
1226 | if (item != null) { | |
1227 | if (item.isEnabled()) { | |
1228 | // Let the menu item dispatch | |
1229 | item.dispatch(); | |
1230 | return; | |
1231 | } | |
fca67db0 | 1232 | } |
5dfd1c11 | 1233 | |
2ce6dab2 KL |
1234 | // Handle the keypress |
1235 | if (onKeypress(keypress)) { | |
1236 | return; | |
1237 | } | |
bd8d51fa | 1238 | } |
fca67db0 | 1239 | } |
a06459bd | 1240 | |
fca67db0 KL |
1241 | if (event instanceof TCommandEvent) { |
1242 | if (onCommand((TCommandEvent) event)) { | |
1243 | return; | |
1244 | } | |
1245 | } | |
1246 | ||
1247 | if (event instanceof TMenuEvent) { | |
1248 | if (onMenu((TMenuEvent) event)) { | |
1249 | return; | |
1250 | } | |
1251 | } | |
1252 | ||
1253 | // Dispatch events to the active window ------------------------------- | |
0ee88b6d | 1254 | boolean dispatchToDesktop = true; |
92453213 KL |
1255 | TWindow window = activeWindow; |
1256 | if (window != null) { | |
1257 | assert (window.isActive()); | |
1258 | assert (window.isShown()); | |
1259 | if (event instanceof TMouseEvent) { | |
1260 | TMouseEvent mouse = (TMouseEvent) event; | |
1261 | // Convert the mouse relative x/y to window coordinates | |
1262 | assert (mouse.getX() == mouse.getAbsoluteX()); | |
1263 | assert (mouse.getY() == mouse.getAbsoluteY()); | |
1264 | mouse.setX(mouse.getX() - window.getX()); | |
1265 | mouse.setY(mouse.getY() - window.getY()); | |
1266 | ||
b6faeac0 KL |
1267 | if (doubleClick != null) { |
1268 | doubleClick.setX(doubleClick.getX() - window.getX()); | |
1269 | doubleClick.setY(doubleClick.getY() - window.getY()); | |
1270 | } | |
1271 | ||
92453213 | 1272 | if (window.mouseWouldHit(mouse)) { |
0ee88b6d | 1273 | dispatchToDesktop = false; |
fca67db0 | 1274 | } |
92453213 KL |
1275 | } else if (event instanceof TKeypressEvent) { |
1276 | dispatchToDesktop = false; | |
1277 | } | |
0ee88b6d | 1278 | |
92453213 KL |
1279 | if (debugEvents) { |
1280 | System.err.printf("TApplication dispatch event: %s\n", | |
1281 | event); | |
fca67db0 | 1282 | } |
92453213 | 1283 | window.handleEvent(event); |
b6faeac0 KL |
1284 | if (doubleClick != null) { |
1285 | window.handleEvent(doubleClick); | |
1286 | } | |
fca67db0 | 1287 | } |
0ee88b6d KL |
1288 | if (dispatchToDesktop) { |
1289 | // This event is fair game for the desktop to process. | |
1290 | if (desktop != null) { | |
1291 | desktop.handleEvent(event); | |
b6faeac0 KL |
1292 | if (doubleClick != null) { |
1293 | desktop.handleEvent(doubleClick); | |
1294 | } | |
0ee88b6d KL |
1295 | } |
1296 | } | |
fca67db0 | 1297 | } |
0ee88b6d | 1298 | |
fca67db0 KL |
1299 | /** |
1300 | * Dispatch one event to the appropriate widget or application-level | |
1301 | * event handler. This is the secondary event handler used by certain | |
1302 | * special dialogs (currently TMessageBox and TFileOpenBox). | |
1303 | * | |
1304 | * @param event the input event to consume | |
1305 | * @see #primaryHandleEvent(TInputEvent event) | |
1306 | */ | |
1307 | private void secondaryHandleEvent(final TInputEvent event) { | |
b6faeac0 KL |
1308 | TMouseEvent doubleClick = null; |
1309 | ||
e8a11f98 KL |
1310 | // Peek at the mouse position |
1311 | if (event instanceof TMouseEvent) { | |
1312 | TMouseEvent mouse = (TMouseEvent) event; | |
1313 | if ((mouseX != mouse.getX()) || (mouseY != mouse.getY())) { | |
1314 | oldMouseX = mouseX; | |
1315 | oldMouseY = mouseY; | |
1316 | mouseX = mouse.getX(); | |
1317 | mouseY = mouse.getY(); | |
b6faeac0 KL |
1318 | } else { |
1319 | if (mouse.getType() == TMouseEvent.Type.MOUSE_UP) { | |
1320 | if ((mouse.getTime().getTime() - lastMouseUpTime) < | |
1321 | doubleClickTime) { | |
1322 | ||
1323 | // This is a double-click. | |
1324 | doubleClick = new TMouseEvent(TMouseEvent.Type. | |
1325 | MOUSE_DOUBLE_CLICK, | |
1326 | mouse.getX(), mouse.getY(), | |
1327 | mouse.getAbsoluteX(), mouse.getAbsoluteY(), | |
1328 | mouse.isMouse1(), mouse.isMouse2(), | |
1329 | mouse.isMouse3(), | |
1330 | mouse.isMouseWheelUp(), mouse.isMouseWheelDown()); | |
1331 | ||
1332 | } else { | |
1333 | // The first click of a potential double-click. | |
1334 | lastMouseUpTime = mouse.getTime().getTime(); | |
1335 | } | |
1336 | } | |
e8a11f98 KL |
1337 | } |
1338 | } | |
1339 | ||
c6940ed9 | 1340 | secondaryEventReceiver.handleEvent(event); |
b6faeac0 KL |
1341 | if (doubleClick != null) { |
1342 | secondaryEventReceiver.handleEvent(doubleClick); | |
1343 | } | |
c6940ed9 KL |
1344 | } |
1345 | ||
1346 | /** | |
1347 | * Enable a widget to override the primary event thread. | |
1348 | * | |
1349 | * @param widget widget that will receive events | |
1350 | */ | |
1351 | public final void enableSecondaryEventReceiver(final TWidget widget) { | |
be72cb5c KL |
1352 | if (debugThreads) { |
1353 | System.err.println(System.currentTimeMillis() + | |
1354 | " enableSecondaryEventReceiver()"); | |
1355 | } | |
1356 | ||
c6940ed9 KL |
1357 | assert (secondaryEventReceiver == null); |
1358 | assert (secondaryEventHandler == null); | |
a043164f KL |
1359 | assert ((widget instanceof TMessageBox) |
1360 | || (widget instanceof TFileOpenBox)); | |
c6940ed9 KL |
1361 | secondaryEventReceiver = widget; |
1362 | secondaryEventHandler = new WidgetEventHandler(this, false); | |
be72cb5c | 1363 | |
c6940ed9 | 1364 | (new Thread(secondaryEventHandler)).start(); |
c6940ed9 KL |
1365 | } |
1366 | ||
1367 | /** | |
1368 | * Yield to the secondary thread. | |
1369 | */ | |
1370 | public final void yield() { | |
1371 | assert (secondaryEventReceiver != null); | |
99144c71 | 1372 | |
c6940ed9 | 1373 | while (secondaryEventReceiver != null) { |
92554d64 | 1374 | synchronized (primaryEventHandler) { |
c6940ed9 | 1375 | try { |
92554d64 | 1376 | primaryEventHandler.wait(); |
c6940ed9 KL |
1377 | } catch (InterruptedException e) { |
1378 | // SQUASH | |
1379 | } | |
1380 | } | |
1381 | } | |
a06459bd KL |
1382 | } |
1383 | ||
4328bb42 KL |
1384 | /** |
1385 | * Do stuff when there is no user input. | |
1386 | */ | |
1387 | private void doIdle() { | |
99144c71 | 1388 | if (debugThreads) { |
be72cb5c KL |
1389 | System.err.printf(System.currentTimeMillis() + " " + |
1390 | Thread.currentThread() + " doIdle()\n"); | |
99144c71 KL |
1391 | } |
1392 | ||
be72cb5c KL |
1393 | synchronized (timers) { |
1394 | ||
1395 | if (debugThreads) { | |
1396 | System.err.printf(System.currentTimeMillis() + " " + | |
1397 | Thread.currentThread() + " doIdle() 2\n"); | |
1398 | } | |
1399 | ||
1400 | // Run any timers that have timed out | |
1401 | Date now = new Date(); | |
1402 | List<TTimer> keepTimers = new LinkedList<TTimer>(); | |
1403 | for (TTimer timer: timers) { | |
1404 | if (timer.getNextTick().getTime() <= now.getTime()) { | |
1405 | // Something might change, so repaint the screen. | |
1406 | repaint = true; | |
1407 | timer.tick(); | |
1408 | if (timer.recurring) { | |
1409 | keepTimers.add(timer); | |
1410 | } | |
1411 | } else { | |
d502a0e9 | 1412 | keepTimers.add(timer); |
7b5261bc | 1413 | } |
7b5261bc | 1414 | } |
be72cb5c | 1415 | timers = keepTimers; |
7b5261bc | 1416 | } |
7b5261bc KL |
1417 | |
1418 | // Call onIdle's | |
d502a0e9 KL |
1419 | for (TWindow window: windows) { |
1420 | window.onIdle(); | |
7b5261bc | 1421 | } |
92453213 KL |
1422 | if (desktop != null) { |
1423 | desktop.onIdle(); | |
1424 | } | |
4328bb42 | 1425 | } |
7d4115a5 | 1426 | |
2ce6dab2 KL |
1427 | // ------------------------------------------------------------------------ |
1428 | // TWindow management ----------------------------------------------------- | |
1429 | // ------------------------------------------------------------------------ | |
4328bb42 | 1430 | |
92453213 KL |
1431 | /** |
1432 | * Return the total number of windows. | |
1433 | * | |
1434 | * @return the total number of windows | |
1435 | */ | |
1436 | public final int windowCount() { | |
1437 | return windows.size(); | |
1438 | } | |
1439 | ||
1440 | /** | |
8c236a98 | 1441 | * Return the number of windows that are showing. |
92453213 | 1442 | * |
8c236a98 | 1443 | * @return the number of windows that are showing on screen |
92453213 KL |
1444 | */ |
1445 | public final int shownWindowCount() { | |
1446 | int n = 0; | |
1447 | for (TWindow w: windows) { | |
1448 | if (w.isShown()) { | |
1449 | n++; | |
1450 | } | |
1451 | } | |
1452 | return n; | |
1453 | } | |
1454 | ||
8c236a98 KL |
1455 | /** |
1456 | * Return the number of windows that are hidden. | |
1457 | * | |
1458 | * @return the number of windows that are hidden | |
1459 | */ | |
1460 | public final int hiddenWindowCount() { | |
1461 | int n = 0; | |
1462 | for (TWindow w: windows) { | |
1463 | if (w.isHidden()) { | |
1464 | n++; | |
1465 | } | |
1466 | } | |
1467 | return n; | |
1468 | } | |
1469 | ||
92453213 KL |
1470 | /** |
1471 | * Check if a window instance is in this application's window list. | |
1472 | * | |
1473 | * @param window window to look for | |
1474 | * @return true if this window is in the list | |
1475 | */ | |
1476 | public final boolean hasWindow(final TWindow window) { | |
1477 | if (windows.size() == 0) { | |
1478 | return false; | |
1479 | } | |
1480 | for (TWindow w: windows) { | |
1481 | if (w == window) { | |
8c236a98 | 1482 | assert (window.getApplication() == this); |
92453213 KL |
1483 | return true; |
1484 | } | |
1485 | } | |
1486 | return false; | |
1487 | } | |
1488 | ||
1489 | /** | |
1490 | * Activate a window: bring it to the top and have it receive events. | |
1491 | * | |
1492 | * @param window the window to become the new active window | |
1493 | */ | |
1494 | public void activateWindow(final TWindow window) { | |
1495 | if (hasWindow(window) == false) { | |
1496 | /* | |
1497 | * Someone has a handle to a window I don't have. Ignore this | |
1498 | * request. | |
1499 | */ | |
1500 | return; | |
1501 | } | |
1502 | ||
fe0770f9 KL |
1503 | // Whatever window might be moving/dragging, stop it now. |
1504 | for (TWindow w: windows) { | |
1505 | if (w.inMovements()) { | |
1506 | w.stopMovements(); | |
1507 | } | |
1508 | } | |
1509 | ||
92453213 KL |
1510 | assert (windows.size() > 0); |
1511 | ||
1512 | if (window.isHidden()) { | |
1513 | // Unhiding will also activate. | |
1514 | showWindow(window); | |
1515 | return; | |
1516 | } | |
1517 | assert (window.isShown()); | |
1518 | ||
1519 | if (windows.size() == 1) { | |
1520 | assert (window == windows.get(0)); | |
1521 | if (activeWindow == null) { | |
1522 | activeWindow = window; | |
1523 | window.setZ(0); | |
1524 | activeWindow.setActive(true); | |
1525 | activeWindow.onFocus(); | |
1526 | } | |
1527 | ||
1528 | assert (window.isActive()); | |
1529 | assert (activeWindow == window); | |
1530 | return; | |
1531 | } | |
1532 | ||
1533 | if (activeWindow == window) { | |
1534 | assert (window.isActive()); | |
1535 | ||
1536 | // Window is already active, do nothing. | |
1537 | return; | |
1538 | } | |
1539 | ||
1540 | assert (!window.isActive()); | |
1541 | if (activeWindow != null) { | |
1542 | assert (activeWindow.getZ() == 0); | |
1543 | ||
92453213 KL |
1544 | activeWindow.setActive(false); |
1545 | activeWindow.setZ(window.getZ()); | |
499fdccf KL |
1546 | |
1547 | // Unset activeWindow now before unfocus, so that a window | |
1548 | // lifecycle change inside onUnfocus() doesn't call | |
1549 | // switchWindow() and lead to a stack overflow. | |
1550 | TWindow oldActiveWindow = activeWindow; | |
1551 | activeWindow = null; | |
1552 | oldActiveWindow.onUnfocus(); | |
92453213 KL |
1553 | } |
1554 | activeWindow = window; | |
1555 | activeWindow.setZ(0); | |
1556 | activeWindow.setActive(true); | |
1557 | activeWindow.onFocus(); | |
1558 | return; | |
1559 | } | |
1560 | ||
1561 | /** | |
1562 | * Hide a window. | |
1563 | * | |
1564 | * @param window the window to hide | |
1565 | */ | |
1566 | public void hideWindow(final TWindow window) { | |
1567 | if (hasWindow(window) == false) { | |
1568 | /* | |
1569 | * Someone has a handle to a window I don't have. Ignore this | |
1570 | * request. | |
1571 | */ | |
1572 | return; | |
1573 | } | |
1574 | ||
fe0770f9 KL |
1575 | // Whatever window might be moving/dragging, stop it now. |
1576 | for (TWindow w: windows) { | |
1577 | if (w.inMovements()) { | |
1578 | w.stopMovements(); | |
1579 | } | |
1580 | } | |
1581 | ||
92453213 KL |
1582 | assert (windows.size() > 0); |
1583 | ||
1584 | if (!window.hidden) { | |
1585 | if (window == activeWindow) { | |
1586 | if (shownWindowCount() > 1) { | |
1587 | switchWindow(true); | |
1588 | } else { | |
1589 | activeWindow = null; | |
1590 | window.setActive(false); | |
1591 | window.onUnfocus(); | |
1592 | } | |
1593 | } | |
1594 | window.hidden = true; | |
1595 | window.onHide(); | |
1596 | } | |
1597 | } | |
1598 | ||
1599 | /** | |
1600 | * Show a window. | |
1601 | * | |
1602 | * @param window the window to show | |
1603 | */ | |
1604 | public void showWindow(final TWindow window) { | |
1605 | if (hasWindow(window) == false) { | |
1606 | /* | |
1607 | * Someone has a handle to a window I don't have. Ignore this | |
1608 | * request. | |
1609 | */ | |
1610 | return; | |
1611 | } | |
1612 | ||
fe0770f9 KL |
1613 | // Whatever window might be moving/dragging, stop it now. |
1614 | for (TWindow w: windows) { | |
1615 | if (w.inMovements()) { | |
1616 | w.stopMovements(); | |
1617 | } | |
1618 | } | |
1619 | ||
92453213 KL |
1620 | assert (windows.size() > 0); |
1621 | ||
1622 | if (window.hidden) { | |
1623 | window.hidden = false; | |
1624 | window.onShow(); | |
1625 | activateWindow(window); | |
1626 | } | |
1627 | } | |
1628 | ||
48e27807 KL |
1629 | /** |
1630 | * Close window. Note that the window's destructor is NOT called by this | |
1631 | * method, instead the GC is assumed to do the cleanup. | |
1632 | * | |
1633 | * @param window the window to remove | |
1634 | */ | |
1635 | public final void closeWindow(final TWindow window) { | |
92453213 KL |
1636 | if (hasWindow(window) == false) { |
1637 | /* | |
1638 | * Someone has a handle to a window I don't have. Ignore this | |
1639 | * request. | |
1640 | */ | |
1641 | return; | |
1642 | } | |
1643 | ||
bb35d919 | 1644 | synchronized (windows) { |
fe0770f9 KL |
1645 | // Whatever window might be moving/dragging, stop it now. |
1646 | for (TWindow w: windows) { | |
1647 | if (w.inMovements()) { | |
1648 | w.stopMovements(); | |
1649 | } | |
1650 | } | |
1651 | ||
bb35d919 KL |
1652 | int z = window.getZ(); |
1653 | window.setZ(-1); | |
efb7af1f | 1654 | window.onUnfocus(); |
bb35d919 KL |
1655 | Collections.sort(windows); |
1656 | windows.remove(0); | |
92453213 | 1657 | activeWindow = null; |
bb35d919 | 1658 | for (TWindow w: windows) { |
3eacc236 KL |
1659 | |
1660 | // Do not activate a hidden window. | |
1661 | if (w.isHidden()) { | |
1662 | continue; | |
1663 | } | |
1664 | ||
bb35d919 KL |
1665 | if (w.getZ() > z) { |
1666 | w.setZ(w.getZ() - 1); | |
1667 | if (w.getZ() == 0) { | |
1668 | w.setActive(true); | |
efb7af1f | 1669 | w.onFocus(); |
bb35d919 KL |
1670 | assert (activeWindow == null); |
1671 | activeWindow = w; | |
1672 | } else { | |
efb7af1f KL |
1673 | if (w.isActive()) { |
1674 | w.setActive(false); | |
1675 | w.onUnfocus(); | |
1676 | } | |
bb35d919 | 1677 | } |
48e27807 KL |
1678 | } |
1679 | } | |
1680 | } | |
1681 | ||
1682 | // Perform window cleanup | |
1683 | window.onClose(); | |
1684 | ||
48e27807 | 1685 | // Check if we are closing a TMessageBox or similar |
c6940ed9 KL |
1686 | if (secondaryEventReceiver != null) { |
1687 | assert (secondaryEventHandler != null); | |
48e27807 KL |
1688 | |
1689 | // Do not send events to the secondaryEventReceiver anymore, the | |
1690 | // window is closed. | |
1691 | secondaryEventReceiver = null; | |
1692 | ||
92554d64 KL |
1693 | // Wake the secondary thread, it will wake the primary as it |
1694 | // exits. | |
1695 | synchronized (secondaryEventHandler) { | |
1696 | secondaryEventHandler.notify(); | |
48e27807 KL |
1697 | } |
1698 | } | |
92453213 KL |
1699 | |
1700 | // Permit desktop to be active if it is the only thing left. | |
1701 | if (desktop != null) { | |
1702 | if (windows.size() == 0) { | |
1703 | desktop.setActive(true); | |
1704 | } | |
1705 | } | |
48e27807 KL |
1706 | } |
1707 | ||
1708 | /** | |
1709 | * Switch to the next window. | |
1710 | * | |
1711 | * @param forward if true, then switch to the next window in the list, | |
1712 | * otherwise switch to the previous window in the list | |
1713 | */ | |
1714 | public final void switchWindow(final boolean forward) { | |
8c236a98 KL |
1715 | // Only switch if there are multiple visible windows |
1716 | if (shownWindowCount() < 2) { | |
48e27807 KL |
1717 | return; |
1718 | } | |
92453213 | 1719 | assert (activeWindow != null); |
48e27807 | 1720 | |
bb35d919 | 1721 | synchronized (windows) { |
fe0770f9 KL |
1722 | // Whatever window might be moving/dragging, stop it now. |
1723 | for (TWindow w: windows) { | |
1724 | if (w.inMovements()) { | |
1725 | w.stopMovements(); | |
1726 | } | |
1727 | } | |
bb35d919 KL |
1728 | |
1729 | // Swap z/active between active window and the next in the list | |
1730 | int activeWindowI = -1; | |
1731 | for (int i = 0; i < windows.size(); i++) { | |
92453213 KL |
1732 | if (windows.get(i) == activeWindow) { |
1733 | assert (activeWindow.isActive()); | |
bb35d919 KL |
1734 | activeWindowI = i; |
1735 | break; | |
92453213 KL |
1736 | } else { |
1737 | assert (!windows.get(0).isActive()); | |
bb35d919 | 1738 | } |
48e27807 | 1739 | } |
bb35d919 | 1740 | assert (activeWindowI >= 0); |
48e27807 | 1741 | |
bb35d919 | 1742 | // Do not switch if a window is modal |
92453213 | 1743 | if (activeWindow.isModal()) { |
bb35d919 KL |
1744 | return; |
1745 | } | |
48e27807 | 1746 | |
8c236a98 KL |
1747 | int nextWindowI = activeWindowI; |
1748 | for (;;) { | |
1749 | if (forward) { | |
1750 | nextWindowI++; | |
1751 | nextWindowI %= windows.size(); | |
bb35d919 | 1752 | } else { |
8c236a98 KL |
1753 | nextWindowI--; |
1754 | if (nextWindowI < 0) { | |
1755 | nextWindowI = windows.size() - 1; | |
1756 | } | |
bb35d919 | 1757 | } |
bb35d919 | 1758 | |
8c236a98 KL |
1759 | if (windows.get(nextWindowI).isShown()) { |
1760 | activateWindow(windows.get(nextWindowI)); | |
1761 | break; | |
1762 | } | |
1763 | } | |
bb35d919 | 1764 | } // synchronized (windows) |
48e27807 | 1765 | |
48e27807 KL |
1766 | } |
1767 | ||
1768 | /** | |
1769 | * Add a window to my window list and make it active. | |
1770 | * | |
1771 | * @param window new window to add | |
1772 | */ | |
1773 | public final void addWindow(final TWindow window) { | |
a7986f7b KL |
1774 | |
1775 | // Do not add menu windows to the window list. | |
1776 | if (window instanceof TMenu) { | |
1777 | return; | |
1778 | } | |
1779 | ||
0ee88b6d KL |
1780 | // Do not add the desktop to the window list. |
1781 | if (window instanceof TDesktop) { | |
1782 | return; | |
1783 | } | |
1784 | ||
bb35d919 | 1785 | synchronized (windows) { |
fe0770f9 KL |
1786 | // Whatever window might be moving/dragging, stop it now. |
1787 | for (TWindow w: windows) { | |
1788 | if (w.inMovements()) { | |
1789 | w.stopMovements(); | |
1790 | } | |
1791 | } | |
1792 | ||
2ce6dab2 KL |
1793 | // Do not allow a modal window to spawn a non-modal window. If a |
1794 | // modal window is active, then this window will become modal | |
1795 | // too. | |
1796 | if (modalWindowActive()) { | |
1797 | window.flags |= TWindow.MODAL; | |
a7986f7b | 1798 | window.flags |= TWindow.CENTERED; |
92453213 | 1799 | window.hidden = false; |
bb35d919 | 1800 | } |
92453213 KL |
1801 | if (window.isShown()) { |
1802 | for (TWindow w: windows) { | |
1803 | if (w.isActive()) { | |
1804 | w.setActive(false); | |
1805 | w.onUnfocus(); | |
1806 | } | |
1807 | w.setZ(w.getZ() + 1); | |
efb7af1f | 1808 | } |
bb35d919 KL |
1809 | } |
1810 | windows.add(window); | |
92453213 KL |
1811 | if (window.isShown()) { |
1812 | activeWindow = window; | |
1813 | activeWindow.setZ(0); | |
1814 | activeWindow.setActive(true); | |
1815 | activeWindow.onFocus(); | |
1816 | } | |
a7986f7b KL |
1817 | |
1818 | if (((window.flags & TWindow.CENTERED) == 0) | |
1819 | && smartWindowPlacement) { | |
1820 | ||
1821 | doSmartPlacement(window); | |
1822 | } | |
48e27807 | 1823 | } |
92453213 KL |
1824 | |
1825 | // Desktop cannot be active over any other window. | |
1826 | if (desktop != null) { | |
1827 | desktop.setActive(false); | |
1828 | } | |
48e27807 KL |
1829 | } |
1830 | ||
fca67db0 KL |
1831 | /** |
1832 | * Check if there is a system-modal window on top. | |
1833 | * | |
1834 | * @return true if the active window is modal | |
1835 | */ | |
1836 | private boolean modalWindowActive() { | |
1837 | if (windows.size() == 0) { | |
1838 | return false; | |
1839 | } | |
2ce6dab2 KL |
1840 | |
1841 | for (TWindow w: windows) { | |
1842 | if (w.isModal()) { | |
1843 | return true; | |
1844 | } | |
1845 | } | |
1846 | ||
1847 | return false; | |
1848 | } | |
1849 | ||
1850 | /** | |
1851 | * Close all open windows. | |
1852 | */ | |
1853 | private void closeAllWindows() { | |
1854 | // Don't do anything if we are in the menu | |
1855 | if (activeMenu != null) { | |
1856 | return; | |
1857 | } | |
1858 | while (windows.size() > 0) { | |
1859 | closeWindow(windows.get(0)); | |
1860 | } | |
fca67db0 KL |
1861 | } |
1862 | ||
2ce6dab2 KL |
1863 | /** |
1864 | * Re-layout the open windows as non-overlapping tiles. This produces | |
1865 | * almost the same results as Turbo Pascal 7.0's IDE. | |
1866 | */ | |
1867 | private void tileWindows() { | |
1868 | synchronized (windows) { | |
1869 | // Don't do anything if we are in the menu | |
1870 | if (activeMenu != null) { | |
1871 | return; | |
1872 | } | |
1873 | int z = windows.size(); | |
1874 | if (z == 0) { | |
1875 | return; | |
1876 | } | |
1877 | int a = 0; | |
1878 | int b = 0; | |
1879 | a = (int)(Math.sqrt(z)); | |
1880 | int c = 0; | |
1881 | while (c < a) { | |
1882 | b = (z - c) / a; | |
1883 | if (((a * b) + c) == z) { | |
1884 | break; | |
1885 | } | |
1886 | c++; | |
1887 | } | |
1888 | assert (a > 0); | |
1889 | assert (b > 0); | |
1890 | assert (c < a); | |
1891 | int newWidth = (getScreen().getWidth() / a); | |
1892 | int newHeight1 = ((getScreen().getHeight() - 1) / b); | |
1893 | int newHeight2 = ((getScreen().getHeight() - 1) / (b + c)); | |
1894 | ||
1895 | List<TWindow> sorted = new LinkedList<TWindow>(windows); | |
1896 | Collections.sort(sorted); | |
1897 | Collections.reverse(sorted); | |
1898 | for (int i = 0; i < sorted.size(); i++) { | |
1899 | int logicalX = i / b; | |
1900 | int logicalY = i % b; | |
1901 | if (i >= ((a - 1) * b)) { | |
1902 | logicalX = a - 1; | |
1903 | logicalY = i - ((a - 1) * b); | |
1904 | } | |
1905 | ||
1906 | TWindow w = sorted.get(i); | |
1907 | w.setX(logicalX * newWidth); | |
1908 | w.setWidth(newWidth); | |
1909 | if (i >= ((a - 1) * b)) { | |
1910 | w.setY((logicalY * newHeight2) + 1); | |
1911 | w.setHeight(newHeight2); | |
1912 | } else { | |
1913 | w.setY((logicalY * newHeight1) + 1); | |
1914 | w.setHeight(newHeight1); | |
1915 | } | |
1916 | } | |
1917 | } | |
1918 | } | |
1919 | ||
1920 | /** | |
1921 | * Re-layout the open windows as overlapping cascaded windows. | |
1922 | */ | |
1923 | private void cascadeWindows() { | |
1924 | synchronized (windows) { | |
1925 | // Don't do anything if we are in the menu | |
1926 | if (activeMenu != null) { | |
1927 | return; | |
1928 | } | |
1929 | int x = 0; | |
1930 | int y = 1; | |
1931 | List<TWindow> sorted = new LinkedList<TWindow>(windows); | |
1932 | Collections.sort(sorted); | |
1933 | Collections.reverse(sorted); | |
1934 | for (TWindow window: sorted) { | |
1935 | window.setX(x); | |
1936 | window.setY(y); | |
1937 | x++; | |
1938 | y++; | |
1939 | if (x > getScreen().getWidth()) { | |
1940 | x = 0; | |
1941 | } | |
1942 | if (y >= getScreen().getHeight()) { | |
1943 | y = 1; | |
1944 | } | |
1945 | } | |
1946 | } | |
1947 | } | |
1948 | ||
a7986f7b KL |
1949 | /** |
1950 | * Place a window to minimize its overlap with other windows. | |
1951 | * | |
1952 | * @param window the window to place | |
1953 | */ | |
1954 | public final void doSmartPlacement(final TWindow window) { | |
1955 | // This is a pretty dumb algorithm, but seems to work. The hardest | |
1956 | // part is computing these "overlap" values seeking a minimum average | |
1957 | // overlap. | |
1958 | int xMin = 0; | |
1959 | int yMin = desktopTop; | |
1960 | int xMax = getScreen().getWidth() - window.getWidth() + 1; | |
1961 | int yMax = desktopBottom - window.getHeight() + 1; | |
1962 | if (xMax < xMin) { | |
1963 | xMax = xMin; | |
1964 | } | |
1965 | if (yMax < yMin) { | |
1966 | yMax = yMin; | |
1967 | } | |
1968 | ||
1969 | if ((xMin == xMax) && (yMin == yMax)) { | |
1970 | // No work to do, bail out. | |
1971 | return; | |
1972 | } | |
1973 | ||
1974 | // Compute the overlap matrix without the new window. | |
1975 | int width = getScreen().getWidth(); | |
1976 | int height = getScreen().getHeight(); | |
1977 | int overlapMatrix[][] = new int[width][height]; | |
1978 | for (TWindow w: windows) { | |
1979 | if (window == w) { | |
1980 | continue; | |
1981 | } | |
1982 | for (int x = w.getX(); x < w.getX() + w.getWidth(); x++) { | |
9ff1c0e3 KL |
1983 | if (x < 0) { |
1984 | continue; | |
1985 | } | |
8c236a98 | 1986 | if (x >= width) { |
a7986f7b KL |
1987 | continue; |
1988 | } | |
1989 | for (int y = w.getY(); y < w.getY() + w.getHeight(); y++) { | |
9ff1c0e3 KL |
1990 | if (y < 0) { |
1991 | continue; | |
1992 | } | |
8c236a98 | 1993 | if (y >= height) { |
a7986f7b KL |
1994 | continue; |
1995 | } | |
1996 | overlapMatrix[x][y]++; | |
1997 | } | |
1998 | } | |
1999 | } | |
2000 | ||
2001 | long oldOverlapTotal = 0; | |
2002 | long oldOverlapN = 0; | |
2003 | for (int x = 0; x < width; x++) { | |
2004 | for (int y = 0; y < height; y++) { | |
2005 | oldOverlapTotal += overlapMatrix[x][y]; | |
2006 | if (overlapMatrix[x][y] > 0) { | |
2007 | oldOverlapN++; | |
2008 | } | |
2009 | } | |
2010 | } | |
2011 | ||
2012 | ||
2013 | double oldOverlapAvg = (double) oldOverlapTotal / (double) oldOverlapN; | |
2014 | boolean first = true; | |
2015 | int windowX = window.getX(); | |
2016 | int windowY = window.getY(); | |
2017 | ||
2018 | // For each possible (x, y) position for the new window, compute a | |
2019 | // new overlap matrix. | |
2020 | for (int x = xMin; x < xMax; x++) { | |
2021 | for (int y = yMin; y < yMax; y++) { | |
2022 | ||
2023 | // Start with the matrix minus this window. | |
2024 | int newMatrix[][] = new int[width][height]; | |
2025 | for (int mx = 0; mx < width; mx++) { | |
2026 | for (int my = 0; my < height; my++) { | |
2027 | newMatrix[mx][my] = overlapMatrix[mx][my]; | |
2028 | } | |
2029 | } | |
2030 | ||
2031 | // Add this window's values to the new overlap matrix. | |
2032 | long newOverlapTotal = 0; | |
2033 | long newOverlapN = 0; | |
2034 | // Start by adding each new cell. | |
2035 | for (int wx = x; wx < x + window.getWidth(); wx++) { | |
8c236a98 | 2036 | if (wx >= width) { |
a7986f7b KL |
2037 | continue; |
2038 | } | |
2039 | for (int wy = y; wy < y + window.getHeight(); wy++) { | |
8c236a98 | 2040 | if (wy >= height) { |
a7986f7b KL |
2041 | continue; |
2042 | } | |
2043 | newMatrix[wx][wy]++; | |
2044 | } | |
2045 | } | |
2046 | // Now figure out the new value for total coverage. | |
2047 | for (int mx = 0; mx < width; mx++) { | |
2048 | for (int my = 0; my < height; my++) { | |
2049 | newOverlapTotal += newMatrix[x][y]; | |
2050 | if (newMatrix[mx][my] > 0) { | |
2051 | newOverlapN++; | |
2052 | } | |
2053 | } | |
2054 | } | |
2055 | double newOverlapAvg = (double) newOverlapTotal / (double) newOverlapN; | |
2056 | ||
2057 | if (first) { | |
2058 | // First time: just record what we got. | |
2059 | oldOverlapAvg = newOverlapAvg; | |
2060 | first = false; | |
2061 | } else { | |
2062 | // All other times: pick a new best (x, y) and save the | |
2063 | // overlap value. | |
2064 | if (newOverlapAvg < oldOverlapAvg) { | |
2065 | windowX = x; | |
2066 | windowY = y; | |
2067 | oldOverlapAvg = newOverlapAvg; | |
2068 | } | |
2069 | } | |
2070 | ||
2071 | } // for (int x = xMin; x < xMax; x++) | |
2072 | ||
2073 | } // for (int y = yMin; y < yMax; y++) | |
2074 | ||
2075 | // Finally, set the window's new coordinates. | |
2076 | window.setX(windowX); | |
2077 | window.setY(windowY); | |
2078 | } | |
2079 | ||
2ce6dab2 KL |
2080 | // ------------------------------------------------------------------------ |
2081 | // TMenu management ------------------------------------------------------- | |
2082 | // ------------------------------------------------------------------------ | |
2083 | ||
fca67db0 KL |
2084 | /** |
2085 | * Check if a mouse event would hit either the active menu or any open | |
2086 | * sub-menus. | |
2087 | * | |
2088 | * @param mouse mouse event | |
2089 | * @return true if the mouse would hit the active menu or an open | |
2090 | * sub-menu | |
2091 | */ | |
2092 | private boolean mouseOnMenu(final TMouseEvent mouse) { | |
2093 | assert (activeMenu != null); | |
2094 | List<TMenu> menus = new LinkedList<TMenu>(subMenus); | |
2095 | Collections.reverse(menus); | |
2096 | for (TMenu menu: menus) { | |
2097 | if (menu.mouseWouldHit(mouse)) { | |
2098 | return true; | |
2099 | } | |
2100 | } | |
2101 | return activeMenu.mouseWouldHit(mouse); | |
2102 | } | |
2103 | ||
2104 | /** | |
2105 | * See if we need to switch window or activate the menu based on | |
2106 | * a mouse click. | |
2107 | * | |
2108 | * @param mouse mouse event | |
2109 | */ | |
2110 | private void checkSwitchFocus(final TMouseEvent mouse) { | |
2111 | ||
2112 | if ((mouse.getType() == TMouseEvent.Type.MOUSE_DOWN) | |
2113 | && (activeMenu != null) | |
2114 | && (mouse.getAbsoluteY() != 0) | |
2115 | && (!mouseOnMenu(mouse)) | |
2116 | ) { | |
2117 | // They clicked outside the active menu, turn it off | |
2118 | activeMenu.setActive(false); | |
2119 | activeMenu = null; | |
2120 | for (TMenu menu: subMenus) { | |
2121 | menu.setActive(false); | |
2122 | } | |
2123 | subMenus.clear(); | |
2124 | // Continue checks | |
2125 | } | |
2126 | ||
2127 | // See if they hit the menu bar | |
2128 | if ((mouse.getType() == TMouseEvent.Type.MOUSE_DOWN) | |
7c870d89 | 2129 | && (mouse.isMouse1()) |
fca67db0 KL |
2130 | && (!modalWindowActive()) |
2131 | && (mouse.getAbsoluteY() == 0) | |
2132 | ) { | |
2133 | ||
2134 | for (TMenu menu: subMenus) { | |
2135 | menu.setActive(false); | |
2136 | } | |
2137 | subMenus.clear(); | |
2138 | ||
2139 | // They selected the menu, go activate it | |
2140 | for (TMenu menu: menus) { | |
159f076d KL |
2141 | if ((mouse.getAbsoluteX() >= menu.getTitleX()) |
2142 | && (mouse.getAbsoluteX() < menu.getTitleX() | |
fca67db0 KL |
2143 | + menu.getTitle().length() + 2) |
2144 | ) { | |
2145 | menu.setActive(true); | |
2146 | activeMenu = menu; | |
2147 | } else { | |
2148 | menu.setActive(false); | |
2149 | } | |
2150 | } | |
fca67db0 KL |
2151 | return; |
2152 | } | |
2153 | ||
2154 | // See if they hit the menu bar | |
2155 | if ((mouse.getType() == TMouseEvent.Type.MOUSE_MOTION) | |
7c870d89 | 2156 | && (mouse.isMouse1()) |
fca67db0 KL |
2157 | && (activeMenu != null) |
2158 | && (mouse.getAbsoluteY() == 0) | |
2159 | ) { | |
2160 | ||
2161 | TMenu oldMenu = activeMenu; | |
2162 | for (TMenu menu: subMenus) { | |
2163 | menu.setActive(false); | |
2164 | } | |
2165 | subMenus.clear(); | |
2166 | ||
2167 | // See if we should switch menus | |
2168 | for (TMenu menu: menus) { | |
159f076d KL |
2169 | if ((mouse.getAbsoluteX() >= menu.getTitleX()) |
2170 | && (mouse.getAbsoluteX() < menu.getTitleX() | |
fca67db0 KL |
2171 | + menu.getTitle().length() + 2) |
2172 | ) { | |
2173 | menu.setActive(true); | |
2174 | activeMenu = menu; | |
2175 | } | |
2176 | } | |
2177 | if (oldMenu != activeMenu) { | |
2178 | // They switched menus | |
2179 | oldMenu.setActive(false); | |
2180 | } | |
fca67db0 KL |
2181 | return; |
2182 | } | |
2183 | ||
72fca17b KL |
2184 | // If a menu is still active, don't switch windows |
2185 | if (activeMenu != null) { | |
fca67db0 KL |
2186 | return; |
2187 | } | |
2188 | ||
72fca17b KL |
2189 | // Only switch if there are multiple windows |
2190 | if (windows.size() < 2) { | |
fca67db0 KL |
2191 | return; |
2192 | } | |
2193 | ||
72fca17b KL |
2194 | if (((focusFollowsMouse == true) |
2195 | && (mouse.getType() == TMouseEvent.Type.MOUSE_MOTION)) | |
2196 | || (mouse.getType() == TMouseEvent.Type.MOUSE_UP) | |
2197 | ) { | |
2198 | synchronized (windows) { | |
2199 | Collections.sort(windows); | |
2200 | if (windows.get(0).isModal()) { | |
2201 | // Modal windows don't switch | |
2202 | return; | |
2203 | } | |
fca67db0 | 2204 | |
72fca17b KL |
2205 | for (TWindow window: windows) { |
2206 | assert (!window.isModal()); | |
92453213 | 2207 | |
72fca17b KL |
2208 | if (window.isHidden()) { |
2209 | assert (!window.isActive()); | |
2210 | continue; | |
2211 | } | |
92453213 | 2212 | |
72fca17b KL |
2213 | if (window.mouseWouldHit(mouse)) { |
2214 | if (window == windows.get(0)) { | |
2215 | // Clicked on the same window, nothing to do | |
2216 | assert (window.isActive()); | |
2217 | return; | |
2218 | } | |
2219 | ||
2220 | // We will be switching to another window | |
2221 | assert (windows.get(0).isActive()); | |
2222 | assert (windows.get(0) == activeWindow); | |
2223 | assert (!window.isActive()); | |
2224 | activeWindow.onUnfocus(); | |
2225 | activeWindow.setActive(false); | |
2226 | activeWindow.setZ(window.getZ()); | |
2227 | activeWindow = window; | |
2228 | window.setZ(0); | |
2229 | window.setActive(true); | |
2230 | window.onFocus(); | |
bb35d919 KL |
2231 | return; |
2232 | } | |
fca67db0 | 2233 | } |
fca67db0 | 2234 | } |
72fca17b KL |
2235 | |
2236 | // Clicked on the background, nothing to do | |
2237 | return; | |
fca67db0 KL |
2238 | } |
2239 | ||
72fca17b KL |
2240 | // Nothing to do: this isn't a mouse up, or focus isn't following |
2241 | // mouse. | |
fca67db0 KL |
2242 | return; |
2243 | } | |
2244 | ||
2245 | /** | |
2246 | * Turn off the menu. | |
2247 | */ | |
928811d8 | 2248 | public final void closeMenu() { |
fca67db0 KL |
2249 | if (activeMenu != null) { |
2250 | activeMenu.setActive(false); | |
2251 | activeMenu = null; | |
2252 | for (TMenu menu: subMenus) { | |
2253 | menu.setActive(false); | |
2254 | } | |
2255 | subMenus.clear(); | |
2256 | } | |
fca67db0 KL |
2257 | } |
2258 | ||
e8a11f98 KL |
2259 | /** |
2260 | * Get a (shallow) copy of the menu list. | |
2261 | * | |
2262 | * @return a copy of the menu list | |
2263 | */ | |
2264 | public final List<TMenu> getAllMenus() { | |
2265 | return new LinkedList<TMenu>(menus); | |
2266 | } | |
2267 | ||
2268 | /** | |
2269 | * Add a top-level menu to the list. | |
2270 | * | |
2271 | * @param menu the menu to add | |
2272 | * @throws IllegalArgumentException if the menu is already used in | |
2273 | * another TApplication | |
2274 | */ | |
2275 | public final void addMenu(final TMenu menu) { | |
2276 | if ((menu.getApplication() != null) | |
2277 | && (menu.getApplication() != this) | |
2278 | ) { | |
2279 | throw new IllegalArgumentException("Menu " + menu + " is already " + | |
2280 | "part of application " + menu.getApplication()); | |
2281 | } | |
2282 | closeMenu(); | |
2283 | menus.add(menu); | |
2284 | recomputeMenuX(); | |
2285 | } | |
2286 | ||
2287 | /** | |
2288 | * Remove a top-level menu from the list. | |
2289 | * | |
2290 | * @param menu the menu to remove | |
2291 | * @throws IllegalArgumentException if the menu is already used in | |
2292 | * another TApplication | |
2293 | */ | |
2294 | public final void removeMenu(final TMenu menu) { | |
2295 | if ((menu.getApplication() != null) | |
2296 | && (menu.getApplication() != this) | |
2297 | ) { | |
2298 | throw new IllegalArgumentException("Menu " + menu + " is already " + | |
2299 | "part of application " + menu.getApplication()); | |
2300 | } | |
2301 | closeMenu(); | |
2302 | menus.remove(menu); | |
2303 | recomputeMenuX(); | |
2304 | } | |
2305 | ||
fca67db0 KL |
2306 | /** |
2307 | * Turn off a sub-menu. | |
2308 | */ | |
928811d8 | 2309 | public final void closeSubMenu() { |
fca67db0 KL |
2310 | assert (activeMenu != null); |
2311 | TMenu item = subMenus.get(subMenus.size() - 1); | |
2312 | assert (item != null); | |
2313 | item.setActive(false); | |
2314 | subMenus.remove(subMenus.size() - 1); | |
fca67db0 KL |
2315 | } |
2316 | ||
2317 | /** | |
2318 | * Switch to the next menu. | |
2319 | * | |
2320 | * @param forward if true, then switch to the next menu in the list, | |
2321 | * otherwise switch to the previous menu in the list | |
2322 | */ | |
928811d8 | 2323 | public final void switchMenu(final boolean forward) { |
fca67db0 KL |
2324 | assert (activeMenu != null); |
2325 | ||
2326 | for (TMenu menu: subMenus) { | |
2327 | menu.setActive(false); | |
2328 | } | |
2329 | subMenus.clear(); | |
2330 | ||
2331 | for (int i = 0; i < menus.size(); i++) { | |
2332 | if (activeMenu == menus.get(i)) { | |
2333 | if (forward) { | |
2334 | if (i < menus.size() - 1) { | |
2335 | i++; | |
2336 | } | |
2337 | } else { | |
2338 | if (i > 0) { | |
2339 | i--; | |
2340 | } | |
2341 | } | |
2342 | activeMenu.setActive(false); | |
2343 | activeMenu = menus.get(i); | |
2344 | activeMenu.setActive(true); | |
fca67db0 KL |
2345 | return; |
2346 | } | |
2347 | } | |
2348 | } | |
2349 | ||
928811d8 | 2350 | /** |
efb7af1f KL |
2351 | * Add a menu item to the global list. If it has a keyboard accelerator, |
2352 | * that will be added the global hash. | |
928811d8 | 2353 | * |
efb7af1f | 2354 | * @param item the menu item |
928811d8 | 2355 | */ |
efb7af1f KL |
2356 | public final void addMenuItem(final TMenuItem item) { |
2357 | menuItems.add(item); | |
2358 | ||
2359 | TKeypress key = item.getKey(); | |
2360 | if (key != null) { | |
2361 | synchronized (accelerators) { | |
2362 | assert (accelerators.get(key) == null); | |
2363 | accelerators.put(key.toLowerCase(), item); | |
2364 | } | |
2365 | } | |
2366 | } | |
2367 | ||
2368 | /** | |
2369 | * Disable one menu item. | |
2370 | * | |
2371 | * @param id the menu item ID | |
2372 | */ | |
2373 | public final void disableMenuItem(final int id) { | |
2374 | for (TMenuItem item: menuItems) { | |
2375 | if (item.getId() == id) { | |
2376 | item.setEnabled(false); | |
2377 | } | |
2378 | } | |
2379 | } | |
e826b451 | 2380 | |
efb7af1f KL |
2381 | /** |
2382 | * Disable the range of menu items with ID's between lower and upper, | |
2383 | * inclusive. | |
2384 | * | |
2385 | * @param lower the lowest menu item ID | |
2386 | * @param upper the highest menu item ID | |
2387 | */ | |
2388 | public final void disableMenuItems(final int lower, final int upper) { | |
2389 | for (TMenuItem item: menuItems) { | |
2390 | if ((item.getId() >= lower) && (item.getId() <= upper)) { | |
2391 | item.setEnabled(false); | |
2392 | } | |
2393 | } | |
2394 | } | |
2395 | ||
2396 | /** | |
2397 | * Enable one menu item. | |
2398 | * | |
2399 | * @param id the menu item ID | |
2400 | */ | |
2401 | public final void enableMenuItem(final int id) { | |
2402 | for (TMenuItem item: menuItems) { | |
2403 | if (item.getId() == id) { | |
2404 | item.setEnabled(true); | |
2405 | } | |
2406 | } | |
2407 | } | |
2408 | ||
2409 | /** | |
2410 | * Enable the range of menu items with ID's between lower and upper, | |
2411 | * inclusive. | |
2412 | * | |
2413 | * @param lower the lowest menu item ID | |
2414 | * @param upper the highest menu item ID | |
2415 | */ | |
2416 | public final void enableMenuItems(final int lower, final int upper) { | |
2417 | for (TMenuItem item: menuItems) { | |
2418 | if ((item.getId() >= lower) && (item.getId() <= upper)) { | |
2419 | item.setEnabled(true); | |
2420 | } | |
e826b451 | 2421 | } |
928811d8 KL |
2422 | } |
2423 | ||
2424 | /** | |
2425 | * Recompute menu x positions based on their title length. | |
2426 | */ | |
2427 | public final void recomputeMenuX() { | |
2428 | int x = 0; | |
2429 | for (TMenu menu: menus) { | |
2430 | menu.setX(x); | |
159f076d | 2431 | menu.setTitleX(x); |
928811d8 | 2432 | x += menu.getTitle().length() + 2; |
68c5cd6b KL |
2433 | |
2434 | // Don't let the menu window exceed the screen width | |
2435 | int rightEdge = menu.getX() + menu.getWidth(); | |
2436 | if (rightEdge > getScreen().getWidth()) { | |
2437 | menu.setX(getScreen().getWidth() - menu.getWidth()); | |
2438 | } | |
928811d8 KL |
2439 | } |
2440 | } | |
2441 | ||
b2d49e0f KL |
2442 | /** |
2443 | * Post an event to process. | |
2444 | * | |
2445 | * @param event new event to add to the queue | |
2446 | */ | |
2447 | public final void postEvent(final TInputEvent event) { | |
2448 | synchronized (this) { | |
2449 | synchronized (fillEventQueue) { | |
2450 | fillEventQueue.add(event); | |
2451 | } | |
2452 | if (debugThreads) { | |
2453 | System.err.println(System.currentTimeMillis() + " " + | |
2454 | Thread.currentThread() + " postEvent() wake up main"); | |
2455 | } | |
2456 | this.notify(); | |
2457 | } | |
2458 | } | |
2459 | ||
928811d8 KL |
2460 | /** |
2461 | * Post an event to process and turn off the menu. | |
2462 | * | |
2463 | * @param event new event to add to the queue | |
2464 | */ | |
5dfd1c11 | 2465 | public final void postMenuEvent(final TInputEvent event) { |
be72cb5c KL |
2466 | synchronized (this) { |
2467 | synchronized (fillEventQueue) { | |
2468 | fillEventQueue.add(event); | |
2469 | } | |
2470 | if (debugThreads) { | |
2471 | System.err.println(System.currentTimeMillis() + " " + | |
2472 | Thread.currentThread() + " postMenuEvent() wake up main"); | |
2473 | } | |
2474 | closeMenu(); | |
2475 | this.notify(); | |
8e688b92 | 2476 | } |
928811d8 KL |
2477 | } |
2478 | ||
2479 | /** | |
2480 | * Add a sub-menu to the list of open sub-menus. | |
2481 | * | |
2482 | * @param menu sub-menu | |
2483 | */ | |
2484 | public final void addSubMenu(final TMenu menu) { | |
2485 | subMenus.add(menu); | |
2486 | } | |
2487 | ||
8e688b92 KL |
2488 | /** |
2489 | * Convenience function to add a top-level menu. | |
2490 | * | |
2491 | * @param title menu title | |
2492 | * @return the new menu | |
2493 | */ | |
87a17f3c | 2494 | public final TMenu addMenu(final String title) { |
8e688b92 KL |
2495 | int x = 0; |
2496 | int y = 0; | |
2497 | TMenu menu = new TMenu(this, x, y, title); | |
2498 | menus.add(menu); | |
2499 | recomputeMenuX(); | |
2500 | return menu; | |
2501 | } | |
2502 | ||
2503 | /** | |
2504 | * Convenience function to add a default "File" menu. | |
2505 | * | |
2506 | * @return the new menu | |
2507 | */ | |
2508 | public final TMenu addFileMenu() { | |
339652cc | 2509 | TMenu fileMenu = addMenu(i18n.getString("fileMenuTitle")); |
8e688b92 KL |
2510 | fileMenu.addDefaultItem(TMenu.MID_OPEN_FILE); |
2511 | fileMenu.addSeparator(); | |
2512 | fileMenu.addDefaultItem(TMenu.MID_SHELL); | |
2513 | fileMenu.addDefaultItem(TMenu.MID_EXIT); | |
339652cc KL |
2514 | TStatusBar statusBar = fileMenu.newStatusBar(i18n. |
2515 | getString("fileMenuStatus")); | |
2516 | statusBar.addShortcutKeypress(kbF1, cmHelp, i18n.getString("Help")); | |
8e688b92 KL |
2517 | return fileMenu; |
2518 | } | |
2519 | ||
2520 | /** | |
2521 | * Convenience function to add a default "Edit" menu. | |
2522 | * | |
2523 | * @return the new menu | |
2524 | */ | |
2525 | public final TMenu addEditMenu() { | |
339652cc | 2526 | TMenu editMenu = addMenu(i18n.getString("editMenuTitle")); |
8e688b92 KL |
2527 | editMenu.addDefaultItem(TMenu.MID_CUT); |
2528 | editMenu.addDefaultItem(TMenu.MID_COPY); | |
2529 | editMenu.addDefaultItem(TMenu.MID_PASTE); | |
2530 | editMenu.addDefaultItem(TMenu.MID_CLEAR); | |
339652cc KL |
2531 | TStatusBar statusBar = editMenu.newStatusBar(i18n. |
2532 | getString("editMenuStatus")); | |
2533 | statusBar.addShortcutKeypress(kbF1, cmHelp, i18n.getString("Help")); | |
8e688b92 KL |
2534 | return editMenu; |
2535 | } | |
2536 | ||
2537 | /** | |
2538 | * Convenience function to add a default "Window" menu. | |
2539 | * | |
2540 | * @return the new menu | |
2541 | */ | |
c6940ed9 | 2542 | public final TMenu addWindowMenu() { |
339652cc | 2543 | TMenu windowMenu = addMenu(i18n.getString("windowMenuTitle")); |
8e688b92 KL |
2544 | windowMenu.addDefaultItem(TMenu.MID_TILE); |
2545 | windowMenu.addDefaultItem(TMenu.MID_CASCADE); | |
2546 | windowMenu.addDefaultItem(TMenu.MID_CLOSE_ALL); | |
2547 | windowMenu.addSeparator(); | |
2548 | windowMenu.addDefaultItem(TMenu.MID_WINDOW_MOVE); | |
2549 | windowMenu.addDefaultItem(TMenu.MID_WINDOW_ZOOM); | |
2550 | windowMenu.addDefaultItem(TMenu.MID_WINDOW_NEXT); | |
2551 | windowMenu.addDefaultItem(TMenu.MID_WINDOW_PREVIOUS); | |
2552 | windowMenu.addDefaultItem(TMenu.MID_WINDOW_CLOSE); | |
339652cc KL |
2553 | TStatusBar statusBar = windowMenu.newStatusBar(i18n. |
2554 | getString("windowMenuStatus")); | |
2555 | statusBar.addShortcutKeypress(kbF1, cmHelp, i18n.getString("Help")); | |
8e688b92 KL |
2556 | return windowMenu; |
2557 | } | |
2558 | ||
55d2b2c2 KL |
2559 | /** |
2560 | * Convenience function to add a default "Help" menu. | |
2561 | * | |
2562 | * @return the new menu | |
2563 | */ | |
2564 | public final TMenu addHelpMenu() { | |
339652cc | 2565 | TMenu helpMenu = addMenu(i18n.getString("helpMenuTitle")); |
55d2b2c2 KL |
2566 | helpMenu.addDefaultItem(TMenu.MID_HELP_CONTENTS); |
2567 | helpMenu.addDefaultItem(TMenu.MID_HELP_INDEX); | |
2568 | helpMenu.addDefaultItem(TMenu.MID_HELP_SEARCH); | |
2569 | helpMenu.addDefaultItem(TMenu.MID_HELP_PREVIOUS); | |
2570 | helpMenu.addDefaultItem(TMenu.MID_HELP_HELP); | |
2571 | helpMenu.addDefaultItem(TMenu.MID_HELP_ACTIVE_FILE); | |
2572 | helpMenu.addSeparator(); | |
2573 | helpMenu.addDefaultItem(TMenu.MID_ABOUT); | |
339652cc KL |
2574 | TStatusBar statusBar = helpMenu.newStatusBar(i18n. |
2575 | getString("helpMenuStatus")); | |
2576 | statusBar.addShortcutKeypress(kbF1, cmHelp, i18n.getString("Help")); | |
55d2b2c2 KL |
2577 | return helpMenu; |
2578 | } | |
2579 | ||
2ce6dab2 KL |
2580 | // ------------------------------------------------------------------------ |
2581 | // Event handlers --------------------------------------------------------- | |
2582 | // ------------------------------------------------------------------------ | |
2583 | ||
8e688b92 | 2584 | /** |
2ce6dab2 KL |
2585 | * Method that TApplication subclasses can override to handle menu or |
2586 | * posted command events. | |
2587 | * | |
2588 | * @param command command event | |
2589 | * @return if true, this event was consumed | |
8e688b92 | 2590 | */ |
2ce6dab2 KL |
2591 | protected boolean onCommand(final TCommandEvent command) { |
2592 | // Default: handle cmExit | |
2593 | if (command.equals(cmExit)) { | |
339652cc KL |
2594 | if (messageBox(i18n.getString("exitDialogTitle"), |
2595 | i18n.getString("exitDialogText"), | |
2ce6dab2 | 2596 | TMessageBox.Type.YESNO).getResult() == TMessageBox.Result.YES) { |
be72cb5c | 2597 | exit(); |
2ce6dab2 KL |
2598 | } |
2599 | return true; | |
8e688b92 | 2600 | } |
2ce6dab2 KL |
2601 | |
2602 | if (command.equals(cmShell)) { | |
2603 | openTerminal(0, 0, TWindow.RESIZABLE); | |
2604 | return true; | |
2605 | } | |
2606 | ||
2607 | if (command.equals(cmTile)) { | |
2608 | tileWindows(); | |
2609 | return true; | |
2610 | } | |
2611 | if (command.equals(cmCascade)) { | |
2612 | cascadeWindows(); | |
2613 | return true; | |
2614 | } | |
2615 | if (command.equals(cmCloseAll)) { | |
2616 | closeAllWindows(); | |
2617 | return true; | |
8e688b92 | 2618 | } |
2ce6dab2 | 2619 | |
71a389c9 KL |
2620 | if (command.equals(cmMenu)) { |
2621 | if (!modalWindowActive() && (activeMenu == null)) { | |
2622 | if (menus.size() > 0) { | |
2623 | menus.get(0).setActive(true); | |
2624 | activeMenu = menus.get(0); | |
2625 | return true; | |
2626 | } | |
2627 | } | |
2628 | } | |
2629 | ||
2ce6dab2 | 2630 | return false; |
8e688b92 KL |
2631 | } |
2632 | ||
2633 | /** | |
2ce6dab2 KL |
2634 | * Method that TApplication subclasses can override to handle menu |
2635 | * events. | |
2636 | * | |
2637 | * @param menu menu event | |
2638 | * @return if true, this event was consumed | |
8e688b92 | 2639 | */ |
2ce6dab2 KL |
2640 | protected boolean onMenu(final TMenuEvent menu) { |
2641 | ||
2642 | // Default: handle MID_EXIT | |
2643 | if (menu.getId() == TMenu.MID_EXIT) { | |
339652cc KL |
2644 | if (messageBox(i18n.getString("exitDialogTitle"), |
2645 | i18n.getString("exitDialogText"), | |
2ce6dab2 | 2646 | TMessageBox.Type.YESNO).getResult() == TMessageBox.Result.YES) { |
be72cb5c | 2647 | exit(); |
8e688b92 | 2648 | } |
2ce6dab2 KL |
2649 | return true; |
2650 | } | |
bb35d919 | 2651 | |
2ce6dab2 KL |
2652 | if (menu.getId() == TMenu.MID_SHELL) { |
2653 | openTerminal(0, 0, TWindow.RESIZABLE); | |
2654 | return true; | |
2655 | } | |
8e688b92 | 2656 | |
2ce6dab2 KL |
2657 | if (menu.getId() == TMenu.MID_TILE) { |
2658 | tileWindows(); | |
2659 | return true; | |
2660 | } | |
2661 | if (menu.getId() == TMenu.MID_CASCADE) { | |
2662 | cascadeWindows(); | |
2663 | return true; | |
2664 | } | |
2665 | if (menu.getId() == TMenu.MID_CLOSE_ALL) { | |
2666 | closeAllWindows(); | |
2667 | return true; | |
2668 | } | |
2669 | if (menu.getId() == TMenu.MID_ABOUT) { | |
2670 | showAboutDialog(); | |
2671 | return true; | |
2672 | } | |
1c19fdea KL |
2673 | if (menu.getId() == TMenu.MID_REPAINT) { |
2674 | doRepaint(); | |
2675 | return true; | |
2676 | } | |
2ce6dab2 KL |
2677 | return false; |
2678 | } | |
2679 | ||
2680 | /** | |
2681 | * Method that TApplication subclasses can override to handle keystrokes. | |
2682 | * | |
2683 | * @param keypress keystroke event | |
2684 | * @return if true, this event was consumed | |
2685 | */ | |
2686 | protected boolean onKeypress(final TKeypressEvent keypress) { | |
2687 | // Default: only menu shortcuts | |
2688 | ||
2689 | // Process Alt-F, Alt-E, etc. menu shortcut keys | |
2690 | if (!keypress.getKey().isFnKey() | |
2691 | && keypress.getKey().isAlt() | |
2692 | && !keypress.getKey().isCtrl() | |
2693 | && (activeMenu == null) | |
2694 | && !modalWindowActive() | |
2695 | ) { | |
2696 | ||
2697 | assert (subMenus.size() == 0); | |
2698 | ||
2699 | for (TMenu menu: menus) { | |
2700 | if (Character.toLowerCase(menu.getMnemonic().getShortcut()) | |
2701 | == Character.toLowerCase(keypress.getKey().getChar()) | |
2702 | ) { | |
2703 | activeMenu = menu; | |
2704 | menu.setActive(true); | |
2705 | return true; | |
bb35d919 | 2706 | } |
8e688b92 KL |
2707 | } |
2708 | } | |
2ce6dab2 KL |
2709 | |
2710 | return false; | |
8e688b92 KL |
2711 | } |
2712 | ||
2ce6dab2 KL |
2713 | // ------------------------------------------------------------------------ |
2714 | // TTimer management ------------------------------------------------------ | |
2715 | // ------------------------------------------------------------------------ | |
2716 | ||
8e688b92 | 2717 | /** |
2ce6dab2 KL |
2718 | * Get the amount of time I can sleep before missing a Timer tick. |
2719 | * | |
2720 | * @param timeout = initial (maximum) timeout in millis | |
2721 | * @return number of milliseconds between now and the next timer event | |
8e688b92 | 2722 | */ |
2ce6dab2 KL |
2723 | private long getSleepTime(final long timeout) { |
2724 | Date now = new Date(); | |
2725 | long nowTime = now.getTime(); | |
2726 | long sleepTime = timeout; | |
2ce6dab2 | 2727 | |
be72cb5c KL |
2728 | synchronized (timers) { |
2729 | for (TTimer timer: timers) { | |
2730 | long nextTickTime = timer.getNextTick().getTime(); | |
2731 | if (nextTickTime < nowTime) { | |
2732 | return 0; | |
2733 | } | |
2734 | ||
2735 | long timeDifference = nextTickTime - nowTime; | |
2736 | if (timeDifference < sleepTime) { | |
2737 | sleepTime = timeDifference; | |
2738 | } | |
8e688b92 KL |
2739 | } |
2740 | } | |
be72cb5c | 2741 | |
2ce6dab2 KL |
2742 | assert (sleepTime >= 0); |
2743 | assert (sleepTime <= timeout); | |
2744 | return sleepTime; | |
8e688b92 KL |
2745 | } |
2746 | ||
d502a0e9 KL |
2747 | /** |
2748 | * Convenience function to add a timer. | |
2749 | * | |
2750 | * @param duration number of milliseconds to wait between ticks | |
2751 | * @param recurring if true, re-schedule this timer after every tick | |
2752 | * @param action function to call when button is pressed | |
c6940ed9 | 2753 | * @return the timer |
d502a0e9 KL |
2754 | */ |
2755 | public final TTimer addTimer(final long duration, final boolean recurring, | |
2756 | final TAction action) { | |
2757 | ||
2758 | TTimer timer = new TTimer(duration, recurring, action); | |
2759 | synchronized (timers) { | |
2760 | timers.add(timer); | |
2761 | } | |
2762 | return timer; | |
2763 | } | |
2764 | ||
2765 | /** | |
2766 | * Convenience function to remove a timer. | |
2767 | * | |
2768 | * @param timer timer to remove | |
2769 | */ | |
2770 | public final void removeTimer(final TTimer timer) { | |
2771 | synchronized (timers) { | |
2772 | timers.remove(timer); | |
2773 | } | |
2774 | } | |
2775 | ||
2ce6dab2 KL |
2776 | // ------------------------------------------------------------------------ |
2777 | // Other TWindow constructors --------------------------------------------- | |
2778 | // ------------------------------------------------------------------------ | |
2779 | ||
c6940ed9 KL |
2780 | /** |
2781 | * Convenience function to spawn a message box. | |
2782 | * | |
2783 | * @param title window title, will be centered along the top border | |
2784 | * @param caption message to display. Use embedded newlines to get a | |
2785 | * multi-line box. | |
2786 | * @return the new message box | |
2787 | */ | |
2788 | public final TMessageBox messageBox(final String title, | |
2789 | final String caption) { | |
2790 | ||
2791 | return new TMessageBox(this, title, caption, TMessageBox.Type.OK); | |
2792 | } | |
2793 | ||
2794 | /** | |
2795 | * Convenience function to spawn a message box. | |
2796 | * | |
2797 | * @param title window title, will be centered along the top border | |
2798 | * @param caption message to display. Use embedded newlines to get a | |
2799 | * multi-line box. | |
2800 | * @param type one of the TMessageBox.Type constants. Default is | |
2801 | * Type.OK. | |
2802 | * @return the new message box | |
2803 | */ | |
2804 | public final TMessageBox messageBox(final String title, | |
2805 | final String caption, final TMessageBox.Type type) { | |
2806 | ||
2807 | return new TMessageBox(this, title, caption, type); | |
2808 | } | |
2809 | ||
2810 | /** | |
2811 | * Convenience function to spawn an input box. | |
2812 | * | |
2813 | * @param title window title, will be centered along the top border | |
2814 | * @param caption message to display. Use embedded newlines to get a | |
2815 | * multi-line box. | |
2816 | * @return the new input box | |
2817 | */ | |
2818 | public final TInputBox inputBox(final String title, final String caption) { | |
2819 | ||
2820 | return new TInputBox(this, title, caption); | |
2821 | } | |
2822 | ||
2823 | /** | |
2824 | * Convenience function to spawn an input box. | |
2825 | * | |
2826 | * @param title window title, will be centered along the top border | |
2827 | * @param caption message to display. Use embedded newlines to get a | |
2828 | * multi-line box. | |
2829 | * @param text initial text to seed the field with | |
2830 | * @return the new input box | |
2831 | */ | |
2832 | public final TInputBox inputBox(final String title, final String caption, | |
2833 | final String text) { | |
2834 | ||
2835 | return new TInputBox(this, title, caption, text); | |
2836 | } | |
1ac2ccb1 | 2837 | |
34a42e78 KL |
2838 | /** |
2839 | * Convenience function to open a terminal window. | |
2840 | * | |
2841 | * @param x column relative to parent | |
2842 | * @param y row relative to parent | |
2843 | * @return the terminal new window | |
2844 | */ | |
2845 | public final TTerminalWindow openTerminal(final int x, final int y) { | |
2846 | return openTerminal(x, y, TWindow.RESIZABLE); | |
2847 | } | |
2848 | ||
2849 | /** | |
2850 | * Convenience function to open a terminal window. | |
2851 | * | |
2852 | * @param x column relative to parent | |
2853 | * @param y row relative to parent | |
2854 | * @param flags mask of CENTERED, MODAL, or RESIZABLE | |
2855 | * @return the terminal new window | |
2856 | */ | |
2857 | public final TTerminalWindow openTerminal(final int x, final int y, | |
2858 | final int flags) { | |
2859 | ||
2860 | return new TTerminalWindow(this, x, y, flags); | |
2861 | } | |
2862 | ||
6f8ff91a KL |
2863 | /** |
2864 | * Convenience function to open a terminal window and execute a custom | |
2865 | * command line inside it. | |
2866 | * | |
2867 | * @param x column relative to parent | |
2868 | * @param y row relative to parent | |
2869 | * @param commandLine the command line to execute | |
2870 | * @return the terminal new window | |
2871 | */ | |
2872 | public final TTerminalWindow openTerminal(final int x, final int y, | |
2873 | final String commandLine) { | |
2874 | ||
2875 | return openTerminal(x, y, TWindow.RESIZABLE, commandLine); | |
2876 | } | |
2877 | ||
a0d734e6 KL |
2878 | /** |
2879 | * Convenience function to open a terminal window and execute a custom | |
2880 | * command line inside it. | |
2881 | * | |
2882 | * @param x column relative to parent | |
2883 | * @param y row relative to parent | |
2884 | * @param flags mask of CENTERED, MODAL, or RESIZABLE | |
2885 | * @param command the command line to execute | |
2886 | * @return the terminal new window | |
2887 | */ | |
2888 | public final TTerminalWindow openTerminal(final int x, final int y, | |
2889 | final int flags, final String [] command) { | |
2890 | ||
2891 | return new TTerminalWindow(this, x, y, flags, command); | |
2892 | } | |
2893 | ||
6f8ff91a KL |
2894 | /** |
2895 | * Convenience function to open a terminal window and execute a custom | |
2896 | * command line inside it. | |
2897 | * | |
2898 | * @param x column relative to parent | |
2899 | * @param y row relative to parent | |
2900 | * @param flags mask of CENTERED, MODAL, or RESIZABLE | |
2901 | * @param commandLine the command line to execute | |
2902 | * @return the terminal new window | |
2903 | */ | |
2904 | public final TTerminalWindow openTerminal(final int x, final int y, | |
2905 | final int flags, final String commandLine) { | |
2906 | ||
a0d734e6 | 2907 | return new TTerminalWindow(this, x, y, flags, commandLine.split("\\s")); |
6f8ff91a KL |
2908 | } |
2909 | ||
0d47c546 KL |
2910 | /** |
2911 | * Convenience function to spawn an file open box. | |
2912 | * | |
2913 | * @param path path of selected file | |
2914 | * @return the result of the new file open box | |
329fd62e | 2915 | * @throws IOException if java.io operation throws |
0d47c546 KL |
2916 | */ |
2917 | public final String fileOpenBox(final String path) throws IOException { | |
2918 | ||
2919 | TFileOpenBox box = new TFileOpenBox(this, path, TFileOpenBox.Type.OPEN); | |
2920 | return box.getFilename(); | |
2921 | } | |
2922 | ||
2923 | /** | |
2924 | * Convenience function to spawn an file open box. | |
2925 | * | |
2926 | * @param path path of selected file | |
2927 | * @param type one of the Type constants | |
2928 | * @return the result of the new file open box | |
329fd62e | 2929 | * @throws IOException if java.io operation throws |
0d47c546 KL |
2930 | */ |
2931 | public final String fileOpenBox(final String path, | |
2932 | final TFileOpenBox.Type type) throws IOException { | |
2933 | ||
2934 | TFileOpenBox box = new TFileOpenBox(this, path, type); | |
2935 | return box.getFilename(); | |
2936 | } | |
2937 | ||
92453213 KL |
2938 | /** |
2939 | * Convenience function to create a new window and make it active. | |
2940 | * Window will be located at (0, 0). | |
2941 | * | |
2942 | * @param title window title, will be centered along the top border | |
2943 | * @param width width of window | |
2944 | * @param height height of window | |
2945 | */ | |
2946 | public final TWindow addWindow(final String title, final int width, | |
2947 | final int height) { | |
2948 | ||
2949 | TWindow window = new TWindow(this, title, 0, 0, width, height); | |
2950 | return window; | |
2951 | } | |
1978ad50 | 2952 | |
92453213 KL |
2953 | /** |
2954 | * Convenience function to create a new window and make it active. | |
2955 | * Window will be located at (0, 0). | |
2956 | * | |
2957 | * @param title window title, will be centered along the top border | |
2958 | * @param width width of window | |
2959 | * @param height height of window | |
2960 | * @param flags bitmask of RESIZABLE, CENTERED, or MODAL | |
2961 | */ | |
2962 | public final TWindow addWindow(final String title, | |
2963 | final int width, final int height, final int flags) { | |
2964 | ||
2965 | TWindow window = new TWindow(this, title, 0, 0, width, height, flags); | |
2966 | return window; | |
2967 | } | |
2968 | ||
2969 | /** | |
2970 | * Convenience function to create a new window and make it active. | |
2971 | * | |
2972 | * @param title window title, will be centered along the top border | |
2973 | * @param x column relative to parent | |
2974 | * @param y row relative to parent | |
2975 | * @param width width of window | |
2976 | * @param height height of window | |
2977 | */ | |
2978 | public final TWindow addWindow(final String title, | |
2979 | final int x, final int y, final int width, final int height) { | |
2980 | ||
2981 | TWindow window = new TWindow(this, title, x, y, width, height); | |
2982 | return window; | |
2983 | } | |
2984 | ||
2985 | /** | |
2986 | * Convenience function to create a new window and make it active. | |
2987 | * | |
92453213 KL |
2988 | * @param title window title, will be centered along the top border |
2989 | * @param x column relative to parent | |
2990 | * @param y row relative to parent | |
2991 | * @param width width of window | |
2992 | * @param height height of window | |
2993 | * @param flags mask of RESIZABLE, CENTERED, or MODAL | |
2994 | */ | |
2995 | public final TWindow addWindow(final String title, | |
2996 | final int x, final int y, final int width, final int height, | |
2997 | final int flags) { | |
2998 | ||
2999 | TWindow window = new TWindow(this, title, x, y, width, height, flags); | |
3000 | return window; | |
3001 | } | |
3002 | ||
1978ad50 KL |
3003 | /** |
3004 | * Convenience function to open a file in an editor window and make it | |
3005 | * active. | |
3006 | * | |
3007 | * @param file the file to open | |
3008 | * @throws IOException if a java.io operation throws | |
3009 | */ | |
3010 | public final TEditorWindow addEditor(final File file) throws IOException { | |
3011 | ||
3012 | TEditorWindow editor = new TEditorWindow(this, file); | |
3013 | return editor; | |
3014 | } | |
3015 | ||
7d4115a5 | 3016 | } |