Merge pull request #487 from mayerwin/patch-1
[mono.git] / mcs / class / Managed.Windows.Forms / System.Windows.Forms / ImageList.cs
index 1c47c3a6e395a8d6c54d136a0e58f174210bf5c3..b0f0c4a0272aadd341da2e62cc2c569b0c8d6606 100644 (file)
@@ -74,9 +74,7 @@ namespace System.Windows.Forms
 {
        [DefaultProperty("Images")]
        [Designer("System.Windows.Forms.Design.ImageListDesigner, " + Consts.AssemblySystem_Design)]
-#if NET_2_0
        [DesignerSerializer("System.Windows.Forms.Design.ImageListCodeDomSerializer, " + Consts.AssemblySystem_Design, "System.ComponentModel.Design.Serialization.CodeDomSerializer, " + Consts.AssemblySystem_Design)]
-#endif
        [ToolboxItemFilter("System.Windows.Forms")]
        [TypeConverter(typeof(ImageListConverter))]
        public sealed class ImageList : System.ComponentModel.Component
@@ -85,10 +83,7 @@ namespace System.Windows.Forms
                private const ColorDepth DefaultColorDepth = ColorDepth.Depth8Bit;
                private static readonly Size DefaultImageSize = new Size(16, 16);
                private static readonly Color DefaultTransparentColor = Color.Transparent;
-
-#if NET_2_0
                private object tag;
-#endif
                private readonly ImageCollection images;
                #endregion // Private Fields
 
@@ -98,19 +93,8 @@ namespace System.Windows.Forms
                {
                        private const int AlphaMask = unchecked((int)0xFF000000);
 
-                       private
-#if NET_2_0
-                       static
-#else
-                       sealed
-#endif
-                       class IndexedColorDepths
+                       private static class IndexedColorDepths
                        {
-#if !NET_2_0
-                               private IndexedColorDepths()
-                               {
-                               }
-#endif
                                internal static readonly ColorPalette Palette4Bit;
                                internal static readonly ColorPalette Palette8Bit;
                                private static readonly int[] squares;
@@ -219,14 +203,10 @@ namespace System.Windows.Forms
                        private Size imageSize = DefaultImageSize;
                        private Color transparentColor = DefaultTransparentColor;
                        private ArrayList list = new ArrayList();
-#if NET_2_0
                        private ArrayList keys = new ArrayList();
-#endif
                        private int count;
                        private bool handleCreated;
-#if NET_2_0
                        private int lastKeyIndex = -1;
-#endif
                        private readonly ImageList owner;
                        #endregion // ImageCollection Private Fields
 
@@ -299,27 +279,21 @@ namespace System.Windows.Forms
                                        Image[] streamImages;
 
                                        if (value == null) {
-#if NET_2_0
                                                if (this.handleCreated)
                                                        DestroyHandle();
                                                else
                                                        this.Clear();
-#endif
                                        }
                                        // Only deserialized ImageListStreamers are used.
                                        else if ((streamImages = value.Images) != null) {
                                                this.list = new ArrayList(streamImages.Length);
                                                this.count = 0;
                                                this.handleCreated = true;
-#if NET_2_0
                                                this.keys = new ArrayList(streamImages.Length);
-#endif
 
                                                for (index = 0; index < streamImages.Length; index++) {
                                                        list.Add((Image)streamImages[index].Clone());
-#if NET_2_0
                                                        keys.Add(null);
-#endif
                                                }
 
                                                // Invalid ColorDepth values are ignored.
@@ -327,10 +301,8 @@ namespace System.Windows.Forms
                                                        this.colorDepth = (ColorDepth)value.ColorDepth;
                                                this.imageSize = value.ImageSize;
 
-#if NET_2_0
                                                // Event is raised even when handle was not created yet.
                                                owner.OnRecreateHandle();
-#endif
                                        }
                                }
                        }
@@ -392,7 +364,6 @@ namespace System.Windows.Forms
                                }
                        }
 
-#if NET_2_0
                        public Image this[string key] {
                                get {
                                        int index;
@@ -417,11 +388,9 @@ namespace System.Windows.Forms
                                        return keyCollection;
                                }
                        }
-#endif
                        #endregion // ImageCollection Public Instance Properties
 
                        #region ImageCollection Private Static Methods
-#if NET_2_0
                        private static bool CompareKeys(string key1, string key2)
                        {
                                // Keys are case-insensitive and keys with different length
@@ -432,20 +401,13 @@ namespace System.Windows.Forms
 
                                return string.Compare(key1, key2, true, CultureInfo.InvariantCulture) == 0;
                        }
