Search in sources :

Example 36 with Switch

use of com.codename1.components.Switch in project CodenameOne by codenameone.

the class DefaultLookAndFeel method calculateTextFieldSpan.

public Spans calculateTextFieldSpan(TextSelection sel, TextArea ta) {
    // setFG(g, ta);
    Span out = sel.newSpan(ta);
    // display ******** if it is a password field
    String displayText = getTextFieldString(ta);
    Style style = ta.getStyle();
    int x = 0;
    // ta.getCursorX();
    int cursorCharPosition = ta.hasFocus() ? ta.getCursorPosition() : 0;
    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 (!Display.getInstance().platformUsesInputMode() || 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);
        // int xOffset = 0;
        // int len = displayText.length();
        // int charH = f.getHeight();
        int h = getSelectionHeight(f);
        switch(ta.getVerticalAlignment()) {
            case Component.BOTTOM:
                // g.drawString(displayText, displayX, ta.getY() + ta.getHeight() - style.getPaddingBottom() - f.getHeight(), style.getTextDecoration());
                append(sel, ta, out, displayText, f, 0, displayX, ta.getY() + ta.getHeight() - style.getPaddingBottom() - h, h);
                // c = sel.newChar(i, charX, ta.getY() + ta.getHeight() - style.getPaddingBottom() - f.getHeight(), charW , charH);
                break;
            case Component.CENTER:
                // g.drawString(displayText, displayX, ta.getY() + ta.getHeight() / 2  - f.getHeight() / 2, style.getTextDecoration());
                // c = sel.newChar(i, charX, ta.getY() + ta.getHeight() / 2  - f.getHeight() / 2, charW, charH);
                append(sel, ta, out, displayText, f, 0, displayX, ta.getY() + ta.getHeight() / 2 - h / 2, h);
                break;
            default:
                // g.drawString(displayText, displayX, ta.getY() + style.getPaddingTop(), style.getTextDecoration());
                // c = sel.newChar(i, charX, ta.getY() + style.getPaddingTop(), charW, charH);
                append(sel, ta, out, displayText, f, 0, displayX, ta.getY() + style.getPaddingTop(), h);
                break;
        }
        // g.setClip(cx, cy, cw, ch);
        // g.popClip();
        out = out.translate(ta.getAbsoluteX() - sel.getSelectionRoot().getAbsoluteX() - ta.getX(), ta.getAbsoluteY() - sel.getSelectionRoot().getAbsoluteY() - ta.getY());
        Spans spansOut = sel.newSpans();
        spansOut.add(out);
        return spansOut;
    } else {
        // drawTextArea(g, ta);
        return calculateTextAreaSpan(sel, ta);
    }
}
Also used : Span(com.codename1.ui.TextSelection.Span) Font(com.codename1.ui.Font) Spans(com.codename1.ui.TextSelection.Spans)

Example 37 with Switch

use of com.codename1.components.Switch in project CodenameOne by codenameone.

the class DefaultLookAndFeel method drawTextArea.

/**
 * {@inheritDoc}
 */
