2007-08-28 Jonathan Pobst <monkey@jpobst.com>
[mono.git] / mcs / class / Managed.Windows.Forms / System.Windows.Forms / FlowLayoutPanel.cs
index 45239455e731fc27b133df238f3703e19fdab3b2..4008d98d6633703934e851e88a3c7a723124e333 100644 (file)
@@ -30,6 +30,7 @@
 using System.Windows.Forms.Layout;
 using System.ComponentModel;
 using System.Runtime.InteropServices;
+using System.Drawing;
 
 namespace System.Windows.Forms
 {
@@ -38,6 +39,7 @@ namespace System.Windows.Forms
        [ProvideProperty ("FlowBreak", typeof (Control))]
        [DefaultProperty ("FlowDirection")]
        [Docking (DockingBehavior.Ask)]
+       [Designer ("System.Windows.Forms.Design.FlowLayoutPanelDesigner, " + Consts.AssemblySystem_Design, "System.ComponentModel.Design.IDesigner")]
        public class FlowLayoutPanel : Panel, IExtenderProvider
        {
                private FlowLayoutSettings settings;
@@ -101,6 +103,65 @@ namespace System.Windows.Forms
                        return false;
                }
                #endregion
+
+               #region Internal Methods
+               internal override Size GetPreferredSizeCore (Size proposedSize)
+               {
+                       int width = 0;
+                       int height = 0;
+                       bool horizontal = FlowDirection == FlowDirection.LeftToRight || FlowDirection == FlowDirection.RightToLeft;
+                       if (!WrapContents || (horizontal && proposedSize.Width == 0) || (!horizontal && proposedSize.Height == 0)) {
+                               foreach (Control control in Controls) {
+                                       Size control_preferred_size = control.PreferredSize;
+                                       Padding control_margin = control.Margin;
+                                       if (horizontal) {
+                                               width += control_preferred_size.Width + control_margin.Horizontal;
+                                               height = Math.Max (height, control_preferred_size.Height + control_margin.Vertical);
+                                       } else {
+                                               height += control_preferred_size.Height + control_margin.Vertical;
+                                               width = Math.Max (width, control_preferred_size.Width + control_margin.Horizontal);
+                                       }
+                               }
+                       } else {
+                               int size_in_flow_direction = 0;
+                               int size_in_other_direction = 0;
+                               int increase;
+                               foreach (Control control in Controls) {
+                                       Size control_preferred_size = control.PreferredSize;
+                                       Padding control_margin = control.Margin;
+                                       if (horizontal) {
+                                               increase = control_preferred_size.Width + control_margin.Horizontal;
+                                               if (size_in_flow_direction != 0 && size_in_flow_direction + increase >= proposedSize.Width) {
+                                                       width = Math.Max (width, size_in_flow_direction);
+                                                       size_in_flow_direction = 0;
+                                                       height += size_in_other_direction;
+                                                       size_in_other_direction = 0;
+                                               }
+                                               size_in_flow_direction += increase;
+                                               size_in_other_direction = Math.Max (size_in_other_direction, control_preferred_size.Height + control_margin.Vertical);
+                                       } else {
+                                               increase = control_preferred_size.Height + control_margin.Vertical;
+                                               if (size_in_flow_direction != 0 && size_in_flow_direction + increase >= proposedSize.Height) {
+                                                       height = Math.Max (height, size_in_flow_direction);
+                                                       size_in_flow_direction = 0;
+                                                       width += size_in_other_direction;
+                                                       size_in_other_direction = 0;
+                                               }
+                                               size_in_flow_direction += increase;
+                                               size_in_other_direction = Math.Max (size_in_other_direction, control_preferred_size.Width + control_margin.Horizontal);
+                                       }
+                               }
+                               if (horizontal) {
+                                       width = Math.Max (width, size_in_flow_direction);
+                                       height += size_in_other_direction;
+                               } else {
+                                       height = Math.Max (height, size_in_flow_direction);
+                                       width += size_in_other_direction;
+                               }
+                       }
+                       return new Size (width, height);
+               }
+               #endregion
        }
 }
 #endif