ui/winui/toolkit.cpp

Tue, 12 Nov 2024 15:05:57 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Tue, 12 Nov 2024 15:05:57 +0100
changeset 79
483d7342b439
parent 77
5de33c2d94c6
permissions
-rw-r--r--

update toolkit (winui dialog window)

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

mercurial