19#include <winpr/assert.h>
20#include <winpr/cast.h>
22#include <rdtk/config.h>
26#include "rdtk_text_field.h"
28int rdtk_text_field_draw(rdtkSurface* surface, uint16_t nXDst, uint16_t nYDst, uint16_t nWidth,
29 uint16_t nHeight, rdtkTextField* textField,
const char* text)
31 uint16_t textWidth = 0;
32 uint16_t textHeight = 0;
34 WINPR_ASSERT(surface);
35 WINPR_ASSERT(textField);
38 rdtkEngine* engine = surface->engine;
39 rdtkFont* font = engine->font;
40 textField = surface->engine->textField;
41 rdtkNinePatch* ninePatch = textField->ninePatch;
43 const int rc = 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 fwd = (ninePatch->width - ninePatch->fillWidth);
53 const int fhd = (ninePatch->height - ninePatch->fillHeight);
55 uint16_t fillWidth = nWidth - WINPR_ASSERTING_INT_CAST(uint16_t, fwd);
56 uint16_t fillHeight = nHeight - WINPR_ASSERTING_INT_CAST(uint16_t, fhd);
57 uint16_t offsetX = WINPR_ASSERTING_INT_CAST(uint16_t, ninePatch->fillLeft);
58 uint16_t offsetY = WINPR_ASSERTING_INT_CAST(uint16_t, ninePatch->fillTop);
60 if (textWidth < fillWidth)
62 const int wd = ((fillWidth - textWidth) / 2) + ninePatch->fillLeft;
63 offsetX = WINPR_ASSERTING_INT_CAST(uint16_t, wd);
65 else if (textWidth < ninePatch->width)
67 const int wd = ((ninePatch->width - textWidth) / 2);
68 offsetX = WINPR_ASSERTING_INT_CAST(uint16_t, wd);
71 if (textHeight < fillHeight)
73 const int wd = ((fillHeight - textHeight) / 2) + ninePatch->fillTop;
74 offsetY = WINPR_ASSERTING_INT_CAST(uint16_t, wd);
76 else if (textHeight < ninePatch->height)
78 const int wd = ((ninePatch->height - textHeight) / 2);
79 offsetY = WINPR_ASSERTING_INT_CAST(uint16_t, wd);
82 return rdtk_font_draw_text(surface, nXDst + offsetX, nYDst + offsetY, font, text);
88rdtkTextField* rdtk_text_field_new(rdtkEngine* engine, rdtkNinePatch* ninePatch)
91 WINPR_ASSERT(ninePatch);
93 rdtkTextField* textField = (rdtkTextField*)calloc(1,
sizeof(rdtkTextField));
98 textField->engine = engine;
99 textField->ninePatch = ninePatch;
104void rdtk_text_field_free(rdtkTextField* textField)
109int rdtk_text_field_engine_init(rdtkEngine* engine)
111 WINPR_ASSERT(engine);
113 if (!engine->textField)
115 engine->textField = rdtk_text_field_new(engine, engine->textField9patch);
116 if (!engine->textField)
123int rdtk_text_field_engine_uninit(rdtkEngine* engine)
125 WINPR_ASSERT(engine);
126 if (engine->textField)
128 rdtk_text_field_free(engine->textField);
129 engine->textField = NULL;