This commit was manufactured by cvs2svn to create branch 'mono-1-0'.
[mono.git] / mcs / class / System.Windows.Forms / System.Windows.Forms / PictureBox.cs
index a299946f7203f01fa8ffe2b2279c10a6c97e9c8f..8c067f97cefa0d7df66d6de48e0aec93f1a30d0b 100644 (file)
 // Author:
 //   stubbed out by Hossein Safavi (hsafavi@purdue.edu)
 //     Dennis Hayes (dennish@raytek.com)
+//   Aleksey Ryabchuk (ryabchuk@yahoo.com)
 //
 // (C) 2002 Ximian, Inc
 //
 
+//
+// Permission is hereby granted, free of charge, to any person obtaining
+// a copy of this software and associated documentation files (the
+// "Software"), to deal in the Software without restriction, including
+// without limitation the rights to use, copy, modify, merge, publish,
+// distribute, sublicense, and/or sell copies of the Software, and to
+// permit persons to whom the Software is furnished to do so, subject to
+// the following conditions:
+// 
+// The above copyright notice and this permission notice shall be
+// included in all copies or substantial portions of the Software.
+// 
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+//
+using System.Drawing;
+using System.ComponentModel;
+
 namespace System.Windows.Forms {
 
        //<summary>
        //</summary>
-       using System.Drawing;
-       using System.ComponentModel;
        public class PictureBox : Control {
 
                Image image;
                PictureBoxSizeMode sizeMode;
-               //
-               // --- Public Constructor
-               //
-               [MonoTODO]
+               BorderStyle borderStyle;
+
                public PictureBox () 
                {
                        image = null;
                        sizeMode = PictureBoxSizeMode.Normal;
+                       borderStyle = BorderStyle.None;
+                       SetStyle ( ControlStyles.UserPaint, true );
+                       SetStyle ( ControlStyles.Selectable, false );
                }
-               //
-               // --- Public Properties
-               //
 
-               [MonoTODO]
-               public Image Image {
-                       get {
-                               return image;
+               public BorderStyle BorderStyle {
+                       get {   return borderStyle; }
+                       set {
+                               if ( !Enum.IsDefined ( typeof(BorderStyle), value ) )
+                                       throw new InvalidEnumArgumentException( "BorderStyle",
+                                               (int)value,
+                                               typeof(BorderStyle));
+                               
+                               if ( borderStyle != value ) {
+                                       borderStyle = value;
+                                       RecreateHandle ( );
+                               }
                        }
+               }
+
+
+               public Image Image {
+                       get { return image; }
                        set {
                                image = value;
+
+                               if ( sizeMode == PictureBoxSizeMode.AutoSize && image != null )
+                                       SetBounds ( 0, 0, 0, 0, BoundsSpecified.None );
+
+                               Invalidate ( );
                        }
                }
 
-               [MonoTODO]
+               [EditorBrowsable (EditorBrowsableState.Never)]   
                public new ImeMode ImeMode {
-                       get {
-                               return base.ImeMode;
-                       }
-                       set {
-                               base.ImeMode = value;
-                       }
+                       get { return base.ImeMode; }
+                       set { base.ImeMode = value;}
                }
-               //
-               // --- Protected Properties
-               //
-               [MonoTODO]
+
                protected override CreateParams CreateParams {
                        get {
-                               CreateParams createParams = new CreateParams ();
-                               window = new ControlNativeWindow (this);
-
-                               createParams.Caption = Text;
-                               createParams.ClassName = "PICTUREBOX";
-                               createParams.X = Left;
-                               createParams.Y = Top;
-                               createParams.Width = Width;
-                               createParams.Height = Height;
-                               createParams.ClassStyle = 0;
-                               createParams.ExStyle = 0;
-                               createParams.Param = 0;
-                               //                      createParams.Parent = Parent.Handle;
-                               createParams.Style = (int) (
+                               RegisterDefaultWindowClass ( );
+
+                               CreateParams createParams = base.CreateParams;
+
+                               createParams.ClassName = Win32.DEFAULT_WINDOW_CLASS;
+
+                               createParams.Style |= (int) (
                                        WindowStyles.WS_CHILD | 
-                                       WindowStyles.WS_VISIBLE);
-                               window.CreateHandle (createParams);
+                                       WindowStyles.WS_VISIBLE |
+                                       WindowStyles.WS_CLIPCHILDREN |
+                                       WindowStyles.WS_CLIPSIBLINGS );
+
+                               switch ( BorderStyle ) {
+                                       case BorderStyle.Fixed3D:
+                                               createParams.ExStyle |= (int)WindowExStyles.WS_EX_CLIENTEDGE;
+                                               break;
+                                       case BorderStyle.FixedSingle:
+                                               createParams.Style |= (int) WindowStyles.WS_BORDER;
+                                               break;
+                               };
                                return createParams;
                        }               
                }
-               [MonoTODO]
+
                protected override ImeMode DefaultImeMode {
-                       get {
-                               return base.DefaultImeMode;
-                       }
+                       get { return ImeMode.Disable; }
                }
-               [MonoTODO]
+
                protected override Size DefaultSize {
-                       get {
-                               return new Size(100,50);
-                       }
+                       get { return new Size(100,50); }
                }
-               //
-               // --- Public Methods
-               //
-               [MonoTODO]
+
                public override string ToString()
                {
-                       //FIXME:
-                       return base.ToString();
+                       return GetType( ).FullName.ToString ( ) + ", SizeMode: " + SizeMode.ToString ( );
                }
 
-               [MonoTODO]
-               public PictureBoxSizeMode SizeMode () {
-                       return sizeMode;
+               public PictureBoxSizeMode SizeMode {
+                       get { return sizeMode; }
+                       set {   
+                               if ( !Enum.IsDefined ( typeof(PictureBoxSizeMode), value ) )
+                                       throw new InvalidEnumArgumentException( "SizeMode",
+                                               (int)value,
+                                               typeof( PictureBoxSizeMode ) );
+
+                               if ( sizeMode != value ) {
+                                       sizeMode = value;
+
+                                       if ( sizeMode == PictureBoxSizeMode.AutoSize )
+                                               SetBounds ( 0, 0, 0, 0, BoundsSpecified.None );
+                                       
+                                       SetStyle ( ControlStyles.AllPaintingInWmPaint, sizeMode == PictureBoxSizeMode.StretchImage );
+
+                                       Invalidate ( );
+                                       OnSizeModeChanged ( EventArgs.Empty );
+                               }
+                       }
                }
+
                
-               //
-               // --- Protected Methods
-               //
+               [MonoTODO]
+               protected override void Dispose(bool disposing) { 
+                       base.Dispose(disposing);
+               }               
 
                [MonoTODO]
                protected override void OnEnabledChanged(EventArgs e) 
@@ -113,45 +158,77 @@ namespace System.Windows.Forms {
                        //FIXME:
                        base.OnEnabledChanged(e);
                }
-               [MonoTODO]
-               protected override void OnPaint(PaintEventArgs e) 
+
+               protected override void OnPaint(PaintEventArgs pe) 
                {
-                       //FIXME:
-                       base.OnPaint(e);
+                       if ( Image != null ) {
+                               switch ( SizeMode ) {
+                               case PictureBoxSizeMode.StretchImage:
+                                       pe.Graphics.DrawImage ( Image, ClientRectangle );
+                               break;
+                               case PictureBoxSizeMode.CenterImage:
+                                       int dx = (ClientRectangle.Width - Image.Width)/2;
+                                       int dy = (ClientRectangle.Height- Image.Height)/2;
+                                       pe.Graphics.DrawImage ( Image, dx, dy );
+                               break;
+                               default:
+                                       pe.Graphics.DrawImage ( Image, 0, 0 );
+                               break;
+                               }
+                       }
+                       base.OnPaint(pe);
                }
-               [MonoTODO]
+
                protected override void OnParentChanged(EventArgs e) 
                {
-                       //FIXME:
+                       if ( Parent != null ) {
+                               BackColor = Parent.BackColor;
+                               Invalidate ( );
+                       }
+                               
                        base.OnParentChanged(e);
                }
-               [MonoTODO]
+
                protected override void OnResize(EventArgs e) 
                {
-                       //FIXME:
+                       if ( SizeMode == PictureBoxSizeMode.CenterImage )
+                               Invalidate ( );
+                       else if ( SizeMode == PictureBoxSizeMode.StretchImage && IsHandleCreated)
+                               Win32.InvalidateRect ( Handle, IntPtr.Zero, 0 );
+
                        base.OnResize(e);
                }
-               [MonoTODO]
+
                protected virtual void OnSizeModeChanged(EventArgs e)
                {
-                       //FIXME:
+                       if ( SizeModeChanged != null )
+                               SizeModeChanged ( this, e );
                }
+
                [MonoTODO]
                protected override void OnVisibleChanged(EventArgs e) 
                {
-                       //FIXME:
+                       base.OnVisibleChanged ( e );
                }
+
                [MonoTODO]
+               //this should be inherited.
+               protected override void OnPaintBackground (PaintEventArgs e) {
+                       if ( SizeMode != PictureBoxSizeMode.StretchImage ) 
+                               base.OnPaintBackground ( e );
+               }
+
                protected override void SetBoundsCore(int x,int y,int width,int height,BoundsSpecified specified) 
                {
-                       //FIXME:
+                       if ( SizeMode == PictureBoxSizeMode.AutoSize && Image != null ) {
+                               width = Image.Width;
+                               height= Image.Height;
+                               specified = BoundsSpecified.Size;
+                       }
+                               
                        base.SetBoundsCore(x, y, width, height, specified);
                }
-               //
-               // --- Public Events
-               //
 
-               [MonoTODO]
                public event EventHandler SizeModeChanged;
        }
 }