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 private 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 private String title
= "";
81 * @return window title
83 public final String
getTitle() {
90 * @param title new window title
92 public final void setTitle(final String title
) {
97 * Window is resizable (default yes).
99 public static final int RESIZABLE
= 0x01;
102 * Window is modal (default no).
104 public static final int MODAL
= 0x02;
107 * Window is centered (default no).
109 public static final int CENTERED
= 0x04;
114 private int flags
= RESIZABLE
;
117 * Z order. Lower number means more in-front.
122 * Get Z order. Lower number means more in-front.
124 * @return Z value. Lower number means more in-front.
126 public final int getZ() {
131 * Set Z order. Lower number means more in-front.
133 * @param z the new Z value. Lower number means more in-front.
135 public final void setZ(final int z
) {
140 * If true, then the user clicked on the title bar and is moving the
143 private boolean inWindowMove
= false;
146 * If true, then the user clicked on the bottom right corner and is
147 * resizing the window.
149 private boolean inWindowResize
= false;
152 * If true, then the user selected "Size/Move" (or hit Ctrl-F5) and is
153 * resizing/moving the window via the keyboard.
155 private boolean inKeyboardResize
= false;
158 * If true, this window is maximized.
160 private boolean maximized
= false;
163 * Remember mouse state.
165 private TMouseEvent mouse
;
167 // For moving the window. resizing also uses moveWindowMouseX/Y
168 private int moveWindowMouseX
;
169 private int moveWindowMouseY
;
170 private int oldWindowX
;
171 private int oldWindowY
;
174 private int resizeWindowWidth
;
175 private int resizeWindowHeight
;
176 private int minimumWindowWidth
= 10;
177 private int minimumWindowHeight
= 2;
178 private int maximumWindowWidth
= -1;
179 private int maximumWindowHeight
= -1;
181 // For maximize/restore
182 private int restoreWindowWidth
;
183 private int restoreWindowHeight
;
184 private int restoreWindowX
;
185 private int restoreWindowY
;
188 * Public constructor. Window will be located at (0, 0).
190 * @param application TApplication that manages this window
191 * @param title window title, will be centered along the top border
192 * @param width width of window
193 * @param height height of window
195 public TWindow(final TApplication application
, final String title
,
196 final int width
, final int height
) {
198 this(application
, title
, 0, 0, width
, height
, RESIZABLE
);
202 * Public constructor. Window will be located at (0, 0).
204 * @param application TApplication that manages this window
205 * @param title window title, will be centered along the top border
206 * @param width width of window
207 * @param height height of window
208 * @param flags bitmask of RESIZABLE, CENTERED, or MODAL
210 public TWindow(final TApplication application
, final String title
,
211 final int width
, final int height
, final int flags
) {
213 this(application
, title
, 0, 0, width
, height
, flags
);
217 * Public constructor.
219 * @param application TApplication that manages this window
220 * @param title window title, will be centered along the top border
221 * @param x column relative to parent
222 * @param y row relative to parent
223 * @param width width of window
224 * @param height height of window
226 public TWindow(final TApplication application
, final String title
,
227 final int x
, final int y
, final int width
, final int height
) {
229 this(application
, title
, x
, y
, width
, height
, RESIZABLE
);
233 * Public constructor.
235 * @param application TApplication that manages this window
236 * @param title window title, will be centered along the top border
237 * @param x column relative to parent
238 * @param y row relative to parent
239 * @param width width of window
240 * @param height height of window
241 * @param flags mask of RESIZABLE, CENTERED, or MODAL
243 public TWindow(final TApplication application
, final String title
,
244 final int x
, final int y
, final int width
, final int height
,
249 // I am my own window and parent
250 setupForTWindow(this, x
, y
+ application
.getDesktopTop(),
255 this.application
= application
;
258 // Minimum width/height are 10 and 2
259 assert (width
>= 10);
260 assert (getHeight() >= 2);
262 // MODAL implies CENTERED
264 this.flags
|= CENTERED
;
267 // Center window if specified
270 // Add me to the application
271 application
.addWindow(this);
275 * Recenter the window on-screen.
277 public final void center() {
278 if ((flags
& CENTERED
) != 0) {
279 if (getWidth() < getScreen().getWidth()) {
280 setX((getScreen().getWidth() - getWidth()) / 2);
284 setY(((application
.getDesktopBottom()
285 - application
.getDesktopTop()) - getHeight()) / 2);
289 setY(getY() + application
.getDesktopTop());
294 * Returns true if this window is modal.
296 * @return true if this window is modal
298 public final boolean isModal() {
299 if ((flags
& MODAL
) == 0) {
306 * Comparison operator sorts on z.
308 * @param that another TWindow instance
309 * @return difference between this.z and that.z
312 public final int compareTo(final TWindow that
) {
313 return (this.z
- that
.z
);
317 * Returns true if the mouse is currently on the close button.
319 * @return true if mouse is currently on the close button
321 private boolean mouseOnClose() {
323 && (mouse
.getAbsoluteY() == getY())
324 && (mouse
.getAbsoluteX() == getX() + 3)
332 * Returns true if the mouse is currently on the maximize/restore button.
334 * @return true if the mouse is currently on the maximize/restore button
336 private boolean mouseOnMaximize() {
339 && (mouse
.getAbsoluteY() == getY())
340 && (mouse
.getAbsoluteX() == getX() + getWidth() - 4)
348 * Returns true if the mouse is currently on the resizable lower right
351 * @return true if the mouse is currently on the resizable lower right
354 private boolean mouseOnResize() {
355 if (((flags
& RESIZABLE
) != 0)
358 && (mouse
.getAbsoluteY() == getY() + getHeight() - 1)
359 && ((mouse
.getAbsoluteX() == getX() + getWidth() - 1)
360 || (mouse
.getAbsoluteX() == getX() + getWidth() - 2))
368 * Retrieve the background color.
370 * @return the background color
372 private final CellAttributes
getBackground() {
374 && (inWindowMove
|| inWindowResize
|| inKeyboardResize
)
376 assert (getActive());
377 return application
.getTheme().getColor("twindow.background.windowmove");
378 } else if (isModal() && inWindowMove
) {
379 assert (getActive());
380 return application
.getTheme().getColor("twindow.background.modal");
381 } else if (isModal()) {
383 return application
.getTheme().getColor("twindow.background.modal");
385 return application
.getTheme().getColor("twindow.background.modal.inactive");
386 } else if (getActive()) {
388 return application
.getTheme().getColor("twindow.background");
391 return application
.getTheme().getColor("twindow.background.inactive");
396 * Retrieve the border color.
398 * @return the border color
400 private final CellAttributes
getBorder() {
402 && (inWindowMove
|| inWindowResize
|| inKeyboardResize
)
404 assert (getActive());
405 return application
.getTheme().getColor("twindow.border.windowmove");
406 } else if (isModal() && inWindowMove
) {
407 assert (getActive());
408 return application
.getTheme().getColor("twindow.border.modal.windowmove");
409 } else if (isModal()) {
411 return application
.getTheme().getColor("twindow.border.modal");
413 return application
.getTheme().getColor("twindow.border.modal.inactive");
415 } else if (getActive()) {
417 return application
.getTheme().getColor("twindow.border");
420 return application
.getTheme().getColor("twindow.border.inactive");
425 * Retrieve the border line type.
427 * @return the border line type
429 private final int getBorderType() {
431 && (inWindowMove
|| inWindowResize
|| inKeyboardResize
)
433 assert (getActive());
435 } else if (isModal() && inWindowMove
) {
436 assert (getActive());
438 } else if (isModal()) {
444 } else if (getActive()) {
452 * Subclasses should override this method to cleanup resources. This is
453 * called by application.closeWindow().
455 public void onClose() {
456 // Default: do nothing
460 * Called by TApplication.drawChildren() to render on screen.
464 // Draw the box and background first.
465 CellAttributes border
= getBorder();
466 CellAttributes background
= getBackground();
467 int borderType
= getBorderType();
469 getScreen().drawBox(0, 0, getWidth(), getHeight(), border
,
470 background
, borderType
, true);
473 int titleLeft
= (getWidth() - title
.length() - 2) / 2;
474 putCharXY(titleLeft
, 0, ' ', border
);
475 putStrXY(titleLeft
+ 1, 0, title
);
476 putCharXY(titleLeft
+ title
.length() + 1, 0, ' ', border
);
480 // Draw the close button
481 putCharXY(2, 0, '[', border
);
482 putCharXY(4, 0, ']', border
);
483 if (mouseOnClose() && mouse
.getMouse1()) {
484 putCharXY(3, 0, GraphicsChars
.CP437
[0x0F],
486 ? application
.getTheme().getColor("twindow.border.windowmove")
487 : application
.getTheme().getColor("twindow.border.modal.windowmove"));
489 putCharXY(3, 0, GraphicsChars
.CP437
[0xFE],
491 ? application
.getTheme().getColor("twindow.border.windowmove")
492 : application
.getTheme().getColor("twindow.border.modal.windowmove"));
495 // Draw the maximize button
498 putCharXY(getWidth() - 5, 0, '[', border
);
499 putCharXY(getWidth() - 3, 0, ']', border
);
500 if (mouseOnMaximize() && mouse
.getMouse1()) {
501 putCharXY(getWidth() - 4, 0, GraphicsChars
.CP437
[0x0F],
502 application
.getTheme().getColor("twindow.border.windowmove"));
505 putCharXY(getWidth() - 4, 0, GraphicsChars
.CP437
[0x12],
506 application
.getTheme().getColor("twindow.border.windowmove"));
508 putCharXY(getWidth() - 4, 0, GraphicsChars
.UPARROW
,
509 application
.getTheme().getColor("twindow.border.windowmove"));
513 // Draw the resize corner
514 if ((flags
& RESIZABLE
) != 0) {
515 putCharXY(getWidth() - 2, getHeight() - 1, GraphicsChars
.SINGLE_BAR
,
516 application
.getTheme().getColor("twindow.border.windowmove"));
517 putCharXY(getWidth() - 1, getHeight() - 1, GraphicsChars
.LRCORNER
,
518 application
.getTheme().getColor("twindow.border.windowmove"));
525 * Handle mouse button presses.
527 * @param mouse mouse button event
530 public void onMouseDown(final TMouseEvent mouse
) {
532 application
.setRepaint();
534 inKeyboardResize
= false;
536 if ((mouse
.getAbsoluteY() == getY())
538 && (getX() <= mouse
.getAbsoluteX())
539 && (mouse
.getAbsoluteX() < getX() + getWidth())
541 && !mouseOnMaximize()
543 // Begin moving window
545 moveWindowMouseX
= mouse
.getAbsoluteX();
546 moveWindowMouseY
= mouse
.getAbsoluteY();
554 if (mouseOnResize()) {
555 // Begin window resize
556 inWindowResize
= true;
557 moveWindowMouseX
= mouse
.getAbsoluteX();
558 moveWindowMouseY
= mouse
.getAbsoluteY();
559 resizeWindowWidth
= getWidth();
560 resizeWindowHeight
= getHeight();
567 // I didn't take it, pass it on to my children
568 super.onMouseDown(mouse
);
574 private void maximize() {
575 restoreWindowWidth
= getWidth();
576 restoreWindowHeight
= getHeight();
577 restoreWindowX
= getX();
578 restoreWindowY
= getY();
579 setWidth(getScreen().getWidth());
580 setHeight(application
.getDesktopBottom() - 1);
587 * Restote (unmaximize) window.
589 private void restore() {
590 setWidth(restoreWindowWidth
);
591 setHeight(restoreWindowHeight
);
592 setX(restoreWindowX
);
593 setY(restoreWindowY
);
598 * Handle mouse button releases.
600 * @param mouse mouse button release event
603 public void onMouseUp(final TMouseEvent mouse
) {
605 application
.setRepaint();
607 if ((inWindowMove
) && (mouse
.getMouse1())) {
608 // Stop moving window
609 inWindowMove
= false;
613 if ((inWindowResize
) && (mouse
.getMouse1())) {
614 // Stop resizing window
615 inWindowResize
= false;
619 if (mouse
.getMouse1() && mouseOnClose()) {
621 application
.closeWindow(this);
625 if ((mouse
.getAbsoluteY() == getY())
627 && mouseOnMaximize()) {
635 // Pass a resize event to my children
636 onResize(new TResizeEvent(TResizeEvent
.Type
.WIDGET
,
637 getWidth(), getHeight()));
641 // I didn't take it, pass it on to my children
642 super.onMouseUp(mouse
);
646 * Handle mouse movements.
648 * @param mouse mouse motion event
651 public void onMouseMotion(final TMouseEvent mouse
) {
653 application
.setRepaint();
657 setX(oldWindowX
+ (mouse
.getAbsoluteX() - moveWindowMouseX
));
658 setY(oldWindowY
+ (mouse
.getAbsoluteY() - moveWindowMouseY
));
659 // Don't cover up the menu bar
660 if (getY() < application
.getDesktopTop()) {
661 setY(application
.getDesktopTop());
666 if (inWindowResize
) {
668 setWidth(resizeWindowWidth
+ (mouse
.getAbsoluteX()
669 - moveWindowMouseX
));
670 setHeight(resizeWindowHeight
+ (mouse
.getAbsoluteY()
671 - moveWindowMouseY
));
672 if (getX() + getWidth() > getScreen().getWidth()) {
673 setWidth(getScreen().getWidth() - getX());
675 if (getY() + getHeight() > application
.getDesktopBottom()) {
676 setY(application
.getDesktopBottom() - getHeight() + 1);
678 // Don't cover up the menu bar
679 if (getY() < application
.getDesktopTop()) {
680 setY(application
.getDesktopTop());
683 // Keep within min/max bounds
684 if (getWidth() < minimumWindowWidth
) {
685 setWidth(minimumWindowWidth
);
686 inWindowResize
= false;
688 if (getHeight() < minimumWindowHeight
) {
689 setHeight(minimumWindowHeight
);
690 inWindowResize
= false;
692 if ((maximumWindowWidth
> 0)
693 && (getWidth() > maximumWindowWidth
)
695 setWidth(maximumWindowWidth
);
696 inWindowResize
= false;
698 if ((maximumWindowHeight
> 0)
699 && (getHeight() > maximumWindowHeight
)
701 setHeight(maximumWindowHeight
);
702 inWindowResize
= false;
705 // Pass a resize event to my children
706 onResize(new TResizeEvent(TResizeEvent
.Type
.WIDGET
,
707 getWidth(), getHeight()));
711 // I didn't take it, pass it on to my children
712 super.onMouseMotion(mouse
);
718 * @param keypress keystroke event
721 public void onKeypress(final TKeypressEvent keypress
) {
723 if (inKeyboardResize
) {
725 // ESC - Exit size/move
726 if (keypress
.equals(kbEsc
)) {
727 inKeyboardResize
= false;
730 if (keypress
.equals(kbLeft
)) {
735 if (keypress
.equals(kbRight
)) {
736 if (getX() < getScreen().getWidth() - 1) {
740 if (keypress
.equals(kbDown
)) {
741 if (getY() < application
.getDesktopBottom() - 1) {
745 if (keypress
.equals(kbUp
)) {
750 if (keypress
.equals(kbShiftLeft
)) {
751 if (getWidth() > minimumWindowWidth
) {
752 setWidth(getWidth() - 1);
755 if (keypress
.equals(kbShiftRight
)) {
756 if (getWidth() < maximumWindowWidth
) {
757 setWidth(getWidth() + 1);
760 if (keypress
.equals(kbShiftUp
)) {
761 if (getHeight() > minimumWindowHeight
) {
762 setHeight(getHeight() - 1);
765 if (keypress
.equals(kbShiftDown
)) {
766 if (getHeight() < maximumWindowHeight
) {
767 setHeight(getHeight() + 1);
774 // These keystrokes will typically not be seen unless a subclass
775 // overrides onMenu() due to how TApplication dispatches
778 // Ctrl-W - close window
779 if (keypress
.equals(kbCtrlW
)) {
780 application
.closeWindow(this);
784 // F6 - behave like Alt-TAB
785 if (keypress
.equals(kbF6
)) {
786 application
.switchWindow(true);
790 // Shift-F6 - behave like Shift-Alt-TAB
791 if (keypress
.equals(kbShiftF6
)) {
792 application
.switchWindow(false);
797 if (keypress
.equals(kbF5
)) {
805 // Ctrl-F5 - size/move
806 if (keypress
.equals(kbCtrlF5
)) {
807 inKeyboardResize
= !inKeyboardResize
;
810 // I didn't take it, pass it on to my children
811 super.onKeypress(keypress
);
815 * Handle posted command events.
817 * @param command command event
820 public void onCommand(final TCommandEvent command
) {
822 // These commands will typically not be seen unless a subclass
823 // overrides onMenu() due to how TApplication dispatches
826 if (command
.equals(cmWindowClose
)) {
827 application
.closeWindow(this);
831 if (command
.equals(cmWindowNext
)) {
832 application
.switchWindow(true);
836 if (command
.equals(cmWindowPrevious
)) {
837 application
.switchWindow(false);
841 if (command
.equals(cmWindowMove
)) {
842 inKeyboardResize
= true;
846 if (command
.equals(cmWindowZoom
)) {
854 // I didn't take it, pass it on to my children
855 super.onCommand(command
);
859 * Handle posted menu events.
861 * @param menu menu event
864 public void onMenu(final TMenuEvent menu
) {
865 if (menu
.getId() == TMenu
.MID_WINDOW_CLOSE
) {
866 application
.closeWindow(this);
870 if (menu
.getId() == TMenu
.MID_WINDOW_NEXT
) {
871 application
.switchWindow(true);
875 if (menu
.getId() == TMenu
.MID_WINDOW_PREVIOUS
) {
876 application
.switchWindow(false);
880 if (menu
.getId() == TMenu
.MID_WINDOW_MOVE
) {
881 inKeyboardResize
= true;
885 if (menu
.getId() == TMenu
.MID_WINDOW_ZOOM
) {
894 // I didn't take it, pass it on to my children
898 // ------------------------------------------------------------------------
899 // Passthru for Screen functions ------------------------------------------
900 // ------------------------------------------------------------------------
903 * Get 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 * @return attributes at (x, y)
909 public final CellAttributes
getAttrXY(final int x
, final int y
) {
910 return getScreen().getAttrXY(x
, y
);
914 * Set the attributes at one location.
916 * @param x column coordinate. 0 is the left-most column.
917 * @param y row coordinate. 0 is the top-most row.
918 * @param attr attributes to use (bold, foreColor, backColor)
920 public final void putAttrXY(final int x
, final int y
,
921 final CellAttributes attr
) {
923 getScreen().putAttrXY(x
, y
, attr
);
927 * Set the attributes at one location.
929 * @param x column coordinate. 0 is the left-most column.
930 * @param y row coordinate. 0 is the top-most row.
931 * @param attr attributes to use (bold, foreColor, backColor)
932 * @param clip if true, honor clipping/offset
934 public final void putAttrXY(final int x
, final int y
,
935 final CellAttributes attr
, final boolean clip
) {
937 getScreen().putAttrXY(x
, y
, attr
, clip
);
941 * Fill the entire screen with one character with attributes.
943 * @param ch character to draw
944 * @param attr attributes to use (bold, foreColor, backColor)
946 public final void putAll(final char ch
, final CellAttributes attr
) {
947 getScreen().putAll(ch
, attr
);
951 * Render one character with attributes.
953 * @param x column coordinate. 0 is the left-most column.
954 * @param y row coordinate. 0 is the top-most row.
955 * @param ch character + attributes to draw
957 public final void putCharXY(final int x
, final int y
, final Cell ch
) {
958 getScreen().putCharXY(x
, y
, ch
);
962 * Render one character with attributes.
964 * @param x column coordinate. 0 is the left-most column.
965 * @param y row coordinate. 0 is the top-most row.
966 * @param ch character to draw
967 * @param attr attributes to use (bold, foreColor, backColor)
969 public final void putCharXY(final int x
, final int y
, final char ch
,
970 final CellAttributes attr
) {
972 getScreen().putCharXY(x
, y
, ch
, attr
);
976 * Render one character without changing the underlying attributes.
978 * @param x column coordinate. 0 is the left-most column.
979 * @param y row coordinate. 0 is the top-most row.
980 * @param ch character to draw
982 public final void putCharXY(final int x
, final int y
, final char ch
) {
983 getScreen().putCharXY(x
, y
, ch
);
987 * Render a string. Does not wrap if the string exceeds the line.
989 * @param x column coordinate. 0 is the left-most column.
990 * @param y row coordinate. 0 is the top-most row.
991 * @param str string to draw
992 * @param attr attributes to use (bold, foreColor, backColor)
994 public final void putStrXY(final int x
, final int y
, final String str
,
995 final CellAttributes attr
) {
997 getScreen().putStrXY(x
, y
, str
, attr
);
1001 * Render a string without changing the underlying attribute. Does not
1002 * wrap if the string exceeds the line.
1004 * @param x column coordinate. 0 is the left-most column.
1005 * @param y row coordinate. 0 is the top-most row.
1006 * @param str string to draw
1008 public final void putStrXY(final int x
, final int y
, final String str
) {
1009 getScreen().putStrXY(x
, y
, str
);
1013 * Draw a vertical line from (x, y) to (x, y + n).
1015 * @param x column coordinate. 0 is the left-most column.
1016 * @param y row coordinate. 0 is the top-most row.
1017 * @param n number of characters to draw
1018 * @param ch character to draw
1019 * @param attr attributes to use (bold, foreColor, backColor)
1021 public final void vLineXY(final int x
, final int y
, final int n
,
1022 final char ch
, final CellAttributes attr
) {
1024 getScreen().vLineXY(x
, y
, n
, ch
, attr
);
1028 * Draw a horizontal line from (x, y) to (x + n, y).
1030 * @param x column coordinate. 0 is the left-most column.
1031 * @param y row coordinate. 0 is the top-most row.
1032 * @param n number of characters to draw
1033 * @param ch character to draw
1034 * @param attr attributes to use (bold, foreColor, backColor)
1036 public final void hLineXY(final int x
, final int y
, final int n
,
1037 final char ch
, final CellAttributes attr
) {
1039 getScreen().hLineXY(x
, y
, n
, ch
, attr
);