public void drawTextArea(Graphics g, TextArea ta) {
    setFG(g, ta);
    int alpha = g.concatenateAlpha(ta.getStyle().getFgAlpha());
    int line = ta.getLines();
    int oX = g.getClipX();
    int oY = g.getClipY();
    int oWidth = g.getClipWidth();
    int oHeight = g.getClipHeight();
    Font f = ta.getStyle().getFont();
    int fontHeight = f.getHeight();
    int align = reverseAlignForBidi(ta);
    int leftPadding = ta.getStyle().getPaddingLeft(ta.isRTL());
    int rightPadding = ta.getStyle().getPaddingRight(ta.isRTL());
    int topPadding = ta.getStyle().getPaddingTop();
    switch(ta.getVerticalAlignment()) {
        case Component.CENTER:
            topPadding += Math.max(0, (ta.getInnerHeight() - ta.getRowsGap() * (line - 1) - fontHeight * line) / 2);
            break;
        case Component.BOTTOM:
            topPadding += Math.max(0, (ta.getInnerHeight() - ta.getRowsGap() * (line - 1) - fontHeight * line));
    }
    boolean shouldBreak = false;
    for (int i = 0; i < line; i++) {
        int x = ta.getX() + leftPadding;
        int y = ta.getY() + topPadding + (ta.getRowsGap() + fontHeight) * i;
        if (Rectangle.intersects(x, y, ta.getWidth(), fontHeight, oX, oY, oWidth, oHeight)) {
            String rowText = (String) ta.getTextAt(i);
            // display ******** if it is a password field
            String displayText = "";
            if ((ta.getConstraint() & TextArea.PASSWORD) != 0) {
                int rlen = rowText.length();
                for (int j = 0; j < rlen; j++) {
                    displayText += passwordChar;
                }
            } else {
                displayText = rowText;
            }
            switch(align) {
                case Component.RIGHT:
                    x = ta.getX() + ta.getWidth() - rightPadding - f.stringWidth(displayText);
                    break;
                case Component.CENTER:
                    x += (ta.getWidth() - leftPadding - rightPadding - f.stringWidth(displayText)) / 2;
                    break;
            }
            int nextY = ta.getY() + topPadding + (ta.getRowsGap() + fontHeight) * (i + 2);
            // add "..." at the last row
            if (ta.isEndsWith3Points() && ta.getGrowLimit() == (i + 1) && ta.getGrowLimit() != line) {
                if (displayText.length() > 3) {
                    displayText = displayText.substring(0, displayText.length() - 3);
                }
                g.drawString(displayText + "...", x, y, ta.getStyle().getTextDecoration());
                return;
            } else {
                g.drawString(displayText, x, y, ta.getStyle().getTextDecoration());
            }
            shouldBreak = true;
        } else {
            if (shouldBreak) {
                break;
            }
        }
    }
    g.setAlpha(alpha);
}
Also used : Font(com.codename1.ui.Font)

Example 38 with Switch

