Sun, 28 Jan 2024 09:35:44 +0100
add icon parameter for buttons (WinUI3)
make/vs/testapp/main.c | file | annotate | diff | comparison | revisions | |
ui/ui/button.h | file | annotate | diff | comparison | revisions | |
ui/ui/toolkit.h | file | annotate | diff | comparison | revisions | |
ui/winui/button.cpp | file | annotate | diff | comparison | revisions |
--- a/make/vs/testapp/main.c Fri Jan 26 17:17:14 2024 +0100 +++ b/make/vs/testapp/main.c Sun Jan 28 09:35:44 2024 +0100 @@ -248,8 +248,8 @@ ui_scrolledwindow0(obj) { ui_grid(obj, .margin = 10, .columnspacing = 5, .rowspacing = 20) { ui_button(obj, .label = "Button1", .onclick = action1, .onclickdata = "action1"); - ui_button(obj, .label = "Button2", .onclick = action1, .onclickdata = "action2"); - ui_button(obj, .label = "Button3", .onclick = action1, .onclickdata = "action3", .hexpand = true); + ui_button(obj, .label = "Button2", .icon = "Back", .onclick = action1, .onclickdata = "action2"); + ui_button(obj, .icon = "Forward", .onclick = action1, .onclickdata = "action3", .hexpand = true); ui_newline(obj); ui_button(obj, .label = "Button4", .onclick = action1, .onclickdata = "action4");
--- a/ui/ui/button.h Fri Jan 26 17:17:14 2024 +0100 +++ b/ui/ui/button.h Sun Jan 28 09:35:44 2024 +0100 @@ -44,6 +44,8 @@ const char* label; const char* stockid; + const char* icon; + UiLabelType labeltype; ui_callback onclick; void* onclickdata; } UiButtonArgs; @@ -57,6 +59,8 @@ const char* label; const char* stockid; + const char* icon; + UiLabelType labeltype; UiInteger* value; const char* varname; ui_callback onchange;
--- a/ui/ui/toolkit.h Fri Jan 26 17:17:14 2024 +0100 +++ b/ui/ui/toolkit.h Sun Jan 28 09:35:44 2024 +0100 @@ -168,9 +168,12 @@ typedef struct UiTabbedPane UiTabbedPane; typedef enum UiTri UiTri; +typedef enum UiLabelType UiLabelType; enum UiMouseEventType { UI_PRESS = 0, UI_PRESS2 }; +enum UiLabelType { UI_LABEL_DEFAULT, UI_LABEL_TEXT, UI_LABEL_ICON, UI_LABEL_TEXT_ICON }; + typedef void(*ui_callback)(UiEvent*, void*); /* event, user data */
--- a/ui/winui/button.cpp Fri Jan 26 17:17:14 2024 +0100 +++ b/ui/winui/button.cpp Sun Jan 28 09:35:44 2024 +0100 @@ -46,13 +46,39 @@ -static void set_button_label(ButtonBase button, const char* label, const char* stockid) { - if (label) { +static void set_button_label(ButtonBase button, const char* label, const char* stockid, const char *icon, UiLabelType type) { + // TODO: stockid + + if (type == UI_LABEL_ICON) { + label = NULL; + } + else if (type == UI_LABEL_TEXT) { + icon = NULL; + } + + if (label && icon) { + StackPanel panel = StackPanel(); + panel.Orientation(Orientation::Horizontal); + panel.Spacing(5); + + panel.Children().Append(ui_get_icon(icon)); + + wchar_t* wlabel = str2wstr(label, nullptr); + TextBlock label = TextBlock(); + label.Text(wlabel); + panel.Children().Append(label); + free(wlabel); + + button.Content(panel); + } + else if (label) { wchar_t* wlabel = str2wstr(label, nullptr); button.Content(box_value(wlabel)); free(wlabel); } - // TODO: stockid + else if (icon) { + button.Content(ui_get_icon(icon)); + } } @@ -61,7 +87,7 @@ // create button with label Button button = Button(); - set_button_label(button, args.label, args.stockid); + set_button_label(button, args.label, args.stockid, args.icon, args.labeltype); // create toolkit wrapper object and register destructor UIElement elm = button; @@ -149,7 +175,7 @@ UiObject* current = uic_current_obj(obj); // set label - set_button_label(button, args.label, args.stockid); + set_button_label(button, args.label, args.stockid, args.icon, args.labeltype); togglebutton_register_callback(button, obj, args); // create toolkit wrapper object and register destructor @@ -236,7 +262,7 @@ UiObject* current = uic_current_obj(obj); // set label - set_button_label(button, args.label, args.stockid); + set_button_label(button, args.label, args.stockid, args.icon, args.labeltype); togglebutton_register_callback(button, obj, args); // create toolkit wrapper object and register destructor