|
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 "pch.h" |
|
30 |
|
31 #include "list.h" |
|
32 #include "container.h" |
|
33 #include "util.h" |
|
34 |
|
35 #include "../common/context.h" |
|
36 #include "../common/object.h" |
|
37 |
|
38 |
|
39 using namespace winrt; |
|
40 using namespace Microsoft::UI::Xaml; |
|
41 using namespace Microsoft::UI::Xaml::Controls; |
|
42 using namespace Windows::UI::Xaml::Interop; |
|
43 using namespace winrt::Windows::Foundation; |
|
44 using namespace Microsoft::UI::Xaml::Markup; |
|
45 using namespace Microsoft::UI::Xaml::Media; |
|
46 using namespace winrt::Microsoft::UI::Xaml::Controls::Primitives; |
|
47 |
|
48 |
|
49 UIWIDGET ui_listview_create(UiObject* obj, UiListArgs args) { |
|
50 UiObject* current = uic_current_obj(obj); |
|
51 |
|
52 // create listview and toolkit wrapper |
|
53 ListView listview = ListView(); |
|
54 if (args.multiselection) { |
|
55 listview.SelectionMode(ListViewSelectionMode::Extended); |
|
56 } |
|
57 |
|
58 bool clickEnabled = listview.IsItemClickEnabled(); |
|
59 listview.IsItemClickEnabled(true); |
|
60 |
|
61 |
|
62 UIElement elm = listview; |
|
63 UiWidget* widget = new UiWidget(elm); |
|
64 widget->data1 = args.model; |
|
65 widget->data2 = args.getvalue; |
|
66 ui_context_add_widget_destructor(current->ctx, widget); |
|
67 |
|
68 // bind var |
|
69 UiVar* var = uic_widget_var(obj->ctx, current->ctx, args.list, args.varname, UI_VAR_LIST); |
|
70 if (var) { |
|
71 UiList* list = (UiList*)var->value; |
|
72 list->update = ui_simple_list_update; |
|
73 list->obj = widget; |
|
74 ui_simple_list_update(list, 0); |
|
75 } |
|
76 |
|
77 if (args.onselection) { |
|
78 ui_callback onselection = args.onselection; |
|
79 void* cbdata = args.onselectiondata; |
|
80 listview.SelectionChanged([onselection, cbdata, obj](IInspectable const& sender, RoutedEventArgs evtargs) { |
|
81 std::vector<int> selectedRows = ui_create_listview_selection(sender.as<ListView>()); |
|
82 |
|
83 UiListSelection selection; |
|
84 selection.rows = selectedRows.data(); |
|
85 selection.count = selectedRows.size(); |
|
86 |
|
87 UiEvent evt; |
|
88 evt.obj = obj; |
|
89 evt.window = obj->window; |
|
90 evt.document = obj->ctx->document; |
|
91 evt.eventdata = &selection; |
|
92 evt.intval = 0; |
|
93 onselection(&evt, cbdata); |
|
94 }); |
|
95 } |
|
96 if (args.onactivate) { |
|
97 ui_callback cb = args.onactivate; |
|
98 void* cbdata = args.onactivatedata; |
|
99 listview.ItemClick([cb, cbdata, obj](IInspectable const& sender, RoutedEventArgs evtargs) { |
|
100 std::vector<int> selectedRows = ui_create_listview_selection(sender.as<ListView>()); |
|
101 UiListSelection selection; |
|
102 selection.rows = selectedRows.data(); |
|
103 selection.count = selectedRows.size(); |
|
104 |
|
105 UiEvent evt; |
|
106 evt.obj = obj; |
|
107 evt.window = obj->window; |
|
108 evt.document = obj->ctx->document; |
|
109 evt.eventdata = &selection; |
|
110 evt.intval = 0; |
|
111 cb(&evt, cbdata); |
|
112 }); |
|
113 } |
|
114 |
|
115 // add listview to current container |
|
116 UI_APPLY_LAYOUT1(current, args); |
|
117 |
|
118 current->container->Add(listview, false); |
|
119 |
|
120 return widget; |
|
121 } |
|
122 |
|
123 |
|
124 UIWIDGET ui_combobox_create(UiObject* obj, UiListArgs args) { |
|
125 UiObject* current = uic_current_obj(obj); |
|
126 |
|
127 // create listview and toolkit wrapper |
|
128 ComboBox combobox = ComboBox(); |
|
129 |
|
130 UIElement elm = combobox; |
|
131 UiWidget* widget = new UiWidget(elm); |
|
132 widget->data1 = args.model; |
|
133 widget->data2 = args.getvalue; |
|
134 ui_context_add_widget_destructor(current->ctx, widget); |
|
135 |
|
136 // bind var |
|
137 UiVar* var = uic_widget_var(obj->ctx, current->ctx, args.list, args.varname, UI_VAR_LIST); |
|
138 if (var) { |
|
139 UiList* list = (UiList*)var->value; |
|
140 list->update = ui_simple_list_update; |
|
141 list->obj = widget; |
|
142 ui_simple_list_update(list, 0); |
|
143 } |
|
144 |
|
145 if (args.onactivate) { |
|
146 ui_callback cb = args.onactivate; |
|
147 void* cbdata = args.onactivatedata; |
|
148 combobox.SelectionChanged([cb, cbdata, obj](IInspectable const& sender, RoutedEventArgs evtargs) { |
|
149 int selectedrow = sender.as<ComboBox>().SelectedIndex(); |
|
150 UiListSelection selection; |
|
151 selection.count = 1; |
|
152 selection.rows = &selectedrow; |
|
153 |
|
154 UiEvent evt; |
|
155 evt.obj = obj; |
|
156 evt.window = obj->window; |
|
157 evt.document = obj->ctx->document; |
|
158 evt.eventdata = &selection; |
|
159 evt.intval = selectedrow; |
|
160 cb(&evt, cbdata); |
|
161 }); |
|
162 } |
|
163 |
|
164 // add listview to current container |
|
165 UI_APPLY_LAYOUT1(current, args); |
|
166 |
|
167 current->container->Add(combobox, false); |
|
168 |
|
169 return widget; |
|
170 } |
|
171 |
|
172 UIEXPORT UIWIDGET ui_breadcrumbbar_create(UiObject* obj, UiListArgs args) { |
|
173 UiObject* current = uic_current_obj(obj); |
|
174 |
|
175 // create listview and toolkit wrapper |
|
176 BreadcrumbBar bcbar = BreadcrumbBar(); |
|
177 |
|
178 UIElement elm = bcbar; |
|
179 UiWidget* widget = new UiWidget(elm); |
|
180 widget->data1 = args.model; |
|
181 widget->data2 = args.getvalue; |
|
182 ui_context_add_widget_destructor(current->ctx, widget); |
|
183 |
|
184 // bind var |
|
185 UiVar* var = uic_widget_var(obj->ctx, current->ctx, args.list, args.varname, UI_VAR_LIST); |
|
186 if (var) { |
|
187 UiList* list = (UiList*)var->value; |
|
188 list->update = ui_breadcrumbbar_update; |
|
189 list->obj = widget; |
|
190 ui_breadcrumbbar_update(list, 0); |
|
191 } |
|
192 |
|
193 if (args.onactivate) { |
|
194 ui_callback cb = args.onactivate; |
|
195 void* cbdata = args.onactivatedata; |
|
196 bcbar.ItemClicked([cb, cbdata, obj](IInspectable const& sender, BreadcrumbBarItemClickedEventArgs evtargs) { |
|
197 UiEvent evt; |
|
198 evt.obj = obj; |
|
199 evt.window = obj->window; |
|
200 evt.document = obj->ctx->document; |
|
201 evt.eventdata = nullptr; |
|
202 evt.intval = evtargs.Index(); |
|
203 cb(&evt, cbdata); |
|
204 }); |
|
205 } |
|
206 |
|
207 // add listview to current container |
|
208 UI_APPLY_LAYOUT1(current, args); |
|
209 |
|
210 current->container->Add(bcbar, false); |
|
211 |
|
212 return widget; |
|
213 } |
|
214 |
|
215 static void* getstrvalue(void* elm, int ignore) { |
|
216 return elm; |
|
217 } |
|
218 |
|
219 void ui_simple_list_update(UiList* list, int i) { |
|
220 UiWidget* widget = (UiWidget*)list->obj; |
|
221 UiModel* model = (UiModel*)widget->data1; |
|
222 ui_getvaluefunc getvalue = (ui_getvaluefunc)widget->data2; |
|
223 ItemsControl listview = widget->uielement.as<ItemsControl>(); |
|
224 auto items = listview.Items(); |
|
225 |
|
226 // priority: getvalue, model.getvalue, getstrvalue (fallback) |
|
227 if (getvalue == nullptr) { |
|
228 if (model && model->getvalue) { |
|
229 getvalue = model->getvalue; |
|
230 } else { |
|
231 getvalue = getstrvalue; |
|
232 } |
|
233 } |
|
234 |
|
235 // add list elements to listview.Items |
|
236 items.Clear(); |
|
237 void* elm = list->first(list); |
|
238 while (elm) { |
|
239 char* value = (char*)getvalue(elm, 0); |
|
240 wchar_t* wstr = str2wstr(value, nullptr); |
|
241 items.Append(box_value(wstr)); |
|
242 free(wstr); |
|
243 |
|
244 elm = list->next(list); |
|
245 } |
|
246 } |
|
247 |
|
248 extern "C" void ui_breadcrumbbar_update(UiList * list, int i) { |
|
249 UiWidget* widget = (UiWidget*)list->obj; |
|
250 UiModel* model = (UiModel*)widget->data1; |
|
251 ui_getvaluefunc getvalue = (ui_getvaluefunc)widget->data2; |
|
252 |
|
253 // priority: getvalue, model.getvalue, getstrvalue (fallback) |
|
254 if (getvalue == nullptr) { |
|
255 if (model && model->getvalue) { |
|
256 getvalue = model->getvalue; |
|
257 } |
|
258 else { |
|
259 getvalue = getstrvalue; |
|
260 } |
|
261 } |
|
262 |
|
263 BreadcrumbBar bar = widget->uielement.as<BreadcrumbBar>(); |
|
264 |
|
265 Windows::Foundation::Collections::IVector<Windows::Foundation::IInspectable> items { winrt::single_threaded_vector<Windows::Foundation::IInspectable>() }; |
|
266 void* elm = list->first(list); |
|
267 while (elm) { |
|
268 char* value = (char*)getvalue(elm, 0); |
|
269 wchar_t* wstr = str2wstr(value, nullptr); |
|
270 items.Append(box_value(wstr)); |
|
271 free(wstr); |
|
272 |
|
273 elm = list->next(list); |
|
274 } |
|
275 |
|
276 bar.ItemsSource(items); |
|
277 } |
|
278 |
|
279 |
|
280 std::vector<int> ui_create_listview_selection(ListView listview) { |
|
281 std::vector<int> selection; |
|
282 int p = 0; |
|
283 auto ranges = listview.SelectedRanges(); |
|
284 for (auto range : ranges) { |
|
285 int begin = range.FirstIndex(); |
|
286 int end = range.LastIndex(); |
|
287 for (int i = begin; i <= end; i++) { |
|
288 selection.push_back(i); |
|
289 } |
|
290 } |
|
291 return selection; |
|
292 } |
|
293 |