GCC Code Coverage Report


Directory: src/gate/
File: src/gate/ui/gateui.hpp
Date: 2026-08-06 22:46:24
Exec Total Coverage
Lines: 62 74 83.8%
Functions: 37 83 44.6%
Branches: 5 16 31.2%

Line Branch Exec Source
1 /* GATE PROJECT LICENSE:
2 +----------------------------------------------------------------------------+
3 | Copyright (c) 2018-2026, Stefan Meislinger <sm@opengate.at> |
4 | All rights reserved. |
5 | |
6 | Redistribution and use in source and binary forms, with or without |
7 | modification, are permitted provided that the following conditions are met:|
8 | |
9 | 1. Redistributions of source code must retain the above copyright notice, |
10 | this list of conditions and the following disclaimer. |
11 | 2. Redistributions in binary form must reproduce the above copyright |
12 | notice, this list of conditions and the following disclaimer in the |
13 | documentation and/or other materials provided with the distribution. |
14 | |
15 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"|
16 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
17 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
18 | ARE DISCLAIMED.IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
19 | LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
20 | CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
21 | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
22 | INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
23 | CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
24 | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
25 | THE POSSIBILITY OF SUCH DAMAGE. |
26 +----------------------------------------------------------------------------+
27 */
28
29 /** @file
30 * @brief UI base structures
31 * @ingroup gateui_cpp
32 */
33
34 #ifndef GATE_UI_GATEUI_HPP_INCLUDED
35 #define GATE_UI_GATEUI_HPP_INCLUDED
36
37 #include "gate/ui/gate_ui_api.hpp"
38 #include "gate/ui/gateui.h"
39 #include "gate/arrays.hpp"
40 #include "gate/strings.hpp"
41 #include "gate/delegates.hpp"
42 #include "gate/wrappers.hpp"
43 #include "gate/graphics/colors.hpp"
44
45 namespace gate
46 {
47 namespace ui
48 {
49 /// @brief Control coordinate point (x,y)
50 struct GATE_UI_CPP_API Point : public gate_ui_point_t
51 {
52 30 inline Point(int32_t cx = 0, int32_t cy = 0)
53 30 {
54 30 this->x = cx;
55 30 this->y = cy;
56 30 }
57
58 3 inline Point(gate_ui_point_t const& src)
59 3 : gate_ui_point_t(src)
60 {
61 3 }
62
63 inline gate_ui_point_t* c_impl()
64 {
65 return this;
66 }
67
68 16 inline gate_ui_point_t const* c_impl() const
69 {
70 16 return this;
71 }
72
73 2 inline bool_t operator!() const
74 {
75
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
2 return (this->x == 0) && (this->y == 0);
76 }
77 };
78
79 /// @brief Control size dimensions (width, height)
80 struct GATE_UI_CPP_API Size : public gate_ui_size_t
81 {
82 9 inline Size(int32_t widthvalue = 0, int32_t heightvalue = 0)
83 9 {
84 9 this->width = widthvalue;
85 9 this->height = heightvalue;
86 9 }
87 3 inline Size(gate_ui_size_t const& src)
88 3 : gate_ui_size_t(src)
89 {
90 3 }
91 inline gate_ui_size_t* c_impl()
92 {
93 return this;
94 }
95 2 inline gate_ui_size_t const* c_impl() const
96 {
97 2 return this;
98 }
99 4 inline bool_t operator!() const
100 {
101
3/4
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
4 return (this->width == 0) && (this->height == 0);
102 }
103 };
104
105 /// @brief Control position (x,y,width,height)
106 struct GATE_UI_CPP_API Position : public gate_ui_position_t
107 {
108 26 inline Position(int32_t xvalue = 0, int32_t yvalue = 0, int32_t widthvalue = 0, int32_t heightvalue = 0)
109 26 {
110 26 this->pos.x = xvalue;
111 26 this->pos.y = yvalue;
112 26 this->size.width = widthvalue;
113 26 this->size.height = heightvalue;
114 26 }
115 2 inline Position(gate_ui_position_t const& pos)
116 2 : gate_ui_position_t(pos)
117 {
118 2 }
119
120 inline gate_ui_position_t* c_impl()
121 {
122 return this;
123 }
124 5 inline gate_ui_position_t const* c_impl() const
125 {
126 5 return this;
127 }
128
129 inline bool_t operator!() const
130 {
131 return (this->pos.x == 0) && (this->pos.y == 0)
132 && (this->size.width == 0) && (this->size.width == 0)
133 ;
134 }
135
136 inline int32_t getLeft() const
137 {
138 return this->pos.x;
139 }
140 inline int32_t getTop() const
141 {
142 return this->pos.y;
143 }
144 inline int32_t getRight() const
145 {
146 return this->pos.x + this->size.width;
147 }
148 inline int32_t getBottom() const
149 {
150 return this->pos.x + this->size.height;
151 }
152 2 inline int32_t getWidth() const
153 {
154 2 return this->size.width;
155 }
156 2 inline int32_t getHeight() const
157 {
158 2 return this->size.height;
159 }
160 inline int32_t getCenterX() const
161 {
162 return this->pos.x + this->size.width / 2;
163 }
164 inline int32_t getCenterY() const
165 {
166 return this->pos.y + this->size.height / 2;
167 }
168 };
169
170 /// @brief UI color
171 typedef gate::graph::Color Color;
172
173 /// @brief UI font info wrapper
174 struct GATE_UI_CPP_API Font : public gate_ui_font_t
175 {
176 2 inline Font()
177 2 {
178 2 gate_ui_font_t* ptrfnt = this;
179 2 gate_mem_clear(ptrfnt, sizeof(gate_ui_font_t));
180 2 }
181 inline Font(Font const& src)
182 {
183 gate_mem_copy(this, &src, sizeof(src));
184 }
185 inline Font& operator=(Font const& src)
186 {
187 gate_mem_move(this, &src, sizeof(src));
188 return *this;
189 }
190
191 inline gate_ui_font_t* c_impl()
192 {
193 return this;
194 }
195 6 inline gate_ui_font_t const* c_impl() const
196 {
197 6 return this;
198 }
199
200 inline void setName(String const& name)
201 {
202 size_t len = name.length();
203 if (len >= sizeof(this->font_name))
204 {
205 len = sizeof(this->font_name) - 1;
206 }
207 gate_mem_copy(&this->font_name[0], name.c_str(), len);
208 this->font_name[len] = 0;
209 }
210
211 2 inline void setColor(Color const& col)
212 {
213 2 gate_mem_copy(&this->color, col.c_impl(), sizeof(this->color));
214 2 }
215 };
216
217 /// @brief User input event IDs
218 struct Input
219 {
220 static uint32_t const Key_Alt;
221 static uint32_t const Key_Ctrl;
222 static uint32_t const Key_Shift;
223
224 static uint32_t const Mouse_Left;
225 static uint32_t const Mouse_Right;
226 static uint32_t const Mouse_Middle;
227
228 };
229
230 class Control;
231
232 /// @brief UI host interface
233 class GATE_UI_CPP_API Host : public IRunnable, public IQuitable, private NonCopyable
234 {
235 public:
236 Host(); // empty host, no initialization
237 Host(uintptr_t appHandle); // create new host
238 virtual ~Host() noexcept;
239
240 void init(uintptr_t appHandle);
241
242 virtual void run();
243 virtual void quit();
244
245 static uint32_t const Color_Background;
246 static uint32_t const Color_Workspace;
247 static uint32_t const Color_Window;
248 static uint32_t const Color_WindowText;
249 static uint32_t const Color_WindowTextDisabled;
250 static uint32_t const Color_Control;
251 static uint32_t const Color_ControlText;
252 static uint32_t const Color_ControlTextDisabled;
253 static uint32_t const Color_ControlBorderLight;
254 static uint32_t const Color_ControlBorderHiLight;
255 static uint32_t const Color_ControlBorderShadow;
256 static uint32_t const Color_ControlBorderDarkShadow;
257 static uint32_t const Color_MenuBackground;
258 static uint32_t const Color_MenuText;
259 static uint32_t const Color_SelectedMenuBackground;
260 static uint32_t const Color_SelectedMenuText;
261
262 static uint32_t const FontType_Standard;
263 static uint32_t const FontType_SansSerif;
264 static uint32_t const FontType_Serif;
265 static uint32_t const FontType_Monospace;
266
267 uint32_t getLineHeight();
268 uint32_t getControlHeight(uint32_t lines = 1);
269 int32_t getUnitLength(real32_t units = 1.0f);
270 Color getDefaultColor(uint32_t colortype);
271 Font getDefaultFont(uint32_t fonttype);
272 int32_t getPixelsOfPoints(real32_t points);
273 real32_t getPointsOfPixels(int32_t pixels);
274 Array<String> getFonts();
275
276 String getClipboardText();
277 bool getClipboardText(String& text);
278 void setClipboardText(String const& text);
279
280 Position getDefaultWorkarea();
281 Position getTotalWorkarea();
282
283
284 gate_ui_host_t* operator*();
285 gate_ui_host_t const* operator*() const;
286 gate_ui_host_t* c_impl();
287 gate_ui_host_t const* c_impl() const;
288
289 static Host& getHost(Control& ctrl);
290 static Host& getHost(gate_ui_host_t& hostImpl);
291
292 private:
293 gate_ui_host_t* ptr;
294 gate_ui_host_t impl;
295 };
296
297 /// @brief UI event argument base class
298 struct GATE_UI_CPP_API EventArg
299 {
300
301 };
302
303 /// @brief Generic UI point event argument
304 struct GATE_UI_CPP_API PointArg : EventArg, Point
305 {
306 inline PointArg(int32_t x = 0, int32_t y = 0)
307 : Point(x, y)
308 {
309 }
310 };
311
312 /// @brief Generic UI keyboard event argument
313 struct GATE_UI_CPP_API KeyCharArg : EventArg
314 {
315 char_32_t Char;
316
317 inline KeyCharArg(char_32_t chr = 0)
318 : Char(chr)
319 {
320 }
321 };
322
323 /// @brief Generic UI size event argument
324 struct GATE_UI_CPP_API SizeArg : EventArg, Size
325 {
326 inline SizeArg(int32_t width = 0, int32_t height = 0)
327 : Size(width, height)
328 {
329 }
330 };
331
332 /// @brief Generic UI position event argument
333 struct GATE_UI_CPP_API PositionArg : EventArg, Position
334 {
335 inline PositionArg(int32_t x = 0, int32_t y = 0, int32_t width = 0, int32_t height = 0)
336 : Position(x, y, width, height)
337 {
338 }
339 };
340
341 /// @brief UI event host template
342 /// @tparam CTRL sender control type
343 /// @tparam ARG argument type
344 template<class CTRL, class ARG>
345 class Event : protected MulticastDelegate2<CTRL*, ARG*>
346 {
347 public:
348 typedef MulticastDelegate2<CTRL*, ARG*> base_t;
349 typedef Event<CTRL, ARG> self_t;
350 typedef Delegate2<CTRL*, ARG*> delegate_t;
351
352 14 void invoke(CTRL* sender, ARG* arg)
353 {
354 14 base_t::invoke(sender, arg);
355 14 }
356
357 14 void operator()(CTRL* sender, ARG* arg)
358 {
359 14 this->invoke(sender, arg);
360 14 }
361
362 void clear()
363 {
364 base_t::clear();
365 }
366 20 void add(delegate_t const& del)
367 {
368 20 base_t::operator+=(del);
369 20 }
370 10 self_t& operator+=(delegate_t const& del)
371 {
372 10 this->add(del);
373 10 return *this;
374 }
375 };
376
377
378 /// @brief UI control base class
379 class GATE_UI_CPP_API Control : private NonCopyable
380 {
381 public:
382
383 public:
384 virtual ~Control() noexcept;
385
386 bool isCreated() const noexcept;
387 bool isEnabled() const;
388 bool isFocused() const;
389 bool isVisible() const;
390 Position getPosition() const;
391 Size getSize() const;
392 uint32_t getTextLength() const;
393 String getText() const;
394 int32_t getState() const;
395
396 void setEnabled(bool enabled);
397 void setVisible(bool visible);
398 void setPosition(Point const* position, Size const* size);
399 void setPosition(Position const& pos);
400 void setFocus();
401 void setText(String const& text);
402 void setState(int32_t state);
403
404 void destroy() noexcept;
405
406 static uint32_t const Flag_Enabled;
407 static uint32_t const Flag_Visible;
408 static uint32_t const Flag_Resizable;
409
410 static enumint_t const Action_Activate;
411 static enumint_t const Action_ContextMenu;
412
413 gate_ui_ctrl_t* c_impl() const; // returns the native c implementation structure
414 gate_ui_ctrl_t* operator*() const; // returns the native c implementation structure only if the control is created
415
416 protected:
417 Control(gate_ui_ctrl_t* ctrlimpl);
418
419 void attachNativeControl(gate_ui_ctrl_t* ctl);
420 void failIfCreated(char const* sourceFunction = NULL);
421 void failIfNotCreated(char const* sourceFunction = NULL);
422
423 private:
424 gate_ui_ctrl_t* ctrl_impl; // hardlink to native c implementation structure
425 gate_ui_ctrl_t* ctrl; // link to native c implementation structure if control is created
426 };
427
428 typedef Ref<Control> ControlRef;
429
430
431 /// @brief UI grid layout definition
432 struct GATE_UI_CPP_API Layout : public gate_ui_layout_t, private NonCopyable
433 {
434 public:
435 static enumint_t const Type_Auto;
436 static enumint_t const Type_Pixel;
437 static enumint_t const Type_Percent;
438 static enumint_t const Type_UnitScale;
439
440 Layout();
441 ~Layout();
442
443 void clear();
444 uint32_t addColumn(enumint_t type = Type_Auto, real32_t width = 0.0f);
445 uint32_t addRow(enumint_t type = Type_Auto, real32_t height = 0.0f);
446 void setColumn(uint32_t col, enumint_t type, real32_t value);
447 void setRow(uint32_t row, enumint_t type, real32_t value);
448
449 void setBorder(uint32_t borderWidth);
450 void setColumnSpace(uint32_t colSpace);
451 void setRowSpace(uint32_t rowSpace);
452
453 void setControl(Control const& ctrl, uint32_t row, uint32_t col, uint32_t rowspan = 1, uint32_t colspan = 1);
454 bool_t findControl(Control const& ctrl, uint32_t& row, uint32_t& col);
455 bool_t changeControlRowHeight(Control const& ctrl, enumint_t type, real32_t height);
456 bool_t changeControlColWidth(Control const& ctrl, enumint_t type, real32_t width);
457
458 void apply(uint32_t unitLength, Position const& pose);
459 };
460
461
462 /// @brief UI control container base class
463 class GATE_UI_CPP_API ControlContainer : public Control
464 {
465 public:
466 virtual ~ControlContainer() noexcept;
467
468 virtual Array<Control*> getChildren() const;
469 virtual void setLayout(Layout const* layout);
470
471 protected:
472 ControlContainer(gate_ui_ctrl_t* ctrlimpl);
473
474 };
475
476
477 /// @brief control manager interface
478 class GATE_UI_CPP_API IControlManager
479 {
480 public:
481 virtual ~IControlManager() noexcept;
482
483 virtual Host& getHost() = 0;
484 virtual void add(ControlRef ctrl) = 0;
485 virtual void remove(Control& ctrl) = 0;
486 };
487
488 /// @brief Host control manager implementation
489 class GATE_UI_CPP_API HostControlManager : public IControlManager, private NonCopyable
490 {
491 public: // inherited from IControlManager
492 virtual ~HostControlManager() noexcept;
493
494 virtual Host& getHost();
495 virtual void add(ControlRef ctrl);
496 virtual void remove(Control& ctrl);
497
498 public:
499 typedef ArrayList<ControlRef> ControlList;
500 HostControlManager();
501
502 void createHost(uintptr_t hostHandle);
503 void cleanupControls();
504 void destroyHost();
505
506 private:
507 Optional<Host> uihost;
508 ControlList activeControls;
509 ControlList inactiveControls;
510 };
511
512
513 } // end of namespace ui
514
515 } // end of namespace gate
516
517 #endif
518