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) { | |
bb35d919 KL |
1022 | synchronized (windows) { |
1023 | int z = window.getZ(); | |
1024 | window.setZ(-1); | |
1025 | Collections.sort(windows); | |
1026 | windows.remove(0); | |
1027 | TWindow activeWindow = null; | |
1028 | for (TWindow w: windows) { | |
1029 | if (w.getZ() > z) { | |
1030 | w.setZ(w.getZ() - 1); | |
1031 | if (w.getZ() == 0) { | |
1032 | w.setActive(true); | |
1033 | assert (activeWindow == null); | |
1034 | activeWindow = w; | |
1035 | } else { | |
1036 | w.setActive(false); | |
1037 | } | |
48e27807 KL |
1038 | } |
1039 | } | |
1040 | } | |
1041 | ||
1042 | // Perform window cleanup | |
1043 | window.onClose(); | |
1044 | ||
1045 | // Refresh screen | |
1046 | repaint = true; | |
1047 | ||
1048 | // Check if we are closing a TMessageBox or similar | |
c6940ed9 KL |
1049 | if (secondaryEventReceiver != null) { |
1050 | assert (secondaryEventHandler != null); | |
48e27807 KL |
1051 | |
1052 | // Do not send events to the secondaryEventReceiver anymore, the | |
1053 | // window is closed. | |
1054 | secondaryEventReceiver = null; | |
1055 | ||
92554d64 KL |
1056 | // Wake the secondary thread, it will wake the primary as it |
1057 | // exits. | |
1058 | synchronized (secondaryEventHandler) { | |
1059 | secondaryEventHandler.notify(); | |
48e27807 KL |
1060 | } |
1061 | } | |
48e27807 KL |
1062 | } |
1063 | ||
1064 | /** | |
1065 | * Switch to the next window. | |
1066 | * | |
1067 | * @param forward if true, then switch to the next window in the list, | |
1068 | * otherwise switch to the previous window in the list | |
1069 | */ | |
1070 | public final void switchWindow(final boolean forward) { | |
48e27807 | 1071 | // Only switch if there are multiple windows |
fca67db0 | 1072 | if (windows.size() < 2) { |
48e27807 KL |
1073 | return; |
1074 | } | |
1075 | ||
bb35d919 KL |
1076 | synchronized (windows) { |
1077 | ||
1078 | // Swap z/active between active window and the next in the list | |
1079 | int activeWindowI = -1; | |
1080 | for (int i = 0; i < windows.size(); i++) { | |
1081 | if (windows.get(i).getActive()) { | |
1082 | activeWindowI = i; | |
1083 | break; | |
1084 | } | |
48e27807 | 1085 | } |
bb35d919 | 1086 | assert (activeWindowI >= 0); |
48e27807 | 1087 | |
bb35d919 KL |
1088 | // Do not switch if a window is modal |
1089 | if (windows.get(activeWindowI).isModal()) { | |
1090 | return; | |
1091 | } | |
48e27807 | 1092 | |
bb35d919 KL |
1093 | int nextWindowI; |
1094 | if (forward) { | |
1095 | nextWindowI = (activeWindowI + 1) % windows.size(); | |
48e27807 | 1096 | } else { |
bb35d919 KL |
1097 | if (activeWindowI == 0) { |
1098 | nextWindowI = windows.size() - 1; | |
1099 | } else { | |
1100 | nextWindowI = activeWindowI - 1; | |
1101 | } | |
48e27807 | 1102 | } |
bb35d919 KL |
1103 | windows.get(activeWindowI).setActive(false); |
1104 | windows.get(activeWindowI).setZ(windows.get(nextWindowI).getZ()); | |
1105 | windows.get(nextWindowI).setZ(0); | |
1106 | windows.get(nextWindowI).setActive(true); | |
1107 | ||
1108 | } // synchronized (windows) | |
48e27807 KL |
1109 | |
1110 | // Refresh | |
1111 | repaint = true; | |
48e27807 KL |
1112 | } |
1113 | ||
1114 | /** | |
1115 | * Add a window to my window list and make it active. | |
1116 | * | |
1117 | * @param window new window to add | |
1118 | */ | |
1119 | public final void addWindow(final TWindow window) { | |
bb35d919 KL |
1120 | synchronized (windows) { |
1121 | // Do not allow a modal window to spawn a non-modal window | |
1122 | if ((windows.size() > 0) && (windows.get(0).isModal())) { | |
1123 | assert (window.isModal()); | |
1124 | } | |
1125 | for (TWindow w: windows) { | |
1126 | w.setActive(false); | |
1127 | w.setZ(w.getZ() + 1); | |
1128 | } | |
1129 | windows.add(window); | |
1130 | window.setActive(true); | |
1131 | window.setZ(0); | |
48e27807 | 1132 | } |
48e27807 KL |
1133 | } |
1134 | ||
fca67db0 KL |
1135 | /** |
1136 | * Check if there is a system-modal window on top. | |
1137 | * | |
1138 | * @return true if the active window is modal | |
1139 | */ | |
1140 | private boolean modalWindowActive() { | |
1141 | if (windows.size() == 0) { | |
1142 | return false; | |
1143 | } | |
1144 | return windows.get(windows.size() - 1).isModal(); | |
1145 | } | |
1146 | ||
1147 | /** | |
1148 | * Check if a mouse event would hit either the active menu or any open | |
1149 | * sub-menus. | |
1150 | * | |
1151 | * @param mouse mouse event | |
1152 | * @return true if the mouse would hit the active menu or an open | |
1153 | * sub-menu | |
1154 | */ | |
1155 | private boolean mouseOnMenu(final TMouseEvent mouse) { | |
1156 | assert (activeMenu != null); | |
1157 | List<TMenu> menus = new LinkedList<TMenu>(subMenus); | |
1158 | Collections.reverse(menus); | |
1159 | for (TMenu menu: menus) { | |
1160 | if (menu.mouseWouldHit(mouse)) { | |
1161 | return true; | |
1162 | } | |
1163 | } | |
1164 | return activeMenu.mouseWouldHit(mouse); | |
1165 | } | |
1166 | ||
1167 | /** | |
1168 | * See if we need to switch window or activate the menu based on | |
1169 | * a mouse click. | |
1170 | * | |
1171 | * @param mouse mouse event | |
1172 | */ | |
1173 | private void checkSwitchFocus(final TMouseEvent mouse) { | |
1174 | ||
1175 | if ((mouse.getType() == TMouseEvent.Type.MOUSE_DOWN) | |
1176 | && (activeMenu != null) | |
1177 | && (mouse.getAbsoluteY() != 0) | |
1178 | && (!mouseOnMenu(mouse)) | |
1179 | ) { | |
1180 | // They clicked outside the active menu, turn it off | |
1181 | activeMenu.setActive(false); | |
1182 | activeMenu = null; | |
1183 | for (TMenu menu: subMenus) { | |
1184 | menu.setActive(false); | |
1185 | } | |
1186 | subMenus.clear(); | |
1187 | // Continue checks | |
1188 | } | |
1189 | ||
1190 | // See if they hit the menu bar | |
1191 | if ((mouse.getType() == TMouseEvent.Type.MOUSE_DOWN) | |
1192 | && (mouse.getMouse1()) | |
1193 | && (!modalWindowActive()) | |
1194 | && (mouse.getAbsoluteY() == 0) | |
1195 | ) { | |
1196 | ||
1197 | for (TMenu menu: subMenus) { | |
1198 | menu.setActive(false); | |
1199 | } | |
1200 | subMenus.clear(); | |
1201 | ||
1202 | // They selected the menu, go activate it | |
1203 | for (TMenu menu: menus) { | |
1204 | if ((mouse.getAbsoluteX() >= menu.getX()) | |
1205 | && (mouse.getAbsoluteX() < menu.getX() | |
1206 | + menu.getTitle().length() + 2) | |
1207 | ) { | |
1208 | menu.setActive(true); | |
1209 | activeMenu = menu; | |
1210 | } else { | |
1211 | menu.setActive(false); | |
1212 | } | |
1213 | } | |
1214 | repaint = true; | |
1215 | return; | |
1216 | } | |
1217 | ||
1218 | // See if they hit the menu bar | |
1219 | if ((mouse.getType() == TMouseEvent.Type.MOUSE_MOTION) | |
1220 | && (mouse.getMouse1()) | |
1221 | && (activeMenu != null) | |
1222 | && (mouse.getAbsoluteY() == 0) | |
1223 | ) { | |
1224 | ||
1225 | TMenu oldMenu = activeMenu; | |
1226 | for (TMenu menu: subMenus) { | |
1227 | menu.setActive(false); | |
1228 | } | |
1229 | subMenus.clear(); | |
1230 | ||
1231 | // See if we should switch menus | |
1232 | for (TMenu menu: menus) { | |
1233 | if ((mouse.getAbsoluteX() >= menu.getX()) | |
1234 | && (mouse.getAbsoluteX() < menu.getX() | |
1235 | + menu.getTitle().length() + 2) | |
1236 | ) { | |
1237 | menu.setActive(true); | |
1238 | activeMenu = menu; | |
1239 | } | |
1240 | } | |
1241 | if (oldMenu != activeMenu) { | |
1242 | // They switched menus | |
1243 | oldMenu.setActive(false); | |
1244 | } | |
1245 | repaint = true; | |
1246 | return; | |
1247 | } | |
1248 | ||
1249 | // Only switch if there are multiple windows | |
1250 | if (windows.size() < 2) { | |
1251 | return; | |
1252 | } | |
1253 | ||
1254 | // Switch on the upclick | |
1255 | if (mouse.getType() != TMouseEvent.Type.MOUSE_UP) { | |
1256 | return; | |
1257 | } | |
1258 | ||
bb35d919 KL |
1259 | synchronized (windows) { |
1260 | Collections.sort(windows); | |
1261 | if (windows.get(0).isModal()) { | |
1262 | // Modal windows don't switch | |
1263 | return; | |
1264 | } | |
fca67db0 | 1265 | |
bb35d919 KL |
1266 | for (TWindow window: windows) { |
1267 | assert (!window.isModal()); | |
1268 | if (window.mouseWouldHit(mouse)) { | |
1269 | if (window == windows.get(0)) { | |
1270 | // Clicked on the same window, nothing to do | |
1271 | return; | |
1272 | } | |
1273 | ||
1274 | // We will be switching to another window | |
1275 | assert (windows.get(0).getActive()); | |
1276 | assert (!window.getActive()); | |
1277 | windows.get(0).setActive(false); | |
1278 | windows.get(0).setZ(window.getZ()); | |
1279 | window.setZ(0); | |
1280 | window.setActive(true); | |
1281 | repaint = true; | |
fca67db0 KL |
1282 | return; |
1283 | } | |
fca67db0 KL |
1284 | } |
1285 | } | |
1286 | ||
1287 | // Clicked on the background, nothing to do | |
1288 | return; | |
1289 | } | |
1290 | ||
1291 | /** | |
1292 | * Turn off the menu. | |
1293 | */ | |
928811d8 | 1294 | public final void closeMenu() { |
fca67db0 KL |
1295 | if (activeMenu != null) { |
1296 | activeMenu.setActive(false); | |
1297 | activeMenu = null; | |
1298 | for (TMenu menu: subMenus) { | |
1299 | menu.setActive(false); | |
1300 | } | |
1301 | subMenus.clear(); | |
1302 | } | |
1303 | repaint = true; | |
1304 | } | |
1305 | ||
1306 | /** | |
1307 | * Turn off a sub-menu. | |
1308 | */ | |
928811d8 | 1309 | public final void closeSubMenu() { |
fca67db0 KL |
1310 | assert (activeMenu != null); |
1311 | TMenu item = subMenus.get(subMenus.size() - 1); | |
1312 | assert (item != null); | |
1313 | item.setActive(false); | |
1314 | subMenus.remove(subMenus.size() - 1); | |
1315 | repaint = true; | |
1316 | } | |
1317 | ||
1318 | /** | |
1319 | * Switch to the next menu. | |
1320 | * | |
1321 | * @param forward if true, then switch to the next menu in the list, | |
1322 | * otherwise switch to the previous menu in the list | |
1323 | */ | |
928811d8 | 1324 | public final void switchMenu(final boolean forward) { |
fca67db0 KL |
1325 | assert (activeMenu != null); |
1326 | ||
1327 | for (TMenu menu: subMenus) { | |
1328 | menu.setActive(false); | |
1329 | } | |
1330 | subMenus.clear(); | |
1331 | ||
1332 | for (int i = 0; i < menus.size(); i++) { | |
1333 | if (activeMenu == menus.get(i)) { | |
1334 | if (forward) { | |
1335 | if (i < menus.size() - 1) { | |
1336 | i++; | |
1337 | } | |
1338 | } else { | |
1339 | if (i > 0) { | |
1340 | i--; | |
1341 | } | |
1342 | } | |
1343 | activeMenu.setActive(false); | |
1344 | activeMenu = menus.get(i); | |
1345 | activeMenu.setActive(true); | |
1346 | repaint = true; | |
1347 | return; | |
1348 | } | |
1349 | } | |
1350 | } | |
1351 | ||
1352 | /** | |
1353 | * Method that TApplication subclasses can override to handle menu or | |
1354 | * posted command events. | |
1355 | * | |
1356 | * @param command command event | |
1357 | * @return if true, this event was consumed | |
1358 | */ | |
1359 | protected boolean onCommand(final TCommandEvent command) { | |
fca67db0 KL |
1360 | // Default: handle cmExit |
1361 | if (command.equals(cmExit)) { | |
1362 | if (messageBox("Confirmation", "Exit application?", | |
c6940ed9 | 1363 | TMessageBox.Type.YESNO).getResult() == TMessageBox.Result.YES) { |
fca67db0 KL |
1364 | quit = true; |
1365 | } | |
1366 | repaint = true; | |
1367 | return true; | |
1368 | } | |
1369 | ||
1370 | if (command.equals(cmShell)) { | |
34a42e78 | 1371 | openTerminal(0, 0, TWindow.RESIZABLE); |
fca67db0 KL |
1372 | repaint = true; |
1373 | return true; | |
1374 | } | |
1375 | ||
1376 | if (command.equals(cmTile)) { | |
1377 | tileWindows(); | |
1378 | repaint = true; | |
1379 | return true; | |
1380 | } | |
1381 | if (command.equals(cmCascade)) { | |
1382 | cascadeWindows(); | |
1383 | repaint = true; | |
1384 | return true; | |
1385 | } | |
1386 | if (command.equals(cmCloseAll)) { | |
1387 | closeAllWindows(); | |
1388 | repaint = true; | |
1389 | return true; | |
1390 | } | |
c6940ed9 | 1391 | |
fca67db0 KL |
1392 | return false; |
1393 | } | |
1394 | ||
1395 | /** | |
1396 | * Method that TApplication subclasses can override to handle menu | |
1397 | * events. | |
1398 | * | |
1399 | * @param menu menu event | |
1400 | * @return if true, this event was consumed | |
1401 | */ | |
1402 | protected boolean onMenu(final TMenuEvent menu) { | |
1403 | ||
fca67db0 | 1404 | // Default: handle MID_EXIT |
8e688b92 | 1405 | if (menu.getId() == TMenu.MID_EXIT) { |
fca67db0 | 1406 | if (messageBox("Confirmation", "Exit application?", |
c6940ed9 | 1407 | TMessageBox.Type.YESNO).getResult() == TMessageBox.Result.YES) { |
fca67db0 KL |
1408 | quit = true; |
1409 | } | |
1410 | // System.err.printf("onMenu MID_EXIT result: quit = %s\n", quit); | |
1411 | repaint = true; | |
1412 | return true; | |
1413 | } | |
1414 | ||
34a42e78 KL |
1415 | if (menu.getId() == TMenu.MID_SHELL) { |
1416 | openTerminal(0, 0, TWindow.RESIZABLE); | |
fca67db0 KL |
1417 | repaint = true; |
1418 | return true; | |
1419 | } | |
1420 | ||
8e688b92 | 1421 | if (menu.getId() == TMenu.MID_TILE) { |
fca67db0 KL |
1422 | tileWindows(); |
1423 | repaint = true; | |
1424 | return true; | |
1425 | } | |
8e688b92 | 1426 | if (menu.getId() == TMenu.MID_CASCADE) { |
fca67db0 KL |
1427 | cascadeWindows(); |
1428 | repaint = true; | |
1429 | return true; | |
1430 | } | |
8e688b92 | 1431 | if (menu.getId() == TMenu.MID_CLOSE_ALL) { |
fca67db0 KL |
1432 | closeAllWindows(); |
1433 | repaint = true; | |
1434 | return true; | |
1435 | } | |
fca67db0 KL |
1436 | return false; |
1437 | } | |
1438 | ||
1439 | /** | |
1440 | * Method that TApplication subclasses can override to handle keystrokes. | |
1441 | * | |
1442 | * @param keypress keystroke event | |
1443 | * @return if true, this event was consumed | |
1444 | */ | |
1445 | protected boolean onKeypress(final TKeypressEvent keypress) { | |
1446 | // Default: only menu shortcuts | |
1447 | ||
1448 | // Process Alt-F, Alt-E, etc. menu shortcut keys | |
1449 | if (!keypress.getKey().getIsKey() | |
1450 | && keypress.getKey().getAlt() | |
1451 | && !keypress.getKey().getCtrl() | |
1452 | && (activeMenu == null) | |
1453 | ) { | |
1454 | ||
1455 | assert (subMenus.size() == 0); | |
1456 | ||
1457 | for (TMenu menu: menus) { | |
1458 | if (Character.toLowerCase(menu.getMnemonic().getShortcut()) | |
1459 | == Character.toLowerCase(keypress.getKey().getCh()) | |
1460 | ) { | |
1461 | activeMenu = menu; | |
1462 | menu.setActive(true); | |
1463 | repaint = true; | |
1464 | return true; | |
1465 | } | |
1466 | } | |
1467 | } | |
1468 | ||
1469 | return false; | |
1470 | } | |
48e27807 | 1471 | |
928811d8 KL |
1472 | /** |
1473 | * Add a keyboard accelerator to the global hash. | |
1474 | * | |
1475 | * @param item menu item this accelerator relates to | |
1476 | * @param keypress keypress that will dispatch a TMenuEvent | |
1477 | */ | |
1478 | public final void addAccelerator(final TMenuItem item, | |
1479 | final TKeypress keypress) { | |
e826b451 KL |
1480 | |
1481 | // System.err.printf("addAccelerator: key %s item %s\n", keypress, item); | |
1482 | ||
1483 | synchronized (accelerators) { | |
1484 | assert (accelerators.get(keypress) == null); | |
1485 | accelerators.put(keypress, item); | |
1486 | } | |
928811d8 KL |
1487 | } |
1488 | ||
1489 | /** | |
1490 | * Recompute menu x positions based on their title length. | |
1491 | */ | |
1492 | public final void recomputeMenuX() { | |
1493 | int x = 0; | |
1494 | for (TMenu menu: menus) { | |
1495 | menu.setX(x); | |
1496 | x += menu.getTitle().length() + 2; | |
1497 | } | |
1498 | } | |
1499 | ||
1500 | /** | |
1501 | * Post an event to process and turn off the menu. | |
1502 | * | |
1503 | * @param event new event to add to the queue | |
1504 | */ | |
1505 | public final void addMenuEvent(final TInputEvent event) { | |
8e688b92 KL |
1506 | synchronized (fillEventQueue) { |
1507 | fillEventQueue.add(event); | |
1508 | } | |
928811d8 KL |
1509 | closeMenu(); |
1510 | } | |
1511 | ||
1512 | /** | |
1513 | * Add a sub-menu to the list of open sub-menus. | |
1514 | * | |
1515 | * @param menu sub-menu | |
1516 | */ | |
1517 | public final void addSubMenu(final TMenu menu) { | |
1518 | subMenus.add(menu); | |
1519 | } | |
1520 | ||
8e688b92 KL |
1521 | /** |
1522 | * Convenience function to add a top-level menu. | |
1523 | * | |
1524 | * @param title menu title | |
1525 | * @return the new menu | |
1526 | */ | |
87a17f3c | 1527 | public final TMenu addMenu(final String title) { |
8e688b92 KL |
1528 | int x = 0; |
1529 | int y = 0; | |
1530 | TMenu menu = new TMenu(this, x, y, title); | |
1531 | menus.add(menu); | |
1532 | recomputeMenuX(); | |
1533 | return menu; | |
1534 | } | |
1535 | ||
1536 | /** | |
1537 | * Convenience function to add a default "File" menu. | |
1538 | * | |
1539 | * @return the new menu | |
1540 | */ | |
1541 | public final TMenu addFileMenu() { | |
1542 | TMenu fileMenu = addMenu("&File"); | |
1543 | fileMenu.addDefaultItem(TMenu.MID_OPEN_FILE); | |
1544 | fileMenu.addSeparator(); | |
1545 | fileMenu.addDefaultItem(TMenu.MID_SHELL); | |
1546 | fileMenu.addDefaultItem(TMenu.MID_EXIT); | |
1547 | return fileMenu; | |
1548 | } | |
1549 | ||
1550 | /** | |
1551 | * Convenience function to add a default "Edit" menu. | |
1552 | * | |
1553 | * @return the new menu | |
1554 | */ | |
1555 | public final TMenu addEditMenu() { | |
1556 | TMenu editMenu = addMenu("&Edit"); | |
1557 | editMenu.addDefaultItem(TMenu.MID_CUT); | |
1558 | editMenu.addDefaultItem(TMenu.MID_COPY); | |
1559 | editMenu.addDefaultItem(TMenu.MID_PASTE); | |
1560 | editMenu.addDefaultItem(TMenu.MID_CLEAR); | |
1561 | return editMenu; | |
1562 | } | |
1563 | ||
1564 | /** | |
1565 | * Convenience function to add a default "Window" menu. | |
1566 | * | |
1567 | * @return the new menu | |
1568 | */ | |
c6940ed9 | 1569 | public final TMenu addWindowMenu() { |
8e688b92 KL |
1570 | TMenu windowMenu = addMenu("&Window"); |
1571 | windowMenu.addDefaultItem(TMenu.MID_TILE); | |
1572 | windowMenu.addDefaultItem(TMenu.MID_CASCADE); | |
1573 | windowMenu.addDefaultItem(TMenu.MID_CLOSE_ALL); | |
1574 | windowMenu.addSeparator(); | |
1575 | windowMenu.addDefaultItem(TMenu.MID_WINDOW_MOVE); | |
1576 | windowMenu.addDefaultItem(TMenu.MID_WINDOW_ZOOM); | |
1577 | windowMenu.addDefaultItem(TMenu.MID_WINDOW_NEXT); | |
1578 | windowMenu.addDefaultItem(TMenu.MID_WINDOW_PREVIOUS); | |
1579 | windowMenu.addDefaultItem(TMenu.MID_WINDOW_CLOSE); | |
1580 | return windowMenu; | |
1581 | } | |
1582 | ||
1583 | /** | |
1584 | * Close all open windows. | |
1585 | */ | |
1586 | private void closeAllWindows() { | |
1587 | // Don't do anything if we are in the menu | |
1588 | if (activeMenu != null) { | |
1589 | return; | |
1590 | } | |
bb35d919 KL |
1591 | |
1592 | synchronized (windows) { | |
1593 | for (TWindow window: windows) { | |
1594 | closeWindow(window); | |
1595 | } | |
8e688b92 KL |
1596 | } |
1597 | } | |
1598 | ||
1599 | /** | |
1600 | * Re-layout the open windows as non-overlapping tiles. This produces | |
1601 | * almost the same results as Turbo Pascal 7.0's IDE. | |
1602 | */ | |
1603 | private void tileWindows() { | |
bb35d919 KL |
1604 | synchronized (windows) { |
1605 | // Don't do anything if we are in the menu | |
1606 | if (activeMenu != null) { | |
1607 | return; | |
8e688b92 | 1608 | } |
bb35d919 KL |
1609 | int z = windows.size(); |
1610 | if (z == 0) { | |
1611 | return; | |
1612 | } | |
1613 | int a = 0; | |
1614 | int b = 0; | |
1615 | a = (int)(Math.sqrt(z)); | |
1616 | int c = 0; | |
1617 | while (c < a) { | |
1618 | b = (z - c) / a; | |
1619 | if (((a * b) + c) == z) { | |
1620 | break; | |
1621 | } | |
1622 | c++; | |
8e688b92 | 1623 | } |
bb35d919 KL |
1624 | assert (a > 0); |
1625 | assert (b > 0); | |
1626 | assert (c < a); | |
1627 | int newWidth = (getScreen().getWidth() / a); | |
1628 | int newHeight1 = ((getScreen().getHeight() - 1) / b); | |
1629 | int newHeight2 = ((getScreen().getHeight() - 1) / (b + c)); | |
1630 | ||
1631 | List<TWindow> sorted = new LinkedList<TWindow>(windows); | |
1632 | Collections.sort(sorted); | |
1633 | Collections.reverse(sorted); | |
1634 | for (int i = 0; i < sorted.size(); i++) { | |
1635 | int logicalX = i / b; | |
1636 | int logicalY = i % b; | |
1637 | if (i >= ((a - 1) * b)) { | |
1638 | logicalX = a - 1; | |
1639 | logicalY = i - ((a - 1) * b); | |
1640 | } | |
8e688b92 | 1641 | |
bb35d919 KL |
1642 | TWindow w = sorted.get(i); |
1643 | w.setX(logicalX * newWidth); | |
1644 | w.setWidth(newWidth); | |
1645 | if (i >= ((a - 1) * b)) { | |
1646 | w.setY((logicalY * newHeight2) + 1); | |
1647 | w.setHeight(newHeight2); | |
1648 | } else { | |
1649 | w.setY((logicalY * newHeight1) + 1); | |
1650 | w.setHeight(newHeight1); | |
1651 | } | |
8e688b92 KL |
1652 | } |
1653 | } | |
1654 | } | |
1655 | ||
1656 | /** | |
1657 | * Re-layout the open windows as overlapping cascaded windows. | |
1658 | */ | |
1659 | private void cascadeWindows() { | |
bb35d919 KL |
1660 | synchronized (windows) { |
1661 | // Don't do anything if we are in the menu | |
1662 | if (activeMenu != null) { | |
1663 | return; | |
8e688b92 | 1664 | } |
bb35d919 KL |
1665 | int x = 0; |
1666 | int y = 1; | |
1667 | List<TWindow> sorted = new LinkedList<TWindow>(windows); | |
1668 | Collections.sort(sorted); | |
1669 | Collections.reverse(sorted); | |
1670 | for (TWindow window: sorted) { | |
1671 | window.setX(x); | |
1672 | window.setY(y); | |
1673 | x++; | |
1674 | y++; | |
1675 | if (x > getScreen().getWidth()) { | |
1676 | x = 0; | |
1677 | } | |
1678 | if (y >= getScreen().getHeight()) { | |
1679 | y = 1; | |
1680 | } | |
8e688b92 KL |
1681 | } |
1682 | } | |
1683 | } | |
1684 | ||
d502a0e9 KL |
1685 | /** |
1686 | * Convenience function to add a timer. | |
1687 | * | |
1688 | * @param duration number of milliseconds to wait between ticks | |
1689 | * @param recurring if true, re-schedule this timer after every tick | |
1690 | * @param action function to call when button is pressed | |
c6940ed9 | 1691 | * @return the timer |
d502a0e9 KL |
1692 | */ |
1693 | public final TTimer addTimer(final long duration, final boolean recurring, | |
1694 | final TAction action) { | |
1695 | ||
1696 | TTimer timer = new TTimer(duration, recurring, action); | |
1697 | synchronized (timers) { | |
1698 | timers.add(timer); | |
1699 | } | |
1700 | return timer; | |
1701 | } | |
1702 | ||
1703 | /** | |
1704 | * Convenience function to remove a timer. | |
1705 | * | |
1706 | * @param timer timer to remove | |
1707 | */ | |
1708 | public final void removeTimer(final TTimer timer) { | |
1709 | synchronized (timers) { | |
1710 | timers.remove(timer); | |
1711 | } | |
1712 | } | |
1713 | ||
c6940ed9 KL |
1714 | /** |
1715 | * Convenience function to spawn a message box. | |
1716 | * | |
1717 | * @param title window title, will be centered along the top border | |
1718 | * @param caption message to display. Use embedded newlines to get a | |
1719 | * multi-line box. | |
1720 | * @return the new message box | |
1721 | */ | |
1722 | public final TMessageBox messageBox(final String title, | |
1723 | final String caption) { | |
1724 | ||
1725 | return new TMessageBox(this, title, caption, TMessageBox.Type.OK); | |
1726 | } | |
1727 | ||
1728 | /** | |
1729 | * Convenience function to spawn a message box. | |
1730 | * | |
1731 | * @param title window title, will be centered along the top border | |
1732 | * @param caption message to display. Use embedded newlines to get a | |
1733 | * multi-line box. | |
1734 | * @param type one of the TMessageBox.Type constants. Default is | |
1735 | * Type.OK. | |
1736 | * @return the new message box | |
1737 | */ | |
1738 | public final TMessageBox messageBox(final String title, | |
1739 | final String caption, final TMessageBox.Type type) { | |
1740 | ||
1741 | return new TMessageBox(this, title, caption, type); | |
1742 | } | |
1743 | ||
1744 | /** | |
1745 | * Convenience function to spawn an input box. | |
1746 | * | |
1747 | * @param title window title, will be centered along the top border | |
1748 | * @param caption message to display. Use embedded newlines to get a | |
1749 | * multi-line box. | |
1750 | * @return the new input box | |
1751 | */ | |
1752 | public final TInputBox inputBox(final String title, final String caption) { | |
1753 | ||
1754 | return new TInputBox(this, title, caption); | |
1755 | } | |
1756 | ||
1757 | /** | |
1758 | * Convenience function to spawn an input box. | |
1759 | * | |
1760 | * @param title window title, will be centered along the top border | |
1761 | * @param caption message to display. Use embedded newlines to get a | |
1762 | * multi-line box. | |
1763 | * @param text initial text to seed the field with | |
1764 | * @return the new input box | |
1765 | */ | |
1766 | public final TInputBox inputBox(final String title, final String caption, | |
1767 | final String text) { | |
1768 | ||
1769 | return new TInputBox(this, title, caption, text); | |
1770 | } | |
1ac2ccb1 | 1771 | |
34a42e78 KL |
1772 | /** |
1773 | * Convenience function to open a terminal window. | |
1774 | * | |
1775 | * @param x column relative to parent | |
1776 | * @param y row relative to parent | |
1777 | * @return the terminal new window | |
1778 | */ | |
1779 | public final TTerminalWindow openTerminal(final int x, final int y) { | |
1780 | return openTerminal(x, y, TWindow.RESIZABLE); | |
1781 | } | |
1782 | ||
1783 | /** | |
1784 | * Convenience function to open a terminal window. | |
1785 | * | |
1786 | * @param x column relative to parent | |
1787 | * @param y row relative to parent | |
1788 | * @param flags mask of CENTERED, MODAL, or RESIZABLE | |
1789 | * @return the terminal new window | |
1790 | */ | |
1791 | public final TTerminalWindow openTerminal(final int x, final int y, | |
1792 | final int flags) { | |
1793 | ||
1794 | return new TTerminalWindow(this, x, y, flags); | |
1795 | } | |
1796 | ||
7d4115a5 | 1797 | } |