Author: kornelpal
Date: 2005-09-28 05:34:32 -0400 (Wed, 28 Sep 2005)
New Revision: 50935

Modified:
   trunk/mcs/class/Managed.Windows.Forms/System.Windows.Forms/ChangeLog
   trunk/mcs/class/Managed.Windows.Forms/System.Windows.Forms/ImageList.cs
   
trunk/mcs/class/Managed.Windows.Forms/System.Windows.Forms/ImageListStreamer.cs
Log:
ImageList.cs: Added comments about the current implementation. Added 
ReduceColorDepth, IndexedColorDepths and GetNearestColor to can use 
Format32bppArgb to preserve transparency and can use Graphics.FromImage while 
using the specified ColorDepth. ReduceColorDepth uses unsafe code with 
Bitmap.LockBits for better performance. Revised the whole file to match MS.NET 
behaviour and provide better performance. Non-public interface members are 
calling public members even when they throw NotSupportedException for better 
maintainability. Moved ColorDepth, ImageSize, ImageStream and TransparentColor 
implementations to ImageCollection for better performance as these properties 
are not used by ImageList.; ImageListStreamer.cs: Added a new internal 
constructor that takes an ImageList.ImageCollection and serializes Images based 
on ImageCollection.ToArray(). Renamed ImageColorDepth to ColorDepth to match 
ImageList property name.

Modified: trunk/mcs/class/Managed.Windows.Forms/System.Windows.Forms/ChangeLog
===================================================================
--- trunk/mcs/class/Managed.Windows.Forms/System.Windows.Forms/ChangeLog        
2005-09-28 08:46:05 UTC (rev 50934)
+++ trunk/mcs/class/Managed.Windows.Forms/System.Windows.Forms/ChangeLog        
2005-09-28 09:34:32 UTC (rev 50935)
@@ -1,3 +1,21 @@
+2005-09-28  Kornél Pál  <[EMAIL PROTECTED]>
+
+       * ImageList.cs: Added comments about the current implementation. Added
+         ReduceColorDepth, IndexedColorDepths and GetNearestColor to can use
+         Format32bppArgb to preserve transparency and can use 
Graphics.FromImage
+         while using the specified ColorDepth. ReduceColorDepth uses unsafe 
code
+         with Bitmap.LockBits for better performance. Revised the whole file to
+         match MS.NET behaviour and provide better performance. Non-public
+         interface members are calling public members even when they throw
+         NotSupportedException for better maintainability. Moved ColorDepth,
+         ImageSize, ImageStream and TransparentColor implementations to
+         ImageCollection for better performance as these properties are not 
used
+         by ImageList.
+       * ImageListStreamer.cs: Added a new internal constructor that takes an
+         ImageList.ImageCollection and serializes Images based on
+         ImageCollection.ToArray(). Renamed ImageColorDepth to ColorDepth to
+         match ImageList property name.
+
 2005-09-28  Kazuki Oikawa <[EMAIL PROTECTED]>
 
        * ListBox.cs: Fixes IndexFromPoint for last item

Modified: 
trunk/mcs/class/Managed.Windows.Forms/System.Windows.Forms/ImageList.cs
===================================================================
--- trunk/mcs/class/Managed.Windows.Forms/System.Windows.Forms/ImageList.cs     
2005-09-28 08:46:05 UTC (rev 50934)
+++ trunk/mcs/class/Managed.Windows.Forms/System.Windows.Forms/ImageList.cs     
2005-09-28 09:34:32 UTC (rev 50935)
@@ -1,3 +1,15 @@
+//
+// System.Windows.Forms.ImageList.cs
+//
+// Authors:
+//   Peter Bartok <[EMAIL PROTECTED]>
+//   Kornél Pál <http://www.kornelpal.hu/>
+//
+// Copyright (C) 2004-2005 Novell, Inc.
+// Copyright (C) 2005 Kornél Pál
+//
+
+//
 // 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
@@ -5,10 +17,10 @@
 // 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
@@ -17,56 +29,245 @@
 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 //
-// Copyright (c) 2004-2005 Novell, Inc.
+
+// COMPLETE
+
 //
-// Authors:
-//     Peter Bartok    [EMAIL PROTECTED]
+// Differences between MS.NET ImageList and this implementation:
 //
+// This is a fully managed image list implementation.
+//
+// Images are stored as Format32bppArgb internally but ColorDepth is applied
+// to the colors of images. Images[index] returns a Format32bppArgb copy of
+// the image so this difference is only internal.
+//
+// MS.NET has no alpha channel support (except for icons in 32-bit mode with
+// comctl32.dll version 6.0) but this implementation has full alpha channel
+// support in 32-bit mode.
+//
+// Handle should be an HIMAGELIST returned by ImageList_Create. This
+// implementation uses (IntPtr)(-1) that is a non-zero but invalid handle.
+//
+// MS.NET 1.x shares the same HIMAGELIST between ImageLists that were
+// initialized from the same ImageListStreamer and doesn't update ImageSize
+// and ColorDepth that are treated as bugs.
+//
 
