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
248 if (icon == NULL && pstr == NULL) {
249 return NULL;
250 }
251
252 label = widget_new();
253 label->theme = icon;
254 label->string_utf8 = pstr;
257 set_wtype(label, WT_T2_LABEL);
258 label->mod = SDL_KMOD_NONE;
259 baseclass_redraw = label->redraw;
260 label->redraw = redraw_label;
261
262 remake_label_size(label);
263
264 label->size.w = MAX(label->size.w, w);
265 label->size.h = MAX(label->size.h, h);
266
267 ptheme = create_surf(label->size.w, label->size.h * 2, SDL_SWSURFACE);
268
269 colorkey = SDL_MapRGBA(ptheme->format, pstr->bgcol.r,
270 pstr->bgcol.g, pstr->bgcol.b, pstr->bgcol.a);
272
273 label->size.x = 0;
274 label->size.y = 0;
275 area = label->size;
276 label->dst = gui_layer_new(0, 0, ptheme);
277
278 /* Normal */
279 redraw_iconlabel(label);
280
281 /* Selected */
282 area.x = 0;
283 area.y = label->size.h;
284
285 if (flags & WF_RESTORE_BACKGROUND) {
287 map_rgba(ptheme->format, bg_color));
288 store = pstr->bgcol;
289 SDL_GetRGBA(getpixel(ptheme, area.x , area.y), ptheme->format,
290 &pstr->bgcol.r, &pstr->bgcol.g,
291 &pstr->bgcol.b, &pstr->bgcol.a);
292 } else {
294 }
295
296 label->size.y = label->size.h;
297 redraw_iconlabel(label);
298
299 if (flags & WF_RESTORE_BACKGROUND) {
300 pstr->bgcol = store;
301 }
302
303 label->size.x = 0;
304 label->size.y = 0;
305 if (flags & WF_FREE_THEME) {
306 FREESURFACE(label->theme);
307 }
308 label->theme = ptheme;
309 FC_FREE(label->dst);
310 label->dst = pdest;
311
312 return label;
313}
314
315/**********************************************************************/
319{
320 SDL_Rect start, area;
321 SDL_Color store = {0, 0, 0, 0};
326 icon_label->size.h * 2, SDL_SWSURFACE);
327
328 colorkey = SDL_MapRGBA(ptheme->format,
329 icon_label->string_utf8->bgcol.r,
330 icon_label->string_utf8->bgcol.g,
331 icon_label->string_utf8->bgcol.b,
332 icon_label->string_utf8->bgcol.a);
334
335 start = icon_label->size;
336 icon_label->size.x = 0;
337 icon_label->size.y = 0;
338 area = start;
339 pdest = icon_label->dst->surface;
340 icon_label->dst->surface = ptheme;
341
342 /* Normal */
344
345 /* Selected */
346 area.x = 0;
347 area.y = icon_label->size.h;
348
349 if (flags & WF_RESTORE_BACKGROUND) {
351 map_rgba(ptheme->format, bg_color));
352 store = icon_label->string_utf8->bgcol;
353 SDL_GetRGBA(getpixel(ptheme, area.x , area.y), ptheme->format,
354 &icon_label->string_utf8->bgcol.r,
355 &icon_label->string_utf8->bgcol.g,
356 &icon_label->string_utf8->bgcol.b,
357 &icon_label->string_utf8->bgcol.a);
358 } else {
360 }
361
362 icon_label->size.y = icon_label->size.h;
364
365 if (flags & WF_RESTORE_BACKGROUND) {
366 icon_label->string_utf8->bgcol = store;
367 }
368
369 icon_label->size = start;
370 if (flags & WF_FREE_THEME) {
371 FREESURFACE(icon_label->theme);
372 }
373 icon_label->theme = ptheme;
374 if (flags & WF_FREE_STRING) {
375 FREEUTF8STR(icon_label->string_utf8);
376 }
377 icon_label->dst->surface = pdest;
379
380 icon_label->redraw = redraw_label;
381
382 return icon_label;
383}
384
385#if 0
386/**********************************************************************/
389static int redraw_themelabel(struct widget *label)
390{
391 int ret;
392 Sint16 x, y;
393 SDL_Surface *text = NULL;
394
395 if (!label) {
396 return -3;
397 }
398
399 if ((text = create_text_surf_from_utf8(label->string_utf8)) == NULL) {
400 return (-4);
401 }
402
403 if (label->string_utf8->style & SF_CENTER) {
404 x = (label->size.w - text->w) / 2;
405 } else {
406 if (label->string_utf8->style & SF_CENTER_RIGHT) {
407 x = label->size.w - text->w - adj_size(5);
408 } else {
409 x = adj_size(5);
410 }
411 }
412
413 y = (label->size.h - text->h) / 2;
414
415 /* Redraw theme */
416 if (label->theme) {
417 ret = blit_entire_src(label->theme, label->dst->surface, label->size.x, label->size.y);
418 if (ret) {
419 return ret;
420 }
421 }
422
423 ret = blit_entire_src(text, label->dst->surface, label->size.x + x, label->size.y + y);
424
425 FREESURFACE(text);
426
427 return ret;
428}
429#endif /* 0 */
430
431/**********************************************************************/
434int redraw_iconlabel(struct widget *label)
435{
436 int space, ret = 0; /* FIXME: possibly uninitialized */
437 Sint16 x, xI, yI;
438 Sint16 y = 0; /* FIXME: possibly uninitialized */
439 SDL_Surface *text;
441 Uint32 flags;
442
443 if (label == NULL) {
444 return -3;
445 }
446
447 SDL_SetSurfaceClipRect(label->dst->surface, &label->size);
448
449 flags = get_wflags(label);
450
451 if (flags & WF_DRAW_TEXT_LABEL_WITH_SPACE) {
452 space = adj_size(5);
453 } else {
454 space = 0;
455 }
456
458
459 if (label->theme) { /* Icon */
460 if (text) {
461 if (flags & WF_ICON_CENTER_RIGHT) {
462 xI = label->size.w - label->theme->w - space;
463 } else {
464 if (flags & WF_ICON_CENTER) {
465 xI = (label->size.w - label->theme->w) / 2;
466 } else {
467 xI = space;
468 }
469 }
470
471 if (flags & WF_ICON_ABOVE_TEXT) {
472 yI = 0;
473 y = label->theme->h + adj_size(3)
474 + (label->size.h - (label->theme->h + adj_size(3)) - text->h) / 2;
475 } else {
476 if (flags & WF_ICON_UNDER_TEXT) {
477 y = (label->size.h - (label->theme->h + adj_size(3)) - text->h) / 2;
478 yI = y + text->h + adj_size(3);
479 } else {
480 yI = (label->size.h - label->theme->h) / 2;
481 y = (label->size.h - text->h) / 2;
482 }
483 }
484 /* Text */
485 } else {
486#if 0
487 yI = (label->size.h - label->theme->h) / 2;
488 xI = (label->size.w - label->theme->w) / 2;
489#endif /* 0 */
490 yI = 0;
491 xI = space;
492 }
493
494 dst.x = label->size.x + xI;
495 dst.y = label->size.y + yI;
496
497 ret = alphablit(label->theme, NULL, label->dst->surface, &dst, 255);
498
499 if (ret) {
500 return ret - 10;
501 }
502 }
503
504 if (text) {
505 if (label->theme) { /* Icon */
506 if (!(flags & WF_ICON_ABOVE_TEXT) && !(flags & WF_ICON_UNDER_TEXT)) {
507 if (flags & WF_ICON_CENTER_RIGHT) {
508 if (label->string_utf8->style & SF_CENTER) {
509 x = (label->size.w - (label->theme->w + 5 + space) -
510 text->w) / 2;
511 } else {
512 if (label->string_utf8->style & SF_CENTER_RIGHT) {
513 x = label->size.w - (label->theme->w + 5 + space) - text->w;
514 } else {
515 x = space;
516 }
517 }
518 /* WF_ICON_CENTER_RIGHT */
519 } else {
520 if (flags & WF_ICON_CENTER) {
521 /* text is blit on icon */
522 goto alone;
523 } else { /* WF_ICON_CENTER_LEFT */
524 if (label->string_utf8->style & SF_CENTER) {
525 x = space + label->theme->w + adj_size(5) + ((label->size.w -
526 (space +
527 label->theme->w + adj_size(5)) -
528 text->w) / 2);
529 } else {
530 if (label->string_utf8->style & SF_CENTER_RIGHT) {
531 x = label->size.w - text->w - space;
532 } else {
533 x = space + label->theme->w + adj_size(5);
534 }
535 }
536 } /* WF_ICON_CENTER_LEFT */
537 }
538 /* !WF_ICON_ABOVE_TEXT && !WF_ICON_UNDER_TEXT */
539 } else {
540 goto alone;
541 }
542 /* label->theme == Icon */
543 } else {
544 y = (label->size.h - text->h) / 2;
545
546 alone:
547 if (label->string_utf8->style & SF_CENTER) {
548 x = (label->size.w - text->w) / 2;
549 } else {
550 if (label->string_utf8->style & SF_CENTER_RIGHT) {
551 x = label->size.w - text->w - space;
552 } else {
553 x = space;
554 }
555 }
556 }
557
558 dst.x = label->size.x + x;
559 dst.y = label->size.y + y;
560
561 ret = alphablit(text, NULL, label->dst->surface, &dst, 255);
562 FREESURFACE(text);
563 }
564
566
567 return ret;
568}
569
570/**********************************************************************/
573int draw_label(struct widget *label, Sint16 start_x, Sint16 start_y)
574{
575 label->size.x = start_x;
576 label->size.y = start_y;
577
578 return redraw_label(label);
579}
static QString bold(QString text)
Definition citydlg.cpp:3853
char * incite_cost
Definition comments.c:74
SDL_Color * get_theme_color(enum theme_color themecolor)
Definition colors.c:47
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: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