Freeciv-3.2
Loading...
Searching...
No Matches
widget_icon.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 "themespec.h"
28
29#include "widget.h"
30#include "widget_p.h"
31
32static int (*baseclass_redraw)(struct widget *pwidget);
33
34/* =================================================== */
35/* ===================== ICON ======================== */
36/* =================================================== */
37
38/**********************************************************************/
41static int redraw_icon(struct widget *icon)
42{
43 int ret;
44 SDL_Rect src, area = icon->size;
45
46 ret = (*baseclass_redraw)(icon);
47 if (ret != 0) {
48 return ret;
49 }
50
51 if (!icon->theme) {
52 return -3;
53 }
54
55 src.x = (icon->theme->w / 4) * (Uint8) (get_wstate(icon));
56 src.y = 0;
57 src.w = (icon->theme->w / 4);
58 src.h = icon->theme->h;
59
60 if (icon->size.w != src.w) {
61 area.x += (icon->size.w - src.w) / 2;
62 }
63
64 if (icon->size.h != src.h) {
65 area.y += (icon->size.h - src.h) / 2;
66 }
67
68 return alphablit(icon->theme, &src, icon->dst->surface, &area, 255);
69}
70
71/**********************************************************************/
74static int redraw_icon2(struct widget *icon)
75{
76 int ret;
77 SDL_Rect dest;
78
79 ret = (*baseclass_redraw)(icon);
80 if (ret != 0) {
81 return ret;
82 }
83
84 if (!icon) {
85 return -3;
86 }
87
88 if (!icon->theme) {
89 return -4;
90 }
91
92 dest.x = icon->size.x;
93 dest.y = icon->size.y;
94 dest.w = icon->theme->w;
95 dest.h = icon->theme->h;
96
97 switch (get_wstate(icon)) {
98 case FC_WS_SELECTED:
100 dest.x + 1, dest.y + 1,
101 dest.w + adj_size(2), dest.h + adj_size(2),
103 break;
104
105 case FC_WS_PRESSED:
106 create_frame(icon->dst->surface,
107 dest.x + 1, dest.y + 1,
108 dest.w + adj_size(2), dest.h + adj_size(2),
110
111 create_frame(icon->dst->surface,
112 dest.x, dest.y,
113 dest.w + adj_size(3), dest.h + adj_size(3),
115 break;
116
117 case FC_WS_DISABLED:
118 create_frame(icon->dst->surface,
119 dest.x + 1, dest.y + 1,
120 dest.w + adj_size(2), dest.h + adj_size(2),
122 break;
123 case FC_WS_NORMAL:
124 /* No frame by default */
125 break;
126 }
127
128 dest.x += adj_size(2);
129 dest.y += adj_size(2);
130 ret = alphablit(icon->theme, NULL, icon->dst->surface, &dest, 255);
131 if (ret) {
132 return ret;
133 }
134
135 return 0;
136}
137
138/**********************************************************************/
142{
143 if ((new_theme) && (icon_widget)) {
144 FREESURFACE(icon_widget->theme);
145 icon_widget->theme = new_theme;
146 icon_widget->size.w = new_theme->w / 4;
147 icon_widget->size.h = new_theme->h;
148 }
149}
150
151/**********************************************************************/
155{
156 SDL_Color bg_color = { 255, 255, 255, 128 };
157 SDL_Rect dest;
158 SDL_Rect src;
160
161 get_smaller_surface_rect(icon, &src);
162 ptheme = create_surf((src.w + adj_size(4)) * 4, src.h + adj_size(4),
164
165 dest.x = adj_size(2);
166 dest.y = (ptheme->h - src.h) / 2;
167
168 /* normal */
169 alphablit(icon, &src, ptheme, &dest, 255);
170
171 /* selected */
172 dest.x += (src.w + adj_size(4));
173 alphablit(icon, &src, ptheme, &dest, 255);
174
175 /* draw selected frame */
177 dest.x - 1, dest.y - 1, src.w + 1, src.h + 1,
179
180 /* pressed */
181 dest.x += (src.w + adj_size(4));
182 alphablit(icon, &src, ptheme, &dest, 255);
183
184 /* draw selected frame */
186 dest.x - 1, dest.y - 1, src.w + 1, src.h + 1,
188 /* draw press frame */
190 dest.x - adj_size(2), dest.y - adj_size(2),
191 src.w + adj_size(3), src.h + adj_size(3),
193
194 /* disabled */
195 dest.x += (src.w + adj_size(4));
196 alphablit(icon, &src, ptheme, &dest, 255);
197 dest.w = src.w;
198 dest.h = src.h;
199
201
202 return ptheme;
203}
204
205/**********************************************************************/
209 struct gui_layer *pdest,
210 Uint32 flags)
211{
212 struct widget *icon_widget = widget_new();
213
214 icon_widget->theme = icon_theme;
215
220 icon_widget->dst = pdest;
221
223 icon_widget->redraw = redraw_icon;
224
225 if (icon_theme != NULL) {
226 icon_widget->size.w = icon_theme->w / 4;
227 icon_widget->size.h = icon_theme->h;
228 }
229
230 return icon_widget;
231}
232
233/**********************************************************************/
236int draw_icon(struct widget *icon, Sint16 start_x, Sint16 start_y)
237{
238 icon->size.x = start_x;
239 icon->size.y = start_y;
240
241 if (get_wflags(icon) & WF_RESTORE_BACKGROUND) {
243 }
244
245 return draw_icon_from_theme(icon->theme, get_wstate(icon), icon->dst,
246 icon->size.x, icon->size.y);
247}
248
249/**********************************************************************/
265 struct gui_layer *pdest, Sint16 start_x, Sint16 start_y)
266{
267 SDL_Rect src, des = {.x = start_x, .y = start_y, .w = 0, .h = 0};
268
269 if (icon_theme == NULL) {
270 return -3;
271 }
272
273 src.x = 0 + (icon_theme->w / 4) * state;
274 src.y = 0;
275 src.w = icon_theme->w / 4;
276 src.h = icon_theme->h;
277
278 return alphablit(icon_theme, &src, pdest->surface, &des, 255);
279}
280
281/**********************************************************************/
295{
296 SDL_Rect src;
297
298 if (icon_theme == NULL) {
299 return NULL;
300 }
301
302 src.w = icon_theme->w / 4;
303 src.x = src.w * state;
304 src.y = 0;
305 src.h = icon_theme->h;
306
308}
309
310/* =================================================== */
311/* ===================== ICON2 ======================= */
312/* =================================================== */
313
314/**********************************************************************/
318 bool free_old_theme)
319{
320 if ((new_theme) && (icon_widget)) {
321 if (free_old_theme) {
322 FREESURFACE(icon_widget->theme);
323 }
324 icon_widget->theme = new_theme;
325 icon_widget->size.w = new_theme->w + adj_size(4);
326 icon_widget->size.h = new_theme->h + adj_size(4);
327 }
328}
329
330/**********************************************************************/
334 Uint32 flags)
335{
336 struct widget *icon_widget = widget_new();
337
338 icon_widget->theme = icon;
339
344 icon_widget->dst = pdest;
345
347 icon_widget->redraw = redraw_icon2;
348
349 if (icon != NULL) {
350 icon_widget->size.w = icon->w + adj_size(4);
351 icon_widget->size.h = icon->h + adj_size(4);
352 }
353
354 return icon_widget;
355}
char * incite_cost
Definition comments.c:74
SDL_Color * get_theme_color(enum theme_color themecolor)
Definition colors.c:47
int fill_rect_alpha(SDL_Surface *surf, SDL_Rect *prect, SDL_Color *pcolor)
Definition graphics.c:865
SDL_Surface * crop_rect_from_surface(SDL_Surface *psource, SDL_Rect *prect)
Definition graphics.c:236
int alphablit(SDL_Surface *src, SDL_Rect *srcrect, SDL_Surface *dst, SDL_Rect *dstrect, unsigned char alpha_mod)
Definition graphics.c:199
SDL_Surface * create_surf(int width, int height, Uint32 flags)
Definition graphics.c:351
void get_smaller_surface_rect(SDL_Surface *surf, SDL_Rect *rect)
Definition graphics.c:936
void create_frame(SDL_Surface *dest, Sint16 left, Sint16 top, Sint16 width, Sint16 height, SDL_Color *pcolor)
Definition graphics.c:1348
#define FREESURFACE(ptr)
Definition graphics.h:322
#define adj_size(size)
Definition gui_main.h:141
@ COLOR_THEME_CUSTOM_WIDGET_PRESSED_FRAME
Definition themecolors.h:28
@ COLOR_THEME_WIDGET_DISABLED_TEXT
Definition themecolors.h:42
@ COLOR_THEME_CUSTOM_WIDGET_SELECTED_FRAME
Definition themecolors.h:26
void refresh_widget_background(struct widget *pwidget)
Definition widget.c:1151
@ 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_FREE_GFX
Definition widget.h:70
@ WF_FREE_STRING
Definition widget.h:76
@ WF_RESTORE_BACKGROUND
Definition widget.h:85
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_ICON2
Definition widget.h:60
@ WT_ICON
Definition widget.h:49
struct widget * widget_new(void)
struct widget * create_themeicon(SDL_Surface *icon_theme, struct gui_layer *pdest, Uint32 flags)
struct widget * create_icon2(SDL_Surface *icon, struct gui_layer *pdest, Uint32 flags)
void set_new_icon2_theme(struct widget *icon_widget, SDL_Surface *new_theme, bool free_old_theme)
SDL_Surface * create_icon_theme_surf(SDL_Surface *icon)
static int(* baseclass_redraw)(struct widget *pwidget)
Definition widget_icon.c:36
SDL_Surface * create_icon_from_theme(SDL_Surface *icon_theme, Uint8 state)
int draw_icon_from_theme(SDL_Surface *icon_theme, Uint8 state, struct gui_layer *pdest, Sint16 start_x, Sint16 start_y)
static int redraw_icon(struct widget *icon)
Definition widget_icon.c:45
void set_new_icon_theme(struct widget *icon_widget, SDL_Surface *new_theme)
static int redraw_icon2(struct widget *icon)
Definition widget_icon.c:78
int draw_icon(struct widget *icon, Sint16 start_x, Sint16 start_y)
SDL_Surface * surface
Definition graphics.h:229
SDL_Surface * theme
Definition widget.h:118
struct gui_layer * dst
Definition widget.h:116
SDL_Rect area
Definition widget.h:149
SDL_Rect size
Definition widget.h:145