FreeRDP
Loading...
Searching...
No Matches
rdtk_text_field.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_text_field.h"
27
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)
30{
31 uint16_t textWidth = 0;
32 uint16_t textHeight = 0;
33
34 WINPR_ASSERT(surface);
35 WINPR_ASSERT(textField);
36 WINPR_ASSERT(text);
37
38 rdtkEngine* engine = surface->engine;
39 rdtkFont* font = engine->font;
40 textField = surface->engine->textField;
41 rdtkNinePatch* ninePatch = textField->ninePatch;
42
43 const int rc = rdtk_font_text_draw_size(font, &textWidth, &textHeight, text);
44 if (rc < 0)
45 return rc;
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 fwd = (ninePatch->width - ninePatch->fillWidth);
53 const int fhd = (ninePatch->height - ninePatch->fillHeight);
54
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);
59
60 if (textWidth < fillWidth)
61 {
62 const int wd = ((fillWidth - textWidth) / 2) + ninePatch->fillLeft;
63 offsetX = WINPR_ASSERTING_INT_CAST(uint16_t, wd);
64 }
65 else if (textWidth < ninePatch->width)
66 {
67 const int wd = ((ninePatch->width - textWidth) / 2);
68 offsetX = WINPR_ASSERTING_INT_CAST(uint16_t, wd);
69 }
70
71 if (textHeight < fillHeight)
72 {
73 const int wd = ((fillHeight - textHeight) / 2) + ninePatch->fillTop;
74 offsetY = WINPR_ASSERTING_INT_CAST(uint16_t, wd);
75 }
76 else if (textHeight < ninePatch->height)
77 {
78 const int wd = ((ninePatch->height - textHeight) / 2);
79 offsetY = WINPR_ASSERTING_INT_CAST(uint16_t, wd);
80 }
81
82 return rdtk_font_draw_text(surface, nXDst + offsetX, nYDst + offsetY, font, text);
83 }
84
85 return 1;
86}
87
88rdtkTextField* rdtk_text_field_new(rdtkEngine* engine, rdtkNinePatch* ninePatch)
89{
90 WINPR_ASSERT(engine);
91 WINPR_ASSERT(ninePatch);
92
93 rdtkTextField* textField = (rdtkTextField*)calloc(1, sizeof(rdtkTextField));
94
95 if (!textField)
96 return NULL;
97
98 textField->engine = engine;
99 textField->ninePatch = ninePatch;
100
101 return textField;
102}
103
104void rdtk_text_field_free(rdtkTextField* textField)
105{
106 free(textField);
107}
108
109int rdtk_text_field_engine_init(rdtkEngine* engine)
110{
111 WINPR_ASSERT(engine);
112
113 if (!engine->textField)
114 {
115 engine->textField = rdtk_text_field_new(engine, engine->textField9patch);
116 if (!engine->textField)
117 return -1;
118 }
119
120 return 1;
121}
122
123int rdtk_text_field_engine_uninit(rdtkEngine* engine)
124{
125 WINPR_ASSERT(engine);
126 if (engine->textField)
127 {
128 rdtk_text_field_free(engine->textField);
129 engine->textField = NULL;
130 }
131
132 return 1;
133}