FreeRDP
Loading...
Searching...
No Matches
rdtk_button.c
1
19#include <winpr/assert.h>
20#include <winpr/cast.h>
21
22#include <rdtk/config.h>
23
24#include "rdtk_font.h"
25
26#include "rdtk_button.h"
27
28int rdtk_button_draw(rdtkSurface* surface, uint16_t nXDst, uint16_t nYDst, uint16_t nWidth,
29 uint16_t nHeight, rdtkButton* button, const char* text)
30{
31 uint16_t textWidth = 0;
32 uint16_t textHeight = 0;
33
34 WINPR_ASSERT(surface);
35 WINPR_ASSERT(button);
36 WINPR_ASSERT(text);
37
38 rdtkEngine* engine = surface->engine;
39 rdtkFont* font = engine->font;
40 rdtkNinePatch* ninePatch = button->ninePatch;
41
42 const int rc1 = rdtk_font_text_draw_size(font, &textWidth, &textHeight, text);
43 if (rc1 < 0)
44 return rc1;
45
46 const int rc2 = rdtk_nine_patch_draw(surface, nXDst, nYDst, nWidth, nHeight, ninePatch);
47 if (rc2 < 0)
48 return rc2;
49
50 if ((textWidth > 0) && (textHeight > 0))
51 {
52 const int wd = (ninePatch->width - ninePatch->fillWidth);
53 const int hd = (ninePatch->height - ninePatch->fillHeight);
54
55 const uint16_t fillWidth = nWidth - WINPR_ASSERTING_INT_CAST(uint16_t, wd);
56 const uint16_t fillHeight = nHeight - WINPR_ASSERTING_INT_CAST(uint16_t, hd);
57 uint16_t offsetX = WINPR_ASSERTING_INT_CAST(UINT16, ninePatch->fillLeft);
58 uint16_t offsetY = WINPR_ASSERTING_INT_CAST(UINT16, ninePatch->fillTop);
59
60 if (textWidth < fillWidth)
61 {
62 const int twd = ((fillWidth - textWidth) / 2) + ninePatch->fillLeft;
63 offsetX = WINPR_ASSERTING_INT_CAST(uint16_t, twd);
64 }
65 else if (textWidth < ninePatch->width)
66 {
67 const int twd = ((ninePatch->width - textWidth) / 2);
68 offsetX = WINPR_ASSERTING_INT_CAST(uint16_t, twd);
69 }
70
71 if (textHeight < fillHeight)
72 {
73 const int twd = ((fillHeight - textHeight) / 2) + ninePatch->fillTop;
74 offsetY = WINPR_ASSERTING_INT_CAST(uint16_t, twd);
75 }
76 else if (textHeight < ninePatch->height)
77 {
78 const int twd = ((ninePatch->height - textHeight) / 2);
79 offsetY = WINPR_ASSERTING_INT_CAST(uint16_t, twd);
80 }
81
82 return rdtk_font_draw_text(surface, nXDst + offsetX, nYDst + offsetY, font, text);
83 }
84
85 return 1;
86}
87
88rdtkButton* rdtk_button_new(rdtkEngine* engine, rdtkNinePatch* ninePatch)
89{
90 WINPR_ASSERT(engine);
91 WINPR_ASSERT(ninePatch);
92
93 rdtkButton* button = (rdtkButton*)calloc(1, sizeof(rdtkButton));
94
95 if (!button)
96 return NULL;
97
98 button->engine = engine;
99 button->ninePatch = ninePatch;
100
101 return button;
102}
103
104void rdtk_button_free(rdtkButton* button)
105{
106 free(button);
107}
108
109int rdtk_button_engine_init(rdtkEngine* engine)
110{
111 WINPR_ASSERT(engine);
112
113 if (!engine->button)
114 {
115 engine->button = rdtk_button_new(engine, engine->button9patch);
116 if (!engine->button)
117 return -1;
118 }
119
120 return 1;
121}
122
123int rdtk_button_engine_uninit(rdtkEngine* engine)
124{
125 WINPR_ASSERT(engine);
126
127 if (engine->button)
128 {
129 rdtk_button_free(engine->button);
130 engine->button = NULL;
131 }
132
133 return 1;
134}