Freeciv-3.2
Loading...
Searching...
No Matches
widget_button.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
24/* gui-sdl3 */
25#include "colors.h"
26#include "graphics.h"
27#include "gui_tilespec.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/**********************************************************************/
51{
52 SDL_Rect dest = { 0, 0, 0, 0 };
54 SDL_Surface *button = NULL, *text = NULL, *icon = icon_button->theme2;
55 Uint16 ix, iy, x;
56 Uint16 y = 0; /* FIXME: possibly uninitialized */
57 int ret;
58
59 ret = (*baseclass_redraw)(icon_button);
60 if (ret != 0) {
61 return ret;
62 }
63
64 if (icon_button->string_utf8 != NULL) {
65 /* Make copy of string_utf8 */
66 tmp_str = *icon_button->string_utf8;
67
70 } else if (get_wstate(icon_button) == FC_WS_SELECTED) {
72 tmp_str.style |= TTF_STYLE_BOLD;
73 } else if (get_wstate(icon_button) == FC_WS_PRESSED) {
75 } else if (get_wstate(icon_button) == FC_WS_DISABLED) {
77 }
78
80 }
81
82 if (!text && !icon) {
83 return 1;
84 }
85
86 /* Create Button graphic */
88 icon_button->size.w, icon_button->size.h);
89
90 clear_surface(icon_button->dst->surface, &icon_button->size);
91 alphablit(button, NULL, icon_button->dst->surface, &icon_button->size, 255);
92 FREESURFACE(button);
93
94 if (icon) { /* Icon */
95 if (text) {
97 ix = icon_button->size.w - icon->w - 5;
98 } else {
100 ix = (icon_button->size.w - icon->w) / 2;
101 } else {
102 ix = 5;
103 }
104 }
105
107 iy = 3;
108 y = 3 + icon->h + 3 + (icon_button->size.h -
109 (icon->h + 6) - text->h) / 2;
110 } else {
112 y = 3 + (icon_button->size.h - (icon->h + 3) - text->h) / 2;
113 iy = y + text->h + 3;
114 } else { /* Center */
115 iy = (icon_button->size.h - icon->h) / 2;
116 y = (icon_button->size.h - text->h) / 2;
117 }
118 }
119 } else { /* No text */
120 iy = (icon_button->size.h - icon->h) / 2;
121 ix = (icon_button->size.w - icon->w) / 2;
122 }
123
125 ix += 1;
126 iy += 1;
127 }
128
129 dest.x = icon_button->size.x + ix;
130 dest.y = icon_button->size.y + iy;
131
132 ret = alphablit(icon, NULL, icon_button->dst->surface, &dest, 255);
133 if (ret) {
134 FREESURFACE(text);
135 return ret;
136 }
137 }
138
139 if (text) {
140 if (icon) {
144 if (icon_button->string_utf8->style & SF_CENTER) {
145 x = (icon_button->size.w - (icon->w + 5) - text->w) / 2;
146 } else {
147 if (icon_button->string_utf8->style & SF_CENTER_RIGHT) {
148 x = icon_button->size.w - (icon->w + 7) - text->w;
149 } else {
150 x = 5;
151 }
152 }
153 /* End WF_ICON_CENTER_RIGHT */
154 } else {
156 /* Text is blit on icon */
157 goto alone;
158 /* End WF_ICON_CENTER */
159 } else { /* Icon center left - default */
160 if (icon_button->string_utf8->style & SF_CENTER) {
161 x = 5 + icon->w + ((icon_button->size.w -
162 (icon->w + 5) - text->w) / 2);
163 } else {
164 if (icon_button->string_utf8->style & SF_CENTER_RIGHT) {
165 x = icon_button->size.w - text->w - 5;
166 } else { /* Text center left */
167 x = 5 + icon->w + 3;
168 }
169 }
170 } /* End icon center left - default */
171 }
172 /* 888888888888888888 */
173 } else {
174 goto alone;
175 }
176 } else {
177 /* !icon */
178 y = (icon_button->size.h - text->h) / 2;
179
180 alone:
181 if (icon_button->string_utf8->style & SF_CENTER) {
182 x = (icon_button->size.w - text->w) / 2;
183 } else {
184 if (icon_button->string_utf8->style & SF_CENTER_RIGHT) {
185 x = icon_button->size.w - text->w - 5;
186 } else {
187 x = 5;
188 }
189 }
190 }
191
193 x += 1;
194 } else {
195 y -= 1;
196 }
197
198 dest.x = icon_button->size.x + x;
199 dest.y = icon_button->size.y + y;
200
201 ret = alphablit(text, NULL, icon_button->dst->surface, &dest, 255);
202 }
203
204 FREESURFACE(text);
205
206 return ret;
207}
208
209/**********************************************************************/
225{
226 int ret;
227 SDL_Surface *icon;
229
230 ret = (*baseclass_redraw)(theme_icon_button);
231 if (ret != 0) {
232 return ret;
233 }
234
238
239 theme_icon_button->theme2 = icon;
240
242
245
246 return ret;
247}
248
249/**********************************************************************/
262 utf8_str *pstr, Uint32 flags)
263{
264 SDL_Rect buf = {0, 0, 0, 0};
265 int w = 0, h = 0;
266 struct widget *button;
267
268 if (!icon && !pstr) {
269 return NULL;
270 }
271
272 button = widget_new();
273
274 button->theme = current_theme->button;
275 button->theme2 = icon;
276 button->string_utf8 = pstr;
277 set_wflag(button, (WF_FREE_STRING | flags));
278 set_wstate(button, FC_WS_DISABLED);
279 set_wtype(button, WT_I_BUTTON);
280 button->mod = SDL_KMOD_NONE;
281 button->dst = pdest;
282
283 baseclass_redraw = button->redraw;
284 button->redraw = redraw_ibutton;
285
286 if (pstr) {
287 button->string_utf8->style |= SF_CENTER;
288 /* if BOLD == true then longest wight */
289 if (!(pstr->style & TTF_STYLE_BOLD)) {
290 pstr->style |= TTF_STYLE_BOLD;
292 pstr->style &= ~TTF_STYLE_BOLD;
293 } else {
295 }
296
297 w = MAX(w, buf.w);
298 h = MAX(h, buf.h);
299 }
300
301 if (icon) {
302 if (pstr) {
303 if ((flags & WF_ICON_UNDER_TEXT) || (flags & WF_ICON_ABOVE_TEXT)) {
304 w = MAX(w, icon->w + adj_size(2));
305 h = MAX(h, buf.h + icon->h + adj_size(4));
306 } else {
307 w = MAX(w, buf.w + icon->w + adj_size(20));
308 h = MAX(h, icon->h + adj_size(2));
309 }
310 } else {
311 w = MAX(w, icon->w + adj_size(2));
312 h = MAX(h, icon->h + adj_size(2));
313 }
314 } else {
315 w += adj_size(10);
316 h += adj_size(2);
317 }
318
319 correct_size_bcgnd_surf(current_theme->button, &w, &h);
320
321 button->size.w = w;
322 button->size.h = h;
323
324 return button;
325}
326
327/**********************************************************************/
340 struct gui_layer *pdest,
341 utf8_str *pstr,
342 Uint32 flags)
343{
344 /* Extract a single icon */
346 struct widget *button = create_icon_button(icon, pdest, pstr, flags);
347
348 FREESURFACE(button->theme2);
349 button->theme2 = icon_theme;
350 set_wtype(button, WT_TI_BUTTON);
351
352 button->redraw = redraw_tibutton;
353
354 return button;
355}
char * incite_cost
Definition comments.c:75
QString current_theme
Definition fc_client.cpp:65
SDL_Color * get_theme_color(enum theme_color themecolor)
Definition colors.c:47
int alphablit(SDL_Surface *src, SDL_Rect *srcrect, SDL_Surface *dst, SDL_Rect *dstrect, unsigned char alpha_mod)
Definition graphics.c:199
int clear_surface(SDL_Surface *surf, SDL_Rect *dstrect)
Definition graphics.c:400
#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 SF_CENTER
Definition gui_string.h:40
#define SF_CENTER_RIGHT
Definition gui_string.h:41
@ COLOR_THEME_WIDGET_NORMAL_TEXT
Definition themecolors.h:39
@ COLOR_THEME_WIDGET_DISABLED_TEXT
Definition themecolors.h:42
@ COLOR_THEME_WIDGET_SELECTED_TEXT
Definition themecolors.h:40
@ COLOR_THEME_WIDGET_PRESSED_TEXT
Definition themecolors.h:41
void correct_size_bcgnd_surf(SDL_Surface *ptheme, int *width, int *height)
Definition widget.c:63
SDL_Surface * create_bcgnd_surf(SDL_Surface *ptheme, enum widget_state state, Uint16 width, Uint16 height)
Definition widget.c:78
@ FC_WS_DISABLED
Definition widget.h:99
@ FC_WS_NORMAL
Definition widget.h:96
@ FC_WS_PRESSED
Definition widget.h:98
@ 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_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_ICON_UNDER_TEXT
Definition widget.h:82
void set_wtype(struct widget *pwidget, enum widget_type type)
Definition widget_core.c:45
void set_wflag(struct widget *pwidget, enum widget_flag flag)
Definition widget_core.c:54
@ WT_TI_BUTTON
Definition widget.h:46
@ WT_I_BUTTON
Definition widget.h:44
struct widget * create_themeicon_button(SDL_Surface *icon_theme, struct gui_layer *pdest, utf8_str *pstr, Uint32 flags)
static int redraw_tibutton(struct widget *theme_icon_button)
struct widget * create_icon_button(SDL_Surface *icon, struct gui_layer *pdest, utf8_str *pstr, Uint32 flags)
static int(* baseclass_redraw)(struct widget *pwidget)
static int redraw_ibutton(struct widget *icon_button)
struct widget * widget_new(void)
SDL_Surface * create_icon_from_theme(SDL_Surface *icon_theme, Uint8 state)
#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
Uint8 style
Definition gui_string.h:53
SDL_Surface * theme
Definition widget.h:118
SDL_Surface * theme2
Definition widget.h:119
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 size
Definition widget.h:145