Search in sources :

Example 1 with TextLayoutHitInfo

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

the class GTextArea method mouseEvent.

/**
	 * Will respond to mouse events.
	 */
public void mouseEvent(MouseEvent event) {
    if (!visible || !enabled || !available)
        return;
    calcTransformedOrigin(winApp.mouseX, winApp.mouseY);
    // Remove translation
    ox -= tx;
    // Remove translation
    oy -= ty;
    currSpot = whichHotSpot(ox, oy);
    if (currSpot == 1 || focusIsWith == this)
        cursorIsOver = this;
    else if (cursorIsOver == this)
        cursorIsOver = null;
    switch(event.getAction()) {
        case MouseEvent.PRESS:
            if (currSpot == 1) {
                if (focusIsWith != this && z >= focusObjectZ()) {
                    keepCursorInView = true;
                    takeFocus();
                }
                dragging = false;
                if (stext == null || stext.length() == 0) {
                    stext = new StyledString(" ", wrapWidth);
                    stext.getLines(buffer.g2);
                }
                endTLHI = stext.calculateFromXY(buffer.g2, ox + ptx, oy + pty);
                startTLHI = new TextLayoutHitInfo(endTLHI);
                calculateCaretPos(endTLHI);
                bufferInvalid = true;
            } else {
                // Not over this control so if we have focus loose it
                if (focusIsWith == this)
                    loseFocus(null);
            }
            break;
        case MouseEvent.RELEASE:
            dragging = false;
            bufferInvalid = true;
            break;
        case MouseEvent.DRAG:
            if (focusIsWith == this) {
                keepCursorInView = true;
                dragging = true;
                endTLHI = stext.calculateFromXY(buffer.g2, ox + ptx, oy + pty);
                calculateCaretPos(endTLHI);
                fireEvent(this, GEvent.SELECTION_CHANGED);
                bufferInvalid = true;
            }
            break;
    }
}
Also used : TextLayoutHitInfo(g4p_controls.StyledString.TextLayoutHitInfo)

Example 2 with TextLayoutHitInfo

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

the class GEditableTextControl method clearStyle.

/**
	 * Clear any styles applied to the selected text.
	 */
public void clearStyle() {
    if (!hasSelection())
        return;
    TextLayoutHitInfo startSelTLHI;
    TextLayoutHitInfo endSelTLHI;
    if (endTLHI.compareTo(startTLHI) == -1) {
        startSelTLHI = endTLHI;
        endSelTLHI = startTLHI;
    } else {
        startSelTLHI = startTLHI;
        endSelTLHI = endTLHI;
    }
    int ss = startSelTLHI.tli.startCharIndex + startSelTLHI.thi.getInsertionIndex();
    int ee = endSelTLHI.tli.startCharIndex + endSelTLHI.thi.getInsertionIndex();
    stext.clearAttributes(ss, ee);
    // We have modified the text style so the end of the selection may have
    // moved, so it needs to be recalculated. The start will be unaffected.
    stext.getLines(buffer.g2);
    endSelTLHI.tli = stext.getTLIforCharNo(ee);
    int cn = ee - endSelTLHI.tli.startCharIndex;
    if (// start of line
    cn == 0)
        endSelTLHI.thi = endSelTLHI.tli.layout.getNextLeftHit(1);
    else
        endSelTLHI.thi = endSelTLHI.tli.layout.getNextRightHit(cn - 1);
    bufferInvalid = true;
}
Also used : TextLayoutHitInfo(g4p_controls.StyledString.TextLayoutHitInfo)

Example 3 with TextLayoutHitInfo

use of g4p_controls.StyledString.TextLayoutHitInfo 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 TextLayoutHitInfo

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

the class GTextArea method updateBuffer.

/**
	 * Add text to the end of the current text. This is useful for a logging' type activity.
	 * 
	 * @param extraText the text to append
	 */
//	public void appendText(String extraText){
//		appendText(extraText, false);
//	}
/**
	 * If the buffer is invalid then redraw it.
	 */
