Search in sources :

Example 1 with Button

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

the class GenericListCellRenderer method extractLastClickedComponent.

/**
 * Allows partitioning the renderer into "areas" that can be clicked. When
 * receiving an action event in the list this method allows a developer to
 * query the renderer to "see" whether a button within the component was "touched"
 * by the user on a touch screen device.
 * This method will reset the value to null after returning a none-null value!
 *
 * @return a button or null
 */
public Button extractLastClickedComponent() {
    Button c = lastClickedComponent;
    lastClickedComponent = null;
    return c;
}
Also used : RadioButton(com.codename1.ui.RadioButton) Button(com.codename1.ui.Button)

Example 2 with Button

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

the class DefaultLookAndFeel method drawComponent.

private void drawComponent(Graphics g, Label l, Image icon, Image stateIcon, int preserveSpaceForState) {
    setFG(g, l);
    int gap = l.getGap();
    int stateIconSize = 0;
    int stateIconYPosition = 0;
    String text = l.getText();
    Style style = l.getStyle();
    int cmpX = l.getX();
    int cmpY = l.getY();
    int cmpHeight = l.getHeight();
    int cmpWidth = l.getWidth();
    boolean rtl = l.isRTL();
    int leftPadding = style.getPaddingLeft(rtl);
    int rightPadding = style.getPaddingRight(rtl);
    int topPadding = style.getPaddingTop();
    int bottomPadding = style.getPaddingBottom();
    Font font = style.getFont();
    int fontHeight = 0;
    if (text == null) {
        text = "";
    }
    if (text.length() > 0) {
        fontHeight = font.getHeight();
    }
    int x = cmpX + leftPadding;
    int y = cmpY + topPadding;
    boolean opposite = false;
    if (stateIcon != null) {
        // square image width == height
        stateIconSize = stateIcon.getWidth();
        preserveSpaceForState = stateIconSize + gap;
        stateIconYPosition = cmpY + topPadding + (cmpHeight - topPadding - bottomPadding) / 2 - stateIconSize / 2;
        int tX = cmpX;
        if (((Button) l).isOppositeSide()) {
            if (rtl) {
                tX += leftPadding;
            } else {
                tX = tX + cmpWidth - leftPadding - stateIconSize;
            }
            cmpWidth -= leftPadding - stateIconSize;
            preserveSpaceForState = 0;
            opposite = true;
        } else {
            x = cmpX + leftPadding + preserveSpaceForState;
            if (rtl) {
                tX = tX + cmpWidth - leftPadding - stateIconSize;
            } else {
                tX += leftPadding;
            }
        }
        g.drawImage(stateIcon, tX, stateIconYPosition);
    }
    // default for bottom left alignment
    int align = reverseAlignForBidi(l, style.getAlignment());
    int textPos = reverseAlignForBidi(l, l.getTextPosition());
    // set initial x,y position according to the alignment and textPosition
    if (align == Component.LEFT) {
        switch(textPos) {
            case Label.LEFT:
            case Label.RIGHT:
                y = y + (cmpHeight - (topPadding + bottomPadding + Math.max(((icon != null) ? icon.getHeight() : 0), fontHeight))) / 2;
                break;
            case Label.BOTTOM:
            case Label.TOP:
                y = y + (cmpHeight - (topPadding + bottomPadding + ((icon != null) ? icon.getHeight() + gap : 0) + fontHeight)) / 2;
                break;
        }
    } else if (align == Component.CENTER) {
        switch(textPos) {
            case Label.LEFT:
            case Label.RIGHT:
                x = x + (cmpWidth - (preserveSpaceForState + leftPadding + rightPadding + ((icon != null) ? icon.getWidth() + l.getGap() : 0) + l.getStringWidth(font))) / 2;
                if (!opposite) {
                    x = Math.max(x, cmpX + leftPadding + preserveSpaceForState);
                } else {
                    x = Math.min(x, cmpX + leftPadding + preserveSpaceForState);
                }
                y = y + (cmpHeight - (topPadding + bottomPadding + Math.max(((icon != null) ? icon.getHeight() : 0), fontHeight))) / 2;
                break;
            case Label.BOTTOM:
            case Label.TOP:
                x = x + (cmpWidth - (preserveSpaceForState + leftPadding + rightPadding + Math.max(((icon != null) ? icon.getWidth() + l.getGap() : 0), l.getStringWidth(font)))) / 2;
                if (!opposite) {
                    x = Math.max(x, cmpX + leftPadding + preserveSpaceForState);
                } else {
                    x = Math.min(x, cmpX + leftPadding + preserveSpaceForState);
                }
                y = y + (cmpHeight - (topPadding + bottomPadding + ((icon != null) ? icon.getHeight() + gap : 0) + fontHeight)) / 2;
                break;
        }
    } else if (align == Component.RIGHT) {
        switch(textPos) {
            case Label.LEFT:
            case Label.RIGHT:
                x = cmpX + cmpWidth - rightPadding - (((icon != null) ? (icon.getWidth() + gap) : 0) + l.getStringWidth(font));
                if (l.isRTL()) {
                    if (!opposite) {
                        x = Math.max(x - preserveSpaceForState, cmpX + leftPadding);
                    } else {
                        x = Math.min(x - preserveSpaceForState, cmpX + leftPadding);
                    }
                } else {
                    if (!opposite) {
                        x = Math.max(x, cmpX + leftPadding + preserveSpaceForState);
                    } else {
                        x = Math.min(x, cmpX + leftPadding + preserveSpaceForState);
                    }
                }
                y = y + (cmpHeight - (topPadding + bottomPadding + Math.max(((icon != null) ? icon.getHeight() : 0), fontHeight))) / 2;
                break;
            case Label.BOTTOM:
            case Label.TOP:
                x = cmpX + cmpWidth - rightPadding - (Math.max(((icon != null) ? (icon.getWidth()) : 0), l.getStringWidth(font)));
                if (!opposite) {
                    x = Math.max(x, cmpX + leftPadding + preserveSpaceForState);
                } else {
                    x = Math.min(x, cmpX + leftPadding + preserveSpaceForState);
                }
                y = y + (cmpHeight - (topPadding + bottomPadding + ((icon != null) ? icon.getHeight() + gap : 0) + fontHeight)) / 2;
                break;
        }
    }
    int textSpaceW = cmpWidth - rightPadding - leftPadding;
    if (icon != null && (textPos == Label.RIGHT || textPos == Label.LEFT)) {
        textSpaceW = textSpaceW - icon.getWidth();
    }
    if (stateIcon != null) {
        textSpaceW = textSpaceW - stateIconSize;
    } else {
        textSpaceW = textSpaceW - preserveSpaceForState;
    }
    if (icon == null) {
        // no icon only string
        drawLabelString(g, l, text, x, y, textSpaceW);
    } else {
        int strWidth = l.getStringWidth(font);
        int iconWidth = icon.getWidth();
        int iconHeight = icon.getHeight();
        int iconStringWGap;
        int iconStringHGap;
        switch(textPos) {
            case Label.LEFT:
                if (iconHeight > fontHeight) {
                    iconStringHGap = (iconHeight - fontHeight) / 2;
                    strWidth = drawLabelStringValign(g, l, text, x, y, iconStringHGap, iconHeight, textSpaceW, fontHeight);
                    g.drawImage(icon, x + strWidth + gap, y);
                } else {
                    iconStringHGap = (fontHeight - iconHeight) / 2;
                    strWidth = drawLabelString(g, l, text, x, y, textSpaceW);
                    g.drawImage(icon, x + strWidth + gap, y + iconStringHGap);
                }
                break;
            case Label.RIGHT:
                if (iconHeight > fontHeight) {
                    iconStringHGap = (iconHeight - fontHeight) / 2;
                    g.drawImage(icon, x, y);
                    drawLabelStringValign(g, l, text, x + iconWidth + gap, y, iconStringHGap, iconHeight, textSpaceW, fontHeight);
                } else {
                    iconStringHGap = (fontHeight - iconHeight) / 2;
                    g.drawImage(icon, x, y + iconStringHGap);
                    drawLabelString(g, l, text, x + iconWidth + gap, y, textSpaceW);
                }
                break;
            case Label.BOTTOM:
                if (iconWidth > strWidth) {
                    // center align the smaller
                    iconStringWGap = (iconWidth - strWidth) / 2;
                    g.drawImage(icon, x, y);
                    drawLabelString(g, l, text, x + iconStringWGap, y + iconHeight + gap, textSpaceW);
                } else {
                    iconStringWGap = (Math.min(strWidth, textSpaceW) - iconWidth) / 2;
                    g.drawImage(icon, x + iconStringWGap, y);
                    drawLabelString(g, l, text, x, y + iconHeight + gap, textSpaceW);
                }
                break;
            case Label.TOP:
                if (iconWidth > strWidth) {
                    // center align the smaller
                    iconStringWGap = (iconWidth - strWidth) / 2;
                    drawLabelString(g, l, text, x + iconStringWGap, y, textSpaceW);
                    g.drawImage(icon, x, y + fontHeight + gap);
                } else {
                    iconStringWGap = (Math.min(strWidth, textSpaceW) - iconWidth) / 2;
                    drawLabelString(g, l, text, x, y, textSpaceW);
                    g.drawImage(icon, x + iconStringWGap, y + fontHeight + gap);
                }
                break;
        }
    }
}
Also used : Button(com.codename1.ui.Button) Font(com.codename1.ui.Font)

