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
implements Comparable
<TWindow
> {
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 * Public constructor. Window will be located at (0, 0).
193 * @param application TApplication that manages this window
194 * @param title window title, will be centered along the top border
195 * @param width width of window
196 * @param height height of window
198 public TWindow(final TApplication application
, final String title
,
199 final int width
, final int height
) {
201 this(application
, title
, 0, 0, width
, height
, RESIZABLE
);
205 * Public constructor. Window will be located at (0, 0).
207 * @param application TApplication that manages this window
208 * @param title window title, will be centered along the top border
209 * @param width width of window
210 * @param height height of window
211 * @param flags bitmask of RESIZABLE, CENTERED, or MODAL
213 public TWindow(final TApplication application
, final String title
,
214 final int width
, final int height
, final int flags
) {
216 this(application
, title
, 0, 0, width
, height
, flags
);
220 * Public constructor.
222 * @param application TApplication that manages this window
223 * @param title window title, will be centered along the top border
224 * @param x column relative to parent
225 * @param y row relative to parent
226 * @param width width of window
227 * @param height height of window
229 public TWindow(final TApplication application
, final String title
,
230 final int x
, final int y
, final int width
, final int height
) {
232 this(application
, title
, x
, y
, width
, height
, RESIZABLE
);
236 * Public constructor.
238 * @param application TApplication that manages this window
239 * @param title window title, will be centered along the top border
240 * @param x column relative to parent
241 * @param y row relative to parent
242 * @param width width of window
243 * @param height height of window
244 * @param flags mask of RESIZABLE, CENTERED, or MODAL
246 public TWindow(final TApplication application
, final String title
,
247 final int x
, final int y
, final int width
, final int height
,
252 // I am my own window and parent
253 setupForTWindow(this, x
, y
+ application
.getDesktopTop(),
258 this.application
= application
;
261 // Minimum width/height are 10 and 2
262 assert (width
>= 10);
263 assert (getHeight() >= 2);
265 // MODAL implies CENTERED
267 this.flags
|= CENTERED
;
270 // Center window if specified
273 // Add me to the application
274 application
.addWindow(this);
278 * Recenter the window on-screen.
280 public final void center() {
281 if ((flags
& CENTERED
) != 0) {
282 if (getWidth() < getScreen().getWidth()) {
283 setX((getScreen().getWidth() - getWidth()) / 2);
287 setY(((application
.getDesktopBottom()
288 - application
.getDesktopTop()) - getHeight()) / 2);
292 setY(getY() + application
.getDesktopTop());
297 * Returns true if this window is modal.
299 * @return true if this window is modal
301 public final boolean isModal() {
302 if ((flags
& MODAL
) == 0) {
309 * Comparison operator sorts on z.
311 * @param that another TWindow instance
312 * @return difference between this.z and that.z
315 public final int compareTo(final TWindow that
) {
316 return (this.z
- that
.z
);
320 * Returns true if the mouse is currently on the close button.
322 * @return true if mouse is currently on the close button
324 private boolean mouseOnClose() {
326 && (mouse
.getAbsoluteY() == getY())
327 && (mouse
.getAbsoluteX() == getX() + 3)
335 * Returns true if the mouse is currently on the maximize/restore button.
337 * @return true if the mouse is currently on the maximize/restore button
339 private boolean mouseOnMaximize() {
342 && (mouse
.getAbsoluteY() == getY())
343 && (mouse
.getAbsoluteX() == getX() + getWidth() - 4)
351 * Returns true if the mouse is currently on the resizable lower right
354 * @return true if the mouse is currently on the resizable lower right
357 private boolean mouseOnResize() {
358 if (((flags
& RESIZABLE
) != 0)
361 && (mouse
.getAbsoluteY() == getY() + getHeight() - 1)
362 && ((mouse
.getAbsoluteX() == getX() + getWidth() - 1)
363 || (mouse
.getAbsoluteX() == getX() + getWidth() - 2))
371 * Retrieve the background color.
373 * @return the background color
375 public final CellAttributes
getBackground() {
377 && (inWindowMove
|| inWindowResize
|| inKeyboardResize
)
379 assert (getActive());
380 return getTheme().getColor("twindow.background.windowmove");
381 } else if (isModal() && inWindowMove
) {
382 assert (getActive());
383 return getTheme().getColor("twindow.background.modal");
384 } else if (isModal()) {
386 return getTheme().getColor("twindow.background.modal");
388 return getTheme().getColor("twindow.background.modal.inactive");
389 } else if (getActive()) {
391 return getTheme().getColor("twindow.background");
394 return getTheme().getColor("twindow.background.inactive");
399 * Retrieve the border color.
401 * @return the border color
403 private CellAttributes
getBorder() {
405 && (inWindowMove
|| inWindowResize
|| inKeyboardResize
)
407 assert (getActive());
408 return getTheme().getColor("twindow.border.windowmove");
409 } else if (isModal() && inWindowMove
) {
410 assert (getActive());
411 return getTheme().getColor("twindow.border.modal.windowmove");
412 } else if (isModal()) {
414 return getTheme().getColor("twindow.border.modal");
416 return getTheme().getColor("twindow.border.modal.inactive");
418 } else if (getActive()) {
420 return getTheme().getColor("twindow.border");
423 return getTheme().getColor("twindow.border.inactive");
428 * Retrieve the border line type.
430 * @return the border line type
432 private int getBorderType() {
434 && (inWindowMove
|| inWindowResize
|| inKeyboardResize
)
436 assert (getActive());
438 } else if (isModal() && inWindowMove
) {
439 assert (getActive());
441 } else if (isModal()) {
447 } else if (getActive()) {
455 * Subclasses should override this method to cleanup resources. This is
456 * called by application.closeWindow().
458 public void onClose() {
459 // Default: do nothing
463 * Called by TApplication.drawChildren() to render on screen.
467 // Draw the box and background first.
468 CellAttributes border
= getBorder();
469 CellAttributes background
= getBackground();
470 int borderType
= getBorderType();
472 getScreen().drawBox(0, 0, getWidth(), getHeight(), border
,
473 background
, borderType
, true);
476 int titleLeft
= (getWidth() - title
.length() - 2) / 2;
477 putCharXY(titleLeft
, 0, ' ', border
);
478 putStrXY(titleLeft
+ 1, 0, title
);
479 putCharXY(titleLeft
+ title
.length() + 1, 0, ' ', border
);
483 // Draw the close button
484 putCharXY(2, 0, '[', border
);
485 putCharXY(4, 0, ']', border
);
486 if (mouseOnClose() && mouse
.getMouse1()) {
487 putCharXY(3, 0, GraphicsChars
.CP437
[0x0F],
489 ?
getTheme().getColor("twindow.border.windowmove")
490 : getTheme().getColor("twindow.border.modal.windowmove"));
492 putCharXY(3, 0, GraphicsChars
.CP437
[0xFE],
494 ?
getTheme().getColor("twindow.border.windowmove")
495 : getTheme().getColor("twindow.border.modal.windowmove"));
498 // Draw the maximize button
501 putCharXY(getWidth() - 5, 0, '[', border
);
502 putCharXY(getWidth() - 3, 0, ']', border
);
503 if (mouseOnMaximize() && mouse
.getMouse1()) {
504 putCharXY(getWidth() - 4, 0, GraphicsChars
.CP437
[0x0F],
505 getTheme().getColor("twindow.border.windowmove"));
508 putCharXY(getWidth() - 4, 0, GraphicsChars
.CP437
[0x12],
509 getTheme().getColor("twindow.border.windowmove"));
511 putCharXY(getWidth() - 4, 0, GraphicsChars
.UPARROW
,
512 getTheme().getColor("twindow.border.windowmove"));
516 // Draw the resize corner
517 if ((flags
& RESIZABLE
) != 0) {
518 putCharXY(getWidth() - 2, getHeight() - 1,
519 GraphicsChars
.SINGLE_BAR
,
520 getTheme().getColor("twindow.border.windowmove"));
521 putCharXY(getWidth() - 1, getHeight() - 1,
522 GraphicsChars
.LRCORNER
,
523 getTheme().getColor("twindow.border.windowmove"));
530 * Handle mouse button presses.
532 * @param mouse mouse button event
535 public void onMouseDown(final TMouseEvent mouse
) {
537 application
.setRepaint();
539 inKeyboardResize
= false;
541 if ((mouse
.getAbsoluteY() == getY())
543 && (getX() <= mouse
.getAbsoluteX())
544 && (mouse
.getAbsoluteX() < getX() + getWidth())
546 && !mouseOnMaximize()
548 // Begin moving window
550 moveWindowMouseX
= mouse
.getAbsoluteX();
551 moveWindowMouseY
= mouse
.getAbsoluteY();
559 if (mouseOnResize()) {
560 // Begin window resize
561 inWindowResize
= true;
562 moveWindowMouseX
= mouse
.getAbsoluteX();
563 moveWindowMouseY
= mouse
.getAbsoluteY();
564 resizeWindowWidth
= getWidth();
565 resizeWindowHeight
= getHeight();
572 // I didn't take it, pass it on to my children
573 super.onMouseDown(mouse
);
579 private void maximize() {
580 restoreWindowWidth
= getWidth();
581 restoreWindowHeight
= getHeight();
582 restoreWindowX
= getX();
583 restoreWindowY
= getY();
584 setWidth(getScreen().getWidth());
585 setHeight(application
.getDesktopBottom() - 1);
592 * Restote (unmaximize) window.
594 private void restore() {
595 setWidth(restoreWindowWidth
);
596 setHeight(restoreWindowHeight
);
597 setX(restoreWindowX
);
598 setY(restoreWindowY
);
603 * Handle mouse button releases.
605 * @param mouse mouse button release event
608 public void onMouseUp(final TMouseEvent mouse
) {
610 application
.setRepaint();
612 if ((inWindowMove
) && (mouse
.getMouse1())) {
613 // Stop moving window
614 inWindowMove
= false;
618 if ((inWindowResize
) && (mouse
.getMouse1())) {
619 // Stop resizing window
620 inWindowResize
= false;
624 if (mouse
.getMouse1() && mouseOnClose()) {
626 application
.closeWindow(this);
630 if ((mouse
.getAbsoluteY() == getY())
632 && mouseOnMaximize()) {
640 // Pass a resize event to my children
641 onResize(new TResizeEvent(TResizeEvent
.Type
.WIDGET
,
642 getWidth(), getHeight()));
646 // I didn't take it, pass it on to my children
647 super.onMouseUp(mouse
);
651 * Handle mouse movements.
653 * @param mouse mouse motion event
656 public void onMouseMotion(final TMouseEvent mouse
) {
658 application
.setRepaint();
662 setX(oldWindowX
+ (mouse
.getAbsoluteX() - moveWindowMouseX
));
663 setY(oldWindowY
+ (mouse
.getAbsoluteY() - moveWindowMouseY
));
664 // Don't cover up the menu bar
665 if (getY() < application
.getDesktopTop()) {
666 setY(application
.getDesktopTop());
671 if (inWindowResize
) {
673 setWidth(resizeWindowWidth
+ (mouse
.getAbsoluteX()
674 - moveWindowMouseX
));
675 setHeight(resizeWindowHeight
+ (mouse
.getAbsoluteY()
676 - moveWindowMouseY
));
677 if (getX() + getWidth() > getScreen().getWidth()) {
678 setWidth(getScreen().getWidth() - getX());
680 if (getY() + getHeight() > application
.getDesktopBottom()) {
681 setY(application
.getDesktopBottom() - getHeight() + 1);
683 // Don't cover up the menu bar
684 if (getY() < application
.getDesktopTop()) {
685 setY(application
.getDesktopTop());
688 // Keep within min/max bounds
689 if (getWidth() < minimumWindowWidth
) {
690 setWidth(minimumWindowWidth
);
691 inWindowResize
= false;
693 if (getHeight() < minimumWindowHeight
) {
694 setHeight(minimumWindowHeight
);
695 inWindowResize
= false;
697 if ((maximumWindowWidth
> 0)
698 && (getWidth() > maximumWindowWidth
)
700 setWidth(maximumWindowWidth
);
701 inWindowResize
= false;
703 if ((maximumWindowHeight
> 0)
704 && (getHeight() > maximumWindowHeight
)
706 setHeight(maximumWindowHeight
);
707 inWindowResize
= false;
710 // Pass a resize event to my children
711 onResize(new TResizeEvent(TResizeEvent
.Type
.WIDGET
,
712 getWidth(), getHeight()));
716 // I didn't take it, pass it on to my children
717 super.onMouseMotion(mouse
);
723 * @param keypress keystroke event
726 public void onKeypress(final TKeypressEvent keypress
) {
728 if (inKeyboardResize
) {
730 // ESC - Exit size/move
731 if (keypress
.equals(kbEsc
)) {
732 inKeyboardResize
= false;
735 if (keypress
.equals(kbLeft
)) {
740 if (keypress
.equals(kbRight
)) {
741 if (getX() < getScreen().getWidth() - 1) {
745 if (keypress
.equals(kbDown
)) {
746 if (getY() < application
.getDesktopBottom() - 1) {
750 if (keypress
.equals(kbUp
)) {
755 if (keypress
.equals(kbShiftLeft
)) {
756 if (getWidth() > minimumWindowWidth
) {
757 setWidth(getWidth() - 1);
760 if (keypress
.equals(kbShiftRight
)) {
761 if (getWidth() < maximumWindowWidth
) {
762 setWidth(getWidth() + 1);
765 if (keypress
.equals(kbShiftUp
)) {
766 if (getHeight() > minimumWindowHeight
) {
767 setHeight(getHeight() - 1);
770 if (keypress
.equals(kbShiftDown
)) {
771 if (getHeight() < maximumWindowHeight
) {
772 setHeight(getHeight() + 1);
779 // These keystrokes will typically not be seen unless a subclass
780 // overrides onMenu() due to how TApplication dispatches
783 // Ctrl-W - close window
784 if (keypress
.equals(kbCtrlW
)) {
785 application
.closeWindow(this);
789 // F6 - behave like Alt-TAB
790 if (keypress
.equals(kbF6
)) {
791 application
.switchWindow(true);
795 // Shift-F6 - behave like Shift-Alt-TAB
796 if (keypress
.equals(kbShiftF6
)) {
797 application
.switchWindow(false);
802 if (keypress
.equals(kbF5
)) {
810 // Ctrl-F5 - size/move
811 if (keypress
.equals(kbCtrlF5
)) {
812 inKeyboardResize
= !inKeyboardResize
;
815 // I didn't take it, pass it on to my children
816 super.onKeypress(keypress
);
820 * Handle posted command events.
822 * @param command command event
825 public void onCommand(final TCommandEvent command
) {
827 // These commands will typically not be seen unless a subclass
828 // overrides onMenu() due to how TApplication dispatches
831 if (command
.equals(cmWindowClose
)) {
832 application
.closeWindow(this);
836 if (command
.equals(cmWindowNext
)) {
837 application
.switchWindow(true);
841 if (command
.equals(cmWindowPrevious
)) {
842 application
.switchWindow(false);
846 if (command
.equals(cmWindowMove
)) {
847 inKeyboardResize
= true;
851 if (command
.equals(cmWindowZoom
)) {
859 // I didn't take it, pass it on to my children
860 super.onCommand(command
);
864 * Handle posted menu events.
866 * @param menu menu event
869 public void onMenu(final TMenuEvent menu
) {
870 if (menu
.getId() == TMenu
.MID_WINDOW_CLOSE
) {
871 application
.closeWindow(this);
875 if (menu
.getId() == TMenu
.MID_WINDOW_NEXT
) {
876 application
.switchWindow(true);
880 if (menu
.getId() == TMenu
.MID_WINDOW_PREVIOUS
) {
881 application
.switchWindow(false);
885 if (menu
.getId() == TMenu
.MID_WINDOW_MOVE
) {
886 inKeyboardResize
= true;
890 if (menu
.getId() == TMenu
.MID_WINDOW_ZOOM
) {
899 // I didn't take it, pass it on to my children
903 // ------------------------------------------------------------------------
904 // Passthru for Screen functions ------------------------------------------
905 // ------------------------------------------------------------------------
908 * Get the attributes at one location.
910 * @param x column coordinate. 0 is the left-most column.
911 * @param y row coordinate. 0 is the top-most row.
912 * @return attributes at (x, y)
914 public final CellAttributes
getAttrXY(final int x
, final int y
) {
915 return getScreen().getAttrXY(x
, y
);
919 * Set the attributes at one location.
921 * @param x column coordinate. 0 is the left-most column.
922 * @param y row coordinate. 0 is the top-most row.
923 * @param attr attributes to use (bold, foreColor, backColor)
925 public final void putAttrXY(final int x
, final int y
,
926 final CellAttributes attr
) {
928 getScreen().putAttrXY(x
, y
, attr
);
932 * Set the attributes at one location.
934 * @param x column coordinate. 0 is the left-most column.
935 * @param y row coordinate. 0 is the top-most row.
936 * @param attr attributes to use (bold, foreColor, backColor)
937 * @param clip if true, honor clipping/offset
939 public final void putAttrXY(final int x
, final int y
,
940 final CellAttributes attr
, final boolean clip
) {
942 getScreen().putAttrXY(x
, y
, attr
, clip
);
946 * Fill the entire screen with one character with attributes.
948 * @param ch character to draw
949 * @param attr attributes to use (bold, foreColor, backColor)
951 public final void putAll(final char ch
, final CellAttributes attr
) {
952 getScreen().putAll(ch
, attr
);
956 * Render one character with attributes.
958 * @param x column coordinate. 0 is the left-most column.
959 * @param y row coordinate. 0 is the top-most row.
960 * @param ch character + attributes to draw
962 public final void putCharXY(final int x
, final int y
, final Cell ch
) {
963 getScreen().putCharXY(x
, y
, ch
);
967 * Render one character with attributes.
969 * @param x column coordinate. 0 is the left-most column.
970 * @param y row coordinate. 0 is the top-most row.
971 * @param ch character to draw
972 * @param attr attributes to use (bold, foreColor, backColor)
974 public final void putCharXY(final int x
, final int y
, final char ch
,
975 final CellAttributes attr
) {
977 getScreen().putCharXY(x
, y
, ch
, attr
);
981 * Render one character without changing the underlying attributes.
983 * @param x column coordinate. 0 is the left-most column.
984 * @param y row coordinate. 0 is the top-most row.
985 * @param ch character to draw
987 public final void putCharXY(final int x
, final int y
, final char ch
) {
988 getScreen().putCharXY(x
, y
, ch
);
992 * Render a string. Does not wrap if the string exceeds the line.
994 * @param x column coordinate. 0 is the left-most column.
995 * @param y row coordinate. 0 is the top-most row.
996 * @param str string to draw
997 * @param attr attributes to use (bold, foreColor, backColor)
999 public final void putStrXY(final int x
, final int y
, final String str
,
1000 final CellAttributes attr
) {
1002 getScreen().putStrXY(x
, y
, str
, attr
);
1006 * Render a string without changing the underlying attribute. Does not
1007 * wrap if the string exceeds the line.
1009 * @param x column coordinate. 0 is the left-most column.
1010 * @param y row coordinate. 0 is the top-most row.
1011 * @param str string to draw
1013 public final void putStrXY(final int x
, final int y
, final String str
) {
1014 getScreen().putStrXY(x
, y
, str
);
1018 * Draw a vertical line from (x, y) to (x, y + n).
1020 * @param x column coordinate. 0 is the left-most column.
1021 * @param y row coordinate. 0 is the top-most row.
1022 * @param n number of characters to draw
1023 * @param ch character to draw
1024 * @param attr attributes to use (bold, foreColor, backColor)
1026 public final void vLineXY(final int x
, final int y
, final int n
,
1027 final char ch
, final CellAttributes attr
) {
1029 getScreen().vLineXY(x
, y
, n
, ch
, attr
);
1033 * Draw a horizontal line from (x, y) to (x + n, y).
1035 * @param x column coordinate. 0 is the left-most column.
1036 * @param y row coordinate. 0 is the top-most row.
1037 * @param n number of characters to draw
1038 * @param ch character to draw
1039 * @param attr attributes to use (bold, foreColor, backColor)
1041 public final void hLineXY(final int x
, final int y
, final int n
,
1042 final char ch
, final CellAttributes attr
) {
1044 getScreen().hLineXY(x
, y
, n
, ch
, attr
);