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