Example 3 with Button

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

the class DefaultLookAndFeel method drawRadioButton.

/**
 * {@inheritDoc}
 */
public void drawRadioButton(Graphics g, Button rb) {
    if (rButtonImages != null) {
        Image x;
        if (rButtonImagesFocus != null && rButtonImagesFocus[0] != null && rb.hasFocus() && Display.getInstance().shouldRenderSelection(rb)) {
            if (rb.isEnabled()) {
                x = rButtonImagesFocus[rb.isSelected() ? 1 : 0];
            } else {
                x = rButtonImagesFocus[rb.isSelected() ? 3 : 2];
            }
        } else {
            if (rb.isEnabled()) {
                x = rButtonImages[rb.isSelected() ? 1 : 0];
            } else {
                x = rButtonImages[rb.isSelected() ? 3 : 2];
            }
        }
        drawComponent(g, rb, rb.getIconFromState(), x, 0);
    } else {
        Style style = rb.getStyle();
        // radio button radius needs to be of the size of the font height even
        // when no text is in the radio button this is a good indication of phone DPI
        int height = rb.getStyle().getFont().getHeight();
        drawComponent(g, rb, rb.getIconFromState(), null, height + rb.getGap());
        g.setColor(style.getFgColor());
        int x = rb.getX();
        if (rb.isRTL()) {
            x = x + rb.getWidth() - style.getPaddingLeft(rb.isRTL()) - height;
        } else {
            x += style.getPaddingLeft(rb.isRTL());
        }
        int y = rb.getY();
        // center the RadioButton
        y += Math.max(0, rb.getHeight() / 2 - height / 2);
        g.drawArc(x, y, height, height, 0, 360);
        if (rb.isSelected()) {
            int color = g.getColor();
            int destColor = findDestColor(color);
            g.fillRadialGradient(color, destColor, x + 3, y + 3, height - 5, height - 5);
        }
    }
}
Also used : Image(com.codename1.ui.Image) FontImage(com.codename1.ui.FontImage)

