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