./
[mono.git] / mcs / class / System.Drawing / System.Drawing / Icon.jvm.cs
1 //
2 // System.Drawing.Icon.cs
3 //
4 // Authors:
5 //   Andrew Skiba (andrews@mainsoft.com)
6 //   Dennis Hayes (dennish@Raytek.com)
7 //   Andreas Nahr (ClassDevelopment@A-SoftTech.com)
8 //   Sanjay Gupta (gsanjay@novell.com)
9 //
10 // Copyright (C) 2005 Mainsoft, Corp. http://mainsoft.com
11 // Copyright (C) 2002 Ximian, Inc. http://www.ximian.com
12 // Copyright (C) 2004 Novell, Inc. http://www.novell.com
13 //
14 // Permission is hereby granted, free of charge, to any person obtaining
15 // a copy of this software and associated documentation files (the
16 // "Software"), to deal in the Software without restriction, including
17 // without limitation the rights to use, copy, modify, merge, publish,
18 // distribute, sublicense, and/or sell copies of the Software, and to
19 // permit persons to whom the Software is furnished to do so, subject to
20 // the following conditions:
21 // 
22 // The above copyright notice and this permission notice shall be
23 // included in all copies or substantial portions of the Software.
24 // 
25 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
29 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
30 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
31 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 //
33
34 using System;
35 using System.IO;
36 using System.Drawing.Imaging;
37 using System.Runtime.Serialization;
38 using System.Runtime.InteropServices;
39 using System.ComponentModel;
40
41 namespace System.Drawing
42 {
43         [Serializable]
44         [ComVisible (false)]
45         [TypeConverter(typeof(IconConverter))]
46         public sealed class Icon 
47                 : MarshalByRefObject, ISerializable, ICloneable, IDisposable
48         {
49                 private System.Drawing.Bitmap _bitmap;
50
51                 #region Ctors
52                 private void SelectSize (int width, int height) {
53                         int count = _bitmap.GetFrameCount (FrameDimension.Resolution);
54                         bool sizeObtained = false;
55                         for (int i=0; i<count; i++){
56                                 _bitmap.SelectActiveFrame (
57                                         System.Drawing.Imaging.FrameDimension.Resolution, i);
58                                 if (!sizeObtained)
59                                         if (_bitmap.Height==height && _bitmap.Width==width) {
60                                                 sizeObtained = true;
61                                                 break;
62                                         }
63                         }
64
65                         if (!sizeObtained){
66                                 uint largestSize = 0;
67                                 Bitmap tmpBmp = _bitmap;
68                                 for (int j=0; j<count; j++){
69                                         tmpBmp.SelectActiveFrame (FrameDimension.Resolution, j);
70                                         uint thisSize = (uint)_bitmap.Height * (uint)_bitmap.Width;
71                                         if (thisSize >= largestSize){
72                                                 largestSize = thisSize;
73                                                 _bitmap = tmpBmp;
74                                         }
75                                 }
76                         }
77                 }
78                         
79                 private Icon () {
80                 }
81                 
82                 internal Icon (Bitmap bitmap) {
83                         _bitmap = bitmap;
84                 }
85
86                 public Icon (Icon original, int width, int height) {                    
87                         _bitmap = original._bitmap;
88                         SelectSize (width, height);
89                 }
90
91                 public Icon (Icon original, Size size)
92                         :this (original, size.Width, size.Height) {
93                 }
94
95                 public Icon (Stream stream) 
96                         : this (stream, 32, 32) {
97                 }
98
99                 public Icon (Stream stream, int width, int height)
100                 {
101                         _bitmap = new Bitmap (stream, false, ImageFormat.Icon);
102                         SelectSize (width, height);
103                 }
104
105                 public Icon (string fileName) {
106                         _bitmap = new Bitmap (fileName, false, ImageFormat.Icon);
107                 }
108
109                 public Icon (Type type, string resource)
110                 {
111                         using (Stream s = type.Assembly.GetManifestResourceStream (resource)) {
112                                 if (s == null)
113                                         throw new FileNotFoundException ("Resource name was not found: `" + resource + "'");
114                                 _bitmap = new Bitmap (s, false, ImageFormat.Icon);
115                         }
116                 }
117
118                 private Icon (SerializationInfo info, StreamingContext context)
119                 {
120                         //FIXME, need to check how MS stores Icon structure
121                         //Will serialized form help
122                         throw new NotImplementedException ();
123                 }
124                 #endregion
125
126                 void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context)
127                 {
128                         throw new NotImplementedException ();
129                 }
130
131                 public void Dispose ()
132                 {
133 #if INTPTR_SUPPORT
134                         if (winHandle!=IntPtr.Zero)
135                                 winHandle = IntPtr.Zero;
136 #endif
137                 }
138
139                 public object Clone ()
140                 {
141                         Icon newIcon = new Icon ();
142                         newIcon._bitmap = (Bitmap)_bitmap.Clone ();
143                         return newIcon;
144                 }
145
146 #if INTPTR_SUPPORT
147                 public static Icon FromHandle (IntPtr handle)
148                 {
149                         throw new NotImplementedException ();
150                 }
151 #endif
152                 public void Save (Stream outputStream)  {
153                         _bitmap.Save (outputStream, System.Drawing.Imaging.ImageFormat.Icon);
154                 }
155
156                 public Bitmap ToBitmap () {
157                         return _bitmap;
158                 }
159
160                 public override string ToString ()
161                 {
162                         //is this correct, this is what returned by .Net
163                         return "<Icon>";                        
164                 }
165
166 #if INTPTR_SUPPORT
167                 [Browsable (false)]
168                 public IntPtr Handle {
169                         get { 
170                                 return winHandle;
171                         }
172                 }
173 #endif
174
175                 [Browsable (false)]
176                 public int Height {
177                         get {
178                                 return _bitmap.Height;
179                         }
180                 }
181
182                 public Size Size {
183                         get {
184                                 return _bitmap.Size;
185                         }
186                 }
187
188                 [Browsable (false)]
189                 public int Width {
190                         get {
191                                 return _bitmap.Width;
192                         }
193                 }
194         }
195 }