Merge branch 'master' of github.com:mono/mono into masterwork
[mono.git] / mcs / class / System.Drawing / System.Drawing / ToolboxBitmapAttribute.cs
index e28d1ac62661992c4faf83468a909d2b92f7f6d3..9737b48e1dcceb1825189a0dc9ef489eee389ac3 100644 (file)
@@ -5,7 +5,31 @@
 //   Dennis Hayes (dennish@Raytek.com)
 //   Andreas Nahr (ClassDevelopment@A-SoftTech.com)
 //
-// (C) 2002 Ximian, Inc
+// Copyright (C) 2002 Ximian, Inc. http://www.ximian.com
+// Copyright (C) 2004 Novell, Inc. http://www.novell.com
+//
+
+//
+// Copyright (C) 2004 Novell, Inc (http://www.novell.com)
+//
+// Permission is hereby granted, free of charge, to any person obtaining
+// a copy of this software and associated documentation files (the
+// "Software"), to deal in the Software without restriction, including
+// without limitation the rights to use, copy, modify, merge, publish,
+// distribute, sublicense, and/or sell copies of the Software, and to
+// permit persons to whom the Software is furnished to do so, subject to
+// the following conditions:
+// 
+// The above copyright notice and this permission notice shall be
+// included in all copies or substantial portions of the Software.
+// 
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 //
 
 using System;
@@ -15,36 +39,26 @@ 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
-                       //
                }
                
-               [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)
@@ -81,16 +95,45 @@ 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";
+
+                       try {
+                               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;
+                       } catch {
+                               return null;
+                       }
                }
        }
 }