setX(x);
setY(y);
- setHeight(2);
- setWidth(StringUtils.width(mnemonic.getRawLabel()) + 3);
+ super.setHeight(2);
+ super.setWidth(StringUtils.width(mnemonic.getRawLabel()) + 3);
shadowColor = new CellAttributes();
shadowColor.setTo(getWindow().getBackground());
shadowColor.setForeColor(Color.BLACK);
shadowColor.setBold(false);
+
+ // Since we set dimensions after TWidget's constructor, we need to
+ // update the layout manager.
+ if (getParent().getLayoutManager() != null) {
+ getParent().getLayoutManager().remove(this);
+ getParent().getLayoutManager().add(this);
+ }
}
/**
// TWidget ----------------------------------------------------------------
// ------------------------------------------------------------------------
+ /**
+ * Override TWidget's width: we can only set width at construction time.
+ *
+ * @param width new widget width (ignored)
+ */
+ @Override
+ public void setWidth(final int width) {
+ // Do nothing
+ }
+
+ /**
+ * Override TWidget's height: we can only set height at construction
+ * time.
+ *
+ * @param height new widget height (ignored)
+ */
+ @Override
+ public void setHeight(final int height) {
+ // Do nothing
+ }
+
/**
* Draw a button with a shadow.
*/
field.setText(list.getSelected());
list.setEnabled(false);
list.setVisible(false);
- TComboBox.this.setHeight(1);
+ TComboBox.super.setHeight(1);
if (TComboBox.this.limitToListValue == false) {
TComboBox.this.activate(field);
}
list.setEnabled(false);
list.setVisible(false);
- setHeight(1);
+ super.setHeight(1);
if (limitToListValue) {
field.setEnabled(false);
} else {
// TWidget ----------------------------------------------------------------
// ------------------------------------------------------------------------
+ /**
+ * Override TWidget's width: we can only set width at construction time.
+ *
+ * @param width new widget width (ignored)
+ */
+ @Override
+ public void setWidth(final int width) {
+ // Do nothing
+ }
+
+ /**
+ * Override TWidget's height: we can only set height at construction
+ * time.
+ *
+ * @param height new widget height (ignored)
+ */
+ @Override
+ public void setHeight(final int height) {
+ // Do nothing
+ }
+
/**
* Draw the combobox down arrow.
*/
public void hideList() {
list.setEnabled(false);
list.setVisible(false);
- setHeight(1);
+ super.setHeight(1);
if (limitToListValue == false) {
activate(field);
}
public void showList() {
list.setEnabled(true);
list.setVisible(true);
- setHeight(list.getHeight() + 1);
+ super.setHeight(list.getHeight() + 1);
activate(list);
}
// TWidget ----------------------------------------------------------------
// ------------------------------------------------------------------------
+ /**
+ * Override TWidget's height: we can only set height at construction
+ * time.
+ *
+ * @param height new widget height (ignored)
+ */
+ @Override
+ public void setHeight(final int height) {
+ // Do nothing
+ }
+
/**
* Draw the text field.
*/
// TWidget ----------------------------------------------------------------
// ------------------------------------------------------------------------
+ /**
+ * Override TWidget's height: we can only set height at construction
+ * time.
+ *
+ * @param height new widget height (ignored)
+ */
+ @Override
+ public void setHeight(final int height) {
+ // Do nothing
+ }
+
/**
* Draw a static label.
*/
// TWidget ----------------------------------------------------------------
// ------------------------------------------------------------------------
+ /**
+ * Override TWidget's height: we can only set height at construction
+ * time.
+ *
+ * @param height new widget height (ignored)
+ */
+ @Override
+ public void setHeight(final int height) {
+ // Do nothing
+ }
+
/**
* Draw a static progress bar.
*/
// TWidget ----------------------------------------------------------------
// ------------------------------------------------------------------------
+ /**
+ * Override TWidget's width: we can only set width at construction time.
+ *
+ * @param width new widget width (ignored)
+ */
+ @Override
+ public void setWidth(final int width) {
+ // Do nothing
+ }
+
+ /**
+ * Override TWidget's height: we can only set height at construction
+ * time.
+ *
+ * @param height new widget height (ignored)
+ */
+ @Override
+ public void setHeight(final int height) {
+ // Do nothing
+ }
+
/**
* Draw a radio button with label.
*/
// TWidget ----------------------------------------------------------------
// ------------------------------------------------------------------------
+ /**
+ * Override TWidget's width: we can only set width at construction time.
+ *
+ * @param width new widget width (ignored)
+ */
+ @Override
+ public void setWidth(final int width) {
+ // Do nothing
+ }
+
+ /**
+ * Override TWidget's height: we can only set height at construction
+ * time.
+ *
+ * @param height new widget height (ignored)
+ */
+ @Override
+ public void setHeight(final int height) {
+ // Do nothing
+ }
+
/**
* Draw a radio button with label.
*/
int buttonX = 1;
int buttonY = getChildren().size() + 1;
if (StringUtils.width(label) + 4 > getWidth()) {
- setWidth(StringUtils.width(label) + 7);
+ super.setWidth(StringUtils.width(label) + 7);
}
- setHeight(getChildren().size() + 3);
+ super.setHeight(getChildren().size() + 3);
TRadioButton button = new TRadioButton(this, buttonX, buttonY, label,
getChildren().size() + 1);
import jexer.event.TMenuEvent;
import jexer.event.TMouseEvent;
import jexer.event.TResizeEvent;
+import jexer.layout.LayoutManager;
import jexer.menu.TMenu;
import jexer.ttree.TTreeItem;
import jexer.ttree.TTreeView;
*/
private int cursorY = 0;
+ /**
+ * Layout manager.
+ */
+ private LayoutManager layout = null;
+
// ------------------------------------------------------------------------
// Constructors -----------------------------------------------------------
// ------------------------------------------------------------------------
this.parent = parent;
children = new ArrayList<TWidget>();
- if (parent != null) {
- this.window = parent.window;
- parent.addChild(this);
- }
-
this.x = x;
this.y = y;
this.width = width;
this.height = height;
+
+ if (parent != null) {
+ this.window = parent.window;
+ parent.addChild(this);
+ }
}
/**
if (resize.getType() == TResizeEvent.Type.WIDGET) {
width = resize.getWidth();
height = resize.getHeight();
+ if (layout != null) {
+ layout.onResize(resize);
+ }
} else {
// Let children see the screen resize
for (TWidget widget: children) {
}
children.remove(child);
child.parent = null;
+ if (layout != null) {
+ layout.remove(this);
+ }
}
/**
*
* @return widget width
*/
- public final int getWidth() {
+ public int getWidth() {
return this.width;
}
*
* @param width new widget width
*/
- public final void setWidth(final int width) {
+ public void setWidth(final int width) {
this.width = width;
+ if (layout != null) {
+ layout.onResize(new TResizeEvent(TResizeEvent.Type.WIDGET,
+ width, height));
+ }
}
/**
*
* @return widget height
*/
- public final int getHeight() {
+ public int getHeight() {
return this.height;
}
*
* @param height new widget height
*/
- public final void setHeight(final int height) {
+ public void setHeight(final int height) {
this.height = height;
+ if (layout != null) {
+ layout.onResize(new TResizeEvent(TResizeEvent.Type.WIDGET,
+ width, height));
+ }
}
/**
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);
+ }
+ }
}
/**
for (int i = 0; i < children.size(); i++) {
children.get(i).tabOrder = i;
}
+ if (layout != null) {
+ layout.add(child);
+ }
}
/**
import jexer.TMessageBox;
import jexer.TRadioGroup;
import jexer.TWindow;
+import jexer.layout.StretchLayoutManager;
import static jexer.TCommand.*;
import static jexer.TKeypress.*;
// centered on screen.
super(parent, i18n.getString("windowTitle"), 0, 0, 60, 17, flags);
+ setLayoutManager(new StretchLayoutManager(getWidth(), getHeight()));
+
int row = 1;
// Add some widgets
import jexer.TWidget;
import jexer.TWindow;
import jexer.event.TCommandEvent;
+import jexer.layout.StretchLayoutManager;
import static jexer.TCommand.*;
import static jexer.TKeypress.*;
// centered on screen.
super(parent, i18n.getString("windowTitle"), 0, 0, 64, 23, flags);
+ setLayoutManager(new StretchLayoutManager(getWidth(), getHeight()));
+
int row = 1;
// Add some widgets
import jexer.TInputBox;
import jexer.TMessageBox;
import jexer.TWindow;
+import jexer.layout.StretchLayoutManager;
import static jexer.TCommand.*;
import static jexer.TKeypress.*;
// will be centered on screen.
super(parent, i18n.getString("windowTitle"), 0, 0, 64, 18, flags);
+ setLayoutManager(new StretchLayoutManager(getWidth(), getHeight()));
+
int row = 1;
// Add some widgets
import jexer.TLabel;
import jexer.TMessageBox;
import jexer.TWindow;
+import jexer.layout.StretchLayoutManager;
import static jexer.TCommand.*;
import static jexer.TKeypress.*;
// will be centered on screen.
super(parent, i18n.getString("windowTitle"), 0, 0, 60, 20, flags);
+ setLayoutManager(new StretchLayoutManager(getWidth(), getHeight()));
+
int row = 1;
addLabel(i18n.getString("textField1"), 1, row);
*/
package jexer.layout;
+import java.awt.Rectangle;
+import java.util.HashMap;
+
import jexer.TWidget;
import jexer.event.TResizeEvent;
// Variables --------------------------------------------------------------
// ------------------------------------------------------------------------
+ /**
+ * Current width.
+ */
+ private int width = 0;
+
+ /**
+ * Current height.
+ */
+ private int height = 0;
+
+ /**
+ * Original width.
+ */
+ private int originalWidth = 0;
+
+ /**
+ * Original height.
+ */
+ private int originalHeight = 0;
+
+ /**
+ * Map of widget to original dimensions.
+ */
+ private HashMap<TWidget, Rectangle> children = new HashMap<TWidget, Rectangle>();
+
// ------------------------------------------------------------------------
// Constructors -----------------------------------------------------------
// ------------------------------------------------------------------------
+ /**
+ * Public constructor.
+ *
+ * @param width the width of the parent widget
+ * @param height the height of the parent widget
+ */
+ public StretchLayoutManager(final int width, final int height) {
+ originalWidth = width;
+ originalHeight = height;
+ }
// ------------------------------------------------------------------------
// LayoutManager ----------------------------------------------------------
* @param resize resize event
*/
public void onResize(final TResizeEvent resize) {
- // TODO
+ if (resize.getType() == TResizeEvent.Type.WIDGET) {
+ width = resize.getWidth();
+ height = resize.getHeight();
+ layoutChildren();
+ }
}
/**
* @param child the widget to manage
*/
public void add(final TWidget child) {
- // TODO
+ Rectangle rect = new Rectangle(child.getX(), child.getY(),
+ child.getWidth(), child.getHeight());
+ children.put(child, rect);
}
/**
* @param child the widget to remove
*/
public void remove(final TWidget child) {
- // TODO
+ children.remove(child);
+ }
+
+ // ------------------------------------------------------------------------
+ // StretchLayoutManager ---------------------------------------------------
+ // ------------------------------------------------------------------------
+
+ /**
+ * Resize/reposition child widgets based on difference between current
+ * dimensions and the original dimensions.
+ */
+ public void layoutChildren() {
+ double widthRatio = (double) width / originalWidth;
+ if (!Double.isFinite(widthRatio)) {
+ widthRatio = 1;
+ }
+ double heightRatio = (double) height / originalHeight;
+ if (!Double.isFinite(heightRatio)) {
+ heightRatio = 1;
+ }
+ for (TWidget child: children.keySet()) {
+ Rectangle rect = children.get(child);
+ child.setDimensions((int) (rect.getX() * widthRatio),
+ (int) (rect.getY() * heightRatio),
+ (int) (rect.getWidth() * widthRatio),
+ (int) (rect.getHeight() * heightRatio));
+ }
}
}