GCC Code Coverage Report


Directory: src/gate/
File: src/gate/ui/gateui.hpp
Date: 2026-09-21 06:26:40
Exec Total Coverage
Lines: 65 74 87.8%
Functions: 42 83 50.6%
Branches: 8 16 50.0%

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