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