prepare table dnd (WinUI3) newapi

Thu, 19 Oct 2023 21:19:19 +0200

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Thu, 19 Oct 2023 21:19:19 +0200
branch
newapi
changeset 223
8d7ca1b320e2
parent 222
1121b61f8828
child 224
88bc21b19213

prepare table dnd (WinUI3)

make/vs/testapp/main.c file | annotate | diff | comparison | revisions
ui/ui/tree.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/make/vs/testapp/main.c	Thu Oct 19 20:38:16 2023 +0200
+++ b/make/vs/testapp/main.c	Thu Oct 19 21:19:19 2023 +0200
@@ -143,6 +143,18 @@
     printf("index: %d\n", i);
 }
 
+void dragstart(UiEvent* event, void* data) {
+
+}
+
+void dragcomplete(UiEvent* event, void* data) {
+
+}
+
+void drop(UiEvent* event, void* data) {
+
+}
+
 void application_startup(UiEvent* event, void* data) {
     UiObject* obj = ui_window("Test", NULL);
     WindowData* wdata = ui_malloc(obj->ctx, sizeof(WindowData));
@@ -294,7 +306,8 @@
             UiModel* model = ui_model(obj->ctx, UI_ICON_TEXT, "Col 1", UI_STRING, "Col 2", UI_STRING, "Col 3", -1);
             model->getvalue = table_getvalue;
             ui_table(obj,   .colspan = 3, .model = model, .list = wdata->list2, .onactivate = action_onactivate,
-                            .onselection = action_listselection_changed, .enabledrag = true, .enabledrop = true);
+                            .onselection = action_listselection_changed, .enabledrag = true, .enabledrop = true,
+                            .ondragstart = dragstart, .ondragcomplete = dragcomplete, .ondrop = drop);
             ui_model_free(obj->ctx, model);
         }
     }   
--- a/ui/ui/tree.h	Thu Oct 19 20:38:16 2023 +0200
+++ b/ui/ui/tree.h	Thu Oct 19 21:19:19 2023 +0200
@@ -111,6 +111,11 @@
     int *rows;
 };
 
+struct UiTableDndEvent {
+    UiListSelection selection;
+    void* dnd;
+};
+
 struct UiListArgs {
     UiTri fill;
     UiBool hexpand;
--- a/ui/winui/table.cpp	Thu Oct 19 20:38:16 2023 +0200
+++ b/ui/winui/table.cpp	Thu Oct 19 21:19:19 2023 +0200
@@ -269,14 +269,72 @@
 			}
 
 			// dnd
