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