Search in sources :

Example 86 with Container

use of com.codename1.ui.Container in project CodenameOne by codenameone.

the class CoordinateLayout method layoutContainer.

/**
 * {@inheritDoc}
 */
public void layoutContainer(Container parent) {
    if (width < 0) {
        return;
    }
    int numOfcomponents = parent.getComponentCount();
    int parentW = parent.getWidth();
    int parentH = parent.getHeight();
    for (int i = 0; i < numOfcomponents; i++) {
        Component cmp = parent.getComponentAt(i);
        int x = cmp.getX() * parentW / width;
        int y = cmp.getY() * parentH / height;
        cmp.setX(x);
        cmp.setY(y);
        cmp.setWidth(cmp.getPreferredW());
        cmp.setHeight(cmp.getPreferredH());
    }
    width = parentW;
    height = parentH;
}
Also used : Component(com.codename1.ui.Component)

Example 87 with Container

use of com.codename1.ui.Container in project CodenameOne by codenameone.

the class FlowLayout method layoutContainer.

/**
 * {@inheritDoc}
 */
public void layoutContainer(Container parent) {
    Style s = parent.getStyle();
    int x = s.getPaddingLeft(parent.isRTL());
    int width = parent.getLayoutWidth() - parent.getSideGap() - s.getPaddingLeft(parent.isRTL()) - x;
    boolean rtl = parent.isRTL();
    if (rtl) {
        x += parent.getSideGap();
    }
    int initX = x;
    int y = s.getPaddingTop();
    int rowH = 0;
    int start = 0;
    int rowBaseline = 0;
    int maxComponentWidth = width;
    int numOfcomponents = parent.getComponentCount();
    for (int i = 0; i < numOfcomponents; i++) {
        Component cmp = parent.getComponentAt(i);
        Style style = cmp.getStyle();
        int marginX = style.getMarginLeftNoRTL() + style.getMarginRightNoRTL();
        cmp.setWidth(Math.min(maxComponentWidth - marginX, cmp.getPreferredW()));
        cmp.setHeight(cmp.getPreferredH());
        if ((x == s.getPaddingLeft(rtl)) || (x + cmp.getPreferredW() <= width)) {
            // We take the actual LEFT since drawing is done in reverse
            x += cmp.getStyle().getMarginLeftNoRTL();
            if (rtl) {
                cmp.setX(Math.max(width + initX - (x - initX) - cmp.getPreferredW(), style.getMarginLeftNoRTL()));
            } else {
                cmp.setX(x);
            }
            cmp.setY(y + cmp.getStyle().getMarginTop());
            x += cmp.getWidth() + cmp.getStyle().getMarginRightNoRTL();
            rowH = Math.max(rowH, cmp.getHeight() + cmp.getStyle().getMarginTop() + cmp.getStyle().getMarginBottom());
            if (valign == Component.BASELINE) {
                int cmpPrefH = cmp.getPreferredH();
                int cmpBaseline = cmp.getBaseline(cmp.getPreferredW(), cmpPrefH);
                rowBaseline = Math.max(rowBaseline, cmpBaseline + cmp.getStyle().getMarginTop());
                rowH = Math.max(rowH, rowBaseline + cmp.getStyle().getMarginBottom() + cmpPrefH - cmpBaseline);
            }
        } else {
            moveComponents(parent, s.getPaddingLeft(rtl), y, width - s.getPaddingLeft(rtl) - x, rowH, start, i, rowBaseline);
            if (fillRows) {
                fillRow(parent, width, start, i);
            }
            x = initX + cmp.getStyle().getMarginLeftNoRTL();
            y += rowH;
            rowBaseline = 0;
            if (rtl) {
                cmp.setX(Math.max(width + initX - (x - initX) - cmp.getPreferredW(), style.getMarginLeftNoRTL()));
            } else {
                cmp.setX(x);
            }
            cmp.setY(y + cmp.getStyle().getMarginTop());
            rowH = cmp.getPreferredH() + cmp.getStyle().getMarginTop() + cmp.getStyle().getMarginBottom();
            if (valign == Component.BASELINE) {
                int cmpPrefH = cmp.getPreferredH();
                int cmpBaseline = cmp.getBaseline(cmp.getPreferredW(), cmpPrefH);
                rowBaseline = Math.max(rowBaseline, cmpBaseline + cmp.getStyle().getMarginTop());
                rowH = Math.max(rowH, rowBaseline + cmp.getStyle().getMarginBottom() + cmpPrefH - cmpBaseline);
            }
            x += cmp.getPreferredW() + cmp.getStyle().getMarginRightNoRTL();
            start = i;
        }
    }
    moveComponents(parent, s.getPaddingLeft(rtl), y, width - s.getPaddingLeft(rtl) - x, rowH, start, numOfcomponents, rowBaseline);
    if (fillRows) {
        fillRow(parent, width, start, numOfcomponents);
    }
}
Also used : Style(com.codename1.ui.plaf.Style) Component(com.codename1.ui.Component)

Example 88 with Container

use of com.codename1.ui.Container in project CodenameOne by codenameone.

the class FlowLayout method getPreferredSize.

/**
 * {@inheritDoc}
 */
