# HG changeset patch # User Olaf Wintermann # Date 1697743159 -7200 # Node ID 8d7ca1b320e2e8e0b714548662b1fb8ffab97d6c # Parent 1121b61f8828b81d7eb598afc9308c018ac5837f prepare table dnd (WinUI3) diff -r 1121b61f8828 -r 8d7ca1b320e2 make/vs/testapp/main.c --- 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); } } diff -r 1121b61f8828 -r 8d7ca1b320e2 ui/ui/tree.h --- 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; diff -r 1121b61f8828 -r 8d7ca1b320e2 ui/winui/table.cpp --- 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); + } } diff -r 1121b61f8828 -r 8d7ca1b320e2 ui/winui/table.h --- 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 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;