{
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;
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);
}
[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)
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;
}
}
}