GCC Code Coverage Report


Directory: src/gate/
File: src/gate/ui/gateui.hpp
Date: 2025-09-14 13:10:38
Exec Total Coverage
Lines: 0 59 0.0%
Functions: 0 70 0.0%
Branches: 0 12 0.0%

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