use of automenta.vivisect.gui.StyledString.TextLayoutHitInfo in project opennars by opennars.
the class GEditableTextControl method setSelectedTextStyle.
/**
* If some text has been selected then set the style. If there is no selection then
* the text is unchanged.
*
* @param style
*/
public void setSelectedTextStyle(TextAttribute style, Object value) {
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.addAttribute(style, value, 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;
}
use of automenta.vivisect.gui.StyledString.TextLayoutHitInfo in project opennars by opennars.
the class GEditableTextControl method getSelectedText.
// SELECTED / HIGHLIGHTED TEXT
/**
* Get the text that has been selected (highlighted) by the user. <br>
* @return the selected text without styling
*/
public String getSelectedText() {
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();
String s = stext.getPlainText().substring(ss, ee);
return s;
}
use of automenta.vivisect.gui.StyledString.TextLayoutHitInfo in project opennars by opennars.
the class GTextArea method insertText.
/**
* Insert text at the display position specified. <br>
*
* The area line number starts at 0 and includes any lines scrolled off the top. So if
* three lines have been scrolled off the top the first visible line is number 3. <br>
*
* No events will be generated and the caret will be moved to the end of any inserted text. <br>
*
* @param text the text to insert
* @param lineNo the area line number
* @param charNo the character position to insert text in display line
* @param startWithEOL if true,inserted text will start on newline
* @param endWithEOL if true, text after inserted text will start on new line
* @return true if some characters were inserted
*/
public boolean insertText(String text, int lineNo, int charNo, boolean startWithEOL, boolean endWithEOL) {
if (text != null && text.length() > 0) {
int pos = stext.getPos(lineNo, charNo);
int change = stext.insertCharacters(text, lineNo, charNo, startWithEOL, endWithEOL);
// displayCaretPos("Caret starts at ");
if (change != 0) {
LinkedList<TextLayoutInfo> lines = stext.getLines(buffer.g2);
updateScrollbars(lines.getLast().layout.getVisibleAdvance());
// Move caret to end of insert if possible
pos += change;
TextLayoutHitInfo tlhi = stext.getTLHIforCharPosition(pos);
if (tlhi != null) {
endTLHI.copyFrom(tlhi);
moveCaretLeft(endTLHI);
startTLHI.copyFrom(endTLHI);
// displayCaretPos("Caret ends at ");
calculateCaretPos(tlhi);
keepCursorInView = true;
showCaret = true;
}
bufferInvalid = true;
return true;
}
}
return false;
}
use of automenta.vivisect.gui.StyledString.TextLayoutHitInfo in project opennars by opennars.
the class GTextArea method mouseEvent.
/**
* Will respond to mouse events.
*/
public void mouseEvent(MouseEvent event) {
if (!visible || !enabled || !available)
return;
calcTransformedOrigin(winApp.getCursorX(), winApp.getCursorY());
// 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;
}
}
use of automenta.vivisect.gui.StyledString.TextLayoutHitInfo in project opennars by opennars.
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() && promptText != null)
lines = promptText.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();
}
}
Aggregations