add breadcrumb bar newapi

Fri, 06 Oct 2023 19:38:10 +0200

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Fri, 06 Oct 2023 19:38:10 +0200
branch
newapi
changeset 204
4a258d47f964
parent 203
0e94be3d9722
child 205
b1ac0dd1d38b

add breadcrumb bar

make/vs/testapp/main.c file | annotate | diff | comparison | revisions
ui/ui/tree.h file | annotate | diff | comparison | revisions
ui/winui/list.cpp file | annotate | diff | comparison | revisions
ui/winui/list.h file | annotate | diff | comparison | revisions
--- a/make/vs/testapp/main.c	Thu Oct 05 18:50:42 2023 +0200
+++ b/make/vs/testapp/main.c	Fri Oct 06 19:38:10 2023 +0200
@@ -45,6 +45,7 @@
     UiString* t2;
     UiString* t3;
     UiList* list2;
+    UiList* list3;
 } WindowData;
 
 void action1(UiEvent* event, void* data) {
@@ -123,6 +124,11 @@
 
 }
 
+void action_breadcrumb(UiEvent* event, void* data) {
+    int i = event->intval;
+    printf("index: %d\n", i);
+}
+
 void application_startup(UiEvent* event, void* data) {
     UiObject* obj = ui_window("Test", NULL);
     WindowData* wdata = ui_malloc(obj->ctx, sizeof(WindowData));
@@ -134,6 +140,7 @@
     wdata->password = ui_string_new(obj->ctx, "password");
     wdata->list = ui_list_new(obj->ctx, "list");
     wdata->list2 = ui_list_new(obj->ctx, "list2");
+    wdata->list3 = ui_list_new(obj->ctx, "list3");
     wdata->t1 = ui_string_new(obj->ctx, "t1");
     wdata->t2 = ui_string_new(obj->ctx, "t2");
     wdata->t3 = ui_string_new(obj->ctx, "t3");
@@ -145,6 +152,12 @@
     ui_list_append(wdata->list, "Item5");
     ui_list_append(wdata->list, "Item6");
 
+    ui_list_append(wdata->list3, "usr");
+    ui_list_append(wdata->list3, "share");
+    ui_list_append(wdata->list3, "test");
+    ui_list_append(wdata->list3, "dir");
+
+
     TableData* td1 = malloc(sizeof(TableData));
     TableData* td2 = malloc(sizeof(TableData));
     TableData* td3 = malloc(sizeof(TableData));
@@ -190,6 +203,9 @@
             ui_switch(obj, .label = "test", .onchange = action_switch);
             ui_newline(obj);
 
+            ui_breadcrumbbar(obj, .list = wdata->list3, .onactivate=action_breadcrumb);
+            ui_newline(obj);
+
             ui_textfield(obj, .value = wdata->text);
             ui_passwordfield(obj, .value = wdata->password);
             ui_newline(obj);
--- a/ui/ui/tree.h	Thu Oct 05 18:50:42 2023 +0200
+++ b/ui/ui/tree.h	Fri Oct 06 19:38:10 2023 +0200
@@ -134,27 +134,19 @@
 #define ui_listview(obj, ...) ui_listview_create(obj, (UiListArgs) { __VA_ARGS__ } )
 #define ui_table(obj, ...) ui_table_create(obj, (UiListArgs) { __VA_ARGS__ } )
 #define ui_combobox(obj, ...) ui_combobox_create(obj, (UiListArgs) { __VA_ARGS__ } )
+#define ui_breadcrumbbar(obj, ...) ui_breadcrumbbar_create(obj, (UiListArgs) { __VA_ARGS__ } )
 
 UIEXPORT UIWIDGET ui_listview_create(UiObject* obj, UiListArgs args);
 UIEXPORT UIWIDGET ui_table_create(UiObject* obj, UiListArgs args);
-UIEXPORT UIWIDGET ui_combobox_create(UiObject* obj, UiListArgs);
+UIEXPORT UIWIDGET ui_combobox_create(UiObject* obj, UiListArgs args);
+UIEXPORT UIWIDGET ui_breadcrumbbar_create(UiObject* obj, UiListArgs args);
 
-UIWIDGET ui_listview_deprecated(UiObject *obj, UiList *list, ui_getvaluefunc getvalue, ui_callback f, void *udata);
-UIWIDGET ui_listview_str_deprecated(UiObject *obj, UiList *list, ui_callback f, void *udata);
-UIWIDGET ui_listview_nv_deprecated(UiObject *obj, char *listname, ui_getvaluefunc getvalue, ui_callback f, void *udata);
-
-UIWIDGET ui_table_deprecated(UiObject *obj, UiList *data, UiModel *model, UiListCallbacks cb);
-UIWIDGET ui_table_nv_deprecated(UiObject *obj, char *varname, UiModel *model, UiListCallbacks cb);
 
 void ui_table_dragsource(UIWIDGET tablewidget, int actions, char *target0, ...);
 void ui_table_dragsource_a(UIWIDGET tablewidget, int actions, char **targets, int nelm);
 void ui_table_dragdest(UIWIDGET tablewidget, int actions, char *target0, ...);
 void ui_table_dragdest_a(UIWIDGET tablewidget, int actions, char **targets, int nelm);
 
-UIWIDGET ui_combobox_deprecated(UiObject *obj, UiList *list, ui_getvaluefunc getvalue, ui_callback f, void *udata);
-UIWIDGET ui_combobox_str_deprecated(UiObject *obj, UiList *list, ui_callback f, void *udata);
-UIWIDGET ui_combobox_nv_deprecated(UiObject *obj, char *varname, ui_getvaluefunc getvalue, ui_callback f, void *udata);
-
 #ifdef	__cplusplus
 }
 #endif
--- a/ui/winui/list.cpp	Thu Oct 05 18:50:42 2023 +0200
+++ b/ui/winui/list.cpp	Fri Oct 06 19:38:10 2023 +0200
@@ -165,6 +165,49 @@
     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;
+}
+
 static void* getstrvalue(void* elm, int ignore) {
     return elm;
 }
@@ -198,6 +241,37 @@
     }
 }
 
+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);
+}
+
 std::vector<int> ui_create_listview_selection(ListView listview) {
     std::vector<int> selection;
     int p = 0;
--- a/ui/winui/list.h	Thu Oct 05 18:50:42 2023 +0200
+++ b/ui/winui/list.h	Fri Oct 06 19:38:10 2023 +0200
@@ -35,5 +35,7 @@
 
 extern "C" void ui_simple_list_update(UiList * list, int i);
 
+extern "C" void ui_breadcrumbbar_update(UiList * list, int i);
+
 std::vector<int> ui_create_listview_selection(winrt::Microsoft::UI::Xaml::Controls::ListView listview);
 

mercurial