In System.Drawing:
authorMichael Hutchinson <mhutchinson@novell.com>
Wed, 24 Aug 2005 19:00:34 +0000 (19:00 -0000)
committerMichael Hutchinson <mhutchinson@novell.com>
Wed, 24 Aug 2005 19:00:34 +0000 (19:00 -0000)
2005-05-24 Michael Hutchinson <m.j.hutchinson@gmail.com>

* ToolboxBitmapAttribute.cs: Implemented retrieving the image

In System.Drawing.Design:
2005-08-24 Michael Hutchinson <m.j.hutchinson@gmail.com>

* ToolboxItem.cs: Implemented retrieving item's bitmap

svn path=/trunk/mcs/; revision=48806

mcs/class/System.Drawing/System.Drawing.Design/ChangeLog
mcs/class/System.Drawing/System.Drawing.Design/ToolboxItem.cs
mcs/class/System.Drawing/System.Drawing/ChangeLog
mcs/class/System.Drawing/System.Drawing/ToolboxBitmapAttribute.cs

index a6894dfb1d90ec9637008c81ed1c39d10e66608f..303f37a3e2b8105daab0af200b9eb20ba80e957b 100644 (file)
@@ -1,3 +1,7 @@
+2005-08-24 Michael Hutchinson <m.j.hutchinson@gmail.com>
+
+       * ToolboxItem.cs: Implemented retrieving item's bitmap
+
 2005-08-09  Michael Hutchinson <m.j.hutchinson@gmail.com>
 
        * ToolboxItemCollection.cs: Call base constructor
index 508afdab2812d41cfb6f512c25e82487f28d1cf5..f1547ce393ebc89b6a4a8c9ee5136435b8acbe34 100644 (file)
@@ -46,7 +46,7 @@ namespace System.Drawing.Design
        {
 
                private AssemblyName assembly;
-               private Bitmap bitmap;
+               private Bitmap bitmap = null;
                private ICollection filter = new ToolboxItemFilterAttribute[0];
                private string displayname = string.Empty;
                private bool locked = false;
@@ -203,17 +203,35 @@ namespace System.Drawing.Design
                        Assembly assembly = typeRes.GetAssembly(assemblyName, true);\r
                        if (reference)\r
                                typeRes.ReferenceAssembly(assemblyName);\r
-                       return assembly.GetType(typeName, true);
+                       return typeRes.GetType(typeName, true);
                }
 
-               [MonoTODO]
+               [MonoTODO ("Should we be returning empty bitmap, or null?")]
                public virtual void Initialize (Type type) 
                {
                        assembly = type.Assembly.GetName();
                        displayname = type.Name;
                        name = type.FullName;
+                       
                        // seems to be a right place to create the bitmap
-                       bitmap = new Bitmap (16, 16); // FIXME: load bitmap from resources if present, else set some default bitmap 
+                       System.Drawing.Image image = null;
+                       foreach (object attribute in type.GetCustomAttributes(true)) {
+                               ToolboxBitmapAttribute tba = attribute as ToolboxBitmapAttribute;
+                               if (tba != null) {
+                                       image = tba.GetImage (type);
+                                       break;
+                               }
+                       }
+                       //fallback: check for image even if not attribute
+                       if (image == null)
+                               image = ToolboxBitmapAttribute.GetImageFromResource (type, null, false);
+                       
+                       if (image != null) {
+                               if (image is Bitmap)
+                                       bitmap = (Bitmap) image;
+                               else
+                                       bitmap = new Bitmap (image);
+                       }
 
                        filter = type.GetCustomAttributes (typeof (ToolboxItemFilterAttribute), true);
                }
index f6c14b6ea7e5a3a8b50262131121d2d49caa450e..8fb22c35602562ffa5e2c45565edc63bf32583ce 100644 (file)
@@ -1,3 +1,7 @@
+2005-05-24 Michael Hutchinson <m.j.hutchinson@gmail.com>
+
+       * ToolboxBitmapAttribute.cs: Implemented retrieving the image
+
 2005-08-23 Vladimir Krasnov <vladimirk@mainsoft.com>
 
        * Graphics.jvm.cs: Fixed ResetTransform, TransfromPoints
index c500c946357d99df000f3364b2568bd777fa7428..eb800dc075738023406f44401dd905515ff956fc 100644 (file)
@@ -39,36 +39,27 @@ namespace System.Drawing
        [AttributeUsage (AttributeTargets.Class)]
        public class ToolboxBitmapAttribute : Attribute
        {
-               private Image smallImage;
-               private Image bigImage;
+               private Image smallImage = null;
+               private Image bigImage = null;
                public static readonly ToolboxBitmapAttribute Default = new ToolboxBitmapAttribute();
 
                private ToolboxBitmapAttribute ()
                {
                }
 
-               [MonoTODO ("implement")]
                public ToolboxBitmapAttribute (string imageFile)
                {
-                       //
-                       // TODO: Add constructor logic here
-                       //
+                       smallImage = new Bitmap (imageFile);
                }
                
-               [MonoTODO ("implement")]
                public ToolboxBitmapAttribute (Type t)
                {
-                       //
-                       // TODO: Add constructor logic here
-                       //
+                       smallImage = GetImageFromResource (t, null, false);
                }
                
-               [MonoTODO ("implement")]
                public ToolboxBitmapAttribute (Type t, string name)
                {
-                       //
-                       // TODO: Add constructor logic here
-                       //
+                       smallImage = GetImageFromResource (t, name, false);
                }
 
                public override bool Equals (object value)
@@ -105,16 +96,41 @@ namespace System.Drawing
                        return GetImage (type, null, large);
                }
 
-               [MonoTODO ("implement")]
                public Image GetImage (Type type, string imgName, bool large)
                {
-                       return null;
+                       if (smallImage == null)
+                                       smallImage = GetImageFromResource (type, imgName, false);
+                       
+                       if (large) {
+                               if (bigImage == null)
+                                       bigImage = new Bitmap (smallImage, 32, 32);
+                               return bigImage;
+                       }
+                       else
+                               return smallImage;
                }
 
-               [MonoTODO ("implement")]
                public static Image GetImageFromResource (Type t, string imageName, bool large)
                {
-                       return null;
+                       Bitmap bitmap;
+                       if (imageName == null)
+                               imageName = t.Name + ".bmp";
+                       
+                       using (System.IO.Stream s = t.Assembly.GetManifestResourceStream (t.Namespace + "." + imageName)){
+                               if (s == null) {
+                                       return null;
+                               }
+                               else {
+                                       bitmap = new Bitmap (s, false);
+                               }
+                       }
+                       
+                       //FIXME: thrown too easily
+                       //if (bitmap.Width != 16 || bitmap.Height != 16)
+                       //      throw new Exception ("ToolboxBitmap must be 16x16 pixels");
+                       
+                       if (large) return new Bitmap (bitmap, 32, 32);
+                       return bitmap;
                }
        }
 }