-// If we ever need to support the Handle property, we should create a static 
ArrayList,
-// and store the ImageList object in it, and return the index as handle
-// That way, once we try to support P/Invokes, we can make the handle back to 
the object
-
-
-// COMPLETE
-
 using System.Collections;
 using System.ComponentModel;
 using System.Drawing;
 using System.Drawing.Imaging;
+using System.Runtime.InteropServices;
 
-namespace System.Windows.Forms {
+namespace System.Windows.Forms
+{
        [DefaultProperty("Images")]
        [Designer("System.Windows.Forms.Design.ImageListDesigner, " + 
Consts.AssemblySystem_Design, "System.ComponentModel.Design.IDesigner")]
        [ToolboxItemFilter("System.Windows.Forms", ToolboxItemFilterType.Allow)]
        [TypeConverter("System.Windows.Forms.ImageListConverter, " + 
Consts.AssemblySystem_Windows_Forms)]
-       public sealed class ImageList : System.ComponentModel.Component {
-               #region Local Variables
-               private ColorDepth              color_depth;
-               private ImageCollection         image_collection;
-               private Size                    size;
-               private Color                   transparency_color;
-               private Delegate                handler;
-               private ImageListStreamer       image_stream;
-               private IntPtr                  handle; 
-               #endregion      // Local Variables
+       public sealed class ImageList : System.ComponentModel.Component
+       {
+               #region Private Fields
+               private EventHandler recreateHandle;
+               private readonly ImageCollection images;
+               #endregion // Private Fields
 
                #region Sub-classes
                [Editor("System.Windows.Forms.Design.ImageCollectionEditor, " + 
Consts.AssemblySystem_Design, typeof(System.Drawing.Design.UITypeEditor))]
-               public sealed class ImageCollection : IList, ICollection, 
IEnumerable {
-                       #region ImageCollection Local Variables
-                       private ImageList       owner;
-                       private ArrayList       list;
-                       #endregion      // ImageCollection Local Variables
+               public sealed class ImageCollection : IList, ICollection, 
IEnumerable
+               {
+                       private const int AlphaMask = 
unchecked((int)0xFF000000);
 
-                       #region ImageCollection Private Constructors
-                       internal ImageCollection(ImageList owner) {
-                               this.owner=owner;
-                               this.list=new ArrayList();
+                       [StructLayout(LayoutKind.Explicit)]
+                       private struct ArgbColor
+                       {
+                               [FieldOffset(0)]
+                               internal int argb;
+                               [FieldOffset(0)]
+                               internal byte blue;
+                               [FieldOffset(1)]
+                               internal byte green;
+                               [FieldOffset(2)]
+                               internal byte red;
+                               [FieldOffset(3)]
+                               internal byte alpha;
                        }
-                       #endregion      // ImageCollection Private Constructor
 
-                       #region ImageCollection Public Instance Properties
+                       private
+#if NET_2_0
+                       static
+#else
+                       sealed
+#endif
+                       class IndexedColorDepths
+                       {
+#if !NET_2_0
+                               private IndexedColorDepths()
+                               {
+                               }
+#endif
+                               internal static readonly ArgbColor[] 
Palette4Bit;
+                               internal static readonly ArgbColor[] 
Palette8Bit;
+                               private static readonly int[] squares;
+
+                               static IndexedColorDepths()
+                               {
+                                       Color[] palette;
+                                       Bitmap bitmap;
+                                       int index;
+                                       int count;
+
+                                       bitmap = new Bitmap(1, 1, 
PixelFormat.Format4bppIndexed);
+                                       palette = bitmap.Palette.Entries;
+                                       bitmap.Dispose();
+
+                                       Palette4Bit = new ArgbColor[count = 
palette.Length];
+                                       for (index = 0; index < count; index++)
+                                               Palette4Bit[index].argb = 
palette[index].ToArgb();
+
+                                       bitmap = new Bitmap(1, 1, 
PixelFormat.Format8bppIndexed);
+                                       palette = bitmap.Palette.Entries;
+                                       bitmap.Dispose();
+
+                                       Palette8Bit = new ArgbColor[count = 
palette.Length];
+                                       for (index = 0; index < count; index++)
+                                               Palette8Bit[index].argb = 
palette[index].ToArgb();
+
+                                       squares = new int[511];
+                                       for (index = 0; index < 256; index++)
+                                               squares[255 + index] = 
squares[255 - index] = index * index;
+                               }
+
+                               internal static int GetNearestColor(ArgbColor[] 
palette, int color)
+                               {
+                                       int index;
+                                       int count;
+                                       int red;
+                                       int green;
+                                       int blue;
+                                       int nearestColor;
+                                       int minDistance;
+                                       int distance;
+
+                                       count = palette.Length;
+                                       for (index = 0; index < count; index++)
+                                               if (palette[index].argb == 
color)
+                                                       return color;
+
+                                       red = 
unchecked((int)(unchecked((uint)color) >> 16) & 0xFF);
+                                       green = 
unchecked((int)(unchecked((uint)color) >> 8) & 0xFF);
+                                       blue = color & 0xFF;
+                                       nearestColor = AlphaMask;
+                                       minDistance = int.MaxValue;
+
+                                       for (index = 0; index < count; index++)
+                                               if ((distance = squares[255 + 
palette[index].red - red] + squares[255 + palette[index].green - green] + 
squares[255 + palette[index].blue - blue]) < minDistance) {
+                                                       nearestColor = 
palette[index].argb;
+                                                       minDistance = distance;
+                                               }
+
+                                       return nearestColor;
+                               }
+
+                       }
+
+                       #region ImageCollection Private Fields
+                       private ColorDepth colorDepth = ColorDepth.Depth8Bit;
+                       private Color transparentColor = Color.Transparent;
+                       private Size imageSize = new Size(16, 16);
+                       private readonly ImageList owner;
+                       private readonly ArrayList list;
+                       #endregion // ImageCollection Private Fields
+
+                       #region ImageCollection Internal Constructors
+                       // For use in ImageList
+                       internal ImageCollection(ImageList owner)
+                       {
+                               this.owner = owner;
+                               this.list = new ArrayList();
+                       }
+                       #endregion // ImageCollection Internal Constructor
+
+                       #region ImageCollection Internal Instance Properties
+                       // For use in ImageList
+                       internal ColorDepth ColorDepth {
+                               get {
+                                       return this.colorDepth;
+                               }
+
+                               set {
+                                       if (!Enum.IsDefined(typeof(ColorDepth), 
value))
+                                               throw new 
InvalidEnumArgumentException("value", (int)value, typeof(ColorDepth));
+
+                                       if (this.colorDepth != value) {
+                                               this.colorDepth = value;
+                                               list.Clear();
+                                               owner.RaiseRecreateHandle();
+                                       }
+                               }
+                       }
+
+                       // For use in ImageList
+                       internal Size ImageSize {
+                               get {
+                                       return this.imageSize;
+                               }
+
+                               set {
+                                       if (value.Width < 1 || value.Width > 
256 || value.Height < 1 || value.Height > 256)
+                                               throw new 
ArgumentException("ImageSize.Width and Height must be between 1 and 256", 
"value");
+
+                                       if (this.imageSize != value) {
+                                               this.imageSize = value;
+                                               list.Clear();
+                                               owner.RaiseRecreateHandle();
+                                       }
+                               }
+                       }
+
+                       // For use in ImageList
+                       internal ImageListStreamer ImageStream {
+                               get {
+                                       return list.Count == 0 ? null : new 
ImageListStreamer(this);
+                               }
+
+                               set {
+                                       int index;
+                                       Image[] streamImages;
+
+                                       if (value == null) {
+#if NET_2_0
+                                               list.Clear();
+#endif
+                                       }
+                                       else if ((streamImages = value.Images) 
!= null) {
+                                               list.Clear();
+
+                                               for (index = 0; index < 
streamImages.Length; index++)
+                                                       
list.Add((Image)streamImages[index].Clone());
+
+                                               this.imageSize = 
value.ImageSize;
+                                               this.colorDepth = 
value.ColorDepth;
+#if NET_2_0
+                                               owner.RaiseRecreateHandle();
+#endif
+                                       }
+                               }
+                       }
+
+                       // For use in ImageList
+                       internal Color TransparentColor {
+                               get {
+                                       return this.transparentColor;
+                               }
+
+                               set {
+                                       this.transparentColor = value;
+                               }
+                       }
+                       #endregion // ImageCollection Internal Instance 
Properties
+
+                       #region ImageCollection Public Instance Properties
                        [Browsable(false)]
                        public int Count {
                                get {
@@ -76,13 +277,13 @@
 
                        public bool Empty {
                                get {
-                                       return list.Count==0;
+                                       return list.Count == 0;
                                }
                        }
 
                        public bool IsReadOnly {
                                get {
-                                       return list.IsReadOnly;
+                                       return false;
                                }
                        }
 
@@ -90,175 +291,242 @@
                        
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
                        public Image this[int index] {
                                get {
-                                       if (index<0 || index>=list.Count) {
-                                               throw new 
ArgumentOutOfRangeException("index", index, "ImageCollection does not have that 
many images");
-                                       }
-                                       return (Image)list[index];
+                                       return (Image)GetImage(index).Clone();
                                }
 
                                set {
-                                       if (index<0 || index>=list.Count) {
-                                               throw new 
ArgumentOutOfRangeException("index", index, "ImageCollection does not have that 
many images");
-                                       }
+                                       if (index < 0 || index >= list.Count)
+                                               throw new 
ArgumentOutOfRangeException("index");
 
-                                       if (value==null) {
-                                               throw new 
ArgumentOutOfRangeException("value", value, "Image cannot be null");
-                                       }
-
-                                       list[index]=value;
-                                       // What happens if the bitmap had a 
previous 'MakeTransparent' done to it?
-                                       
((Bitmap)list[index]).MakeTransparent(owner.transparency_color);
+                                       list[index] = CreateImage(value, 
this.transparentColor);
                                }
                        }
-                       #endregion      // ImageCollection Public Instance 
Properties
+                       #endregion // ImageCollection Public Instance Properties
 
-                       #region ImageCollection Private Instance Methods
-                       private int AddInternal(Image image) {
-                               int             width;
-                               int             height;
+                       #region ImageCollection Private Instance Methods
+                       private Image CreateImage(Image value, Color 
transparentColor)
+                       {
+                               int width;
+                               int height;
+                               Bitmap bitmap;
+                               Graphics graphics;
+                               ImageAttributes imageAttributes;
 
-                               width=owner.ImageSize.Width;
-                               height=owner.ImageSize.Height;
+                               if (value == null)
+                                       throw new 
ArgumentNullException("value");
 
-                               // Check if we can add straight or if we have 
to resize
-                               if (image.Width!=width || image.Height!=height) 
{
-                                       Graphics        g;
-                                       Bitmap          reformatted_image;
+                               if (!(value is Bitmap))
+                                       throw new ArgumentException("Image must 
be a Bitmap.");
 
-                                       reformatted_image = new Bitmap(width, 
height);
-                                       g=Graphics.FromImage(reformatted_image);
+                               if (transparentColor.A == 0)
+                                       imageAttributes = null;
+                               else {
+                                       imageAttributes = new ImageAttributes();
+                                       
imageAttributes.SetColorKey(transparentColor, transparentColor);
+                               }
 
-                                       g.DrawImage (image, new Rectangle (0, 
0, width, height),
-                                                       0, 0, image.Width, 
image.Height,
-                                                       GraphicsUnit.Pixel);
-                                       g.Dispose();
+                               bitmap = new Bitmap((width = 
this.imageSize.Width), (height = this.imageSize.Height), 
PixelFormat.Format32bppArgb);
+                               graphics = Graphics.FromImage(bitmap);
+                               graphics.DrawImage(value, new Rectangle(0, 0, 
width, height), 0, 0, value.Width, value.Height, GraphicsUnit.Pixel, 
imageAttributes);
+                               graphics.Dispose();
 
-                                       return list.Add(reformatted_image);
-                               } else {
-                                       return list.Add(image);
-                               }
+                               return ReduceColorDepth(bitmap);
                        }
 
-                       internal void Dispose() {
-#if dontwantthis
-                               if (list!=null) {
-                                       for (int i=0; i<list.Count; i++) {
-                                               ((Image)list[i]).Dispose();
+                       private unsafe Image ReduceColorDepth(Bitmap bitmap)
+                       {
+                               int y;
+                               int x;
+                               int origPixel;
+                               int pixel;
+                               int height;
+                               int widthBytes;
+                               int stride;
+                               byte* pixels;
+                               BitmapData bitmapData;
+                               ArgbColor[] palette;
+
+                               if (this.colorDepth < ColorDepth.Depth32Bit) {
+                                       bitmapData = bitmap.LockBits(new 
Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.ReadWrite, 
PixelFormat.Format32bppArgb);
+                                       try {
+                                               pixels = 
(byte*)bitmapData.Scan0;
+                                               height = bitmapData.Height;
+                                               widthBytes = bitmapData.Width 
<< 2;
+                                               stride = bitmapData.Stride;
+
+                                               if (this.colorDepth < 
ColorDepth.Depth16Bit) {
+                                                       palette = 
(this.colorDepth < ColorDepth.Depth8Bit) ? IndexedColorDepths.Palette4Bit : 
IndexedColorDepths.Palette8Bit;
+
+                                                       for (y = 0; y < height; 
y++) {
+                                                               for (x = 0; x < 
widthBytes; x += 4)
+                                                                       if 
((pixel = (((origPixel = *(int*)(pixels + x)) & AlphaMask) == 0) ? 0x00000000 : 
IndexedColorDepths.GetNearestColor(palette, origPixel | AlphaMask)) != 
origPixel)
+                                                                               
*(int*)(pixels + x) = pixel;
+
+                                                               pixels += 
stride;
+                                                       }
+                                               }
+                                               else if (this.colorDepth < 
ColorDepth.Depth24Bit) {
+                                                       for (y = 0; y < height; 
y++) {
+                                                               for (x = 0; x < 
widthBytes; x += 4)
+                                                                       if 
((pixel = (((origPixel = *(int*)(pixels + x)) & AlphaMask) == 0) ? 0x00000000 : 
(origPixel & 0x00F8F8F8) | AlphaMask) != origPixel)
+                                                                               
*(int*)(pixels + x) = pixel;
+
+                                                               pixels += 
stride;
+                                                       }
+                                               }
+                                               else {
+                                                       for (y = 0; y < height; 
y++) {
+                                                               for (x = 0; x < 
widthBytes; x += 4)
+                                                                       if 
((pixel = (((origPixel = *(int*)(pixels + x)) & AlphaMask) == 0) ? 0x00000000 : 
origPixel | AlphaMask) != origPixel)
+                                                                               
*(int*)(pixels + x) = pixel;
+
+                                                               pixels += 
stride;
+                                                       }
+                                               }
                                        }
+                                       finally {
+                                               bitmap.UnlockBits(bitmapData);
+                                       }
                                }
-#endif
+
+                               return bitmap;
                        }
-                       #endregion      // ImageCollection Private Instance 
Methods
+                       #endregion // ImageCollection Private Instance Methods
 
-                       #region ImageCollection Public Instance Methods
-                       public int Add(Image value, Color transparentColor) {
-                               if (value==null) {
-                                       throw new 
ArgumentNullException("value", "Cannot add null image");
-                               }
+                       #region ImageCollection Internal Instance Methods
+                       // For use in ImageList
+                       internal Image GetImage(int index)
+                       {
+                               if (index < 0 || index >= list.Count)
+                                       throw new 
ArgumentOutOfRangeException("index");
 
-                               
((Bitmap)value).MakeTransparent(owner.transparency_color);
-                               return AddInternal(value);
-                               
+                               return (Image)list[index];
                        }
 
-                       public void Add(Icon value) {
-                               Image image;
+                       // For use in ImageListStreamer
+                       internal Image[] ToArray()
+                       {
+                               Image[] images = new Image[list.Count];
 
-                               image = value.ToBitmap();
+                               list.CopyTo(images);
+                               return images;
+                       }
+                       #endregion // ImageCollection Internal Instance Methods
 
-                               if (value==null || image==null) {
-                                       throw new 
ArgumentNullException("value", "Cannot add null icon");
-                               }
+                       #region ImageCollection Public Instance Methods
+                       public void Add(Icon value)
+                       {
+                               if (value == null)
+                                       throw new 
ArgumentNullException("value");
 
-                               
((Bitmap)image).MakeTransparent(owner.transparency_color);
-                               AddInternal(image);
+                               list.Add(ReduceColorDepth(value.ToBitmap()));
                        }
 
-                       public void Add(Image value) {
-                               if (value==null) {
-                                       throw new 
ArgumentNullException("value", "Cannot add null image");
-                               }
-                               
((Bitmap)value).MakeTransparent(owner.transparency_color);
-                               AddInternal(value);
+                       public void Add(Image value)
+                       {
+                               Add(value, this.transparentColor);
                        }
 
-                       public int AddStrip(Image value) {
-                               int             image_count;
-                               int             width;
-                               int             height;
-                               Bitmap          image;
-                               Graphics        g;
+                       public int Add(Image value, Color transparentColor)
+                       {
+                               return list.Add(CreateImage(value, 
transparentColor));
+                       }
 
-                               if (value==null) {
-                                       throw new 
ArgumentNullException("value", "Cannot add null images");
-                               }
+                       public int AddStrip(Image value)
+                       {
+                               int imageX;
+                               int imageWidth;
+                               int width;
+                               int height;
+                               int index;
+                               Bitmap bitmap;
+                               Graphics graphics;
+                               Rectangle imageRect;
+                               ImageAttributes imageAttributes;
 
-                               if ((value.Width % owner.ImageSize.Width) != 0) 
{
-                                       throw new ArgumentException("Strip is 
not a multiple of the ImageList with", "value");
-                               }
+                               if (value == null)
+                                       throw new 
ArgumentNullException("value");
 
-                               // MSDN: The number of images is inferred from 
the width. A strip is multiple images side-by-side
-                               width=owner.ImageSize.Width;
-                               height=owner.ImageSize.Height;
-                               image_count=value.Width/width;
-                               for (int i=0; i<image_count; i++) {
-                                       image = new Bitmap(width, height);
-                                       g = Graphics.FromImage(image);
+                               if (value.Width == 0 || (value.Width % (width = 
this.imageSize.Width)) != 0)
+                                       throw new ArgumentException("Width of 
image strip must be a positive multiple of ImageSize.Width.", "value");
 
-                                       g.DrawImage(value, new Rectangle(0, 0, 
width, height), i*width, 0, width, height, GraphicsUnit.Pixel);
-                                       AddInternal(image);
+                               if (value.Height != (height = 
this.imageSize.Height))
+                                       throw new ArgumentException("Height of 
image strip must be equal to ImageSize.Height.", "value");
 
-                                       g.Dispose();
+                               imageWidth = value.Width;
+                               imageRect = new Rectangle(0, 0, width, height);
+                               if (this.transparentColor.A == 0)
+                                       imageAttributes = null;
+                               else {
+                                       imageAttributes = new ImageAttributes();
+                                       
imageAttributes.SetColorKey(this.transparentColor, this.transparentColor);
                                }
 
-                               // FIXME - is this right? MSDN says to return 
the index, but we might have multiple...
-                               return image_count;
+                               index = list.Count;
+                               for (imageX = 0; imageX < imageWidth; imageX += 
width) {
+                                       bitmap = new Bitmap(width, height, 
PixelFormat.Format32bppArgb);
+                                       graphics = Graphics.FromImage(bitmap);
+                                       graphics.DrawImage(value, imageRect, 
imageX, 0, width, height, GraphicsUnit.Pixel, imageAttributes);
+                                       graphics.Dispose();
+
+                                       list.Add(ReduceColorDepth(bitmap));
+                               }
+
+                               return index;
                        }
 
-                       public void Clear() {
+                       public void Clear()
+                       {
                                list.Clear();
                        }
 
-                       public bool Contains(Image image) {
-                               return list.Contains(image);
+                       public bool Contains(Image image)
+                       {
+                               throw new NotSupportedException();
                        }
 
-                       public IEnumerator GetEnumerator() {
-                               return list.GetEnumerator();
+
+                       public IEnumerator GetEnumerator()
+                       {
+                               Image[] images = new Image[list.Count];
+                               int index;
+
+                               for (index = 0; index < images.Length; index++)
+                                       images[index] = 
(Image)((Image)list[index]).Clone();
+
+                               return images.GetEnumerator();
                        }
 
-                       public int IndexOf(Image image) {
-                               return list.IndexOf(image);
+                       public int IndexOf(Image image)
+                       {
+                               throw new NotSupportedException();
                        }
 
-                       public void Remove(Image image) {
-                               list.Remove(image);
+                       public void Remove(Image image)
+                       {
+                               throw new NotSupportedException();
                        }
 
-                       public void RemoveAt(int index) {
-                               if (index<0 || index>=list.Count) {
-                                       throw new 
ArgumentOutOfRangeException("index", index, "ImageCollection does not have that 
many images");
-                               }
+                       public void RemoveAt(int index)
+                       {
+                               if (index < 0 || index >= list.Count)
+                                       throw new 
ArgumentOutOfRangeException("index");
 
                                list.RemoveAt(index);
                        }
-                       #endregion      // ImageCollection Public Instance 
Methods
+                       #endregion // ImageCollection Public Instance Methods
 
-                       #region ImageCollection Interface Properties
+                       #region ImageCollection Interface Properties
                        object IList.this[int index] {
                                get {
-                                       if (index<0 || index>=list.Count) {
-                                               throw new 
ArgumentOutOfRangeException("index", index, "ImageCollection does not have that 
many images");
-                                       }
                                        return this[index];
                                }
 
                                set {
-                                       if (!(value is Bitmap)) {
-                                               throw new 
ArgumentException("Object of type Image required", "value");
-                                       }
+                                       if (!(value is Image))
+                                               throw new 
ArgumentException("value");
 
-                                       this[index]=(Image)value;
+                                       this[index] = (Image)value;
                                }
                        }
 
@@ -268,104 +536,85 @@
                                }
                        }
 
-                       bool IList.IsReadOnly {
-                               get {
-                                       return list.IsReadOnly;
-                               }
-                       }
-
                        bool ICollection.IsSynchronized {
                                get {
-                                       return list.IsSynchronized;
+                                       return false;
                                }
                        }
 
                        object ICollection.SyncRoot {
                                get {
-                                       return list.SyncRoot;
+                                       return this;
                                }
                        }
-                       #endregion      // ImageCollection Interface Properties
+                       #endregion // ImageCollection Interface Properties
 
-                       #region ImageCollection Interface Methods
-                       int IList.Add(object value) {
-                               if (value == null) {
-                                       throw new 
ArgumentNullException("value", "Cannot add null images");
-                               }
+                       #region ImageCollection Interface Methods
+                       int IList.Add(object value)
+                       {
+                               int index;
 
-                               if (!(value is Bitmap)) {
-                                       throw new ArgumentException("Object of 
type Image required", "value");
-                               }
+                               if (!(value is Image))
+                                       throw new ArgumentException("value");
 
-                               return list.Add(value);
+                               index = this.Count;
+                               this.Add((Image)value);
+                               return index;
                        }
 
-                       bool IList.Contains(object value) {
-                               if (!(value is Bitmap)) {
-                                       throw new ArgumentException("Object of 
type Image required", "value");
-                               }
-
-                               return this.Contains((Image) value);
+                       bool IList.Contains(object value)
+                       {
+                               return (value is Image) ? 
this.Contains((Image)value) : false;
                        }
 
-                       int IList.IndexOf(object value) {
-                               if (!(value is Bitmap)) {
-                                       throw new ArgumentException("Object of 
type Image required", "value");
-                               }
-
-                               return this.IndexOf((Image) value);
+                       int IList.IndexOf(object value)
+                       {
+                               return (value is Image) ? 
this.IndexOf((Image)value) : -1;
                        }
 
-                       void IList.Insert(int index, object value) {
-                               if (!(value is Bitmap)) {
-                                       throw new ArgumentException("Object of 
type Image required", "value");
-                               }
-                               list.Insert(index, value);
+                       void IList.Insert(int index, object value)
+                       {
+                               throw new NotSupportedException();
                        }
 
-                       void IList.Remove(object value) {
-                               if (!(value is Bitmap)) {
-                                       throw new ArgumentException("Object of 
type Image required", "value");
-                               }
-                               list.Remove(value);
+                       void IList.Remove(object value)
+                       {
+                               if (value is Image)
+                                       this.Remove((Image)value);
                        }
 
-                       void ICollection.CopyTo(Array array, int index) {
-                               if (list.Count>0) {
-                                       list.CopyTo(array, index);
-                               }
+                       void ICollection.CopyTo(Array array, int index)
+                       {
+                               int imageIndex;
+
+                               for (imageIndex = 0; imageIndex < this.Count; 
imageIndex++)
+                                       array.SetValue(this[index], index++);
                        }
-                       #endregion      // ImageCollection Interface Methods
+                       #endregion // ImageCollection Interface Methods
                }
-               #endregion      // Sub-classes
+               #endregion // Sub-classes
 
                #region Public Constructors
-               public ImageList() {
-                       color_depth = ColorDepth.Depth8Bit;
-                       transparency_color = Color.Transparent;
-                       size = new Size(16, 16);
-                       image_collection = new ImageCollection(this);
-                       handle = IntPtr.Zero;
+               public ImageList()
+               {
+                       images = new ImageCollection(this);
                }
 
-               public ImageList(System.ComponentModel.IContainer container) : 
this ()
+               public ImageList(System.ComponentModel.IContainer container) : 
this()
                {
-                       color_depth = ColorDepth.Depth8Bit;
-                       transparency_color = Color.Transparent;
-                       size = new Size(16, 16);
-                       container.Add (this);
+                       container.Add(this);
                }
-               #endregion      // Public Constructors
+               #endregion // Public Constructors
 
                #region Public Instance Properties
                [DefaultValue(ColorDepth.Depth8Bit)]
                public ColorDepth ColorDepth {
                        get {
-                               return this.color_depth;
+                               return images.ColorDepth;
                        }
 
                        set {
-                               this.color_depth=value;
+                               images.ColorDepth = value;
                        }
                }
 
@@ -374,9 +623,7 @@
                
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
                public IntPtr Handle {
                        get {
-                               if (RecreateHandle!=null) RecreateHandle(this, 
EventArgs.Empty);
-                               handle = new IntPtr(1);
-                               return handle;
+                               return (IntPtr)(-1);
                        }
                }
 
@@ -385,10 +632,7 @@
                
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
                public bool HandleCreated {
                        get {
-                               if (handle != IntPtr.Zero) {
-                                       return true;
-                               }
-                               return false;
+                               return true;
                        }
                }
 
@@ -397,21 +641,18 @@
                
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
                public ImageCollection Images {
                        get {
-                               return this.image_collection;
+                               return this.images;
                        }
                }
 
                [Localizable(true)]
                public Size ImageSize {
                        get {
-                               return this.size;
+                               return images.ImageSize;
                        }
 
                        set {
-                               if (value.Width<1 || value.Width>256 || 
value.Height<1 || value.Height>256) {
-                                       throw new ArgumentException("ImageSize 
width and height must be between 1 and 255", "value");
-                               }
-                               this.size=value;
+                               images.ImageSize = value;
                        }
                }
 
@@ -420,71 +661,74 @@
                [EditorBrowsable(EditorBrowsableState.Advanced)]
                public ImageListStreamer ImageStream {
                        get {
-                               return image_stream;
+                               return images.ImageStream;
                        }
 
                        set {
-                               image_stream = value;
-
-                               size = image_stream.ImageSize;
-                               color_depth = image_stream.ImageColorDepth;
-                               transparency_color = image_stream.BackColor;
-
-                               image_collection.Clear ();
-
-                               foreach (Image image in image_stream.Images)
-                                       image_collection.Add (image);
+                               images.ImageStream = value;
                        }
                }
 
                public Color TransparentColor {
                        get {
-                               return this.transparency_color;
+                               return images.TransparentColor;
                        }
 
                        set {
-                               this.transparency_color=value;
+                               images.TransparentColor = value;
                        }
                }
-               #endregion      // Public Instance Properties
+               #endregion // Public Instance Properties
 
+               #region Private Instance Methods
+               private void RaiseRecreateHandle()
+               {
+                       if (recreateHandle != null)
+                               recreateHandle(this, EventArgs.Empty);
+               }
+               #endregion // Private Instance Methods
+
                #region Public Instance Methods
-               public void Draw(Graphics g, Point pt, int index) {
+               public void Draw(Graphics g, Point pt, int index)
+               {
                        this.Draw(g, pt.X, pt.Y, index);
                }
 
-               public void Draw(Graphics g, int x, int y, int index) {
-                       this.Draw(g, x, y, this.size.Width, this.size.Height, 
index);
+               public void Draw(Graphics g, int x, int y, int index)
+               {
+                       g.DrawImage(images.GetImage(index), x, y);
                }
 
-               public void Draw(Graphics g, int x, int y, int width, int 
height, int index) {
-                       Image   i;
-
-                       if ((index < 0) || (index >= this.Images.Count)) {
-                               throw new ArgumentOutOfRangeException("index", 
index, "ImageList does not contain that many images");
-                       }
-                       i = this.Images[index];
-                       g.DrawImage(i, x, y, width, height);
+               public void Draw(Graphics g, int x, int y, int width, int 
height, int index)
+               {
+                       g.DrawImage(images.GetImage(index), x, y, width, 
height);
                }
 
-               public override string ToString() {
-                       return base.ToString () + " Images.Count: " + 
Images.Count + ", ImageSize: " + ImageSize;
+               public override string ToString()
+               {
+                       return base.ToString() + " Images.Count: " + 
images.Count.ToString() + ", ImageSize: " + images.ImageSize.ToString();
                }
-               #endregion      // Public Instance Methods
+               #endregion // Public Instance Methods
 
-               #region Protected Instance Methods
-               protected override void Dispose(bool disposing) {
-                       if (image_collection!=null) {
-                               image_collection.Dispose();
-                       }
+               #region Protected Instance Methods
+               protected override void Dispose(bool disposing)
+               {
+                       base.Dispose(disposing);
                }
+               #endregion // Protected Instance Methods
 
-               #endregion      // Protected Instance Methods
-
                #region Events
                [Browsable(false)]
                [EditorBrowsable(EditorBrowsableState.Advanced)]
-               public event EventHandler RecreateHandle;
-               #endregion      // Events
+               public event EventHandler RecreateHandle {
+                       add {
+                               recreateHandle += value;
+                       }
+
+                       remove {
+                               recreateHandle -= value;
+                       }
+               }
+               #endregion // Events
        }
 }

Modified: 
trunk/mcs/class/Managed.Windows.Forms/System.Windows.Forms/ImageListStreamer.cs
===================================================================
--- 
trunk/mcs/class/Managed.Windows.Forms/System.Windows.Forms/ImageListStreamer.cs 
    2005-09-28 08:46:05 UTC (rev 50934)
+++ 
trunk/mcs/class/Managed.Windows.Forms/System.Windows.Forms/ImageListStreamer.cs 
    2005-09-28 09:34:32 UTC (rev 50935)
@@ -40,11 +40,16 @@
        public sealed class ImageListStreamer : ISerializable {
 
                private static byte [] signature = new byte [] {77 , 83 , 70 , 
116};
-               
+
+               private readonly ImageList.ImageCollection imageCollection;
                private Image [] images;
                private Size image_size;
                private Color back_color;
 
+               internal ImageListStreamer (ImageList.ImageCollection 
imageCollection) {
+                       this.imageCollection = imageCollection;
+               }
+               
                private ImageListStreamer (SerializationInfo info, 
StreamingContext context) {
 
                        byte [] data = (byte [])info.GetValue ("Data", typeof 
(byte []));
@@ -144,10 +149,12 @@
                        info.AddValue ("Data", stream.ToArray (), typeof (byte 
[]));
                }
 
+               [MonoTODO ("Images should be written to the stream")]
                private byte [] GetStreamData ()
                {
                        MemoryStream stream = new MemoryStream ();
                        BinaryWriter writer = new BinaryWriter (stream);
+                       Image [] images = (imageCollection != null) ? 
imageCollection.ToArray () : this.images;
 
                        int cols = 4;
                        int rows = images.Length / cols;
@@ -207,7 +214,7 @@
                        get { return image_size; }
                }
 
-               internal ColorDepth ImageColorDepth {
+               internal ColorDepth ColorDepth {
                        get { return ColorDepth.Depth32Bit; }
                }
 

_______________________________________________
Mono-patches maillist  -  [email protected]
http://lists.ximian.com/mailman/listinfo/mono-patches

Reply via email to