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