X-Git-Url: http://git.nikiroo.be/?a=blobdiff_plain;f=src%2Fjexer%2FTWindow.java;h=c7b59c82ab5d9d19a90f843668c6695c03c122b5;hb=3649b9210ea425f398ba8c24f9509669cf72aa96;hp=b850789ac380e9da32198f324cc14a07ce71dd7f;hpb=48e27807150e00bc9a92844382ebc8cedf1d265f;p=fanfix.git diff --git a/src/jexer/TWindow.java b/src/jexer/TWindow.java index b850789..c7b59c8 100644 --- a/src/jexer/TWindow.java +++ b/src/jexer/TWindow.java @@ -1,4 +1,4 @@ -/** +/* * Jexer - Java Text User Interface * * License: LGPLv3 or later @@ -39,6 +39,7 @@ import jexer.event.TMenuEvent; import jexer.event.TMouseEvent; import jexer.event.TResizeEvent; import jexer.io.Screen; +import jexer.menu.TMenu; import static jexer.TCommand.*; import static jexer.TKeypress.*; @@ -50,13 +51,14 @@ public class TWindow extends TWidget { /** * Window's parent TApplication. */ - protected TApplication application; + private TApplication application; /** * Get this TWindow's parent TApplication. * * @return this TWindow's parent TApplication */ + @Override public final TApplication getApplication() { return application; } @@ -66,6 +68,7 @@ public class TWindow extends TWidget { * * @return the Screen */ + @Override public final Screen getScreen() { return application.getScreen(); } @@ -73,7 +76,25 @@ public class TWindow extends TWidget { /** * Window title. */ - protected String title = ""; + private String title = ""; + + /** + * Get window title. + * + * @return window title + */ + public final String getTitle() { + return title; + } + + /** + * Set window title. + * + * @param title new window title + */ + public final void setTitle(final String title) { + this.title = title; + } /** * Window is resizable (default yes). @@ -100,17 +121,35 @@ public class TWindow extends TWidget { */ private int z = 0; + /** + * Get Z order. Lower number means more in-front. + * + * @return Z value. Lower number means more in-front. + */ + public final int getZ() { + return z; + } + + /** + * Set Z order. Lower number means more in-front. + * + * @param z the new Z value. Lower number means more in-front. + */ + public final void setZ(final int z) { + this.z = z; + } + /** * If true, then the user clicked on the title bar and is moving the * window. */ - private boolean inWindowMove = false; + protected boolean inWindowMove = false; /** * If true, then the user clicked on the bottom right corner and is * resizing the window. */ - private boolean inWindowResize = false; + protected boolean inWindowResize = false; /** * If true, then the user selected "Size/Move" (or hit Ctrl-F5) and is @@ -148,6 +187,15 @@ public class TWindow extends TWidget { private int restoreWindowX; private int restoreWindowY; + /** + * Set the maximum width for this window. + * + * @param maximumWindowWidth new maximum width + */ + public final void setMaximumWindowWidth(final int maximumWindowWidth) { + this.maximumWindowWidth = maximumWindowWidth; + } + /** * Public constructor. Window will be located at (0, 0). * @@ -208,22 +256,20 @@ public class TWindow extends TWidget { final int x, final int y, final int width, final int height, final int flags) { + super(); + // I am my own window and parent - this.parent = this; - this.window = this; + setupForTWindow(this, x, y + application.getDesktopTop(), + width, height); // Save fields this.title = title; this.application = application; - this.x = x; - this.y = y + application.getDesktopTop(); - this.width = width; - this.height = height; this.flags = flags; // Minimum width/height are 10 and 2 assert (width >= 10); - assert (height >= 2); + assert (getHeight() >= 2); // MODAL implies CENTERED if (isModal()) { @@ -242,18 +288,17 @@ public class TWindow extends TWidget { */ public final void center() { if ((flags & CENTERED) != 0) { - if (width < getScreen().getWidth()) { - x = (getScreen().getWidth() - width) / 2; + if (getWidth() < getScreen().getWidth()) { + setX((getScreen().getWidth() - getWidth()) / 2); } else { - x = 0; + setX(0); } - y = (application.getDesktopBottom() - application.getDesktopTop()); - y -= height; - y /= 2; - if (y < 0) { - y = 0; + setY(((application.getDesktopBottom() + - application.getDesktopTop()) - getHeight()) / 2); + if (getY() < 0) { + setY(0); } - y += application.getDesktopTop(); + setY(getY() + application.getDesktopTop()); } } @@ -269,16 +314,6 @@ public class TWindow extends TWidget { return true; } - /** - * Comparison operator sorts on z. - * - * @param that another TWindow instance - * @return difference between this.z and that.z - */ - public final int compare(final TWindow that) { - return (z - that.z); - } - /** * Returns true if the mouse is currently on the close button. * @@ -286,8 +321,8 @@ public class TWindow extends TWidget { */ private boolean mouseOnClose() { if ((mouse != null) - && (mouse.getAbsoluteY() == y) - && (mouse.getAbsoluteX() == x + 3) + && (mouse.getAbsoluteY() == getY()) + && (mouse.getAbsoluteX() == getX() + 3) ) { return true; } @@ -302,8 +337,8 @@ public class TWindow extends TWidget { private boolean mouseOnMaximize() { if ((mouse != null) && !isModal() - && (mouse.getAbsoluteY() == y) - && (mouse.getAbsoluteX() == x + width - 4) + && (mouse.getAbsoluteY() == getY()) + && (mouse.getAbsoluteX() == getX() + getWidth() - 4) ) { return true; } @@ -321,9 +356,9 @@ public class TWindow extends TWidget { if (((flags & RESIZABLE) != 0) && !isModal() && (mouse != null) - && (mouse.getAbsoluteY() == y + height - 1) - && ((mouse.getAbsoluteX() == x + width - 1) - || (mouse.getAbsoluteX() == x + width - 2)) + && (mouse.getAbsoluteY() == getY() + getHeight() - 1) + && ((mouse.getAbsoluteX() == getX() + getWidth() - 1) + || (mouse.getAbsoluteX() == getX() + getWidth() - 2)) ) { return true; } @@ -335,26 +370,26 @@ public class TWindow extends TWidget { * * @return the background color */ - protected final CellAttributes getBackground() { + public final CellAttributes getBackground() { if (!isModal() && (inWindowMove || inWindowResize || inKeyboardResize) ) { - assert (active); - return application.getTheme().getColor("twindow.background.windowmove"); + assert (isActive()); + return getTheme().getColor("twindow.background.windowmove"); } else if (isModal() && inWindowMove) { - assert (active); - return application.getTheme().getColor("twindow.background.modal"); + assert (isActive()); + return getTheme().getColor("twindow.background.modal"); } else if (isModal()) { - if (active) { - return application.getTheme().getColor("twindow.background.modal"); + if (isActive()) { + return getTheme().getColor("twindow.background.modal"); } - return application.getTheme().getColor("twindow.background.modal.inactive"); - } else if (active) { + return getTheme().getColor("twindow.background.modal.inactive"); + } else if (isActive()) { assert (!isModal()); - return application.getTheme().getColor("twindow.background"); + return getTheme().getColor("twindow.background"); } else { assert (!isModal()); - return application.getTheme().getColor("twindow.background.inactive"); + return getTheme().getColor("twindow.background.inactive"); } } @@ -363,27 +398,27 @@ public class TWindow extends TWidget { * * @return the border color */ - protected final CellAttributes getBorder() { + public CellAttributes getBorder() { if (!isModal() && (inWindowMove || inWindowResize || inKeyboardResize) ) { - assert (active); - return application.getTheme().getColor("twindow.border.windowmove"); + assert (isActive()); + return getTheme().getColor("twindow.border.windowmove"); } else if (isModal() && inWindowMove) { - assert (active); - return application.getTheme().getColor("twindow.border.modal.windowmove"); + assert (isActive()); + return getTheme().getColor("twindow.border.modal.windowmove"); } else if (isModal()) { - if (active) { - return application.getTheme().getColor("twindow.border.modal"); + if (isActive()) { + return getTheme().getColor("twindow.border.modal"); } else { - return application.getTheme().getColor("twindow.border.modal.inactive"); + return getTheme().getColor("twindow.border.modal.inactive"); } - } else if (active) { + } else if (isActive()) { assert (!isModal()); - return application.getTheme().getColor("twindow.border"); + return getTheme().getColor("twindow.border"); } else { assert (!isModal()); - return application.getTheme().getColor("twindow.border.inactive"); + return getTheme().getColor("twindow.border.inactive"); } } @@ -392,22 +427,22 @@ public class TWindow extends TWidget { * * @return the border line type */ - protected final int getBorderType() { + private int getBorderType() { if (!isModal() && (inWindowMove || inWindowResize || inKeyboardResize) ) { - assert (active); + assert (isActive()); return 1; } else if (isModal() && inWindowMove) { - assert (active); + assert (isActive()); return 1; } else if (isModal()) { - if (active) { + if (isActive()) { return 2; } else { return 1; } - } else if (active) { + } else if (isActive()) { return 2; } else { return 1; @@ -422,6 +457,22 @@ public class TWindow extends TWidget { // Default: do nothing } + /** + * Called by application.switchWindow() when this window gets the + * focus, and also by application.addWindow(). + */ + public void onFocus() { + // Default: do nothing + } + + /** + * Called by application.switchWindow() when another window gets the + * focus. + */ + public void onUnfocus() { + // Default: do nothing + } + /** * Called by TApplication.drawChildren() to render on screen. */ @@ -432,56 +483,58 @@ public class TWindow extends TWidget { CellAttributes background = getBackground(); int borderType = getBorderType(); - getScreen().drawBox(0, 0, width, height, border, + getScreen().drawBox(0, 0, getWidth(), getHeight(), border, background, borderType, true); // Draw the title - int titleLeft = (width - title.length() - 2) / 2; + int titleLeft = (getWidth() - title.length() - 2) / 2; putCharXY(titleLeft, 0, ' ', border); - putStrXY(titleLeft + 1, 0, title); + putStringXY(titleLeft + 1, 0, title); putCharXY(titleLeft + title.length() + 1, 0, ' ', border); - if (active) { + if (isActive()) { // Draw the close button putCharXY(2, 0, '[', border); putCharXY(4, 0, ']', border); - if (mouseOnClose() && mouse.getMouse1()) { + if (mouseOnClose() && mouse.isMouse1()) { putCharXY(3, 0, GraphicsChars.CP437[0x0F], !isModal() - ? application.getTheme().getColor("twindow.border.windowmove") - : application.getTheme().getColor("twindow.border.modal.windowmove")); + ? getTheme().getColor("twindow.border.windowmove") + : getTheme().getColor("twindow.border.modal.windowmove")); } else { putCharXY(3, 0, GraphicsChars.CP437[0xFE], !isModal() - ? application.getTheme().getColor("twindow.border.windowmove") - : application.getTheme().getColor("twindow.border.modal.windowmove")); + ? getTheme().getColor("twindow.border.windowmove") + : getTheme().getColor("twindow.border.modal.windowmove")); } // Draw the maximize button if (!isModal()) { - putCharXY(width - 5, 0, '[', border); - putCharXY(width - 3, 0, ']', border); - if (mouseOnMaximize() && mouse.getMouse1()) { - putCharXY(width - 4, 0, GraphicsChars.CP437[0x0F], - application.getTheme().getColor("twindow.border.windowmove")); + putCharXY(getWidth() - 5, 0, '[', border); + putCharXY(getWidth() - 3, 0, ']', border); + if (mouseOnMaximize() && mouse.isMouse1()) { + putCharXY(getWidth() - 4, 0, GraphicsChars.CP437[0x0F], + getTheme().getColor("twindow.border.windowmove")); } else { if (maximized) { - putCharXY(width - 4, 0, GraphicsChars.CP437[0x12], - application.getTheme().getColor("twindow.border.windowmove")); + putCharXY(getWidth() - 4, 0, GraphicsChars.CP437[0x12], + getTheme().getColor("twindow.border.windowmove")); } else { - putCharXY(width - 4, 0, GraphicsChars.UPARROW, - application.getTheme().getColor("twindow.border.windowmove")); + putCharXY(getWidth() - 4, 0, GraphicsChars.UPARROW, + getTheme().getColor("twindow.border.windowmove")); } } // Draw the resize corner if ((flags & RESIZABLE) != 0) { - putCharXY(width - 2, height - 1, GraphicsChars.SINGLE_BAR, - application.getTheme().getColor("twindow.border.windowmove")); - putCharXY(width - 1, height - 1, GraphicsChars.LRCORNER, - application.getTheme().getColor("twindow.border.windowmove")); + putCharXY(getWidth() - 2, getHeight() - 1, + GraphicsChars.SINGLE_BAR, + getTheme().getColor("twindow.border.windowmove")); + putCharXY(getWidth() - 1, getHeight() - 1, + GraphicsChars.LRCORNER, + getTheme().getColor("twindow.border.windowmove")); } } } @@ -495,14 +548,13 @@ public class TWindow extends TWidget { @Override public void onMouseDown(final TMouseEvent mouse) { this.mouse = mouse; - application.setRepaint(); inKeyboardResize = false; - if ((mouse.getAbsoluteY() == y) - && mouse.getMouse1() - && (x <= mouse.getAbsoluteX()) - && (mouse.getAbsoluteX() < x + width) + if ((mouse.getAbsoluteY() == getY()) + && mouse.isMouse1() + && (getX() <= mouse.getAbsoluteX()) + && (mouse.getAbsoluteX() < getX() + getWidth()) && !mouseOnClose() && !mouseOnMaximize() ) { @@ -510,8 +562,8 @@ public class TWindow extends TWidget { inWindowMove = true; moveWindowMouseX = mouse.getAbsoluteX(); moveWindowMouseY = mouse.getAbsoluteY(); - oldWindowX = x; - oldWindowY = y; + oldWindowX = getX(); + oldWindowY = getY(); if (maximized) { maximized = false; } @@ -522,8 +574,8 @@ public class TWindow extends TWidget { inWindowResize = true; moveWindowMouseX = mouse.getAbsoluteX(); moveWindowMouseY = mouse.getAbsoluteY(); - resizeWindowWidth = width; - resizeWindowHeight = height; + resizeWindowWidth = getWidth(); + resizeWindowHeight = getHeight(); if (maximized) { maximized = false; } @@ -538,14 +590,14 @@ public class TWindow extends TWidget { * Maximize window. */ private void maximize() { - restoreWindowWidth = width; - restoreWindowHeight = height; - restoreWindowX = x; - restoreWindowY = y; - width = getScreen().getWidth(); - height = application.getDesktopBottom() - 1; - x = 0; - y = 1; + restoreWindowWidth = getWidth(); + restoreWindowHeight = getHeight(); + restoreWindowX = getX(); + restoreWindowY = getY(); + setWidth(getScreen().getWidth()); + setHeight(application.getDesktopBottom() - 1); + setX(0); + setY(1); maximized = true; } @@ -553,10 +605,10 @@ public class TWindow extends TWidget { * Restote (unmaximize) window. */ private void restore() { - width = restoreWindowWidth; - height = restoreWindowHeight; - x = restoreWindowX; - y = restoreWindowY; + setWidth(restoreWindowWidth); + setHeight(restoreWindowHeight); + setX(restoreWindowX); + setY(restoreWindowY); maximized = false; } @@ -568,27 +620,27 @@ public class TWindow extends TWidget { @Override public void onMouseUp(final TMouseEvent mouse) { this.mouse = mouse; - application.setRepaint(); - if ((inWindowMove) && (mouse.getMouse1())) { + if ((inWindowMove) && (mouse.isMouse1())) { // Stop moving window inWindowMove = false; return; } - if ((inWindowResize) && (mouse.getMouse1())) { + if ((inWindowResize) && (mouse.isMouse1())) { // Stop resizing window inWindowResize = false; return; } - if (mouse.getMouse1() && mouseOnClose()) { + if (mouse.isMouse1() && mouseOnClose()) { // Close window application.closeWindow(this); return; } - if ((mouse.getAbsoluteY() == y) && mouse.getMouse1() + if ((mouse.getAbsoluteY() == getY()) + && mouse.isMouse1() && mouseOnMaximize()) { if (maximized) { // Restore @@ -598,7 +650,8 @@ public class TWindow extends TWidget { maximize(); } // Pass a resize event to my children - onResize(new TResizeEvent(TResizeEvent.Type.WIDGET, width, height)); + onResize(new TResizeEvent(TResizeEvent.Type.WIDGET, + getWidth(), getHeight())); return; } @@ -614,54 +667,60 @@ public class TWindow extends TWidget { @Override public void onMouseMotion(final TMouseEvent mouse) { this.mouse = mouse; - application.setRepaint(); if (inWindowMove) { // Move window over - x = oldWindowX + (mouse.getAbsoluteX() - moveWindowMouseX); - y = oldWindowY + (mouse.getAbsoluteY() - moveWindowMouseY); + setX(oldWindowX + (mouse.getAbsoluteX() - moveWindowMouseX)); + setY(oldWindowY + (mouse.getAbsoluteY() - moveWindowMouseY)); // Don't cover up the menu bar - if (y < application.getDesktopTop()) { - y = application.getDesktopTop(); + if (getY() < application.getDesktopTop()) { + setY(application.getDesktopTop()); } return; } if (inWindowResize) { // Move window over - width = resizeWindowWidth + (mouse.getAbsoluteX() - moveWindowMouseX); - height = resizeWindowHeight + (mouse.getAbsoluteY() - moveWindowMouseY); - if (x + width > getScreen().getWidth()) { - width = getScreen().getWidth() - x; + setWidth(resizeWindowWidth + (mouse.getAbsoluteX() + - moveWindowMouseX)); + setHeight(resizeWindowHeight + (mouse.getAbsoluteY() + - moveWindowMouseY)); + if (getX() + getWidth() > getScreen().getWidth()) { + setWidth(getScreen().getWidth() - getX()); } - if (y + height > application.getDesktopBottom()) { - y = application.getDesktopBottom() - height + 1; + if (getY() + getHeight() > application.getDesktopBottom()) { + setY(application.getDesktopBottom() - getHeight() + 1); } // Don't cover up the menu bar - if (y < application.getDesktopTop()) { - y = application.getDesktopTop(); + if (getY() < application.getDesktopTop()) { + setY(application.getDesktopTop()); } // Keep within min/max bounds - if (width < minimumWindowWidth) { - width = minimumWindowWidth; + if (getWidth() < minimumWindowWidth) { + setWidth(minimumWindowWidth); inWindowResize = false; } - if (height < minimumWindowHeight) { - height = minimumWindowHeight; + if (getHeight() < minimumWindowHeight) { + setHeight(minimumWindowHeight); inWindowResize = false; } - if ((maximumWindowWidth > 0) && (width > maximumWindowWidth)) { - width = maximumWindowWidth; + if ((maximumWindowWidth > 0) + && (getWidth() > maximumWindowWidth) + ) { + setWidth(maximumWindowWidth); inWindowResize = false; } - if ((maximumWindowHeight > 0) && (height > maximumWindowHeight)) { - height = maximumWindowHeight; + if ((maximumWindowHeight > 0) + && (getHeight() > maximumWindowHeight) + ) { + setHeight(maximumWindowHeight); inWindowResize = false; } // Pass a resize event to my children - onResize(new TResizeEvent(TResizeEvent.Type.WIDGET, width, height)); + onResize(new TResizeEvent(TResizeEvent.Type.WIDGET, + getWidth(), getHeight())); return; } @@ -685,46 +744,58 @@ public class TWindow extends TWidget { } if (keypress.equals(kbLeft)) { - if (x > 0) { - x--; + if (getX() > 0) { + setX(getX() - 1); } } if (keypress.equals(kbRight)) { - if (x < getScreen().getWidth() - 1) { - x++; + if (getX() < getScreen().getWidth() - 1) { + setX(getX() + 1); } } if (keypress.equals(kbDown)) { - if (y < application.getDesktopBottom() - 1) { - y++; + if (getY() < application.getDesktopBottom() - 1) { + setY(getY() + 1); } } if (keypress.equals(kbUp)) { - if (y > 1) { - y--; + if (getY() > 1) { + setY(getY() - 1); } } if (keypress.equals(kbShiftLeft)) { - if (width > minimumWindowWidth) { - width--; + if ((getWidth() > minimumWindowWidth) + || (minimumWindowWidth <= 0) + ) { + setWidth(getWidth() - 1); } } if (keypress.equals(kbShiftRight)) { - if (width < maximumWindowWidth) { - width++; + if ((getWidth() < maximumWindowWidth) + || (maximumWindowWidth <= 0) + ) { + setWidth(getWidth() + 1); } } if (keypress.equals(kbShiftUp)) { - if (height > minimumWindowHeight) { - height--; + if ((getHeight() > minimumWindowHeight) + || (minimumWindowHeight <= 0) + ) { + setHeight(getHeight() - 1); } } if (keypress.equals(kbShiftDown)) { - if (height < maximumWindowHeight) { - height++; + if ((getHeight() < maximumWindowHeight) + || (maximumWindowHeight <= 0) + ) { + setHeight(getHeight() + 1); } } + // Pass a resize event to my children + onResize(new TResizeEvent(TResizeEvent.Type.WIDGET, + getWidth(), getHeight())); + return; } @@ -948,10 +1019,10 @@ public class TWindow extends TWidget { * @param str string to draw * @param attr attributes to use (bold, foreColor, backColor) */ - public final void putStrXY(final int x, final int y, final String str, + public final void putStringXY(final int x, final int y, final String str, final CellAttributes attr) { - getScreen().putStrXY(x, y, str, attr); + getScreen().putStringXY(x, y, str, attr); } /** @@ -962,8 +1033,8 @@ public class TWindow extends TWidget { * @param y row coordinate. 0 is the top-most row. * @param str string to draw */ - public final void putStrXY(final int x, final int y, final String str) { - getScreen().putStrXY(x, y, str); + public final void putStringXY(final int x, final int y, final String str) { + getScreen().putStringXY(x, y, str); } /**