This commit was manufactured by cvs2svn to create branch 'mono-1-0'.
[mono.git] / mcs / class / System.Windows.Forms / System.Windows.Forms / PictureBox.cs
1 //
2 // System.Windows.Forms.PicureBox
3 //
4 // Author:
5 //   stubbed out by Hossein Safavi (hsafavi@purdue.edu)
6 //      Dennis Hayes (dennish@raytek.com)
7 //   Aleksey Ryabchuk (ryabchuk@yahoo.com)
8 //
9 // (C) 2002 Ximian, Inc
10 //
11
12 //
13 // Permission is hereby granted, free of charge, to any person obtaining
14 // a copy of this software and associated documentation files (the
15 // "Software"), to deal in the Software without restriction, including
16 // without limitation the rights to use, copy, modify, merge, publish,
17 // distribute, sublicense, and/or sell copies of the Software, and to
18 // permit persons to whom the Software is furnished to do so, subject to
19 // the following conditions:
20 // 
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
23 // 
24 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
28 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
29 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 //
32 using System.Drawing;
33 using System.ComponentModel;
34
35 namespace System.Windows.Forms {
36
37         //<summary>
38         //</summary>
39         public class PictureBox : Control {
40
41                 Image image;
42                 PictureBoxSizeMode sizeMode;
43                 BorderStyle borderStyle;
44
45                 public PictureBox () 
46                 {
47                         image = null;
48                         sizeMode = PictureBoxSizeMode.Normal;
49                         borderStyle = BorderStyle.None;
50                         SetStyle ( ControlStyles.UserPaint, true );
51                         SetStyle ( ControlStyles.Selectable, false );
52                 }
53
54                 public BorderStyle BorderStyle {
55                         get {   return borderStyle; }
56                         set {
57                                 if ( !Enum.IsDefined ( typeof(BorderStyle), value ) )
58                                         throw new InvalidEnumArgumentException( "BorderStyle",
59                                                 (int)value,
60                                                 typeof(BorderStyle));
61                                 
62                                 if ( borderStyle != value ) {
63                                         borderStyle = value;
64                                         RecreateHandle ( );
65                                 }
66                         }
67                 }
68
69
70                 public Image Image {
71                         get { return image; }
72                         set {
73                                 image = value;
74
75                                 if ( sizeMode == PictureBoxSizeMode.AutoSize && image != null )
76                                         SetBounds ( 0, 0, 0, 0, BoundsSpecified.None );
77
78                                 Invalidate ( );
79                         }
80                 }
81
82                 [EditorBrowsable (EditorBrowsableState.Never)]   
83                 public new ImeMode ImeMode {
84                         get { return base.ImeMode; }
85                         set { base.ImeMode = value;}
86                 }
87
88                 protected override CreateParams CreateParams {
89                         get {
90                                 RegisterDefaultWindowClass ( );
91
92                                 CreateParams createParams = base.CreateParams;
93
94                                 createParams.ClassName = Win32.DEFAULT_WINDOW_CLASS;
95
96                                 createParams.Style |= (int) (
97                                         WindowStyles.WS_CHILD | 
98                                         WindowStyles.WS_VISIBLE |
99                                         WindowStyles.WS_CLIPCHILDREN |
100                                         WindowStyles.WS_CLIPSIBLINGS );
101
102                                 switch ( BorderStyle ) {
103                                         case BorderStyle.Fixed3D:
104                                                 createParams.ExStyle |= (int)WindowExStyles.WS_EX_CLIENTEDGE;
105                                                 break;
106                                         case BorderStyle.FixedSingle:
107                                                 createParams.Style |= (int) WindowStyles.WS_BORDER;
108                                                 break;
109                                 };
110                                 return createParams;
111                         }               
112                 }
113
114                 protected override ImeMode DefaultImeMode {
115                         get { return ImeMode.Disable; }
116                 }
117
118                 protected override Size DefaultSize {
119                         get { return new Size(100,50); }
120                 }
121
122                 public override string ToString()
123                 {
124                         return GetType( ).FullName.ToString ( ) + ", SizeMode: " + SizeMode.ToString ( );
125                 }
126
127                 public PictureBoxSizeMode SizeMode {
128                         get { return sizeMode; }
129                         set {   
130                                 if ( !Enum.IsDefined ( typeof(PictureBoxSizeMode), value ) )
131                                         throw new InvalidEnumArgumentException( "SizeMode",
132                                                 (int)value,
133                                                 typeof( PictureBoxSizeMode ) );
134
135                                 if ( sizeMode != value ) {
136                                         sizeMode = value;
137
138                                         if ( sizeMode == PictureBoxSizeMode.AutoSize )
139                                                 SetBounds ( 0, 0, 0, 0, BoundsSpecified.None );
140                                         
141                                         SetStyle ( ControlStyles.AllPaintingInWmPaint, sizeMode == PictureBoxSizeMode.StretchImage );
142
143                                         Invalidate ( );
144                                         OnSizeModeChanged ( EventArgs.Empty );
145                                 }
146                         }
147                 }
148
149                 
150                 [MonoTODO]
151                 protected override void Dispose(bool disposing) { 
152                         base.Dispose(disposing);
153                 }               
154
155                 [MonoTODO]
156                 protected override void OnEnabledChanged(EventArgs e) 
157                 {
158                         //FIXME:
159                         base.OnEnabledChanged(e);
160                 }
161
162                 protected override void OnPaint(PaintEventArgs pe) 
163                 {
164                         if ( Image != null ) {
165                                 switch ( SizeMode ) {
166                                 case PictureBoxSizeMode.StretchImage:
167                                         pe.Graphics.DrawImage ( Image, ClientRectangle );
168                                 break;
169                                 case PictureBoxSizeMode.CenterImage:
170                                         int dx = (ClientRectangle.Width - Image.Width)/2;
171                                         int dy = (ClientRectangle.Height- Image.Height)/2;
172                                         pe.Graphics.DrawImage ( Image, dx, dy );
173                                 break;
174                                 default:
175                                         pe.Graphics.DrawImage ( Image, 0, 0 );
176                                 break;
177                                 }
178                         }
179                         base.OnPaint(pe);
180                 }
181
182                 protected override void OnParentChanged(EventArgs e) 
183                 {
184                         if ( Parent != null ) {
185                                 BackColor = Parent.BackColor;
186                                 Invalidate ( );
187                         }
188                                 
189                         base.OnParentChanged(e);
190                 }
191
192                 protected override void OnResize(EventArgs e) 
193                 {
194                         if ( SizeMode == PictureBoxSizeMode.CenterImage )
195                                 Invalidate ( );
196                         else if ( SizeMode == PictureBoxSizeMode.StretchImage && IsHandleCreated)
197                                 Win32.InvalidateRect ( Handle, IntPtr.Zero, 0 );
198
199                         base.OnResize(e);
200                 }
201
202                 protected virtual void OnSizeModeChanged(EventArgs e)
203                 {
204                         if ( SizeModeChanged != null )
205                                 SizeModeChanged ( this, e );
206                 }
207
208                 [MonoTODO]
209                 protected override void OnVisibleChanged(EventArgs e) 
210                 {
211                         base.OnVisibleChanged ( e );
212                 }
213
214                 [MonoTODO]
215                 //this should be inherited.
216                 protected override void OnPaintBackground (PaintEventArgs e) {
217                         if ( SizeMode != PictureBoxSizeMode.StretchImage ) 
218                                 base.OnPaintBackground ( e );
219                 }
220
221                 protected override void SetBoundsCore(int x,int y,int width,int height,BoundsSpecified specified) 
222                 {
223                         if ( SizeMode == PictureBoxSizeMode.AutoSize && Image != null ) {
224                                 width = Image.Width;
225                                 height= Image.Height;
226                                 specified = BoundsSpecified.Size;
227                         }
228                                 
229                         base.SetBoundsCore(x, y, width, height, specified);
230                 }
231
232                 public event EventHandler SizeModeChanged;
233         }
234 }