19 #include <winpr/assert.h>
20 #include <winpr/cast.h>
22 #include <rdtk/config.h>
24 #include "rdtk_font.h"
26 #include "rdtk_text_field.h"
28 int 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 rdtk_font_text_draw_size(font, &textWidth, &textHeight, text);
45 rdtk_nine_patch_draw(surface, nXDst, nYDst, nWidth, nHeight, ninePatch);
47 if ((textWidth > 0) && (textHeight > 0))
49 const int fwd = (ninePatch->width - ninePatch->fillWidth);
50 const int fhd = (ninePatch->height - ninePatch->fillHeight);
52 uint16_t fillWidth = nWidth - WINPR_ASSERTING_INT_CAST(uint16_t, fwd);
53 uint16_t fillHeight = nHeight - WINPR_ASSERTING_INT_CAST(uint16_t, fhd);
54 uint16_t offsetX = WINPR_ASSERTING_INT_CAST(uint16_t, ninePatch->fillLeft);
55 uint16_t offsetY = WINPR_ASSERTING_INT_CAST(uint16_t, ninePatch->fillTop);
57 if (textWidth < fillWidth)
59 const int wd = ((fillWidth - textWidth) / 2) + ninePatch->fillLeft;
60 offsetX = WINPR_ASSERTING_INT_CAST(uint16_t, wd);
62 else if (textWidth < ninePatch->width)
64 const int wd = ((ninePatch->width - textWidth) / 2);
65 offsetX = WINPR_ASSERTING_INT_CAST(uint16_t, wd);
68 if (textHeight < fillHeight)
70 const int wd = ((fillHeight - textHeight) / 2) + ninePatch->fillTop;
71 offsetY = WINPR_ASSERTING_INT_CAST(uint16_t, wd);
73 else if (textHeight < ninePatch->height)
75 const int wd = ((ninePatch->height - textHeight) / 2);
76 offsetY = WINPR_ASSERTING_INT_CAST(uint16_t, wd);
79 rdtk_font_draw_text(surface, nXDst + offsetX, nYDst + offsetY, font, text);
85 rdtkTextField* rdtk_text_field_new(rdtkEngine* engine, rdtkNinePatch* ninePatch)
88 WINPR_ASSERT(ninePatch);
90 rdtkTextField* textField = (rdtkTextField*)calloc(1,
sizeof(rdtkTextField));
95 textField->engine = engine;
96 textField->ninePatch = ninePatch;
101 void rdtk_text_field_free(rdtkTextField* textField)
106 int rdtk_text_field_engine_init(rdtkEngine* engine)
108 WINPR_ASSERT(engine);
110 if (!engine->textField)
112 engine->textField = rdtk_text_field_new(engine, engine->textField9patch);
113 if (!engine->textField)
120 int rdtk_text_field_engine_uninit(rdtkEngine* engine)
122 WINPR_ASSERT(engine);
123 if (engine->textField)
125 rdtk_text_field_free(engine->textField);
126 engine->textField = NULL;