GUIFramework 1.1.0
Framework for desktop GUI applications in C++.
Loading...
Searching...
No Matches
WindowHolder.cpp
Go to the documentation of this file.
1#include "WindowHolder.h"
2
3#include "GUIFramework.h"
6
7using namespace std;
8
9namespace gui_framework
10{
11 WindowHolder::WindowHolder(BaseDialogBox* dialogBox) :
12 compositeWindow(dialogBox),
13 unregisterClass(false)
14 {
15
16 }
17
18 WindowHolder::WindowHolder(unique_ptr<BaseComposite>&& compositeWindow, bool unregisterClass) noexcept :
19 compositeWindow(move(compositeWindow)),
20 unregisterClass(unregisterClass)
21 {
22 if (BaseMainWindow* mainWindow = dynamic_cast<BaseMainWindow*>(this->compositeWindow.get()))
23 {
24 mainWindow->createMarkup();
25 }
26 }
27
28 BaseComposite* WindowHolder::get()
29 {
30 return compositeWindow.get();
31 }
32
33 const BaseComposite* WindowHolder::get() const
34 {
35 return compositeWindow.get();
36 }
37
38 int WindowHolder::runMainLoop(const vector<uint32_t>& registeredHotkeyIds)
39 {
40 MSG message = {};
41 int code;
42 GUIFramework& instance = GUIFramework::get();
43 DWORD currentThreadId = GetCurrentThreadId();
44
45 while (code = GetMessageW(&message, nullptr, NULL, NULL))
46 {
47 if (code == -1)
48 {
49 break;
50 }
51
52 TranslateMessage(&message);
53
54 DispatchMessageW(&message);
55
56 if (message.message == WM_KEYDOWN)
57 {
58 instance.processHotkeys();
59 }
60 else if (message.message == custom_window_messages::runOnUIThreadFunctions)
61 {
62 (*reinterpret_cast<std::function<void()>*>(message.wParam))();
63 }
64
65 if (currentThreadId == instance.uiThreadId)
66 {
67 unique_lock<recursive_mutex> runOnUIThreadLock(instance.runOnUIThreadMutex);
68 queue<function<void()>>& runOnUIFunctions = instance.runOnUIFunctions;
69
70 while (runOnUIFunctions.size())
71 {
72 function<void()>& currentFunction = runOnUIFunctions.front();
73 function<void()>* functionWrapper = new function<void()>();
74
75 (*functionWrapper) = [currentFunction, functionWrapper]()
76 {
77 currentFunction();
78
79 delete functionWrapper;
80 };
81
82 runOnUIFunctions.pop();
83
84 PostThreadMessageW(currentThreadId, custom_window_messages::runOnUIThreadFunctions, reinterpret_cast<WPARAM>(functionWrapper), NULL);
85 }
86 }
87 }
88
89 for (uint32_t hotkeyId : registeredHotkeyIds)
90 {
91 instance.unregisterHotkey(hotkeyId);
92 }
93
94 if (code == -1)
95 {
96 throw exceptions::GetLastErrorException(code, __FILE__, __FUNCTION__, __LINE__);
97 }
98
99 return static_cast<int>(message.wParam);
100 }
101
102 WindowHolder::~WindowHolder()
103 {
104 if (unregisterClass)
105 {
106 utility::unregisterClass(compositeWindow->getClassName());
107 }
108 }
109}
Base class for all windows that has children windows.
Singleton with GUIFramework settings and some functionality.
bool unregisterHotkey(size_t hotkeyId)
Thread safe unregister hotkey.
Exception that receive error code from GetLastError function.
void unregisterClass(wstring_view className)
Definition Utility.cpp:16