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:
9 // The above copyright notice and this permission notice shall be
10 // included in all copies or substantial portions of the Software.
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.
20 // Copyright (c) 2004-2005 Novell, Inc.
23 // Jackson Harper (jackson@ximian.com)
29 using System.ComponentModel;
30 using System.ComponentModel.Design;
32 using System.Drawing.Imaging;
33 using System.Runtime.InteropServices;
35 namespace System.Windows.Forms {
36 [DefaultProperty("Image")]
37 [Designer("System.Windows.Forms.Design.PictureBoxDesigner, " + Consts.AssemblySystem_Design, "System.ComponentModel.Design.IDesigner")]
39 [Docking (DockingBehavior.Ask)]
40 [ClassInterface (ClassInterfaceType.AutoDispatch)]
42 [DefaultBindingProperty ("Image")]
44 public class PictureBox : Control
51 private PictureBoxSizeMode size_mode;
52 //private bool recalc;
54 private Image initial_image;
56 private int no_update;
59 private EventHandler frame_handler;
61 #region Public Constructor
66 SetStyle (ControlStyles.OptimizedDoubleBuffer, true);
68 SetStyle (ControlStyles.DoubleBuffer, true);
70 SetStyle (ControlStyles.Opaque, false);
71 SetStyle (ControlStyles.Selectable, false);
72 SetStyle (ControlStyles.SupportsTransparentBackColor, true);
73 HandleCreated += new EventHandler(PictureBox_HandleCreated);
75 #endregion // Public Constructor
77 #region Public Properties
78 [DefaultValue(PictureBoxSizeMode.Normal)]
80 [RefreshProperties(RefreshProperties.Repaint)]
81 public PictureBoxSizeMode SizeMode {
82 get { return size_mode; }
84 if (size_mode == value)
93 OnSizeModeChanged (EventArgs.Empty);
104 get { return image; }
110 if (IsHandleCreated) {
112 if (image != null && ImageAnimator.CanAnimate (image)) {
113 frame_handler = new EventHandler (OnAnimateImage);
114 ImageAnimator.Animate (image, frame_handler);
116 if (no_update == 0) {
124 [DefaultValue(BorderStyle.None)]
126 public BorderStyle BorderStyle {
127 get { return InternalBorderStyle; }
128 set { InternalBorderStyle = value; }
132 [EditorBrowsable(EditorBrowsableState.Never)]
133 public new bool CausesValidation {
134 get { return base.CausesValidation; }
135 set { base.CausesValidation = value; }
139 [RefreshProperties (RefreshProperties.All)]
141 public Image InitialImage {
142 get { return initial_image; }
143 set { initial_image = value; }
148 [EditorBrowsable(EditorBrowsableState.Never)]
149 public new ImeMode ImeMode {
150 get { return base.ImeMode; }
151 set { base.ImeMode = value; }
155 [EditorBrowsable(EditorBrowsableState.Never)]
156 public override RightToLeft RightToLeft {
157 get { return base.RightToLeft; }
158 set { base.RightToLeft = value; }
162 [EditorBrowsable(EditorBrowsableState.Never)]
163 public new int TabIndex {
164 get { return base.TabIndex; }
165 set { base.TabIndex = value; }
169 [EditorBrowsable(EditorBrowsableState.Never)]
170 public new bool TabStop {
171 get { return base.TabStop; }
172 set { base.TabStop = value; }
177 [EditorBrowsable(EditorBrowsableState.Never)]
178 public override string Text {
179 get { return base.Text; }
180 set { base.Text = value; }
183 protected override CreateParams CreateParams {
185 return base.CreateParams;
189 protected override ImeMode DefaultImeMode {
190 get { return base.DefaultImeMode; }
194 [EditorBrowsable(EditorBrowsableState.Never)]
195 public override Font Font {
196 get { return base.Font; }
197 set { base.Font = value; }
201 [EditorBrowsable(EditorBrowsableState.Never)]
202 public override Color ForeColor {
203 get { return base.ForeColor; }
204 set { base.ForeColor = value; }
208 [EditorBrowsable(EditorBrowsableState.Never)]
209 public override bool AllowDrop {
210 get { return base.AllowDrop; }
211 set { base.AllowDrop = value; }
213 #endregion // Public Properties
215 #region Protected Instance Methods
216 protected override Size DefaultSize {
217 get { return ThemeEngine.Current.PictureBoxDefaultSize; }
220 protected override void Dispose (bool disposing)
227 initial_image = null;
230 base.Dispose (disposing);
233 protected override void OnPaint (PaintEventArgs pe)
235 ThemeEngine.Current.DrawPictureBox (pe.Graphics, pe.ClipRectangle, this);
239 protected override void OnVisibleChanged (EventArgs e)
241 base.OnVisibleChanged (e);
245 protected virtual void OnSizeModeChanged (EventArgs e)
247 EventHandler eh = (EventHandler)(Events [SizeModeChangedEvent]);
252 protected override void OnEnabledChanged (EventArgs e)
254 base.OnEnabledChanged (e);
257 protected override void OnParentChanged (EventArgs e)
259 base.OnParentChanged (e);
262 protected override void OnResize (EventArgs e)
266 if (size_mode == PictureBoxSizeMode.CenterImage || size_mode == PictureBoxSizeMode.StretchImage)
269 if (size_mode == PictureBoxSizeMode.Zoom)
274 protected override void SetBoundsCore (int x, int y, int width, int height, BoundsSpecified specified)
276 if (size_mode == PictureBoxSizeMode.AutoSize && image != null) {
278 height = image.Height;
280 base.SetBoundsCore (x, y, width, height, specified);
282 #endregion // Protected Instance Methods
285 #region ISupportInitialize Interface
286 void System.ComponentModel.ISupportInitialize.BeginInit() {
290 void System.ComponentModel.ISupportInitialize.EndInit() {
294 if (no_update == 0) {
299 #endregion // ISupportInitialize Interface
302 #region Private Methods
303 private void StopAnimation ()
305 if (frame_handler == null)
307 ImageAnimator.StopAnimate (image, frame_handler);
308 frame_handler = null;
311 private void UpdateSize ()
315 if (size_mode == PictureBoxSizeMode.AutoSize)
316 ClientSize = image.Size;
319 private void Redraw (bool recalc)
321 //this.recalc = recalc;
324 private void OnAnimateImage (object sender, EventArgs e)
326 // This is called from a worker thread,BeginInvoke is used
327 // so the control is updated from the correct thread
328 BeginInvoke (new EventHandler (UpdateAnimatedImage), new object [] { this, e });
331 private void UpdateAnimatedImage (object sender, EventArgs e)
333 ImageAnimator.UpdateFrames (image);
338 private void PictureBox_HandleCreated(object sender, EventArgs e) {
340 if (image != null && ImageAnimator.CanAnimate (image)) {
341 frame_handler = new EventHandler (OnAnimateImage);
342 ImageAnimator.Animate (image, frame_handler);
344 if (no_update == 0) {
349 #endregion // Private Methods
351 #region Public Instance Methods
352 public override string ToString() {
353 return String.Format("{0}, SizeMode: {1}", base.ToString (), SizeMode);
359 [EditorBrowsable(EditorBrowsableState.Never)]
360 public new event EventHandler CausesValidationChanged {
361 add { base.CausesValidationChanged += value; }
362 remove { base.CausesValidationChanged -= value; }
366 [EditorBrowsable(EditorBrowsableState.Never)]
367 public new event EventHandler Enter {
368 add { base.Enter += value; }
369 remove { base.Enter -= value; }
373 [EditorBrowsable(EditorBrowsableState.Never)]
374 public new event EventHandler FontChanged {
375 add { base.FontChanged += value; }
376 remove { base.FontChanged -= value; }
380 [EditorBrowsable(EditorBrowsableState.Never)]
381 public new event EventHandler ForeColorChanged {
382 add { base.ForeColorChanged += value; }
383 remove { base.ForeColorChanged -= value; }
387 [EditorBrowsable(EditorBrowsableState.Never)]
388 public new event EventHandler ImeModeChanged {
389 add { base.ImeModeChanged += value; }
390 remove { base.ImeModeChanged -= value; }
394 [EditorBrowsable(EditorBrowsableState.Never)]
395 public new event KeyEventHandler KeyDown {
396 add { base.KeyDown += value; }
397 remove { base.KeyDown -= value; }
401 [EditorBrowsable(EditorBrowsableState.Never)]
402 public new event KeyPressEventHandler KeyPress {
403 add { base.KeyPress += value; }
404 remove { base.KeyPress -= value; }
408 [EditorBrowsable(EditorBrowsableState.Never)]
409 public new event KeyEventHandler KeyUp {
410 add { base.KeyUp += value; }
411 remove { base.KeyUp -= value; }
415 [EditorBrowsable(EditorBrowsableState.Never)]
416 public new event EventHandler Leave {
417 add { base.Leave += value; }
418 remove { base.Leave -= value; }
422 [EditorBrowsable(EditorBrowsableState.Never)]
423 public new event EventHandler RightToLeftChanged {
424 add { base.RightToLeftChanged += value; }
425 remove { base.RightToLeftChanged -= value; }
429 [EditorBrowsable(EditorBrowsableState.Never)]
430 public new event EventHandler TabIndexChanged {
431 add { base.TabIndexChanged += value; }
432 remove { base.TabIndexChanged -= value; }
436 [EditorBrowsable(EditorBrowsableState.Never)]
437 public new event EventHandler TabStopChanged {
438 add { base.TabStopChanged += value; }
439 remove { base.TabStopChanged -= value; }
443 [EditorBrowsable(EditorBrowsableState.Never)]
444 public new event EventHandler TextChanged {
445 add { base.TextChanged += value; }
446 remove { base.TextChanged -= value; }
449 static object SizeModeChangedEvent = new object ();
450 public event EventHandler SizeModeChanged {
451 add { Events.AddHandler (SizeModeChangedEvent, value); }
452 remove { Events.RemoveHandler (SizeModeChangedEvent, value); }