ui/winui/table.cpp

changeset 0
2483f517c562
child 3
f154867f54dc
equal deleted inserted replaced
-1:000000000000 0:2483f517c562
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 "table.h"
32 #include "container.h"
33 #include "util.h"
34 #include "icons.h"
35
36 #include "../common/context.h"
37 #include "../common/object.h"
38 #include "../common/types.h"
39
40 #include <winrt/Microsoft.UI.Xaml.Data.h>
41 #include <winrt/Microsoft.UI.Xaml.Media.h>
42 #include <winrt/Microsoft.UI.Xaml.Input.h>
43 #include <winrt/Windows.UI.Core.h>
44 #include <winrt/Windows.ApplicationModel.h>
45 #include <winrt/Windows.ApplicationModel.DataTransfer.h>
46
47 using namespace winrt;
48 using namespace Microsoft::UI::Xaml;
49 using namespace Microsoft::UI::Xaml::Controls;
50 using namespace Windows::UI::Xaml::Interop;
51 using namespace winrt::Windows::Foundation;
52 using namespace winrt::Microsoft::UI::Xaml::Controls::Primitives;
53 using namespace winrt::Microsoft::UI::Xaml::Media;
54 using namespace winrt::Windows::UI::Xaml::Input;
55
56 static UINT ui_double_click_time = GetDoubleClickTime();
57
58 extern "C" void reg_table_destructor(UiContext * ctx, UiTable * table) {
59 // TODO:
60 }
61
62 UIEXPORT UIWIDGET ui_table_create(UiObject* obj, UiListArgs args) {
63 if (!args.model) {
64 return nullptr;
65 }
66
67 UiObject* current = uic_current_obj(obj);
68
69 // create widgets and wrapper obj
70 ScrollViewer scrollW = ScrollViewer();
71 Grid grid = Grid();
72 scrollW.Content(grid);
73 UiTable* uitable = new UiTable(obj, scrollW, grid);
74 reg_table_destructor(current->ctx, uitable);
75
76 uitable->getvalue = args.model->getvalue ? args.model->getvalue : args.getvalue;
77 uitable->onselection = args.onselection;
78 uitable->onselectiondata = args.onselectiondata;
79 uitable->onactivate = args.onactivate;
80 uitable->onactivatedata = args.onactivatedata;
81 uitable->ondragstart = args.ondragstart;
82 uitable->ondragstartdata = args.ondragstartdata;
83 uitable->ondragcomplete = args.ondragcomplete;
84 uitable->ondrop = args.ondrop;
85 uitable->ondropdata = args.ondropsdata;
86
87 // grid styling
88 winrt::Windows::UI::Color bg = { 255, 255, 255, 255 }; // test color
89 SolidColorBrush brush = SolidColorBrush(bg);
90 grid.Background(brush);
91
92 // add columns from args.model
93 uitable->add_header(args.model);
94
95 // bind var
96 UiVar* var = uic_widget_var(obj->ctx, current->ctx, args.list, args.varname, UI_VAR_LIST);
97 if (var) {
98 UiList* list = (UiList*)var->value;
99 list->update = ui_table_update;
100 list->obj = uitable;
101 uitable->update(list, 0);
102 }
103
104 // create toolkit wrapper object and register destructor
105 UIElement elm = scrollW;
106 UiWidget* widget = new UiWidget(elm);
107 ui_context_add_widget_destructor(current->ctx, widget);
108
109 // add scrollW to current container
110 UI_APPLY_LAYOUT1(current, args);
111
112 current->container->Add(scrollW, false);
113
114 return widget;
115 }
116
117 extern "C" void ui_table_update(UiList * list, int i) {
118 UiTable* table = (UiTable*)list->obj;
119 table->clear();
120 table->update(list, i);
121 }
122
123 UiTable::UiTable(UiObject *obj, winrt::Microsoft::UI::Xaml::Controls::ScrollViewer scrollW, winrt::Microsoft::UI::Xaml::Controls::Grid grid) {
124 this->obj = obj;
125
126 this->scrollw = scrollw;
127 this->grid = grid;
128
129 winrt::Windows::UI::Color highlightBg = { 255, 234, 234, 234 };
130 highlightBrush = SolidColorBrush(highlightBg);
131
132 winrt::Windows::UI::Color defaultBg = { 0, 0, 0, 0 }; // default
133 defaultBrush = SolidColorBrush(defaultBg);
134
135 winrt::Windows::UI::Color selectedBg = { 255, 204, 232, 255 }; // test color
136 selectedBrush = SolidColorBrush(selectedBg);
137
138 winrt::Windows::UI::Color selectedFg = { 255, 0, 90, 158 }; // test color
139 selectedBorderBrush = SolidColorBrush(selectedFg);
140
141 grid.KeyDown(
142 winrt::Microsoft::UI::Xaml::Input::KeyEventHandler(
143 [=](IInspectable const& sender, winrt::Microsoft::UI::Xaml::Input::KeyRoutedEventArgs const& args) {
144 // key event for hanling the table cursor or enter
145 })
146 );
147 }
148
149 UiTable::~UiTable() {
150 ui_model_free(NULL, model);
151 }
152
153 void UiTable::add_header(UiModel* model) {
154 this->model = ui_model_copy(NULL, model);
155
156 GridLength gl;
157 gl.Value = 0;
158 gl.GridUnitType = GridUnitType::Auto;
159
160 // add header row definition
161 auto headerRowDef = RowDefinition();
162 headerRowDef.Height(gl);
163 grid.RowDefinitions().Append(headerRowDef);
164
165
166 for (int i = 0; i < model->columns;i++) {
167 char* title = model->titles[i];
168 UiModelType type = model->types[i];
169
170 // add grid column definition
171 auto colDef = ColumnDefinition();
172 colDef.Width(gl);
173 grid.ColumnDefinitions().Append(colDef);
174
175 // add button
176 auto button = Button();
177 wchar_t* wlabel = str2wstr(title, nullptr);
178 button.Content(box_value(wlabel));
179 free(wlabel);
180
181 // some styling for the button
182 Thickness border = { 0,0,1,0 };
183 CornerRadius corner = { 0,0,0,0 };
184 button.BorderThickness(border);
185 button.CornerRadius(corner);
186
187 grid.SetColumn(button, i);
188 grid.SetRow(button, 0);
189 grid.Children().Append(button);
190
191 UiTableColumn h;
192 h.header = button;
193 header.push_back(h);
194 }
195
196 maxrows = 1;
197 }
198
199 static void textblock_set_str(TextBlock &t, const char *str) {
200 if (str) {
201 wchar_t* wstr = str2wstr(str, nullptr);
202 t.Text(winrt::hstring(wstr));
203 free(wstr);
204 }
205 }
206
207 static void textblock_set_int(TextBlock& t, int i) {
208 wchar_t buf[16];
209 swprintf(buf, 16, L"%d", i);
210 t.Text(winrt::hstring(buf));
211 }
212
213 static ULONG64 getsystime() {
214 SYSTEMTIME st;
215 GetSystemTime(&st);
216 return st.wYear * 10000000000000 +
217 st.wMonth * 100000000000 +
218 st.wDay * 1000000000 +
219 st.wHour * 10000000 +
220 st.wMinute * 100000 +
221 st.wSecond * 1000 +
222 st.wMilliseconds;
223 }
224
225 void UiTable::update(UiList* list, int i) {
226 if (getvalue == nullptr) {
227 return;
228 }
229
230 Thickness b1 = { 1, 1, 0, 1 }; // first col
231 Thickness b2 = { 0, 1, 0, 1 }; // middle
232 Thickness b3 = { 0, 1, 1, 1 }; // last col
233
234 GridLength gl;
235 gl.Value = 0;
236 gl.GridUnitType = GridUnitType::Auto;
237
238 // iterate model
239 int row = 1;
240 void* elm = list->first(list);
241 while (elm) {
242 if (row >= maxrows) {
243 auto rowdef = RowDefinition();
244 rowdef.Height(gl);
245 grid.RowDefinitions().Append(rowdef);
246 maxrows = row;
247 }
248
249 Thickness cellpadding = { 10,0,4,0 };
250
251 // model column, usually the same as col, however UI_ICON_TEXT uses two columns in the model
252 int model_col = 0;
253 for (int col = 0; col < header.size(); col++, model_col++) {
254 // create ui elements with the correct cell border
255 // dependeing on the column
256 Border cellBorder = Border();
257 cellBorder.Background(defaultBrush);
258 cellBorder.BorderBrush(defaultBrush);
259 if (col == 0) {
260 cellBorder.BorderThickness(b1);
261 }
262 else if (col + 1 == header.size()) {
263 cellBorder.BorderThickness(b3);
264 }
265 else {
266 cellBorder.BorderThickness(b2);
267 }
268
269 // dnd
270 if (ondragstart) {
271 cellBorder.CanDrag(true);
272 cellBorder.DragStarting([this](IInspectable const& sender, DragStartingEventArgs args) {
273 UiDnD dnd;
274 dnd.evttype = 0;
275 dnd.dndstartargs = args;
276 dnd.dndcompletedargs = { nullptr };
277 dnd.drageventargs = { nullptr };
278 dnd.data = args.Data();
279
280 UiListDnd dndevt;
281 dndevt.selection = uiselection();
282 dndevt.dnd = &dnd;
283
284 UiEvent evt;
285 evt.obj = this->obj;
286 evt.window = evt.obj->window;
287 evt.document = obj->ctx->document;
288 evt.eventdata = &dndevt;
289 evt.intval = 0;
290
291 this->ondragstart(&evt, this->ondragstartdata);
292
293 if (dndevt.selection.rows) {
294 free(dndevt.selection.rows);
295 }
296 });
297 cellBorder.DropCompleted([this](IInspectable const& sender, DropCompletedEventArgs args) {
298 UiDnD dnd;
299 dnd.evttype = 1;
300 dnd.dndstartargs = { nullptr };
301 dnd.dndcompletedargs = args;
302 dnd.drageventargs = { nullptr };
303 dnd.data = { nullptr };
304
305 UiListDnd dndevt;
306 dndevt.selection = uiselection();
307 dndevt.dnd = &dnd;
308
309 UiEvent evt;
310 evt.obj = this->obj;
311 evt.window = evt.obj->window;
312 evt.document = obj->ctx->document;
313 evt.eventdata = &dndevt;
314 evt.intval = 0;
315
316 if (this->ondragcomplete) {
317 this->ondragcomplete(&evt, this->ondragcompletedata);
318 }
319 if (dndevt.selection.rows) {
320 free(dndevt.selection.rows);
321 }
322 });
323 }
324 if (ondrop) {
325 cellBorder.AllowDrop(true);
326 cellBorder.Drop(DragEventHandler([this](winrt::Windows::Foundation::IInspectable const& sender, DragEventArgs const& args){
327 UiDnD dnd;
328 dnd.evttype = 2;
329 dnd.dndstartargs = { nullptr };
330 dnd.dndcompletedargs = { nullptr };
331 dnd.drageventargs = args;
332 dnd.data = args.Data();
333
334 UiListDnd dndevt;
335 dndevt.selection = uiselection();
336 dndevt.dnd = &dnd;
337
338 UiEvent evt;
339 evt.obj = this->obj;
340 evt.window = evt.obj->window;
341 evt.document = obj->ctx->document;
342 evt.eventdata = &dndevt;
343 evt.intval = 0;
344
345 this->ondrop(&evt, this->ondropdata);
346
347 if (dndevt.selection.rows) {
348 free(dndevt.selection.rows);
349 }
350 }));
351 }
352
353 // set the cell value
354 // depending on the type, we create different cell controls
355 UiModelType type = model->types[col];
356 switch (type) {
357 case UI_STRING: {
358 TextBlock cell = TextBlock();
359 cell.Padding(cellpadding);
360 cell.VerticalAlignment(VerticalAlignment::Stretch);
361 textblock_set_str(cell, (char*)getvalue(elm, model_col));
362 cellBorder.Child(cell);
363
364 break;
365 }
366 case UI_INTEGER: {
367 TextBlock cell = TextBlock();
368 cell.Padding(cellpadding);
369 cell.VerticalAlignment(VerticalAlignment::Stretch);
370 int *value = (int*)getvalue(elm, model_col);
371 if (value) {
372 textblock_set_int(cell, *value);
373 }
374 cellBorder.Child(cell);
375 break;
376 }
377 case UI_ICON: {
378 UiIcon* iconConstr = (UiIcon*)getvalue(elm, model_col);
379 if (iconConstr) {
380 IconElement icon = iconConstr->getIcon();
381 cellBorder.Child(icon);
382 }
383 break;
384 }
385 case UI_ICON_TEXT: {
386 StackPanel cellPanel = StackPanel();
387 cellPanel.Spacing(2);
388 cellPanel.Padding(cellpadding);
389 cellPanel.VerticalAlignment(VerticalAlignment::Stretch);
390
391 cellPanel.Orientation(Orientation::Horizontal);
392 UiIcon* iconConstr = (UiIcon*)getvalue(elm, model_col++);
393 char* str = (char*)getvalue(elm, model_col);
394 if (iconConstr) {
395 IconElement icon = iconConstr->getIcon();
396 cellPanel.Children().Append(icon);
397 }
398 TextBlock cell = TextBlock();
399 textblock_set_str(cell, str);
400 cellPanel.Children().Append(cell);
401 cellBorder.Child(cellPanel);
402 break;
403 }
404 }
405
406 // event handler
407 cellBorder.PointerPressed(
408 winrt::Microsoft::UI::Xaml::Input::PointerEventHandler(
409 [=](IInspectable const& sender, winrt::Microsoft::UI::Xaml::Input::PointerRoutedEventArgs const& args) {
410 winrt::Windows::System::VirtualKeyModifiers modifiers = args.KeyModifiers();
411 bool update_selection = true;
412
413 if (modifiers == winrt::Windows::System::VirtualKeyModifiers::Control) {
414 // add/remove current row
415 if (!is_row_selected(row)) {
416 row_background(row, selectedBrush, selectedBorderBrush);
417 selection.push_back(row);
418 }
419 else {
420 row_background(row, highlightBrush, highlightBrush);
421 remove_from_selection(row);
422 }
423 }
424 else if (modifiers == winrt::Windows::System::VirtualKeyModifiers::None || selection.size() == 0) {
425 // no modifier or shift is pressed but there is no selection
426 if (selection.size() > 0) {
427 change_rows_bg(selection, defaultBrush, defaultBrush);
428 }
429
430 row_background(row, selectedBrush, selectedBorderBrush);
431 selection = { row };
432 if (modifiers == winrt::Windows::System::VirtualKeyModifiers::None) {
433 SYSTEMTIME st;
434 GetSystemTime(&st);
435
436 ULONG64 now = getsystime();
437 ULONG64 tdiff = now - lastPointerPress;
438 if (tdiff < ui_double_click_time && onactivate != nullptr) {
439 // two pointer presse events in short time and we have an onactivate handler
440 update_selection = false; // we don't want an additional selection event
441 lastPointerPress = 0; // reset double-click
442
443 int selectedrow = row - 1; // subtract header row
444
445 UiListSelection selection;
446 selection.count = 1;
447 selection.rows = &selectedrow;
448
449 UiEvent evt;
450 evt.obj = obj;
451 evt.window = obj->window;
452 evt.document = obj->ctx->document;
453 evt.eventdata = &selection;
454 evt.intval = selectedrow;
455 onactivate(&evt, onactivatedata);
456 }
457 else {
458 lastPointerPress = now;
459 }
460 }
461 }
462 else if (modifiers == winrt::Windows::System::VirtualKeyModifiers::Shift) {
463 // select everything between the first selection and the current row
464 std::sort(selection.begin(), selection.end());
465 int first = selection.front();
466 int last = row;
467 if (first > row) {
468 last = first;
469 first = row;
470 }
471
472 // clear previous selection
473 change_rows_bg(selection, defaultBrush, defaultBrush);
474
475 // create new selection
476 std::vector<int> newselection;
477 for (int s = first; s <= last; s++) {
478 newselection.push_back(s);
479 }
480 selection = newselection;
481 change_rows_bg(selection, selectedBrush, selectedBorderBrush);
482 }
483
484 if (update_selection) {
485 call_handler(onselection, onselectiondata);
486 }
487 })
488 );
489 cellBorder.PointerReleased(
490 winrt::Microsoft::UI::Xaml::Input::PointerEventHandler(
491 [=](IInspectable const& sender, winrt::Microsoft::UI::Xaml::Input::PointerRoutedEventArgs const& args) {
492
493 })
494 );
495 cellBorder.PointerEntered(
496 winrt::Microsoft::UI::Xaml::Input::PointerEventHandler(
497 [=](IInspectable const& sender, winrt::Microsoft::UI::Xaml::Input::PointerRoutedEventArgs const& args) {
498 if (!is_row_selected(row)) {
499 row_background(row, highlightBrush, highlightBrush);
500 }
501 })
502 );
503 cellBorder.PointerExited(
504 winrt::Microsoft::UI::Xaml::Input::PointerEventHandler(
505 [=](IInspectable const& sender, winrt::Microsoft::UI::Xaml::Input::PointerRoutedEventArgs const& args) {
506 if (!is_row_selected(row)) {
507 row_background(row, defaultBrush, defaultBrush);
508 }
509 })
510 );
511
512 grid.SetColumn(cellBorder, col);
513 grid.SetRow(cellBorder, row);
514 grid.Children().Append(cellBorder);
515 }
516
517 row++;
518 elm = list->next(list);
519 }
520 }
521
522 void UiTable::clear() {
523 for (int i = grid.Children().Size()-1; i >= 0; i--) {
524 FrameworkElement elm = grid.Children().GetAt(i).as<FrameworkElement>();
525 int child_row = grid.GetRow(elm);
526 if (child_row > 0) {
527 grid.Children().RemoveAt(i);
528 }
529 }
530
531 // TODO: should we clean row definitions?
532 }
533
534 void UiTable::row_background(int row, winrt::Microsoft::UI::Xaml::Media::Brush brush, winrt::Microsoft::UI::Xaml::Media::Brush borderBrush) {
535 Thickness b1 = { 1, 1, 0, 1 }; // first col
536 Thickness b2 = { 0, 1, 0, 1 }; // middle
537 Thickness b3 = { 0, 1, 1, 1 }; // last col
538
539 for (auto child : grid.Children()) {
540 FrameworkElement elm = child.as<FrameworkElement>();
541 int child_row = grid.GetRow(elm);
542 if (child_row == row) {
543 Border b = elm.as<Border>();
544 b.Background(brush);
545 b.BorderBrush(borderBrush);
546 }
547 }
548 }
549
550 void UiTable::change_rows_bg(std::vector<int> rows, winrt::Microsoft::UI::Xaml::Media::Brush brush, winrt::Microsoft::UI::Xaml::Media::Brush borderBrush) {
551 std::for_each(rows.cbegin(), rows.cend(), [&](const int& row) {row_background(row, brush, borderBrush); });
552 }
553
554 bool UiTable::is_row_selected(int row) {
555 return std::find(selection.begin(), selection.end(), row) != selection.end() ? true : false;
556 }
557
558 void UiTable::remove_from_selection(int row) {
559 selection.erase(std::remove(selection.begin(), selection.end(), row), selection.end());
560 selection.shrink_to_fit();
561 }
562
563 UiListSelection UiTable::uiselection() {
564 std::sort(selection.begin(), selection.end());
565
566 UiListSelection selobj;
567 selobj.count = selection.size();
568 selobj.rows = nullptr;
569 if (selobj.count > 0) {
570 selobj.rows = (int*)calloc(selobj.count, sizeof(int));
571 memcpy(selobj.rows, selection.data(), selobj.count * sizeof(int));
572 for (int i = 0; i < selobj.count; i++) {
573 selobj.rows[i]--;
574 }
575 }
576 return selobj;
577 }
578
579 void UiTable::call_handler(ui_callback cb, void* cbdata) {
580 if (!cb) {
581 return;
582 }
583
584 UiListSelection selobj = uiselection();
585
586 UiEvent evt;
587 evt.obj = obj;
588 evt.window = obj->window;
589 evt.document = obj->ctx->document;
590 evt.eventdata = &selobj;
591 evt.intval = 0;
592 cb(&evt, cbdata);
593
594 if (selobj.rows) {
595 free(selobj.rows);
596 }
597 }

mercurial