X-Git-Url: http://git.nikiroo.be/?a=blobdiff_plain;f=src%2Fjexer%2FTWidget.java;h=034c1e9b26f0ae8fdb1a06550456db2d46bee606;hb=6738825512004e0992555778e7124b568dd5044c;hp=d48ffe4c9ad6e477123c1e940e659360b347a1ab;hpb=fc2af49443133106c95da9aaf8b7126be8c7dedd;p=fanfix.git diff --git a/src/jexer/TWidget.java b/src/jexer/TWidget.java index d48ffe4..034c1e9 100644 --- a/src/jexer/TWidget.java +++ b/src/jexer/TWidget.java @@ -729,6 +729,15 @@ public abstract class TWidget implements Comparable { } } + /** + * Remove a child widget from this container. + * + * @param child the child widget to remove + */ + public final void remove(final TWidget child) { + remove(child, true); + } + /** * Remove a child widget from this container. * @@ -746,6 +755,7 @@ public abstract class TWidget implements Comparable { } children.remove(child); child.parent = null; + child.window = null; if (layout != null) { layout.remove(this); } @@ -763,29 +773,31 @@ public abstract class TWidget implements Comparable { if (parent != null) { parent.remove(this, doClose); + window = null; } assert (parent == null); - window = newParent.window; - newParent.addChild(this); + assert (window == null); + parent = newParent; + setWindow(parent.window); + parent.addChild(this); } /** - * Set this widget's window to a specific window. Parent must already be - * null. Having a null parent with a specified window is only used - * within Jexer by TStatusBar because TApplication routes events directly - * to it and calls its draw() method. Any other non-parented widgets - * will require similar special case functionality to receive events or - * be drawn to screen. + * Set this widget's window to a specific window. + * + * Having a null parent with a specified window is only used within Jexer + * by TStatusBar because TApplication routes events directly to it and + * calls its draw() method. Any other non-parented widgets will require + * similar special case functionality to receive events or be drawn to + * screen. * * @param window the window to use */ public final void setWindow(final TWindow window) { - - if (parent != null) { - throw new IllegalArgumentException("Cannot have different " + - "windows for parent and child"); - } this.window = window; + for (TWidget child: getChildren()) { + child.setWindow(window); + } } /** @@ -1042,6 +1054,11 @@ public abstract class TWidget implements Comparable { assert (window != null); + if (window instanceof TDesktop) { + // Desktop doesn't have a window border. + return cursorVisible; + } + // If cursor is out of my window's bounds, it is not visible. if ((getCursorAbsoluteX() >= window.getAbsoluteX() + window.getWidth() - 1) @@ -1094,19 +1111,25 @@ public abstract class TWidget implements Comparable { /** * Get this TWidget's parent TApplication. * - * @return the parent TApplication + * @return the parent TApplication, or null if not assigned */ public TApplication getApplication() { - return window.getApplication(); + if (window != null) { + return window.getApplication(); + } + return null; } /** * Get the Screen. * - * @return the Screen + * @return the Screen, or null if not assigned */ public Screen getScreen() { - return window.getScreen(); + if (window != null) { + return window.getScreen(); + } + return null; } /** @@ -1232,6 +1255,10 @@ public abstract class TWidget implements Comparable { * Called by parent to render to TWindow. Note package private access. */ final void drawChildren() { + if (window == null) { + return; + } + // Set my clipping rectangle assert (window != null); assert (getScreen() != null); @@ -1248,10 +1275,16 @@ public abstract class TWidget implements Comparable { int absoluteRightEdge = window.getAbsoluteX() + window.getWidth(); int absoluteBottomEdge = window.getAbsoluteY() + window.getHeight(); - if (!(this instanceof TWindow) && !(this instanceof TVScroller)) { + if (!(this instanceof TWindow) + && !(this instanceof TVScroller) + && !(parent instanceof TDesktop) + ) { absoluteRightEdge -= 1; } - if (!(this instanceof TWindow) && !(this instanceof THScroller)) { + if (!(this instanceof TWindow) + && !(this instanceof THScroller) + && !(parent instanceof TDesktop) + ) { absoluteBottomEdge -= 1; } int myRightEdge = getAbsoluteX() + width; @@ -1514,6 +1547,98 @@ public abstract class TWidget implements Comparable { return this; } + /** + * Split this widget into two, putting all of this widget's children into + * a new TPanel, and returning a new TSplitPane with that panel on the + * left or right pane. + * + * @param newWidgetOnLeft if true, the new widget (if specified) will be + * on the left pane, and this widget's children will be placed on the + * right pane + * @param newWidget the new widget to add to the other pane, or null + * @return the new split pane widget + */ + public TSplitPane splitVertical(final boolean newWidgetOnLeft, + final TWidget newWidget) { + + TPanel panel = new TPanel(null, x, y, width, height); + TSplitPane splitPane = new TSplitPane(null, x, y, width, height, true); + List widgets = new ArrayList(children); + for (TWidget w: widgets) { + w.setParent(panel, false); + } + children.clear(); + splitPane.setParent(parent, false); + parent = null; + window = null; + if (newWidgetOnLeft) { + splitPane.setLeft(newWidget); + splitPane.setRight(panel); + } else { + splitPane.setRight(newWidget); + splitPane.setLeft(panel); + } + activate(splitPane); + for (TWidget w: widgets) { + assert (w.window != null); + assert (w.parent != null); + } + assert (splitPane.getWindow() != null); + assert (splitPane.getParent() != null); + assert (panel.getWindow() != null); + assert (panel.getParent() != null); + assert (splitPane.isActive() == true); + assert (panel.isActive() == true); + return splitPane; + } + + /** + * Split this widget into two, putting all of this widget's children into + * a new TPanel, and returning a new TSplitPane with that panel on the + * top or bottom pane. + * + * @param newWidgetOnTop if true, the new widget (if specified) will be + * on the top pane, and this widget's children will be placed on the + * bottom pane + * @param newWidget the new widget to add to the other pane, or null + * @return the new split pane widget + */ + public TSplitPane splitHorizontal(final boolean newWidgetOnTop, + final TWidget newWidget) { + + TPanel panel = new TPanel(null, x, y, width, height); + TSplitPane splitPane = new TSplitPane(null, x, y, width, height, false); + List widgets = new ArrayList(children); + for (TWidget w: widgets) { + w.setParent(panel, false); + } + children.clear(); + splitPane.setParent(parent, false); + parent = null; + splitPane.setTop(panel); + parent = null; + window = null; + if (newWidgetOnTop) { + splitPane.setTop(newWidget); + splitPane.setBottom(panel); + } else { + splitPane.setBottom(newWidget); + splitPane.setTop(panel); + } + activate(splitPane); + for (TWidget w: widgets) { + assert (w.window != null); + assert (w.parent != null); + } + assert (splitPane.getWindow() != null); + assert (splitPane.getParent() != null); + assert (panel.getWindow() != null); + assert (panel.getParent() != null); + assert (splitPane.isActive() == true); + assert (panel.isActive() == true); + return splitPane; + } + // ------------------------------------------------------------------------ // Passthru for Screen functions ------------------------------------------ // ------------------------------------------------------------------------ @@ -2507,4 +2632,20 @@ public abstract class TWidget implements Comparable { return new TPanel(this, x, y, width, height); } + /** + * Convenience function to add a split pane to this container/window. + * + * @param x column relative to parent + * @param y row relative to parent + * @param width width of text area + * @param height height of text area + * @param vertical if true, split vertically + * @return the new split pane + */ + public final TSplitPane addSplitPane(final int x, final int y, + final int width, final int height, final boolean vertical) { + + return new TSplitPane(this, x, y, width, height, vertical); + } + }