* X11Keyboard.cs: Detect and use the num lock mask.
[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 Novell, Inc.
21 //
22 // Authors:
23 //      Jackson Harper (jackson@ximian.com)
24 //
25
26
27 using System;
28 using System.Drawing;
29 using System.Drawing.Imaging;
30
31 namespace System.Windows.Forms {
32
33         public class PictureBox : Control {
34
35                 private Image image;
36                 private PictureBoxSizeMode size_mode;
37                 private BorderStyle border_style;
38                 private bool redraw;
39                 private bool recalc;
40
41                 private EventHandler frame_handler;
42
43                 public PictureBox ()
44                 {
45                         redraw = true;
46                         recalc = true;
47                 }
48
49                 public PictureBoxSizeMode SizeMode {
50                         get { return size_mode; }
51                         set {
52                                 if (size_mode == value)
53                                         return;
54                                 size_mode = value;
55                                 UpdateSize ();
56                                 Redraw (true);
57                                 Invalidate ();
58                         }
59                 }
60
61                 public Image Image {
62                         get { return image; }
63                         set {
64                                 StopAnimation ();
65
66                                 image = value;
67                                 UpdateSize ();
68                                 if (image != null && ImageAnimator.CanAnimate (image)) {
69                                         frame_handler = new EventHandler (OnAnimateImage);
70                                         ImageAnimator.Animate (image, frame_handler);
71                                 }
72                                 Redraw (true);
73                                 Invalidate ();
74                         }
75                 }
76
77                 public BorderStyle BorderStyle {
78                         get { return border_style; }
79                         set {
80                                 border_style = value;
81                                 Redraw (true);
82                         }
83                 }
84
85                 public new bool CausesValidation {
86                         get { return base.CausesValidation; }
87                         set { base.CausesValidation = value; }
88                 }
89
90                 public new ImeMode ImeMode {
91                         get { return base.ImeMode; }
92                         set { base.ImeMode = value; }
93                 }
94
95                 public override RightToLeft RightToLeft {
96                         get { return base.RightToLeft; }
97                         set { base.RightToLeft = value; }
98                 }
99
100                 public new int TabIndex {
101                         get { return base.TabIndex; }
102                         set { base.TabIndex = value; }
103                 }
104
105                 public new bool TabStop {
106                         get { return base.TabStop; }
107                         set { base.TabStop = value; }
108                 }
109
110                 public override string Text {
111                         get { return base.Text; }
112                         set { base.Text = value; }
113                 }
114
115                 protected override CreateParams CreateParams {
116                         get {
117                                 return base.CreateParams;
118                         }
119                 }
120
121                 protected override ImeMode DefaultImeMode {
122                         get { return base.DefaultImeMode; }
123                 }
124
125                 public override Font Font {
126                         get { return base.Font; }
127                         set { base.Font = value; }
128                 }
129
130                 public override Color ForeColor {
131                         get { return base.ForeColor; }
132                         set { base.ForeColor = value; }
133                 }
134
135                 protected override Size DefaultSize {
136                         get { return ThemeEngine.Current.PictureBoxDefaultSize; }
137                 }
138
139                 protected override void Dispose (bool disposing)
140                 {
141                         if (image != null) {
142                                 StopAnimation ();
143                                 image.Dispose ();
144                                 image = null;
145                         }
146                         base.Dispose (disposing);
147                 }
148
149                 protected override void OnPaint (PaintEventArgs pe)
150                 {
151                         if (this.Width <= 0 || this.Height <=  0 || this.Visible == false)
152                                 return;
153
154                         Draw ();
155                         pe.Graphics.DrawImage (this.ImageBuffer, pe.ClipRectangle, pe.ClipRectangle, GraphicsUnit.Pixel);
156                 }
157
158                 protected override void SetBoundsCore (int x, int y, int width, int height, BoundsSpecified specified)
159                 {
160                         if (size_mode == PictureBoxSizeMode.AutoSize && image != null) {
161                                 width = image.Width;
162                                 height = image.Height;
163                         }
164                         base.SetBoundsCore (x, y, width, height, specified);
165                 }
166
167                 private void StopAnimation ()
168                 {
169                         if (frame_handler == null)
170                                 return;
171                         ImageAnimator.StopAnimate (image, frame_handler);
172                         frame_handler = null;
173                 }
174
175                 private void UpdateSize ()
176                 {
177                         if (image == null)
178                                 return;
179                         if (size_mode == PictureBoxSizeMode.AutoSize)
180                                 ClientSize = image.Size; 
181                 }
182
183                 private void Redraw (bool recalc)
184                 {
185                         redraw = true;
186                         this.recalc = recalc;
187                 }
188
189                 private void OnAnimateImage (object sender, EventArgs e)
190                 {
191                         // This is called from a worker thread,BeginInvoke is used
192                         // so the control is updated from the correct thread
193                         BeginInvoke (new EventHandler (UpdateAnimatedImage), new object [] { this, e });
194                 }
195
196                 private void UpdateAnimatedImage (object sender, EventArgs e)
197                 {
198                         ImageAnimator.UpdateFrames (image);
199                         Redraw (false);
200                         Refresh ();
201                 }
202
203                 [MonoTODO ("Borders and stuff, and move into the Theme")]
204                 private void Draw ()
205                 {
206                         if (redraw) {
207                                 ThemeEngine.Current.DrawPictureBox (DeviceContext, this);
208                         }
209                         redraw = false;
210                 }
211         }
212 }
213