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 * 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 * Returns true if the mouse is currently on the close button.
311 * @return true if mouse is currently on the close button
313 private boolean mouseOnClose() {
315 && (mouse
.getAbsoluteY() == getY())
316 && (mouse
.getAbsoluteX() == getX() + 3)
324 * Returns true if the mouse is currently on the maximize/restore button.
326 * @return true if the mouse is currently on the maximize/restore button
328 private boolean mouseOnMaximize() {
331 && (mouse
.getAbsoluteY() == getY())
332 && (mouse
.getAbsoluteX() == getX() + getWidth() - 4)
340 * Returns true if the mouse is currently on the resizable lower right
343 * @return true if the mouse is currently on the resizable lower right
346 private boolean mouseOnResize() {
347 if (((flags
& RESIZABLE
) != 0)
350 && (mouse
.getAbsoluteY() == getY() + getHeight() - 1)
351 && ((mouse
.getAbsoluteX() == getX() + getWidth() - 1)
352 || (mouse
.getAbsoluteX() == getX() + getWidth() - 2))
360 * Retrieve the background color.
362 * @return the background color
364 public final CellAttributes
getBackground() {
366 && (inWindowMove
|| inWindowResize
|| inKeyboardResize
)
368 assert (getActive());
369 return getTheme().getColor("twindow.background.windowmove");
370 } else if (isModal() && inWindowMove
) {
371 assert (getActive());
372 return getTheme().getColor("twindow.background.modal");
373 } else if (isModal()) {
375 return getTheme().getColor("twindow.background.modal");
377 return getTheme().getColor("twindow.background.modal.inactive");
378 } else if (getActive()) {
380 return getTheme().getColor("twindow.background");
383 return getTheme().getColor("twindow.background.inactive");
388 * Retrieve the border color.
390 * @return the border color
392 private CellAttributes
getBorder() {
394 && (inWindowMove
|| inWindowResize
|| inKeyboardResize
)
396 assert (getActive());
397 return getTheme().getColor("twindow.border.windowmove");
398 } else if (isModal() && inWindowMove
) {
399 assert (getActive());
400 return getTheme().getColor("twindow.border.modal.windowmove");
401 } else if (isModal()) {
403 return getTheme().getColor("twindow.border.modal");
405 return getTheme().getColor("twindow.border.modal.inactive");
407 } else if (getActive()) {
409 return getTheme().getColor("twindow.border");
412 return getTheme().getColor("twindow.border.inactive");
417 * Retrieve the border line type.
419 * @return the border line type
421 private int getBorderType() {
423 && (inWindowMove
|| inWindowResize
|| inKeyboardResize
)
425 assert (getActive());
427 } else if (isModal() && inWindowMove
) {
428 assert (getActive());
430 } else if (isModal()) {
436 } else if (getActive()) {
444 * Subclasses should override this method to cleanup resources. This is
445 * called by application.closeWindow().
447 public void onClose() {
448 // Default: do nothing
452 * Called by TApplication.drawChildren() to render on screen.
456 // Draw the box and background first.
457 CellAttributes border
= getBorder();
458 CellAttributes background
= getBackground();
459 int borderType
= getBorderType();
461 getScreen().drawBox(0, 0, getWidth(), getHeight(), border
,
462 background
, borderType
, true);
465 int titleLeft
= (getWidth() - title
.length() - 2) / 2;
466 putCharXY(titleLeft
, 0, ' ', border
);
467 putStrXY(titleLeft
+ 1, 0, title
);
468 putCharXY(titleLeft
+ title
.length() + 1, 0, ' ', border
);
472 // Draw the close button
473 putCharXY(2, 0, '[', border
);
474 putCharXY(4, 0, ']', border
);
475 if (mouseOnClose() && mouse
.getMouse1()) {
476 putCharXY(3, 0, GraphicsChars
.CP437
[0x0F],
478 ?
getTheme().getColor("twindow.border.windowmove")
479 : getTheme().getColor("twindow.border.modal.windowmove"));
481 putCharXY(3, 0, GraphicsChars
.CP437
[0xFE],
483 ?
getTheme().getColor("twindow.border.windowmove")
484 : getTheme().getColor("twindow.border.modal.windowmove"));
487 // Draw the maximize button
490 putCharXY(getWidth() - 5, 0, '[', border
);
491 putCharXY(getWidth() - 3, 0, ']', border
);
492 if (mouseOnMaximize() && mouse
.getMouse1()) {
493 putCharXY(getWidth() - 4, 0, GraphicsChars
.CP437
[0x0F],
494 getTheme().getColor("twindow.border.windowmove"));
497 putCharXY(getWidth() - 4, 0, GraphicsChars
.CP437
[0x12],
498 getTheme().getColor("twindow.border.windowmove"));
500 putCharXY(getWidth() - 4, 0, GraphicsChars
.UPARROW
,
501 getTheme().getColor("twindow.border.windowmove"));
505 // Draw the resize corner
506 if ((flags
& RESIZABLE
) != 0) {
507 putCharXY(getWidth() - 2, getHeight() - 1,
508 GraphicsChars
.SINGLE_BAR
,
509 getTheme().getColor("twindow.border.windowmove"));
510 putCharXY(getWidth() - 1, getHeight() - 1,
511 GraphicsChars
.LRCORNER
,
512 getTheme().getColor("twindow.border.windowmove"));
519 * Handle mouse button presses.
521 * @param mouse mouse button event
524 public void onMouseDown(final TMouseEvent mouse
) {
526 application
.setRepaint();
528 inKeyboardResize
= false;
530 if ((mouse
.getAbsoluteY() == getY())
532 && (getX() <= mouse
.getAbsoluteX())
533 && (mouse
.getAbsoluteX() < getX() + getWidth())
535 && !mouseOnMaximize()
537 // Begin moving window
539 moveWindowMouseX
= mouse
.getAbsoluteX();
540 moveWindowMouseY
= mouse
.getAbsoluteY();
548 if (mouseOnResize()) {
549 // Begin window resize
550 inWindowResize
= true;
551 moveWindowMouseX
= mouse
.getAbsoluteX();
552 moveWindowMouseY
= mouse
.getAbsoluteY();
553 resizeWindowWidth
= getWidth();
554 resizeWindowHeight
= getHeight();
561 // I didn't take it, pass it on to my children
562 super.onMouseDown(mouse
);
568 private void maximize() {
569 restoreWindowWidth
= getWidth();
570 restoreWindowHeight
= getHeight();
571 restoreWindowX
= getX();
572 restoreWindowY
= getY();
573 setWidth(getScreen().getWidth());
574 setHeight(application
.getDesktopBottom() - 1);
581 * Restote (unmaximize) window.
583 private void restore() {
584 setWidth(restoreWindowWidth
);
585 setHeight(restoreWindowHeight
);
586 setX(restoreWindowX
);
587 setY(restoreWindowY
);
592 * Handle mouse button releases.
594 * @param mouse mouse button release event
597 public void onMouseUp(final TMouseEvent mouse
) {
599 application
.setRepaint();
601 if ((inWindowMove
) && (mouse
.getMouse1())) {
602 // Stop moving window
603 inWindowMove
= false;
607 if ((inWindowResize
) && (mouse
.getMouse1())) {
608 // Stop resizing window
609 inWindowResize
= false;
613 if (mouse
.getMouse1() && mouseOnClose()) {
615 application
.closeWindow(this);
619 if ((mouse
.getAbsoluteY() == getY())
621 && mouseOnMaximize()) {
629 // Pass a resize event to my children
630 onResize(new TResizeEvent(TResizeEvent
.Type
.WIDGET
,
631 getWidth(), getHeight()));
635 // I didn't take it, pass it on to my children
636 super.onMouseUp(mouse
);
640 * Handle mouse movements.
642 * @param mouse mouse motion event
645 public void onMouseMotion(final TMouseEvent mouse
) {
647 application
.setRepaint();
651 setX(oldWindowX
+ (mouse
.getAbsoluteX() - moveWindowMouseX
));
652 setY(oldWindowY
+ (mouse
.getAbsoluteY() - moveWindowMouseY
));
653 // Don't cover up the menu bar
654 if (getY() < application
.getDesktopTop()) {
655 setY(application
.getDesktopTop());
660 if (inWindowResize
) {
662 setWidth(resizeWindowWidth
+ (mouse
.getAbsoluteX()
663 - moveWindowMouseX
));
664 setHeight(resizeWindowHeight
+ (mouse
.getAbsoluteY()
665 - moveWindowMouseY
));
666 if (getX() + getWidth() > getScreen().getWidth()) {
667 setWidth(getScreen().getWidth() - getX());
669 if (getY() + getHeight() > application
.getDesktopBottom()) {
670 setY(application
.getDesktopBottom() - getHeight() + 1);
672 // Don't cover up the menu bar
673 if (getY() < application
.getDesktopTop()) {
674 setY(application
.getDesktopTop());
677 // Keep within min/max bounds
678 if (getWidth() < minimumWindowWidth
) {
679 setWidth(minimumWindowWidth
);
680 inWindowResize
= false;
682 if (getHeight() < minimumWindowHeight
) {
683 setHeight(minimumWindowHeight
);
684 inWindowResize
= false;
686 if ((maximumWindowWidth
> 0)
687 && (getWidth() > maximumWindowWidth
)
689 setWidth(maximumWindowWidth
);
690 inWindowResize
= false;
692 if ((maximumWindowHeight
> 0)
693 && (getHeight() > maximumWindowHeight
)
695 setHeight(maximumWindowHeight
);
696 inWindowResize
= false;
699 // Pass a resize event to my children
700 onResize(new TResizeEvent(TResizeEvent
.Type
.WIDGET
,
701 getWidth(), getHeight()));
705 // I didn't take it, pass it on to my children
706 super.onMouseMotion(mouse
);
712 * @param keypress keystroke event
715 public void onKeypress(final TKeypressEvent keypress
) {
717 if (inKeyboardResize
) {
719 // ESC - Exit size/move
720 if (keypress
.equals(kbEsc
)) {
721 inKeyboardResize
= false;
724 if (keypress
.equals(kbLeft
)) {
729 if (keypress
.equals(kbRight
)) {
730 if (getX() < getScreen().getWidth() - 1) {
734 if (keypress
.equals(kbDown
)) {
735 if (getY() < application
.getDesktopBottom() - 1) {
739 if (keypress
.equals(kbUp
)) {
744 if (keypress
.equals(kbShiftLeft
)) {
745 if (getWidth() > minimumWindowWidth
) {
746 setWidth(getWidth() - 1);
749 if (keypress
.equals(kbShiftRight
)) {
750 if (getWidth() < maximumWindowWidth
) {
751 setWidth(getWidth() + 1);
754 if (keypress
.equals(kbShiftUp
)) {
755 if (getHeight() > minimumWindowHeight
) {
756 setHeight(getHeight() - 1);
759 if (keypress
.equals(kbShiftDown
)) {
760 if (getHeight() < maximumWindowHeight
) {
761 setHeight(getHeight() + 1);
768 // These keystrokes will typically not be seen unless a subclass
769 // overrides onMenu() due to how TApplication dispatches
772 // Ctrl-W - close window
773 if (keypress
.equals(kbCtrlW
)) {
774 application
.closeWindow(this);
778 // F6 - behave like Alt-TAB
779 if (keypress
.equals(kbF6
)) {
780 application
.switchWindow(true);
784 // Shift-F6 - behave like Shift-Alt-TAB
785 if (keypress
.equals(kbShiftF6
)) {
786 application
.switchWindow(false);
791 if (keypress
.equals(kbF5
)) {
799 // Ctrl-F5 - size/move
800 if (keypress
.equals(kbCtrlF5
)) {
801 inKeyboardResize
= !inKeyboardResize
;
804 // I didn't take it, pass it on to my children
805 super.onKeypress(keypress
);
809 * Handle posted command events.
811 * @param command command event
814 public void onCommand(final TCommandEvent command
) {
816 // These commands will typically not be seen unless a subclass
817 // overrides onMenu() due to how TApplication dispatches
820 if (command
.equals(cmWindowClose
)) {
821 application
.closeWindow(this);
825 if (command
.equals(cmWindowNext
)) {
826 application
.switchWindow(true);
830 if (command
.equals(cmWindowPrevious
)) {
831 application
.switchWindow(false);
835 if (command
.equals(cmWindowMove
)) {
836 inKeyboardResize
= true;
840 if (command
.equals(cmWindowZoom
)) {
848 // I didn't take it, pass it on to my children
849 super.onCommand(command
);
853 * Handle posted menu events.
855 * @param menu menu event
858 public void onMenu(final TMenuEvent menu
) {
859 if (menu
.getId() == TMenu
.MID_WINDOW_CLOSE
) {
860 application
.closeWindow(this);
864 if (menu
.getId() == TMenu
.MID_WINDOW_NEXT
) {
865 application
.switchWindow(true);
869 if (menu
.getId() == TMenu
.MID_WINDOW_PREVIOUS
) {
870 application
.switchWindow(false);
874 if (menu
.getId() == TMenu
.MID_WINDOW_MOVE
) {
875 inKeyboardResize
= true;
879 if (menu
.getId() == TMenu
.MID_WINDOW_ZOOM
) {
888 // I didn't take it, pass it on to my children
892 // ------------------------------------------------------------------------
893 // Passthru for Screen functions ------------------------------------------
894 // ------------------------------------------------------------------------
897 * Get the attributes at one location.
899 * @param x column coordinate. 0 is the left-most column.
900 * @param y row coordinate. 0 is the top-most row.
901 * @return attributes at (x, y)
903 public final CellAttributes
getAttrXY(final int x
, final int y
) {
904 return getScreen().getAttrXY(x
, y
);
908 * Set 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 * @param attr attributes to use (bold, foreColor, backColor)
914 public final void putAttrXY(final int x
, final int y
,
915 final CellAttributes attr
) {
917 getScreen().putAttrXY(x
, y
, attr
);
921 * Set the attributes at one location.
923 * @param x column coordinate. 0 is the left-most column.
924 * @param y row coordinate. 0 is the top-most row.
925 * @param attr attributes to use (bold, foreColor, backColor)
926 * @param clip if true, honor clipping/offset
928 public final void putAttrXY(final int x
, final int y
,
929 final CellAttributes attr
, final boolean clip
) {
931 getScreen().putAttrXY(x
, y
, attr
, clip
);
935 * Fill the entire screen with one character with attributes.
937 * @param ch character to draw
938 * @param attr attributes to use (bold, foreColor, backColor)
940 public final void putAll(final char ch
, final CellAttributes attr
) {
941 getScreen().putAll(ch
, attr
);
945 * Render one character with attributes.
947 * @param x column coordinate. 0 is the left-most column.
948 * @param y row coordinate. 0 is the top-most row.
949 * @param ch character + attributes to draw
951 public final void putCharXY(final int x
, final int y
, final Cell ch
) {
952 getScreen().putCharXY(x
, y
, ch
);
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 to draw
961 * @param attr attributes to use (bold, foreColor, backColor)
963 public final void putCharXY(final int x
, final int y
, final char ch
,
964 final CellAttributes attr
) {
966 getScreen().putCharXY(x
, y
, ch
, attr
);
970 * Render one character without changing the underlying attributes.
972 * @param x column coordinate. 0 is the left-most column.
973 * @param y row coordinate. 0 is the top-most row.
974 * @param ch character to draw
976 public final void putCharXY(final int x
, final int y
, final char ch
) {
977 getScreen().putCharXY(x
, y
, ch
);
981 * Render a string. Does not wrap if the string exceeds the line.
983 * @param x column coordinate. 0 is the left-most column.
984 * @param y row coordinate. 0 is the top-most row.
985 * @param str string to draw
986 * @param attr attributes to use (bold, foreColor, backColor)
988 public final void putStrXY(final int x
, final int y
, final String str
,
989 final CellAttributes attr
) {
991 getScreen().putStrXY(x
, y
, str
, attr
);
995 * Render a string without changing the underlying attribute. Does not
996 * wrap if the string exceeds the line.
998 * @param x column coordinate. 0 is the left-most column.
999 * @param y row coordinate. 0 is the top-most row.
1000 * @param str string to draw
1002 public final void putStrXY(final int x
, final int y
, final String str
) {
1003 getScreen().putStrXY(x
, y
, str
);
1007 * Draw a vertical line from (x, y) to (x, y + n).
1009 * @param x column coordinate. 0 is the left-most column.
1010 * @param y row coordinate. 0 is the top-most row.
1011 * @param n number of characters to draw
1012 * @param ch character to draw
1013 * @param attr attributes to use (bold, foreColor, backColor)
1015 public final void vLineXY(final int x
, final int y
, final int n
,
1016 final char ch
, final CellAttributes attr
) {
1018 getScreen().vLineXY(x
, y
, n
, ch
, attr
);
1022 * Draw a horizontal line from (x, y) to (x + n, y).
1024 * @param x column coordinate. 0 is the left-most column.
1025 * @param y row coordinate. 0 is the top-most row.
1026 * @param n number of characters to draw
1027 * @param ch character to draw
1028 * @param attr attributes to use (bold, foreColor, backColor)
1030 public final void hLineXY(final int x
, final int y
, final int n
,
1031 final char ch
, final CellAttributes attr
) {
1033 getScreen().hLineXY(x
, y
, n
, ch
, attr
);