Freeciv-3.3
Loading...
Searching...
No Matches
widget_core.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/* gui-sdl2 */
26#include "colors.h"
27#include "graphics.h"
28#include "mapview.h"
29#include "themespec.h"
30#include "widget.h"
31#include "widget_p.h"
32
33/**********************************************************************/
36void set_wstate(struct widget *pwidget, enum widget_state state)
37{
39 pwidget->state_types_flags |= state;
40}
41
42/**********************************************************************/
45void set_wtype(struct widget *pwidget, enum widget_type type)
46{
47 pwidget->state_types_flags &= ~TYPE_MASK;
48 pwidget->state_types_flags |= type;
49}
50
51/**********************************************************************/
54void set_wflag(struct widget *pwidget, enum widget_flag flag)
55{
56 (pwidget)->state_types_flags |= ((flag) & FLAG_MASK);
57}
58
59/**********************************************************************/
62void clear_wflag(struct widget *pwidget, enum widget_flag flag)
63{
64 (pwidget)->state_types_flags &= ~((flag) & FLAG_MASK);
65}
66
67/**********************************************************************/
70enum widget_state get_wstate(const struct widget *pwidget)
71{
72 return ((enum widget_state)(pwidget->state_types_flags & STATE_MASK));
73}
74
75/**********************************************************************/
78enum widget_type get_wtype(const struct widget *pwidget)
79{
80 return ((enum widget_type)(pwidget->state_types_flags & TYPE_MASK));
81}
82
83/**********************************************************************/
86enum widget_flag get_wflags(const struct widget *pwidget)
87{
88 return ((enum widget_flag)(pwidget->state_types_flags & FLAG_MASK));
89}
90
91/**********************************************************************/
94void widget_free(struct widget **pwidget)
95{
96 struct widget *gui = *pwidget;
97
99 FREEUTF8STR(gui->string_utf8);
100 }
102 FREEUTF8STR(gui->info_label);
103 }
104 if (get_wflags(gui) & WF_FREE_GFX) {
105 FREESURFACE(gui->gfx);
106 }
108 if (get_wtype(gui) == WT_CHECKBOX) {
109 FREESURFACE(gui->private_data.cbox->true_theme);
110 FREESURFACE(gui->private_data.cbox->false_theme);
111 } else {
112 FREESURFACE(gui->theme);
113 }
114 }
116 FREESURFACE(gui->theme2);
117 }
118 if (get_wflags(gui) & WF_FREE_DATA) {
119 FC_FREE(gui->data.ptr);
120 }
122 FC_FREE(gui->private_data.ptr);
123 }
124 if (NULL != gui->destroy) {
125 gui->destroy(gui);
126 }
127
128 FC_FREE(*pwidget);
129}
130
131/**********************************************************************/
134static void widget_core_set_area(struct widget *pwidget, SDL_Rect area)
135{
136 pwidget->area = area;
137}
138
139/**********************************************************************/
142static void widget_core_set_position(struct widget *pwidget, int x, int y)
143{
144 pwidget->size.x = x;
145 pwidget->size.y = y;
146}
147
148/**********************************************************************/
151static void widget_core_resize(struct widget *pwidget, int w, int h)
152{
153 pwidget->size.w = w;
154 pwidget->size.h = h;
155}
156
157/**********************************************************************/
160static int widget_core_redraw(struct widget *pwidget)
161{
162 if (!pwidget || (get_wflags(pwidget) & WF_HIDDEN)) {
163 return -1;
164 }
165
166 if (pwidget->gfx) {
167 widget_undraw(pwidget);
168 }
169
170 if (!pwidget->gfx && (get_wflags(pwidget) & WF_RESTORE_BACKGROUND)) {
172 }
173
174 return 0;
175}
176
177/**********************************************************************/
180static void widget_core_draw_frame(struct widget *pwidget)
181{
183}
184
185/**********************************************************************/
188static void widget_core_mark_dirty(struct widget *pwidget)
189{
190 SDL_Rect rect = {
191 pwidget->dst->dest_rect.x + pwidget->size.x,
192 pwidget->dst->dest_rect.y + pwidget->size.y,
193 pwidget->size.w,
194 pwidget->size.h
195 };
196
197 dirty_sdl_rect(&rect);
198}
199
200/**********************************************************************/
203static void widget_core_flush(struct widget *pwidget)
204{
205 SDL_Rect rect = {
206 pwidget->dst->dest_rect.x + pwidget->size.x,
207 pwidget->dst->dest_rect.y + pwidget->size.y,
208 pwidget->size.w,
209 pwidget->size.h
210 };
211
212 flush_rect(&rect, FALSE);
213}
214
215/**********************************************************************/
218static void widget_core_undraw(struct widget *pwidget)
219{
220 if (get_wflags(pwidget) & WF_RESTORE_BACKGROUND) {
221 if (pwidget->gfx) {
222 clear_surface(pwidget->dst->surface, &pwidget->size);
223 blit_entire_src(pwidget->gfx, pwidget->dst->surface,
224 pwidget->size.x, pwidget->size.y);
225 }
226 } else {
227 clear_surface(pwidget->dst->surface, &pwidget->size);
228 }
229}
230
231/**********************************************************************/
234static void widget_core_select(struct widget *pwidget)
235{
236 widget_redraw(pwidget);
237 widget_flush(pwidget);
238}
239
240/**********************************************************************/
243static void widget_core_unselect(struct widget *pwidget)
244{
245 widget_redraw(pwidget);
246 widget_flush(pwidget);
247}
248
249/**********************************************************************/
252struct widget *widget_new(void)
253{
254 struct widget *pwidget = fc_calloc(1, sizeof(struct widget));
255
258 pwidget->resize = widget_core_resize;
259 pwidget->redraw = widget_core_redraw;
262 pwidget->flush = widget_core_flush;
263 pwidget->undraw = widget_core_undraw;
264 pwidget->select = widget_core_select;
266
267 return pwidget;
268}
char * incite_cost
Definition comments.c:74
GType type
Definition repodlgs.c:1313
int blit_entire_src(SDL_Surface *psrc, SDL_Surface *pdest, Sint16 dest_x, Sint16 dest_y)
Definition graphics.c:417
int clear_surface(SDL_Surface *surf, SDL_Rect *dstrect)
Definition graphics.c:400
#define FREESURFACE(ptr)
Definition graphics.h:322
#define FREEUTF8STR(pstr)
Definition gui_string.h:93
void flush_rect(SDL_Rect *rect, bool force_flush)
Definition mapview.c:103
void dirty_sdl_rect(SDL_Rect *Rect)
Definition mapview.c:181
void refresh_widget_background(struct widget *pwidget)
Definition widget.c:1151
widget_state
Definition widget.h:95
#define draw_frame_inside_widget(pwidget)
Definition widget.h:315
static void widget_flush(struct widget *pwidget)
Definition widget.h:291
static void widget_undraw(struct widget *pwidget)
Definition widget.h:296
widget_flag
Definition widget.h:67
@ WF_FREE_GFX
Definition widget.h:70
@ WF_WIDGET_HAS_INFO_LABEL
Definition widget.h:88
@ WF_FREE_DATA
Definition widget.h:78
@ WF_FREE_PRIVATE_DATA
Definition widget.h:80
@ WF_FREE_THEME2
Definition widget.h:74
@ WF_FREE_STRING
Definition widget.h:76
@ WF_RESTORE_BACKGROUND
Definition widget.h:85
@ WF_HIDDEN
Definition widget.h:68
@ WF_FREE_THEME
Definition widget.h:72
static int widget_redraw(struct widget *pwidget)
Definition widget.h:276
widget_type
Definition widget.h:41
@ WT_CHECKBOX
Definition widget.h:58
static void widget_core_mark_dirty(struct widget *pwidget)
static int widget_core_redraw(struct widget *pwidget)
static void widget_core_unselect(struct widget *pwidget)
static void widget_core_select(struct widget *pwidget)
void clear_wflag(struct widget *pwidget, enum widget_flag flag)
Definition widget_core.c:62
void widget_free(struct widget **pwidget)
Definition widget_core.c:94
static void widget_core_set_position(struct widget *pwidget, int x, int y)
static void widget_core_resize(struct widget *pwidget, int w, int h)
static void widget_core_flush(struct widget *pwidget)
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
void set_wtype(struct widget *pwidget, enum widget_type type)
Definition widget_core.c:45
static void widget_core_set_area(struct widget *pwidget, SDL_Rect area)
enum widget_type get_wtype(const struct widget *pwidget)
Definition widget_core.c:78
struct widget * widget_new(void)
static void widget_core_draw_frame(struct widget *pwidget)
static void widget_core_undraw(struct widget *pwidget)
void set_wflag(struct widget *pwidget, enum widget_flag flag)
Definition widget_core.c:54
#define STATE_MASK
Definition widget_p.h:17
#define FLAG_MASK
Definition widget_p.h:19
#define TYPE_MASK
Definition widget_p.h:18
#define fc_calloc(n, esz)
Definition mem.h:38
#define FC_FREE(ptr)
Definition mem.h:41
static mpgui * gui
Definition mpgui_qt.cpp:52
struct sprite int int y
Definition sprite_g.h:31
struct sprite int x
Definition sprite_g.h:31
SDL_Surface * surface
Definition graphics.h:229
SDL_Rect dest_rect
Definition graphics.h:228
void(* unselect)(struct widget *pwidget)
Definition widget.h:168
void(* set_area)(struct widget *pwidget, SDL_Rect area)
Definition widget.h:159
void(* draw_frame)(struct widget *pwidget)
Definition widget.h:162
void(* set_position)(struct widget *pwidget, int x, int y)
Definition widget.h:160
void(* resize)(struct widget *pwidget, int w, int h)
Definition widget.h:161
void(* flush)(struct widget *pwidget)
Definition widget.h:165
int(* redraw)(struct widget *pwidget)
Definition widget.h:163
struct gui_layer * dst
Definition widget.h:116
void(* mark_dirty)(struct widget *pwidget)
Definition widget.h:164
Uint32 state_types_flags
Definition widget.h:143
SDL_Rect area
Definition widget.h:149
void(* undraw)(struct widget *pwidget)
Definition widget.h:166
SDL_Surface * gfx
Definition widget.h:120
void(* select)(struct widget *pwidget)
Definition widget.h:167
SDL_Rect size
Definition widget.h:145
#define FALSE
Definition support.h:47