소스 검색

Applying contributed patch from Stefan Noack.

2007-05-07  Jonathan Pobst  <[email protected]>
	
	* Control.cs: Add [Get|Set]AutoSizeMode.

2007-05-07  Jonathan Pobst  <[email protected]>

	* DefaultLayout.cs: Implement AutoSize logic.	

2007-05-07  Jonathan Pobst  <[email protected]>

	* ControlTest.cs: Tests demonstrating Control.AutoSize.

svn path=/trunk/mcs/; revision=76858
Jonathan Pobst 18 년 전
부모
커밋
9b356dff8a

+ 5 - 0
mcs/class/Managed.Windows.Forms/System.Windows.Forms.Layout/ChangeLog

@@ -1,3 +1,8 @@
+2007-05-07  Jonathan Pobst  <[email protected]>
+	Applying contributed patch from Stefan Noack.
+
+	* DefaultLayout.cs: Implement AutoSize logic.	
+
 2007-04-24  Andreia Gaita  <[email protected]>
 
 	* TableLayoutSettingsTypeConverter.cs: Implemented the converters

+ 55 - 6
mcs/class/Managed.Windows.Forms/System.Windows.Forms.Layout/DefaultLayout.cs

@@ -24,6 +24,7 @@
 //
 // Authors:
 //	Jonathan Pobst ([email protected])
+//	Stefan Noack ([email protected])
 //
 
 using System;
@@ -111,14 +112,8 @@ namespace System.Windows.Forms.Layout
 				left = child.Left;
 				top = child.Top;
 				
-#if NET_2_0
-				Size preferredsize = child.PreferredSize;
-				width = preferredsize.Width;
-				height = preferredsize.Height;
-#else
 				width = child.Width;
 				height = child.Height;
-#endif
 
 				if ((anchor & AnchorStyles.Right) != 0) {
 					if ((anchor & AnchorStyles.Left) != 0)
@@ -154,6 +149,57 @@ namespace System.Windows.Forms.Layout
 				child.SetBounds (left, top, width, height, BoundsSpecified.None);
 			}
 		}
+		
+#if NET_2_0
+		void LayoutAutoSizedChildren (Control parent, Control[] controls)
+		{
+			for (int i = 0; i < controls.Length; i++) {
+				int left;
+				int top;
+				int width;
+				int height;
+
+				Control child = controls[i];
+				if (!child.VisibleInternal
+				    || child.ControlLayoutType == Control.LayoutType.Dock
+				    || !child.AutoSize)
+					continue;
+
+				left = child.Left;
+				top = child.Top;
+				
+				Size preferredsize = child.PreferredSize;
+				
+				if (child.GetAutoSizeMode () == AutoSizeMode.GrowAndShrink) {
+					width = preferredsize.Width;
+					height = preferredsize.Height;
+				} else {
+					width = child.Width;
+					height = child.Height;
+					if (preferredsize.Width > width)
+						width = preferredsize.Width;
+					if (preferredsize.Height > height)
+						height = preferredsize.Height;
+						
+				}
+			
+				// Sanity
+				if (width < child.MinimumSize.Width)
+					width = child.MinimumSize.Width;
+
+				if (height < child.MinimumSize.Height)
+					height = child.MinimumSize.Height;
+				
+				if (child.MaximumSize.Width != 0 && width > child.MaximumSize.Width)
+					width = child.MaximumSize.Width;
+
+				if (child.MaximumSize.Height != 0 && height > child.MaximumSize.Height)
+					height = child.MaximumSize.Height;
+
+				child.SetBounds (left, top, width, height, BoundsSpecified.None);
+			}
+		}
+#endif
 
 		public override bool Layout (object container, LayoutEventArgs args)
 		{
@@ -163,6 +209,9 @@ namespace System.Windows.Forms.Layout
 
 			LayoutDockedChildren (parent, controls);
 			LayoutAnchoredChildren (parent, controls);
+#if NET_2_0
+			LayoutAutoSizedChildren (parent, controls);
+#endif
 
 			return false;
 		}

+ 5 - 0
mcs/class/Managed.Windows.Forms/System.Windows.Forms/ChangeLog

@@ -1,3 +1,8 @@
+2007-05-07  Jonathan Pobst  <[email protected]>
+	Applying contributed patch from Stefan Noack.
+	
+	* Control.cs: Add [Get|Set]AutoSizeMode.
+
 2007-05-07  Jonathan Pobst  <[email protected]>
 
 	* MdiClient.cs: Unmerge menus when the last child is closed.

