Freeciv-3.3
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;
162 SDL_Rect src;
164
165 get_smaller_surface_rect(icon, &src);
166 ptheme = create_surf((src.w + adj_size(4)) * 4, src.h + adj_size(4),
168
169 dest.x = adj_size(2);
170 dest.y = (ptheme->h - src.h) / 2;
171
172 /* normal */
173 alphablit(icon, &src, ptheme, &dest, 255);
174
175 /* selected */
176 dest.x += (src.w + adj_size(4));
177 alphablit(icon, &src, ptheme, &dest, 255);
178
179 /* draw selected frame */
181 dest.x - 1, dest.y - 1, src.w + 1, src.h + 1,
183
184 /* pressed */
185 dest.x += (src.w + adj_size(4));
186 alphablit(icon, &src, ptheme, &dest, 255);
187
188 /* draw selected frame */
190 dest.x - 1, dest.y - 1, src.w + 1, src.h + 1,
192 /* draw press frame */
194 dest.x - adj_size(2), dest.y - adj_size(2),
195 src.w + adj_size(3), src.h + adj_size(3),
197
198 /* disabled */
199 dest.x += (src.w + adj_size(4));
200 alphablit(icon, &src, ptheme, &dest, 255);
201 dest.w = src.w;
202 dest.h = src.h;
203
205
206 return ptheme;
207}
208
209/**********************************************************************/
213 struct gui_layer *pdest,
214 Uint32 flags)
215{
216 struct widget *icon_widget = widget_new();
217
218 icon_widget->theme = icon_theme;
219
223 icon_widget->mod = KMOD_NONE;
224 icon_widget->dst = pdest;
225
227 icon_widget->redraw = redraw_icon;
228
229 if (icon_theme) {
230 icon_widget->size.w = icon_theme->w / 4;
231 icon_widget->size.h = icon_theme->h;
232 }
233
234 return icon_widget;
235}
236
237/**********************************************************************/
240int draw_icon(struct widget *icon, Sint16 start_x, Sint16 start_y)
241{
242 icon->size.x = start_x;
243 icon->size.y = start_y;
244
245 if (get_wflags(icon) & WF_RESTORE_BACKGROUND) {
247 }
248
249 return draw_icon_from_theme(icon->theme, get_wstate(icon), icon->dst,
250 icon->size.x, icon->size.y);
251}
252
253/**********************************************************************/
269 struct gui_layer *pdest, Sint16 start_x, Sint16 start_y)
270{
271 SDL_Rect src, des = {.x = start_x, .y = start_y, .w = 0, .h = 0};
272
273 if (icon_theme == NULL) {
274 return -3;
275 }
276
277 src.x = 0 + (icon_theme->w / 4) * state;
278 src.y = 0;
279 src.w = icon_theme->w / 4;
280 src.h = icon_theme->h;
281
282 return alphablit(icon_theme, &src, pdest->surface, &des, 255);
283}
284
285/**********************************************************************/
299{
300 SDL_Rect src;
301
302 if (icon_theme == NULL) {
303 return NULL;
304 }
305
306 src.w = icon_theme->w / 4;
307 src.x = src.w * state;
308 src.y = 0;
309 src.h = icon_theme->h;
310
312}
313
314/* =================================================== */
315/* ===================== ICON2 ======================= */
316/* =================================================== */
317
318/**********************************************************************/
322 bool free_old_theme)
323{
324 if ((new_theme) && (icon_widget)) {
325 if (free_old_theme) {
326 FREESURFACE(icon_widget->theme);
327 }
328 icon_widget->theme = new_theme;
329 icon_widget->size.w = new_theme->w + adj_size(4);
330 icon_widget->size.h = new_theme->h + adj_size(4);
331 }
332}
333
334/**********************************************************************/
338 Uint32 flags)
339{
340 struct widget *icon_widget = widget_new();
341
342 icon_widget->theme = icon;
343
347 icon_widget->mod = KMOD_NONE;
348 icon_widget->dst = pdest;
349
351 icon_widget->redraw = redraw_icon2;
352
353 if (icon) {
354 icon_widget->size.w = icon->w + adj_size(4);
355 icon_widget->size.h = icon->h + adj_size(4);
356 }
357
358 return icon_widget;
359}
char * incite_cost
Definition comments.c:76
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:1350
#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:29
@ COLOR_THEME_WIDGET_DISABLED_TEXT
Definition themecolors.h:43
@ COLOR_THEME_CUSTOM_WIDGET_SELECTED_FRAME
Definition themecolors.h:27
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