summaryrefslogtreecommitdiffstats
path: root/client/SDL/dialogs/sdl_widget.hpp
blob: ebc7dbd4915a0e08dda49be2e1a3aae7b4dff08b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
/**
 * FreeRDP: A Remote Desktop Protocol Implementation
 * SDL Client helper dialogs
 *
 * Copyright 2023 Armin Novak <armin.novak@thincast.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#pragma once

#include <string>

#include <vector>
#include <SDL.h>
#include <SDL_ttf.h>

#if defined(_MSC_VER)
#include <BaseTsd.h>
typedef SSIZE_T ssize_t;
#endif

#if !defined(HAS_NOEXCEPT)
#if defined(__clang__)
#if __has_feature(cxx_noexcept)
#define HAS_NOEXCEPT
#endif
#elif defined(__GXX_EXPERIMENTAL_CXX0X__) && __GNUC__ * 10 + __GNUC_MINOR__ >= 46 || \
    defined(_MSC_FULL_VER) && _MSC_FULL_VER >= 190023026
#define HAS_NOEXCEPT
#endif
#endif

#ifndef HAS_NOEXCEPT
#define noexcept
#endif

class SdlWidget
{
  public:
	SdlWidget(SDL_Renderer* renderer, const SDL_Rect& rect, bool input);
	SdlWidget(SDL_Renderer* renderer, const SDL_Rect& rect, SDL_RWops* ops);
	SdlWidget(SdlWidget&& other) noexcept;
	virtual ~SdlWidget();

	bool fill(SDL_Renderer* renderer, SDL_Color color);
	bool fill(SDL_Renderer* renderer, const std::vector<SDL_Color>& colors);
	bool update_text(SDL_Renderer* renderer, const std::string& text, SDL_Color fgcolor);
	bool update_text(SDL_Renderer* renderer, const std::string& text, SDL_Color fgcolor,
	                 SDL_Color bgcolor);

	bool wrap() const;
	bool set_wrap(bool wrap = true, size_t width = 0);
	const SDL_Rect& rect() const;

  public:
#define widget_log_error(res, what) SdlWidget::error_ex(res, what, __FILE__, __LINE__, __func__)
	static bool error_ex(Uint32 res, const char* what, const char* file, size_t line,
	                     const char* fkt);

  private:
	SdlWidget(const SdlWidget& other) = delete;

	SDL_Texture* render_text(SDL_Renderer* renderer, const std::string& text, SDL_Color fgcolor,
	                         SDL_Rect& src, SDL_Rect& dst);
	SDL_Texture* render_text_wrapped(SDL_Renderer* renderer, const std::string& text,
	                                 SDL_Color fgcolor, SDL_Rect& src, SDL_Rect& dst);

  private:
	TTF_Font* _font = nullptr;
	SDL_Texture* _image = nullptr;
	SDL_Rect _rect;
	bool _input = false;
	bool _wrap = false;
	size_t _text_width = 0;
};

bool clear_window(SDL_Renderer* renderer);