Search in sources :

Example 6 with Span

use of com.codename1.ui.TextSelection.Span in project CodenameOne by codenameone.

the class SpanLabelTest2897 method start.

public void start() {
    if (current != null) {
        current.show();
        return;
    }
    Form hi = new Form("Welcome");
    Container container = new Container(BoxLayout.y());
    SpanLabel label = new SpanLabel("Span label only shows one row. We need to update the columns for rendering, otherwise it will still wrap at the old number of columns.");
    container.add(label);
    SpanLabel label1 = new SpanLabel("Set span label text component row count manually. We need to update the columns for rendering, otherwise it will still wrap at the old number of columns.");
    // label1.getTextComponent().setRows(4);
    container.add(label1);
    SpanButton btn = new SpanButton("Hello");
    container.add(btn);
    btn = new SpanButton("Set span button text component row count manually. We need to update the columns for rendering, otherwise it will still wrap at the old number of columns.");
    container.add(btn);
    hi.add(container);
    hi.show();
}
Also used : Container(com.codename1.ui.Container) Form(com.codename1.ui.Form) SpanButton(com.codename1.components.SpanButton) SpanLabel(com.codename1.components.SpanLabel)

Example 7 with Span

use of com.codename1.ui.TextSelection.Span in project CodenameOne by codenameone.

the class DefaultLookAndFeel method calculateLabelSpan.

public Span calculateLabelSpan(TextSelection sel, Label l) {
    Image icon = l.getMaskedIcon();
    Image stateIcon = null;
    int preserveSpaceForState = 0;
    // 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() : 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
        return calculateSpanForLabelString(sel, 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;
                    return calculateSpanForLabelStringValign(sel, l, text, x, y, iconStringHGap, iconHeight, textSpaceW, fontHeight);
                } else {
                    iconStringHGap = (fontHeight - iconHeight) / 2;
                    // strWidth = drawLabelString(l, text, x, y, textSpaceW);
                    return calculateSpanForLabelString(sel, l, text, x, y, textSpaceW);
                // g.drawImage(icon, x + strWidth + gap, y + iconStringHGap);
                }
            case Label.RIGHT:
                if (iconHeight > fontHeight) {
                    iconStringHGap = (iconHeight - fontHeight) / 2;
                    // g.drawImage(icon, x, y);
                    return calculateSpanForLabelStringValign(sel, l, text, x + iconWidth + gap, y, iconStringHGap, iconHeight, textSpaceW, fontHeight);
                } else {
                    iconStringHGap = (fontHeight - iconHeight) / 2;
                    // g.drawImage(icon, x, y + iconStringHGap);
                    return calculateSpanForLabelString(sel, l, text, x + iconWidth + gap, y, textSpaceW);
                }
            case Label.BOTTOM:
                if (iconWidth > strWidth) {
                    // center align the smaller
                    iconStringWGap = (iconWidth - strWidth) / 2;
                    // g.drawImage(icon, x, y);
                    return calculateSpanForLabelString(sel, l, text, x + iconStringWGap, y + iconHeight + gap, textSpaceW);
                } else {
                    iconStringWGap = (Math.min(strWidth, textSpaceW) - iconWidth) / 2;
                    return calculateSpanForLabelString(sel, l, text, x, y + iconHeight + gap, textSpaceW);
                }
            case Label.TOP:
                if (iconWidth > strWidth) {
                    // center align the smaller
                    iconStringWGap = (iconWidth - strWidth) / 2;
                    return calculateSpanForLabelString(sel, l, text, x + iconStringWGap, y, textSpaceW);
                // g.drawImage(icon, x, y + fontHeight + gap);
                } else {
                    iconStringWGap = (Math.min(strWidth, textSpaceW) - iconWidth) / 2;
                    return calculateSpanForLabelString(sel, l, text, x, y, textSpaceW);
                // g.drawImage(icon, x + iconStringWGap, y + fontHeight + gap);
                }
        }
    }
    return sel.newSpan(l);
}
Also used : Button(com.codename1.ui.Button) FontImage(com.codename1.ui.FontImage) Image(com.codename1.ui.Image) Font(com.codename1.ui.Font)

Example 8 with Span

use of com.codename1.ui.TextSelection.Span in project CodenameOne by codenameone.

the class DefaultLookAndFeel method calculateTextAreaSpan.

@Override
public Spans calculateTextAreaSpan(TextSelection sel, TextArea ta) {
    Spans out = sel.newSpans();
    // setFG(g, ta);
    // Span out = sel.newSpan(ta);
    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() + fontHeight) * line) / 2);
            break;
        case Component.BOTTOM:
            topPadding += Math.max(0, (ta.getInnerHeight() - (ta.getRowsGap() + fontHeight) * line));
    }
    // boolean shouldBreak = false;
    int posOffset = 0;
    int lastRowBottom = 0;
    for (int i = 0; i < line; i++) {
        Span rowSpan = sel.newSpan(ta);
        int x = ta.getX() + leftPadding;
        int y = ta.getY() + topPadding + (ta.getRowsGap() + fontHeight) * i;
        int adjustedY = Math.max(y, lastRowBottom);
        int yDiff = adjustedY - y;
        y = adjustedY;
        // 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;
        }
        posOffset = ta.getText().indexOf(rowText, posOffset);
        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;
        }
        // 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());
            append(sel, ta, rowSpan, displayText + "...", f, posOffset, x, y, getSelectionHeight(f) - yDiff);
            lastRowBottom = rowSpan.getBounds().getY() + rowSpan.getBounds().getHeight();
            rowSpan = rowSpan.translate(ta.getAbsoluteX() - sel.getSelectionRoot().getAbsoluteX() - ta.getX(), ta.getAbsoluteY() - sel.getSelectionRoot().getAbsoluteY() - ta.getY());
            out.add(rowSpan);
            return out;
        } else {
            // g.drawString(displayText, x, y ,ta.getStyle().getTextDecoration());
            append(sel, ta, rowSpan, displayText, f, posOffset, x, y, getSelectionHeight(f) - yDiff);
            lastRowBottom = rowSpan.getBounds().getY() + rowSpan.getBounds().getHeight();
            rowSpan = rowSpan.translate(ta.getAbsoluteX() - sel.getSelectionRoot().getAbsoluteX() - ta.getX(), ta.getAbsoluteY() - sel.getSelectionRoot().getAbsoluteY() - ta.getY());
            out.add(rowSpan);
        }
        posOffset += displayText.length();
    // shouldBreak = true;
    // }else{
    // if(shouldBreak){
    // break;
    // }
    // }
    }
    return out;
}
Also used : Span(com.codename1.ui.TextSelection.Span) Font(com.codename1.ui.Font) Spans(com.codename1.ui.TextSelection.Spans)

