Hi,
I fixed the usage of the p0 variable in one function. This caused
JTextComponent.viewToModel to return unsuitable results.

getPositionFromAbove and getPositionFromBelow have been rewritten. They now work
as one would expect it from the JDK but I have to find out whether they are
responsible for the magic caret position trick, too.

2006-02-10  Robert Schuster  <[EMAIL PROTECTED]>

        * javax/swing/text/Utilities.java:
        (getTabbedTextOffset): Fixed usage of variable p0.
        (getPositionAbove): Rewritten.
        (getPositionBelow): Rewritten.

cya
Robert
Index: javax/swing/text/Utilities.java
===================================================================
RCS file: /cvsroot/classpath/classpath/javax/swing/text/Utilities.java,v
retrieving revision 1.20
diff -u -r1.20 Utilities.java
--- javax/swing/text/Utilities.java	31 Jan 2006 10:03:32 -0000	1.20
+++ javax/swing/text/Utilities.java	10 Feb 2006 09:27:11 -0000
@@ -241,9 +241,10 @@
     int pos;
     int currentX = x0;
 
-    for (pos = p0; pos < s.count; pos++)
+    for (pos = 0; pos < s.count; pos++)
       {
         char nextChar = s.array[s.offset+pos];
+        
         if (nextChar == 0)
           {
             if (! round)
@@ -259,6 +260,7 @@
             else
               currentX = (int) te.nextTabStop(currentX, pos);
           }
+        
         if (currentX > x)
           {
             if (! round)
@@ -266,7 +268,8 @@
             break;
           }
       }
-    return pos;
+    
+    return pos + p0;
   }
 
   /**
@@ -574,15 +577,29 @@
   public static final int getPositionAbove(JTextComponent c, int offset, int x)
     throws BadLocationException
   {
-    View rootView = c.getUI().getRootView(c);
-    Rectangle r = c.modelToView(offset);
-    int offs = c.viewToModel(new Point(x, r.y));
-    int pos = rootView.getNextVisualPositionFrom(offs,
-                                    Position.Bias.Forward,
-                                    SwingUtilities.calculateInnerArea(c, null),
-                                    SwingConstants.NORTH,
-                                    new Position.Bias[1]);
-    return pos;
+    int offs = getRowStart(c, offset);
+    
+    if(offs == -1)
+      return -1;
+
+    // Effectively calculates the y value of the previous line.
+    Point pt = c.modelToView(offs-1).getLocation();
+    
+    pt.x = x;
+    
+    // Calculate a simple fitting offset.
+    offs = c.viewToModel(pt);
+    
+    // Find out the real x positions of the calculated character and its
+    // neighbour.
+    int offsX = c.modelToView(offs).getLocation().x;
+    int offsXNext = c.modelToView(offs+1).getLocation().x;
+    
+    // Chose the one which is nearer to us and return its offset.
+    if (Math.abs(offsX-x) < Math.abs(offsXNext-x))
+      return offs;
+    else
+      return offs+1;
   }
 
   /**
@@ -601,14 +618,31 @@
   public static final int getPositionBelow(JTextComponent c, int offset, int x)
     throws BadLocationException
   {
-    View rootView = c.getUI().getRootView(c);
-    Rectangle r = c.modelToView(offset);
-    int offs = c.viewToModel(new Point(x, r.y));
-    int pos = rootView.getNextVisualPositionFrom(offs,
-                                    Position.Bias.Forward,
-                                    SwingUtilities.calculateInnerArea(c, null),
-                                    SwingConstants.SOUTH,
-                                    new Position.Bias[1]);
-    return pos;
-  }
+    int offs = getRowEnd(c, offset);
+    
+    if(offs == -1)
+      return -1;
+
+    // Effectively calculates the y value of the previous line.
+    Point pt = c.modelToView(offs+1).getLocation();
+    
+    pt.x = x;
+    
+    // Calculate a simple fitting offset.
+    offs = c.viewToModel(pt);
+    
+    if (offs == c.getDocument().getLength())
+      return offs;
+    
+    // Find out the real x positions of the calculated character and its
+    // neighbour.
+    int offsX = c.modelToView(offs).getLocation().x;
+    int offsXNext = c.modelToView(offs+1).getLocation().x;
+    
+    // Chose the one which is nearer to us and return its offset.
+    if (Math.abs(offsX-x) < Math.abs(offsXNext-x))
+      return offs;
+    else
+      return offs+1;
+    }
 }

Attachment: signature.asc
Description: OpenPGP digital signature

Reply via email to