00001
00038 #include <stdint.h>
00039 #include <stdlib.h>
00040 #include <assert.h>
00041 #include <membag.h>
00042 #include <string.h>
00043 #include <gfx/wtk.h>
00044
00057 struct wtk_label {
00059 struct win_window *container;
00061 char *caption;
00063 bool align_right;
00064 };
00065
00074 struct win_window *wtk_label_as_child(struct wtk_label *label)
00075 {
00076 assert(label);
00077 return label->container;
00078 }
00079
00081 bool wtk_label_change(struct wtk_label *label, const char *caption)
00082 {
00083 assert(label);
00084 assert(caption);
00085
00086 uint8_t new_len = strlen(caption);
00087 uint8_t old_len = 0;
00088
00089 if (caption) {
00090 old_len = strlen(label->caption);
00091 }
00092
00093
00094
00095
00096 if (new_len > old_len) {
00097
00098
00099 if (caption) {
00100 membag_free(label->caption);
00101 }
00102
00103
00104 label->caption = membag_alloc((new_len + 1) * sizeof(char));
00105 if (!label->caption) {
00106 goto outofmem_caption;
00107 }
00108 }
00109
00110 wtk_copy_string(label->caption, caption);
00111
00112
00113 win_redraw(label->container);
00114
00115 return true;
00116
00117 outofmem_caption:
00118 return false;
00119 }
00120
00131 static bool wtk_label_handler(struct win_window *win,
00132 enum win_event_type type, void const *data)
00133 {
00134
00135 struct wtk_label *label = (struct wtk_label *)win_get_custom_data(win);
00136
00137 switch (type) {
00138 case WIN_EVENT_DRAW:
00139 {
00140
00141 assert(win == label->container);
00142
00143
00144
00145
00146 struct win_clip_region const *clip =
00147 (struct win_clip_region const *)data;
00148 struct win_area const *area = win_get_area(win);
00149
00150 if (label->align_right == false) {
00151
00152 gfx_draw_string(label->caption,
00153 clip->origin.x,
00154 clip->origin.y,
00155 &sysfont,
00156 WTK_STATICTEXT_CAPTION_COLOR,
00157 GFX_COLOR_TRANSPARENT);
00158 } else {
00159
00160 gfx_coord_t width;
00161 gfx_coord_t height;
00162
00163 gfx_get_string_bounding_box(label->caption,
00164 &sysfont, &width, &height);
00165
00166 gfx_draw_string(label->caption,
00167 clip->origin.x + area->size.x -
00168 width, clip->origin.y, &sysfont,
00169 WTK_STATICTEXT_CAPTION_COLOR,
00170 GFX_COLOR_TRANSPARENT);
00171 }
00172
00173
00174
00175
00176 return true;
00177 }
00178
00179 case WIN_EVENT_DESTROY:
00180
00181 assert(win == label->container);
00182
00183
00184
00185
00186 membag_free(label->caption);
00187 membag_free(label);
00188
00189
00190
00191
00192 return true;
00193
00194 default:
00195
00196 return false;
00197 }
00198 }
00199
00209 void wtk_label_size_hint(struct win_point *size, const char *caption)
00210 {
00211 assert(size);
00212 assert(caption);
00213
00214 gfx_get_string_bounding_box(caption, &sysfont, &size->x, &size->y);
00215 }
00216
00235 struct wtk_label *wtk_label_create(struct win_window *parent,
00236 struct win_area const *area,
00237 char const *caption, bool align_right)
00238 {
00239 struct win_attributes attr;
00240 struct wtk_label *label;
00241
00242 assert(area);
00243 assert(caption);
00244 assert(parent);
00245
00246
00247 label = membag_alloc(sizeof(struct wtk_label));
00248 if (!label) {
00249 goto outofmem_label;
00250 }
00251
00252 label->align_right = align_right;
00253
00254
00255 label->caption = membag_alloc((strlen(caption) + 1) * sizeof(char));
00256 if (!label->caption) {
00257 goto outofmem_caption;
00258 }
00259
00260 wtk_copy_string(label->caption, caption);
00261
00262
00263 attr.event_handler = wtk_label_handler;
00264 attr.custom = label;
00265
00266
00267 attr.area = *area;
00268 attr.background = NULL;
00269 attr.behavior = WIN_BEHAVIOR_REDRAW_PARENT;
00270
00271 label->container = win_create(parent, &attr);
00272 if (!label->container) {
00273 goto outofmem_container;
00274 }
00275
00276 return label;
00277
00278 outofmem_container:
00279 membag_free(label->caption);
00280
00281 outofmem_caption:
00282 membag_free(label);
00283
00284 outofmem_label:
00285 return NULL;
00286 }
00287