This is the mail archive of the java-patches@gcc.gnu.org mailing list for the Java project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[gui][patch] fixes to viewport


hi,

I committed this patch to the java-gui-branch. it fixes viewport layout calculations so that they behave more like sun's swing (not like sun's documentation, which plainly lies!)

it also includes some small fixes to missing methods and properties elsewhere in swing.

-graydon

2004-06-15 Graydon Hoare <graydon@redhat.com>

	* javax/swing/ImageIcon.java (ImageIcon): New constructor.
	* javax/swing/JFrame.java (defaultLookAndFeelDecorated): New property.
	* javax/swing/JViewport.java
	(getExtentSize): Return size rather than preferred size.
	(toViewCoordinates): New methods.
	(getViewSize): Return size rather than preferred size.
	(setViewSize): Note view size as set.
	* javax/swing/ViewportLayout.java (layoutContainer): Reimplement.
	* javax/swing/plaf/basic/BasicScrollBarUI.java
	(getPreferredSize): Don't redo layout.
	* javax/swing/plaf/basic/BasicViewportUI.java
	(paint): Translate image properly and eat exceptions.
--- javax/swing/ImageIcon.java	10 Jun 2004 08:49:07 -0000	1.2.18.4
+++ javax/swing/ImageIcon.java	15 Jun 2004 22:21:39 -0000
@@ -66,6 +66,11 @@
   {
   }
 
