GUIFramework 1.1.0
Framework for desktop GUI applications in C++.
Loading...
Searching...
No Matches
BaseComposite.h
Go to the documentation of this file.
1#pragma once
2
4#include "Menu/Menu.h"
8
9namespace gui_framework
10{
11 namespace interfaces
12 {
13 class ICloseable;
14 }
15}
16
17namespace gui_framework
18{
21 public BaseComponent,
22 public interfaces::IIterable<BaseComponent, iterators::composite_forward_iterator, iterators::composite_const_forward_iterator>
23 {
24 public:
25 enum class exitMode
26 {
27 destroyWindow,
28 quit
29 };
30
31 protected:
32 std::string windowFunctionName;
35 std::vector<std::unique_ptr<BaseComponent>> children;
36 std::unordered_map<HMENU, Menu> popupMenus;
37 std::unique_ptr<Menu> mainMenu;
38 std::function<void()> onDestroy;
41
42 protected:
43 virtual LRESULT compositeWindowMessagesHandle(HWND handle, UINT message, WPARAM wparam, LPARAM lparam, bool& isUsed);
44
45 LRESULT windowMessagesHandle(HWND handle, UINT message, WPARAM wparam, LPARAM lparam, bool& isUsed) override;
46
47 virtual LRESULT preWindowMessagesHandle(HWND handle, UINT message, WPARAM wparam, LPARAM lparam, bool& isUsed) override;
48
49 private:
50 std::vector<std::pair<std::string, json::utility::jsonObject>> getChildrenStructure() const;
51
52 void addChild(BaseComponent* child);
53
54 void setExitCode(int exitCode);
55
56 public:
61 BaseComposite(const std::wstring& className, const std::wstring& windowName, const utility::ComponentSettings& settings, const interfaces::IStyles& styles, BaseComposite* parent = nullptr, const std::string& windowFunctionName = "", const std::string& moduleName = "", uint16_t smallIconResource = NULL, uint16_t largeIconResource = NULL);
62
63 void removeChild(BaseComponent* child);
64
65 void removeComponents(const std::wstring& componentName);
66
67 BaseComponent* findChild(const std::wstring& windowName) const;
68
69 BaseComponent* findChild(HWND handle) const;
70
71 std::vector<BaseComponent*> findChildren(const std::wstring& windowName) const;
72
75 virtual std::unique_ptr<Menu>& createMainMenu(const std::wstring& menuName);
76
79 virtual Menu& addPopupMenu(const std::wstring& menuName);
80
83 virtual void removePopupMenus(const std::wstring& menuName);
84
85 void setExitMode(exitMode mode);
86
87 void setOnDestroy(const std::function<void()>& onDestroy);
88
94 void setOnDestroy(const std::string& onDestroyFunctionName, const std::string& onDestroyFunctionModuleName);
95
96 exitMode getExitMode() const;
97
98 int getExitCode() const;
99
100 const std::vector<std::unique_ptr<BaseComponent>>& getChildren() const;
101
102 const std::unique_ptr<Menu>& getMainMenu() const;
103
104 std::unique_ptr<Menu>& getMainMenu();
105
106 std::vector<const Menu*> getPopupMenus() const;
107
108 const std::function<void()>& getOnDestroy() const;
109
112 virtual size_t getHash() const override = 0;
113
114 virtual iterators::composite_forward_iterator begin() noexcept override;
115
116 virtual iterators::composite_const_forward_iterator cbegin() const noexcept override;
117
118 virtual iterators::composite_forward_iterator end() noexcept override;
119
120 virtual iterators::composite_const_forward_iterator cend() const noexcept override;
121
122 virtual void setBackgroundColor(uint8_t red, uint8_t green, uint8_t blue) override;
123
124 virtual json::JSONBuilder getStructure() const override;
125
126 virtual ~BaseComposite();
127#pragma region FriendClasses
128 friend class BaseComponent;
129
131#pragma endregion
132 };
133}
134
135#define CREATE_DEFAULT_WINDOW_FUNCTION(className) extern "C" __declspec(dllexport) LRESULT className##WindowFunction(HWND handle, UINT message, WPARAM wparam, LPARAM lparam) \
136{ \
137 static gui_framework::BaseComposite* topLevelWindow = nullptr; \
138 \
139 switch(message) \
140 { \
141 case WM_CLOSE: \
142 if (topLevelWindow && (topLevelWindow->getHandle() == handle && topLevelWindow->getExitMode() == gui_framework::BaseComposite::exitMode::quit)) \
143 { \
144 if (gui_framework::__utility::useOnClose(topLevelWindow)) \
145 { \
146 DestroyWindow(handle); \
147 } \
148 \
149 return 0; \
150 } \
151 else \
152 { \
153 return DefWindowProcW(handle, message, wparam, lparam); \
154 } \
155 \
156 case WM_DESTROY: \
157 if (topLevelWindow) \
158 { \
159 topLevelWindow->getOnDestroy()(); \
160 \
161 if (topLevelWindow->getHandle() == handle && topLevelWindow->getExitMode() == gui_framework::BaseComposite::exitMode::quit) \
162 { \
163 PostQuitMessage(topLevelWindow->getExitCode()); \
164 } \
165 \
166 return 0; \
167 } \
168 else \
169 { \
170 return DefWindowProcW(handle, message, wparam, lparam); \
171 } \
172 \
173 case gui_framework::custom_window_messages::initTopLevelWindowPointer: \
174 topLevelWindow = reinterpret_cast<gui_framework::BaseComposite*>(wparam); \
175 \
176 return 0; \
177 \
178 case gui_framework::custom_window_messages::deinitTopLevelWindowPointer: \
179 topLevelWindow = nullptr; \
180 \
181 return 0; \
182 } \
183 \
184 if (topLevelWindow) \
185 { \
186 bool isUsed = false; \
187 \
188 LRESULT result = topLevelWindow->handleMessages(handle, message, wparam, lparam, isUsed); \
189 \
190 return isUsed ? \
191 result : \
192 DefWindowProcW(handle, message, wparam, lparam); \
193 } \
194 else \
195 { \
196 return DefWindowProcW(handle, message, wparam, lparam); \
197 } \
198}
#define GUI_FRAMEWORK_API
Base class for all windows, controls, etc.
Base class for all windows that has children windows.
std::unique_ptr< Menu > mainMenu
void setOnDestroy(const std::string &onDestroyFunctionName, const std::string &onDestroyFunctionModuleName)
Load function from module. Can be seriazlied.
void setOnDestroy(const std::function< void()> &onDestroy)
BaseComponent * findChild(const std::wstring &windowName) const
virtual size_t getHash() const override=0
Used as key in creators.
std::string onDestroyFunctionModuleName
std::unordered_map< HMENU, Menu > popupMenus
std::vector< std::unique_ptr< BaseComponent > > children
std::function< void()> onDestroy
Menu class.
Definition Menu.h:9
Specify iterable class.
Definition IIterable.h:15
Provides styles for other classes.
Definition IStyles.h:11
IBaseForwardIterator implementation for BaseComponent.