Thu, 19 Oct 2023 21:19:19 +0200
prepare table dnd (WinUI3)
/* * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. * * Copyright 2023 Olaf Wintermann. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ #include "pch.h" #include "list.h" #include "container.h" #include "util.h" #include "../common/context.h" #include "../common/object.h" using namespace winrt; using namespace Microsoft::UI::Xaml; using namespace Microsoft::UI::Xaml::Controls; using namespace Windows::UI::Xaml::Interop; using namespace winrt::Windows::Foundation; using namespace Microsoft::UI::Xaml::Markup; using namespace Microsoft::UI::Xaml::Media; using namespace winrt::Microsoft::UI::Xaml::Controls::Primitives; UIWIDGET ui_listview_create(UiObject* obj, UiListArgs args) { UiObject* current = uic_current_obj(obj); // create listview and toolkit wrapper ListView listview = ListView(); if (args.multiselection) { listview.SelectionMode(ListViewSelectionMode::Extended); } bool clickEnabled = listview.IsItemClickEnabled(); listview.IsItemClickEnabled(true); UIElement elm = listview; UiWidget* widget = new UiWidget(elm); widget->data1 = args.model; widget->data2 = args.getvalue; ui_context_add_widget_destructor(current->ctx, widget); // bind var UiVar* var = uic_widget_var(obj->ctx, current->ctx, args.list, args.varname, UI_VAR_LIST); if (var) { UiList* list = (UiList*)var->value; list->update = ui_simple_list_update; list->obj = widget; ui_simple_list_update(list, 0); } if (args.onselection) { ui_callback onselection = args.onselection; void* cbdata = args.onselectiondata; listview.SelectionChanged([onselection, cbdata, obj](IInspectable const& sender, RoutedEventArgs evtargs) { std::vector<int> selectedRows = ui_create_listview_selection(sender.as<ListView>()); UiListSelection selection; selection.rows = selectedRows.data(); selection.count = selectedRows.size(); UiEvent evt; evt.obj = obj; evt.window = obj->window; evt.document = obj->ctx->document; evt.eventdata = &selection; evt.intval = 0; onselection(&evt, cbdata); }); } if (args.onactivate) { ui_callback cb = args.onactivate; void* cbdata = args.onactivatedata; listview.ItemClick([cb, cbdata, obj](IInspectable const& sender, RoutedEventArgs evtargs) { std::vector<int> selectedRows = ui_create_listview_selection(sender.as<ListView>()); UiListSelection selection; selection.rows = selectedRows.data(); selection.count = selectedRows.size(); UiEvent evt; evt.obj = obj; evt.window = obj->window; evt.document = obj->ctx->document; evt.eventdata = &selection; evt.intval = 0; cb(&evt, cbdata); }); } // add listview to current container UI_APPLY_LAYOUT1(current, args); current->container->Add(listview, false); return widget; } UIWIDGET ui_combobox_create(UiObject* obj, UiListArgs args) { UiObject* current = uic_current_obj(obj); // create listview and toolkit wrapper ComboBox combobox = ComboBox(); UIElement elm = combobox; UiWidget* widget = new UiWidget(elm); widget->data1 = args.model; widget->data2 = args.getvalue; ui_context_add_widget_destructor(current->ctx, widget); // bind var UiVar* var = uic_widget_var(obj->ctx, current->ctx, args.list, args.varname, UI_VAR_LIST); if (var) { UiList* list = (UiList*)var->value; list->update = ui_simple_list_update; list->obj = widget; ui_simple_list_update(list, 0); } if (args.onactivate) { ui_callback cb = args.onactivate; void* cbdata = args.onactivatedata; combobox.SelectionChanged([cb, cbdata, obj](IInspectable const& sender, RoutedEventArgs evtargs) { int selectedrow = sender.as<ComboBox>().SelectedIndex(); UiListSelection selection; selection.count = 1; selection.rows = &selectedrow; UiEvent evt; evt.obj = obj; evt.window = obj->window; evt.document = obj->ctx->document; evt.eventdata = &selection; evt.intval = selectedrow; cb(&evt, cbdata); }); } // add listview to current container UI_APPLY_LAYOUT1(current, args); current->container->Add(combobox, false); return widget; } UIEXPORT UIWIDGET ui_breadcrumbbar_create(UiObject* obj, UiListArgs args) { UiObject* current = uic_current_obj(obj); // create listview and toolkit wrapper BreadcrumbBar bcbar = BreadcrumbBar(); UIElement elm = bcbar; UiWidget* widget = new UiWidget(elm); widget->data1 = args.model; widget->data2 = args.getvalue; ui_context_add_widget_destructor(current->ctx, widget); // bind var UiVar* var = uic_widget_var(obj->ctx, current->ctx, args.list, args.varname, UI_VAR_LIST); if (var) { UiList* list = (UiList*)var->value; list->update = ui_breadcrumbbar_update; list->obj = widget; ui_breadcrumbbar_update(list, 0); } if (args.onactivate) { ui_callback cb = args.onactivate; void* cbdata = args.onactivatedata; bcbar.ItemClicked([cb, cbdata, obj](IInspectable const& sender, BreadcrumbBarItemClickedEventArgs evtargs) { UiEvent evt; evt.obj = obj; evt.window = obj->window; evt.document = obj->ctx->document; evt.eventdata = nullptr; evt.intval = evtargs.Index(); cb(&evt, cbdata); }); } // add listview to current container UI_APPLY_LAYOUT1(current, args); current->container->Add(bcbar, false); return widget; } extern "C" static void destroy_ui_pathbar(void* ptr) { UiPathBar* pb = (UiPathBar*)ptr; delete pb; } static void ui_context_add_pathbar_destructor(UiContext* ctx, UiPathBar* pb) { cxMempoolRegister(ctx->mp, pb, destroy_ui_pathbar); } UIEXPORT UIWIDGET ui_pathbar_create(UiObject* obj, UiPathBarArgs args) { UiObject* current = uic_current_obj(obj); // create view and toolkit wrapper Border pathbar = Border(); IInspectable bgRes = Application::Current().Resources().Lookup(box_value(L"TextControlBackground")); IInspectable borderThicknessRes = Application::Current().Resources().Lookup(box_value(L"TextControlBorderThemeThickness")); IInspectable borderBrushRes = Application::Current().Resources().Lookup(box_value(L"TextControlBorderBrush")); // IInspectable cornerRes = Application::Current().Resources().Lookup(box_value(L"TextControlCornerRadius")); Brush bgBrush = unbox_value<Brush>(bgRes); Thickness border = unbox_value<Thickness>(borderThicknessRes); Brush borderBrush = unbox_value<Brush>(borderBrushRes); CornerRadius cornerRadius = { 4, 4, 4, 4 }; //unbox_value<CornerRadius>(cornerRes); pathbar.Background(bgBrush); pathbar.BorderBrush(borderBrush); pathbar.BorderThickness(border); pathbar.CornerRadius(cornerRadius); Grid content = Grid(); pathbar.Child(content); GridLength gl; gl.Value = 1; gl.GridUnitType = GridUnitType::Star; ColumnDefinition coldef = ColumnDefinition(); coldef.Width(gl); content.ColumnDefinitions().Append(coldef); TextBox pathTextBox = TextBox(); Thickness t = { 0, 0, 0, 0 }; CornerRadius c = { 0 ,0, 0, 0 }; pathTextBox.BorderThickness(t); //pathTextBox.CornerRadius(c); pathTextBox.HorizontalAlignment(HorizontalAlignment::Stretch); content.SetColumn(pathTextBox, 0); content.Children().Append(pathTextBox); // stackpanel for buttons StackPanel buttons = StackPanel(); buttons.Orientation(Orientation::Horizontal); buttons.Visibility(Visibility::Collapsed); content.SetColumn(buttons, 0); content.Children().Append(buttons); if (args.ontextinput) { // TODO } //pathTextBox.Visibility(Visibility::Collapsed); UiPathBar* uipathbar = new UiPathBar; ui_context_add_pathbar_destructor(current->ctx, uipathbar); uipathbar->grid = content; uipathbar->buttons = buttons; uipathbar->textbox = pathTextBox; uipathbar->obj = obj; uipathbar->enabledrag = args.enabledrag; uipathbar->enabledrop = args.enabledrop; uipathbar->getvalue = args.getvalue; uipathbar->model = args.model; uipathbar->onactivate = args.onactivate; uipathbar->onactivatedata = args.onactivatedata; uipathbar->ondragstart = args.ondragstart; uipathbar->ondragstartdata = args.ondragstartdata; uipathbar->ondragcomplete = args.ondragcomplete; uipathbar->ondragcompletedata = args.ondragcompletedata; uipathbar->ondrop = args.ondrop; uipathbar->ondropdata = args.ondropsdata; UIElement elm = pathbar; UiWidget* widget = new UiWidget(elm); widget->data1 = uipathbar; ui_context_add_widget_destructor(current->ctx, widget); // bind var UiVar* var = uic_widget_var(obj->ctx, current->ctx, args.list, args.varname, UI_VAR_LIST); if (var) { UiList* list = (UiList*)var->value; list->update = ui_pathbar_update; list->obj = widget; ui_pathbar_update(list, 0); } // add listview to current container UI_APPLY_LAYOUT1(current, args); current->container->Add(pathbar, false); return widget; } static void* getstrvalue(void* elm, int ignore) { return elm; } void ui_simple_list_update(UiList* list, int i) { UiWidget* widget = (UiWidget*)list->obj; UiModel* model = (UiModel*)widget->data1; ui_getvaluefunc getvalue = (ui_getvaluefunc)widget->data2; ItemsControl listview = widget->uielement.as<ItemsControl>(); auto items = listview.Items(); // priority: getvalue, model.getvalue, getstrvalue (fallback) if (getvalue == nullptr) { if (model && model->getvalue) { getvalue = model->getvalue; } else { getvalue = getstrvalue; } } // add list elements to listview.Items items.Clear(); void* elm = list->first(list); while (elm) { char* value = (char*)getvalue(elm, 0); wchar_t* wstr = str2wstr(value, nullptr); items.Append(box_value(wstr)); free(wstr); elm = list->next(list); } } extern "C" void ui_breadcrumbbar_update(UiList * list, int i) { UiWidget* widget = (UiWidget*)list->obj; UiModel* model = (UiModel*)widget->data1; ui_getvaluefunc getvalue = (ui_getvaluefunc)widget->data2; // priority: getvalue, model.getvalue, getstrvalue (fallback) if (getvalue == nullptr) { if (model && model->getvalue) { getvalue = model->getvalue; } else { getvalue = getstrvalue; } } BreadcrumbBar bar = widget->uielement.as<BreadcrumbBar>(); Windows::Foundation::Collections::IVector<Windows::Foundation::IInspectable> items { winrt::single_threaded_vector<Windows::Foundation::IInspectable>() }; void* elm = list->first(list); while (elm) { char* value = (char*)getvalue(elm, 0); wchar_t* wstr = str2wstr(value, nullptr); items.Append(box_value(wstr)); free(wstr); elm = list->next(list); } bar.ItemsSource(items); } static void ui_pathbar_clear(StackPanel &buttons) { for (int i = buttons.Children().Size() - 1; i >= 0; i--) { buttons.Children().RemoveAt(i); } } extern "C" void ui_pathbar_update(UiList * list, int i) { UiWidget* widget = (UiWidget*)list->obj; UiPathBar* pb = (UiPathBar*)widget->data1; Grid grid = pb->grid; UiModel* model = pb->model; ui_getvaluefunc getvalue = pb->getvalue; // priority: getvalue, model.getvalue, getstrvalue (fallback) if (getvalue == nullptr) { if (model && model->getvalue) { getvalue = model->getvalue; } else { getvalue = getstrvalue; } } // hide textbox, show button panel pb->textbox.Visibility(Visibility::Collapsed); pb->buttons.Visibility(Visibility::Visible); // clear old buttons ui_pathbar_clear(pb->buttons); // add new buttons void* elm = list->first(list); int j = 0; while (elm) { char* value = (char*)getvalue(elm, 0); wchar_t* wstr = str2wstr(value, nullptr); Button button = Button(); button.Content(box_value(wstr)); free(wstr); if (pb->onactivate) { button.Click([pb, j](IInspectable const& sender, RoutedEventArgs) { UiEvent evt; evt.obj = pb->obj; evt.window = evt.obj->window; evt.document = evt.obj->ctx->document; evt.eventdata = nullptr; evt.intval = j; pb->onactivate(&evt, pb->onactivatedata); }); } Thickness t = { 0, 0, 1, 0 }; CornerRadius c = { 0 ,0, 0, 0 }; button.BorderThickness(t); button.CornerRadius(c); pb->buttons.Children().Append(button); j++; elm = list->next(list); } } std::vector<int> ui_create_listview_selection(ListView listview) { std::vector<int> selection; int p = 0; auto ranges = listview.SelectedRanges(); for (auto range : ranges) { int begin = range.FirstIndex(); int end = range.LastIndex(); for (int i = begin; i <= end; i++) { selection.push_back(i); } } return selection; }