Commit | Line | Data |
---|---|---|
daa4106c | 1 | /* |
7b5261bc | 2 | * Jexer - Java Text User Interface |
7d4115a5 KL |
3 | * |
4 | * License: LGPLv3 or later | |
5 | * | |
7b5261bc KL |
6 | * This module is licensed under the GNU Lesser General Public License |
7 | * Version 3. Please see the file "COPYING" in this directory for more | |
8 | * information about the GNU Lesser General Public License Version 3. | |
7d4115a5 KL |
9 | * |
10 | * Copyright (C) 2015 Kevin Lamonte | |
11 | * | |
12 | * This program is free software; you can redistribute it and/or | |
13 | * modify it under the terms of the GNU Lesser General Public License | |
14 | * as published by the Free Software Foundation; either version 3 of | |
15 | * the License, or (at your option) any later version. | |
16 | * | |
17 | * This program is distributed in the hope that it will be useful, but | |
18 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
20 | * General Public License for more details. | |
21 | * | |
22 | * You should have received a copy of the GNU Lesser General Public | |
23 | * License along with this program; if not, see | |
24 | * http://www.gnu.org/licenses/, or write to the Free Software | |
25 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA | |
26 | * 02110-1301 USA | |
7b5261bc KL |
27 | * |
28 | * @author Kevin Lamonte [kevin.lamonte@gmail.com] | |
29 | * @version 1 | |
7d4115a5 KL |
30 | */ |
31 | package jexer; | |
32 | ||
4328bb42 | 33 | import java.io.InputStream; |
0d47c546 | 34 | import java.io.IOException; |
4328bb42 KL |
35 | import java.io.OutputStream; |
36 | import java.io.UnsupportedEncodingException; | |
a06459bd | 37 | import java.util.Collections; |
d502a0e9 | 38 | import java.util.Date; |
e826b451 | 39 | import java.util.HashMap; |
c6940ed9 | 40 | import java.util.ArrayList; |
4328bb42 KL |
41 | import java.util.LinkedList; |
42 | import java.util.List; | |
e826b451 | 43 | import java.util.Map; |
4328bb42 KL |
44 | |
45 | import jexer.bits.CellAttributes; | |
46 | import jexer.bits.ColorTheme; | |
47 | import jexer.bits.GraphicsChars; | |
48 | import jexer.event.TCommandEvent; | |
49 | import jexer.event.TInputEvent; | |
50 | import jexer.event.TKeypressEvent; | |
fca67db0 | 51 | import jexer.event.TMenuEvent; |
4328bb42 KL |
52 | import jexer.event.TMouseEvent; |
53 | import jexer.event.TResizeEvent; | |
54 | import jexer.backend.Backend; | |
a4406f4e | 55 | import jexer.backend.SwingBackend; |
4328bb42 | 56 | import jexer.backend.ECMA48Backend; |
48e27807 | 57 | import jexer.io.Screen; |
928811d8 KL |
58 | import jexer.menu.TMenu; |
59 | import jexer.menu.TMenuItem; | |
4328bb42 | 60 | import static jexer.TCommand.*; |
4328bb42 | 61 | |
7d4115a5 KL |
62 | /** |
63 | * TApplication sets up a full Text User Interface application. | |
64 | */ | |
a4406f4e | 65 | public class TApplication implements Runnable { |
7d4115a5 | 66 | |
99144c71 KL |
67 | /** |
68 | * If true, emit thread stuff to System.err. | |
69 | */ | |
70 | private static final boolean debugThreads = false; | |
71 | ||
a83fea2b KL |
72 | /** |
73 | * If true, emit events being processed to System.err. | |
74 | */ | |
75 | private static final boolean debugEvents = false; | |
76 | ||
a4406f4e KL |
77 | /** |
78 | * Two backend types are available. | |
79 | */ | |
80 | public static enum BackendType { | |
81 | /** | |
82 | * A Swing JFrame. | |
83 | */ | |
84 | SWING, | |
85 | ||
86 | /** | |
87 | * An ECMA48 / ANSI X3.64 / XTERM style terminal. | |
88 | */ | |
89 | ECMA48, | |
90 | ||
91 | /** | |
92 | * Synonym for ECMA48 | |
93 | */ | |
94 | XTERM | |
95 | } | |
96 | ||
c6940ed9 KL |
97 | /** |
98 | * WidgetEventHandler is the main event consumer loop. There are at most | |
99 | * two such threads in existence: the primary for normal case and a | |
100 | * secondary that is used for TMessageBox, TInputBox, and similar. | |
101 | */ | |
102 | private class WidgetEventHandler implements Runnable { | |
103 | /** | |
104 | * The main application. | |
105 | */ | |
106 | private TApplication application; | |
107 | ||
108 | /** | |
109 | * Whether or not this WidgetEventHandler is the primary or secondary | |
110 | * thread. | |
111 | */ | |
112 | private boolean primary = true; | |
113 | ||
114 | /** | |
115 | * Public constructor. | |
116 | * | |
117 | * @param application the main application | |
118 | * @param primary if true, this is the primary event handler thread | |
119 | */ | |
120 | public WidgetEventHandler(final TApplication application, | |
121 | final boolean primary) { | |
122 | ||
123 | this.application = application; | |
124 | this.primary = primary; | |
125 | } | |
126 | ||
127 | /** | |
128 | * The consumer loop. | |
129 | */ | |
130 | public void run() { | |
131 | ||
132 | // Loop forever | |
133 | while (!application.quit) { | |
134 | ||
135 | // Wait until application notifies me | |
136 | while (!application.quit) { | |
137 | try { | |
138 | synchronized (application.drainEventQueue) { | |
139 | if (application.drainEventQueue.size() > 0) { | |
140 | break; | |
141 | } | |
142 | } | |
92554d64 KL |
143 | |
144 | synchronized (this) { | |
bd8d51fa KL |
145 | if (debugThreads) { |
146 | System.err.printf("%s %s sleep\n", this, | |
147 | primary ? "primary" : "secondary"); | |
148 | } | |
92554d64 KL |
149 | |
150 | this.wait(); | |
151 | ||
bd8d51fa KL |
152 | if (debugThreads) { |
153 | System.err.printf("%s %s AWAKE\n", this, | |
154 | primary ? "primary" : "secondary"); | |
155 | } | |
92554d64 | 156 | |
c6940ed9 KL |
157 | if ((!primary) |
158 | && (application.secondaryEventReceiver == null) | |
159 | ) { | |
92554d64 KL |
160 | // Secondary thread, emergency exit. If we |
161 | // got here then something went wrong with | |
162 | // the handoff between yield() and | |
163 | // closeWindow(). | |
92554d64 KL |
164 | synchronized (application.primaryEventHandler) { |
165 | application.primaryEventHandler.notify(); | |
166 | } | |
167 | application.secondaryEventHandler = null; | |
bd8d51fa KL |
168 | throw new RuntimeException( |
169 | "secondary exited at wrong time"); | |
c6940ed9 KL |
170 | } |
171 | break; | |
172 | } | |
173 | } catch (InterruptedException e) { | |
174 | // SQUASH | |
175 | } | |
176 | } | |
177 | ||
ef368bd0 KL |
178 | // Wait for drawAll() or doIdle() to be done, then handle the |
179 | // events. | |
180 | boolean oldLock = lockHandleEvent(); | |
181 | assert (oldLock == false); | |
182 | ||
c6940ed9 KL |
183 | // Pull all events off the queue |
184 | for (;;) { | |
185 | TInputEvent event = null; | |
186 | synchronized (application.drainEventQueue) { | |
187 | if (application.drainEventQueue.size() == 0) { | |
188 | break; | |
189 | } | |
190 | event = application.drainEventQueue.remove(0); | |
191 | } | |
bd8d51fa | 192 | application.repaint = true; |
c6940ed9 KL |
193 | if (primary) { |
194 | primaryHandleEvent(event); | |
195 | } else { | |
196 | secondaryHandleEvent(event); | |
197 | } | |
198 | if ((!primary) | |
199 | && (application.secondaryEventReceiver == null) | |
200 | ) { | |
99144c71 KL |
201 | // Secondary thread, time to exit. |
202 | ||
203 | // DO NOT UNLOCK. Primary thread just came back from | |
204 | // primaryHandleEvent() and will unlock in the else | |
92554d64 KL |
205 | // block below. Just wake it up. |
206 | synchronized (application.primaryEventHandler) { | |
207 | application.primaryEventHandler.notify(); | |
208 | } | |
209 | // Now eliminate my reference so that | |
210 | // wakeEventHandler() resumes working on the primary. | |
211 | application.secondaryEventHandler = null; | |
212 | ||
213 | // All done! | |
c6940ed9 KL |
214 | return; |
215 | } | |
92554d64 KL |
216 | } // for (;;) |
217 | ||
ef368bd0 KL |
218 | // Unlock. Either I am primary thread, or I am secondary |
219 | // thread and still running. | |
220 | oldLock = unlockHandleEvent(); | |
221 | assert (oldLock == true); | |
222 | ||
92554d64 KL |
223 | // I have done some work of some kind. Tell the main run() |
224 | // loop to wake up now. | |
225 | synchronized (application) { | |
226 | application.notify(); | |
c6940ed9 | 227 | } |
92554d64 | 228 | |
c6940ed9 KL |
229 | } // while (true) (main runnable loop) |
230 | } | |
231 | } | |
232 | ||
233 | /** | |
234 | * The primary event handler thread. | |
235 | */ | |
92554d64 | 236 | private volatile WidgetEventHandler primaryEventHandler; |
c6940ed9 KL |
237 | |
238 | /** | |
239 | * The secondary event handler thread. | |
240 | */ | |
92554d64 | 241 | private volatile WidgetEventHandler secondaryEventHandler; |
c6940ed9 KL |
242 | |
243 | /** | |
244 | * The widget receiving events from the secondary event handler thread. | |
245 | */ | |
92554d64 | 246 | private volatile TWidget secondaryEventReceiver; |
c6940ed9 | 247 | |
99144c71 KL |
248 | /** |
249 | * Spinlock for the primary and secondary event handlers. | |
250 | * WidgetEventHandler.run() is responsible for setting this value. | |
251 | */ | |
252 | private volatile boolean insideHandleEvent = false; | |
253 | ||
92554d64 KL |
254 | /** |
255 | * Wake the sleeping active event handler. | |
256 | */ | |
257 | private void wakeEventHandler() { | |
258 | if (secondaryEventHandler != null) { | |
259 | synchronized (secondaryEventHandler) { | |
260 | secondaryEventHandler.notify(); | |
261 | } | |
262 | } else { | |
263 | assert (primaryEventHandler != null); | |
264 | synchronized (primaryEventHandler) { | |
265 | primaryEventHandler.notify(); | |
266 | } | |
267 | } | |
268 | } | |
269 | ||
99144c71 KL |
270 | /** |
271 | * Set the insideHandleEvent flag to true. lockoutEventHandlers() will | |
272 | * spin indefinitely until unlockHandleEvent() is called. | |
273 | * | |
274 | * @return the old value of insideHandleEvent | |
275 | */ | |
276 | private boolean lockHandleEvent() { | |
277 | if (debugThreads) { | |
278 | System.err.printf(" >> lockHandleEvent(): oldValue %s", | |
279 | insideHandleEvent); | |
280 | } | |
281 | boolean oldValue = true; | |
282 | ||
283 | synchronized (this) { | |
284 | // Wait for TApplication.run() to finish using the global state | |
285 | // before allowing further event processing. | |
ef368bd0 KL |
286 | while (lockoutHandleEvent == true) { |
287 | try { | |
288 | // Backoff so that the backend can finish its work. | |
289 | Thread.sleep(5); | |
290 | } catch (InterruptedException e) { | |
291 | // SQUASH | |
292 | } | |
293 | } | |
99144c71 KL |
294 | |
295 | oldValue = insideHandleEvent; | |
296 | insideHandleEvent = true; | |
297 | } | |
298 | ||
299 | if (debugThreads) { | |
300 | System.err.printf(" ***\n"); | |
301 | } | |
302 | return oldValue; | |
303 | } | |
304 | ||
305 | /** | |
306 | * Set the insideHandleEvent flag to false. lockoutEventHandlers() will | |
307 | * spin indefinitely until unlockHandleEvent() is called. | |
308 | * | |
309 | * @return the old value of insideHandleEvent | |
310 | */ | |
311 | private boolean unlockHandleEvent() { | |
312 | if (debugThreads) { | |
313 | System.err.printf(" << unlockHandleEvent(): oldValue %s\n", | |
314 | insideHandleEvent); | |
315 | } | |
316 | synchronized (this) { | |
317 | boolean oldValue = insideHandleEvent; | |
318 | insideHandleEvent = false; | |
319 | return oldValue; | |
320 | } | |
321 | } | |
322 | ||
323 | /** | |
324 | * Spinlock for the primary and secondary event handlers. When true, the | |
325 | * event handlers will spinlock wait before calling handleEvent(). | |
326 | */ | |
327 | private volatile boolean lockoutHandleEvent = false; | |
328 | ||
329 | /** | |
330 | * TApplication.run() needs to be able rely on the global data structures | |
331 | * being intact when calling doIdle() and drawAll(). Tell the event | |
332 | * handlers to wait for an unlock before handling their events. | |
333 | */ | |
334 | private void stopEventHandlers() { | |
335 | if (debugThreads) { | |
336 | System.err.printf(">> stopEventHandlers()"); | |
337 | } | |
338 | ||
339 | lockoutHandleEvent = true; | |
340 | // Wait for the last event to finish processing before returning | |
341 | // control to TApplication.run(). | |
ef368bd0 KL |
342 | while (insideHandleEvent == true) { |
343 | try { | |
344 | // Backoff so that the event handler can finish its work. | |
345 | Thread.sleep(1); | |
346 | } catch (InterruptedException e) { | |
347 | // SQUASH | |
348 | } | |
349 | } | |
99144c71 KL |
350 | |
351 | if (debugThreads) { | |
352 | System.err.printf(" XXX\n"); | |
353 | } | |
354 | } | |
355 | ||
356 | /** | |
357 | * TApplication.run() needs to be able rely on the global data structures | |
358 | * being intact when calling doIdle() and drawAll(). Tell the event | |
359 | * handlers that it is now OK to handle their events. | |
360 | */ | |
361 | private void startEventHandlers() { | |
362 | if (debugThreads) { | |
363 | System.err.printf("<< startEventHandlers()\n"); | |
364 | } | |
365 | lockoutHandleEvent = false; | |
366 | } | |
367 | ||
7d4115a5 | 368 | /** |
4328bb42 KL |
369 | * Access to the physical screen, keyboard, and mouse. |
370 | */ | |
7b5261bc | 371 | private Backend backend; |
4328bb42 | 372 | |
48e27807 KL |
373 | /** |
374 | * Get the Screen. | |
375 | * | |
376 | * @return the Screen | |
377 | */ | |
378 | public final Screen getScreen() { | |
379 | return backend.getScreen(); | |
380 | } | |
381 | ||
4328bb42 | 382 | /** |
7b5261bc | 383 | * Actual mouse coordinate X. |
4328bb42 KL |
384 | */ |
385 | private int mouseX; | |
386 | ||
387 | /** | |
7b5261bc | 388 | * Actual mouse coordinate Y. |
4328bb42 KL |
389 | */ |
390 | private int mouseY; | |
391 | ||
bd8d51fa KL |
392 | /** |
393 | * Old version of mouse coordinate X. | |
394 | */ | |
395 | private int oldMouseX; | |
396 | ||
397 | /** | |
398 | * Old version mouse coordinate Y. | |
399 | */ | |
400 | private int oldMouseY; | |
401 | ||
4328bb42 | 402 | /** |
8e688b92 | 403 | * Event queue that is filled by run(). |
4328bb42 | 404 | */ |
8e688b92 KL |
405 | private List<TInputEvent> fillEventQueue; |
406 | ||
407 | /** | |
408 | * Event queue that will be drained by either primary or secondary | |
409 | * Thread. | |
410 | */ | |
411 | private List<TInputEvent> drainEventQueue; | |
4328bb42 | 412 | |
fca67db0 KL |
413 | /** |
414 | * Top-level menus in this application. | |
415 | */ | |
416 | private List<TMenu> menus; | |
417 | ||
418 | /** | |
419 | * Stack of activated sub-menus in this application. | |
420 | */ | |
421 | private List<TMenu> subMenus; | |
422 | ||
423 | /** | |
424 | * The currently acive menu. | |
425 | */ | |
426 | private TMenu activeMenu = null; | |
427 | ||
e826b451 KL |
428 | /** |
429 | * Active keyboard accelerators. | |
430 | */ | |
431 | private Map<TKeypress, TMenuItem> accelerators; | |
432 | ||
4328bb42 KL |
433 | /** |
434 | * Windows and widgets pull colors from this ColorTheme. | |
435 | */ | |
7b5261bc KL |
436 | private ColorTheme theme; |
437 | ||
438 | /** | |
439 | * Get the color theme. | |
440 | * | |
441 | * @return the theme | |
442 | */ | |
443 | public final ColorTheme getTheme() { | |
444 | return theme; | |
445 | } | |
4328bb42 | 446 | |
a06459bd KL |
447 | /** |
448 | * The top-level windows (but not menus). | |
449 | */ | |
fca67db0 | 450 | private List<TWindow> windows; |
a06459bd | 451 | |
d502a0e9 KL |
452 | /** |
453 | * Timers that are being ticked. | |
454 | */ | |
455 | private List<TTimer> timers; | |
456 | ||
4328bb42 KL |
457 | /** |
458 | * When true, exit the application. | |
459 | */ | |
92554d64 | 460 | private volatile boolean quit = false; |
4328bb42 KL |
461 | |
462 | /** | |
463 | * When true, repaint the entire screen. | |
464 | */ | |
92554d64 | 465 | private volatile boolean repaint = true; |
4328bb42 | 466 | |
4328bb42 | 467 | /** |
7b5261bc KL |
468 | * Y coordinate of the top edge of the desktop. For now this is a |
469 | * constant. Someday it would be nice to have a multi-line menu or | |
470 | * toolbars. | |
4328bb42 | 471 | */ |
48e27807 KL |
472 | private static final int desktopTop = 1; |
473 | ||
474 | /** | |
475 | * Get Y coordinate of the top edge of the desktop. | |
476 | * | |
477 | * @return Y coordinate of the top edge of the desktop | |
478 | */ | |
479 | public final int getDesktopTop() { | |
480 | return desktopTop; | |
481 | } | |
4328bb42 KL |
482 | |
483 | /** | |
484 | * Y coordinate of the bottom edge of the desktop. | |
485 | */ | |
48e27807 KL |
486 | private int desktopBottom; |
487 | ||
488 | /** | |
489 | * Get Y coordinate of the bottom edge of the desktop. | |
490 | * | |
491 | * @return Y coordinate of the bottom edge of the desktop | |
492 | */ | |
493 | public final int getDesktopBottom() { | |
494 | return desktopBottom; | |
495 | } | |
4328bb42 KL |
496 | |
497 | /** | |
498 | * Public constructor. | |
499 | * | |
a4406f4e KL |
500 | * @param backendType BackendType.XTERM, BackendType.ECMA48 or |
501 | * BackendType.SWING | |
502 | * @throws UnsupportedEncodingException if an exception is thrown when | |
503 | * creating the InputStreamReader | |
504 | */ | |
505 | public TApplication(final BackendType backendType) | |
506 | throws UnsupportedEncodingException { | |
507 | ||
508 | switch (backendType) { | |
509 | case SWING: | |
510 | backend = new SwingBackend(this); | |
511 | break; | |
512 | case XTERM: | |
513 | // Fall through... | |
514 | case ECMA48: | |
515 | backend = new ECMA48Backend(this, null, null); | |
516 | } | |
517 | TApplicationImpl(); | |
518 | } | |
519 | ||
520 | /** | |
521 | * Public constructor. The backend type will be BackendType.ECMA48. | |
522 | * | |
4328bb42 KL |
523 | * @param input an InputStream connected to the remote user, or null for |
524 | * System.in. If System.in is used, then on non-Windows systems it will | |
525 | * be put in raw mode; shutdown() will (blindly!) put System.in in cooked | |
526 | * mode. input is always converted to a Reader with UTF-8 encoding. | |
527 | * @param output an OutputStream connected to the remote user, or null | |
528 | * for System.out. output is always converted to a Writer with UTF-8 | |
529 | * encoding. | |
7b5261bc KL |
530 | * @throws UnsupportedEncodingException if an exception is thrown when |
531 | * creating the InputStreamReader | |
4328bb42 | 532 | */ |
7b5261bc KL |
533 | public TApplication(final InputStream input, |
534 | final OutputStream output) throws UnsupportedEncodingException { | |
4328bb42 | 535 | |
a4406f4e KL |
536 | backend = new ECMA48Backend(this, input, output); |
537 | TApplicationImpl(); | |
538 | } | |
30bd4abd | 539 | |
a4406f4e KL |
540 | /** |
541 | * Public constructor. This hook enables use with new non-Jexer | |
542 | * backends. | |
543 | * | |
544 | * @param backend a Backend that is already ready to go. | |
545 | */ | |
546 | public TApplication(final Backend backend) { | |
547 | this.backend = backend; | |
548 | TApplicationImpl(); | |
549 | } | |
30bd4abd | 550 | |
a4406f4e KL |
551 | /** |
552 | * Finish construction once the backend is set. | |
553 | */ | |
554 | private void TApplicationImpl() { | |
8e688b92 KL |
555 | theme = new ColorTheme(); |
556 | desktopBottom = getScreen().getHeight() - 1; | |
c6940ed9 KL |
557 | fillEventQueue = new ArrayList<TInputEvent>(); |
558 | drainEventQueue = new ArrayList<TInputEvent>(); | |
8e688b92 KL |
559 | windows = new LinkedList<TWindow>(); |
560 | menus = new LinkedList<TMenu>(); | |
561 | subMenus = new LinkedList<TMenu>(); | |
d502a0e9 | 562 | timers = new LinkedList<TTimer>(); |
e826b451 | 563 | accelerators = new HashMap<TKeypress, TMenuItem>(); |
c6940ed9 KL |
564 | |
565 | // Setup the main consumer thread | |
566 | primaryEventHandler = new WidgetEventHandler(this, true); | |
567 | (new Thread(primaryEventHandler)).start(); | |
4328bb42 KL |
568 | } |
569 | ||
570 | /** | |
bd8d51fa KL |
571 | * Invert the cell color at a position. This is used to track the mouse. |
572 | * | |
573 | * @param x column position | |
574 | * @param y row position | |
4328bb42 | 575 | */ |
bd8d51fa KL |
576 | private void invertCell(final int x, final int y) { |
577 | synchronized (getScreen()) { | |
578 | CellAttributes attr = getScreen().getAttrXY(x, y); | |
579 | attr.setForeColor(attr.getForeColor().invert()); | |
580 | attr.setBackColor(attr.getBackColor().invert()); | |
581 | getScreen().putAttrXY(x, y, attr, false); | |
7b5261bc | 582 | } |
4328bb42 KL |
583 | } |
584 | ||
585 | /** | |
586 | * Draw everything. | |
587 | */ | |
7c870d89 | 588 | private void drawAll() { |
99144c71 KL |
589 | if (debugThreads) { |
590 | System.err.printf("drawAll() enter\n"); | |
591 | } | |
592 | ||
bd8d51fa | 593 | if (!repaint) { |
ef368bd0 KL |
594 | if ((oldMouseX != mouseX) || (oldMouseY != mouseY)) { |
595 | // The only thing that has happened is the mouse moved. | |
596 | // Clear the old position and draw the new position. | |
597 | invertCell(oldMouseX, oldMouseY); | |
598 | invertCell(mouseX, mouseY); | |
599 | oldMouseX = mouseX; | |
600 | oldMouseY = mouseY; | |
bd8d51fa KL |
601 | } |
602 | if (getScreen().isDirty()) { | |
603 | backend.flushScreen(); | |
604 | } | |
7b5261bc KL |
605 | return; |
606 | } | |
607 | ||
99144c71 KL |
608 | if (debugThreads) { |
609 | System.err.printf("drawAll() REDRAW\n"); | |
610 | } | |
611 | ||
7b5261bc KL |
612 | // If true, the cursor is not visible |
613 | boolean cursor = false; | |
614 | ||
615 | // Start with a clean screen | |
a06459bd | 616 | getScreen().clear(); |
7b5261bc KL |
617 | |
618 | // Draw the background | |
619 | CellAttributes background = theme.getColor("tapplication.background"); | |
a06459bd | 620 | getScreen().putAll(GraphicsChars.HATCH, background); |
7b5261bc | 621 | |
7b5261bc | 622 | // Draw each window in reverse Z order |
a06459bd KL |
623 | List<TWindow> sorted = new LinkedList<TWindow>(windows); |
624 | Collections.sort(sorted); | |
625 | Collections.reverse(sorted); | |
626 | for (TWindow window: sorted) { | |
627 | window.drawChildren(); | |
7b5261bc KL |
628 | } |
629 | ||
630 | // Draw the blank menubar line - reset the screen clipping first so | |
631 | // it won't trim it out. | |
a06459bd KL |
632 | getScreen().resetClipping(); |
633 | getScreen().hLineXY(0, 0, getScreen().getWidth(), ' ', | |
7b5261bc KL |
634 | theme.getColor("tmenu")); |
635 | // Now draw the menus. | |
636 | int x = 1; | |
fca67db0 | 637 | for (TMenu menu: menus) { |
7b5261bc KL |
638 | CellAttributes menuColor; |
639 | CellAttributes menuMnemonicColor; | |
7c870d89 | 640 | if (menu.isActive()) { |
7b5261bc KL |
641 | menuColor = theme.getColor("tmenu.highlighted"); |
642 | menuMnemonicColor = theme.getColor("tmenu.mnemonic.highlighted"); | |
643 | } else { | |
644 | menuColor = theme.getColor("tmenu"); | |
645 | menuMnemonicColor = theme.getColor("tmenu.mnemonic"); | |
646 | } | |
647 | // Draw the menu title | |
fca67db0 | 648 | getScreen().hLineXY(x, 0, menu.getTitle().length() + 2, ' ', |
7b5261bc | 649 | menuColor); |
0d47c546 | 650 | getScreen().putStringXY(x + 1, 0, menu.getTitle(), menuColor); |
7b5261bc | 651 | // Draw the highlight character |
fca67db0 KL |
652 | getScreen().putCharXY(x + 1 + menu.getMnemonic().getShortcutIdx(), |
653 | 0, menu.getMnemonic().getShortcut(), menuMnemonicColor); | |
7b5261bc | 654 | |
7c870d89 | 655 | if (menu.isActive()) { |
a06459bd | 656 | menu.drawChildren(); |
7b5261bc | 657 | // Reset the screen clipping so we can draw the next title. |
a06459bd | 658 | getScreen().resetClipping(); |
7b5261bc | 659 | } |
fca67db0 | 660 | x += menu.getTitle().length() + 2; |
7b5261bc KL |
661 | } |
662 | ||
a06459bd | 663 | for (TMenu menu: subMenus) { |
7b5261bc | 664 | // Reset the screen clipping so we can draw the next sub-menu. |
a06459bd KL |
665 | getScreen().resetClipping(); |
666 | menu.drawChildren(); | |
7b5261bc | 667 | } |
7b5261bc KL |
668 | |
669 | // Draw the mouse pointer | |
bd8d51fa | 670 | invertCell(mouseX, mouseY); |
7b5261bc | 671 | |
7b5261bc KL |
672 | // Place the cursor if it is visible |
673 | TWidget activeWidget = null; | |
a06459bd KL |
674 | if (sorted.size() > 0) { |
675 | activeWidget = sorted.get(sorted.size() - 1).getActiveChild(); | |
7c870d89 | 676 | if (activeWidget.isCursorVisible()) { |
a06459bd | 677 | getScreen().putCursor(true, activeWidget.getCursorAbsoluteX(), |
7b5261bc KL |
678 | activeWidget.getCursorAbsoluteY()); |
679 | cursor = true; | |
680 | } | |
681 | } | |
682 | ||
683 | // Kill the cursor | |
fca67db0 | 684 | if (!cursor) { |
a06459bd | 685 | getScreen().hideCursor(); |
7b5261bc | 686 | } |
7b5261bc KL |
687 | |
688 | // Flush the screen contents | |
689 | backend.flushScreen(); | |
690 | ||
691 | repaint = false; | |
4328bb42 KL |
692 | } |
693 | ||
694 | /** | |
7b5261bc | 695 | * Run this application until it exits. |
4328bb42 | 696 | */ |
a4406f4e | 697 | public void run() { |
7b5261bc KL |
698 | while (!quit) { |
699 | // Timeout is in milliseconds, so default timeout after 1 second | |
700 | // of inactivity. | |
92554d64 KL |
701 | long timeout = 0; |
702 | ||
703 | // If I've got no updates to render, wait for something from the | |
704 | // backend or a timer. | |
bd8d51fa KL |
705 | if (!repaint |
706 | && ((mouseX == oldMouseX) && (mouseY == oldMouseY)) | |
707 | ) { | |
e3dfbd23 KL |
708 | // Never sleep longer than 50 millis. We need time for |
709 | // windows with background tasks to update the display, and | |
710 | // still flip buffers reasonably quickly in | |
711 | // backend.flushPhysical(). | |
712 | timeout = getSleepTime(50); | |
8e688b92 | 713 | } |
92554d64 KL |
714 | |
715 | if (timeout > 0) { | |
716 | // As of now, I've got nothing to do: no I/O, nothing from | |
717 | // the consumer threads, no timers that need to run ASAP. So | |
718 | // wait until either the backend or the consumer threads have | |
719 | // something to do. | |
720 | try { | |
721 | synchronized (this) { | |
722 | this.wait(timeout); | |
723 | } | |
724 | } catch (InterruptedException e) { | |
725 | // I'm awake and don't care why, let's see what's going | |
726 | // on out there. | |
8e688b92 | 727 | } |
bd8d51fa | 728 | repaint = true; |
7b5261bc KL |
729 | } |
730 | ||
ef368bd0 KL |
731 | // Prevent stepping on the primary or secondary event handler. |
732 | stopEventHandlers(); | |
733 | ||
8e688b92 | 734 | // Pull any pending I/O events |
92554d64 | 735 | backend.getEvents(fillEventQueue); |
8e688b92 KL |
736 | |
737 | // Dispatch each event to the appropriate handler, one at a time. | |
738 | for (;;) { | |
739 | TInputEvent event = null; | |
ef368bd0 KL |
740 | if (fillEventQueue.size() == 0) { |
741 | break; | |
8e688b92 | 742 | } |
ef368bd0 | 743 | event = fillEventQueue.remove(0); |
8e688b92 KL |
744 | metaHandleEvent(event); |
745 | } | |
7b5261bc | 746 | |
92554d64 | 747 | // Wake a consumer thread if we have any pending events. |
ef368bd0 KL |
748 | if (drainEventQueue.size() > 0) { |
749 | wakeEventHandler(); | |
92554d64 KL |
750 | } |
751 | ||
7b5261bc KL |
752 | // Process timers and call doIdle()'s |
753 | doIdle(); | |
754 | ||
755 | // Update the screen | |
87a17f3c KL |
756 | synchronized (getScreen()) { |
757 | drawAll(); | |
758 | } | |
99144c71 KL |
759 | |
760 | // Let the event handlers run again. | |
761 | startEventHandlers(); | |
7b5261bc | 762 | |
92554d64 KL |
763 | } // while (!quit) |
764 | ||
765 | // Shutdown the event consumer threads | |
766 | if (secondaryEventHandler != null) { | |
767 | synchronized (secondaryEventHandler) { | |
768 | secondaryEventHandler.notify(); | |
769 | } | |
770 | } | |
771 | if (primaryEventHandler != null) { | |
772 | synchronized (primaryEventHandler) { | |
773 | primaryEventHandler.notify(); | |
774 | } | |
7b5261bc KL |
775 | } |
776 | ||
92554d64 | 777 | // Shutdown the user I/O thread(s) |
7b5261bc | 778 | backend.shutdown(); |
92554d64 KL |
779 | |
780 | // Close all the windows. This gives them an opportunity to release | |
781 | // resources. | |
782 | closeAllWindows(); | |
783 | ||
4328bb42 KL |
784 | } |
785 | ||
786 | /** | |
787 | * Peek at certain application-level events, add to eventQueue, and wake | |
8e688b92 | 788 | * up the consuming Thread. |
4328bb42 | 789 | * |
8e688b92 | 790 | * @param event the input event to consume |
4328bb42 | 791 | */ |
8e688b92 | 792 | private void metaHandleEvent(final TInputEvent event) { |
7b5261bc | 793 | |
a83fea2b KL |
794 | if (debugEvents) { |
795 | System.err.printf(String.format("metaHandleEvents event: %s\n", | |
796 | event)); System.err.flush(); | |
797 | } | |
7b5261bc | 798 | |
8e688b92 KL |
799 | if (quit) { |
800 | // Do no more processing if the application is already trying | |
801 | // to exit. | |
802 | return; | |
803 | } | |
7b5261bc | 804 | |
8e688b92 | 805 | // Special application-wide events ------------------------------- |
7b5261bc | 806 | |
8e688b92 KL |
807 | // Abort everything |
808 | if (event instanceof TCommandEvent) { | |
809 | TCommandEvent command = (TCommandEvent) event; | |
810 | if (command.getCmd().equals(cmAbort)) { | |
811 | quit = true; | |
812 | return; | |
7b5261bc | 813 | } |
8e688b92 | 814 | } |
7b5261bc | 815 | |
8e688b92 KL |
816 | // Screen resize |
817 | if (event instanceof TResizeEvent) { | |
818 | TResizeEvent resize = (TResizeEvent) event; | |
bd8d51fa KL |
819 | synchronized (getScreen()) { |
820 | getScreen().setDimensions(resize.getWidth(), | |
821 | resize.getHeight()); | |
822 | desktopBottom = getScreen().getHeight() - 1; | |
823 | mouseX = 0; | |
824 | mouseY = 0; | |
825 | oldMouseX = 0; | |
826 | oldMouseY = 0; | |
827 | } | |
8e688b92 KL |
828 | return; |
829 | } | |
7b5261bc | 830 | |
8e688b92 KL |
831 | // Peek at the mouse position |
832 | if (event instanceof TMouseEvent) { | |
833 | TMouseEvent mouse = (TMouseEvent) event; | |
bd8d51fa KL |
834 | synchronized (getScreen()) { |
835 | if ((mouseX != mouse.getX()) || (mouseY != mouse.getY())) { | |
836 | oldMouseX = mouseX; | |
837 | oldMouseY = mouseY; | |
838 | mouseX = mouse.getX(); | |
839 | mouseY = mouse.getY(); | |
840 | } | |
7b5261bc | 841 | } |
8e688b92 | 842 | } |
7b5261bc | 843 | |
bd8d51fa | 844 | // Put into the main queue |
ef368bd0 | 845 | drainEventQueue.add(event); |
4328bb42 KL |
846 | } |
847 | ||
a06459bd KL |
848 | /** |
849 | * Dispatch one event to the appropriate widget or application-level | |
fca67db0 KL |
850 | * event handler. This is the primary event handler, it has the normal |
851 | * application-wide event handling. | |
a06459bd KL |
852 | * |
853 | * @param event the input event to consume | |
fca67db0 | 854 | * @see #secondaryHandleEvent(TInputEvent event) |
a06459bd | 855 | */ |
fca67db0 KL |
856 | private void primaryHandleEvent(final TInputEvent event) { |
857 | ||
a83fea2b KL |
858 | if (debugEvents) { |
859 | System.err.printf("Handle event: %s\n", event); | |
860 | } | |
fca67db0 KL |
861 | |
862 | // Special application-wide events ----------------------------------- | |
863 | ||
864 | // Peek at the mouse position | |
865 | if (event instanceof TMouseEvent) { | |
866 | // See if we need to switch focus to another window or the menu | |
867 | checkSwitchFocus((TMouseEvent) event); | |
868 | } | |
869 | ||
870 | // Handle menu events | |
871 | if ((activeMenu != null) && !(event instanceof TCommandEvent)) { | |
872 | TMenu menu = activeMenu; | |
873 | ||
874 | if (event instanceof TMouseEvent) { | |
875 | TMouseEvent mouse = (TMouseEvent) event; | |
876 | ||
877 | while (subMenus.size() > 0) { | |
878 | TMenu subMenu = subMenus.get(subMenus.size() - 1); | |
879 | if (subMenu.mouseWouldHit(mouse)) { | |
880 | break; | |
881 | } | |
882 | if ((mouse.getType() == TMouseEvent.Type.MOUSE_MOTION) | |
7c870d89 KL |
883 | && (!mouse.isMouse1()) |
884 | && (!mouse.isMouse2()) | |
885 | && (!mouse.isMouse3()) | |
886 | && (!mouse.isMouseWheelUp()) | |
887 | && (!mouse.isMouseWheelDown()) | |
fca67db0 KL |
888 | ) { |
889 | break; | |
890 | } | |
891 | // We navigated away from a sub-menu, so close it | |
892 | closeSubMenu(); | |
893 | } | |
894 | ||
895 | // Convert the mouse relative x/y to menu coordinates | |
896 | assert (mouse.getX() == mouse.getAbsoluteX()); | |
897 | assert (mouse.getY() == mouse.getAbsoluteY()); | |
898 | if (subMenus.size() > 0) { | |
899 | menu = subMenus.get(subMenus.size() - 1); | |
900 | } | |
901 | mouse.setX(mouse.getX() - menu.getX()); | |
902 | mouse.setY(mouse.getY() - menu.getY()); | |
903 | } | |
904 | menu.handleEvent(event); | |
905 | return; | |
906 | } | |
a06459bd | 907 | |
fca67db0 KL |
908 | if (event instanceof TKeypressEvent) { |
909 | TKeypressEvent keypress = (TKeypressEvent) event; | |
e826b451 | 910 | |
fca67db0 KL |
911 | // See if this key matches an accelerator, and if so dispatch the |
912 | // menu event. | |
913 | TKeypress keypressLowercase = keypress.getKey().toLowerCase(); | |
e826b451 KL |
914 | TMenuItem item = null; |
915 | synchronized (accelerators) { | |
916 | item = accelerators.get(keypressLowercase); | |
917 | } | |
fca67db0 | 918 | if (item != null) { |
7c870d89 | 919 | if (item.isEnabled()) { |
bd8d51fa KL |
920 | // Let the menu item dispatch |
921 | item.dispatch(); | |
fca67db0 KL |
922 | return; |
923 | } | |
924 | } | |
bd8d51fa KL |
925 | // Handle the keypress |
926 | if (onKeypress(keypress)) { | |
927 | return; | |
928 | } | |
fca67db0 | 929 | } |
a06459bd | 930 | |
fca67db0 KL |
931 | if (event instanceof TCommandEvent) { |
932 | if (onCommand((TCommandEvent) event)) { | |
933 | return; | |
934 | } | |
935 | } | |
936 | ||
937 | if (event instanceof TMenuEvent) { | |
938 | if (onMenu((TMenuEvent) event)) { | |
939 | return; | |
940 | } | |
941 | } | |
942 | ||
943 | // Dispatch events to the active window ------------------------------- | |
944 | for (TWindow window: windows) { | |
7c870d89 | 945 | if (window.isActive()) { |
a06459bd KL |
946 | if (event instanceof TMouseEvent) { |
947 | TMouseEvent mouse = (TMouseEvent) event; | |
fca67db0 KL |
948 | // Convert the mouse relative x/y to window coordinates |
949 | assert (mouse.getX() == mouse.getAbsoluteX()); | |
950 | assert (mouse.getY() == mouse.getAbsoluteY()); | |
951 | mouse.setX(mouse.getX() - window.getX()); | |
952 | mouse.setY(mouse.getY() - window.getY()); | |
953 | } | |
a83fea2b KL |
954 | if (debugEvents) { |
955 | System.err.printf("TApplication dispatch event: %s\n", | |
956 | event); | |
957 | } | |
fca67db0 KL |
958 | window.handleEvent(event); |
959 | break; | |
960 | } | |
961 | } | |
962 | } | |
963 | /** | |
964 | * Dispatch one event to the appropriate widget or application-level | |
965 | * event handler. This is the secondary event handler used by certain | |
966 | * special dialogs (currently TMessageBox and TFileOpenBox). | |
967 | * | |
968 | * @param event the input event to consume | |
969 | * @see #primaryHandleEvent(TInputEvent event) | |
970 | */ | |
971 | private void secondaryHandleEvent(final TInputEvent event) { | |
c6940ed9 KL |
972 | secondaryEventReceiver.handleEvent(event); |
973 | } | |
974 | ||
975 | /** | |
976 | * Enable a widget to override the primary event thread. | |
977 | * | |
978 | * @param widget widget that will receive events | |
979 | */ | |
980 | public final void enableSecondaryEventReceiver(final TWidget widget) { | |
981 | assert (secondaryEventReceiver == null); | |
982 | assert (secondaryEventHandler == null); | |
a043164f KL |
983 | assert ((widget instanceof TMessageBox) |
984 | || (widget instanceof TFileOpenBox)); | |
c6940ed9 KL |
985 | secondaryEventReceiver = widget; |
986 | secondaryEventHandler = new WidgetEventHandler(this, false); | |
987 | (new Thread(secondaryEventHandler)).start(); | |
c6940ed9 KL |
988 | } |
989 | ||
990 | /** | |
991 | * Yield to the secondary thread. | |
992 | */ | |
993 | public final void yield() { | |
994 | assert (secondaryEventReceiver != null); | |
99144c71 KL |
995 | // This is where we handoff the event handler lock from the primary |
996 | // to secondary thread. We unlock here, and in a future loop the | |
997 | // secondary thread locks again. When it gives up, we have the | |
998 | // single lock back. | |
999 | boolean oldLock = unlockHandleEvent(); | |
1000 | assert (oldLock == true); | |
1001 | ||
c6940ed9 | 1002 | while (secondaryEventReceiver != null) { |
92554d64 | 1003 | synchronized (primaryEventHandler) { |
c6940ed9 | 1004 | try { |
92554d64 | 1005 | primaryEventHandler.wait(); |
c6940ed9 KL |
1006 | } catch (InterruptedException e) { |
1007 | // SQUASH | |
1008 | } | |
1009 | } | |
1010 | } | |
a06459bd KL |
1011 | } |
1012 | ||
4328bb42 KL |
1013 | /** |
1014 | * Do stuff when there is no user input. | |
1015 | */ | |
1016 | private void doIdle() { | |
99144c71 KL |
1017 | if (debugThreads) { |
1018 | System.err.printf("doIdle()\n"); | |
1019 | } | |
1020 | ||
7b5261bc | 1021 | // Now run any timers that have timed out |
d502a0e9 KL |
1022 | Date now = new Date(); |
1023 | List<TTimer> keepTimers = new LinkedList<TTimer>(); | |
1024 | for (TTimer timer: timers) { | |
92554d64 | 1025 | if (timer.getNextTick().getTime() <= now.getTime()) { |
d502a0e9 | 1026 | timer.tick(); |
c6940ed9 | 1027 | if (timer.recurring) { |
d502a0e9 | 1028 | keepTimers.add(timer); |
7b5261bc KL |
1029 | } |
1030 | } else { | |
d502a0e9 | 1031 | keepTimers.add(timer); |
7b5261bc KL |
1032 | } |
1033 | } | |
1034 | timers = keepTimers; | |
1035 | ||
1036 | // Call onIdle's | |
d502a0e9 KL |
1037 | for (TWindow window: windows) { |
1038 | window.onIdle(); | |
7b5261bc | 1039 | } |
4328bb42 | 1040 | } |
7d4115a5 | 1041 | |
4328bb42 KL |
1042 | /** |
1043 | * Get the amount of time I can sleep before missing a Timer tick. | |
1044 | * | |
92554d64 | 1045 | * @param timeout = initial (maximum) timeout in millis |
4328bb42 KL |
1046 | * @return number of milliseconds between now and the next timer event |
1047 | */ | |
bd8d51fa | 1048 | private long getSleepTime(final long timeout) { |
d502a0e9 | 1049 | Date now = new Date(); |
92554d64 | 1050 | long nowTime = now.getTime(); |
d502a0e9 KL |
1051 | long sleepTime = timeout; |
1052 | for (TTimer timer: timers) { | |
92554d64 KL |
1053 | long nextTickTime = timer.getNextTick().getTime(); |
1054 | if (nextTickTime < nowTime) { | |
7b5261bc KL |
1055 | return 0; |
1056 | } | |
92554d64 KL |
1057 | |
1058 | long timeDifference = nextTickTime - nowTime; | |
1059 | if (timeDifference < sleepTime) { | |
1060 | sleepTime = timeDifference; | |
7b5261bc KL |
1061 | } |
1062 | } | |
d502a0e9 | 1063 | assert (sleepTime >= 0); |
92554d64 KL |
1064 | assert (sleepTime <= timeout); |
1065 | return sleepTime; | |
7d4115a5 | 1066 | } |
4328bb42 | 1067 | |
48e27807 KL |
1068 | /** |
1069 | * Close window. Note that the window's destructor is NOT called by this | |
1070 | * method, instead the GC is assumed to do the cleanup. | |
1071 | * | |
1072 | * @param window the window to remove | |
1073 | */ | |
1074 | public final void closeWindow(final TWindow window) { | |
bb35d919 KL |
1075 | synchronized (windows) { |
1076 | int z = window.getZ(); | |
1077 | window.setZ(-1); | |
1078 | Collections.sort(windows); | |
1079 | windows.remove(0); | |
1080 | TWindow activeWindow = null; | |
1081 | for (TWindow w: windows) { | |
1082 | if (w.getZ() > z) { | |
1083 | w.setZ(w.getZ() - 1); | |
1084 | if (w.getZ() == 0) { | |
1085 | w.setActive(true); | |
1086 | assert (activeWindow == null); | |
1087 | activeWindow = w; | |
1088 | } else { | |
1089 | w.setActive(false); | |
1090 | } | |
48e27807 KL |
1091 | } |
1092 | } | |
1093 | } | |
1094 | ||
1095 | // Perform window cleanup | |
1096 | window.onClose(); | |
1097 | ||
48e27807 | 1098 | // Check if we are closing a TMessageBox or similar |
c6940ed9 KL |
1099 | if (secondaryEventReceiver != null) { |
1100 | assert (secondaryEventHandler != null); | |
48e27807 KL |
1101 | |
1102 | // Do not send events to the secondaryEventReceiver anymore, the | |
1103 | // window is closed. | |
1104 | secondaryEventReceiver = null; | |
1105 | ||
92554d64 KL |
1106 | // Wake the secondary thread, it will wake the primary as it |
1107 | // exits. | |
1108 | synchronized (secondaryEventHandler) { | |
1109 | secondaryEventHandler.notify(); | |
48e27807 KL |
1110 | } |
1111 | } | |
48e27807 KL |
1112 | } |
1113 | ||
1114 | /** | |
1115 | * Switch to the next window. | |
1116 | * | |
1117 | * @param forward if true, then switch to the next window in the list, | |
1118 | * otherwise switch to the previous window in the list | |
1119 | */ | |
1120 | public final void switchWindow(final boolean forward) { | |
48e27807 | 1121 | // Only switch if there are multiple windows |
fca67db0 | 1122 | if (windows.size() < 2) { |
48e27807 KL |
1123 | return; |
1124 | } | |
1125 | ||
bb35d919 KL |
1126 | synchronized (windows) { |
1127 | ||
1128 | // Swap z/active between active window and the next in the list | |
1129 | int activeWindowI = -1; | |
1130 | for (int i = 0; i < windows.size(); i++) { | |
7c870d89 | 1131 | if (windows.get(i).isActive()) { |
bb35d919 KL |
1132 | activeWindowI = i; |
1133 | break; | |
1134 | } | |
48e27807 | 1135 | } |
bb35d919 | 1136 | assert (activeWindowI >= 0); |
48e27807 | 1137 | |
bb35d919 KL |
1138 | // Do not switch if a window is modal |
1139 | if (windows.get(activeWindowI).isModal()) { | |
1140 | return; | |
1141 | } | |
48e27807 | 1142 | |
bb35d919 KL |
1143 | int nextWindowI; |
1144 | if (forward) { | |
1145 | nextWindowI = (activeWindowI + 1) % windows.size(); | |
48e27807 | 1146 | } else { |
bb35d919 KL |
1147 | if (activeWindowI == 0) { |
1148 | nextWindowI = windows.size() - 1; | |
1149 | } else { | |
1150 | nextWindowI = activeWindowI - 1; | |
1151 | } | |
48e27807 | 1152 | } |
bb35d919 KL |
1153 | windows.get(activeWindowI).setActive(false); |
1154 | windows.get(activeWindowI).setZ(windows.get(nextWindowI).getZ()); | |
1155 | windows.get(nextWindowI).setZ(0); | |
1156 | windows.get(nextWindowI).setActive(true); | |
1157 | ||
1158 | } // synchronized (windows) | |
48e27807 | 1159 | |
48e27807 KL |
1160 | } |
1161 | ||
1162 | /** | |
1163 | * Add a window to my window list and make it active. | |
1164 | * | |
1165 | * @param window new window to add | |
1166 | */ | |
1167 | public final void addWindow(final TWindow window) { | |
bb35d919 KL |
1168 | synchronized (windows) { |
1169 | // Do not allow a modal window to spawn a non-modal window | |
1170 | if ((windows.size() > 0) && (windows.get(0).isModal())) { | |
1171 | assert (window.isModal()); | |
1172 | } | |
1173 | for (TWindow w: windows) { | |
1174 | w.setActive(false); | |
1175 | w.setZ(w.getZ() + 1); | |
1176 | } | |
1177 | windows.add(window); | |
1178 | window.setActive(true); | |
1179 | window.setZ(0); | |
48e27807 | 1180 | } |
48e27807 KL |
1181 | } |
1182 | ||
fca67db0 KL |
1183 | /** |
1184 | * Check if there is a system-modal window on top. | |
1185 | * | |
1186 | * @return true if the active window is modal | |
1187 | */ | |
1188 | private boolean modalWindowActive() { | |
1189 | if (windows.size() == 0) { | |
1190 | return false; | |
1191 | } | |
1192 | return windows.get(windows.size() - 1).isModal(); | |
1193 | } | |
1194 | ||
1195 | /** | |
1196 | * Check if a mouse event would hit either the active menu or any open | |
1197 | * sub-menus. | |
1198 | * | |
1199 | * @param mouse mouse event | |
1200 | * @return true if the mouse would hit the active menu or an open | |
1201 | * sub-menu | |
1202 | */ | |
1203 | private boolean mouseOnMenu(final TMouseEvent mouse) { | |
1204 | assert (activeMenu != null); | |
1205 | List<TMenu> menus = new LinkedList<TMenu>(subMenus); | |
1206 | Collections.reverse(menus); | |
1207 | for (TMenu menu: menus) { | |
1208 | if (menu.mouseWouldHit(mouse)) { | |
1209 | return true; | |
1210 | } | |
1211 | } | |
1212 | return activeMenu.mouseWouldHit(mouse); | |
1213 | } | |
1214 | ||
1215 | /** | |
1216 | * See if we need to switch window or activate the menu based on | |
1217 | * a mouse click. | |
1218 | * | |
1219 | * @param mouse mouse event | |
1220 | */ | |
1221 | private void checkSwitchFocus(final TMouseEvent mouse) { | |
1222 | ||
1223 | if ((mouse.getType() == TMouseEvent.Type.MOUSE_DOWN) | |
1224 | && (activeMenu != null) | |
1225 | && (mouse.getAbsoluteY() != 0) | |
1226 | && (!mouseOnMenu(mouse)) | |
1227 | ) { | |
1228 | // They clicked outside the active menu, turn it off | |
1229 | activeMenu.setActive(false); | |
1230 | activeMenu = null; | |
1231 | for (TMenu menu: subMenus) { | |
1232 | menu.setActive(false); | |
1233 | } | |
1234 | subMenus.clear(); | |
1235 | // Continue checks | |
1236 | } | |
1237 | ||
1238 | // See if they hit the menu bar | |
1239 | if ((mouse.getType() == TMouseEvent.Type.MOUSE_DOWN) | |
7c870d89 | 1240 | && (mouse.isMouse1()) |
fca67db0 KL |
1241 | && (!modalWindowActive()) |
1242 | && (mouse.getAbsoluteY() == 0) | |
1243 | ) { | |
1244 | ||
1245 | for (TMenu menu: subMenus) { | |
1246 | menu.setActive(false); | |
1247 | } | |
1248 | subMenus.clear(); | |
1249 | ||
1250 | // They selected the menu, go activate it | |
1251 | for (TMenu menu: menus) { | |
1252 | if ((mouse.getAbsoluteX() >= menu.getX()) | |
1253 | && (mouse.getAbsoluteX() < menu.getX() | |
1254 | + menu.getTitle().length() + 2) | |
1255 | ) { | |
1256 | menu.setActive(true); | |
1257 | activeMenu = menu; | |
1258 | } else { | |
1259 | menu.setActive(false); | |
1260 | } | |
1261 | } | |
fca67db0 KL |
1262 | return; |
1263 | } | |
1264 | ||
1265 | // See if they hit the menu bar | |
1266 | if ((mouse.getType() == TMouseEvent.Type.MOUSE_MOTION) | |
7c870d89 | 1267 | && (mouse.isMouse1()) |
fca67db0 KL |
1268 | && (activeMenu != null) |
1269 | && (mouse.getAbsoluteY() == 0) | |
1270 | ) { | |
1271 | ||
1272 | TMenu oldMenu = activeMenu; | |
1273 | for (TMenu menu: subMenus) { | |
1274 | menu.setActive(false); | |
1275 | } | |
1276 | subMenus.clear(); | |
1277 | ||
1278 | // See if we should switch menus | |
1279 | for (TMenu menu: menus) { | |
1280 | if ((mouse.getAbsoluteX() >= menu.getX()) | |
1281 | && (mouse.getAbsoluteX() < menu.getX() | |
1282 | + menu.getTitle().length() + 2) | |
1283 | ) { | |
1284 | menu.setActive(true); | |
1285 | activeMenu = menu; | |
1286 | } | |
1287 | } | |
1288 | if (oldMenu != activeMenu) { | |
1289 | // They switched menus | |
1290 | oldMenu.setActive(false); | |
1291 | } | |
fca67db0 KL |
1292 | return; |
1293 | } | |
1294 | ||
1295 | // Only switch if there are multiple windows | |
1296 | if (windows.size() < 2) { | |
1297 | return; | |
1298 | } | |
1299 | ||
1300 | // Switch on the upclick | |
1301 | if (mouse.getType() != TMouseEvent.Type.MOUSE_UP) { | |
1302 | return; | |
1303 | } | |
1304 | ||
bb35d919 KL |
1305 | synchronized (windows) { |
1306 | Collections.sort(windows); | |
1307 | if (windows.get(0).isModal()) { | |
1308 | // Modal windows don't switch | |
1309 | return; | |
1310 | } | |
fca67db0 | 1311 | |
bb35d919 KL |
1312 | for (TWindow window: windows) { |
1313 | assert (!window.isModal()); | |
1314 | if (window.mouseWouldHit(mouse)) { | |
1315 | if (window == windows.get(0)) { | |
1316 | // Clicked on the same window, nothing to do | |
1317 | return; | |
1318 | } | |
1319 | ||
1320 | // We will be switching to another window | |
7c870d89 KL |
1321 | assert (windows.get(0).isActive()); |
1322 | assert (!window.isActive()); | |
bb35d919 KL |
1323 | windows.get(0).setActive(false); |
1324 | windows.get(0).setZ(window.getZ()); | |
1325 | window.setZ(0); | |
1326 | window.setActive(true); | |
fca67db0 KL |
1327 | return; |
1328 | } | |
fca67db0 KL |
1329 | } |
1330 | } | |
1331 | ||
1332 | // Clicked on the background, nothing to do | |
1333 | return; | |
1334 | } | |
1335 | ||
1336 | /** | |
1337 | * Turn off the menu. | |
1338 | */ | |
928811d8 | 1339 | public final void closeMenu() { |
fca67db0 KL |
1340 | if (activeMenu != null) { |
1341 | activeMenu.setActive(false); | |
1342 | activeMenu = null; | |
1343 | for (TMenu menu: subMenus) { | |
1344 | menu.setActive(false); | |
1345 | } | |
1346 | subMenus.clear(); | |
1347 | } | |
fca67db0 KL |
1348 | } |
1349 | ||
1350 | /** | |
1351 | * Turn off a sub-menu. | |
1352 | */ | |
928811d8 | 1353 | public final void closeSubMenu() { |
fca67db0 KL |
1354 | assert (activeMenu != null); |
1355 | TMenu item = subMenus.get(subMenus.size() - 1); | |
1356 | assert (item != null); | |
1357 | item.setActive(false); | |
1358 | subMenus.remove(subMenus.size() - 1); | |
fca67db0 KL |
1359 | } |
1360 | ||
1361 | /** | |
1362 | * Switch to the next menu. | |
1363 | * | |
1364 | * @param forward if true, then switch to the next menu in the list, | |
1365 | * otherwise switch to the previous menu in the list | |
1366 | */ | |
928811d8 | 1367 | public final void switchMenu(final boolean forward) { |
fca67db0 KL |
1368 | assert (activeMenu != null); |
1369 | ||
1370 | for (TMenu menu: subMenus) { | |
1371 | menu.setActive(false); | |
1372 | } | |
1373 | subMenus.clear(); | |
1374 | ||
1375 | for (int i = 0; i < menus.size(); i++) { | |
1376 | if (activeMenu == menus.get(i)) { | |
1377 | if (forward) { | |
1378 | if (i < menus.size() - 1) { | |
1379 | i++; | |
1380 | } | |
1381 | } else { | |
1382 | if (i > 0) { | |
1383 | i--; | |
1384 | } | |
1385 | } | |
1386 | activeMenu.setActive(false); | |
1387 | activeMenu = menus.get(i); | |
1388 | activeMenu.setActive(true); | |
fca67db0 KL |
1389 | return; |
1390 | } | |
1391 | } | |
1392 | } | |
1393 | ||
1394 | /** | |
1395 | * Method that TApplication subclasses can override to handle menu or | |
1396 | * posted command events. | |
1397 | * | |
1398 | * @param command command event | |
1399 | * @return if true, this event was consumed | |
1400 | */ | |
1401 | protected boolean onCommand(final TCommandEvent command) { | |
fca67db0 KL |
1402 | // Default: handle cmExit |
1403 | if (command.equals(cmExit)) { | |
1404 | if (messageBox("Confirmation", "Exit application?", | |
c6940ed9 | 1405 | TMessageBox.Type.YESNO).getResult() == TMessageBox.Result.YES) { |
fca67db0 KL |
1406 | quit = true; |
1407 | } | |
fca67db0 KL |
1408 | return true; |
1409 | } | |
1410 | ||
1411 | if (command.equals(cmShell)) { | |
34a42e78 | 1412 | openTerminal(0, 0, TWindow.RESIZABLE); |
fca67db0 KL |
1413 | return true; |
1414 | } | |
1415 | ||
1416 | if (command.equals(cmTile)) { | |
1417 | tileWindows(); | |
fca67db0 KL |
1418 | return true; |
1419 | } | |
1420 | if (command.equals(cmCascade)) { | |
1421 | cascadeWindows(); | |
fca67db0 KL |
1422 | return true; |
1423 | } | |
1424 | if (command.equals(cmCloseAll)) { | |
1425 | closeAllWindows(); | |
fca67db0 KL |
1426 | return true; |
1427 | } | |
c6940ed9 | 1428 | |
fca67db0 KL |
1429 | return false; |
1430 | } | |
1431 | ||
1432 | /** | |
1433 | * Method that TApplication subclasses can override to handle menu | |
1434 | * events. | |
1435 | * | |
1436 | * @param menu menu event | |
1437 | * @return if true, this event was consumed | |
1438 | */ | |
1439 | protected boolean onMenu(final TMenuEvent menu) { | |
1440 | ||
fca67db0 | 1441 | // Default: handle MID_EXIT |
8e688b92 | 1442 | if (menu.getId() == TMenu.MID_EXIT) { |
fca67db0 | 1443 | if (messageBox("Confirmation", "Exit application?", |
c6940ed9 | 1444 | TMessageBox.Type.YESNO).getResult() == TMessageBox.Result.YES) { |
fca67db0 KL |
1445 | quit = true; |
1446 | } | |
fca67db0 KL |
1447 | return true; |
1448 | } | |
1449 | ||
34a42e78 KL |
1450 | if (menu.getId() == TMenu.MID_SHELL) { |
1451 | openTerminal(0, 0, TWindow.RESIZABLE); | |
fca67db0 KL |
1452 | return true; |
1453 | } | |
1454 | ||
8e688b92 | 1455 | if (menu.getId() == TMenu.MID_TILE) { |
fca67db0 | 1456 | tileWindows(); |
fca67db0 KL |
1457 | return true; |
1458 | } | |
8e688b92 | 1459 | if (menu.getId() == TMenu.MID_CASCADE) { |
fca67db0 | 1460 | cascadeWindows(); |
fca67db0 KL |
1461 | return true; |
1462 | } | |
8e688b92 | 1463 | if (menu.getId() == TMenu.MID_CLOSE_ALL) { |
fca67db0 | 1464 | closeAllWindows(); |
fca67db0 KL |
1465 | return true; |
1466 | } | |
fca67db0 KL |
1467 | return false; |
1468 | } | |
1469 | ||
1470 | /** | |
1471 | * Method that TApplication subclasses can override to handle keystrokes. | |
1472 | * | |
1473 | * @param keypress keystroke event | |
1474 | * @return if true, this event was consumed | |
1475 | */ | |
1476 | protected boolean onKeypress(final TKeypressEvent keypress) { | |
1477 | // Default: only menu shortcuts | |
1478 | ||
1479 | // Process Alt-F, Alt-E, etc. menu shortcut keys | |
7c870d89 KL |
1480 | if (!keypress.getKey().isFnKey() |
1481 | && keypress.getKey().isAlt() | |
1482 | && !keypress.getKey().isCtrl() | |
fca67db0 KL |
1483 | && (activeMenu == null) |
1484 | ) { | |
1485 | ||
1486 | assert (subMenus.size() == 0); | |
1487 | ||
1488 | for (TMenu menu: menus) { | |
1489 | if (Character.toLowerCase(menu.getMnemonic().getShortcut()) | |
7c870d89 | 1490 | == Character.toLowerCase(keypress.getKey().getChar()) |
fca67db0 KL |
1491 | ) { |
1492 | activeMenu = menu; | |
1493 | menu.setActive(true); | |
fca67db0 KL |
1494 | return true; |
1495 | } | |
1496 | } | |
1497 | } | |
1498 | ||
1499 | return false; | |
1500 | } | |
48e27807 | 1501 | |
928811d8 KL |
1502 | /** |
1503 | * Add a keyboard accelerator to the global hash. | |
1504 | * | |
1505 | * @param item menu item this accelerator relates to | |
1506 | * @param keypress keypress that will dispatch a TMenuEvent | |
1507 | */ | |
1508 | public final void addAccelerator(final TMenuItem item, | |
1509 | final TKeypress keypress) { | |
e826b451 | 1510 | |
e826b451 KL |
1511 | synchronized (accelerators) { |
1512 | assert (accelerators.get(keypress) == null); | |
1513 | accelerators.put(keypress, item); | |
1514 | } | |
928811d8 KL |
1515 | } |
1516 | ||
1517 | /** | |
1518 | * Recompute menu x positions based on their title length. | |
1519 | */ | |
1520 | public final void recomputeMenuX() { | |
1521 | int x = 0; | |
1522 | for (TMenu menu: menus) { | |
1523 | menu.setX(x); | |
1524 | x += menu.getTitle().length() + 2; | |
1525 | } | |
1526 | } | |
1527 | ||
1528 | /** | |
1529 | * Post an event to process and turn off the menu. | |
1530 | * | |
1531 | * @param event new event to add to the queue | |
1532 | */ | |
1533 | public final void addMenuEvent(final TInputEvent event) { | |
8e688b92 KL |
1534 | synchronized (fillEventQueue) { |
1535 | fillEventQueue.add(event); | |
1536 | } | |
928811d8 KL |
1537 | closeMenu(); |
1538 | } | |
1539 | ||
1540 | /** | |
1541 | * Add a sub-menu to the list of open sub-menus. | |
1542 | * | |
1543 | * @param menu sub-menu | |
1544 | */ | |
1545 | public final void addSubMenu(final TMenu menu) { | |
1546 | subMenus.add(menu); | |
1547 | } | |
1548 | ||
8e688b92 KL |
1549 | /** |
1550 | * Convenience function to add a top-level menu. | |
1551 | * | |
1552 | * @param title menu title | |
1553 | * @return the new menu | |
1554 | */ | |
87a17f3c | 1555 | public final TMenu addMenu(final String title) { |
8e688b92 KL |
1556 | int x = 0; |
1557 | int y = 0; | |
1558 | TMenu menu = new TMenu(this, x, y, title); | |
1559 | menus.add(menu); | |
1560 | recomputeMenuX(); | |
1561 | return menu; | |
1562 | } | |
1563 | ||
1564 | /** | |
1565 | * Convenience function to add a default "File" menu. | |
1566 | * | |
1567 | * @return the new menu | |
1568 | */ | |
1569 | public final TMenu addFileMenu() { | |
1570 | TMenu fileMenu = addMenu("&File"); | |
1571 | fileMenu.addDefaultItem(TMenu.MID_OPEN_FILE); | |
1572 | fileMenu.addSeparator(); | |
1573 | fileMenu.addDefaultItem(TMenu.MID_SHELL); | |
1574 | fileMenu.addDefaultItem(TMenu.MID_EXIT); | |
1575 | return fileMenu; | |
1576 | } | |
1577 | ||
1578 | /** | |
1579 | * Convenience function to add a default "Edit" menu. | |
1580 | * | |
1581 | * @return the new menu | |
1582 | */ | |
1583 | public final TMenu addEditMenu() { | |
1584 | TMenu editMenu = addMenu("&Edit"); | |
1585 | editMenu.addDefaultItem(TMenu.MID_CUT); | |
1586 | editMenu.addDefaultItem(TMenu.MID_COPY); | |
1587 | editMenu.addDefaultItem(TMenu.MID_PASTE); | |
1588 | editMenu.addDefaultItem(TMenu.MID_CLEAR); | |
1589 | return editMenu; | |
1590 | } | |
1591 | ||
1592 | /** | |
1593 | * Convenience function to add a default "Window" menu. | |
1594 | * | |
1595 | * @return the new menu | |
1596 | */ | |
c6940ed9 | 1597 | public final TMenu addWindowMenu() { |
8e688b92 KL |
1598 | TMenu windowMenu = addMenu("&Window"); |
1599 | windowMenu.addDefaultItem(TMenu.MID_TILE); | |
1600 | windowMenu.addDefaultItem(TMenu.MID_CASCADE); | |
1601 | windowMenu.addDefaultItem(TMenu.MID_CLOSE_ALL); | |
1602 | windowMenu.addSeparator(); | |
1603 | windowMenu.addDefaultItem(TMenu.MID_WINDOW_MOVE); | |
1604 | windowMenu.addDefaultItem(TMenu.MID_WINDOW_ZOOM); | |
1605 | windowMenu.addDefaultItem(TMenu.MID_WINDOW_NEXT); | |
1606 | windowMenu.addDefaultItem(TMenu.MID_WINDOW_PREVIOUS); | |
1607 | windowMenu.addDefaultItem(TMenu.MID_WINDOW_CLOSE); | |
1608 | return windowMenu; | |
1609 | } | |
1610 | ||
1611 | /** | |
1612 | * Close all open windows. | |
1613 | */ | |
1614 | private void closeAllWindows() { | |
1615 | // Don't do anything if we are in the menu | |
1616 | if (activeMenu != null) { | |
1617 | return; | |
1618 | } | |
bb35d919 KL |
1619 | |
1620 | synchronized (windows) { | |
1621 | for (TWindow window: windows) { | |
1622 | closeWindow(window); | |
1623 | } | |
8e688b92 KL |
1624 | } |
1625 | } | |
1626 | ||
1627 | /** | |
1628 | * Re-layout the open windows as non-overlapping tiles. This produces | |
1629 | * almost the same results as Turbo Pascal 7.0's IDE. | |
1630 | */ | |
1631 | private void tileWindows() { | |
bb35d919 KL |
1632 | synchronized (windows) { |
1633 | // Don't do anything if we are in the menu | |
1634 | if (activeMenu != null) { | |
1635 | return; | |
8e688b92 | 1636 | } |
bb35d919 KL |
1637 | int z = windows.size(); |
1638 | if (z == 0) { | |
1639 | return; | |
1640 | } | |
1641 | int a = 0; | |
1642 | int b = 0; | |
1643 | a = (int)(Math.sqrt(z)); | |
1644 | int c = 0; | |
1645 | while (c < a) { | |
1646 | b = (z - c) / a; | |
1647 | if (((a * b) + c) == z) { | |
1648 | break; | |
1649 | } | |
1650 | c++; | |
8e688b92 | 1651 | } |
bb35d919 KL |
1652 | assert (a > 0); |
1653 | assert (b > 0); | |
1654 | assert (c < a); | |
1655 | int newWidth = (getScreen().getWidth() / a); | |
1656 | int newHeight1 = ((getScreen().getHeight() - 1) / b); | |
1657 | int newHeight2 = ((getScreen().getHeight() - 1) / (b + c)); | |
1658 | ||
1659 | List<TWindow> sorted = new LinkedList<TWindow>(windows); | |
1660 | Collections.sort(sorted); | |
1661 | Collections.reverse(sorted); | |
1662 | for (int i = 0; i < sorted.size(); i++) { | |
1663 | int logicalX = i / b; | |
1664 | int logicalY = i % b; | |
1665 | if (i >= ((a - 1) * b)) { | |
1666 | logicalX = a - 1; | |
1667 | logicalY = i - ((a - 1) * b); | |
1668 | } | |
8e688b92 | 1669 | |
bb35d919 KL |
1670 | TWindow w = sorted.get(i); |
1671 | w.setX(logicalX * newWidth); | |
1672 | w.setWidth(newWidth); | |
1673 | if (i >= ((a - 1) * b)) { | |
1674 | w.setY((logicalY * newHeight2) + 1); | |
1675 | w.setHeight(newHeight2); | |
1676 | } else { | |
1677 | w.setY((logicalY * newHeight1) + 1); | |
1678 | w.setHeight(newHeight1); | |
1679 | } | |
8e688b92 KL |
1680 | } |
1681 | } | |
1682 | } | |
1683 | ||
1684 | /** | |
1685 | * Re-layout the open windows as overlapping cascaded windows. | |
1686 | */ | |
1687 | private void cascadeWindows() { | |
bb35d919 KL |
1688 | synchronized (windows) { |
1689 | // Don't do anything if we are in the menu | |
1690 | if (activeMenu != null) { | |
1691 | return; | |
8e688b92 | 1692 | } |
bb35d919 KL |
1693 | int x = 0; |
1694 | int y = 1; | |
1695 | List<TWindow> sorted = new LinkedList<TWindow>(windows); | |
1696 | Collections.sort(sorted); | |
1697 | Collections.reverse(sorted); | |
1698 | for (TWindow window: sorted) { | |
1699 | window.setX(x); | |
1700 | window.setY(y); | |
1701 | x++; | |
1702 | y++; | |
1703 | if (x > getScreen().getWidth()) { | |
1704 | x = 0; | |
1705 | } | |
1706 | if (y >= getScreen().getHeight()) { | |
1707 | y = 1; | |
1708 | } | |
8e688b92 KL |
1709 | } |
1710 | } | |
1711 | } | |
1712 | ||
d502a0e9 KL |
1713 | /** |
1714 | * Convenience function to add a timer. | |
1715 | * | |
1716 | * @param duration number of milliseconds to wait between ticks | |
1717 | * @param recurring if true, re-schedule this timer after every tick | |
1718 | * @param action function to call when button is pressed | |
c6940ed9 | 1719 | * @return the timer |
d502a0e9 KL |
1720 | */ |
1721 | public final TTimer addTimer(final long duration, final boolean recurring, | |
1722 | final TAction action) { | |
1723 | ||
1724 | TTimer timer = new TTimer(duration, recurring, action); | |
1725 | synchronized (timers) { | |
1726 | timers.add(timer); | |
1727 | } | |
1728 | return timer; | |
1729 | } | |
1730 | ||
1731 | /** | |
1732 | * Convenience function to remove a timer. | |
1733 | * | |
1734 | * @param timer timer to remove | |
1735 | */ | |
1736 | public final void removeTimer(final TTimer timer) { | |
1737 | synchronized (timers) { | |
1738 | timers.remove(timer); | |
1739 | } | |
1740 | } | |
1741 | ||
c6940ed9 KL |
1742 | /** |
1743 | * Convenience function to spawn a message box. | |
1744 | * | |
1745 | * @param title window title, will be centered along the top border | |
1746 | * @param caption message to display. Use embedded newlines to get a | |
1747 | * multi-line box. | |
1748 | * @return the new message box | |
1749 | */ | |
1750 | public final TMessageBox messageBox(final String title, | |
1751 | final String caption) { | |
1752 | ||
1753 | return new TMessageBox(this, title, caption, TMessageBox.Type.OK); | |
1754 | } | |
1755 | ||
1756 | /** | |
1757 | * Convenience function to spawn a message box. | |
1758 | * | |
1759 | * @param title window title, will be centered along the top border | |
1760 | * @param caption message to display. Use embedded newlines to get a | |
1761 | * multi-line box. | |
1762 | * @param type one of the TMessageBox.Type constants. Default is | |
1763 | * Type.OK. | |
1764 | * @return the new message box | |
1765 | */ | |
1766 | public final TMessageBox messageBox(final String title, | |
1767 | final String caption, final TMessageBox.Type type) { | |
1768 | ||
1769 | return new TMessageBox(this, title, caption, type); | |
1770 | } | |
1771 | ||
1772 | /** | |
1773 | * Convenience function to spawn an input box. | |
1774 | * | |
1775 | * @param title window title, will be centered along the top border | |
1776 | * @param caption message to display. Use embedded newlines to get a | |
1777 | * multi-line box. | |
1778 | * @return the new input box | |
1779 | */ | |
1780 | public final TInputBox inputBox(final String title, final String caption) { | |
1781 | ||
1782 | return new TInputBox(this, title, caption); | |
1783 | } | |
1784 | ||
1785 | /** | |
1786 | * Convenience function to spawn an input box. | |
1787 | * | |
1788 | * @param title window title, will be centered along the top border | |
1789 | * @param caption message to display. Use embedded newlines to get a | |
1790 | * multi-line box. | |
1791 | * @param text initial text to seed the field with | |
1792 | * @return the new input box | |
1793 | */ | |
1794 | public final TInputBox inputBox(final String title, final String caption, | |
1795 | final String text) { | |
1796 | ||
1797 | return new TInputBox(this, title, caption, text); | |
1798 | } | |
1ac2ccb1 | 1799 | |
34a42e78 KL |
1800 | /** |
1801 | * Convenience function to open a terminal window. | |
1802 | * | |
1803 | * @param x column relative to parent | |
1804 | * @param y row relative to parent | |
1805 | * @return the terminal new window | |
1806 | */ | |
1807 | public final TTerminalWindow openTerminal(final int x, final int y) { | |
1808 | return openTerminal(x, y, TWindow.RESIZABLE); | |
1809 | } | |
1810 | ||
1811 | /** | |
1812 | * Convenience function to open a terminal window. | |
1813 | * | |
1814 | * @param x column relative to parent | |
1815 | * @param y row relative to parent | |
1816 | * @param flags mask of CENTERED, MODAL, or RESIZABLE | |
1817 | * @return the terminal new window | |
1818 | */ | |
1819 | public final TTerminalWindow openTerminal(final int x, final int y, | |
1820 | final int flags) { | |
1821 | ||
1822 | return new TTerminalWindow(this, x, y, flags); | |
1823 | } | |
1824 | ||
0d47c546 KL |
1825 | /** |
1826 | * Convenience function to spawn an file open box. | |
1827 | * | |
1828 | * @param path path of selected file | |
1829 | * @return the result of the new file open box | |
1830 | */ | |
1831 | public final String fileOpenBox(final String path) throws IOException { | |
1832 | ||
1833 | TFileOpenBox box = new TFileOpenBox(this, path, TFileOpenBox.Type.OPEN); | |
1834 | return box.getFilename(); | |
1835 | } | |
1836 | ||
1837 | /** | |
1838 | * Convenience function to spawn an file open box. | |
1839 | * | |
1840 | * @param path path of selected file | |
1841 | * @param type one of the Type constants | |
1842 | * @return the result of the new file open box | |
1843 | */ | |
1844 | public final String fileOpenBox(final String path, | |
1845 | final TFileOpenBox.Type type) throws IOException { | |
1846 | ||
1847 | TFileOpenBox box = new TFileOpenBox(this, path, type); | |
1848 | return box.getFilename(); | |
1849 | } | |
1850 | ||
7d4115a5 | 1851 | } |