ui/winui/commandbar.cpp

branch
newapi
changeset 207
93b9f502cb88
parent 206
7ebc5a747c6f
child 208
f632bc0589ab
equal deleted inserted replaced
206:7ebc5a747c6f 207:93b9f502cb88
33 #include "util.h" 33 #include "util.h"
34 #include "../common/object.h" 34 #include "../common/object.h"
35 #include "../common/context.h" 35 #include "../common/context.h"
36 36
37 #include "button.h" 37 #include "button.h"
38 #include "appmenu.h"
38 39
39 using namespace winrt; 40 using namespace winrt;
40 using namespace Microsoft::UI::Xaml; 41 using namespace Microsoft::UI::Xaml;
41 using namespace Microsoft::UI::Xaml::Controls; 42 using namespace Microsoft::UI::Xaml::Controls;
42 using namespace Microsoft::UI::Xaml::XamlTypeInfo; 43 using namespace Microsoft::UI::Xaml::XamlTypeInfo;
43 using namespace Microsoft::UI::Xaml::Markup; 44 using namespace Microsoft::UI::Xaml::Markup;
44 using namespace Windows::UI::Xaml::Interop; 45 using namespace Windows::UI::Xaml::Interop;
45 46
46 static void create_item(UiObject* obj, CommandBar cb, UiToolbarItemI* i); 47 static void create_item(UiObject* obj, Windows::Foundation::Collections::IObservableVector<ICommandBarElement> cb, UiToolbarItemI* i);
47 static void create_cmditem(UiObject* obj, CommandBar cb, UiToolbarItem* item); 48 static void create_cmditem(UiObject* obj, Windows::Foundation::Collections::IObservableVector<ICommandBarElement> cb, UiToolbarItem* item);
48 static void create_toggleitem(UiObject* obj, CommandBar cb, UiToolbarToggleItem* item); 49 static void create_toggleitem(UiObject* obj, Windows::Foundation::Collections::IObservableVector<ICommandBarElement> cb, UiToolbarToggleItem* item);
50 static void create_menuitem(UiObject* obj, Windows::Foundation::Collections::IObservableVector<ICommandBarElement> cb, UiToolbarMenuItem* item);
51
52 static void create_appmenu_items(UiObject* obj, Windows::Foundation::Collections::IObservableVector<ICommandBarElement> cb, UiToolbarMenuItem* i);
49 53
50 CommandBar ui_create_toolbar(UiObject *obj) { 54 CommandBar ui_create_toolbar(UiObject *obj) {
51 55
52 CommandBar cb = CommandBar(); 56 CommandBar cb = CommandBar();
53 cb.DefaultLabelPosition(CommandBarDefaultLabelPosition::Right); 57 cb.DefaultLabelPosition(CommandBarDefaultLabelPosition::Right);
58 cx_foreach(char*, def, i) { 62 cx_foreach(char*, def, i) {
59 UiToolbarItemI* item = uic_toolbar_get_item(def); 63 UiToolbarItemI* item = uic_toolbar_get_item(def);
60 if (!item) { 64 if (!item) {
61 exit(-1); // TODO: maybe an error dialog? 65 exit(-1); // TODO: maybe an error dialog?
62 } 66 }
63 create_item(obj, cb, item); 67 create_item(obj, cb.PrimaryCommands(), item);
64 } 68 }
69
70 // add appmenu
71 UiToolbarMenuItem* appmenu = uic_get_appmenu();
72 if (appmenu) {
73 create_appmenu_items(obj, cb.SecondaryCommands(), appmenu);
74 }
75
76
65 return cb; 77 return cb;
66 } 78 }
67 79
68 static void create_item(UiObject* obj, CommandBar cb, UiToolbarItemI* i) { 80 static void create_item(UiObject* obj, Windows::Foundation::Collections::IObservableVector<ICommandBarElement> cb, UiToolbarItemI* i) {
69 switch (i->type) { 81 switch (i->type) {
70 case UI_TOOLBAR_ITEM: { 82 case UI_TOOLBAR_ITEM: {
71 create_cmditem(obj, cb, (UiToolbarItem*)i); 83 create_cmditem(obj, cb, (UiToolbarItem*)i);
72 break; 84 break;
73 } 85 }
74 case UI_TOOLBAR_TOGGLEITEM: { 86 case UI_TOOLBAR_TOGGLEITEM: {
75 create_toggleitem(obj, cb, (UiToolbarToggleItem*)i); 87 create_toggleitem(obj, cb, (UiToolbarToggleItem*)i);
76 break; 88 break;
77 } 89 }
78 } 90 case UI_TOOLBAR_MENU: {
79 } 91 create_menuitem(obj, cb, (UiToolbarMenuItem*)i);
80 92 break;
81 static void create_cmditem(UiObject* obj, CommandBar cb, UiToolbarItem* item) { 93 }
94 }
95 }
96
97 static void create_appmenu_items(UiObject* obj, Windows::Foundation::Collections::IObservableVector<ICommandBarElement> cb, UiToolbarMenuItem* i) {
98 for (UiMenuItemI* mi = i->menu.items_begin; mi; mi = mi->next) {
99 // convert UiMenuItemI to UiToolbarItemI
100 switch (mi->type) {
101 case UI_MENU_SUBMENU: {
102 UiMenu* mitem = (UiMenu*)mi;
103 UiToolbarMenuItem tbitem;
104 memset(&tbitem, 0, sizeof(UiToolbarMenuItem));
105 tbitem.item.type = UI_TOOLBAR_MENU;
106 tbitem.args.label = mitem->label;
107 tbitem.menu.items_begin = mitem->items_begin;
108 tbitem.menu.items_end = mitem->items_end;
109 create_menuitem(obj, cb, &tbitem);
110 break;
111 }
112 case UI_MENU_ITEM: {
113 UiMenuItem* mitem = (UiMenuItem*)mi;
114 UiToolbarItem tbitem;
115 memset(&tbitem, 0, sizeof(UiToolbarItem));
116 tbitem.item.type = UI_TOOLBAR_ITEM;
117 tbitem.args.label = mitem->label;
118 tbitem.args.onclick = mitem->callback;
119 tbitem.args.onclickdata = mitem->userdata;
120 create_cmditem(obj, cb, &tbitem);
121 break;
122 }
123 }
124 }
125 }
126
127 static void create_cmditem(UiObject* obj, Windows::Foundation::Collections::IObservableVector<ICommandBarElement> cb, UiToolbarItem* item) {
82 AppBarButton button = AppBarButton(); 128 AppBarButton button = AppBarButton();
83 if (item->args.label) { 129 if (item->args.label) {
84 wchar_t* wlabel = str2wstr(item->args.label, nullptr); 130 wchar_t* wlabel = str2wstr(item->args.label, nullptr);
85 button.Content(box_value(wlabel)); 131 button.Label(wlabel);
86 free(wlabel); 132 free(wlabel);
87 } 133 }
88 134
89 // register callback 135 // register callback
90 if (item->args.onclick) { 136 if (item->args.onclick) {
99 evt.intval = 0; 145 evt.intval = 0;
100 cbfunc(&evt, cbdata); 146 cbfunc(&evt, cbdata);
101 }); 147 });
102 } 148 }
103 149
104 cb.PrimaryCommands().Append(button); 150 cb.Append(button);
105 } 151 }
106 152
107 153 static void create_toggleitem(UiObject *obj, Windows::Foundation::Collections::IObservableVector<ICommandBarElement> cb, UiToolbarToggleItem* item) {
108
109 static void create_toggleitem(UiObject *obj, CommandBar cb, UiToolbarToggleItem* item) {
110 AppBarToggleButton button = AppBarToggleButton(); 154 AppBarToggleButton button = AppBarToggleButton();
111 if (item->args.label) { 155 if (item->args.label) {
112 wchar_t* wlabel = str2wstr(item->args.label, nullptr); 156 wchar_t* wlabel = str2wstr(item->args.label, nullptr);
113 button.Content(box_value(wlabel)); 157 button.Label(wlabel);
114 free(wlabel); 158 free(wlabel);
115 } 159 }
116 160
117 UiVar* var = uic_widget_var(obj->ctx, obj->ctx, nullptr, item->args.varname, UI_VAR_INTEGER); 161 UiVar* var = uic_widget_var(obj->ctx, obj->ctx, nullptr, item->args.varname, UI_VAR_INTEGER);
118 if (var) { 162 if (var) {
136 args.onchange = item->args.onchange; 180 args.onchange = item->args.onchange;
137 args.onchangedata = item->args.onchangedata; 181 args.onchangedata = item->args.onchangedata;
138 togglebutton_register_callback(button, obj, args); 182 togglebutton_register_callback(button, obj, args);
139 183
140 184
141 cb.PrimaryCommands().Append(button); 185 cb.Append(button);
142 } 186 }
143 187
144 188 static void create_menuitem(UiObject* obj, Windows::Foundation::Collections::IObservableVector<ICommandBarElement> cb, UiToolbarMenuItem* item) {
189 AppBarButton button = AppBarButton();
190 if (item->args.label) {
191 wchar_t* wlabel = str2wstr(item->args.label, nullptr);
192 button.Label(wlabel);
193 free(wlabel);
194 }
195
196 MenuFlyoutItem mi = MenuFlyoutItem();
197
198 MenuFlyout flyout = ui_create_menu_flyout(obj, &item->menu);
199 button.Flyout(flyout);
200
201 cb.Append(button);
202 }

mercurial