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/* 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
37static int (*baseclass_redraw)(struct widget *pwidget);
38
39/**********************************************************************/
55{
56 SDL_Rect dest = { 0, 0, 0, 0 };
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
74 } else if (get_wstate(icon_button) == FC_WS_SELECTED) {
76 tmp_str.style |= TTF_STYLE_BOLD;
77 } else if (get_wstate(icon_button) == FC_WS_PRESSED) {
79 } else if (get_wstate(icon_button) == FC_WS_DISABLED) {
81 }
82
84 }
85
86 if (!text && !icon) {
87 return 1;
88 }
89
90 /* Create Button graphic */
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) {
101 ix = icon_button->size.w - icon->w - 5;
102 } else {
104 ix = (icon_button->size.w - icon->w) / 2;
105 } else {
106 ix = 5;
107 }
108 }
109
111 iy = 3;
112 y = 3 + icon->h + 3 + (icon_button->size.h -
113 (icon->h + 6) - text->h) / 2;
114 } else {
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
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) {
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 {
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
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/**********************************************************************/
229{
230 int ret;
231 SDL_Surface *icon;
233
234 ret = (*baseclass_redraw)(theme_icon_button);
235 if (ret != 0) {
236 return ret;
237 }
238
242
243 theme_icon_button->theme2 = icon;
244
246
249
250 return ret;
251}
252
253/**********************************************************************/
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;
296 pstr->style &= ~TTF_STYLE_BOLD;
297 } else {
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/**********************************************************************/
344 struct gui_layer *pdest,
345 utf8_str *pstr,
346 Uint32 flags)
347{
348 /* Extract a single icon */
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}
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