From 36f52cfc476a2fde161b734e58eeff4d18015e7d Mon Sep 17 00:00:00 2001 From: Matti Tahvonen Date: Thu, 10 Jul 2008 12:20:23 +0000 Subject: [PATCH] partial fix for #1902. FF seems to bug some pixels still (too narrow). svn changeset:5077/svn branch:trunk --- .../terminal/gwt/client/ui/ITextField.java | 84 ++++++++++++++++++- .../toolkit/tests/tickets/Ticket1902.java | 12 ++- 2 files changed, 93 insertions(+), 3 deletions(-) diff --git a/src/com/itmill/toolkit/terminal/gwt/client/ui/ITextField.java b/src/com/itmill/toolkit/terminal/gwt/client/ui/ITextField.java index b024e37c6d..e09c44ea19 100644 --- a/src/com/itmill/toolkit/terminal/gwt/client/ui/ITextField.java +++ b/src/com/itmill/toolkit/terminal/gwt/client/ui/ITextField.java @@ -12,9 +12,11 @@ import com.google.gwt.user.client.ui.FocusListener; import com.google.gwt.user.client.ui.TextBoxBase; import com.google.gwt.user.client.ui.Widget; import com.itmill.toolkit.terminal.gwt.client.ApplicationConnection; +import com.itmill.toolkit.terminal.gwt.client.ContainerResizedListener; import com.itmill.toolkit.terminal.gwt.client.Paintable; import com.itmill.toolkit.terminal.gwt.client.Tooltip; import com.itmill.toolkit.terminal.gwt.client.UIDL; +import com.itmill.toolkit.terminal.gwt.client.Util; /** * This class represents a basic text input field with one row. @@ -23,7 +25,7 @@ import com.itmill.toolkit.terminal.gwt.client.UIDL; * */ public class ITextField extends TextBoxBase implements Paintable, Field, - ChangeListener, FocusListener { + ChangeListener, FocusListener, ContainerResizedListener { /** * The input node CSS classname. @@ -39,6 +41,10 @@ public class ITextField extends TextBoxBase implements Paintable, Field, protected ApplicationConnection client; private boolean immediate = false; + private float proportionalHeight = -1; + private float proportionalWidth = -1; + private int extraHorizontalPixels = -1; + private int extraVerticalPixels = -1; public ITextField() { this(DOM.createInputText()); @@ -118,4 +124,80 @@ public class ITextField extends TextBoxBase implements Paintable, Field, } catch (e) {} }-*/; + public void setHeight(String height) { + if (height != null && height.indexOf("%") > 0) { + // special handling for proportional height + proportionalHeight = Float.parseFloat(height.substring(0, height + .indexOf("%"))) / 100; + iLayout(); + } else { + super.setHeight(height); + proportionalHeight = -1; + } + } + + public void setWidth(String width) { + if (width != null && width.indexOf("%") > 0) { + // special handling for proportional w + proportionalWidth = Float.parseFloat(width.substring(0, width + .indexOf("%"))) / 100; + iLayout(); + } else { + super.setHeight(width); + proportionalWidth = -1; + } + } + + public void iLayout() { + if (proportionalWidth >= 0) { + int availPixels = (int) (DOM.getElementPropertyInt(DOM + .getParent(getElement()), "clientWidth") * proportionalWidth); + availPixels -= getExtraHorizontalPixels(); + super.setWidth(availPixels + "px"); + } + if (proportionalHeight >= 0) { + int availPixels = (int) (DOM.getElementPropertyInt(DOM + .getParent(getElement()), "clientHeight") * proportionalHeight); + availPixels -= getExtraVerticalPixels(); + super.setHeight(availPixels + "px"); + } + } + + /** + * @return space used by components paddings and borders + */ + private int getExtraHorizontalPixels() { + if (extraHorizontalPixels < 0) { + detectExtraSizes(); + } + return extraHorizontalPixels; + } + + /** + * @return space used by components paddings and borders + */ + private int getExtraVerticalPixels() { + if (extraVerticalPixels < 0) { + detectExtraSizes(); + } + return extraVerticalPixels; + } + + /** + * Detects space used by components paddings and borders. Used when + * relational size are used. + */ + private void detectExtraSizes() { + Element clone = Util.cloneNode(getElement(), false); + DOM.setElementAttribute(clone, "id", ""); + DOM.setStyleAttribute(clone, "visibility", "hidden"); + DOM.setStyleAttribute(clone, "position", "absolute"); + DOM.setStyleAttribute(clone, "width", "0"); + DOM.setStyleAttribute(clone, "height", "0"); + DOM.appendChild(DOM.getParent(getElement()), clone); + extraHorizontalPixels = DOM.getElementPropertyInt(clone, "offsetWidth"); + extraVerticalPixels = DOM.getElementPropertyInt(clone, "offsetHeight"); + DOM.removeChild(DOM.getParent(getElement()), clone); + } + } diff --git a/src/com/itmill/toolkit/tests/tickets/Ticket1902.java b/src/com/itmill/toolkit/tests/tickets/Ticket1902.java index d8ba095d4c..9779fc9700 100644 --- a/src/com/itmill/toolkit/tests/tickets/Ticket1902.java +++ b/src/com/itmill/toolkit/tests/tickets/Ticket1902.java @@ -35,7 +35,7 @@ public class Ticket1902 extends Application { })); // 100% wide component - Button b2 = new Button("100% wide button"); + TextField b2 = new TextField("100% wide field"); mainLayout.addComponent(b2); b2.setWidth("100%"); @@ -57,7 +57,7 @@ public class Ticket1902 extends Application { OrderedLayout lo2 = new OrderedLayout(); lo2.setStyleName("red-background"); mainLayout.addComponent(lo2); - lo2.setWidth(400); + lo2.setWidth("50%"); lo2.setHeight(200); Button b3 = new Button("100% wide button"); @@ -67,5 +67,13 @@ public class Ticket1902 extends Application { TextField tf2 = new TextField("100% wide textfield"); lo2.addComponent(tf2); tf2.setWidth("100%"); + // tf2 = new TextField("50% wide, 100% height textfield"); // does not + // work with caption (10.7.2008 mac hosted mode) due layouts are broken + // in trunk + tf2 = new TextField(); + tf2.setRows(2); // trigger textArea impl. + tf2.setHeight("100%"); + tf2.setWidth("50%"); + lo2.addComponent(tf2); } } \ No newline at end of file -- 2.39.5