UNIXworkcode

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 "commandbar.h" 32 33 #include "util.h" 34 #include "../common/object.h" 35 #include "../common/context.h" 36 37 #include "button.h" 38 #include "appmenu.h" 39 #include "icons.h" 40 41 using namespace winrt; 42 using namespace Microsoft::UI::Xaml; 43 using namespace Microsoft::UI::Xaml::Controls; 44 using namespace Microsoft::UI::Xaml::XamlTypeInfo; 45 using namespace Microsoft::UI::Xaml::Markup; 46 using namespace Windows::UI::Xaml::Interop; 47 48 static void create_item(UiObject* obj, Windows::Foundation::Collections::IObservableVector<ICommandBarElement> cb, UiToolbarItemI* i); 49 static void create_cmditem(UiObject* obj, Windows::Foundation::Collections::IObservableVector<ICommandBarElement> cb, UiToolbarItem* item); 50 static void create_toggleitem(UiObject* obj, Windows::Foundation::Collections::IObservableVector<ICommandBarElement> cb, UiToolbarToggleItem* item); 51 static void create_menuitem(UiObject* obj, Windows::Foundation::Collections::IObservableVector<ICommandBarElement> cb, UiToolbarMenuItem* item); 52 53 static void create_appmenu_items(UiObject* obj, Windows::Foundation::Collections::IObservableVector<ICommandBarElement> cb, UiToolbarMenuItem* i); 54 55 CommandBar ui_create_toolbar(UiObject *obj, CxList* defaults, bool addappmenu) { 56 57 CommandBar cb = CommandBar(); 58 cb.DefaultLabelPosition(CommandBarDefaultLabelPosition::Right); 59 60 // add pre-configured items 61 CxIterator i = cxListIterator(defaults); 62 cx_foreach(char*, def, i) { 63 UiToolbarItemI* item = uic_toolbar_get_item(def); 64 if (!item) { 65 exit(-1); // TODO: maybe an error dialog? 66 } 67 create_item(obj, cb.PrimaryCommands(), item); 68 } 69 70 // add appmenu 71 if (addappmenu) { 72 UiToolbarMenuItem* appmenu = uic_get_appmenu(); 73 if (appmenu) { 74 create_appmenu_items(obj, cb.SecondaryCommands(), appmenu); 75 } 76 } 77 78 return cb; 79 } 80 81 static void create_item(UiObject* obj, Windows::Foundation::Collections::IObservableVector<ICommandBarElement> cb, UiToolbarItemI* i) { 82 switch (i->type) { 83 case UI_TOOLBAR_ITEM: { 84 create_cmditem(obj, cb, (UiToolbarItem*)i); 85 break; 86 } 87 case UI_TOOLBAR_TOGGLEITEM: { 88 create_toggleitem(obj, cb, (UiToolbarToggleItem*)i); 89 break; 90 } 91 case UI_TOOLBAR_MENU: { 92 create_menuitem(obj, cb, (UiToolbarMenuItem*)i); 93 break; 94 } 95 } 96 } 97 98 static void create_appmenu_items(UiObject* obj, Windows::Foundation::Collections::IObservableVector<ICommandBarElement> cb, UiToolbarMenuItem* i) { 99 for (UiMenuItemI* mi = i->menu.items_begin; mi; mi = mi->next) { 100 // convert UiMenuItemI to UiToolbarItemI 101 switch (mi->type) { 102 case UI_MENU: { 103 UiMenu* mitem = (UiMenu*)mi; 104 UiToolbarMenuItem tbitem; 105 memset(&tbitem, 0, sizeof(UiToolbarMenuItem)); 106 tbitem.item.type = UI_TOOLBAR_MENU; 107 tbitem.args.label = mitem->label; 108 tbitem.menu.items_begin = mitem->items_begin; 109 tbitem.menu.items_end = mitem->items_end; 110 create_menuitem(obj, cb, &tbitem); 111 break; 112 } 113 case UI_MENU_ITEM: { 114 UiMenuItem* mitem = (UiMenuItem*)mi; 115 UiToolbarItem tbitem; 116 memset(&tbitem, 0, sizeof(UiToolbarItem)); 117 tbitem.item.type = UI_TOOLBAR_ITEM; 118 tbitem.args.label = mitem->label; 119 tbitem.args.onclick = mitem->callback; 120 tbitem.args.onclickdata = mitem->userdata; 121 create_cmditem(obj, cb, &tbitem); 122 break; 123 } 124 } 125 } 126 } 127 128 static void create_cmditem(UiObject* obj, Windows::Foundation::Collections::IObservableVector<ICommandBarElement> cb, UiToolbarItem* item) { 129 AppBarButton button = AppBarButton(); 130 if (item->args.label) { 131 wchar_t* wlabel = str2wstr(item->args.label, nullptr); 132 button.Label(wlabel); 133 free(wlabel); 134 } 135 if(item->args.icon) { 136 button.Icon(ui_get_icon(item->args.icon)); 137 } 138 139 UIElement elm = button; 140 UiWidget* widget = new UiWidget(elm); 141 ui_context_add_widget_destructor(obj->ctx, widget); 142 ui_set_widget_groups(obj->ctx, widget, item->args.groups); 143 144 // register callback 145 if (item->args.onclick) { 146 ui_callback cbfunc = item->args.onclick; 147 void* cbdata = item->args.onclickdata; 148 button.Click([cbfunc, cbdata, obj](Windows::Foundation::IInspectable const& sender, RoutedEventArgs) { 149 UiEvent evt; 150 evt.obj = obj; 151 evt.window = obj->window; 152 evt.document = obj->ctx->document; 153 evt.eventdata = nullptr; 154 evt.intval = 0; 155 cbfunc(&evt, cbdata); 156 }); 157 } 158 159 cb.Append(button); 160 } 161 162 static void create_toggleitem(UiObject *obj, Windows::Foundation::Collections::IObservableVector<ICommandBarElement> cb, UiToolbarToggleItem* item) { 163 AppBarToggleButton button = AppBarToggleButton(); 164 if (item->args.label) { 165 wchar_t* wlabel = str2wstr(item->args.label, nullptr); 166 button.Label(wlabel); 167 free(wlabel); 168 } 169 if (item->args.icon) { 170 button.Icon(ui_get_icon(item->args.icon)); 171 } 172 173 UiVar* var = uic_widget_var(obj->ctx, obj->ctx, nullptr, item->args.varname, UI_VAR_INTEGER); 174 if (var) { 175 UIElement elm = button; 176 UiWidget* widget = new UiWidget(elm); 177 ui_context_add_widget_destructor(obj->ctx, widget); 178 ui_set_widget_groups(obj->ctx, widget, item->args.groups); 179 180 UiInteger* value = (UiInteger*)var->value; 181 int64_t i = value->value; 182 value->get = ui_toggle_button_get; 183 value->set = ui_toggle_button_set; 184 value->obj = widget; 185 ui_toggle_button_set(value, i); // init togglebutton state 186 187 // listener for notifying observers 188 togglebutton_register_checked_observers(button, obj, var); 189 togglebutton_register_unchecked_observers(button, obj, var); 190 } 191 192 UiToggleArgs args = {}; 193 args.onchange = item->args.onchange; 194 args.onchangedata = item->args.onchangedata; 195 togglebutton_register_callback(button, obj, args); 196 197 198 cb.Append(button); 199 } 200 201 static void create_menuitem(UiObject* obj, Windows::Foundation::Collections::IObservableVector<ICommandBarElement> cb, UiToolbarMenuItem* item) { 202 AppBarButton button = AppBarButton(); 203 if (item->args.label) { 204 wchar_t* wlabel = str2wstr(item->args.label, nullptr); 205 button.Label(wlabel); 206 free(wlabel); 207 } 208 if (item->args.icon) { 209 button.Icon(ui_get_icon(item->args.icon)); 210 } 211 212 MenuFlyoutItem mi = MenuFlyoutItem(); 213 214 MenuFlyout flyout = ui_create_menu_flyout(obj, &item->menu); 215 button.Flyout(flyout); 216 217 cb.Append(button); 218 } 219