Fixes broken synthetic load event firing on IE.
Patch by: jgw
Review by: jlabanca (desk check)


git-svn-id: https://google-web-toolkit.googlecode.com/svn/releases/1.6@4584 8db76d5a-ed1c-0410-87a9-c151d255dfc7
diff --git a/user/src/com/google/gwt/user/client/ui/Image.java b/user/src/com/google/gwt/user/client/ui/Image.java
index b33e4fb..f6af018 100644
--- a/user/src/com/google/gwt/user/client/ui/Image.java
+++ b/user/src/com/google/gwt/user/client/ui/Image.java
@@ -19,7 +19,6 @@
 import com.google.gwt.dom.client.Document;
 import com.google.gwt.dom.client.Element;
 import com.google.gwt.dom.client.ImageElement;
-import com.google.gwt.dom.client.NativeEvent;
 import com.google.gwt.event.dom.client.ClickEvent;
 import com.google.gwt.event.dom.client.ClickHandler;
 import com.google.gwt.event.dom.client.ErrorEvent;
@@ -43,8 +42,6 @@
 import com.google.gwt.event.dom.client.MouseWheelEvent;
 import com.google.gwt.event.dom.client.MouseWheelHandler;
 import com.google.gwt.event.shared.HandlerRegistration;
-import com.google.gwt.user.client.Command;
-import com.google.gwt.user.client.DeferredCommand;
 import com.google.gwt.user.client.Event;
 import com.google.gwt.user.client.ui.impl.ClippedImageImpl;
 
@@ -161,7 +158,7 @@
         this.height = height;
 
         impl.adjust(image.getElement(), url, left, top, width, height);
-        fireSyntheticLoadEvent(image);
+        impl.fireSyntheticLoadEvent(image);
       }
     }
 
@@ -182,7 +179,7 @@
         this.height = height;
 
         impl.adjust(image.getElement(), url, left, top, width, height);
-        fireSyntheticLoadEvent(image);
+        impl.fireSyntheticLoadEvent(image);
       }
     }
 
@@ -191,28 +188,6 @@
     protected String getStateName() {
       return "clipped";
     }
-
-    private void fireSyntheticLoadEvent(final Image image) {
-      /*
-       * We need to synthesize a load event, because the native events that are
-       * fired would correspond to the loading of clear.cache.gif, which is
-       * incorrect. A native event would not even fire in Internet Explorer,
-       * because the root element is a wrapper element around the <img> element.
-       * Since we are synthesizing a load event, we do not need to sink the
-       * onload event.
-       * 
-       * We use a deferred command here to simulate the native version of the
-       * load event as closely as possible. In the native event case, it is
-       * unlikely that a second load event would occur while you are in the load
-       * event handler.
-       */
-      DeferredCommand.addCommand(new Command() {
-        public void execute() {
-          NativeEvent evt = Document.get().createLoadEvent();
-          image.getElement().dispatchEvent(evt);
-        }
-      });
-    }
   }
 
   /**
diff --git a/user/src/com/google/gwt/user/client/ui/impl/ClippedImageImpl.java b/user/src/com/google/gwt/user/client/ui/impl/ClippedImageImpl.java
index 5a835f3..4e78056 100644
--- a/user/src/com/google/gwt/user/client/ui/impl/ClippedImageImpl.java
+++ b/user/src/com/google/gwt/user/client/ui/impl/ClippedImageImpl.java
@@ -18,6 +18,10 @@
 import com.google.gwt.core.client.GWT;
 import com.google.gwt.dom.client.Document;
 import com.google.gwt.dom.client.Element;
+import com.google.gwt.dom.client.NativeEvent;
+import com.google.gwt.user.client.Command;
+import com.google.gwt.user.client.DeferredCommand;
+import com.google.gwt.user.client.ui.Image;
 
 /**
  * Uses a combination of a clear image and a background image to clip all except
@@ -54,4 +58,26 @@
 
     return clippedImgHtml;
   }
+
+  public void fireSyntheticLoadEvent(final Image image) {
+    /*
+     * We need to synthesize a load event, because the native events that are
+     * fired would correspond to the loading of clear.cache.gif, which is
+     * incorrect. A native event would not even fire in Internet Explorer,
+     * because the root element is a wrapper element around the <img> element.
+     * Since we are synthesizing a load event, we do not need to sink the
+     * onload event.
+     * 
+     * We use a deferred command here to simulate the native version of the
+     * load event as closely as possible. In the native event case, it is
+     * unlikely that a second load event would occur while you are in the load
+     * event handler.
+     */
+    DeferredCommand.addCommand(new Command() {
+      public void execute() {
+        NativeEvent evt = Document.get().createLoadEvent();
+        image.getElement().dispatchEvent(evt);
+      }
+    });
+  }
 }
diff --git a/user/src/com/google/gwt/user/client/ui/impl/ClippedImageImplIE6.java b/user/src/com/google/gwt/user/client/ui/impl/ClippedImageImplIE6.java
index 6c83175..3666ce8 100644
--- a/user/src/com/google/gwt/user/client/ui/impl/ClippedImageImplIE6.java
+++ b/user/src/com/google/gwt/user/client/ui/impl/ClippedImageImplIE6.java
@@ -16,7 +16,13 @@
 package com.google.gwt.user.client.ui.impl;
 
 import com.google.gwt.core.client.GWT;
+import com.google.gwt.dom.client.Document;
 import com.google.gwt.dom.client.Element;
+import com.google.gwt.dom.client.NativeEvent;
+import com.google.gwt.user.client.Command;
+import com.google.gwt.user.client.DeferredCommand;
+import com.google.gwt.user.client.Event;
+import com.google.gwt.user.client.ui.Image;
 
 /**
  * Implements the clipped image as a IMG inside a custom tag because we can't
@@ -65,6 +71,31 @@
   }
 
   @Override
+  public Element createStructure(String url, int left, int top, int width,
+      int height) {
+    // We need to explicitly sink ONLOAD on the child image element, because it
+    // can't be fired on the clipper.
+    Element clipper = super.createStructure(url, left, top, width, height);
+    Element img = clipper.getFirstChildElement();
+    Event.sinkEvents(img, Event.ONLOAD);
+    return clipper;
+  }
+
+  public void fireSyntheticLoadEvent(final Image image) {
+    // This is the same as the superclass' implementation, except that it
+    // explicitly checks for the 'clipper' element, and dispatches the event
+    // on the img (you can't dispatch events on the clipper).
+    DeferredCommand.addCommand(new Command() {
+      public void execute() {
+        NativeEvent evt = Document.get().createLoadEvent();
+        Element clipper = image.getElement();
+        Element img = clipper.getFirstChildElement();
+        img.dispatchEvent(evt);
+      }
+    });
+  }
+
+  @Override
   public String getHTML(String url, int left, int top, int width, int height) {
     String clipperStyle = "overflow: hidden; width: " + width + "px; height: "
         + height + "px; padding: 0px; zoom: 1";