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