FreeRDP
rdtk_text_field.c
1 
19 #include <winpr/assert.h>
20 
21 #include <rdtk/config.h>
22 
23 #include "rdtk_font.h"
24 
25 #include "rdtk_text_field.h"
26 
27 int rdtk_text_field_draw(rdtkSurface* surface, uint16_t nXDst, uint16_t nYDst, uint16_t nWidth,
28  uint16_t nHeight, rdtkTextField* textField, const char* text)
29 {
30  uint16_t offsetX = 0;
31  uint16_t offsetY = 0;
32  uint16_t textWidth = 0;
33  uint16_t textHeight = 0;
34  uint16_t fillWidth = 0;
35  uint16_t fillHeight = 0;
36  rdtkFont* font = NULL;
37  rdtkEngine* engine = NULL;
38  rdtkNinePatch* ninePatch = NULL;
39 
40  WINPR_ASSERT(surface);
41  WINPR_ASSERT(textField);
42  WINPR_ASSERT(text);
43 
44  engine = surface->engine;
45  font = engine->font;
46  textField = surface->engine->textField;
47  ninePatch = textField->ninePatch;
48 
49  rdtk_font_text_draw_size(font, &textWidth, &textHeight, text);
50 
51  rdtk_nine_patch_draw(surface, nXDst, nYDst, nWidth, nHeight, ninePatch);
52 
53  if ((textWidth > 0) && (textHeight > 0))
54  {
55  fillWidth = nWidth - (ninePatch->width - ninePatch->fillWidth);
56  fillHeight = nHeight - (ninePatch->height - ninePatch->fillHeight);
57 
58  offsetX = ninePatch->fillLeft;
59  offsetY = ninePatch->fillTop;
60 
61  if (textWidth < fillWidth)
62  offsetX = ((fillWidth - textWidth) / 2) + ninePatch->fillLeft;
63  else if (textWidth < ninePatch->width)
64  offsetX = ((ninePatch->width - textWidth) / 2);
65 
66  if (textHeight < fillHeight)
67  offsetY = ((fillHeight - textHeight) / 2) + ninePatch->fillTop;
68  else if (textHeight < ninePatch->height)
69  offsetY = ((ninePatch->height - textHeight) / 2);
70 
71  rdtk_font_draw_text(surface, nXDst + offsetX, nYDst + offsetY, font, text);
72  }
73 
74  return 1;
75 }
76 
77 rdtkTextField* rdtk_text_field_new(rdtkEngine* engine, rdtkNinePatch* ninePatch)
78 {
79  WINPR_ASSERT(engine);
80  WINPR_ASSERT(ninePatch);
81 
82  rdtkTextField* textField = (rdtkTextField*)calloc(1, sizeof(rdtkTextField));
83 
84  if (!textField)
85  return NULL;
86 
87  textField->engine = engine;
88  textField->ninePatch = ninePatch;
89 
90  return textField;
91 }
92 
93 void rdtk_text_field_free(rdtkTextField* textField)
94 {
95  free(textField);
96 }
97 
98 int rdtk_text_field_engine_init(rdtkEngine* engine)
99 {
100  WINPR_ASSERT(engine);
101 
102  if (!engine->textField)
103  {
104  engine->textField = rdtk_text_field_new(engine, engine->textField9patch);
105  if (!engine->textField)
106  return -1;
107  }
108 
109  return 1;
110 }
111 
112 int rdtk_text_field_engine_uninit(rdtkEngine* engine)
113 {
114  WINPR_ASSERT(engine);
115  if (engine->textField)
116  {
117  rdtk_text_field_free(engine->textField);
118  engine->textField = NULL;
119  }
120 
121  return 1;
122 }