Example 4 with Button

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

the class DefaultLookAndFeel method drawCheckBox.

/**
 * {@inheritDoc}
 */
public void drawCheckBox(Graphics g, Button cb) {
    if (chkBoxImages != null) {
        Image x;
        if (chkBoxImagesFocus != null && chkBoxImagesFocus[0] != null && cb.hasFocus() && Display.getInstance().shouldRenderSelection(cb)) {
            if (cb.isEnabled()) {
                x = chkBoxImagesFocus[cb.isSelected() ? 1 : 0];
            } else {
                x = chkBoxImagesFocus[cb.isSelected() ? 3 : 2];
            }
        } else {
            if (cb.isEnabled()) {
                x = chkBoxImages[cb.isSelected() ? 1 : 0];
            } else {
                x = chkBoxImages[cb.isSelected() ? 3 : 2];
            }
        }
        drawComponent(g, cb, cb.getIconFromState(), x, 0);
    } else {
        Style style = cb.getStyle();
        // checkbox square needs to be the height and width of the font height even
        // when no text is in the check box this is a good indication of phone DPI
        int height = cb.getStyle().getFont().getHeight();
        drawComponent(g, cb, cb.getIconFromState(), null, height);
        int gradientColor;
        g.setColor(style.getFgColor());
        gradientColor = style.getBgColor();
        int width = height;
        int rectWidth = scaleCoordinate(12f, 16, width);
        int tX = cb.getX();
        if (cb.isRTL()) {
            tX = tX + cb.getWidth() - style.getPaddingLeft(cb.isRTL()) - rectWidth;
        } else {
            tX += style.getPaddingLeft(cb.isRTL());
        }
        int tY = cb.getY() + style.getPaddingTop() + (cb.getHeight() - style.getPaddingTop() - style.getPaddingBottom()) / 2 - height / 2;
        g.translate(tX, tY);
        int x = scaleCoordinate(1.04f, 16, width);
        int y = scaleCoordinate(4.0f, 16, height);
        int rectHeight = scaleCoordinate(12f, 16, height);
        // brighten or darken the color slightly
        int destColor = findDestColor(gradientColor);
        g.fillLinearGradient(gradientColor, destColor, x + 1, y + 1, rectWidth - 2, rectHeight - 1, false);
        g.drawRoundRect(x, y, rectWidth, rectHeight, 5, 5);
        if (cb.isSelected()) {
            int color = g.getColor();
            g.setColor(0x111111);
            g.translate(0, 1);
            fillCheckbox(g, width, height);
            g.setColor(color);
            g.translate(0, -1);
            fillCheckbox(g, width, height);
        }
        g.translate(-tX, -tY);
    }
}
Also used : Image(com.codename1.ui.Image) FontImage(com.codename1.ui.FontImage)

