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/* SDL3 */
19#include <SDL3/SDL.h>
20
21/* utility */
22#include "log.h"
23#include "mem.h"
24
25/* client/gui-sdl3 */
26#include "colors.h"
27#include "graphics.h"
28#include "themespec.h"
29
30#include "widget.h"
31#include "widget_p.h"
32
33static int (*baseclass_redraw)(struct widget *pwidget);
34
35/**********************************************************************/
38static inline int redraw_themelabel2(struct widget *label)
39{
40 SDL_Rect src = {0,0, label->size.w, label->size.h};
41 SDL_Rect dst = {label->size.x, label->size.y, 0, 0};
42/*
43 if (!label) {
44 return -3;
45 }
46*/
47 if (get_wstate(label) == FC_WS_SELECTED) {
48 src.y = label->size.h;
49 }
50
51 return alphablit(label->theme, &src, label->dst->surface, &dst, 255);
52}
53
54/**********************************************************************/
57static int redraw_label(struct widget *label)
58{
59 int ret;
60 SDL_Rect area = label->size;
62 SDL_Color backup_color = {0, 0, 0, 0};
63
64 ret = (*baseclass_redraw)(label);
65 if (ret != 0) {
66 return ret;
67 }
68
69 if (get_wtype(label) == WT_T2_LABEL) {
70 return redraw_themelabel2(label);
71 }
72
73 /* Redraw selected bar */
74 if (get_wstate(label) == FC_WS_SELECTED) {
75 if (get_wflags(label) & WF_SELECT_WITHOUT_BAR) {
76 if (label->string_utf8 != NULL) {
78 label->string_utf8->fgcol = bar_color;
79 if (label->string_utf8->style & TTF_STYLE_BOLD) {
81 } else {
83 }
84 }
85 } else {
87 }
88 }
89
90 /* Redraw icon label */
91 ret = redraw_iconlabel(label);
92
93 if ((get_wstate(label) == FC_WS_SELECTED) && (label->string_utf8 != NULL)) {
94 if (get_wflags(label) & WF_SELECT_WITHOUT_BAR) {
97 } else {
99 }
101 } else {
102 if (label->string_utf8->render == 3) {
104 }
105 }
106 }
107
108 return ret;
109}
110
111/**********************************************************************/
114void remake_label_size(struct widget *label)
115{
116 SDL_Surface *icon = label->theme;
117 utf8_str *text = label->string_utf8;
118 Uint32 flags = get_wflags(label);
119 SDL_Rect buf = { 0, 0, 0, 0 };
120 Uint16 w = 0, h = 0, space;
121
122 if (flags & WF_DRAW_TEXT_LABEL_WITH_SPACE) {
123 space = adj_size(10);
124 } else {
125 space = 0;
126 }
127
128 if (text != NULL) {
130 bool bold = TRUE;
131
132 if (without_box) {
133 bold = ((text->style & TTF_STYLE_BOLD) == TTF_STYLE_BOLD);
134 text->style |= TTF_STYLE_BOLD;
135 }
136
137 utf8_str_size(text, &buf);
138
139 if (without_box && !bold) {
140 text->style &= ~TTF_STYLE_BOLD;
141 }
142
143 w = MAX(w, buf.w + space);
144 h = MAX(h, buf.h);
145 }
146
147 if (icon) {
148 if (text != NULL) {
149 if ((flags & WF_ICON_UNDER_TEXT) || (flags & WF_ICON_ABOVE_TEXT)) {
150 w = MAX(w, icon->w + space);
151 h = MAX(h, buf.h + icon->h + adj_size(3));
152 } else {
153 if (flags & WF_ICON_CENTER) {
154 w = MAX(w, icon->w + space);
155 h = MAX(h, icon->h);
156 } else {
157 w = MAX(w, buf.w + icon->w + adj_size(5) + space);
158 h = MAX(h, icon->h);
159 }
160 }
161 /* Text */
162 } else {
163 w = MAX(w, icon->w + space);
164 h = MAX(h, icon->h);
165 }
166 }
167
168 /* Icon */
169 label->size.w = w;
170 label->size.h = h;
171}
172
173/**********************************************************************/
177 utf8_str *pstr, Uint16 w, Uint16 h,
178 Uint32 flags)
179{
180 struct widget *label = NULL;
181
182 if (icon == NULL && pstr == NULL) {
183 return NULL;
184 }
185
186 label = widget_new();
187 label->theme = icon;
188 label->string_utf8 = pstr;
189 set_wflag(label,
191 WF_RESTORE_BACKGROUND | flags));
193 set_wtype(label, WT_T_LABEL);
194 label->mod = SDL_KMOD_NONE;
195 label->dst = pdest;
196
197 baseclass_redraw = label->redraw;
198 label->redraw = redraw_label;
199
200 remake_label_size(label);
201
202 label->size.w = MAX(label->size.w, w);
203 label->size.h = MAX(label->size.h, h);
204
205 return label;
206}
207
208/**********************************************************************/
212 utf8_str *pstr, Uint32 flags)
213{
214 struct widget *icon_label = NULL;
215
217
218 icon_label->theme = icon;
219 icon_label->string_utf8 = pstr;
224 icon_label->dst = pdest;
225
227 icon_label->redraw = redraw_label;
228
230
231 return icon_label;
232}
233
234/**********************************************************************/
238 utf8_str *pstr, Uint16 w, Uint16 h,
239 Uint32 flags)
240{
241 struct widget *label = NULL;
244 SDL_Color store = {0, 0, 0, 0};
247 const struct SDL_PixelFormatDetails *details;
248
249 if (icon == NULL && pstr == NULL) {
250 return NULL;
251 }
252
253 label = widget_new();
254 label->theme = icon;
255 label->string_utf8 = pstr;
258 set_wtype(label, WT_T2_LABEL);
259 label->mod = SDL_KMOD_NONE;
260 baseclass_redraw = label->redraw;
261 label->redraw = redraw_label;
262
263 remake_label_size(label);
264
265 label->size.w = MAX(label->size.w, w);
266 label->size.h = MAX(label->size.h, h);
267
268 ptheme = create_surf(label->size.w, label->size.h * 2);
269
272 pstr->bgcol.r, pstr->bgcol.g, pstr->bgcol.b,
273 pstr->bgcol.a);
275
276 label->size.x = 0;
277 label->size.y = 0;
278 area = label->size;
279 label->dst = gui_layer_new(0, 0, ptheme);
280
281 /* Normal */
282 redraw_iconlabel(label);
283
284 /* Selected */
285 area.x = 0;
286 area.y = label->size.h;
287
288 if (flags & WF_RESTORE_BACKGROUND) {
291 store = pstr->bgcol;
292 SDL_GetRGBA(get_pixel(ptheme, area.x , area.y),
293 details, NULL,
294 &pstr->bgcol.r, &pstr->bgcol.g,
295 &pstr->bgcol.b, &pstr->bgcol.a);
296 } else {
298 }
299
300 label->size.y = label->size.h;
301 redraw_iconlabel(label);
302
303 if (flags & WF_RESTORE_BACKGROUND) {
304 pstr->bgcol = store;
305 }
306
307 label->size.x = 0;
308 label->size.y = 0;
309 if (flags & WF_FREE_THEME) {
310 FREESURFACE(label->theme);
311 }
312 label->theme = ptheme;
313 FC_FREE(label->dst);
314 label->dst = pdest;
315
316 return label;
317}
318
319/**********************************************************************/
323{
324 SDL_Rect start, area;
325 SDL_Color store = {0, 0, 0, 0};
330 icon_label->size.h * 2);
333
335 icon_label->string_utf8->bgcol.r,
336 icon_label->string_utf8->bgcol.g,
337 icon_label->string_utf8->bgcol.b,
338 icon_label->string_utf8->bgcol.a);
340
341 start = icon_label->size;
342 icon_label->size.x = 0;
343 icon_label->size.y = 0;
344 area = start;
345 pdest = icon_label->dst->surface;
346 icon_label->dst->surface = ptheme;
347
348 /* Normal */
350
351 /* Selected */
352 area.x = 0;
353 area.y = icon_label->size.h;
354
355 if (flags & WF_RESTORE_BACKGROUND) {
358 store = icon_label->string_utf8->bgcol;
360 details, NULL,
361 &icon_label->string_utf8->bgcol.r,
362 &icon_label->string_utf8->bgcol.g,
363 &icon_label->string_utf8->bgcol.b,
364 &icon_label->string_utf8->bgcol.a);
365 } else {
367 }
368
369 icon_label->size.y = icon_label->size.h;
371
372 if (flags & WF_RESTORE_BACKGROUND) {
373 icon_label->string_utf8->bgcol = store;
374 }
375
376 icon_label->size = start;
377 if (flags & WF_FREE_THEME) {
378 FREESURFACE(icon_label->theme);
379 }
380 icon_label->theme = ptheme;
381 if (flags & WF_FREE_STRING) {
382 FREEUTF8STR(icon_label->string_utf8);
383 }
384 icon_label->dst->surface = pdest;
386
387 icon_label->redraw = redraw_label;
388
389 return icon_label;
390}
391
392/**********************************************************************/
395int redraw_iconlabel(struct widget *label)
396{
397 int space, ret = 0; /* FIXME: possibly uninitialized */
398 Sint16 x, xI, yI;
399 Sint16 y = 0; /* FIXME: possibly uninitialized */
400 SDL_Surface *text;
402 Uint32 flags;
403
404 if (label == NULL) {
405 return -3;
406 }
407
408 SDL_SetSurfaceClipRect(label->dst->surface, &label->size);
409
410 flags = get_wflags(label);
411
412 if (flags & WF_DRAW_TEXT_LABEL_WITH_SPACE) {
413 space = adj_size(5);
414 } else {
415 space = 0;
416 }
417
419
420 if (label->theme) { /* Icon */
421 if (text) {
422 if (flags & WF_ICON_CENTER_RIGHT) {
423 xI = label->size.w - label->theme->w - space;
424 } else {
425 if (flags & WF_ICON_CENTER) {
426 xI = (label->size.w - label->theme->w) / 2;
427 } else {
428 xI = space;
429 }
430 }
431
432 if (flags & WF_ICON_ABOVE_TEXT) {
433 yI = 0;
434 y = label->theme->h + adj_size(3)
435 + (label->size.h - (label->theme->h + adj_size(3)) - text->h) / 2;
436 } else {
437 if (flags & WF_ICON_UNDER_TEXT) {
438 y = (label->size.h - (label->theme->h + adj_size(3)) - text->h) / 2;
439 yI = y + text->h + adj_size(3);
440 } else {
441 yI = (label->size.h - label->theme->h) / 2;
442 y = (label->size.h - text->h) / 2;
443 }
444 }
445 /* Text */
446 } else {
447#if 0
448 yI = (label->size.h - label->theme->h) / 2;
449 xI = (label->size.w - label->theme->w) / 2;
450#endif /* 0 */
451 yI = 0;
452 xI = space;
453 }
454
455 dst.x = label->size.x + xI;
456 dst.y = label->size.y + yI;
457
458 ret = alphablit(label->theme, NULL, label->dst->surface, &dst, 255);
459
460 if (ret) {
461 return ret - 10;
462 }
463 }
464
465 if (text) {
466 if (label->theme) { /* Icon */
467 if (!(flags & WF_ICON_ABOVE_TEXT) && !(flags & WF_ICON_UNDER_TEXT)) {
468 if (flags & WF_ICON_CENTER_RIGHT) {
469 if (label->string_utf8->style & SF_CENTER) {
470 x = (label->size.w - (label->theme->w + 5 + space) -
471 text->w) / 2;
472 } else {
473 if (label->string_utf8->style & SF_CENTER_RIGHT) {
474 x = label->size.w - (label->theme->w + 5 + space) - text->w;
475 } else {
476 x = space;
477 }
478 }
479 /* WF_ICON_CENTER_RIGHT */
480 } else {
481 if (flags & WF_ICON_CENTER) {
482 /* text is blit on icon */
483 goto alone;
484 } else { /* WF_ICON_CENTER_LEFT */
485 if (label->string_utf8->style & SF_CENTER) {
486 x = space + label->theme->w + adj_size(5) + ((label->size.w -
487 (space +
488 label->theme->w + adj_size(5)) -
489 text->w) / 2);
490 } else {
491 if (label->string_utf8->style & SF_CENTER_RIGHT) {
492 x = label->size.w - text->w - space;
493 } else {
494 x = space + label->theme->w + adj_size(5);
495 }
496 }
497 } /* WF_ICON_CENTER_LEFT */
498 }
499 /* !WF_ICON_ABOVE_TEXT && !WF_ICON_UNDER_TEXT */
500 } else {
501 goto alone;
502 }
503 /* label->theme == Icon */
504 } else {
505 y = (label->size.h - text->h) / 2;
506
507 alone:
508 if (label->string_utf8->style & SF_CENTER) {
509 x = (label->size.w - text->w) / 2;
510 } else {
511 if (label->string_utf8->style & SF_CENTER_RIGHT) {
512 x = label->size.w - text->w - space;
513 } else {
514 x = space;
515 }
516 }
517 }
518
519 dst.x = label->size.x + x;
520 dst.y = label->size.y + y;
521
522 ret = alphablit(text, NULL, label->dst->surface, &dst, 255);
523 FREESURFACE(text);
524 }
525
527
528 return ret;
529}
530
531/**********************************************************************/
534int draw_label(struct widget *label, Sint16 start_x, Sint16 start_y)
535{
536 label->size.x = start_x;
537 label->size.y = start_y;
538
539 return redraw_label(label);
540}
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 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 map_rgba_details(details, color)
Definition graphics.h:306
#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