New test.
[mono.git] / mcs / class / Managed.Windows.Forms / System.Windows.Forms / PictureBox.cs
1 // Permission is hereby granted, free of charge, to any person obtaining
2 // a copy of this software and associated documentation files (the
3 // "Software"), to deal in the Software without restriction, including
4 // without limitation the rights to use, copy, modify, merge, publish,
5 // distribute, sublicense, and/or sell copies of the Software, and to
6 // permit persons to whom the Software is furnished to do so, subject to
7 // the following conditions:
8 // 
9 // The above copyright notice and this permission notice shall be
10 // included in all copies or substantial portions of the Software.
11 // 
12 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
13 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
14 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
15 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
16 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
17 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
18 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
19 //
20 // Copyright (c) 2004-2005 Novell, Inc.
21 //
22 // Authors:
23 //      Jackson Harper (jackson@ximian.com)
24 //
25
26 // COMPLETE
27
28 using System;
29 using System.ComponentModel;
30 using System.ComponentModel.Design;
31 using System.Drawing;
32 using System.Drawing.Imaging;
33 using System.Runtime.InteropServices;
34
35 namespace System.Windows.Forms {
36         [DefaultProperty("Image")]
37         [Designer("System.Windows.Forms.Design.PictureBoxDesigner, " + Consts.AssemblySystem_Design, "System.ComponentModel.Design.IDesigner")]
38         public class PictureBox : Control 
39 #if NET_2_0
40                                         , ISupportInitialize
41 #endif
42         {
43                 #region Fields
44                 private Image   image;
45                 private PictureBoxSizeMode size_mode;
46                 private bool    redraw;
47                 private bool    recalc;
48                 private bool    allow_drop;
49                 private Image   initial_image;
50                 private int     no_update;
51                 #endregion      // Fields
52
53                 private EventHandler frame_handler;
54
55                 #region Public Constructor
56                 public PictureBox ()
57                 {
58                         redraw = true;
59                         recalc = true;
60                         allow_drop = false;
61                         SetStyle (ControlStyles.DoubleBuffer, true);
62                         SetStyle (ControlStyles.Opaque, false);
63                         SetStyle (ControlStyles.Selectable, false);
64                         SetStyle (ControlStyles.SupportsTransparentBackColor, true);
65                         HandleCreated += new EventHandler(PictureBox_HandleCreated);
66                 }
67                 #endregion      // Public Constructor
68
69                 #region Public Properties
70                 [DefaultValue(PictureBoxSizeMode.Normal)]
71                 [Localizable(true)]
72                 [RefreshProperties(RefreshProperties.Repaint)]
73                 public PictureBoxSizeMode SizeMode {
74                         get { return size_mode; }
75                         set {
76                                 if (size_mode == value)
77                                         return;
78                                 size_mode = value;
79                                 UpdateSize ();
80                                 if (no_update == 0) {
81                                         Redraw (true);
82                                         Invalidate ();
83                                 }
84
85                                 OnSizeModeChanged (EventArgs.Empty);
86                         }
87                 }
88
89                 [DefaultValue(null)]
90                 [Localizable(true)]
91                 public Image Image {
92                         get { return image; }
93                         set {
94                                 StopAnimation ();
95
96                                 image = value;
97
98                                 if (IsHandleCreated) {
99                                         UpdateSize ();
100                                         if (image != null && ImageAnimator.CanAnimate (image)) {
101                                                 frame_handler = new EventHandler (OnAnimateImage);
102                                                 ImageAnimator.Animate (image, frame_handler);
103                                         }
104                                         if (no_update == 0) {
105                                                 Redraw (true);
106                                                 Invalidate ();
107                                         }
108                                 }
109                         }
110                 }
111
112                 [DefaultValue(BorderStyle.None)]
113                 [DispId(-504)]
114                 public BorderStyle BorderStyle {
115                         get { return InternalBorderStyle; }
116                         set { InternalBorderStyle = value; }
117                 }
118
119                 [Browsable(false)]
120                 [EditorBrowsable(EditorBrowsableState.Never)]
121                 public new bool CausesValidation {
122                         get { return base.CausesValidation; }
123                         set { base.CausesValidation = value; }
124                 }
125
126 #if NET_2_0
127                 [DefaultValue(null)]
128                 [Localizable(true)]
129                 public Image InitialImage {
130                         get { return initial_image; }
131                         set { initial_image = value; }
132                 }
133 #endif
134
135                 [Browsable(false)]
136                 [EditorBrowsable(EditorBrowsableState.Never)]
137                 public new ImeMode ImeMode {
138                         get { return base.ImeMode; }
139                         set { base.ImeMode = value; }
140                 }
141
142                 [Browsable(false)]
143                 [EditorBrowsable(EditorBrowsableState.Never)]
144                 public override RightToLeft RightToLeft {
145                         get { return base.RightToLeft; }
146                         set { base.RightToLeft = value; }
147                 }
148
149                 [Browsable(false)]
150                 [EditorBrowsable(EditorBrowsableState.Never)]
151                 public new int TabIndex {
152                         get { return base.TabIndex; }
153                         set { base.TabIndex = value; }
154                 }
155
156                 [Browsable(false)]
157                 [EditorBrowsable(EditorBrowsableState.Never)]
158                 public new bool TabStop {
159                         get { return base.TabStop; }
160                         set { base.TabStop = value; }
161                 }
162
163                 [Bindable(false)]
164                 [Browsable(false)]
165                 [EditorBrowsable(EditorBrowsableState.Never)]
166                 public override string Text {
167                         get { return base.Text; }
168                         set { base.Text = value; }
169                 }
170
171                 protected override CreateParams CreateParams {
172                         get {
173                                 return base.CreateParams;
174                         }
175                 }
176
177                 protected override ImeMode DefaultImeMode {
178                         get { return base.DefaultImeMode; }
179                 }
180
181                 [Browsable(false)]
182                 [EditorBrowsable(EditorBrowsableState.Never)]
183                 public override Font Font {
184                         get { return base.Font; }
185                         set { base.Font = value; }
186                 }
187
188                 [Browsable(false)]
189                 [EditorBrowsable(EditorBrowsableState.Never)]
190                 public override Color ForeColor {
191                         get { return base.ForeColor; }
192                         set { base.ForeColor = value; }
193                 }
194
195                 [Browsable(false)]
196                 [EditorBrowsable(EditorBrowsableState.Never)]
197                 public override bool AllowDrop {
198                         get {
199                                 return allow_drop;
200                         }
201                         set {
202                                 if (allow_drop != value) {
203                                         allow_drop = value;
204                                 }
205                         }
206                 }
207                 #endregion      // Public Properties
208
209                 #region Protected Instance Methods
210                 protected override Size DefaultSize {
211                         get { return ThemeEngine.Current.PictureBoxDefaultSize; }
212                 }
213
214                 protected override void Dispose (bool disposing)
215                 {
216                         if (image != null) {
217                                 StopAnimation ();
218                                 image = null;
219                         }
220                         initial_image = null;
221
222                         base.Dispose (disposing);
223                 }
224
225                 protected override void OnPaint (PaintEventArgs pe)
226                 {
227                         ThemeEngine.Current.DrawPictureBox (pe.Graphics, pe.ClipRectangle, this);
228                         base.OnPaint (pe);
229                 }
230
231                 protected override void OnVisibleChanged (EventArgs e)
232                 {
233                         base.OnVisibleChanged (e);
234                         Redraw (true);
235                 }
236
237                 protected virtual void OnSizeModeChanged (EventArgs e)
238                 {
239                         if (SizeModeChanged != null)
240                                 SizeModeChanged (this, e);
241                 }
242
243                 protected override void OnEnabledChanged (EventArgs e)
244                 {
245                         base.OnEnabledChanged (e);
246                 }
247
248                 protected override void OnParentChanged (EventArgs e)
249                 {
250                         base.OnParentChanged (e);
251                 }
252
253                 protected override void OnResize (EventArgs e)
254                 {
255                         base.OnResize (e);
256                         redraw = true;
257
258                         if (size_mode == PictureBoxSizeMode.CenterImage || size_mode == PictureBoxSizeMode.StretchImage)
259                                 Refresh ();
260                 }
261
262                 protected override void SetBoundsCore (int x, int y, int width, int height, BoundsSpecified specified)
263                 {
264                         if (size_mode == PictureBoxSizeMode.AutoSize && image != null) {
265                                 width = image.Width;
266                                 height = image.Height;
267                         }
268                         base.SetBoundsCore (x, y, width, height, specified);
269                 }
270                 #endregion      // Protected Instance Methods
271
272 #if NET_2_0
273                 #region ISupportInitialize Interface
274                 void System.ComponentModel.ISupportInitialize.BeginInit() {
275                         no_update++;
276                 }
277
278                 void System.ComponentModel.ISupportInitialize.EndInit() {
279                         if (no_update > 0) {
280                                 no_update--;
281                         }
282                         if (no_update == 0) {
283                                 Redraw (true);
284                                 Invalidate ();
285                         }
286                 }
287                 #endregion      // ISupportInitialize Interface
288 #endif
289
290                 #region Private Methods
291                 private void StopAnimation ()
292                 {
293                         if (frame_handler == null)
294                                 return;
295                         ImageAnimator.StopAnimate (image, frame_handler);
296                         frame_handler = null;
297                 }
298
299                 private void UpdateSize ()
300                 {
301                         if (image == null)
302                                 return;
303                         if (size_mode == PictureBoxSizeMode.AutoSize)
304                                 ClientSize = image.Size; 
305                 }
306
307                 private void Redraw (bool recalc)
308                 {
309                         redraw = true;
310                         this.recalc = recalc;
311                 }
312
313                 private void OnAnimateImage (object sender, EventArgs e)
314                 {
315                         // This is called from a worker thread,BeginInvoke is used
316                         // so the control is updated from the correct thread
317                         BeginInvoke (new EventHandler (UpdateAnimatedImage), new object [] { this, e });
318                 }
319
320                 private void UpdateAnimatedImage (object sender, EventArgs e)
321                 {
322                         ImageAnimator.UpdateFrames (image);
323                         Redraw (false);
324                         Refresh ();
325                 }
326
327                 private void PictureBox_HandleCreated(object sender, EventArgs e) {
328                         UpdateSize ();
329                         if (image != null && ImageAnimator.CanAnimate (image)) {
330                                 frame_handler = new EventHandler (OnAnimateImage);
331                                 ImageAnimator.Animate (image, frame_handler);
332                         }
333                         if (no_update == 0) {
334                                 Redraw (true);
335                                 Invalidate ();
336                         }
337                 }
338                 #endregion      // Private Methods
339
340                 #region Public Instance Methods
341                 public override string ToString() {
342                         return base.ToString ();
343                 }
344                 #endregion
345
346                 #region Events
347                 [Browsable(false)]
348                 [EditorBrowsable(EditorBrowsableState.Never)]
349                 public new event EventHandler CausesValidationChanged {
350                         add { base.CausesValidationChanged += value; }
351                         remove { base.CausesValidationChanged -= value; }
352                 }
353
354                 [Browsable(false)]
355                 [EditorBrowsable(EditorBrowsableState.Never)]
356                 public new event EventHandler Enter {
357                         add { base.Enter += value; }
358                         remove { base.Enter -= value; }
359                 }
360
361                 [Browsable(false)]
362                 [EditorBrowsable(EditorBrowsableState.Never)]
363                 public new event EventHandler FontChanged {
364                         add { base.FontChanged += value; }
365                         remove { base.FontChanged -= value; }
366                 }
367
368                 [Browsable(false)]
369                 [EditorBrowsable(EditorBrowsableState.Never)]
370                 public new event EventHandler ForeColorChanged {
371                         add { base.ForeColorChanged += value; }
372                         remove { base.ForeColorChanged -= value; }
373                 }
374
375                 [Browsable(false)]
376                 [EditorBrowsable(EditorBrowsableState.Never)]
377                 public new event EventHandler ImeModeChanged {
378                         add { base.ImeModeChanged += value; }
379                         remove { base.ImeModeChanged -= value; }
380                 }
381
382                 [Browsable(false)]
383                 [EditorBrowsable(EditorBrowsableState.Never)]
384                 public new event KeyEventHandler KeyDown {
385                         add { base.KeyDown += value; }
386                         remove { base.KeyDown -= value; }
387                 }
388
389                 [Browsable(false)]
390                 [EditorBrowsable(EditorBrowsableState.Never)]
391                 public new event KeyPressEventHandler KeyPress {
392                         add { base.KeyPress += value; }
393                         remove { base.KeyPress -= value; }
394                 }
395
396                 [Browsable(false)]
397                 [EditorBrowsable(EditorBrowsableState.Never)]
398                 public new event KeyEventHandler KeyUp {
399                         add { base.KeyUp += value; }
400                         remove { base.KeyUp -= value; }
401                 }
402
403                 [Browsable(false)]
404                 [EditorBrowsable(EditorBrowsableState.Never)]
405                 public new event EventHandler Leave {
406                         add { base.Leave += value; }
407                         remove { base.Leave -= value; }
408                 }
409
410                 [Browsable(false)]
411                 [EditorBrowsable(EditorBrowsableState.Never)]
412                 public new event EventHandler RightToLeftChanged {
413                         add { base.RightToLeftChanged += value; }
414                         remove { base.RightToLeftChanged -= value; }
415                 }
416
417                 [Browsable(false)]
418                 [EditorBrowsable(EditorBrowsableState.Never)]
419                 public new event EventHandler TabIndexChanged {
420                         add { base.TabIndexChanged += value; }
421                         remove { base.TabIndexChanged -= value; }
422                 }
423
424                 [Browsable(false)]
425                 [EditorBrowsable(EditorBrowsableState.Never)]
426                 public new event EventHandler TabStopChanged {
427                         add { base.TabStopChanged += value; }
428                         remove { base.TabStopChanged -= value; }
429                 }
430
431                 [Browsable(false)]
432                 [EditorBrowsable(EditorBrowsableState.Never)]
433                 public new event EventHandler TextChanged {
434                         add { base.TextChanged += value; }
435                         remove { base.TextChanged -= value; }
436                 }
437
438                 public event EventHandler SizeModeChanged;
439                 #endregion      // Events
440         }
441 }
442