Search in sources :

Example 6 with Font

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

the class DefaultLookAndFeel method getTextAreaSize.

/**
 * {@inheritDoc}
 */
public Dimension getTextAreaSize(TextArea ta, boolean pref) {
    int prefW = 0;
    int prefH = 0;
    Style style = ta.getStyle();
    Font f = style.getFont();
    // if this is a text field the preferred size should be the text width
    if (ta.getRows() == 1) {
        prefW = f.stringWidth(ta.getText());
    } else {
        prefW = f.charWidth(TextArea.getWidestChar()) * ta.getColumns();
    }
    int rows;
    if (pref) {
        rows = ta.getActualRows();
    } else {
        rows = ta.getLines();
    }
    prefH = (f.getHeight() + ta.getRowsGap()) * rows;
    if (!ta.isActAsLabel()) {
        int columns = ta.getColumns();
        String str = "";
        for (int iter = 0; iter < columns; iter++) {
            str += TextArea.getWidestChar();
        }
        if (columns > 0) {
            prefW = Math.max(prefW, f.stringWidth(str));
        }
    }
    prefH = Math.max(prefH, rows * f.getHeight());
    prefW += style.getPaddingRightNoRTL() + style.getPaddingLeftNoRTL();
    prefH += style.getPaddingTop() + style.getPaddingBottom();
    if (style.getBorder() != null) {
        prefW = Math.max(style.getBorder().getMinimumWidth(), prefW);
        prefH = Math.max(style.getBorder().getMinimumHeight(), prefH);
    }
    if (isBackgroundImageDetermineSize() && style.getBgImage() != null) {
        prefW = Math.max(style.getBgImage().getWidth(), prefW);
        prefH = Math.max(style.getBgImage().getHeight(), prefH);
    }
    return new Dimension(prefW, prefH);
}
Also used : Dimension(com.codename1.ui.geom.Dimension) Font(com.codename1.ui.Font)

Example 7 with Font

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

the class DefaultLookAndFeel method drawTextField.

/**
 * {@inheritDoc}
 */