-			if (enabledrag) {
-				cellBorder.CanDrag(enabledrag);
-				cellBorder.DragStarting([](IInspectable const& sender, DragStartingEventArgs args) {
-					//args.Data().SetText(L"test");
+			if (ondragstart) {
+				cellBorder.CanDrag(true);
+				cellBorder.DragStarting([this](IInspectable const& sender, DragStartingEventArgs args) {
+						UiWinuiTableDnd dndevt;
+						dndevt.evtobj.selection = uiselection();
+						dndevt.evtobj.dnd = &dndevt;
+						dndevt.evttype = 0;
+						dndevt.dndstartargs = args;
+
+						UiEvent evt;
+						evt.obj = this->obj;
+						evt.window = evt.obj->window;
+						evt.document = obj->ctx->document;
+						evt.eventdata = &dndevt;
+						evt.intval = 0;
+					
+						this->ondragstart(&evt, this->ondragstartdata);
+
+						if (dndevt.evtobj.selection.rows) {
+							free(dndevt.evtobj.selection.rows);
+						}
+					});
+				cellBorder.DropCompleted([this](IInspectable const& sender, DropCompletedEventArgs args) {
+						UiWinuiTableDnd dndevt;
+						dndevt.evtobj.selection = uiselection();
+						dndevt.evtobj.dnd = &dndevt;
+						dndevt.evttype = 1;
+						dndevt.dndcompletedargs = args;
+
+						UiEvent evt;
+						evt.obj = this->obj;
+						evt.window = evt.obj->window;
+						evt.document = obj->ctx->document;
+						evt.eventdata = &dndevt;
+						evt.intval = 0;
+
+						if (this->ondragcomplete) {
+							this->ondragcomplete(&evt, this->ondragcompletedata);
+						}
+						if (dndevt.evtobj.selection.rows) {
+							free(dndevt.evtobj.selection.rows);
+						}
 					});
 			}
-			if (enabledrop) {
+			if (ondrop) {
 				cellBorder.AllowDrop(enabledrop);
+				cellBorder.Drop(DragEventHandler([this](winrt::Windows::Foundation::IInspectable const& sender, DragEventArgs const& args){
+						UiWinuiTableDnd dndevt;
+						dndevt.evtobj.selection = uiselection();
+						dndevt.evtobj.dnd = &dndevt;
+						dndevt.evttype = 2;
+						dndevt.drageventargs = args;
+
+						UiEvent evt;
+						evt.obj = this->obj;
+						evt.window = evt.obj->window;
+						evt.document = obj->ctx->document;
+						evt.eventdata = &dndevt;
+						evt.intval = 0;
+
+						this->ondrop(&evt, this->ondropdata);
+
+						if (dndevt.evtobj.selection.rows) {
+							free(dndevt.evtobj.selection.rows);
+						}
+					}));
 			}
 
 			// set the cell value
@@ -489,11 +547,7 @@
 	selection.shrink_to_fit();
 }
 
-void UiTable::call_handler(ui_callback cb, void* cbdata) {
-	if (!cb) {
-		return;
-	}
-
+UiListSelection UiTable::uiselection() {
 	std::sort(selection.begin(), selection.end());
 
 	UiListSelection selobj;
@@ -506,6 +560,15 @@
 			selobj.rows[i]--;
 		}
 	}
+	return selobj;
+}
+
+void UiTable::call_handler(ui_callback cb, void* cbdata) {
+	if (!cb) {
+		return;
+	}
+
+	UiListSelection selobj = uiselection();
 
 	UiEvent evt;
 	evt.obj = obj;
@@ -514,4 +577,8 @@
 	evt.eventdata = &selobj;
 	evt.intval = 0;
 	cb(&evt, cbdata);
+
+	if (selobj.rows) {
+		free(selobj.rows);
+	}
 }
--- a/ui/winui/table.h	Thu Oct 19 20:38:16 2023 +0200
+++ b/ui/winui/table.h	Thu Oct 19 21:19:19 2023 +0200
@@ -39,6 +39,14 @@
 
 } UiTableColumn;
 
+struct UiWinuiTableDnd {
+	UiTableDndEvent evtobj;
+	int evttype = 0;
+	winrt::Microsoft::UI::Xaml::DragStartingEventArgs dndstartargs = { nullptr };
+	winrt::Microsoft::UI::Xaml::DropCompletedEventArgs dndcompletedargs = { nullptr };
+	winrt::Microsoft::UI::Xaml::DragEventArgs drageventargs = { nullptr };
+};
+
 typedef struct UiTable {
 	winrt::Microsoft::UI::Xaml::Controls::ScrollViewer scrollw;
 	winrt::Microsoft::UI::Xaml::Controls::Grid grid;
@@ -64,8 +72,8 @@
 	int lastSelection = 0;
 	ULONG64 lastPointerPress = 0;
 	std::vector<int> selection;
-	bool enabledrag = false;
-	bool enabledrop = false;
+	bool enabledrag = false; // TODO: remove
+	bool enabledrop = false; // TODO: remove
 
 	UiTable(UiObject *obj, winrt::Microsoft::UI::Xaml::Controls::ScrollViewer scrollW, winrt::Microsoft::UI::Xaml::Controls::Grid grid);
 
@@ -85,6 +93,8 @@
 
 	void remove_from_selection(int row);
 
+	UiListSelection uiselection();
+
 	void call_handler(ui_callback cb, void *cbdata);
 } UiTable;
 

mercurial