1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using System.Threading.Tasks;
6 using System.Windows;
7 using System.Windows.Controls;
8
9 namespace UI
10 {
11 public interface Container
12 {
13 Layout Layout { get; set; }
14
15 void Add(UIElement control, bool fill);
16 }
17
18 public class Layout
19 {
20 public bool? Fill { get; set; }
21 public bool Hexpand { get; set; }
22 public bool Vexpand { get; set; }
23 public bool NewLine { get; set; }
24 public int GridWidth { get; set; }
25 public String Label { get; set; }
26
27 public Layout()
28 {
29 Reset();
30 }
31
32 public bool IsFill(bool fill)
33 {
34 if (Fill != null)
35 {
36
37 return (bool)Fill;
38 }
39 return fill;
40 }
41
42 public void Reset()
43 {
44 Fill = null;
45 Hexpand = false;
46 Vexpand = false;
47 NewLine = false;
48 GridWidth = 1;
49 Label = null;
50 }
51 }
52
53 public enum BoxOrientation
54 {
55 VERTICAL,
56 HORIZONTAL
57 }
58
59 public class BoxContainer : Grid, Container
60 {
61 public Layout Layout { get; set; }
62
63 private BoxOrientation Orientation;
64 private int Spacing;
65
66 private int x = 0;
67 private int y = 0;
68
69 private bool filled = false;
70
71 public BoxContainer(BoxOrientation orientation, int margin, int spacing) : base()
72 {
73 Layout = new Layout();
74 Margin = new Thickness(margin);
75 Spacing = spacing;
76
77 Orientation = orientation;
78 if(Orientation == BoxOrientation.HORIZONTAL)
79 {
80 RowDefinition row = new RowDefinition();
81 row.Height = new GridLength(1, GridUnitType.Star);
82 RowDefinitions.Add(row);
83 }
84 else
85 {
86 ColumnDefinition col = new ColumnDefinition();
87 col.Width = new GridLength(1, GridUnitType.Star);
88 ColumnDefinitions.Add(col);
89 }
90 }
91
92 public BoxContainer(Container parent, BoxOrientation orientation, int margin, int spacing) : this(orientation, margin, spacing)
93 {
94 parent.Add(this, true);
95 }
96
97 public void Add(UIElement control, bool fill)
98 {
99 fill = Layout.IsFill(fill);
100
101 if(Orientation == BoxOrientation.HORIZONTAL)
102 {
103 if(Spacing > 0)
104 {
105 ColumnDefinition spaceCol = new ColumnDefinition();
106 spaceCol.Width = new GridLength(Spacing, GridUnitType.Pixel);
107 ColumnDefinitions.Add(spaceCol);
108 x++;
109 }
110
111 ColumnDefinition col = new ColumnDefinition();
112 if(filled && fill)
113 {
114 fill = false;
115 Console.WriteLine("BoxContainer can only contain one filled control");
116 }
117 if(fill)
118 {
119 col.Width = new GridLength(1, GridUnitType.Star);
120 filled = true;
121 }
122 else
123 {
124 col.Width = GridLength.Auto;
125 }
126 ColumnDefinitions.Add(col);
127 }
128 else
129 {
130 if (Spacing > 0)
131 {
132 RowDefinition spaceRow = new RowDefinition();
133 spaceRow.Height = new GridLength(Spacing, GridUnitType.Pixel);
134 RowDefinitions.Add(spaceRow);
135 y++;
136 }
137
138 RowDefinition row = new RowDefinition();
139 if (filled && fill)
140 {
141 fill = false;
142 Console.WriteLine("BoxContainer can only contain one filled control");
143 }
144 if(fill)
145 {
146 row.Height = new GridLength(1, GridUnitType.Star);
147 filled = true;
148 }
149 else
150 {
151 row.Height = GridLength.Auto;
152 }
153 RowDefinitions.Add(row);
154 }
155
156 Grid.SetColumn(control, x);
157 Grid.SetRow(control, y);
158 Children.Add(control);
159
160 if(Orientation == BoxOrientation.HORIZONTAL)
161 {
162 x++;
163 }
164 else
165 {
166 y++;
167 }
168
169 Layout.Reset();
170 }
171 }
172
173 public class GridContainer : Grid, Container
174 {
175 public Layout Layout { get; set; }
176
177 private int X = 0;
178 private int Y = 0;
179 private int CurrentWidth = 0;
180 private int CurrentHeight = 0;
181
182 private int ColSpacing;
183 private int RowSpacing;
184
185 public GridContainer(Container parent, int margin, int colspacing, int rowspacing) : base()
186 {
187 Layout = new Layout();
188
189 Margin = new Thickness(margin);
190 ColSpacing = colspacing;
191 RowSpacing = rowspacing;
192
193 parent.Add(this, true);
194 }
195
196 public void Add(UIElement control, bool fill)
197 {
198 if(Layout.NewLine)
199 {
200 X = 0;
201 Y++;
202 }
203
204 ColumnDefinition col;
205 RowDefinition row;
206 bool getcol = false;
207 if(X >= CurrentWidth)
208 {
209 if (ColSpacing > 0 && X != 0)
210 {
211 ColumnDefinition spaceCol = new ColumnDefinition();
212 spaceCol.Width = new GridLength(ColSpacing, GridUnitType.Pixel);
213 ColumnDefinitions.Add(spaceCol);
214 X++;
215 }
216
217 col = new ColumnDefinition();
218 col.Width = GridLength.Auto;
219 ColumnDefinitions.Add(col);
220
221 CurrentWidth = X + 1;
222 }
223 else
224 {
225 if (ColSpacing > 0 && X % 2 > 0)
226 {
227 X++;
228 }
229 col = ColumnDefinitions.ElementAt(X);
230 }
231
232 if(getcol)
233 {
234 col = ColumnDefinitions.ElementAt(X);
235 }
236
237 if (Y >= CurrentHeight)
238 {
239 if (RowSpacing > 0 && Y != 0)
240 {
241 RowDefinition spaceRow = new RowDefinition();
242 spaceRow.Height = new GridLength(RowSpacing, GridUnitType.Pixel);
243 RowDefinitions.Add(spaceRow);
244 Y++;
245 }
246
247 row = new RowDefinition();
248 row.Height = GridLength.Auto;
249 RowDefinitions.Add(row);
250 CurrentHeight = Y + 1;
251 }
252 else
253 {
254 row = RowDefinitions.ElementAt(Y);
255 }
256
257 if(Layout.Hexpand)
258 {
259 col.Width = new GridLength(1, GridUnitType.Star);
260 }
261 if(Layout.Vexpand)
262 {
263 row.Height = new GridLength(1, GridUnitType.Star);
264 }
265
266 int gridwidth = Layout.GridWidth;
267 if(gridwidth > 1)
268 {
269 gridwidth++;
270 }
271
272 Grid.SetColumn(control, X);
273 Grid.SetRow(control, Y);
274 Grid.SetColumnSpan(control, gridwidth);
275 Children.Add(control);
276
277 Layout.Reset();
278 X += gridwidth;
279 }
280 }
281
282 public class ScrollViewerContainer : ScrollViewer, Container
283 {
284 public Layout Layout { get; set; }
285
286 public ScrollViewerContainer(Container parent) : base()
287 {
288 Layout = new Layout();
289
290 HorizontalScrollBarVisibility = ScrollBarVisibility.Auto;
291 VerticalScrollBarVisibility = ScrollBarVisibility.Auto;
292
293 parent.Add(this, true);
294 }
295
296 public void Add(UIElement control, bool fill)
297 {
298 Content = control;
299 }
300 }
301
302 public class TabViewContainer : TabControl, Container
303 {
304 public Layout Layout { get; set; }
305
306 public TabViewContainer(Container parent) : base()
307 {
308 Layout = new Layout();
309 parent.Add(this, true);
310 }
311
312 public void Add(UIElement control, bool fill)
313 {
314 TabItem tab = new TabItem();
315 tab.Header = Layout.Label != null ? Layout.Label : "New Tab";
316 Items.Add(tab);
317 tab.Content = control;
318 Layout.Reset();
319 }
320 }
321 }
322