Freeciv-3.3
Loading...
Searching...
No Matches
widget_label.c
Go to the documentation of this file.
1/***********************************************************************
2 Freeciv - Copyright (C) 2006 - The Freeciv Project
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License as published by
5 the Free Software Foundation; either version 2, or (at your option)
6 any later version.
7
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
12***********************************************************************/
13
14#ifdef HAVE_CONFIG_H
15#include <fc_config.h>
16#endif
17
18/* SDL2 */
19#ifdef SDL2_PLAIN_INCLUDE
20#include <SDL.h>
21#else /* SDL2_PLAIN_INCLUDE */
22#include <SDL2/SDL.h>
23#endif /* SDL2_PLAIN_INCLUDE */
24
25/* utility */
26#include "log.h"
27#include "mem.h"
28
29/* client/gui-sdl2 */
30#include "colors.h"
31#include "graphics.h"
32#include "themespec.h"
33
34#include "widget.h"
35#include "widget_p.h"
36
37static int (*baseclass_redraw)(struct widget *pwidget);
38
39/**********************************************************************/
42static inline int redraw_themelabel2(struct widget *label)
43{
44 SDL_Rect src = {0,0, label->size.w, label->size.h};
45 SDL_Rect dst = {label->size.x, label->size.y, 0, 0};
46/*
47 if (!label) {
48 return -3;
49 }
50*/
51 if (get_wstate(label) == FC_WS_SELECTED) {
52 src.y = label->size.h;
53 }
54
55 return alphablit(label->theme, &src, label->dst->surface, &dst, 255);
56}
57
58/**********************************************************************/
61static int redraw_label(struct widget *label)
62{
63 int ret;
64 SDL_Rect area = label->size;
66 SDL_Color backup_color = {0, 0, 0, 0};
67
68 ret = (*baseclass_redraw)(label);
69 if (ret != 0) {
70 return ret;
71 }
72
73 if (get_wtype(label) == WT_T2_LABEL) {
74 return redraw_themelabel2(label);
75 }
76
77 /* redraw selected bar */
78 if (get_wstate(label) == FC_WS_SELECTED) {
79 if (get_wflags(label) & WF_SELECT_WITHOUT_BAR) {
80 if (label->string_utf8 != NULL) {
82 label->string_utf8->fgcol = bar_color;
83 if (label->string_utf8->style & TTF_STYLE_BOLD) {
85 } else {
87 }
88 }
89 } else {
91 }
92 }
93
94 /* redraw icon label */
95 ret = redraw_iconlabel(label);
96
97 if ((get_wstate(label) == FC_WS_SELECTED) && (label->string_utf8 != NULL)) {
98 if (get_wflags(label) & WF_SELECT_WITHOUT_BAR) {
101 } else {
103 }
105 } else {
106 if (label->string_utf8->render == 3) {
108 }
109 }
110 }
111
112 return ret;
113}
114
115/**********************************************************************/
118void remake_label_size(struct widget *label)
119{
120 SDL_Surface *icon = label->theme;
121 utf8_str *text = label->string_utf8;
122 Uint32 flags = get_wflags(label);
123 SDL_Rect buf = { 0, 0, 0, 0 };
124 Uint16 w = 0, h = 0, space;
125
126 if (flags & WF_DRAW_TEXT_LABEL_WITH_SPACE) {
127 space = adj_size(10);
128 } else {
129 space = 0;
130 }
131
132 if (text != NULL) {
134 bool bold = TRUE;
135
136 if (without_box) {
137 bold = ((text->style & TTF_STYLE_BOLD) == TTF_STYLE_BOLD);
138 text->style |= TTF_STYLE_BOLD;
139 }
140
141 utf8_str_size(text, &buf);
142
143 if (without_box && !bold) {
144 text->style &= ~TTF_STYLE_BOLD;
145 }
146
147 w = MAX(w, buf.w + space);
148 h = MAX(h, buf.h);
149 }
150
151 if (icon) {
152 if (text != NULL) {
153 if ((flags & WF_ICON_UNDER_TEXT) || (flags & WF_ICON_ABOVE_TEXT)) {
154 w = MAX(w, icon->w + space);
155 h = MAX(h, buf.h + icon->h + adj_size(3));
156 } else {
157 if (flags & WF_ICON_CENTER) {
158 w = MAX(w, icon->w + space);
159 h = MAX(h, icon->h);
160 } else {
161 w = MAX(w, buf.w + icon->w + adj_size(5) + space);
162 h = MAX(h, icon->h);
163 }
164 }
165 /* text */
166 } else {
167 w = MAX(w, icon->w + space);
168 h = MAX(h, icon->h);
169 }
170 }
171
172 /* icon */
173 label->size.w = w;
174 label->size.h = h;
175}
176
177/**********************************************************************/
181 utf8_str *pstr, Uint16 w, Uint16 h,
182 Uint32 flags)
183{
184 struct widget *label = NULL;
185
186 if (icon == NULL && pstr == NULL) {
187 return NULL;
188 }
189
190 label = widget_new();
191 label->theme = icon;
192 label->string_utf8 = pstr;
193 set_wflag(label,
195 WF_RESTORE_BACKGROUND | flags));
197 set_wtype(label, WT_T_LABEL);
198 label->mod = KMOD_NONE;
199 label->dst = pdest;
200
201 baseclass_redraw = label->redraw;
202 label->redraw = redraw_label;
203
204 remake_label_size(label);
205
206 label->size.w = MAX(label->size.w, w);
207 label->size.h = MAX(label->size.h, h);
208
209 return label;
210}
211
212/**********************************************************************/
216 utf8_str *pstr, Uint32 flags)
217{
218 struct widget *icon_label = NULL;
219
221
222 icon_label->theme = icon;
223 icon_label->string_utf8 = pstr;
227 icon_label->mod = KMOD_NONE;
228 icon_label->dst = pdest;
229
231 icon_label->redraw = redraw_label;
232
234
235 return icon_label;
236}
237
238/**********************************************************************/
242 utf8_str *pstr, Uint16 w, Uint16 h,
243 Uint32 flags)
244{
245 struct widget *label = NULL;
248 SDL_Color store = {0, 0, 0, 0};
251
252 if (icon == NULL && pstr == NULL) {
253 return NULL;
254 }
255
256 label = widget_new();
257 label->theme = icon;
258 label->string_utf8 = pstr;
261 set_wtype(label, WT_T2_LABEL);
262 label->mod = KMOD_NONE;
263 baseclass_redraw = label->redraw;
264 label->redraw = redraw_label;
265
266 remake_label_size(label);
267
268 label->size.w = MAX(label->size.w, w);
269 label->size.h = MAX(label->size.h, h);
270
271 ptheme = create_surf(label->size.w, label->size.h * 2, SDL_SWSURFACE);
272
273 colorkey = SDL_MapRGBA(ptheme->format, pstr->bgcol.r,
274 pstr->bgcol.g, pstr->bgcol.b, pstr->bgcol.a);
276
277 label->size.x = 0;
278 label->size.y = 0;
279 area = label->size;
280 label->dst = gui_layer_new(0, 0, ptheme);
281
282 /* Normal */
283 redraw_iconlabel(label);
284
285 /* Selected */
286 area.x = 0;
287 area.y = label->size.h;
288
289 if (flags & WF_RESTORE_BACKGROUND) {
291 store = pstr->bgcol;
292 SDL_GetRGBA(get_pixel(ptheme, area.x , area.y), ptheme->format,
293 &pstr->bgcol.r, &pstr->bgcol.g,
294 &pstr->bgcol.b, &pstr->bgcol.a);
295 } else {
297 }
298
299 label->size.y = label->size.h;
300 redraw_iconlabel(label);
301
302 if (flags & WF_RESTORE_BACKGROUND) {
303 pstr->bgcol = store;
304 }
305
306 label->size.x = 0;
307 label->size.y = 0;
308 if (flags & WF_FREE_THEME) {
309 FREESURFACE(label->theme);
310 }
311 label->theme = ptheme;
312 FC_FREE(label->dst);
313 label->dst = pdest;
314
315 return label;
316}
317
318/**********************************************************************/
322{
323 SDL_Rect start, area;
324 SDL_Color store = {0, 0, 0, 0};
329 icon_label->size.h * 2, SDL_SWSURFACE);
330
331 colorkey = SDL_MapRGBA(ptheme->format,
332 icon_label->string_utf8->bgcol.r,
333 icon_label->string_utf8->bgcol.g,
334 icon_label->string_utf8->bgcol.b,
335 icon_label->string_utf8->bgcol.a);
337
338 start = icon_label->size;
339 icon_label->size.x = 0;
340 icon_label->size.y = 0;
341 area = start;
342 pdest = icon_label->dst->surface;
343 icon_label->dst->surface = ptheme;
344
345 /* Normal */
347
348 /* Selected */
349 area.x = 0;
350 area.y = icon_label->size.h;
351
352 if (flags & WF_RESTORE_BACKGROUND) {
354 store = icon_label->string_utf8->bgcol;
355 SDL_GetRGBA(get_pixel(ptheme, area.x , area.y), ptheme->format,
356 &icon_label->string_utf8->bgcol.r,
357 &icon_label->string_utf8->bgcol.g,
358 &icon_label->string_utf8->bgcol.b,
359 &icon_label->string_utf8->bgcol.a);
360 } else {
362 }
363
364 icon_label->size.y = icon_label->size.h;
366
367 if (flags & WF_RESTORE_BACKGROUND) {
368 icon_label->string_utf8->bgcol = store;
369 }
370
371 icon_label->size = start;
372 if (flags & WF_FREE_THEME) {
373 FREESURFACE(icon_label->theme);
374 }
375 icon_label->theme = ptheme;
376 if (flags & WF_FREE_STRING) {
377 FREEUTF8STR(icon_label->string_utf8);
378 }
379 icon_label->dst->surface = pdest;
381
382 icon_label->redraw = redraw_label;
383
384 return icon_label;
385}
386
387/**********************************************************************/
390int redraw_iconlabel(struct widget *label)
391{
392 int space, ret = 0; /* FIXME: possibly uninitialized */
393 Sint16 x, xI, yI;
394 Sint16 y = 0; /* FIXME: possibly uninitialized */
395 SDL_Surface *text;
397 Uint32 flags;
398
399 if (!label) {
400 return -3;
401 }
402
403 SDL_SetClipRect(label->dst->surface, &label->size);
404
405 flags = get_wflags(label);
406
407 if (flags & WF_DRAW_TEXT_LABEL_WITH_SPACE) {
408 space = adj_size(5);
409 } else {
410 space = 0;
411 }
412
414
415 if (label->theme) { /* Icon */
416 if (text) {
417 if (flags & WF_ICON_CENTER_RIGHT) {
418 xI = label->size.w - label->theme->w - space;
419 } else {
420 if (flags & WF_ICON_CENTER) {
421 xI = (label->size.w - label->theme->w) / 2;
422 } else {
423 xI = space;
424 }
425 }
426
427 if (flags & WF_ICON_ABOVE_TEXT) {
428 yI = 0;
429 y = label->theme->h + adj_size(3)
430 + (label->size.h - (label->theme->h + adj_size(3)) - text->h) / 2;
431 } else {
432 if (flags & WF_ICON_UNDER_TEXT) {
433 y = (label->size.h - (label->theme->h + adj_size(3)) - text->h) / 2;
434 yI = y + text->h + adj_size(3);
435 } else {
436 yI = (label->size.h - label->theme->h) / 2;
437 y = (label->size.h - text->h) / 2;
438 }
439 }
440 /* text */
441 } else {
442#if 0
443 yI = (label->size.h - label->theme->h) / 2;
444 xI = (label->size.w - label->theme->w) / 2;
445#endif /* 0 */
446 yI = 0;
447 xI = space;
448 }
449
450 dst.x = label->size.x + xI;
451 dst.y = label->size.y + yI;
452
453 ret = alphablit(label->theme, NULL, label->dst->surface, &dst, 255);
454
455 if (ret) {
456 return ret - 10;
457 }
458 }
459
460 if (text) {
461 if (label->theme) { /* Icon */
462 if (!(flags & WF_ICON_ABOVE_TEXT) && !(flags & WF_ICON_UNDER_TEXT)) {
463 if (flags & WF_ICON_CENTER_RIGHT) {
464 if (label->string_utf8->style & SF_CENTER) {
465 x = (label->size.w - (label->theme->w + 5 + space) -
466 text->w) / 2;
467 } else {
468 if (label->string_utf8->style & SF_CENTER_RIGHT) {
469 x = label->size.w - (label->theme->w + 5 + space) - text->w;
470 } else {
471 x = space;
472 }
473 }
474 /* WF_ICON_CENTER_RIGHT */
475 } else {
476 if (flags & WF_ICON_CENTER) {
477 /* text is blit on icon */
478 goto alone;
479 } else { /* WF_ICON_CENTER_LEFT */
480 if (label->string_utf8->style & SF_CENTER) {
481 x = space + label->theme->w + adj_size(5) + ((label->size.w -
482 (space +
483 label->theme->w + adj_size(5)) -
484 text->w) / 2);
485 } else {
486 if (label->string_utf8->style & SF_CENTER_RIGHT) {
487 x = label->size.w - text->w - space;
488 } else {
489 x = space + label->theme->w + adj_size(5);
490 }
491 }
492 } /* WF_ICON_CENTER_LEFT */
493 }
494 /* !WF_ICON_ABOVE_TEXT && !WF_ICON_UNDER_TEXT */
495 } else {
496 goto alone;
497 }
498 /* label->theme == Icon */
499 } else {
500 y = (label->size.h - text->h) / 2;
501
502 alone:
503 if (label->string_utf8->style & SF_CENTER) {
504 x = (label->size.w - text->w) / 2;
505 } else {
506 if (label->string_utf8->style & SF_CENTER_RIGHT) {
507 x = label->size.w - text->w - space;
508 } else {
509 x = space;
510 }
511 }
512 }
513
514 dst.x = label->size.x + x;
515 dst.y = label->size.y + y;
516
517 ret = alphablit(text, NULL, label->dst->surface, &dst, 255);
518 FREESURFACE(text);
519 }
520
522
523 return ret;
524}
525
526/**********************************************************************/
529int draw_label(struct widget *label, Sint16 start_x, Sint16 start_y)
530{
531 label->size.x = start_x;
532 label->size.y = start_y;
533
534 return redraw_label(label);
535}
static QString bold(QString text)
Definition citydlg.cpp:3988
char * incite_cost
Definition comments.c:76
SDL_Color * get_theme_color(enum theme_color themecolor)
Definition colors.c:47
Uint32 get_pixel(SDL_Surface *surf, Sint16 x, Sint16 y)
Definition graphics.c:430
int fill_rect_alpha(SDL_Surface *surf, SDL_Rect *prect, SDL_Color *pcolor)
Definition graphics.c:865
struct gui_layer * gui_layer_new(int x, int y, SDL_Surface *surface)
Definition graphics.c:66
int alphablit(SDL_Surface *src, SDL_Rect *srcrect, SDL_Surface *dst, SDL_Rect *dstrect, unsigned char alpha_mod)
Definition graphics.c:199
SDL_Surface * create_surf(int width, int height, Uint32 flags)
Definition graphics.c:351
#define FREESURFACE(ptr)
Definition graphics.h:322
#define map_rgba(format, color)
Definition graphics.h:315
#define adj_size(size)
Definition gui_main.h:141
void utf8_str_size(utf8_str *pstr, SDL_Rect *fill)
Definition gui_string.c:106
SDL_Surface * create_text_surf_from_utf8(utf8_str *pstr)
Definition gui_string.c:425
#define FREEUTF8STR(pstr)
Definition gui_string.h:93
#define SF_CENTER
Definition gui_string.h:40
#define SF_CENTER_RIGHT
Definition gui_string.h:41
@ COLOR_THEME_LABEL_BAR
Definition themecolors.h:33
@ COLOR_THEME_THEMELABEL2_BG
Definition themecolors.h:39
@ FC_WS_DISABLED
Definition widget.h:99
@ FC_WS_SELECTED
Definition widget.h:97
enum widget_flag get_wflags(const struct widget *pwidget)
Definition widget_core.c:86
void set_wstate(struct widget *pwidget, enum widget_state state)
Definition widget_core.c:36
enum widget_state get_wstate(const struct widget *pwidget)
Definition widget_core.c:70
@ WF_FREE_GFX
Definition widget.h:70
@ WF_ICON_CENTER
Definition widget.h:83
@ WF_ICON_ABOVE_TEXT
Definition widget.h:81
@ WF_ICON_CENTER_RIGHT
Definition widget.h:84
@ WF_FREE_STRING
Definition widget.h:76
@ WF_SELECT_WITHOUT_BAR
Definition widget.h:89
@ WF_RESTORE_BACKGROUND
Definition widget.h:85
@ WF_FREE_THEME
Definition widget.h:72
@ WF_DRAW_TEXT_LABEL_WITH_SPACE
Definition widget.h:87
@ WF_ICON_UNDER_TEXT
Definition widget.h:82
void set_wtype(struct widget *pwidget, enum widget_type type)
Definition widget_core.c:45
enum widget_type get_wtype(const struct widget *pwidget)
Definition widget_core.c:78
void set_wflag(struct widget *pwidget, enum widget_flag flag)
Definition widget_core.c:54
@ WT_T2_LABEL
Definition widget.h:61
@ WT_T_LABEL
Definition widget.h:53
@ WT_I_LABEL
Definition widget.h:54
struct widget * widget_new(void)
void remake_label_size(struct widget *label)
int redraw_iconlabel(struct widget *label)
struct widget * convert_iconlabel_to_themeiconlabel2(struct widget *icon_label)
struct widget * create_themelabel(SDL_Surface *icon, struct gui_layer *pdest, utf8_str *pstr, Uint16 w, Uint16 h, Uint32 flags)
int draw_label(struct widget *label, Sint16 start_x, Sint16 start_y)
static int(* baseclass_redraw)(struct widget *pwidget)
struct widget * create_themelabel2(SDL_Surface *icon, struct gui_layer *pdest, utf8_str *pstr, Uint16 w, Uint16 h, Uint32 flags)
struct widget * create_iconlabel(SDL_Surface *icon, struct gui_layer *pdest, utf8_str *pstr, Uint32 flags)
static int redraw_themelabel2(struct widget *label)
static int redraw_label(struct widget *label)
#define FC_FREE(ptr)
Definition mem.h:41
#define MAX(x, y)
Definition shared.h:54
struct sprite int int y
Definition sprite_g.h:31
struct sprite int x
Definition sprite_g.h:31
SDL_Surface * surface
Definition graphics.h:229
Uint8 style
Definition gui_string.h:53
SDL_Color bgcol
Definition gui_string.h:58
SDL_Color fgcol
Definition gui_string.h:57
Uint8 render
Definition gui_string.h:54
SDL_Surface * theme
Definition widget.h:118
int(* redraw)(struct widget *pwidget)
Definition widget.h:163
struct gui_layer * dst
Definition widget.h:116
Uint16 mod
Definition widget.h:154
utf8_str * string_utf8
Definition widget.h:121
SDL_Rect area
Definition widget.h:149
SDL_Rect size
Definition widget.h:145
#define TRUE
Definition support.h:46