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));
163
164 dest.x = adj_size(2);
165 dest.y = (ptheme->h - src.h) / 2;
166
167 /* Normal */
168 alphablit(icon, &src, ptheme, &dest, 255);
169
170 /* Selected */
171 dest.x += (src.w + adj_size(4));
172 alphablit(icon, &src, ptheme, &dest, 255);
173
174 /* Draw selected frame */
176 dest.x - 1, dest.y - 1, src.w + 1, src.h + 1,
178
179 /* Pressed */
180 dest.x += (src.w + adj_size(4));
181 alphablit(icon, &src, ptheme, &dest, 255);
182
183 /* Draw selected frame */
185 dest.x - 1, dest.y - 1, src.w + 1, src.h + 1,
187 /* Draw press frame */
189 dest.x - adj_size(2), dest.y - adj_size(2),
190 src.w + adj_size(3), src.h + adj_size(3),
192
193 /* Disabled */
194 dest.x += (src.w + adj_size(4));
195 alphablit(icon, &src, ptheme, &dest, 255);
196 dest.w = src.w;
197 dest.h = src.h;
198
200
201 return ptheme;
202}
203
204/**********************************************************************/
208 struct gui_layer *pdest,
209 Uint32 flags)
210{
211 struct widget *icon_widget = widget_new();
212
213 icon_widget->theme = icon_theme;
214
219 icon_widget->dst = pdest;
220
222 icon_widget->redraw = redraw_icon;
223
224 if (icon_theme != NULL) {
225 icon_widget->size.w = icon_theme->w / 4;
226 icon_widget->size.h = icon_theme->h;
227 }
228
229 return icon_widget;
230}
231
232/**********************************************************************/
235int draw_icon(struct widget *icon, Sint16 start_x, Sint16 start_y)
236{
237 icon->size.x = start_x;
238 icon->size.y = start_y;
239
240 if (get_wflags(icon) & WF_RESTORE_BACKGROUND) {
242 }
243
244 return draw_icon_from_theme(icon->theme, get_wstate(icon), icon->dst,
245 icon->size.x, icon->size.y);
246}
247
248/**********************************************************************/
264 struct gui_layer *pdest, Sint16 start_x, Sint16 start_y)
265{
266 SDL_Rect src, des = {.x = start_x, .y = start_y, .w = 0, .h = 0};
267
268 if (icon_theme == NULL) {
269 return -3;
270 }
271
272 src.x = 0 + (icon_theme->w / 4) * state;
273 src.y = 0;
274 src.w = icon_theme->w / 4;
275 src.h = icon_theme->h;
276
277 return alphablit(icon_theme, &src, pdest->surface, &des, 255);
278}
279
280/**********************************************************************/
294{
295 SDL_Rect src;
296
297 if (icon_theme == NULL) {
298 return NULL;
299 }
300
301 src.w = icon_theme->w / 4;
302 src.x = src.w * state;
303 src.y = 0;
304 src.h = icon_theme->h;
305
307}
308
309/* =================================================== */
310/* ===================== ICON2 ======================= */
311/* =================================================== */
312
313/**********************************************************************/
317 bool free_old_theme)
318{
319 if ((new_theme) && (icon_widget)) {
320 if (free_old_theme) {
321 FREESURFACE(icon_widget->theme);
322 }
323 icon_widget->theme = new_theme;
324 icon_widget->size.w = new_theme->w + adj_size(4);
325 icon_widget->size.h = new_theme->h + adj_size(4);
326 }
327}
328
329/**********************************************************************/
333 Uint32 flags)
334{
335 struct widget *icon_widget = widget_new();
336
337 icon_widget->theme = icon;
338
343 icon_widget->dst = pdest;
344
346 icon_widget->redraw = redraw_icon2;
347
348 if (icon != NULL) {
349 icon_widget->size.w = icon->w + adj_size(4);
350 icon_widget->size.h = icon->h + adj_size(4);
351 }
352
353 return icon_widget;
354}
char * incite_cost
Definition comments.c:75
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