Freeciv-3.3: /fast/freeciv/nightly/main/client/gui-sdl3/widget_button.c Source File

Freeciv-3.3

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
33 static int (*baseclass_redraw)(struct widget *pwidget);
34
35/**********************************************************************/
50 static int redraw_ibutton(struct widget *icon_button)
51{
52 SDL_Rect dest = { 0, 0, 0, 0 };
53 utf8_str tmp_str;
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
68 if (get_wstate(icon_button) == FC_WS_NORMAL) {
69 tmp_str.fgcol = *get_theme_color(COLOR_THEME_WIDGET_NORMAL_TEXT);
70 } else if (get_wstate(icon_button) == FC_WS_SELECTED) {
71 tmp_str.fgcol = *get_theme_color(COLOR_THEME_WIDGET_SELECTED_TEXT);
72 tmp_str.style |= TTF_STYLE_BOLD;
73 } else if (get_wstate(icon_button) == FC_WS_PRESSED) {
74 tmp_str.fgcol = *get_theme_color(COLOR_THEME_WIDGET_PRESSED_TEXT);
75 } else if (get_wstate(icon_button) == FC_WS_DISABLED) {
76 tmp_str.fgcol = *get_theme_color(COLOR_THEME_WIDGET_DISABLED_TEXT);
77 }
78
79 text = create_text_surf_from_utf8(&tmp_str);
80 }
81
82 if (!text && !icon) {
83 return 1;
84 }
85
86 /* Create Button graphic */
87 button = create_bcgnd_surf(icon_button->theme, get_wstate(icon_button),
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) {
96 if (get_wflags(icon_button) & WF_ICON_CENTER_RIGHT) {
97 ix = icon_button->size.w - icon->w - 5;
98 } else {
99 if (get_wflags(icon_button) & WF_ICON_CENTER) {
100 ix = (icon_button->size.w - icon->w) / 2;
101 } else {
102 ix = 5;
103 }
104 }
105
106 if (get_wflags(icon_button) & WF_ICON_ABOVE_TEXT) {
107 iy = 3;
108 y = 3 + icon->h + 3 + (icon_button->size.h -
109 (icon->h + 6) - text->h) / 2;
110 } else {
111 if (get_wflags(icon_button) & WF_ICON_UNDER_TEXT) {
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
124 if (get_wstate(icon_button) == FC_WS_PRESSED) {
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) {
141 if (!(get_wflags(icon_button) & WF_ICON_ABOVE_TEXT)
142 && !(get_wflags(icon_button) & WF_ICON_UNDER_TEXT)) {
143 if (get_wflags(icon_button) & WF_ICON_CENTER_RIGHT) {
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 {
155 if (get_wflags(icon_button) & WF_ICON_CENTER) {
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
192 if (get_wstate(icon_button) == FC_WS_PRESSED) {
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/**********************************************************************/
224 static int redraw_tibutton(struct widget *theme_icon_button)
225{
226 int ret;
227 SDL_Surface *icon;
228 SDL_Surface *copy_of_icon_theme;
229
230 ret = (*baseclass_redraw)(theme_icon_button);
231 if (ret != 0) {
232 return ret;
233 }
234
235 icon = create_icon_from_theme(theme_icon_button->theme2,
236 get_wstate(theme_icon_button));
237 copy_of_icon_theme = theme_icon_button->theme2;
238
239 theme_icon_button->theme2 = icon;
240
241 ret = redraw_ibutton(theme_icon_button);
242
243 FREESURFACE(theme_icon_button->theme2);
244 theme_icon_button->theme2 = copy_of_icon_theme;
245
246 return ret;
247}
248
249/**********************************************************************/
261 struct widget *create_icon_button(SDL_Surface *icon, struct gui_layer *pdest,
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;
291 utf8_str_size(pstr, &buf);
292 pstr->style &= ~TTF_STYLE_BOLD;
293 } else {
294 utf8_str_size(pstr, &buf);
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/**********************************************************************/
339 struct widget *create_themeicon_button(SDL_Surface *icon_theme,
340 struct gui_layer *pdest,
341 utf8_str *pstr,
342 Uint32 flags)
343{
344 /* Extract a single icon */
345 SDL_Surface *icon = create_icon_from_theme(icon_theme, 1);
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}
incite_cost
char * incite_cost
Definition comments.c:74
current_theme
QString current_theme
Definition fc_client.cpp:64
get_theme_color
SDL_Color * get_theme_color(enum theme_color themecolor)
Definition colors.c:47
alphablit
int alphablit(SDL_Surface *src, SDL_Rect *srcrect, SDL_Surface *dst, SDL_Rect *dstrect, unsigned char alpha_mod)
Definition graphics.c:199
clear_surface
int clear_surface(SDL_Surface *surf, SDL_Rect *dstrect)
Definition graphics.c:400
FREESURFACE
#define FREESURFACE(ptr)
Definition graphics.h:322
adj_size
#define adj_size(size)
Definition gui_main.h:141
utf8_str_size
void utf8_str_size(utf8_str *pstr, SDL_Rect *fill)
Definition gui_string.c:106
create_text_surf_from_utf8
SDL_Surface * create_text_surf_from_utf8(utf8_str *pstr)
Definition gui_string.c:425
SF_CENTER
#define SF_CENTER
Definition gui_string.h:40
SF_CENTER_RIGHT
#define SF_CENTER_RIGHT
Definition gui_string.h:41
COLOR_THEME_WIDGET_NORMAL_TEXT
@ COLOR_THEME_WIDGET_NORMAL_TEXT
Definition themecolors.h:40
COLOR_THEME_WIDGET_DISABLED_TEXT
@ COLOR_THEME_WIDGET_DISABLED_TEXT
Definition themecolors.h:43
COLOR_THEME_WIDGET_SELECTED_TEXT
@ COLOR_THEME_WIDGET_SELECTED_TEXT
Definition themecolors.h:41
COLOR_THEME_WIDGET_PRESSED_TEXT
@ COLOR_THEME_WIDGET_PRESSED_TEXT
Definition themecolors.h:42
correct_size_bcgnd_surf
void correct_size_bcgnd_surf(SDL_Surface *ptheme, int *width, int *height)
Definition widget.c:63
create_bcgnd_surf
SDL_Surface * create_bcgnd_surf(SDL_Surface *ptheme, enum widget_state state, Uint16 width, Uint16 height)
Definition widget.c:78
FC_WS_DISABLED
@ FC_WS_DISABLED
Definition widget.h:99
FC_WS_NORMAL
@ FC_WS_NORMAL
Definition widget.h:96
FC_WS_PRESSED
@ FC_WS_PRESSED
Definition widget.h:98
FC_WS_SELECTED
@ FC_WS_SELECTED
Definition widget.h:97
get_wflags
enum widget_flag get_wflags(const struct widget *pwidget)
Definition widget_core.c:86
set_wstate
void set_wstate(struct widget *pwidget, enum widget_state state)
Definition widget_core.c:36
get_wstate
enum widget_state get_wstate(const struct widget *pwidget)
Definition widget_core.c:70
WF_ICON_CENTER
@ WF_ICON_CENTER
Definition widget.h:83
WF_ICON_ABOVE_TEXT
@ WF_ICON_ABOVE_TEXT
Definition widget.h:81
WF_ICON_CENTER_RIGHT
@ WF_ICON_CENTER_RIGHT
Definition widget.h:84
WF_FREE_STRING
@ WF_FREE_STRING
Definition widget.h:76
WF_ICON_UNDER_TEXT
@ WF_ICON_UNDER_TEXT
Definition widget.h:82
set_wtype
void set_wtype(struct widget *pwidget, enum widget_type type)
Definition widget_core.c:45
set_wflag
void set_wflag(struct widget *pwidget, enum widget_flag flag)
Definition widget_core.c:54
WT_TI_BUTTON
@ WT_TI_BUTTON
Definition widget.h:46
WT_I_BUTTON
@ WT_I_BUTTON
Definition widget.h:44
create_themeicon_button
struct widget * create_themeicon_button(SDL_Surface *icon_theme, struct gui_layer *pdest, utf8_str *pstr, Uint32 flags)
Definition widget_button.c:343
redraw_tibutton
static int redraw_tibutton(struct widget *theme_icon_button)
Definition widget_button.c:228
create_icon_button
struct widget * create_icon_button(SDL_Surface *icon, struct gui_layer *pdest, utf8_str *pstr, Uint32 flags)
Definition widget_button.c:265
baseclass_redraw
static int(* baseclass_redraw)(struct widget *pwidget)
Definition widget_button.c:37
redraw_ibutton
static int redraw_ibutton(struct widget *icon_button)
Definition widget_button.c:54
widget_new
struct widget * widget_new(void)
Definition widget_core.c:252
create_icon_from_theme
SDL_Surface * create_icon_from_theme(SDL_Surface *icon_theme, Uint8 state)
Definition widget_icon.c:298
gui_tilespec.h
themespec.h
widget.h
widget_p.h
colors.h
graphics.h
log.h
MAX
#define MAX(x, y)
Definition shared.h:54
y
struct sprite int int y
Definition sprite_g.h:31
x
struct sprite int x
Definition sprite_g.h:31
gui_layer
Definition graphics.h:227
utf8_str
Definition gui_string.h:52
utf8_str::style
Uint8 style
Definition gui_string.h:53
widget
Definition widget.h:112
widget::theme
SDL_Surface * theme
Definition widget.h:118
widget::theme2
SDL_Surface * theme2
Definition widget.h:119
widget::redraw
int(* redraw)(struct widget *pwidget)
Definition widget.h:163
widget::dst
struct gui_layer * dst
Definition widget.h:116
widget::mod
Uint16 mod
Definition widget.h:154
widget::string_utf8
utf8_str * string_utf8
Definition widget.h:121
widget::size
SDL_Rect size
Definition widget.h:145