Example 9 with Span

use of com.codename1.ui.TextSelection.Span in project CodenameOne by codenameone.

the class DefaultLookAndFeel method calculateSpanForLabelStringValign.

private Span calculateSpanForLabelStringValign(TextSelection sel, Label l, String str, int x, int y, int iconStringHGap, int iconHeight, int textSpaceW, int fontHeight) {
    switch(l.getVerticalAlignment()) {
        case Component.TOP:
            return calculateSpanForLabelString(sel, l, str, x, y, textSpaceW);
        case Component.CENTER:
            return calculateSpanForLabelString(sel, l, str, x, y + iconHeight / 2 - fontHeight / 2, textSpaceW);
        case Component.BASELINE:
            Font iconFont = l.getIconStyleComponent().getStyle().getFont();
            Font textFont = l.getStyle().getFont();
            if (iconFont == null) {
                iconFont = Font.getDefaultFont();
            }
            if (textFont == null) {
                textFont = Font.getDefaultFont();
            }
            int ascentDiff = iconFont.getAscent() - textFont.getAscent();
            return calculateSpanForLabelString(sel, l, str, x, y + ascentDiff, textSpaceW);
        default:
            return calculateSpanForLabelString(sel, l, str, x, y + iconStringHGap, textSpaceW);
    }
}
Also used : Font(com.codename1.ui.Font)

Example 10 with Span

use of com.codename1.ui.TextSelection.Span in project CodenameOne by codenameone.

the class DefaultLookAndFeel method calculateSpanForLabelText.

public Span calculateSpanForLabelText(TextSelection sel, Label l, String text, int x, int y, int textSpaceW) {
    Span span = sel.newSpan(l);
    Style style = l.getStyle();
    Font f = style.getFont();
    int h = f.getHeight() + (int) (f.getHeight() * 0.25);
    y -= (int) (f.getHeight() * 0.25);
    boolean rtl = l.isRTL();
    boolean isTickerRunning = l.isTickerRunning();
    int txtW = l.getStringWidth(f);
    int curPos = text.length();
    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);
                    // xPos = f.stringWidth(displayText.substring(0, cursorCharPosition));
                    // g.drawString(points, l.getShiftText() + x, y,l.getStyle().getTextDecoration());
                    // g.clipRect(pointsW+l.getShiftText() + x, y, textSpaceW - pointsW, f.getHeight());
                    Char nextChar = sel.newChar(curPos, pointsW + l.getShiftText() + x, y, textSpaceW - pointsW, h);
                    span.add(nextChar);
                }
                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);
                }
            }
        }
    }
    int len = text.length();
    int xPos = 0;
    curPos = 1;
    while (curPos <= len) {
        int newXpos = f.stringWidth(text.substring(0, curPos));
        Char next = sel.newChar(curPos - 1, l.getShiftText() + x + xPos, y, newXpos - xPos, h);
        span.add(next);
        xPos = newXpos;
        curPos++;
    }
    // System.out.println("Span: "+span);
    return span.translate(l.getAbsoluteX() - sel.getSelectionRoot().getAbsoluteX() - l.getX(), l.getAbsoluteY() - sel.getSelectionRoot().getAbsoluteY() - l.getY());
}
Also used : Char(com.codename1.ui.TextSelection.Char) Span(com.codename1.ui.TextSelection.Span) Font(com.codename1.ui.Font)

Aggregations

Font (com.codename1.ui.Font)5 SpanLabel (com.codename1.components.SpanLabel)4 BorderLayout (com.codename1.ui.layouts.BorderLayout)4 Form (com.codename1.ui.Form)3 Span (com.codename1.ui.TextSelection.Span)3 SpanButton (com.codename1.components.SpanButton)2 Button (com.codename1.ui.Button)2 Container (com.codename1.ui.Container)2 Char (com.codename1.ui.TextSelection.Char)2 Spans (com.codename1.ui.TextSelection.Spans)2 LayeredLayout (com.codename1.ui.layouts.LayeredLayout)2 BrowserComponent (com.codename1.ui.BrowserComponent)1 CN.getCurrentForm (com.codename1.ui.CN.getCurrentForm)1 FontImage (com.codename1.ui.FontImage)1 Image (com.codename1.ui.Image)1 Label (com.codename1.ui.Label)1 SwipeableContainer (com.codename1.ui.SwipeableContainer)1 TextField (com.codename1.ui.TextField)1 Dimension (com.codename1.ui.geom.Dimension)1 BoxLayout (com.codename1.ui.layouts.BoxLayout)1