1 // Permission is hereby granted, free of charge, to any person obtaining
2 // a copy of this software and associated documentation files (the
3 // "Software"), to deal in the Software without restriction, including
4 // without limitation the rights to use, copy, modify, merge, publish,
5 // distribute, sublicense, and/or sell copies of the Software, and to
6 // permit persons to whom the Software is furnished to do so, subject to
7 // the following conditions:
9 // The above copyright notice and this permission notice shall be
10 // included in all copies or substantial portions of the Software.
12 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
13 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
14 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
15 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
16 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
17 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
18 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20 // Copyright (c) 2004 Novell, Inc.
23 // Peter Bartok pbartok@novell.com
26 // $Log: ImageList.cs,v $
27 // Revision 1.7 2004/11/04 11:04:40 ravindra
28 // Implemented the missing overload for Draw method.
30 // Revision 1.6 2004/08/21 01:50:52 ravindra
31 // Changed draw method to use the arguments passed in to draw the image.
33 // Revision 1.5 2004/08/19 21:39:09 pbartok
34 // - Added missing Draw() method
35 // - Added missing RecreateHandle event
37 // Revision 1.4 2004/08/11 17:43:08 pbartok
38 // - Removed disposing of the actual images when the list is disposed
40 // Revision 1.3 2004/08/11 13:43:13 pbartok
41 // - Added missing creation of the collection list
43 // Revision 1.2 2004/08/09 23:12:13 pbartok
44 // - Fixed several bugs Ravindra pointed out
46 // Revision 1.1 2004/07/15 20:05:28 pbartok
47 // - Implemented ImageList and ImageList.ImageCollection classes
48 // - Added ColorDepth enumeration
49 // - Updated SWF VS.Net project
55 using System.Collections;
56 using System.ComponentModel;
58 using System.Drawing.Imaging;
60 namespace System.Windows.Forms {
61 public sealed class ImageList : System.ComponentModel.Component {
62 #region Local Variables
63 private ColorDepth color_depth;
64 private ImageCollection image_collection;
66 private Color transparency_color;
67 private Delegate handler;
68 private ImageListStreamer image_stream;
69 #endregion // Local Variables
72 public sealed class ImageCollection : IList, ICollection, IEnumerable {
73 #region ImageCollection Local Variables
74 private ImageList owner;
75 private ArrayList list;
76 #endregion // ImageCollection Local Variables
78 #region ImageCollection Private Constructors
79 internal ImageCollection(ImageList owner) {
81 this.list=new ArrayList();
83 #endregion // ImageCollection Private Constructor
85 #region ImageCollection Public Instance Properties
98 public bool IsReadOnly {
100 return list.IsReadOnly;
104 public Image this[int index] {
106 if (index<0 || index>=list.Count) {
107 throw new ArgumentOutOfRangeException("index", index, "ImageCollection does not have that many images");
109 return (Image)list[index];
113 if (index<0 || index>=list.Count) {
114 throw new ArgumentOutOfRangeException("index", index, "ImageCollection does not have that many images");
118 throw new ArgumentOutOfRangeException("value", value, "Image cannot be null");
122 // What happens if the bitmap had a previous 'MakeTransparent' done to it?
123 ((Bitmap)list[index]).MakeTransparent(owner.transparency_color);
126 #endregion // ImageCollection Public Instance Properties
128 #region ImageCollection Private Instance Methods
129 private int AddInternal(Image image) {
134 width=owner.ImageSize.Width;
135 height=owner.ImageSize.Height;
136 switch(owner.color_depth) {
137 case ColorDepth.Depth4Bit: format=PixelFormat.Format4bppIndexed; break;
138 case ColorDepth.Depth8Bit: format=PixelFormat.Format8bppIndexed; break;
139 case ColorDepth.Depth16Bit: format=PixelFormat.Format16bppRgb555; break;
140 case ColorDepth.Depth24Bit: format=PixelFormat.Format24bppRgb; break;
141 case ColorDepth.Depth32Bit: format=PixelFormat.Format32bppRgb; break;
142 default: format=PixelFormat.Format32bppRgb; break;
145 // Check if we can add straight or if we have to resize
146 if (image.Width!=width || image.Height!=height || image.PixelFormat!=format) {
148 Bitmap reformatted_image;
150 reformatted_image = new Bitmap(width, height, format);
151 g=Graphics.FromImage(reformatted_image);
153 g.DrawImage(image, new Rectangle(0, 0, width, height), 0, 0, width, height, GraphicsUnit.Pixel);
156 return list.Add(reformatted_image);
158 return list.Add(image);
162 internal void Dispose() {
165 for (int i=0; i<list.Count; i++) {
166 ((Image)list[i]).Dispose();
171 #endregion // ImageCollection Private Instance Methods
173 #region ImageCollection Public Instance Methods
174 public int Add(Image value, Color transparentColor) {
176 throw new ArgumentNullException("value", "Cannot add null image");
179 ((Bitmap)value).MakeTransparent(owner.transparency_color);
180 return AddInternal(value);
184 public void Add(Icon value) {
187 image = value.ToBitmap();
189 if (value==null || image==null) {
190 throw new ArgumentNullException("value", "Cannot add null icon");
193 ((Bitmap)image).MakeTransparent(owner.transparency_color);
197 public void Add(Image value) {
199 throw new ArgumentNullException("value", "Cannot add null image");
201 ((Bitmap)value).MakeTransparent(owner.transparency_color);
205 public int AddStrip(Image value) {
213 throw new ArgumentNullException("value", "Cannot add null images");
216 if ((value.Width % owner.ImageSize.Width) != 0) {
217 throw new ArgumentException("Strip is not a multiple of the ImageList with", "value");
220 // MSDN: The number of images is inferred from the width. A strip is multiple images side-by-side
221 width=owner.ImageSize.Width;
222 height=owner.ImageSize.Height;
223 image_count=value.Width/width;
224 for (int i=0; i<image_count; i++) {
225 image = new Bitmap(value, width, height);
226 g = Graphics.FromImage(image);
228 g.DrawImage(value, new Rectangle(0, 0, width, height), i*width, 0, width, height, GraphicsUnit.Pixel);
235 // FIXME - is this right? MSDN says to return the index, but we might have multiple...
239 public void Clear() {
243 public bool Contains(Image image) {
244 return list.Contains(image);
247 public IEnumerator GetEnumerator() {
248 return list.GetEnumerator();
251 public int IndexOf(Image image) {
252 return list.IndexOf(image);
255 public void Remove(Image image) {
259 public void RemoveAt(int index) {
260 if (index<0 || index>=list.Count) {
261 throw new ArgumentOutOfRangeException("index", index, "ImageCollection does not have that many images");
264 list.RemoveAt(index);
266 #endregion // ImageCollection Public Instance Methods
268 #region ImageCollection Interface Properties
269 object IList.this[int index] {
271 if (index<0 || index>=list.Count) {
272 throw new ArgumentOutOfRangeException("index", index, "ImageCollection does not have that many images");
278 if (!(value is Bitmap)) {
279 throw new ArgumentException("Object of type Image required", "value");
282 this[index]=(Image)value;
286 bool IList.IsFixedSize {
292 bool IList.IsReadOnly {
294 return list.IsReadOnly;
298 bool ICollection.IsSynchronized {
300 return list.IsSynchronized;
304 object ICollection.SyncRoot {
306 return list.SyncRoot;
309 #endregion // ImageCollection Interface Properties
311 #region ImageCollection Interface Methods
312 int IList.Add(object value) {
314 throw new ArgumentNullException("value", "Cannot add null images");
317 if (!(value is Bitmap)) {
318 throw new ArgumentException("Object of type Image required", "value");
321 return list.Add(value);
324 bool IList.Contains(object value) {
325 if (!(value is Bitmap)) {
326 throw new ArgumentException("Object of type Image required", "value");
329 return this.Contains((Image) value);
332 int IList.IndexOf(object value) {
333 if (!(value is Bitmap)) {
334 throw new ArgumentException("Object of type Image required", "value");
337 return this.IndexOf((Image) value);
340 void IList.Insert(int index, object value) {
341 if (!(value is Bitmap)) {
342 throw new ArgumentException("Object of type Image required", "value");
344 list.Insert(index, value);
347 void IList.Remove(object value) {
348 if (!(value is Bitmap)) {
349 throw new ArgumentException("Object of type Image required", "value");
354 void ICollection.CopyTo(Array array, int index) {
356 list.CopyTo(array, index);
359 #endregion // ImageCollection Interface Methods
361 #endregion // Sub-classes
363 #region Public Constructors
365 color_depth = ColorDepth.Depth8Bit;
366 transparency_color = Color.Transparent;
367 size = new Size(16, 16);
368 image_collection = new ImageCollection(this);
372 public ImageList(System.ComponentModel.IContainer container) {
373 throw new NotImplementedException();
375 #endregion // Public Constructors
377 #region Public Instance Properties
378 public ColorDepth ColorDepth {
380 return this.color_depth;
384 this.color_depth=value;
388 [MonoTODO("Determine if we support HBITMAP handles, this would involve XplatUI")]
389 public IntPtr Handle {
391 if (RecreateHandle!=null) RecreateHandle(this, EventArgs.Empty);
396 [MonoTODO("Determine if we support HBITMAP handles, this would involve XplatUI")]
397 public bool HandleCreated {
403 public ImageCollection Images {
405 return this.image_collection;
409 public Size ImageSize {
415 if (value.Width<1 || value.Width>256 || value.Height<1 || value.Height>256) {
416 throw new ArgumentException("ImageSize width and height must be between 1 and 255", "value");
422 public ImageListStreamer ImageStream {
428 image_stream = value;
432 public Color TransparentColor {
434 return this.transparency_color;
438 this.transparency_color=value;
441 #endregion // Public Instance Properties
443 #region Public Instance Methods
444 public void Draw(Graphics g, Point pt, int index) {
445 this.Draw(g, pt.X, pt.Y, index);
448 public void Draw(Graphics g, int x, int y, int index) {
449 this.Draw(g, x, y, this.size.Width, this.size.Height, index);
452 public void Draw(Graphics g, int x, int y, int width, int height, int index) {
455 if ((index < 0) || (index >= this.Images.Count)) {
456 throw new ArgumentOutOfRangeException("index", index, "ImageList does not contain that many images");
458 i = this.Images[index];
459 g.DrawImage(i, x, y, width, height);
462 public override string ToString() {
\r
463 return "ImageList Size "+this.size.Width.ToString()+"x"+this.size.Height.ToString()+", Depth "+this.color_depth.ToString()+", Transparency color "+this.transparency_color.ToString();
\r
465 #endregion // Public Instance Methods
467 #region Protected Instance Methods
468 protected override void Dispose(bool disposing) {
\r
469 if (image_collection!=null) {
\r
470 image_collection.Dispose();
\r
474 #endregion // Protected Instance Methods
477 public event EventHandler RecreateHandle;