public Dimension getPreferredSize(Container parent) {
    int parentWidth = parent.getWidth();
    if (parentWidth == 0) {
        parent.invalidate();
    }
    int width = 0;
    int height = 0;
    int w = 0;
    int numOfcomponents = parent.getComponentCount();
    Style parentStyle = parent.getStyle();
    int parentPadding = parentStyle.getHorizontalPadding();
    for (int i = 0; i < numOfcomponents; i++) {
        Component cmp = parent.getComponentAt(i);
        height = Math.max(height, cmp.getPreferredH() + cmp.getStyle().getMarginTop() + cmp.getStyle().getMarginBottom());
        int prefW = cmp.getPreferredW() + cmp.getStyle().getMarginRightNoRTL() + cmp.getStyle().getMarginLeftNoRTL();
        w += prefW;
        // we need to break a line
        if (parentWidth > parentPadding && w > parentWidth && i > 0) {
            height += cmp.getPreferredH() + cmp.getStyle().getMarginTop() + cmp.getStyle().getMarginBottom();
            width = Math.max(w, width);
            w = prefW;
        }
    }
    width = Math.max(w, width);
    dim.setWidth(width + parent.getStyle().getPaddingLeftNoRTL() + parent.getStyle().getPaddingRightNoRTL());
    dim.setHeight(height + parent.getStyle().getPaddingTop() + parent.getStyle().getPaddingBottom());
    return dim;
}
Also used : Style(com.codename1.ui.plaf.Style) Component(com.codename1.ui.Component)

Example 89 with Container

use of com.codename1.ui.Container in project CodenameOne by codenameone.

the class FlowLayout method fillRow.

/**
 * This method tries to fill up the available space in a row.
 * This method is called if isFillRows() returns true.
 *
 * @param target the parent container
 * @param width the width of the row to fill
 * @param start the index of the first component in this row
 * @param end the index of the last component in this row
 */
protected void fillRow(Container target, int width, int start, int end) {
    int available = width;
    for (int iter = start; iter < end; iter++) {
        Component c = target.getComponentAt(iter);
        available -= (c.getWidth() + c.getStyle().getMarginRightNoRTL() + c.getStyle().getMarginLeftNoRTL());
    }
    if (available > 0 && end - start > 0) {
        int perComponent = available / (end - start);
        int lastComponent = perComponent + available % (end - start);
        if (perComponent > 0) {
            int addOffset = 0;
            boolean rtl = target.isRTL();
            for (int iter = start; iter < end - 1; iter++) {
                Component c = target.getComponentAt(iter);
                c.setWidth(c.getWidth() + perComponent);
                if (rtl) {
                    addOffset += perComponent;
                    c.setX(c.getX() - addOffset);
                } else {
                    c.setX(c.getX() + addOffset);
                    addOffset += perComponent;
                }
            }
            Component c = target.getComponentAt(end - 1);
            if (rtl) {
                addOffset += lastComponent;
                c.setX(c.getX() - addOffset);
            } else {
                c.setX(c.getX() + addOffset);
            }
            c.setWidth(c.getWidth() + lastComponent);
        } else {
            Component c = target.getComponentAt(end - 1);
            c.setWidth(c.getWidth() + lastComponent);
        }
    }
}
Also used : Component(com.codename1.ui.Component)

Example 90 with Container

use of com.codename1.ui.Container in project CodenameOne by codenameone.

the class GridBagLayoutInfo method getPreferredSize.

public Dimension getPreferredSize(Container parent) {
    Style s = parent.getStyle();
    ParentInfo info = lastParentInfo = getParentInfo(parent);
    if (getComponentsNumber(parent) == 0) {
        return new Dimension(s.getHorizontalPadding(), s.getVerticalPadding());
    }
    try {
        validate(parent, info);
    } catch (RuntimeException e) {
        // awt.87=PreferredLayoutSize: {0}
        Log.e(e);
        // $NON-NLS-1$
        throw new IllegalArgumentException("PreferredLayoutSize: " + e.getMessage());
    }
    Dimension d = info.grid.preferredSize();
    d.setWidth(d.getWidth() + s.getHorizontalPadding());
    d.setHeight(d.getHeight() + s.getVerticalPadding());
    return d;
// return addInsets(grid.preferredSize(), parent);
}
Also used : Style(com.codename1.ui.plaf.Style) Dimension(com.codename1.ui.geom.Dimension)

Aggregations

Container (com.codename1.ui.Container)85 Component (com.codename1.ui.Component)65 BorderLayout (com.codename1.ui.layouts.BorderLayout)46 Style (com.codename1.ui.plaf.Style)38 ActionEvent (com.codename1.ui.events.ActionEvent)29 Form (com.codename1.ui.Form)26 Label (com.codename1.ui.Label)21 BoxLayout (com.codename1.ui.layouts.BoxLayout)21 Dimension (com.codename1.ui.geom.Dimension)20 ActionListener (com.codename1.ui.events.ActionListener)19 Button (com.codename1.ui.Button)15 FlowLayout (com.codename1.ui.layouts.FlowLayout)15 ArrayList (java.util.ArrayList)14 LayeredLayout (com.codename1.ui.layouts.LayeredLayout)12 TextArea (com.codename1.ui.TextArea)11 Point (com.codename1.ui.geom.Point)11 Rectangle (com.codename1.ui.geom.Rectangle)11 Dialog (com.codename1.ui.Dialog)10 RadioButton (com.codename1.ui.RadioButton)10 Vector (java.util.Vector)9