DrawString implementation
[mono.git] / mcs / class / System.Drawing / System.Drawing / Size.cs
1 //
2 // System.Drawing.Size.cs
3 //
4 // Author:
5 //   Mike Kestner (mkestner@speakeasy.net)
6 //
7 // (C) 2001 Mike Kestner
8 //
9
10 using System;
11 using System.Runtime.Serialization;
12
13 namespace System.Drawing {
14         
15         [Serializable]
16         public struct Size { 
17                 
18                 // Private Height and width fields.
19                 int width, height;
20
21                 // -----------------------
22                 // Public Shared Members
23                 // -----------------------
24
25                 /// <summary>
26                 ///     Empty Shared Field
27                 /// </summary>
28                 ///
29                 /// <remarks>
30                 ///     An uninitialized Size Structure.
31                 /// </remarks>
32                 
33                 public static readonly Size Empty;
34
35                 /// <summary>
36                 ///     Ceiling Shared Method
37                 /// </summary>
38                 ///
39                 /// <remarks>
40                 ///     Produces a Size structure from a SizeF structure by
41                 ///     taking the ceiling of the Width and Height properties.
42                 /// </remarks>
43                 
44                 public static Size Ceiling (SizeF value)
45                 {
46                         int w, h;
47                         checked {
48                                 w = (int) Math.Ceiling (value.Width);
49                                 h = (int) Math.Ceiling (value.Height);
50                         }
51
52                         return new Size (w, h);
53                 }
54
55                 /// <summary>
56                 ///     Round Shared Method
57                 /// </summary>
58                 ///
59                 /// <remarks>
60                 ///     Produces a Size structure from a SizeF structure by
61                 ///     rounding the Width and Height properties.
62                 /// </remarks>
63                 
64                 public static Size Round (SizeF value)
65                 {
66                         int w, h;
67                         checked {
68                                 w = (int) Math.Round (value.Width);
69                                 h = (int) Math.Round (value.Height);
70                         }
71
72                         return new Size (w, h);
73                 }
74
75                 /// <summary>
76                 ///     Truncate Shared Method
77                 /// </summary>
78                 ///
79                 /// <remarks>
80                 ///     Produces a Size structure from a SizeF structure by
81                 ///     truncating the Width and Height properties.
82                 /// </remarks>
83                 
84                 public static Size Truncate (SizeF value)
85                 {
86                         int w, h;
87                         checked {
88                                 w = (int) value.Width;
89                                 h = (int) value.Height;
90                         }
91
92                         return new Size (w, h);
93                 }
94
95                 /// <summary>
96                 ///     Addition Operator
97                 /// </summary>
98                 ///
99                 /// <remarks>
100                 ///     Addition of two Size structures.
101                 /// </remarks>
102
103                 public static Size operator + (Size sz1, Size sz2)
104                 {
105                         return new Size (sz1.Width + sz2.Width, 
106                                          sz1.Height + sz2.Height);
107                 }
108                 
109                 /// <summary>
110                 ///     Equality Operator
111                 /// </summary>
112                 ///
113                 /// <remarks>
114                 ///     Compares two Size objects. The return value is
115                 ///     based on the equivalence of the Width and Height 
116                 ///     properties of the two Sizes.
117                 /// </remarks>
118
119                 public static bool operator == (Size sz_a, Size sz_b)
120                 {
121                         return ((sz_a.Width == sz_b.Width) && 
122                                 (sz_a.Height == sz_b.Height));
123                 }
124                 
125                 /// <summary>
126                 ///     Inequality Operator
127                 /// </summary>
128                 ///
129                 /// <remarks>
130                 ///     Compares two Size objects. The return value is
131                 ///     based on the equivalence of the Width and Height 
132                 ///     properties of the two Sizes.
133                 /// </remarks>
134
135                 public static bool operator != (Size sz_a, Size sz_b)
136                 {
137                         return ((sz_a.Width != sz_b.Width) || 
138                                 (sz_a.Height != sz_b.Height));
139                 }
140                 
141                 /// <summary>
142                 ///     Subtraction Operator
143                 /// </summary>
144                 ///
145                 /// <remarks>
146                 ///     Subtracts two Size structures.
147                 /// </remarks>
148
149                 public static Size operator - (Size sz1, Size sz2)
150                 {
151                         return new Size (sz1.Width - sz2.Width, 
152                                          sz1.Height - sz2.Height);
153                 }
154                 
155                 /// <summary>
156                 ///     Size to Point Conversion
157                 /// </summary>
158                 ///
159                 /// <remarks>
160                 ///     Returns a Point based on the dimensions of a given 
161                 ///     Size. Requires explicit cast.
162                 /// </remarks>
163
164                 public static explicit operator Point (Size sz)
165                 {
166                         return new Point (sz.Width, sz.Height);
167                 }
168
169                 /// <summary>
170                 ///     Size to SizeF Conversion
171                 /// </summary>
172                 ///
173                 /// <remarks>
174                 ///     Creates a SizeF based on the dimensions of a given 
175                 ///     Size. No explicit cast is required.
176                 /// </remarks>
177
178                 public static implicit operator SizeF (Size sz)
179                 {
180                         return new SizeF (sz.Width, sz.Height);
181                 }
182
183
184                 // -----------------------
185                 // Public Constructors
186                 // -----------------------
187
188                 /// <summary>
189                 ///     Size Constructor
190                 /// </summary>
191                 ///
192                 /// <remarks>
193                 ///     Creates a Size from a Point value.
194                 /// </remarks>
195                 
196                 public Size (Point pt)
197                 {
198                         width = pt.X;
199                         height = pt.Y;
200                 }
201
202                 /// <summary>
203                 ///     Size Constructor
204                 /// </summary>
205                 ///
206                 /// <remarks>
207                 ///     Creates a Size from specified dimensions.
208                 /// </remarks>
209                 
210                 public Size (int width, int height)
211                 {
212                         this.width = width;
213                         this.height = height;
214                 }
215
216                 // -----------------------
217                 // Public Instance Members
218                 // -----------------------
219
220                 /// <summary>
221                 ///     IsEmpty Property
222                 /// </summary>
223                 ///
224                 /// <remarks>
225                 ///     Indicates if both Width and Height are zero.
226                 /// </remarks>
227                 
228                 public bool IsEmpty {
229                         get {
230                                 return ((Width == 0) && (Height == 0));
231                         }
232                 }
233
234                 /// <summary>
235                 ///     Width Property
236                 /// </summary>
237                 ///
238                 /// <remarks>
239                 ///     The Width coordinate of the Size.
240                 /// </remarks>
241                 
242                 public int Width {
243                         get {
244                                 return width;
245                         }
246                         set {
247                                 width = value;
248                         }
249                 }
250
251                 /// <summary>
252                 ///     Height Property
253                 /// </summary>
254                 ///
255                 /// <remarks>
256                 ///     The Height coordinate of the Size.
257                 /// </remarks>
258                 
259                 public int Height {
260                         get {
261                                 return height;
262                         }
263                         set {
264                                 height = value;
265                         }
266                 }
267
268                 /// <summary>
269                 ///     Equals Method
270                 /// </summary>
271                 ///
272                 /// <remarks>
273                 ///     Checks equivalence of this Size and another object.
274                 /// </remarks>
275                 
276                 public override bool Equals (object o)
277                 {
278                         if (!(o is Size))
279                                 return false;
280
281                         return (this == (Size) o);
282                 }
283
284                 /// <summary>
285                 ///     GetHashCode Method
286                 /// </summary>
287                 ///
288                 /// <remarks>
289                 ///     Calculates a hashing value.
290                 /// </remarks>
291                 
292                 public override int GetHashCode ()
293                 {
294                         return Width^Height;
295                 }
296
297                 /// <summary>
298                 ///     ToString Method
299                 /// </summary>
300                 ///
301                 /// <remarks>
302                 ///     Formats the Size as a string in coordinate notation.
303                 /// </remarks>
304                 
305                 public override string ToString ()
306                 {
307                         return String.Format ("[{0},{1}]", Width, Height);
308                 }
309
310         }
311 }