-#endif
                        #endregion // ImageCollection Private Static Methods
 
                        #region ImageCollection Private Instance Methods
-#if NET_2_0
                        private int AddItem(string key, ImageListItem item)
-#else
-                       private int AddItem(ImageListItem item)
-#endif
                        {
                                int itemIndex;
-#if NET_2_0
                                int index;
-#endif
 
                                if (this.handleCreated)
                                        itemIndex = AddItemInternal(item);
@@ -457,13 +419,11 @@ namespace System.Windows.Forms
                                        this.count += item.ImageCount;
                                }
 
-#if NET_2_0
                                if ((item.Flags & ItemFlags.ImageStrip) == 0)
                                        keys.Add(key);
                                else
                                        for (index = 0; index < item.ImageCount; index++)
                                                keys.Add(null);
-#endif
 
                                return itemIndex;
                        }
@@ -567,26 +527,23 @@ namespace System.Windows.Forms
                        {
                                int imageWidth;
                                int imageHeight;
-                               Bitmap bitmap;
-                               Graphics graphics;
                                ImageAttributes imageAttributes;
 
                                if (transparentColor.A == 0)
                                        imageAttributes = null;
                                else {
                                        imageAttributes = new ImageAttributes();
-                                       imageAttributes.SetColorKey(transparentColor, transparentColor);
+                                       imageAttributes.SetColorKey (transparentColor, transparentColor);
                                }
 
-                               bitmap = new Bitmap(imageWidth = this.imageSize.Width, imageHeight = this.imageSize.Height, PixelFormat.Format32bppArgb);
-                               graphics = Graphics.FromImage(bitmap);
-                               graphics.DrawImage(value, new Rectangle(0, 0, imageWidth, imageHeight), 0, 0, value.Width, value.Height, GraphicsUnit.Pixel, imageAttributes);
-                               graphics.Dispose();
+                               var bitmap = new Bitmap (imageWidth = this.imageSize.Width, imageHeight = this.imageSize.Height, PixelFormat.Format32bppArgb);
+                               using (var graphics = Graphics.FromImage (bitmap))
+                                       graphics.DrawImage (value, new Rectangle(0, 0, imageWidth, imageHeight), 0, 0, value.Width, value.Height, GraphicsUnit.Pixel, imageAttributes);
 
                                if (imageAttributes != null)
-                                       imageAttributes.Dispose();
+                                       imageAttributes.Dispose ();
 
-                               ReduceColorDepth(bitmap);
+                               ReduceColorDepth (bitmap);
                                return bitmap;
                        }
 
@@ -662,9 +619,7 @@ namespace System.Windows.Forms
                                        this.list = new ArrayList();
                                        this.count = 0;
                                        this.handleCreated = false;
-#if NET_2_0
                                        keys = new ArrayList();
-#endif
                                }
                        }
 
@@ -694,32 +649,19 @@ namespace System.Windows.Forms
                        #region ImageCollection Public Instance Methods
                        public void Add(Icon value)
                        {
-#if NET_2_0
                                Add(null, value);
-#else
-                               AddItem(new ImageListItem(value));
-#endif
                        }
 
                        public void Add(Image value)
                        {
-#if NET_2_0
                                Add(null, value);
-#else
-                               AddItem(new ImageListItem(value));
-#endif
                        }
 
                        public int Add(Image value, Color transparentColor)
                        {
-#if NET_2_0
                                return AddItem(null, new ImageListItem(value, transparentColor));
-#else
-                               return AddItem(new ImageListItem(value, transparentColor));
-#endif
                        }
 
-#if NET_2_0
                        public void Add(string key, Icon icon)
                        {
                                // Argument has name icon but exceptions use name value.
@@ -742,7 +684,6 @@ namespace System.Windows.Forms
                                for (index = 0; index < images.Length; index++)
                                        Add(images[index]);
                        }
-#endif
 
                        public int AddStrip(Image value)
                        {
@@ -758,37 +699,27 @@ namespace System.Windows.Forms
                                if (value.Height != this.imageSize.Height)
                                        throw new ArgumentException("Height of image strip must be equal to ImageSize.Height.", "value");
 
-#if NET_2_0
                                return AddItem(null, new ImageListItem(value, width / imageWidth));
-#else
-                               return AddItem(new ImageListItem(value, width / imageWidth));
-#endif
                        }
 
                        public void Clear()
                        {
                                list.Clear();
-                               if (this.handleCreated)
+                               if (!this.handleCreated)
                                        this.count = 0;
-#if NET_2_0
                                keys.Clear();
-#endif
                        }
 
-#if NET_2_0
                        [EditorBrowsable(EditorBrowsableState.Never)]