+  public ImageIcon(URL url)
+  {
+    image = Toolkit.getDefaultToolkit().getImage(url);
+  }
+
   public ImageIcon(String file, String descr)
   {
     this.file = file;
--- javax/swing/JFrame.java	10 Jun 2004 14:59:46 -0000	1.4.2.5
+++ javax/swing/JFrame.java	15 Jun 2004 22:21:40 -0000
@@ -65,6 +65,17 @@
 
     private int close_action = HIDE_ON_CLOSE;    
     
+  private static boolean defaultLookAndFeelDecorated = false;    
+
+  public static void setDefaultLookAndFeelDecorated(boolean d)
+  {
+    defaultLookAndFeelDecorated = d;
+  }
+
+  public static boolean isDefaultLookAndFeelDecorated()
+  {
+    return defaultLookAndFeelDecorated;
+  }
 
     /***************************************************
      *
--- javax/swing/JViewport.java	8 Jun 2004 08:42:53 -0000	1.3.2.5
+++ javax/swing/JViewport.java	15 Jun 2004 22:21:40 -0000
@@ -135,11 +135,23 @@
   public Dimension getExtentSize()
   {
     if (extentSize == null)
-      return getPreferredSize();
+      return toViewCoordinates(getSize());
     else
       return extentSize;
   }
 
+  public Dimension toViewCoordinates(Dimension size)
+  {
+    return size;
+  }
+
+  public Point toViewCoordinates(Point p)
+  {
+    Point pos = getViewPosition();
+    return new Point(p.x + pos.x,
+                     p.y + pos.y);
+  }
+
   public void setExtentSize(Dimension newSize)
   {
     extentSize = newSize;
@@ -148,10 +160,10 @@
 
   public Dimension getViewSize()
   {
-    if (viewSize == null)
-      return getView().getPreferredSize();
-    else
+    if (isViewSizeSet)
       return viewSize;
+    else
+      return getView().getSize();
   }
 
 
@@ -160,10 +172,17 @@
     viewSize = newSize;
     Component view = getView();
     if (view != null)
-      view.setSize(newSize);
+      view.setSize(viewSize);
+    isViewSizeSet = true;
     fireStateChanged();
   }
 
+  /**
+   * Get the viewport's position in view space. Despite confusing name,
+   * this really does return the viewport's (0,0) position in view space,
+   * not the view's position.
+   */
+
   public Point getViewPosition()
   {
     Component view = getView();
--- javax/swing/ViewportLayout.java	21 May 2004 23:34:14 -0000	1.3.8.2
+++ javax/swing/ViewportLayout.java	15 Jun 2004 22:21:40 -0000
@@ -71,7 +71,6 @@
       {
         Scrollable sc = (Scrollable) view;
         Dimension d = sc.getPreferredScrollableViewportSize();
-        // System.err.println(this + ".preferredLayoutSize() : scrollable -> " + d);
         return d;
       }
     else
@@ -83,20 +82,84 @@
     Component view = vp.getView();
     return view.getMinimumSize();
   }
+
+  /**
+   * Layout the view and viewport to respect the following rules. These are
+   * not precisely the rules described in sun's javadocs, but they are the
+   * rules which sun's swing implementation follows, if you watch its
+   * behavior:
+   *
+   * <ol> 
+   * 
+   * <li>If the port is larger than the view's minimum size, put the port
+   * at view position <code>(0,0)</code> and make the view's size equal to
+   * the port's.</li>
+   *
+   * <li>If the port is smaller than the view, leave the view at its
+   * minimum size. also, do not move the port, <em>unless</em> the port
+   * extends into space <em>past</em> the edge of the view. If so, move the
+   * port up or to the left, in view space, by the amount of empty space
+   * (keep the lower and right edges lined up)</li>
+   *
+   * </ol>
+   *
+   * @see JViewport#getViewSize
+   * @see JViewport#setViewSize
+   * @see JViewport#getViewPosition
+   * @see JViewport#setViewPosition
+   */
+
   public void layoutContainer(Container parent) 
   {
-    JViewport vp = (JViewport)parent;
-    Component view = vp.getView();
-    Rectangle portBounds = vp.getBounds();
+    // The way to interpret this function is basically to ignore the names
+    // of methods it calls, and focus on the variable names here. getViewRect
+    // doesn't, for example, return the view; it returns the port bounds in
+    // view space. Likwise setViewPosition doesn't reposition the view; it 
+    // positions the port, in view coordinates.
+
+    JViewport port = (JViewport) parent;    
+    Component view = port.getView();
+
+    // These dimensions and positions are in *view space*.  Do not mix
+    // variables in here from port space (eg. parent.getBounds()). This
+    // function should be entirely in view space, because the methods on
+    // the viewport require inputs in view space.
+
+    Rectangle portBounds = port.getViewRect();
+    Dimension viewSize = port.getViewSize();
     Dimension viewMinimum = view.getMinimumSize();
-    int width = Math.max(portBounds.width, 
-                         viewMinimum.width);
-    int height = Math.max(portBounds.height, 
-                          viewMinimum.height);
-    int x = Math.min(0, portBounds.width - width);
-    int y = Math.min(0, portBounds.height - height);
-    // System.err.println(this + ".layoutContainer() : width = " + width + ", height = " + height);
-    vp.setViewPosition(new Point(x, y));
-    vp.setViewSize(new Dimension(width, height));
+    Point portLowerRight = new Point(portBounds.x + portBounds.width,
+                                     portBounds.y + portBounds.height);
+        
+    // vertical implementation of the above rules
+    if (portBounds.height >= viewMinimum.height)
+      {
+        portBounds.y = 0;
+        viewSize.height = portBounds.height;
+      }
+    else
+      {
+        viewSize.height = viewMinimum.height;
+        int overextension = portLowerRight.y - viewSize.height;
+        if (overextension > 0)
+            portBounds.y -= overextension;
+      }
+
+    // horizontal implementation of the above rules
+    if (portBounds.width >= viewMinimum.width)
+      {
+        portBounds.x = 0;
+        viewSize.width = portBounds.width;
+      }
+    else
+      {
+        viewSize.width = viewMinimum.width;
+        int overextension = portLowerRight.x - viewSize.width;
+        if (overextension > 0)
+            portBounds.x -= overextension;
+      }
+
+    port.setViewPosition(portBounds.getLocation());
+    port.setViewSize(viewSize);
   }
 }
--- javax/swing/plaf/basic/BasicScrollBarUI.java	10 Jun 2004 10:32:39 -0000	1.1.2.7
+++ javax/swing/plaf/basic/BasicScrollBarUI.java	15 Jun 2004 22:21:40 -0000
@@ -791,7 +791,7 @@
    */
   public Dimension getPreferredSize(JComponent c)
   {
-    layoutContainer(scrollbar);
+    calculatePreferredSize();
     return preferredSize;
   }
 
--- javax/swing/plaf/basic/BasicViewportUI.java	21 May 2004 23:34:16 -0000	1.3.8.4
+++ javax/swing/plaf/basic/BasicViewportUI.java	15 Jun 2004 22:21:40 -0000
@@ -173,10 +173,18 @@
         g.clearRect(0, 0, portBounds.width, portBounds.height);
       }
 
+    g2.translate(-pos.x, -pos.y);
+    try 
+      {
     view.paint(g2);
+      }
+    finally
+      {
+        g2.translate(pos.x, pos.y);
+      }
     g2 = null;
     g.drawImage(backingStoreImage, 
-                -pos.x, -pos.y, 
+                0, 0, 
                 (ImageObserver)null);
   }
 }

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]