GUIFramework 1.1.0
Framework for desktop GUI applications in C++.
Loading...
Searching...
No Matches
Utility.cpp
Go to the documentation of this file.
1#include "Utility.h"
2
5#include "GUIFramework.h"
6
9
10using namespace std;
11
12namespace gui_framework
13{
14 namespace utility
15 {
16 void unregisterClass(wstring_view className)
17 {
18 UnregisterClassW(className.data(), GetModuleHandleW(nullptr));
19 }
20
21 void appendStyle(HWND handle, LONG_PTR newStyle)
22 {
23 SetWindowLongPtrW
24 (
25 handle,
26 GWL_STYLE,
27 GetWindowLongPtrW(handle, GWL_STYLE) | newStyle
28 );
29 }
30
31 void removeStyle(HWND handle, LONG_PTR styleToRemove)
32 {
33 SetWindowLongPtrW
34 (
35 handle,
36 GWL_STYLE,
37 GetWindowLongPtrW(handle, GWL_STYLE) & ~styleToRemove
38 );
39 }
40
41 string to_string(wstring_view stringToConvert, uint32_t codepage)
42 {
43 string result;
44
45 int size = WideCharToMultiByte
46 (
47 codepage,
48 NULL,
49 stringToConvert.data(),
50 -1,
51 nullptr,
52 NULL,
53 NULL,
54 NULL
55 );
56
57 if (!size)
58 {
59 throw json::exceptions::WrongEncodingException();
60 }
61
62 result.resize(static_cast<size_t>(size) - 1);
63
64 if (!WideCharToMultiByte
65 (
66 codepage,
67 NULL,
68 stringToConvert.data(),
69 -1,
70 result.data(),
71 size,
72 NULL,
73 NULL
74 ))
75 {
76 throw json::exceptions::WrongEncodingException();
77 }
78
79 return result;
80 }
81
82 wstring to_wstring(const string& stringToConvert, uint32_t codepage)
83 {
84 wstring result;
85
86 int size = MultiByteToWideChar
87 (
88 codepage,
89 NULL,
90 stringToConvert.data(),
91 -1,
92 nullptr,
93 NULL
94 );
95
96 if (!size)
97 {
98 throw json::exceptions::WrongEncodingException();
99 }
100
101 result.resize(static_cast<size_t>(size) - 1);
102
103 if (!MultiByteToWideChar
104 (
105 codepage,
106 NULL,
107 stringToConvert.data(),
108 -1,
109 result.data(),
110 size
111 ))
112 {
113 throw json::exceptions::WrongEncodingException();
114 }
115
116 return result;
117 }
118
120 {
121 return GetModuleHandleW(nullptr);
122 }
123
124 string getStringFromRawPath(const filesystem::path& pathFromRawString)
125 {
126 ostringstream fixRawString;
127 string fixedPath;
128
129 fixRawString << pathFromRawString;
130
131 fixedPath = fixRawString.str();
132
133 erase(fixedPath, '\"');
134
135 return fixedPath;
136 }
137
138 void loadFunctionFromModule(function<void()>& onClick, const string& functionName, const string& moduleName)
139 {
140 GUIFramework& instance = GUIFramework::get();
141 const HMODULE& module = instance.getModules().at(moduleName);
142
143 onClickSignature tem = reinterpret_cast<onClickSignature>(GetProcAddress(module, functionName.data()));
144
145 if (!tem)
146 {
147 throw exceptions::CantFindFunctionFromModuleException(functionName, moduleName, __FILE__, __FUNCTION__, __LINE__);
148 }
149
150 onClick = tem;
151 }
152
153 void loadEventCallbackFromModule(function<void(const wstring&)>& eventCallback, const string& functionName, const string& moduleName)
154 {
155 GUIFramework& instance = GUIFramework::get();
156 const HMODULE& module = instance.getModules().at(moduleName);
157
158 richEditCallbackSignature tem = reinterpret_cast<richEditCallbackSignature>(GetProcAddress(module, functionName.data()));
159
160 if (!tem)
161 {
162 throw exceptions::CantFindFunctionFromModuleException(functionName, moduleName, __FILE__, __FUNCTION__, __LINE__);
163 }
164
165 eventCallback = tem;
166 }
167
168 int getCenterX(int width)
169 {
170 return (GetSystemMetrics(SM_CXSCREEN) - GetSystemMetrics(SM_CXBORDER) - width) / 2;
171 }
172
173 int getCenterY(int height)
174 {
175 return (GetSystemMetrics(SM_CYSCREEN) - GetSystemMetrics(SM_CYBORDER) - height) / 2;
176 }
177
179 {
180 return "1.0.2";
181 }
182 }
183
184 namespace __utility
185 {
186 void throwNotImplementedException(string_view methodName, string_view className)
187 {
188 try
189 {
191 {
192 throw exceptions::NotImplemented(methodName, className, __FILE__, __LINE__);
193 }
194 }
195 catch (const json::exceptions::CantFindValueException&)
196 {
197
198 }
199 }
200
201 string extendedException(const string& exceptionMessage, string_view fileName, string_view methodName, int line)
202 {
203 try
204 {
205 if (GUIFramework::get().getJSONSettings().getBool(json_settings::usingExtendedExceptions))
206 {
207 return format(R"({} in file "{}" in "{}" method on {} line)"sv, exceptionMessage, fileName.substr(fileName.rfind('\\') + 1), methodName, line);
208 }
209 }
210 catch (const json::exceptions::CantFindValueException&)
211 {
212
213 }
214
215 return exceptionMessage;
216 }
217
218 bool useOnClose(any topLevelWindow)
219 {
220 interfaces::ICloseable* tem = dynamic_cast<interfaces::ICloseable*>(any_cast<BaseComposite*>(topLevelWindow));
221
222 return tem && tem->getOnClose()();
223 }
224 }
225}
Singleton with GUIFramework settings and some functionality.
const std::unordered_map< std::string, HMODULE, localization::utility::StringViewHash, localization::utility::StringViewEqual > & getModules() const
Get all loaded modules.
static GUIFramework & get()
Singleton instance access.
Throws by not implemented methods.
virtual const std::function< bool()> & getOnClose() const final
bool useOnClose(any topLevelWindow)
Definition Utility.cpp:218
string extendedException(const string &exceptionMessage, string_view fileName, string_view methodName, int line)
Definition Utility.cpp:201
void throwNotImplementedException(string_view methodName, string_view className)
Definition Utility.cpp:186
const std::string usingNotImplementedExceptions
wstring to_wstring(const string &stringToConvert, uint32_t codepage)
Definition Utility.cpp:82
HMODULE getCurrentModule()
Get handle to current executable.
Definition Utility.cpp:119
void loadFunctionFromModule(function< void()> &onClick, const string &functionName, const string &moduleName)
Definition Utility.cpp:138
int getCenterY(int height)
Get center Y coordinate.
Definition Utility.cpp:173
string getStringFromRawPath(const filesystem::path &pathFromRawString)
Definition Utility.cpp:124
int getCenterX(int width)
Get center X coordinate.
Definition Utility.cpp:168
void removeStyle(HWND handle, LONG_PTR styleToRemove)
Remove WinAPI style.
Definition Utility.cpp:31
void unregisterClass(wstring_view className)
Definition Utility.cpp:16
void loadEventCallbackFromModule(function< void(const wstring &)> &eventCallback, const string &functionName, const string &moduleName)
Definition Utility.cpp:153
string getGUIFrameworkVersion()
Get current version of GUIFramework.
Definition Utility.cpp:178
void appendStyle(HWND handle, LONG_PTR newStyle)
Append WinAPI style.
Definition Utility.cpp:21
string to_string(wstring_view stringToConvert, uint32_t codepage)
Definition Utility.cpp:41
void(*)() onClickSignature
Default on click signature.
void(*)(const std::wstring &) richEditCallbackSignature
Used in rich edit auto url detect events.