ui/winui/toolkit.cpp

Thu, 15 Feb 2024 21:33:08 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Thu, 15 Feb 2024 21:33:08 +0100
branch
newapi
changeset 262
81778b9589d0
parent 235
9c79f00fbf36
child 373
2eede3d98aba
permissions
-rw-r--r--

port progressbar to new API (GTK)

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"
189
4daddc326877 add onchange event for toggle buttons (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 184
diff changeset
39
213
7e39db525fd9 add icons (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 212
diff changeset
40 #include "icons.h"
7e39db525fd9 add icons (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 212
diff changeset
41
205
b1ac0dd1d38b new winui vs project, add toolbar
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 189
diff changeset
42 #include "MainWindow.xaml.h"
b1ac0dd1d38b new winui vs project, add toolbar
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 189
diff changeset
43
b1ac0dd1d38b new winui vs project, add toolbar
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 189
diff changeset
44 #include "App.xaml.h"
177
e79a60b3a7cb minimal working winui app
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 173
diff changeset
45
e79a60b3a7cb minimal working winui app
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 173
diff changeset
46 using namespace winrt;
e79a60b3a7cb minimal working winui app
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 173
diff changeset
47 using namespace Microsoft::UI::Xaml;
e79a60b3a7cb minimal working winui app
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 173
diff changeset
48 using namespace Microsoft::UI::Xaml::Controls;
e79a60b3a7cb minimal working winui app
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 173
diff changeset
49 using namespace Microsoft::UI::Xaml::XamlTypeInfo;
e79a60b3a7cb minimal working winui app
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 173
diff changeset
50 using namespace Microsoft::UI::Xaml::Markup;
e79a60b3a7cb minimal working winui app
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 173
diff changeset
51 using namespace Windows::UI::Xaml::Interop;
184
8c9b4b28aaa9 implement ui_button (WinUI)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 183
diff changeset
52 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
53 using namespace Windows::UI::Core;
177
e79a60b3a7cb minimal working winui app
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 173
diff changeset
54
e79a60b3a7cb minimal working winui app
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 173
diff changeset
55 static const char* application_name;
e79a60b3a7cb minimal working winui app
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 173
diff changeset
56
e79a60b3a7cb minimal working winui app
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 173
diff changeset
57 static ui_callback startup_func;
e79a60b3a7cb minimal working winui app
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 173
diff changeset
58 static void* startup_data;
e79a60b3a7cb minimal working winui app
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 173
diff changeset
59
e79a60b3a7cb minimal working winui app
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 173
diff changeset
60 static ui_callback open_func;
e79a60b3a7cb minimal working winui app
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 173
diff changeset
61 void* open_data;
e79a60b3a7cb minimal working winui app
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 173
diff changeset
62
e79a60b3a7cb minimal working winui app
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 173
diff changeset
63 static ui_callback exit_func;
e79a60b3a7cb minimal working winui app
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 173
diff changeset
64 void* exit_data;
e79a60b3a7cb minimal working winui app
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 173
diff changeset
65
e79a60b3a7cb minimal working winui app
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 173
diff changeset
66 static ui_callback appclose_fnc;
e79a60b3a7cb minimal working winui app
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 173
diff changeset
67
e79a60b3a7cb minimal working winui app
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 173
diff changeset
68 static void* appclose_udata;
e79a60b3a7cb minimal working winui app
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 173
diff changeset
69
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 static UiObject* active_window;
e79a60b3a7cb minimal working winui app
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 173
diff changeset
72
234
9036b346cd66 implement ui_job() and add ui_call_mainthread (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 232
diff changeset
73 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
74
209
770b3548906c use generated App class (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 208
diff changeset
75 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
76 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
77
209
770b3548906c use generated App class (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 208
diff changeset
78 if (startup_func) {
770b3548906c use generated App class (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 208
diff changeset
79 startup_func(NULL, startup_data);
770b3548906c use generated App class (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 208
diff changeset
80 }
770b3548906c use generated App class (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 208
diff changeset
81 }
173
809581724cc7 add VS project
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
82
177
e79a60b3a7cb minimal working winui app
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 173
diff changeset
83 class App : public ApplicationT<App, IXamlMetadataProvider> {
e79a60b3a7cb minimal working winui app
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 173
diff changeset
84 public:
e79a60b3a7cb minimal working winui app
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 173
diff changeset
85 void OnLaunched(LaunchActivatedEventArgs const&) {
e79a60b3a7cb minimal working winui app
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 173
diff changeset
86 Resources().MergedDictionaries().Append(XamlControlsResources());
e79a60b3a7cb minimal working winui app
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 173
diff changeset
87 if (startup_func) {
e79a60b3a7cb minimal working winui app
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 173
diff changeset
88 startup_func(NULL, startup_data);
e79a60b3a7cb minimal working winui app
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 173
diff changeset
89 }
205
b1ac0dd1d38b new winui vs project, add toolbar
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 189
diff changeset
90
b1ac0dd1d38b new winui vs project, add toolbar
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 189
diff changeset
91 //auto window = make<winui::implementation::MainWindow>();
b1ac0dd1d38b new winui vs project, add toolbar
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 189
diff changeset
92 //window.Activate();
177
e79a60b3a7cb minimal working winui app
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 173
diff changeset
93 }
e79a60b3a7cb minimal working winui app
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 173
diff changeset
94 IXamlType GetXamlType(TypeName const& type) {
e79a60b3a7cb minimal working winui app
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 173
diff changeset
95 return provider.GetXamlType(type);
e79a60b3a7cb minimal working winui app
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 173
diff changeset
96 }
e79a60b3a7cb minimal working winui app
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 173
diff changeset
97 IXamlType GetXamlType(hstring const& fullname) {
e79a60b3a7cb minimal working winui app
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 173
diff changeset
98 return provider.GetXamlType(fullname);
e79a60b3a7cb minimal working winui app
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 173
diff changeset
99 }
e79a60b3a7cb minimal working winui app
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 173
diff changeset
100 com_array<XmlnsDefinition> GetXmlnsDefinitions() {
e79a60b3a7cb minimal working winui app
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 173
diff changeset
101 return provider.GetXmlnsDefinitions();
e79a60b3a7cb minimal working winui app
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 173
diff changeset
102 }
e79a60b3a7cb minimal working winui app
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 173
diff changeset
103 private:
e79a60b3a7cb minimal working winui app
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 173
diff changeset
104 XamlControlsXamlMetaDataProvider provider;
e79a60b3a7cb minimal working winui app
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 173
diff changeset
105 };
e79a60b3a7cb minimal working winui app
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 173
diff changeset
106
183
3ce2eb11913b create WinUI wrapper classes for usage in UiObject and widget creation functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 181
diff changeset
107 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
108
184
8c9b4b28aaa9 implement ui_button (WinUI)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 183
diff changeset
109 extern "C" void destroy_ui_window_wrapper(void* ptr) {
8c9b4b28aaa9 implement ui_button (WinUI)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 183
diff changeset
110 UiWindow* win = (UiWindow*)ptr;
8c9b4b28aaa9 implement ui_button (WinUI)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 183
diff changeset
111 delete win;
8c9b4b28aaa9 implement ui_button (WinUI)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 183
diff changeset
112 }
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 extern "C" void destroy_ui_widget_wrapper(void* ptr) {
8c9b4b28aaa9 implement ui_button (WinUI)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 183
diff changeset
115 UiWidget* widget = (UiWidget*)ptr;
8c9b4b28aaa9 implement ui_button (WinUI)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 183
diff changeset
116 delete widget;
8c9b4b28aaa9 implement ui_button (WinUI)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 183
diff changeset
117 }
8c9b4b28aaa9 implement ui_button (WinUI)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 183
diff changeset
118
212
ad5c74af14c4 add cleanup code for wrapper objects (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 209
diff changeset
119 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
120 UiContainer* ctn = (UiContainer*)ptr;
ad5c74af14c4 add cleanup code for wrapper objects (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 209
diff changeset
121 delete ctn;
ad5c74af14c4 add cleanup code for wrapper objects (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 209
diff changeset
122 }
ad5c74af14c4 add cleanup code for wrapper objects (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 209
diff changeset
123
184
8c9b4b28aaa9 implement ui_button (WinUI)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 183
diff changeset
124 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
125 cxMempoolRegister(ctx->mp, win, destroy_ui_window_wrapper);
184
8c9b4b28aaa9 implement ui_button (WinUI)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 183
diff changeset
126 }
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 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
129 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
130 }
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 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
133 cxMempoolRegister(ctx->mp, container, destroy_ui_container_wrapper);
184
8c9b4b28aaa9 implement ui_button (WinUI)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 183
diff changeset
134 }
183
3ce2eb11913b create WinUI wrapper classes for usage in UiObject and widget creation functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 181
diff changeset
135
177
e79a60b3a7cb minimal working winui app
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 173
diff changeset
136
189
4daddc326877 add onchange event for toggle buttons (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 184
diff changeset
137 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
138 UiEvent evt;
4daddc326877 add onchange event for toggle buttons (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 184
diff changeset
139 evt.obj = obj;
4daddc326877 add onchange event for toggle buttons (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 184
diff changeset
140 evt.window = obj->window;
4daddc326877 add onchange event for toggle buttons (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 184
diff changeset
141 evt.document = obj->ctx->document;
4daddc326877 add onchange event for toggle buttons (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 184
diff changeset
142 evt.eventdata = nullptr;
4daddc326877 add onchange event for toggle buttons (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 184
diff changeset
143 evt.intval = i;
4daddc326877 add onchange event for toggle buttons (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 184
diff changeset
144 return evt;
4daddc326877 add onchange event for toggle buttons (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 184
diff changeset
145 }
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
207
93b9f502cb88 add toolbar appmenu and menu buttons
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 205
diff changeset
148 #include <MddBootstrap.h>
177
e79a60b3a7cb minimal working winui app
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 173
diff changeset
149
208
f632bc0589ab new menu api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 207
diff changeset
150 void ui_appsdk_bootstrap(void) {
f632bc0589ab new menu api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 207
diff changeset
151 const UINT32 majorMinorVersion{ 0x00010002 };
f632bc0589ab new menu api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 207
diff changeset
152 PCWSTR versionTag{ L"" };
f632bc0589ab new menu api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 207
diff changeset
153 const PACKAGE_VERSION minVersion{};
f632bc0589ab new menu api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 207
diff changeset
154
f632bc0589ab new menu api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 207
diff changeset
155 const HRESULT hr = MddBootstrapInitialize(majorMinorVersion, versionTag, minVersion);
f632bc0589ab new menu api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 207
diff changeset
156 if (FAILED(hr)) {
f632bc0589ab new menu api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 207
diff changeset
157 exit(102);
f632bc0589ab new menu api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 207
diff changeset
158 }
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
177
e79a60b3a7cb minimal working winui app
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 173
diff changeset
161 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
162 application_name = appname;
205
b1ac0dd1d38b new winui vs project, add toolbar
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 189
diff changeset
163
208
f632bc0589ab new menu api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 207
diff changeset
164 //ui_appsdk_bootstrap();
f632bc0589ab new menu api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 207
diff changeset
165
229
a952760955b4 add menu item list (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 221
diff changeset
166 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
167 uic_docmgr_init();
205
b1ac0dd1d38b new winui vs project, add toolbar
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 189
diff changeset
168 uic_toolbar_init();
177
e79a60b3a7cb minimal working winui app
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 173
diff changeset
169 }
e79a60b3a7cb minimal working winui app
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 173
diff changeset
170
e79a60b3a7cb minimal working winui app
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 173
diff changeset
171 const char* ui_appname() {
e79a60b3a7cb minimal working winui app
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 173
diff changeset
172 return application_name;
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
e79a60b3a7cb minimal working winui app
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 173
diff changeset
175 void ui_onstartup(ui_callback f, void* userdata) {
e79a60b3a7cb minimal working winui app
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 173
diff changeset
176 startup_func = f;
e79a60b3a7cb minimal working winui app
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 173
diff changeset
177 startup_data = userdata;
e79a60b3a7cb minimal working winui app
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 173
diff changeset
178 }
e79a60b3a7cb minimal working winui app
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 173
diff changeset
179
e79a60b3a7cb minimal working winui app
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 173
diff changeset
180 void ui_onopen(ui_callback f, void* userdata) {
e79a60b3a7cb minimal working winui app
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 173
diff changeset
181 open_func = f;
e79a60b3a7cb minimal working winui app
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 173
diff changeset
182 open_data = userdata;
e79a60b3a7cb minimal working winui app
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 173
diff changeset
183 }
e79a60b3a7cb minimal working winui app
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 173
diff changeset
184
e79a60b3a7cb minimal working winui app
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 173
diff changeset
185 void ui_onexit(ui_callback f, void* userdata) {
e79a60b3a7cb minimal working winui app
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 173
diff changeset
186 exit_func = f;
e79a60b3a7cb minimal working winui app
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 173
diff changeset
187 exit_data = userdata;
e79a60b3a7cb minimal working winui app
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 173
diff changeset
188 }
e79a60b3a7cb minimal working winui app
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 173
diff changeset
189
e79a60b3a7cb minimal working winui app
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 173
diff changeset
190 void ui_main() {
221
a82d9beaa94a add semi-functional pathbar (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 213
diff changeset
191 /*
177
e79a60b3a7cb minimal working winui app
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 173
diff changeset
192 init_apartment();
209
770b3548906c use generated App class (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 208
diff changeset
193 //Application::Start([](auto&&) {make<App>(); });
770b3548906c use generated App class (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 208
diff changeset
194
770b3548906c use generated App class (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 208
diff changeset
195 ::winrt::Microsoft::UI::Xaml::Application::Start(
770b3548906c use generated App class (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 208
diff changeset
196 [](auto&&)
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::make<::winrt::winui::implementation::App>();
770b3548906c use generated App class (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 208
diff changeset
199 });
221
a82d9beaa94a add semi-functional pathbar (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 213
diff changeset
200 */
a82d9beaa94a add semi-functional pathbar (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 213
diff changeset
201 {
a82d9beaa94a add semi-functional pathbar (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 213
diff changeset
202 void (WINAPI * pfnXamlCheckProcessRequirements)();
a82d9beaa94a add semi-functional pathbar (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 213
diff changeset
203 auto module = ::LoadLibrary(L"Microsoft.ui.xaml.dll");
a82d9beaa94a add semi-functional pathbar (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 213
diff changeset
204 if (module)
a82d9beaa94a add semi-functional pathbar (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 213
diff changeset
205 {
a82d9beaa94a add semi-functional pathbar (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 213
diff changeset
206 pfnXamlCheckProcessRequirements = reinterpret_cast<decltype(pfnXamlCheckProcessRequirements)>(GetProcAddress(module, "XamlCheckProcessRequirements"));
a82d9beaa94a add semi-functional pathbar (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 213
diff changeset
207 if (pfnXamlCheckProcessRequirements)
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)();
a82d9beaa94a add semi-functional pathbar (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 213
diff changeset
210 }
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 ::FreeLibrary(module);
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
a82d9beaa94a add semi-functional pathbar (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 213
diff changeset
216 winrt::init_apartment(winrt::apartment_type::single_threaded);
a82d9beaa94a add semi-functional pathbar (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 213
diff changeset
217 ::winrt::Microsoft::UI::Xaml::Application::Start(
a82d9beaa94a add semi-functional pathbar (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 213
diff changeset
218 [](auto&&)
a82d9beaa94a add semi-functional pathbar (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 213
diff changeset
219 {
a82d9beaa94a add semi-functional pathbar (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 213
diff changeset
220 ::winrt::make<::winrt::winui::implementation::App>();
a82d9beaa94a add semi-functional pathbar (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 213
diff changeset
221 });
177
e79a60b3a7cb minimal working winui app
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 173
diff changeset
222 }
e79a60b3a7cb minimal working winui app
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 173
diff changeset
223
183
3ce2eb11913b create WinUI wrapper classes for usage in UiObject and widget creation functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 181
diff changeset
224 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
225 public:
3ce2eb11913b create WinUI wrapper classes for usage in UiObject and widget creation functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 181
diff changeset
226 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
227 };
3ce2eb11913b create WinUI wrapper classes for usage in UiObject and widget creation functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 181
diff changeset
228
177
e79a60b3a7cb minimal working winui app
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 173
diff changeset
229 void ui_show(UiObject* obj) {
181
c52d88ea020b redefine UIWIDGET, remove widget abstraction class (WinUI)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 179
diff changeset
230 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
231 obj->wobj->window.Activate();
181
c52d88ea020b redefine UIWIDGET, remove widget abstraction class (WinUI)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 179
diff changeset
232 } else {
183
3ce2eb11913b create WinUI wrapper classes for usage in UiObject and widget creation functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 181
diff changeset
233 // TODO
181
c52d88ea020b redefine UIWIDGET, remove widget abstraction class (WinUI)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 179
diff changeset
234 }
177
e79a60b3a7cb minimal working winui app
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 173
diff changeset
235 }
e79a60b3a7cb minimal working winui app
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 173
diff changeset
236
e79a60b3a7cb minimal working winui app
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 173
diff changeset
237 void ui_close(UiObject* obj) {
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
234
9036b346cd66 implement ui_job() and add ui_call_mainthread (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 232
diff changeset
241 static void ui_job_finished(UiJob *job) {
9036b346cd66 implement ui_job() and add ui_call_mainthread (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 232
diff changeset
242 UiEvent event;
9036b346cd66 implement ui_job() and add ui_call_mainthread (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 232
diff changeset
243 event.obj = job->obj;
9036b346cd66 implement ui_job() and add ui_call_mainthread (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 232
diff changeset
244 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
245 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
246 event.intval = 0;
9036b346cd66 implement ui_job() and add ui_call_mainthread (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 232
diff changeset
247 event.eventdata = NULL;
9036b346cd66 implement ui_job() and add ui_call_mainthread (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 232
diff changeset
248 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
249 }
9036b346cd66 implement ui_job() and add ui_call_mainthread (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 232
diff changeset
250
9036b346cd66 implement ui_job() and add ui_call_mainthread (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 232
diff changeset
251 static void ui_job_thread(UiJob* job) {
235
9c79f00fbf36 add threadpool (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 234
diff changeset
252 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
253 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
254 {
9036b346cd66 implement ui_job() and add ui_call_mainthread (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 232
diff changeset
255 UiEvent event;
9036b346cd66 implement ui_job() and add ui_call_mainthread (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 232
diff changeset
256 event.obj = job->obj;
9036b346cd66 implement ui_job() and add ui_call_mainthread (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 232
diff changeset
257 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
258 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
259 event.intval = 0;
9036b346cd66 implement ui_job() and add ui_call_mainthread (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 232
diff changeset
260 event.eventdata = NULL;
9036b346cd66 implement ui_job() and add ui_call_mainthread (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 232
diff changeset
261 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
262 delete job;
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 if (!isQueued) {
9036b346cd66 implement ui_job() and add ui_call_mainthread (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 232
diff changeset
265 // 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
266 exit(-1);
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 else {
9036b346cd66 implement ui_job() and add ui_call_mainthread (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 232
diff changeset
270 delete job;
9036b346cd66 implement ui_job() and add ui_call_mainthread (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 232
diff changeset
271 }
9036b346cd66 implement ui_job() and add ui_call_mainthread (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 232
diff changeset
272 }
9036b346cd66 implement ui_job() and add ui_call_mainthread (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 232
diff changeset
273
9036b346cd66 implement ui_job() and add ui_call_mainthread (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 232
diff changeset
274 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
275 UiJob* job = new UiJob;
9036b346cd66 implement ui_job() and add ui_call_mainthread (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 232
diff changeset
276 job->obj = obj;
9036b346cd66 implement ui_job() and add ui_call_mainthread (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 232
diff changeset
277 job->job_func = tf;
9036b346cd66 implement ui_job() and add ui_call_mainthread (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 232
diff changeset
278 job->job_data = td;
9036b346cd66 implement ui_job() and add ui_call_mainthread (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 232
diff changeset
279 job->finish_callback = f;
9036b346cd66 implement ui_job() and add ui_call_mainthread (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 232
diff changeset
280 job->finish_data = fd;
9036b346cd66 implement ui_job() and add ui_call_mainthread (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 232
diff changeset
281
9036b346cd66 implement ui_job() and add ui_call_mainthread (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 232
diff changeset
282 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
283 jobThread.detach();
9036b346cd66 implement ui_job() and add ui_call_mainthread (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 232
diff changeset
284 }
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 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
287 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
288 {
9036b346cd66 implement ui_job() and add ui_call_mainthread (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 232
diff changeset
289 (void)tf(td);
9036b346cd66 implement ui_job() and add ui_call_mainthread (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 232
diff changeset
290 });
9036b346cd66 implement ui_job() and add ui_call_mainthread (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 232
diff changeset
291 if (!isQueued) {
9036b346cd66 implement ui_job() and add ui_call_mainthread (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 232
diff changeset
292 // 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
293 exit(-1);
9036b346cd66 implement ui_job() and add ui_call_mainthread (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 232
diff changeset
294 }
9036b346cd66 implement ui_job() and add ui_call_mainthread (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 232
diff changeset
295 }
235
9c79f00fbf36 add threadpool (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 234
diff changeset
296
9c79f00fbf36 add threadpool (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 234
diff changeset
297 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
298
9c79f00fbf36 add threadpool (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 234
diff changeset
299 static void ui_threadpool_run(UiThreadpool* pool) {
9c79f00fbf36 add threadpool (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 234
diff changeset
300 for (;;) {
9c79f00fbf36 add threadpool (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 234
diff changeset
301 UiJob* job = pool->GetJob();
9c79f00fbf36 add threadpool (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 234
diff changeset
302 if (job == &kill_job) {
9c79f00fbf36 add threadpool (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 234
diff changeset
303 return;
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 else if (job) {
9c79f00fbf36 add threadpool (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 234
diff changeset
306 ui_job_thread(job);
9c79f00fbf36 add threadpool (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 234
diff changeset
307 }
9c79f00fbf36 add threadpool (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 234
diff changeset
308 }
9c79f00fbf36 add threadpool (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 234
diff changeset
309 }
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 UiThreadpool::UiThreadpool(int nthreads) {
9c79f00fbf36 add threadpool (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 234
diff changeset
312 for (int i = 0; i < nthreads; i++) {
9c79f00fbf36 add threadpool (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 234
diff changeset
313 std::thread thread(ui_threadpool_run, this);
9c79f00fbf36 add threadpool (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 234
diff changeset
314 thread.detach();
9c79f00fbf36 add threadpool (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 234
diff changeset
315 }
9c79f00fbf36 add threadpool (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 234
diff changeset
316 }
9c79f00fbf36 add threadpool (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 234
diff changeset
317
9c79f00fbf36 add threadpool (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 234
diff changeset
318 void UiThreadpool::EnqueueJob(UiJob* job)
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 std::unique_lock<std::mutex> lock(mutex);
9c79f00fbf36 add threadpool (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 234
diff changeset
321 queue.push(job);
9c79f00fbf36 add threadpool (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 234
diff changeset
322 lock.unlock();
9c79f00fbf36 add threadpool (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 234
diff changeset
323 condition.notify_one();
9c79f00fbf36 add threadpool (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 234
diff changeset
324 }
9c79f00fbf36 add threadpool (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 234
diff changeset
325
9c79f00fbf36 add threadpool (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 234
diff changeset
326 UiJob* UiThreadpool::GetJob() {
9c79f00fbf36 add threadpool (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 234
diff changeset
327 std::unique_lock<std::mutex> lock(mutex);
9c79f00fbf36 add threadpool (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 234
diff changeset
328
9c79f00fbf36 add threadpool (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 234
diff changeset
329 UiJob* job = nullptr;
9c79f00fbf36 add threadpool (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 234
diff changeset
330 while (!job) {
9c79f00fbf36 add threadpool (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 234
diff changeset
331 if (queue.empty()) {
9c79f00fbf36 add threadpool (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 234
diff changeset
332 condition.wait(lock);
9c79f00fbf36 add threadpool (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 234
diff changeset
333 continue;
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 else
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 job = queue.front();
9c79f00fbf36 add threadpool (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 234
diff changeset
338 queue.pop();
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 }
9c79f00fbf36 add threadpool (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 234
diff changeset
341
9c79f00fbf36 add threadpool (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 234
diff changeset
342 return job;
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
9c79f00fbf36 add threadpool (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 234
diff changeset
345 UIEXPORT UiThreadpool* ui_threadpool_create(int nthreads) {
9c79f00fbf36 add threadpool (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 234
diff changeset
346 return new UiThreadpool(nthreads);
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
9c79f00fbf36 add threadpool (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 234
diff changeset
349 UIEXPORT void ui_threadpool_destroy(UiThreadpool* pool) {
9c79f00fbf36 add threadpool (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 234
diff changeset
350 // TODO
9c79f00fbf36 add threadpool (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 234
diff changeset
351 }
9c79f00fbf36 add threadpool (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 234
diff changeset
352
9c79f00fbf36 add threadpool (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 234
diff changeset
353 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
354 UiJob* job = new UiJob;
9c79f00fbf36 add threadpool (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 234
diff changeset
355 job->obj = obj;
9c79f00fbf36 add threadpool (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 234
diff changeset
356 job->job_func = tf;
9c79f00fbf36 add threadpool (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 234
diff changeset
357 job->job_data = td;
9c79f00fbf36 add threadpool (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 234
diff changeset
358 job->finish_callback = f;
9c79f00fbf36 add threadpool (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 234
diff changeset
359 job->finish_data = fd;
9c79f00fbf36 add threadpool (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 234
diff changeset
360 pool->EnqueueJob(job);
9c79f00fbf36 add threadpool (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 234
diff changeset
361 }
9c79f00fbf36 add threadpool (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 234
diff changeset
362

mercurial