private void drawsel(GOut g, Message msg, int y) { RichText rt = (RichText) msg.text(); boolean sel = msg != selstart.msg; for (RichText.Part part = rt.parts; part != null; part = part.next) { if (!(part instanceof RichText.TextPart)) continue; RichText.TextPart tp = (RichText.TextPart) part; if (tp.start == tp.end) continue; TextHitInfo a, b; if (sel) { a = TextHitInfo.leading(0); } else if (tp == selstart.part) { a = selstart.ch; sel = true; } else { continue; } if (tp == selend.part) { sel = false; b = selend.ch; } else { b = TextHitInfo.trailing(tp.end - tp.start - 1); } Coord ul = new Coord(tp.x + (int) tp.advance(0, a.getInsertionIndex()), tp.y + y); Coord sz = new Coord((int) tp.advance(a.getInsertionIndex(), b.getInsertionIndex()), tp.height()); g.chcolor(0, 0, 255, 255); g.frect(ul, sz); g.chcolor(); if (!sel) break; } }
public CharPos charat(Coord c) { if (c.y < -sb.val) { if (msgs.size() < 1) return (null); Message msg = msgs.get(0); if (!(msg.text() instanceof RichText)) return (null); RichText.TextPart fp = null; for (RichText.Part part = ((RichText) msg.text()).parts; part != null; part = part.next) { if (part instanceof RichText.TextPart) { fp = (RichText.TextPart) part; break; } } if (fp == null) return (null); return (new CharPos(msg, fp, TextHitInfo.leading(0))); } Coord hc = new Coord(); Message msg = messageat(c, hc); if ((msg == null) || !(msg.text() instanceof RichText)) return (null); RichText rt = (RichText) msg.text(); RichText.Part p = rt.partat(hc); if (p == null) { RichText.TextPart lp = null; for (RichText.Part part = ((RichText) msg.text()).parts; part != null; part = part.next) { if (part instanceof RichText.TextPart) lp = (RichText.TextPart) part; } if (lp == null) return (null); return (new CharPos(msg, lp, TextHitInfo.trailing(lp.end - lp.start - 1))); } if (!(p instanceof RichText.TextPart)) return (null); RichText.TextPart tp = (RichText.TextPart) p; return (new CharPos(msg, tp, tp.charat(hc))); }
/** Send the composed text to the client. */ private void sendComposedText() { AttributedString as = new AttributedString(buffer.toString()); as.addAttribute( TextAttribute.INPUT_METHOD_HIGHLIGHT, InputMethodHighlight.SELECTED_RAW_TEXT_HIGHLIGHT); context.dispatchInputMethodEvent( InputMethodEvent.INPUT_METHOD_TEXT_CHANGED, as.getIterator(), 0, TextHitInfo.leading(insertionPoint), null); }
/** Send the committed text to the client. */ private void sendCommittedText() { AttributedString as = new AttributedString(buffer.toString()); context.dispatchInputMethodEvent( InputMethodEvent.INPUT_METHOD_TEXT_CHANGED, as.getIterator(), buffer.length(), TextHitInfo.leading(insertionPoint), null); buffer.setLength(0); insertionPoint = 0; format = UNSET; }
/** Move the insertion point one position to the right in the composed text. */ private void moveCaretRight() { int len = buffer.length(); if (++insertionPoint > len) { insertionPoint = len; beep(); } context.dispatchInputMethodEvent( InputMethodEvent.CARET_POSITION_CHANGED, null, 0, TextHitInfo.leading(insertionPoint), null); }
/** * Move the insertion point one position to the left in the composed text. Do not let the caret * move to the left of the "\\u" or "\\U". */ private void moveCaretLeft() { int len = buffer.length(); if (--insertionPoint < 2) { insertionPoint++; beep(); } else if (format == SURROGATE_PAIR && insertionPoint == 7) { insertionPoint = 8; beep(); } context.dispatchInputMethodEvent( InputMethodEvent.CARET_POSITION_CHANGED, null, 0, TextHitInfo.leading(insertionPoint), null); }
public void render(int w, int h, Graphics2D g2) { FontRenderContext frc = g2.getFontRenderContext(); for (int i = 0; i < 2; i++) { layouts[i] = new TextLayout(text[i], fonts[i], frc); float rw = layouts[i].getAdvance(); float rh = layouts[i].getAscent() + layouts[i].getDescent(); float rx = (float) ((w - rw) / 2); float ry = (float) ((i == 0) ? h / 3 : h * 0.75f); // draw highlighted shape Shape hilite = layouts[i].getLogicalHighlightShape(0, curPos[i]); AffineTransform at = AffineTransform.getTranslateInstance(rx, ry); hilite = at.createTransformedShape(hilite); float hy = (float) hilite.getBounds2D().getY(); float hh = (float) hilite.getBounds2D().getHeight(); g2.setColor(colors[i]); g2.fill(hilite); // get caret shape Shape[] shapes = layouts[i].getCaretShapes(curPos[i]); Shape caret = at.createTransformedShape(shapes[0]); g2.setColor(Color.black); layouts[i].draw(g2, rx, ry); g2.draw(caret); g2.draw(new Rectangle2D.Float(rx, hy, rw, hh)); // Display character advances. for (int j = 0; j <= layouts[i].getCharacterCount(); j++) { float[] cInfo = layouts[i].getCaretInfo(TextHitInfo.leading(j)); String str = String.valueOf((int) cInfo[0]); TextLayout tl = new TextLayout(str, smallF, frc); tl.draw(g2, (float) rx + cInfo[0] - tl.getAdvance() / 2, hy + hh + tl.getAscent() + 1.0f); } } }
public void sendInputMethodEvent( int id, long when, String text, int[] clauseBoundary, String[] clauseReading, int[] attributeBoundary, byte[] attributeValue, int commitedTextLength, int caretPos, int visiblePos) { AttributedCharacterIterator iterator = null; if (text != null) { // construct AttributedString AttributedString attrStr = new AttributedString(text); // set Language Information attrStr.addAttribute(Attribute.LANGUAGE, Locale.getDefault(), 0, text.length()); // set Clause and Reading Information if (clauseBoundary != null && clauseReading != null && clauseReading.length != 0 && clauseBoundary.length == clauseReading.length + 1 && clauseBoundary[0] == 0 && clauseBoundary[clauseReading.length] == text.length()) { for (int i = 0; i < clauseBoundary.length - 1; i++) { attrStr.addAttribute( Attribute.INPUT_METHOD_SEGMENT, new Annotation(null), clauseBoundary[i], clauseBoundary[i + 1]); attrStr.addAttribute( Attribute.READING, new Annotation(clauseReading[i]), clauseBoundary[i], clauseBoundary[i + 1]); } } else { // if (clauseBoundary != null) // System.out.println("Invalid clause information!"); attrStr.addAttribute( Attribute.INPUT_METHOD_SEGMENT, new Annotation(null), 0, text.length()); attrStr.addAttribute(Attribute.READING, new Annotation(""), 0, text.length()); } // set Hilight Information if (attributeBoundary != null && attributeValue != null && attributeValue.length != 0 && attributeBoundary.length == attributeValue.length + 1 && attributeBoundary[0] == 0 && attributeBoundary[attributeValue.length] == text.length()) { for (int i = 0; i < attributeBoundary.length - 1; i++) { InputMethodHighlight highlight; switch (attributeValue[i]) { case ATTR_TARGET_CONVERTED: highlight = InputMethodHighlight.SELECTED_CONVERTED_TEXT_HIGHLIGHT; break; case ATTR_CONVERTED: highlight = InputMethodHighlight.UNSELECTED_CONVERTED_TEXT_HIGHLIGHT; break; case ATTR_TARGET_NOTCONVERTED: highlight = InputMethodHighlight.SELECTED_RAW_TEXT_HIGHLIGHT; break; case ATTR_INPUT: case ATTR_INPUT_ERROR: default: highlight = InputMethodHighlight.UNSELECTED_RAW_TEXT_HIGHLIGHT; break; } attrStr.addAttribute( TextAttribute.INPUT_METHOD_HIGHLIGHT, highlight, attributeBoundary[i], attributeBoundary[i + 1]); } } else { // if (attributeBoundary != null) // System.out.println("Invalid attribute information!"); attrStr.addAttribute( TextAttribute.INPUT_METHOD_HIGHLIGHT, InputMethodHighlight.UNSELECTED_CONVERTED_TEXT_HIGHLIGHT, 0, text.length()); } // get iterator iterator = attrStr.getIterator(); } Component source = getClientComponent(); if (source == null) return; InputMethodEvent event = new InputMethodEvent( source, id, when, iterator, commitedTextLength, TextHitInfo.leading(caretPos), TextHitInfo.leading(visiblePos)); WToolkit.postEvent(WToolkit.targetToAppContext(source), event); }