Search in sources :

Example 6 with TextSelection

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

use of com.codename1.ui.TextSelection 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 8 with TextSelection

use of com.codename1.ui.TextSelection 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 Span (com.codename1.ui.TextSelection.Span)3 Char (com.codename1.ui.TextSelection.Char)2 Spans (com.codename1.ui.TextSelection.Spans)2 SpanLabel (com.codename1.components.SpanLabel)1 Button (com.codename1.ui.Button)1 Container (com.codename1.ui.Container)1 FontImage (com.codename1.ui.FontImage)1 Form (com.codename1.ui.Form)1 Image (com.codename1.ui.Image)1 Label (com.codename1.ui.Label)1 TextArea (com.codename1.ui.TextArea)1 TextField (com.codename1.ui.TextField)1 TextSelection (com.codename1.ui.TextSelection)1 ActionEvent (com.codename1.ui.events.ActionEvent)1