GUIFramework 1.1.0
Framework for desktop GUI applications in C++.
Loading...
Searching...
No Matches
BaseWindow.cpp
Go to the documentation of this file.
1#include "BaseWindow.h"
2
4
8
9using namespace std;
10
11namespace gui_framework
12{
13 BaseWindow::drawedImages::drawedImages(smartPointerType<utility::BaseLoadableHolder>&& images, utility::BaseLoadableHolder::imageType type) noexcept :
14 images(move(images)),
15 type(type)
16 {
17
18 }
19
20 void BaseWindow::drawedImages::addImage(BaseWindow* owner, int x, int y, const filesystem::path& pathToImage)
21 {
22 uint16_t index = images->addImage(pathToImage);
23
24 coordinates.insert({ index, {x, y} });
25 }
26
27 void BaseWindow::drawedImages::removeImage(const filesystem::path& pathToImage)
28 {
29 uint16_t index = images->getImageIndex(pathToImage);
30
31 images->removeImage(index);
32
33 coordinates.erase(index);
34 }
35
36 BaseWindow::BaseWindow(const std::wstring& className, const std::wstring& windowName, const utility::ComponentSettings& settings, const interfaces::IStyles& styles, BaseComposite* parent, const string& windowFunctionName, const string& moduleName, uint16_t smallIconResource, uint16_t largeIconResource) :
38 (
41 settings,
42 styles,
43 parent,
45 moduleName,
46 smallIconResource,
47 largeIconResource
48 ),
50 {
51
52 }
53
54 void BaseWindow::initDrawing(const string& pictureBlockName, uint16_t imagesWidth, uint16_t imagesHeight, utility::BaseLoadableHolder::imageType type)
55 {
57
58 switch (type)
59 {
60 case utility::BaseLoadableHolder::imageType::bitmap:
61 images = utility::make_smart_pointer<utility::ImagesHolder>(imagesWidth, imagesHeight);
62
63 break;
64 case utility::BaseLoadableHolder::imageType::icon:
65 images = utility::make_smart_pointer<utility::IconsHolder>(imagesWidth, imagesHeight);
66
67 break;
68 case utility::BaseLoadableHolder::imageType::cursor:
69 throw runtime_error("Wrong type value");
70
71 break;
72 default:
73 break;
74 }
75
76 pictures.try_emplace(pictureBlockName, move(images), type);
77 }
78
79 void BaseWindow::addImage(const string& pictureBlockName, int x, int y, const filesystem::path& pathToImage)
80 {
81 drawedImages& images = pictures.at(pictureBlockName);
82
83 if (!filesystem::exists(pathToImage))
84 {
85 throw exceptions::FileDoesNotExist(pathToImage, __FILE__, __FUNCTION__, __LINE__);
86 }
87
88 images.addImage(this, x, y, pathToImage);
89
90 this->drawAllImages();
91 }
92
93 void BaseWindow::removeImage(const string& pictureBlockName, const filesystem::path& pathToImage)
94 {
95 drawedImages& images = pictures.at(pictureBlockName);
96
97 images.removeImage(pathToImage);
98
99 this->drawAllImages();
100 }
101
103 {
104 InvalidateRect(handle, nullptr, true);
105
106 PAINTSTRUCT paint = {};
107 HDC deviceContext = BeginPaint(handle, &paint);
108 LPARAM drawData = NULL;
109
110 for (const auto& [picturesBlockName, data] : pictures)
111 {
112 uint32_t flags = DSS_NORMAL;
113 uint16_t width = data.images->getImagesWidth();
114 uint16_t height = data.images->getImagesHeight();
115
116 for (const auto& [index, coordinates] : data.coordinates)
117 {
118 switch (data.type)
119 {
120 case utility::BaseLoadableHolder::imageType::bitmap:
121 drawData = reinterpret_cast<LPARAM>(dynamic_cast<utility::ImagesHolder*>(data.images.get())->getImage(index));
122
123 flags |= DST_BITMAP;
124
125 break;
126 case utility::BaseLoadableHolder::imageType::icon:
127 drawData = reinterpret_cast<LPARAM>(dynamic_cast<utility::IconsHolder*>(data.images.get())->getIcon(index));
128
129 flags |= DST_ICON;
130
131 break;
132
133 default:
134 break;
135 }
136
137 DrawStateW(deviceContext, NULL, nullptr, drawData, NULL, coordinates.first, coordinates.second, width, height, flags);
138 }
139 }
140
141 ReleaseDC(handle, deviceContext);
142
143 EndPaint(handle, &paint);
144 }
145
146 void BaseWindow::setBackgroundColor(uint8_t red, uint8_t green, uint8_t blue)
147 {
148 BaseComposite::setBackgroundColor(red, green, blue);
149
150 this->drawAllImages();
151 }
152
153 json::JSONBuilder BaseWindow::getStructure() const
154 {
155 using json::utility::jsonObject;
156
157 if (pictures.empty())
158 {
160 }
161
162 json::JSONBuilder builder = BaseComposite::getStructure();
163 jsonObject& current = get<jsonObject>(builder[utility::to_string(windowName, ISerializable::getCodepage())]);
164
165 for (const auto& [pictureBlockName, data] : pictures)
166 {
167 string imageHolderName = pictureBlockName + "ImageHolder";
168 vector<jsonObject> jsonCoordinates;
169
170 auto& lastImageHolderStructure = data.images->loadBaseLoadableHolderStructure(current);
171
172 lastImageHolderStructure.first = imageHolderName;
173
174 for (const auto& [index, coordinates] : data.coordinates)
175 {
176 jsonObject object;
177
178 object.data.push_back({ "x"s, static_cast<int64_t>(coordinates.first) });
179 object.data.push_back({ "y"s, static_cast<int64_t>(coordinates.second) });
180
181 json::utility::appendArray(move(object), jsonCoordinates);
182 }
183
184 get<jsonObject>(lastImageHolderStructure.second).data.push_back({ "coordinates"s, move(jsonCoordinates) });
185 }
186
187 return builder;
188 }
189}
std::unique_ptr< T > smartPointerType
Compatible smart pointer type.
const std::wstring className
const std::wstring windowName
Base class for all windows that has children windows.
virtual json::JSONBuilder getStructure() const override
virtual void setBackgroundColor(uint8_t red, uint8_t green, uint8_t blue) override
virtual void initDrawing(const std::string &pictureBlockName, uint16_t imagesWidth, uint16_t imagesHeight, utility::BaseLoadableHolder::imageType type) final
Initialize block of pictures with same size.
virtual void setBackgroundColor(uint8_t red, uint8_t green, uint8_t blue) final override
virtual json::JSONBuilder getStructure() const override
virtual void addImage(const std::string &pictureBlockName, int x, int y, const std::filesystem::path &pathToImage)
Draw image in window.
virtual void drawAllImages() final
Clear window and draw all images.
std::unordered_map< std::string, drawedImages > pictures
Definition BaseWindow.h:29
virtual void removeImage(const std::string &pictureBlockName, const std::filesystem::path &pathToImage)
Remove image from window.
BaseWindow(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)
Throws by asset finding methods.
Provides styles for other classes.
Definition IStyles.h:11
smartPointerType< T > make_smart_pointer(Args &&... args)
Make function with current build configuration compatibility.
Definition Utility.h:96
string to_string(wstring_view stringToConvert, uint32_t codepage)
Definition Utility.cpp:41