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")]
38 public class PictureBox : Control
45 private PictureBoxSizeMode size_mode;
48 private bool allow_drop;
49 private Image initial_image;
50 private int no_update;
53 private EventHandler frame_handler;
55 #region Public Constructor
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);
67 #endregion // Public Constructor
69 #region Public Properties
70 [DefaultValue(PictureBoxSizeMode.Normal)]
72 [RefreshProperties(RefreshProperties.Repaint)]
73 public PictureBoxSizeMode SizeMode {
74 get { return size_mode; }
76 if (size_mode == value)
85 OnSizeModeChanged (EventArgs.Empty);
98 if (IsHandleCreated) {
100 if (image != null && ImageAnimator.CanAnimate (image)) {
101 frame_handler = new EventHandler (OnAnimateImage);
102 ImageAnimator.Animate (image, frame_handler);
104 if (no_update == 0) {
112 [DefaultValue(BorderStyle.None)]
114 public BorderStyle BorderStyle {
115 get { return InternalBorderStyle; }
116 set { InternalBorderStyle = value; }
120 [EditorBrowsable(EditorBrowsableState.Never)]
121 public new bool CausesValidation {
122 get { return base.CausesValidation; }
123 set { base.CausesValidation = value; }
129 public Image InitialImage {
130 get { return initial_image; }
131 set { initial_image = value; }
136 [EditorBrowsable(EditorBrowsableState.Never)]
137 public new ImeMode ImeMode {
138 get { return base.ImeMode; }
139 set { base.ImeMode = value; }
143 [EditorBrowsable(EditorBrowsableState.Never)]
144 public override RightToLeft RightToLeft {
145 get { return base.RightToLeft; }
146 set { base.RightToLeft = value; }
150 [EditorBrowsable(EditorBrowsableState.Never)]
151 public new int TabIndex {
152 get { return base.TabIndex; }
153 set { base.TabIndex = value; }
157 [EditorBrowsable(EditorBrowsableState.Never)]
158 public new bool TabStop {
159 get { return base.TabStop; }
160 set { base.TabStop = value; }
165 [EditorBrowsable(EditorBrowsableState.Never)]
166 public override string Text {
167 get { return base.Text; }
168 set { base.Text = value; }
171 protected override CreateParams CreateParams {
173 return base.CreateParams;
177 protected override ImeMode DefaultImeMode {
178 get { return base.DefaultImeMode; }
182 [EditorBrowsable(EditorBrowsableState.Never)]
183 public override Font Font {
184 get { return base.Font; }
185 set { base.Font = value; }
189 [EditorBrowsable(EditorBrowsableState.Never)]
190 public override Color ForeColor {
191 get { return base.ForeColor; }
192 set { base.ForeColor = value; }
196 [EditorBrowsable(EditorBrowsableState.Never)]
197 public override bool AllowDrop {
202 if (allow_drop != value) {
207 #endregion // Public Properties
209 #region Protected Instance Methods
210 protected override Size DefaultSize {
211 get { return ThemeEngine.Current.PictureBoxDefaultSize; }
214 protected override void Dispose (bool disposing)
220 initial_image = null;
222 base.Dispose (disposing);
225 protected override void OnPaint (PaintEventArgs pe)
227 ThemeEngine.Current.DrawPictureBox (pe.Graphics, pe.ClipRectangle, this);
231 protected override void OnVisibleChanged (EventArgs e)
233 base.OnVisibleChanged (e);
237 protected virtual void OnSizeModeChanged (EventArgs e)
239 if (SizeModeChanged != null)
240 SizeModeChanged (this, e);
243 protected override void OnEnabledChanged (EventArgs e)
245 base.OnEnabledChanged (e);
248 protected override void OnParentChanged (EventArgs e)
250 base.OnParentChanged (e);
253 protected override void OnResize (EventArgs e)
258 if (size_mode == PictureBoxSizeMode.CenterImage || size_mode == PictureBoxSizeMode.StretchImage)
262 protected override void SetBoundsCore (int x, int y, int width, int height, BoundsSpecified specified)
264 if (size_mode == PictureBoxSizeMode.AutoSize && image != null) {
266 height = image.Height;
268 base.SetBoundsCore (x, y, width, height, specified);
270 #endregion // Protected Instance Methods
273 #region ISupportInitialize Interface
274 void System.ComponentModel.ISupportInitialize.BeginInit() {
278 void System.ComponentModel.ISupportInitialize.EndInit() {
282 if (no_update == 0) {
287 #endregion // ISupportInitialize Interface
290 #region Private Methods
291 private void StopAnimation ()
293 if (frame_handler == null)
295 ImageAnimator.StopAnimate (image, frame_handler);
296 frame_handler = null;
299 private void UpdateSize ()
303 if (size_mode == PictureBoxSizeMode.AutoSize)
304 ClientSize = image.Size;
307 private void Redraw (bool recalc)
310 this.recalc = recalc;
313 private void OnAnimateImage (object sender, EventArgs e)
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 });
320 private void UpdateAnimatedImage (object sender, EventArgs e)
322 ImageAnimator.UpdateFrames (image);
327 private void PictureBox_HandleCreated(object sender, EventArgs e) {
329 if (image != null && ImageAnimator.CanAnimate (image)) {
330 frame_handler = new EventHandler (OnAnimateImage);
331 ImageAnimator.Animate (image, frame_handler);
333 if (no_update == 0) {
338 #endregion // Private Methods
340 #region Public Instance Methods
341 public override string ToString() {
342 return base.ToString ();
348 [EditorBrowsable(EditorBrowsableState.Never)]
349 public new event EventHandler CausesValidationChanged {
350 add { base.CausesValidationChanged += value; }
351 remove { base.CausesValidationChanged -= value; }
355 [EditorBrowsable(EditorBrowsableState.Never)]
356 public new event EventHandler Enter {
357 add { base.Enter += value; }
358 remove { base.Enter -= value; }
362 [EditorBrowsable(EditorBrowsableState.Never)]
363 public new event EventHandler FontChanged {
364 add { base.FontChanged += value; }
365 remove { base.FontChanged -= value; }
369 [EditorBrowsable(EditorBrowsableState.Never)]
370 public new event EventHandler ForeColorChanged {
371 add { base.ForeColorChanged += value; }
372 remove { base.ForeColorChanged -= value; }
376 [EditorBrowsable(EditorBrowsableState.Never)]
377 public new event EventHandler ImeModeChanged {
378 add { base.ImeModeChanged += value; }
379 remove { base.ImeModeChanged -= value; }
383 [EditorBrowsable(EditorBrowsableState.Never)]
384 public new event KeyEventHandler KeyDown {
385 add { base.KeyDown += value; }
386 remove { base.KeyDown -= value; }
390 [EditorBrowsable(EditorBrowsableState.Never)]
391 public new event KeyPressEventHandler KeyPress {
392 add { base.KeyPress += value; }
393 remove { base.KeyPress -= value; }
397 [EditorBrowsable(EditorBrowsableState.Never)]
398 public new event KeyEventHandler KeyUp {
399 add { base.KeyUp += value; }
400 remove { base.KeyUp -= value; }
404 [EditorBrowsable(EditorBrowsableState.Never)]
405 public new event EventHandler Leave {
406 add { base.Leave += value; }
407 remove { base.Leave -= value; }
411 [EditorBrowsable(EditorBrowsableState.Never)]
412 public new event EventHandler RightToLeftChanged {
413 add { base.RightToLeftChanged += value; }
414 remove { base.RightToLeftChanged -= value; }
418 [EditorBrowsable(EditorBrowsableState.Never)]
419 public new event EventHandler TabIndexChanged {
420 add { base.TabIndexChanged += value; }
421 remove { base.TabIndexChanged -= value; }
425 [EditorBrowsable(EditorBrowsableState.Never)]
426 public new event EventHandler TabStopChanged {
427 add { base.TabStopChanged += value; }
428 remove { base.TabStopChanged -= value; }
432 [EditorBrowsable(EditorBrowsableState.Never)]
433 public new event EventHandler TextChanged {
434 add { base.TextChanged += value; }
435 remove { base.TextChanged -= value; }
438 public event EventHandler SizeModeChanged;