Freeciv-3.1
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/**********************************************************************/
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/**********************************************************************/
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(getpixel(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(getpixel(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#if 0
388/**********************************************************************/
391static int redraw_themelabel(struct widget *label)
392{
393 int ret;
394 Sint16 x, y;
395 SDL_Surface *text = NULL;
396
397 if (!label) {
398 return -3;
399 }
400
401 if ((text = create_text_surf_from_utf8(label->string_utf8)) == NULL) {
402 return (-4);
403 }
404
405 if (label->string_utf8->style & SF_CENTER) {
406 x = (label->size.w - text->w) / 2;
407 } else {
408 if (label->string_utf8->style & SF_CENTER_RIGHT) {
409 x = label->size.w - text->w - adj_size(5);
410 } else {
411 x = adj_size(5);
412 }
413 }
414
415 y = (label->size.h - text->h) / 2;
416
417 /* redraw theme */
418 if (label->theme) {
419 ret = blit_entire_src(label->theme, label->dst->surface, label->size.x, label->size.y);
420 if (ret) {
421 return ret;
422 }
423 }
424
425 ret = blit_entire_src(text, label->dst->surface, label->size.x + x, label->size.y + y);
426
427 FREESURFACE(text);
428
429 return ret;
430}
431#endif /* 0 */
432
433/**********************************************************************/
436int redraw_iconlabel(struct widget *label)
437{
438 int space, ret = 0; /* FIXME: possibly uninitialized */
439 Sint16 x, xI, yI;
440 Sint16 y = 0; /* FIXME: possibly uninitialized */
441 SDL_Surface *text;
443 Uint32 flags;
444
445 if (!label) {
446 return -3;
447 }
448
449 SDL_SetClipRect(label->dst->surface, &label->size);
450
451 flags = get_wflags(label);
452
453 if (flags & WF_DRAW_TEXT_LABEL_WITH_SPACE) {
454 space = adj_size(5);
455 } else {
456 space = 0;
457 }
458
460
461 if (label->theme) { /* Icon */
462 if (text) {
463 if (flags & WF_ICON_CENTER_RIGHT) {
464 xI = label->size.w - label->theme->w - space;
465 } else {
466 if (flags & WF_ICON_CENTER) {
467 xI = (label->size.w - label->theme->w) / 2;
468 } else {
469 xI = space;
470 }
471 }
472
473 if (flags & WF_ICON_ABOVE_TEXT) {
474 yI = 0;
475 y = label->theme->h + adj_size(3)
476 + (label->size.h - (label->theme->h + adj_size(3)) - text->h) / 2;
477 } else {
478 if (flags & WF_ICON_UNDER_TEXT) {
479 y = (label->size.h - (label->theme->h + adj_size(3)) - text->h) / 2;
480 yI = y + text->h + adj_size(3);
481 } else {
482 yI = (label->size.h - label->theme->h) / 2;
483 y = (label->size.h - text->h) / 2;
484 }
485 }
486 /* text */
487 } else {
488#if 0
489 yI = (label->size.h - label->theme->h) / 2;
490 xI = (label->size.w - label->theme->w) / 2;
491#endif /* 0 */
492 yI = 0;
493 xI = space;
494 }
495
496 dst.x = label->size.x + xI;
497 dst.y = label->size.y + yI;
498
499 ret = alphablit(label->theme, NULL, label->dst->surface, &dst, 255);
500
501 if (ret) {
502 return ret - 10;
503 }
504 }
505
506 if (text) {
507 if (label->theme) { /* Icon */
508 if (!(flags & WF_ICON_ABOVE_TEXT) && !(flags & WF_ICON_UNDER_TEXT)) {
509 if (flags & WF_ICON_CENTER_RIGHT) {
510 if (label->string_utf8->style & SF_CENTER) {
511 x = (label->size.w - (label->theme->w + 5 + space) -
512 text->w) / 2;
513 } else {
514 if (label->string_utf8->style & SF_CENTER_RIGHT) {
515 x = label->size.w - (label->theme->w + 5 + space) - text->w;
516 } else {
517 x = space;
518 }
519 }
520 /* WF_ICON_CENTER_RIGHT */
521 } else {
522 if (flags & WF_ICON_CENTER) {
523 /* text is blit on icon */
524 goto alone;
525 } else { /* WF_ICON_CENTER_LEFT */
526 if (label->string_utf8->style & SF_CENTER) {
527 x = space + label->theme->w + adj_size(5) + ((label->size.w -
528 (space +
529 label->theme->w + adj_size(5)) -
530 text->w) / 2);
531 } else {
532 if (label->string_utf8->style & SF_CENTER_RIGHT) {
533 x = label->size.w - text->w - space;
534 } else {
535 x = space + label->theme->w + adj_size(5);
536 }
537 }
538 } /* WF_ICON_CENTER_LEFT */
539 }
540 /* !WF_ICON_ABOVE_TEXT && !WF_ICON_UNDER_TEXT */
541 } else {
542 goto alone;
543 }
544 /* label->theme == Icon */
545 } else {
546 y = (label->size.h - text->h) / 2;
547
548 alone:
549 if (label->string_utf8->style & SF_CENTER) {
550 x = (label->size.w - text->w) / 2;
551 } else {
552 if (label->string_utf8->style & SF_CENTER_RIGHT) {
553 x = label->size.w - text->w - space;
554 } else {
555 x = space;
556 }
557 }
558 }
559
560 dst.x = label->size.x + x;
561 dst.y = label->size.y + y;
562
563 ret = alphablit(text, NULL, label->dst->surface, &dst, 255);
564 FREESURFACE(text);
565 }
566
568
569 return ret;
570}
571
572/**********************************************************************/
575int draw_label(struct widget *label, Sint16 start_x, Sint16 start_y)
576{
577 label->size.x = start_x;
578 label->size.y = start_y;
579
580 return redraw_label(label);
581}
static QString bold(QString text)
Definition citydlg.cpp:3885
SDL_Color * get_theme_color(enum theme_color themecolor)
Definition colors.c:44
Uint32 getpixel(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
int blit_entire_src(SDL_Surface *psrc, SDL_Surface *pdest, Sint16 dest_x, Sint16 dest_y)
Definition graphics.c:417
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:138
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:92
#define SF_CENTER
Definition gui_string.h:40
#define SF_CENTER_RIGHT
Definition gui_string.h:41
#define FC_FREE(ptr)
Definition mem.h:41
#define MAX(x, y)
Definition shared.h:54
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
struct gui_layer * dst
Definition widget.h:116
int(* redraw)(struct widget *pwidget)
Definition widget.h:163
struct container * cont
Definition widget.h:127
Uint16 mod
Definition widget.h:154
SDL_Surface * theme
Definition widget.h:118
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
@ COLOR_THEME_LABEL_BAR
Definition themecolors.h:32
@ COLOR_THEME_THEMELABEL2_BG
Definition themecolors.h:38
@ 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)