ui/winui/window.cpp

changeset 0
2483f517c562
child 13
5a8762fcfecc
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 "window.h"
32
33 #include "appmenu.h"
34 #include "commandbar.h"
35 #include "container.h"
36 #include "util.h"
37
38 #include "../common/context.h"
39
40 #include <stdlib.h>
41
42 #include <cx/mempool.h>
43
44 #include "MainWindow.xaml.h"
45
46
47 using namespace winrt;
48 using namespace Microsoft::UI::Xaml;
49 using namespace Microsoft::UI::Xaml::Controls;
50 using namespace Microsoft::UI::Xaml::Controls::Primitives;
51 using namespace Microsoft::UI::Xaml::XamlTypeInfo;
52 using namespace Microsoft::UI::Xaml::Markup;
53 using namespace Windows::UI::Xaml::Interop;
54 using namespace winrt::Windows::Foundation;
55
56 UiWindow::UiWindow(winrt::Microsoft::UI::Xaml::Window& win) : window(win) {}
57
58 UiObject* ui_window(const char* title, void* window_data) {
59 CxMempool* mp = cxBasicMempoolCreate(256);
60 UiObject* obj = (UiObject*)cxCalloc(mp->allocator, 1, sizeof(UiObject));
61
62 obj->ctx = uic_context(obj, mp);
63 obj->window = window_data;
64
65 Window window = Window();
66 //Window window = make<winui::implementation::MainWindow>();
67
68 winrt::Windows::Foundation::Uri resourceLocator{ L"ms-appx:///MainWindow.xaml" };
69 Application::LoadComponent(window, resourceLocator, ComponentResourceLocation::Nested);
70
71 window.ExtendsContentIntoTitleBar(true);
72
73 Grid grid = Grid();
74 window.Content(grid);
75
76 StackPanel titleBar = StackPanel();
77 Thickness titleBarPadding = { 10, 5, 5, 10 };
78 titleBar.Padding(titleBarPadding);
79 titleBar.Orientation(Orientation::Horizontal);
80 TextBlock titleLabel = TextBlock();
81 titleBar.Children().Append(titleLabel);
82
83 if (title) {
84 wchar_t* wtitle = str2wstr(title, nullptr);
85 window.Title(wtitle);
86 titleLabel.Text(hstring(wtitle));
87 free(wtitle);
88 }
89
90 window.SetTitleBar(titleBar);
91
92 obj->wobj = new UiWindow(window);
93 ui_context_add_window_destructor(obj->ctx, obj->wobj);
94
95 window.Closed([obj](IInspectable const& sender, WindowEventArgs) {
96 cxMempoolDestroy(obj->ctx->mp);
97 });
98
99 obj->container = new UiBoxContainer(grid, UI_BOX_CONTAINER_VBOX, 0, 0);
100
101 titleBar.VerticalAlignment(VerticalAlignment::Top);
102 obj->container->Add(titleBar, false);
103
104 if (uic_get_menu_list()) {
105 // create/add menubar
106 MenuBar mb = ui_create_menubar(obj);
107 mb.VerticalAlignment(VerticalAlignment::Top);
108 obj->container->Add(mb, false);
109 }
110
111 if (uic_toolbar_isenabled()) {
112 // create a grid for the toolbar: ColumnDefinitions="Auto, *, Auto"
113 Grid toolbar_grid = Grid();
114 GridLength gl;
115 gl.Value = 0;
116 gl.GridUnitType = GridUnitType::Auto;
117
118 ColumnDefinition coldef0 = ColumnDefinition();
119 coldef0.Width(gl);
120 toolbar_grid.ColumnDefinitions().Append(coldef0);
121
122 gl.Value = 1;
123 gl.GridUnitType = GridUnitType::Star;
124 ColumnDefinition coldef1 = ColumnDefinition();
125 coldef1.Width(gl);
126 toolbar_grid.ColumnDefinitions().Append(coldef1);
127
128 gl.Value = 0;
129 gl.GridUnitType = GridUnitType::Auto;
130 ColumnDefinition coldef2 = ColumnDefinition();
131 coldef2.Width(gl);
132 toolbar_grid.ColumnDefinitions().Append(coldef2);
133
134 // rowdef
135 gl.Value = 0;
136 gl.GridUnitType = GridUnitType::Auto;
137 RowDefinition rowdef = RowDefinition();
138 rowdef.Height(gl);
139 toolbar_grid.RowDefinitions().Append(rowdef);
140
141
142 // create commandbar
143 CxList* def_l = uic_get_toolbar_defaults(UI_TOOLBAR_LEFT);
144 CxList* def_c = uic_get_toolbar_defaults(UI_TOOLBAR_CENTER);
145 CxList* def_r = uic_get_toolbar_defaults(UI_TOOLBAR_RIGHT);
146
147 bool addappmenu = true;
148 if (def_r->size > 0) {
149 CommandBar toolbar_r = ui_create_toolbar(obj, def_r, addappmenu);
150 toolbar_grid.SetColumn(toolbar_r, 2);
151 toolbar_grid.SetRow(toolbar_r, 0);
152 toolbar_grid.Children().Append(toolbar_r);
153 addappmenu = false;
154 }
155 if (def_c->size > 0) {
156 CommandBar toolbar_c = ui_create_toolbar(obj, def_c, addappmenu);
157 toolbar_c.HorizontalAlignment(HorizontalAlignment::Center);
158 toolbar_grid.SetColumn(toolbar_c, 1);
159 toolbar_grid.SetRow(toolbar_c, 0);
160 toolbar_grid.Children().Append(toolbar_c);
161 addappmenu = false;
162 }
163 if (def_l->size > 0) {
164 CommandBar toolbar_l = ui_create_toolbar(obj, def_l, addappmenu);
165 toolbar_grid.SetColumn(toolbar_l, 0);
166 toolbar_grid.SetRow(toolbar_l, 0);
167 toolbar_grid.Children().Append(toolbar_l);
168 }
169
170 toolbar_grid.VerticalAlignment(VerticalAlignment::Top);
171 obj->container->Add(toolbar_grid, false);
172 }
173
174 obj->window = window_data;
175
176 return obj;
177 }

mercurial