protected void updateBuffer() {
    if (bufferInvalid) {
        Graphics2D g2d = buffer.g2;
        // Get the latest lines of text
        LinkedList<TextLayoutInfo> lines = stext.getLines(g2d);
        if (lines.isEmpty() && defaultText != null)
            lines = defaultText.getLines(g2d);
        bufferInvalid = false;
        TextLayoutHitInfo startSelTLHI = null, endSelTLHI = null;
        buffer.beginDraw();
        // Whole control surface if opaque
        if (opaque)
            buffer.background(palette[6]);
        else
            buffer.background(buffer.color(255, 0));
        // Now move to top left corner of text display area
        buffer.translate(tx, ty);
        // Typing area surface
        buffer.noStroke();
        buffer.fill(palette[7]);
        buffer.rect(-1, -1, tw + 2, th + 2);
        g2d.setClip(gpTextDisplayArea);
        buffer.translate(-ptx, -pty);
        // Translate in preparation for display selection and text
        if (hasSelection()) {
            if (endTLHI.compareTo(startTLHI) == -1) {
                startSelTLHI = endTLHI;
                endSelTLHI = startTLHI;
            } else {
                startSelTLHI = startTLHI;
                endSelTLHI = endTLHI;
            }
        }
        // Display selection and text
        for (TextLayoutInfo lineInfo : lines) {
            TextLayout layout = lineInfo.layout;
            buffer.translate(0, layout.getAscent());
            // Draw selection if any
            if (hasSelection() && lineInfo.compareTo(startSelTLHI.tli) >= 0 && lineInfo.compareTo(endSelTLHI.tli) <= 0) {
                int ss = 0;
                ss = (lineInfo.compareTo(startSelTLHI.tli) == 0) ? startSelTLHI.thi.getInsertionIndex() : 0;
                int ee = endSelTLHI.thi.getInsertionIndex();
                ee = (lineInfo.compareTo(endSelTLHI.tli) == 0) ? endSelTLHI.thi.getInsertionIndex() : lineInfo.nbrChars - 1;
                g2d.setColor(jpalette[14]);
                Shape selShape = layout.getLogicalHighlightShape(ss, ee);
                g2d.fill(selShape);
            }
            // display text
            g2d.setColor(jpalette[2]);
            lineInfo.layout.draw(g2d, 0, 0);
            buffer.translate(0, layout.getDescent() + layout.getLeading());
        }
        g2d.setClip(null);
        buffer.endDraw();
    }
}
Also used : TextLayoutHitInfo(g4p_controls.StyledString.TextLayoutHitInfo) Shape(java.awt.Shape) TextLayoutInfo(g4p_controls.StyledString.TextLayoutInfo) Graphics2D(java.awt.Graphics2D) TextLayout(java.awt.font.TextLayout)

Example 5 with TextLayoutHitInfo

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

the class GTextField method updateBuffer.

/**
	 * If the buffer is invalid then redraw it.
	 * @TODO need to use palette for colours
	 */
protected void updateBuffer() {
    if (bufferInvalid) {
        Graphics2D g2d = buffer.g2;
        // Get the latest lines of text
        LinkedList<TextLayoutInfo> lines = stext.getLines(g2d);
        if (lines.isEmpty() && defaultText != null)
            lines = defaultText.getLines(g2d);
        bufferInvalid = false;
        TextLayoutHitInfo startSelTLHI = null, endSelTLHI = null;
        buffer.beginDraw();
        // Whole control surface if opaque
        if (opaque)
            buffer.background(palette[6]);
        else
            buffer.background(buffer.color(255, 0));
        // Now move to top left corner of text display area
        buffer.translate(tx, ty);
        // Typing area surface
        buffer.noStroke();
        buffer.fill(palette[7]);
        buffer.rect(-1, -1, tw + 2, th + 2);
        g2d.setClip(gpTextDisplayArea);
        buffer.translate(-ptx, -pty);
        if (hasSelection()) {
            if (endTLHI.compareTo(startTLHI) == -1) {
                startSelTLHI = endTLHI;
                endSelTLHI = startTLHI;
            } else {
                startSelTLHI = startTLHI;
                endSelTLHI = endTLHI;
            }
        }
        // Display selection and text
        for (TextLayoutInfo lineInfo : lines) {
            TextLayout layout = lineInfo.layout;
            buffer.translate(0, layout.getAscent());
            // Draw selection if any
            if (hasSelection() && lineInfo.compareTo(startSelTLHI.tli) >= 0 && lineInfo.compareTo(endSelTLHI.tli) <= 0) {
                int ss = startSelTLHI.thi.getInsertionIndex();
                int ee = endSelTLHI.thi.getInsertionIndex();
                g2d.setColor(jpalette[14]);
                Shape selShape = layout.getLogicalHighlightShape(ss, ee);
                g2d.fill(selShape);
            }
            // Draw text
            g2d.setColor(jpalette[2]);
            lineInfo.layout.draw(g2d, 0, 0);
            buffer.translate(0, layout.getDescent() + layout.getLeading());
        }
        g2d.setClip(null);
        buffer.endDraw();
    }
}
Also used : TextLayoutHitInfo(g4p_controls.StyledString.TextLayoutHitInfo) Shape(java.awt.Shape) TextLayoutInfo(g4p_controls.StyledString.TextLayoutInfo) Graphics2D(java.awt.Graphics2D) TextLayout(java.awt.font.TextLayout)

Aggregations

TextLayoutHitInfo (g4p_controls.StyledString.TextLayoutHitInfo)9 TextLayoutInfo (g4p_controls.StyledString.TextLayoutInfo)3 Graphics2D (java.awt.Graphics2D)2 Shape (java.awt.Shape)2 TextLayout (java.awt.font.TextLayout)2