FreeRDP
SDL2/dialogs/sdl_widget.hpp
1 
20 #pragma once
21 
22 #include <string>
23 
24 #include <vector>
25 #include <SDL.h>
26 #include <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, SDL_Rect rect, bool input);
52 #if defined(WITH_SDL_IMAGE_DIALOGS)
53  SdlWidget(SDL_Renderer* renderer, SDL_Rect rect, SDL_RWops* ops);
54 #endif
55  SdlWidget(SdlWidget&& other) noexcept;
56  virtual ~SdlWidget();
57 
58  bool fill(SDL_Renderer* renderer, SDL_Color color);
59  bool fill(SDL_Renderer* renderer, const std::vector<SDL_Color>& colors);
60  bool update_text(SDL_Renderer* renderer, const std::string& text, SDL_Color fgcolor);
61  bool update_text(SDL_Renderer* renderer, const std::string& text, SDL_Color fgcolor,
62  SDL_Color bgcolor);
63 
64  [[nodiscard]] bool wrap() const;
65  bool set_wrap(bool wrap = true, size_t width = 0);
66  [[nodiscard]] const SDL_Rect& rect() const;
67 
68 #define widget_log_error(res, what) SdlWidget::error_ex(res, what, __FILE__, __LINE__, __func__)
69  static bool error_ex(Uint32 res, const char* what, const char* file, size_t line,
70  const char* fkt);
71 
72  SdlWidget(const SdlWidget& other) = delete;
73  SdlWidget& operator=(const SdlWidget& other) = delete;
74  SdlWidget& operator=(SdlWidget&& other) = delete;
75 
76  private:
77  SDL_Texture* render_text(SDL_Renderer* renderer, const std::string& text, SDL_Color fgcolor,
78  SDL_Rect& src, SDL_Rect& dst);
79  SDL_Texture* render_text_wrapped(SDL_Renderer* renderer, const std::string& text,
80  SDL_Color fgcolor, SDL_Rect& src, SDL_Rect& dst);
81 
82  TTF_Font* _font = nullptr;
83  SDL_Texture* _image = nullptr;
84  SDL_Rect _rect;
85  bool _input = false;
86  bool _wrap = false;
87  size_t _text_width = 0;
88 };
89 
90 bool clear_window(SDL_Renderer* renderer);