public void drawTextField(Graphics g, TextArea ta) {
    setFG(g, ta);
    // display ******** if it is a password field
    String displayText = getTextFieldString(ta);
    Style style = ta.getStyle();
    int x = 0;
    // ta.getCursorX();
    int cursorCharPosition = ta.getCursorPosition();
    Font f = style.getFont();
    int cursorX = 0;
    int xPos = 0;
    int align = reverseAlignForBidi(ta);
    int displayX = 0;
    String inputMode = ta.getInputMode();
    int inputModeWidth = f.stringWidth(inputMode);
    // QWERTY devices don't quite have an input mode hide it also when we have a VK
    if (ta.isQwertyInput() || Display.getInstance().isVirtualKeyboardShowing()) {
        inputMode = "";
        inputModeWidth = 0;
    }
    if (ta.isSingleLineTextArea()) {
        // there is currently no support for CENTER aligned text fields
        if (align == Component.LEFT) {
            if (cursorCharPosition > 0) {
                cursorCharPosition = Math.min(displayText.length(), cursorCharPosition);
                xPos = f.stringWidth(displayText.substring(0, cursorCharPosition));
                cursorX = ta.getX() + style.getPaddingLeft(ta.isRTL()) + xPos;
                // no point in showing the input mode when there is only one input mode...
                if (inputModeWidth > 0 && ta.getInputModeOrder() != null && ta.getInputModeOrder().length == 1) {
                    inputModeWidth = 0;
                }
                if (ta.isEnableInputScroll()) {
                    if (ta.getWidth() > (f.getHeight() * 2) && cursorX >= ta.getWidth() - inputModeWidth - style.getPaddingLeft(ta.isRTL())) {
                        if (x + xPos >= ta.getWidth() - inputModeWidth - style.getPaddingLeft(ta.isRTL()) * 2) {
                            x = ta.getWidth() - inputModeWidth - style.getPaddingLeft(ta.isRTL()) * 2 - xPos - 1;
                        }
                    }
                }
            }
            displayX = ta.getX() + x + style.getPaddingLeft(ta.isRTL());
        } else {
            x = 0;
            cursorX = getTextFieldCursorX(ta);
            int baseX = ta.getX() + style.getPaddingLeftNoRTL() + inputModeWidth;
            int endX = ta.getX() + ta.getWidth() - style.getPaddingRightNoRTL();
            if (cursorX < baseX) {
                x = baseX - cursorX;
            } else {
                if (cursorX > endX) {
                    x = endX - cursorX;
                }
            }
            displayX = ta.getX() + ta.getWidth() - style.getPaddingRightNoRTL() - style.getPaddingLeftNoRTL() - f.stringWidth(displayText) + x;
        }
        int cx = g.getClipX();
        int cy = g.getClipY();
        int cw = g.getClipWidth();
        int ch = g.getClipHeight();
        int clipx = ta.getX() + style.getPaddingLeft(ta.isRTL());
        int clipw = ta.getWidth() - style.getPaddingLeft(ta.isRTL()) - style.getPaddingRight(ta.isRTL());
        // g.pushClip();
        g.clipRect(clipx, cy, clipw, ch);
        switch(ta.getVerticalAlignment()) {
            case Component.BOTTOM:
                g.drawString(displayText, displayX, ta.getY() + ta.getHeight() - style.getPaddingBottom() - f.getHeight(), style.getTextDecoration());
                break;
            case Component.CENTER:
                g.drawString(displayText, displayX, ta.getY() + ta.getHeight() / 2 - f.getHeight() / 2, style.getTextDecoration());
                break;
            default:
                g.drawString(displayText, displayX, ta.getY() + style.getPaddingTop(), style.getTextDecoration());
                break;
        }
        g.setClip(cx, cy, cw, ch);
    // g.popClip();
    } else {
        drawTextArea(g, ta);
    }
    // no point in showing the input mode when there is only one input mode...
    if (inputModeWidth > 0 && ta.getInputModeOrder() != null && ta.getInputModeOrder().length > 1) {
        if (ta.handlesInput() && ta.getWidth() / 2 > inputModeWidth) {
            int drawXPos = ta.getX() + style.getPaddingLeft(ta.isRTL());
            if ((!ta.isRTL() && style.getAlignment() == Component.LEFT) || (ta.isRTL() && style.getAlignment() == Component.RIGHT)) {
                drawXPos = drawXPos + ta.getWidth() - inputModeWidth - style.getPaddingRightNoRTL() - style.getPaddingLeftNoRTL();
            }
            g.setColor(style.getFgColor());
            int inputIndicatorY = ta.getY() + ta.getScrollY() + ta.getHeight() - style.getPaddingBottom() - f.getHeight();
            g.fillRect(drawXPos, inputIndicatorY, inputModeWidth, f.getHeight(), (byte) 140);
            g.setColor(style.getBgColor());
            g.drawString(inputMode, drawXPos, inputIndicatorY);
        }
    }
}
Also used : Font(com.codename1.ui.Font)

Example 8 with Font

use of com.codename1.ui.Font 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 9 with Font

use of com.codename1.ui.Font 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 10 with Font

use of com.codename1.ui.Font 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

Font (com.codename1.ui.Font)30 Image (com.codename1.ui.Image)12 Hashtable (java.util.Hashtable)10 EditorFont (com.codename1.ui.EditorFont)8 Border (com.codename1.ui.plaf.Border)8 Component (com.codename1.ui.Component)7 Style (com.codename1.ui.plaf.Style)7 EncodedImage (com.codename1.ui.EncodedImage)6 TextArea (com.codename1.ui.TextArea)6 AnimationObject (com.codename1.ui.animations.AnimationObject)6 BufferedImage (java.awt.image.BufferedImage)6 IOException (java.io.IOException)6 java.awt (java.awt)5 Container (com.codename1.ui.Container)4 Dimension (com.codename1.ui.geom.Dimension)4 RoundBorder (com.codename1.ui.plaf.RoundBorder)4 RoundRectBorder (com.codename1.ui.plaf.RoundRectBorder)4 ParseException (java.text.ParseException)4 Paint (android.graphics.Paint)3 EditorTTFFont (com.codename1.ui.EditorTTFFont)3