Add a more functional (i.e. fewer-stubs) implementation of System.Data.Linq.
[mono.git] / mcs / class / System.Drawing / System.Drawing.Drawing2D / LinearGradientBrush.cs
1 //
2 // System.Drawing.Drawing2D.LinearGradientBrush.cs
3 //
4 // Authors:
5 //   Dennis Hayes (dennish@Raytek.com)
6 //   Ravindra (rkumar@novell.com)
7 //
8 // Copyright (C) 2002/3 Ximian, Inc. http://www.ximian.com
9 // Copyright (C) 2004,2006 Novell, Inc (http://www.novell.com)
10 //
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
18 // 
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 // 
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 //
30
31 using System.ComponentModel;
32
33 namespace System.Drawing.Drawing2D {
34
35         public sealed class LinearGradientBrush : Brush
36         {
37                 RectangleF rectangle;
38                 
39                 internal LinearGradientBrush (IntPtr native) : base (native)
40                 {
41                         Status status = GDIPlus.GdipGetLineRect (native, out rectangle);
42                         GDIPlus.CheckStatus (status);
43                 }
44
45                 public LinearGradientBrush (Point point1, Point point2, Color color1, Color color2)
46                 {
47                         Status status = GDIPlus.GdipCreateLineBrushI (ref point1, ref point2, color1.ToArgb (), color2.ToArgb (), WrapMode.Tile, out nativeObject);
48                         GDIPlus.CheckStatus (status);
49
50                         status = GDIPlus.GdipGetLineRect (nativeObject, out rectangle);
51                         GDIPlus.CheckStatus (status);
52                 }
53
54                 public LinearGradientBrush (PointF point1, PointF point2, Color color1, Color color2)
55                 {
56                         Status status = GDIPlus.GdipCreateLineBrush (ref point1, ref point2, color1.ToArgb (), color2.ToArgb (), WrapMode.Tile, out nativeObject);
57                         GDIPlus.CheckStatus (status);
58
59                         status = GDIPlus.GdipGetLineRect (nativeObject, out rectangle);
60                         GDIPlus.CheckStatus (status);
61                 }
62
63                 public LinearGradientBrush (Rectangle rect, Color color1, Color color2, LinearGradientMode linearGradientMode)
64                 {
65                         Status status = GDIPlus.GdipCreateLineBrushFromRectI (ref rect, color1.ToArgb (), color2.ToArgb (), linearGradientMode, WrapMode.Tile, out nativeObject);
66                         GDIPlus.CheckStatus (status);
67
68                         rectangle = (RectangleF) rect;
69                 }
70
71                 public LinearGradientBrush (Rectangle rect, Color color1, Color color2, float angle) : this (rect, color1, color2, angle, false)
72                 {
73                 }
74
75                 public LinearGradientBrush (RectangleF rect, Color color1, Color color2, LinearGradientMode linearGradientMode)
76                 {
77                         Status status = GDIPlus.GdipCreateLineBrushFromRect (ref rect, color1.ToArgb (), color2.ToArgb (), linearGradientMode, WrapMode.Tile, out nativeObject);
78                         GDIPlus.CheckStatus (status);
79
80                         rectangle = rect;
81                 }
82
83                 public LinearGradientBrush (RectangleF rect, Color color1, Color color2, float angle) : this (rect, color1, color2, angle, false)
84                 {
85                 }
86
87                 public LinearGradientBrush (Rectangle rect, Color color1, Color color2, float angle, bool isAngleScaleable)
88                 {
89                         Status status = GDIPlus.GdipCreateLineBrushFromRectWithAngleI (ref rect, color1.ToArgb (), color2.ToArgb (), angle, isAngleScaleable, WrapMode.Tile, out nativeObject);
90                         GDIPlus.CheckStatus (status);
91
92                         rectangle = (RectangleF) rect;
93                 }
94
95                 public LinearGradientBrush (RectangleF rect, Color color1, Color color2, float angle, bool isAngleScaleable)
96                 {
97                         Status status = GDIPlus.GdipCreateLineBrushFromRectWithAngle (ref rect, color1.ToArgb (), color2.ToArgb (), angle, isAngleScaleable, WrapMode.Tile, out nativeObject);
98                         GDIPlus.CheckStatus (status);
99
100                         rectangle = rect;
101                 }
102
103                 // Public Properties
104
105                 public Blend Blend {
106                         get {
107                                 int count;
108                                 Status status = GDIPlus.GdipGetLineBlendCount (nativeObject, out count);
109                                 GDIPlus.CheckStatus (status);
110                                 float [] factors = new float [count];
111                                 float [] positions = new float [count];
112                                 status = GDIPlus.GdipGetLineBlend (nativeObject, factors, positions, count);
113                                 GDIPlus.CheckStatus (status);
114
115                                 Blend blend = new Blend ();
116                                 blend.Factors = factors;
117                                 blend.Positions = positions;
118
119                                 return blend;
120                         }
121                         set {
122                                 // no null check, MS throws a NullReferenceException here
123                                 int count;
124                                 float [] factors = value.Factors;
125                                 float [] positions = value.Positions;
126                                 count = factors.Length;
127
128                                 if (count == 0 || positions.Length == 0)
129                                         throw new ArgumentException ("Invalid Blend object. It should have at least 2 elements in each of the factors and positions arrays.");
130
131                                 if (count != positions.Length)
132                                         throw new ArgumentException ("Invalid Blend object. It should contain the same number of factors and positions values.");
133
134                                 if (positions [0] != 0.0F)
135                                         throw new ArgumentException ("Invalid Blend object. The positions array must have 0.0 as its first element.");
136
137                                 if (positions [count - 1] != 1.0F)
138                                         throw new ArgumentException ("Invalid Blend object. The positions array must have 1.0 as its last element.");
139
140                                 Status status = GDIPlus.GdipSetLineBlend (nativeObject, factors, positions, count);
141                                 GDIPlus.CheckStatus (status);
142                         }
143                 }
144
145                 [MonoTODO ("The GammaCorrection value is ignored when using libgdiplus.")]
146                 public bool GammaCorrection {
147                         get {
148                                 bool gammaCorrection;
149                                 Status status = GDIPlus.GdipGetLineGammaCorrection (nativeObject, out gammaCorrection);
150                                 GDIPlus.CheckStatus (status);
151                                 return gammaCorrection;
152                         }
153                         set {
154                                 Status status = GDIPlus.GdipSetLineGammaCorrection (nativeObject, value);
155                                 GDIPlus.CheckStatus (status);
156                         }
157                 }
158
159                 public ColorBlend InterpolationColors {
160                         get {
161                                 int count;
162                                 Status status = GDIPlus.GdipGetLinePresetBlendCount (nativeObject, out count);
163                                 GDIPlus.CheckStatus (status);
164                                 int [] intcolors = new int [count];
165                                 float [] positions = new float [count];
166                                 status = GDIPlus.GdipGetLinePresetBlend (nativeObject, intcolors, positions, count);
167                                 GDIPlus.CheckStatus (status);
168
169                                 ColorBlend interpolationColors = new ColorBlend ();
170                                 Color [] colors = new Color [count];
171                                 for (int i = 0; i < count; i++)
172                                         colors [i] = Color.FromArgb (intcolors [i]);
173                                 interpolationColors.Colors = colors;
174                                 interpolationColors.Positions = positions;
175
176                                 return interpolationColors;
177                         }
178                         set {
179                                 if (value == null)
180                                         throw new ArgumentException ("InterpolationColors is null");
181                                 int count;
182                                 Color [] colors = value.Colors;
183                                 float [] positions = value.Positions;
184                                 count = colors.Length;
185
186                                 if (count == 0 || positions.Length == 0)
187                                         throw new ArgumentException ("Invalid ColorBlend object. It should have at least 2 elements in each of the colors and positions arrays.");
188
189                                 if (count != positions.Length)
190                                         throw new ArgumentException ("Invalid ColorBlend object. It should contain the same number of positions and color values.");
191
192                                 if (positions [0] != 0.0F)
193                                         throw new ArgumentException ("Invalid ColorBlend object. The positions array must have 0.0 as its first element.");
194
195                                 if (positions [count - 1] != 1.0F)
196                                         throw new ArgumentException ("Invalid ColorBlend object. The positions array must have 1.0 as its last element.");
197
198                                 int [] blend = new int [colors.Length];
199                                 for (int i = 0; i < colors.Length; i++)
200                                         blend [i] = colors [i].ToArgb ();
201
202                                 Status status = GDIPlus.GdipSetLinePresetBlend (nativeObject, blend, positions, count);
203                                 GDIPlus.CheckStatus (status);
204                         }
205                 }
206
207                 public Color [] LinearColors {
208                         get {
209                                 int [] colors = new int [2];
210                                 Status status = GDIPlus.GdipGetLineColors (nativeObject, colors);
211                                 GDIPlus.CheckStatus (status);
212                                 Color [] linearColors = new Color [2];
213                                 linearColors [0] = Color.FromArgb (colors [0]);
214                                 linearColors [1] = Color.FromArgb (colors [1]);
215
216                                 return linearColors;
217                         }
218                         set {
219                                 // no null check, MS throws a NullReferenceException here
220                                 Status status = GDIPlus.GdipSetLineColors (nativeObject, value [0].ToArgb (), value [1].ToArgb ());
221                                 GDIPlus.CheckStatus (status);
222                         }
223                 }
224
225                 public RectangleF Rectangle {
226                         get {
227                                 return rectangle;
228                         }
229                 }
230
231                 public Matrix Transform {
232                         get {
233                                 Matrix matrix = new Matrix ();
234                                 Status status = GDIPlus.GdipGetLineTransform (nativeObject, matrix.nativeMatrix);
235                                 GDIPlus.CheckStatus (status);
236
237                                 return matrix;
238                         }
239                         set {
240                                 if (value == null)
241                                         throw new ArgumentNullException ("Transform");
242
243                                 Status status = GDIPlus.GdipSetLineTransform (nativeObject, value.nativeMatrix);
244                                 GDIPlus.CheckStatus (status);
245                         }
246                 }
247
248                 public WrapMode WrapMode {
249                         get {
250                                 WrapMode wrapMode;
251                                 Status status = GDIPlus.GdipGetLineWrapMode (nativeObject, out wrapMode);
252                                 GDIPlus.CheckStatus (status);
253
254                                 return wrapMode;
255                         }
256                         set {
257                                 // note: Clamp isn't valid (context wise) but it is checked in libgdiplus
258                                 if ((value < WrapMode.Tile) || (value > WrapMode.Clamp))
259                                         throw new InvalidEnumArgumentException ("WrapMode");
260
261                                 Status status = GDIPlus.GdipSetLineWrapMode (nativeObject, value);
262                                 GDIPlus.CheckStatus (status);
263                         }
264                 }
265
266                 // Public Methods
267
268                 public void MultiplyTransform (Matrix matrix)
269                 {
270                         MultiplyTransform (matrix, MatrixOrder.Prepend);
271                 }
272
273                 public void MultiplyTransform (Matrix matrix, MatrixOrder order)
274                 {
275                         if (matrix == null)
276                                 throw new ArgumentNullException ("matrix");
277
278                         Status status = GDIPlus.GdipMultiplyLineTransform (nativeObject, matrix.nativeMatrix, order);
279                         GDIPlus.CheckStatus (status);
280                 }
281
282                 public void ResetTransform ()
283                 {
284                         Status status = GDIPlus.GdipResetLineTransform (nativeObject);
285                         GDIPlus.CheckStatus (status);
286                 }
287
288                 public void RotateTransform (float angle)
289                 {
290                         RotateTransform (angle, MatrixOrder.Prepend);
291                 }
292
293                 public void RotateTransform (float angle, MatrixOrder order)
294                 {
295                         Status status = GDIPlus.GdipRotateLineTransform (nativeObject, angle, order);
296                         GDIPlus.CheckStatus (status);
297                 }
298
299                 public void ScaleTransform (float sx, float sy)
300                 {
301                         ScaleTransform (sx, sy, MatrixOrder.Prepend);
302                 }
303
304                 public void ScaleTransform (float sx, float sy, MatrixOrder order)
305                 {
306                         Status status = GDIPlus.GdipScaleLineTransform (nativeObject, sx, sy, order);
307                         GDIPlus.CheckStatus (status);
308                 }
309
310                 public void SetBlendTriangularShape (float focus)
311                 {
312                         SetBlendTriangularShape (focus, 1.0F);
313                 }
314
315                 public void SetBlendTriangularShape (float focus, float scale)
316                 {
317                         if (focus < 0 || focus > 1 || scale < 0 || scale > 1)
318                                 throw new ArgumentException ("Invalid parameter passed.");
319
320                         Status status = GDIPlus.GdipSetLineLinearBlend (nativeObject, focus, scale);
321                         GDIPlus.CheckStatus (status);
322                 }
323
324                 public void SetSigmaBellShape (float focus)
325                 {
326                         SetSigmaBellShape (focus, 1.0F);
327                 }
328
329                 public void SetSigmaBellShape (float focus, float scale)
330                 {
331                         if (focus < 0 || focus > 1 || scale < 0 || scale > 1)
332                                 throw new ArgumentException ("Invalid parameter passed.");
333
334                         Status status = GDIPlus.GdipSetLineSigmaBlend (nativeObject, focus, scale);
335                         GDIPlus.CheckStatus (status);
336                 }
337
338                 public void TranslateTransform (float dx, float dy)
339                 {
340                         TranslateTransform (dx, dy, MatrixOrder.Prepend);
341                 }
342
343                 public void TranslateTransform (float dx, float dy, MatrixOrder order)
344                 {
345                         Status status = GDIPlus.GdipTranslateLineTransform (nativeObject, dx, dy, order);
346                         GDIPlus.CheckStatus (status);
347                 }
348
349                 public override object Clone ()
350                 {
351                         IntPtr clonePtr;
352                         Status status = GDIPlus.GdipCloneBrush (nativeObject, out clonePtr);
353                         GDIPlus.CheckStatus (status);
354
355                         return new LinearGradientBrush (clonePtr);
356                 }
357         }
358 }