use of java.awt.font.TextHitInfo 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;
}
use of java.awt.font.TextHitInfo 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;
}
use of java.awt.font.TextHitInfo 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;
}
use of java.awt.font.TextHitInfo in project binnavi by google.
the class ZyCaret method calcHitPosition.
private int calcHitPosition(final int caretPosition, final double x, final double y, final double zoomFactor) {
boolean switched = false;
int lp = m_mouse_pressed_y;
int lr = m_mouse_released_y;
if (lp > lr) {
lp = m_mouse_released_y;
lr = m_mouse_pressed_y;
switched = true;
}
final int linecount = m_content.getLineCount();
final double height = (float) m_content.getLineHeight();
int maxIndex = caretPosition;
for (int line = lp; line <= lr; line++) {
double deltaY = 0;
if (switched) {
deltaY = height * zoomFactor * line;
} else {
deltaY = -(height * zoomFactor * (linecount - line));
}
final TextLayout textLayout = m_content.getLineContent(line).getTextLayout();
final TextHitInfo hitInfo = textLayout.hitTestChar((float) x, (float) (y + deltaY), textLayout.getBounds());
final int insertionIndex = hitInfo.getInsertionIndex();
if ((caretPosition < insertionIndex) && (insertionIndex > maxIndex)) {
maxIndex = insertionIndex;
}
}
return maxIndex;
}
use of java.awt.font.TextHitInfo in project jdk8u_jdk by JetBrains.
the class JTextComponent method setInputMethodCaretPosition.
//
// Sets the caret position according to the passed input method
// event. Also, sets/resets composed text caret appropriately.
//
private void setInputMethodCaretPosition(InputMethodEvent e) {
int dot;
if (composedTextExists()) {
dot = composedTextStart.getOffset();
if (!(caret instanceof ComposedTextCaret)) {
if (composedTextCaret == null) {
composedTextCaret = new ComposedTextCaret();
}
originalCaret = caret;
// Sets composed text caret
exchangeCaret(originalCaret, composedTextCaret);
}
TextHitInfo caretPos = e.getCaret();
if (caretPos != null) {
int index = caretPos.getInsertionIndex();
dot += index;
if (index == 0) {
// becomes visible.
try {
Rectangle d = modelToView(dot);
Rectangle end = modelToView(composedTextEnd.getOffset());
Rectangle b = getBounds();
d.x += Math.min(end.x - d.x, b.width);
scrollRectToVisible(d);
} catch (BadLocationException ble) {
}
}
}
caret.setDot(dot);
} else if (caret instanceof ComposedTextCaret) {
dot = caret.getDot();
// Restores original caret
exchangeCaret(caret, originalCaret);
caret.setDot(dot);
}
}
Aggregations