Hi, the attached patch fixes the clipping problem in FieldView when there is an allocation area to be considered. While testing the patch I found out that a similar problem exists for the DefaultHighlight.DefaultHighlightPainter's paint method and therefore fixed that, too.
@Roman: Swing book says that *usually* the clip is not used in a View's paint
method however that does not read like *must not* and I saw no other possibility
to fix the issue. Would you have solved it differently?
The ChangeLog:
2006-04-14 Robert Schuster <[EMAIL PROTECTED]>
* javax/swing/text/FieldView.java:
(paint): Apply clipping rectangle of the allocation area
before painting the text.
* javax/swing/text/DefaultHighlighter.java:
(DefaultHighlighter.DefaultHighlightPainter): Use SwingUtilities to
compute union and intersection, calculate intersection with allocation
area before painting, adjust x and width when painting multiple lines
by the range of the allocation area.
cya
Robert
Index: javax/swing/text/FieldView.java
===================================================================
RCS file: /cvsroot/classpath/classpath/javax/swing/text/FieldView.java,v
retrieving revision 1.14
diff -u -r1.14 FieldView.java
--- javax/swing/text/FieldView.java 13 Apr 2006 13:03:16 -0000 1.14
+++ javax/swing/text/FieldView.java 14 Apr 2006 18:06:47 -0000
@@ -236,8 +236,13 @@
checkContainer();
Shape newAlloc = adjustAllocation(s);
-
+
+ // Set a clip to prevent drawing outside of the allocation area.
+ // TODO: Is there a better way to achieve this?
+ Shape clip = g.getClip();
+ g.setClip(s);
super.paint(g, newAlloc);
+ g.setClip(clip);
}
public void insertUpdate(DocumentEvent ev, Shape shape, ViewFactory vf)
Index: javax/swing/text/DefaultHighlighter.java
===================================================================
RCS file: /cvsroot/classpath/classpath/javax/swing/text/DefaultHighlighter.java,v
retrieving revision 1.10
diff -u -r1.10 DefaultHighlighter.java
--- javax/swing/text/DefaultHighlighter.java 14 Apr 2006 17:32:39 -0000 1.10
+++ javax/swing/text/DefaultHighlighter.java 14 Apr 2006 18:06:47 -0000
@@ -47,6 +47,7 @@
import java.awt.Shape;
import java.util.ArrayList;
+import javax.swing.SwingUtilities;
import javax.swing.plaf.TextUI;
public class DefaultHighlighter extends LayeredHighlighter
@@ -93,8 +94,18 @@
Rectangle l0 = ui.modelToView(t, p0, null);
Rectangle l1 = ui.modelToView(t, p1, null);
+ // Note: The computed locations may lie outside of the allocation
+ // area if the text is scrolled.
+
if (l0.y == l1.y)
- paintHighlight(g, l0.union(l1));
+ {
+ SwingUtilities.computeUnion(l0.x, l0.y, l0.width, l0.height, l1);
+
+ // Paint only inside the allocation area.
+ SwingUtilities.computeIntersection(rect.x, rect.y, rect.width, rect.height, l1);
+
+ paintHighlight(g, l1);
+ }
else
{
// 1. The line of p0 is painted from the position of p0
@@ -106,7 +117,11 @@
// position of p1.
// Highlight first line until the end.
- l0.width = rect.width - l0.x;
+ // If rect.x is non-zero the calculation will properly adjust the
+ // area to be painted.
+ l0.x -= rect.x;
+ l0.width = rect.width - l0.x - rect.x;
+
paintHighlight(g, l0);
int posBelow = Utilities.getPositionBelow(t, p0, l0.x);
signature.asc
Description: OpenPGP digital signature
