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