ui/wpf/UIcore/Container.cs

changeset 101
1c943d43fa81
parent 85
91f45354d1e2
child 104
3efe0210e27e
--- a/ui/wpf/UIcore/Container.cs	Sun Jan 17 19:19:28 2016 +0100
+++ b/ui/wpf/UIcore/Container.cs	Wed Jan 20 11:35:01 2016 +0100
@@ -18,10 +18,13 @@
     public class Layout
     {
         public bool? Fill { get; set; }
+        public bool Hexpand { get; set; }
+        public bool Vexpand { get; set; }
+        public bool NewLine { get; set; }
 
         public Layout()
         {
-            Fill = null;
+            Reset();
         }
 
         public bool IsFill(bool fill)
@@ -37,6 +40,9 @@
         public void Reset()
         {
             Fill = null;
+            Hexpand = false;
+            Vexpand = false;
+            NewLine = false;
         }
     }
 
@@ -146,20 +152,76 @@
         }
     }
     
-    public class GridContainer : Container
+    public class GridContainer : Grid, Container
     {
         public Layout Layout { get; set; }
-        
-        public Grid Grid;
+
+        private int X = 0;
+        private int Y = 0;
+        private int CurrentWidth = 0;
+        private int CurrentHeight = 0;
 
-        public GridContainer(System.Windows.Controls.Grid grid)
+        public GridContainer(Container parent, int margin, int colspacing, int rowspacing) : base()
         {
-            Grid = grid;
+            Layout = new Layout();
+
+            parent.Add(this, true);
         }
 
         public void Add(UIElement control, bool fill)
         {
+            if(Layout.NewLine)
+            {
+                X = 0;
+                Y++;
+            }
 
+            ColumnDefinition col;
+            RowDefinition row;
+            if(X >= CurrentWidth)
+            {
+                col = new ColumnDefinition();
+                col.Width = GridLength.Auto;
+                ColumnDefinitions.Add(col);
+                CurrentWidth = X + 1;
+            }
+            else
+            {
+                col = ColumnDefinitions.ElementAt(X);
+            }
+
+            if (Y >= CurrentHeight)
+            {
+                row = new RowDefinition();
+                row.Height = GridLength.Auto;
+                RowDefinitions.Add(row);
+                CurrentHeight = Y + 1;
+            }
+            else
+            {
+                row = RowDefinitions.ElementAt(Y);
+            }
+
+            if(Layout.Hexpand)
+            {
+                col.Width = new GridLength(1, GridUnitType.Star);
+            }
+            if(Layout.Vexpand)
+            {
+                row.Height = new GridLength(1, GridUnitType.Star);
+            }
+
+            Grid.SetColumn(control, X);
+            Grid.SetRow(control, Y);
+            Children.Add(control);
+
+            Layout.Reset();
+            X++;
+        }
+
+        public static GridContainer CreateGridContainer(Container parent, int margin, int colspacing, int rowspacing)
+        {
+            return Application.GetInstance().Exec<GridContainer>(() => new GridContainer(parent, margin, colspacing, rowspacing));
         }
     }
 }

mercurial