protected void paintCaret(Graphics gfx, int line, int y) { // System.out.println("painting caret " + line + " " + y); if (textArea.isCaretVisible()) { // System.out.println("caret is visible"); int offset = textArea.getCaretPosition() - textArea.getLineStartOffset(line); int caretX = textArea._offsetToX(line, offset); int caretWidth = ((blockCaret || textArea.isOverwriteEnabled()) ? fm.charWidth('w') : 1); y += fm.getLeading() + fm.getMaxDescent(); int height = fm.getHeight(); // System.out.println("caretX, width = " + caretX + " " + caretWidth); gfx.setColor(caretColor); if (textArea.isOverwriteEnabled()) { gfx.fillRect(caretX, y + height - 1, caretWidth, 1); } else { // some machines don't like the drawRect for the single // pixel caret.. this caused a lot of hell because on that // minority of machines, the caret wouldn't show up past // the first column. the fix is to use drawLine() in // those cases, as a workaround. if (caretWidth == 1) { gfx.drawLine(caretX, y, caretX, y + height - 1); } else { gfx.drawRect(caretX, y, caretWidth - 1, height - 1); } // gfx.drawRect(caretX, y, caretWidth, height - 1); } } }
protected void paintSyntaxLine( Graphics gfx, TokenMarker tokenMarker, int line, Font defaultFont, Color defaultColor, int x, int y) { textArea.getLineText(currentLineIndex, currentLine); currentLineTokens = tokenMarker.markTokens(currentLine, currentLineIndex); paintHighlight(gfx, line, y); gfx.setFont(defaultFont); gfx.setColor(defaultColor); y += fm.getHeight(); x = SyntaxUtilities.paintSyntaxLine(currentLine, currentLineTokens, styles, this, gfx, x, y); /* * Draw characters via input method. */ if (compositionTextPainter != null && compositionTextPainter.hasComposedTextLayout()) { compositionTextPainter.draw(gfx, lineHighlightColor); } if (eolMarkers) { gfx.setColor(eolMarkerColor); gfx.drawString(".", x, y); } }
public ColorPane() { super(); Font font = new Font("Monospaced", Font.PLAIN, 12); FontMetrics fm = getFontMetrics(font); lineHeight = fm.getHeight(); setFont(font); setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5)); }
protected void paintLineHighlight(Graphics gfx, int line, int y) { int height = fm.getHeight(); y += fm.getLeading() + fm.getMaxDescent(); int selectionStart = textArea.getSelectionStart(); int selectionEnd = textArea.getSelectionStop(); if (selectionStart == selectionEnd) { if (lineHighlight) { gfx.setColor(lineHighlightColor); gfx.fillRect(0, y, getWidth(), height); } } else { gfx.setColor(selectionColor); int selectionStartLine = textArea.getSelectionStartLine(); int selectionEndLine = textArea.getSelectionStopLine(); int lineStart = textArea.getLineStartOffset(line); int x1, x2; if (textArea.isSelectionRectangular()) { int lineLen = textArea.getLineLength(line); x1 = textArea._offsetToX( line, Math.min( lineLen, selectionStart - textArea.getLineStartOffset(selectionStartLine))); x2 = textArea._offsetToX( line, Math.min(lineLen, selectionEnd - textArea.getLineStartOffset(selectionEndLine))); if (x1 == x2) x2++; } else if (selectionStartLine == selectionEndLine) { x1 = textArea._offsetToX(line, selectionStart - lineStart); x2 = textArea._offsetToX(line, selectionEnd - lineStart); } else if (line == selectionStartLine) { x1 = textArea._offsetToX(line, selectionStart - lineStart); x2 = getWidth(); } else if (line == selectionEndLine) { // x1 = 0; // hack from stendahl to avoid doing weird side selection thing x1 = textArea._offsetToX(line, 0); // attempt at getting the gutter too, but doesn't seem to work // x1 = textArea._offsetToX(line, -textArea.getHorizontalOffset()); x2 = textArea._offsetToX(line, selectionEnd - lineStart); } else { // x1 = 0; // hack from stendahl to avoid doing weird side selection thing x1 = textArea._offsetToX(line, 0); // attempt at getting the gutter too, but doesn't seem to work // x1 = textArea._offsetToX(line, -textArea.getHorizontalOffset()); x2 = getWidth(); } // "inlined" min/max() gfx.fillRect(x1 > x2 ? x2 : x1, y, x1 > x2 ? (x1 - x2) : (x2 - x1), height); } }
protected void paintBracketHighlight(Graphics gfx, int line, int y) { int position = textArea.getBracketPosition(); if (position == -1) return; y += fm.getLeading() + fm.getMaxDescent(); int x = textArea._offsetToX(line, position); gfx.setColor(bracketHighlightColor); // Hack!!! Since there is no fast way to get the character // from the bracket matching routine, we use ( since all // brackets probably have the same width anyway gfx.drawRect(x, y, fm.charWidth('(') - 1, fm.getHeight() - 1); }
protected void paintLineHighlight(Graphics gfx, int line, int y) { int height = fm.getHeight(); y += fm.getLeading() + fm.getMaxDescent(); int selectionStart = textArea.getSelectionStart(); int selectionEnd = textArea.getSelectionEnd(); if (selectionStart == selectionEnd) { if (lineHighlight) { gfx.setColor(lineHighlightColor); gfx.fillRect(0, y, getWidth(), height); } } else { gfx.setColor(selectionColor); int selectionStartLine = textArea.getSelectionStartLine(); int selectionEndLine = textArea.getSelectionEndLine(); int lineStart = textArea.getLineStartOffset(line); int x1, x2; if (textArea.isSelectionRectangular()) { int lineLen = textArea.getLineLength(line); x1 = textArea._offsetToX( line, Math.min( lineLen, selectionStart - textArea.getLineStartOffset(selectionStartLine))); x2 = textArea._offsetToX( line, Math.min(lineLen, selectionEnd - textArea.getLineStartOffset(selectionEndLine))); if (x1 == x2) x2++; } else if (selectionStartLine == selectionEndLine) { x1 = textArea._offsetToX(line, selectionStart - lineStart); x2 = textArea._offsetToX(line, selectionEnd - lineStart); } else if (line == selectionStartLine) { x1 = textArea._offsetToX(line, selectionStart - lineStart); x2 = getWidth(); } else if (line == selectionEndLine) { x1 = 0; x2 = textArea._offsetToX(line, selectionEnd - lineStart); } else { x1 = 0; x2 = getWidth(); } // "inlined" min/max() gfx.fillRect(x1 > x2 ? x2 : x1, y, x1 > x2 ? (x1 - x2) : (x2 - x1), height); } }
protected void paintPlainLine( Graphics gfx, int line, Font defaultFont, Color defaultColor, int x, int y) { paintHighlight(gfx, line, y); textArea.getLineText(line, currentLine); gfx.setFont(defaultFont); gfx.setColor(defaultColor); y += fm.getHeight(); x = Utilities.drawTabbedText(currentLine, x, y, gfx, this, 0); if (eolMarkers) { gfx.setColor(eolMarkerColor); gfx.drawString(".", x, y); } }
protected void paintCaret(Graphics gfx, int line, int y) { if (textArea.isCaretVisible()) { int offset = textArea.getCaretPosition() - textArea.getLineStartOffset(line); int caretX = textArea._offsetToX(line, offset); int caretWidth = ((blockCaret || textArea.isOverwriteEnabled()) ? fm.charWidth('w') : 1); y += fm.getLeading() + fm.getMaxDescent(); int height = fm.getHeight(); gfx.setColor(caretColor); if (textArea.isOverwriteEnabled()) { gfx.fillRect(caretX, y + height - 1, caretWidth, 1); } else { gfx.drawRect(caretX, y, caretWidth - 1, height - 1); } } }
/* * Determine the Y offset for the current row */ private int getOffsetY(int rowStartOffset, FontMetrics fontMetrics) throws BadLocationException { // Get the bounding rectangle of the row Rectangle r = component.modelToView(rowStartOffset); int lineHeight = fontMetrics.getHeight(); int y = r.y + r.height; int descent = 0; // The text needs to be positioned above the bottom of the bounding // rectangle based on the descent of the font(s) contained on the row. if (r.height == lineHeight) { // default font is being used descent = fontMetrics.getDescent(); } else { // We need to check all the attributes for font changes if (fonts == null) { fonts = new HashMap<String, FontMetrics>(); } Element root = component.getDocument().getDefaultRootElement(); int index = root.getElementIndex(rowStartOffset); Element line = root.getElement(index); for (int i = 0; i < line.getElementCount(); i++) { Element child = line.getElement(i); AttributeSet as = child.getAttributes(); String fontFamily = (String) as.getAttribute(StyleConstants.FontFamily); Integer fontSize = (Integer) as.getAttribute(StyleConstants.FontSize); String key = fontFamily + fontSize; FontMetrics fm = fonts.get(key); if (fm == null) { Font font = new Font(fontFamily, Font.PLAIN, fontSize); fm = component.getFontMetrics(font); fonts.put(key, fm); } descent = Math.max(descent, fm.getDescent()); } } return y - descent; }
protected void paintLine(Graphics gfx, TokenMarker tokenMarker, int line, int x) { Font defaultFont = getFont(); Color defaultColor = getForeground(); currentLineIndex = line; int y = textArea.lineToY(line); if (line < 0 || line >= textArea.getLineCount()) { if (paintInvalid) { paintHighlight(gfx, line, y); styles[Token.INVALID].setGraphicsFlags(gfx, defaultFont); gfx.drawString("~", 0, y + fm.getHeight()); } } else if (tokenMarker == null) { paintPlainLine(gfx, line, defaultFont, defaultColor, x, y); } else { paintSyntaxLine(gfx, tokenMarker, line, defaultFont, defaultColor, x, y); } }
protected void paintPlainLine( Graphics gfx, int line, Font defaultFont, Color defaultColor, int x, int y) { paintHighlight(gfx, line, y); textArea.getLineText(line, currentLine); gfx.setFont(defaultFont); gfx.setColor(defaultColor); y += fm.getHeight(); x = Utilities.drawTabbedText(currentLine, x, y, gfx, this, 0); // Draw characters via input method. if (compositionTextPainter != null && compositionTextPainter.hasComposedTextLayout()) { compositionTextPainter.draw(gfx, lineHighlightColor); } if (eolMarkers) { gfx.setColor(eolMarkerColor); gfx.drawString(".", x, y); } }
protected void paintSyntaxLine( Graphics gfx, TokenMarker tokenMarker, int line, Font defaultFont, Color defaultColor, int x, int y) { textArea.getLineText(currentLineIndex, currentLine); currentLineTokens = tokenMarker.markTokens(currentLine, currentLineIndex); paintHighlight(gfx, line, y); gfx.setFont(defaultFont); gfx.setColor(defaultColor); y += fm.getHeight(); x = SyntaxUtilities.paintSyntaxLine(currentLine, currentLineTokens, styles, this, gfx, x, y); if (eolMarkers) { gfx.setColor(eolMarkerColor); gfx.drawString(".", x, y); } }
/** * Converts a y co-ordinate to a line index. * * @param y The y co-ordinate */ public int yToLine(int y) { FontMetrics fm = painter.getFontMetrics(); int height = fm.getHeight(); return Math.max(0, Math.min(getLineCount() - 1, y / height + firstLine)); }
/** * Converts a line index to a y co-ordinate. * * @param line The line */ public int lineToY(int line) { FontMetrics fm = painter.getFontMetrics(); return (line - firstLine) * fm.getHeight() - (fm.getLeading() + fm.getMaxDescent()); }
private int getFontHeight() { return fmRef.getHeight(); }