+ protected TWidget(final TWidget parent, final boolean enabled,
+ final int x, final int y, final int width, final int height) {
+
+ if (width < 0) {
+ throw new IllegalArgumentException("width cannot be negative");
+ }
+ if (height < 0) {
+ throw new IllegalArgumentException("height cannot be negative");
+ }
+
+ this.enabled = enabled;
+ this.parent = parent;
+ children = new ArrayList<TWidget>();
+
+ this.x = x;
+ this.y = y;
+ this.width = width;
+ this.height = height;
+
+ if (parent != null) {
+ this.window = parent.window;
+ parent.addChild(this);
+ }
+ }
+
+ /**
+ * Backdoor access for TWindow's constructor. ONLY TWindow USES THIS.
+ *
+ * @param window the top-level window
+ * @param x column relative to parent
+ * @param y row relative to parent
+ * @param width width of window
+ * @param height height of window
+ */
+ protected final void setupForTWindow(final TWindow window,
+ final int x, final int y, final int width, final int height) {
+
+ if (width < 0) {
+ throw new IllegalArgumentException("width cannot be negative");
+ }
+ if (height < 0) {
+ throw new IllegalArgumentException("height cannot be negative");
+ }
+
+ this.parent = window;
+ this.window = window;
+ this.x = x;
+ this.y = y;
+ this.width = width;
+ this.height = height;
+ }
+
+ // ------------------------------------------------------------------------
+ // Event handlers ---------------------------------------------------------
+ // ------------------------------------------------------------------------
+
+ /**
+ * Subclasses should override this method to cleanup resources. This is
+ * called by TWindow.onClose().
+ */
+ protected void close() {
+ // Default: call close() on children.
+ for (TWidget w: getChildren()) {
+ w.close();
+ }
+ }
+
+ /**
+ * Check if a mouse press/release event coordinate is contained in this
+ * widget.
+ *
+ * @param mouse a mouse-based event
+ * @return whether or not a mouse click would be sent to this widget
+ */
+ public final boolean mouseWouldHit(final TMouseEvent mouse) {
+
+ if (!enabled) {
+ return false;
+ }
+
+ if ((this instanceof TTreeItem)
+ && ((y < 0) || (y > parent.getHeight() - 1))
+ ) {
+ return false;
+ }
+
+ if ((mouse.getAbsoluteX() >= getAbsoluteX())
+ && (mouse.getAbsoluteX() < getAbsoluteX() + width)
+ && (mouse.getAbsoluteY() >= getAbsoluteY())
+ && (mouse.getAbsoluteY() < getAbsoluteY() + height)
+ ) {
+ return true;
+ }
+ return false;
+ }
+
+ /**
+ * Method that subclasses can override to handle keystrokes.
+ *
+ * @param keypress keystroke event
+ */
+ public void onKeypress(final TKeypressEvent keypress) {
+ assert (parent != null);
+
+ if ((children.size() == 0)
+ || (this instanceof TTreeView)
+ || (this instanceof TText)
+ || (this instanceof TComboBox)
+ ) {
+
+ // Defaults:
+ // tab / shift-tab - switch to next/previous widget
+ // left-arrow or up-arrow: same as shift-tab
+ if ((keypress.equals(kbTab))
+ || (keypress.equals(kbDown) && !(this instanceof TComboBox))
+ ) {
+ parent.switchWidget(true);
+ return;
+ } else if ((keypress.equals(kbShiftTab))
+ || (keypress.equals(kbBackTab))
+ || (keypress.equals(kbUp) && !(this instanceof TComboBox))
+ ) {
+ parent.switchWidget(false);
+ return;
+ }
+ }
+
+ if ((children.size() == 0)
+ && !(this instanceof TTreeView)
+ ) {
+
+ // Defaults:
+ // right-arrow or down-arrow: same as tab
+ if (keypress.equals(kbRight)) {
+ parent.switchWidget(true);
+ return;
+ } else if (keypress.equals(kbLeft)) {
+ parent.switchWidget(false);
+ return;
+ }
+ }
+
+ // If I have any buttons on me AND this is an Alt-key that matches
+ // its mnemonic, send it an Enter keystroke.
+ for (TWidget widget: children) {
+ if (widget instanceof TButton) {
+ TButton button = (TButton) widget;
+ if (button.isEnabled()
+ && !keypress.getKey().isFnKey()
+ && keypress.getKey().isAlt()
+ && !keypress.getKey().isCtrl()
+ && (Character.toLowerCase(button.getMnemonic().getShortcut())
+ == Character.toLowerCase(keypress.getKey().getChar()))
+ ) {
+
+ widget.onKeypress(new TKeypressEvent(kbEnter));
+ return;
+ }
+ }
+ }
+
+ // If I have any labels on me AND this is an Alt-key that matches
+ // its mnemonic, call its action.
+ for (TWidget widget: children) {
+ if (widget instanceof TLabel) {
+ TLabel label = (TLabel) widget;
+ if (!keypress.getKey().isFnKey()
+ && keypress.getKey().isAlt()
+ && !keypress.getKey().isCtrl()
+ && (Character.toLowerCase(label.getMnemonic().getShortcut())
+ == Character.toLowerCase(keypress.getKey().getChar()))
+ ) {
+
+ label.dispatch();
+ return;
+ }
+ }
+ }
+
+ // If I have any radiobuttons on me AND this is an Alt-key that
+ // matches its mnemonic, select it and send a Space to it.
+ for (TWidget widget: children) {
+ if (widget instanceof TRadioButton) {
+ TRadioButton button = (TRadioButton) widget;
+ if (button.isEnabled()
+ && !keypress.getKey().isFnKey()
+ && keypress.getKey().isAlt()
+ && !keypress.getKey().isCtrl()
+ && (Character.toLowerCase(button.getMnemonic().getShortcut())
+ == Character.toLowerCase(keypress.getKey().getChar()))
+ ) {
+ activate(widget);
+ widget.onKeypress(new TKeypressEvent(kbSpace));
+ return;
+ }
+ }
+ if (widget instanceof TRadioGroup) {
+ for (TWidget child: widget.getChildren()) {
+ if (child instanceof TRadioButton) {
+ TRadioButton button = (TRadioButton) child;
+ if (button.isEnabled()
+ && !keypress.getKey().isFnKey()
+ && keypress.getKey().isAlt()
+ && !keypress.getKey().isCtrl()
+ && (Character.toLowerCase(button.getMnemonic().getShortcut())
+ == Character.toLowerCase(keypress.getKey().getChar()))
+ ) {
+ activate(widget);
+ widget.activate(child);
+ child.onKeypress(new TKeypressEvent(kbSpace));
+ return;
+ }
+ }
+ }
+ }
+ }
+
+ // If I have any checkboxes on me AND this is an Alt-key that matches
+ // its mnemonic, select it and set it to checked.
+ for (TWidget widget: children) {
+ if (widget instanceof TCheckBox) {
+ TCheckBox checkBox = (TCheckBox) widget;
+ if (checkBox.isEnabled()
+ && !keypress.getKey().isFnKey()
+ && keypress.getKey().isAlt()
+ && !keypress.getKey().isCtrl()
+ && (Character.toLowerCase(checkBox.getMnemonic().getShortcut())
+ == Character.toLowerCase(keypress.getKey().getChar()))
+ ) {
+ activate(checkBox);
+ checkBox.setChecked(true);
+ return;
+ }
+ }
+ }
+
+ // Dispatch the keypress to an active widget
+ for (TWidget widget: children) {
+ if (widget.active) {
+ widget.onKeypress(keypress);
+ return;
+ }
+ }
+ }
+
+ /**
+ * Method that subclasses can override to handle mouse button presses.
+ *
+ * @param mouse mouse button event
+ */
+ public void onMouseDown(final TMouseEvent mouse) {
+ // Default: do nothing, pass to children instead
+ if (activeChild != null) {
+ if (activeChild.mouseWouldHit(mouse)) {
+ // Dispatch to the active child
+
+ // Set x and y relative to the child's coordinates
+ mouse.setX(mouse.getAbsoluteX() - activeChild.getAbsoluteX());
+ mouse.setY(mouse.getAbsoluteY() - activeChild.getAbsoluteY());
+ activeChild.onMouseDown(mouse);
+ return;
+ }
+ }
+ for (int i = children.size() - 1 ; i >= 0 ; i--) {
+ TWidget widget = children.get(i);
+ if (widget.mouseWouldHit(mouse)) {
+ // Dispatch to this child, also activate it
+ activate(widget);
+
+ // Set x and y relative to the child's coordinates
+ mouse.setX(mouse.getAbsoluteX() - widget.getAbsoluteX());
+ mouse.setY(mouse.getAbsoluteY() - widget.getAbsoluteY());
+ widget.onMouseDown(mouse);
+ return;
+ }
+ }
+ }
+
+ /**
+ * Method that subclasses can override to handle mouse button releases.
+ *
+ * @param mouse mouse button event
+ */
+ public void onMouseUp(final TMouseEvent mouse) {
+ // Default: do nothing, pass to children instead
+ if (activeChild != null) {
+ if (activeChild.mouseWouldHit(mouse)) {
+ // Dispatch to the active child
+
+ // Set x and y relative to the child's coordinates
+ mouse.setX(mouse.getAbsoluteX() - activeChild.getAbsoluteX());
+ mouse.setY(mouse.getAbsoluteY() - activeChild.getAbsoluteY());
+ activeChild.onMouseUp(mouse);
+ return;
+ }
+ }
+ for (int i = children.size() - 1 ; i >= 0 ; i--) {
+ TWidget widget = children.get(i);
+ if (widget.mouseWouldHit(mouse)) {
+ // Dispatch to this child, also activate it
+ activate(widget);
+
+ // Set x and y relative to the child's coordinates
+ mouse.setX(mouse.getAbsoluteX() - widget.getAbsoluteX());
+ mouse.setY(mouse.getAbsoluteY() - widget.getAbsoluteY());
+ widget.onMouseUp(mouse);
+ return;
+ }
+ }
+ }
+
+ /**
+ * Method that subclasses can override to handle mouse movements.
+ *
+ * @param mouse mouse motion event
+ */
+ public void onMouseMotion(final TMouseEvent mouse) {
+ // Default: do nothing, pass it on to ALL of my children. This way
+ // the children can see the mouse "leaving" their area.
+ for (TWidget widget: children) {
+ // Set x and y relative to the child's coordinates
+ mouse.setX(mouse.getAbsoluteX() - widget.getAbsoluteX());
+ mouse.setY(mouse.getAbsoluteY() - widget.getAbsoluteY());
+ widget.onMouseMotion(mouse);
+ }
+ }
+
+ /**
+ * Method that subclasses can override to handle mouse button
+ * double-clicks.
+ *
+ * @param mouse mouse button event
+ */
+ public void onMouseDoubleClick(final TMouseEvent mouse) {
+ // Default: do nothing, pass to children instead
+ if (activeChild != null) {
+ if (activeChild.mouseWouldHit(mouse)) {
+ // Dispatch to the active child
+
+ // Set x and y relative to the child's coordinates
+ mouse.setX(mouse.getAbsoluteX() - activeChild.getAbsoluteX());
+ mouse.setY(mouse.getAbsoluteY() - activeChild.getAbsoluteY());
+ activeChild.onMouseDoubleClick(mouse);
+ return;
+ }
+ }
+ for (int i = children.size() - 1 ; i >= 0 ; i--) {
+ TWidget widget = children.get(i);
+ if (widget.mouseWouldHit(mouse)) {
+ // Dispatch to this child, also activate it
+ activate(widget);
+
+ // Set x and y relative to the child's coordinates
+ mouse.setX(mouse.getAbsoluteX() - widget.getAbsoluteX());
+ mouse.setY(mouse.getAbsoluteY() - widget.getAbsoluteY());
+ widget.onMouseDoubleClick(mouse);
+ return;
+ }
+ }
+ }
+
+ /**
+ * Method that subclasses can override to handle window/screen resize
+ * events.
+ *
+ * @param resize resize event
+ */
+ public void onResize(final TResizeEvent resize) {
+ // Default: change my width/height.
+ if (resize.getType() == TResizeEvent.Type.WIDGET) {
+ width = resize.getWidth();
+ height = resize.getHeight();
+ if (layout != null) {
+ if (this instanceof TWindow) {
+ layout.onResize(new TResizeEvent(TResizeEvent.Type.WIDGET,
+ width - 2, height - 2));
+ } else {
+ layout.onResize(resize);
+ }
+ }
+ } else {
+ // Let children see the screen resize
+ for (TWidget widget: children) {
+ widget.onResize(resize);
+ }
+ }
+ }
+
+ /**
+ * Method that subclasses can override to handle posted command events.
+ *
+ * @param command command event
+ */
+ public void onCommand(final TCommandEvent command) {
+ // Default: do nothing, pass to children instead
+ for (TWidget widget: children) {
+ widget.onCommand(command);
+ }
+ }
+
+ /**
+ * Method that subclasses can override to handle menu or posted menu
+ * events.
+ *
+ * @param menu menu event
+ */
+ public void onMenu(final TMenuEvent menu) {
+ // Default: do nothing, pass to children instead
+ for (TWidget widget: children) {
+ widget.onMenu(menu);
+ }
+ }
+
+ /**
+ * Method that subclasses can override to do processing when the UI is
+ * idle. Note that repainting is NOT assumed. To get a refresh after
+ * onIdle, call doRepaint().
+ */
+ public void onIdle() {
+ // Default: do nothing, pass to children instead
+ for (TWidget widget: children) {
+ widget.onIdle();
+ }
+ }
+
+ /**
+ * Consume event. Subclasses that want to intercept all events in one go
+ * can override this method.
+ *
+ * @param event keyboard, mouse, resize, command, or menu event
+ */
+ public void handleEvent(final TInputEvent event) {
+ /*
+ System.err.printf("TWidget (%s) event: %s\n", this.getClass().getName(),
+ event);
+ */
+
+ if (!enabled) {
+ // Discard event
+ // System.err.println(" -- discard --");
+ return;
+ }
+
+ if (event instanceof TKeypressEvent) {
+ onKeypress((TKeypressEvent) event);
+ } else if (event instanceof TMouseEvent) {
+
+ TMouseEvent mouse = (TMouseEvent) event;
+
+ switch (mouse.getType()) {
+
+ case MOUSE_DOWN:
+ onMouseDown(mouse);
+ break;
+
+ case MOUSE_UP:
+ onMouseUp(mouse);
+ break;
+
+ case MOUSE_MOTION:
+ onMouseMotion(mouse);
+ break;
+
+ case MOUSE_DOUBLE_CLICK:
+ onMouseDoubleClick(mouse);
+ break;
+
+ default:
+ throw new IllegalArgumentException("Invalid mouse event type: "
+ + mouse.getType());
+ }
+ } else if (event instanceof TResizeEvent) {
+ onResize((TResizeEvent) event);
+ } else if (event instanceof TCommandEvent) {
+ onCommand((TCommandEvent) event);
+ } else if (event instanceof TMenuEvent) {
+ onMenu((TMenuEvent) event);
+ }
+
+ // Do nothing else
+ return;
+ }
+
+ // ------------------------------------------------------------------------
+ // TWidget ----------------------------------------------------------------
+ // ------------------------------------------------------------------------
+
+ /**
+ * Get parent widget.
+ *
+ * @return parent widget
+ */
+ public final TWidget getParent() {
+ return parent;
+ }
+
+ /**
+ * Get the list of child widgets that this widget contains.
+ *
+ * @return the list of child widgets
+ */
+ public List<TWidget> getChildren() {
+ return children;
+ }
+
+ /**
+ * Remove this widget from its parent container. close() will be called
+ * before it is removed.
+ */
+ public final void remove() {
+ remove(true);
+ }
+
+ /**
+ * Remove this widget from its parent container.
+ *
+ * @param doClose if true, call the close() method before removing the
+ * child
+ */
+ public final void remove(final boolean doClose) {
+ if (parent != null) {
+ parent.remove(this, doClose);
+ }
+ }
+
+ /**
+ * 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.
+ *
+ * @param child the child widget to remove
+ * @param doClose if true, call the close() method before removing the
+ * child
+ */
+ public final void remove(final TWidget child, final boolean doClose) {
+ if (!children.contains(child)) {
+ throw new IndexOutOfBoundsException("child widget is not in " +
+ "list of children of this parent");
+ }
+ if (doClose) {
+ child.close();
+ }
+ children.remove(child);
+ child.parent = null;
+ child.window = null;
+ if (layout != null) {
+ layout.remove(this);
+ }
+ }
+
+ /**
+ * Set this widget's parent to a different widget.
+ *
+ * @param newParent new parent widget
+ * @param doClose if true, call the close() method before removing the
+ * child from its existing parent widget
+ */
+ public final void setParent(final TWidget newParent,
+ final boolean doClose) {
+
+ if (parent != null) {
+ parent.remove(this, doClose);
+ window = null;
+ }
+ assert (parent == null);
+ assert (window == null);
+ parent = newParent;
+ setWindow(parent.window);
+ parent.addChild(this);
+ }
+
+ /**
+ * 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) {
+ this.window = window;
+ for (TWidget child: getChildren()) {
+ child.setWindow(window);
+ }
+ }
+
+ /**
+ * Remove a child widget from this container, and all of its children
+ * recursively from their parent containers.
+ *
+ * @param child the child widget to remove
+ * @param doClose if true, call the close() method before removing each
+ * child
+ */
+ public final void removeAll(final TWidget child, final boolean doClose) {
+ remove(child, doClose);
+ for (TWidget w: child.children) {
+ child.removeAll(w, doClose);
+ }
+ }
+
+ /**
+ * Get active flag.
+ *
+ * @return if true, this widget will receive events
+ */
+ public final boolean isActive() {
+ return active;
+ }
+
+ /**
+ * Set active flag.
+ *
+ * @param active if true, this widget will receive events
+ */
+ public final void setActive(final boolean active) {
+ this.active = active;
+ }
+
+ /**
+ * Get the window this widget is on.
+ *
+ * @return the window
+ */
+ public final TWindow getWindow() {
+ return window;
+ }
+
+ /**
+ * Get X position.
+ *
+ * @return absolute X position of the top-left corner
+ */
+ public final int getX() {
+ return x;
+ }
+
+ /**
+ * Set X position.
+ *
+ * @param x absolute X position of the top-left corner
+ */
+ public final void setX(final int x) {
+ this.x = x;
+ }
+
+ /**
+ * Get Y position.
+ *
+ * @return absolute Y position of the top-left corner
+ */
+ public final int getY() {
+ return y;
+ }
+
+ /**
+ * Set Y position.
+ *
+ * @param y absolute Y position of the top-left corner
+ */
+ public final void setY(final int y) {
+ this.y = y;
+ }
+
+ /**
+ * Get the width.
+ *
+ * @return widget width
+ */
+ public int getWidth() {
+ return this.width;
+ }
+
+ /**
+ * Change the width.
+ *
+ * @param width new widget width
+ */
+ public void setWidth(final int width) {
+ this.width = width;
+ if (layout != null) {
+ layout.onResize(new TResizeEvent(TResizeEvent.Type.WIDGET,
+ width, height));
+ }
+ }
+
+ /**
+ * Get the height.
+ *
+ * @return widget height
+ */
+ public int getHeight() {
+ return this.height;
+ }
+
+ /**
+ * Change the height.
+ *
+ * @param height new widget height
+ */
+ public void setHeight(final int height) {
+ this.height = height;
+ if (layout != null) {
+ layout.onResize(new TResizeEvent(TResizeEvent.Type.WIDGET,
+ width, height));
+ }
+ }
+
+ /**
+ * Change the dimensions.
+ *
+ * @param x absolute X position of the top-left corner
+ * @param y absolute Y position of the top-left corner
+ * @param width new widget width
+ * @param height new widget height
+ */
+ public final void setDimensions(final int x, final int y, final int width,
+ final int height) {
+
+ setX(x);
+ setY(y);
+ setWidth(width);
+ setHeight(height);
+ if (layout != null) {
+ layout.onResize(new TResizeEvent(TResizeEvent.Type.WIDGET,
+ width, height));
+ }
+ }
+
+ /**
+ * Get the layout manager.
+ *
+ * @return the layout manager, or null if not set
+ */
+ public LayoutManager getLayoutManager() {
+ return layout;
+ }
+
+ /**
+ * Set the layout manager.
+ *
+ * @param layout the new layout manager
+ */
+ public void setLayoutManager(LayoutManager layout) {
+ if (this.layout != null) {
+ for (TWidget w: children) {
+ this.layout.remove(w);
+ }
+ this.layout = null;
+ }
+ this.layout = layout;
+ if (this.layout != null) {
+ for (TWidget w: children) {
+ this.layout.add(w);
+ }
+ }
+ }
+
+ /**
+ * Get enabled flag.
+ *
+ * @return if true, this widget can be tabbed to or receive events
+ */
+ public final boolean isEnabled() {
+ return enabled;
+ }
+
+ /**
+ * Set enabled flag.
+ *
+ * @param enabled if true, this widget can be tabbed to or receive events
+ */
+ public final void setEnabled(final boolean enabled) {
+ this.enabled = enabled;
+ if (!enabled) {
+ active = false;
+ // See if there are any active siblings to switch to
+ boolean foundSibling = false;
+ if (parent != null) {
+ for (TWidget w: parent.children) {
+ if ((w.enabled)
+ && !(this instanceof THScroller)
+ && !(this instanceof TVScroller)
+ ) {
+ parent.activate(w);
+ foundSibling = true;
+ break;
+ }
+ }
+ if (!foundSibling) {
+ parent.activeChild = null;
+ }
+ }
+ }
+ }
+
+ /**
+ * Set visible flag.
+ *
+ * @param visible if true, this widget will be drawn
+ */
+ public final void setVisible(final boolean visible) {
+ this.visible = visible;
+ }
+
+ /**
+ * See if this widget is visible.
+ *
+ * @return if true, this widget will be drawn
+ */
+ public final boolean isVisible() {
+ return visible;
+ }
+
+ /**
+ * Set visible cursor flag.
+ *
+ * @param cursorVisible if true, this widget has a cursor
+ */
+ public final void setCursorVisible(final boolean cursorVisible) {
+ this.cursorVisible = cursorVisible;
+ }
+
+ /**
+ * See if this widget has a visible cursor.
+ *
+ * @return if true, this widget has a visible cursor
+ */
+ 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;
+ }
+
+ /**
+ * 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)
+ */
+ public final 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)
+ ) {