+ public final boolean isCursorVisible() {
+ // If cursor is out of my bounds, it is not visible.
+ if ((cursorX >= width)
+ || (cursorX < 0)
+ || (cursorY >= height)
+ || (cursorY < 0)
+ ) {
+ return false;
+ }
+
+ 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)
+ || (getCursorAbsoluteX() < 0)
+ || (getCursorAbsoluteY() >= window.getAbsoluteY()
+ + window.getHeight() - 1)
+ || (getCursorAbsoluteY() < 0)
+ ) {
+ return false;
+ }
+ return cursorVisible;
+ }
+
+ /**
+ * Get cursor X value.
+ *
+ * @return cursor column position in relative coordinates
+ */
+ public final int getCursorX() {
+ return cursorX;
+ }
+
+ /**
+ * Set cursor X value.
+ *
+ * @param cursorX column position in relative coordinates
+ */
+ public final void setCursorX(final int cursorX) {
+ this.cursorX = cursorX;
+ }
+
+ /**
+ * Get cursor Y value.
+ *
+ * @return cursor row position in relative coordinates
+ */
+ public final int getCursorY() {
+ return cursorY;
+ }
+
+ /**
+ * Set cursor Y value.
+ *
+ * @param cursorY row position in relative coordinates
+ */
+ public final void setCursorY(final int cursorY) {
+ this.cursorY = cursorY;
+ }
+
+ /**
+ * Get this TWidget's parent TApplication.
+ *
+ * @return the parent TApplication, or null if not assigned
+ */
+ public TApplication getApplication() {
+ if (window != null) {
+ return window.getApplication();
+ }
+ return null;
+ }
+
+ /**
+ * Get the Screen.
+ *
+ * @return the Screen, or null if not assigned
+ */
+ public Screen getScreen() {
+ if (window != null) {
+ return window.getScreen();
+ }
+ return null;
+ }
+
+ /**
+ * Get the Clipboard.
+ *
+ * @return the Clipboard, or null if not assigned
+ */
+ public Clipboard getClipboard() {
+ if (window != null) {
+ return window.getApplication().getClipboard();
+ }
+ return null;
+ }
+
+ /**
+ * Comparison operator. For various subclasses it sorts on:
+ * <ul>
+ * <li>tabOrder for TWidgets</li>
+ * <li>z for TWindows</li>
+ * <li>text for TTreeItems</li>
+ * </ul>
+ *
+ * @param that another TWidget, TWindow, or TTreeItem instance
+ * @return difference between this.tabOrder and that.tabOrder, or
+ * difference between this.z and that.z, or String.compareTo(text)
+ */
+ @Override
+ public int compareTo(final TWidget that) {
+ if ((this instanceof TWindow)
+ && (that instanceof TWindow)
+ ) {
+ return (((TWindow) this).getZ() - ((TWindow) that).getZ());
+ }
+ if ((this instanceof TTreeItem)
+ && (that instanceof TTreeItem)
+ ) {
+ return (((TTreeItem) this).getText().compareTo(
+ ((TTreeItem) that).getText()));
+ }
+ return (this.tabOrder - that.tabOrder);
+ }
+
+ /**
+ * See if this widget should render with the active color.
+ *
+ * @return true if this widget is active and all of its parents are
+ * active.
+ */
+ public final boolean isAbsoluteActive() {
+ if (parent == this) {
+ return active;
+ }
+ return (active && (parent == null ? true : parent.isAbsoluteActive()));
+ }
+
+ /**
+ * Returns the cursor X position.
+ *
+ * @return absolute screen column number for the cursor's X position
+ */
+ public final int getCursorAbsoluteX() {
+ return getAbsoluteX() + cursorX;
+ }
+
+ /**
+ * Returns the cursor Y position.
+ *
+ * @return absolute screen row number for the cursor's Y position
+ */
+ public final int getCursorAbsoluteY() {
+ return getAbsoluteY() + cursorY;
+ }
+
+ /**
+ * Compute my absolute X position as the sum of my X plus all my parent's
+ * X's.
+ *
+ * @return absolute screen column number for my X position
+ */
+ public final int getAbsoluteX() {
+ assert (parent != null);
+ if (parent == this) {
+ return x;
+ }
+ if ((parent instanceof TWindow)
+ && !(parent instanceof TMenu)
+ && !(parent instanceof TDesktop)
+ ) {
+ // Widgets on a TWindow have (0,0) as their top-left, but this is
+ // actually the TWindow's (1,1).
+ return parent.getAbsoluteX() + x + 1;
+ }
+ return parent.getAbsoluteX() + x;
+ }
+
+ /**
+ * Compute my absolute Y position as the sum of my Y plus all my parent's
+ * Y's.
+ *
+ * @return absolute screen row number for my Y position
+ */
+ public final int getAbsoluteY() {
+ assert (parent != null);
+ if (parent == this) {
+ return y;
+ }
+ if ((parent instanceof TWindow)
+ && !(parent instanceof TMenu)
+ && !(parent instanceof TDesktop)
+ ) {
+ // Widgets on a TWindow have (0,0) as their top-left, but this is
+ // actually the TWindow's (1,1).
+ return parent.getAbsoluteY() + y + 1;
+ }
+ return parent.getAbsoluteY() + y;
+ }
+
+ /**
+ * Get the global color theme.
+ *
+ * @return the ColorTheme
+ */
+ protected final ColorTheme getTheme() {
+ return window.getApplication().getTheme();
+ }
+
+ /**
+ * See if this widget can be drawn onto a screen.
+ *
+ * @return true if this widget is part of the hierarchy that can draw to
+ * a screen
+ */
+ public final boolean isDrawable() {
+ if ((window == null)
+ || (window.getScreen() == null)
+ || (parent == null)
+ ) {
+ return false;
+ }
+ if (parent == this) {
+ return true;
+ }
+ return (parent.isDrawable());
+ }
+
+ /**
+ * Draw my specific widget. When called, the screen rectangle I draw
+ * into is already setup (offset and clipping).
+ */
+ public void draw() {
+ // Default widget draws nothing.
+ }
+
+ /**
+ * Called by parent to render to TWindow. Note package private access.
+ */
+ final void drawChildren() {
+ if (!isDrawable()) {
+ return;
+ }
+
+ // Set my clipping rectangle
+ assert (window != null);
+ assert (getScreen() != null);
+ Screen screen = getScreen();
+
+ // Special case: TStatusBar is drawn by TApplication, not anything
+ // else.
+ if (this instanceof TStatusBar) {
+ return;
+ }
+
+ screen.setClipRight(width);
+ screen.setClipBottom(height);
+
+ int absoluteRightEdge = window.getAbsoluteX() + window.getWidth();
+ int absoluteBottomEdge = window.getAbsoluteY() + window.getHeight();
+ if (!(this instanceof TWindow)
+ && !(this instanceof TVScroller)
+ && !(window instanceof TDesktop)
+ ) {
+ absoluteRightEdge -= 1;
+ }
+ if (!(this instanceof TWindow)
+ && !(this instanceof THScroller)
+ && !(window instanceof TDesktop)
+ ) {
+ absoluteBottomEdge -= 1;
+ }
+ int myRightEdge = getAbsoluteX() + width;
+ int myBottomEdge = getAbsoluteY() + height;
+ if (getAbsoluteX() > absoluteRightEdge) {
+ // I am offscreen
+ screen.setClipRight(0);
+ } else if (myRightEdge > absoluteRightEdge) {
+ screen.setClipRight(screen.getClipRight()
+ - (myRightEdge - absoluteRightEdge));
+ }
+ if (getAbsoluteY() > absoluteBottomEdge) {
+ // I am offscreen
+ screen.setClipBottom(0);
+ } else if (myBottomEdge > absoluteBottomEdge) {
+ screen.setClipBottom(screen.getClipBottom()
+ - (myBottomEdge - absoluteBottomEdge));
+ }
+
+ // Set my offset
+ screen.setOffsetX(getAbsoluteX());
+ screen.setOffsetY(getAbsoluteY());
+
+ // Draw me
+ draw();
+ if (!isDrawable()) {
+ // An action taken by a draw method unhooked me from the UI.
+ // Bail out.
+ return;
+ }
+
+ assert (visible == true);
+
+ // Continue down the chain. Draw the active child last so that it
+ // is on top.
+ for (TWidget widget: children) {
+ if (widget.isVisible() && (widget != activeChild)) {
+ widget.drawChildren();
+ if (!isDrawable()) {
+ // An action taken by a draw method unhooked me from the UI.
+ // Bail out.
+ return;
+ }
+ }
+ }
+ if (activeChild != null) {
+ activeChild.drawChildren();
+ }
+ }
+
+ /**
+ * Repaint the screen on the next update.
+ */
+ protected final void doRepaint() {
+ window.getApplication().doRepaint();
+ }
+
+ /**
+ * Add a child widget to my list of children. We set its tabOrder to 0
+ * and increment the tabOrder of all other children.
+ *
+ * @param child TWidget to add
+ */
+ public void addChild(final TWidget child) {
+ children.add(child);
+
+ if ((child.enabled)
+ && !(child instanceof THScroller)
+ && !(child instanceof TVScroller)
+ ) {
+ for (TWidget widget: children) {
+ widget.active = false;
+ }
+ child.active = true;
+ activeChild = child;
+ }
+ for (int i = 0; i < children.size(); i++) {
+ children.get(i).tabOrder = i;
+ }
+ if (layout != null) {
+ layout.add(child);
+ }
+ }
+
+ /**
+ * Reset the tab order of children to match their position in the list.
+ * Available so that subclasses can re-order their widgets if needed.
+ */
+ protected void resetTabOrder() {
+ for (int i = 0; i < children.size(); i++) {
+ children.get(i).tabOrder = i;
+ }
+ }
+
+ /**
+ * Remove and {@link TWidget#close()} the given child from this {@link TWidget}.
+ * <p>
+ * Will also reorder the tab values of the remaining children.
+ *
+ * @param child the child to remove
+ *
+ * @return TRUE if the child was removed, FALSE if it was not found
+ */
+ public boolean removeChild(final TWidget child) {
+ if (children.remove(child)) {
+ child.close();
+ child.parent = null;
+ child.window = null;
+
+ resetTabOrder();
+
+ return true;
+ }
+
+ return false;
+ }
+
+ /**
+ * Switch the active child.
+ *
+ * @param child TWidget to activate
+ */
+ public final void activate(final TWidget child) {
+ assert (child.enabled);
+ if ((child instanceof THScroller)
+ || (child instanceof TVScroller)
+ ) {
+ return;
+ }
+
+ if (children.size() == 1) {
+ if (children.get(0).enabled == true) {
+ child.active = true;
+ activeChild = child;
+ }
+ } else {
+ if (child != activeChild) {
+ if (activeChild != null) {
+ activeChild.active = false;
+ }
+ }
+ child.active = true;
+ activeChild = child;
+ }
+ }
+
+ /**
+ * Switch the active child.
+ *
+ * @param tabOrder tabOrder of the child to activate. If that child
+ * isn't enabled, then the next enabled child will be activated.
+ */
+ public final void activate(final int tabOrder) {
+ if (children.size() == 1) {
+ if (children.get(0).enabled == true) {
+ children.get(0).active = true;
+ activeChild = children.get(0);
+ }
+ return;
+ }
+
+ TWidget child = null;
+ for (TWidget widget: children) {
+ if ((widget.enabled)
+ && !(widget instanceof THScroller)
+ && !(widget instanceof TVScroller)
+ && (widget.tabOrder >= tabOrder)
+ ) {
+ child = widget;
+ break;
+ }
+ }
+ if ((child != null) && (child != activeChild)) {
+ if (activeChild != null) {
+ activeChild.active = false;
+ }
+ assert (child.enabled);
+ child.active = true;
+ activeChild = child;
+ }
+ }
+
+ /**
+ * Make this widget the active child of its parent. Note that this is
+ * not final since TWindow overrides activate().
+ */
+ public void activate() {
+ if (enabled) {
+ if (parent != null) {
+ parent.activate(this);
+ }
+ }
+ }
+
+ /**
+ * Make this widget, all of its parents, the active child.
+ */
+ public final void activateAll() {
+ activate();
+ if (parent == this) {
+ return;
+ }
+ if (parent != null) {
+ parent.activateAll();
+ }
+ }
+
+ /**
+ * Switch the active widget with the next in the tab order.
+ *
+ * @param forward if true, then switch to the next enabled widget in the
+ * list, otherwise switch to the previous enabled widget in the list
+ */
+ public final void switchWidget(final boolean forward) {
+
+ // No children: do nothing.
+ if (children.size() == 0) {
+ return;
+ }
+
+ assert (parent != null);
+
+ // If there is only one child, make it active if it is enabled.
+ if (children.size() == 1) {
+ if (children.get(0).enabled == true) {
+ activeChild = children.get(0);
+ activeChild.active = true;
+ } else {
+ children.get(0).active = false;
+ activeChild = null;
+ }
+ return;
+ }
+
+ // Two or more children: go forward or backward to the next enabled
+ // child.
+ int tabOrder = 0;
+ if (activeChild != null) {
+ tabOrder = activeChild.tabOrder;
+ }
+ do {
+ if (forward) {
+ tabOrder++;
+ } else {
+ tabOrder--;
+ }
+ if (tabOrder < 0) {
+
+ // If at the end, pass the switch to my parent.
+ if ((!forward) && (parent != this)) {
+ parent.switchWidget(forward);
+ return;
+ }
+
+ tabOrder = children.size() - 1;
+ } else if (tabOrder == children.size()) {
+ // If at the end, pass the switch to my parent.
+ if ((forward) && (parent != this)) {
+ parent.switchWidget(forward);
+ return;
+ }
+
+ tabOrder = 0;
+ }
+ if (activeChild == null) {
+ if (tabOrder == 0) {
+ // We wrapped around
+ break;
+ }
+ } else if (activeChild.tabOrder == tabOrder) {
+ // We wrapped around
+ break;
+ }
+ } while ((!children.get(tabOrder).enabled)
+ && !(children.get(tabOrder) instanceof THScroller)
+ && !(children.get(tabOrder) instanceof TVScroller));
+
+ if (activeChild != null) {
+ assert (children.get(tabOrder).enabled);
+
+ activeChild.active = false;
+ }
+ if (children.get(tabOrder).enabled == true) {
+ children.get(tabOrder).active = true;
+ activeChild = children.get(tabOrder);
+ }
+ }
+
+ /**
+ * Returns my active widget.
+ *
+ * @return widget that is active, or this if no children
+ */
+ public TWidget getActiveChild() {
+ if ((this instanceof THScroller)
+ || (this instanceof TVScroller)
+ ) {
+ return parent;
+ }
+
+ for (TWidget widget: children) {
+ if (widget.active) {
+ return widget.getActiveChild();
+ }
+ }
+ // No active children, return me
+ return this;
+ }
+
+ /**
+ * Insert a vertical split between this widget and parent, and optionally
+ * put another widget in the other side of the split.
+ *
+ * @param newWidgetOnLeft if true, the new widget (if specified) will be
+ * on the left pane, and this widget 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) {
+
+ TSplitPane splitPane = new TSplitPane(null, x, y, width, height, true);
+ TWidget myParent = parent;
+ remove(false);
+ if (myParent instanceof TSplitPane) {
+ // TSplitPane has a left/right/top/bottom link to me somewhere,
+ // replace it with a link to splitPane.
+ ((TSplitPane) myParent).replaceWidget(this, splitPane);
+ }
+ splitPane.setParent(myParent, false);
+ if (newWidgetOnLeft) {
+ splitPane.setLeft(newWidget);
+ splitPane.setRight(this);
+ } else {
+ splitPane.setLeft(this);
+ splitPane.setRight(newWidget);
+ }
+ if (newWidget != null) {
+ newWidget.activateAll();
+ } else {
+ activateAll();
+ }
+
+ assert (parent != null);
+ assert (window != null);
+ assert (splitPane.getWindow() != null);
+ assert (splitPane.getParent() != null);
+ assert (splitPane.isActive() == true);
+ assert (parent == splitPane);
+ if (newWidget != null) {
+ assert (newWidget.parent == parent);
+ assert (newWidget.active == true);
+ assert (active == false);
+ } else {
+ assert (active == true);
+ }
+ return splitPane;
+ }
+
+ /**
+ * Insert a horizontal split between this widget and parent, and
+ * optionally put another widget in the other side of the split.
+ *
+ * @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) {
+
+ TSplitPane splitPane = new TSplitPane(null, x, y, width, height, false);
+ TWidget myParent = parent;
+ remove(false);
+ if (myParent instanceof TSplitPane) {
+ // TSplitPane has a left/right/top/bottom link to me somewhere,
+ // replace it with a link to splitPane.
+ ((TSplitPane) myParent).replaceWidget(this, splitPane);
+ }
+ splitPane.setParent(myParent, false);
+ if (newWidgetOnTop) {
+ splitPane.setTop(newWidget);
+ splitPane.setBottom(this);
+ } else {
+ splitPane.setTop(this);
+ splitPane.setBottom(newWidget);
+ }
+ if (newWidget != null) {
+ newWidget.activateAll();
+ } else {
+ activateAll();
+ }
+
+ assert (parent != null);
+ assert (window != null);
+ assert (splitPane.getWindow() != null);
+ assert (splitPane.getParent() != null);
+ assert (splitPane.isActive() == true);
+ assert (parent == splitPane);
+ if (newWidget != null) {
+ assert (newWidget.parent == parent);
+ assert (newWidget.active == true);
+ assert (active == false);
+ } else {
+ assert (active == true);
+ }
+ return splitPane;
+ }
+
+ /**
+ * Generate a human-readable string for this widget.
+ *
+ * @return a human-readable string
+ */
+ @Override
+ public String toString() {
+ return String.format("%s(%8x) position (%d, %d) geometry %dx%d " +
+ "active %s enabled %s visible %s", getClass().getName(),
+ hashCode(), x, y, width, height, active, enabled, visible);
+ }
+
+ /**
+ * Generate a string for this widget's hierarchy.
+ *
+ * @param prefix a prefix to use for this widget's place in the hierarchy
+ * @return a pretty-printable string of this hierarchy
+ */
+ protected String toPrettyString(final String prefix) {
+ StringBuilder sb = new StringBuilder(prefix);
+ sb.append(toString());
+ String newPrefix = "";
+ for (int i = 0; i < prefix.length(); i++) {
+ newPrefix += " ";
+ }
+ for (int i = 0; i < children.size(); i++) {
+ TWidget child= children.get(i);
+ sb.append("\n");
+ if (i == children.size() - 1) {
+ sb.append(child.toPrettyString(newPrefix + " \u2514\u2500"));
+ } else {
+ sb.append(child.toPrettyString(newPrefix + " \u251c\u2500"));
+ }
+ }
+ return sb.toString();
+ }
+
+ /**
+ * Generate a string for this widget's hierarchy.
+ *
+ * @return a pretty-printable string of this hierarchy
+ */
+ public String toPrettyString() {
+ return toPrettyString("");
+ }
+
+ // ------------------------------------------------------------------------
+ // Passthru for Screen functions ------------------------------------------
+ // ------------------------------------------------------------------------
+
+ /**
+ * Get the attributes at one location.
+ *
+ * @param x column coordinate. 0 is the left-most column.
+ * @param y row coordinate. 0 is the top-most row.
+ * @return attributes at (x, y)
+ */
+ protected final CellAttributes getAttrXY(final int x, final int y) {
+ return getScreen().getAttrXY(x, y);
+ }
+
+ /**
+ * Set the attributes at one location.
+ *
+ * @param x column coordinate. 0 is the left-most column.
+ * @param y row coordinate. 0 is the top-most row.
+ * @param attr attributes to use (bold, foreColor, backColor)
+ */
+ protected final void putAttrXY(final int x, final int y,
+ final CellAttributes attr) {
+
+ getScreen().putAttrXY(x, y, attr);
+ }
+
+ /**
+ * Set the attributes at one location.
+ *
+ * @param x column coordinate. 0 is the left-most column.
+ * @param y row coordinate. 0 is the top-most row.
+ * @param attr attributes to use (bold, foreColor, backColor)
+ * @param clip if true, honor clipping/offset
+ */
+ protected final void putAttrXY(final int x, final int y,
+ final CellAttributes attr, final boolean clip) {
+
+ getScreen().putAttrXY(x, y, attr, clip);
+ }
+
+ /**
+ * Fill the entire screen with one character with attributes.
+ *
+ * @param ch character to draw
+ * @param attr attributes to use (bold, foreColor, backColor)
+ */
+ protected final void putAll(final int ch, final CellAttributes attr) {
+ getScreen().putAll(ch, attr);
+ }
+
+ /**
+ * Render one character with attributes.
+ *
+ * @param x column coordinate. 0 is the left-most column.
+ * @param y row coordinate. 0 is the top-most row.
+ * @param ch character + attributes to draw
+ */
+ protected final void putCharXY(final int x, final int y, final Cell ch) {
+ getScreen().putCharXY(x, y, ch);