Commit | Line | Data |
---|---|---|
daa4106c | 1 | /* |
7b5261bc | 2 | * Jexer - Java Text User Interface |
7d4115a5 | 3 | * |
e16dda65 | 4 | * The MIT License (MIT) |
7d4115a5 | 5 | * |
a69ed767 | 6 | * Copyright (C) 2019 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 | ||
e23ea538 | 31 | import java.io.File; |
4941d2d6 | 32 | import java.io.FileInputStream; |
4328bb42 | 33 | import java.io.InputStream; |
0d47c546 | 34 | import java.io.IOException; |
4328bb42 | 35 | import java.io.OutputStream; |
6985c572 KL |
36 | import java.io.PrintWriter; |
37 | import java.io.Reader; | |
4328bb42 | 38 | import java.io.UnsupportedEncodingException; |
e23ea538 | 39 | import java.text.MessageFormat; |
a69ed767 | 40 | import java.util.ArrayList; |
a06459bd | 41 | import java.util.Collections; |
d502a0e9 | 42 | import java.util.Date; |
e826b451 | 43 | import java.util.HashMap; |
4328bb42 KL |
44 | import java.util.LinkedList; |
45 | import java.util.List; | |
e826b451 | 46 | import java.util.Map; |
339652cc | 47 | import java.util.ResourceBundle; |
4328bb42 | 48 | |
a69ed767 | 49 | import jexer.bits.Cell; |
4328bb42 | 50 | import jexer.bits.CellAttributes; |
54eaded0 | 51 | import jexer.bits.Clipboard; |
4328bb42 | 52 | import jexer.bits.ColorTheme; |
e820d5dd | 53 | import jexer.bits.StringUtils; |
4328bb42 KL |
54 | import jexer.event.TCommandEvent; |
55 | import jexer.event.TInputEvent; | |
56 | import jexer.event.TKeypressEvent; | |
fca67db0 | 57 | import jexer.event.TMenuEvent; |
4328bb42 KL |
58 | import jexer.event.TMouseEvent; |
59 | import jexer.event.TResizeEvent; | |
60 | import jexer.backend.Backend; | |
be72cb5c | 61 | import jexer.backend.MultiBackend; |
a69ed767 | 62 | import jexer.backend.Screen; |
a4406f4e | 63 | import jexer.backend.SwingBackend; |
4328bb42 | 64 | import jexer.backend.ECMA48Backend; |
3e074355 | 65 | import jexer.backend.TWindowBackend; |
4941d2d6 KL |
66 | import jexer.help.HelpFile; |
67 | import jexer.help.Topic; | |
928811d8 KL |
68 | import jexer.menu.TMenu; |
69 | import jexer.menu.TMenuItem; | |
1dac6b8d | 70 | import jexer.menu.TSubMenu; |
4328bb42 | 71 | import static jexer.TCommand.*; |
2ce6dab2 | 72 | import static jexer.TKeypress.*; |
4328bb42 | 73 | |
7d4115a5 | 74 | /** |
42873e30 KL |
75 | * TApplication is the main driver class for a full Text User Interface |
76 | * application. It manages windows, provides a menu bar and status bar, and | |
77 | * processes events received from the user. | |
7d4115a5 | 78 | */ |
a4406f4e | 79 | public class TApplication implements Runnable { |
7d4115a5 | 80 | |
339652cc KL |
81 | /** |
82 | * Translated strings. | |
83 | */ | |
84 | private static final ResourceBundle i18n = ResourceBundle.getBundle(TApplication.class.getName()); | |
85 | ||
2ce6dab2 | 86 | // ------------------------------------------------------------------------ |
d36057df | 87 | // Constants -------------------------------------------------------------- |
2ce6dab2 KL |
88 | // ------------------------------------------------------------------------ |
89 | ||
99144c71 KL |
90 | /** |
91 | * If true, emit thread stuff to System.err. | |
92 | */ | |
93 | private static final boolean debugThreads = false; | |
94 | ||
a83fea2b KL |
95 | /** |
96 | * If true, emit events being processed to System.err. | |
97 | */ | |
98 | private static final boolean debugEvents = false; | |
99 | ||
a7986f7b KL |
100 | /** |
101 | * If true, do "smart placement" on new windows that are not specified to | |
102 | * be centered. | |
103 | */ | |
104 | private static final boolean smartWindowPlacement = true; | |
105 | ||
a4406f4e KL |
106 | /** |
107 | * Two backend types are available. | |
108 | */ | |
109 | public static enum BackendType { | |
110 | /** | |
111 | * A Swing JFrame. | |
112 | */ | |
113 | SWING, | |
114 | ||
115 | /** | |
116 | * An ECMA48 / ANSI X3.64 / XTERM style terminal. | |
117 | */ | |
118 | ECMA48, | |
119 | ||
120 | /** | |
329fd62e | 121 | * Synonym for ECMA48. |
a4406f4e KL |
122 | */ |
123 | XTERM | |
124 | } | |
125 | ||
2ce6dab2 | 126 | // ------------------------------------------------------------------------ |
d36057df | 127 | // Variables -------------------------------------------------------------- |
2ce6dab2 KL |
128 | // ------------------------------------------------------------------------ |
129 | ||
d36057df KL |
130 | /** |
131 | * The primary event handler thread. | |
132 | */ | |
133 | private volatile WidgetEventHandler primaryEventHandler; | |
134 | ||
135 | /** | |
136 | * The secondary event handler thread. | |
137 | */ | |
138 | private volatile WidgetEventHandler secondaryEventHandler; | |
139 | ||
d14e2d78 KL |
140 | /** |
141 | * The screen handler thread. | |
142 | */ | |
143 | private volatile ScreenHandler screenHandler; | |
144 | ||
d36057df KL |
145 | /** |
146 | * The widget receiving events from the secondary event handler thread. | |
147 | */ | |
148 | private volatile TWidget secondaryEventReceiver; | |
149 | ||
150 | /** | |
151 | * Access to the physical screen, keyboard, and mouse. | |
152 | */ | |
153 | private Backend backend; | |
154 | ||
54eaded0 KL |
155 | /** |
156 | * The clipboard for copy and paste. | |
157 | */ | |
158 | private Clipboard clipboard = new Clipboard(); | |
159 | ||
d36057df KL |
160 | /** |
161 | * Actual mouse coordinate X. | |
162 | */ | |
163 | private int mouseX; | |
164 | ||
165 | /** | |
166 | * Actual mouse coordinate Y. | |
167 | */ | |
168 | private int mouseY; | |
169 | ||
a69ed767 KL |
170 | /** |
171 | * Old drawn version of mouse coordinate X. | |
172 | */ | |
173 | private int oldDrawnMouseX; | |
174 | ||
175 | /** | |
176 | * Old drawn version mouse coordinate Y. | |
177 | */ | |
178 | private int oldDrawnMouseY; | |
179 | ||
180 | /** | |
181 | * Old drawn version mouse cell. | |
182 | */ | |
183 | private Cell oldDrawnMouseCell = new Cell(); | |
184 | ||
d36057df KL |
185 | /** |
186 | * The last mouse up click time, used to determine if this is a mouse | |
187 | * double-click. | |
188 | */ | |
189 | private long lastMouseUpTime; | |
190 | ||
191 | /** | |
192 | * The amount of millis between mouse up events to assume a double-click. | |
193 | */ | |
194 | private long doubleClickTime = 250; | |
195 | ||
196 | /** | |
197 | * Event queue that is filled by run(). | |
198 | */ | |
199 | private List<TInputEvent> fillEventQueue; | |
200 | ||
201 | /** | |
202 | * Event queue that will be drained by either primary or secondary | |
203 | * Thread. | |
204 | */ | |
205 | private List<TInputEvent> drainEventQueue; | |
206 | ||
207 | /** | |
208 | * Top-level menus in this application. | |
209 | */ | |
210 | private List<TMenu> menus; | |
211 | ||
212 | /** | |
213 | * Stack of activated sub-menus in this application. | |
214 | */ | |
215 | private List<TMenu> subMenus; | |
216 | ||
217 | /** | |
218 | * The currently active menu. | |
219 | */ | |
220 | private TMenu activeMenu = null; | |
221 | ||
222 | /** | |
223 | * Active keyboard accelerators. | |
224 | */ | |
225 | private Map<TKeypress, TMenuItem> accelerators; | |
226 | ||
227 | /** | |
228 | * All menu items. | |
229 | */ | |
230 | private List<TMenuItem> menuItems; | |
231 | ||
232 | /** | |
233 | * Windows and widgets pull colors from this ColorTheme. | |
234 | */ | |
235 | private ColorTheme theme; | |
236 | ||
237 | /** | |
238 | * The top-level windows (but not menus). | |
239 | */ | |
240 | private List<TWindow> windows; | |
241 | ||
d36057df KL |
242 | /** |
243 | * Timers that are being ticked. | |
244 | */ | |
245 | private List<TTimer> timers; | |
246 | ||
247 | /** | |
248 | * When true, the application has been started. | |
249 | */ | |
250 | private volatile boolean started = false; | |
251 | ||
252 | /** | |
253 | * When true, exit the application. | |
254 | */ | |
255 | private volatile boolean quit = false; | |
256 | ||
257 | /** | |
258 | * When true, repaint the entire screen. | |
259 | */ | |
260 | private volatile boolean repaint = true; | |
261 | ||
262 | /** | |
263 | * Y coordinate of the top edge of the desktop. For now this is a | |
264 | * constant. Someday it would be nice to have a multi-line menu or | |
265 | * toolbars. | |
266 | */ | |
2bb26984 | 267 | private int desktopTop = 1; |
d36057df KL |
268 | |
269 | /** | |
270 | * Y coordinate of the bottom edge of the desktop. | |
271 | */ | |
272 | private int desktopBottom; | |
273 | ||
274 | /** | |
275 | * An optional TDesktop background window that is drawn underneath | |
276 | * everything else. | |
277 | */ | |
278 | private TDesktop desktop; | |
279 | ||
280 | /** | |
281 | * If true, focus follows mouse: windows automatically raised if the | |
282 | * mouse passes over them. | |
283 | */ | |
284 | private boolean focusFollowsMouse = false; | |
285 | ||
80b1b7b5 KL |
286 | /** |
287 | * If true, display a text-based mouse cursor. | |
288 | */ | |
289 | private boolean textMouse = true; | |
290 | ||
291 | /** | |
292 | * If true, hide the mouse after typing a keystroke. | |
293 | */ | |
294 | private boolean hideMouseWhenTyping = false; | |
295 | ||
296 | /** | |
297 | * If true, the mouse should not be displayed because a keystroke was | |
298 | * typed. | |
299 | */ | |
300 | private boolean typingHidMouse = false; | |
301 | ||
2bb26984 KL |
302 | /** |
303 | * If true, hide the status bar. | |
304 | */ | |
305 | private boolean hideStatusBar = false; | |
306 | ||
307 | /** | |
308 | * If true, hide the menu bar. | |
309 | */ | |
310 | private boolean hideMenuBar = false; | |
311 | ||
a69ed767 KL |
312 | /** |
313 | * The list of commands to run before the next I/O check. | |
314 | */ | |
315 | private List<Runnable> invokeLaters = new LinkedList<Runnable>(); | |
316 | ||
ea544542 KL |
317 | /** |
318 | * The last time the screen was resized. | |
319 | */ | |
320 | private long screenResizeTime = 0; | |
321 | ||
54eaded0 KL |
322 | /** |
323 | * If true, screen selection is a rectangle. | |
324 | */ | |
325 | private boolean screenSelectionRectangle = false; | |
326 | ||
327 | /** | |
328 | * If true, the mouse is dragging a screen selection. | |
329 | */ | |
330 | private boolean inScreenSelection = false; | |
331 | ||
332 | /** | |
333 | * Screen selection starting X. | |
334 | */ | |
335 | private int screenSelectionX0; | |
336 | ||
337 | /** | |
338 | * Screen selection starting Y. | |
339 | */ | |
340 | private int screenSelectionY0; | |
341 | ||
342 | /** | |
343 | * Screen selection ending X. | |
344 | */ | |
345 | private int screenSelectionX1; | |
346 | ||
347 | /** | |
348 | * Screen selection ending Y. | |
349 | */ | |
350 | private int screenSelectionY1; | |
351 | ||
4941d2d6 KL |
352 | /** |
353 | * The help file data. Note package private access. | |
354 | */ | |
355 | HelpFile helpFile; | |
356 | ||
357 | /** | |
358 | * The stack of help topics. Note package private access. | |
359 | */ | |
360 | ArrayList<Topic> helpTopics = new ArrayList<Topic>(); | |
361 | ||
c6940ed9 KL |
362 | /** |
363 | * WidgetEventHandler is the main event consumer loop. There are at most | |
364 | * two such threads in existence: the primary for normal case and a | |
365 | * secondary that is used for TMessageBox, TInputBox, and similar. | |
366 | */ | |
367 | private class WidgetEventHandler implements Runnable { | |
368 | /** | |
369 | * The main application. | |
370 | */ | |
371 | private TApplication application; | |
372 | ||
373 | /** | |
374 | * Whether or not this WidgetEventHandler is the primary or secondary | |
375 | * thread. | |
376 | */ | |
377 | private boolean primary = true; | |
378 | ||
379 | /** | |
380 | * Public constructor. | |
381 | * | |
382 | * @param application the main application | |
383 | * @param primary if true, this is the primary event handler thread | |
384 | */ | |
385 | public WidgetEventHandler(final TApplication application, | |
386 | final boolean primary) { | |
387 | ||
388 | this.application = application; | |
389 | this.primary = primary; | |
390 | } | |
391 | ||
392 | /** | |
393 | * The consumer loop. | |
394 | */ | |
395 | public void run() { | |
a69ed767 KL |
396 | // Wrap everything in a try, so that if we go belly up we can let |
397 | // the user have their terminal back. | |
398 | try { | |
399 | runImpl(); | |
400 | } catch (Throwable t) { | |
401 | this.application.restoreConsole(); | |
402 | t.printStackTrace(); | |
403 | this.application.exit(); | |
404 | } | |
405 | } | |
406 | ||
407 | /** | |
408 | * The consumer loop. | |
409 | */ | |
410 | private void runImpl() { | |
be72cb5c | 411 | boolean first = true; |
c6940ed9 KL |
412 | |
413 | // Loop forever | |
414 | while (!application.quit) { | |
415 | ||
416 | // Wait until application notifies me | |
417 | while (!application.quit) { | |
418 | try { | |
419 | synchronized (application.drainEventQueue) { | |
420 | if (application.drainEventQueue.size() > 0) { | |
421 | break; | |
422 | } | |
423 | } | |
92554d64 | 424 | |
be72cb5c KL |
425 | long timeout = 0; |
426 | if (first) { | |
427 | first = false; | |
428 | } else { | |
429 | timeout = application.getSleepTime(1000); | |
430 | } | |
431 | ||
432 | if (timeout == 0) { | |
433 | // A timer needs to fire, break out. | |
434 | break; | |
435 | } | |
92554d64 | 436 | |
be72cb5c | 437 | if (debugThreads) { |
a69ed767 | 438 | System.err.printf("%d %s %s %s sleep %d millis\n", |
be72cb5c | 439 | System.currentTimeMillis(), this, |
a69ed767 KL |
440 | primary ? "primary" : "secondary", |
441 | Thread.currentThread(), timeout); | |
be72cb5c | 442 | } |
92554d64 | 443 | |
be72cb5c KL |
444 | synchronized (this) { |
445 | this.wait(timeout); | |
446 | } | |
92554d64 | 447 | |
be72cb5c | 448 | if (debugThreads) { |
a69ed767 | 449 | System.err.printf("%d %s %s %s AWAKE\n", |
be72cb5c | 450 | System.currentTimeMillis(), this, |
a69ed767 KL |
451 | primary ? "primary" : "secondary", |
452 | Thread.currentThread()); | |
be72cb5c KL |
453 | } |
454 | ||
455 | if ((!primary) | |
456 | && (application.secondaryEventReceiver == null) | |
457 | ) { | |
458 | // Secondary thread, emergency exit. If we got | |
459 | // here then something went wrong with the | |
460 | // handoff between yield() and closeWindow(). | |
461 | synchronized (application.primaryEventHandler) { | |
462 | application.primaryEventHandler.notify(); | |
c6940ed9 | 463 | } |
be72cb5c KL |
464 | application.secondaryEventHandler = null; |
465 | throw new RuntimeException("secondary exited " + | |
466 | "at wrong time"); | |
c6940ed9 | 467 | } |
be72cb5c | 468 | break; |
c6940ed9 KL |
469 | } catch (InterruptedException e) { |
470 | // SQUASH | |
471 | } | |
be72cb5c | 472 | } // while (!application.quit) |
ef368bd0 | 473 | |
c6940ed9 KL |
474 | // Pull all events off the queue |
475 | for (;;) { | |
476 | TInputEvent event = null; | |
477 | synchronized (application.drainEventQueue) { | |
478 | if (application.drainEventQueue.size() == 0) { | |
479 | break; | |
480 | } | |
481 | event = application.drainEventQueue.remove(0); | |
482 | } | |
be72cb5c KL |
483 | |
484 | // We will have an event to process, so repaint the | |
485 | // screen at the end. | |
bd8d51fa | 486 | application.repaint = true; |
be72cb5c | 487 | |
c6940ed9 KL |
488 | if (primary) { |
489 | primaryHandleEvent(event); | |
490 | } else { | |
491 | secondaryHandleEvent(event); | |
492 | } | |
493 | if ((!primary) | |
494 | && (application.secondaryEventReceiver == null) | |
495 | ) { | |
99144c71 KL |
496 | // Secondary thread, time to exit. |
497 | ||
a69ed767 KL |
498 | // Eliminate my reference so that wakeEventHandler() |
499 | // resumes working on the primary. | |
500 | application.secondaryEventHandler = null; | |
501 | ||
b9724916 KL |
502 | // We are ready to exit, wake up the primary thread. |
503 | // Remember that it is currently sleeping inside its | |
504 | // primaryHandleEvent(). | |
92554d64 KL |
505 | synchronized (application.primaryEventHandler) { |
506 | application.primaryEventHandler.notify(); | |
507 | } | |
92554d64 KL |
508 | |
509 | // All done! | |
c6940ed9 KL |
510 | return; |
511 | } | |
92554d64 | 512 | |
be72cb5c | 513 | } // for (;;) |
ef368bd0 | 514 | |
be72cb5c KL |
515 | // Fire timers, update screen. |
516 | if (!quit) { | |
517 | application.finishEventProcessing(); | |
c6940ed9 | 518 | } |
92554d64 | 519 | |
c6940ed9 KL |
520 | } // while (true) (main runnable loop) |
521 | } | |
522 | } | |
523 | ||
d14e2d78 KL |
524 | /** |
525 | * ScreenHandler pushes screen updates to the physical device. | |
526 | */ | |
527 | private class ScreenHandler implements Runnable { | |
528 | /** | |
529 | * The main application. | |
530 | */ | |
531 | private TApplication application; | |
532 | ||
533 | /** | |
534 | * The dirty flag. | |
535 | */ | |
536 | private boolean dirty = false; | |
537 | ||
538 | /** | |
539 | * Public constructor. | |
540 | * | |
541 | * @param application the main application | |
542 | */ | |
543 | public ScreenHandler(final TApplication application) { | |
544 | this.application = application; | |
545 | } | |
546 | ||
547 | /** | |
548 | * The screen update loop. | |
549 | */ | |
550 | public void run() { | |
551 | // Wrap everything in a try, so that if we go belly up we can let | |
552 | // the user have their terminal back. | |
553 | try { | |
554 | runImpl(); | |
555 | } catch (Throwable t) { | |
556 | this.application.restoreConsole(); | |
557 | t.printStackTrace(); | |
558 | this.application.exit(); | |
559 | } | |
560 | } | |
561 | ||
562 | /** | |
563 | * The update loop. | |
564 | */ | |
565 | private void runImpl() { | |
566 | ||
567 | // Loop forever | |
568 | while (!application.quit) { | |
569 | ||
570 | // Wait until application notifies me | |
571 | while (!application.quit) { | |
572 | try { | |
573 | synchronized (this) { | |
574 | if (dirty) { | |
575 | dirty = false; | |
576 | break; | |
577 | } | |
578 | ||
579 | // Always check within 50 milliseconds. | |
580 | this.wait(50); | |
581 | } | |
582 | } catch (InterruptedException e) { | |
583 | // SQUASH | |
584 | } | |
585 | } // while (!application.quit) | |
586 | ||
e820d5dd | 587 | // Flush the screen contents |
d14e2d78 KL |
588 | if (debugThreads) { |
589 | System.err.printf("%d %s backend.flushScreen()\n", | |
590 | System.currentTimeMillis(), Thread.currentThread()); | |
591 | } | |
592 | synchronized (getScreen()) { | |
593 | backend.flushScreen(); | |
594 | } | |
595 | } // while (true) (main runnable loop) | |
596 | ||
597 | // Shutdown the user I/O thread(s) | |
598 | backend.shutdown(); | |
599 | } | |
600 | ||
601 | /** | |
602 | * Set the dirty flag. | |
603 | */ | |
604 | public void setDirty() { | |
605 | synchronized (this) { | |
606 | dirty = true; | |
607 | } | |
608 | } | |
609 | ||
610 | } | |
611 | ||
d36057df KL |
612 | // ------------------------------------------------------------------------ |
613 | // Constructors ----------------------------------------------------------- | |
614 | // ------------------------------------------------------------------------ | |
c6940ed9 KL |
615 | |
616 | /** | |
d36057df KL |
617 | * Public constructor. |
618 | * | |
619 | * @param backendType BackendType.XTERM, BackendType.ECMA48 or | |
620 | * BackendType.SWING | |
621 | * @param windowWidth the number of text columns to start with | |
622 | * @param windowHeight the number of text rows to start with | |
623 | * @param fontSize the size in points | |
624 | * @throws UnsupportedEncodingException if an exception is thrown when | |
625 | * creating the InputStreamReader | |
c6940ed9 | 626 | */ |
d36057df KL |
627 | public TApplication(final BackendType backendType, final int windowWidth, |
628 | final int windowHeight, final int fontSize) | |
629 | throws UnsupportedEncodingException { | |
c6940ed9 | 630 | |
d36057df KL |
631 | switch (backendType) { |
632 | case SWING: | |
633 | backend = new SwingBackend(this, windowWidth, windowHeight, | |
634 | fontSize); | |
635 | break; | |
636 | case XTERM: | |
637 | // Fall through... | |
638 | case ECMA48: | |
639 | backend = new ECMA48Backend(this, null, null, windowWidth, | |
640 | windowHeight, fontSize); | |
641 | break; | |
642 | default: | |
643 | throw new IllegalArgumentException("Invalid backend type: " | |
644 | + backendType); | |
645 | } | |
646 | TApplicationImpl(); | |
647 | } | |
c6940ed9 | 648 | |
92554d64 | 649 | /** |
d36057df KL |
650 | * Public constructor. |
651 | * | |
652 | * @param backendType BackendType.XTERM, BackendType.ECMA48 or | |
653 | * BackendType.SWING | |
654 | * @throws UnsupportedEncodingException if an exception is thrown when | |
655 | * creating the InputStreamReader | |
92554d64 | 656 | */ |
d36057df KL |
657 | public TApplication(final BackendType backendType) |
658 | throws UnsupportedEncodingException { | |
b2d49e0f | 659 | |
d36057df KL |
660 | switch (backendType) { |
661 | case SWING: | |
662 | // The default SwingBackend is 80x25, 20 pt font. If you want to | |
663 | // change that, you can pass the extra arguments to the | |
664 | // SwingBackend constructor here. For example, if you wanted | |
665 | // 90x30, 16 pt font: | |
666 | // | |
667 | // backend = new SwingBackend(this, 90, 30, 16); | |
668 | backend = new SwingBackend(this); | |
669 | break; | |
670 | case XTERM: | |
671 | // Fall through... | |
672 | case ECMA48: | |
673 | backend = new ECMA48Backend(this, null, null); | |
674 | break; | |
675 | default: | |
676 | throw new IllegalArgumentException("Invalid backend type: " | |
677 | + backendType); | |
92554d64 | 678 | } |
d36057df | 679 | TApplicationImpl(); |
92554d64 KL |
680 | } |
681 | ||
7d4115a5 | 682 | /** |
d36057df | 683 | * Public constructor. The backend type will be BackendType.ECMA48. |
55d2b2c2 | 684 | * |
d36057df KL |
685 | * @param input an InputStream connected to the remote user, or null for |
686 | * System.in. If System.in is used, then on non-Windows systems it will | |
687 | * be put in raw mode; shutdown() will (blindly!) put System.in in cooked | |
688 | * mode. input is always converted to a Reader with UTF-8 encoding. | |
689 | * @param output an OutputStream connected to the remote user, or null | |
690 | * for System.out. output is always converted to a Writer with UTF-8 | |
691 | * encoding. | |
692 | * @throws UnsupportedEncodingException if an exception is thrown when | |
693 | * creating the InputStreamReader | |
55d2b2c2 | 694 | */ |
d36057df KL |
695 | public TApplication(final InputStream input, |
696 | final OutputStream output) throws UnsupportedEncodingException { | |
697 | ||
698 | backend = new ECMA48Backend(this, input, output); | |
699 | TApplicationImpl(); | |
55d2b2c2 KL |
700 | } |
701 | ||
48e27807 | 702 | /** |
d36057df | 703 | * Public constructor. The backend type will be BackendType.ECMA48. |
48e27807 | 704 | * |
d36057df KL |
705 | * @param input the InputStream underlying 'reader'. Its available() |
706 | * method is used to determine if reader.read() will block or not. | |
707 | * @param reader a Reader connected to the remote user. | |
708 | * @param writer a PrintWriter connected to the remote user. | |
709 | * @param setRawMode if true, set System.in into raw mode with stty. | |
710 | * This should in general not be used. It is here solely for Demo3, | |
711 | * which uses System.in. | |
712 | * @throws IllegalArgumentException if input, reader, or writer are null. | |
48e27807 | 713 | */ |
d36057df KL |
714 | public TApplication(final InputStream input, final Reader reader, |
715 | final PrintWriter writer, final boolean setRawMode) { | |
716 | ||
717 | backend = new ECMA48Backend(this, input, reader, writer, setRawMode); | |
718 | TApplicationImpl(); | |
48e27807 KL |
719 | } |
720 | ||
4328bb42 | 721 | /** |
d36057df KL |
722 | * Public constructor. The backend type will be BackendType.ECMA48. |
723 | * | |
724 | * @param input the InputStream underlying 'reader'. Its available() | |
725 | * method is used to determine if reader.read() will block or not. | |
726 | * @param reader a Reader connected to the remote user. | |
727 | * @param writer a PrintWriter connected to the remote user. | |
728 | * @throws IllegalArgumentException if input, reader, or writer are null. | |
4328bb42 | 729 | */ |
d36057df KL |
730 | public TApplication(final InputStream input, final Reader reader, |
731 | final PrintWriter writer) { | |
4328bb42 | 732 | |
d36057df KL |
733 | this(input, reader, writer, false); |
734 | } | |
4328bb42 | 735 | |
bd8d51fa | 736 | /** |
d36057df KL |
737 | * Public constructor. This hook enables use with new non-Jexer |
738 | * backends. | |
739 | * | |
740 | * @param backend a Backend that is already ready to go. | |
bd8d51fa | 741 | */ |
d36057df KL |
742 | public TApplication(final Backend backend) { |
743 | this.backend = backend; | |
744 | backend.setListener(this); | |
745 | TApplicationImpl(); | |
746 | } | |
bd8d51fa KL |
747 | |
748 | /** | |
d36057df | 749 | * Finish construction once the backend is set. |
bd8d51fa | 750 | */ |
d36057df | 751 | private void TApplicationImpl() { |
2bb26984 KL |
752 | // Text block mouse option |
753 | if (System.getProperty("jexer.textMouse", "true").equals("false")) { | |
754 | textMouse = false; | |
755 | } | |
756 | ||
757 | // Hide mouse when typing option | |
758 | if (System.getProperty("jexer.hideMouseWhenTyping", | |
759 | "false").equals("true")) { | |
760 | ||
761 | hideMouseWhenTyping = true; | |
762 | } | |
763 | ||
764 | // Hide status bar option | |
765 | if (System.getProperty("jexer.hideStatusBar", | |
766 | "false").equals("true")) { | |
767 | hideStatusBar = true; | |
768 | } | |
769 | ||
770 | // Hide menu bar option | |
771 | if (System.getProperty("jexer.hideMenuBar", "false").equals("true")) { | |
772 | hideMenuBar = true; | |
773 | } | |
774 | ||
d36057df | 775 | theme = new ColorTheme(); |
2bb26984 KL |
776 | desktopTop = (hideMenuBar ? 0 : 1); |
777 | desktopBottom = getScreen().getHeight() - 1 + (hideStatusBar ? 1 : 0); | |
a69ed767 KL |
778 | fillEventQueue = new LinkedList<TInputEvent>(); |
779 | drainEventQueue = new LinkedList<TInputEvent>(); | |
d36057df | 780 | windows = new LinkedList<TWindow>(); |
a69ed767 KL |
781 | menus = new ArrayList<TMenu>(); |
782 | subMenus = new ArrayList<TMenu>(); | |
d36057df KL |
783 | timers = new LinkedList<TTimer>(); |
784 | accelerators = new HashMap<TKeypress, TMenuItem>(); | |
a69ed767 | 785 | menuItems = new LinkedList<TMenuItem>(); |
d36057df | 786 | desktop = new TDesktop(this); |
bd8d51fa | 787 | |
d36057df KL |
788 | // Special case: the Swing backend needs to have a timer to drive its |
789 | // blink state. | |
790 | if ((backend instanceof SwingBackend) | |
791 | || (backend instanceof MultiBackend) | |
792 | ) { | |
793 | // Default to 500 millis, unless a SwingBackend has its own | |
794 | // value. | |
795 | long millis = 500; | |
796 | if (backend instanceof SwingBackend) { | |
797 | millis = ((SwingBackend) backend).getBlinkMillis(); | |
798 | } | |
799 | if (millis > 0) { | |
800 | addTimer(millis, true, | |
801 | new TAction() { | |
802 | public void DO() { | |
803 | TApplication.this.doRepaint(); | |
804 | } | |
805 | } | |
806 | ); | |
807 | } | |
808 | } | |
80b1b7b5 | 809 | |
4941d2d6 KL |
810 | // Load the help system |
811 | try { | |
812 | ClassLoader loader = Thread.currentThread().getContextClassLoader(); | |
813 | helpFile = new HelpFile(); | |
814 | helpFile.load(loader.getResourceAsStream("help.xml")); | |
815 | } catch (Exception e) { | |
816 | new TExceptionDialog(this, e); | |
817 | } | |
d36057df | 818 | } |
b6faeac0 | 819 | |
d36057df KL |
820 | // ------------------------------------------------------------------------ |
821 | // Runnable --------------------------------------------------------------- | |
822 | // ------------------------------------------------------------------------ | |
b6faeac0 | 823 | |
4328bb42 | 824 | /** |
d36057df | 825 | * Run this application until it exits. |
4328bb42 | 826 | */ |
d36057df | 827 | public void run() { |
abb84744 KL |
828 | // System.err.println("*** TApplication.run() begins ***"); |
829 | ||
d14e2d78 KL |
830 | // Start the screen updater thread |
831 | screenHandler = new ScreenHandler(this); | |
832 | (new Thread(screenHandler)).start(); | |
833 | ||
d36057df KL |
834 | // Start the main consumer thread |
835 | primaryEventHandler = new WidgetEventHandler(this, true); | |
836 | (new Thread(primaryEventHandler)).start(); | |
8e688b92 | 837 | |
d36057df | 838 | started = true; |
4328bb42 | 839 | |
d36057df KL |
840 | while (!quit) { |
841 | synchronized (this) { | |
842 | boolean doWait = false; | |
fca67db0 | 843 | |
d36057df KL |
844 | if (!backend.hasEvents()) { |
845 | synchronized (fillEventQueue) { | |
846 | if (fillEventQueue.size() == 0) { | |
847 | doWait = true; | |
848 | } | |
849 | } | |
850 | } | |
fca67db0 | 851 | |
d36057df KL |
852 | if (doWait) { |
853 | // No I/O to dispatch, so wait until the backend | |
854 | // provides new I/O. | |
855 | try { | |
856 | if (debugThreads) { | |
857 | System.err.println(System.currentTimeMillis() + | |
a69ed767 | 858 | " " + Thread.currentThread() + " MAIN sleep"); |
d36057df | 859 | } |
fca67db0 | 860 | |
d36057df | 861 | this.wait(); |
e826b451 | 862 | |
d36057df KL |
863 | if (debugThreads) { |
864 | System.err.println(System.currentTimeMillis() + | |
a69ed767 | 865 | " " + Thread.currentThread() + " MAIN AWAKE"); |
d36057df KL |
866 | } |
867 | } catch (InterruptedException e) { | |
868 | // I'm awake and don't care why, let's see what's | |
869 | // going on out there. | |
870 | } | |
871 | } | |
efb7af1f | 872 | |
d36057df | 873 | } // synchronized (this) |
7b5261bc | 874 | |
d36057df KL |
875 | synchronized (fillEventQueue) { |
876 | // Pull any pending I/O events | |
877 | backend.getEvents(fillEventQueue); | |
4328bb42 | 878 | |
d36057df KL |
879 | // Dispatch each event to the appropriate handler, one at a |
880 | // time. | |
881 | for (;;) { | |
882 | TInputEvent event = null; | |
883 | if (fillEventQueue.size() == 0) { | |
884 | break; | |
885 | } | |
886 | event = fillEventQueue.remove(0); | |
887 | metaHandleEvent(event); | |
888 | } | |
889 | } | |
a06459bd | 890 | |
d36057df KL |
891 | // Wake a consumer thread if we have any pending events. |
892 | if (drainEventQueue.size() > 0) { | |
893 | wakeEventHandler(); | |
894 | } | |
92453213 | 895 | |
d36057df | 896 | } // while (!quit) |
d502a0e9 | 897 | |
d36057df KL |
898 | // Shutdown the event consumer threads |
899 | if (secondaryEventHandler != null) { | |
900 | synchronized (secondaryEventHandler) { | |
901 | secondaryEventHandler.notify(); | |
902 | } | |
903 | } | |
904 | if (primaryEventHandler != null) { | |
905 | synchronized (primaryEventHandler) { | |
906 | primaryEventHandler.notify(); | |
907 | } | |
908 | } | |
b2d49e0f | 909 | |
d36057df KL |
910 | // Close all the windows. This gives them an opportunity to release |
911 | // resources. | |
912 | closeAllWindows(); | |
4328bb42 | 913 | |
955c55b7 KL |
914 | // Close the desktop. |
915 | if (desktop != null) { | |
916 | setDesktop(null); | |
917 | } | |
918 | ||
abb84744 KL |
919 | // Give the overarching application an opportunity to release |
920 | // resources. | |
921 | onExit(); | |
922 | ||
923 | // System.err.println("*** TApplication.run() exits ***"); | |
be72cb5c KL |
924 | } |
925 | ||
d36057df KL |
926 | // ------------------------------------------------------------------------ |
927 | // Event handlers --------------------------------------------------------- | |
928 | // ------------------------------------------------------------------------ | |
48e27807 KL |
929 | |
930 | /** | |
d36057df KL |
931 | * Method that TApplication subclasses can override to handle menu or |
932 | * posted command events. | |
48e27807 | 933 | * |
d36057df KL |
934 | * @param command command event |
935 | * @return if true, this event was consumed | |
48e27807 | 936 | */ |
d36057df KL |
937 | protected boolean onCommand(final TCommandEvent command) { |
938 | // Default: handle cmExit | |
939 | if (command.equals(cmExit)) { | |
940 | if (messageBox(i18n.getString("exitDialogTitle"), | |
941 | i18n.getString("exitDialogText"), | |
a69ed767 KL |
942 | TMessageBox.Type.YESNO).isYes()) { |
943 | ||
d36057df KL |
944 | exit(); |
945 | } | |
946 | return true; | |
947 | } | |
48e27807 | 948 | |
4941d2d6 KL |
949 | if (command.equals(cmHelp)) { |
950 | if (getActiveWindow() != null) { | |
951 | new THelpWindow(this, getActiveWindow().getHelpTopic()); | |
952 | } else { | |
953 | new THelpWindow(this); | |
954 | } | |
955 | return true; | |
956 | } | |
957 | ||
d36057df KL |
958 | if (command.equals(cmShell)) { |
959 | openTerminal(0, 0, TWindow.RESIZABLE); | |
960 | return true; | |
961 | } | |
4328bb42 | 962 | |
d36057df KL |
963 | if (command.equals(cmTile)) { |
964 | tileWindows(); | |
965 | return true; | |
966 | } | |
967 | if (command.equals(cmCascade)) { | |
968 | cascadeWindows(); | |
969 | return true; | |
970 | } | |
971 | if (command.equals(cmCloseAll)) { | |
972 | closeAllWindows(); | |
973 | return true; | |
974 | } | |
0ee88b6d | 975 | |
2bb26984 | 976 | if (command.equals(cmMenu) && (hideMenuBar == false)) { |
d36057df KL |
977 | if (!modalWindowActive() && (activeMenu == null)) { |
978 | if (menus.size() > 0) { | |
979 | menus.get(0).setActive(true); | |
980 | activeMenu = menus.get(0); | |
981 | return true; | |
982 | } | |
983 | } | |
0ee88b6d | 984 | } |
0ee88b6d | 985 | |
d36057df | 986 | return false; |
0ee88b6d KL |
987 | } |
988 | ||
92453213 | 989 | /** |
d36057df KL |
990 | * Method that TApplication subclasses can override to handle menu |
991 | * events. | |
92453213 | 992 | * |
d36057df KL |
993 | * @param menu menu event |
994 | * @return if true, this event was consumed | |
92453213 | 995 | */ |
d36057df | 996 | protected boolean onMenu(final TMenuEvent menu) { |
92453213 | 997 | |
d36057df KL |
998 | // Default: handle MID_EXIT |
999 | if (menu.getId() == TMenu.MID_EXIT) { | |
1000 | if (messageBox(i18n.getString("exitDialogTitle"), | |
1001 | i18n.getString("exitDialogText"), | |
a69ed767 KL |
1002 | TMessageBox.Type.YESNO).isYes()) { |
1003 | ||
d36057df KL |
1004 | exit(); |
1005 | } | |
1006 | return true; | |
1007 | } | |
92453213 | 1008 | |
4941d2d6 KL |
1009 | if (menu.getId() == TMenu.MID_HELP_HELP) { |
1010 | new THelpWindow(this, THelpWindow.HELP_HELP); | |
1011 | return true; | |
1012 | } | |
1013 | ||
1014 | if (menu.getId() == TMenu.MID_HELP_CONTENTS) { | |
1015 | new THelpWindow(this, helpFile.getTableOfContents()); | |
1016 | return true; | |
1017 | } | |
1018 | ||
1019 | if (menu.getId() == TMenu.MID_HELP_INDEX) { | |
1020 | new THelpWindow(this, helpFile.getIndex()); | |
1021 | return true; | |
1022 | } | |
1023 | ||
1024 | if (menu.getId() == TMenu.MID_HELP_SEARCH) { | |
1025 | TInputBox inputBox = inputBox(i18n. | |
1026 | getString("searchHelpInputBoxTitle"), | |
1027 | i18n.getString("searchHelpInputBoxCaption"), "", | |
1028 | TInputBox.Type.OKCANCEL); | |
1029 | if (inputBox.isOk()) { | |
1030 | new THelpWindow(this, | |
1031 | helpFile.getSearchResults(inputBox.getText())); | |
1032 | } | |
1033 | return true; | |
1034 | } | |
1035 | ||
1036 | if (menu.getId() == TMenu.MID_HELP_PREVIOUS) { | |
1037 | if (helpTopics.size() > 1) { | |
1038 | Topic previous = helpTopics.remove(helpTopics.size() - 2); | |
1039 | helpTopics.remove(helpTopics.size() - 1); | |
1040 | new THelpWindow(this, previous); | |
1041 | } else { | |
1042 | new THelpWindow(this, helpFile.getTableOfContents()); | |
1043 | } | |
1044 | return true; | |
1045 | } | |
1046 | ||
1047 | if (menu.getId() == TMenu.MID_HELP_ACTIVE_FILE) { | |
1048 | try { | |
1049 | List<String> filters = new ArrayList<String>(); | |
1050 | filters.add("^.*\\.[Xx][Mm][Ll]$"); | |
1051 | String filename = fileOpenBox(".", TFileOpenBox.Type.OPEN, | |
1052 | filters); | |
1053 | if (filename != null) { | |
1054 | helpTopics = new ArrayList<Topic>(); | |
1055 | helpFile = new HelpFile(); | |
1056 | helpFile.load(new FileInputStream(filename)); | |
1057 | } | |
1058 | } catch (Exception e) { | |
1059 | // Show this exception to the user. | |
1060 | new TExceptionDialog(this, e); | |
1061 | } | |
1062 | return true; | |
1063 | } | |
1064 | ||
d36057df KL |
1065 | if (menu.getId() == TMenu.MID_SHELL) { |
1066 | openTerminal(0, 0, TWindow.RESIZABLE); | |
1067 | return true; | |
1068 | } | |
72fca17b | 1069 | |
d36057df KL |
1070 | if (menu.getId() == TMenu.MID_TILE) { |
1071 | tileWindows(); | |
1072 | return true; | |
1073 | } | |
1074 | if (menu.getId() == TMenu.MID_CASCADE) { | |
1075 | cascadeWindows(); | |
1076 | return true; | |
1077 | } | |
1078 | if (menu.getId() == TMenu.MID_CLOSE_ALL) { | |
1079 | closeAllWindows(); | |
1080 | return true; | |
1081 | } | |
e23ea538 KL |
1082 | if (menu.getId() == TMenu.MID_ABOUT) { |
1083 | showAboutDialog(); | |
1084 | return true; | |
1085 | } | |
d36057df | 1086 | if (menu.getId() == TMenu.MID_REPAINT) { |
a69ed767 | 1087 | getScreen().clearPhysical(); |
d36057df KL |
1088 | doRepaint(); |
1089 | return true; | |
1090 | } | |
e23ea538 KL |
1091 | if (menu.getId() == TMenu.MID_VIEW_IMAGE) { |
1092 | openImage(); | |
1093 | return true; | |
1094 | } | |
a75902fa | 1095 | if (menu.getId() == TMenu.MID_SCREEN_OPTIONS) { |
e23ea538 KL |
1096 | new TFontChooserWindow(this); |
1097 | return true; | |
1098 | } | |
51e46b3e KL |
1099 | |
1100 | if (menu.getId() == TMenu.MID_CUT) { | |
1101 | postMenuEvent(new TCommandEvent(cmCut)); | |
1102 | return true; | |
1103 | } | |
1104 | if (menu.getId() == TMenu.MID_COPY) { | |
1105 | postMenuEvent(new TCommandEvent(cmCopy)); | |
1106 | return true; | |
1107 | } | |
1108 | if (menu.getId() == TMenu.MID_PASTE) { | |
1109 | postMenuEvent(new TCommandEvent(cmPaste)); | |
1110 | return true; | |
1111 | } | |
1112 | if (menu.getId() == TMenu.MID_CLEAR) { | |
1113 | postMenuEvent(new TCommandEvent(cmClear)); | |
1114 | return true; | |
1115 | } | |
1116 | ||
d36057df | 1117 | return false; |
72fca17b KL |
1118 | } |
1119 | ||
1120 | /** | |
d36057df | 1121 | * Method that TApplication subclasses can override to handle keystrokes. |
72fca17b | 1122 | * |
d36057df KL |
1123 | * @param keypress keystroke event |
1124 | * @return if true, this event was consumed | |
72fca17b | 1125 | */ |
d36057df KL |
1126 | protected boolean onKeypress(final TKeypressEvent keypress) { |
1127 | // Default: only menu shortcuts | |
72fca17b | 1128 | |
d36057df KL |
1129 | // Process Alt-F, Alt-E, etc. menu shortcut keys |
1130 | if (!keypress.getKey().isFnKey() | |
1131 | && keypress.getKey().isAlt() | |
1132 | && !keypress.getKey().isCtrl() | |
1133 | && (activeMenu == null) | |
1134 | && !modalWindowActive() | |
2bb26984 | 1135 | && (hideMenuBar == false) |
d36057df | 1136 | ) { |
2ce6dab2 | 1137 | |
d36057df | 1138 | assert (subMenus.size() == 0); |
2ce6dab2 | 1139 | |
d36057df KL |
1140 | for (TMenu menu: menus) { |
1141 | if (Character.toLowerCase(menu.getMnemonic().getShortcut()) | |
1142 | == Character.toLowerCase(keypress.getKey().getChar()) | |
1143 | ) { | |
1144 | activeMenu = menu; | |
1145 | menu.setActive(true); | |
1146 | return true; | |
1147 | } | |
1148 | } | |
1149 | } | |
1150 | ||
1151 | return false; | |
1152 | } | |
2ce6dab2 | 1153 | |
eb29bbb5 | 1154 | /** |
d36057df | 1155 | * Process background events, and update the screen. |
eb29bbb5 | 1156 | */ |
d36057df KL |
1157 | private void finishEventProcessing() { |
1158 | if (debugThreads) { | |
1159 | System.err.printf(System.currentTimeMillis() + " " + | |
1160 | Thread.currentThread() + " finishEventProcessing()\n"); | |
1161 | } | |
eb29bbb5 | 1162 | |
51e46b3e KL |
1163 | // See if we need to enable/disable the edit menu. |
1164 | EditMenuUser widget = null; | |
1165 | if (activeMenu == null) { | |
3d3e09e6 | 1166 | TWindow activeWindow = getActiveWindow(); |
51e46b3e KL |
1167 | if (activeWindow != null) { |
1168 | if (activeWindow.getActiveChild() instanceof EditMenuUser) { | |
1169 | widget = (EditMenuUser) activeWindow.getActiveChild(); | |
1170 | } | |
1171 | } else if (desktop != null) { | |
1172 | if (desktop.getActiveChild() instanceof EditMenuUser) { | |
1173 | widget = (EditMenuUser) desktop.getActiveChild(); | |
1174 | } | |
1175 | } | |
1176 | if (widget == null) { | |
1177 | disableMenuItem(TMenu.MID_CUT); | |
1178 | disableMenuItem(TMenu.MID_COPY); | |
1179 | disableMenuItem(TMenu.MID_PASTE); | |
1180 | disableMenuItem(TMenu.MID_CLEAR); | |
1181 | } else { | |
1182 | if (widget.isEditMenuCut()) { | |
1183 | enableMenuItem(TMenu.MID_CUT); | |
1184 | } else { | |
1185 | disableMenuItem(TMenu.MID_CUT); | |
1186 | } | |
1187 | if (widget.isEditMenuCopy()) { | |
1188 | enableMenuItem(TMenu.MID_COPY); | |
1189 | } else { | |
1190 | disableMenuItem(TMenu.MID_COPY); | |
1191 | } | |
1192 | if (widget.isEditMenuPaste()) { | |
1193 | enableMenuItem(TMenu.MID_PASTE); | |
1194 | } else { | |
1195 | disableMenuItem(TMenu.MID_PASTE); | |
1196 | } | |
1197 | if (widget.isEditMenuClear()) { | |
1198 | enableMenuItem(TMenu.MID_CLEAR); | |
1199 | } else { | |
1200 | disableMenuItem(TMenu.MID_CLEAR); | |
1201 | } | |
1202 | } | |
1203 | } | |
1204 | ||
d36057df KL |
1205 | // Process timers and call doIdle()'s |
1206 | doIdle(); | |
1207 | ||
1208 | // Update the screen | |
1209 | synchronized (getScreen()) { | |
1210 | drawAll(); | |
1211 | } | |
1212 | ||
d14e2d78 KL |
1213 | // Wake up the screen repainter |
1214 | wakeScreenHandler(); | |
1215 | ||
d36057df KL |
1216 | if (debugThreads) { |
1217 | System.err.printf(System.currentTimeMillis() + " " + | |
1218 | Thread.currentThread() + " finishEventProcessing() END\n"); | |
eb29bbb5 | 1219 | } |
eb29bbb5 KL |
1220 | } |
1221 | ||
4328bb42 | 1222 | /** |
d36057df KL |
1223 | * Peek at certain application-level events, add to eventQueue, and wake |
1224 | * up the consuming Thread. | |
4328bb42 | 1225 | * |
d36057df | 1226 | * @param event the input event to consume |
a4406f4e | 1227 | */ |
d36057df | 1228 | private void metaHandleEvent(final TInputEvent event) { |
a4406f4e | 1229 | |
d36057df KL |
1230 | if (debugEvents) { |
1231 | System.err.printf(String.format("metaHandleEvents event: %s\n", | |
1232 | event)); System.err.flush(); | |
a4406f4e | 1233 | } |
6985c572 | 1234 | |
d36057df KL |
1235 | if (quit) { |
1236 | // Do no more processing if the application is already trying | |
1237 | // to exit. | |
1238 | return; | |
1239 | } | |
30bd4abd | 1240 | |
d36057df | 1241 | // Special application-wide events ------------------------------- |
c6940ed9 | 1242 | |
d36057df KL |
1243 | // Abort everything |
1244 | if (event instanceof TCommandEvent) { | |
1245 | TCommandEvent command = (TCommandEvent) event; | |
abb84744 | 1246 | if (command.equals(cmAbort)) { |
d36057df KL |
1247 | exit(); |
1248 | return; | |
be72cb5c KL |
1249 | } |
1250 | } | |
4328bb42 | 1251 | |
d36057df KL |
1252 | synchronized (drainEventQueue) { |
1253 | // Screen resize | |
1254 | if (event instanceof TResizeEvent) { | |
1255 | TResizeEvent resize = (TResizeEvent) event; | |
1256 | synchronized (getScreen()) { | |
ea544542 KL |
1257 | if ((System.currentTimeMillis() - screenResizeTime >= 15) |
1258 | || (resize.getWidth() < getScreen().getWidth()) | |
1259 | || (resize.getHeight() < getScreen().getHeight()) | |
1260 | ) { | |
1261 | getScreen().setDimensions(resize.getWidth(), | |
1262 | resize.getHeight()); | |
1263 | screenResizeTime = System.currentTimeMillis(); | |
1264 | } | |
d36057df | 1265 | desktopBottom = getScreen().getHeight() - 1; |
2bb26984 KL |
1266 | if (hideStatusBar) { |
1267 | desktopBottom++; | |
1268 | } | |
d36057df KL |
1269 | mouseX = 0; |
1270 | mouseY = 0; | |
d36057df KL |
1271 | } |
1272 | if (desktop != null) { | |
2bc32111 KL |
1273 | desktop.setDimensions(0, desktopTop, resize.getWidth(), |
1274 | (desktopBottom - desktopTop)); | |
5434cb2b | 1275 | desktop.onResize(resize); |
d36057df | 1276 | } |
2ce6dab2 | 1277 | |
d36057df KL |
1278 | // Change menu edges if needed. |
1279 | recomputeMenuX(); | |
be72cb5c | 1280 | |
d36057df KL |
1281 | // We are dirty, redraw the screen. |
1282 | doRepaint(); | |
be72cb5c | 1283 | |
d36057df KL |
1284 | /* |
1285 | System.err.println("New screen: " + resize.getWidth() + | |
1286 | " x " + resize.getHeight()); | |
1287 | */ | |
1288 | return; | |
1289 | } | |
be72cb5c | 1290 | |
d36057df KL |
1291 | // Put into the main queue |
1292 | drainEventQueue.add(event); | |
be72cb5c KL |
1293 | } |
1294 | } | |
1295 | ||
4328bb42 | 1296 | /** |
d36057df KL |
1297 | * Dispatch one event to the appropriate widget or application-level |
1298 | * event handler. This is the primary event handler, it has the normal | |
1299 | * application-wide event handling. | |
bd8d51fa | 1300 | * |
d36057df KL |
1301 | * @param event the input event to consume |
1302 | * @see #secondaryHandleEvent(TInputEvent event) | |
4328bb42 | 1303 | */ |
d36057df KL |
1304 | private void primaryHandleEvent(final TInputEvent event) { |
1305 | ||
1306 | if (debugEvents) { | |
a69ed767 KL |
1307 | System.err.printf("%s primaryHandleEvent: %s\n", |
1308 | Thread.currentThread(), event); | |
7b5261bc | 1309 | } |
d36057df | 1310 | TMouseEvent doubleClick = null; |
4328bb42 | 1311 | |
d36057df | 1312 | // Special application-wide events ----------------------------------- |
339652cc | 1313 | |
80b1b7b5 KL |
1314 | if (event instanceof TKeypressEvent) { |
1315 | if (hideMouseWhenTyping) { | |
1316 | typingHidMouse = true; | |
1317 | } | |
1318 | } | |
1319 | ||
d36057df KL |
1320 | // Peek at the mouse position |
1321 | if (event instanceof TMouseEvent) { | |
80b1b7b5 KL |
1322 | typingHidMouse = false; |
1323 | ||
d36057df | 1324 | TMouseEvent mouse = (TMouseEvent) event; |
54eaded0 KL |
1325 | if (mouse.isMouse1() && (mouse.isShift() || mouse.isCtrl())) { |
1326 | // Screen selection. | |
1327 | if (inScreenSelection) { | |
1328 | screenSelectionX1 = mouse.getX(); | |
1329 | screenSelectionY1 = mouse.getY(); | |
1330 | } else { | |
1331 | inScreenSelection = true; | |
1332 | screenSelectionX0 = mouse.getX(); | |
1333 | screenSelectionY0 = mouse.getY(); | |
1334 | screenSelectionX1 = mouse.getX(); | |
1335 | screenSelectionY1 = mouse.getY(); | |
1336 | screenSelectionRectangle = mouse.isCtrl(); | |
1337 | } | |
1338 | } else { | |
1339 | if (inScreenSelection) { | |
1340 | getScreen().copySelection(clipboard, screenSelectionX0, | |
1341 | screenSelectionY0, screenSelectionX1, screenSelectionY1, | |
1342 | screenSelectionRectangle); | |
1343 | } | |
1344 | inScreenSelection = false; | |
1345 | } | |
1346 | ||
d36057df | 1347 | if ((mouseX != mouse.getX()) || (mouseY != mouse.getY())) { |
d36057df KL |
1348 | mouseX = mouse.getX(); |
1349 | mouseY = mouse.getY(); | |
1350 | } else { | |
e23ea538 KL |
1351 | if ((mouse.getType() == TMouseEvent.Type.MOUSE_DOWN) |
1352 | && (!mouse.isMouseWheelUp()) | |
1353 | && (!mouse.isMouseWheelDown()) | |
1354 | ) { | |
d36057df KL |
1355 | if ((mouse.getTime().getTime() - lastMouseUpTime) < |
1356 | doubleClickTime) { | |
99144c71 | 1357 | |
d36057df KL |
1358 | // This is a double-click. |
1359 | doubleClick = new TMouseEvent(TMouseEvent.Type. | |
1360 | MOUSE_DOUBLE_CLICK, | |
1361 | mouse.getX(), mouse.getY(), | |
1362 | mouse.getAbsoluteX(), mouse.getAbsoluteY(), | |
1363 | mouse.isMouse1(), mouse.isMouse2(), | |
1364 | mouse.isMouse3(), | |
6e9daafb KL |
1365 | mouse.isMouseWheelUp(), mouse.isMouseWheelDown(), |
1366 | mouse.isAlt(), mouse.isCtrl(), mouse.isShift()); | |
d36057df KL |
1367 | |
1368 | } else { | |
1369 | // The first click of a potential double-click. | |
1370 | lastMouseUpTime = mouse.getTime().getTime(); | |
1371 | } | |
1d14ffab | 1372 | } |
bd8d51fa | 1373 | } |
7b5261bc | 1374 | |
d36057df KL |
1375 | // See if we need to switch focus to another window or the menu |
1376 | checkSwitchFocus((TMouseEvent) event); | |
99144c71 KL |
1377 | } |
1378 | ||
d36057df KL |
1379 | // Handle menu events |
1380 | if ((activeMenu != null) && !(event instanceof TCommandEvent)) { | |
1381 | TMenu menu = activeMenu; | |
7b5261bc | 1382 | |
d36057df KL |
1383 | if (event instanceof TMouseEvent) { |
1384 | TMouseEvent mouse = (TMouseEvent) event; | |
7b5261bc | 1385 | |
d36057df KL |
1386 | while (subMenus.size() > 0) { |
1387 | TMenu subMenu = subMenus.get(subMenus.size() - 1); | |
1388 | if (subMenu.mouseWouldHit(mouse)) { | |
1389 | break; | |
1390 | } | |
1391 | if ((mouse.getType() == TMouseEvent.Type.MOUSE_MOTION) | |
1392 | && (!mouse.isMouse1()) | |
1393 | && (!mouse.isMouse2()) | |
1394 | && (!mouse.isMouse3()) | |
1395 | && (!mouse.isMouseWheelUp()) | |
1396 | && (!mouse.isMouseWheelDown()) | |
1397 | ) { | |
1398 | break; | |
1399 | } | |
1400 | // We navigated away from a sub-menu, so close it | |
1401 | closeSubMenu(); | |
1402 | } | |
7b5261bc | 1403 | |
d36057df KL |
1404 | // Convert the mouse relative x/y to menu coordinates |
1405 | assert (mouse.getX() == mouse.getAbsoluteX()); | |
1406 | assert (mouse.getY() == mouse.getAbsoluteY()); | |
1407 | if (subMenus.size() > 0) { | |
1408 | menu = subMenus.get(subMenus.size() - 1); | |
1409 | } | |
1410 | mouse.setX(mouse.getX() - menu.getX()); | |
1411 | mouse.setY(mouse.getY() - menu.getY()); | |
7b5261bc | 1412 | } |
d36057df KL |
1413 | menu.handleEvent(event); |
1414 | return; | |
7b5261bc | 1415 | } |
7b5261bc | 1416 | |
d36057df KL |
1417 | if (event instanceof TKeypressEvent) { |
1418 | TKeypressEvent keypress = (TKeypressEvent) event; | |
2ce6dab2 | 1419 | |
d36057df KL |
1420 | // See if this key matches an accelerator, and is not being |
1421 | // shortcutted by the active window, and if so dispatch the menu | |
1422 | // event. | |
1423 | boolean windowWillShortcut = false; | |
3d3e09e6 | 1424 | TWindow activeWindow = getActiveWindow(); |
d36057df KL |
1425 | if (activeWindow != null) { |
1426 | assert (activeWindow.isShown()); | |
1427 | if (activeWindow.isShortcutKeypress(keypress.getKey())) { | |
1428 | // We do not process this key, it will be passed to the | |
1429 | // window instead. | |
1430 | windowWillShortcut = true; | |
1431 | } | |
1432 | } | |
7b5261bc | 1433 | |
d36057df KL |
1434 | if (!windowWillShortcut && !modalWindowActive()) { |
1435 | TKeypress keypressLowercase = keypress.getKey().toLowerCase(); | |
1436 | TMenuItem item = null; | |
1437 | synchronized (accelerators) { | |
1438 | item = accelerators.get(keypressLowercase); | |
1439 | } | |
1440 | if (item != null) { | |
1441 | if (item.isEnabled()) { | |
1442 | // Let the menu item dispatch | |
1443 | item.dispatch(); | |
1444 | return; | |
339652cc | 1445 | } |
be72cb5c | 1446 | } |
d36057df KL |
1447 | |
1448 | // Handle the keypress | |
1449 | if (onKeypress(keypress)) { | |
1450 | return; | |
1451 | } | |
7b5261bc KL |
1452 | } |
1453 | } | |
1454 | ||
d36057df KL |
1455 | if (event instanceof TCommandEvent) { |
1456 | if (onCommand((TCommandEvent) event)) { | |
1457 | return; | |
1458 | } | |
7b5261bc | 1459 | } |
7b5261bc | 1460 | |
d36057df KL |
1461 | if (event instanceof TMenuEvent) { |
1462 | if (onMenu((TMenuEvent) event)) { | |
1463 | return; | |
1464 | } | |
1d14ffab | 1465 | } |
7b5261bc | 1466 | |
d36057df KL |
1467 | // Dispatch events to the active window ------------------------------- |
1468 | boolean dispatchToDesktop = true; | |
3d3e09e6 | 1469 | TWindow window = getActiveWindow(); |
d36057df KL |
1470 | if (window != null) { |
1471 | assert (window.isActive()); | |
1472 | assert (window.isShown()); | |
1473 | if (event instanceof TMouseEvent) { | |
1474 | TMouseEvent mouse = (TMouseEvent) event; | |
1475 | // Convert the mouse relative x/y to window coordinates | |
1476 | assert (mouse.getX() == mouse.getAbsoluteX()); | |
1477 | assert (mouse.getY() == mouse.getAbsoluteY()); | |
1478 | mouse.setX(mouse.getX() - window.getX()); | |
1479 | mouse.setY(mouse.getY() - window.getY()); | |
4328bb42 | 1480 | |
d36057df KL |
1481 | if (doubleClick != null) { |
1482 | doubleClick.setX(doubleClick.getX() - window.getX()); | |
1483 | doubleClick.setY(doubleClick.getY() - window.getY()); | |
1484 | } | |
2ce6dab2 | 1485 | |
d36057df KL |
1486 | if (window.mouseWouldHit(mouse)) { |
1487 | dispatchToDesktop = false; | |
1488 | } | |
1489 | } else if (event instanceof TKeypressEvent) { | |
1490 | dispatchToDesktop = false; | |
5ffeabcc KL |
1491 | } else if (event instanceof TMenuEvent) { |
1492 | dispatchToDesktop = false; | |
d36057df KL |
1493 | } |
1494 | ||
1495 | if (debugEvents) { | |
1496 | System.err.printf("TApplication dispatch event: %s\n", | |
1497 | event); | |
1498 | } | |
1499 | window.handleEvent(event); | |
1500 | if (doubleClick != null) { | |
1501 | window.handleEvent(doubleClick); | |
1502 | } | |
1503 | } | |
1504 | if (dispatchToDesktop) { | |
1505 | // This event is fair game for the desktop to process. | |
1506 | if (desktop != null) { | |
1507 | desktop.handleEvent(event); | |
1508 | if (doubleClick != null) { | |
1509 | desktop.handleEvent(doubleClick); | |
1510 | } | |
1511 | } | |
be72cb5c | 1512 | } |
42873e30 KL |
1513 | } |
1514 | ||
4328bb42 | 1515 | /** |
d36057df KL |
1516 | * Dispatch one event to the appropriate widget or application-level |
1517 | * event handler. This is the secondary event handler used by certain | |
1518 | * special dialogs (currently TMessageBox and TFileOpenBox). | |
1519 | * | |
1520 | * @param event the input event to consume | |
1521 | * @see #primaryHandleEvent(TInputEvent event) | |
4328bb42 | 1522 | */ |
d36057df KL |
1523 | private void secondaryHandleEvent(final TInputEvent event) { |
1524 | TMouseEvent doubleClick = null; | |
2027327c | 1525 | |
a69ed767 KL |
1526 | if (debugEvents) { |
1527 | System.err.printf("%s secondaryHandleEvent: %s\n", | |
1528 | Thread.currentThread(), event); | |
1529 | } | |
1530 | ||
d36057df KL |
1531 | // Peek at the mouse position |
1532 | if (event instanceof TMouseEvent) { | |
80b1b7b5 KL |
1533 | typingHidMouse = false; |
1534 | ||
d36057df KL |
1535 | TMouseEvent mouse = (TMouseEvent) event; |
1536 | if ((mouseX != mouse.getX()) || (mouseY != mouse.getY())) { | |
d36057df KL |
1537 | mouseX = mouse.getX(); |
1538 | mouseY = mouse.getY(); | |
1539 | } else { | |
e23ea538 KL |
1540 | if ((mouse.getType() == TMouseEvent.Type.MOUSE_DOWN) |
1541 | && (!mouse.isMouseWheelUp()) | |
1542 | && (!mouse.isMouseWheelDown()) | |
1543 | ) { | |
d36057df KL |
1544 | if ((mouse.getTime().getTime() - lastMouseUpTime) < |
1545 | doubleClickTime) { | |
b2d49e0f | 1546 | |
d36057df KL |
1547 | // This is a double-click. |
1548 | doubleClick = new TMouseEvent(TMouseEvent.Type. | |
1549 | MOUSE_DOUBLE_CLICK, | |
1550 | mouse.getX(), mouse.getY(), | |
1551 | mouse.getAbsoluteX(), mouse.getAbsoluteY(), | |
1552 | mouse.isMouse1(), mouse.isMouse2(), | |
1553 | mouse.isMouse3(), | |
6e9daafb KL |
1554 | mouse.isMouseWheelUp(), mouse.isMouseWheelDown(), |
1555 | mouse.isAlt(), mouse.isCtrl(), mouse.isShift()); | |
be72cb5c | 1556 | |
d36057df KL |
1557 | } else { |
1558 | // The first click of a potential double-click. | |
1559 | lastMouseUpTime = mouse.getTime().getTime(); | |
6358f6e5 | 1560 | } |
be72cb5c | 1561 | } |
d36057df KL |
1562 | } |
1563 | } | |
be72cb5c | 1564 | |
d36057df | 1565 | secondaryEventReceiver.handleEvent(event); |
5255f69c KL |
1566 | // Note that it is possible for secondaryEventReceiver to be null |
1567 | // now, because its handleEvent() might have finished out on the | |
1568 | // secondary thread. So put any extra processing inside a null | |
1569 | // check. | |
1570 | if (secondaryEventReceiver != null) { | |
1571 | if (doubleClick != null) { | |
1572 | secondaryEventReceiver.handleEvent(doubleClick); | |
1573 | } | |
d36057df KL |
1574 | } |
1575 | } | |
be72cb5c | 1576 | |
d36057df KL |
1577 | /** |
1578 | * Enable a widget to override the primary event thread. | |
1579 | * | |
1580 | * @param widget widget that will receive events | |
1581 | */ | |
1582 | public final void enableSecondaryEventReceiver(final TWidget widget) { | |
1583 | if (debugThreads) { | |
1584 | System.err.println(System.currentTimeMillis() + | |
1585 | " enableSecondaryEventReceiver()"); | |
1586 | } | |
be72cb5c | 1587 | |
d36057df KL |
1588 | assert (secondaryEventReceiver == null); |
1589 | assert (secondaryEventHandler == null); | |
1590 | assert ((widget instanceof TMessageBox) | |
1591 | || (widget instanceof TFileOpenBox)); | |
1592 | secondaryEventReceiver = widget; | |
1593 | secondaryEventHandler = new WidgetEventHandler(this, false); | |
1594 | ||
1595 | (new Thread(secondaryEventHandler)).start(); | |
1596 | } | |
1597 | ||
1598 | /** | |
1599 | * Yield to the secondary thread. | |
1600 | */ | |
1601 | public final void yield() { | |
a69ed767 KL |
1602 | if (debugThreads) { |
1603 | System.err.printf(System.currentTimeMillis() + " " + | |
1604 | Thread.currentThread() + " yield()\n"); | |
1605 | } | |
1606 | ||
d36057df KL |
1607 | assert (secondaryEventReceiver != null); |
1608 | ||
1609 | while (secondaryEventReceiver != null) { | |
1610 | synchronized (primaryEventHandler) { | |
1611 | try { | |
1612 | primaryEventHandler.wait(); | |
1613 | } catch (InterruptedException e) { | |
1614 | // SQUASH | |
8e688b92 | 1615 | } |
d36057df KL |
1616 | } |
1617 | } | |
1618 | } | |
7b5261bc | 1619 | |
d36057df KL |
1620 | /** |
1621 | * Do stuff when there is no user input. | |
1622 | */ | |
1623 | private void doIdle() { | |
1624 | if (debugThreads) { | |
1625 | System.err.printf(System.currentTimeMillis() + " " + | |
1626 | Thread.currentThread() + " doIdle()\n"); | |
1627 | } | |
ef368bd0 | 1628 | |
d36057df | 1629 | synchronized (timers) { |
8e688b92 | 1630 | |
d36057df KL |
1631 | if (debugThreads) { |
1632 | System.err.printf(System.currentTimeMillis() + " " + | |
1633 | Thread.currentThread() + " doIdle() 2\n"); | |
1634 | } | |
1635 | ||
1636 | // Run any timers that have timed out | |
1637 | Date now = new Date(); | |
1638 | List<TTimer> keepTimers = new LinkedList<TTimer>(); | |
1639 | for (TTimer timer: timers) { | |
1640 | if (timer.getNextTick().getTime() <= now.getTime()) { | |
1641 | // Something might change, so repaint the screen. | |
1642 | repaint = true; | |
1643 | timer.tick(); | |
1644 | if (timer.recurring) { | |
1645 | keepTimers.add(timer); | |
be72cb5c | 1646 | } |
d36057df KL |
1647 | } else { |
1648 | keepTimers.add(timer); | |
8e688b92 | 1649 | } |
8e688b92 | 1650 | } |
e394cb85 KL |
1651 | timers.clear(); |
1652 | timers.addAll(keepTimers); | |
d36057df | 1653 | } |
7b5261bc | 1654 | |
d36057df KL |
1655 | // Call onIdle's |
1656 | for (TWindow window: windows) { | |
1657 | window.onIdle(); | |
1658 | } | |
1659 | if (desktop != null) { | |
1660 | desktop.onIdle(); | |
1661 | } | |
a69ed767 | 1662 | |
3c5921e6 KL |
1663 | // Run any invokeLaters. We make a copy, and run that, because one |
1664 | // of these Runnables might add call TApplication.invokeLater(). | |
1665 | List<Runnable> invokes = new ArrayList<Runnable>(); | |
a69ed767 | 1666 | synchronized (invokeLaters) { |
3c5921e6 | 1667 | invokes.addAll(invokeLaters); |
a69ed767 KL |
1668 | invokeLaters.clear(); |
1669 | } | |
3c5921e6 KL |
1670 | for (Runnable invoke: invokes) { |
1671 | invoke.run(); | |
1672 | } | |
1673 | doRepaint(); | |
a69ed767 | 1674 | |
d36057df | 1675 | } |
92554d64 | 1676 | |
d36057df KL |
1677 | /** |
1678 | * Wake the sleeping active event handler. | |
1679 | */ | |
1680 | private void wakeEventHandler() { | |
1681 | if (!started) { | |
1682 | return; | |
1683 | } | |
92554d64 | 1684 | |
92554d64 KL |
1685 | if (secondaryEventHandler != null) { |
1686 | synchronized (secondaryEventHandler) { | |
1687 | secondaryEventHandler.notify(); | |
1688 | } | |
d36057df KL |
1689 | } else { |
1690 | assert (primaryEventHandler != null); | |
92554d64 KL |
1691 | synchronized (primaryEventHandler) { |
1692 | primaryEventHandler.notify(); | |
1693 | } | |
7b5261bc | 1694 | } |
d36057df | 1695 | } |
7b5261bc | 1696 | |
d14e2d78 KL |
1697 | /** |
1698 | * Wake the sleeping screen handler. | |
1699 | */ | |
1700 | private void wakeScreenHandler() { | |
1701 | if (!started) { | |
1702 | return; | |
1703 | } | |
1704 | ||
1705 | synchronized (screenHandler) { | |
1706 | screenHandler.notify(); | |
1707 | } | |
1708 | } | |
1709 | ||
d36057df KL |
1710 | // ------------------------------------------------------------------------ |
1711 | // TApplication ----------------------------------------------------------- | |
1712 | // ------------------------------------------------------------------------ | |
92554d64 | 1713 | |
a69ed767 KL |
1714 | /** |
1715 | * Place a command on the run queue, and run it before the next round of | |
1716 | * checking I/O. | |
1717 | * | |
1718 | * @param command the command to run later | |
1719 | */ | |
1720 | public void invokeLater(final Runnable command) { | |
1721 | synchronized (invokeLaters) { | |
1722 | invokeLaters.add(command); | |
1723 | } | |
e23ea538 | 1724 | doRepaint(); |
a69ed767 KL |
1725 | } |
1726 | ||
1727 | /** | |
1728 | * Restore the console to sane defaults. This is meant to be used for | |
1729 | * improper exits (e.g. a caught exception in main()), and should not be | |
1730 | * necessary for normal program termination. | |
1731 | */ | |
1732 | public void restoreConsole() { | |
1733 | if (backend != null) { | |
1734 | if (backend instanceof ECMA48Backend) { | |
1735 | backend.shutdown(); | |
1736 | } | |
1737 | } | |
1738 | } | |
1739 | ||
d36057df KL |
1740 | /** |
1741 | * Get the Backend. | |
1742 | * | |
1743 | * @return the Backend | |
1744 | */ | |
1745 | public final Backend getBackend() { | |
1746 | return backend; | |
4328bb42 KL |
1747 | } |
1748 | ||
1749 | /** | |
d36057df | 1750 | * Get the Screen. |
4328bb42 | 1751 | * |
d36057df | 1752 | * @return the Screen |
4328bb42 | 1753 | */ |
d36057df KL |
1754 | public final Screen getScreen() { |
1755 | if (backend instanceof TWindowBackend) { | |
1756 | // We are being rendered to a TWindow. We can't use its | |
1757 | // getScreen() method because that is how it is rendering to a | |
1758 | // hardware backend somewhere. Instead use its getOtherScreen() | |
1759 | // method. | |
1760 | return ((TWindowBackend) backend).getOtherScreen(); | |
1761 | } else { | |
1762 | return backend.getScreen(); | |
8e688b92 | 1763 | } |
d36057df | 1764 | } |
7b5261bc | 1765 | |
d36057df KL |
1766 | /** |
1767 | * Get the color theme. | |
1768 | * | |
1769 | * @return the theme | |
1770 | */ | |
1771 | public final ColorTheme getTheme() { | |
1772 | return theme; | |
1773 | } | |
7b5261bc | 1774 | |
51e46b3e KL |
1775 | /** |
1776 | * Get the clipboard. | |
1777 | * | |
1778 | * @return the clipboard | |
1779 | */ | |
1780 | public final Clipboard getClipboard() { | |
1781 | return clipboard; | |
1782 | } | |
1783 | ||
d36057df KL |
1784 | /** |
1785 | * Repaint the screen on the next update. | |
1786 | */ | |
1787 | public void doRepaint() { | |
1788 | repaint = true; | |
1789 | wakeEventHandler(); | |
1790 | } | |
68c5cd6b | 1791 | |
d36057df KL |
1792 | /** |
1793 | * Get Y coordinate of the top edge of the desktop. | |
1794 | * | |
1795 | * @return Y coordinate of the top edge of the desktop | |
1796 | */ | |
1797 | public final int getDesktopTop() { | |
1798 | return desktopTop; | |
1799 | } | |
68c5cd6b | 1800 | |
d36057df KL |
1801 | /** |
1802 | * Get Y coordinate of the bottom edge of the desktop. | |
1803 | * | |
1804 | * @return Y coordinate of the bottom edge of the desktop | |
1805 | */ | |
1806 | public final int getDesktopBottom() { | |
1807 | return desktopBottom; | |
1808 | } | |
7b5261bc | 1809 | |
d36057df KL |
1810 | /** |
1811 | * Set the TDesktop instance. | |
1812 | * | |
1813 | * @param desktop a TDesktop instance, or null to remove the one that is | |
1814 | * set | |
1815 | */ | |
1816 | public final void setDesktop(final TDesktop desktop) { | |
1817 | if (this.desktop != null) { | |
955c55b7 KL |
1818 | this.desktop.onPreClose(); |
1819 | this.desktop.onUnfocus(); | |
d36057df | 1820 | this.desktop.onClose(); |
be72cb5c | 1821 | } |
d36057df | 1822 | this.desktop = desktop; |
4328bb42 KL |
1823 | } |
1824 | ||
a06459bd | 1825 | /** |
d36057df | 1826 | * Get the TDesktop instance. |
a06459bd | 1827 | * |
d36057df | 1828 | * @return the desktop, or null if it is not set |
a06459bd | 1829 | */ |
d36057df KL |
1830 | public final TDesktop getDesktop() { |
1831 | return desktop; | |
1832 | } | |
fca67db0 | 1833 | |
d36057df KL |
1834 | /** |
1835 | * Get the current active window. | |
1836 | * | |
1837 | * @return the active window, or null if it is not set | |
1838 | */ | |
1839 | public final TWindow getActiveWindow() { | |
3d3e09e6 KL |
1840 | for (TWindow window: windows) { |
1841 | if (window.isShown() && window.isActive()) { | |
1842 | return window; | |
1843 | } | |
1844 | } | |
1845 | return null; | |
d36057df | 1846 | } |
fca67db0 | 1847 | |
d36057df KL |
1848 | /** |
1849 | * Get a (shallow) copy of the window list. | |
1850 | * | |
1851 | * @return a copy of the list of windows for this application | |
1852 | */ | |
1853 | public final List<TWindow> getAllWindows() { | |
a69ed767 | 1854 | List<TWindow> result = new ArrayList<TWindow>(); |
d36057df KL |
1855 | result.addAll(windows); |
1856 | return result; | |
1857 | } | |
b6faeac0 | 1858 | |
d36057df KL |
1859 | /** |
1860 | * Get focusFollowsMouse flag. | |
1861 | * | |
1862 | * @return true if focus follows mouse: windows automatically raised if | |
1863 | * the mouse passes over them | |
1864 | */ | |
1865 | public boolean getFocusFollowsMouse() { | |
1866 | return focusFollowsMouse; | |
1867 | } | |
b6faeac0 | 1868 | |
d36057df KL |
1869 | /** |
1870 | * Set focusFollowsMouse flag. | |
1871 | * | |
1872 | * @param focusFollowsMouse if true, focus follows mouse: windows | |
1873 | * automatically raised if the mouse passes over them | |
1874 | */ | |
1875 | public void setFocusFollowsMouse(final boolean focusFollowsMouse) { | |
1876 | this.focusFollowsMouse = focusFollowsMouse; | |
1877 | } | |
e8a11f98 | 1878 | |
e23ea538 KL |
1879 | /** |
1880 | * Display the about dialog. | |
1881 | */ | |
1882 | protected void showAboutDialog() { | |
1883 | String version = getClass().getPackage().getImplementationVersion(); | |
1884 | if (version == null) { | |
1885 | // This is Java 9+, use a hardcoded string here. | |
c334c9a2 | 1886 | version = "1.0.0"; |
e23ea538 KL |
1887 | } |
1888 | messageBox(i18n.getString("aboutDialogTitle"), | |
1889 | MessageFormat.format(i18n.getString("aboutDialogText"), version), | |
1890 | TMessageBox.Type.OK); | |
1891 | } | |
1892 | ||
1893 | /** | |
1894 | * Handle the Tool | Open image menu item. | |
1895 | */ | |
1896 | private void openImage() { | |
1897 | try { | |
1898 | List<String> filters = new ArrayList<String>(); | |
1899 | filters.add("^.*\\.[Jj][Pp][Gg]$"); | |
1900 | filters.add("^.*\\.[Jj][Pp][Ee][Gg]$"); | |
1901 | filters.add("^.*\\.[Pp][Nn][Gg]$"); | |
1902 | filters.add("^.*\\.[Gg][Ii][Ff]$"); | |
1903 | filters.add("^.*\\.[Bb][Mm][Pp]$"); | |
1904 | String filename = fileOpenBox(".", TFileOpenBox.Type.OPEN, filters); | |
1905 | if (filename != null) { | |
1906 | new TImageWindow(this, new File(filename)); | |
1907 | } | |
1908 | } catch (IOException e) { | |
1909 | // Show this exception to the user. | |
1910 | new TExceptionDialog(this, e); | |
1911 | } | |
1912 | } | |
1913 | ||
9696a8f6 KL |
1914 | /** |
1915 | * Check if application is still running. | |
1916 | * | |
1917 | * @return true if the application is running | |
1918 | */ | |
1919 | public final boolean isRunning() { | |
1920 | if (quit == true) { | |
1921 | return false; | |
1922 | } | |
1923 | return true; | |
1924 | } | |
1925 | ||
d36057df KL |
1926 | // ------------------------------------------------------------------------ |
1927 | // Screen refresh loop ---------------------------------------------------- | |
1928 | // ------------------------------------------------------------------------ | |
fca67db0 | 1929 | |
d36057df | 1930 | /** |
aac4f7d6 | 1931 | * Draw the text mouse at position. |
d36057df KL |
1932 | * |
1933 | * @param x column position | |
1934 | * @param y row position | |
1935 | */ | |
aac4f7d6 | 1936 | private void drawTextMouse(final int x, final int y) { |
3d3e09e6 | 1937 | TWindow activeWindow = getActiveWindow(); |
3af53a35 | 1938 | |
d36057df | 1939 | if (debugThreads) { |
aac4f7d6 | 1940 | System.err.printf("%d %s drawTextMouse() %d %d\n", |
d36057df | 1941 | System.currentTimeMillis(), Thread.currentThread(), x, y); |
978a5d8f KL |
1942 | |
1943 | if (activeWindow != null) { | |
1944 | System.err.println("activeWindow.hasHiddenMouse() " + | |
1945 | activeWindow.hasHiddenMouse()); | |
1946 | } | |
1947 | } | |
1948 | ||
1949 | // If this cell is on top of a visible window that has requested a | |
1950 | // hidden mouse, bail out. | |
1951 | if ((activeWindow != null) && (activeMenu == null)) { | |
1952 | if ((activeWindow.hasHiddenMouse() == true) | |
1953 | && (x > activeWindow.getX()) | |
1954 | && (x < activeWindow.getX() + activeWindow.getWidth() - 1) | |
1955 | && (y > activeWindow.getY()) | |
1956 | && (y < activeWindow.getY() + activeWindow.getHeight() - 1) | |
1957 | ) { | |
9ad2ce4f KL |
1958 | return; |
1959 | } | |
1960 | } | |
1961 | ||
1962 | // If this cell is on top of the desktop, and the desktop has | |
1963 | // requested a hidden mouse, bail out. | |
1964 | if ((desktop != null) && (activeWindow == null) && (activeMenu == null)) { | |
1965 | if ((desktop.hasHiddenMouse() == true) | |
1966 | && (x > desktop.getX()) | |
1967 | && (x < desktop.getX() + desktop.getWidth() - 1) | |
1968 | && (y > desktop.getY()) | |
1969 | && (y < desktop.getY() + desktop.getHeight() - 1) | |
1970 | ) { | |
978a5d8f KL |
1971 | return; |
1972 | } | |
d36057df | 1973 | } |
978a5d8f | 1974 | |
aac4f7d6 | 1975 | getScreen().invertCell(x, y); |
d36057df | 1976 | } |
fca67db0 | 1977 | |
d36057df KL |
1978 | /** |
1979 | * Draw everything. | |
1980 | */ | |
1981 | private void drawAll() { | |
1982 | boolean menuIsActive = false; | |
fca67db0 | 1983 | |
d36057df KL |
1984 | if (debugThreads) { |
1985 | System.err.printf("%d %s drawAll() enter\n", | |
1986 | System.currentTimeMillis(), Thread.currentThread()); | |
fca67db0 | 1987 | } |
a06459bd | 1988 | |
a69ed767 | 1989 | // I don't think this does anything useful anymore... |
d36057df KL |
1990 | if (!repaint) { |
1991 | if (debugThreads) { | |
1992 | System.err.printf("%d %s drawAll() !repaint\n", | |
1993 | System.currentTimeMillis(), Thread.currentThread()); | |
e826b451 | 1994 | } |
a69ed767 KL |
1995 | if ((oldDrawnMouseX != mouseX) || (oldDrawnMouseY != mouseY)) { |
1996 | if (debugThreads) { | |
1997 | System.err.printf("%d %s drawAll() !repaint MOUSE\n", | |
1998 | System.currentTimeMillis(), Thread.currentThread()); | |
fca67db0 | 1999 | } |
a69ed767 KL |
2000 | |
2001 | // The only thing that has happened is the mouse moved. | |
2002 | ||
2003 | // Redraw the old cell at that position, and save the cell at | |
2004 | // the new mouse position. | |
2005 | if (debugThreads) { | |
2006 | System.err.printf("%d %s restoreImage() %d %d\n", | |
2007 | System.currentTimeMillis(), Thread.currentThread(), | |
2008 | oldDrawnMouseX, oldDrawnMouseY); | |
2ce6dab2 | 2009 | } |
a69ed767 KL |
2010 | oldDrawnMouseCell.restoreImage(); |
2011 | getScreen().putCharXY(oldDrawnMouseX, oldDrawnMouseY, | |
2012 | oldDrawnMouseCell); | |
2013 | oldDrawnMouseCell = getScreen().getCharXY(mouseX, mouseY); | |
e6469faa | 2014 | if (backend instanceof ECMA48Backend) { |
a69ed767 KL |
2015 | // Special case: the entire row containing the mouse has |
2016 | // to be re-drawn if it has any image data, AND any rows | |
2017 | // in between. | |
2018 | if (oldDrawnMouseY != mouseY) { | |
2019 | for (int i = oldDrawnMouseY; ;) { | |
2020 | getScreen().unsetImageRow(i); | |
2021 | if (i == mouseY) { | |
2022 | break; | |
2023 | } | |
2024 | if (oldDrawnMouseY < mouseY) { | |
2025 | i++; | |
2026 | } else { | |
2027 | i--; | |
2028 | } | |
2029 | } | |
2030 | } else { | |
2031 | getScreen().unsetImageRow(mouseY); | |
2032 | } | |
2033 | } | |
2034 | ||
54eaded0 KL |
2035 | if (inScreenSelection) { |
2036 | getScreen().setSelection(screenSelectionX0, | |
2037 | screenSelectionY0, screenSelectionX1, screenSelectionY1, | |
2038 | screenSelectionRectangle); | |
2039 | } | |
2040 | ||
80b1b7b5 KL |
2041 | if ((textMouse == true) && (typingHidMouse == false)) { |
2042 | // Draw mouse at the new position. | |
aac4f7d6 | 2043 | drawTextMouse(mouseX, mouseY); |
80b1b7b5 | 2044 | } |
a69ed767 KL |
2045 | |
2046 | oldDrawnMouseX = mouseX; | |
2047 | oldDrawnMouseY = mouseY; | |
2048 | } | |
e6469faa | 2049 | if (getScreen().isDirty()) { |
d14e2d78 | 2050 | screenHandler.setDirty(); |
fca67db0 | 2051 | } |
a69ed767 | 2052 | return; |
fca67db0 KL |
2053 | } |
2054 | ||
d36057df KL |
2055 | if (debugThreads) { |
2056 | System.err.printf("%d %s drawAll() REDRAW\n", | |
2057 | System.currentTimeMillis(), Thread.currentThread()); | |
fca67db0 KL |
2058 | } |
2059 | ||
d36057df KL |
2060 | // If true, the cursor is not visible |
2061 | boolean cursor = false; | |
92453213 | 2062 | |
d36057df KL |
2063 | // Start with a clean screen |
2064 | getScreen().clear(); | |
b6faeac0 | 2065 | |
d36057df KL |
2066 | // Draw the desktop |
2067 | if (desktop != null) { | |
2068 | desktop.drawChildren(); | |
2069 | } | |
0ee88b6d | 2070 | |
d36057df | 2071 | // Draw each window in reverse Z order |
a69ed767 | 2072 | List<TWindow> sorted = new ArrayList<TWindow>(windows); |
d36057df KL |
2073 | Collections.sort(sorted); |
2074 | TWindow topLevel = null; | |
2075 | if (sorted.size() > 0) { | |
2076 | topLevel = sorted.get(0); | |
2077 | } | |
2078 | Collections.reverse(sorted); | |
2079 | for (TWindow window: sorted) { | |
2080 | if (window.isShown()) { | |
2081 | window.drawChildren(); | |
b6faeac0 | 2082 | } |
fca67db0 | 2083 | } |
d36057df | 2084 | |
2bb26984 | 2085 | if (hideMenuBar == false) { |
0ee88b6d | 2086 | |
2bb26984 KL |
2087 | // Draw the blank menubar line - reset the screen clipping first |
2088 | // so it won't trim it out. | |
d36057df | 2089 | getScreen().resetClipping(); |
2bb26984 KL |
2090 | getScreen().hLineXY(0, 0, getScreen().getWidth(), ' ', |
2091 | theme.getColor("tmenu")); | |
2092 | // Now draw the menus. | |
2093 | int x = 1; | |
2094 | for (TMenu menu: menus) { | |
2095 | CellAttributes menuColor; | |
2096 | CellAttributes menuMnemonicColor; | |
2097 | if (menu.isActive()) { | |
2098 | menuIsActive = true; | |
2099 | menuColor = theme.getColor("tmenu.highlighted"); | |
2100 | menuMnemonicColor = theme.getColor("tmenu.mnemonic.highlighted"); | |
2101 | topLevel = menu; | |
2102 | } else { | |
2103 | menuColor = theme.getColor("tmenu"); | |
2104 | menuMnemonicColor = theme.getColor("tmenu.mnemonic"); | |
2105 | } | |
2106 | // Draw the menu title | |
2107 | getScreen().hLineXY(x, 0, | |
2108 | StringUtils.width(menu.getTitle()) + 2, ' ', menuColor); | |
2109 | getScreen().putStringXY(x + 1, 0, menu.getTitle(), menuColor); | |
2110 | // Draw the highlight character | |
2111 | getScreen().putCharXY(x + 1 + | |
2112 | menu.getMnemonic().getScreenShortcutIdx(), | |
2113 | 0, menu.getMnemonic().getShortcut(), menuMnemonicColor); | |
2114 | ||
2115 | if (menu.isActive()) { | |
2116 | ((TWindow) menu).drawChildren(); | |
2117 | // Reset the screen clipping so we can draw the next | |
2118 | // title. | |
2119 | getScreen().resetClipping(); | |
2120 | } | |
2121 | x += StringUtils.width(menu.getTitle()) + 2; | |
2122 | } | |
2123 | ||
2124 | for (TMenu menu: subMenus) { | |
2125 | // Reset the screen clipping so we can draw the next | |
2126 | // sub-menu. | |
2127 | getScreen().resetClipping(); | |
2128 | ((TWindow) menu).drawChildren(); | |
2129 | } | |
d36057df | 2130 | } |
a69ed767 | 2131 | getScreen().resetClipping(); |
b6faeac0 | 2132 | |
2bb26984 KL |
2133 | if (hideStatusBar == false) { |
2134 | // Draw the status bar of the top-level window | |
2135 | TStatusBar statusBar = null; | |
2136 | if (topLevel != null) { | |
3c5921e6 KL |
2137 | if (topLevel.isShown()) { |
2138 | statusBar = topLevel.getStatusBar(); | |
2139 | } | |
2bb26984 KL |
2140 | } |
2141 | if (statusBar != null) { | |
2142 | getScreen().resetClipping(); | |
2143 | statusBar.setWidth(getScreen().getWidth()); | |
2144 | statusBar.setY(getScreen().getHeight() - topLevel.getY()); | |
2145 | statusBar.draw(); | |
2146 | } else { | |
2147 | CellAttributes barColor = new CellAttributes(); | |
2148 | barColor.setTo(getTheme().getColor("tstatusbar.text")); | |
2149 | getScreen().hLineXY(0, desktopBottom, getScreen().getWidth(), | |
2150 | ' ', barColor); | |
2151 | } | |
d36057df | 2152 | } |
b6faeac0 | 2153 | |
d36057df | 2154 | // Draw the mouse pointer |
a69ed767 KL |
2155 | if (debugThreads) { |
2156 | System.err.printf("%d %s restoreImage() %d %d\n", | |
2157 | System.currentTimeMillis(), Thread.currentThread(), | |
2158 | oldDrawnMouseX, oldDrawnMouseY); | |
2159 | } | |
2160 | oldDrawnMouseCell = getScreen().getCharXY(mouseX, mouseY); | |
e6469faa | 2161 | if (backend instanceof ECMA48Backend) { |
a69ed767 KL |
2162 | // Special case: the entire row containing the mouse has to be |
2163 | // re-drawn if it has any image data, AND any rows in between. | |
2164 | if (oldDrawnMouseY != mouseY) { | |
2165 | for (int i = oldDrawnMouseY; ;) { | |
2166 | getScreen().unsetImageRow(i); | |
2167 | if (i == mouseY) { | |
2168 | break; | |
2169 | } | |
2170 | if (oldDrawnMouseY < mouseY) { | |
2171 | i++; | |
2172 | } else { | |
2173 | i--; | |
2174 | } | |
2175 | } | |
2176 | } else { | |
2177 | getScreen().unsetImageRow(mouseY); | |
2178 | } | |
2179 | } | |
54eaded0 KL |
2180 | |
2181 | if (inScreenSelection) { | |
2182 | getScreen().setSelection(screenSelectionX0, screenSelectionY0, | |
2183 | screenSelectionX1, screenSelectionY1, screenSelectionRectangle); | |
2184 | } | |
2185 | ||
80b1b7b5 | 2186 | if ((textMouse == true) && (typingHidMouse == false)) { |
aac4f7d6 | 2187 | drawTextMouse(mouseX, mouseY); |
80b1b7b5 | 2188 | } |
a69ed767 KL |
2189 | oldDrawnMouseX = mouseX; |
2190 | oldDrawnMouseY = mouseY; | |
b6faeac0 | 2191 | |
d36057df KL |
2192 | // Place the cursor if it is visible |
2193 | if (!menuIsActive) { | |
2bc32111 KL |
2194 | |
2195 | int visibleWindowCount = 0; | |
2196 | for (TWindow window: sorted) { | |
2197 | if (window.isShown()) { | |
2198 | visibleWindowCount++; | |
2199 | } | |
2200 | } | |
2201 | if (visibleWindowCount == 0) { | |
2202 | // No windows are visible, only the desktop. Allow it to | |
2203 | // have the cursor. | |
2204 | if (desktop != null) { | |
2205 | sorted.add(desktop); | |
2206 | } | |
2207 | } | |
2208 | ||
d36057df KL |
2209 | TWidget activeWidget = null; |
2210 | if (sorted.size() > 0) { | |
2211 | activeWidget = sorted.get(sorted.size() - 1).getActiveChild(); | |
2bc32111 KL |
2212 | int cursorClipTop = desktopTop; |
2213 | int cursorClipBottom = desktopBottom; | |
d36057df | 2214 | if (activeWidget.isCursorVisible()) { |
2bc32111 KL |
2215 | if ((activeWidget.getCursorAbsoluteY() <= cursorClipBottom) |
2216 | && (activeWidget.getCursorAbsoluteY() >= cursorClipTop) | |
d36057df KL |
2217 | ) { |
2218 | getScreen().putCursor(true, | |
2219 | activeWidget.getCursorAbsoluteX(), | |
2220 | activeWidget.getCursorAbsoluteY()); | |
2221 | cursor = true; | |
b6faeac0 | 2222 | } else { |
a69ed767 KL |
2223 | // Turn off the cursor. Also place it at 0,0. |
2224 | getScreen().putCursor(false, 0, 0); | |
d36057df | 2225 | cursor = false; |
b6faeac0 KL |
2226 | } |
2227 | } | |
e8a11f98 KL |
2228 | } |
2229 | } | |
2230 | ||
d36057df KL |
2231 | // Kill the cursor |
2232 | if (!cursor) { | |
2233 | getScreen().hideCursor(); | |
b6faeac0 | 2234 | } |
c6940ed9 | 2235 | |
e6469faa | 2236 | if (getScreen().isDirty()) { |
d14e2d78 | 2237 | screenHandler.setDirty(); |
be72cb5c | 2238 | } |
d36057df | 2239 | repaint = false; |
a06459bd KL |
2240 | } |
2241 | ||
4328bb42 | 2242 | /** |
d36057df | 2243 | * Force this application to exit. |
4328bb42 | 2244 | */ |
d36057df KL |
2245 | public void exit() { |
2246 | quit = true; | |
2247 | synchronized (this) { | |
2248 | this.notify(); | |
92453213 | 2249 | } |
4328bb42 | 2250 | } |
7d4115a5 | 2251 | |
abb84744 KL |
2252 | /** |
2253 | * Subclasses can use this hook to cleanup resources. Called as the last | |
2254 | * step of TApplication.run(). | |
2255 | */ | |
2256 | public void onExit() { | |
2257 | // Default does nothing. | |
2258 | } | |
2259 | ||
2ce6dab2 KL |
2260 | // ------------------------------------------------------------------------ |
2261 | // TWindow management ----------------------------------------------------- | |
2262 | // ------------------------------------------------------------------------ | |
4328bb42 | 2263 | |
92453213 KL |
2264 | /** |
2265 | * Return the total number of windows. | |
2266 | * | |
2267 | * @return the total number of windows | |
2268 | */ | |
2269 | public final int windowCount() { | |
2270 | return windows.size(); | |
2271 | } | |
2272 | ||
2273 | /** | |
8c236a98 | 2274 | * Return the number of windows that are showing. |
92453213 | 2275 | * |
8c236a98 | 2276 | * @return the number of windows that are showing on screen |
92453213 KL |
2277 | */ |
2278 | public final int shownWindowCount() { | |
2279 | int n = 0; | |
2280 | for (TWindow w: windows) { | |
2281 | if (w.isShown()) { | |
2282 | n++; | |
2283 | } | |
2284 | } | |
2285 | return n; | |
2286 | } | |
2287 | ||
8c236a98 KL |
2288 | /** |
2289 | * Return the number of windows that are hidden. | |
2290 | * | |
2291 | * @return the number of windows that are hidden | |
2292 | */ | |
2293 | public final int hiddenWindowCount() { | |
2294 | int n = 0; | |
2295 | for (TWindow w: windows) { | |
2296 | if (w.isHidden()) { | |
2297 | n++; | |
2298 | } | |
2299 | } | |
2300 | return n; | |
2301 | } | |
2302 | ||
92453213 KL |
2303 | /** |
2304 | * Check if a window instance is in this application's window list. | |
2305 | * | |
2306 | * @param window window to look for | |
2307 | * @return true if this window is in the list | |
2308 | */ | |
2309 | public final boolean hasWindow(final TWindow window) { | |
2310 | if (windows.size() == 0) { | |
2311 | return false; | |
2312 | } | |
2313 | for (TWindow w: windows) { | |
2314 | if (w == window) { | |
8c236a98 | 2315 | assert (window.getApplication() == this); |
92453213 KL |
2316 | return true; |
2317 | } | |
2318 | } | |
2319 | return false; | |
2320 | } | |
2321 | ||
2322 | /** | |
2323 | * Activate a window: bring it to the top and have it receive events. | |
2324 | * | |
2325 | * @param window the window to become the new active window | |
2326 | */ | |
3d3e09e6 | 2327 | public final void activateWindow(final TWindow window) { |
92453213 KL |
2328 | if (hasWindow(window) == false) { |
2329 | /* | |
2330 | * Someone has a handle to a window I don't have. Ignore this | |
2331 | * request. | |
2332 | */ | |
2333 | return; | |
2334 | } | |
2335 | ||
3d3e09e6 KL |
2336 | if (modalWindowActive() && !window.isModal()) { |
2337 | // Do not activate a non-modal on top of a modal. | |
2338 | return; | |
fe0770f9 KL |
2339 | } |
2340 | ||
3d3e09e6 KL |
2341 | synchronized (windows) { |
2342 | // Whatever window might be moving/dragging, stop it now. | |
2343 | for (TWindow w: windows) { | |
2344 | if (w.inMovements()) { | |
2345 | w.stopMovements(); | |
2346 | } | |
2347 | } | |
92453213 | 2348 | |
3d3e09e6 | 2349 | assert (windows.size() > 0); |
92453213 | 2350 | |
3d3e09e6 KL |
2351 | if (window.isHidden()) { |
2352 | // Unhiding will also activate. | |
2353 | showWindow(window); | |
2354 | return; | |
92453213 | 2355 | } |
3d3e09e6 | 2356 | assert (window.isShown()); |
92453213 | 2357 | |
3d3e09e6 KL |
2358 | if (windows.size() == 1) { |
2359 | assert (window == windows.get(0)); | |
2360 | window.setZ(0); | |
2361 | window.setActive(true); | |
2362 | window.onFocus(); | |
2363 | return; | |
2364 | } | |
92453213 | 2365 | |
3d3e09e6 KL |
2366 | if (getActiveWindow() == window) { |
2367 | assert (window.isActive()); | |
92453213 | 2368 | |
3d3e09e6 KL |
2369 | // Window is already active, do nothing. |
2370 | return; | |
2371 | } | |
92453213 | 2372 | |
3d3e09e6 | 2373 | assert (!window.isActive()); |
a69ed767 | 2374 | |
3d3e09e6 KL |
2375 | window.setZ(-1); |
2376 | Collections.sort(windows); | |
2377 | int newZ = 0; | |
a69ed767 | 2378 | for (TWindow w: windows) { |
3d3e09e6 KL |
2379 | w.setZ(newZ); |
2380 | newZ++; | |
2381 | if ((w != window) && w.isActive()) { | |
2382 | w.onUnfocus(); | |
a69ed767 | 2383 | } |
3d3e09e6 | 2384 | w.setActive(false); |
a69ed767 | 2385 | } |
3d3e09e6 KL |
2386 | window.setActive(true); |
2387 | window.onFocus(); | |
2388 | ||
2389 | } // synchronized (windows) | |
499fdccf | 2390 | |
92453213 KL |
2391 | return; |
2392 | } | |
2393 | ||
2394 | /** | |
2395 | * Hide a window. | |
2396 | * | |
2397 | * @param window the window to hide | |
2398 | */ | |
2399 | public void hideWindow(final TWindow window) { | |
2400 | if (hasWindow(window) == false) { | |
2401 | /* | |
2402 | * Someone has a handle to a window I don't have. Ignore this | |
2403 | * request. | |
2404 | */ | |
2405 | return; | |
2406 | } | |
2407 | ||
3d3e09e6 KL |
2408 | synchronized (windows) { |
2409 | ||
2410 | // Whatever window might be moving/dragging, stop it now. | |
2411 | for (TWindow w: windows) { | |
2412 | if (w.inMovements()) { | |
2413 | w.stopMovements(); | |
2414 | } | |
fe0770f9 | 2415 | } |
fe0770f9 | 2416 | |
3d3e09e6 | 2417 | assert (windows.size() > 0); |
92453213 | 2418 | |
3d3e09e6 KL |
2419 | if (window.hidden) { |
2420 | return; | |
92453213 | 2421 | } |
3d3e09e6 KL |
2422 | |
2423 | window.setActive(false); | |
92453213 KL |
2424 | window.hidden = true; |
2425 | window.onHide(); | |
3d3e09e6 KL |
2426 | |
2427 | TWindow activeWindow = null; | |
2428 | for (TWindow w: windows) { | |
2429 | if (w.isShown()) { | |
2430 | activeWindow = w; | |
2431 | break; | |
2432 | } | |
2433 | } | |
2434 | assert (activeWindow != window); | |
2435 | if (activeWindow != null) { | |
2436 | activateWindow(activeWindow); | |
2437 | } | |
2438 | ||
2439 | } // synchronized (windows) | |
2440 | ||
92453213 KL |
2441 | } |
2442 | ||
2443 | /** | |
2444 | * Show a window. | |
2445 | * | |
2446 | * @param window the window to show | |
2447 | */ | |
2448 | public void showWindow(final TWindow window) { | |
2449 | if (hasWindow(window) == false) { | |
2450 | /* | |
2451 | * Someone has a handle to a window I don't have. Ignore this | |
2452 | * request. | |
2453 | */ | |
2454 | return; | |
2455 | } | |
2456 | ||
92453213 KL |
2457 | if (window.hidden) { |
2458 | window.hidden = false; | |
2459 | window.onShow(); | |
2460 | activateWindow(window); | |
2461 | } | |
3d3e09e6 | 2462 | |
92453213 KL |
2463 | } |
2464 | ||
48e27807 | 2465 | /** |
3d3e09e6 | 2466 | * Close window. |
48e27807 KL |
2467 | * |
2468 | * @param window the window to remove | |
2469 | */ | |
2470 | public final void closeWindow(final TWindow window) { | |
92453213 KL |
2471 | if (hasWindow(window) == false) { |
2472 | /* | |
2473 | * Someone has a handle to a window I don't have. Ignore this | |
2474 | * request. | |
2475 | */ | |
2476 | return; | |
2477 | } | |
2478 | ||
a69ed767 KL |
2479 | // Let window know that it is about to be closed, while it is still |
2480 | // visible on screen. | |
2481 | window.onPreClose(); | |
2482 | ||
bb35d919 | 2483 | synchronized (windows) { |
fe0770f9 | 2484 | |
3d3e09e6 | 2485 | window.stopMovements(); |
efb7af1f | 2486 | window.onUnfocus(); |
e23ea538 | 2487 | windows.remove(window); |
bb35d919 | 2488 | Collections.sort(windows); |
e23ea538 | 2489 | |
3d3e09e6 KL |
2490 | TWindow nextWindow = null; |
2491 | int newZ = 0; | |
bb35d919 | 2492 | for (TWindow w: windows) { |
3d3e09e6 | 2493 | w.stopMovements(); |
e23ea538 KL |
2494 | w.setZ(newZ); |
2495 | newZ++; | |
3eacc236 KL |
2496 | |
2497 | // Do not activate a hidden window. | |
2498 | if (w.isHidden()) { | |
2499 | continue; | |
2500 | } | |
3d3e09e6 KL |
2501 | if (nextWindow == null) { |
2502 | nextWindow = w; | |
2503 | } else { | |
2504 | if (w.isActive()) { | |
2505 | w.setActive(false); | |
2506 | w.onUnfocus(); | |
2507 | } | |
e23ea538 | 2508 | } |
3d3e09e6 | 2509 | } |
e23ea538 | 2510 | |
3d3e09e6 KL |
2511 | if (nextWindow != null) { |
2512 | nextWindow.setActive(true); | |
2513 | nextWindow.onFocus(); | |
48e27807 | 2514 | } |
3d3e09e6 KL |
2515 | |
2516 | } // synchronized (windows) | |
48e27807 KL |
2517 | |
2518 | // Perform window cleanup | |
2519 | window.onClose(); | |
2520 | ||
48e27807 | 2521 | // Check if we are closing a TMessageBox or similar |
c6940ed9 KL |
2522 | if (secondaryEventReceiver != null) { |
2523 | assert (secondaryEventHandler != null); | |
48e27807 KL |
2524 | |
2525 | // Do not send events to the secondaryEventReceiver anymore, the | |
2526 | // window is closed. | |
2527 | secondaryEventReceiver = null; | |
2528 | ||
92554d64 KL |
2529 | // Wake the secondary thread, it will wake the primary as it |
2530 | // exits. | |
2531 | synchronized (secondaryEventHandler) { | |
2532 | secondaryEventHandler.notify(); | |
48e27807 | 2533 | } |
3d3e09e6 KL |
2534 | |
2535 | } // synchronized (windows) | |
92453213 KL |
2536 | |
2537 | // Permit desktop to be active if it is the only thing left. | |
2538 | if (desktop != null) { | |
2539 | if (windows.size() == 0) { | |
2540 | desktop.setActive(true); | |
2541 | } | |
2542 | } | |
48e27807 KL |
2543 | } |
2544 | ||
2545 | /** | |
2546 | * Switch to the next window. | |
2547 | * | |
2548 | * @param forward if true, then switch to the next window in the list, | |
2549 | * otherwise switch to the previous window in the list | |
2550 | */ | |
2551 | public final void switchWindow(final boolean forward) { | |
8c236a98 KL |
2552 | // Only switch if there are multiple visible windows |
2553 | if (shownWindowCount() < 2) { | |
48e27807 KL |
2554 | return; |
2555 | } | |
3d3e09e6 KL |
2556 | |
2557 | if (modalWindowActive()) { | |
2558 | // Do not switch if a window is modal | |
2559 | return; | |
2560 | } | |
48e27807 | 2561 | |
bb35d919 KL |
2562 | synchronized (windows) { |
2563 | ||
3d3e09e6 KL |
2564 | TWindow window = windows.get(0); |
2565 | do { | |
2566 | assert (window != null); | |
2567 | if (forward) { | |
2568 | window.setZ(windows.size()); | |
92453213 | 2569 | } else { |
3d3e09e6 KL |
2570 | TWindow lastWindow = windows.get(windows.size() - 1); |
2571 | lastWindow.setZ(-1); | |
bb35d919 | 2572 | } |
48e27807 | 2573 | |
3d3e09e6 KL |
2574 | Collections.sort(windows); |
2575 | int newZ = 0; | |
2576 | for (TWindow w: windows) { | |
2577 | w.setZ(newZ); | |
2578 | newZ++; | |
bb35d919 | 2579 | } |
bb35d919 | 2580 | |
3d3e09e6 KL |
2581 | window = windows.get(0); |
2582 | } while (!window.isShown()); | |
2583 | ||
2584 | // The next visible window is now on top. Renumber the list. | |
2585 | for (TWindow w: windows) { | |
2586 | w.stopMovements(); | |
2587 | if ((w != window) && w.isActive()) { | |
2588 | assert (w.isShown()); | |
2589 | w.setActive(false); | |
2590 | w.onUnfocus(); | |
8c236a98 KL |
2591 | } |
2592 | } | |
48e27807 | 2593 | |
3d3e09e6 KL |
2594 | // Next visible window is on top. |
2595 | assert (window.isShown()); | |
2596 | window.setActive(true); | |
2597 | window.onFocus(); | |
2598 | ||
2599 | } // synchronized (windows) | |
48e27807 KL |
2600 | } |
2601 | ||
2602 | /** | |
051e2913 KL |
2603 | * Add a window to my window list and make it active. Note package |
2604 | * private access. | |
48e27807 KL |
2605 | * |
2606 | * @param window new window to add | |
2607 | */ | |
051e2913 | 2608 | final void addWindowToApplication(final TWindow window) { |
a7986f7b KL |
2609 | |
2610 | // Do not add menu windows to the window list. | |
2611 | if (window instanceof TMenu) { | |
2612 | return; | |
2613 | } | |
2614 | ||
0ee88b6d KL |
2615 | // Do not add the desktop to the window list. |
2616 | if (window instanceof TDesktop) { | |
2617 | return; | |
2618 | } | |
2619 | ||
bb35d919 | 2620 | synchronized (windows) { |
051e2913 KL |
2621 | if (windows.contains(window)) { |
2622 | throw new IllegalArgumentException("Window " + window + | |
2623 | " is already in window list"); | |
2624 | } | |
2625 | ||
fe0770f9 KL |
2626 | // Whatever window might be moving/dragging, stop it now. |
2627 | for (TWindow w: windows) { | |
2628 | if (w.inMovements()) { | |
2629 | w.stopMovements(); | |
2630 | } | |
2631 | } | |
2632 | ||
2ce6dab2 KL |
2633 | // Do not allow a modal window to spawn a non-modal window. If a |
2634 | // modal window is active, then this window will become modal | |
2635 | // too. | |
2636 | if (modalWindowActive()) { | |
2637 | window.flags |= TWindow.MODAL; | |
a7986f7b | 2638 | window.flags |= TWindow.CENTERED; |
92453213 | 2639 | window.hidden = false; |
bb35d919 | 2640 | } |
92453213 KL |
2641 | if (window.isShown()) { |
2642 | for (TWindow w: windows) { | |
2643 | if (w.isActive()) { | |
2644 | w.setActive(false); | |
2645 | w.onUnfocus(); | |
2646 | } | |
2647 | w.setZ(w.getZ() + 1); | |
efb7af1f | 2648 | } |
3d3e09e6 KL |
2649 | window.setZ(0); |
2650 | window.setActive(true); | |
2651 | window.onFocus(); | |
2652 | windows.add(0, window); | |
2653 | } else { | |
2654 | window.setZ(windows.size()); | |
2655 | windows.add(window); | |
92453213 | 2656 | } |
a7986f7b KL |
2657 | |
2658 | if (((window.flags & TWindow.CENTERED) == 0) | |
d36057df KL |
2659 | && ((window.flags & TWindow.ABSOLUTEXY) == 0) |
2660 | && (smartWindowPlacement == true) | |
2bb26984 | 2661 | && (!(window instanceof TDesktop)) |
d36057df | 2662 | ) { |
a7986f7b KL |
2663 | |
2664 | doSmartPlacement(window); | |
2665 | } | |
48e27807 | 2666 | } |
92453213 KL |
2667 | |
2668 | // Desktop cannot be active over any other window. | |
2669 | if (desktop != null) { | |
2670 | desktop.setActive(false); | |
2671 | } | |
3d3e09e6 | 2672 | |
48e27807 KL |
2673 | } |
2674 | ||
fca67db0 KL |
2675 | /** |
2676 | * Check if there is a system-modal window on top. | |
2677 | * | |
2678 | * @return true if the active window is modal | |
2679 | */ | |
2680 | private boolean modalWindowActive() { | |
2681 | if (windows.size() == 0) { | |
2682 | return false; | |
2683 | } | |
2ce6dab2 KL |
2684 | |
2685 | for (TWindow w: windows) { | |
2686 | if (w.isModal()) { | |
2687 | return true; | |
2688 | } | |
2689 | } | |
2690 | ||
2691 | return false; | |
2692 | } | |
2693 | ||
9696a8f6 KL |
2694 | /** |
2695 | * Check if there is a window with overridden menu flag on top. | |
2696 | * | |
2697 | * @return true if the active window is overriding the menu | |
2698 | */ | |
2699 | private boolean overrideMenuWindowActive() { | |
3d3e09e6 | 2700 | TWindow activeWindow = getActiveWindow(); |
9696a8f6 KL |
2701 | if (activeWindow != null) { |
2702 | if (activeWindow.hasOverriddenMenu()) { | |
2703 | return true; | |
2704 | } | |
2705 | } | |
2706 | ||
2707 | return false; | |
2708 | } | |
2709 | ||
2ce6dab2 KL |
2710 | /** |
2711 | * Close all open windows. | |
2712 | */ | |
2713 | private void closeAllWindows() { | |
2714 | // Don't do anything if we are in the menu | |
2715 | if (activeMenu != null) { | |
2716 | return; | |
2717 | } | |
2718 | while (windows.size() > 0) { | |
2719 | closeWindow(windows.get(0)); | |
2720 | } | |
fca67db0 KL |
2721 | } |
2722 | ||
2ce6dab2 KL |
2723 | /** |
2724 | * Re-layout the open windows as non-overlapping tiles. This produces | |
2725 | * almost the same results as Turbo Pascal 7.0's IDE. | |
2726 | */ | |
2727 | private void tileWindows() { | |
2728 | synchronized (windows) { | |
2729 | // Don't do anything if we are in the menu | |
2730 | if (activeMenu != null) { | |
2731 | return; | |
2732 | } | |
2733 | int z = windows.size(); | |
2734 | if (z == 0) { | |
2735 | return; | |
2736 | } | |
2737 | int a = 0; | |
2738 | int b = 0; | |
2739 | a = (int)(Math.sqrt(z)); | |
2740 | int c = 0; | |
2741 | while (c < a) { | |
2742 | b = (z - c) / a; | |
2743 | if (((a * b) + c) == z) { | |
2744 | break; | |
2745 | } | |
2746 | c++; | |
2747 | } | |
2748 | assert (a > 0); | |
2749 | assert (b > 0); | |
2750 | assert (c < a); | |
2751 | int newWidth = (getScreen().getWidth() / a); | |
2752 | int newHeight1 = ((getScreen().getHeight() - 1) / b); | |
2753 | int newHeight2 = ((getScreen().getHeight() - 1) / (b + c)); | |
2754 | ||
a69ed767 | 2755 | List<TWindow> sorted = new ArrayList<TWindow>(windows); |
2ce6dab2 KL |
2756 | Collections.sort(sorted); |
2757 | Collections.reverse(sorted); | |
2758 | for (int i = 0; i < sorted.size(); i++) { | |
2759 | int logicalX = i / b; | |
2760 | int logicalY = i % b; | |
2761 | if (i >= ((a - 1) * b)) { | |
2762 | logicalX = a - 1; | |
2763 | logicalY = i - ((a - 1) * b); | |
2764 | } | |
2765 | ||
2766 | TWindow w = sorted.get(i); | |
7d922e0d KL |
2767 | int oldWidth = w.getWidth(); |
2768 | int oldHeight = w.getHeight(); | |
2769 | ||
2ce6dab2 KL |
2770 | w.setX(logicalX * newWidth); |
2771 | w.setWidth(newWidth); | |
2772 | if (i >= ((a - 1) * b)) { | |
2773 | w.setY((logicalY * newHeight2) + 1); | |
2774 | w.setHeight(newHeight2); | |
2775 | } else { | |
2776 | w.setY((logicalY * newHeight1) + 1); | |
2777 | w.setHeight(newHeight1); | |
2778 | } | |
7d922e0d KL |
2779 | if ((w.getWidth() != oldWidth) |
2780 | || (w.getHeight() != oldHeight) | |
2781 | ) { | |
2782 | w.onResize(new TResizeEvent(TResizeEvent.Type.WIDGET, | |
2783 | w.getWidth(), w.getHeight())); | |
2784 | } | |
2ce6dab2 KL |
2785 | } |
2786 | } | |
2787 | } | |
2788 | ||
2789 | /** | |
2790 | * Re-layout the open windows as overlapping cascaded windows. | |
2791 | */ | |
2792 | private void cascadeWindows() { | |
2793 | synchronized (windows) { | |
2794 | // Don't do anything if we are in the menu | |
2795 | if (activeMenu != null) { | |
2796 | return; | |
2797 | } | |
2798 | int x = 0; | |
2799 | int y = 1; | |
a69ed767 | 2800 | List<TWindow> sorted = new ArrayList<TWindow>(windows); |
2ce6dab2 KL |
2801 | Collections.sort(sorted); |
2802 | Collections.reverse(sorted); | |
2803 | for (TWindow window: sorted) { | |
2804 | window.setX(x); | |
2805 | window.setY(y); | |
2806 | x++; | |
2807 | y++; | |
2808 | if (x > getScreen().getWidth()) { | |
2809 | x = 0; | |
2810 | } | |
2811 | if (y >= getScreen().getHeight()) { | |
2812 | y = 1; | |
2813 | } | |
2814 | } | |
2815 | } | |
2816 | } | |
2817 | ||
a7986f7b KL |
2818 | /** |
2819 | * Place a window to minimize its overlap with other windows. | |
2820 | * | |
2821 | * @param window the window to place | |
2822 | */ | |
2823 | public final void doSmartPlacement(final TWindow window) { | |
2824 | // This is a pretty dumb algorithm, but seems to work. The hardest | |
2825 | // part is computing these "overlap" values seeking a minimum average | |
2826 | // overlap. | |
2827 | int xMin = 0; | |
2828 | int yMin = desktopTop; | |
2829 | int xMax = getScreen().getWidth() - window.getWidth() + 1; | |
2830 | int yMax = desktopBottom - window.getHeight() + 1; | |
2831 | if (xMax < xMin) { | |
2832 | xMax = xMin; | |
2833 | } | |
2834 | if (yMax < yMin) { | |
2835 | yMax = yMin; | |
2836 | } | |
2837 | ||
2838 | if ((xMin == xMax) && (yMin == yMax)) { | |
2839 | // No work to do, bail out. | |
2840 | return; | |
2841 | } | |
2842 | ||
2843 | // Compute the overlap matrix without the new window. | |
2844 | int width = getScreen().getWidth(); | |
2845 | int height = getScreen().getHeight(); | |
2846 | int overlapMatrix[][] = new int[width][height]; | |
2847 | for (TWindow w: windows) { | |
2848 | if (window == w) { | |
2849 | continue; | |
2850 | } | |
2851 | for (int x = w.getX(); x < w.getX() + w.getWidth(); x++) { | |
9ff1c0e3 KL |
2852 | if (x < 0) { |
2853 | continue; | |
2854 | } | |
8c236a98 | 2855 | if (x >= width) { |
a7986f7b KL |
2856 | continue; |
2857 | } | |
2858 | for (int y = w.getY(); y < w.getY() + w.getHeight(); y++) { | |
9ff1c0e3 KL |
2859 | if (y < 0) { |
2860 | continue; | |
2861 | } | |
8c236a98 | 2862 | if (y >= height) { |
a7986f7b KL |
2863 | continue; |
2864 | } | |
2865 | overlapMatrix[x][y]++; | |
2866 | } | |
2867 | } | |
2868 | } | |
2869 | ||
2870 | long oldOverlapTotal = 0; | |
2871 | long oldOverlapN = 0; | |
2872 | for (int x = 0; x < width; x++) { | |
2873 | for (int y = 0; y < height; y++) { | |
2874 | oldOverlapTotal += overlapMatrix[x][y]; | |
2875 | if (overlapMatrix[x][y] > 0) { | |
2876 | oldOverlapN++; | |
2877 | } | |
2878 | } | |
2879 | } | |
2880 | ||
2881 | ||
2882 | double oldOverlapAvg = (double) oldOverlapTotal / (double) oldOverlapN; | |
2883 | boolean first = true; | |
2884 | int windowX = window.getX(); | |
2885 | int windowY = window.getY(); | |
2886 | ||
2887 | // For each possible (x, y) position for the new window, compute a | |
2888 | // new overlap matrix. | |
2889 | for (int x = xMin; x < xMax; x++) { | |
2890 | for (int y = yMin; y < yMax; y++) { | |
2891 | ||
2892 | // Start with the matrix minus this window. | |
2893 | int newMatrix[][] = new int[width][height]; | |
2894 | for (int mx = 0; mx < width; mx++) { | |
2895 | for (int my = 0; my < height; my++) { | |
2896 | newMatrix[mx][my] = overlapMatrix[mx][my]; | |
2897 | } | |
2898 | } | |
2899 | ||
2900 | // Add this window's values to the new overlap matrix. | |
2901 | long newOverlapTotal = 0; | |
2902 | long newOverlapN = 0; | |
2903 | // Start by adding each new cell. | |
2904 | for (int wx = x; wx < x + window.getWidth(); wx++) { | |
8c236a98 | 2905 | if (wx >= width) { |
a7986f7b KL |
2906 | continue; |
2907 | } | |
2908 | for (int wy = y; wy < y + window.getHeight(); wy++) { | |
8c236a98 | 2909 | if (wy >= height) { |
a7986f7b KL |
2910 | continue; |
2911 | } | |
2912 | newMatrix[wx][wy]++; | |
2913 | } | |
2914 | } | |
2915 | // Now figure out the new value for total coverage. | |
2916 | for (int mx = 0; mx < width; mx++) { | |
2917 | for (int my = 0; my < height; my++) { | |
2918 | newOverlapTotal += newMatrix[x][y]; | |
2919 | if (newMatrix[mx][my] > 0) { | |
2920 | newOverlapN++; | |
2921 | } | |
2922 | } | |
2923 | } | |
2924 | double newOverlapAvg = (double) newOverlapTotal / (double) newOverlapN; | |
2925 | ||
2926 | if (first) { | |
2927 | // First time: just record what we got. | |
2928 | oldOverlapAvg = newOverlapAvg; | |
2929 | first = false; | |
2930 | } else { | |
2931 | // All other times: pick a new best (x, y) and save the | |
2932 | // overlap value. | |
2933 | if (newOverlapAvg < oldOverlapAvg) { | |
2934 | windowX = x; | |
2935 | windowY = y; | |
2936 | oldOverlapAvg = newOverlapAvg; | |
2937 | } | |
2938 | } | |
2939 | ||
2940 | } // for (int x = xMin; x < xMax; x++) | |
2941 | ||
2942 | } // for (int y = yMin; y < yMax; y++) | |
2943 | ||
2944 | // Finally, set the window's new coordinates. | |
2945 | window.setX(windowX); | |
2946 | window.setY(windowY); | |
2947 | } | |
2948 | ||
a69ed767 | 2949 | // ------------------------------------------------------------------------ |
2ce6dab2 KL |
2950 | // TMenu management ------------------------------------------------------- |
2951 | // ------------------------------------------------------------------------ | |
2952 | ||
fca67db0 KL |
2953 | /** |
2954 | * Check if a mouse event would hit either the active menu or any open | |
2955 | * sub-menus. | |
2956 | * | |
2957 | * @param mouse mouse event | |
2958 | * @return true if the mouse would hit the active menu or an open | |
2959 | * sub-menu | |
2960 | */ | |
2961 | private boolean mouseOnMenu(final TMouseEvent mouse) { | |
2962 | assert (activeMenu != null); | |
a69ed767 | 2963 | List<TMenu> menus = new ArrayList<TMenu>(subMenus); |
fca67db0 KL |
2964 | Collections.reverse(menus); |
2965 | for (TMenu menu: menus) { | |
2966 | if (menu.mouseWouldHit(mouse)) { | |
2967 | return true; | |
2968 | } | |
2969 | } | |
2970 | return activeMenu.mouseWouldHit(mouse); | |
2971 | } | |
2972 | ||
2973 | /** | |
2974 | * See if we need to switch window or activate the menu based on | |
2975 | * a mouse click. | |
2976 | * | |
2977 | * @param mouse mouse event | |
2978 | */ | |
2979 | private void checkSwitchFocus(final TMouseEvent mouse) { | |
2980 | ||
2981 | if ((mouse.getType() == TMouseEvent.Type.MOUSE_DOWN) | |
2982 | && (activeMenu != null) | |
2983 | && (mouse.getAbsoluteY() != 0) | |
2984 | && (!mouseOnMenu(mouse)) | |
2985 | ) { | |
2986 | // They clicked outside the active menu, turn it off | |
2987 | activeMenu.setActive(false); | |
2988 | activeMenu = null; | |
2989 | for (TMenu menu: subMenus) { | |
2990 | menu.setActive(false); | |
2991 | } | |
2992 | subMenus.clear(); | |
2993 | // Continue checks | |
2994 | } | |
2995 | ||
2996 | // See if they hit the menu bar | |
2997 | if ((mouse.getType() == TMouseEvent.Type.MOUSE_DOWN) | |
7c870d89 | 2998 | && (mouse.isMouse1()) |
fca67db0 | 2999 | && (!modalWindowActive()) |
9696a8f6 | 3000 | && (!overrideMenuWindowActive()) |
fca67db0 | 3001 | && (mouse.getAbsoluteY() == 0) |
2bb26984 | 3002 | && (hideMenuBar == false) |
fca67db0 KL |
3003 | ) { |
3004 | ||
3005 | for (TMenu menu: subMenus) { | |
3006 | menu.setActive(false); | |
3007 | } | |
3008 | subMenus.clear(); | |
3009 | ||
3010 | // They selected the menu, go activate it | |
3011 | for (TMenu menu: menus) { | |
159f076d KL |
3012 | if ((mouse.getAbsoluteX() >= menu.getTitleX()) |
3013 | && (mouse.getAbsoluteX() < menu.getTitleX() | |
e820d5dd | 3014 | + StringUtils.width(menu.getTitle()) + 2) |
fca67db0 KL |
3015 | ) { |
3016 | menu.setActive(true); | |
3017 | activeMenu = menu; | |
3018 | } else { | |
3019 | menu.setActive(false); | |
3020 | } | |
3021 | } | |
fca67db0 KL |
3022 | return; |
3023 | } | |
3024 | ||
3025 | // See if they hit the menu bar | |
3026 | if ((mouse.getType() == TMouseEvent.Type.MOUSE_MOTION) | |
7c870d89 | 3027 | && (mouse.isMouse1()) |
fca67db0 KL |
3028 | && (activeMenu != null) |
3029 | && (mouse.getAbsoluteY() == 0) | |
2bb26984 | 3030 | && (hideMenuBar == false) |
fca67db0 KL |
3031 | ) { |
3032 | ||
3033 | TMenu oldMenu = activeMenu; | |
3034 | for (TMenu menu: subMenus) { | |
3035 | menu.setActive(false); | |
3036 | } | |
3037 | subMenus.clear(); | |
3038 | ||
3039 | // See if we should switch menus | |
3040 | for (TMenu menu: menus) { | |
159f076d KL |
3041 | if ((mouse.getAbsoluteX() >= menu.getTitleX()) |
3042 | && (mouse.getAbsoluteX() < menu.getTitleX() | |
e820d5dd | 3043 | + StringUtils.width(menu.getTitle()) + 2) |
fca67db0 KL |
3044 | ) { |
3045 | menu.setActive(true); | |
3046 | activeMenu = menu; | |
3047 | } | |
3048 | } | |
3049 | if (oldMenu != activeMenu) { | |
3050 | // They switched menus | |
3051 | oldMenu.setActive(false); | |
3052 | } | |
fca67db0 KL |
3053 | return; |
3054 | } | |
3055 | ||
72fca17b KL |
3056 | // If a menu is still active, don't switch windows |
3057 | if (activeMenu != null) { | |
fca67db0 KL |
3058 | return; |
3059 | } | |
3060 | ||
72fca17b KL |
3061 | // Only switch if there are multiple windows |
3062 | if (windows.size() < 2) { | |
fca67db0 KL |
3063 | return; |
3064 | } | |
3065 | ||
72fca17b KL |
3066 | if (((focusFollowsMouse == true) |
3067 | && (mouse.getType() == TMouseEvent.Type.MOUSE_MOTION)) | |
e23ea538 | 3068 | || (mouse.getType() == TMouseEvent.Type.MOUSE_DOWN) |
72fca17b KL |
3069 | ) { |
3070 | synchronized (windows) { | |
72fca17b KL |
3071 | if (windows.get(0).isModal()) { |
3072 | // Modal windows don't switch | |
3073 | return; | |
3074 | } | |
fca67db0 | 3075 | |
72fca17b KL |
3076 | for (TWindow window: windows) { |
3077 | assert (!window.isModal()); | |
92453213 | 3078 | |
72fca17b KL |
3079 | if (window.isHidden()) { |
3080 | assert (!window.isActive()); | |
3081 | continue; | |
3082 | } | |
92453213 | 3083 | |
72fca17b | 3084 | if (window.mouseWouldHit(mouse)) { |
3d3e09e6 | 3085 | activateWindow(window); |
bb35d919 KL |
3086 | return; |
3087 | } | |
fca67db0 | 3088 | } |
fca67db0 | 3089 | } |
72fca17b KL |
3090 | |
3091 | // Clicked on the background, nothing to do | |
3092 | return; | |
fca67db0 KL |
3093 | } |
3094 | ||
72fca17b KL |
3095 | // Nothing to do: this isn't a mouse up, or focus isn't following |
3096 | // mouse. | |
fca67db0 KL |
3097 | return; |
3098 | } | |
3099 | ||
3100 | /** | |
3101 | * Turn off the menu. | |
3102 | */ | |
928811d8 | 3103 | public final void closeMenu() { |
fca67db0 KL |
3104 | if (activeMenu != null) { |
3105 | activeMenu.setActive(false); | |
3106 | activeMenu = null; | |
3107 | for (TMenu menu: subMenus) { | |
3108 | menu.setActive(false); | |
3109 | } | |
3110 | subMenus.clear(); | |
3111 | } | |
fca67db0 KL |
3112 | } |
3113 | ||
e8a11f98 KL |
3114 | /** |
3115 | * Get a (shallow) copy of the menu list. | |
3116 | * | |
3117 | * @return a copy of the menu list | |
3118 | */ | |
3119 | public final List<TMenu> getAllMenus() { | |
a69ed767 | 3120 | return new ArrayList<TMenu>(menus); |
e8a11f98 KL |
3121 | } |
3122 | ||
3123 | /** | |
3124 | * Add a top-level menu to the list. | |
3125 | * | |
3126 | * @param menu the menu to add | |
3127 | * @throws IllegalArgumentException if the menu is already used in | |
3128 | * another TApplication | |
3129 | */ | |
3130 | public final void addMenu(final TMenu menu) { | |
3131 | if ((menu.getApplication() != null) | |
3132 | && (menu.getApplication() != this) | |
3133 | ) { | |
3134 | throw new IllegalArgumentException("Menu " + menu + " is already " + | |
3135 | "part of application " + menu.getApplication()); | |
3136 | } | |
3137 | closeMenu(); | |
3138 | menus.add(menu); | |
3139 | recomputeMenuX(); | |
3140 | } | |
3141 | ||
3142 | /** | |
3143 | * Remove a top-level menu from the list. | |
3144 | * | |
3145 | * @param menu the menu to remove | |
3146 | * @throws IllegalArgumentException if the menu is already used in | |
3147 | * another TApplication | |
3148 | */ | |
3149 | public final void removeMenu(final TMenu menu) { | |
3150 | if ((menu.getApplication() != null) | |
3151 | && (menu.getApplication() != this) | |
3152 | ) { | |
3153 | throw new IllegalArgumentException("Menu " + menu + " is already " + | |
3154 | "part of application " + menu.getApplication()); | |
3155 | } | |
3156 | closeMenu(); | |
3157 | menus.remove(menu); | |
3158 | recomputeMenuX(); | |
3159 | } | |
3160 | ||
fca67db0 KL |
3161 | /** |
3162 | * Turn off a sub-menu. | |
3163 | */ | |
928811d8 | 3164 | public final void closeSubMenu() { |
fca67db0 KL |
3165 | assert (activeMenu != null); |
3166 | TMenu item = subMenus.get(subMenus.size() - 1); | |
3167 | assert (item != null); | |
3168 | item.setActive(false); | |
3169 | subMenus.remove(subMenus.size() - 1); | |
fca67db0 KL |
3170 | } |
3171 | ||
3172 | /** | |
3173 | * Switch to the next menu. | |
3174 | * | |
3175 | * @param forward if true, then switch to the next menu in the list, | |
3176 | * otherwise switch to the previous menu in the list | |
3177 | */ | |
928811d8 | 3178 | public final void switchMenu(final boolean forward) { |
fca67db0 | 3179 | assert (activeMenu != null); |
2bb26984 | 3180 | assert (hideMenuBar == false); |
fca67db0 KL |
3181 | |
3182 | for (TMenu menu: subMenus) { | |
3183 | menu.setActive(false); | |
3184 | } | |
3185 | subMenus.clear(); | |
3186 | ||
3187 | for (int i = 0; i < menus.size(); i++) { | |
3188 | if (activeMenu == menus.get(i)) { | |
3189 | if (forward) { | |
3190 | if (i < menus.size() - 1) { | |
3191 | i++; | |
a69ed767 KL |
3192 | } else { |
3193 | i = 0; | |
fca67db0 KL |
3194 | } |
3195 | } else { | |
3196 | if (i > 0) { | |
3197 | i--; | |
a69ed767 KL |
3198 | } else { |
3199 | i = menus.size() - 1; | |
fca67db0 KL |
3200 | } |
3201 | } | |
3202 | activeMenu.setActive(false); | |
3203 | activeMenu = menus.get(i); | |
3204 | activeMenu.setActive(true); | |
fca67db0 KL |
3205 | return; |
3206 | } | |
3207 | } | |
3208 | } | |
3209 | ||
928811d8 | 3210 | /** |
efb7af1f KL |
3211 | * Add a menu item to the global list. If it has a keyboard accelerator, |
3212 | * that will be added the global hash. | |
928811d8 | 3213 | * |
efb7af1f | 3214 | * @param item the menu item |
928811d8 | 3215 | */ |
efb7af1f KL |
3216 | public final void addMenuItem(final TMenuItem item) { |
3217 | menuItems.add(item); | |
3218 | ||
3219 | TKeypress key = item.getKey(); | |
3220 | if (key != null) { | |
3221 | synchronized (accelerators) { | |
3222 | assert (accelerators.get(key) == null); | |
3223 | accelerators.put(key.toLowerCase(), item); | |
3224 | } | |
3225 | } | |
3226 | } | |
3227 | ||
3228 | /** | |
3229 | * Disable one menu item. | |
3230 | * | |
3231 | * @param id the menu item ID | |
3232 | */ | |
3233 | public final void disableMenuItem(final int id) { | |
3234 | for (TMenuItem item: menuItems) { | |
3235 | if (item.getId() == id) { | |
3236 | item.setEnabled(false); | |
3237 | } | |
3238 | } | |
3239 | } | |
e826b451 | 3240 | |
efb7af1f KL |
3241 | /** |
3242 | * Disable the range of menu items with ID's between lower and upper, | |
3243 | * inclusive. | |
3244 | * | |
3245 | * @param lower the lowest menu item ID | |
3246 | * @param upper the highest menu item ID | |
3247 | */ | |
3248 | public final void disableMenuItems(final int lower, final int upper) { | |
3249 | for (TMenuItem item: menuItems) { | |
3250 | if ((item.getId() >= lower) && (item.getId() <= upper)) { | |
3251 | item.setEnabled(false); | |
a69ed767 | 3252 | item.getParent().activate(0); |
efb7af1f KL |
3253 | } |
3254 | } | |
3255 | } | |
3256 | ||
3257 | /** | |
3258 | * Enable one menu item. | |
3259 | * | |
3260 | * @param id the menu item ID | |
3261 | */ | |
3262 | public final void enableMenuItem(final int id) { | |
3263 | for (TMenuItem item: menuItems) { | |
3264 | if (item.getId() == id) { | |
3265 | item.setEnabled(true); | |
a69ed767 | 3266 | item.getParent().activate(0); |
efb7af1f KL |
3267 | } |
3268 | } | |
3269 | } | |
3270 | ||
3271 | /** | |
3272 | * Enable the range of menu items with ID's between lower and upper, | |
3273 | * inclusive. | |
3274 | * | |
3275 | * @param lower the lowest menu item ID | |
3276 | * @param upper the highest menu item ID | |
3277 | */ | |
3278 | public final void enableMenuItems(final int lower, final int upper) { | |
3279 | for (TMenuItem item: menuItems) { | |
3280 | if ((item.getId() >= lower) && (item.getId() <= upper)) { | |
3281 | item.setEnabled(true); | |
a69ed767 | 3282 | item.getParent().activate(0); |
efb7af1f | 3283 | } |
e826b451 | 3284 | } |
928811d8 KL |
3285 | } |
3286 | ||
77961919 KL |
3287 | /** |
3288 | * Get the menu item associated with this ID. | |
3289 | * | |
3290 | * @param id the menu item ID | |
3291 | * @return the menu item, or null if not found | |
3292 | */ | |
3293 | public final TMenuItem getMenuItem(final int id) { | |
3294 | for (TMenuItem item: menuItems) { | |
3295 | if (item.getId() == id) { | |
3296 | return item; | |
3297 | } | |
3298 | } | |
3299 | return null; | |
3300 | } | |
3301 | ||
928811d8 KL |
3302 | /** |
3303 | * Recompute menu x positions based on their title length. | |
3304 | */ | |
3305 | public final void recomputeMenuX() { | |
3306 | int x = 0; | |
3307 | for (TMenu menu: menus) { | |
3308 | menu.setX(x); | |
159f076d | 3309 | menu.setTitleX(x); |
e820d5dd | 3310 | x += StringUtils.width(menu.getTitle()) + 2; |
68c5cd6b KL |
3311 | |
3312 | // Don't let the menu window exceed the screen width | |
3313 | int rightEdge = menu.getX() + menu.getWidth(); | |
3314 | if (rightEdge > getScreen().getWidth()) { | |
3315 | menu.setX(getScreen().getWidth() - menu.getWidth()); | |
3316 | } | |
928811d8 KL |
3317 | } |
3318 | } | |
3319 | ||
b2d49e0f KL |
3320 | /** |
3321 | * Post an event to process. | |
3322 | * | |
3323 | * @param event new event to add to the queue | |
3324 | */ | |
3325 | public final void postEvent(final TInputEvent event) { | |
3326 | synchronized (this) { | |
3327 | synchronized (fillEventQueue) { | |
3328 | fillEventQueue.add(event); | |
3329 | } | |
3330 | if (debugThreads) { | |
3331 | System.err.println(System.currentTimeMillis() + " " + | |
3332 | Thread.currentThread() + " postEvent() wake up main"); | |
3333 | } | |
3334 | this.notify(); | |
3335 | } | |
3336 | } | |
3337 | ||
928811d8 KL |
3338 | /** |
3339 | * Post an event to process and turn off the menu. | |
3340 | * | |
3341 | * @param event new event to add to the queue | |
3342 | */ | |
5dfd1c11 | 3343 | public final void postMenuEvent(final TInputEvent event) { |
be72cb5c KL |
3344 | synchronized (this) { |
3345 | synchronized (fillEventQueue) { | |
3346 | fillEventQueue.add(event); | |
3347 | } | |
3348 | if (debugThreads) { | |
3349 | System.err.println(System.currentTimeMillis() + " " + | |
3350 | Thread.currentThread() + " postMenuEvent() wake up main"); | |
3351 | } | |
3352 | closeMenu(); | |
3353 | this.notify(); | |
8e688b92 | 3354 | } |
928811d8 KL |
3355 | } |
3356 | ||
3357 | /** | |
3358 | * Add a sub-menu to the list of open sub-menus. | |
3359 | * | |
3360 | * @param menu sub-menu | |
3361 | */ | |
3362 | public final void addSubMenu(final TMenu menu) { | |
3363 | subMenus.add(menu); | |
3364 | } | |
3365 | ||
8e688b92 KL |
3366 | /** |
3367 | * Convenience function to add a top-level menu. | |
3368 | * | |
3369 | * @param title menu title | |
3370 | * @return the new menu | |
3371 | */ | |
87a17f3c | 3372 | public final TMenu addMenu(final String title) { |
8e688b92 KL |
3373 | int x = 0; |
3374 | int y = 0; | |
3375 | TMenu menu = new TMenu(this, x, y, title); | |
3376 | menus.add(menu); | |
3377 | recomputeMenuX(); | |
3378 | return menu; | |
3379 | } | |
3380 | ||
e23ea538 KL |
3381 | /** |
3382 | * Convenience function to add a default tools (hamburger) menu. | |
3383 | * | |
3384 | * @return the new menu | |
3385 | */ | |
3386 | public final TMenu addToolMenu() { | |
3387 | TMenu toolMenu = addMenu(i18n.getString("toolMenuTitle")); | |
3388 | toolMenu.addDefaultItem(TMenu.MID_REPAINT); | |
3389 | toolMenu.addDefaultItem(TMenu.MID_VIEW_IMAGE); | |
a75902fa | 3390 | toolMenu.addDefaultItem(TMenu.MID_SCREEN_OPTIONS); |
e23ea538 KL |
3391 | TStatusBar toolStatusBar = toolMenu.newStatusBar(i18n. |
3392 | getString("toolMenuStatus")); | |
3393 | toolStatusBar.addShortcutKeypress(kbF1, cmHelp, i18n.getString("Help")); | |
3394 | return toolMenu; | |
3395 | } | |
3396 | ||
8e688b92 KL |
3397 | /** |
3398 | * Convenience function to add a default "File" menu. | |
3399 | * | |
3400 | * @return the new menu | |
3401 | */ | |
3402 | public final TMenu addFileMenu() { | |
339652cc | 3403 | TMenu fileMenu = addMenu(i18n.getString("fileMenuTitle")); |
8e688b92 | 3404 | fileMenu.addDefaultItem(TMenu.MID_SHELL); |
b9724916 | 3405 | fileMenu.addSeparator(); |
8e688b92 | 3406 | fileMenu.addDefaultItem(TMenu.MID_EXIT); |
339652cc KL |
3407 | TStatusBar statusBar = fileMenu.newStatusBar(i18n. |
3408 | getString("fileMenuStatus")); | |
3409 | statusBar.addShortcutKeypress(kbF1, cmHelp, i18n.getString("Help")); | |
8e688b92 KL |
3410 | return fileMenu; |
3411 | } | |
3412 | ||
3413 | /** | |
3414 | * Convenience function to add a default "Edit" menu. | |
3415 | * | |
3416 | * @return the new menu | |
3417 | */ | |
3418 | public final TMenu addEditMenu() { | |
339652cc | 3419 | TMenu editMenu = addMenu(i18n.getString("editMenuTitle")); |
51e46b3e KL |
3420 | editMenu.addDefaultItem(TMenu.MID_CUT, false); |
3421 | editMenu.addDefaultItem(TMenu.MID_COPY, false); | |
3422 | editMenu.addDefaultItem(TMenu.MID_PASTE, false); | |
3423 | editMenu.addDefaultItem(TMenu.MID_CLEAR, false); | |
339652cc KL |
3424 | TStatusBar statusBar = editMenu.newStatusBar(i18n. |
3425 | getString("editMenuStatus")); | |
3426 | statusBar.addShortcutKeypress(kbF1, cmHelp, i18n.getString("Help")); | |
8e688b92 KL |
3427 | return editMenu; |
3428 | } | |
3429 | ||
3430 | /** | |
3431 | * Convenience function to add a default "Window" menu. | |
3432 | * | |
3433 | * @return the new menu | |
3434 | */ | |
c6940ed9 | 3435 | public final TMenu addWindowMenu() { |
339652cc | 3436 | TMenu windowMenu = addMenu(i18n.getString("windowMenuTitle")); |
8e688b92 KL |
3437 | windowMenu.addDefaultItem(TMenu.MID_TILE); |
3438 | windowMenu.addDefaultItem(TMenu.MID_CASCADE); | |
3439 | windowMenu.addDefaultItem(TMenu.MID_CLOSE_ALL); | |
3440 | windowMenu.addSeparator(); | |
3441 | windowMenu.addDefaultItem(TMenu.MID_WINDOW_MOVE); | |
3442 | windowMenu.addDefaultItem(TMenu.MID_WINDOW_ZOOM); | |
3443 | windowMenu.addDefaultItem(TMenu.MID_WINDOW_NEXT); | |
3444 | windowMenu.addDefaultItem(TMenu.MID_WINDOW_PREVIOUS); | |
3445 | windowMenu.addDefaultItem(TMenu.MID_WINDOW_CLOSE); | |
339652cc KL |
3446 | TStatusBar statusBar = windowMenu.newStatusBar(i18n. |
3447 | getString("windowMenuStatus")); | |
3448 | statusBar.addShortcutKeypress(kbF1, cmHelp, i18n.getString("Help")); | |
8e688b92 KL |
3449 | return windowMenu; |
3450 | } | |
3451 | ||
55d2b2c2 KL |
3452 | /** |
3453 | * Convenience function to add a default "Help" menu. | |
3454 | * | |
3455 | * @return the new menu | |
3456 | */ | |
3457 | public final TMenu addHelpMenu() { | |
339652cc | 3458 | TMenu helpMenu = addMenu(i18n.getString("helpMenuTitle")); |
55d2b2c2 KL |
3459 | helpMenu.addDefaultItem(TMenu.MID_HELP_CONTENTS); |
3460 | helpMenu.addDefaultItem(TMenu.MID_HELP_INDEX); | |
3461 | helpMenu.addDefaultItem(TMenu.MID_HELP_SEARCH); | |
3462 | helpMenu.addDefaultItem(TMenu.MID_HELP_PREVIOUS); | |
3463 | helpMenu.addDefaultItem(TMenu.MID_HELP_HELP); | |
3464 | helpMenu.addDefaultItem(TMenu.MID_HELP_ACTIVE_FILE); | |
3465 | helpMenu.addSeparator(); | |
3466 | helpMenu.addDefaultItem(TMenu.MID_ABOUT); | |
339652cc KL |
3467 | TStatusBar statusBar = helpMenu.newStatusBar(i18n. |
3468 | getString("helpMenuStatus")); | |
3469 | statusBar.addShortcutKeypress(kbF1, cmHelp, i18n.getString("Help")); | |
55d2b2c2 KL |
3470 | return helpMenu; |
3471 | } | |
3472 | ||
1dac6b8d KL |
3473 | /** |
3474 | * Convenience function to add a default "Table" menu. | |
3475 | * | |
3476 | * @return the new menu | |
3477 | */ | |
3478 | public final TMenu addTableMenu() { | |
3479 | TMenu tableMenu = addMenu(i18n.getString("tableMenuTitle")); | |
2b427404 KL |
3480 | tableMenu.addDefaultItem(TMenu.MID_TABLE_RENAME_COLUMN, false); |
3481 | tableMenu.addDefaultItem(TMenu.MID_TABLE_RENAME_ROW, false); | |
3482 | tableMenu.addSeparator(); | |
3483 | ||
77961919 KL |
3484 | TSubMenu viewMenu = tableMenu.addSubMenu(i18n. |
3485 | getString("tableSubMenuView")); | |
3486 | viewMenu.addDefaultItem(TMenu.MID_TABLE_VIEW_ROW_LABELS, false); | |
3487 | viewMenu.addDefaultItem(TMenu.MID_TABLE_VIEW_COLUMN_LABELS, false); | |
3488 | viewMenu.addDefaultItem(TMenu.MID_TABLE_VIEW_HIGHLIGHT_ROW, false); | |
3489 | viewMenu.addDefaultItem(TMenu.MID_TABLE_VIEW_HIGHLIGHT_COLUMN, false); | |
3490 | ||
1dac6b8d KL |
3491 | TSubMenu borderMenu = tableMenu.addSubMenu(i18n. |
3492 | getString("tableSubMenuBorders")); | |
3493 | borderMenu.addDefaultItem(TMenu.MID_TABLE_BORDER_NONE, false); | |
3494 | borderMenu.addDefaultItem(TMenu.MID_TABLE_BORDER_ALL, false); | |
e9bb3c1e KL |
3495 | borderMenu.addDefaultItem(TMenu.MID_TABLE_BORDER_CELL_NONE, false); |
3496 | borderMenu.addDefaultItem(TMenu.MID_TABLE_BORDER_CELL_ALL, false); | |
1dac6b8d KL |
3497 | borderMenu.addDefaultItem(TMenu.MID_TABLE_BORDER_RIGHT, false); |
3498 | borderMenu.addDefaultItem(TMenu.MID_TABLE_BORDER_LEFT, false); | |
3499 | borderMenu.addDefaultItem(TMenu.MID_TABLE_BORDER_TOP, false); | |
3500 | borderMenu.addDefaultItem(TMenu.MID_TABLE_BORDER_BOTTOM, false); | |
3501 | borderMenu.addDefaultItem(TMenu.MID_TABLE_BORDER_DOUBLE_BOTTOM, false); | |
3502 | borderMenu.addDefaultItem(TMenu.MID_TABLE_BORDER_THICK_BOTTOM, false); | |
3503 | TSubMenu deleteMenu = tableMenu.addSubMenu(i18n. | |
3504 | getString("tableSubMenuDelete")); | |
3505 | deleteMenu.addDefaultItem(TMenu.MID_TABLE_DELETE_LEFT, false); | |
3506 | deleteMenu.addDefaultItem(TMenu.MID_TABLE_DELETE_UP, false); | |
3507 | deleteMenu.addDefaultItem(TMenu.MID_TABLE_DELETE_ROW, false); | |
3508 | deleteMenu.addDefaultItem(TMenu.MID_TABLE_DELETE_COLUMN, false); | |
3509 | TSubMenu insertMenu = tableMenu.addSubMenu(i18n. | |
3510 | getString("tableSubMenuInsert")); | |
3511 | insertMenu.addDefaultItem(TMenu.MID_TABLE_INSERT_LEFT, false); | |
3512 | insertMenu.addDefaultItem(TMenu.MID_TABLE_INSERT_RIGHT, false); | |
3513 | insertMenu.addDefaultItem(TMenu.MID_TABLE_INSERT_ABOVE, false); | |
3514 | insertMenu.addDefaultItem(TMenu.MID_TABLE_INSERT_BELOW, false); | |
3515 | TSubMenu columnMenu = tableMenu.addSubMenu(i18n. | |
3516 | getString("tableSubMenuColumn")); | |
3517 | columnMenu.addDefaultItem(TMenu.MID_TABLE_COLUMN_NARROW, false); | |
3518 | columnMenu.addDefaultItem(TMenu.MID_TABLE_COLUMN_WIDEN, false); | |
3519 | TSubMenu fileMenu = tableMenu.addSubMenu(i18n. | |
3520 | getString("tableSubMenuFile")); | |
f528c340 | 3521 | fileMenu.addDefaultItem(TMenu.MID_TABLE_FILE_OPEN_CSV, false); |
1dac6b8d KL |
3522 | fileMenu.addDefaultItem(TMenu.MID_TABLE_FILE_SAVE_CSV, false); |
3523 | fileMenu.addDefaultItem(TMenu.MID_TABLE_FILE_SAVE_TEXT, false); | |
3524 | ||
3525 | TStatusBar statusBar = tableMenu.newStatusBar(i18n. | |
3526 | getString("tableMenuStatus")); | |
3527 | statusBar.addShortcutKeypress(kbF1, cmHelp, i18n.getString("Help")); | |
3528 | return tableMenu; | |
3529 | } | |
3530 | ||
2ce6dab2 KL |
3531 | // ------------------------------------------------------------------------ |
3532 | // TTimer management ------------------------------------------------------ | |
3533 | // ------------------------------------------------------------------------ | |
3534 | ||
8e688b92 | 3535 | /** |
2ce6dab2 KL |
3536 | * Get the amount of time I can sleep before missing a Timer tick. |
3537 | * | |
3538 | * @param timeout = initial (maximum) timeout in millis | |
3539 | * @return number of milliseconds between now and the next timer event | |
8e688b92 | 3540 | */ |
2ce6dab2 KL |
3541 | private long getSleepTime(final long timeout) { |
3542 | Date now = new Date(); | |
3543 | long nowTime = now.getTime(); | |
3544 | long sleepTime = timeout; | |
2ce6dab2 | 3545 | |
be72cb5c KL |
3546 | synchronized (timers) { |
3547 | for (TTimer timer: timers) { | |
3548 | long nextTickTime = timer.getNextTick().getTime(); | |
3549 | if (nextTickTime < nowTime) { | |
3550 | return 0; | |
3551 | } | |
3552 | ||
3553 | long timeDifference = nextTickTime - nowTime; | |
3554 | if (timeDifference < sleepTime) { | |
3555 | sleepTime = timeDifference; | |
3556 | } | |
8e688b92 KL |
3557 | } |
3558 | } | |
be72cb5c | 3559 | |
2ce6dab2 KL |
3560 | assert (sleepTime >= 0); |
3561 | assert (sleepTime <= timeout); | |
3562 | return sleepTime; | |
8e688b92 KL |
3563 | } |
3564 | ||
d502a0e9 KL |
3565 | /** |
3566 | * Convenience function to add a timer. | |
3567 | * | |
3568 | * @param duration number of milliseconds to wait between ticks | |
3569 | * @param recurring if true, re-schedule this timer after every tick | |
3570 | * @param action function to call when button is pressed | |
c6940ed9 | 3571 | * @return the timer |
d502a0e9 KL |
3572 | */ |
3573 | public final TTimer addTimer(final long duration, final boolean recurring, | |
3574 | final TAction action) { | |
3575 | ||
3576 | TTimer timer = new TTimer(duration, recurring, action); | |
3577 | synchronized (timers) { | |
3578 | timers.add(timer); | |
3579 | } | |
3580 | return timer; | |
3581 | } | |
3582 | ||
3583 | /** | |
3584 | * Convenience function to remove a timer. | |
3585 | * | |
3586 | * @param timer timer to remove | |
3587 | */ | |
3588 | public final void removeTimer(final TTimer timer) { | |
3589 | synchronized (timers) { | |
3590 | timers.remove(timer); | |
3591 | } | |
3592 | } | |
3593 | ||
2ce6dab2 KL |
3594 | // ------------------------------------------------------------------------ |
3595 | // Other TWindow constructors --------------------------------------------- | |
3596 | // ------------------------------------------------------------------------ | |
3597 | ||
c6940ed9 KL |
3598 | /** |
3599 | * Convenience function to spawn a message box. | |
3600 | * | |
3601 | * @param title window title, will be centered along the top border | |
3602 | * @param caption message to display. Use embedded newlines to get a | |
3603 | * multi-line box. | |
3604 | * @return the new message box | |
3605 | */ | |
3606 | public final TMessageBox messageBox(final String title, | |
3607 | final String caption) { | |
3608 | ||
3609 | return new TMessageBox(this, title, caption, TMessageBox.Type.OK); | |
3610 | } | |
3611 | ||
3612 | /** | |
3613 | * Convenience function to spawn a message box. | |
3614 | * | |
3615 | * @param title window title, will be centered along the top border | |
3616 | * @param caption message to display. Use embedded newlines to get a | |
3617 | * multi-line box. | |
3618 | * @param type one of the TMessageBox.Type constants. Default is | |
3619 | * Type.OK. | |
3620 | * @return the new message box | |
3621 | */ | |
3622 | public final TMessageBox messageBox(final String title, | |
3623 | final String caption, final TMessageBox.Type type) { | |
3624 | ||
3625 | return new TMessageBox(this, title, caption, type); | |
3626 | } | |
3627 | ||
3628 | /** | |
3629 | * Convenience function to spawn an input box. | |
3630 | * | |
3631 | * @param title window title, will be centered along the top border | |
3632 | * @param caption message to display. Use embedded newlines to get a | |
3633 | * multi-line box. | |
3634 | * @return the new input box | |
3635 | */ | |
3636 | public final TInputBox inputBox(final String title, final String caption) { | |
3637 | ||
3638 | return new TInputBox(this, title, caption); | |
3639 | } | |
3640 | ||
3641 | /** | |
3642 | * Convenience function to spawn an input box. | |
3643 | * | |
3644 | * @param title window title, will be centered along the top border | |
3645 | * @param caption message to display. Use embedded newlines to get a | |
3646 | * multi-line box. | |
3647 | * @param text initial text to seed the field with | |
3648 | * @return the new input box | |
3649 | */ | |
3650 | public final TInputBox inputBox(final String title, final String caption, | |
3651 | final String text) { | |
3652 | ||
3653 | return new TInputBox(this, title, caption, text); | |
3654 | } | |
1ac2ccb1 | 3655 | |
72b6bd90 KL |
3656 | /** |
3657 | * Convenience function to spawn an input box. | |
3658 | * | |
3659 | * @param title window title, will be centered along the top border | |
3660 | * @param caption message to display. Use embedded newlines to get a | |
3661 | * multi-line box. | |
3662 | * @param text initial text to seed the field with | |
3663 | * @param type one of the Type constants. Default is Type.OK. | |
3664 | * @return the new input box | |
3665 | */ | |
3666 | public final TInputBox inputBox(final String title, final String caption, | |
3667 | final String text, final TInputBox.Type type) { | |
3668 | ||
3669 | return new TInputBox(this, title, caption, text, type); | |
3670 | } | |
3671 | ||
34a42e78 KL |
3672 | /** |
3673 | * Convenience function to open a terminal window. | |
3674 | * | |
3675 | * @param x column relative to parent | |
3676 | * @param y row relative to parent | |
3677 | * @return the terminal new window | |
3678 | */ | |
3679 | public final TTerminalWindow openTerminal(final int x, final int y) { | |
3680 | return openTerminal(x, y, TWindow.RESIZABLE); | |
3681 | } | |
3682 | ||
a69ed767 KL |
3683 | /** |
3684 | * Convenience function to open a terminal window. | |
3685 | * | |
3686 | * @param x column relative to parent | |
3687 | * @param y row relative to parent | |
3688 | * @param closeOnExit if true, close the window when the command exits | |
3689 | * @return the terminal new window | |
3690 | */ | |
3691 | public final TTerminalWindow openTerminal(final int x, final int y, | |
3692 | final boolean closeOnExit) { | |
3693 | ||
3694 | return openTerminal(x, y, TWindow.RESIZABLE, closeOnExit); | |
3695 | } | |
3696 | ||
34a42e78 KL |
3697 | /** |
3698 | * Convenience function to open a terminal window. | |
3699 | * | |
3700 | * @param x column relative to parent | |
3701 | * @param y row relative to parent | |
3702 | * @param flags mask of CENTERED, MODAL, or RESIZABLE | |
3703 | * @return the terminal new window | |
3704 | */ | |
3705 | public final TTerminalWindow openTerminal(final int x, final int y, | |
3706 | final int flags) { | |
3707 | ||
3708 | return new TTerminalWindow(this, x, y, flags); | |
3709 | } | |
3710 | ||
a69ed767 KL |
3711 | /** |
3712 | * Convenience function to open a terminal window. | |
3713 | * | |
3714 | * @param x column relative to parent | |
3715 | * @param y row relative to parent | |
3716 | * @param flags mask of CENTERED, MODAL, or RESIZABLE | |
3717 | * @param closeOnExit if true, close the window when the command exits | |
3718 | * @return the terminal new window | |
3719 | */ | |
3720 | public final TTerminalWindow openTerminal(final int x, final int y, | |
3721 | final int flags, final boolean closeOnExit) { | |
3722 | ||
3723 | return new TTerminalWindow(this, x, y, flags, closeOnExit); | |
3724 | } | |
3725 | ||
6f8ff91a KL |
3726 | /** |
3727 | * Convenience function to open a terminal window and execute a custom | |
3728 | * command line inside it. | |
3729 | * | |
3730 | * @param x column relative to parent | |
3731 | * @param y row relative to parent | |
3732 | * @param commandLine the command line to execute | |
3733 | * @return the terminal new window | |
3734 | */ | |
3735 | public final TTerminalWindow openTerminal(final int x, final int y, | |
3736 | final String commandLine) { | |
3737 | ||
3738 | return openTerminal(x, y, TWindow.RESIZABLE, commandLine); | |
3739 | } | |
3740 | ||
a69ed767 KL |
3741 | /** |
3742 | * Convenience function to open a terminal window and execute a custom | |
3743 | * command line inside it. | |
3744 | * | |
3745 | * @param x column relative to parent | |
3746 | * @param y row relative to parent | |
3747 | * @param commandLine the command line to execute | |
3748 | * @param closeOnExit if true, close the window when the command exits | |
3749 | * @return the terminal new window | |
3750 | */ | |
3751 | public final TTerminalWindow openTerminal(final int x, final int y, | |
3752 | final String commandLine, final boolean closeOnExit) { | |
3753 | ||
3754 | return openTerminal(x, y, TWindow.RESIZABLE, commandLine, closeOnExit); | |
3755 | } | |
3756 | ||
a0d734e6 KL |
3757 | /** |
3758 | * Convenience function to open a terminal window and execute a custom | |
3759 | * command line inside it. | |
3760 | * | |
3761 | * @param x column relative to parent | |
3762 | * @param y row relative to parent | |
3763 | * @param flags mask of CENTERED, MODAL, or RESIZABLE | |
3764 | * @param command the command line to execute | |
3765 | * @return the terminal new window | |
3766 | */ | |
3767 | public final TTerminalWindow openTerminal(final int x, final int y, | |
3768 | final int flags, final String [] command) { | |
3769 | ||
3770 | return new TTerminalWindow(this, x, y, flags, command); | |
3771 | } | |
3772 | ||
a69ed767 KL |
3773 | /** |
3774 | * Convenience function to open a terminal window and execute a custom | |
3775 | * command line inside it. | |
3776 | * | |
3777 | * @param x column relative to parent | |
3778 | * @param y row relative to parent | |
3779 | * @param flags mask of CENTERED, MODAL, or RESIZABLE | |
3780 | * @param command the command line to execute | |
3781 | * @param closeOnExit if true, close the window when the command exits | |
3782 | * @return the terminal new window | |
3783 | */ | |
3784 | public final TTerminalWindow openTerminal(final int x, final int y, | |
3785 | final int flags, final String [] command, final boolean closeOnExit) { | |
3786 | ||
3787 | return new TTerminalWindow(this, x, y, flags, command, closeOnExit); | |
3788 | } | |
3789 | ||
6f8ff91a KL |
3790 | /** |
3791 | * Convenience function to open a terminal window and execute a custom | |
3792 | * command line inside it. | |
3793 | * | |
3794 | * @param x column relative to parent | |
3795 | * @param y row relative to parent | |
3796 | * @param flags mask of CENTERED, MODAL, or RESIZABLE | |
3797 | * @param commandLine the command line to execute | |
3798 | * @return the terminal new window | |
3799 | */ | |
3800 | public final TTerminalWindow openTerminal(final int x, final int y, | |
3801 | final int flags, final String commandLine) { | |
3802 | ||
00691e80 | 3803 | return new TTerminalWindow(this, x, y, flags, commandLine.split("\\s+")); |
6f8ff91a KL |
3804 | } |
3805 | ||
a69ed767 KL |
3806 | /** |
3807 | * Convenience function to open a terminal window and execute a custom | |
3808 | * command line inside it. | |
3809 | * | |
3810 | * @param x column relative to parent | |
3811 | * @param y row relative to parent | |
3812 | * @param flags mask of CENTERED, MODAL, or RESIZABLE | |
3813 | * @param commandLine the command line to execute | |
3814 | * @param closeOnExit if true, close the window when the command exits | |
3815 | * @return the terminal new window | |
3816 | */ | |
3817 | public final TTerminalWindow openTerminal(final int x, final int y, | |
3818 | final int flags, final String commandLine, final boolean closeOnExit) { | |
3819 | ||
00691e80 | 3820 | return new TTerminalWindow(this, x, y, flags, commandLine.split("\\s+"), |
a69ed767 KL |
3821 | closeOnExit); |
3822 | } | |
3823 | ||
0d47c546 KL |
3824 | /** |
3825 | * Convenience function to spawn an file open box. | |
3826 | * | |
3827 | * @param path path of selected file | |
3828 | * @return the result of the new file open box | |
329fd62e | 3829 | * @throws IOException if java.io operation throws |
0d47c546 KL |
3830 | */ |
3831 | public final String fileOpenBox(final String path) throws IOException { | |
3832 | ||
3833 | TFileOpenBox box = new TFileOpenBox(this, path, TFileOpenBox.Type.OPEN); | |
3834 | return box.getFilename(); | |
3835 | } | |
3836 | ||
3837 | /** | |
3838 | * Convenience function to spawn an file open box. | |
3839 | * | |
3840 | * @param path path of selected file | |
3841 | * @param type one of the Type constants | |
3842 | * @return the result of the new file open box | |
329fd62e | 3843 | * @throws IOException if java.io operation throws |
0d47c546 KL |
3844 | */ |
3845 | public final String fileOpenBox(final String path, | |
3846 | final TFileOpenBox.Type type) throws IOException { | |
3847 | ||
3848 | TFileOpenBox box = new TFileOpenBox(this, path, type); | |
3849 | return box.getFilename(); | |
3850 | } | |
3851 | ||
a69ed767 KL |
3852 | /** |
3853 | * Convenience function to spawn a file open box. | |
3854 | * | |
3855 | * @param path path of selected file | |
3856 | * @param type one of the Type constants | |
3857 | * @param filter a string that files must match to be displayed | |
3858 | * @return the result of the new file open box | |
3859 | * @throws IOException of a java.io operation throws | |
3860 | */ | |
3861 | public final String fileOpenBox(final String path, | |
3862 | final TFileOpenBox.Type type, final String filter) throws IOException { | |
3863 | ||
3864 | ArrayList<String> filters = new ArrayList<String>(); | |
3865 | filters.add(filter); | |
3866 | ||
3867 | TFileOpenBox box = new TFileOpenBox(this, path, type, filters); | |
3868 | return box.getFilename(); | |
3869 | } | |
3870 | ||
3871 | /** | |
3872 | * Convenience function to spawn a file open box. | |
3873 | * | |
3874 | * @param path path of selected file | |
3875 | * @param type one of the Type constants | |
3876 | * @param filters a list of strings that files must match to be displayed | |
3877 | * @return the result of the new file open box | |
3878 | * @throws IOException of a java.io operation throws | |
3879 | */ | |
3880 | public final String fileOpenBox(final String path, | |
3881 | final TFileOpenBox.Type type, | |
3882 | final List<String> filters) throws IOException { | |
3883 | ||
3884 | TFileOpenBox box = new TFileOpenBox(this, path, type, filters); | |
3885 | return box.getFilename(); | |
3886 | } | |
3887 | ||
92453213 KL |
3888 | /** |
3889 | * Convenience function to create a new window and make it active. | |
3890 | * Window will be located at (0, 0). | |
3891 | * | |
3892 | * @param title window title, will be centered along the top border | |
3893 | * @param width width of window | |
3894 | * @param height height of window | |
43ad7b6c | 3895 | * @return the new window |
92453213 KL |
3896 | */ |
3897 | public final TWindow addWindow(final String title, final int width, | |
3898 | final int height) { | |
3899 | ||
3900 | TWindow window = new TWindow(this, title, 0, 0, width, height); | |
3901 | return window; | |
3902 | } | |
1978ad50 | 3903 | |
92453213 KL |
3904 | /** |
3905 | * Convenience function to create a new window and make it active. | |
3906 | * Window will be located at (0, 0). | |
3907 | * | |
3908 | * @param title window title, will be centered along the top border | |
3909 | * @param width width of window | |
3910 | * @param height height of window | |
3911 | * @param flags bitmask of RESIZABLE, CENTERED, or MODAL | |
43ad7b6c | 3912 | * @return the new window |
92453213 KL |
3913 | */ |
3914 | public final TWindow addWindow(final String title, | |
3915 | final int width, final int height, final int flags) { | |
3916 | ||
3917 | TWindow window = new TWindow(this, title, 0, 0, width, height, flags); | |
3918 | return window; | |
3919 | } | |
3920 | ||
3921 | /** | |
3922 | * Convenience function to create a new window and make it active. | |
3923 | * | |
3924 | * @param title window title, will be centered along the top border | |
3925 | * @param x column relative to parent | |
3926 | * @param y row relative to parent | |
3927 | * @param width width of window | |
3928 | * @param height height of window | |
43ad7b6c | 3929 | * @return the new window |
92453213 KL |
3930 | */ |
3931 | public final TWindow addWindow(final String title, | |
3932 | final int x, final int y, final int width, final int height) { | |
3933 | ||
3934 | TWindow window = new TWindow(this, title, x, y, width, height); | |
3935 | return window; | |
3936 | } | |
3937 | ||
3938 | /** | |
3939 | * Convenience function to create a new window and make it active. | |
3940 | * | |
92453213 KL |
3941 | * @param title window title, will be centered along the top border |
3942 | * @param x column relative to parent | |
3943 | * @param y row relative to parent | |
3944 | * @param width width of window | |
3945 | * @param height height of window | |
3946 | * @param flags mask of RESIZABLE, CENTERED, or MODAL | |
43ad7b6c | 3947 | * @return the new window |
92453213 KL |
3948 | */ |
3949 | public final TWindow addWindow(final String title, | |
3950 | final int x, final int y, final int width, final int height, | |
3951 | final int flags) { | |
3952 | ||
3953 | TWindow window = new TWindow(this, title, x, y, width, height, flags); | |
3954 | return window; | |
3955 | } | |
3956 | ||
7d4115a5 | 3957 | } |