ui/winui/commandbar.cpp

changeset 0
2483f517c562
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 "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 // register callback
140 if (item->args.onclick) {
141 ui_callback cbfunc = item->args.onclick;
142 void* cbdata = item->args.onclickdata;
143 button.Click([cbfunc, cbdata, obj](Windows::Foundation::IInspectable const& sender, RoutedEventArgs) {
144 UiEvent evt;
145 evt.obj = obj;
146 evt.window = obj->window;
147 evt.document = obj->ctx->document;
148 evt.eventdata = nullptr;
149 evt.intval = 0;
150 cbfunc(&evt, cbdata);
151 });
152 }
153
154 cb.Append(button);
155 }
156
157 static void create_toggleitem(UiObject *obj, Windows::Foundation::Collections::IObservableVector<ICommandBarElement> cb, UiToolbarToggleItem* item) {
158 AppBarToggleButton button = AppBarToggleButton();
159 if (item->args.label) {
160 wchar_t* wlabel = str2wstr(item->args.label, nullptr);
161 button.Label(wlabel);
162 free(wlabel);
163 }
164 if (item->args.icon) {
165 button.Icon(ui_get_icon(item->args.icon));
166 }
167
168 UiVar* var = uic_widget_var(obj->ctx, obj->ctx, nullptr, item->args.varname, UI_VAR_INTEGER);
169 if (var) {
170 UIElement elm = button;
171 UiWidget* widget = new UiWidget(elm);
172 ui_context_add_widget_destructor(obj->ctx, widget);
173
174 UiInteger* value = (UiInteger*)var->value;
175 int64_t i = value->value;
176 value->get = ui_toggle_button_get;
177 value->set = ui_toggle_button_set;
178 value->obj = widget;
179 ui_toggle_button_set(value, i); // init togglebutton state
180
181 // listener for notifying observers
182 togglebutton_register_checked_observers(button, obj, var);
183 togglebutton_register_unchecked_observers(button, obj, var);
184 }
185
186 UiToggleArgs args = {};
187 args.onchange = item->args.onchange;
188 args.onchangedata = item->args.onchangedata;
189 togglebutton_register_callback(button, obj, args);
190
191
192 cb.Append(button);
193 }
194
195 static void create_menuitem(UiObject* obj, Windows::Foundation::Collections::IObservableVector<ICommandBarElement> cb, UiToolbarMenuItem* item) {
196 AppBarButton button = AppBarButton();
197 if (item->args.label) {
198 wchar_t* wlabel = str2wstr(item->args.label, nullptr);
199 button.Label(wlabel);
200 free(wlabel);
201 }
202 if (item->args.icon) {
203 button.Icon(ui_get_icon(item->args.icon));
204 }
205
206 MenuFlyoutItem mi = MenuFlyoutItem();
207
208 MenuFlyout flyout = ui_create_menu_flyout(obj, &item->menu);
209 button.Flyout(flyout);
210
211 cb.Append(button);
212 }

mercurial