Hi, this patch fixes PR 26675 and some problems which are related to this. At first my code to draw selected and unselected text in the same line did not work. I found out that this is because the return value of Utilities.drawTabbedText did not return the x coordinate after the last drawn char.
This is now fixed. You can see the results of this patch in the swing demo: Open the TextArea subdemo, go to the custom colors tab and select some text in the bottom right textarea. It should turn selected text to white and the selection color itself is black. This patch affects all textcomponents which use the PlainView class. 2006-03-17 Robert Schuster <[EMAIL PROTECTED]> * javax/swing/text/PlainView.java: (drawLine): Rewritten. (drawSelectedText): Corrected last argument for Utilities.drawTabbedText() call. (paint): Store start and end of selection in object variables, store constant values of for-loop in local variables. * javax/swing/text/Utilities.java: (drawTabbedText): Add 'pixelWidth' to the return value, store constant value of for-loop in local variable. cya Robert
Index: javax/swing/text/PlainView.java =================================================================== RCS file: /cvsroot/classpath/classpath/javax/swing/text/PlainView.java,v retrieving revision 1.38 diff -u -r1.38 PlainView.java --- javax/swing/text/PlainView.java 1 Mar 2006 20:39:49 -0000 1.38 +++ javax/swing/text/PlainView.java 17 Mar 2006 15:10:17 -0000 @@ -59,6 +59,18 @@ * The color that is used to draw disabled text fields. */ Color disabledColor; + + /** + * While painting this is the textcomponent's current start index + * of the selection. + */ + int selectionStart; + + /** + * While painting this is the textcomponent's current end index + * of the selection. + */ + int selectionEnd; Font font; @@ -150,12 +162,47 @@ { try { - metrics = g.getFontMetrics(); - // FIXME: Selected text are not drawn yet. Element line = getElement().getElement(lineIndex); - drawUnselectedText(g, x, y, line.getStartOffset(), - line.getEndOffset() - 1); - //drawSelectedText(g, , , , ); + int startOffset = line.getStartOffset(); + int endOffset = line.getEndOffset(); + + if (selectionStart <= startOffset) + // Selection starts before the line ... + if (selectionEnd <= startOffset) + { + // end ends before the line: Draw completely unselected text. + drawUnselectedText(g, x, y, startOffset, endOffset); + } + else if (selectionEnd <= endOffset) + { + // and ends within the line: First part is selected, + // second is not. + x = drawSelectedText(g, x, y, startOffset, selectionEnd); + drawUnselectedText(g, x, y, selectionEnd, endOffset); + } + else + // and ends behind the line: Draw completely selected text. + drawSelectedText(g, x, y, startOffset, endOffset); + else if (selectionStart < endOffset) + // Selection starts within the line .. + if (selectionEnd < endOffset) + { + // and ends within it: First part unselected, second part + // selected, third part unselected. + x = drawUnselectedText(g, x, y, startOffset, selectionStart); + x = drawSelectedText(g, x, y, selectionStart, selectionEnd); + drawUnselectedText(g, x, y, selectionEnd, endOffset); + } + else + { + // and ends behind the line: First part unselected, second + // part selected. + x = drawUnselectedText(g, x, y, startOffset, selectionStart); + drawSelectedText(g, x, y, selectionStart, selectionEnd); + } + else + // Selection is behind this line: Draw completely unselected text. + drawUnselectedText(g, x, y, startOffset, endOffset); } catch (BadLocationException e) { @@ -171,7 +218,7 @@ g.setColor(selectedColor); Segment segment = getLineBuffer(); getDocument().getText(p0, p1 - p0, segment); - return Utilities.drawTabbedText(segment, x, y, g, this, 0); + return Utilities.drawTabbedText(segment, x, y, g, this, segment.offset); } /** @@ -212,6 +259,8 @@ selectedColor = textComponent.getSelectedTextColor(); unselectedColor = textComponent.getForeground(); disabledColor = textComponent.getDisabledTextColor(); + selectionStart = textComponent.getSelectionStart(); + selectionEnd = textComponent.getSelectionEnd(); Rectangle rect = s.getBounds(); @@ -219,11 +268,13 @@ Document document = textComponent.getDocument(); Element root = document.getDefaultRootElement(); int y = rect.y + metrics.getAscent(); + int height = metrics.getHeight(); - for (int i = 0; i < root.getElementCount(); i++) + int count = root.getElementCount(); + for (int i = 0; i < count; i++) { drawLine(i, g, rect.x, y); - y += metrics.getHeight(); + y += height; } } Index: javax/swing/text/Utilities.java =================================================================== RCS file: /cvsroot/classpath/classpath/javax/swing/text/Utilities.java,v retrieving revision 1.25 diff -u -r1.25 Utilities.java --- javax/swing/text/Utilities.java 10 Mar 2006 22:14:53 -0000 1.25 +++ javax/swing/text/Utilities.java 17 Mar 2006 15:10:17 -0000 @@ -99,8 +99,10 @@ int pixelWidth = 0; int pos = s.offset; int len = 0; + + int end = s.offset + s.count; - for (int offset = s.offset; offset < (s.offset + s.count); ++offset) + for (int offset = s.offset; offset < end; ++offset) { char c = buffer[offset]; if (c == '\t' || c == '\n') @@ -140,7 +142,7 @@ if (len > 0) g.drawChars(buffer, pos, len, pixelX, pixelY + ascent); - return pixelX; + return pixelX + pixelWidth; } /**
signature.asc
Description: OpenPGP digital signature