use of com.codename1.components.Switch 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 alpha = g.concatenateAlpha(l.getStyle().getFgAlpha());
    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;
    boolean stateButtonOnLeft = 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;
                stateButtonOnLeft = true;
                x = cmpX + leftPadding + preserveSpaceForState;
            } else {
                tX = tX + cmpWidth - leftPadding - stateIconSize;
            }
            // cmpWidth -= leftPadding - stateIconSize;
            // preserveSpaceForState = 0;
            opposite = true;
        } else {
            if (rtl) {
                tX = tX + cmpWidth - leftPadding - stateIconSize;
            } else {
                x = cmpX + leftPadding + preserveSpaceForState;
                tX += leftPadding;
                stateButtonOnLeft = true;
            }
        }
        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;
                if (textPos == Label.RIGHT && stateIcon == null) {
                    x += preserveSpaceForState;
                }
                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;
                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));
                x = stateButtonOnLeft ? x : x - 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;
    int textSpaceX = cmpX + leftPadding;
    if (icon != null && (textPos == Label.RIGHT || textPos == Label.LEFT)) {
        textSpaceW = textSpaceW - icon.getWidth();
        textSpaceX = (textPos == Label.RIGHT) ? textSpaceX + icon.getWidth() : textSpaceX;
    }
    textSpaceW = textSpaceW - preserveSpaceForState;
    textSpaceX = stateButtonOnLeft ? textSpaceX + preserveSpaceForState : textSpaceX;
    if (icon == null) {
        // no icon only string
        drawLabelString(g, l, text, x, y, textSpaceX, 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, textSpaceX, textSpaceW, fontHeight);
                    g.drawImage(icon, x + strWidth + gap, y);
                } else {
                    strWidth = drawLabelString(g, l, text, x, y, textSpaceX, textSpaceW);
                    drawLabelImageValign(g, l, icon, x + strWidth + gap, y, fontHeight, iconHeight);
                }
                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, textSpaceX, textSpaceW, fontHeight);
                } else {
                    drawLabelImageValign(g, l, icon, x, y, fontHeight, iconHeight);
                    drawLabelString(g, l, text, x + iconWidth + gap, y, textSpaceX, 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, textSpaceX, textSpaceW);
                } else {
                    iconStringWGap = (Math.min(strWidth, textSpaceW) - iconWidth) / 2;
                    g.drawImage(icon, x + iconStringWGap, y);
                    drawLabelString(g, l, text, x, y + iconHeight + gap, textSpaceX, textSpaceW);
                }
                break;
            case Label.TOP:
                if (iconWidth > strWidth) {
                    // center align the smaller
                    iconStringWGap = (iconWidth - strWidth) / 2;
                    drawLabelString(g, l, text, x + iconStringWGap, y, textSpaceX, textSpaceW);
                    g.drawImage(icon, x, y + fontHeight + gap);
                } else {
                    iconStringWGap = (Math.min(strWidth, textSpaceW) - iconWidth) / 2;
                    drawLabelString(g, l, text, x, y, textSpaceX, textSpaceW);
                    g.drawImage(icon, x + iconStringWGap, y + fontHeight + gap);
                }
                break;
        }
    }
    String badgeText = l.getBadgeText();
    if (badgeText != null && badgeText.length() > 0) {
        Component badgeCmp = l.getBadgeStyleComponent();
        int badgePaddingTop = CN.convertToPixels(1);
        int badgePaddingBottom = badgePaddingTop;
        int badgePaddingLeft = badgePaddingTop;
        int badgePaddingRight = badgePaddingTop;
        int fgColor = 0xffffff;
        int bgColor = 0x666666;
        int strokeColor = bgColor;
        Font badgeFont = null;
        Style badgeStyle;
        if (badgeCmp == null) {
            badgeStyle = l.getUIManager().getComponentStyle("Badge");
        } else {
            badgeStyle = badgeCmp.getStyle();
        }
        if (badgeStyle != null) {
            fgColor = badgeStyle.getFgColor();
            bgColor = badgeStyle.getBgColor();
            if (badgeStyle.getBorder() instanceof RoundBorder) {
                strokeColor = ((RoundBorder) badgeStyle.getBorder()).getStrokeColor();
            } else {
                strokeColor = bgColor;
            }
            badgeFont = badgeStyle.getFont();
            badgePaddingTop = badgeStyle.getPaddingTop();
            badgePaddingBottom = badgeStyle.getPaddingBottom();
            badgePaddingLeft = badgeStyle.getPaddingLeftNoRTL();
            badgePaddingRight = badgeStyle.getPaddingRightNoRTL();
        }
        if (badgeFont == null) {
            if (Font.isNativeFontSchemeSupported()) {
                badgeFont = Font.createTrueTypeFont(Font.NATIVE_MAIN_LIGHT).derive(fontHeight / 2, 0);
            } else {
                badgeFont = Font.createSystemFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN, Font.SIZE_SMALL);
            }
        }
        int badgeFontHeight = badgeFont.getHeight();
        int badgeTextWidth = badgeFont.stringWidth(badgeText);
        GeneralPath path = new GeneralPath();
        Rectangle rect = new Rectangle(cmpX + cmpWidth - badgeTextWidth - badgePaddingLeft - badgePaddingRight, cmpY, badgePaddingLeft + badgePaddingRight + badgeTextWidth, badgePaddingTop + badgePaddingBottom + badgeFontHeight);
        if (rect.getWidth() < rect.getHeight()) {
            rect.setX(cmpX + cmpWidth - rect.getHeight());
            rect.setWidth(rect.getHeight());
        }
        path.moveTo(rect.getX() + rect.getHeight() / 2, rect.getY());
        path.lineTo(rect.getX() + rect.getWidth() - rect.getHeight() / 2, rect.getY());
        path.arcTo(rect.getX() + rect.getWidth() - rect.getHeight() / 2, rect.getY() + rect.getHeight() / 2, rect.getX() + rect.getWidth() - rect.getHeight() / 2, rect.getY() + rect.getHeight(), true);
        path.lineTo(rect.getX() + rect.getHeight() / 2, rect.getY() + rect.getHeight());
        path.arcTo(rect.getX() + rect.getHeight() / 2, rect.getY() + rect.getHeight() / 2, rect.getX() + rect.getHeight() / 2, rect.getY(), true);
        path.closePath();
        int col = g.getColor();
        boolean antialias = g.isAntiAliased();
        g.setAntiAliased(true);
        g.setColor(bgColor);
        g.fillShape(path);
        if (bgColor != strokeColor) {
            g.setColor(strokeColor);
            int alpha2 = g.concatenateAlpha(badgeStyle.getFgAlpha());
            g.drawShape(path, new Stroke(1, Stroke.CAP_SQUARE, Stroke.JOIN_MITER, 1f));
            g.setAlpha(alpha2);
        }
        g.setColor(fgColor);
        g.setFont(badgeFont);
        int alpha2 = g.concatenateAlpha(badgeStyle.getFgAlpha());
        g.drawString(badgeText, rect.getX() + rect.getWidth() / 2 - badgeTextWidth / 2, rect.getY() + badgePaddingTop);
        g.setAlpha(alpha2);
        g.setColor(col);
        g.setAntiAliased(antialias);
    }
    g.setAlpha(alpha);
}
Also used : Stroke(com.codename1.ui.Stroke) GeneralPath(com.codename1.ui.geom.GeneralPath) Button(com.codename1.ui.Button) Rectangle(com.codename1.ui.geom.Rectangle) Component(com.codename1.ui.Component) Font(com.codename1.ui.Font)