Example 5 with Button

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

the class DefaultLookAndFeel method getRadioButtonPreferredSize.

/**
 * {@inheritDoc}
 */
public Dimension getRadioButtonPreferredSize(Button rb) {
    if (rb.isToggle()) {
        return getButtonPreferredSize(rb);
    }
    threeImageCache[0] = rb.getMaskedIcon();
    threeImageCache[1] = rb.getRolloverIcon();
    threeImageCache[2] = rb.getPressedIcon();
    if (rButtonImages != null) {
        return getPreferredSize(rb, threeImageCache, rButtonImages[0]);
    }
    Dimension d = getPreferredSize(rb, threeImageCache, null);
    // radio button radius needs to be of the size of the font height even
    // when no text is in the radio button this is a good indication of phone DPI
    int height = rb.getStyle().getFont().getHeight();
    // allow for radio buttons without a string within them
    d.setHeight(Math.max(height, d.getHeight()));
    if (rButtonImages != null && rButtonImages.length > 0) {
        d.setWidth(rButtonImages[0].getWidth() + d.getWidth() + rb.getGap());
    } else {
        d.setWidth(d.getWidth() + height + rb.getGap());
    }
    return d;
}
Also used : Dimension(com.codename1.ui.geom.Dimension)

Aggregations

Button (com.codename1.ui.Button)31 BorderLayout (com.codename1.ui.layouts.BorderLayout)25 ActionEvent (com.codename1.ui.events.ActionEvent)21 Container (com.codename1.ui.Container)18 ActionListener (com.codename1.ui.events.ActionListener)17 Component (com.codename1.ui.Component)14 RadioButton (com.codename1.ui.RadioButton)14 Form (com.codename1.ui.Form)12 Label (com.codename1.ui.Label)11 BoxLayout (com.codename1.ui.layouts.BoxLayout)11 TextArea (com.codename1.ui.TextArea)10 Hashtable (java.util.Hashtable)9 Command (com.codename1.ui.Command)8 Image (com.codename1.ui.Image)8 TextField (com.codename1.ui.TextField)8 FlowLayout (com.codename1.ui.layouts.FlowLayout)8 GridLayout (com.codename1.ui.layouts.GridLayout)8 UIManager (com.codename1.ui.plaf.UIManager)8 CheckBox (com.codename1.ui.CheckBox)7 Style (com.codename1.ui.plaf.Style)7