X-Git-Url: http://git.nikiroo.be/?a=blobdiff_plain;f=src%2Fjexer%2FTWindow.java;h=10f4e59d5df5d946a5d7032475a590552bbdf7ac;hb=e16dda65585466c8987bd1efd718431450a96605;hp=b850789ac380e9da32198f324cc14a07ce71dd7f;hpb=48e27807150e00bc9a92844382ebc8cedf1d265f;p=fanfix.git diff --git a/src/jexer/TWindow.java b/src/jexer/TWindow.java index b850789..10f4e59 100644 --- a/src/jexer/TWindow.java +++ b/src/jexer/TWindow.java @@ -1,29 +1,27 @@ -/** +/* * Jexer - Java Text User Interface * - * License: LGPLv3 or later - * - * This module is licensed under the GNU Lesser General Public License - * Version 3. Please see the file "COPYING" in this directory for more - * information about the GNU Lesser General Public License Version 3. + * The MIT License (MIT) * - * Copyright (C) 2015 Kevin Lamonte + * Copyright (C) 2016 Kevin Lamonte * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public License - * as published by the Free Software Foundation; either version 3 of - * the License, or (at your option) any later version. + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: * - * This program is distributed in the hope that it will be useful, but - * WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * General Public License for more details. + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. * - * You should have received a copy of the GNU Lesser General Public - * License along with this program; if not, see - * http://www.gnu.org/licenses/, or write to the Free Software - * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA - * 02110-1301 USA + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER + * DEALINGS IN THE SOFTWARE. * * @author Kevin Lamonte [kevin.lamonte@gmail.com] * @version 1 @@ -39,6 +37,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 +49,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 +66,7 @@ public class TWindow extends TWidget { * * @return the Screen */ + @Override public final Screen getScreen() { return application.getScreen(); } @@ -73,7 +74,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 +119,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 +185,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 +254,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 +286,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 +312,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 +319,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 +335,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 +354,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 +368,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 +396,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 +425,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 +455,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 +481,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 +546,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 +560,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 +572,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 +588,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 +603,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 +618,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 +648,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 +665,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 +742,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 +1017,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 +1031,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); } /**