|
1 /* |
|
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
|
3 * |
|
4 * Copyright 2023 Olaf Wintermann. All rights reserved. |
|
5 * |
|
6 * Redistribution and use in source and binary forms, with or without |
|
7 * modification, are permitted provided that the following conditions are met: |
|
8 * |
|
9 * 1. Redistributions of source code must retain the above copyright |
|
10 * notice, this list of conditions and the following disclaimer. |
|
11 * |
|
12 * 2. Redistributions in binary form must reproduce the above copyright |
|
13 * notice, this list of conditions and the following disclaimer in the |
|
14 * documentation and/or other materials provided with the distribution. |
|
15 * |
|
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
|
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
|
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
|
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
|
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
|
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
|
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
|
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
|
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
|
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
|
26 * POSSIBILITY OF SUCH DAMAGE. |
|
27 */ |
|
28 |
|
29 #include "list.h" |
|
30 #include "container.h" |
|
31 #include "util.h" |
|
32 |
|
33 #include "../common/context.h" |
|
34 #include "../common/object.h" |
|
35 |
|
36 #include <winrt/Microsoft.UI.Xaml.Data.h> |
|
37 |
|
38 using namespace winrt; |
|
39 using namespace Microsoft::UI::Xaml; |
|
40 using namespace Microsoft::UI::Xaml::Controls; |
|
41 using namespace Windows::UI::Xaml::Interop; |
|
42 using namespace winrt::Windows::Foundation; |
|
43 using namespace winrt::Microsoft::UI::Xaml::Controls::Primitives; |
|
44 |
|
45 UIWIDGET ui_listview_create(UiObject* obj, UiListArgs args) { |
|
46 UiObject* current = uic_current_obj(obj); |
|
47 |
|
48 // create listview and toolkit wrapper |
|
49 ListView listview = ListView(); |
|
50 if (args.multiselection) { |
|
51 listview.SelectionMode(ListViewSelectionMode::Extended); |
|
52 } |
|
53 |
|
54 bool clickEnabled = listview.IsItemClickEnabled(); |
|
55 listview.IsItemClickEnabled(true); |
|
56 |
|
57 |
|
58 UIElement elm = listview; |
|
59 UiWidget* widget = new UiWidget(elm); |
|
60 widget->data1 = args.model; |
|
61 widget->data2 = args.getvalue; |
|
62 ui_context_add_widget_destructor(current->ctx, widget); |
|
63 |
|
64 // bind var |
|
65 UiVar* var = uic_widget_var(obj->ctx, current->ctx, args.list, args.varname, UI_VAR_LIST); |
|
66 if (var) { |
|
67 UiList* list = (UiList*)var->value; |
|
68 list->update = ui_simple_list_update; |
|
69 list->obj = widget; |
|
70 ui_simple_list_update(list, 0); |
|
71 } |
|
72 |
|
73 if (args.onselection) { |
|
74 ui_callback onselection = args.onselection; |
|
75 void* cbdata = args.onselectiondata; |
|
76 listview.SelectionChanged([onselection, cbdata, obj](IInspectable const& sender, RoutedEventArgs evtargs) { |
|
77 std::vector<int> selectedRows = ui_create_listview_selection(sender.as<ListView>()); |
|
78 |
|
79 UiListSelection selection; |
|
80 selection.rows = selectedRows.data(); |
|
81 selection.count = selectedRows.size(); |
|
82 |
|
83 UiEvent evt; |
|
84 evt.obj = obj; |
|
85 evt.window = obj->window; |
|
86 evt.document = obj->ctx->document; |
|
87 evt.eventdata = &selection; |
|
88 evt.intval = 0; |
|
89 onselection(&evt, cbdata); |
|
90 }); |
|
91 } |
|
92 if (args.onactivate) { |
|
93 ui_callback cb = args.onactivate; |
|
94 void* cbdata = args.onactivatedata; |
|
95 listview.ItemClick([cb, cbdata, obj](IInspectable const& sender, RoutedEventArgs evtargs) { |
|
96 std::vector<int> selectedRows = ui_create_listview_selection(sender.as<ListView>()); |
|
97 UiListSelection selection; |
|
98 selection.rows = selectedRows.data(); |
|
99 selection.count = selectedRows.size(); |
|
100 |
|
101 UiEvent evt; |
|
102 evt.obj = obj; |
|
103 evt.window = obj->window; |
|
104 evt.document = obj->ctx->document; |
|
105 evt.eventdata = &selection; |
|
106 evt.intval = 0; |
|
107 cb(&evt, cbdata); |
|
108 }); |
|
109 } |
|
110 |
|
111 // add listview to current container |
|
112 UI_APPLY_LAYOUT1(current, args); |
|
113 |
|
114 current->container->Add(listview, false); |
|
115 |
|
116 return widget; |
|
117 } |
|
118 |
|
119 static void* getstrvalue(void* elm, int ignore) { |
|
120 return elm; |
|
121 } |
|
122 |
|
123 void ui_simple_list_update(UiList* list, int i) { |
|
124 UiWidget* widget = (UiWidget*)list->obj; |
|
125 UiModel* model = (UiModel*)widget->data1; |
|
126 ui_getvaluefunc getvalue = (ui_getvaluefunc)widget->data2; |
|
127 ListView listview = widget->uielement.as<ListView>(); |
|
128 auto items = listview.Items(); |
|
129 |
|
130 // priority: getvalue, model.getvalue, getstrvalue (fallback) |
|
131 if (getvalue == nullptr) { |
|
132 if (model && model->getvalue) { |
|
133 getvalue = model->getvalue; |
|
134 } else { |
|
135 getvalue = getstrvalue; |
|
136 } |
|
137 } |
|
138 |
|
139 // add list elements to listview.Items |
|
140 items.Clear(); |
|
141 void* elm = list->first(list); |
|
142 while (elm) { |
|
143 char* value = (char*)getvalue(elm, 0); |
|
144 wchar_t* wstr = str2wstr(value, nullptr); |
|
145 items.Append(box_value(wstr)); |
|
146 free(wstr); |
|
147 |
|
148 elm = list->next(list); |
|
149 } |
|
150 } |
|
151 |
|
152 std::vector<int> ui_create_listview_selection(ListView listview) { |
|
153 std::vector<int> selection; |
|
154 int p = 0; |
|
155 auto ranges = listview.SelectedRanges(); |
|
156 for (auto range : ranges) { |
|
157 int begin = range.FirstIndex(); |
|
158 int end = range.LastIndex(); |
|
159 for (int i = begin; i <= end; i++) { |
|
160 selection.push_back(i); |
|
161 } |
|
162 } |
|
163 return selection; |
|
164 } |
|
165 |