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