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