Tue, 12 Nov 2024 15:05:57 +0100
update toolkit (winui dialog window)
ui/common/properties.c | file | annotate | diff | comparison | revisions | |
ui/ui/properties.h | file | annotate | diff | comparison | revisions | |
ui/ui/toolkit.h | file | annotate | diff | comparison | revisions | |
ui/winui/button.cpp | file | annotate | diff | comparison | revisions | |
ui/winui/button.h | file | annotate | diff | comparison | revisions | |
ui/winui/toolkit.cpp | file | annotate | diff | comparison | revisions | |
ui/winui/window.cpp | file | annotate | diff | comparison | revisions |
--- a/ui/common/properties.c Mon Nov 11 22:42:09 2024 +0100 +++ b/ui/common/properties.c Tue Nov 12 15:05:57 2024 +0100 @@ -39,6 +39,7 @@ #include <cx/string.h> #include <cx/buffer.h> +#include <cx/hash_map.h> #include "ucx_properties.h" static CxMap *application_properties;
--- a/ui/ui/properties.h Mon Nov 11 22:42:09 2024 +0100 +++ b/ui/ui/properties.h Tue Nov 12 15:05:57 2024 +0100 @@ -30,14 +30,11 @@ #define UI_PROPERTIES_H #include "toolkit.h" -#include <cx/map.h> #ifdef __cplusplus extern "C" { #endif -typedef CxMap UiProperties; - char* ui_get_property(char *name); void ui_set_property(char *name, char *value);
--- a/ui/ui/toolkit.h Mon Nov 11 22:42:09 2024 +0100 +++ b/ui/ui/toolkit.h Tue Nov 12 15:05:57 2024 +0100 @@ -84,6 +84,7 @@ #ifdef __cplusplus +#include <functional> #ifndef UI_WINUI_PCH #include <Windows.h> #undef GetCurrentTime @@ -105,6 +106,7 @@ void* data1 = nullptr; void* data2 = nullptr; void* data3 = nullptr; + std::function<void()> Show; UiWidget(winrt::Microsoft::UI::Xaml::UIElement& elm); };
--- a/ui/winui/button.cpp Mon Nov 11 22:42:09 2024 +0100 +++ b/ui/winui/button.cpp Tue Nov 12 15:05:57 2024 +0100 @@ -46,7 +46,7 @@ -static void set_button_label(ButtonBase button, const char* label, const char* stockid, const char *icon, UiLabelType type) { +void ui_set_button_label(ButtonBase button, const char* label, const char* stockid, const char *icon, UiLabelType type) { // TODO: stockid if (type == UI_LABEL_ICON) { @@ -92,7 +92,7 @@ // create button with label Button button = Button(); - set_button_label(button, args.label, args.stockid, args.icon, args.labeltype); + ui_set_button_label(button, args.label, args.stockid, args.icon, args.labeltype); // create toolkit wrapper object and register destructor UIElement elm = button; @@ -180,7 +180,7 @@ UiObject* current = uic_current_obj(obj); // set label - set_button_label(button, args.label, args.stockid, args.icon, args.labeltype); + ui_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 @@ -267,7 +267,7 @@ UiObject* current = uic_current_obj(obj); // set label - set_button_label(button, args.label, args.stockid, args.icon, args.labeltype); + ui_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
--- a/ui/winui/button.h Mon Nov 11 22:42:09 2024 +0100 +++ b/ui/winui/button.h Tue Nov 12 15:05:57 2024 +0100 @@ -35,6 +35,7 @@ #include "../common/context.h" +void ui_set_button_label(winrt::Microsoft::UI::Xaml::Controls::Primitives::ButtonBase button, const char* label, const char* stockid, const char *icon, UiLabelType type); void togglebutton_register_checked_observers(winrt::Microsoft::UI::Xaml::Controls::Primitives::ToggleButton button, UiObject* obj, UiVar* var); void togglebutton_register_unchecked_observers(winrt::Microsoft::UI::Xaml::Controls::Primitives::ToggleButton button, UiObject* obj, UiVar* var);
--- a/ui/winui/toolkit.cpp Mon Nov 11 22:42:09 2024 +0100 +++ b/ui/winui/toolkit.cpp Tue Nov 12 15:05:57 2024 +0100 @@ -232,8 +232,8 @@ void ui_show(UiObject* obj) { if (obj->wobj) { obj->wobj->window.Activate(); - } else { - // TODO + } else if(obj->widget && obj->widget->Show) { + obj->widget->Show(); } }
--- a/ui/winui/window.cpp Mon Nov 11 22:42:09 2024 +0100 +++ b/ui/winui/window.cpp Tue Nov 12 15:05:57 2024 +0100 @@ -35,6 +35,7 @@ #include "commandbar.h" #include "container.h" #include "util.h" +#include "button.h" #include "../common/context.h" #include "../common/object.h" @@ -201,8 +202,168 @@ return obj; } +static void dialog_button_add_callback(ContentDialog dialog, Button button, int num, UiObject *obj, ui_callback onclick, void *onclickdata) { + button.Click([dialog, num, obj, onclick, onclickdata](IInspectable const& sender, RoutedEventArgs) { + if (onclick) { + UiEvent evt; + evt.obj = obj; + evt.window = obj->window; + evt.document = obj->ctx->document; + evt.eventdata = nullptr; + evt.intval = num; + onclick(&evt, onclickdata); + } + dialog.Hide(); + }); +} + UIEXPORT UiObject* ui_dialog_window_create(UiObject *parent, UiDialogWindowArgs args) { - return NULL; + UiWindow *window = parent->wobj; + if (!window) { + return NULL; + } + + CxMempool* mp = cxBasicMempoolCreate(256); + UiObject* obj = (UiObject*)cxCalloc(mp->allocator, 1, sizeof(UiObject)); + + obj->ctx = uic_context(obj, mp); + + ContentDialog dialog = ContentDialog(); + UIElement elm = dialog; + UiWidget* widget = new UiWidget(elm); + ui_context_add_widget_destructor(obj->ctx, widget); + obj->widget = widget; + + if (args.title) { + wchar_t* wtitle = str2wstr(args.title, nullptr); + dialog.Title(box_value(wtitle)); + free(wtitle); + } + + + Grid dialogContent = Grid(); + GridLength gl; + + // content row + gl.Value = 1; + gl.GridUnitType = GridUnitType::Star; + RowDefinition rowdef0 = RowDefinition(); + rowdef0.Height(gl); + dialogContent.RowDefinitions().Append(rowdef0); + + // button row + gl.Value = 0; + gl.GridUnitType = GridUnitType::Auto; + RowDefinition rowdef1 = RowDefinition(); + rowdef1.Height(gl); + dialogContent.RowDefinitions().Append(rowdef1); + + // coldef + gl.Value = 1; + gl.GridUnitType = GridUnitType::Star; + ColumnDefinition coldef = ColumnDefinition(); + coldef.Width(gl); + dialogContent.ColumnDefinitions().Append(coldef); + + // content + Grid grid = Grid(); + grid.SetRow(grid, 0); + grid.SetColumn(grid, 0); + dialogContent.Children().Append(grid); + obj->container = new UiBoxContainer(grid, UI_BOX_CONTAINER_VBOX, 0, 0); + + // buttons + Grid buttons = Grid(); + Thickness btnsMargin = { (double)0, (double)10, (double)0, (double)0 }; + buttons.Margin(btnsMargin); + + RowDefinition btnrowdef = RowDefinition(); + gl.Value = 0; + gl.GridUnitType = GridUnitType::Auto; + btnrowdef.Height(gl); + buttons.RowDefinitions().Append(btnrowdef); + + gl.Value = 1; + gl.GridUnitType = GridUnitType::Auto; + int c = 0; + if (args.lbutton1) { + ColumnDefinition bcoldef = ColumnDefinition(); + bcoldef.Width(gl); + buttons.ColumnDefinitions().Append(bcoldef); + + Button btn = Button(); + ui_set_button_label(btn, args.lbutton1, NULL, NULL, UI_LABEL_TEXT); + Thickness margin = { (double)5, (double)5, (double)5, (double)5 }; + btn.Margin(margin); + btn.HorizontalAlignment(HorizontalAlignment::Stretch); + dialog_button_add_callback(dialog, btn, 1, obj, args.onclick, args.onclickdata); + + buttons.SetRow(btn, 0); + buttons.SetColumn(btn, c++); + buttons.Children().Append(btn); + } + if (args.lbutton2) { + ColumnDefinition bcoldef = ColumnDefinition(); + bcoldef.Width(gl); + buttons.ColumnDefinitions().Append(bcoldef); + + Button btn = Button(); + ui_set_button_label(btn, args.lbutton2, NULL, NULL, UI_LABEL_TEXT); + Thickness margin = { (double)5, (double)5, (double)5, (double)5 }; + btn.Margin(margin); + btn.HorizontalAlignment(HorizontalAlignment::Stretch); + dialog_button_add_callback(dialog, btn, 2, obj, args.onclick, args.onclickdata); + + buttons.SetRow(btn, 0); + buttons.SetColumn(btn, c++); + buttons.Children().Append(btn); + } + if (args.rbutton3) { + ColumnDefinition bcoldef = ColumnDefinition(); + bcoldef.Width(gl); + buttons.ColumnDefinitions().Append(bcoldef); + + Button btn = Button(); + ui_set_button_label(btn, args.rbutton3, NULL, NULL, UI_LABEL_TEXT); + Thickness margin = { (double)5, (double)5, (double)5, (double)5 }; + btn.Margin(margin); + btn.HorizontalAlignment(HorizontalAlignment::Stretch); + dialog_button_add_callback(dialog, btn, 3, obj, args.onclick, args.onclickdata); + + buttons.SetRow(btn, 0); + buttons.SetColumn(btn, c++); + buttons.Children().Append(btn); + } + if (args.rbutton4) { + ColumnDefinition bcoldef = ColumnDefinition(); + bcoldef.Width(gl); + buttons.ColumnDefinitions().Append(bcoldef); + + Button btn = Button(); + ui_set_button_label(btn, args.rbutton4, NULL, NULL, UI_LABEL_TEXT); + Thickness margin = { (double)5, (double)5, (double)5, (double)5 }; + btn.Margin(margin); + btn.HorizontalAlignment(HorizontalAlignment::Stretch); + dialog_button_add_callback(dialog, btn, 4, obj, args.onclick, args.onclickdata); + + buttons.SetRow(btn, 0); + buttons.SetColumn(btn, c++); + buttons.Children().Append(btn); + } + + dialogContent.SetRow(buttons, 1); + dialogContent.SetColumn(buttons, 0); + dialogContent.Children().Append(buttons); + + + dialog.Content(dialogContent); + dialog.XamlRoot(window->window.Content().XamlRoot()); + + obj->widget->Show = [dialog]() { + dialog.ShowAsync(); + }; + + return obj; } void ui_window_size(UiObject *obj, int width, int height) {