GUIFramework 1.1.0
Framework for desktop GUI applications in C++.
Loading...
Searching...
No Matches
BaseListBox.cpp
Go to the documentation of this file.
1#include "BaseListBox.h"
2
4
6
7#pragma push_macro("min")
8#pragma push_macro("max")
9#undef min
10#undef max
11
12using namespace std;
13
14namespace gui_framework
15{
16 void BaseListBox::updateLocalization(size_t index, wstring_view localizedText)
17 {
18 this->changeValue(localizedText, index);
19 }
20
21 BaseListBox::BaseListBox(wstring_view listBoxName, const utility::ComponentSettings& settings, const styles::ListBoxStyles& styles, BaseComposite* parent) :
23 (
24 standard_classes::listBox,
25 listBoxName,
26 settings,
27 styles,
28 parent
29 ),
30 IResizableComponent
31 (
32 handle,
33 parent ? parent->getHandle() : nullptr,
34 true
35 ),
36 requiredSize{ 0, 0 },
37 columnsWidth(0)
38 {
39
40 }
41
42 LRESULT BaseListBox::addValue(wstring_view value)
43 {
44 LRESULT result = SendMessageW(handle, LB_ADDSTRING, NULL, reinterpret_cast<LPARAM>(value.data()));
45
46 if (result == LB_ERR)
47 {
48 throw exceptions::SelectListException(__FUNCTION__, result, __FILE__, __FUNCTION__, __LINE__);
49 }
50 else if (result == LB_ERRSPACE)
51 {
52 throw exceptions::SelectListException(__FUNCTION__, result, __FILE__, __FUNCTION__, __LINE__, exception_messages::notEnoughSpace);
53 }
54
56
57 return result;
58 }
59
60 LRESULT BaseListBox::addValue(string_view localizationKey)
61 {
62 this->addLocalizationKey(localizationKey);
63
64 return this->addValue(localization::WTextLocalization::get()[localizationKey]);
65 }
66
67 LRESULT BaseListBox::removeValue(size_t index)
68 {
69 LRESULT result = SendMessageW(handle, LB_DELETESTRING, index, NULL);
70
71 if (result == LB_ERR)
72 {
73 throw exceptions::SelectListException(__FUNCTION__, result, __FILE__, __FUNCTION__, __LINE__);
74 }
75
76 IMultipleTextLocalized::removeLocalizationKey(index);
77
79
80 return result;
81 }
82
83 LRESULT BaseListBox::insertValue(wstring_view value, LRESULT index)
84 {
85 LRESULT result = SendMessageW(handle, LB_INSERTSTRING, index, reinterpret_cast<LPARAM>(value.data()));
86
87 if (result == LB_ERR)
88 {
89 throw exceptions::SelectListException(__FUNCTION__, result, __FILE__, __FUNCTION__, __LINE__);
90 }
91 else if (result == LB_ERRSPACE)
92 {
93 throw exceptions::SelectListException(__FUNCTION__, result, __FILE__, __FUNCTION__, __LINE__, exception_messages::notEnoughSpace);
94 }
95
97
98 return result;
99 }
100
101 LRESULT BaseListBox::insertValue(string_view localizationKey, LRESULT index)
102 {
103 this->addLocalizationKey(localizationKey);
104
105 return this->insertValue(localization::WTextLocalization::get()[localizationKey], index);
106 }
107
108 LRESULT BaseListBox::changeValue(wstring_view newValue, LRESULT index)
109 {
110 LRESULT result = SendMessageW(handle, LB_SETITEMDATA, index, reinterpret_cast<LPARAM>(newValue.data()));
111
112 if (result == LB_ERR)
113 {
114 throw exceptions::SelectListException(__FUNCTION__, result, __FILE__, __FUNCTION__, __LINE__);
115 }
116
118
119 return result;
120 }
121
122 LRESULT BaseListBox::changeValue(string_view localizationKey, LRESULT index)
123 {
124 this->addLocalizationKey(localizationKey);
125
126 return this->changeValue(localization::WTextLocalization::get()[localizationKey], index);
127 }
128
129 LRESULT BaseListBox::findSubstring(wstring_view subStringToFind)
130 {
131 LRESULT findedIndex = SendMessageW(handle, LB_FINDSTRING, 0, reinterpret_cast<LPARAM>(subStringToFind.data()));
132
133 if (findedIndex == LB_ERR)
134 {
135 throw exceptions::SelectListException(__FUNCTION__, findedIndex, __FILE__, __FUNCTION__, __LINE__);
136 }
137
138 return findedIndex;
139 }
140
141 LRESULT BaseListBox::findString(wstring_view stringToFind)
142 {
143 LRESULT findedIndex = SendMessageW(handle, LB_FINDSTRINGEXACT, 0, reinterpret_cast<LPARAM>(stringToFind.data()));
144
145 if (findedIndex == LB_ERR)
146 {
147 throw exceptions::SelectListException(__FUNCTION__, findedIndex, __FILE__, __FUNCTION__, __LINE__);
148 }
149
150 return findedIndex;
151 }
152
153 wstring BaseListBox::getValue(size_t index) const
154 {
155 wstring result;
156 LRESULT size = SendMessageW(handle, LB_GETTEXTLEN, index, NULL);
157
158 if (size == LB_ERR)
159 {
160 throw exceptions::SelectListException(__FUNCTION__, size, __FILE__, __FUNCTION__, __LINE__);
161 }
162
163 result.resize(++size);
164
165 LRESULT errorCode = SendMessageW(handle, LB_GETTEXT, index, reinterpret_cast<LPARAM>(result.data()));
166
167 if (errorCode == LB_ERR)
168 {
169 throw exceptions::SelectListException(__FUNCTION__, errorCode, __FILE__, __FUNCTION__, __LINE__);
170 }
171
172 result.pop_back();
173
174 return result;
175 }
176
178 {
179 return SendMessageW(handle, LB_GETCURSEL, NULL, NULL);
180 }
181
182 LRESULT BaseListBox::setCurrentSelection(LRESULT index) const
183 {
184 LRESULT result = SendMessageW(handle, LB_SETCURSEL, index, NULL);
185
186 if (result == LB_ERR)
187 {
188 throw exceptions::SelectListException(__FUNCTION__, result, __FILE__, __FUNCTION__, __LINE__);
189 }
190
191 return result;
192 }
193
194 LRESULT BaseListBox::size() const
195 {
196 LRESULT result = SendMessageW(handle, LB_GETCOUNT, NULL, NULL);
197
198 if (result == LB_ERR)
199 {
200 throw exceptions::SelectListException(__FUNCTION__, result, __FILE__, __FUNCTION__, __LINE__);
201 }
202
203 return result;
204 }
205
207 {
208 SendMessageW(handle, LB_RESETCONTENT, NULL, NULL);
209 }
210
211 LRESULT BaseListBox::setItemsHeight(uint8_t height)
212 {
213 LRESULT result = SendMessageW(handle, LB_SETITEMHEIGHT, NULL, height);
214
215 if (result == LB_ERR)
216 {
217 throw exceptions::SelectListException(__FUNCTION__, result, __FILE__, __FUNCTION__, __LINE__);
218 }
219
220 return result;
221 }
222
223 LRESULT BaseListBox::setColumnsWidth(uint8_t width)
224 {
225 LRESULT result = SendMessageW(handle, LB_SETCOLUMNWIDTH, width, NULL);
226
227 if (result == LB_ERR)
228 {
229 throw exceptions::SelectListException(__FUNCTION__, result, __FILE__, __FUNCTION__, __LINE__);
230 }
231
232 columnsWidth = width;
233
234 return result;
235 }
236
238 {
239 LRESULT result = SendMessageW(handle, LB_GETITEMHEIGHT, NULL, NULL);
240
241 if (result == LB_ERR)
242 {
243 throw exceptions::SelectListException(__FUNCTION__, result, __FILE__, __FUNCTION__, __LINE__);
244 }
245
246 return result;
247 }
248
250 {
251 return columnsWidth;
252 }
253
254 void BaseListBox::resize(uint16_t width, uint16_t height)
255 {
256 if (autoResize && !blockResize)
257 {
258 LRESULT currentSize = this->size();
259 HDC deviceContext = GetDC(handle);
260 int heightSum = 0;
261
262 if (currentSize == CB_ERR || !currentSize)
263 {
264 double widthCoefficient = static_cast<double>(width) / parentWidth;
265
266 MoveWindow
267 (
268 handle,
269 static_cast<int>(desiredX * widthCoefficient),
270 static_cast<int>(desiredY * (static_cast<double>(height) / parentHeight)),
271 static_cast<int>(desiredWidth * widthCoefficient),
273 true
274 );
275
276 ShowWindow(handle, SW_SHOW);
277
278 return;
279 }
280
281 for (size_t i = 0; i < static_cast<size_t>(currentSize); i++)
282 {
283 wstring value = this->getValue(i);
284 SIZE valueSizes;
285
286 if (GetTextExtentPoint32W(deviceContext, value.data(), static_cast<int>(value.size()), &valueSizes))
287 {
288 if (valueSizes.cx > requiredSize.cx)
289 {
290 requiredSize.cx = valueSizes.cx;
291 }
292
293 if (valueSizes.cy > requiredSize.cy)
294 {
295 requiredSize.cy = valueSizes.cy;
296 }
297
298 heightSum += valueSizes.cy;
299 }
300 }
301
302 uint16_t newHeight = 0;
303
304 if (heightSum > desiredHeight)
305 {
306 utility::appendStyle(handle, WS_VSCROLL);
307
308 newHeight = desiredHeight;
309 }
310 else
311 {
312 utility::removeStyle(handle, WS_VSCROLL);
313
314 this->setItemsHeight(static_cast<uint8_t>(requiredSize.cy));
315
316 newHeight = static_cast<uint16_t>(heightSum + requiredSize.cy * 2);
317 }
318
319 MoveWindow
320 (
321 handle,
322 static_cast<int>(desiredX * (static_cast<double>(width) / parentWidth)),
323 static_cast<int>(desiredY * (static_cast<double>(height) / parentHeight)),
324 max(requiredSize.cx + standard_sizes::listBoxAdditionalWidth, static_cast<long>(desiredWidth * (static_cast<double>(width) / parentWidth))),
325 newHeight,
326 true
327 );
328
329 ShowWindow(handle, SW_SHOW);
330 }
331 }
332
333 json::JSONBuilder BaseListBox::getStructure() const
334 {
335 using json::utility::jsonObject;
336
337 uint32_t codepage = ISerializable::getCodepage();
338 json::JSONBuilder builder = BaseComponent::getStructure();
339 jsonObject& current = get<jsonObject>(builder[utility::to_string(windowName, codepage)]);
340 vector<jsonObject> values;
341 LRESULT currentSize = this->size();
342
343 current.data.push_back({ "columnWidth"s, static_cast<uint64_t>(columnsWidth) });
344
345 if (currentSize > 0)
346 {
347 for (size_t i = 0; i < static_cast<size_t>(currentSize); i++)
348 {
349 json::utility::appendArray(utility::to_string(this->getValue(i), codepage), values);
350 }
351
352 current.data.push_back({ "listBoxValues"s, move(values) });
353 }
354
355 return builder;
356 }
357}
358
359#pragma pop_macro("min")
360#pragma pop_macro("max")
Base class for all windows, controls, etc.
virtual json::JSONBuilder getStructure() const override
const std::wstring windowName
Base class for all windows that has children windows.
std::wstring getValue(size_t index) const
LRESULT setItemsHeight(uint8_t height)
LRESULT addValue(std::wstring_view value)
LRESULT getItemsHeight() const
virtual LRESULT insertValue(std::wstring_view value, LRESULT index)
virtual json::JSONBuilder getStructure() const override
LRESULT findSubstring(std::wstring_view subStringToFind)
uint8_t getColumnsWidth() const
LRESULT findString(std::wstring_view stringToFind)
virtual LRESULT setCurrentSelection(LRESULT index) const
LRESULT removeValue(size_t index)
LRESULT getCurrentSelectionIndex() const
BaseListBox(std::wstring_view listBoxName, const utility::ComponentSettings &settings, const styles::ListBoxStyles &styles, BaseComposite *parent)
virtual LRESULT changeValue(std::wstring_view newValue, LRESULT index)
virtual void updateLocalization(size_t index, std::wstring_view localizedText) override
virtual void resize(uint16_t width, uint16_t height) override
LRESULT setColumnsWidth(uint8_t width)
Exception for all list classes exceptions.
void addLocalizationKey(std::string_view localizationKey)
Base class for list box styles.
constexpr std::string_view notEnoughSpace
void removeStyle(HWND handle, LONG_PTR styleToRemove)
Remove WinAPI style.
Definition Utility.cpp:31
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