Merge branch 'subtree'
[fanfix.git] / src / jexer / TWindow.java
CommitLineData
daa4106c 1/*
48e27807
KL
2 * Jexer - Java Text User Interface
3 *
e16dda65 4 * The MIT License (MIT)
48e27807 5 *
a69ed767 6 * Copyright (C) 2019 Kevin Lamonte
48e27807 7 *
e16dda65
KL
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the "Software"),
10 * to deal in the Software without restriction, including without limitation
11 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 * and/or sell copies of the Software, and to permit persons to whom the
13 * Software is furnished to do so, subject to the following conditions:
48e27807 14 *
e16dda65
KL
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
48e27807 17 *
e16dda65
KL
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24 * DEALINGS IN THE SOFTWARE.
48e27807
KL
25 *
26 * @author Kevin Lamonte [kevin.lamonte@gmail.com]
27 * @version 1
28 */
29package jexer;
30
5dfd1c11 31import java.util.HashSet;
d36057df 32import java.util.Set;
5dfd1c11 33
42873e30 34import jexer.backend.Screen;
48e27807
KL
35import jexer.bits.CellAttributes;
36import jexer.bits.GraphicsChars;
97bc3f29 37import jexer.bits.StringUtils;
48e27807
KL
38import jexer.event.TCommandEvent;
39import jexer.event.TKeypressEvent;
40import jexer.event.TMenuEvent;
41import jexer.event.TMouseEvent;
42import jexer.event.TResizeEvent;
928811d8 43import jexer.menu.TMenu;
48e27807
KL
44import static jexer.TCommand.*;
45import static jexer.TKeypress.*;
46
47/**
48 * TWindow is the top-level container and drawing surface for other widgets.
49 */
2b9c27db 50public class TWindow extends TWidget {
48e27807 51
2ce6dab2 52 // ------------------------------------------------------------------------
d36057df 53 // Constants --------------------------------------------------------------
2ce6dab2
KL
54 // ------------------------------------------------------------------------
55
48e27807 56 /**
2ce6dab2 57 * Window is resizable (default yes).
48e27807 58 */
2ce6dab2 59 public static final int RESIZABLE = 0x01;
48e27807
KL
60
61 /**
2ce6dab2 62 * Window is modal (default no).
48e27807 63 */
2ce6dab2 64 public static final int MODAL = 0x02;
48e27807
KL
65
66 /**
2ce6dab2 67 * Window is centered (default no).
48e27807 68 */
2ce6dab2
KL
69 public static final int CENTERED = 0x04;
70
78a56d5d
KL
71 /**
72 * Window has no close box (default no). Window can still be closed via
73 * TApplication.closeWindow() and TWindow.close().
74 */
75 public static final int NOCLOSEBOX = 0x08;
76
68c5cd6b
KL
77 /**
78 * Window has no maximize box (default no).
79 */
80 public static final int NOZOOMBOX = 0x10;
81
48e27807 82 /**
d36057df
KL
83 * Window is placed at absolute position (no smart placement) (default
84 * no).
fca67db0 85 */
d36057df 86 public static final int ABSOLUTEXY = 0x20;
fca67db0
KL
87
88 /**
d36057df
KL
89 * Hitting the closebox with the mouse calls TApplication.hideWindow()
90 * rather than TApplication.closeWindow() (default no).
fca67db0 91 */
d36057df 92 public static final int HIDEONCLOSE = 0x40;
48e27807 93
9696a8f6
KL
94 /**
95 * Menus cannot be used when this window is active (default no).
96 */
97 public static final int OVERRIDEMENU = 0x80;
98
2ce6dab2 99 // ------------------------------------------------------------------------
d36057df 100 // Variables --------------------------------------------------------------
2ce6dab2 101 // ------------------------------------------------------------------------
48e27807
KL
102
103 /**
d36057df 104 * Window flags. Note package private access.
48e27807 105 */
d36057df 106 int flags = RESIZABLE;
48e27807
KL
107
108 /**
d36057df 109 * Window title.
48e27807 110 */
d36057df 111 private String title = "";
48e27807
KL
112
113 /**
d36057df 114 * Window's parent TApplication.
48e27807 115 */
d36057df 116 private TApplication application;
48e27807
KL
117
118 /**
119 * Z order. Lower number means more in-front.
120 */
121 private int z = 0;
122
5dfd1c11
KL
123 /**
124 * Window's keyboard shortcuts. Any key in this set will be passed to
125 * the window directly rather than processed through the menu
126 * accelerators.
127 */
d36057df 128 private Set<TKeypress> keyboardShortcuts = new HashSet<TKeypress>();
2ce6dab2 129
48e27807
KL
130 /**
131 * If true, then the user clicked on the title bar and is moving the
132 * window.
133 */
bd8d51fa 134 protected boolean inWindowMove = false;
48e27807
KL
135
136 /**
137 * If true, then the user clicked on the bottom right corner and is
138 * resizing the window.
139 */
bd8d51fa 140 protected boolean inWindowResize = false;
48e27807
KL
141
142 /**
143 * If true, then the user selected "Size/Move" (or hit Ctrl-F5) and is
144 * resizing/moving the window via the keyboard.
145 */
d36057df 146 protected boolean inKeyboardResize = false;
48e27807
KL
147
148 /**
149 * If true, this window is maximized.
150 */
151 private boolean maximized = false;
152
153 /**
154 * Remember mouse state.
155 */
928811d8 156 protected TMouseEvent mouse;
48e27807
KL
157
158 // For moving the window. resizing also uses moveWindowMouseX/Y
159 private int moveWindowMouseX;
160 private int moveWindowMouseY;
161 private int oldWindowX;
162 private int oldWindowY;
163
164 // Resizing
165 private int resizeWindowWidth;
166 private int resizeWindowHeight;
167 private int minimumWindowWidth = 10;
168 private int minimumWindowHeight = 2;
169 private int maximumWindowWidth = -1;
170 private int maximumWindowHeight = -1;
171
172 // For maximize/restore
173 private int restoreWindowWidth;
174 private int restoreWindowHeight;
175 private int restoreWindowX;
176 private int restoreWindowY;
177
34a42e78 178 /**
d36057df
KL
179 * Hidden flag. A hidden window will still have its onIdle() called, and
180 * will also have onClose() called at application exit. Note package
181 * private access: TApplication will force hidden false if a modal window
182 * is active.
34a42e78 183 */
d36057df 184 boolean hidden = false;
34a42e78 185
71a389c9 186 /**
d36057df
KL
187 * A window may have a status bar associated with it. TApplication will
188 * draw this status bar last, and will also route events to it first
189 * before the window.
71a389c9 190 */
d36057df
KL
191 protected TStatusBar statusBar = null;
192
978a5d8f
KL
193 /**
194 * A window may request that TApplication NOT draw the mouse cursor over
195 * it by setting this to true. This is currently only used within Jexer
196 * by TTerminalWindow so that only the bottom-most instance of nested
197 * Jexer's draws the mouse within its application window. But perhaps
198 * other applications can use it, so public getter/setter is provided.
199 */
200 private boolean hideMouse = false;
201
4941d2d6
KL
202 /**
203 * The help topic for this window.
204 */
205 protected String helpTopic = "Help";
206
d36057df
KL
207 // ------------------------------------------------------------------------
208 // Constructors -----------------------------------------------------------
209 // ------------------------------------------------------------------------
71a389c9
KL
210
211 /**
d36057df 212 * Public constructor. Window will be located at (0, 0).
71a389c9 213 *
d36057df
KL
214 * @param application TApplication that manages this window
215 * @param title window title, will be centered along the top border
216 * @param width width of window
217 * @param height height of window
71a389c9 218 */
d36057df
KL
219 public TWindow(final TApplication application, final String title,
220 final int width, final int height) {
221
222 this(application, title, 0, 0, width, height, RESIZABLE);
71a389c9
KL
223 }
224
225 /**
d36057df 226 * Public constructor. Window will be located at (0, 0).
71a389c9 227 *
d36057df
KL
228 * @param application TApplication that manages this window
229 * @param title window title, will be centered along the top border
230 * @param width width of window
231 * @param height height of window
232 * @param flags bitmask of RESIZABLE, CENTERED, or MODAL
71a389c9 233 */
d36057df
KL
234 public TWindow(final TApplication application, final String title,
235 final int width, final int height, final int flags) {
71a389c9 236
d36057df 237 this(application, title, 0, 0, width, height, flags);
2ce6dab2
KL
238 }
239
240 /**
d36057df
KL
241 * Public constructor.
242 *
243 * @param application TApplication that manages this window
244 * @param title window title, will be centered along the top border
245 * @param x column relative to parent
246 * @param y row relative to parent
247 * @param width width of window
248 * @param height height of window
2ce6dab2 249 */
d36057df
KL
250 public TWindow(final TApplication application, final String title,
251 final int x, final int y, final int width, final int height) {
92453213 252
d36057df 253 this(application, title, x, y, width, height, RESIZABLE);
48e27807
KL
254 }
255
256 /**
257 * Public constructor.
258 *
259 * @param application TApplication that manages this window
260 * @param title window title, will be centered along the top border
261 * @param x column relative to parent
262 * @param y row relative to parent
263 * @param width width of window
264 * @param height height of window
265 * @param flags mask of RESIZABLE, CENTERED, or MODAL
266 */
267 public TWindow(final TApplication application, final String title,
268 final int x, final int y, final int width, final int height,
269 final int flags) {
270
fca67db0
KL
271 super();
272
48e27807 273 // I am my own window and parent
fca67db0
KL
274 setupForTWindow(this, x, y + application.getDesktopTop(),
275 width, height);
48e27807
KL
276
277 // Save fields
278 this.title = title;
279 this.application = application;
48e27807
KL
280 this.flags = flags;
281
282 // Minimum width/height are 10 and 2
283 assert (width >= 10);
fca67db0 284 assert (getHeight() >= 2);
48e27807
KL
285
286 // MODAL implies CENTERED
287 if (isModal()) {
288 this.flags |= CENTERED;
289 }
290
291 // Center window if specified
292 center();
293
294 // Add me to the application
d36057df 295 application.addWindowToApplication(this);
48e27807
KL
296 }
297
2ce6dab2 298 // ------------------------------------------------------------------------
d36057df 299 // Event handlers ---------------------------------------------------------
2ce6dab2 300 // ------------------------------------------------------------------------
48e27807 301
fe0770f9 302 /**
d36057df 303 * Returns true if the mouse is currently on the close button.
fe0770f9 304 *
d36057df 305 * @return true if mouse is currently on the close button
fe0770f9 306 */
d36057df
KL
307 protected boolean mouseOnClose() {
308 if ((flags & NOCLOSEBOX) != 0) {
309 return false;
310 }
311 if ((mouse != null)
312 && (mouse.getAbsoluteY() == getY())
313 && (mouse.getAbsoluteX() == getX() + 3)
314 ) {
fe0770f9
KL
315 return true;
316 }
317 return false;
318 }
319
320 /**
d36057df 321 * Returns true if the mouse is currently on the maximize/restore button.
48e27807 322 *
d36057df 323 * @return true if the mouse is currently on the maximize/restore button
48e27807 324 */
d36057df
KL
325 protected boolean mouseOnMaximize() {
326 if ((flags & NOZOOMBOX) != 0) {
48e27807
KL
327 return false;
328 }
d36057df
KL
329 if ((mouse != null)
330 && !isModal()
331 && (mouse.getAbsoluteY() == getY())
332 && (mouse.getAbsoluteX() == getX() + getWidth() - 4)
333 ) {
334 return true;
335 }
336 return false;
48e27807
KL
337 }
338
78a56d5d 339 /**
d36057df
KL
340 * Returns true if the mouse is currently on the resizable lower right
341 * corner.
78a56d5d 342 *
d36057df
KL
343 * @return true if the mouse is currently on the resizable lower right
344 * corner
78a56d5d 345 */
d36057df
KL
346 protected boolean mouseOnResize() {
347 if (((flags & RESIZABLE) != 0)
348 && !isModal()
349 && (mouse != null)
350 && (mouse.getAbsoluteY() == getY() + getHeight() - 1)
351 && ((mouse.getAbsoluteX() == getX() + getWidth() - 1)
352 || (mouse.getAbsoluteX() == getX() + getWidth() - 2))
353 ) {
78a56d5d
KL
354 return true;
355 }
356 return false;
357 }
358
a69ed767
KL
359 /**
360 * Subclasses should override this method to perform any user prompting
361 * before they are offscreen. Note that unlike other windowing toolkits,
362 * windows can NOT use this function in some manner to avoid being
363 * closed. This is called by application.closeWindow().
364 */
365 protected void onPreClose() {
366 // Default: do nothing.
367 }
368
68c5cd6b 369 /**
d36057df
KL
370 * Subclasses should override this method to cleanup resources. This is
371 * called by application.closeWindow().
68c5cd6b 372 */
a69ed767
KL
373 protected void onClose() {
374 // Default: perform widget-specific cleanup.
375 for (TWidget w: getChildren()) {
376 w.close();
377 }
68c5cd6b
KL
378 }
379
48e27807 380 /**
d36057df
KL
381 * Called by application.switchWindow() when this window gets the
382 * focus, and also by application.addWindow().
48e27807 383 */
a69ed767 384 protected void onFocus() {
d36057df 385 // Default: do nothing
48e27807
KL
386 }
387
388 /**
d36057df
KL
389 * Called by application.switchWindow() when another window gets the
390 * focus.
48e27807 391 */
a69ed767 392 protected void onUnfocus() {
d36057df 393 // Default: do nothing
48e27807
KL
394 }
395
396 /**
d36057df 397 * Called by application.hideWindow().
92453213 398 */
a69ed767 399 protected void onHide() {
92453213
KL
400 // Default: do nothing
401 }
402
403 /**
404 * Called by application.showWindow().
405 */
a69ed767 406 protected void onShow() {
92453213
KL
407 // Default: do nothing
408 }
409
48e27807
KL
410 /**
411 * Handle mouse button presses.
412 *
413 * @param mouse mouse button event
414 */
415 @Override
416 public void onMouseDown(final TMouseEvent mouse) {
417 this.mouse = mouse;
48e27807
KL
418
419 inKeyboardResize = false;
a69ed767
KL
420 inWindowMove = false;
421 inWindowResize = false;
48e27807 422
fca67db0 423 if ((mouse.getAbsoluteY() == getY())
7c870d89 424 && mouse.isMouse1()
fca67db0
KL
425 && (getX() <= mouse.getAbsoluteX())
426 && (mouse.getAbsoluteX() < getX() + getWidth())
48e27807
KL
427 && !mouseOnClose()
428 && !mouseOnMaximize()
429 ) {
430 // Begin moving window
431 inWindowMove = true;
432 moveWindowMouseX = mouse.getAbsoluteX();
433 moveWindowMouseY = mouse.getAbsoluteY();
fca67db0
KL
434 oldWindowX = getX();
435 oldWindowY = getY();
48e27807
KL
436 if (maximized) {
437 maximized = false;
438 }
439 return;
440 }
441 if (mouseOnResize()) {
442 // Begin window resize
443 inWindowResize = true;
444 moveWindowMouseX = mouse.getAbsoluteX();
445 moveWindowMouseY = mouse.getAbsoluteY();
fca67db0
KL
446 resizeWindowWidth = getWidth();
447 resizeWindowHeight = getHeight();
48e27807
KL
448 if (maximized) {
449 maximized = false;
450 }
451 return;
452 }
453
2ce6dab2
KL
454 // Give the shortcut bar a shot at this.
455 if (statusBar != null) {
456 if (statusBar.statusBarMouseDown(mouse)) {
457 return;
458 }
459 }
460
48e27807
KL
461 // I didn't take it, pass it on to my children
462 super.onMouseDown(mouse);
463 }
464
48e27807
KL
465 /**
466 * Handle mouse button releases.
467 *
468 * @param mouse mouse button release event
469 */
470 @Override
471 public void onMouseUp(final TMouseEvent mouse) {
472 this.mouse = mouse;
48e27807 473
7c870d89 474 if ((inWindowMove) && (mouse.isMouse1())) {
48e27807
KL
475 // Stop moving window
476 inWindowMove = false;
477 return;
478 }
479
7c870d89 480 if ((inWindowResize) && (mouse.isMouse1())) {
48e27807
KL
481 // Stop resizing window
482 inWindowResize = false;
483 return;
484 }
485
7c870d89 486 if (mouse.isMouse1() && mouseOnClose()) {
d36057df
KL
487 if ((flags & HIDEONCLOSE) == 0) {
488 // Close window
489 application.closeWindow(this);
490 } else {
491 // Hide window
492 application.hideWindow(this);
493 }
48e27807
KL
494 return;
495 }
496
fca67db0 497 if ((mouse.getAbsoluteY() == getY())
7c870d89 498 && mouse.isMouse1()
48e27807
KL
499 && mouseOnMaximize()) {
500 if (maximized) {
501 // Restore
502 restore();
503 } else {
504 // Maximize
505 maximize();
506 }
507 // Pass a resize event to my children
fca67db0
KL
508 onResize(new TResizeEvent(TResizeEvent.Type.WIDGET,
509 getWidth(), getHeight()));
48e27807
KL
510 return;
511 }
512
2ce6dab2
KL
513 // Give the shortcut bar a shot at this.
514 if (statusBar != null) {
515 if (statusBar.statusBarMouseUp(mouse)) {
516 return;
517 }
518 }
519
48e27807
KL
520 // I didn't take it, pass it on to my children
521 super.onMouseUp(mouse);
522 }
523
524 /**
525 * Handle mouse movements.
526 *
527 * @param mouse mouse motion event
528 */
529 @Override
530 public void onMouseMotion(final TMouseEvent mouse) {
531 this.mouse = mouse;
48e27807
KL
532
533 if (inWindowMove) {
534 // Move window over
fca67db0
KL
535 setX(oldWindowX + (mouse.getAbsoluteX() - moveWindowMouseX));
536 setY(oldWindowY + (mouse.getAbsoluteY() - moveWindowMouseY));
48e27807 537 // Don't cover up the menu bar
fca67db0
KL
538 if (getY() < application.getDesktopTop()) {
539 setY(application.getDesktopTop());
48e27807 540 }
2ce6dab2
KL
541 // Don't go below the status bar
542 if (getY() >= application.getDesktopBottom()) {
543 setY(application.getDesktopBottom() - 1);
544 }
48e27807
KL
545 return;
546 }
547
548 if (inWindowResize) {
549 // Move window over
fca67db0
KL
550 setWidth(resizeWindowWidth + (mouse.getAbsoluteX()
551 - moveWindowMouseX));
552 setHeight(resizeWindowHeight + (mouse.getAbsoluteY()
553 - moveWindowMouseY));
554 if (getX() + getWidth() > getScreen().getWidth()) {
555 setWidth(getScreen().getWidth() - getX());
48e27807 556 }
fca67db0
KL
557 if (getY() + getHeight() > application.getDesktopBottom()) {
558 setY(application.getDesktopBottom() - getHeight() + 1);
48e27807
KL
559 }
560 // Don't cover up the menu bar
fca67db0
KL
561 if (getY() < application.getDesktopTop()) {
562 setY(application.getDesktopTop());
48e27807
KL
563 }
564
565 // Keep within min/max bounds
fca67db0
KL
566 if (getWidth() < minimumWindowWidth) {
567 setWidth(minimumWindowWidth);
48e27807 568 }
fca67db0
KL
569 if (getHeight() < minimumWindowHeight) {
570 setHeight(minimumWindowHeight);
48e27807 571 }
fca67db0
KL
572 if ((maximumWindowWidth > 0)
573 && (getWidth() > maximumWindowWidth)
574 ) {
575 setWidth(maximumWindowWidth);
48e27807 576 }
fca67db0
KL
577 if ((maximumWindowHeight > 0)
578 && (getHeight() > maximumWindowHeight)
579 ) {
580 setHeight(maximumWindowHeight);
6b45ccfe
KL
581 }
582 if (getHeight() + getY() >= getApplication().getDesktopBottom()) {
583 setHeight(getApplication().getDesktopBottom() - getY());
48e27807
KL
584 }
585
586 // Pass a resize event to my children
fca67db0
KL
587 onResize(new TResizeEvent(TResizeEvent.Type.WIDGET,
588 getWidth(), getHeight()));
48e27807
KL
589 return;
590 }
591
2ce6dab2
KL
592 // Give the shortcut bar a shot at this.
593 if (statusBar != null) {
594 statusBar.statusBarMouseMotion(mouse);
595 }
596
48e27807
KL
597 // I didn't take it, pass it on to my children
598 super.onMouseMotion(mouse);
599 }
600
601 /**
602 * Handle keystrokes.
603 *
604 * @param keypress keystroke event
605 */
606 @Override
607 public void onKeypress(final TKeypressEvent keypress) {
608
1aff59b6
KL
609 if (inWindowMove || inWindowResize) {
610 // ESC or ENTER - Exit size/move
611 if (keypress.equals(kbEsc) || keypress.equals(kbEnter)) {
612 inWindowMove = false;
613 inWindowResize = false;
614 return;
615 }
616 }
617
48e27807
KL
618 if (inKeyboardResize) {
619
32437017
KL
620 // ESC or ENTER - Exit size/move
621 if (keypress.equals(kbEsc) || keypress.equals(kbEnter)) {
48e27807
KL
622 inKeyboardResize = false;
623 }
624
625 if (keypress.equals(kbLeft)) {
fca67db0
KL
626 if (getX() > 0) {
627 setX(getX() - 1);
48e27807
KL
628 }
629 }
630 if (keypress.equals(kbRight)) {
fca67db0
KL
631 if (getX() < getScreen().getWidth() - 1) {
632 setX(getX() + 1);
48e27807
KL
633 }
634 }
635 if (keypress.equals(kbDown)) {
fca67db0
KL
636 if (getY() < application.getDesktopBottom() - 1) {
637 setY(getY() + 1);
48e27807
KL
638 }
639 }
640 if (keypress.equals(kbUp)) {
fca67db0
KL
641 if (getY() > 1) {
642 setY(getY() - 1);
48e27807
KL
643 }
644 }
3b0a5f8b
KL
645
646 /*
647 * Only permit keyboard resizing if the window was RESIZABLE.
648 */
649 if ((flags & RESIZABLE) != 0) {
650
651 if (keypress.equals(kbShiftLeft)) {
652 if ((getWidth() > minimumWindowWidth)
653 || (minimumWindowWidth <= 0)
654 ) {
655 setWidth(getWidth() - 1);
656 }
48e27807 657 }
3b0a5f8b
KL
658 if (keypress.equals(kbShiftRight)) {
659 if ((getWidth() < maximumWindowWidth)
660 || (maximumWindowWidth <= 0)
661 ) {
662 setWidth(getWidth() + 1);
663 }
48e27807 664 }
3b0a5f8b
KL
665 if (keypress.equals(kbShiftUp)) {
666 if ((getHeight() > minimumWindowHeight)
667 || (minimumWindowHeight <= 0)
668 ) {
669 setHeight(getHeight() - 1);
670 }
48e27807 671 }
3b0a5f8b
KL
672 if (keypress.equals(kbShiftDown)) {
673 if ((getHeight() < maximumWindowHeight)
674 || (maximumWindowHeight <= 0)
675 ) {
676 setHeight(getHeight() + 1);
677 }
48e27807 678 }
48e27807 679
3b0a5f8b
KL
680 // Pass a resize event to my children
681 onResize(new TResizeEvent(TResizeEvent.Type.WIDGET,
682 getWidth(), getHeight()));
683
684 } // if ((flags & RESIZABLE) != 0)
0d47c546 685
48e27807
KL
686 return;
687 }
688
2ce6dab2
KL
689 // Give the shortcut bar a shot at this.
690 if (statusBar != null) {
691 if (statusBar.statusBarKeypress(keypress)) {
692 return;
693 }
694 }
695
48e27807
KL
696 // These keystrokes will typically not be seen unless a subclass
697 // overrides onMenu() due to how TApplication dispatches
698 // accelerators.
699
92453213 700 if (!(this instanceof TDesktop)) {
48e27807 701
92453213
KL
702 // Ctrl-W - close window
703 if (keypress.equals(kbCtrlW)) {
78a56d5d 704 if ((flags & NOCLOSEBOX) == 0) {
d36057df
KL
705 if ((flags & HIDEONCLOSE) == 0) {
706 // Close window
707 application.closeWindow(this);
708 } else {
709 // Hide window
710 application.hideWindow(this);
711 }
78a56d5d 712 }
92453213
KL
713 return;
714 }
48e27807 715
92453213
KL
716 // F6 - behave like Alt-TAB
717 if (keypress.equals(kbF6)) {
718 application.switchWindow(true);
719 return;
720 }
48e27807 721
92453213
KL
722 // Shift-F6 - behave like Shift-Alt-TAB
723 if (keypress.equals(kbShiftF6)) {
724 application.switchWindow(false);
725 return;
48e27807 726 }
48e27807 727
92453213 728 // F5 - zoom
68c5cd6b 729 if (keypress.equals(kbF5) && ((flags & NOZOOMBOX) == 0)) {
92453213
KL
730 if (maximized) {
731 restore();
732 } else {
733 maximize();
734 }
735 }
736
737 // Ctrl-F5 - size/move
738 if (keypress.equals(kbCtrlF5)) {
739 inKeyboardResize = !inKeyboardResize;
740 }
741
742 } // if (!(this instanceof TDesktop))
48e27807
KL
743
744 // I didn't take it, pass it on to my children
745 super.onKeypress(keypress);
746 }
747
748 /**
749 * Handle posted command events.
750 *
751 * @param command command event
752 */
753 @Override
754 public void onCommand(final TCommandEvent command) {
755
756 // These commands will typically not be seen unless a subclass
757 // overrides onMenu() due to how TApplication dispatches
758 // accelerators.
759
92453213 760 if (!(this instanceof TDesktop)) {
48e27807 761
92453213 762 if (command.equals(cmWindowClose)) {
78a56d5d 763 if ((flags & NOCLOSEBOX) == 0) {
d36057df
KL
764 if ((flags & HIDEONCLOSE) == 0) {
765 // Close window
766 application.closeWindow(this);
767 } else {
768 // Hide window
769 application.hideWindow(this);
770 }
78a56d5d 771 }
92453213
KL
772 return;
773 }
48e27807 774
92453213
KL
775 if (command.equals(cmWindowNext)) {
776 application.switchWindow(true);
777 return;
778 }
48e27807 779
92453213
KL
780 if (command.equals(cmWindowPrevious)) {
781 application.switchWindow(false);
782 return;
783 }
48e27807 784
92453213
KL
785 if (command.equals(cmWindowMove)) {
786 inKeyboardResize = true;
787 return;
788 }
789
68c5cd6b 790 if (command.equals(cmWindowZoom) && ((flags & NOZOOMBOX) == 0)) {
92453213
KL
791 if (maximized) {
792 restore();
793 } else {
794 maximize();
795 }
48e27807 796 }
92453213
KL
797
798 } // if (!(this instanceof TDesktop))
48e27807
KL
799
800 // I didn't take it, pass it on to my children
801 super.onCommand(command);
802 }
803
804 /**
805 * Handle posted menu events.
806 *
807 * @param menu menu event
808 */
809 @Override
810 public void onMenu(final TMenuEvent menu) {
48e27807 811
92453213 812 if (!(this instanceof TDesktop)) {
48e27807 813
92453213 814 if (menu.getId() == TMenu.MID_WINDOW_CLOSE) {
78a56d5d 815 if ((flags & NOCLOSEBOX) == 0) {
d36057df
KL
816 if ((flags & HIDEONCLOSE) == 0) {
817 // Close window
818 application.closeWindow(this);
819 } else {
820 // Hide window
821 application.hideWindow(this);
822 }
78a56d5d 823 }
92453213
KL
824 return;
825 }
48e27807 826
92453213
KL
827 if (menu.getId() == TMenu.MID_WINDOW_NEXT) {
828 application.switchWindow(true);
829 return;
830 }
48e27807 831
92453213
KL
832 if (menu.getId() == TMenu.MID_WINDOW_PREVIOUS) {
833 application.switchWindow(false);
834 return;
48e27807 835 }
92453213
KL
836
837 if (menu.getId() == TMenu.MID_WINDOW_MOVE) {
838 inKeyboardResize = true;
839 return;
840 }
841
68c5cd6b
KL
842 if ((menu.getId() == TMenu.MID_WINDOW_ZOOM)
843 && ((flags & NOZOOMBOX) == 0)
844 ) {
92453213
KL
845 if (maximized) {
846 restore();
847 } else {
848 maximize();
849 }
850 return;
851 }
852
853 } // if (!(this instanceof TDesktop))
48e27807
KL
854
855 // I didn't take it, pass it on to my children
856 super.onMenu(menu);
857 }
858
5ffeabcc
KL
859 /**
860 * Method that subclasses can override to handle window/screen resize
861 * events.
862 *
863 * @param resize resize event
864 */
865 @Override
866 public void onResize(final TResizeEvent resize) {
867 if (resize.getType() == TResizeEvent.Type.WIDGET) {
868 if (getChildren().size() == 1) {
869 TWidget child = getChildren().get(0);
870 if ((child instanceof TSplitPane)
871 || (child instanceof TPanel)
872 ) {
2bc32111
KL
873 if (this instanceof TDesktop) {
874 child.onResize(new TResizeEvent(
875 TResizeEvent.Type.WIDGET,
876 resize.getWidth(), resize.getHeight()));
877 } else {
878 child.onResize(new TResizeEvent(
879 TResizeEvent.Type.WIDGET,
5ffeabcc 880 resize.getWidth() - 2, resize.getHeight() - 2));
2bc32111 881 }
5ffeabcc
KL
882 }
883 return;
884 }
885 }
886
887 // Pass on to TWidget.
888 super.onResize(resize);
889 }
890
d36057df
KL
891 // ------------------------------------------------------------------------
892 // TWidget ----------------------------------------------------------------
893 // ------------------------------------------------------------------------
894
895 /**
896 * Get this TWindow's parent TApplication.
897 *
898 * @return this TWindow's parent TApplication
899 */
900 @Override
901 public final TApplication getApplication() {
902 return application;
903 }
904
905 /**
906 * Get the Screen.
907 *
908 * @return the Screen
909 */
910 @Override
911 public final Screen getScreen() {
912 return application.getScreen();
913 }
914
915 /**
916 * Called by TApplication.drawChildren() to render on screen.
917 */
918 @Override
919 public void draw() {
920 // Draw the box and background first.
921 CellAttributes border = getBorder();
922 CellAttributes background = getBackground();
923 int borderType = getBorderType();
924
a69ed767
KL
925 drawBox(0, 0, getWidth(), getHeight(), border, background, borderType,
926 true);
d36057df
KL
927
928 // Draw the title
97bc3f29
KL
929 int titleLength = StringUtils.width(title);
930 int titleLeft = (getWidth() - titleLength - 2) / 2;
d36057df 931 putCharXY(titleLeft, 0, ' ', border);
97bc3f29
KL
932 putStringXY(titleLeft + 1, 0, title, border);
933 putCharXY(titleLeft + titleLength + 1, 0, ' ', border);
d36057df
KL
934
935 if (isActive()) {
936
937 // Draw the close button
938 if ((flags & NOCLOSEBOX) == 0) {
939 putCharXY(2, 0, '[', border);
940 putCharXY(4, 0, ']', border);
941 if (mouseOnClose() && mouse.isMouse1()) {
942 putCharXY(3, 0, GraphicsChars.CP437[0x0F],
943 getBorderControls());
944 } else {
945 putCharXY(3, 0, GraphicsChars.CP437[0xFE],
946 getBorderControls());
947 }
948 }
949
950 // Draw the maximize button
951 if (!isModal() && ((flags & NOZOOMBOX) == 0)) {
952
953 putCharXY(getWidth() - 5, 0, '[', border);
954 putCharXY(getWidth() - 3, 0, ']', border);
955 if (mouseOnMaximize() && mouse.isMouse1()) {
956 putCharXY(getWidth() - 4, 0, GraphicsChars.CP437[0x0F],
957 getBorderControls());
958 } else {
959 if (maximized) {
960 putCharXY(getWidth() - 4, 0, GraphicsChars.CP437[0x12],
961 getBorderControls());
962 } else {
963 putCharXY(getWidth() - 4, 0, GraphicsChars.UPARROW,
964 getBorderControls());
965 }
966 }
967
968 // Draw the resize corner
969 if ((flags & RESIZABLE) != 0) {
970 putCharXY(getWidth() - 2, getHeight() - 1,
971 GraphicsChars.SINGLE_BAR, getBorderControls());
972 putCharXY(getWidth() - 1, getHeight() - 1,
973 GraphicsChars.LRCORNER, getBorderControls());
974 }
975 }
976 }
977 }
978
979 // ------------------------------------------------------------------------
980 // TWindow ----------------------------------------------------------------
981 // ------------------------------------------------------------------------
982
983 /**
984 * Get window title.
985 *
986 * @return window title
987 */
988 public final String getTitle() {
989 return title;
990 }
991
992 /**
993 * Set window title.
994 *
995 * @param title new window title
996 */
997 public final void setTitle(final String title) {
998 this.title = title;
999 }
1000
1001 /**
1002 * Get Z order. Lower number means more in-front.
1003 *
1004 * @return Z value. Lower number means more in-front.
1005 */
1006 public final int getZ() {
1007 return z;
1008 }
1009
1010 /**
1011 * Set Z order. Lower number means more in-front.
1012 *
1013 * @param z the new Z value. Lower number means more in-front.
1014 */
1015 public final void setZ(final int z) {
1016 this.z = z;
1017 }
1018
1019 /**
1020 * Add a keypress to be overridden for this window.
1021 *
1022 * @param key the key to start taking control of
1023 */
1024 protected void addShortcutKeypress(final TKeypress key) {
1025 keyboardShortcuts.add(key);
1026 }
1027
1028 /**
1029 * Remove a keypress to be overridden for this window.
1030 *
1031 * @param key the key to stop taking control of
1032 */
1033 protected void removeShortcutKeypress(final TKeypress key) {
1034 keyboardShortcuts.remove(key);
1035 }
1036
1037 /**
1038 * Remove all keypresses to be overridden for this window.
1039 */
1040 protected void clearShortcutKeypresses() {
1041 keyboardShortcuts.clear();
1042 }
1043
1044 /**
1045 * Determine if a keypress is overridden for this window.
1046 *
1047 * @param key the key to check
1048 * @return true if this window wants to process this key on its own
1049 */
1050 public boolean isShortcutKeypress(final TKeypress key) {
1051 return keyboardShortcuts.contains(key);
1052 }
1053
1054 /**
1055 * Get the window's status bar, or null if it does not have one.
1056 *
1057 * @return the status bar, or null
1058 */
1059 public TStatusBar getStatusBar() {
1060 return statusBar;
1061 }
1062
1063 /**
1064 * Set the window's status bar to a new one.
1065 *
1066 * @param text the status bar text
1067 * @return the status bar
1068 */
1069 public TStatusBar newStatusBar(final String text) {
1070 statusBar = new TStatusBar(this, text);
1071 return statusBar;
1072 }
1073
1074 /**
1075 * Set the maximum width for this window.
1076 *
1077 * @param maximumWindowWidth new maximum width
1078 */
1079 public final void setMaximumWindowWidth(final int maximumWindowWidth) {
1080 if ((maximumWindowWidth != -1)
1081 && (maximumWindowWidth < minimumWindowWidth + 1)
1082 ) {
1083 throw new IllegalArgumentException("Maximum window width cannot " +
1084 "be smaller than minimum window width + 1");
1085 }
1086 this.maximumWindowWidth = maximumWindowWidth;
1087 }
1088
1089 /**
1090 * Set the minimum width for this window.
1091 *
1092 * @param minimumWindowWidth new minimum width
1093 */
1094 public final void setMinimumWindowWidth(final int minimumWindowWidth) {
1095 if ((maximumWindowWidth != -1)
1096 && (minimumWindowWidth > maximumWindowWidth - 1)
1097 ) {
1098 throw new IllegalArgumentException("Minimum window width cannot " +
1099 "be larger than maximum window width - 1");
1100 }
1101 this.minimumWindowWidth = minimumWindowWidth;
1102 }
1103
1104 /**
1105 * Set the maximum height for this window.
1106 *
1107 * @param maximumWindowHeight new maximum height
1108 */
1109 public final void setMaximumWindowHeight(final int maximumWindowHeight) {
1110 if ((maximumWindowHeight != -1)
1111 && (maximumWindowHeight < minimumWindowHeight + 1)
1112 ) {
1113 throw new IllegalArgumentException("Maximum window height cannot " +
1114 "be smaller than minimum window height + 1");
1115 }
1116 this.maximumWindowHeight = maximumWindowHeight;
1117 }
1118
1119 /**
1120 * Set the minimum height for this window.
1121 *
1122 * @param minimumWindowHeight new minimum height
1123 */
1124 public final void setMinimumWindowHeight(final int minimumWindowHeight) {
1125 if ((maximumWindowHeight != -1)
1126 && (minimumWindowHeight > maximumWindowHeight - 1)
1127 ) {
1128 throw new IllegalArgumentException("Minimum window height cannot " +
1129 "be larger than maximum window height - 1");
1130 }
1131 this.minimumWindowHeight = minimumWindowHeight;
1132 }
1133
1134 /**
1135 * Recenter the window on-screen.
1136 */
1137 public final void center() {
1138 if ((flags & CENTERED) != 0) {
1139 if (getWidth() < getScreen().getWidth()) {
1140 setX((getScreen().getWidth() - getWidth()) / 2);
1141 } else {
1142 setX(0);
1143 }
1144 setY(((application.getDesktopBottom()
1145 - application.getDesktopTop()) - getHeight()) / 2);
1146 if (getY() < 0) {
1147 setY(0);
1148 }
1149 setY(getY() + application.getDesktopTop());
1150 }
1151 }
1152
1153 /**
1154 * Maximize window.
1155 */
1156 public void maximize() {
1157 if (maximized) {
1158 return;
1159 }
1160
1161 restoreWindowWidth = getWidth();
1162 restoreWindowHeight = getHeight();
1163 restoreWindowX = getX();
1164 restoreWindowY = getY();
1165 setWidth(getScreen().getWidth());
2bb26984 1166 setHeight(application.getDesktopBottom() - application.getDesktopTop());
d36057df 1167 setX(0);
2bb26984 1168 setY(application.getDesktopTop());
d36057df
KL
1169 maximized = true;
1170
1171 onResize(new TResizeEvent(TResizeEvent.Type.WIDGET, getWidth(),
1172 getHeight()));
1173 }
1174
1175 /**
1176 * Restore (unmaximize) window.
1177 */
1178 public void restore() {
1179 if (!maximized) {
1180 return;
1181 }
1182
1183 setWidth(restoreWindowWidth);
1184 setHeight(restoreWindowHeight);
1185 setX(restoreWindowX);
1186 setY(restoreWindowY);
1187 maximized = false;
1188
1189 onResize(new TResizeEvent(TResizeEvent.Type.WIDGET, getWidth(),
1190 getHeight()));
1191 }
1192
1193 /**
1194 * Returns true if this window is hidden.
1195 *
1196 * @return true if this window is hidden, false if the window is shown
1197 */
1198 public final boolean isHidden() {
1199 return hidden;
1200 }
1201
1202 /**
1203 * Returns true if this window is shown.
1204 *
1205 * @return true if this window is shown, false if the window is hidden
1206 */
1207 public final boolean isShown() {
1208 return !hidden;
1209 }
1210
1211 /**
1212 * Hide window. A hidden window will still have its onIdle() called, and
1213 * will also have onClose() called at application exit. Hidden windows
1214 * will not receive any other events.
1215 */
1216 public void hide() {
1217 application.hideWindow(this);
1218 }
1219
1220 /**
1221 * Show window.
1222 */
1223 public void show() {
1224 application.showWindow(this);
1225 }
1226
1227 /**
1228 * Activate window (bring to top and receive events).
1229 */
5218e73c 1230 @Override
d36057df
KL
1231 public void activate() {
1232 application.activateWindow(this);
1233 }
1234
1235 /**
1236 * Close window. Note that windows without a close box can still be
1237 * closed by calling the close() method.
1238 */
c88c4ced 1239 @Override
d36057df
KL
1240 public void close() {
1241 application.closeWindow(this);
1242 }
1243
1244 /**
1245 * See if this window is undergoing any movement/resize/etc.
1246 *
1247 * @return true if the window is moving
1248 */
1249 public boolean inMovements() {
1250 if (inWindowResize || inWindowMove || inKeyboardResize) {
1251 return true;
1252 }
1253 return false;
1254 }
1255
1256 /**
1257 * Stop any pending movement/resize/etc.
1258 */
1259 public void stopMovements() {
1260 inWindowResize = false;
1261 inWindowMove = false;
1262 inKeyboardResize = false;
1263 }
1264
1265 /**
1266 * Returns true if this window is modal.
1267 *
1268 * @return true if this window is modal
1269 */
1270 public final boolean isModal() {
1271 if ((flags & MODAL) == 0) {
1272 return false;
1273 }
1274 return true;
1275 }
1276
1277 /**
1278 * Returns true if this window has a close box.
1279 *
1280 * @return true if this window has a close box
1281 */
1282 public final boolean hasCloseBox() {
1283 if ((flags & NOCLOSEBOX) != 0) {
1284 return true;
1285 }
1286 return false;
1287 }
1288
1289 /**
1290 * Returns true if this window has a maximize/zoom box.
1291 *
1292 * @return true if this window has a maximize/zoom box
1293 */
1294 public final boolean hasZoomBox() {
1295 if ((flags & NOZOOMBOX) != 0) {
1296 return true;
9696a8f6
KL
1297 }
1298 return false;
1299 }
1300
1301 /**
1302 * Returns true if this window does not want menus to work while it is
1303 * visible.
1304 *
1305 * @return true if this window does not want menus to work while it is
1306 * visible
1307 */
1308 public final boolean hasOverriddenMenu() {
1309 if ((flags & OVERRIDEMENU) != 0) {
1310 return true;
d36057df
KL
1311 }
1312 return false;
1313 }
1314
1315 /**
1316 * Retrieve the background color.
1317 *
1318 * @return the background color
1319 */
1320 public CellAttributes getBackground() {
1321 if (!isModal()
1322 && (inWindowMove || inWindowResize || inKeyboardResize)
1323 ) {
1324 assert (isActive());
1325 return getTheme().getColor("twindow.background.windowmove");
1326 } else if (isModal() && inWindowMove) {
1327 assert (isActive());
1328 return getTheme().getColor("twindow.background.modal");
1329 } else if (isModal()) {
1330 if (isActive()) {
1331 return getTheme().getColor("twindow.background.modal");
1332 }
1333 return getTheme().getColor("twindow.background.modal.inactive");
1334 } else if (isActive()) {
1335 assert (!isModal());
1336 return getTheme().getColor("twindow.background");
1337 } else {
1338 assert (!isModal());
1339 return getTheme().getColor("twindow.background.inactive");
1340 }
1341 }
1342
1343 /**
1344 * Retrieve the border color.
1345 *
1346 * @return the border color
1347 */
1348 public CellAttributes getBorder() {
1349 if (!isModal()
1350 && (inWindowMove || inWindowResize || inKeyboardResize)
1351 ) {
a69ed767
KL
1352 if (!isActive()) {
1353 // The user's terminal never passed a mouse up event, and now
1354 // another window is active but we never finished a drag.
1355 inWindowMove = false;
1356 inWindowResize = false;
1357 inKeyboardResize = false;
1358 return getTheme().getColor("twindow.border.inactive");
1359 }
1360
d36057df
KL
1361 return getTheme().getColor("twindow.border.windowmove");
1362 } else if (isModal() && inWindowMove) {
1363 assert (isActive());
1364 return getTheme().getColor("twindow.border.modal.windowmove");
1365 } else if (isModal()) {
1366 if (isActive()) {
1367 return getTheme().getColor("twindow.border.modal");
1368 } else {
1369 return getTheme().getColor("twindow.border.modal.inactive");
1370 }
1371 } else if (isActive()) {
1372 assert (!isModal());
1373 return getTheme().getColor("twindow.border");
1374 } else {
1375 assert (!isModal());
1376 return getTheme().getColor("twindow.border.inactive");
1377 }
1378 }
1379
1380 /**
1381 * Retrieve the color used by the window movement/sizing controls.
1382 *
1383 * @return the color used by the zoom box, resize bar, and close box
1384 */
1385 public CellAttributes getBorderControls() {
1386 if (isModal()) {
1387 return getTheme().getColor("twindow.border.modal.windowmove");
1388 }
1389 return getTheme().getColor("twindow.border.windowmove");
1390 }
1391
1392 /**
1393 * Retrieve the border line type.
1394 *
1395 * @return the border line type
1396 */
1397 private int getBorderType() {
1398 if (!isModal()
1399 && (inWindowMove || inWindowResize || inKeyboardResize)
1400 ) {
1401 assert (isActive());
1402 return 1;
1403 } else if (isModal() && inWindowMove) {
1404 assert (isActive());
1405 return 1;
1406 } else if (isModal()) {
1407 if (isActive()) {
1408 return 2;
1409 } else {
1410 return 1;
1411 }
1412 } else if (isActive()) {
1413 return 2;
1414 } else {
1415 return 1;
1416 }
1417 }
1418
978a5d8f
KL
1419 /**
1420 * Returns true if this window does not want the application-wide mouse
1421 * cursor drawn over it.
1422 *
1423 * @return true if this window does not want the application-wide mouse
1424 * cursor drawn over it
1425 */
80b1b7b5 1426 public boolean hasHiddenMouse() {
978a5d8f
KL
1427 return hideMouse;
1428 }
1429
1430 /**
1431 * Set request to prevent the application-wide mouse cursor from being
1432 * drawn over this window.
1433 *
1434 * @param hideMouse if true, this window does not want the
1435 * application-wide mouse cursor drawn over it
1436 */
1437 public final void setHiddenMouse(final boolean hideMouse) {
1438 this.hideMouse = hideMouse;
1439 }
1440
4941d2d6
KL
1441 /**
1442 * Get this window's help topic to load.
1443 *
1444 * @return the topic name
1445 */
1446 public String getHelpTopic() {
1447 return helpTopic;
1448 }
1449
90d87fca
KL
1450 /**
1451 * Generate a human-readable string for this window.
1452 *
1453 * @return a human-readable string
1454 */
1455 @Override
1456 public String toString() {
4941d2d6
KL
1457 return String.format("%s(%8x) \'%s\' Z %d position (%d, %d) " +
1458 "geometry %dx%d hidden %s modal %s",
1459 getClass().getName(), hashCode(), title, getZ(),
90d87fca
KL
1460 getX(), getY(), getWidth(), getHeight(), hidden, isModal());
1461 }
1462
48e27807 1463}