X-Git-Url: http://git.nikiroo.be/?a=blobdiff_plain;f=src%2Fjexer%2FTWindow.java;h=0a74a40dcc830f8cf739c0ad0f56c9c7ea4f943a;hb=92554d64c21c6a477fd23a06ca3a64a542b622a3;hp=0e94808fd05c1b0e3e9342402f642d9b2ed7f5c2;hpb=a06459bd6b0e65c9b590dbdf6ed9349043119215;p=fanfix.git diff --git a/src/jexer/TWindow.java b/src/jexer/TWindow.java index 0e94808..0a74a40 100644 --- a/src/jexer/TWindow.java +++ b/src/jexer/TWindow.java @@ -39,24 +39,26 @@ 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.*; /** * TWindow is the top-level container and drawing surface for other widgets. */ -public class TWindow extends TWidget implements Comparable { +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 implements Comparable { * * @return the Screen */ + @Override public final Screen getScreen() { return application.getScreen(); } @@ -73,7 +76,25 @@ public class TWindow extends TWidget implements Comparable { /** * 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). @@ -166,6 +187,15 @@ public class TWindow extends TWidget implements Comparable { 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). * @@ -226,22 +256,20 @@ public class TWindow extends TWidget implements Comparable { 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()) { @@ -260,18 +288,17 @@ public class TWindow extends TWidget implements Comparable { */ 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()); } } @@ -287,17 +314,6 @@ public class TWindow extends TWidget implements Comparable { return true; } - /** - * Comparison operator sorts on z. - * - * @param that another TWindow instance - * @return difference between this.z and that.z - */ - @Override - public final int compareTo(final TWindow that) { - return (this.z - that.z); - } - /** * Returns true if the mouse is currently on the close button. * @@ -305,8 +321,8 @@ public class TWindow extends TWidget implements Comparable { */ private boolean mouseOnClose() { if ((mouse != null) - && (mouse.getAbsoluteY() == y) - && (mouse.getAbsoluteX() == x + 3) + && (mouse.getAbsoluteY() == getY()) + && (mouse.getAbsoluteX() == getX() + 3) ) { return true; } @@ -321,8 +337,8 @@ public class TWindow extends TWidget implements Comparable { 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; } @@ -340,9 +356,9 @@ public class TWindow extends TWidget implements Comparable { 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; } @@ -354,26 +370,26 @@ public class TWindow extends TWidget implements Comparable { * * @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 (getActive()); + return getTheme().getColor("twindow.background.windowmove"); } else if (isModal() && inWindowMove) { - assert (active); - return application.getTheme().getColor("twindow.background.modal"); + assert (getActive()); + return getTheme().getColor("twindow.background.modal"); } else if (isModal()) { - if (active) { - return application.getTheme().getColor("twindow.background.modal"); + if (getActive()) { + 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 (getActive()) { 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"); } } @@ -382,27 +398,27 @@ public class TWindow extends TWidget implements Comparable { * * @return the border color */ - protected final CellAttributes getBorder() { + private CellAttributes getBorder() { if (!isModal() && (inWindowMove || inWindowResize || inKeyboardResize) ) { - assert (active); - return application.getTheme().getColor("twindow.border.windowmove"); + assert (getActive()); + return getTheme().getColor("twindow.border.windowmove"); } else if (isModal() && inWindowMove) { - assert (active); - return application.getTheme().getColor("twindow.border.modal.windowmove"); + assert (getActive()); + return getTheme().getColor("twindow.border.modal.windowmove"); } else if (isModal()) { - if (active) { - return application.getTheme().getColor("twindow.border.modal"); + if (getActive()) { + 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 (getActive()) { 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"); } } @@ -411,22 +427,22 @@ public class TWindow extends TWidget implements Comparable { * * @return the border line type */ - protected final int getBorderType() { + private int getBorderType() { if (!isModal() && (inWindowMove || inWindowResize || inKeyboardResize) ) { - assert (active); + assert (getActive()); return 1; } else if (isModal() && inWindowMove) { - assert (active); + assert (getActive()); return 1; } else if (isModal()) { - if (active) { + if (getActive()) { return 2; } else { return 1; } - } else if (active) { + } else if (getActive()) { return 2; } else { return 1; @@ -451,16 +467,16 @@ public class TWindow extends TWidget implements Comparable { 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); putCharXY(titleLeft + title.length() + 1, 0, ' ', border); - if (active) { + if (getActive()) { // Draw the close button putCharXY(2, 0, '[', border); @@ -468,39 +484,41 @@ public class TWindow extends TWidget implements Comparable { if (mouseOnClose() && mouse.getMouse1()) { 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); + putCharXY(getWidth() - 5, 0, '[', border); + putCharXY(getWidth() - 3, 0, ']', border); if (mouseOnMaximize() && mouse.getMouse1()) { - putCharXY(width - 4, 0, GraphicsChars.CP437[0x0F], - application.getTheme().getColor("twindow.border.windowmove")); + 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")); } } } @@ -514,14 +532,13 @@ public class TWindow extends TWidget implements Comparable { @Override public void onMouseDown(final TMouseEvent mouse) { this.mouse = mouse; - application.setRepaint(); inKeyboardResize = false; - if ((mouse.getAbsoluteY() == y) + if ((mouse.getAbsoluteY() == getY()) && mouse.getMouse1() - && (x <= mouse.getAbsoluteX()) - && (mouse.getAbsoluteX() < x + width) + && (getX() <= mouse.getAbsoluteX()) + && (mouse.getAbsoluteX() < getX() + getWidth()) && !mouseOnClose() && !mouseOnMaximize() ) { @@ -529,8 +546,8 @@ public class TWindow extends TWidget implements Comparable { inWindowMove = true; moveWindowMouseX = mouse.getAbsoluteX(); moveWindowMouseY = mouse.getAbsoluteY(); - oldWindowX = x; - oldWindowY = y; + oldWindowX = getX(); + oldWindowY = getY(); if (maximized) { maximized = false; } @@ -541,8 +558,8 @@ public class TWindow extends TWidget implements Comparable { inWindowResize = true; moveWindowMouseX = mouse.getAbsoluteX(); moveWindowMouseY = mouse.getAbsoluteY(); - resizeWindowWidth = width; - resizeWindowHeight = height; + resizeWindowWidth = getWidth(); + resizeWindowHeight = getHeight(); if (maximized) { maximized = false; } @@ -557,14 +574,14 @@ public class TWindow extends TWidget implements Comparable { * 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; } @@ -572,10 +589,10 @@ public class TWindow extends TWidget implements Comparable { * Restote (unmaximize) window. */ private void restore() { - width = restoreWindowWidth; - height = restoreWindowHeight; - x = restoreWindowX; - y = restoreWindowY; + setWidth(restoreWindowWidth); + setHeight(restoreWindowHeight); + setX(restoreWindowX); + setY(restoreWindowY); maximized = false; } @@ -587,7 +604,6 @@ public class TWindow extends TWidget implements Comparable { @Override public void onMouseUp(final TMouseEvent mouse) { this.mouse = mouse; - application.setRepaint(); if ((inWindowMove) && (mouse.getMouse1())) { // Stop moving window @@ -607,7 +623,8 @@ public class TWindow extends TWidget implements Comparable { return; } - if ((mouse.getAbsoluteY() == y) && mouse.getMouse1() + if ((mouse.getAbsoluteY() == getY()) + && mouse.getMouse1() && mouseOnMaximize()) { if (maximized) { // Restore @@ -617,7 +634,8 @@ public class TWindow extends TWidget implements Comparable { 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; } @@ -633,54 +651,60 @@ public class TWindow extends TWidget implements Comparable { @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; } @@ -704,43 +728,51 @@ public class TWindow extends TWidget implements Comparable { } 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); } }