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