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 | /** | |
e8a11f98 | 583 | * Get a (shallow) copy of the window list. |
92453213 KL |
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 | |
bd8d51fa | 1074 | // Put into the main queue |
ef368bd0 | 1075 | drainEventQueue.add(event); |
4328bb42 KL |
1076 | } |
1077 | ||
a06459bd KL |
1078 | /** |
1079 | * Dispatch one event to the appropriate widget or application-level | |
fca67db0 KL |
1080 | * event handler. This is the primary event handler, it has the normal |
1081 | * application-wide event handling. | |
a06459bd KL |
1082 | * |
1083 | * @param event the input event to consume | |
fca67db0 | 1084 | * @see #secondaryHandleEvent(TInputEvent event) |
a06459bd | 1085 | */ |
fca67db0 KL |
1086 | private void primaryHandleEvent(final TInputEvent event) { |
1087 | ||
a83fea2b KL |
1088 | if (debugEvents) { |
1089 | System.err.printf("Handle event: %s\n", event); | |
1090 | } | |
fca67db0 KL |
1091 | |
1092 | // Special application-wide events ----------------------------------- | |
1093 | ||
1094 | // Peek at the mouse position | |
1095 | if (event instanceof TMouseEvent) { | |
e8a11f98 KL |
1096 | TMouseEvent mouse = (TMouseEvent) event; |
1097 | if ((mouseX != mouse.getX()) || (mouseY != mouse.getY())) { | |
1098 | oldMouseX = mouseX; | |
1099 | oldMouseY = mouseY; | |
1100 | mouseX = mouse.getX(); | |
1101 | mouseY = mouse.getY(); | |
1102 | } | |
1103 | ||
fca67db0 KL |
1104 | // See if we need to switch focus to another window or the menu |
1105 | checkSwitchFocus((TMouseEvent) event); | |
1106 | } | |
1107 | ||
1108 | // Handle menu events | |
1109 | if ((activeMenu != null) && !(event instanceof TCommandEvent)) { | |
1110 | TMenu menu = activeMenu; | |
1111 | ||
1112 | if (event instanceof TMouseEvent) { | |
1113 | TMouseEvent mouse = (TMouseEvent) event; | |
1114 | ||
1115 | while (subMenus.size() > 0) { | |
1116 | TMenu subMenu = subMenus.get(subMenus.size() - 1); | |
1117 | if (subMenu.mouseWouldHit(mouse)) { | |
1118 | break; | |
1119 | } | |
1120 | if ((mouse.getType() == TMouseEvent.Type.MOUSE_MOTION) | |
7c870d89 KL |
1121 | && (!mouse.isMouse1()) |
1122 | && (!mouse.isMouse2()) | |
1123 | && (!mouse.isMouse3()) | |
1124 | && (!mouse.isMouseWheelUp()) | |
1125 | && (!mouse.isMouseWheelDown()) | |
fca67db0 KL |
1126 | ) { |
1127 | break; | |
1128 | } | |
1129 | // We navigated away from a sub-menu, so close it | |
1130 | closeSubMenu(); | |
1131 | } | |
1132 | ||
1133 | // Convert the mouse relative x/y to menu coordinates | |
1134 | assert (mouse.getX() == mouse.getAbsoluteX()); | |
1135 | assert (mouse.getY() == mouse.getAbsoluteY()); | |
1136 | if (subMenus.size() > 0) { | |
1137 | menu = subMenus.get(subMenus.size() - 1); | |
1138 | } | |
1139 | mouse.setX(mouse.getX() - menu.getX()); | |
1140 | mouse.setY(mouse.getY() - menu.getY()); | |
1141 | } | |
1142 | menu.handleEvent(event); | |
1143 | return; | |
1144 | } | |
a06459bd | 1145 | |
fca67db0 KL |
1146 | if (event instanceof TKeypressEvent) { |
1147 | TKeypressEvent keypress = (TKeypressEvent) event; | |
e826b451 | 1148 | |
5dfd1c11 KL |
1149 | // See if this key matches an accelerator, and is not being |
1150 | // shortcutted by the active window, and if so dispatch the menu | |
1151 | // event. | |
1152 | boolean windowWillShortcut = false; | |
92453213 KL |
1153 | if (activeWindow != null) { |
1154 | assert (activeWindow.isShown()); | |
1155 | if (activeWindow.isShortcutKeypress(keypress.getKey())) { | |
1156 | // We do not process this key, it will be passed to the | |
1157 | // window instead. | |
1158 | windowWillShortcut = true; | |
5dfd1c11 | 1159 | } |
e826b451 | 1160 | } |
5dfd1c11 | 1161 | |
2ce6dab2 | 1162 | if (!windowWillShortcut && !modalWindowActive()) { |
5dfd1c11 KL |
1163 | TKeypress keypressLowercase = keypress.getKey().toLowerCase(); |
1164 | TMenuItem item = null; | |
1165 | synchronized (accelerators) { | |
1166 | item = accelerators.get(keypressLowercase); | |
1167 | } | |
1168 | if (item != null) { | |
1169 | if (item.isEnabled()) { | |
1170 | // Let the menu item dispatch | |
1171 | item.dispatch(); | |
1172 | return; | |
1173 | } | |
fca67db0 | 1174 | } |
5dfd1c11 | 1175 | |
2ce6dab2 KL |
1176 | // Handle the keypress |
1177 | if (onKeypress(keypress)) { | |
1178 | return; | |
1179 | } | |
bd8d51fa | 1180 | } |
fca67db0 | 1181 | } |
a06459bd | 1182 | |
fca67db0 KL |
1183 | if (event instanceof TCommandEvent) { |
1184 | if (onCommand((TCommandEvent) event)) { | |
1185 | return; | |
1186 | } | |
1187 | } | |
1188 | ||
1189 | if (event instanceof TMenuEvent) { | |
1190 | if (onMenu((TMenuEvent) event)) { | |
1191 | return; | |
1192 | } | |
1193 | } | |
1194 | ||
1195 | // Dispatch events to the active window ------------------------------- | |
0ee88b6d | 1196 | boolean dispatchToDesktop = true; |
92453213 KL |
1197 | TWindow window = activeWindow; |
1198 | if (window != null) { | |
1199 | assert (window.isActive()); | |
1200 | assert (window.isShown()); | |
1201 | if (event instanceof TMouseEvent) { | |
1202 | TMouseEvent mouse = (TMouseEvent) event; | |
1203 | // Convert the mouse relative x/y to window coordinates | |
1204 | assert (mouse.getX() == mouse.getAbsoluteX()); | |
1205 | assert (mouse.getY() == mouse.getAbsoluteY()); | |
1206 | mouse.setX(mouse.getX() - window.getX()); | |
1207 | mouse.setY(mouse.getY() - window.getY()); | |
1208 | ||
1209 | if (window.mouseWouldHit(mouse)) { | |
0ee88b6d | 1210 | dispatchToDesktop = false; |
fca67db0 | 1211 | } |
92453213 KL |
1212 | } else if (event instanceof TKeypressEvent) { |
1213 | dispatchToDesktop = false; | |
1214 | } | |
0ee88b6d | 1215 | |
92453213 KL |
1216 | if (debugEvents) { |
1217 | System.err.printf("TApplication dispatch event: %s\n", | |
1218 | event); | |
fca67db0 | 1219 | } |
92453213 | 1220 | window.handleEvent(event); |
fca67db0 | 1221 | } |
0ee88b6d KL |
1222 | if (dispatchToDesktop) { |
1223 | // This event is fair game for the desktop to process. | |
1224 | if (desktop != null) { | |
1225 | desktop.handleEvent(event); | |
1226 | } | |
1227 | } | |
fca67db0 | 1228 | } |
0ee88b6d | 1229 | |
fca67db0 KL |
1230 | /** |
1231 | * Dispatch one event to the appropriate widget or application-level | |
1232 | * event handler. This is the secondary event handler used by certain | |
1233 | * special dialogs (currently TMessageBox and TFileOpenBox). | |
1234 | * | |
1235 | * @param event the input event to consume | |
1236 | * @see #primaryHandleEvent(TInputEvent event) | |
1237 | */ | |
1238 | private void secondaryHandleEvent(final TInputEvent event) { | |
e8a11f98 KL |
1239 | // Peek at the mouse position |
1240 | if (event instanceof TMouseEvent) { | |
1241 | TMouseEvent mouse = (TMouseEvent) event; | |
1242 | if ((mouseX != mouse.getX()) || (mouseY != mouse.getY())) { | |
1243 | oldMouseX = mouseX; | |
1244 | oldMouseY = mouseY; | |
1245 | mouseX = mouse.getX(); | |
1246 | mouseY = mouse.getY(); | |
1247 | } | |
1248 | } | |
1249 | ||
c6940ed9 KL |
1250 | secondaryEventReceiver.handleEvent(event); |
1251 | } | |
1252 | ||
1253 | /** | |
1254 | * Enable a widget to override the primary event thread. | |
1255 | * | |
1256 | * @param widget widget that will receive events | |
1257 | */ | |
1258 | public final void enableSecondaryEventReceiver(final TWidget widget) { | |
1259 | assert (secondaryEventReceiver == null); | |
1260 | assert (secondaryEventHandler == null); | |
a043164f KL |
1261 | assert ((widget instanceof TMessageBox) |
1262 | || (widget instanceof TFileOpenBox)); | |
c6940ed9 KL |
1263 | secondaryEventReceiver = widget; |
1264 | secondaryEventHandler = new WidgetEventHandler(this, false); | |
1265 | (new Thread(secondaryEventHandler)).start(); | |
c6940ed9 KL |
1266 | } |
1267 | ||
1268 | /** | |
1269 | * Yield to the secondary thread. | |
1270 | */ | |
1271 | public final void yield() { | |
1272 | assert (secondaryEventReceiver != null); | |
99144c71 KL |
1273 | // This is where we handoff the event handler lock from the primary |
1274 | // to secondary thread. We unlock here, and in a future loop the | |
1275 | // secondary thread locks again. When it gives up, we have the | |
1276 | // single lock back. | |
1277 | boolean oldLock = unlockHandleEvent(); | |
329fd62e | 1278 | assert (oldLock); |
99144c71 | 1279 | |
c6940ed9 | 1280 | while (secondaryEventReceiver != null) { |
92554d64 | 1281 | synchronized (primaryEventHandler) { |
c6940ed9 | 1282 | try { |
92554d64 | 1283 | primaryEventHandler.wait(); |
c6940ed9 KL |
1284 | } catch (InterruptedException e) { |
1285 | // SQUASH | |
1286 | } | |
1287 | } | |
1288 | } | |
a06459bd KL |
1289 | } |
1290 | ||
4328bb42 KL |
1291 | /** |
1292 | * Do stuff when there is no user input. | |
1293 | */ | |
1294 | private void doIdle() { | |
99144c71 KL |
1295 | if (debugThreads) { |
1296 | System.err.printf("doIdle()\n"); | |
1297 | } | |
1298 | ||
7b5261bc | 1299 | // Now run any timers that have timed out |
d502a0e9 KL |
1300 | Date now = new Date(); |
1301 | List<TTimer> keepTimers = new LinkedList<TTimer>(); | |
1302 | for (TTimer timer: timers) { | |
92554d64 | 1303 | if (timer.getNextTick().getTime() <= now.getTime()) { |
d502a0e9 | 1304 | timer.tick(); |
c6940ed9 | 1305 | if (timer.recurring) { |
d502a0e9 | 1306 | keepTimers.add(timer); |
7b5261bc KL |
1307 | } |
1308 | } else { | |
d502a0e9 | 1309 | keepTimers.add(timer); |
7b5261bc KL |
1310 | } |
1311 | } | |
1312 | timers = keepTimers; | |
1313 | ||
1314 | // Call onIdle's | |
d502a0e9 KL |
1315 | for (TWindow window: windows) { |
1316 | window.onIdle(); | |
7b5261bc | 1317 | } |
92453213 KL |
1318 | if (desktop != null) { |
1319 | desktop.onIdle(); | |
1320 | } | |
4328bb42 | 1321 | } |
7d4115a5 | 1322 | |
2ce6dab2 KL |
1323 | // ------------------------------------------------------------------------ |
1324 | // TWindow management ----------------------------------------------------- | |
1325 | // ------------------------------------------------------------------------ | |
4328bb42 | 1326 | |
92453213 KL |
1327 | /** |
1328 | * Return the total number of windows. | |
1329 | * | |
1330 | * @return the total number of windows | |
1331 | */ | |
1332 | public final int windowCount() { | |
1333 | return windows.size(); | |
1334 | } | |
1335 | ||
1336 | /** | |
8c236a98 | 1337 | * Return the number of windows that are showing. |
92453213 | 1338 | * |
8c236a98 | 1339 | * @return the number of windows that are showing on screen |
92453213 KL |
1340 | */ |
1341 | public final int shownWindowCount() { | |
1342 | int n = 0; | |
1343 | for (TWindow w: windows) { | |
1344 | if (w.isShown()) { | |
1345 | n++; | |
1346 | } | |
1347 | } | |
1348 | return n; | |
1349 | } | |
1350 | ||
8c236a98 KL |
1351 | /** |
1352 | * Return the number of windows that are hidden. | |
1353 | * | |
1354 | * @return the number of windows that are hidden | |
1355 | */ | |
1356 | public final int hiddenWindowCount() { | |
1357 | int n = 0; | |
1358 | for (TWindow w: windows) { | |
1359 | if (w.isHidden()) { | |
1360 | n++; | |
1361 | } | |
1362 | } | |
1363 | return n; | |
1364 | } | |
1365 | ||
92453213 KL |
1366 | /** |
1367 | * Check if a window instance is in this application's window list. | |
1368 | * | |
1369 | * @param window window to look for | |
1370 | * @return true if this window is in the list | |
1371 | */ | |
1372 | public final boolean hasWindow(final TWindow window) { | |
1373 | if (windows.size() == 0) { | |
1374 | return false; | |
1375 | } | |
1376 | for (TWindow w: windows) { | |
1377 | if (w == window) { | |
8c236a98 | 1378 | assert (window.getApplication() == this); |
92453213 KL |
1379 | return true; |
1380 | } | |
1381 | } | |
1382 | return false; | |
1383 | } | |
1384 | ||
1385 | /** | |
1386 | * Activate a window: bring it to the top and have it receive events. | |
1387 | * | |
1388 | * @param window the window to become the new active window | |
1389 | */ | |
1390 | public void activateWindow(final TWindow window) { | |
1391 | if (hasWindow(window) == false) { | |
1392 | /* | |
1393 | * Someone has a handle to a window I don't have. Ignore this | |
1394 | * request. | |
1395 | */ | |
1396 | return; | |
1397 | } | |
1398 | ||
fe0770f9 KL |
1399 | // Whatever window might be moving/dragging, stop it now. |
1400 | for (TWindow w: windows) { | |
1401 | if (w.inMovements()) { | |
1402 | w.stopMovements(); | |
1403 | } | |
1404 | } | |
1405 | ||
92453213 KL |
1406 | assert (windows.size() > 0); |
1407 | ||
1408 | if (window.isHidden()) { | |
1409 | // Unhiding will also activate. | |
1410 | showWindow(window); | |
1411 | return; | |
1412 | } | |
1413 | assert (window.isShown()); | |
1414 | ||
1415 | if (windows.size() == 1) { | |
1416 | assert (window == windows.get(0)); | |
1417 | if (activeWindow == null) { | |
1418 | activeWindow = window; | |
1419 | window.setZ(0); | |
1420 | activeWindow.setActive(true); | |
1421 | activeWindow.onFocus(); | |
1422 | } | |
1423 | ||
1424 | assert (window.isActive()); | |
1425 | assert (activeWindow == window); | |
1426 | return; | |
1427 | } | |
1428 | ||
1429 | if (activeWindow == window) { | |
1430 | assert (window.isActive()); | |
1431 | ||
1432 | // Window is already active, do nothing. | |
1433 | return; | |
1434 | } | |
1435 | ||
1436 | assert (!window.isActive()); | |
1437 | if (activeWindow != null) { | |
1438 | assert (activeWindow.getZ() == 0); | |
1439 | ||
1440 | activeWindow.onUnfocus(); | |
1441 | activeWindow.setActive(false); | |
1442 | activeWindow.setZ(window.getZ()); | |
1443 | } | |
1444 | activeWindow = window; | |
1445 | activeWindow.setZ(0); | |
1446 | activeWindow.setActive(true); | |
1447 | activeWindow.onFocus(); | |
1448 | return; | |
1449 | } | |
1450 | ||
1451 | /** | |
1452 | * Hide a window. | |
1453 | * | |
1454 | * @param window the window to hide | |
1455 | */ | |
1456 | public void hideWindow(final TWindow window) { | |
1457 | if (hasWindow(window) == false) { | |
1458 | /* | |
1459 | * Someone has a handle to a window I don't have. Ignore this | |
1460 | * request. | |
1461 | */ | |
1462 | return; | |
1463 | } | |
1464 | ||
fe0770f9 KL |
1465 | // Whatever window might be moving/dragging, stop it now. |
1466 | for (TWindow w: windows) { | |
1467 | if (w.inMovements()) { | |
1468 | w.stopMovements(); | |
1469 | } | |
1470 | } | |
1471 | ||
92453213 KL |
1472 | assert (windows.size() > 0); |
1473 | ||
1474 | if (!window.hidden) { | |
1475 | if (window == activeWindow) { | |
1476 | if (shownWindowCount() > 1) { | |
1477 | switchWindow(true); | |
1478 | } else { | |
1479 | activeWindow = null; | |
1480 | window.setActive(false); | |
1481 | window.onUnfocus(); | |
1482 | } | |
1483 | } | |
1484 | window.hidden = true; | |
1485 | window.onHide(); | |
1486 | } | |
1487 | } | |
1488 | ||
1489 | /** | |
1490 | * Show a window. | |
1491 | * | |
1492 | * @param window the window to show | |
1493 | */ | |
1494 | public void showWindow(final TWindow window) { | |
1495 | if (hasWindow(window) == false) { | |
1496 | /* | |
1497 | * Someone has a handle to a window I don't have. Ignore this | |
1498 | * request. | |
1499 | */ | |
1500 | return; | |
1501 | } | |
1502 | ||
fe0770f9 KL |
1503 | // Whatever window might be moving/dragging, stop it now. |
1504 | for (TWindow w: windows) { | |
1505 | if (w.inMovements()) { | |
1506 | w.stopMovements(); | |
1507 | } | |
1508 | } | |
1509 | ||
92453213 KL |
1510 | assert (windows.size() > 0); |
1511 | ||
1512 | if (window.hidden) { | |
1513 | window.hidden = false; | |
1514 | window.onShow(); | |
1515 | activateWindow(window); | |
1516 | } | |
1517 | } | |
1518 | ||
48e27807 KL |
1519 | /** |
1520 | * Close window. Note that the window's destructor is NOT called by this | |
1521 | * method, instead the GC is assumed to do the cleanup. | |
1522 | * | |
1523 | * @param window the window to remove | |
1524 | */ | |
1525 | public final void closeWindow(final TWindow window) { | |
92453213 KL |
1526 | if (hasWindow(window) == false) { |
1527 | /* | |
1528 | * Someone has a handle to a window I don't have. Ignore this | |
1529 | * request. | |
1530 | */ | |
1531 | return; | |
1532 | } | |
1533 | ||
bb35d919 | 1534 | synchronized (windows) { |
fe0770f9 KL |
1535 | // Whatever window might be moving/dragging, stop it now. |
1536 | for (TWindow w: windows) { | |
1537 | if (w.inMovements()) { | |
1538 | w.stopMovements(); | |
1539 | } | |
1540 | } | |
1541 | ||
bb35d919 KL |
1542 | int z = window.getZ(); |
1543 | window.setZ(-1); | |
efb7af1f | 1544 | window.onUnfocus(); |
bb35d919 KL |
1545 | Collections.sort(windows); |
1546 | windows.remove(0); | |
92453213 | 1547 | activeWindow = null; |
bb35d919 KL |
1548 | for (TWindow w: windows) { |
1549 | if (w.getZ() > z) { | |
1550 | w.setZ(w.getZ() - 1); | |
1551 | if (w.getZ() == 0) { | |
1552 | w.setActive(true); | |
efb7af1f | 1553 | w.onFocus(); |
bb35d919 KL |
1554 | assert (activeWindow == null); |
1555 | activeWindow = w; | |
1556 | } else { | |
efb7af1f KL |
1557 | if (w.isActive()) { |
1558 | w.setActive(false); | |
1559 | w.onUnfocus(); | |
1560 | } | |
bb35d919 | 1561 | } |
48e27807 KL |
1562 | } |
1563 | } | |
1564 | } | |
1565 | ||
1566 | // Perform window cleanup | |
1567 | window.onClose(); | |
1568 | ||
48e27807 | 1569 | // Check if we are closing a TMessageBox or similar |
c6940ed9 KL |
1570 | if (secondaryEventReceiver != null) { |
1571 | assert (secondaryEventHandler != null); | |
48e27807 KL |
1572 | |
1573 | // Do not send events to the secondaryEventReceiver anymore, the | |
1574 | // window is closed. | |
1575 | secondaryEventReceiver = null; | |
1576 | ||
92554d64 KL |
1577 | // Wake the secondary thread, it will wake the primary as it |
1578 | // exits. | |
1579 | synchronized (secondaryEventHandler) { | |
1580 | secondaryEventHandler.notify(); | |
48e27807 KL |
1581 | } |
1582 | } | |
92453213 KL |
1583 | |
1584 | // Permit desktop to be active if it is the only thing left. | |
1585 | if (desktop != null) { | |
1586 | if (windows.size() == 0) { | |
1587 | desktop.setActive(true); | |
1588 | } | |
1589 | } | |
48e27807 KL |
1590 | } |
1591 | ||
1592 | /** | |
1593 | * Switch to the next window. | |
1594 | * | |
1595 | * @param forward if true, then switch to the next window in the list, | |
1596 | * otherwise switch to the previous window in the list | |
1597 | */ | |
1598 | public final void switchWindow(final boolean forward) { | |
8c236a98 KL |
1599 | // Only switch if there are multiple visible windows |
1600 | if (shownWindowCount() < 2) { | |
48e27807 KL |
1601 | return; |
1602 | } | |
92453213 | 1603 | assert (activeWindow != null); |
48e27807 | 1604 | |
bb35d919 | 1605 | synchronized (windows) { |
fe0770f9 KL |
1606 | // Whatever window might be moving/dragging, stop it now. |
1607 | for (TWindow w: windows) { | |
1608 | if (w.inMovements()) { | |
1609 | w.stopMovements(); | |
1610 | } | |
1611 | } | |
bb35d919 KL |
1612 | |
1613 | // Swap z/active between active window and the next in the list | |
1614 | int activeWindowI = -1; | |
1615 | for (int i = 0; i < windows.size(); i++) { | |
92453213 KL |
1616 | if (windows.get(i) == activeWindow) { |
1617 | assert (activeWindow.isActive()); | |
bb35d919 KL |
1618 | activeWindowI = i; |
1619 | break; | |
92453213 KL |
1620 | } else { |
1621 | assert (!windows.get(0).isActive()); | |
bb35d919 | 1622 | } |
48e27807 | 1623 | } |
bb35d919 | 1624 | assert (activeWindowI >= 0); |
48e27807 | 1625 | |
bb35d919 | 1626 | // Do not switch if a window is modal |
92453213 | 1627 | if (activeWindow.isModal()) { |
bb35d919 KL |
1628 | return; |
1629 | } | |
48e27807 | 1630 | |
8c236a98 KL |
1631 | int nextWindowI = activeWindowI; |
1632 | for (;;) { | |
1633 | if (forward) { | |
1634 | nextWindowI++; | |
1635 | nextWindowI %= windows.size(); | |
bb35d919 | 1636 | } else { |
8c236a98 KL |
1637 | nextWindowI--; |
1638 | if (nextWindowI < 0) { | |
1639 | nextWindowI = windows.size() - 1; | |
1640 | } | |
bb35d919 | 1641 | } |
bb35d919 | 1642 | |
8c236a98 KL |
1643 | if (windows.get(nextWindowI).isShown()) { |
1644 | activateWindow(windows.get(nextWindowI)); | |
1645 | break; | |
1646 | } | |
1647 | } | |
bb35d919 | 1648 | } // synchronized (windows) |
48e27807 | 1649 | |
48e27807 KL |
1650 | } |
1651 | ||
1652 | /** | |
1653 | * Add a window to my window list and make it active. | |
1654 | * | |
1655 | * @param window new window to add | |
1656 | */ | |
1657 | public final void addWindow(final TWindow window) { | |
a7986f7b KL |
1658 | |
1659 | // Do not add menu windows to the window list. | |
1660 | if (window instanceof TMenu) { | |
1661 | return; | |
1662 | } | |
1663 | ||
0ee88b6d KL |
1664 | // Do not add the desktop to the window list. |
1665 | if (window instanceof TDesktop) { | |
1666 | return; | |
1667 | } | |
1668 | ||
bb35d919 | 1669 | synchronized (windows) { |
fe0770f9 KL |
1670 | // Whatever window might be moving/dragging, stop it now. |
1671 | for (TWindow w: windows) { | |
1672 | if (w.inMovements()) { | |
1673 | w.stopMovements(); | |
1674 | } | |
1675 | } | |
1676 | ||
2ce6dab2 KL |
1677 | // Do not allow a modal window to spawn a non-modal window. If a |
1678 | // modal window is active, then this window will become modal | |
1679 | // too. | |
1680 | if (modalWindowActive()) { | |
1681 | window.flags |= TWindow.MODAL; | |
a7986f7b | 1682 | window.flags |= TWindow.CENTERED; |
92453213 | 1683 | window.hidden = false; |
bb35d919 | 1684 | } |
92453213 KL |
1685 | if (window.isShown()) { |
1686 | for (TWindow w: windows) { | |
1687 | if (w.isActive()) { | |
1688 | w.setActive(false); | |
1689 | w.onUnfocus(); | |
1690 | } | |
1691 | w.setZ(w.getZ() + 1); | |
efb7af1f | 1692 | } |
bb35d919 KL |
1693 | } |
1694 | windows.add(window); | |
92453213 KL |
1695 | if (window.isShown()) { |
1696 | activeWindow = window; | |
1697 | activeWindow.setZ(0); | |
1698 | activeWindow.setActive(true); | |
1699 | activeWindow.onFocus(); | |
1700 | } | |
a7986f7b KL |
1701 | |
1702 | if (((window.flags & TWindow.CENTERED) == 0) | |
1703 | && smartWindowPlacement) { | |
1704 | ||
1705 | doSmartPlacement(window); | |
1706 | } | |
48e27807 | 1707 | } |
92453213 KL |
1708 | |
1709 | // Desktop cannot be active over any other window. | |
1710 | if (desktop != null) { | |
1711 | desktop.setActive(false); | |
1712 | } | |
48e27807 KL |
1713 | } |
1714 | ||
fca67db0 KL |
1715 | /** |
1716 | * Check if there is a system-modal window on top. | |
1717 | * | |
1718 | * @return true if the active window is modal | |
1719 | */ | |
1720 | private boolean modalWindowActive() { | |
1721 | if (windows.size() == 0) { | |
1722 | return false; | |
1723 | } | |
2ce6dab2 KL |
1724 | |
1725 | for (TWindow w: windows) { | |
1726 | if (w.isModal()) { | |
1727 | return true; | |
1728 | } | |
1729 | } | |
1730 | ||
1731 | return false; | |
1732 | } | |
1733 | ||
1734 | /** | |
1735 | * Close all open windows. | |
1736 | */ | |
1737 | private void closeAllWindows() { | |
1738 | // Don't do anything if we are in the menu | |
1739 | if (activeMenu != null) { | |
1740 | return; | |
1741 | } | |
1742 | while (windows.size() > 0) { | |
1743 | closeWindow(windows.get(0)); | |
1744 | } | |
fca67db0 KL |
1745 | } |
1746 | ||
2ce6dab2 KL |
1747 | /** |
1748 | * Re-layout the open windows as non-overlapping tiles. This produces | |
1749 | * almost the same results as Turbo Pascal 7.0's IDE. | |
1750 | */ | |
1751 | private void tileWindows() { | |
1752 | synchronized (windows) { | |
1753 | // Don't do anything if we are in the menu | |
1754 | if (activeMenu != null) { | |
1755 | return; | |
1756 | } | |
1757 | int z = windows.size(); | |
1758 | if (z == 0) { | |
1759 | return; | |
1760 | } | |
1761 | int a = 0; | |
1762 | int b = 0; | |
1763 | a = (int)(Math.sqrt(z)); | |
1764 | int c = 0; | |
1765 | while (c < a) { | |
1766 | b = (z - c) / a; | |
1767 | if (((a * b) + c) == z) { | |
1768 | break; | |
1769 | } | |
1770 | c++; | |
1771 | } | |
1772 | assert (a > 0); | |
1773 | assert (b > 0); | |
1774 | assert (c < a); | |
1775 | int newWidth = (getScreen().getWidth() / a); | |
1776 | int newHeight1 = ((getScreen().getHeight() - 1) / b); | |
1777 | int newHeight2 = ((getScreen().getHeight() - 1) / (b + c)); | |
1778 | ||
1779 | List<TWindow> sorted = new LinkedList<TWindow>(windows); | |
1780 | Collections.sort(sorted); | |
1781 | Collections.reverse(sorted); | |
1782 | for (int i = 0; i < sorted.size(); i++) { | |
1783 | int logicalX = i / b; | |
1784 | int logicalY = i % b; | |
1785 | if (i >= ((a - 1) * b)) { | |
1786 | logicalX = a - 1; | |
1787 | logicalY = i - ((a - 1) * b); | |
1788 | } | |
1789 | ||
1790 | TWindow w = sorted.get(i); | |
1791 | w.setX(logicalX * newWidth); | |
1792 | w.setWidth(newWidth); | |
1793 | if (i >= ((a - 1) * b)) { | |
1794 | w.setY((logicalY * newHeight2) + 1); | |
1795 | w.setHeight(newHeight2); | |
1796 | } else { | |
1797 | w.setY((logicalY * newHeight1) + 1); | |
1798 | w.setHeight(newHeight1); | |
1799 | } | |
1800 | } | |
1801 | } | |
1802 | } | |
1803 | ||
1804 | /** | |
1805 | * Re-layout the open windows as overlapping cascaded windows. | |
1806 | */ | |
1807 | private void cascadeWindows() { | |
1808 | synchronized (windows) { | |
1809 | // Don't do anything if we are in the menu | |
1810 | if (activeMenu != null) { | |
1811 | return; | |
1812 | } | |
1813 | int x = 0; | |
1814 | int y = 1; | |
1815 | List<TWindow> sorted = new LinkedList<TWindow>(windows); | |
1816 | Collections.sort(sorted); | |
1817 | Collections.reverse(sorted); | |
1818 | for (TWindow window: sorted) { | |
1819 | window.setX(x); | |
1820 | window.setY(y); | |
1821 | x++; | |
1822 | y++; | |
1823 | if (x > getScreen().getWidth()) { | |
1824 | x = 0; | |
1825 | } | |
1826 | if (y >= getScreen().getHeight()) { | |
1827 | y = 1; | |
1828 | } | |
1829 | } | |
1830 | } | |
1831 | } | |
1832 | ||
a7986f7b KL |
1833 | /** |
1834 | * Place a window to minimize its overlap with other windows. | |
1835 | * | |
1836 | * @param window the window to place | |
1837 | */ | |
1838 | public final void doSmartPlacement(final TWindow window) { | |
1839 | // This is a pretty dumb algorithm, but seems to work. The hardest | |
1840 | // part is computing these "overlap" values seeking a minimum average | |
1841 | // overlap. | |
1842 | int xMin = 0; | |
1843 | int yMin = desktopTop; | |
1844 | int xMax = getScreen().getWidth() - window.getWidth() + 1; | |
1845 | int yMax = desktopBottom - window.getHeight() + 1; | |
1846 | if (xMax < xMin) { | |
1847 | xMax = xMin; | |
1848 | } | |
1849 | if (yMax < yMin) { | |
1850 | yMax = yMin; | |
1851 | } | |
1852 | ||
1853 | if ((xMin == xMax) && (yMin == yMax)) { | |
1854 | // No work to do, bail out. | |
1855 | return; | |
1856 | } | |
1857 | ||
1858 | // Compute the overlap matrix without the new window. | |
1859 | int width = getScreen().getWidth(); | |
1860 | int height = getScreen().getHeight(); | |
1861 | int overlapMatrix[][] = new int[width][height]; | |
1862 | for (TWindow w: windows) { | |
1863 | if (window == w) { | |
1864 | continue; | |
1865 | } | |
1866 | for (int x = w.getX(); x < w.getX() + w.getWidth(); x++) { | |
8c236a98 | 1867 | if (x >= width) { |
a7986f7b KL |
1868 | continue; |
1869 | } | |
1870 | for (int y = w.getY(); y < w.getY() + w.getHeight(); y++) { | |
8c236a98 | 1871 | if (y >= height) { |
a7986f7b KL |
1872 | continue; |
1873 | } | |
1874 | overlapMatrix[x][y]++; | |
1875 | } | |
1876 | } | |
1877 | } | |
1878 | ||
1879 | long oldOverlapTotal = 0; | |
1880 | long oldOverlapN = 0; | |
1881 | for (int x = 0; x < width; x++) { | |
1882 | for (int y = 0; y < height; y++) { | |
1883 | oldOverlapTotal += overlapMatrix[x][y]; | |
1884 | if (overlapMatrix[x][y] > 0) { | |
1885 | oldOverlapN++; | |
1886 | } | |
1887 | } | |
1888 | } | |
1889 | ||
1890 | ||
1891 | double oldOverlapAvg = (double) oldOverlapTotal / (double) oldOverlapN; | |
1892 | boolean first = true; | |
1893 | int windowX = window.getX(); | |
1894 | int windowY = window.getY(); | |
1895 | ||
1896 | // For each possible (x, y) position for the new window, compute a | |
1897 | // new overlap matrix. | |
1898 | for (int x = xMin; x < xMax; x++) { | |
1899 | for (int y = yMin; y < yMax; y++) { | |
1900 | ||
1901 | // Start with the matrix minus this window. | |
1902 | int newMatrix[][] = new int[width][height]; | |
1903 | for (int mx = 0; mx < width; mx++) { | |
1904 | for (int my = 0; my < height; my++) { | |
1905 | newMatrix[mx][my] = overlapMatrix[mx][my]; | |
1906 | } | |
1907 | } | |
1908 | ||
1909 | // Add this window's values to the new overlap matrix. | |
1910 | long newOverlapTotal = 0; | |
1911 | long newOverlapN = 0; | |
1912 | // Start by adding each new cell. | |
1913 | for (int wx = x; wx < x + window.getWidth(); wx++) { | |
8c236a98 | 1914 | if (wx >= width) { |
a7986f7b KL |
1915 | continue; |
1916 | } | |
1917 | for (int wy = y; wy < y + window.getHeight(); wy++) { | |
8c236a98 | 1918 | if (wy >= height) { |
a7986f7b KL |
1919 | continue; |
1920 | } | |
1921 | newMatrix[wx][wy]++; | |
1922 | } | |
1923 | } | |
1924 | // Now figure out the new value for total coverage. | |
1925 | for (int mx = 0; mx < width; mx++) { | |
1926 | for (int my = 0; my < height; my++) { | |
1927 | newOverlapTotal += newMatrix[x][y]; | |
1928 | if (newMatrix[mx][my] > 0) { | |
1929 | newOverlapN++; | |
1930 | } | |
1931 | } | |
1932 | } | |
1933 | double newOverlapAvg = (double) newOverlapTotal / (double) newOverlapN; | |
1934 | ||
1935 | if (first) { | |
1936 | // First time: just record what we got. | |
1937 | oldOverlapAvg = newOverlapAvg; | |
1938 | first = false; | |
1939 | } else { | |
1940 | // All other times: pick a new best (x, y) and save the | |
1941 | // overlap value. | |
1942 | if (newOverlapAvg < oldOverlapAvg) { | |
1943 | windowX = x; | |
1944 | windowY = y; | |
1945 | oldOverlapAvg = newOverlapAvg; | |
1946 | } | |
1947 | } | |
1948 | ||
1949 | } // for (int x = xMin; x < xMax; x++) | |
1950 | ||
1951 | } // for (int y = yMin; y < yMax; y++) | |
1952 | ||
1953 | // Finally, set the window's new coordinates. | |
1954 | window.setX(windowX); | |
1955 | window.setY(windowY); | |
1956 | } | |
1957 | ||
2ce6dab2 KL |
1958 | // ------------------------------------------------------------------------ |
1959 | // TMenu management ------------------------------------------------------- | |
1960 | // ------------------------------------------------------------------------ | |
1961 | ||
fca67db0 KL |
1962 | /** |
1963 | * Check if a mouse event would hit either the active menu or any open | |
1964 | * sub-menus. | |
1965 | * | |
1966 | * @param mouse mouse event | |
1967 | * @return true if the mouse would hit the active menu or an open | |
1968 | * sub-menu | |
1969 | */ | |
1970 | private boolean mouseOnMenu(final TMouseEvent mouse) { | |
1971 | assert (activeMenu != null); | |
1972 | List<TMenu> menus = new LinkedList<TMenu>(subMenus); | |
1973 | Collections.reverse(menus); | |
1974 | for (TMenu menu: menus) { | |
1975 | if (menu.mouseWouldHit(mouse)) { | |
1976 | return true; | |
1977 | } | |
1978 | } | |
1979 | return activeMenu.mouseWouldHit(mouse); | |
1980 | } | |
1981 | ||
1982 | /** | |
1983 | * See if we need to switch window or activate the menu based on | |
1984 | * a mouse click. | |
1985 | * | |
1986 | * @param mouse mouse event | |
1987 | */ | |
1988 | private void checkSwitchFocus(final TMouseEvent mouse) { | |
1989 | ||
1990 | if ((mouse.getType() == TMouseEvent.Type.MOUSE_DOWN) | |
1991 | && (activeMenu != null) | |
1992 | && (mouse.getAbsoluteY() != 0) | |
1993 | && (!mouseOnMenu(mouse)) | |
1994 | ) { | |
1995 | // They clicked outside the active menu, turn it off | |
1996 | activeMenu.setActive(false); | |
1997 | activeMenu = null; | |
1998 | for (TMenu menu: subMenus) { | |
1999 | menu.setActive(false); | |
2000 | } | |
2001 | subMenus.clear(); | |
2002 | // Continue checks | |
2003 | } | |
2004 | ||
2005 | // See if they hit the menu bar | |
2006 | if ((mouse.getType() == TMouseEvent.Type.MOUSE_DOWN) | |
7c870d89 | 2007 | && (mouse.isMouse1()) |
fca67db0 KL |
2008 | && (!modalWindowActive()) |
2009 | && (mouse.getAbsoluteY() == 0) | |
2010 | ) { | |
2011 | ||
2012 | for (TMenu menu: subMenus) { | |
2013 | menu.setActive(false); | |
2014 | } | |
2015 | subMenus.clear(); | |
2016 | ||
2017 | // They selected the menu, go activate it | |
2018 | for (TMenu menu: menus) { | |
2019 | if ((mouse.getAbsoluteX() >= menu.getX()) | |
2020 | && (mouse.getAbsoluteX() < menu.getX() | |
2021 | + menu.getTitle().length() + 2) | |
2022 | ) { | |
2023 | menu.setActive(true); | |
2024 | activeMenu = menu; | |
2025 | } else { | |
2026 | menu.setActive(false); | |
2027 | } | |
2028 | } | |
fca67db0 KL |
2029 | return; |
2030 | } | |
2031 | ||
2032 | // See if they hit the menu bar | |
2033 | if ((mouse.getType() == TMouseEvent.Type.MOUSE_MOTION) | |
7c870d89 | 2034 | && (mouse.isMouse1()) |
fca67db0 KL |
2035 | && (activeMenu != null) |
2036 | && (mouse.getAbsoluteY() == 0) | |
2037 | ) { | |
2038 | ||
2039 | TMenu oldMenu = activeMenu; | |
2040 | for (TMenu menu: subMenus) { | |
2041 | menu.setActive(false); | |
2042 | } | |
2043 | subMenus.clear(); | |
2044 | ||
2045 | // See if we should switch menus | |
2046 | for (TMenu menu: menus) { | |
2047 | if ((mouse.getAbsoluteX() >= menu.getX()) | |
2048 | && (mouse.getAbsoluteX() < menu.getX() | |
2049 | + menu.getTitle().length() + 2) | |
2050 | ) { | |
2051 | menu.setActive(true); | |
2052 | activeMenu = menu; | |
2053 | } | |
2054 | } | |
2055 | if (oldMenu != activeMenu) { | |
2056 | // They switched menus | |
2057 | oldMenu.setActive(false); | |
2058 | } | |
fca67db0 KL |
2059 | return; |
2060 | } | |
2061 | ||
72fca17b KL |
2062 | // If a menu is still active, don't switch windows |
2063 | if (activeMenu != null) { | |
fca67db0 KL |
2064 | return; |
2065 | } | |
2066 | ||
72fca17b KL |
2067 | // Only switch if there are multiple windows |
2068 | if (windows.size() < 2) { | |
fca67db0 KL |
2069 | return; |
2070 | } | |
2071 | ||
72fca17b KL |
2072 | if (((focusFollowsMouse == true) |
2073 | && (mouse.getType() == TMouseEvent.Type.MOUSE_MOTION)) | |
2074 | || (mouse.getType() == TMouseEvent.Type.MOUSE_UP) | |
2075 | ) { | |
2076 | synchronized (windows) { | |
2077 | Collections.sort(windows); | |
2078 | if (windows.get(0).isModal()) { | |
2079 | // Modal windows don't switch | |
2080 | return; | |
2081 | } | |
fca67db0 | 2082 | |
72fca17b KL |
2083 | for (TWindow window: windows) { |
2084 | assert (!window.isModal()); | |
92453213 | 2085 | |
72fca17b KL |
2086 | if (window.isHidden()) { |
2087 | assert (!window.isActive()); | |
2088 | continue; | |
2089 | } | |
92453213 | 2090 | |
72fca17b KL |
2091 | if (window.mouseWouldHit(mouse)) { |
2092 | if (window == windows.get(0)) { | |
2093 | // Clicked on the same window, nothing to do | |
2094 | assert (window.isActive()); | |
2095 | return; | |
2096 | } | |
2097 | ||
2098 | // We will be switching to another window | |
2099 | assert (windows.get(0).isActive()); | |
2100 | assert (windows.get(0) == activeWindow); | |
2101 | assert (!window.isActive()); | |
2102 | activeWindow.onUnfocus(); | |
2103 | activeWindow.setActive(false); | |
2104 | activeWindow.setZ(window.getZ()); | |
2105 | activeWindow = window; | |
2106 | window.setZ(0); | |
2107 | window.setActive(true); | |
2108 | window.onFocus(); | |
bb35d919 KL |
2109 | return; |
2110 | } | |
fca67db0 | 2111 | } |
fca67db0 | 2112 | } |
72fca17b KL |
2113 | |
2114 | // Clicked on the background, nothing to do | |
2115 | return; | |
fca67db0 KL |
2116 | } |
2117 | ||
72fca17b KL |
2118 | // Nothing to do: this isn't a mouse up, or focus isn't following |
2119 | // mouse. | |
fca67db0 KL |
2120 | return; |
2121 | } | |
2122 | ||
2123 | /** | |
2124 | * Turn off the menu. | |
2125 | */ | |
928811d8 | 2126 | public final void closeMenu() { |
fca67db0 KL |
2127 | if (activeMenu != null) { |
2128 | activeMenu.setActive(false); | |
2129 | activeMenu = null; | |
2130 | for (TMenu menu: subMenus) { | |
2131 | menu.setActive(false); | |
2132 | } | |
2133 | subMenus.clear(); | |
2134 | } | |
fca67db0 KL |
2135 | } |
2136 | ||
e8a11f98 KL |
2137 | /** |
2138 | * Get a (shallow) copy of the menu list. | |
2139 | * | |
2140 | * @return a copy of the menu list | |
2141 | */ | |
2142 | public final List<TMenu> getAllMenus() { | |
2143 | return new LinkedList<TMenu>(menus); | |
2144 | } | |
2145 | ||
2146 | /** | |
2147 | * Add a top-level menu to the list. | |
2148 | * | |
2149 | * @param menu the menu to add | |
2150 | * @throws IllegalArgumentException if the menu is already used in | |
2151 | * another TApplication | |
2152 | */ | |
2153 | public final void addMenu(final TMenu menu) { | |
2154 | if ((menu.getApplication() != null) | |
2155 | && (menu.getApplication() != this) | |
2156 | ) { | |
2157 | throw new IllegalArgumentException("Menu " + menu + " is already " + | |
2158 | "part of application " + menu.getApplication()); | |
2159 | } | |
2160 | closeMenu(); | |
2161 | menus.add(menu); | |
2162 | recomputeMenuX(); | |
2163 | } | |
2164 | ||
2165 | /** | |
2166 | * Remove a top-level menu from the list. | |
2167 | * | |
2168 | * @param menu the menu to remove | |
2169 | * @throws IllegalArgumentException if the menu is already used in | |
2170 | * another TApplication | |
2171 | */ | |
2172 | public final void removeMenu(final TMenu menu) { | |
2173 | if ((menu.getApplication() != null) | |
2174 | && (menu.getApplication() != this) | |
2175 | ) { | |
2176 | throw new IllegalArgumentException("Menu " + menu + " is already " + | |
2177 | "part of application " + menu.getApplication()); | |
2178 | } | |
2179 | closeMenu(); | |
2180 | menus.remove(menu); | |
2181 | recomputeMenuX(); | |
2182 | } | |
2183 | ||
fca67db0 KL |
2184 | /** |
2185 | * Turn off a sub-menu. | |
2186 | */ | |
928811d8 | 2187 | public final void closeSubMenu() { |
fca67db0 KL |
2188 | assert (activeMenu != null); |
2189 | TMenu item = subMenus.get(subMenus.size() - 1); | |
2190 | assert (item != null); | |
2191 | item.setActive(false); | |
2192 | subMenus.remove(subMenus.size() - 1); | |
fca67db0 KL |
2193 | } |
2194 | ||
2195 | /** | |
2196 | * Switch to the next menu. | |
2197 | * | |
2198 | * @param forward if true, then switch to the next menu in the list, | |
2199 | * otherwise switch to the previous menu in the list | |
2200 | */ | |
928811d8 | 2201 | public final void switchMenu(final boolean forward) { |
fca67db0 KL |
2202 | assert (activeMenu != null); |
2203 | ||
2204 | for (TMenu menu: subMenus) { | |
2205 | menu.setActive(false); | |
2206 | } | |
2207 | subMenus.clear(); | |
2208 | ||
2209 | for (int i = 0; i < menus.size(); i++) { | |
2210 | if (activeMenu == menus.get(i)) { | |
2211 | if (forward) { | |
2212 | if (i < menus.size() - 1) { | |
2213 | i++; | |
2214 | } | |
2215 | } else { | |
2216 | if (i > 0) { | |
2217 | i--; | |
2218 | } | |
2219 | } | |
2220 | activeMenu.setActive(false); | |
2221 | activeMenu = menus.get(i); | |
2222 | activeMenu.setActive(true); | |
fca67db0 KL |
2223 | return; |
2224 | } | |
2225 | } | |
2226 | } | |
2227 | ||
928811d8 | 2228 | /** |
efb7af1f KL |
2229 | * Add a menu item to the global list. If it has a keyboard accelerator, |
2230 | * that will be added the global hash. | |
928811d8 | 2231 | * |
efb7af1f | 2232 | * @param item the menu item |
928811d8 | 2233 | */ |
efb7af1f KL |
2234 | public final void addMenuItem(final TMenuItem item) { |
2235 | menuItems.add(item); | |
2236 | ||
2237 | TKeypress key = item.getKey(); | |
2238 | if (key != null) { | |
2239 | synchronized (accelerators) { | |
2240 | assert (accelerators.get(key) == null); | |
2241 | accelerators.put(key.toLowerCase(), item); | |
2242 | } | |
2243 | } | |
2244 | } | |
2245 | ||
2246 | /** | |
2247 | * Disable one menu item. | |
2248 | * | |
2249 | * @param id the menu item ID | |
2250 | */ | |
2251 | public final void disableMenuItem(final int id) { | |
2252 | for (TMenuItem item: menuItems) { | |
2253 | if (item.getId() == id) { | |
2254 | item.setEnabled(false); | |
2255 | } | |
2256 | } | |
2257 | } | |
e826b451 | 2258 | |
efb7af1f KL |
2259 | /** |
2260 | * Disable the range of menu items with ID's between lower and upper, | |
2261 | * inclusive. | |
2262 | * | |
2263 | * @param lower the lowest menu item ID | |
2264 | * @param upper the highest menu item ID | |
2265 | */ | |
2266 | public final void disableMenuItems(final int lower, final int upper) { | |
2267 | for (TMenuItem item: menuItems) { | |
2268 | if ((item.getId() >= lower) && (item.getId() <= upper)) { | |
2269 | item.setEnabled(false); | |
2270 | } | |
2271 | } | |
2272 | } | |
2273 | ||
2274 | /** | |
2275 | * Enable one menu item. | |
2276 | * | |
2277 | * @param id the menu item ID | |
2278 | */ | |
2279 | public final void enableMenuItem(final int id) { | |
2280 | for (TMenuItem item: menuItems) { | |
2281 | if (item.getId() == id) { | |
2282 | item.setEnabled(true); | |
2283 | } | |
2284 | } | |
2285 | } | |
2286 | ||
2287 | /** | |
2288 | * Enable the range of menu items with ID's between lower and upper, | |
2289 | * inclusive. | |
2290 | * | |
2291 | * @param lower the lowest menu item ID | |
2292 | * @param upper the highest menu item ID | |
2293 | */ | |
2294 | public final void enableMenuItems(final int lower, final int upper) { | |
2295 | for (TMenuItem item: menuItems) { | |
2296 | if ((item.getId() >= lower) && (item.getId() <= upper)) { | |
2297 | item.setEnabled(true); | |
2298 | } | |
e826b451 | 2299 | } |
928811d8 KL |
2300 | } |
2301 | ||
2302 | /** | |
2303 | * Recompute menu x positions based on their title length. | |
2304 | */ | |
2305 | public final void recomputeMenuX() { | |
2306 | int x = 0; | |
2307 | for (TMenu menu: menus) { | |
2308 | menu.setX(x); | |
2309 | x += menu.getTitle().length() + 2; | |
2310 | } | |
2311 | } | |
2312 | ||
2313 | /** | |
2314 | * Post an event to process and turn off the menu. | |
2315 | * | |
2316 | * @param event new event to add to the queue | |
2317 | */ | |
5dfd1c11 | 2318 | public final void postMenuEvent(final TInputEvent event) { |
8e688b92 KL |
2319 | synchronized (fillEventQueue) { |
2320 | fillEventQueue.add(event); | |
2321 | } | |
928811d8 KL |
2322 | closeMenu(); |
2323 | } | |
2324 | ||
2325 | /** | |
2326 | * Add a sub-menu to the list of open sub-menus. | |
2327 | * | |
2328 | * @param menu sub-menu | |
2329 | */ | |
2330 | public final void addSubMenu(final TMenu menu) { | |
2331 | subMenus.add(menu); | |
2332 | } | |
2333 | ||
8e688b92 KL |
2334 | /** |
2335 | * Convenience function to add a top-level menu. | |
2336 | * | |
2337 | * @param title menu title | |
2338 | * @return the new menu | |
2339 | */ | |
87a17f3c | 2340 | public final TMenu addMenu(final String title) { |
8e688b92 KL |
2341 | int x = 0; |
2342 | int y = 0; | |
2343 | TMenu menu = new TMenu(this, x, y, title); | |
2344 | menus.add(menu); | |
2345 | recomputeMenuX(); | |
2346 | return menu; | |
2347 | } | |
2348 | ||
2349 | /** | |
2350 | * Convenience function to add a default "File" menu. | |
2351 | * | |
2352 | * @return the new menu | |
2353 | */ | |
2354 | public final TMenu addFileMenu() { | |
2355 | TMenu fileMenu = addMenu("&File"); | |
2356 | fileMenu.addDefaultItem(TMenu.MID_OPEN_FILE); | |
2357 | fileMenu.addSeparator(); | |
2358 | fileMenu.addDefaultItem(TMenu.MID_SHELL); | |
2359 | fileMenu.addDefaultItem(TMenu.MID_EXIT); | |
2ce6dab2 KL |
2360 | TStatusBar statusBar = fileMenu.newStatusBar("File-management " + |
2361 | "commands (Open, Save, Print, etc.)"); | |
2362 | statusBar.addShortcutKeypress(kbF1, cmHelp, "Help"); | |
8e688b92 KL |
2363 | return fileMenu; |
2364 | } | |
2365 | ||
2366 | /** | |
2367 | * Convenience function to add a default "Edit" menu. | |
2368 | * | |
2369 | * @return the new menu | |
2370 | */ | |
2371 | public final TMenu addEditMenu() { | |
2372 | TMenu editMenu = addMenu("&Edit"); | |
2373 | editMenu.addDefaultItem(TMenu.MID_CUT); | |
2374 | editMenu.addDefaultItem(TMenu.MID_COPY); | |
2375 | editMenu.addDefaultItem(TMenu.MID_PASTE); | |
2376 | editMenu.addDefaultItem(TMenu.MID_CLEAR); | |
2ce6dab2 KL |
2377 | TStatusBar statusBar = editMenu.newStatusBar("Editor operations, " + |
2378 | "undo, and Clipboard access"); | |
2379 | statusBar.addShortcutKeypress(kbF1, cmHelp, "Help"); | |
8e688b92 KL |
2380 | return editMenu; |
2381 | } | |
2382 | ||
2383 | /** | |
2384 | * Convenience function to add a default "Window" menu. | |
2385 | * | |
2386 | * @return the new menu | |
2387 | */ | |
c6940ed9 | 2388 | public final TMenu addWindowMenu() { |
8e688b92 KL |
2389 | TMenu windowMenu = addMenu("&Window"); |
2390 | windowMenu.addDefaultItem(TMenu.MID_TILE); | |
2391 | windowMenu.addDefaultItem(TMenu.MID_CASCADE); | |
2392 | windowMenu.addDefaultItem(TMenu.MID_CLOSE_ALL); | |
2393 | windowMenu.addSeparator(); | |
2394 | windowMenu.addDefaultItem(TMenu.MID_WINDOW_MOVE); | |
2395 | windowMenu.addDefaultItem(TMenu.MID_WINDOW_ZOOM); | |
2396 | windowMenu.addDefaultItem(TMenu.MID_WINDOW_NEXT); | |
2397 | windowMenu.addDefaultItem(TMenu.MID_WINDOW_PREVIOUS); | |
2398 | windowMenu.addDefaultItem(TMenu.MID_WINDOW_CLOSE); | |
2ce6dab2 KL |
2399 | TStatusBar statusBar = windowMenu.newStatusBar("Open, arrange, and " + |
2400 | "list windows"); | |
2401 | statusBar.addShortcutKeypress(kbF1, cmHelp, "Help"); | |
8e688b92 KL |
2402 | return windowMenu; |
2403 | } | |
2404 | ||
55d2b2c2 KL |
2405 | /** |
2406 | * Convenience function to add a default "Help" menu. | |
2407 | * | |
2408 | * @return the new menu | |
2409 | */ | |
2410 | public final TMenu addHelpMenu() { | |
2411 | TMenu helpMenu = addMenu("&Help"); | |
2412 | helpMenu.addDefaultItem(TMenu.MID_HELP_CONTENTS); | |
2413 | helpMenu.addDefaultItem(TMenu.MID_HELP_INDEX); | |
2414 | helpMenu.addDefaultItem(TMenu.MID_HELP_SEARCH); | |
2415 | helpMenu.addDefaultItem(TMenu.MID_HELP_PREVIOUS); | |
2416 | helpMenu.addDefaultItem(TMenu.MID_HELP_HELP); | |
2417 | helpMenu.addDefaultItem(TMenu.MID_HELP_ACTIVE_FILE); | |
2418 | helpMenu.addSeparator(); | |
2419 | helpMenu.addDefaultItem(TMenu.MID_ABOUT); | |
2ce6dab2 KL |
2420 | TStatusBar statusBar = helpMenu.newStatusBar("Access online help"); |
2421 | statusBar.addShortcutKeypress(kbF1, cmHelp, "Help"); | |
55d2b2c2 KL |
2422 | return helpMenu; |
2423 | } | |
2424 | ||
2ce6dab2 KL |
2425 | // ------------------------------------------------------------------------ |
2426 | // Event handlers --------------------------------------------------------- | |
2427 | // ------------------------------------------------------------------------ | |
2428 | ||
8e688b92 | 2429 | /** |
2ce6dab2 KL |
2430 | * Method that TApplication subclasses can override to handle menu or |
2431 | * posted command events. | |
2432 | * | |
2433 | * @param command command event | |
2434 | * @return if true, this event was consumed | |
8e688b92 | 2435 | */ |
2ce6dab2 KL |
2436 | protected boolean onCommand(final TCommandEvent command) { |
2437 | // Default: handle cmExit | |
2438 | if (command.equals(cmExit)) { | |
2439 | if (messageBox("Confirmation", "Exit application?", | |
2440 | TMessageBox.Type.YESNO).getResult() == TMessageBox.Result.YES) { | |
2441 | quit = true; | |
2442 | } | |
2443 | return true; | |
8e688b92 | 2444 | } |
2ce6dab2 KL |
2445 | |
2446 | if (command.equals(cmShell)) { | |
2447 | openTerminal(0, 0, TWindow.RESIZABLE); | |
2448 | return true; | |
2449 | } | |
2450 | ||
2451 | if (command.equals(cmTile)) { | |
2452 | tileWindows(); | |
2453 | return true; | |
2454 | } | |
2455 | if (command.equals(cmCascade)) { | |
2456 | cascadeWindows(); | |
2457 | return true; | |
2458 | } | |
2459 | if (command.equals(cmCloseAll)) { | |
2460 | closeAllWindows(); | |
2461 | return true; | |
8e688b92 | 2462 | } |
2ce6dab2 | 2463 | |
71a389c9 KL |
2464 | if (command.equals(cmMenu)) { |
2465 | if (!modalWindowActive() && (activeMenu == null)) { | |
2466 | if (menus.size() > 0) { | |
2467 | menus.get(0).setActive(true); | |
2468 | activeMenu = menus.get(0); | |
2469 | return true; | |
2470 | } | |
2471 | } | |
2472 | } | |
2473 | ||
2ce6dab2 | 2474 | return false; |
8e688b92 KL |
2475 | } |
2476 | ||
2477 | /** | |
2ce6dab2 KL |
2478 | * Method that TApplication subclasses can override to handle menu |
2479 | * events. | |
2480 | * | |
2481 | * @param menu menu event | |
2482 | * @return if true, this event was consumed | |
8e688b92 | 2483 | */ |
2ce6dab2 KL |
2484 | protected boolean onMenu(final TMenuEvent menu) { |
2485 | ||
2486 | // Default: handle MID_EXIT | |
2487 | if (menu.getId() == TMenu.MID_EXIT) { | |
2488 | if (messageBox("Confirmation", "Exit application?", | |
2489 | TMessageBox.Type.YESNO).getResult() == TMessageBox.Result.YES) { | |
2490 | quit = true; | |
8e688b92 | 2491 | } |
2ce6dab2 KL |
2492 | return true; |
2493 | } | |
bb35d919 | 2494 | |
2ce6dab2 KL |
2495 | if (menu.getId() == TMenu.MID_SHELL) { |
2496 | openTerminal(0, 0, TWindow.RESIZABLE); | |
2497 | return true; | |
2498 | } | |
8e688b92 | 2499 | |
2ce6dab2 KL |
2500 | if (menu.getId() == TMenu.MID_TILE) { |
2501 | tileWindows(); | |
2502 | return true; | |
2503 | } | |
2504 | if (menu.getId() == TMenu.MID_CASCADE) { | |
2505 | cascadeWindows(); | |
2506 | return true; | |
2507 | } | |
2508 | if (menu.getId() == TMenu.MID_CLOSE_ALL) { | |
2509 | closeAllWindows(); | |
2510 | return true; | |
2511 | } | |
2512 | if (menu.getId() == TMenu.MID_ABOUT) { | |
2513 | showAboutDialog(); | |
2514 | return true; | |
2515 | } | |
2516 | return false; | |
2517 | } | |
2518 | ||
2519 | /** | |
2520 | * Method that TApplication subclasses can override to handle keystrokes. | |
2521 | * | |
2522 | * @param keypress keystroke event | |
2523 | * @return if true, this event was consumed | |
2524 | */ | |
2525 | protected boolean onKeypress(final TKeypressEvent keypress) { | |
2526 | // Default: only menu shortcuts | |
2527 | ||
2528 | // Process Alt-F, Alt-E, etc. menu shortcut keys | |
2529 | if (!keypress.getKey().isFnKey() | |
2530 | && keypress.getKey().isAlt() | |
2531 | && !keypress.getKey().isCtrl() | |
2532 | && (activeMenu == null) | |
2533 | && !modalWindowActive() | |
2534 | ) { | |
2535 | ||
2536 | assert (subMenus.size() == 0); | |
2537 | ||
2538 | for (TMenu menu: menus) { | |
2539 | if (Character.toLowerCase(menu.getMnemonic().getShortcut()) | |
2540 | == Character.toLowerCase(keypress.getKey().getChar()) | |
2541 | ) { | |
2542 | activeMenu = menu; | |
2543 | menu.setActive(true); | |
2544 | return true; | |
bb35d919 | 2545 | } |
8e688b92 KL |
2546 | } |
2547 | } | |
2ce6dab2 KL |
2548 | |
2549 | return false; | |
8e688b92 KL |
2550 | } |
2551 | ||
2ce6dab2 KL |
2552 | // ------------------------------------------------------------------------ |
2553 | // TTimer management ------------------------------------------------------ | |
2554 | // ------------------------------------------------------------------------ | |
2555 | ||
8e688b92 | 2556 | /** |
2ce6dab2 KL |
2557 | * Get the amount of time I can sleep before missing a Timer tick. |
2558 | * | |
2559 | * @param timeout = initial (maximum) timeout in millis | |
2560 | * @return number of milliseconds between now and the next timer event | |
8e688b92 | 2561 | */ |
2ce6dab2 KL |
2562 | private long getSleepTime(final long timeout) { |
2563 | Date now = new Date(); | |
2564 | long nowTime = now.getTime(); | |
2565 | long sleepTime = timeout; | |
2566 | for (TTimer timer: timers) { | |
2567 | long nextTickTime = timer.getNextTick().getTime(); | |
2568 | if (nextTickTime < nowTime) { | |
2569 | return 0; | |
8e688b92 | 2570 | } |
2ce6dab2 KL |
2571 | |
2572 | long timeDifference = nextTickTime - nowTime; | |
2573 | if (timeDifference < sleepTime) { | |
2574 | sleepTime = timeDifference; | |
8e688b92 KL |
2575 | } |
2576 | } | |
2ce6dab2 KL |
2577 | assert (sleepTime >= 0); |
2578 | assert (sleepTime <= timeout); | |
2579 | return sleepTime; | |
8e688b92 KL |
2580 | } |
2581 | ||
d502a0e9 KL |
2582 | /** |
2583 | * Convenience function to add a timer. | |
2584 | * | |
2585 | * @param duration number of milliseconds to wait between ticks | |
2586 | * @param recurring if true, re-schedule this timer after every tick | |
2587 | * @param action function to call when button is pressed | |
c6940ed9 | 2588 | * @return the timer |
d502a0e9 KL |
2589 | */ |
2590 | public final TTimer addTimer(final long duration, final boolean recurring, | |
2591 | final TAction action) { | |
2592 | ||
2593 | TTimer timer = new TTimer(duration, recurring, action); | |
2594 | synchronized (timers) { | |
2595 | timers.add(timer); | |
2596 | } | |
2597 | return timer; | |
2598 | } | |
2599 | ||
2600 | /** | |
2601 | * Convenience function to remove a timer. | |
2602 | * | |
2603 | * @param timer timer to remove | |
2604 | */ | |
2605 | public final void removeTimer(final TTimer timer) { | |
2606 | synchronized (timers) { | |
2607 | timers.remove(timer); | |
2608 | } | |
2609 | } | |
2610 | ||
2ce6dab2 KL |
2611 | // ------------------------------------------------------------------------ |
2612 | // Other TWindow constructors --------------------------------------------- | |
2613 | // ------------------------------------------------------------------------ | |
2614 | ||
c6940ed9 KL |
2615 | /** |
2616 | * Convenience function to spawn a message box. | |
2617 | * | |
2618 | * @param title window title, will be centered along the top border | |
2619 | * @param caption message to display. Use embedded newlines to get a | |
2620 | * multi-line box. | |
2621 | * @return the new message box | |
2622 | */ | |
2623 | public final TMessageBox messageBox(final String title, | |
2624 | final String caption) { | |
2625 | ||
2626 | return new TMessageBox(this, title, caption, TMessageBox.Type.OK); | |
2627 | } | |
2628 | ||
2629 | /** | |
2630 | * Convenience function to spawn a message box. | |
2631 | * | |
2632 | * @param title window title, will be centered along the top border | |
2633 | * @param caption message to display. Use embedded newlines to get a | |
2634 | * multi-line box. | |
2635 | * @param type one of the TMessageBox.Type constants. Default is | |
2636 | * Type.OK. | |
2637 | * @return the new message box | |
2638 | */ | |
2639 | public final TMessageBox messageBox(final String title, | |
2640 | final String caption, final TMessageBox.Type type) { | |
2641 | ||
2642 | return new TMessageBox(this, title, caption, type); | |
2643 | } | |
2644 | ||
2645 | /** | |
2646 | * Convenience function to spawn an input box. | |
2647 | * | |
2648 | * @param title window title, will be centered along the top border | |
2649 | * @param caption message to display. Use embedded newlines to get a | |
2650 | * multi-line box. | |
2651 | * @return the new input box | |
2652 | */ | |
2653 | public final TInputBox inputBox(final String title, final String caption) { | |
2654 | ||
2655 | return new TInputBox(this, title, caption); | |
2656 | } | |
2657 | ||
2658 | /** | |
2659 | * Convenience function to spawn an input box. | |
2660 | * | |
2661 | * @param title window title, will be centered along the top border | |
2662 | * @param caption message to display. Use embedded newlines to get a | |
2663 | * multi-line box. | |
2664 | * @param text initial text to seed the field with | |
2665 | * @return the new input box | |
2666 | */ | |
2667 | public final TInputBox inputBox(final String title, final String caption, | |
2668 | final String text) { | |
2669 | ||
2670 | return new TInputBox(this, title, caption, text); | |
2671 | } | |
1ac2ccb1 | 2672 | |
34a42e78 KL |
2673 | /** |
2674 | * Convenience function to open a terminal window. | |
2675 | * | |
2676 | * @param x column relative to parent | |
2677 | * @param y row relative to parent | |
2678 | * @return the terminal new window | |
2679 | */ | |
2680 | public final TTerminalWindow openTerminal(final int x, final int y) { | |
2681 | return openTerminal(x, y, TWindow.RESIZABLE); | |
2682 | } | |
2683 | ||
2684 | /** | |
2685 | * Convenience function to open a terminal window. | |
2686 | * | |
2687 | * @param x column relative to parent | |
2688 | * @param y row relative to parent | |
2689 | * @param flags mask of CENTERED, MODAL, or RESIZABLE | |
2690 | * @return the terminal new window | |
2691 | */ | |
2692 | public final TTerminalWindow openTerminal(final int x, final int y, | |
2693 | final int flags) { | |
2694 | ||
2695 | return new TTerminalWindow(this, x, y, flags); | |
2696 | } | |
2697 | ||
0d47c546 KL |
2698 | /** |
2699 | * Convenience function to spawn an file open box. | |
2700 | * | |
2701 | * @param path path of selected file | |
2702 | * @return the result of the new file open box | |
329fd62e | 2703 | * @throws IOException if java.io operation throws |
0d47c546 KL |
2704 | */ |
2705 | public final String fileOpenBox(final String path) throws IOException { | |
2706 | ||
2707 | TFileOpenBox box = new TFileOpenBox(this, path, TFileOpenBox.Type.OPEN); | |
2708 | return box.getFilename(); | |
2709 | } | |
2710 | ||
2711 | /** | |
2712 | * Convenience function to spawn an file open box. | |
2713 | * | |
2714 | * @param path path of selected file | |
2715 | * @param type one of the Type constants | |
2716 | * @return the result of the new file open box | |
329fd62e | 2717 | * @throws IOException if java.io operation throws |
0d47c546 KL |
2718 | */ |
2719 | public final String fileOpenBox(final String path, | |
2720 | final TFileOpenBox.Type type) throws IOException { | |
2721 | ||
2722 | TFileOpenBox box = new TFileOpenBox(this, path, type); | |
2723 | return box.getFilename(); | |
2724 | } | |
2725 | ||
92453213 KL |
2726 | /** |
2727 | * Convenience function to create a new window and make it active. | |
2728 | * Window will be located at (0, 0). | |
2729 | * | |
2730 | * @param title window title, will be centered along the top border | |
2731 | * @param width width of window | |
2732 | * @param height height of window | |
2733 | */ | |
2734 | public final TWindow addWindow(final String title, final int width, | |
2735 | final int height) { | |
2736 | ||
2737 | TWindow window = new TWindow(this, title, 0, 0, width, height); | |
2738 | return window; | |
2739 | } | |
2740 | /** | |
2741 | * Convenience function to create a new window and make it active. | |
2742 | * Window will be located at (0, 0). | |
2743 | * | |
2744 | * @param title window title, will be centered along the top border | |
2745 | * @param width width of window | |
2746 | * @param height height of window | |
2747 | * @param flags bitmask of RESIZABLE, CENTERED, or MODAL | |
2748 | */ | |
2749 | public final TWindow addWindow(final String title, | |
2750 | final int width, final int height, final int flags) { | |
2751 | ||
2752 | TWindow window = new TWindow(this, title, 0, 0, width, height, flags); | |
2753 | return window; | |
2754 | } | |
2755 | ||
2756 | /** | |
2757 | * Convenience function to create a new window and make it active. | |
2758 | * | |
2759 | * @param title window title, will be centered along the top border | |
2760 | * @param x column relative to parent | |
2761 | * @param y row relative to parent | |
2762 | * @param width width of window | |
2763 | * @param height height of window | |
2764 | */ | |
2765 | public final TWindow addWindow(final String title, | |
2766 | final int x, final int y, final int width, final int height) { | |
2767 | ||
2768 | TWindow window = new TWindow(this, title, x, y, width, height); | |
2769 | return window; | |
2770 | } | |
2771 | ||
2772 | /** | |
2773 | * Convenience function to create a new window and make it active. | |
2774 | * | |
92453213 KL |
2775 | * @param title window title, will be centered along the top border |
2776 | * @param x column relative to parent | |
2777 | * @param y row relative to parent | |
2778 | * @param width width of window | |
2779 | * @param height height of window | |
2780 | * @param flags mask of RESIZABLE, CENTERED, or MODAL | |
2781 | */ | |
2782 | public final TWindow addWindow(final String title, | |
2783 | final int x, final int y, final int width, final int height, | |
2784 | final int flags) { | |
2785 | ||
2786 | TWindow window = new TWindow(this, title, x, y, width, height, flags); | |
2787 | return window; | |
2788 | } | |
2789 | ||
7d4115a5 | 2790 | } |