Example usage for com.google.gwt.dom.client SpanElement getOffsetWidth

List of usage examples for com.google.gwt.dom.client SpanElement getOffsetWidth

Introduction

In this page you can find the example usage for com.google.gwt.dom.client SpanElement getOffsetWidth.

Prototype

@Override
    public int getOffsetWidth() 

Source Link

Usage

From source file:com.bedatadriven.renjin.appengine.client.CommandPrompt.java

License:Apache License

private int measureTextWidthInPixels(String text) {
    SpanElement span = Document.get().createSpanElement();
    RootPanel.get().getElement().appendChild(span);
    span.setAttribute("position", "absolute");
    span.setInnerText(text);/*from   w  w w  . j av a 2  s . c o m*/
    int width = span.getOffsetWidth();
    RootPanel.get().getElement().removeChild(span);
    return width;
}

From source file:com.smartgwt.mobile.client.widgets.layout.NavigationBar.java

License:Open Source License

@SGWTInternal
public void _layOutMembers() {
    if (!isAttached())
        return;//from w ww.  ja  va  2 s  .com
    if (!isVisible() || !navStack.isVisible())
        return;
    if (layoutTimer == null) {
        layoutTimer = new Timer() {

            @Override
            public void run() {
                final float _1vw = Window.getClientWidth(); // 1 viewport width
                final NavigationItem item = ensureItem();
                final Header1 titleView = item.getTitleView();
                final float minTitleViewWidth, titleViewContentWidth;
                if (titleView == null) {
                    minTitleViewWidth = 0.0f;
                    titleViewContentWidth = 0.0f;
                } else {
                    final SpanElement span = titleView._getSpan();
                    span.getStyle().clearPadding();
                    final float titleViewWidth = ElementUtil.getOuterWidth(titleView.getElement(), true);
                    minTitleViewWidth = titleViewWidth - titleView.getElement().getClientWidth();
                    titleViewContentWidth = span.getOffsetWidth();
                }
                final float titleViewAutoWidth = minTitleViewWidth + titleViewContentWidth;
                final NavigationButton leftButton = item.getLeftButton();
                final Canvas rightBarItem = item.getRightBarItem();
                final float leftButtonHMargin, absoluteMinLeftButtonWidth, leftButtonContentWidth;
                if (leftButton == null) {
                    leftButtonHMargin = 0.0f;
                    absoluteMinLeftButtonWidth = 0.0f;
                    leftButtonContentWidth = 0.0f;
                } else {
                    final Element element = leftButton.getElement();
                    leftButtonHMargin = ElementUtil.getHMarginWidth(element);
                    absoluteMinLeftButtonWidth = ElementUtil.getMinMarginBoxWidth(element);
                    leftButtonContentWidth = leftButton._getContentWidth();
                }
                final float rightBarItemHMargin, absoluteMinRightBarItemWidth, rightBarItemContentWidth;
                if (rightBarItem == null) {
                    rightBarItemHMargin = 0.0f;
                    absoluteMinRightBarItemWidth = 0.0f;
                    rightBarItemContentWidth = 0.0f;
                } else {
                    final Element element = rightBarItem.getElement();
                    rightBarItemHMargin = ElementUtil.getHMarginWidth(element);
                    absoluteMinRightBarItemWidth = ElementUtil.getMinMarginBoxWidth(element);
                    rightBarItemContentWidth = ((NavigationBarItem) rightBarItem)._getContentWidth();
                }
                final float leftButtonAutoWidth = absoluteMinLeftButtonWidth + leftButtonContentWidth,
                        rightBarItemAutoWidth = absoluteMinRightBarItemWidth + rightBarItemContentWidth;

                final float totalWidth = titleViewAutoWidth + leftButtonAutoWidth + rightBarItemAutoWidth;

                float newLeftButtonMarginBoxWidth = 0.0f, newRightButtonMarginBoxWidth = 0.0f;
                if (totalWidth + 0.000001f >= _1vw) {
                    final float minLeftButtonWidth = leftButton == null ? 0.0f
                            : absoluteMinLeftButtonWidth + Math.min(53.0f, leftButtonContentWidth),
                            minRightBarItemWidth = rightBarItem == null ? 0.0f
                                    : absoluteMinRightBarItemWidth + (rightBarItem instanceof NavigationButtons
                                            ? rightBarItemContentWidth
                                            : Math.min(53.0f, rightBarItemContentWidth));
                    assert minLeftButtonWidth <= leftButtonAutoWidth + 0.000001f;
                    assert minRightBarItemWidth <= rightBarItemAutoWidth + 0.000001f;

                    final float r = _1vw - titleViewAutoWidth - minLeftButtonWidth - minRightBarItemWidth;
                    if (r < 0.000001f) {
                        newLeftButtonMarginBoxWidth = minLeftButtonWidth;
                        newRightButtonMarginBoxWidth = minRightBarItemWidth;
                    } else { // The title gets as much width as it needs while still giving the left and/or right
                             // buttons a minimum client width (at most 53px).  The remaining width is distributed to
                             // the left and right buttons.
                        if (leftButton != null || rightBarItem != null) {
                            final float denominator = leftButtonAutoWidth - minLeftButtonWidth
                                    + rightBarItemAutoWidth - minRightBarItemWidth;
                            if (denominator < 2.0f + 0.000001f) {
                                newLeftButtonMarginBoxWidth = minLeftButtonWidth;
                                newRightButtonMarginBoxWidth = minRightBarItemWidth;
                            } else {
                                newLeftButtonMarginBoxWidth = minLeftButtonWidth
                                        + (leftButtonAutoWidth - minLeftButtonWidth) / denominator * r;
                                newRightButtonMarginBoxWidth = minRightBarItemWidth
                                        + (rightBarItemAutoWidth - minRightBarItemWidth) / denominator * r;
                            }
                        }
                    }
                } else {
                    newLeftButtonMarginBoxWidth = leftButtonAutoWidth;
                    newRightButtonMarginBoxWidth = rightBarItemAutoWidth;

                    final float leftButtonExtra = Math.max(0.0f, leftButtonAutoWidth - rightBarItemAutoWidth),
                            rightButtonExtra = Math.max(0.0f, rightBarItemAutoWidth - leftButtonAutoWidth);
                    float r = _1vw - totalWidth;
                    assert r + 0.000001f >= 0.0f;
                    if (titleView != null) {
                        if (r + 0.000001f >= leftButtonExtra + rightButtonExtra) {
                            final float rightPadding = leftButtonExtra, leftPadding = rightButtonExtra;
                            final SpanElement span = titleView._getSpan();
                            final Style spanStyle = span.getStyle();
                            spanStyle.setPaddingRight(rightPadding, Unit.PX);
                            spanStyle.setPaddingLeft(leftPadding, Unit.PX);
                        } else {
                            if (leftButtonExtra + 0.000001f >= rightButtonExtra) {
                                final float rightPadding = r;
                                final SpanElement span = titleView._getSpan();
                                span.getStyle().setPaddingRight(rightPadding, Unit.PX);
                            } else {
                                final float leftPadding = r;
                                final SpanElement span = titleView._getSpan();
                                span.getStyle().setPaddingLeft(leftPadding, Unit.PX);
                            }
                        }
                    }
                }
                if (leftButton != null) {
                    ElementUtil.setBorderBoxWidth(leftButton.getElement(),
                            newLeftButtonMarginBoxWidth - leftButtonHMargin);
                }
                if (rightBarItem != null) {
                    ElementUtil.setBorderBoxWidth(rightBarItem.getElement(),
                            newRightButtonMarginBoxWidth - rightBarItemHMargin);
                }
                if (titleView != null) {
                    // Instead of setting the left and right padding, set the `left' and `right'
                    // CSS properties.
                    // There is a WebKit bug that affects iOS 6 where left and right padding
                    // is incorrectly factored into the calculation of how wide the <h1>
                    // element is for the purpose of centering the text.
                    // https://bugs.webkit.org/show_bug.cgi?id=75277
                    titleView.getElement().getStyle().setLeft(newLeftButtonMarginBoxWidth, Unit.PX);
                    titleView.getElement().getStyle().setRight(newRightButtonMarginBoxWidth, Unit.PX);
                    titleView.getElement().getStyle().clearVisibility();
                }

                layoutTimer = null;
            } // run()
        };
        layoutTimer.schedule(1);
    }
}