+ 24 - 3
mcs/class/Managed.Windows.Forms/System.Windows.Forms/Control.cs

@@ -148,6 +148,7 @@ namespace System.Windows.Forms
 		Padding margin;
 		private ContextMenuStrip context_menu_strip;
 		Point auto_scroll_offset;
+		private AutoSizeMode auto_size_mode;
 #endif
 
 		#endregion	// Local Variables
@@ -998,6 +999,7 @@ namespace System.Windows.Forms
 			maximum_size = new Size();
 			minimum_size = new Size();
 			margin = this.DefaultMargin;
+			auto_size_mode = AutoSizeMode.GrowOnly;
 #endif
 
 			control_style = ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint | 
@@ -1794,6 +1796,13 @@ namespace System.Windows.Forms
 			return CreateParams;
 		}
 
+#if NET_2_0
+		internal virtual Size GetPreferredSizeCore (Size proposedSize)
+		{
+			return this.explicit_bounds.Size;
+		}
+#endif
+
 		private void UpdateDistances() {
 			if (parent != null) {
 				if (bounds.Width >= 0)
@@ -2053,7 +2062,6 @@ namespace System.Windows.Forms
 		[Browsable (false)]
 		[EditorBrowsable (EditorBrowsableState.Never)]
 		[DefaultValue (false)]
-		[MonoTODO("This method currently does nothing")]
 		public virtual bool AutoSize {
 			get { return auto_size; }
 			set {
@@ -2490,7 +2498,8 @@ namespace System.Windows.Forms
 				anchor_style = AnchorStyles.Top | AnchorStyles.Left;
 
 				if (dock_style == DockStyle.None) {
-					Bounds = explicit_bounds;
+					bounds = explicit_bounds;
+					layout_type = LayoutType.Anchor;
 				}
 
 				if (parent != null) {
@@ -3526,7 +3535,7 @@ namespace System.Windows.Forms
 #if NET_2_0
 		[EditorBrowsable (EditorBrowsableState.Advanced)]
 		public virtual Size GetPreferredSize (Size proposedSize) {
-			Size retsize = this.explicit_bounds.Size;
+			Size retsize = GetPreferredSizeCore (proposedSize);
 			
 			// If we're bigger than the MaximumSize, fix that
 			if (this.maximum_size.Width != 0 && retsize.Width > this.maximum_size.Width)
@@ -3973,6 +3982,11 @@ namespace System.Windows.Forms
 			// XXX need to implement this.
 			return null;
 		}
+		
+		protected internal AutoSizeMode GetAutoSizeMode () 
+		{
+			return auto_size_mode;
+		}
 #endif
 
 		protected internal bool GetStyle(ControlStyles flag) {
@@ -4308,6 +4322,13 @@ namespace System.Windows.Forms
 				container.ActiveControl = this;
 		}
 
+#if NET_2_0
+		protected void SetAutoSizeMode (AutoSizeMode mode)
+		{
+			auto_size_mode = mode;
+		}
+#endif
+		
 		[EditorBrowsable(EditorBrowsableState.Advanced)]
 		protected virtual void SetBoundsCore(int x, int y, int width, int height, BoundsSpecified specified) {
 			Rectangle old_explicit = explicit_bounds;

+ 4 - 0
mcs/class/Managed.Windows.Forms/Test/System.Windows.Forms/ChangeLog

@@ -1,3 +1,7 @@
+2007-05-07  Jonathan Pobst  <[email protected]>
+
+	* ControlTest.cs: Tests demonstrating Control.AutoSize from Stefan Noack.
+
 2007-05-07  Jonathan Pobst  <[email protected]>
 
 	* ToolStripManagerTest.cs: Add test for bug #81477.

+ 131 - 0
mcs/class/Managed.Windows.Forms/Test/System.Windows.Forms/ControlTest.cs

@@ -3,6 +3,7 @@
 //
 // Authors:
 //      Ritvik Mayank ([email protected])
+//		Stefan Noack ([email protected])
 //
 
 using System;
@@ -24,6 +25,136 @@ namespace MonoTests.System.Windows.Forms
 	[TestFixture]
 	public class ControlTest
 	{
+		
+#if NET_2_0
+		[Test]
+		public void AutoSizeTest ()
+		{
+			ControlAutoSizeTester c = new ControlAutoSizeTester (new Size (23, 17), AutoSizeMode.GrowAndShrink);
+			
+			Form f = new Form();
+			f.Size = new Size (200, 200);
+			c.Parent = f;
+			f.Show();
+			
+			Size s = new Size (42, 42);
+			c.Size = s;
+			
+			Point l = new Point (10, 10);
+			c.Location = l;
+			
+			//Check wether normal size setting is OK
+			Assert.AreEqual (s, c.Size, "#S1");
+			
+			//Check wether size remains without GetPreferredSize implemented even when AutoSize turned on.
+			c.AutoSize = true;
+			f.PerformLayout();
+			Assert.AreEqual (s, c.Size, "#S2");
+			
+			//Simulate a Control implementing GetPreferredSize
+			c.UseCustomPrefSize = true;
+			f.PerformLayout();
+			
+			//Check wether size shrinks to preferred size
+			Assert.AreEqual (c.CustomPrefSize, c.Size, "#S3");
+			//Check wether Location stays constant
+			Assert.AreEqual (l, c.Location, "#L1");
+			
+			//Check wether Dock is respected
+			c.Dock = DockStyle.Bottom;
+			Assert.AreEqual (f.ClientSize.Width, c.Width, "#D1");
+			
+			//Check wether size shrinks to preferred size again
+			c.Dock = DockStyle.None;
+			Assert.AreEqual (c.CustomPrefSize, c.Size, "#S4");
+			
+			//Check wether Anchor is respected for adjusting Locatioon
+			c.Anchor = AnchorStyles.Bottom;
+			f.Height += 50;
+			Assert.AreEqual (l.Y + 50, c.Top, "#A1");
+			//Check wether size is still OK
+			Assert.AreEqual (c.CustomPrefSize, c.Size, "#S5");
+			
+			
+			//just tidy up
+			c.Anchor = AnchorStyles.Top | AnchorStyles.Left;
+			c.Location = l;
+			
+			//Check wether shrinking to zero is possible 
+			c.CustomPrefSize = new Size (0, 0);
+			f.PerformLayout();
+			Assert.AreEqual (c.CustomPrefSize, c.Size, "#S6");
+			
+			//Check wether MinimumSize is honored
+			c.MinimumSize = new Size (10, 12);
+			c.CustomPrefSize = new Size (5, 5);
+			f.PerformLayout();
+			Assert.AreEqual (c.MinimumSize, c.Size, "#S7");
+			c.MinimumSize = new Size (0, 0);
+			
+			//Check wether MaximumSize is honored
+			c.MaximumSize = new Size (100, 120); 
+			c.CustomPrefSize = new Size (500, 500);
+			f.PerformLayout();
+			Assert.AreEqual (c.MaximumSize, c.Size, "#S8");
+			
+			//Check wether shrinking does not happen when GrowOnly
+			c.AutoSize = false;
+			s = new Size (23, 23);
+			c.Size = s;
+			c.CustomPrefSize = new Size (5, 5);
+			c.AutoSizeMode = AutoSizeMode.GrowOnly;
+			c.AutoSize = true;
+			f.PerformLayout();
+			Assert.AreEqual (s, c.Size, "#S9");
+		}
+		
+		public class ControlAutoSizeTester : Control {
+			
+
+			public ControlAutoSizeTester (Size customPrefSize, AutoSizeMode autoSizeMode)
+			{
+				custom_prefsize = customPrefSize;
+				AutoSizeMode = autoSizeMode;
+			}
+			
+			public AutoSizeMode AutoSizeMode {
+				set {
+					base.SetAutoSizeMode (value);
+				}
+			}
+
+			private bool use_custom_prefsize = false;
+			public bool UseCustomPrefSize {
+				set {
+					use_custom_prefsize = value;
+				}
+			}
+			
+			private Size custom_prefsize;
+			
+			public Size CustomPrefSize {
+				get {
+					return custom_prefsize;
+				}
+				set {
+					custom_prefsize = value;
+				}
+			}
+			
+			
+			public override Size GetPreferredSize (Size proposedSize)
+			{
+				if (use_custom_prefsize) {
+					return custom_prefsize;
+				} else {	
+					return base.GetPreferredSize(proposedSize);
+				}
+			}
+			
+		}
+#endif
+		
 		[Test]
 		public void InvokeTestParentHandle ()
 		{