Search in sources :

Example 1 with TextLayoutInfo

use of g4p_controls.StyledString.TextLayoutInfo in project hid-serial by rayshobby.

the class GTextArea method moveCaretUp.

protected boolean moveCaretUp(TextLayoutHitInfo currPos) {
    if (currPos.tli.lineNo == 0)
        return false;
    TextLayoutInfo ntli = stext.getTLIforLineNo(currPos.tli.lineNo - 1);
    TextHitInfo nthi = ntli.layout.hitTestChar(caretX, 0);
    currPos.tli = ntli;
    currPos.thi = nthi;
    return true;
}
Also used : TextHitInfo(java.awt.font.TextHitInfo) TextLayoutInfo(g4p_controls.StyledString.TextLayoutInfo)

Example 2 with TextLayoutInfo

use of g4p_controls.StyledString.TextLayoutInfo in project hid-serial by rayshobby.

the class GTextArea method moveCaretLeft.

/**
	 * Move caret left by one character. If necessary move to the end of the line above
	 * @return true if caret was moved else false
	 */
protected boolean moveCaretLeft(TextLayoutHitInfo currPos) {
    TextLayoutInfo ntli;
    TextHitInfo nthi = currPos.tli.layout.getNextLeftHit(currPos.thi);
    if (nthi == null) {
        // Move the caret to the end of the previous line 
        if (currPos.tli.lineNo == 0)
            // Can't goto previous line because this is the first line
            return false;
        else {
            // Move to end of previous line
            ntli = stext.getTLIforLineNo(currPos.tli.lineNo - 1);
            nthi = ntli.layout.getNextRightHit(ntli.nbrChars - 1);
            currPos.tli = ntli;
            currPos.thi = nthi;
        }
    } else {
        // Move the caret to the left of current position
        currPos.thi = nthi;
    }
    return true;
}
Also used : TextHitInfo(java.awt.font.TextHitInfo) TextLayoutInfo(g4p_controls.StyledString.TextLayoutInfo)

Example 3 with TextLayoutInfo

use of g4p_controls.StyledString.TextLayoutInfo in project hid-serial by rayshobby.

the class GEditableTextControl method setFocus.

/**
	 * Determines whether this component is to have focus or not. <br>
	 * Fire focus events for the GTextField and GTextArea controls
	 * @param focus
	 */
public void setFocus(boolean focus) {
    if (!focus) {
        loseFocus(null);
        return;
    }
    // Make sure we have some text
    if (focusIsWith != this) {
        dragging = false;
        if (stext == null || stext.length() == 0)
            stext = new StyledString(" ", wrapWidth);
        //			text = stext.getPlainText();
        LinkedList<TextLayoutInfo> lines = stext.getLines(buffer.g2);
        startTLHI = new TextLayoutHitInfo(lines.getFirst(), null);
        startTLHI.thi = startTLHI.tli.layout.getNextLeftHit(1);
        endTLHI = new TextLayoutHitInfo(lines.getLast(), null);
        int lastChar = endTLHI.tli.layout.getCharacterCount();
        endTLHI.thi = startTLHI.tli.layout.getNextRightHit(lastChar - 1);
        calculateCaretPos(endTLHI);
        bufferInvalid = true;
    }
    keepCursorInView = true;
    takeFocus();
}
Also used : TextLayoutHitInfo(g4p_controls.StyledString.TextLayoutHitInfo) TextLayoutInfo(g4p_controls.StyledString.TextLayoutInfo)

Example 4 with TextLayoutInfo

use of g4p_controls.StyledString.TextLayoutInfo in project hid-serial by rayshobby.

the class GEditableTextControl method changeText.

