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