-#endif
                        public bool Contains(Image image)
                        {
                                throw new NotSupportedException();
                        }
 
-#if NET_2_0
                        public bool ContainsKey(string key)
                        {
                                return IndexOfKey(key) != -1;
                        }
-#endif
 
                        public IEnumerator GetEnumerator()
                        {
@@ -806,15 +737,12 @@ namespace System.Windows.Forms
                                return images.GetEnumerator();
                        }
 
-#if NET_2_0
                        [EditorBrowsable(EditorBrowsableState.Never)]
-#endif
                        public int IndexOf(Image image)
                        {
                                throw new NotSupportedException();
                        }
 
-#if NET_2_0
                        public int IndexOfKey(string key)
                        {
                                int index;
@@ -836,11 +764,8 @@ namespace System.Windows.Forms
 
                                return this.lastKeyIndex = -1;
                        }
-#endif
 
-#if NET_2_0
                        [EditorBrowsable(EditorBrowsableState.Never)]
-#endif
                        public void Remove(Image image)
                        {
                                throw new NotSupportedException();
@@ -853,14 +778,11 @@ namespace System.Windows.Forms
 
                                CreateHandle();
                                list.RemoveAt(index);
-#if NET_2_0
                                keys.RemoveAt(index);
-#endif
                                if (Changed != null)
                                        Changed (this, EventArgs.Empty);
                        }
 
-#if NET_2_0
                        public void RemoveByKey(string key)
                        {
                                int index;
@@ -877,7 +799,6 @@ namespace System.Windows.Forms
 
                                keys[index] = name;
                        }
-#endif
                        #endregion // ImageCollection Public Instance Methods
 
                        #region ImageCollection Interface Properties
@@ -926,14 +847,14 @@ namespace System.Windows.Forms
                                return index;
                        }
 
-                       bool IList.Contains(object value)
+                       bool IList.Contains(object image)
                        {
-                               return value is Image ? this.Contains((Image)value) : false;
+                               return image is Image ? this.Contains ((Image) image) : false;
                        }
 
-                       int IList.IndexOf(object value)
+                       int IList.IndexOf (object image)
                        {
-                               return value is Image ? this.IndexOf((Image)value) : -1;
+                               return image is Image ? this.IndexOf ((Image) image) : -1;
                        }
 
                        void IList.Insert(int index, object value)
@@ -941,18 +862,16 @@ namespace System.Windows.Forms
                                throw new NotSupportedException();
                        }
 
-                       void IList.Remove(object value)
+                       void IList.Remove (object image)
                        {
-                               if (value is Image)
-                                       this.Remove((Image)value);
+                               if (image is Image)
+                                       this.Remove ((Image) image);
                        }
 
-                       void ICollection.CopyTo(Array array, int index)
+                       void ICollection.CopyTo(Array dest, int index)
                        {
-                               int imageIndex;
-
-                               for (imageIndex = 0; imageIndex < this.Count; imageIndex++)
-                                       array.SetValue(this[index], index++);
+                               for (int imageIndex = 0; imageIndex < this.Count; imageIndex++)
+                                       dest.SetValue (this[imageIndex], index++);
                        }
                        #endregion // ImageCollection Interface Methods
                }
@@ -1001,20 +920,14 @@ namespace System.Windows.Forms
                {
                        // ImageSize is serialized in ImageStream when non-empty.
                        // It is serialized even if it has its default value when empty.
-#if NET_2_0
                        return images.Empty;
-#else
-                       return this.ImageSize != DefaultImageSize;
-#endif
                }
 
-
                internal void ResetColorDepth ()
                {
                        this.ColorDepth = DefaultColorDepth;
                }
 
-#if NET_2_0
                internal void ResetImageSize ()
                {
                        this.ImageSize = DefaultImageSize;
@@ -1024,13 +937,9 @@ namespace System.Windows.Forms
                {
                        this.TransparentColor = Color.LightGray;
                }
-#endif
                #endregion // Private Instance Methods
 
                #region Public Instance Properties
-#if !NET_2_0
-               [DefaultValue(DefaultColorDepth)]
-#endif
                public ColorDepth ColorDepth {
                        get {
                                return images.ColorDepth;
@@ -1092,7 +1001,6 @@ namespace System.Windows.Forms
                        }
                }
 
-#if NET_2_0
                [Bindable(true)]
                [DefaultValue(null)]
                [Localizable(false)]
@@ -1106,7 +1014,6 @@ namespace System.Windows.Forms
                                this.tag = value;
                        }
                }
-#endif
 
                public Color TransparentColor {
                        get {