Freeciv-3.1
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/* 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 "themespec.h"
32
33#include "widget.h"
34#include "widget_p.h"
35
36static int (*baseclass_redraw)(struct widget *pwidget);
37
38/* =================================================== */
39/* ===================== ICON ======================== */
40/* =================================================== */
41
42/**********************************************************************/
45static int redraw_icon(struct widget *icon)
46{
47 int ret;
48 SDL_Rect src, area = icon->size;
49
50 ret = (*baseclass_redraw)(icon);
51 if (ret != 0) {
52 return ret;
53 }
54
55 if (!icon->theme) {
56 return -3;
57 }
58
59 src.x = (icon->theme->w / 4) * (Uint8) (get_wstate(icon));
60 src.y = 0;
61 src.w = (icon->theme->w / 4);
62 src.h = icon->theme->h;
63
64 if (icon->size.w != src.w) {
65 area.x += (icon->size.w - src.w) / 2;
66 }
67
68 if (icon->size.h != src.h) {
69 area.y += (icon->size.h - src.h) / 2;
70 }
71
72 return alphablit(icon->theme, &src, icon->dst->surface, &area, 255);
73}
74
75/**********************************************************************/
78static int redraw_icon2(struct widget *icon)
79{
80 int ret;
81 SDL_Rect dest;
82
83 ret = (*baseclass_redraw)(icon);
84 if (ret != 0) {
85 return ret;
86 }
87
88 if (!icon) {
89 return -3;
90 }
91
92 if (!icon->theme) {
93 return -4;
94 }
95
96 dest.x = icon->size.x;
97 dest.y = icon->size.y;
98 dest.w = icon->theme->w;
99 dest.h = icon->theme->h;
100
101 switch (get_wstate(icon)) {
102 case FC_WS_SELECTED:
103 create_frame(icon->dst->surface,
104 dest.x + 1, dest.y + 1,
105 dest.w + adj_size(2), dest.h + adj_size(2),
107 break;
108
109 case FC_WS_PRESSED:
110 create_frame(icon->dst->surface,
111 dest.x + 1, dest.y + 1,
112 dest.w + adj_size(2), dest.h + adj_size(2),
114
115 create_frame(icon->dst->surface,
116 dest.x, dest.y,
117 dest.w + adj_size(3), dest.h + adj_size(3),
119 break;
120
121 case FC_WS_DISABLED:
122 create_frame(icon->dst->surface,
123 dest.x + 1, dest.y + 1,
124 dest.w + adj_size(2), dest.h + adj_size(2),
126 break;
127 case FC_WS_NORMAL:
128 /* No frame by default */
129 break;
130 }
131
132 dest.x += adj_size(2);
133 dest.y += adj_size(2);
134 ret = alphablit(icon->theme, NULL, icon->dst->surface, &dest, 255);
135 if (ret) {
136 return ret;
137 }
138
139 return 0;
140}
141
142/**********************************************************************/
146{
147 if ((new_theme) && (icon_widget)) {
148 FREESURFACE(icon_widget->theme);
149 icon_widget->theme = new_theme;
150 icon_widget->size.w = new_theme->w / 4;
151 icon_widget->size.h = new_theme->h;
152 }
153}
154
155/**********************************************************************/
159{
160 SDL_Color bg_color = { 255, 255, 255, 128 };
161 SDL_Rect dest, src = get_smaller_surface_rect(icon);
162 SDL_Surface *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
219 icon_widget->mod = KMOD_NONE;
220 icon_widget->dst = pdest;
221
223 icon_widget->redraw = redraw_icon;
224
225 if (icon_theme) {
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 = {start_x, start_y, 0, 0};
268
269 if (!icon_theme) {
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) {
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
343 icon_widget->mod = KMOD_NONE;
344 icon_widget->dst = pdest;
345
347 icon_widget->redraw = redraw_icon2;
348
349 if (icon) {
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}
SDL_Color * get_theme_color(enum theme_color themecolor)
Definition colors.c:44
SDL_Rect get_smaller_surface_rect(SDL_Surface *surf)
Definition graphics.c:936
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 create_frame(SDL_Surface *dest, Sint16 left, Sint16 top, Sint16 width, Sint16 height, SDL_Color *pcolor)
Definition graphics.c:1347
#define FREESURFACE(ptr)
Definition graphics.h:322
#define adj_size(size)
Definition gui_main.h:138
SDL_Surface * surface
Definition graphics.h:229
struct gui_layer * dst
Definition widget.h:116
struct container * cont
Definition widget.h:127
SDL_Surface * theme
Definition widget.h:118
SDL_Rect area
Definition widget.h:149
SDL_Rect size
Definition widget.h:145
@ 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:1150
@ 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)