Merge branch 'master' of github.com:mono/mono into masterwork
[mono.git] / mcs / class / System.Drawing / System.Drawing / TextureBrush.jvm.cs
1 //
2 // System.Drawing.TextureBrush.cs
3 //
4 // Author:
5 //   Dennis Hayes (dennish@Raytek.com)
6 //   Ravindra (rkumar@novell.com)
7 //
8 // (C) 2002 Ximian, Inc
9 // (C) 2004 Novell, Inc.
10 //
11
12 //
13 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
14 //
15 // Permission is hereby granted, free of charge, to any person obtaining
16 // a copy of this software and associated documentation files (the
17 // "Software"), to deal in the Software without restriction, including
18 // without limitation the rights to use, copy, modify, merge, publish,
19 // distribute, sublicense, and/or sell copies of the Software, and to
20 // permit persons to whom the Software is furnished to do so, subject to
21 // the following conditions:
22 // 
23 // The above copyright notice and this permission notice shall be
24 // included in all copies or substantial portions of the Software.
25 // 
26 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
27 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
28 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
29 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
30 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
31 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
32 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
33 //
34
35 using System;
36 using System.Drawing.Drawing2D;
37 using System.Drawing.Imaging;
38
39 using awt = java.awt;
40 using geom = java.awt.geom;
41 using image = java.awt.image;
42
43 namespace System.Drawing {
44         /// <summary>
45         /// Summary description for TextureBrush.
46         /// </summary>
47         public sealed class TextureBrush : Brush {
48                 readonly awt.TexturePaint _nativeObject;
49                 RectangleF _sourceRectangle;
50                 Image _texture = null;
51                 WrapMode _wrapMode;
52
53                 protected override java.awt.Paint NativeObject {
54                         get {
55                                 return _nativeObject;
56                         }
57                 }
58
59                 #region ctors
60
61                 public TextureBrush (Image image) : this (image, WrapMode.Tile) {
62                 }
63
64                 public TextureBrush (Image image, WrapMode wrapMode) : 
65                         this( image, wrapMode, new RectangleF(0, 0, image.Width, image.Height )){
66                 }
67
68                 public TextureBrush (Image image, Rectangle dstRect) : 
69                         this( image, WrapMode.Tile, dstRect ) {
70                 }
71
72                 public TextureBrush (Image image, RectangleF dstRect) : 
73                         this( image, WrapMode.Tile, dstRect ) {
74                 }
75
76                 [MonoTODO]
77                 public TextureBrush (Image image, Rectangle dstRect, ImageAttributes imageAttr) : this( image, dstRect ) {
78                         // TBD: Implement ImageAttributes
79                 }
80
81                 [MonoTODO]
82                 public TextureBrush (Image image, RectangleF dstRect, ImageAttributes imageAttr) : this( image, dstRect ) {
83                         // TBD: Implement ImageAttributes
84                 }
85
86                 public TextureBrush (Image image, WrapMode wrapMode, Rectangle dstRect) :
87                         this( image, wrapMode, new RectangleF(dstRect.X, dstRect.Y, dstRect.Width, dstRect.Height )){
88                 }
89
90                 [MonoTODO]
91                 public TextureBrush (Image image, WrapMode wrapMode, RectangleF dstRect) {
92                         // TBD: check if not metafile
93                         _sourceRectangle = dstRect;
94                         _texture = (Image)((Bitmap)image).Clone(dstRect, image.PixelFormat);
95                         _wrapMode = wrapMode;
96
97                         if (wrapMode != Drawing2D.WrapMode.Tile)
98                                 image = CreateWrappedImage(_texture, wrapMode);
99                         else
100                                 image = _texture;
101
102                         _nativeObject = new awt.TexturePaint((image.BufferedImage)image.NativeObject.CurrentImage.NativeImage,
103                                 new geom.Rectangle2D.Float(0, 0, image.Width, image.Height));
104                 }
105
106                 #endregion
107
108                 #region CreateWrappedImage
109
110                 private Image CreateWrappedImage(Image image, WrapMode wrapMode) {
111                         Image b = null;
112                         Graphics g = null;
113
114                         switch (wrapMode) {
115                                 case Drawing2D.WrapMode.TileFlipX :
116                                         b = new Bitmap(image.Width * 2, image.Height);
117                                         g = Graphics.FromImage( b );
118                                         g.DrawImage(image, new Matrix());
119                                         g.DrawImage(image, new Matrix(-1, 0, 0, 1, image.Width * 2 - 1, 0));
120                                         break;
121                                 case Drawing2D.WrapMode.TileFlipY :
122                                         b = new Bitmap(image.Width, image.Height * 2);
123                                         g = Graphics.FromImage( b );
124                                         g.DrawImage(image, new Matrix());
125                                         g.DrawImage(image, new Matrix(1, 0, 0, -1, 0, image.Height * 2 - 1));
126                                         break;
127                                 case Drawing2D.WrapMode.TileFlipXY :
128                                         b = new Bitmap(image.Width * 2, image.Height * 2);
129                                         g = Graphics.FromImage( b );
130                                         g.DrawImage(image, new Matrix());
131                                         g.DrawImage(image, new Matrix(-1, 0, 0, 1, image.Width * 2 - 1, 0));
132                                         g.DrawImage(image, new Matrix(1, 0, 0, -1, 0, image.Height * 2 - 1));
133                                         g.DrawImage(image, new Matrix(-1, 0, 0, -1, image.Width * 2 - 1, image.Height * 2 - 1));
134                                         break;
135                                 case Drawing2D.WrapMode.Clamp :
136                                         // TBD: Implement WrapMode.Clamp
137                                         return image;
138                                 default : 
139                                         b = image;
140                                         break;
141                         }
142
143                         return b;
144                 }
145
146                 #endregion
147
148                 #region properties
149
150                 public Image Image {
151                         get {
152                                 return (Image)_texture.Clone();
153                         }
154                 }
155
156                 public Matrix Transform {
157                         get {                                   
158                                 return BrushTransform;
159                         }
160                         set {
161                                 BrushTransform = value;
162                         }
163                 }
164
165                 [MonoTODO]
166                 public WrapMode WrapMode {
167                         get {
168                                 return _wrapMode;
169                         }
170                         set {
171                                 _wrapMode = value;
172                         }
173                 }
174
175                 #endregion
176
177                 #region public methods
178
179                 public override object Clone () {
180                         TextureBrush copy = (TextureBrush)InternalClone();
181
182                         if (_texture != null)
183                                 copy._texture = (Image)_texture.Clone();
184
185                         return copy;
186                 }
187
188                 public void MultiplyTransform (Matrix matrix) {
189                         base.BrushMultiplyTransform( matrix );
190                 }
191
192                 public void MultiplyTransform (Matrix matrix, MatrixOrder order) {
193                         base.BrushMultiplyTransform( matrix, order );
194                 }
195
196                 public void ResetTransform () {
197                         base.BrushResetTransform();
198                 }
199
200                 public void RotateTransform (float angle) {
201                         base.BrushRotateTransform( angle );
202                 }
203
204                 public void RotateTransform (float angle, MatrixOrder order) {
205                         base.BrushRotateTransform( angle, order );
206                 }
207
208                 public void ScaleTransform (float sx, float sy) {
209                         base.BrushScaleTransform( sx, sy );
210                 }
211
212                 public void ScaleTransform (float sx, float sy, MatrixOrder order) {
213                         base.BrushScaleTransform( sx, sy, order );
214                 }
215
216                 public void TranslateTransform (float dx, float dy) {
217                         base.BrushTranslateTransform( dx, dy );
218                 }
219
220                 public void TranslateTransform (float dx, float dy, MatrixOrder order) {
221                         base.BrushTranslateTransform( dx, dy, order );
222                 }
223
224                 #endregion
225         }
226 }