19 #include <winpr/assert.h>
20 #include <winpr/cast.h>
22 #include <rdtk/config.h>
24 #include "rdtk_font.h"
26 #include "rdtk_button.h"
28 int rdtk_button_draw(rdtkSurface* surface, uint16_t nXDst, uint16_t nYDst, uint16_t nWidth,
29 uint16_t nHeight, rdtkButton* button,
const char* text)
31 uint16_t textWidth = 0;
32 uint16_t textHeight = 0;
34 WINPR_ASSERT(surface);
38 rdtkEngine* engine = surface->engine;
39 rdtkFont* font = engine->font;
40 rdtkNinePatch* ninePatch = button->ninePatch;
42 rdtk_font_text_draw_size(font, &textWidth, &textHeight, text);
44 rdtk_nine_patch_draw(surface, nXDst, nYDst, nWidth, nHeight, ninePatch);
46 if ((textWidth > 0) && (textHeight > 0))
48 const int wd = (ninePatch->width - ninePatch->fillWidth);
49 const int hd = (ninePatch->height - ninePatch->fillHeight);
51 const uint16_t fillWidth = nWidth - WINPR_ASSERTING_INT_CAST(uint16_t, wd);
52 const uint16_t fillHeight = nHeight - WINPR_ASSERTING_INT_CAST(uint16_t, hd);
53 uint16_t offsetX = WINPR_ASSERTING_INT_CAST(UINT16, ninePatch->fillLeft);
54 uint16_t offsetY = WINPR_ASSERTING_INT_CAST(UINT16, ninePatch->fillTop);
56 if (textWidth < fillWidth)
58 const int twd = ((fillWidth - textWidth) / 2) + ninePatch->fillLeft;
59 offsetX = WINPR_ASSERTING_INT_CAST(uint16_t, twd);
61 else if (textWidth < ninePatch->width)
63 const int twd = ((ninePatch->width - textWidth) / 2);
64 offsetX = WINPR_ASSERTING_INT_CAST(uint16_t, twd);
67 if (textHeight < fillHeight)
69 const int twd = ((fillHeight - textHeight) / 2) + ninePatch->fillTop;
70 offsetY = WINPR_ASSERTING_INT_CAST(uint16_t, twd);
72 else if (textHeight < ninePatch->height)
74 const int twd = ((ninePatch->height - textHeight) / 2);
75 offsetY = WINPR_ASSERTING_INT_CAST(uint16_t, twd);
78 rdtk_font_draw_text(surface, nXDst + offsetX, nYDst + offsetY, font, text);
84 rdtkButton* rdtk_button_new(rdtkEngine* engine, rdtkNinePatch* ninePatch)
87 WINPR_ASSERT(ninePatch);
89 rdtkButton* button = (rdtkButton*)calloc(1,
sizeof(rdtkButton));
94 button->engine = engine;
95 button->ninePatch = ninePatch;
100 void rdtk_button_free(rdtkButton* button)
105 int rdtk_button_engine_init(rdtkEngine* engine)
107 WINPR_ASSERT(engine);
111 engine->button = rdtk_button_new(engine, engine->button9patch);
119 int rdtk_button_engine_uninit(rdtkEngine* engine)
121 WINPR_ASSERT(engine);
125 rdtk_button_free(engine->button);
126 engine->button = NULL;