Freeciv-3.3: /fast/freeciv/nightly/main/client/gui-sdl2/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/* 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
28/* gui-sdl2 */
29#include "colors.h"
30#include "graphics.h"
31#include "gui_tilespec.h"
32#include "themespec.h"
33
34#include "widget.h"
35#include "widget_p.h"
36
37 static int (*baseclass_redraw)(struct widget *pwidget);
38
39/**********************************************************************/
54 static int redraw_ibutton(struct widget *icon_button)
55{
56 SDL_Rect dest = { 0, 0, 0, 0 };
57 utf8_str tmp_str;
58 SDL_Surface *button = NULL, *text = NULL, *icon = icon_button->theme2;
59 Uint16 ix, iy, x;
60 Uint16 y = 0; /* FIXME: possibly uninitialized */
61 int ret;
62
63 ret = (*baseclass_redraw)(icon_button);
64 if (ret != 0) {
65 return ret;
66 }
67
68 if (icon_button->string_utf8 != NULL) {
69 /* Make copy of string_utf8 */
70 tmp_str = *icon_button->string_utf8;
71
72 if (get_wstate(icon_button) == FC_WS_NORMAL) {
73 tmp_str.fgcol = *get_theme_color(COLOR_THEME_WIDGET_NORMAL_TEXT);
74 } else if (get_wstate(icon_button) == FC_WS_SELECTED) {
75 tmp_str.fgcol = *get_theme_color(COLOR_THEME_WIDGET_SELECTED_TEXT);
76 tmp_str.style |= TTF_STYLE_BOLD;
77 } else if (get_wstate(icon_button) == FC_WS_PRESSED) {
78 tmp_str.fgcol = *get_theme_color(COLOR_THEME_WIDGET_PRESSED_TEXT);
79 } else if (get_wstate(icon_button) == FC_WS_DISABLED) {
80 tmp_str.fgcol = *get_theme_color(COLOR_THEME_WIDGET_DISABLED_TEXT);
81 }
82
83 text = create_text_surf_from_utf8(&tmp_str);
84 }
85
86 if (!text && !icon) {
87 return 1;
88 }
89
90 /* Create Button graphic */
91 button = create_bcgnd_surf(icon_button->theme, get_wstate(icon_button),
92 icon_button->size.w, icon_button->size.h);
93
94 clear_surface(icon_button->dst->surface, &icon_button->size);
95 alphablit(button, NULL, icon_button->dst->surface, &icon_button->size, 255);
96 FREESURFACE(button);
97
98 if (icon) { /* Icon */
99 if (text) {
100 if (get_wflags(icon_button) & WF_ICON_CENTER_RIGHT) {
101 ix = icon_button->size.w - icon->w - 5;
102 } else {
103 if (get_wflags(icon_button) & WF_ICON_CENTER) {
104 ix = (icon_button->size.w - icon->w) / 2;
105 } else {
106 ix = 5;
107 }
108 }
109
110 if (get_wflags(icon_button) & WF_ICON_ABOVE_TEXT) {
111 iy = 3;
112 y = 3 + icon->h + 3 + (icon_button->size.h -
113 (icon->h + 6) - text->h) / 2;
114 } else {
115 if (get_wflags(icon_button) & WF_ICON_UNDER_TEXT) {
116 y = 3 + (icon_button->size.h - (icon->h + 3) - text->h) / 2;
117 iy = y + text->h + 3;
118 } else { /* Center */
119 iy = (icon_button->size.h - icon->h) / 2;
120 y = (icon_button->size.h - text->h) / 2;
121 }
122 }
123 } else { /* No text */
124 iy = (icon_button->size.h - icon->h) / 2;
125 ix = (icon_button->size.w - icon->w) / 2;
126 }
127
128 if (get_wstate(icon_button) == FC_WS_PRESSED) {
129 ix += 1;
130 iy += 1;
131 }
132
133 dest.x = icon_button->size.x + ix;
134 dest.y = icon_button->size.y + iy;
135
136 ret = alphablit(icon, NULL, icon_button->dst->surface, &dest, 255);
137 if (ret) {
138 FREESURFACE(text);
139 return ret;
140 }
141 }
142
143 if (text) {
144 if (icon) {
145 if (!(get_wflags(icon_button) & WF_ICON_ABOVE_TEXT)
146 && !(get_wflags(icon_button) & WF_ICON_UNDER_TEXT)) {
147 if (get_wflags(icon_button) & WF_ICON_CENTER_RIGHT) {
148 if (icon_button->string_utf8->style & SF_CENTER) {
149 x = (icon_button->size.w - (icon->w + 5) - text->w) / 2;
150 } else {
151 if (icon_button->string_utf8->style & SF_CENTER_RIGHT) {
152 x = icon_button->size.w - (icon->w + 7) - text->w;
153 } else {
154 x = 5;
155 }
156 }
157 /* End WF_ICON_CENTER_RIGHT */
158 } else {
159 if (get_wflags(icon_button) & WF_ICON_CENTER) {
160 /* Text is blit on icon */
161 goto alone;
162 /* End WF_ICON_CENTER */
163 } else { /* Icon center left - default */
164 if (icon_button->string_utf8->style & SF_CENTER) {
165 x = 5 + icon->w + ((icon_button->size.w -
166 (icon->w + 5) - text->w) / 2);
167 } else {
168 if (icon_button->string_utf8->style & SF_CENTER_RIGHT) {
169 x = icon_button->size.w - text->w - 5;
170 } else { /* Text center left */
171 x = 5 + icon->w + 3;
172 }
173 }
174 } /* End icon center left - default */
175 }
176 /* 888888888888888888 */
177 } else {
178 goto alone;
179 }
180 } else {
181 /* !icon */
182 y = (icon_button->size.h - text->h) / 2;
183
184 alone:
185 if (icon_button->string_utf8->style & SF_CENTER) {
186 x = (icon_button->size.w - text->w) / 2;
187 } else {
188 if (icon_button->string_utf8->style & SF_CENTER_RIGHT) {
189 x = icon_button->size.w - text->w - 5;
190 } else {
191 x = 5;
192 }
193 }
194 }
195
196 if (get_wstate(icon_button) == FC_WS_PRESSED) {
197 x += 1;
198 } else {
199 y -= 1;
200 }
201
202 dest.x = icon_button->size.x + x;
203 dest.y = icon_button->size.y + y;
204
205 ret = alphablit(text, NULL, icon_button->dst->surface, &dest, 255);
206 }
207
208 FREESURFACE(text);
209
210 return ret;
211}
212
213/**********************************************************************/
228 static int redraw_tibutton(struct widget *theme_icon_button)
229{
230 int ret;
231 SDL_Surface *icon;
232 SDL_Surface *copy_of_icon_theme;
233
234 ret = (*baseclass_redraw)(theme_icon_button);
235 if (ret != 0) {
236 return ret;
237 }
238
239 icon = create_icon_from_theme(theme_icon_button->theme2,
240 get_wstate(theme_icon_button));
241 copy_of_icon_theme = theme_icon_button->theme2;
242
243 theme_icon_button->theme2 = icon;
244
245 ret = redraw_ibutton(theme_icon_button);
246
247 FREESURFACE(theme_icon_button->theme2);
248 theme_icon_button->theme2 = copy_of_icon_theme;
249
250 return ret;
251}
252
253/**********************************************************************/
265 struct widget *create_icon_button(SDL_Surface *icon, struct gui_layer *pdest,
266 utf8_str *pstr, Uint32 flags)
267{
268 SDL_Rect buf = {0, 0, 0, 0};
269 int w = 0, h = 0;
270 struct widget *button;
271
272 if (!icon && !pstr) {
273 return NULL;
274 }
275
276 button = widget_new();
277
278 button->theme = current_theme->button;
279 button->theme2 = icon;
280 button->string_utf8 = pstr;
281 set_wflag(button, (WF_FREE_STRING | flags));
282 set_wstate(button, FC_WS_DISABLED);
283 set_wtype(button, WT_I_BUTTON);
284 button->mod = KMOD_NONE;
285 button->dst = pdest;
286
287 baseclass_redraw = button->redraw;
288 button->redraw = redraw_ibutton;
289
290 if (pstr) {
291 button->string_utf8->style |= SF_CENTER;
292 /* if BOLD == true then longest wight */
293 if (!(pstr->style & TTF_STYLE_BOLD)) {
294 pstr->style |= TTF_STYLE_BOLD;
295 utf8_str_size(pstr, &buf);
296 pstr->style &= ~TTF_STYLE_BOLD;
297 } else {
298 utf8_str_size(pstr, &buf);
299 }
300
301 w = MAX(w, buf.w);
302 h = MAX(h, buf.h);
303 }
304
305 if (icon) {
306 if (pstr) {
307 if ((flags & WF_ICON_UNDER_TEXT) || (flags & WF_ICON_ABOVE_TEXT)) {
308 w = MAX(w, icon->w + adj_size(2));
309 h = MAX(h, buf.h + icon->h + adj_size(4));
310 } else {
311 w = MAX(w, buf.w + icon->w + adj_size(20));
312 h = MAX(h, icon->h + adj_size(2));
313 }
314 } else {
315 w = MAX(w, icon->w + adj_size(2));
316 h = MAX(h, icon->h + adj_size(2));
317 }
318 } else {
319 w += adj_size(10);
320 h += adj_size(2);
321 }
322
323 correct_size_bcgnd_surf(current_theme->button, &w, &h);
324
325 button->size.w = w;
326 button->size.h = h;
327
328 return button;
329}
330
331/**********************************************************************/
343 struct widget *create_themeicon_button(SDL_Surface *icon_theme,
344 struct gui_layer *pdest,
345 utf8_str *pstr,
346 Uint32 flags)
347{
348 /* Extract a single icon */
349 SDL_Surface *icon = create_icon_from_theme(icon_theme, 1);
350 struct widget *button = create_icon_button(icon, pdest, pstr, flags);
351
352 FREESURFACE(button->theme2);
353 button->theme2 = icon_theme;
354 set_wtype(button, WT_TI_BUTTON);
355
356 button->redraw = redraw_tibutton;
357
358 return button;
359}
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