2 * Jexer - Java Text User Interface
4 * License: LGPLv3 or later
6 * This module is licensed under the GNU Lesser General Public License
7 * Version 3. Please see the file "COPYING" in this directory for more
8 * information about the GNU Lesser General Public License Version 3.
10 * Copyright (C) 2015 Kevin Lamonte
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU Lesser General Public License
14 * as published by the Free Software Foundation; either version 3 of
15 * the License, or (at your option) any later version.
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
22 * You should have received a copy of the GNU Lesser General Public
23 * License along with this program; if not, see
24 * http://www.gnu.org/licenses/, or write to the Free Software
25 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
28 * @author Kevin Lamonte [kevin.lamonte@gmail.com]
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 static jexer
.TCommand
.*;
43 import static jexer
.TKeypress
.*;
46 * TWindow is the top-level container and drawing surface for other widgets.
48 public class TWindow
extends TWidget
implements Comparable
<TWindow
> {
51 * Window's parent TApplication.
53 protected TApplication application
;
56 * Get this TWindow's parent TApplication.
58 * @return this TWindow's parent TApplication
60 public final TApplication
getApplication() {
69 public final Screen
getScreen() {
70 return application
.getScreen();
76 protected String title
= "";
79 * Window is resizable (default yes).
81 public static final int RESIZABLE
= 0x01;
84 * Window is modal (default no).
86 public static final int MODAL
= 0x02;
89 * Window is centered (default no).
91 public static final int CENTERED
= 0x04;
96 private int flags
= RESIZABLE
;
99 * Z order. Lower number means more in-front.
104 * Get Z order. Lower number means more in-front.
106 * @return Z value. Lower number means more in-front.
108 public final int getZ() {
113 * Set Z order. Lower number means more in-front.
115 * @param z the new Z value. Lower number means more in-front.
117 public final void setZ(final int z
) {
122 * If true, then the user clicked on the title bar and is moving the
125 private boolean inWindowMove
= false;
128 * If true, then the user clicked on the bottom right corner and is
129 * resizing the window.
131 private boolean inWindowResize
= false;
134 * If true, then the user selected "Size/Move" (or hit Ctrl-F5) and is
135 * resizing/moving the window via the keyboard.
137 private boolean inKeyboardResize
= false;
140 * If true, this window is maximized.
142 private boolean maximized
= false;
145 * Remember mouse state.
147 protected TMouseEvent mouse
;
149 // For moving the window. resizing also uses moveWindowMouseX/Y
150 private int moveWindowMouseX
;
151 private int moveWindowMouseY
;
152 private int oldWindowX
;
153 private int oldWindowY
;
156 private int resizeWindowWidth
;
157 private int resizeWindowHeight
;
158 private int minimumWindowWidth
= 10;
159 private int minimumWindowHeight
= 2;
160 private int maximumWindowWidth
= -1;
161 private int maximumWindowHeight
= -1;
163 // For maximize/restore
164 private int restoreWindowWidth
;
165 private int restoreWindowHeight
;
166 private int restoreWindowX
;
167 private int restoreWindowY
;
170 * Public constructor. Window will be located at (0, 0).
172 * @param application TApplication that manages this window
173 * @param title window title, will be centered along the top border
174 * @param width width of window
175 * @param height height of window
177 public TWindow(final TApplication application
, final String title
,
178 final int width
, final int height
) {
180 this(application
, title
, 0, 0, width
, height
, RESIZABLE
);
184 * Public constructor. Window will be located at (0, 0).
186 * @param application TApplication that manages this window
187 * @param title window title, will be centered along the top border
188 * @param width width of window
189 * @param height height of window
190 * @param flags bitmask of RESIZABLE, CENTERED, or MODAL
192 public TWindow(final TApplication application
, final String title
,
193 final int width
, final int height
, final int flags
) {
195 this(application
, title
, 0, 0, width
, height
, flags
);
199 * Public constructor.
201 * @param application TApplication that manages this window
202 * @param title window title, will be centered along the top border
203 * @param x column relative to parent
204 * @param y row relative to parent
205 * @param width width of window
206 * @param height height of window
208 public TWindow(final TApplication application
, final String title
,
209 final int x
, final int y
, final int width
, final int height
) {
211 this(application
, title
, x
, y
, width
, height
, RESIZABLE
);
215 * Public constructor.
217 * @param application TApplication that manages this window
218 * @param title window title, will be centered along the top border
219 * @param x column relative to parent
220 * @param y row relative to parent
221 * @param width width of window
222 * @param height height of window
223 * @param flags mask of RESIZABLE, CENTERED, or MODAL
225 public TWindow(final TApplication application
, final String title
,
226 final int x
, final int y
, final int width
, final int height
,
229 // I am my own window and parent
235 this.application
= application
;
237 this.y
= y
+ application
.getDesktopTop();
239 this.height
= height
;
242 // Minimum width/height are 10 and 2
243 assert (width
>= 10);
244 assert (height
>= 2);
246 // MODAL implies CENTERED
248 this.flags
|= CENTERED
;
251 // Center window if specified
254 // Add me to the application
255 application
.addWindow(this);
259 * Recenter the window on-screen.
261 public final void center() {
262 if ((flags
& CENTERED
) != 0) {
263 if (width
< getScreen().getWidth()) {
264 x
= (getScreen().getWidth() - width
) / 2;
268 y
= (application
.getDesktopBottom() - application
.getDesktopTop());
274 y
+= application
.getDesktopTop();
279 * Returns true if this window is modal.
281 * @return true if this window is modal
283 public final boolean isModal() {
284 if ((flags
& MODAL
) == 0) {
291 * Comparison operator sorts on z.
293 * @param that another TWindow instance
294 * @return difference between this.z and that.z
297 public final int compareTo(final TWindow that
) {
298 return (this.z
- that
.z
);
302 * Returns true if the mouse is currently on the close button.
304 * @return true if mouse is currently on the close button
306 private boolean mouseOnClose() {
308 && (mouse
.getAbsoluteY() == y
)
309 && (mouse
.getAbsoluteX() == x
+ 3)
317 * Returns true if the mouse is currently on the maximize/restore button.
319 * @return true if the mouse is currently on the maximize/restore button
321 private boolean mouseOnMaximize() {
324 && (mouse
.getAbsoluteY() == y
)
325 && (mouse
.getAbsoluteX() == x
+ width
- 4)
333 * Returns true if the mouse is currently on the resizable lower right
336 * @return true if the mouse is currently on the resizable lower right
339 private boolean mouseOnResize() {
340 if (((flags
& RESIZABLE
) != 0)
343 && (mouse
.getAbsoluteY() == y
+ height
- 1)
344 && ((mouse
.getAbsoluteX() == x
+ width
- 1)
345 || (mouse
.getAbsoluteX() == x
+ width
- 2))
353 * Retrieve the background color.
355 * @return the background color
357 protected final CellAttributes
getBackground() {
359 && (inWindowMove
|| inWindowResize
|| inKeyboardResize
)
362 return application
.getTheme().getColor("twindow.background.windowmove");
363 } else if (isModal() && inWindowMove
) {
365 return application
.getTheme().getColor("twindow.background.modal");
366 } else if (isModal()) {
368 return application
.getTheme().getColor("twindow.background.modal");
370 return application
.getTheme().getColor("twindow.background.modal.inactive");
373 return application
.getTheme().getColor("twindow.background");
376 return application
.getTheme().getColor("twindow.background.inactive");
381 * Retrieve the border color.
383 * @return the border color
385 protected final CellAttributes
getBorder() {
387 && (inWindowMove
|| inWindowResize
|| inKeyboardResize
)
390 return application
.getTheme().getColor("twindow.border.windowmove");
391 } else if (isModal() && inWindowMove
) {
393 return application
.getTheme().getColor("twindow.border.modal.windowmove");
394 } else if (isModal()) {
396 return application
.getTheme().getColor("twindow.border.modal");
398 return application
.getTheme().getColor("twindow.border.modal.inactive");
402 return application
.getTheme().getColor("twindow.border");
405 return application
.getTheme().getColor("twindow.border.inactive");
410 * Retrieve the border line type.
412 * @return the border line type
414 protected final int getBorderType() {
416 && (inWindowMove
|| inWindowResize
|| inKeyboardResize
)
420 } else if (isModal() && inWindowMove
) {
423 } else if (isModal()) {
437 * Subclasses should override this method to cleanup resources. This is
438 * called by application.closeWindow().
440 public void onClose() {
441 // Default: do nothing
445 * Called by TApplication.drawChildren() to render on screen.
449 // Draw the box and background first.
450 CellAttributes border
= getBorder();
451 CellAttributes background
= getBackground();
452 int borderType
= getBorderType();
454 getScreen().drawBox(0, 0, width
, height
, border
,
455 background
, borderType
, true);
458 int titleLeft
= (width
- title
.length() - 2) / 2;
459 putCharXY(titleLeft
, 0, ' ', border
);
460 putStrXY(titleLeft
+ 1, 0, title
);
461 putCharXY(titleLeft
+ title
.length() + 1, 0, ' ', border
);
465 // Draw the close button
466 putCharXY(2, 0, '[', border
);
467 putCharXY(4, 0, ']', border
);
468 if (mouseOnClose() && mouse
.getMouse1()) {
469 putCharXY(3, 0, GraphicsChars
.CP437
[0x0F],
471 ? application
.getTheme().getColor("twindow.border.windowmove")
472 : application
.getTheme().getColor("twindow.border.modal.windowmove"));
474 putCharXY(3, 0, GraphicsChars
.CP437
[0xFE],
476 ? application
.getTheme().getColor("twindow.border.windowmove")
477 : application
.getTheme().getColor("twindow.border.modal.windowmove"));
480 // Draw the maximize button
483 putCharXY(width
- 5, 0, '[', border
);
484 putCharXY(width
- 3, 0, ']', border
);
485 if (mouseOnMaximize() && mouse
.getMouse1()) {
486 putCharXY(width
- 4, 0, GraphicsChars
.CP437
[0x0F],
487 application
.getTheme().getColor("twindow.border.windowmove"));
490 putCharXY(width
- 4, 0, GraphicsChars
.CP437
[0x12],
491 application
.getTheme().getColor("twindow.border.windowmove"));
493 putCharXY(width
- 4, 0, GraphicsChars
.UPARROW
,
494 application
.getTheme().getColor("twindow.border.windowmove"));
498 // Draw the resize corner
499 if ((flags
& RESIZABLE
) != 0) {
500 putCharXY(width
- 2, height
- 1, GraphicsChars
.SINGLE_BAR
,
501 application
.getTheme().getColor("twindow.border.windowmove"));
502 putCharXY(width
- 1, height
- 1, GraphicsChars
.LRCORNER
,
503 application
.getTheme().getColor("twindow.border.windowmove"));
510 * Handle mouse button presses.
512 * @param mouse mouse button event
515 public void onMouseDown(final TMouseEvent mouse
) {
517 application
.setRepaint();
519 inKeyboardResize
= false;
521 if ((mouse
.getAbsoluteY() == y
)
523 && (x
<= mouse
.getAbsoluteX())
524 && (mouse
.getAbsoluteX() < x
+ width
)
526 && !mouseOnMaximize()
528 // Begin moving window
530 moveWindowMouseX
= mouse
.getAbsoluteX();
531 moveWindowMouseY
= mouse
.getAbsoluteY();
539 if (mouseOnResize()) {
540 // Begin window resize
541 inWindowResize
= true;
542 moveWindowMouseX
= mouse
.getAbsoluteX();
543 moveWindowMouseY
= mouse
.getAbsoluteY();
544 resizeWindowWidth
= width
;
545 resizeWindowHeight
= height
;
552 // I didn't take it, pass it on to my children
553 super.onMouseDown(mouse
);
559 private void maximize() {
560 restoreWindowWidth
= width
;
561 restoreWindowHeight
= height
;
564 width
= getScreen().getWidth();
565 height
= application
.getDesktopBottom() - 1;
572 * Restote (unmaximize) window.
574 private void restore() {
575 width
= restoreWindowWidth
;
576 height
= restoreWindowHeight
;
583 * Handle mouse button releases.
585 * @param mouse mouse button release event
588 public void onMouseUp(final TMouseEvent mouse
) {
590 application
.setRepaint();
592 if ((inWindowMove
) && (mouse
.getMouse1())) {
593 // Stop moving window
594 inWindowMove
= false;
598 if ((inWindowResize
) && (mouse
.getMouse1())) {
599 // Stop resizing window
600 inWindowResize
= false;
604 if (mouse
.getMouse1() && mouseOnClose()) {
606 application
.closeWindow(this);
610 if ((mouse
.getAbsoluteY() == y
) && mouse
.getMouse1()
611 && mouseOnMaximize()) {
619 // Pass a resize event to my children
620 onResize(new TResizeEvent(TResizeEvent
.Type
.WIDGET
, width
, height
));
624 // I didn't take it, pass it on to my children
625 super.onMouseUp(mouse
);
629 * Handle mouse movements.
631 * @param mouse mouse motion event
634 public void onMouseMotion(final TMouseEvent mouse
) {
636 application
.setRepaint();
640 x
= oldWindowX
+ (mouse
.getAbsoluteX() - moveWindowMouseX
);
641 y
= oldWindowY
+ (mouse
.getAbsoluteY() - moveWindowMouseY
);
642 // Don't cover up the menu bar
643 if (y
< application
.getDesktopTop()) {
644 y
= application
.getDesktopTop();
649 if (inWindowResize
) {
651 width
= resizeWindowWidth
+ (mouse
.getAbsoluteX() - moveWindowMouseX
);
652 height
= resizeWindowHeight
+ (mouse
.getAbsoluteY() - moveWindowMouseY
);
653 if (x
+ width
> getScreen().getWidth()) {
654 width
= getScreen().getWidth() - x
;
656 if (y
+ height
> application
.getDesktopBottom()) {
657 y
= application
.getDesktopBottom() - height
+ 1;
659 // Don't cover up the menu bar
660 if (y
< application
.getDesktopTop()) {
661 y
= application
.getDesktopTop();
664 // Keep within min/max bounds
665 if (width
< minimumWindowWidth
) {
666 width
= minimumWindowWidth
;
667 inWindowResize
= false;
669 if (height
< minimumWindowHeight
) {
670 height
= minimumWindowHeight
;
671 inWindowResize
= false;
673 if ((maximumWindowWidth
> 0) && (width
> maximumWindowWidth
)) {
674 width
= maximumWindowWidth
;
675 inWindowResize
= false;
677 if ((maximumWindowHeight
> 0) && (height
> maximumWindowHeight
)) {
678 height
= maximumWindowHeight
;
679 inWindowResize
= false;
682 // Pass a resize event to my children
683 onResize(new TResizeEvent(TResizeEvent
.Type
.WIDGET
, width
, height
));
687 // I didn't take it, pass it on to my children
688 super.onMouseMotion(mouse
);
694 * @param keypress keystroke event
697 public void onKeypress(final TKeypressEvent keypress
) {
699 if (inKeyboardResize
) {
701 // ESC - Exit size/move
702 if (keypress
.equals(kbEsc
)) {
703 inKeyboardResize
= false;
706 if (keypress
.equals(kbLeft
)) {
711 if (keypress
.equals(kbRight
)) {
712 if (x
< getScreen().getWidth() - 1) {
716 if (keypress
.equals(kbDown
)) {
717 if (y
< application
.getDesktopBottom() - 1) {
721 if (keypress
.equals(kbUp
)) {
726 if (keypress
.equals(kbShiftLeft
)) {
727 if (width
> minimumWindowWidth
) {
731 if (keypress
.equals(kbShiftRight
)) {
732 if (width
< maximumWindowWidth
) {
736 if (keypress
.equals(kbShiftUp
)) {
737 if (height
> minimumWindowHeight
) {
741 if (keypress
.equals(kbShiftDown
)) {
742 if (height
< maximumWindowHeight
) {
750 // These keystrokes will typically not be seen unless a subclass
751 // overrides onMenu() due to how TApplication dispatches
754 // Ctrl-W - close window
755 if (keypress
.equals(kbCtrlW
)) {
756 application
.closeWindow(this);
760 // F6 - behave like Alt-TAB
761 if (keypress
.equals(kbF6
)) {
762 application
.switchWindow(true);
766 // Shift-F6 - behave like Shift-Alt-TAB
767 if (keypress
.equals(kbShiftF6
)) {
768 application
.switchWindow(false);
773 if (keypress
.equals(kbF5
)) {
781 // Ctrl-F5 - size/move
782 if (keypress
.equals(kbCtrlF5
)) {
783 inKeyboardResize
= !inKeyboardResize
;
786 // I didn't take it, pass it on to my children
787 super.onKeypress(keypress
);
791 * Handle posted command events.
793 * @param command command event
796 public void onCommand(final TCommandEvent command
) {
798 // These commands will typically not be seen unless a subclass
799 // overrides onMenu() due to how TApplication dispatches
802 if (command
.equals(cmWindowClose
)) {
803 application
.closeWindow(this);
807 if (command
.equals(cmWindowNext
)) {
808 application
.switchWindow(true);
812 if (command
.equals(cmWindowPrevious
)) {
813 application
.switchWindow(false);
817 if (command
.equals(cmWindowMove
)) {
818 inKeyboardResize
= true;
822 if (command
.equals(cmWindowZoom
)) {
830 // I didn't take it, pass it on to my children
831 super.onCommand(command
);
835 * Handle posted menu events.
837 * @param menu menu event
840 public void onMenu(final TMenuEvent menu
) {
841 if (menu
.getId() == TMenu
.MID_WINDOW_CLOSE
) {
842 application
.closeWindow(this);
846 if (menu
.getId() == TMenu
.MID_WINDOW_NEXT
) {
847 application
.switchWindow(true);
851 if (menu
.getId() == TMenu
.MID_WINDOW_PREVIOUS
) {
852 application
.switchWindow(false);
856 if (menu
.getId() == TMenu
.MID_WINDOW_MOVE
) {
857 inKeyboardResize
= true;
861 if (menu
.getId() == TMenu
.MID_WINDOW_ZOOM
) {
870 // I didn't take it, pass it on to my children
874 // ------------------------------------------------------------------------
875 // Passthru for Screen functions ------------------------------------------
876 // ------------------------------------------------------------------------
879 * Get the attributes at one location.
881 * @param x column coordinate. 0 is the left-most column.
882 * @param y row coordinate. 0 is the top-most row.
883 * @return attributes at (x, y)
885 public final CellAttributes
getAttrXY(final int x
, final int y
) {
886 return getScreen().getAttrXY(x
, y
);
890 * Set the attributes at one location.
892 * @param x column coordinate. 0 is the left-most column.
893 * @param y row coordinate. 0 is the top-most row.
894 * @param attr attributes to use (bold, foreColor, backColor)
896 public final void putAttrXY(final int x
, final int y
,
897 final CellAttributes attr
) {
899 getScreen().putAttrXY(x
, y
, attr
);
903 * Set the attributes at one location.
905 * @param x column coordinate. 0 is the left-most column.
906 * @param y row coordinate. 0 is the top-most row.
907 * @param attr attributes to use (bold, foreColor, backColor)
908 * @param clip if true, honor clipping/offset
910 public final void putAttrXY(final int x
, final int y
,
911 final CellAttributes attr
, final boolean clip
) {
913 getScreen().putAttrXY(x
, y
, attr
, clip
);
917 * Fill the entire screen with one character with attributes.
919 * @param ch character to draw
920 * @param attr attributes to use (bold, foreColor, backColor)
922 public final void putAll(final char ch
, final CellAttributes attr
) {
923 getScreen().putAll(ch
, attr
);
927 * Render one character with attributes.
929 * @param x column coordinate. 0 is the left-most column.
930 * @param y row coordinate. 0 is the top-most row.
931 * @param ch character + attributes to draw
933 public final void putCharXY(final int x
, final int y
, final Cell ch
) {
934 getScreen().putCharXY(x
, y
, ch
);
938 * Render one character with attributes.
940 * @param x column coordinate. 0 is the left-most column.
941 * @param y row coordinate. 0 is the top-most row.
942 * @param ch character to draw
943 * @param attr attributes to use (bold, foreColor, backColor)
945 public final void putCharXY(final int x
, final int y
, final char ch
,
946 final CellAttributes attr
) {
948 getScreen().putCharXY(x
, y
, ch
, attr
);
952 * Render one character without changing the underlying attributes.
954 * @param x column coordinate. 0 is the left-most column.
955 * @param y row coordinate. 0 is the top-most row.
956 * @param ch character to draw
958 public final void putCharXY(final int x
, final int y
, final char ch
) {
959 getScreen().putCharXY(x
, y
, ch
);
963 * Render a string. Does not wrap if the string exceeds the line.
965 * @param x column coordinate. 0 is the left-most column.
966 * @param y row coordinate. 0 is the top-most row.
967 * @param str string to draw
968 * @param attr attributes to use (bold, foreColor, backColor)
970 public final void putStrXY(final int x
, final int y
, final String str
,
971 final CellAttributes attr
) {
973 getScreen().putStrXY(x
, y
, str
, attr
);
977 * Render a string without changing the underlying attribute. Does not
978 * wrap if the string exceeds the line.
980 * @param x column coordinate. 0 is the left-most column.
981 * @param y row coordinate. 0 is the top-most row.
982 * @param str string to draw
984 public final void putStrXY(final int x
, final int y
, final String str
) {
985 getScreen().putStrXY(x
, y
, str
);
989 * Draw a vertical line from (x, y) to (x, y + n).
991 * @param x column coordinate. 0 is the left-most column.
992 * @param y row coordinate. 0 is the top-most row.
993 * @param n number of characters to draw
994 * @param ch character to draw
995 * @param attr attributes to use (bold, foreColor, backColor)
997 public final void vLineXY(final int x
, final int y
, final int n
,
998 final char ch
, final CellAttributes attr
) {
1000 getScreen().vLineXY(x
, y
, n
, ch
, attr
);
1004 * Draw a horizontal line from (x, y) to (x + n, y).
1006 * @param x column coordinate. 0 is the left-most column.
1007 * @param y row coordinate. 0 is the top-most row.
1008 * @param n number of characters to draw
1009 * @param ch character to draw
1010 * @param attr attributes to use (bold, foreColor, backColor)
1012 public final void hLineXY(final int x
, final int y
, final int n
,
1013 final char ch
, final CellAttributes attr
) {
1015 getScreen().hLineXY(x
, y
, n
, ch
, attr
);