Search in sources :

Example 6 with Style

use of com.codename1.ui.plaf.Style in project CodenameOne by codenameone.

the class DefaultLookAndFeel method updateRadioButtonConstants.

private void updateRadioButtonConstants(UIManager m, boolean focus, String append) {
    Image radioSel = m.getThemeImageConstant("radioSelected" + append + "Image");
    if (radioSel != null) {
        Image radioUnsel = m.getThemeImageConstant("radioUnselected" + append + "Image");
        if (radioUnsel != null) {
            Image disUnsel = m.getThemeImageConstant("radioUnselectedDis" + append + "Image");
            Image disSel = m.getThemeImageConstant("radioSelectedDis" + append + "Image");
            if (disUnsel == null) {
                disUnsel = radioUnsel;
            }
            if (disSel == null) {
                disSel = radioSel;
            }
            if (focus) {
                setRadioButtonFocusImages(radioSel, radioUnsel, disSel, disUnsel);
            } else {
                setRadioButtonImages(radioSel, radioUnsel, disSel, disUnsel);
            }
        }
    } else {
        if (!Font.isTrueTypeFileSupported()) {
            return;
        }
        UIManager uim = UIManager.getInstance();
        Style unsel = uim.createStyle("RadioButton.", "", false);
        Style sel = uim.createStyle("RadioButton.", "sel#", true);
        Style dis = uim.createStyle("RadioButton.", "dis#", false);
        FontImage checkedDis = FontImage.createMaterial(FontImage.MATERIAL_RADIO_BUTTON_CHECKED, dis);
        FontImage uncheckedDis = FontImage.createMaterial(FontImage.MATERIAL_RADIO_BUTTON_UNCHECKED, sel);
        if (focus) {
            FontImage checkedSelected = FontImage.createMaterial(FontImage.MATERIAL_RADIO_BUTTON_CHECKED, sel);
            FontImage uncheckedSelected = FontImage.createMaterial(FontImage.MATERIAL_RADIO_BUTTON_UNCHECKED, sel);
            setRadioButtonFocusImages(checkedSelected, uncheckedSelected, checkedDis, uncheckedDis);
        } else {
            FontImage checkedUnselected = FontImage.createMaterial(FontImage.MATERIAL_RADIO_BUTTON_CHECKED, unsel);
            FontImage uncheckedUnselected = FontImage.createMaterial(FontImage.MATERIAL_RADIO_BUTTON_UNCHECKED, unsel);
            setRadioButtonImages(checkedUnselected, uncheckedUnselected, checkedDis, uncheckedDis);
        }
    }
}
Also used : Image(com.codename1.ui.Image) FontImage(com.codename1.ui.FontImage) FontImage(com.codename1.ui.FontImage)

Example 7 with Style

use of com.codename1.ui.plaf.Style in project CodenameOne by codenameone.

the class DefaultLookAndFeel method drawLabelText.

/**
 * Draws the text of a label
 *
 * @param g graphics context
 * @param l label component
 * @param text the text for the label
 * @param x position for the label
 * @param y position for the label
 * @param textSpaceW the width available for the component
 * @return the space used by the drawing
 */
protected int drawLabelText(Graphics g, Label l, String text, int x, int y, int textSpaceW) {
    Style style = l.getStyle();
    Font f = style.getFont();
    boolean rtl = l.isRTL();
    boolean isTickerRunning = l.isTickerRunning();
    int txtW = l.getStringWidth(f);
    if ((!isTickerRunning) || rtl) {
        // if there is no space to draw the text add ... at the end
        if (txtW > textSpaceW && textSpaceW > 0) {
            if (rtl) {
                if ((!isTickerRunning) && (l.isEndsWith3Points())) {
                    String points = "...";
                    int pointsW = f.stringWidth(points);
                    g.drawString(points, l.getShiftText() + x, y, l.getStyle().getTextDecoration());
                    g.clipRect(pointsW + l.getShiftText() + x, y, textSpaceW - pointsW, f.getHeight());
                }
                x = x - txtW + textSpaceW;
            } else {
                if (l.isEndsWith3Points()) {
                    String points = "...";
                    int index = 1;
                    int widest = f.charWidth('W');
                    int pointsW = f.stringWidth(points);
                    int tlen = text.length();
                    while (fastCharWidthCheck(text, index, textSpaceW - pointsW, widest, f) && index < tlen) {
                        index++;
                    }
                    text = text.substring(0, Math.min(text.length(), Math.max(1, index - 1))) + points;
                    txtW = f.stringWidth(text);
                }
            }
        }
    }
    g.drawString(text, l.getShiftText() + x, y, style.getTextDecoration());
    return Math.min(txtW, textSpaceW);
}
Also used : Font(com.codename1.ui.Font)

Example 8 with Style

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

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

use of com.codename1.ui.plaf.Style 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)

Aggregations

Style (com.codename1.ui.plaf.Style)103 Dimension (com.codename1.ui.geom.Dimension)28 Component (com.codename1.ui.Component)25 Image (com.codename1.ui.Image)20 Rectangle (com.codename1.ui.geom.Rectangle)13 Container (com.codename1.ui.Container)11 Font (com.codename1.ui.Font)11 FontImage (com.codename1.ui.FontImage)11 Border (com.codename1.ui.plaf.Border)8 Form (com.codename1.ui.Form)6 Label (com.codename1.ui.Label)6 ActionEvent (com.codename1.ui.events.ActionEvent)6 IOException (java.io.IOException)6 UIManager (com.codename1.ui.plaf.UIManager)5 Vector (java.util.Vector)5 Dialog (com.codename1.ui.Dialog)4 EncodedImage (com.codename1.ui.EncodedImage)4 Graphics (com.codename1.ui.Graphics)4 TextArea (com.codename1.ui.TextArea)4 SpanButton (com.codename1.components.SpanButton)3