FreeRDP
Loading...
Searching...
No Matches
SDL3/dialogs/sdl_widget.hpp
1
20#pragma once
21
22#include <string>
23#include <memory>
24#include <vector>
25#include <SDL3/SDL.h>
26#include <SDL3_ttf/SDL_ttf.h>
27
28#if defined(_MSC_VER)
29#include <BaseTsd.h>
30typedef SSIZE_T ssize_t;
31#endif
32
33#if !defined(HAS_NOEXCEPT)
34#if defined(__clang__)
35#if __has_feature(cxx_noexcept)
36#define HAS_NOEXCEPT
37#endif
38#elif defined(__GXX_EXPERIMENTAL_CXX0X__) && __GNUC__ * 10 + __GNUC_MINOR__ >= 46 || \
39 defined(_MSC_FULL_VER) && _MSC_FULL_VER >= 190023026
40#define HAS_NOEXCEPT
41#endif
42#endif
43
44#ifndef HAS_NOEXCEPT
45#define noexcept
46#endif
47
48class SdlWidget
49{
50 public:
51 SdlWidget(std::shared_ptr<SDL_Renderer>& renderer, const SDL_FRect& rect);
52#if defined(WITH_SDL_IMAGE_DIALOGS)
53 SdlWidget(std::shared_ptr<SDL_Renderer>& renderer, const SDL_FRect& rect, SDL_IOStream* ops);
54#endif
55 SdlWidget(const SdlWidget& other) = delete;
56 SdlWidget(SdlWidget&& other) noexcept;
57 virtual ~SdlWidget();
58
59 SdlWidget& operator=(const SdlWidget& other) = delete;
60 SdlWidget& operator=(SdlWidget&& other) = delete;
61
62 bool fill(SDL_Color color) const;
63 bool fill(const std::vector<SDL_Color>& colors) const;
64 bool update_text(const std::string& text);
65
66 [[nodiscard]] bool wrap() const;
67 bool set_wrap(bool wrap = true, size_t width = 0);
68 [[nodiscard]] const SDL_FRect& rect() const;
69
70 bool update();
71
72#define widget_log_error(res, what) SdlWidget::error_ex(res, what, __FILE__, __LINE__, __func__)
73 static bool error_ex(bool success, const char* what, const char* file, size_t line,
74 const char* fkt);
75
76 protected:
77 std::shared_ptr<SDL_Renderer> _renderer{};
78 SDL_Color _backgroundcolor = { 0x56, 0x56, 0x56, 0xff };
79 SDL_Color _fontcolor = { 0xd1, 0xcf, 0xcd, 0xff };
80 mutable std::string _text;
81
82 virtual bool clear() const;
83 virtual bool updateInternal();
84
85 private:
86 bool draw_rect(const SDL_FRect& rect, SDL_Color color) const;
87 std::shared_ptr<SDL_Texture> render_text(const std::string& text, SDL_Color fgcolor,
88 SDL_FRect& src, SDL_FRect& dst) const;
89 std::shared_ptr<SDL_Texture> render_text_wrapped(const std::string& text, SDL_Color fgcolor,
90 SDL_FRect& src, SDL_FRect& dst) const;
91
92 std::shared_ptr<TTF_Font> _font = nullptr;
93 std::shared_ptr<SDL_Texture> _image = nullptr;
94 std::shared_ptr<TTF_TextEngine> _engine = nullptr;
95 SDL_FRect _rect = {};
96 bool _wrap = false;
97 size_t _text_width = 0;
98};