ui/winui/toolkit.cpp

Tue, 12 Nov 2024 14:58:03 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Tue, 12 Nov 2024 14:58:03 +0100
branch
newapi
changeset 379
958bae372271
parent 373
2eede3d98aba
permissions
-rw-r--r--

implement ui_dialog_window (WINUI)

177
e79a60b3a7cb minimal working winui app
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 173
diff changeset
1 /*
e79a60b3a7cb minimal working winui app
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 173
diff changeset
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
e79a60b3a7cb minimal working winui app
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 173
diff changeset
3 *
e79a60b3a7cb minimal working winui app
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 173
diff changeset
4 * Copyright 2023 Olaf Wintermann. All rights reserved.
e79a60b3a7cb minimal working winui app
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 173
diff changeset
5 *
e79a60b3a7cb minimal working winui app
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 173
diff changeset
6 * Redistribution and use in source and binary forms, with or without
e79a60b3a7cb minimal working winui app
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 173
diff changeset
7 * modification, are permitted provided that the following conditions are met:
e79a60b3a7cb minimal working winui app
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 173
diff changeset
8 *
e79a60b3a7cb minimal working winui app
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 173
diff changeset
9 * 1. Redistributions of source code must retain the above copyright
e79a60b3a7cb minimal working winui app
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 173
diff changeset
10 * notice, this list of conditions and the following disclaimer.
e79a60b3a7cb minimal working winui app
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 173
diff changeset
11 *
e79a60b3a7cb minimal working winui app
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 173
diff changeset
12 * 2. Redistributions in binary form must reproduce the above copyright
e79a60b3a7cb minimal working winui app
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 173
diff changeset
13 * notice, this list of conditions and the following disclaimer in the
e79a60b3a7cb minimal working winui app
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 173
diff changeset
14 * documentation and/or other materials provided with the distribution.
e79a60b3a7cb minimal working winui app
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 173
diff changeset
15 *
e79a60b3a7cb minimal working winui app
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 173
diff changeset
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
e79a60b3a7cb minimal working winui app
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 173
diff changeset
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
e79a60b3a7cb minimal working winui app
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 173
diff changeset
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
e79a60b3a7cb minimal working winui app
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 173
diff changeset
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
e79a60b3a7cb minimal working winui app
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 173
diff changeset
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
e79a60b3a7cb minimal working winui app
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 173
diff changeset
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
e79a60b3a7cb minimal working winui app
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 173
diff changeset
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
e79a60b3a7cb minimal working winui app
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 173
diff changeset
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
e79a60b3a7cb minimal working winui app
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 173
diff changeset
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
e79a60b3a7cb minimal working winui app
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 173
diff changeset
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
e79a60b3a7cb minimal working winui app
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 173
diff changeset
26 * POSSIBILITY OF SUCH DAMAGE.
e79a60b3a7cb minimal working winui app
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 173
diff changeset
27 */
e79a60b3a7cb minimal working winui app
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 173
diff changeset
28
205
b1ac0dd1d38b new winui vs project, add toolbar
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 189
diff changeset
29 #include "pch.h"
177
e79a60b3a7cb minimal working winui app
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 173
diff changeset
30
205
b1ac0dd1d38b new winui vs project, add toolbar
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 189
diff changeset
31 #include "toolkit.h"
177
e79a60b3a7cb minimal working winui app
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 173
diff changeset
32
179
34f4d78647be add ui common to vs build system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 177
diff changeset
33 #include <cx/allocator.h>
184
8c9b4b28aaa9 implement ui_button (WinUI)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 183
diff changeset
34 #include <cx/mempool.h>
179
34f4d78647be add ui common to vs build system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 177
diff changeset
35
189
4daddc326877 add onchange event for toggle buttons (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 184
diff changeset
36 #include "../common/context.h"
232
e2b33055113f fix list binding not copied correctly when attaching a document
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 229
diff changeset
37 #include "../common/document.h"
205
b1ac0dd1d38b new winui vs project, add toolbar
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 189
diff changeset
38 #include "../common/toolbar.h"
379
958bae372271 implement ui_dialog_window (WINUI)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 373
diff changeset
39 #include "../common/properties.h"
189
4daddc326877 add onchange event for toggle buttons (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 184
diff changeset
40
213
7e39db525fd9 add icons (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 212
diff changeset
41 #include "icons.h"
7e39db525fd9 add icons (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 212
diff changeset
42
205
b1ac0dd1d38b new winui vs project, add toolbar
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 189
diff changeset
43 #include "MainWindow.xaml.h"
b1ac0dd1d38b new winui vs project, add toolbar
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 189
diff changeset
44
b1ac0dd1d38b new winui vs project, add toolbar
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 189
diff changeset
45 #include "App.xaml.h"
177
e79a60b3a7cb minimal working winui app
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 173
diff changeset
46
e79a60b3a7cb minimal working winui app
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 173
diff changeset
47 using namespace winrt;
e79a60b3a7cb minimal working winui app
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 173
diff changeset
48 using namespace Microsoft::UI::Xaml;
e79a60b3a7cb minimal working winui app
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 173
diff changeset
49 using namespace Microsoft::UI::Xaml::Controls;
e79a60b3a7cb minimal working winui app
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 173
diff changeset
50 using namespace Microsoft::UI::Xaml::XamlTypeInfo;
e79a60b3a7cb minimal working winui app
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 173
diff changeset
51 using namespace Microsoft::UI::Xaml::Markup;
e79a60b3a7cb minimal working winui app
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 173
diff changeset
52 using namespace Windows::UI::Xaml::Interop;
184
8c9b4b28aaa9 implement ui_button (WinUI)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 183
diff changeset
53 using namespace winrt::Windows::Foundation;
234
9036b346cd66 implement ui_job() and add ui_call_mainthread (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 232
diff changeset
54 using namespace Windows::UI::Core;
177
e79a60b3a7cb minimal working winui app
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 173
diff changeset
55
e79a60b3a7cb minimal working winui app
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 173
diff changeset
56 static const char* application_name;
e79a60b3a7cb minimal working winui app
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 173
diff changeset
57
e79a60b3a7cb minimal working winui app
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 173
diff changeset
58 static ui_callback startup_func;
e79a60b3a7cb minimal working winui app
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 173
diff changeset
59 static void* startup_data;
e79a60b3a7cb minimal working winui app
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 173
diff changeset
60
e79a60b3a7cb minimal working winui app
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 173
diff changeset
61 static ui_callback open_func;
e79a60b3a7cb minimal working winui app
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 173
diff changeset
62 void* open_data;
e79a60b3a7cb minimal working winui app
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 173
diff changeset
63
e79a60b3a7cb minimal working winui app
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 173
diff changeset
64 static ui_callback exit_func;
e79a60b3a7cb minimal working winui app
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 173
diff changeset
65 void* exit_data;
e79a60b3a7cb minimal working winui app
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 173
diff changeset
66
e79a60b3a7cb minimal working winui app
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 173
diff changeset
67 static ui_callback appclose_fnc;
e79a60b3a7cb minimal working winui app
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 173
diff changeset
68
e79a60b3a7cb minimal working winui app
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 173
diff changeset
69 static void* appclose_udata;
e79a60b3a7cb minimal working winui app
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 173
diff changeset
70
e79a60b3a7cb minimal working winui app
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 173
diff changeset
71
e79a60b3a7cb minimal working winui app
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 173
diff changeset
72 static UiObject* active_window;
e79a60b3a7cb minimal working winui app
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 173
diff changeset
73
234
9036b346cd66 implement ui_job() and add ui_call_mainthread (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 232
diff changeset
74 static winrt::Microsoft::UI::Dispatching::DispatcherQueue uiDispatcherQueue = { nullptr };
9036b346cd66 implement ui_job() and add ui_call_mainthread (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 232
diff changeset
75
209
770b3548906c use generated App class (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 208
diff changeset
76 void ui_app_run_startup() {
234
9036b346cd66 implement ui_job() and add ui_call_mainthread (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 232
diff changeset
77 uiDispatcherQueue = winrt::Microsoft::UI::Dispatching::DispatcherQueue::GetForCurrentThread();
9036b346cd66 implement ui_job() and add ui_call_mainthread (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 232
diff changeset
78
209
770b3548906c use generated App class (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 208
diff changeset
79 if (startup_func) {
770b3548906c use generated App class (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 208
diff changeset
80 startup_func(NULL, startup_data);
770b3548906c use generated App class (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 208
diff changeset
81 }
770b3548906c use generated App class (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 208
diff changeset
82 }
173
809581724cc7 add VS project
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
83
177
e79a60b3a7cb minimal working winui app
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 173
diff changeset
84 class App : public ApplicationT<App, IXamlMetadataProvider> {
e79a60b3a7cb minimal working winui app
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 173
diff changeset
85 public:
e79a60b3a7cb minimal working winui app
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 173
diff changeset
86 void OnLaunched(LaunchActivatedEventArgs const&) {
e79a60b3a7cb minimal working winui app
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 173
diff changeset
87 Resources().MergedDictionaries().Append(XamlControlsResources());
e79a60b3a7cb minimal working winui app
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 173
diff changeset
88 if (startup_func) {
e79a60b3a7cb minimal working winui app
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 173
diff changeset
89 startup_func(NULL, startup_data);
e79a60b3a7cb minimal working winui app
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 173
diff changeset
90 }
205
b1ac0dd1d38b new winui vs project, add toolbar
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 189
diff changeset
91
b1ac0dd1d38b new winui vs project, add toolbar
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 189
diff changeset
92 //auto window = make<winui::implementation::MainWindow>();
b1ac0dd1d38b new winui vs project, add toolbar
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 189
diff changeset
93 //window.Activate();
177
e79a60b3a7cb minimal working winui app
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 173
diff changeset
94 }
e79a60b3a7cb minimal working winui app
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 173
diff changeset
95 IXamlType GetXamlType(TypeName const& type) {
e79a60b3a7cb minimal working winui app
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 173
diff changeset
96 return provider.GetXamlType(type);
e79a60b3a7cb minimal working winui app
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 173
diff changeset
97 }
e79a60b3a7cb minimal working winui app
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 173
diff changeset
98 IXamlType GetXamlType(hstring const& fullname) {
e79a60b3a7cb minimal working winui app
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 173
diff changeset
99 return provider.GetXamlType(fullname);
e79a60b3a7cb minimal working winui app
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 173
diff changeset
100 }
e79a60b3a7cb minimal working winui app
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 173
diff changeset
101 com_array<XmlnsDefinition> GetXmlnsDefinitions() {
e79a60b3a7cb minimal working winui app
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 173
diff changeset
102 return provider.GetXmlnsDefinitions();
e79a60b3a7cb minimal working winui app
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 173
diff changeset
103 }
e79a60b3a7cb minimal working winui app
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 173
diff changeset
104 private:
e79a60b3a7cb minimal working winui app
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 173
diff changeset
105 XamlControlsXamlMetaDataProvider provider;
e79a60b3a7cb minimal working winui app
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 173
diff changeset
106 };
e79a60b3a7cb minimal working winui app
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 173
diff changeset
107
183
3ce2eb11913b create WinUI wrapper classes for usage in UiObject and widget creation functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 181
diff changeset
108 UiWidget::UiWidget(winrt::Microsoft::UI::Xaml::UIElement& elm) : uielement(elm) {}
3ce2eb11913b create WinUI wrapper classes for usage in UiObject and widget creation functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 181
diff changeset
109
184
8c9b4b28aaa9 implement ui_button (WinUI)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 183
diff changeset
110 extern "C" void destroy_ui_window_wrapper(void* ptr) {
8c9b4b28aaa9 implement ui_button (WinUI)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 183
diff changeset
111 UiWindow* win = (UiWindow*)ptr;
8c9b4b28aaa9 implement ui_button (WinUI)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 183
diff changeset
112 delete win;
8c9b4b28aaa9 implement ui_button (WinUI)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 183
diff changeset
113 }
8c9b4b28aaa9 implement ui_button (WinUI)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 183
diff changeset
114
8c9b4b28aaa9 implement ui_button (WinUI)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 183
diff changeset
115 extern "C" void destroy_ui_widget_wrapper(void* ptr) {
8c9b4b28aaa9 implement ui_button (WinUI)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 183
diff changeset
116 UiWidget* widget = (UiWidget*)ptr;
8c9b4b28aaa9 implement ui_button (WinUI)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 183
diff changeset
117 delete widget;
8c9b4b28aaa9 implement ui_button (WinUI)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 183
diff changeset
118 }
8c9b4b28aaa9 implement ui_button (WinUI)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 183
diff changeset
119
212
ad5c74af14c4 add cleanup code for wrapper objects (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 209
diff changeset
120 extern "C" void destroy_ui_container_wrapper(void* ptr) {
ad5c74af14c4 add cleanup code for wrapper objects (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 209
diff changeset
121 UiContainer* ctn = (UiContainer*)ptr;
ad5c74af14c4 add cleanup code for wrapper objects (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 209
diff changeset
122 delete ctn;
ad5c74af14c4 add cleanup code for wrapper objects (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 209
diff changeset
123 }
ad5c74af14c4 add cleanup code for wrapper objects (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 209
diff changeset
124
184
8c9b4b28aaa9 implement ui_button (WinUI)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 183
diff changeset
125 void ui_context_add_window_destructor(UiContext* ctx, UiWindow* win) {
212
ad5c74af14c4 add cleanup code for wrapper objects (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 209
diff changeset
126 cxMempoolRegister(ctx->mp, win, destroy_ui_window_wrapper);
184
8c9b4b28aaa9 implement ui_button (WinUI)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 183
diff changeset
127 }
8c9b4b28aaa9 implement ui_button (WinUI)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 183
diff changeset
128
8c9b4b28aaa9 implement ui_button (WinUI)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 183
diff changeset
129 void ui_context_add_widget_destructor(UiContext* ctx, UiWidget* widget) {
212
ad5c74af14c4 add cleanup code for wrapper objects (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 209
diff changeset
130 cxMempoolRegister(ctx->mp, widget, destroy_ui_widget_wrapper);
ad5c74af14c4 add cleanup code for wrapper objects (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 209
diff changeset
131 }
ad5c74af14c4 add cleanup code for wrapper objects (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 209
diff changeset
132
ad5c74af14c4 add cleanup code for wrapper objects (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 209
diff changeset
133 void ui_context_add_container_destructor(UiContext* ctx, UiContainer *container) {
ad5c74af14c4 add cleanup code for wrapper objects (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 209
diff changeset
134 cxMempoolRegister(ctx->mp, container, destroy_ui_container_wrapper);
184
8c9b4b28aaa9 implement ui_button (WinUI)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 183
diff changeset
135 }
183
3ce2eb11913b create WinUI wrapper classes for usage in UiObject and widget creation functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 181
diff changeset
136
177
e79a60b3a7cb minimal working winui app
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 173
diff changeset
137
189
4daddc326877 add onchange event for toggle buttons (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 184
diff changeset
138 UiEvent ui_create_int_event(UiObject* obj, int64_t i) {
4daddc326877 add onchange event for toggle buttons (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 184
diff changeset
139 UiEvent evt;
4daddc326877 add onchange event for toggle buttons (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 184
diff changeset
140 evt.obj = obj;
4daddc326877 add onchange event for toggle buttons (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 184
diff changeset
141 evt.window = obj->window;
4daddc326877 add onchange event for toggle buttons (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 184
diff changeset
142 evt.document = obj->ctx->document;
4daddc326877 add onchange event for toggle buttons (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 184
diff changeset
143 evt.eventdata = nullptr;
4daddc326877 add onchange event for toggle buttons (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 184
diff changeset
144 evt.intval = i;
4daddc326877 add onchange event for toggle buttons (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 184
diff changeset
145 return evt;
4daddc326877 add onchange event for toggle buttons (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 184
diff changeset
146 }
4daddc326877 add onchange event for toggle buttons (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 184
diff changeset
147
4daddc326877 add onchange event for toggle buttons (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 184
diff changeset
148
207
93b9f502cb88 add toolbar appmenu and menu buttons
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 205
diff changeset
149 #include <MddBootstrap.h>
177
e79a60b3a7cb minimal working winui app
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 173
diff changeset
150
208
f632bc0589ab new menu api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 207
diff changeset
151 void ui_appsdk_bootstrap(void) {
f632bc0589ab new menu api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 207
diff changeset
152 const UINT32 majorMinorVersion{ 0x00010002 };
f632bc0589ab new menu api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 207
diff changeset
153 PCWSTR versionTag{ L"" };
f632bc0589ab new menu api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 207
diff changeset
154 const PACKAGE_VERSION minVersion{};
f632bc0589ab new menu api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 207
diff changeset
155
f632bc0589ab new menu api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 207
diff changeset
156 const HRESULT hr = MddBootstrapInitialize(majorMinorVersion, versionTag, minVersion);
f632bc0589ab new menu api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 207
diff changeset
157 if (FAILED(hr)) {
f632bc0589ab new menu api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 207
diff changeset
158 exit(102);
f632bc0589ab new menu api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 207
diff changeset
159 }
f632bc0589ab new menu api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 207
diff changeset
160 }
f632bc0589ab new menu api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 207
diff changeset
161
177
e79a60b3a7cb minimal working winui app
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 173
diff changeset
162 void ui_init(const char* appname, int argc, char** argv) {
e79a60b3a7cb minimal working winui app
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 173
diff changeset
163 application_name = appname;
205
b1ac0dd1d38b new winui vs project, add toolbar
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 189
diff changeset
164
208
f632bc0589ab new menu api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 207
diff changeset
165 //ui_appsdk_bootstrap();
f632bc0589ab new menu api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 207
diff changeset
166
229
a952760955b4 add menu item list (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 221
diff changeset
167 uic_init_global_context();
232
e2b33055113f fix list binding not copied correctly when attaching a document
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 229
diff changeset
168 uic_docmgr_init();
205
b1ac0dd1d38b new winui vs project, add toolbar
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 189
diff changeset
169 uic_toolbar_init();
379
958bae372271 implement ui_dialog_window (WINUI)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 373
diff changeset
170
958bae372271 implement ui_dialog_window (WINUI)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 373
diff changeset
171 uic_load_app_properties();
177
e79a60b3a7cb minimal working winui app
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 173
diff changeset
172 }
e79a60b3a7cb minimal working winui app
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 173
diff changeset
173
e79a60b3a7cb minimal working winui app
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 173
diff changeset
174 const char* ui_appname() {
e79a60b3a7cb minimal working winui app
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 173
diff changeset
175 return application_name;
e79a60b3a7cb minimal working winui app
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 173
diff changeset
176 }
e79a60b3a7cb minimal working winui app
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 173
diff changeset
177
e79a60b3a7cb minimal working winui app
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 173
diff changeset
178 void ui_onstartup(ui_callback f, void* userdata) {
e79a60b3a7cb minimal working winui app
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 173
diff changeset
179 startup_func = f;
e79a60b3a7cb minimal working winui app
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 173
diff changeset
180 startup_data = userdata;
e79a60b3a7cb minimal working winui app
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 173
diff changeset
181 }
e79a60b3a7cb minimal working winui app
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 173
diff changeset
182
e79a60b3a7cb minimal working winui app
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 173
diff changeset
183 void ui_onopen(ui_callback f, void* userdata) {
e79a60b3a7cb minimal working winui app
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 173
diff changeset
184 open_func = f;
e79a60b3a7cb minimal working winui app
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 173
diff changeset
185 open_data = userdata;
e79a60b3a7cb minimal working winui app
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 173
diff changeset
186 }
e79a60b3a7cb minimal working winui app
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 173
diff changeset
187
e79a60b3a7cb minimal working winui app
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 173
diff changeset
188 void ui_onexit(ui_callback f, void* userdata) {
e79a60b3a7cb minimal working winui app
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 173
diff changeset
189 exit_func = f;
e79a60b3a7cb minimal working winui app
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 173
diff changeset
190 exit_data = userdata;
e79a60b3a7cb minimal working winui app
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 173
diff changeset
191 }
e79a60b3a7cb minimal working winui app
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 173
diff changeset
192
e79a60b3a7cb minimal working winui app
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 173
diff changeset
193 void ui_main() {
221
a82d9beaa94a add semi-functional pathbar (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 213
diff changeset
194 /*
177
e79a60b3a7cb minimal working winui app
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 173
diff changeset
195 init_apartment();
209
770b3548906c use generated App class (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 208
diff changeset
196 //Application::Start([](auto&&) {make<App>(); });
770b3548906c use generated App class (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 208
diff changeset
197
770b3548906c use generated App class (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 208
diff changeset
198 ::winrt::Microsoft::UI::Xaml::Application::Start(
770b3548906c use generated App class (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 208
diff changeset
199 [](auto&&)
770b3548906c use generated App class (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 208
diff changeset
200 {
770b3548906c use generated App class (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 208
diff changeset
201 ::winrt::make<::winrt::winui::implementation::App>();
770b3548906c use generated App class (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 208
diff changeset
202 });
221
a82d9beaa94a add semi-functional pathbar (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 213
diff changeset
203 */
a82d9beaa94a add semi-functional pathbar (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 213
diff changeset
204 {
a82d9beaa94a add semi-functional pathbar (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 213
diff changeset
205 void (WINAPI * pfnXamlCheckProcessRequirements)();
a82d9beaa94a add semi-functional pathbar (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 213
diff changeset
206 auto module = ::LoadLibrary(L"Microsoft.ui.xaml.dll");
a82d9beaa94a add semi-functional pathbar (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 213
diff changeset
207 if (module)
a82d9beaa94a add semi-functional pathbar (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 213
diff changeset
208 {
a82d9beaa94a add semi-functional pathbar (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 213
diff changeset
209 pfnXamlCheckProcessRequirements = reinterpret_cast<decltype(pfnXamlCheckProcessRequirements)>(GetProcAddress(module, "XamlCheckProcessRequirements"));
a82d9beaa94a add semi-functional pathbar (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 213
diff changeset
210 if (pfnXamlCheckProcessRequirements)
a82d9beaa94a add semi-functional pathbar (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 213
diff changeset
211 {
a82d9beaa94a add semi-functional pathbar (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 213
diff changeset
212 (*pfnXamlCheckProcessRequirements)();
a82d9beaa94a add semi-functional pathbar (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 213
diff changeset
213 }
a82d9beaa94a add semi-functional pathbar (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 213
diff changeset
214
a82d9beaa94a add semi-functional pathbar (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 213
diff changeset
215 ::FreeLibrary(module);
a82d9beaa94a add semi-functional pathbar (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 213
diff changeset
216 }
a82d9beaa94a add semi-functional pathbar (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 213
diff changeset
217 }
a82d9beaa94a add semi-functional pathbar (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 213
diff changeset
218
a82d9beaa94a add semi-functional pathbar (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 213
diff changeset
219 winrt::init_apartment(winrt::apartment_type::single_threaded);
a82d9beaa94a add semi-functional pathbar (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 213
diff changeset
220 ::winrt::Microsoft::UI::Xaml::Application::Start(
a82d9beaa94a add semi-functional pathbar (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 213
diff changeset
221 [](auto&&)
a82d9beaa94a add semi-functional pathbar (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 213
diff changeset
222 {
a82d9beaa94a add semi-functional pathbar (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 213
diff changeset
223 ::winrt::make<::winrt::winui::implementation::App>();
a82d9beaa94a add semi-functional pathbar (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 213
diff changeset
224 });
177
e79a60b3a7cb minimal working winui app
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 173
diff changeset
225 }
e79a60b3a7cb minimal working winui app
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 173
diff changeset
226
183
3ce2eb11913b create WinUI wrapper classes for usage in UiObject and widget creation functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 181
diff changeset
227 class UiWin {
3ce2eb11913b create WinUI wrapper classes for usage in UiObject and widget creation functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 181
diff changeset
228 public:
3ce2eb11913b create WinUI wrapper classes for usage in UiObject and widget creation functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 181
diff changeset
229 Window window;
3ce2eb11913b create WinUI wrapper classes for usage in UiObject and widget creation functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 181
diff changeset
230 };
3ce2eb11913b create WinUI wrapper classes for usage in UiObject and widget creation functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 181
diff changeset
231
177
e79a60b3a7cb minimal working winui app
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 173
diff changeset
232 void ui_show(UiObject* obj) {
181
c52d88ea020b redefine UIWIDGET, remove widget abstraction class (WinUI)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 179
diff changeset
233 if (obj->wobj) {
183
3ce2eb11913b create WinUI wrapper classes for usage in UiObject and widget creation functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 181
diff changeset
234 obj->wobj->window.Activate();
379
958bae372271 implement ui_dialog_window (WINUI)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 373
diff changeset
235 } else if(obj->widget && obj->widget->Show) {
958bae372271 implement ui_dialog_window (WINUI)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 373
diff changeset
236 obj->widget->Show();
181
c52d88ea020b redefine UIWIDGET, remove widget abstraction class (WinUI)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 179
diff changeset
237 }
177
e79a60b3a7cb minimal working winui app
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 173
diff changeset
238 }
e79a60b3a7cb minimal working winui app
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 173
diff changeset
239
e79a60b3a7cb minimal working winui app
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 173
diff changeset
240 void ui_close(UiObject* obj) {
373
2eede3d98aba implement windows condvar
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 235
diff changeset
241 if (obj->wobj) {
2eede3d98aba implement windows condvar
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 235
diff changeset
242 obj->wobj->window.Close();
2eede3d98aba implement windows condvar
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 235
diff changeset
243 }
234
9036b346cd66 implement ui_job() and add ui_call_mainthread (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 232
diff changeset
244 }
9036b346cd66 implement ui_job() and add ui_call_mainthread (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 232
diff changeset
245
9036b346cd66 implement ui_job() and add ui_call_mainthread (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 232
diff changeset
246 static void ui_job_thread(UiJob* job) {
235
9c79f00fbf36 add threadpool (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 234
diff changeset
247 if (!job->job_func(job->job_data) && job->finish_callback) {
234
9036b346cd66 implement ui_job() and add ui_call_mainthread (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 232
diff changeset
248 bool isQueued = uiDispatcherQueue.TryEnqueue([job]()
9036b346cd66 implement ui_job() and add ui_call_mainthread (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 232
diff changeset
249 {
9036b346cd66 implement ui_job() and add ui_call_mainthread (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 232
diff changeset
250 UiEvent event;
9036b346cd66 implement ui_job() and add ui_call_mainthread (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 232
diff changeset
251 event.obj = job->obj;
9036b346cd66 implement ui_job() and add ui_call_mainthread (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 232
diff changeset
252 event.window = job->obj->window;
9036b346cd66 implement ui_job() and add ui_call_mainthread (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 232
diff changeset
253 event.document = job->obj->ctx->document;
9036b346cd66 implement ui_job() and add ui_call_mainthread (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 232
diff changeset
254 event.intval = 0;
9036b346cd66 implement ui_job() and add ui_call_mainthread (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 232
diff changeset
255 event.eventdata = NULL;
9036b346cd66 implement ui_job() and add ui_call_mainthread (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 232
diff changeset
256 job->finish_callback(&event, job->finish_data);
9036b346cd66 implement ui_job() and add ui_call_mainthread (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 232
diff changeset
257 delete job;
9036b346cd66 implement ui_job() and add ui_call_mainthread (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 232
diff changeset
258 });
9036b346cd66 implement ui_job() and add ui_call_mainthread (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 232
diff changeset
259 if (!isQueued) {
9036b346cd66 implement ui_job() and add ui_call_mainthread (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 232
diff changeset
260 // TODO: error or try again?
9036b346cd66 implement ui_job() and add ui_call_mainthread (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 232
diff changeset
261 exit(-1);
9036b346cd66 implement ui_job() and add ui_call_mainthread (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 232
diff changeset
262 }
9036b346cd66 implement ui_job() and add ui_call_mainthread (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 232
diff changeset
263 }
9036b346cd66 implement ui_job() and add ui_call_mainthread (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 232
diff changeset
264 else {
9036b346cd66 implement ui_job() and add ui_call_mainthread (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 232
diff changeset
265 delete job;
9036b346cd66 implement ui_job() and add ui_call_mainthread (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 232
diff changeset
266 }
9036b346cd66 implement ui_job() and add ui_call_mainthread (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 232
diff changeset
267 }
9036b346cd66 implement ui_job() and add ui_call_mainthread (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 232
diff changeset
268
9036b346cd66 implement ui_job() and add ui_call_mainthread (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 232
diff changeset
269 UIEXPORT void ui_job(UiObject* obj, ui_threadfunc tf, void* td, ui_callback f, void* fd) {
9036b346cd66 implement ui_job() and add ui_call_mainthread (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 232
diff changeset
270 UiJob* job = new UiJob;
9036b346cd66 implement ui_job() and add ui_call_mainthread (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 232
diff changeset
271 job->obj = obj;
9036b346cd66 implement ui_job() and add ui_call_mainthread (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 232
diff changeset
272 job->job_func = tf;
9036b346cd66 implement ui_job() and add ui_call_mainthread (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 232
diff changeset
273 job->job_data = td;
9036b346cd66 implement ui_job() and add ui_call_mainthread (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 232
diff changeset
274 job->finish_callback = f;
9036b346cd66 implement ui_job() and add ui_call_mainthread (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 232
diff changeset
275 job->finish_data = fd;
9036b346cd66 implement ui_job() and add ui_call_mainthread (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 232
diff changeset
276
9036b346cd66 implement ui_job() and add ui_call_mainthread (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 232
diff changeset
277 std::thread jobThread(ui_job_thread, job);
9036b346cd66 implement ui_job() and add ui_call_mainthread (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 232
diff changeset
278 jobThread.detach();
9036b346cd66 implement ui_job() and add ui_call_mainthread (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 232
diff changeset
279 }
9036b346cd66 implement ui_job() and add ui_call_mainthread (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 232
diff changeset
280
9036b346cd66 implement ui_job() and add ui_call_mainthread (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 232
diff changeset
281 UIEXPORT void ui_call_mainthread(ui_threadfunc tf, void* td) {
9036b346cd66 implement ui_job() and add ui_call_mainthread (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 232
diff changeset
282 bool isQueued = uiDispatcherQueue.TryEnqueue([tf, td]()
9036b346cd66 implement ui_job() and add ui_call_mainthread (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 232
diff changeset
283 {
9036b346cd66 implement ui_job() and add ui_call_mainthread (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 232
diff changeset
284 (void)tf(td);
9036b346cd66 implement ui_job() and add ui_call_mainthread (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 232
diff changeset
285 });
9036b346cd66 implement ui_job() and add ui_call_mainthread (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 232
diff changeset
286 if (!isQueued) {
9036b346cd66 implement ui_job() and add ui_call_mainthread (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 232
diff changeset
287 // TODO: error or try again?
9036b346cd66 implement ui_job() and add ui_call_mainthread (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 232
diff changeset
288 exit(-1);
9036b346cd66 implement ui_job() and add ui_call_mainthread (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 232
diff changeset
289 }
9036b346cd66 implement ui_job() and add ui_call_mainthread (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 232
diff changeset
290 }
235
9c79f00fbf36 add threadpool (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 234
diff changeset
291
9c79f00fbf36 add threadpool (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 234
diff changeset
292 static UiJob kill_job; // &kill_job indicates to stop the thread
9c79f00fbf36 add threadpool (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 234
diff changeset
293
9c79f00fbf36 add threadpool (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 234
diff changeset
294 static void ui_threadpool_run(UiThreadpool* pool) {
9c79f00fbf36 add threadpool (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 234
diff changeset
295 for (;;) {
9c79f00fbf36 add threadpool (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 234
diff changeset
296 UiJob* job = pool->GetJob();
9c79f00fbf36 add threadpool (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 234
diff changeset
297 if (job == &kill_job) {
9c79f00fbf36 add threadpool (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 234
diff changeset
298 return;
9c79f00fbf36 add threadpool (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 234
diff changeset
299 }
9c79f00fbf36 add threadpool (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 234
diff changeset
300 else if (job) {
9c79f00fbf36 add threadpool (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 234
diff changeset
301 ui_job_thread(job);
9c79f00fbf36 add threadpool (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 234
diff changeset
302 }
9c79f00fbf36 add threadpool (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 234
diff changeset
303 }
9c79f00fbf36 add threadpool (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 234
diff changeset
304 }
9c79f00fbf36 add threadpool (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 234
diff changeset
305
9c79f00fbf36 add threadpool (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 234
diff changeset
306 UiThreadpool::UiThreadpool(int nthreads) {
9c79f00fbf36 add threadpool (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 234
diff changeset
307 for (int i = 0; i < nthreads; i++) {
9c79f00fbf36 add threadpool (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 234
diff changeset
308 std::thread thread(ui_threadpool_run, this);
9c79f00fbf36 add threadpool (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 234
diff changeset
309 thread.detach();
9c79f00fbf36 add threadpool (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 234
diff changeset
310 }
9c79f00fbf36 add threadpool (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 234
diff changeset
311 }
9c79f00fbf36 add threadpool (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 234
diff changeset
312
9c79f00fbf36 add threadpool (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 234
diff changeset
313 void UiThreadpool::EnqueueJob(UiJob* job)
9c79f00fbf36 add threadpool (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 234
diff changeset
314 {
9c79f00fbf36 add threadpool (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 234
diff changeset
315 std::unique_lock<std::mutex> lock(mutex);
9c79f00fbf36 add threadpool (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 234
diff changeset
316 queue.push(job);
9c79f00fbf36 add threadpool (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 234
diff changeset
317 lock.unlock();
9c79f00fbf36 add threadpool (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 234
diff changeset
318 condition.notify_one();
9c79f00fbf36 add threadpool (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 234
diff changeset
319 }
9c79f00fbf36 add threadpool (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 234
diff changeset
320
9c79f00fbf36 add threadpool (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 234
diff changeset
321 UiJob* UiThreadpool::GetJob() {
9c79f00fbf36 add threadpool (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 234
diff changeset
322 std::unique_lock<std::mutex> lock(mutex);
9c79f00fbf36 add threadpool (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 234
diff changeset
323
9c79f00fbf36 add threadpool (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 234
diff changeset
324 UiJob* job = nullptr;
9c79f00fbf36 add threadpool (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 234
diff changeset
325 while (!job) {
9c79f00fbf36 add threadpool (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 234
diff changeset
326 if (queue.empty()) {
9c79f00fbf36 add threadpool (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 234
diff changeset
327 condition.wait(lock);
9c79f00fbf36 add threadpool (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 234
diff changeset
328 continue;
9c79f00fbf36 add threadpool (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 234
diff changeset
329 }
9c79f00fbf36 add threadpool (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 234
diff changeset
330 else
9c79f00fbf36 add threadpool (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 234
diff changeset
331 {
9c79f00fbf36 add threadpool (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 234
diff changeset
332 job = queue.front();
9c79f00fbf36 add threadpool (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 234
diff changeset
333 queue.pop();
9c79f00fbf36 add threadpool (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 234
diff changeset
334 }
9c79f00fbf36 add threadpool (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 234
diff changeset
335 }
9c79f00fbf36 add threadpool (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 234
diff changeset
336
9c79f00fbf36 add threadpool (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 234
diff changeset
337 return job;
9c79f00fbf36 add threadpool (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 234
diff changeset
338 }
9c79f00fbf36 add threadpool (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 234
diff changeset
339
9c79f00fbf36 add threadpool (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 234
diff changeset
340 UIEXPORT UiThreadpool* ui_threadpool_create(int nthreads) {
9c79f00fbf36 add threadpool (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 234
diff changeset
341 return new UiThreadpool(nthreads);
9c79f00fbf36 add threadpool (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 234
diff changeset
342 }
9c79f00fbf36 add threadpool (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 234
diff changeset
343
9c79f00fbf36 add threadpool (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 234
diff changeset
344 UIEXPORT void ui_threadpool_destroy(UiThreadpool* pool) {
9c79f00fbf36 add threadpool (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 234
diff changeset
345 // TODO
9c79f00fbf36 add threadpool (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 234
diff changeset
346 }
9c79f00fbf36 add threadpool (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 234
diff changeset
347
9c79f00fbf36 add threadpool (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 234
diff changeset
348 UIEXPORT void ui_threadpool_job(UiThreadpool* pool, UiObject* obj, ui_threadfunc tf, void* td, ui_callback f, void* fd) {
9c79f00fbf36 add threadpool (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 234
diff changeset
349 UiJob* job = new UiJob;
9c79f00fbf36 add threadpool (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 234
diff changeset
350 job->obj = obj;
9c79f00fbf36 add threadpool (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 234
diff changeset
351 job->job_func = tf;
9c79f00fbf36 add threadpool (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 234
diff changeset
352 job->job_data = td;
9c79f00fbf36 add threadpool (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 234
diff changeset
353 job->finish_callback = f;
9c79f00fbf36 add threadpool (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 234
diff changeset
354 job->finish_data = fd;
9c79f00fbf36 add threadpool (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 234
diff changeset
355 pool->EnqueueJob(job);
9c79f00fbf36 add threadpool (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 234
diff changeset
356 }
9c79f00fbf36 add threadpool (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 234
diff changeset
357

mercurial