make table columns resizable newapi

Fri, 26 Jan 2024 14:24:39 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Fri, 26 Jan 2024 14:24:39 +0100
branch
newapi
changeset 227
04b317bc6f13
parent 226
4eef1d49f794
child 228
b4d7686b30dc

make table columns resizable

ui/winui/pch.h file | annotate | diff | comparison | revisions
ui/winui/table.cpp file | annotate | diff | comparison | revisions
ui/winui/table.h file | annotate | diff | comparison | revisions
--- a/ui/winui/pch.h	Tue Jan 16 12:49:46 2024 +0100
+++ b/ui/winui/pch.h	Fri Jan 26 14:24:39 2024 +0100
@@ -30,7 +30,9 @@
 #include <winrt/Windows.ApplicationModel.DataTransfer.h>
 #include <wil/cppwinrt_helpers.h>
 #include <winrt/Microsoft.UI.Xaml.Input.h>
+#include <winrt/Microsoft.UI.Input.h>
 #include <winrt/Windows.UI.Core.h>
 #include <winrt/Windows.ApplicationModel.h>
 
+
 #include <winrt/Windows.Storage.Streams.h>
--- a/ui/winui/table.cpp	Tue Jan 16 12:49:46 2024 +0100
+++ b/ui/winui/table.cpp	Fri Jan 26 14:24:39 2024 +0100
@@ -59,6 +59,20 @@
 	// TODO:
 }
 
+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));
+}
+
 UIEXPORT UIWIDGET ui_table_create(UiObject* obj, UiListArgs args) {
 	if (!args.model) {
 		return nullptr;
@@ -162,6 +176,9 @@
 	headerRowDef.Height(gl);
 	grid.RowDefinitions().Append(headerRowDef);
 
+	winrt::Windows::UI::Color borderColor = { 63, 0, 0, 0 };
+	SolidColorBrush borderBrush = SolidColorBrush(borderColor);
+
 
 	for (int i = 0; i < model->columns;i++) {
 		char* title = model->titles[i];
@@ -172,44 +189,89 @@
 		colDef.Width(gl);
 		grid.ColumnDefinitions().Append(colDef);
 
-		// add button
-		auto button = Button();
-		wchar_t* wlabel = str2wstr(title, nullptr);
-		button.Content(box_value(wlabel));
-		free(wlabel);
-
-		// some styling for the button
+		// header column border
+		Border headerBorder = Border();
 		Thickness border = { 0,0,1,0 };
-		CornerRadius corner = { 0,0,0,0 };
-		button.BorderThickness(border);
-		button.CornerRadius(corner);
+		headerBorder.BorderThickness(border);
+		headerBorder.BorderBrush(borderBrush);
+
+		// add text
+		auto hLabel = TextBlock();
+		textblock_set_str(hLabel, title);
+		Thickness cellpadding = { 10,4,4,4 };
+		hLabel.Padding(cellpadding);
+		hLabel.VerticalAlignment(VerticalAlignment::Stretch);
 
-		grid.SetColumn(button, i);
-		grid.SetRow(button, 0);
-		grid.Children().Append(button);
+		// event handler for highlighting and column resizing
+		headerBorder.PointerPressed(
+			winrt::Microsoft::UI::Xaml::Input::PointerEventHandler(
+				[=](IInspectable const& sender, winrt::Microsoft::UI::Xaml::Input::PointerRoutedEventArgs const& args) {
+					// the last column doesn't need resize capabilities
+					if (i + 1 < model->columns) {
+						double width = headerBorder.ActualWidth();
+						auto point = args.GetCurrentPoint(headerBorder);
+						auto position = point.Position();
+						if (position.X + 4 >= width) {
+							this->resize = true;
+							this->resizedCol = headerBorder;
+						}
+					}
+				})
+		);
+		headerBorder.PointerReleased(
+			winrt::Microsoft::UI::Xaml::Input::PointerEventHandler(
+				[=](IInspectable const& sender, winrt::Microsoft::UI::Xaml::Input::PointerRoutedEventArgs const& args) {
+					this->resize = false;
+				})
+		);
+		headerBorder.PointerMoved(
+			winrt::Microsoft::UI::Xaml::Input::PointerEventHandler(
+				[=](IInspectable const& sender, winrt::Microsoft::UI::Xaml::Input::PointerRoutedEventArgs const& args) {
+					if (this->resize) {
+						switch (i) {
+						case 0: OutputDebugString(L"pointer moved 0\n"); break;
+						case 1: OutputDebugString(L"pointer moved 1\n"); break;
+						case 2: OutputDebugString(L"pointer moved 2\n"); break;
+						case 3: OutputDebugString(L"pointer moved 3\n"); break;
+						}
+						auto point = args.GetCurrentPoint(this->resizedCol);
+						auto position = point.Position();
+						if (position.X > 1) {
+							this->resizedCol.Width(position.X);
+						}
+					}
+				})
+		);
+		headerBorder.PointerEntered(
+			winrt::Microsoft::UI::Xaml::Input::PointerEventHandler(
+				[=](IInspectable const& sender, winrt::Microsoft::UI::Xaml::Input::PointerRoutedEventArgs const& args) {
+					// TODO: background
+				})
+		);
+		headerBorder.PointerExited(
+			winrt::Microsoft::UI::Xaml::Input::PointerEventHandler(
+				[=](IInspectable const& sender, winrt::Microsoft::UI::Xaml::Input::PointerRoutedEventArgs const& args) {
+					// TODO: background
+				})
+		);
+
+
+
+		// add controls
+		headerBorder.Child(hLabel);
+
+		grid.SetColumn(headerBorder, i);
+		grid.SetRow(headerBorder, 0);
+		grid.Children().Append(headerBorder);
 
 		UiTableColumn h;
-		h.header = button;
+		h.header = headerBorder;
 		header.push_back(h);
 	}
 
 	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));
-}
-
 static ULONG64 getsystime() {
 	SYSTEMTIME st;
 	GetSystemTime(&st);
--- a/ui/winui/table.h	Tue Jan 16 12:49:46 2024 +0100
+++ b/ui/winui/table.h	Fri Jan 26 14:24:39 2024 +0100
@@ -36,7 +36,7 @@
 
 
 typedef struct UiTableColumn {
-	winrt::Microsoft::UI::Xaml::Controls::Button header;
+	winrt::Microsoft::UI::Xaml::Controls::Border header;
 
 } UiTableColumn;
 
@@ -47,6 +47,10 @@
 	winrt::Microsoft::UI::Xaml::Media::SolidColorBrush highlightBrush;
 	winrt::Microsoft::UI::Xaml::Media::SolidColorBrush selectedBrush;
 	winrt::Microsoft::UI::Xaml::Media::SolidColorBrush selectedBorderBrush;
+
+	winrt::Microsoft::UI::Xaml::Controls::Border resizedCol{ nullptr };
+	bool resize = false;
+
 	UiObject* obj;
 	ui_callback onactivate;
 	void* onactivatedata;

mercurial