Merge pull request #565 from rneatherway/master
[mono.git] / mcs / class / System.Drawing / System.Drawing / TextureBrush.cs
1 //
2 // System.Drawing.TextureBrush.cs
3 //
4 // Authors:
5 //   Dennis Hayes (dennish@Raytek.com)
6 //   Ravindra (rkumar@novell.com)
7 //   Sebastien Pouliot  <sebastien@ximian.com>
8 //
9 // (C) 2002 Ximian, Inc
10 // Copyright (C) 2004,2006-2007 Novell, Inc (http://www.novell.com)
11 //
12 // Permission is hereby granted, free of charge, to any person obtaining
13 // a copy of this software and associated documentation files (the
14 // "Software"), to deal in the Software without restriction, including
15 // without limitation the rights to use, copy, modify, merge, publish,
16 // distribute, sublicense, and/or sell copies of the Software, and to
17 // permit persons to whom the Software is furnished to do so, subject to
18 // the following conditions:
19 // 
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
22 // 
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 //
31
32 using System.ComponentModel;
33 using System.Drawing.Drawing2D;
34 using System.Drawing.Imaging;
35
36 namespace System.Drawing {
37
38         public sealed class TextureBrush : Brush {
39
40                 internal TextureBrush (IntPtr ptr) :
41                         base (ptr)
42                 {
43                 }
44
45                 public TextureBrush (Image bitmap) :
46                         this (bitmap, WrapMode.Tile)
47                 {
48                 }
49
50                 public TextureBrush (Image image, Rectangle dstRect) :
51                         this (image, WrapMode.Tile, dstRect)
52                 {
53                 }
54
55                 public TextureBrush (Image image, RectangleF dstRect) :
56                         this (image, WrapMode.Tile, dstRect)
57                 {
58                 }
59
60                 public TextureBrush (Image image, WrapMode wrapMode)
61                 {
62                         if (image == null)
63                                 throw new ArgumentNullException ("image");
64                         if ((wrapMode < WrapMode.Tile) || (wrapMode > WrapMode.Clamp))
65                                 throw new InvalidEnumArgumentException ("WrapMode");
66
67                         Status status = GDIPlus.GdipCreateTexture (image.nativeObject, wrapMode, out nativeObject);
68                         GDIPlus.CheckStatus (status);
69                 }
70
71                 [MonoLimitation ("ImageAttributes are ignored when using libgdiplus")]
72                 public TextureBrush (Image image, Rectangle dstRect, ImageAttributes imageAttr)
73                 {
74                         if (image == null)
75                                 throw new ArgumentNullException ("image");
76
77                         IntPtr attr = imageAttr == null ? IntPtr.Zero : imageAttr.NativeObject;
78                         Status status = GDIPlus.GdipCreateTextureIAI (image.nativeObject, attr, dstRect.X, dstRect.Y, 
79                                 dstRect.Width, dstRect.Height, out nativeObject);
80                         GDIPlus.CheckStatus (status);
81                 }
82
83                 [MonoLimitation ("ImageAttributes are ignored when using libgdiplus")]
84                 public TextureBrush (Image image, RectangleF dstRect, ImageAttributes imageAttr)
85                 {       
86                         if (image == null)
87                                 throw new ArgumentNullException ("image");
88
89                         IntPtr attr = imageAttr == null ? IntPtr.Zero : imageAttr.NativeObject;
90                         Status status = GDIPlus.GdipCreateTextureIA (image.nativeObject, attr, dstRect.X, dstRect.Y, 
91                                 dstRect.Width, dstRect.Height, out nativeObject);
92                         GDIPlus.CheckStatus (status);                   
93                 }
94
95                 public TextureBrush (Image image, WrapMode wrapMode, Rectangle dstRect)
96                 {
97                         if (image == null)
98                                 throw new ArgumentNullException ("image");
99                         if ((wrapMode < WrapMode.Tile) || (wrapMode > WrapMode.Clamp))
100                                 throw new InvalidEnumArgumentException ("WrapMode");
101
102                         Status status = GDIPlus.GdipCreateTexture2I (image.nativeObject, wrapMode, dstRect.X, dstRect.Y, 
103                                 dstRect.Width, dstRect.Height, out nativeObject);
104                         GDIPlus.CheckStatus (status);
105                 }
106
107                 public TextureBrush (Image image, WrapMode wrapMode, RectangleF dstRect)
108                 {
109                         if (image == null)
110                                 throw new ArgumentNullException ("image");
111                         if ((wrapMode < WrapMode.Tile) || (wrapMode > WrapMode.Clamp))
112                                 throw new InvalidEnumArgumentException ("WrapMode");
113
114                         Status status = GDIPlus.GdipCreateTexture2 (image.nativeObject, wrapMode, dstRect.X, dstRect.Y, 
115                                 dstRect.Width, dstRect.Height, out nativeObject);
116                         GDIPlus.CheckStatus (status);
117                 }
118
119                 // properties
120
121                 public Image Image {
122                         get {
123                                 // this check is required here as GDI+ doesn't check for it 
124                                 if (nativeObject == IntPtr.Zero)
125                                         throw new ArgumentException ("Object was disposed");
126
127                                 IntPtr img;
128                                 Status status = GDIPlus.GdipGetTextureImage (nativeObject, out img);
129                                 GDIPlus.CheckStatus (status);
130                                 return new Bitmap (img);
131                         }
132                 }
133
134                 public Matrix Transform {
135                         get {
136                                 Matrix matrix = new Matrix ();
137                                 Status status = GDIPlus.GdipGetTextureTransform (nativeObject, matrix.nativeMatrix);
138                                 GDIPlus.CheckStatus (status);
139
140                                 return matrix;
141                         }
142                         set {
143                                 if (value == null)
144                                         throw new ArgumentNullException ("Transform");
145
146                                 Status status = GDIPlus.GdipSetTextureTransform (nativeObject, value.nativeMatrix);
147                                 GDIPlus.CheckStatus (status);
148                         }
149                 }
150
151                 public WrapMode WrapMode {
152                         get {
153                                 WrapMode mode;
154                                 Status status = GDIPlus.GdipGetTextureWrapMode (nativeObject, out mode);
155                                 GDIPlus.CheckStatus (status);
156                                 return mode;
157                         }
158                         set {
159                                 if ((value < WrapMode.Tile) || (value > WrapMode.Clamp))
160                                         throw new InvalidEnumArgumentException ("WrapMode");
161
162                                 Status status = GDIPlus.GdipSetTextureWrapMode (nativeObject, value);
163                                 GDIPlus.CheckStatus (status);
164                         }
165                 }
166
167                 // public methods
168
169                 public override object Clone ()
170                 {
171                         IntPtr clonePtr;
172                         Status status = GDIPlus.GdipCloneBrush (nativeObject, out clonePtr);
173                         GDIPlus.CheckStatus (status);
174
175                         return new TextureBrush (clonePtr);
176                 }
177
178                 public void MultiplyTransform (Matrix matrix)
179                 {
180                         MultiplyTransform (matrix, MatrixOrder.Prepend);
181                 }
182
183                 public void MultiplyTransform (Matrix matrix, MatrixOrder order)
184                 {
185                         if (matrix == null)
186                                 throw new ArgumentNullException ("matrix");
187
188                         Status status = GDIPlus.GdipMultiplyTextureTransform (nativeObject, matrix.nativeMatrix, order);
189                         GDIPlus.CheckStatus (status);
190                 }
191
192                 public void ResetTransform ()
193                 {
194                         Status status = GDIPlus.GdipResetTextureTransform (nativeObject);
195                         GDIPlus.CheckStatus (status);
196                 }
197
198                 public void RotateTransform (float angle)
199                 {
200                         RotateTransform (angle, MatrixOrder.Prepend);
201                 }
202
203                 public void RotateTransform (float angle, MatrixOrder order)
204                 {
205                         Status status = GDIPlus.GdipRotateTextureTransform (nativeObject, angle, order);
206                         GDIPlus.CheckStatus (status);
207                 }
208
209                 public void ScaleTransform (float sx, float sy)
210                 {
211                         ScaleTransform (sx, sy, MatrixOrder.Prepend);
212                 }
213
214                 public void ScaleTransform (float sx, float sy, MatrixOrder order)
215                 {
216                         Status status = GDIPlus.GdipScaleTextureTransform (nativeObject, sx, sy, order);
217                         GDIPlus.CheckStatus (status);
218                 }
219
220                 public void TranslateTransform (float dx, float dy)
221                 {
222                         TranslateTransform (dx, dy, MatrixOrder.Prepend);
223                 }
224
225                 public void TranslateTransform (float dx, float dy, MatrixOrder order)
226                 {
227                         Status status = GDIPlus.GdipTranslateTextureTransform (nativeObject, dx, dy, order);
228                         GDIPlus.CheckStatus (status);
229                 }
230         }
231 }