19#include <winpr/assert.h>
20#include <winpr/cast.h>
22#include <rdtk/config.h>
26#include "rdtk_button.h"
28int 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 const int rc1 = rdtk_font_text_draw_size(font, &textWidth, &textHeight, text);
46 const int rc2 = rdtk_nine_patch_draw(surface, nXDst, nYDst, nWidth, nHeight, ninePatch);
50 if ((textWidth > 0) && (textHeight > 0))
52 const int wd = (ninePatch->width - ninePatch->fillWidth);
53 const int hd = (ninePatch->height - ninePatch->fillHeight);
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);
60 if (textWidth < fillWidth)
62 const int twd = ((fillWidth - textWidth) / 2) + ninePatch->fillLeft;
63 offsetX = WINPR_ASSERTING_INT_CAST(uint16_t, twd);
65 else if (textWidth < ninePatch->width)
67 const int twd = ((ninePatch->width - textWidth) / 2);
68 offsetX = WINPR_ASSERTING_INT_CAST(uint16_t, twd);
71 if (textHeight < fillHeight)
73 const int twd = ((fillHeight - textHeight) / 2) + ninePatch->fillTop;
74 offsetY = WINPR_ASSERTING_INT_CAST(uint16_t, twd);
76 else if (textHeight < ninePatch->height)
78 const int twd = ((ninePatch->height - textHeight) / 2);
79 offsetY = WINPR_ASSERTING_INT_CAST(uint16_t, twd);
82 return rdtk_font_draw_text(surface, nXDst + offsetX, nYDst + offsetY, font, text);
88rdtkButton* rdtk_button_new(rdtkEngine* engine, rdtkNinePatch* ninePatch)
91 WINPR_ASSERT(ninePatch);
93 rdtkButton* button = (rdtkButton*)calloc(1,
sizeof(rdtkButton));
98 button->engine = engine;
99 button->ninePatch = ninePatch;
104void rdtk_button_free(rdtkButton* button)
109int rdtk_button_engine_init(rdtkEngine* engine)
111 WINPR_ASSERT(engine);
115 engine->button = rdtk_button_new(engine, engine->button9patch);
123int rdtk_button_engine_uninit(rdtkEngine* engine)
125 WINPR_ASSERT(engine);
129 rdtk_button_free(engine->button);
130 engine->button = NULL;