FreeRDP
SDL3/dialogs/sdl_widget.hpp
1 
20 #pragma once
21 
22 #include <string>
23 
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>
30 typedef 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 
48 class SdlWidget
49 {
50  public:
51  SdlWidget(SDL_Renderer* renderer, const SDL_FRect& rect, bool input);
52  SdlWidget(SDL_Renderer* renderer, const SDL_FRect& rect, SDL_IOStream* ops);
53  SdlWidget(const SdlWidget& other) = delete;
54  SdlWidget(SdlWidget&& other) noexcept;
55  virtual ~SdlWidget();
56 
57  SdlWidget& operator=(const SdlWidget& other) = delete;
58  SdlWidget& operator=(SdlWidget&& other) = delete;
59 
60  bool fill(SDL_Renderer* renderer, SDL_Color color);
61  bool fill(SDL_Renderer* renderer, const std::vector<SDL_Color>& colors);
62  bool update_text(SDL_Renderer* renderer, const std::string& text, SDL_Color fgcolor);
63  bool update_text(SDL_Renderer* renderer, const std::string& text, SDL_Color fgcolor,
64  SDL_Color bgcolor);
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 #define widget_log_error(res, what) SdlWidget::error_ex(res, what, __FILE__, __LINE__, __func__)
71  static bool error_ex(Uint32 res, const char* what, const char* file, size_t line,
72  const char* fkt);
73 
74  private:
75  SDL_Texture* render_text(SDL_Renderer* renderer, const std::string& text, SDL_Color fgcolor,
76  SDL_FRect& src, SDL_FRect& dst);
77  SDL_Texture* render_text_wrapped(SDL_Renderer* renderer, const std::string& text,
78  SDL_Color fgcolor, SDL_FRect& src, SDL_FRect& dst);
79 
80  TTF_Font* _font = nullptr;
81  SDL_Texture* _image = nullptr;
82  SDL_FRect _rect;
83  bool _input = false;
84  bool _wrap = false;
85  size_t _text_width = 0;
86 };
87 
88 bool clear_window(SDL_Renderer* renderer);