Example 39 with Switch

use of com.codename1.components.Switch in project CodenameOne by codenameone.

the class DefaultLookAndFeel method drawTextField.

/**
 * {@inheritDoc}
 */
public void drawTextField(Graphics g, TextArea ta) {
    setFG(g, ta);
    int alpha = g.concatenateAlpha(ta.getStyle().getFgAlpha());
    // display ******** if it is a password field
    String displayText = getTextFieldString(ta);
    Style style = ta.getStyle();
    int x = 0;
    // ta.getCursorX();
    int cursorCharPosition = ta.hasFocus() ? ta.getCursorPosition() : 0;
    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 (!Display.getInstance().platformUsesInputMode() || 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);
        }
    }
    g.setAlpha(alpha);
}
Also used : Font(com.codename1.ui.Font)

Example 40 with Switch

use of com.codename1.components.Switch in project CodenameOne by codenameone.

the class RoundRectBorder method paintBorderBackground.

@Override
public void paintBorderBackground(Graphics g, final Component c) {
    if (getTrackComponent() != null) {
        int cabsY = c.getAbsoluteY();
        int trackY = getTrackComponent().getY();
        int trackX = getTrackComponent().getX();
        int cabsX = c.getAbsoluteX();
        int arrowWH = CN.convertToPixels(arrowSize);
        if (cabsY >= trackY + getTrackComponent().getHeight()) {
            // we are below the component
            arrowDirection = CN.TOP;
            arrowPosition = (trackX + getTrackComponent().getWidth() / 2) - cabsX - arrowWH / 2;
        } else {
            if (trackComponentSide == CN.BOTTOM || cabsY + c.getHeight() <= trackY) {
                // we are above the component
                arrowDirection = CN.BOTTOM;
                arrowPosition = (trackX + getTrackComponent().getWidth() / 2) - cabsX - arrowWH / 2;
            } else {
                if (cabsX >= trackX + getTrackComponent().getWidth()) {
                    // we are to the right of the component
                    arrowDirection = CN.LEFT;
                    arrowPosition = (trackY + getTrackComponent().getHeight() / 2) - cabsY - arrowWH / 2;
                } else {
                    if (cabsX + c.getWidth() <= trackX) {
                        // we are to the left of the component
                        arrowDirection = CN.RIGHT;
                        arrowPosition = (trackY + getTrackComponent().getHeight() / 2) - cabsY - arrowWH / 2;
                    }
                }
            }
        }
    } else if (trackComponentSide >= 0) {
        switch(trackComponentSide) {
            case CN.TOP:
                arrowDirection = CN.TOP;
                arrowPosition = 0;
                if (trackComponentHorizontalPosition >= 0) {
                    arrowPosition = (int) (c.getWidth() * trackComponentHorizontalPosition);
                }
                break;
            case CN.BOTTOM:
                arrowDirection = CN.BOTTOM;
                arrowPosition = 0;
                if (trackComponentHorizontalPosition >= 0) {
                    arrowPosition = (int) (c.getWidth() * trackComponentHorizontalPosition);
                }
                break;
            case CN.LEFT:
                arrowDirection = CN.LEFT;
                arrowPosition = 0;
                if (trackComponentVerticalPosition >= 0) {
                    arrowPosition = (int) (c.getHeight() * trackComponentVerticalPosition);
                }
                break;
            case CN.RIGHT:
                arrowDirection = CN.RIGHT;
                arrowPosition = 0;
                if (trackComponentVerticalPosition >= 0) {
                    arrowPosition = (int) (c.getHeight() * trackComponentVerticalPosition);
                }
                break;
            default:
                break;
        }
    }
    final int w = c.getWidth();
    final int h = c.getHeight();
    int x = c.getX();
    int y = c.getY();
    boolean antiAliased = g.isAntiAliased();
    g.setAntiAliased(true);
    try {
        if (shadowOpacity == 0) {
            Style s = c.getStyle();
            if (s.getBgImage() == null) {
                byte type = s.getBackgroundType();
                if (type == Style.BACKGROUND_IMAGE_SCALED || type == Style.BACKGROUND_NONE) {
                    GeneralPath gp = createShape(w, h);
                    byte bgt = c.getStyle().getBgTransparency();
                    if (bgt != 0) {
                        int a = g.getAlpha();
                        g.setAlpha(bgt & 0xff);
                        g.setColor(s.getBgColor());
                        g.translate(x, y);
                        g.fillShape(gp);
                        if (this.stroke != null && strokeOpacity > 0 && strokeThickness > 0) {
                            g.setAlpha(strokeOpacity);
                            g.setColor(strokeColor);
                            g.drawShape(gp, this.stroke);
                        }
                        g.translate(-x, -y);
                        g.setAlpha(a);
                    }
                    if (this.stroke != null && strokeOpacity > 0 && strokeThickness > 0) {
                        int a = g.getAlpha();
                        g.setAlpha(strokeOpacity);
                        g.setColor(strokeColor);
                        g.translate(x, y);
                        g.drawShape(gp, this.stroke);
                        g.translate(-x, -y);
                        g.setAlpha(a);
                    }
                    return;
                }
            }
        }
        if (w > 0 && h > 0) {
            Image background = (Image) c.getClientProperty(CACHE_KEY + instanceVal);
            if (!dirty && background != null && background.getWidth() == w && background.getHeight() == h) {
                g.drawImage(background, x, y);
                return;
            }
        } else {
            return;
        }
        Image target = createTargetImage(c, w, h, true);
        g.drawImage(target, x, y);
        c.putClientProperty(CACHE_KEY + instanceVal, target);
        dirty = false;
        // update the cache with a more refined version and repaint
        if (!useCache) {
            Display.getInstance().callSeriallyOnIdle(new Runnable() {

                public void run() {
                    if (w == c.getWidth() && h == c.getHeight()) {
                        Image target = createTargetImage(c, w, h, false);
                        c.putClientProperty(CACHE_KEY + instanceVal, target);
                        c.repaint();
                    }
                }
            });
        }
    } finally {
        g.setAntiAliased(antiAliased);
    }
}
Also used : GeneralPath(com.codename1.ui.geom.GeneralPath) Image(com.codename1.ui.Image) EncodedImage(com.codename1.ui.EncodedImage)

Aggregations

Component (com.codename1.ui.Component)19 Font (com.codename1.ui.Font)18 Container (com.codename1.ui.Container)15 Form (com.codename1.ui.Form)14 Style (com.codename1.ui.plaf.Style)12 Button (com.codename1.ui.Button)11 Image (com.codename1.ui.Image)11 TextArea (com.codename1.ui.TextArea)11 ArrayList (java.util.ArrayList)11 File (java.io.File)10 IOException (java.io.IOException)10 Hashtable (java.util.Hashtable)10 BorderLayout (com.codename1.ui.layouts.BorderLayout)9 Label (com.codename1.ui.Label)8 FileInputStream (java.io.FileInputStream)8 ActionListener (com.codename1.ui.events.ActionListener)7 TextField (com.codename1.ui.TextField)6 ActionEvent (com.codename1.ui.events.ActionEvent)6 BoxLayout (com.codename1.ui.layouts.BoxLayout)6 EncodedImage (com.codename1.ui.EncodedImage)5