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 jexer
.menu
.TMenu
;
43 import static jexer
.TCommand
.*;
44 import static jexer
.TKeypress
.*;
47 * TWindow is the top-level container and drawing surface for other widgets.
49 public class TWindow
extends TWidget
{
52 * Window's parent TApplication.
54 private TApplication application
;
57 * Get this TWindow's parent TApplication.
59 * @return this TWindow's parent TApplication
62 public final TApplication
getApplication() {
72 public final Screen
getScreen() {
73 return application
.getScreen();
79 private String title
= "";
84 * @return window title
86 public final String
getTitle() {
93 * @param title new window title
95 public final void setTitle(final String title
) {
100 * Window is resizable (default yes).
102 public static final int RESIZABLE
= 0x01;
105 * Window is modal (default no).
107 public static final int MODAL
= 0x02;
110 * Window is centered (default no).
112 public static final int CENTERED
= 0x04;
117 private int flags
= RESIZABLE
;
120 * Z order. Lower number means more in-front.
125 * Get Z order. Lower number means more in-front.
127 * @return Z value. Lower number means more in-front.
129 public final int getZ() {
134 * Set Z order. Lower number means more in-front.
136 * @param z the new Z value. Lower number means more in-front.
138 public final void setZ(final int z
) {
143 * If true, then the user clicked on the title bar and is moving the
146 private boolean inWindowMove
= false;
149 * If true, then the user clicked on the bottom right corner and is
150 * resizing the window.
152 private boolean inWindowResize
= false;
155 * If true, then the user selected "Size/Move" (or hit Ctrl-F5) and is
156 * resizing/moving the window via the keyboard.
158 private boolean inKeyboardResize
= false;
161 * If true, this window is maximized.
163 private boolean maximized
= false;
166 * Remember mouse state.
168 protected TMouseEvent mouse
;
170 // For moving the window. resizing also uses moveWindowMouseX/Y
171 private int moveWindowMouseX
;
172 private int moveWindowMouseY
;
173 private int oldWindowX
;
174 private int oldWindowY
;
177 private int resizeWindowWidth
;
178 private int resizeWindowHeight
;
179 private int minimumWindowWidth
= 10;
180 private int minimumWindowHeight
= 2;
181 private int maximumWindowWidth
= -1;
182 private int maximumWindowHeight
= -1;
184 // For maximize/restore
185 private int restoreWindowWidth
;
186 private int restoreWindowHeight
;
187 private int restoreWindowX
;
188 private int restoreWindowY
;
191 * Set the maximum width for this window.
193 * @param maximumWindowWidth new maximum width
195 public final void setMaximumWindowWidth(final int maximumWindowWidth
) {
196 this.maximumWindowWidth
= maximumWindowWidth
;
200 * Public constructor. Window will be located at (0, 0).
202 * @param application TApplication that manages this window
203 * @param title window title, will be centered along the top border
204 * @param width width of window
205 * @param height height of window
207 public TWindow(final TApplication application
, final String title
,
208 final int width
, final int height
) {
210 this(application
, title
, 0, 0, width
, height
, RESIZABLE
);
214 * Public constructor. Window will be located at (0, 0).
216 * @param application TApplication that manages this window
217 * @param title window title, will be centered along the top border
218 * @param width width of window
219 * @param height height of window
220 * @param flags bitmask of RESIZABLE, CENTERED, or MODAL
222 public TWindow(final TApplication application
, final String title
,
223 final int width
, final int height
, final int flags
) {
225 this(application
, title
, 0, 0, width
, height
, flags
);
229 * Public constructor.
231 * @param application TApplication that manages this window
232 * @param title window title, will be centered along the top border
233 * @param x column relative to parent
234 * @param y row relative to parent
235 * @param width width of window
236 * @param height height of window
238 public TWindow(final TApplication application
, final String title
,
239 final int x
, final int y
, final int width
, final int height
) {
241 this(application
, title
, x
, y
, width
, height
, RESIZABLE
);
245 * Public constructor.
247 * @param application TApplication that manages this window
248 * @param title window title, will be centered along the top border
249 * @param x column relative to parent
250 * @param y row relative to parent
251 * @param width width of window
252 * @param height height of window
253 * @param flags mask of RESIZABLE, CENTERED, or MODAL
255 public TWindow(final TApplication application
, final String title
,
256 final int x
, final int y
, final int width
, final int height
,
261 // I am my own window and parent
262 setupForTWindow(this, x
, y
+ application
.getDesktopTop(),
267 this.application
= application
;
270 // Minimum width/height are 10 and 2
271 assert (width
>= 10);
272 assert (getHeight() >= 2);
274 // MODAL implies CENTERED
276 this.flags
|= CENTERED
;
279 // Center window if specified
282 // Add me to the application
283 application
.addWindow(this);
287 * Recenter the window on-screen.
289 public final void center() {
290 if ((flags
& CENTERED
) != 0) {
291 if (getWidth() < getScreen().getWidth()) {
292 setX((getScreen().getWidth() - getWidth()) / 2);
296 setY(((application
.getDesktopBottom()
297 - application
.getDesktopTop()) - getHeight()) / 2);
301 setY(getY() + application
.getDesktopTop());
306 * Returns true if this window is modal.
308 * @return true if this window is modal
310 public final boolean isModal() {
311 if ((flags
& MODAL
) == 0) {
318 * Returns true if the mouse is currently on the close button.
320 * @return true if mouse is currently on the close button
322 private boolean mouseOnClose() {
324 && (mouse
.getAbsoluteY() == getY())
325 && (mouse
.getAbsoluteX() == getX() + 3)
333 * Returns true if the mouse is currently on the maximize/restore button.
335 * @return true if the mouse is currently on the maximize/restore button
337 private boolean mouseOnMaximize() {
340 && (mouse
.getAbsoluteY() == getY())
341 && (mouse
.getAbsoluteX() == getX() + getWidth() - 4)
349 * Returns true if the mouse is currently on the resizable lower right
352 * @return true if the mouse is currently on the resizable lower right
355 private boolean mouseOnResize() {
356 if (((flags
& RESIZABLE
) != 0)
359 && (mouse
.getAbsoluteY() == getY() + getHeight() - 1)
360 && ((mouse
.getAbsoluteX() == getX() + getWidth() - 1)
361 || (mouse
.getAbsoluteX() == getX() + getWidth() - 2))
369 * Retrieve the background color.
371 * @return the background color
373 public final CellAttributes
getBackground() {
375 && (inWindowMove
|| inWindowResize
|| inKeyboardResize
)
377 assert (getActive());
378 return getTheme().getColor("twindow.background.windowmove");
379 } else if (isModal() && inWindowMove
) {
380 assert (getActive());
381 return getTheme().getColor("twindow.background.modal");
382 } else if (isModal()) {
384 return getTheme().getColor("twindow.background.modal");
386 return getTheme().getColor("twindow.background.modal.inactive");
387 } else if (getActive()) {
389 return getTheme().getColor("twindow.background");
392 return getTheme().getColor("twindow.background.inactive");
397 * Retrieve the border color.
399 * @return the border color
401 private CellAttributes
getBorder() {
403 && (inWindowMove
|| inWindowResize
|| inKeyboardResize
)
405 assert (getActive());
406 return getTheme().getColor("twindow.border.windowmove");
407 } else if (isModal() && inWindowMove
) {
408 assert (getActive());
409 return getTheme().getColor("twindow.border.modal.windowmove");
410 } else if (isModal()) {
412 return getTheme().getColor("twindow.border.modal");
414 return getTheme().getColor("twindow.border.modal.inactive");
416 } else if (getActive()) {
418 return getTheme().getColor("twindow.border");
421 return getTheme().getColor("twindow.border.inactive");
426 * Retrieve the border line type.
428 * @return the border line type
430 private int getBorderType() {
432 && (inWindowMove
|| inWindowResize
|| inKeyboardResize
)
434 assert (getActive());
436 } else if (isModal() && inWindowMove
) {
437 assert (getActive());
439 } else if (isModal()) {
445 } else if (getActive()) {
453 * Subclasses should override this method to cleanup resources. This is
454 * called by application.closeWindow().
456 public void onClose() {
457 // Default: do nothing
461 * Called by TApplication.drawChildren() to render on screen.
465 // Draw the box and background first.
466 CellAttributes border
= getBorder();
467 CellAttributes background
= getBackground();
468 int borderType
= getBorderType();
470 getScreen().drawBox(0, 0, getWidth(), getHeight(), border
,
471 background
, borderType
, true);
474 int titleLeft
= (getWidth() - title
.length() - 2) / 2;
475 putCharXY(titleLeft
, 0, ' ', border
);
476 putStrXY(titleLeft
+ 1, 0, title
);
477 putCharXY(titleLeft
+ title
.length() + 1, 0, ' ', border
);
481 // Draw the close button
482 putCharXY(2, 0, '[', border
);
483 putCharXY(4, 0, ']', border
);
484 if (mouseOnClose() && mouse
.getMouse1()) {
485 putCharXY(3, 0, GraphicsChars
.CP437
[0x0F],
487 ?
getTheme().getColor("twindow.border.windowmove")
488 : getTheme().getColor("twindow.border.modal.windowmove"));
490 putCharXY(3, 0, GraphicsChars
.CP437
[0xFE],
492 ?
getTheme().getColor("twindow.border.windowmove")
493 : getTheme().getColor("twindow.border.modal.windowmove"));
496 // Draw the maximize button
499 putCharXY(getWidth() - 5, 0, '[', border
);
500 putCharXY(getWidth() - 3, 0, ']', border
);
501 if (mouseOnMaximize() && mouse
.getMouse1()) {
502 putCharXY(getWidth() - 4, 0, GraphicsChars
.CP437
[0x0F],
503 getTheme().getColor("twindow.border.windowmove"));
506 putCharXY(getWidth() - 4, 0, GraphicsChars
.CP437
[0x12],
507 getTheme().getColor("twindow.border.windowmove"));
509 putCharXY(getWidth() - 4, 0, GraphicsChars
.UPARROW
,
510 getTheme().getColor("twindow.border.windowmove"));
514 // Draw the resize corner
515 if ((flags
& RESIZABLE
) != 0) {
516 putCharXY(getWidth() - 2, getHeight() - 1,
517 GraphicsChars
.SINGLE_BAR
,
518 getTheme().getColor("twindow.border.windowmove"));
519 putCharXY(getWidth() - 1, getHeight() - 1,
520 GraphicsChars
.LRCORNER
,
521 getTheme().getColor("twindow.border.windowmove"));
528 * Handle mouse button presses.
530 * @param mouse mouse button event
533 public void onMouseDown(final TMouseEvent mouse
) {
535 application
.setRepaint();
537 inKeyboardResize
= false;
539 if ((mouse
.getAbsoluteY() == getY())
541 && (getX() <= mouse
.getAbsoluteX())
542 && (mouse
.getAbsoluteX() < getX() + getWidth())
544 && !mouseOnMaximize()
546 // Begin moving window
548 moveWindowMouseX
= mouse
.getAbsoluteX();
549 moveWindowMouseY
= mouse
.getAbsoluteY();
557 if (mouseOnResize()) {
558 // Begin window resize
559 inWindowResize
= true;
560 moveWindowMouseX
= mouse
.getAbsoluteX();
561 moveWindowMouseY
= mouse
.getAbsoluteY();
562 resizeWindowWidth
= getWidth();
563 resizeWindowHeight
= getHeight();
570 // I didn't take it, pass it on to my children
571 super.onMouseDown(mouse
);
577 private void maximize() {
578 restoreWindowWidth
= getWidth();
579 restoreWindowHeight
= getHeight();
580 restoreWindowX
= getX();
581 restoreWindowY
= getY();
582 setWidth(getScreen().getWidth());
583 setHeight(application
.getDesktopBottom() - 1);
590 * Restote (unmaximize) window.
592 private void restore() {
593 setWidth(restoreWindowWidth
);
594 setHeight(restoreWindowHeight
);
595 setX(restoreWindowX
);
596 setY(restoreWindowY
);
601 * Handle mouse button releases.
603 * @param mouse mouse button release event
606 public void onMouseUp(final TMouseEvent mouse
) {
608 application
.setRepaint();
610 if ((inWindowMove
) && (mouse
.getMouse1())) {
611 // Stop moving window
612 inWindowMove
= false;
616 if ((inWindowResize
) && (mouse
.getMouse1())) {
617 // Stop resizing window
618 inWindowResize
= false;
622 if (mouse
.getMouse1() && mouseOnClose()) {
624 application
.closeWindow(this);
628 if ((mouse
.getAbsoluteY() == getY())
630 && mouseOnMaximize()) {
638 // Pass a resize event to my children
639 onResize(new TResizeEvent(TResizeEvent
.Type
.WIDGET
,
640 getWidth(), getHeight()));
644 // I didn't take it, pass it on to my children
645 super.onMouseUp(mouse
);
649 * Handle mouse movements.
651 * @param mouse mouse motion event
654 public void onMouseMotion(final TMouseEvent mouse
) {
656 application
.setRepaint();
660 setX(oldWindowX
+ (mouse
.getAbsoluteX() - moveWindowMouseX
));
661 setY(oldWindowY
+ (mouse
.getAbsoluteY() - moveWindowMouseY
));
662 // Don't cover up the menu bar
663 if (getY() < application
.getDesktopTop()) {
664 setY(application
.getDesktopTop());
669 if (inWindowResize
) {
671 setWidth(resizeWindowWidth
+ (mouse
.getAbsoluteX()
672 - moveWindowMouseX
));
673 setHeight(resizeWindowHeight
+ (mouse
.getAbsoluteY()
674 - moveWindowMouseY
));
675 if (getX() + getWidth() > getScreen().getWidth()) {
676 setWidth(getScreen().getWidth() - getX());
678 if (getY() + getHeight() > application
.getDesktopBottom()) {
679 setY(application
.getDesktopBottom() - getHeight() + 1);
681 // Don't cover up the menu bar
682 if (getY() < application
.getDesktopTop()) {
683 setY(application
.getDesktopTop());
686 // Keep within min/max bounds
687 if (getWidth() < minimumWindowWidth
) {
688 setWidth(minimumWindowWidth
);
689 inWindowResize
= false;
691 if (getHeight() < minimumWindowHeight
) {
692 setHeight(minimumWindowHeight
);
693 inWindowResize
= false;
695 if ((maximumWindowWidth
> 0)
696 && (getWidth() > maximumWindowWidth
)
698 setWidth(maximumWindowWidth
);
699 inWindowResize
= false;
701 if ((maximumWindowHeight
> 0)
702 && (getHeight() > maximumWindowHeight
)
704 setHeight(maximumWindowHeight
);
705 inWindowResize
= false;
708 // Pass a resize event to my children
709 onResize(new TResizeEvent(TResizeEvent
.Type
.WIDGET
,
710 getWidth(), getHeight()));
714 // I didn't take it, pass it on to my children
715 super.onMouseMotion(mouse
);
721 * @param keypress keystroke event
724 public void onKeypress(final TKeypressEvent keypress
) {
726 if (inKeyboardResize
) {
728 // ESC - Exit size/move
729 if (keypress
.equals(kbEsc
)) {
730 inKeyboardResize
= false;
733 if (keypress
.equals(kbLeft
)) {
738 if (keypress
.equals(kbRight
)) {
739 if (getX() < getScreen().getWidth() - 1) {
743 if (keypress
.equals(kbDown
)) {
744 if (getY() < application
.getDesktopBottom() - 1) {
748 if (keypress
.equals(kbUp
)) {
753 if (keypress
.equals(kbShiftLeft
)) {
754 if (getWidth() > minimumWindowWidth
) {
755 setWidth(getWidth() - 1);
758 if (keypress
.equals(kbShiftRight
)) {
759 if (getWidth() < maximumWindowWidth
) {
760 setWidth(getWidth() + 1);
763 if (keypress
.equals(kbShiftUp
)) {
764 if (getHeight() > minimumWindowHeight
) {
765 setHeight(getHeight() - 1);
768 if (keypress
.equals(kbShiftDown
)) {
769 if (getHeight() < maximumWindowHeight
) {
770 setHeight(getHeight() + 1);
777 // These keystrokes will typically not be seen unless a subclass
778 // overrides onMenu() due to how TApplication dispatches
781 // Ctrl-W - close window
782 if (keypress
.equals(kbCtrlW
)) {
783 application
.closeWindow(this);
787 // F6 - behave like Alt-TAB
788 if (keypress
.equals(kbF6
)) {
789 application
.switchWindow(true);
793 // Shift-F6 - behave like Shift-Alt-TAB
794 if (keypress
.equals(kbShiftF6
)) {
795 application
.switchWindow(false);
800 if (keypress
.equals(kbF5
)) {
808 // Ctrl-F5 - size/move
809 if (keypress
.equals(kbCtrlF5
)) {
810 inKeyboardResize
= !inKeyboardResize
;
813 // I didn't take it, pass it on to my children
814 super.onKeypress(keypress
);
818 * Handle posted command events.
820 * @param command command event
823 public void onCommand(final TCommandEvent command
) {
825 // These commands will typically not be seen unless a subclass
826 // overrides onMenu() due to how TApplication dispatches
829 if (command
.equals(cmWindowClose
)) {
830 application
.closeWindow(this);
834 if (command
.equals(cmWindowNext
)) {
835 application
.switchWindow(true);
839 if (command
.equals(cmWindowPrevious
)) {
840 application
.switchWindow(false);
844 if (command
.equals(cmWindowMove
)) {
845 inKeyboardResize
= true;
849 if (command
.equals(cmWindowZoom
)) {
857 // I didn't take it, pass it on to my children
858 super.onCommand(command
);
862 * Handle posted menu events.
864 * @param menu menu event
867 public void onMenu(final TMenuEvent menu
) {
868 if (menu
.getId() == TMenu
.MID_WINDOW_CLOSE
) {
869 application
.closeWindow(this);
873 if (menu
.getId() == TMenu
.MID_WINDOW_NEXT
) {
874 application
.switchWindow(true);
878 if (menu
.getId() == TMenu
.MID_WINDOW_PREVIOUS
) {
879 application
.switchWindow(false);
883 if (menu
.getId() == TMenu
.MID_WINDOW_MOVE
) {
884 inKeyboardResize
= true;
888 if (menu
.getId() == TMenu
.MID_WINDOW_ZOOM
) {
897 // I didn't take it, pass it on to my children
901 // ------------------------------------------------------------------------
902 // Passthru for Screen functions ------------------------------------------
903 // ------------------------------------------------------------------------
906 * Get the attributes at one location.
908 * @param x column coordinate. 0 is the left-most column.
909 * @param y row coordinate. 0 is the top-most row.
910 * @return attributes at (x, y)
912 public final CellAttributes
getAttrXY(final int x
, final int y
) {
913 return getScreen().getAttrXY(x
, y
);
917 * Set the attributes at one location.
919 * @param x column coordinate. 0 is the left-most column.
920 * @param y row coordinate. 0 is the top-most row.
921 * @param attr attributes to use (bold, foreColor, backColor)
923 public final void putAttrXY(final int x
, final int y
,
924 final CellAttributes attr
) {
926 getScreen().putAttrXY(x
, y
, attr
);
930 * Set the attributes at one location.
932 * @param x column coordinate. 0 is the left-most column.
933 * @param y row coordinate. 0 is the top-most row.
934 * @param attr attributes to use (bold, foreColor, backColor)
935 * @param clip if true, honor clipping/offset
937 public final void putAttrXY(final int x
, final int y
,
938 final CellAttributes attr
, final boolean clip
) {
940 getScreen().putAttrXY(x
, y
, attr
, clip
);
944 * Fill the entire screen with one character with attributes.
946 * @param ch character to draw
947 * @param attr attributes to use (bold, foreColor, backColor)
949 public final void putAll(final char ch
, final CellAttributes attr
) {
950 getScreen().putAll(ch
, attr
);
954 * Render one character with attributes.
956 * @param x column coordinate. 0 is the left-most column.
957 * @param y row coordinate. 0 is the top-most row.
958 * @param ch character + attributes to draw
960 public final void putCharXY(final int x
, final int y
, final Cell ch
) {
961 getScreen().putCharXY(x
, y
, ch
);
965 * Render one character with attributes.
967 * @param x column coordinate. 0 is the left-most column.
968 * @param y row coordinate. 0 is the top-most row.
969 * @param ch character to draw
970 * @param attr attributes to use (bold, foreColor, backColor)
972 public final void putCharXY(final int x
, final int y
, final char ch
,
973 final CellAttributes attr
) {
975 getScreen().putCharXY(x
, y
, ch
, attr
);
979 * Render one character without changing the underlying attributes.
981 * @param x column coordinate. 0 is the left-most column.
982 * @param y row coordinate. 0 is the top-most row.
983 * @param ch character to draw
985 public final void putCharXY(final int x
, final int y
, final char ch
) {
986 getScreen().putCharXY(x
, y
, ch
);
990 * Render a string. Does not wrap if the string exceeds the line.
992 * @param x column coordinate. 0 is the left-most column.
993 * @param y row coordinate. 0 is the top-most row.
994 * @param str string to draw
995 * @param attr attributes to use (bold, foreColor, backColor)
997 public final void putStrXY(final int x
, final int y
, final String str
,
998 final CellAttributes attr
) {
1000 getScreen().putStrXY(x
, y
, str
, attr
);
1004 * Render a string without changing the underlying attribute. Does not
1005 * wrap if the string exceeds the line.
1007 * @param x column coordinate. 0 is the left-most column.
1008 * @param y row coordinate. 0 is the top-most row.
1009 * @param str string to draw
1011 public final void putStrXY(final int x
, final int y
, final String str
) {
1012 getScreen().putStrXY(x
, y
, str
);
1016 * Draw a vertical line from (x, y) to (x, y + n).
1018 * @param x column coordinate. 0 is the left-most column.
1019 * @param y row coordinate. 0 is the top-most row.
1020 * @param n number of characters to draw
1021 * @param ch character to draw
1022 * @param attr attributes to use (bold, foreColor, backColor)
1024 public final void vLineXY(final int x
, final int y
, final int n
,
1025 final char ch
, final CellAttributes attr
) {
1027 getScreen().vLineXY(x
, y
, n
, ch
, attr
);
1031 * Draw a horizontal line from (x, y) to (x + n, y).
1033 * @param x column coordinate. 0 is the left-most column.
1034 * @param y row coordinate. 0 is the top-most row.
1035 * @param n number of characters to draw
1036 * @param ch character to draw
1037 * @param attr attributes to use (bold, foreColor, backColor)
1039 public final void hLineXY(final int x
, final int y
, final int n
,
1040 final char ch
, final CellAttributes attr
) {
1042 getScreen().hLineXY(x
, y
, n
, ch
, attr
);