JSON protocol: fix field_addr memory leak.
[freeciv.git] / client / gui-sdl2 / widget_core.c
blobc434ece4d355b9ed9801996fbc70f0ec0fec2be5
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.
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 ***********************************************************************/
14 #ifdef HAVE_CONFIG_H
15 #include <fc_config.h>
16 #endif
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 */
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"
33 /**************************************************************************
34 Set state of the widget.
35 **************************************************************************/
36 void set_wstate(struct widget *pWidget, enum widget_state state)
38 pWidget->state_types_flags &= ~STATE_MASK;
39 pWidget->state_types_flags |= state;
42 /**************************************************************************
43 Set type of the widget.
44 **************************************************************************/
45 void set_wtype(struct widget *pWidget, enum widget_type type)
47 pWidget->state_types_flags &= ~TYPE_MASK;
48 pWidget->state_types_flags |= type;
51 /**************************************************************************
52 Set flags of the widget.
53 **************************************************************************/
54 void set_wflag(struct widget *pWidget, enum widget_flag flag)
56 (pWidget)->state_types_flags |= ((flag) & FLAG_MASK);
59 /**************************************************************************
60 Clear flag from the widget.
61 **************************************************************************/
62 void clear_wflag(struct widget *pWidget, enum widget_flag flag)
64 (pWidget)->state_types_flags &= ~((flag) & FLAG_MASK);
67 /**************************************************************************
68 Get state of the widget.
69 **************************************************************************/
70 enum widget_state get_wstate(const struct widget *pWidget)
72 return ((enum widget_state)(pWidget->state_types_flags & STATE_MASK));
75 /**************************************************************************
76 Get type of the widget.
77 **************************************************************************/
78 enum widget_type get_wtype(const struct widget *pWidget)
80 return ((enum widget_type)(pWidget->state_types_flags & TYPE_MASK));
83 /**************************************************************************
84 Get all flags of the widget.
85 **************************************************************************/
86 enum widget_flag get_wflags(const struct widget *pWidget)
88 return ((enum widget_flag)(pWidget->state_types_flags & FLAG_MASK));
91 /**************************************************************************
92 Free resources allocated for the widget.
93 **************************************************************************/
94 void widget_free(struct widget **pWidget)
96 struct widget *pGUI = *pWidget;
98 if (get_wflags(pGUI) & WF_FREE_STRING) {
99 FREEUTF8STR(pGUI->string_utf8);
101 if (get_wflags(pGUI) & WF_WIDGET_HAS_INFO_LABEL) {
102 FREEUTF8STR(pGUI->info_label);
104 if (get_wflags(pGUI) & WF_FREE_GFX) {
105 FREESURFACE(pGUI->gfx);
107 if (get_wflags(pGUI) & WF_FREE_THEME) {
108 if (get_wtype(pGUI) == WT_CHECKBOX) {
109 FREESURFACE(pGUI->private_data.cbox->pTRUE_Theme);
110 FREESURFACE(pGUI->private_data.cbox->pFALSE_Theme);
111 } else {
112 FREESURFACE(pGUI->theme);
115 if (get_wflags(pGUI) & WF_FREE_THEME2) {
116 FREESURFACE(pGUI->theme2);
118 if (get_wflags(pGUI) & WF_FREE_DATA) {
119 FC_FREE(pGUI->data.ptr);
121 if (get_wflags(pGUI) & WF_FREE_PRIVATE_DATA) {
122 FC_FREE(pGUI->private_data.ptr);
124 if (NULL != pGUI->destroy) {
125 pGUI->destroy(pGUI);
128 FC_FREE(*pWidget);
131 /**************************************************************************
132 Set widget area.
133 **************************************************************************/
134 static void widget_core_set_area(struct widget *pwidget, SDL_Rect area)
136 pwidget->area = area;
139 /**************************************************************************
140 Set widget position.
141 **************************************************************************/
142 static void widget_core_set_position(struct widget *pwidget, int x, int y)
144 pwidget->size.x = x;
145 pwidget->size.y = y;
148 /**************************************************************************
149 Set widget size.
150 **************************************************************************/
151 static void widget_core_resize(struct widget *pwidget, int w, int h)
153 pwidget->size.w = w;
154 pwidget->size.h = h;
157 /**************************************************************************
158 Draw widget to the surface its on, if it's visible.
159 **************************************************************************/
160 static int widget_core_redraw(struct widget *pwidget)
162 if (!pwidget || (get_wflags(pwidget) & WF_HIDDEN)) {
163 return -1;
166 if (pwidget->gfx) {
167 widget_undraw(pwidget);
170 if (!pwidget->gfx && (get_wflags(pwidget) & WF_RESTORE_BACKGROUND)) {
171 refresh_widget_background(pwidget);
174 return 0;
177 /**************************************************************************
178 Draw frame of the widget.
179 **************************************************************************/
180 static void widget_core_draw_frame(struct widget *pwidget)
182 draw_frame_inside_widget(pwidget);
185 /**************************************************************************
186 Mark part of the display covered by the widget dirty.
187 **************************************************************************/
188 static void widget_core_mark_dirty(struct widget *pwidget)
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
197 dirty_sdl_rect(&rect);
200 /**************************************************************************
201 Flush part of the display covered by the widget.
202 **************************************************************************/
203 static void widget_core_flush(struct widget *pwidget)
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
212 flush_rect(&rect, FALSE);
215 /**************************************************************************
216 Clear widget from the display.
217 **************************************************************************/
218 static void widget_core_undraw(struct widget *pwidget)
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);
226 } else {
227 clear_surface(pwidget->dst->surface, &pwidget->size);
231 /**************************************************************************
232 Callback for when widget gets selected.
233 **************************************************************************/
234 static void widget_core_select(struct widget *pwidget)
236 widget_redraw(pwidget);
237 widget_flush(pwidget);
240 /**************************************************************************
241 Callback for when widget gets unselected.
242 **************************************************************************/
243 static void widget_core_unselect(struct widget *pwidget)
245 widget_redraw(pwidget);
246 widget_flush(pwidget);
249 /**************************************************************************
250 Create a new widget.
251 **************************************************************************/
252 struct widget *widget_new(void)
254 struct widget *pWidget = fc_calloc(1, sizeof(struct widget));
256 pWidget->set_area = widget_core_set_area;
257 pWidget->set_position = widget_core_set_position;
258 pWidget->resize = widget_core_resize;
259 pWidget->redraw = widget_core_redraw;
260 pWidget->draw_frame = widget_core_draw_frame;
261 pWidget->mark_dirty = widget_core_mark_dirty;
262 pWidget->flush = widget_core_flush;
263 pWidget->undraw = widget_core_undraw;
264 pWidget->select = widget_core_select;
265 pWidget->unselect = widget_core_unselect;
267 return pWidget;