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