[System.Net] Add support for .pac proxy config scripts on mac
[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                 [MonoTODO]
119                 private Icon (SerializationInfo info, StreamingContext context)
120                 {
121                         //FIXME, need to check how MS stores Icon structure
122                         //Will serialized form help
123                         throw new NotImplementedException ();
124                 }
125                 #endregion
126
127                 [MonoTODO]
128                 void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context)
129                 {
130                         throw new NotImplementedException ();
131                 }
132
133                 public void Dispose ()
134                 {
135 #if INTPTR_SUPPORT
136                         if (winHandle!=IntPtr.Zero)
137                                 winHandle = IntPtr.Zero;
138 #endif
139                 }
140
141                 public object Clone ()
142                 {
143                         Icon newIcon = new Icon ();
144                         newIcon._bitmap = (Bitmap)_bitmap.Clone ();
145                         return newIcon;
146                 }
147
148 #if INTPTR_SUPPORT
149                 [MonoTODO]
150                 public static Icon FromHandle (IntPtr handle)
151                 {
152                         throw new NotImplementedException ();
153                 }
154 #endif
155                 public void Save (Stream outputStream)  {
156                         _bitmap.Save (outputStream, System.Drawing.Imaging.ImageFormat.Icon);
157                 }
158
159                 public Bitmap ToBitmap () {
160                         return _bitmap;
161                 }
162
163                 public override string ToString ()
164                 {
165                         //is this correct, this is what returned by .Net
166                         return "<Icon>";                        
167                 }
168
169 #if INTPTR_SUPPORT
170                 [Browsable (false)]
171                 public IntPtr Handle {
172                         get { 
173                                 return winHandle;
174                         }
175                 }
176 #endif
177
178                 [Browsable (false)]
179                 public int Height {
180                         get {
181                                 return _bitmap.Height;
182                         }
183                 }
184
185                 public Size Size {
186                         get {
187                                 return _bitmap.Size;
188                         }
189                 }
190
191                 [Browsable (false)]
192                 public int Width {
193                         get {
194                                 return _bitmap.Width;
195                         }
196                 }
197         }
198 }