ui/winui/table.cpp

branch
newapi
changeset 215
1bd5534c395d
parent 214
279c0c81d3b1
child 216
391c2c723029
--- a/ui/winui/table.cpp	Fri Oct 13 11:26:47 2023 +0200
+++ b/ui/winui/table.cpp	Fri Oct 13 15:20:54 2023 +0200
@@ -31,9 +31,11 @@
 #include "table.h"
 #include "container.h"
 #include "util.h"
+#include "icons.h"
 
 #include "../common/context.h"
 #include "../common/object.h"
+#include "../common/types.h"
 
 #include <winrt/Microsoft.UI.Xaml.Data.h>
 #include <winrt/Microsoft.UI.Xaml.Media.h>
@@ -130,7 +132,13 @@
 	);
 }
 
+UiTable::~UiTable() {
+	ui_model_free(NULL, model);
+}
+
 void UiTable::add_header(UiModel* model) {
+	this->model = ui_model_copy(NULL, model);
+
 	GridLength gl;
 	gl.Value = 0;
 	gl.GridUnitType = GridUnitType::Auto;
@@ -174,6 +182,20 @@
 	maxrows = 1;
 }
 
+static void textblock_set_str(TextBlock &t, const char *str) {
+	if (str) {
+		wchar_t* wstr = str2wstr(str, nullptr);
+		t.Text(winrt::hstring(wstr));
+		free(wstr);
+	}
+}
+
+static void textblock_set_int(TextBlock& t, int i) {
+	wchar_t buf[16];
+	swprintf(buf, 16, L"%d", i);
+	t.Text(winrt::hstring(buf));
+}
+
 void UiTable::update(UiList* list,  int i) {
 	if (getvalue == nullptr) {
 		return;
@@ -197,13 +219,66 @@
 			maxrows = row;
 		}
 
-		for (int col = 0; col < header.size(); col++) {
+		Thickness cellpadding = { 10,0,4,0 };
+
+		int model_col = 0;
+		for (int col = 0; col < header.size(); col++, model_col++) {
 			// create ui elements with the correct cell border
 			// dependeing on the column
 			Border cellBorder = Border();
 			cellBorder.Background(defaultBrush);
-			TextBlock cell = TextBlock();
-			cellBorder.Child(cell);
+
+			// set the cell value
+			UiModelType type = model->types[col];
+			switch (type) {
+				case UI_STRING: {
+					TextBlock cell = TextBlock();
+					cell.Padding(cellpadding);
+					cell.VerticalAlignment(VerticalAlignment::Stretch);
+					textblock_set_str(cell, (char*)getvalue(elm, model_col));
+					cellBorder.Child(cell);
+					break;
+				}
+				case UI_INTEGER: {
+					TextBlock cell = TextBlock();
+					cell.Padding(cellpadding);
+					cell.VerticalAlignment(VerticalAlignment::Stretch);
+					int *value = (int*)getvalue(elm, model_col);
+					if (value) {
+						textblock_set_int(cell, *value);
+					}
+					cellBorder.Child(cell);
+					break;
+				}
+				case UI_ICON: {
+					UiIcon* iconConstr = (UiIcon*)getvalue(elm, model_col);
+					if (iconConstr) {
+						IconElement icon = iconConstr->getIcon();
+						cellBorder.Child(icon);
+					}
+					break;
+				}
+				case UI_ICON_TEXT: {
+					StackPanel cellPanel = StackPanel();
+					cellPanel.Spacing(2);
+					cellPanel.Padding(cellpadding);
+					cellPanel.VerticalAlignment(VerticalAlignment::Stretch);
+
+					cellPanel.Orientation(Orientation::Horizontal);
+					UiIcon* iconConstr = (UiIcon*)getvalue(elm, model_col++);
+					char* str = (char*)getvalue(elm, model_col);
+					if (iconConstr) {
+						IconElement icon = iconConstr->getIcon();
+						cellPanel.Children().Append(icon);
+					}
+					TextBlock cell = TextBlock();
+					textblock_set_str(cell, str);
+					cellPanel.Children().Append(cell);
+					cellBorder.Child(cellPanel);
+					break;
+				}
+			}
+			
 			cellBorder.BorderBrush(defaultBrush);
 			if (col == 0) {
 				cellBorder.BorderThickness(b1);
@@ -214,17 +289,6 @@
 			else {
 				cellBorder.BorderThickness(b2);
 			}
-			Thickness padding = { 10,0,4,0 };
-			cell.Padding(padding);
-			cell.VerticalAlignment(VerticalAlignment::Stretch);
-
-			// set cell value
-			char* value = (char*)getvalue(elm, col);
-			if (value) {
-				wchar_t* wstr = str2wstr(value, nullptr);
-				cell.Text(winrt::hstring(wstr));
-				free(wstr);
-			}
 
 			// event handler
 			cellBorder.PointerPressed(

mercurial