lib: sort fixes
[nikiroo-utils.git] / src / jexer / TWindow.java
1 /*
2 * Jexer - Java Text User Interface
3 *
4 * The MIT License (MIT)
5 *
6 * Copyright (C) 2019 Kevin Lamonte
7 *
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:
14 *
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
17 *
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.
25 *
26 * @author Kevin Lamonte [kevin.lamonte@gmail.com]
27 * @version 1
28 */
29 package jexer;
30
31 import java.util.HashSet;
32 import java.util.Set;
33
34 import jexer.backend.Screen;
35 import jexer.bits.CellAttributes;
36 import jexer.bits.GraphicsChars;
37 import jexer.bits.StringUtils;
38 import jexer.event.TCommandEvent;
39 import jexer.event.TKeypressEvent;
40 import jexer.event.TMenuEvent;
41 import jexer.event.TMouseEvent;
42 import jexer.event.TResizeEvent;
43 import jexer.menu.TMenu;
44 import static jexer.TCommand.*;
45 import static jexer.TKeypress.*;
46
47 /**
48 * TWindow is the top-level container and drawing surface for other widgets.
49 */
50 public class TWindow extends TWidget {
51
52 // ------------------------------------------------------------------------
53 // Constants --------------------------------------------------------------
54 // ------------------------------------------------------------------------
55
56 /**
57 * Window is resizable (default yes).
58 */
59 public static final int RESIZABLE = 0x01;
60
61 /**
62 * Window is modal (default no).
63 */
64 public static final int MODAL = 0x02;
65
66 /**
67 * Window is centered (default no).
68 */
69 public static final int CENTERED = 0x04;
70
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
77 /**
78 * Window has no maximize box (default no).
79 */
80 public static final int NOZOOMBOX = 0x10;
81
82 /**
83 * Window is placed at absolute position (no smart placement) (default
84 * no).
85 */
86 public static final int ABSOLUTEXY = 0x20;
87
88 /**
89 * Hitting the closebox with the mouse calls TApplication.hideWindow()
90 * rather than TApplication.closeWindow() (default no).
91 */
92 public static final int HIDEONCLOSE = 0x40;
93
94 /**
95 * Menus cannot be used when this window is active (default no).
96 */
97 public static final int OVERRIDEMENU = 0x80;
98
99 // ------------------------------------------------------------------------
100 // Variables --------------------------------------------------------------
101 // ------------------------------------------------------------------------
102
103 /**
104 * Window flags. Note package private access.
105 */
106 int flags = RESIZABLE;
107
108 /**
109 * Window title.
110 */
111 private String title = "";
112
113 /**
114 * Window's parent TApplication.
115 */
116 private TApplication application;
117
118 /**
119 * Z order. Lower number means more in-front.
120 */
121 private int z = 0;
122
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 */
128 private Set<TKeypress> keyboardShortcuts = new HashSet<TKeypress>();
129
130 /**
131 * If true, then the user clicked on the title bar and is moving the
132 * window.
133 */
134 protected boolean inWindowMove = false;
135
136 /**
137 * If true, then the user clicked on the bottom right corner and is
138 * resizing the window.
139 */
140 protected boolean inWindowResize = false;
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 */
146 protected boolean inKeyboardResize = false;
147
148 /**
149 * If true, this window is maximized.
150 */
151 private boolean maximized = false;
152
153 /**
154 * Remember mouse state.
155 */
156 protected TMouseEvent mouse;
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
178 /**
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.
183 */
184 boolean hidden = false;
185
186 /**
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.
190 */
191 protected TStatusBar statusBar = null;
192
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
202 /**
203 * The help topic for this window.
204 */
205 protected String helpTopic = "Help";
206
207 // ------------------------------------------------------------------------
208 // Constructors -----------------------------------------------------------
209 // ------------------------------------------------------------------------
210
211 /**
212 * Public constructor. Window will be located at (0, 0).
213 *
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
218 */
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);
223 }
224
225 /**
226 * Public constructor. Window will be located at (0, 0).
227 *
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
233 */
234 public TWindow(final TApplication application, final String title,
235 final int width, final int height, final int flags) {
236
237 this(application, title, 0, 0, width, height, flags);
238 }
239
240 /**
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
249 */
250 public TWindow(final TApplication application, final String title,
251 final int x, final int y, final int width, final int height) {
252
253 this(application, title, x, y, width, height, RESIZABLE);
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
271 super();
272
273 // I am my own window and parent
274 setupForTWindow(this, x, y + application.getDesktopTop(),
275 width, height);
276
277 // Save fields
278 this.title = title;
279 this.application = application;
280 this.flags = flags;
281
282 // Minimum width/height are 10 and 2
283 assert (width >= 10);
284 assert (getHeight() >= 2);
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
295 application.addWindowToApplication(this);
296 }
297
298 // ------------------------------------------------------------------------
299 // Event handlers ---------------------------------------------------------
300 // ------------------------------------------------------------------------
301
302 /**
303 * Returns true if the mouse is currently on the close button.
304 *
305 * @return true if mouse is currently on the close button
306 */
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 ) {
315 return true;
316 }
317 return false;
318 }
319
320 /**
321 * Returns true if the mouse is currently on the maximize/restore button.
322 *
323 * @return true if the mouse is currently on the maximize/restore button
324 */
325 protected boolean mouseOnMaximize() {
326 if ((flags & NOZOOMBOX) != 0) {
327 return false;
328 }
329 if ((mouse != null)
330 && !isModal()
331 && (mouse.getAbsoluteY() == getY())
332 && (mouse.getAbsoluteX() == getX() + getWidth() - 4)
333 ) {
334 return true;
335 }
336 return false;
337 }
338
339 /**
340 * Returns true if the mouse is currently on the resizable lower right
341 * corner.
342 *
343 * @return true if the mouse is currently on the resizable lower right
344 * corner
345 */
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 ) {
354 return true;
355 }
356 return false;
357 }
358
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
369 /**
370 * Subclasses should override this method to cleanup resources. This is
371 * called by application.closeWindow().
372 */
373 protected void onClose() {
374 // Default: perform widget-specific cleanup.
375 for (TWidget w: getChildren()) {
376 w.close();
377 }
378 }
379
380 /**
381 * Called by application.switchWindow() when this window gets the
382 * focus, and also by application.addWindow().
383 */
384 protected void onFocus() {
385 // Default: do nothing
386 }
387
388 /**
389 * Called by application.switchWindow() when another window gets the
390 * focus.
391 */
392 protected void onUnfocus() {
393 // Default: do nothing
394 }
395
396 /**
397 * Called by application.hideWindow().
398 */
399 protected void onHide() {
400 // Default: do nothing
401 }
402
403 /**
404 * Called by application.showWindow().
405 */
406 protected void onShow() {
407 // Default: do nothing
408 }
409
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;
418
419 inKeyboardResize = false;
420 inWindowMove = false;
421 inWindowResize = false;
422
423 if ((mouse.getAbsoluteY() == getY())
424 && mouse.isMouse1()
425 && (getX() <= mouse.getAbsoluteX())
426 && (mouse.getAbsoluteX() < getX() + getWidth())
427 && !mouseOnClose()
428 && !mouseOnMaximize()
429 ) {
430 // Begin moving window
431 inWindowMove = true;
432 moveWindowMouseX = mouse.getAbsoluteX();
433 moveWindowMouseY = mouse.getAbsoluteY();
434 oldWindowX = getX();
435 oldWindowY = getY();
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();
446 resizeWindowWidth = getWidth();
447 resizeWindowHeight = getHeight();
448 if (maximized) {
449 maximized = false;
450 }
451 return;
452 }
453
454 // Give the shortcut bar a shot at this.
455 if (statusBar != null) {
456 if (statusBar.statusBarMouseDown(mouse)) {
457 return;
458 }
459 }
460
461 // I didn't take it, pass it on to my children
462 super.onMouseDown(mouse);
463 }
464
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;
473
474 if ((inWindowMove) && (mouse.isMouse1())) {
475 // Stop moving window
476 inWindowMove = false;
477 return;
478 }
479
480 if ((inWindowResize) && (mouse.isMouse1())) {
481 // Stop resizing window
482 inWindowResize = false;
483 return;
484 }
485
486 if (mouse.isMouse1() && mouseOnClose()) {
487 if ((flags & HIDEONCLOSE) == 0) {
488 // Close window
489 application.closeWindow(this);
490 } else {
491 // Hide window
492 application.hideWindow(this);
493 }
494 return;
495 }
496
497 if ((mouse.getAbsoluteY() == getY())
498 && mouse.isMouse1()
499 && mouseOnMaximize()) {
500 if (maximized) {
501 // Restore
502 restore();
503 } else {
504 // Maximize
505 maximize();
506 }
507 // Pass a resize event to my children
508 onResize(new TResizeEvent(TResizeEvent.Type.WIDGET,
509 getWidth(), getHeight()));
510 return;
511 }
512
513 // Give the shortcut bar a shot at this.
514 if (statusBar != null) {
515 if (statusBar.statusBarMouseUp(mouse)) {
516 return;
517 }
518 }
519
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;
532
533 if (inWindowMove) {
534 // Move window over
535 setX(oldWindowX + (mouse.getAbsoluteX() - moveWindowMouseX));
536 setY(oldWindowY + (mouse.getAbsoluteY() - moveWindowMouseY));
537 // Don't cover up the menu bar
538 if (getY() < application.getDesktopTop()) {
539 setY(application.getDesktopTop());
540 }
541 // Don't go below the status bar
542 if (getY() >= application.getDesktopBottom()) {
543 setY(application.getDesktopBottom() - 1);
544 }
545 return;
546 }
547
548 if (inWindowResize) {
549 // Move window over
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());
556 }
557 if (getY() + getHeight() > application.getDesktopBottom()) {
558 setY(application.getDesktopBottom() - getHeight() + 1);
559 }
560 // Don't cover up the menu bar
561 if (getY() < application.getDesktopTop()) {
562 setY(application.getDesktopTop());
563 }
564
565 // Keep within min/max bounds
566 if (getWidth() < minimumWindowWidth) {
567 setWidth(minimumWindowWidth);
568 }
569 if (getHeight() < minimumWindowHeight) {
570 setHeight(minimumWindowHeight);
571 }
572 if ((maximumWindowWidth > 0)
573 && (getWidth() > maximumWindowWidth)
574 ) {
575 setWidth(maximumWindowWidth);
576 }
577 if ((maximumWindowHeight > 0)
578 && (getHeight() > maximumWindowHeight)
579 ) {
580 setHeight(maximumWindowHeight);
581 }
582 if (getHeight() + getY() >= getApplication().getDesktopBottom()) {
583 setHeight(getApplication().getDesktopBottom() - getY());
584 }
585
586 // Pass a resize event to my children
587 onResize(new TResizeEvent(TResizeEvent.Type.WIDGET,
588 getWidth(), getHeight()));
589 return;
590 }
591
592 // Give the shortcut bar a shot at this.
593 if (statusBar != null) {
594 statusBar.statusBarMouseMotion(mouse);
595 }
596
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
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
618 if (inKeyboardResize) {
619
620 // ESC or ENTER - Exit size/move
621 if (keypress.equals(kbEsc) || keypress.equals(kbEnter)) {
622 inKeyboardResize = false;
623 }
624
625 if (keypress.equals(kbLeft)) {
626 if (getX() > 0) {
627 setX(getX() - 1);
628 }
629 }
630 if (keypress.equals(kbRight)) {
631 if (getX() < getScreen().getWidth() - 1) {
632 setX(getX() + 1);
633 }
634 }
635 if (keypress.equals(kbDown)) {
636 if (getY() < application.getDesktopBottom() - 1) {
637 setY(getY() + 1);
638 }
639 }
640 if (keypress.equals(kbUp)) {
641 if (getY() > 1) {
642 setY(getY() - 1);
643 }
644 }
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 }
657 }
658 if (keypress.equals(kbShiftRight)) {
659 if ((getWidth() < maximumWindowWidth)
660 || (maximumWindowWidth <= 0)
661 ) {
662 setWidth(getWidth() + 1);
663 }
664 }
665 if (keypress.equals(kbShiftUp)) {
666 if ((getHeight() > minimumWindowHeight)
667 || (minimumWindowHeight <= 0)
668 ) {
669 setHeight(getHeight() - 1);
670 }
671 }
672 if (keypress.equals(kbShiftDown)) {
673 if ((getHeight() < maximumWindowHeight)
674 || (maximumWindowHeight <= 0)
675 ) {
676 setHeight(getHeight() + 1);
677 }
678 }
679
680 // Pass a resize event to my children
681 onResize(new TResizeEvent(TResizeEvent.Type.WIDGET,
682 getWidth(), getHeight()));
683
684 } // if ((flags & RESIZABLE) != 0)
685
686 return;
687 }
688
689 // Give the shortcut bar a shot at this.
690 if (statusBar != null) {
691 if (statusBar.statusBarKeypress(keypress)) {
692 return;
693 }
694 }
695
696 // These keystrokes will typically not be seen unless a subclass
697 // overrides onMenu() due to how TApplication dispatches
698 // accelerators.
699
700 if (!(this instanceof TDesktop)) {
701
702 // Ctrl-W - close window
703 if (keypress.equals(kbCtrlW)) {
704 if ((flags & NOCLOSEBOX) == 0) {
705 if ((flags & HIDEONCLOSE) == 0) {
706 // Close window
707 application.closeWindow(this);
708 } else {
709 // Hide window
710 application.hideWindow(this);
711 }
712 }
713 return;
714 }
715
716 // F6 - behave like Alt-TAB
717 if (keypress.equals(kbF6)) {
718 application.switchWindow(true);
719 return;
720 }
721
722 // Shift-F6 - behave like Shift-Alt-TAB
723 if (keypress.equals(kbShiftF6)) {
724 application.switchWindow(false);
725 return;
726 }
727
728 // F5 - zoom
729 if (keypress.equals(kbF5) && ((flags & NOZOOMBOX) == 0)) {
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))
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
760 if (!(this instanceof TDesktop)) {
761
762 if (command.equals(cmWindowClose)) {
763 if ((flags & NOCLOSEBOX) == 0) {
764 if ((flags & HIDEONCLOSE) == 0) {
765 // Close window
766 application.closeWindow(this);
767 } else {
768 // Hide window
769 application.hideWindow(this);
770 }
771 }
772 return;
773 }
774
775 if (command.equals(cmWindowNext)) {
776 application.switchWindow(true);
777 return;
778 }
779
780 if (command.equals(cmWindowPrevious)) {
781 application.switchWindow(false);
782 return;
783 }
784
785 if (command.equals(cmWindowMove)) {
786 inKeyboardResize = true;
787 return;
788 }
789
790 if (command.equals(cmWindowZoom) && ((flags & NOZOOMBOX) == 0)) {
791 if (maximized) {
792 restore();
793 } else {
794 maximize();
795 }
796 }
797
798 } // if (!(this instanceof TDesktop))
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) {
811
812 if (!(this instanceof TDesktop)) {
813
814 if (menu.getId() == TMenu.MID_WINDOW_CLOSE) {
815 if ((flags & NOCLOSEBOX) == 0) {
816 if ((flags & HIDEONCLOSE) == 0) {
817 // Close window
818 application.closeWindow(this);
819 } else {
820 // Hide window
821 application.hideWindow(this);
822 }
823 }
824 return;
825 }
826
827 if (menu.getId() == TMenu.MID_WINDOW_NEXT) {
828 application.switchWindow(true);
829 return;
830 }
831
832 if (menu.getId() == TMenu.MID_WINDOW_PREVIOUS) {
833 application.switchWindow(false);
834 return;
835 }
836
837 if (menu.getId() == TMenu.MID_WINDOW_MOVE) {
838 inKeyboardResize = true;
839 return;
840 }
841
842 if ((menu.getId() == TMenu.MID_WINDOW_ZOOM)
843 && ((flags & NOZOOMBOX) == 0)
844 ) {
845 if (maximized) {
846 restore();
847 } else {
848 maximize();
849 }
850 return;
851 }
852
853 } // if (!(this instanceof TDesktop))
854
855 // I didn't take it, pass it on to my children
856 super.onMenu(menu);
857 }
858
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 ) {
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,
880 resize.getWidth() - 2, resize.getHeight() - 2));
881 }
882 }
883 return;
884 }
885 }
886
887 // Pass on to TWidget.
888 super.onResize(resize);
889 }
890
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
925 drawBox(0, 0, getWidth(), getHeight(), border, background, borderType,
926 true);
927
928 // Draw the title
929 int titleLength = StringUtils.width(title);
930 int titleLeft = (getWidth() - titleLength - 2) / 2;
931 putCharXY(titleLeft, 0, ' ', border);
932 putStringXY(titleLeft + 1, 0, title, border);
933 putCharXY(titleLeft + titleLength + 1, 0, ' ', border);
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());
1166 setHeight(application.getDesktopBottom() - application.getDesktopTop());
1167 setX(0);
1168 setY(application.getDesktopTop());
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 */
1230 @Override
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 */
1239 @Override
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;
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;
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 ) {
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
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
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 */
1426 public boolean hasHiddenMouse() {
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
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
1450 /**
1451 * Generate a human-readable string for this window.
1452 *
1453 * @return a human-readable string
1454 */
1455 @Override
1456 public String toString() {
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(),
1460 getX(), getY(), getWidth(), getHeight(), hidden, isModal());
1461 }
1462
1463 }