|
1 /* |
|
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
|
3 * |
|
4 * Copyright 2023 Olaf Wintermann. All rights reserved. |
|
5 * |
|
6 * Redistribution and use in source and binary forms, with or without |
|
7 * modification, are permitted provided that the following conditions are met: |
|
8 * |
|
9 * 1. Redistributions of source code must retain the above copyright |
|
10 * notice, this list of conditions and the following disclaimer. |
|
11 * |
|
12 * 2. Redistributions in binary form must reproduce the above copyright |
|
13 * notice, this list of conditions and the following disclaimer in the |
|
14 * documentation and/or other materials provided with the distribution. |
|
15 * |
|
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
|
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
|
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
|
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
|
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
|
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
|
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
|
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
|
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
|
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
|
26 * POSSIBILITY OF SUCH DAMAGE. |
|
27 */ |
|
28 |
|
29 #include "pch.h" |
|
30 |
|
31 #include "container.h" |
|
32 |
|
33 #include "../common/context.h" |
|
34 #include "../common/object.h" |
|
35 |
|
36 #include "util.h" |
|
37 |
|
38 |
|
39 void ui_container_begin_close(UiObject* obj) { |
|
40 UiContainer* ct = uic_get_current_container(obj); |
|
41 ct->close = 1; |
|
42 } |
|
43 |
|
44 int ui_container_finish(UiObject* obj) { |
|
45 UiContainer* ct = uic_get_current_container(obj); |
|
46 if (ct->close) { |
|
47 ui_end(obj); |
|
48 return 0; |
|
49 } |
|
50 return 1; |
|
51 } |
|
52 |
|
53 |
|
54 // --------------------- UiBoxContainer --------------------- |
|
55 |
|
56 static UIWIDGET ui_box(UiObject* obj, UiContainerArgs args, UiBoxContainerType type) { |
|
57 UiObject* current = uic_current_obj(obj); |
|
58 UI_APPLY_LAYOUT1(current, args); |
|
59 |
|
60 Grid grid = Grid(); |
|
61 current->container->Add(grid, true); |
|
62 |
|
63 UIElement elm = grid; |
|
64 UiWidget* widget = new UiWidget(elm); |
|
65 ui_context_add_widget_destructor(current->ctx, widget); |
|
66 |
|
67 UiObject* newobj = uic_object_new(obj, widget); |
|
68 newobj->container = new UiBoxContainer(grid, type, args.margin, args.spacing); |
|
69 ui_context_add_container_destructor(current->ctx, newobj->container); |
|
70 uic_obj_add(obj, newobj); |
|
71 |
|
72 return widget; |
|
73 } |
|
74 |
|
75 UIWIDGET ui_vbox_create(UiObject* obj, UiContainerArgs args) { |
|
76 return ui_box(obj, args, UI_BOX_CONTAINER_VBOX); |
|
77 } |
|
78 |
|
79 UIWIDGET ui_hbox_create(UiObject* obj, UiContainerArgs args) { |
|
80 return ui_box(obj, args, UI_BOX_CONTAINER_HBOX); |
|
81 } |
|
82 |
|
83 UiBoxContainer::UiBoxContainer(Grid grid, enum UiBoxContainerType type, int margin, int spacing) { |
|
84 this->grid = grid; |
|
85 this->type = type; |
|
86 |
|
87 Thickness t = { (double)margin, (double)margin, (double)margin, (double)margin }; |
|
88 grid.Margin(t); |
|
89 grid.ColumnSpacing((double)spacing); |
|
90 grid.RowSpacing((double)spacing); |
|
91 |
|
92 GridLength gl; |
|
93 gl.Value = 1; |
|
94 gl.GridUnitType = GridUnitType::Star; |
|
95 |
|
96 // hbox needs one row def, vbox needs one col def |
|
97 // all other col/row defs are created when elements are added |
|
98 if (type == UI_BOX_CONTAINER_HBOX) { |
|
99 boxRowDef = RowDefinition(); |
|
100 boxRowDef.Height(gl); |
|
101 grid.RowDefinitions().Append(boxRowDef); |
|
102 } else { |
|
103 boxColDef = ColumnDefinition(); |
|
104 boxColDef.Width(gl); |
|
105 grid.ColumnDefinitions().Append(boxColDef); |
|
106 } |
|
107 |
|
108 ui_reset_layout(layout); |
|
109 } |
|
110 |
|
111 void UiBoxContainer::Add(FrameworkElement control, UiBool fill) { |
|
112 if (this->layout.fill != UI_LAYOUT_UNDEFINED) { |
|
113 fill = ui_lb2bool(this->layout.fill); |
|
114 } |
|
115 |
|
116 GridLength gl; |
|
117 if (fill) { |
|
118 gl.Value = 1; |
|
119 gl.GridUnitType = GridUnitType::Star; |
|
120 } |
|
121 else { |
|
122 gl.Value = 0; |
|
123 gl.GridUnitType = GridUnitType::Auto; |
|
124 } |
|
125 |
|
126 control.HorizontalAlignment(HorizontalAlignment::Stretch); |
|
127 control.VerticalAlignment(VerticalAlignment::Stretch); |
|
128 |
|
129 if (type == UI_CONTAINER_HBOX) { |
|
130 ColumnDefinition coldef = ColumnDefinition(); |
|
131 coldef.Width(gl); |
|
132 grid.ColumnDefinitions().Append(coldef); |
|
133 grid.SetColumn(control, grid.Children().Size()); |
|
134 grid.SetRow(control, 0); |
|
135 } else { |
|
136 RowDefinition rowdef = RowDefinition(); |
|
137 rowdef.Height(gl); |
|
138 grid.RowDefinitions().Append(rowdef); |
|
139 grid.SetRow(control, grid.Children().Size()); |
|
140 grid.SetColumn(control, 0); |
|
141 } |
|
142 |
|
143 grid.Children().Append(control); |
|
144 |
|
145 ui_reset_layout(layout); |
|
146 } |
|
147 |
|
148 |
|
149 // --------------------- UiGridContainer --------------------- |
|
150 |
|
151 UIWIDGET ui_grid_create(UiObject* obj, UiContainerArgs args) { |
|
152 UiObject* current = uic_current_obj(obj); |
|
153 UI_APPLY_LAYOUT1(current, args); |
|
154 |
|
155 Grid grid = Grid(); |
|
156 current->container->Add(grid, true); |
|
157 |
|
158 UIElement elm = grid; |
|
159 UiWidget* widget = new UiWidget(elm); |
|
160 ui_context_add_widget_destructor(current->ctx, widget); |
|
161 |
|
162 UiObject* newobj = uic_object_new(obj, widget); |
|
163 newobj->container = new UiGridContainer(grid, args.margin, args.columnspacing, args.rowspacing); |
|
164 ui_context_add_container_destructor(current->ctx, newobj->container); |
|
165 uic_obj_add(obj, newobj); |
|
166 |
|
167 return widget; |
|
168 } |
|
169 |
|
170 UiGridContainer::UiGridContainer(Grid grid, int margin, int columnspacing, int rowspacing) { |
|
171 this->grid = grid; |
|
172 Thickness t = { (double)margin, (double)margin, (double)margin, (double)margin }; |
|
173 grid.Margin(t); |
|
174 grid.ColumnSpacing((double)columnspacing); |
|
175 grid.RowSpacing((double)rowspacing); |
|
176 ui_reset_layout(layout); |
|
177 } |
|
178 |
|
179 void UiGridContainer::Add(FrameworkElement control, UiBool fill) { |
|
180 GridLength gl; |
|
181 |
|
182 bool hexpand = false; |
|
183 bool vexpand = false; |
|
184 bool hfill = false; |
|
185 bool vfill = false; |
|
186 if(layout.fill != UI_LAYOUT_UNDEFINED) { |
|
187 fill = ui_lb2bool(layout.fill); |
|
188 } |
|
189 if (layout.hexpand != UI_LAYOUT_UNDEFINED) { |
|
190 hexpand = layout.hexpand; |
|
191 hfill = true; |
|
192 } |
|
193 if (layout.vexpand != UI_LAYOUT_UNDEFINED) { |
|
194 vexpand = layout.vexpand; |
|
195 vfill = true; |
|
196 } |
|
197 if (fill) { |
|
198 hfill = true; |
|
199 vfill = true; |
|
200 } |
|
201 |
|
202 // create new RowDefinition for the new line |
|
203 if (layout.newline || y == -1) { |
|
204 x = 0; |
|
205 y++; |
|
206 RowDefinition rowdef = RowDefinition(); |
|
207 if (vexpand) { |
|
208 gl.GridUnitType = GridUnitType::Star; |
|
209 gl.Value = 1; |
|
210 } |
|
211 else { |
|
212 gl.GridUnitType = GridUnitType::Auto; |
|
213 gl.Value = 0; |
|
214 } |
|
215 rowdef.Height(gl); |
|
216 grid.RowDefinitions().Append(rowdef); |
|
217 } else if (vexpand) { |
|
218 // adjust row |
|
219 gl.GridUnitType = GridUnitType::Star; |
|
220 gl.Value = 1; |
|
221 grid.RowDefinitions().GetAt(y).Height(gl); |
|
222 } |
|
223 |
|
224 // create new columndefinition, if a new column is added |
|
225 if (x == cols) { |
|
226 if (hexpand) { |
|
227 gl.GridUnitType = GridUnitType::Star; |
|
228 gl.Value = 1; |
|
229 } |
|
230 else { |
|
231 gl.GridUnitType = GridUnitType::Auto; |
|
232 gl.Value = 0; |
|
233 } |
|
234 ColumnDefinition coldef = ColumnDefinition(); |
|
235 coldef.Width(gl); |
|
236 grid.ColumnDefinitions().Append(coldef); |
|
237 cols++; |
|
238 } else if(hexpand) { |
|
239 // adjust column |
|
240 if (layout.colspan == 0) { |
|
241 gl.GridUnitType = GridUnitType::Star; |
|
242 gl.Value = 1; |
|
243 grid.ColumnDefinitions().GetAt(x).Width(gl); |
|
244 } else { |
|
245 int adjust_col = x; |
|
246 bool adjust = true; |
|
247 for (int i = 0; i < layout.colspan; i++) { |
|
248 if (grid.ColumnDefinitions().Size() == x + i) { |
|
249 break; |
|
250 } |
|
251 adjust_col = x + i; |
|
252 GridLength w = grid.ColumnDefinitions().GetAt(adjust_col).Width(); |
|
253 if (w.GridUnitType == GridUnitType::Star) { |
|
254 adjust = false; |
|
255 break; |
|
256 } |
|
257 } |
|
258 |
|
259 if (adjust) { |
|
260 gl.GridUnitType = GridUnitType::Star; |
|
261 gl.Value = 1; |
|
262 grid.ColumnDefinitions().GetAt(adjust_col).Width(gl); |
|
263 } |
|
264 } |
|
265 } |
|
266 |
|
267 // add control |
|
268 if (hfill) { |
|
269 control.HorizontalAlignment(HorizontalAlignment::Stretch); |
|
270 } |
|
271 if (vfill) { |
|
272 control.VerticalAlignment(VerticalAlignment::Stretch); |
|
273 } |
|
274 |
|
275 if (layout.colspan > 0) { |
|
276 grid.SetColumnSpan(control, layout.colspan); |
|
277 } |
|
278 if (layout.rowspan > 0) { |
|
279 grid.SetRowSpan(control, layout.rowspan); |
|
280 } |
|
281 |
|
282 grid.SetRow(control, y); |
|
283 grid.SetColumn(control, x); |
|
284 grid.Children().Append(control); |
|
285 |
|
286 x++; |
|
287 |
|
288 ui_reset_layout(layout); |
|
289 } |
|
290 |
|
291 // --------------------- UI Frame --------------------- |
|
292 |
|
293 UIWIDGET ui_frame_create(UiObject* obj, UiFrameArgs args) { |
|
294 // create a grid for the frame, that contains the label and a sub-frame |
|
295 Grid frame = Grid(); |
|
296 |
|
297 GridLength gl; |
|
298 gl.GridUnitType = GridUnitType::Star; |
|
299 gl.Value = 1; |
|
300 |
|
301 ColumnDefinition coldef = ColumnDefinition(); |
|
302 coldef.Width(gl); |
|
303 frame.ColumnDefinitions().Append(coldef); |
|
304 |
|
305 RowDefinition rowdefFrame = RowDefinition(); |
|
306 rowdefFrame.Height(gl); |
|
307 |
|
308 // label |
|
309 int row = 0; |
|
310 if (args.label) { |
|
311 RowDefinition rowdefLabel = RowDefinition(); |
|
312 gl.GridUnitType = GridUnitType::Auto; |
|
313 gl.Value = 0; |
|
314 rowdefLabel.Height(gl); |
|
315 frame.RowDefinitions().Append(rowdefLabel); |
|
316 |
|
317 TextBlock label = TextBlock(); |
|
318 wchar_t* wlabel = str2wstr(args.label, nullptr); |
|
319 winrt::hstring hstr(wlabel); |
|
320 label.Text(hstr); |
|
321 free(wlabel); |
|
322 |
|
323 frame.SetRow(label, row++); |
|
324 frame.SetColumn(label, 0); |
|
325 frame.Children().Append(label); |
|
326 } |
|
327 |
|
328 // workarea frame |
|
329 frame.RowDefinitions().Append(rowdefFrame); |
|
330 |
|
331 Grid workarea = Grid(); |
|
332 frame.SetRow(workarea, row); |
|
333 frame.SetColumn(workarea, 0); |
|
334 frame.Children().Append(workarea); |
|
335 |
|
336 // some styling for the workarea |
|
337 winrt::Microsoft::UI::Xaml::Media::SolidColorBrush brush{ winrt::Microsoft::UI::ColorHelper::FromArgb(150, 150, 150, 150) }; |
|
338 workarea.BorderBrush(brush); |
|
339 CornerRadius radius{ 8, 8, 8, 8 }; |
|
340 Thickness t = { 1, 1, 1, 1 }; |
|
341 workarea.CornerRadius(radius); |
|
342 workarea.BorderThickness(t); |
|
343 |
|
344 Thickness padding = { 10, 10, 10, 10 }; |
|
345 workarea.Padding(padding); |
|
346 |
|
347 // add frame to the parent container |
|
348 UiObject* current = uic_current_obj(obj); |
|
349 UI_APPLY_LAYOUT1(current, args); |
|
350 current->container->Add(frame, true); |
|
351 |
|
352 UIElement elm = frame; |
|
353 UiWidget* widget = new UiWidget(elm); |
|
354 ui_context_add_widget_destructor(current->ctx, widget); |
|
355 |
|
356 // sub container |
|
357 UiContainer* ctn = nullptr; |
|
358 switch (args.subcontainer) { |
|
359 default: |
|
360 case UI_CONTAINER_VBOX: { |
|
361 ctn = new UiBoxContainer(workarea, UI_BOX_CONTAINER_VBOX, args.margin, args.spacing); |
|
362 break; |
|
363 } |
|
364 case UI_CONTAINER_HBOX: { |
|
365 ctn = new UiBoxContainer(workarea, UI_BOX_CONTAINER_HBOX, args.margin, args.spacing); |
|
366 break; |
|
367 } |
|
368 case UI_CONTAINER_GRID: { |
|
369 ctn = new UiGridContainer(workarea, args.margin, args.columnspacing, args.rowspacing); |
|
370 break; |
|
371 } |
|
372 } |
|
373 ui_context_add_container_destructor(current->ctx, ctn); |
|
374 |
|
375 UiObject* newobj = uic_object_new(obj, widget); |
|
376 newobj->container = ctn; |
|
377 uic_obj_add(obj, newobj); |
|
378 |
|
379 return widget; |
|
380 } |
|
381 |
|
382 // --------------------- UI Expander --------------------- |
|
383 |
|
384 UIWIDGET ui_expander_create(UiObject* obj, UiFrameArgs args) { |
|
385 Expander expander = Expander(); |
|
386 if (args.label) { |
|
387 wchar_t* wlabel = str2wstr(args.label, nullptr); |
|
388 expander.Header(box_value(wlabel)); |
|
389 free(wlabel); |
|
390 } |
|
391 expander.IsExpanded(args.isexpanded); |
|
392 |
|
393 // add frame to the parent container |
|
394 UiObject* current = uic_current_obj(obj); |
|
395 UI_APPLY_LAYOUT1(current, args); |
|
396 current->container->Add(expander, true); |
|
397 |
|
398 UIElement elm = expander; |
|
399 UiWidget* widget = new UiWidget(elm); |
|
400 ui_context_add_widget_destructor(current->ctx, widget); |
|
401 |
|
402 Grid content = Grid(); |
|
403 expander.Content(content); |
|
404 |
|
405 UiContainer* ctn = nullptr; |
|
406 switch (args.subcontainer) { |
|
407 default: |
|
408 case UI_CONTAINER_VBOX: { |
|
409 ctn = new UiBoxContainer(content, UI_BOX_CONTAINER_VBOX, args.margin, args.spacing); |
|
410 break; |
|
411 } |
|
412 case UI_CONTAINER_HBOX: { |
|
413 ctn = new UiBoxContainer(content, UI_BOX_CONTAINER_HBOX, args.margin, args.spacing); |
|
414 break; |
|
415 } |
|
416 case UI_CONTAINER_GRID: { |
|
417 ctn = new UiGridContainer(content, args.margin, args.columnspacing, args.rowspacing); |
|
418 break; |
|
419 } |
|
420 } |
|
421 ui_context_add_container_destructor(current->ctx, ctn); |
|
422 |
|
423 UiObject* newobj = uic_object_new(obj, widget); |
|
424 newobj->container = ctn; |
|
425 uic_obj_add(obj, newobj); |
|
426 |
|
427 return widget; |
|
428 } |
|
429 |
|
430 // --------------------- UI ScrolledWindow --------------------- |
|
431 |
|
432 UIWIDGET ui_scrolledwindow_create(UiObject* obj, UiFrameArgs args) { |
|
433 ScrollViewer scrollW = ScrollViewer(); |
|
434 |
|
435 // add frame to the parent container |
|
436 UiObject* current = uic_current_obj(obj); |
|
437 UI_APPLY_LAYOUT1(current, args); |
|
438 current->container->Add(scrollW, true); |
|
439 |
|
440 UIElement elm = scrollW; |
|
441 UiWidget* widget = new UiWidget(elm); |
|
442 ui_context_add_widget_destructor(current->ctx, widget); |
|
443 |
|
444 // create child container |
|
445 Grid content = Grid(); |
|
446 scrollW.Content(content); |
|
447 |
|
448 UiContainer* ctn = nullptr; |
|
449 switch (args.subcontainer) { |
|
450 default: |
|
451 case UI_CONTAINER_VBOX: { |
|
452 ctn = new UiBoxContainer(content, UI_BOX_CONTAINER_VBOX, args.margin, args.spacing); |
|
453 break; |
|
454 } |
|
455 case UI_CONTAINER_HBOX: { |
|
456 ctn = new UiBoxContainer(content, UI_BOX_CONTAINER_HBOX, args.margin, args.spacing); |
|
457 break; |
|
458 } |
|
459 case UI_CONTAINER_GRID: { |
|
460 ctn = new UiGridContainer(content, args.margin, args.columnspacing, args.rowspacing); |
|
461 break; |
|
462 } |
|
463 } |
|
464 ui_context_add_container_destructor(current->ctx, ctn); |
|
465 |
|
466 UiObject* newobj = uic_object_new(obj, widget); |
|
467 newobj->container = ctn; |
|
468 uic_obj_add(obj, newobj); |
|
469 |
|
470 return widget; |
|
471 } |
|
472 |
|
473 // --------------------- UI TabView --------------------- |
|
474 |
|
475 UiTabViewContainer::UiTabViewContainer(UiTabView* tabview) { |
|
476 this->tabview = tabview; |
|
477 } |
|
478 |
|
479 void UiTabViewContainer::Add(FrameworkElement control, UiBool fill) { |
|
480 // noop |
|
481 } |
|
482 |
|
483 static UiObject* create_subcontainer_obj(UiObject* current, Grid subcontainer, UiSubContainerType type, int margin, int spacing, int columnspacing, int rowspacing) { |
|
484 UiContainer* ctn = nullptr; |
|
485 switch (type) { |
|
486 default: |
|
487 case UI_CONTAINER_VBOX: { |
|
488 ctn = new UiBoxContainer(subcontainer, UI_BOX_CONTAINER_VBOX, margin, spacing); |
|
489 break; |
|
490 } |
|
491 case UI_CONTAINER_HBOX: { |
|
492 ctn = new UiBoxContainer(subcontainer, UI_BOX_CONTAINER_HBOX, margin, spacing); |
|
493 break; |
|
494 } |
|
495 case UI_CONTAINER_GRID: { |
|
496 ctn = new UiGridContainer(subcontainer, margin, columnspacing, rowspacing); |
|
497 break; |
|
498 } |
|
499 } |
|
500 ui_context_add_container_destructor(current->ctx, ctn); |
|
501 |
|
502 UIElement elm = subcontainer; |
|
503 UiWidget* widget = new UiWidget(elm); |
|
504 ui_context_add_widget_destructor(current->ctx, widget); |
|
505 UiObject* newobj = uic_object_new(current, widget); |
|
506 newobj->container = ctn; |
|
507 return newobj; |
|
508 } |
|
509 |
|
510 static UiTabView* tabview_pivot_create(UiObject* obj, UiTabViewArgs args) { |
|
511 Pivot pivot = Pivot(); |
|
512 UiPivotTabView* tabview = new UiPivotTabView(obj, pivot, args); |
|
513 |
|
514 return tabview; |
|
515 } |
|
516 |
|
517 UiPivotTabView::UiPivotTabView(UiObject* obj, Pivot pivot, UiTabViewArgs args) { |
|
518 this->current = obj; |
|
519 this->pivot = pivot; |
|
520 this->subcontainer = args.subcontainer; |
|
521 this->margin = args.margin; |
|
522 this->spacing = args.spacing; |
|
523 this->columnspacing = args.columnspacing; |
|
524 this->rowspacing = args.rowspacing; |
|
525 } |
|
526 |
|
527 UiObject* UiPivotTabView::AddTab(const char* label, int index) { |
|
528 TextBlock text = TextBlock(); |
|
529 wchar_t* wlabel = str2wstr(label, nullptr); |
|
530 winrt::hstring hstr(wlabel); |
|
531 text.Text(hstr); |
|
532 free(wlabel); |
|
533 |
|
534 PivotItem item = PivotItem(); |
|
535 item.Header(text); |
|
536 |
|
537 // sub container |
|
538 Grid subcontainer = Grid(); |
|
539 item.Content(subcontainer); |
|
540 pivot.Items().Append(item); |
|
541 |
|
542 return create_subcontainer_obj(current, subcontainer, this->subcontainer, margin, spacing, columnspacing, rowspacing); |
|
543 } |
|
544 |
|
545 void UiPivotTabView::Remove(int index) { |
|
546 pivot.Items().RemoveAt(index); |
|
547 } |
|
548 |
|
549 void UiPivotTabView::Select(int index) { |
|
550 |
|
551 } |
|
552 |
|
553 FrameworkElement UiPivotTabView::GetFrameworkElement() { |
|
554 return pivot; |
|
555 } |
|
556 |
|
557 |
|
558 static UiTabView* tabview_invisible_create(UiObject *obj, UiTabViewArgs args) { |
|
559 Grid container = Grid(); |
|
560 container.HorizontalAlignment(HorizontalAlignment::Stretch); |
|
561 container.VerticalAlignment(VerticalAlignment::Stretch); |
|
562 UiInvisibleTabView *tabview = new UiInvisibleTabView(obj, container, args); |
|
563 return tabview; |
|
564 } |
|
565 |
|
566 UiInvisibleTabView::UiInvisibleTabView(UiObject* obj, Grid container, UiTabViewArgs args) { |
|
567 this->current = obj; |
|
568 this->container = container; |
|
569 this->subcontainer = args.subcontainer; |
|
570 this->margin = args.margin; |
|
571 this->spacing = args.spacing; |
|
572 this->columnspacing = args.columnspacing; |
|
573 this->rowspacing = args.rowspacing; |
|
574 this->currentIndex = -1; |
|
575 |
|
576 GridLength gl; |
|
577 gl.GridUnitType = GridUnitType::Star; |
|
578 gl.Value = 1; |
|
579 |
|
580 ColumnDefinition coldef = ColumnDefinition(); |
|
581 coldef.Width(gl); |
|
582 container.ColumnDefinitions().Append(coldef); |
|
583 |
|
584 RowDefinition rowdef = RowDefinition(); |
|
585 rowdef.Height(gl); |
|
586 container.RowDefinitions().Append(rowdef); |
|
587 } |
|
588 |
|
589 UiObject* UiInvisibleTabView::AddTab(const char* label, int index) { |
|
590 Grid subcontainer = Grid(); |
|
591 subcontainer.HorizontalAlignment(HorizontalAlignment::Stretch); |
|
592 subcontainer.VerticalAlignment(VerticalAlignment::Stretch); |
|
593 |
|
594 if (pages.size() == 0) { |
|
595 container.Children().Append(subcontainer); |
|
596 currentIndex = 0; |
|
597 } |
|
598 |
|
599 if (index < 0) { |
|
600 pages.push_back(subcontainer); |
|
601 } else { |
|
602 pages.insert(pages.begin() + index, subcontainer); |
|
603 } |
|
604 |
|
605 // sub container |
|
606 return create_subcontainer_obj(current, subcontainer, this->subcontainer, margin, spacing, columnspacing, rowspacing); |
|
607 } |
|
608 |
|
609 void UiInvisibleTabView::Remove(int index) { |
|
610 |
|
611 } |
|
612 |
|
613 void UiInvisibleTabView::Select(int index) { |
|
614 if (index >= 0 && index < pages.size()) { |
|
615 if (currentIndex != -1) { |
|
616 container.Children().RemoveAt(0); |
|
617 } |
|
618 |
|
619 container.Children().Append(pages.at(index)); |
|
620 } |
|
621 } |
|
622 |
|
623 FrameworkElement UiInvisibleTabView::GetFrameworkElement() { |
|
624 return container; |
|
625 } |
|
626 |
|
627 |
|
628 static UiTabView* tabview_main_create(UiObject* obj, UiTabViewArgs args) { |
|
629 TabView tabview = TabView(); |
|
630 tabview.IsAddTabButtonVisible(false); |
|
631 //tabview.CanDragTabs(false); |
|
632 //tabview.CanReorderTabs(false); |
|
633 UiMainTabView* uitabview = new UiMainTabView(obj, tabview, args); |
|
634 |
|
635 return uitabview; |
|
636 } |
|
637 |
|
638 UiMainTabView::UiMainTabView(UiObject* obj, TabView tabview, UiTabViewArgs args) { |
|
639 this->current = obj; |
|
640 this->tabview = tabview; |
|
641 this->subcontainer = args.subcontainer; |
|
642 this->margin = args.margin; |
|
643 this->spacing = args.spacing; |
|
644 this->columnspacing = args.columnspacing; |
|
645 this->rowspacing = args.rowspacing; |
|
646 } |
|
647 |
|
648 UiObject* UiMainTabView::AddTab(const char* label, int index) { |
|
649 TextBlock text = TextBlock(); |
|
650 wchar_t* wlabel = str2wstr(label, nullptr); |
|
651 winrt::hstring hstr(wlabel); |
|
652 text.Text(hstr); |
|
653 free(wlabel); |
|
654 |
|
655 TabViewItem item = TabViewItem(); |
|
656 item.Header(text); |
|
657 item.CanDrag(false); |
|
658 item.IsClosable(false); |
|
659 |
|
660 // sub container |
|
661 Grid subcontainer = Grid(); |
|
662 item.Content(subcontainer); |
|
663 tabview.TabItems().Append(item); |
|
664 |
|
665 return create_subcontainer_obj(current, subcontainer, this->subcontainer, margin, spacing, columnspacing, rowspacing); |
|
666 } |
|
667 |
|
668 void UiMainTabView::Remove(int index) { |
|
669 this->tabview.TabItems().RemoveAt(index); |
|
670 } |
|
671 |
|
672 void UiMainTabView::Select(int index) { |
|
673 |
|
674 } |
|
675 |
|
676 FrameworkElement UiMainTabView::GetFrameworkElement() { |
|
677 return tabview; |
|
678 } |
|
679 |
|
680 |
|
681 static UiTabView* tabview_navigationview_create(UiObject* obj, UiTabViewArgs args, UiTabViewType type) { |
|
682 NavigationView navigationview = NavigationView(); |
|
683 UiNavigationTabView* tabview = new UiNavigationTabView(obj, navigationview, args, type); |
|
684 navigationview.IsBackButtonVisible(NavigationViewBackButtonVisible::Collapsed); |
|
685 navigationview.IsSettingsVisible(false); |
|
686 |
|
687 return tabview; |
|
688 } |
|
689 |
|
690 UiNavigationTabView::UiNavigationTabView(UiObject* obj, NavigationView navigationview, UiTabViewArgs args, UiTabViewType type) { |
|
691 this->current = obj; |
|
692 this->navigationview = navigationview; |
|
693 this->type = type; |
|
694 this->margin = args.margin; |
|
695 this->spacing = args.spacing; |
|
696 this->columnspacing = args.columnspacing; |
|
697 this->rowspacing = args.rowspacing; |
|
698 |
|
699 if (type == UI_TABVIEW_NAVIGATION_TOP) { |
|
700 navigationview.PaneDisplayMode(NavigationViewPaneDisplayMode::Top); |
|
701 } |
|
702 |
|
703 navigationview.SelectionChanged({ this, &UiNavigationTabView::SelectionChanged }); |
|
704 } |
|
705 |
|
706 UiObject* UiNavigationTabView::AddTab(const char* label, int index1) { |
|
707 TextBlock text = TextBlock(); |
|
708 wchar_t* wlabel = str2wstr(label, nullptr); |
|
709 winrt::hstring hstr(wlabel); |
|
710 text.Text(hstr); |
|
711 free(wlabel); |
|
712 |
|
713 NavigationViewItem item = NavigationViewItem(); |
|
714 item.Content(text); |
|
715 |
|
716 // sub container |
|
717 Grid subcontainer = Grid(); |
|
718 if (pages.size() == 0) { |
|
719 navigationview.Content(subcontainer); |
|
720 navigationview.SelectedItem(item); |
|
721 } |
|
722 |
|
723 navigationview.MenuItems().Append(item); |
|
724 auto page = std::tuple<NavigationViewItem, FrameworkElement>{ item, subcontainer }; |
|
725 pages.push_back(page); |
|
726 |
|
727 return create_subcontainer_obj(current, subcontainer, this->subcontainer, margin, spacing, columnspacing, rowspacing); |
|
728 } |
|
729 |
|
730 void UiNavigationTabView::Remove(int index) { |
|
731 navigationview.MenuItems().RemoveAt(index); |
|
732 pages.erase(pages.begin() + index); |
|
733 } |
|
734 |
|
735 void UiNavigationTabView::Select(int index) { |
|
736 |
|
737 } |
|
738 |
|
739 FrameworkElement UiNavigationTabView::GetFrameworkElement() { |
|
740 return navigationview; |
|
741 } |
|
742 |
|
743 void UiNavigationTabView::SelectionChanged(NavigationView const& sender, NavigationViewSelectionChangedEventArgs const& args) { |
|
744 for (auto page : pages) { |
|
745 NavigationViewItem item = std::get<0>(page); |
|
746 FrameworkElement elm = std::get<1>(page); |
|
747 if (item == navigationview.SelectedItem()) { |
|
748 navigationview.Content(elm); |
|
749 break; |
|
750 } |
|
751 } |
|
752 } |
|
753 |
|
754 static int64_t ui_tabview_get(UiInteger *i) { |
|
755 return 0; |
|
756 } |
|
757 |
|
758 static void ui_tabview_set(UiInteger *i, int64_t value) { |
|
759 UiTabView *tabview = (UiTabView*)i->obj; |
|
760 tabview->Select(value); |
|
761 } |
|
762 |
|
763 UIWIDGET ui_tabview_create(UiObject* obj, UiTabViewArgs args) { |
|
764 UiTabViewType type = args.tabview == UI_TABVIEW_DEFAULT ? UI_TABVIEW_NAVIGATION_TOP2 : args.tabview; |
|
765 UiTabView* tabview = nullptr; |
|
766 switch (type) { |
|
767 default: { |
|
768 tabview = tabview_pivot_create(obj, args); |
|
769 break; |
|
770 } |
|
771 case UI_TABVIEW_DOC: { |
|
772 tabview = tabview_main_create(obj, args); |
|
773 break; |
|
774 } |
|
775 case UI_TABVIEW_NAVIGATION_SIDE: { |
|
776 tabview = tabview_navigationview_create(obj, args, type); |
|
777 break; |
|
778 } |
|
779 case UI_TABVIEW_NAVIGATION_TOP: { |
|
780 tabview = tabview_navigationview_create(obj, args, type); |
|
781 break; |
|
782 } |
|
783 case UI_TABVIEW_NAVIGATION_TOP2: { |
|
784 tabview = tabview_pivot_create(obj, args); |
|
785 break; |
|
786 } |
|
787 case UI_TABVIEW_INVISIBLE: { |
|
788 tabview = tabview_invisible_create(obj, args); |
|
789 break; |
|
790 } |
|
791 } |
|
792 UiTabViewContainer* ctn = new UiTabViewContainer(tabview); |
|
793 |
|
794 // add frame to the parent container |
|
795 UiObject* current = uic_current_obj(obj); |
|
796 UI_APPLY_LAYOUT1(current, args); |
|
797 current->container->Add(tabview->GetFrameworkElement(), true); |
|
798 |
|
799 UIElement elm = tabview->GetFrameworkElement(); |
|
800 UiWidget* widget = new UiWidget(elm); |
|
801 ui_context_add_widget_destructor(current->ctx, widget); |
|
802 widget->data1 = tabview; |
|
803 |
|
804 // TODO: add tabview destructor |
|
805 |
|
806 // bind variable |
|
807 UiVar* var = uic_widget_var(obj->ctx, current->ctx, args.value, args.varname, UI_VAR_INTEGER); |
|
808 if (var) { |
|
809 UiInteger *i = (UiInteger*)var->value; |
|
810 i->obj = tabview; |
|
811 i->get = ui_tabview_get; |
|
812 i->set = ui_tabview_set; |
|
813 } |
|
814 |
|
815 UiObject* newobj = uic_object_new(obj, widget); |
|
816 newobj->container = ctn; |
|
817 uic_obj_add(obj, newobj); |
|
818 |
|
819 return widget; |
|
820 } |
|
821 |
|
822 void ui_tab_create(UiObject* obj, const char* title) { |
|
823 UiObject* current = uic_current_obj(obj); |
|
824 UiTabView* tabview = (UiTabView*)current->widget->data1; |
|
825 UiObject* newobj = tabview->AddTab(title); |
|
826 uic_obj_add(current, newobj); |
|
827 } |
|
828 |
|
829 UIEXPORT void ui_tabview_select(UIWIDGET tabview, int tab) { |
|
830 UiTabView* t = (UiTabView*)tabview->data1; |
|
831 t->Select(tab); |
|
832 } |
|
833 |
|
834 UIEXPORT void ui_tabview_remove(UIWIDGET tabview, int tab) { |
|
835 UiTabView* t = (UiTabView*)tabview->data1; |
|
836 t->Remove(tab); |
|
837 } |
|
838 |
|
839 UIEXPORT UiObject* ui_tabview_add(UIWIDGET tabview, const char *name, int tab_index) { |
|
840 UiTabView* t = (UiTabView*)tabview->data1; |
|
841 UiObject* newobj = t->AddTab(name, tab_index); |
|
842 return newobj; |
|
843 } |
|
844 |
|
845 |
|
846 |
|
847 // --------------------- UI Headerbar --------------------- |
|
848 |
|
849 // TODO: replace placeholder implementation |
|
850 |
|
851 UIEXPORT UIWIDGET ui_headerbar_create(UiObject *obj, UiHeaderbarArgs args) { |
|
852 UiContainerArgs boxargs = { }; |
|
853 boxargs.fill = UI_OFF; |
|
854 return ui_hbox_create(obj, boxargs); |
|
855 } |
|
856 |
|
857 UIEXPORT void ui_headerbar_start_create(UiObject *obj) { |
|
858 UiContainerArgs boxargs = { }; |
|
859 boxargs.fill = UI_OFF; |
|
860 ui_hbox_create(obj, boxargs); |
|
861 } |
|
862 |
|
863 UIEXPORT void ui_headerbar_center_create(UiObject *obj) { |
|
864 UiContainerArgs boxargs = { }; |
|
865 boxargs.fill = UI_OFF; |
|
866 ui_hbox_create(obj, boxargs); |
|
867 } |
|
868 |
|
869 UIEXPORT void ui_headerbar_end_create(UiObject *obj) { |
|
870 UiContainerArgs boxargs = { }; |
|
871 boxargs.fill = UI_OFF; |
|
872 ui_hbox_create(obj, boxargs); |
|
873 } |
|
874 |
|
875 |
|
876 /* |
|
877 * -------------------- Layout Functions -------------------- |
|
878 * |
|
879 * functions for setting layout attributes for the current container |
|
880 * |
|
881 */ |
|
882 |
|
883 void ui_layout_fill(UiObject* obj, UiBool fill) { |
|
884 UiContainer* ct = uic_get_current_container(obj); |
|
885 ct->layout.fill = ui_bool2lb(fill); |
|
886 } |
|
887 |
|
888 void ui_layout_hexpand(UiObject* obj, UiBool expand) { |
|
889 UiContainer* ct = uic_get_current_container(obj); |
|
890 ct->layout.hexpand = expand; |
|
891 } |
|
892 |
|
893 void ui_layout_vexpand(UiObject* obj, UiBool expand) { |
|
894 UiContainer* ct = uic_get_current_container(obj); |
|
895 ct->layout.vexpand = expand; |
|
896 } |
|
897 |
|
898 void ui_layout_hfill(UiObject* obj, UiBool fill) { |
|
899 UiContainer* ct = uic_get_current_container(obj); |
|
900 ct->layout.hfill = fill; |
|
901 } |
|
902 |
|
903 void ui_layout_vfill(UiObject* obj, UiBool fill) { |
|
904 UiContainer* ct = uic_get_current_container(obj); |
|
905 ct->layout.vfill = fill; |
|
906 } |
|
907 |
|
908 void ui_layout_width(UiObject* obj, int width) { |
|
909 UiContainer* ct = uic_get_current_container(obj); |
|
910 ct->layout.width = width; |
|
911 } |
|
912 |
|
913 void ui_layout_height(UiObject* obj, int height) { |
|
914 UiContainer* ct = uic_get_current_container(obj); |
|
915 ct->layout.height = height; |
|
916 } |
|
917 |
|
918 void ui_layout_colspan(UiObject* obj, int cols) { |
|
919 UiContainer* ct = uic_get_current_container(obj); |
|
920 ct->layout.colspan = cols; |
|
921 } |
|
922 |
|
923 void ui_layout_rowspan(UiObject* obj, int rows) { |
|
924 UiContainer* ct = uic_get_current_container(obj); |
|
925 ct->layout.rowspan = rows; |
|
926 } |
|
927 |
|
928 void ui_newline(UiObject* obj) { |
|
929 UiContainer* ct = uic_get_current_container(obj); |
|
930 ct->layout.newline = TRUE; |
|
931 } |
|
932 |