// Only executed if text has changed
protected void changeText() {
    TextLayoutInfo tli;
    TextHitInfo thi = null, thiRight = null;
    pos += adjust;
    // Force layouts to be updated
    stext.getLines(buffer.g2);
    // Try to get text layout info for the current position
    tli = stext.getTLIforCharNo(pos);
    if (tli == null) {
        // If unable to get a layout for pos then reset everything
        endTLHI = null;
        startTLHI = null;
        ptx = pty = 0;
        caretX = caretY = 0;
    } else {
        int posInLine = pos - tli.startCharIndex;
        // Get some hit info so we can see what is happening
        try {
            thiRight = tli.layout.getNextRightHit(posInLine);
        } catch (Exception excp) {
            thiRight = null;
        }
        if (posInLine <= 0) {
            // At start of line
            thi = tli.layout.getNextLeftHit(thiRight);
        } else if (posInLine >= tli.nbrChars) {
            // End of line
            thi = tli.layout.getNextRightHit(tli.nbrChars - 1);
        } else {
            // Character in line;
            thi = tli.layout.getNextLeftHit(thiRight);
        }
        endTLHI.setInfo(tli, thi);
        // Cursor at end of paragraph graphic
        calculateCaretPos(endTLHI);
        // Is do we have to move cursor to start of next line
        if (newline) {
            ///stext.getWrapWidth() != Integer.MAX_VALUE && caretX > stext.getWrapWidth()){
            if (pos >= stext.length()) {
                stext.insertCharacters(pos, " ");
                stext.getLines(buffer.g2);
            }
            moveCaretRight(endTLHI);
            calculateCaretPos(endTLHI);
        }
        // Finish off by ensuring no selection, invalidate buffer etc.
        startTLHI.copyFrom(endTLHI);
    }
    bufferInvalid = true;
}
Also used : TextHitInfo(java.awt.font.TextHitInfo) TextLayoutInfo(g4p_controls.StyledString.TextLayoutInfo)

Example 5 with TextLayoutInfo

use of g4p_controls.StyledString.TextLayoutInfo in project hid-serial by rayshobby.

the class GButton method updateBuffer.

protected void updateBuffer() {
    if (bufferInvalid) {
        Graphics2D g2d = buffer.g2;
        // Get the latest lines of text
        LinkedList<TextLayoutInfo> lines = stext.getLines(g2d);
        bufferInvalid = false;
        buffer.beginDraw();
        // Back ground colour
        switch(status) {
            case OVER_CONTROL:
                buffer.background(palette[6]);
                break;
            case PRESS_CONTROL:
                buffer.background(palette[14]);
                break;
            default:
                buffer.background(palette[4]);
        }
        g2d.setColor(jpalette[3]);
        g2d.setStroke(pen_1_0);
        g2d.drawRect(0, 0, (int) width - 1, (int) height - 1);
        // Calculate text and icon placement
        calcAlignment();
        // If there is an icon draw it
        if (iconW != 0)
            buffer.image(bicon[status], siX, siY);
        float wrapWidth = stext.getWrapWidth();
        float sx = 0, tw = 0;
        buffer.translate(stX, stY);
        for (TextLayoutInfo lineInfo : lines) {
            TextLayout layout = lineInfo.layout;
            buffer.translate(0, layout.getAscent());
            switch(textAlignH) {
                case CENTER:
                    tw = layout.getVisibleAdvance();
                    tw = (tw > wrapWidth) ? tw - wrapWidth : tw;
                    sx = (wrapWidth - tw) / 2;
                    break;
                case RIGHT:
                    tw = layout.getVisibleAdvance();
                    tw = (tw > wrapWidth) ? tw - wrapWidth : tw;
                    sx = wrapWidth - tw;
                    break;
                case LEFT:
                case JUSTIFY:
                default:
                    sx = 0;
            }
            // display text
            g2d.setColor(jpalette[2]);
            lineInfo.layout.draw(g2d, sx, 0);
            buffer.translate(0, layout.getDescent() + layout.getLeading());
        }
        buffer.endDraw();
    }
}
Also used : TextLayoutInfo(g4p_controls.StyledString.TextLayoutInfo) Graphics2D(java.awt.Graphics2D) TextLayout(java.awt.font.TextLayout)

Aggregations

TextLayoutInfo (g4p_controls.StyledString.TextLayoutInfo)11 Graphics2D (java.awt.Graphics2D)5 TextHitInfo (java.awt.font.TextHitInfo)5 TextLayout (java.awt.font.TextLayout)5 TextLayoutHitInfo (g4p_controls.StyledString.TextLayoutHitInfo)3 Shape (java.awt.Shape)2