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