Freeciv-3.1: /fast/freeciv/nightly/S3_1/client/gui-sdl2/widget_checkbox.c Source File

Freeciv-3.1

Loading...
Searching...
No Matches
widget_checkbox.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 "gui_tilespec.h"
32#include "themespec.h"
33
34#include "widget.h"
35#include "widget_p.h"
36
37 static int (*checkbox_baseclass_redraw)(struct widget *pwidget);
38 static int (*textcheckbox_baseclass_redraw)(struct widget *pwidget);
39
40/**********************************************************************/
43 static int redraw_checkbox(struct widget *icon)
44{
45 int ret;
46 SDL_Rect src, area = icon->size;
47
48 ret = (*checkbox_baseclass_redraw)(icon);
49 if (ret != 0) {
50 return ret;
51 }
52
53 if (!icon->theme) {
54 return -3;
55 }
56
57 src.x = (icon->theme->w / 4) * (Uint8) (get_wstate(icon));
58 src.y = 0;
59 src.w = (icon->theme->w / 4);
60 src.h = icon->theme->h;
61
62 if (icon->size.w != src.w) {
63 area.x += (icon->size.w - src.w) / 2;
64 }
65
66 if (icon->size.h != src.h) {
67 area.y += (icon->size.h - src.h) / 2;
68 }
69
70 return alphablit(icon->theme, &src, icon->dst->surface, &area, 255);
71}
72
73/**********************************************************************/
78 static int redraw_textcheckbox(struct widget *cbox)
79{
80 int ret;
81 SDL_Surface *theme_surface, *icon;
82
83 ret = (*textcheckbox_baseclass_redraw)(cbox);
84 if (ret != 0) {
85 return ret;
86 }
87
88 if (cbox->string_utf8 == NULL) {
89 return widget_redraw(cbox);
90 }
91
92 theme_surface = cbox->theme;
93 icon = create_icon_from_theme(theme_surface, get_wstate(cbox));
94
95 if (!icon) {
96 return -3; /* FIXME: What SDL_Error this maps to, and should it map to none?
97 * Maybe it should be a positive value (SDL_Errors have
98 * negative values space) */
99 }
100
101 cbox->theme = icon;
102
103 /* redraw icon label */
104 ret = redraw_iconlabel(cbox);
105
106 FREESURFACE(icon);
107 cbox->theme = theme_surface;
108
109 return ret;
110}
111
112/**********************************************************************/
115 struct widget *create_checkbox(struct gui_layer *pdest, bool state,
116 Uint32 flags)
117{
118 struct widget *cbox = widget_new();
119 struct checkbox *ptmp = fc_calloc(1, sizeof(struct checkbox));
120
121 if (state) {
122 cbox->theme = current_theme->cbox_sell_icon;
123 } else {
124 cbox->theme = current_theme->cbox_unsell_icon;
125 }
126
127 set_wflag(cbox, (WF_FREE_STRING | WF_FREE_GFX | WF_FREE_PRIVATE_DATA | flags));
128 set_wstate(cbox, FC_WS_DISABLED);
129 set_wtype(cbox, WT_CHECKBOX);
130 cbox->mod = KMOD_NONE;
131 cbox->dst = pdest;
132 ptmp->state = state;
133 ptmp->true_theme = current_theme->cbox_sell_icon;
134 ptmp->false_theme = current_theme->cbox_unsell_icon;
135 cbox->private_data.cbox = ptmp;
136
137 checkbox_baseclass_redraw = cbox->redraw;
138 cbox->redraw = redraw_checkbox;
139
140 cbox->size.w = cbox->theme->w / 4;
141 cbox->size.h = cbox->theme->h;
142
143 return cbox;
144}
145
146/**********************************************************************/
149 struct widget *create_textcheckbox(struct gui_layer *pdest, bool state,
150 utf8_str *pstr, Uint32 flags)
151{
152 struct widget *cbox;
153 struct checkbox *ptmp;
154 SDL_Surface *surf, *icon;
155 struct widget *tmp_widget;
156
157 if (pstr == NULL) {
158 return create_checkbox(pdest, state, flags);
159 }
160
161 ptmp = fc_calloc(1, sizeof(struct checkbox));
162
163 if (state) {
164 surf = current_theme->cbox_sell_icon;
165 } else {
166 surf = current_theme->cbox_unsell_icon;
167 }
168
169 icon = create_icon_from_theme(surf, 0);
170 cbox = create_iconlabel(icon, pdest, pstr, (flags | WF_FREE_PRIVATE_DATA));
171
172 pstr->style &= ~SF_CENTER;
173
174 cbox->theme = surf;
175 FREESURFACE(icon);
176
177 set_wtype(cbox, WT_TCHECKBOX);
178 ptmp->state = state;
179 ptmp->true_theme = current_theme->cbox_sell_icon;
180 ptmp->false_theme = current_theme->cbox_unsell_icon;
181 cbox->private_data.cbox = ptmp;
182
183 tmp_widget = widget_new();
184 /* We can't use cbox->redraw here, because it is of type iconlabel */
185 textcheckbox_baseclass_redraw = tmp_widget->redraw;
186 FREEWIDGET(tmp_widget);
187 cbox->redraw = redraw_textcheckbox;
188
189 return cbox;
190}
191
192/**********************************************************************/
195 int set_new_checkbox_theme(struct widget *cbox,
196 SDL_Surface *true_surf, SDL_Surface *false_surf)
197{
198 struct checkbox *ptmp;
199
200 if (!cbox || (get_wtype(cbox) != WT_CHECKBOX)) {
201 return -1;
202 }
203
204 if (!cbox->private_data.cbox) {
205 cbox->private_data.cbox = fc_calloc(1, sizeof(struct checkbox));
206 set_wflag(cbox, WF_FREE_PRIVATE_DATA);
207 cbox->private_data.cbox->state = FALSE;
208 }
209
210 ptmp = cbox->private_data.cbox;
211 ptmp->true_theme = true_surf;
212 ptmp->false_theme = false_surf;
213 if (ptmp->state) {
214 cbox->theme = true_surf;
215 } else {
216 cbox->theme = false_surf;
217 }
218
219 return 0;
220}
221
222/**********************************************************************/
225 void toggle_checkbox(struct widget *cbox)
226{
227 if (cbox->private_data.cbox->state) {
228 cbox->theme = cbox->private_data.cbox->false_theme;
229 cbox->private_data.cbox->state = FALSE;
230 } else {
231 cbox->theme = cbox->private_data.cbox->true_theme;
232 cbox->private_data.cbox->state = TRUE;
233 }
234}
235
236/**********************************************************************/
239 bool get_checkbox_state(struct widget *cbox)
240{
241 return cbox->private_data.cbox->state;
242}
current_theme
QString current_theme
Definition fc_client.cpp:65
alphablit
int alphablit(SDL_Surface *src, SDL_Rect *srcrect, SDL_Surface *dst, SDL_Rect *dstrect, unsigned char alpha_mod)
Definition graphics.c:199
FREESURFACE
#define FREESURFACE(ptr)
Definition graphics.h:322
colors.h
graphics.h
gui_tilespec.h
log.h
fc_calloc
#define fc_calloc(n, esz)
Definition mem.h:38
checkbox
Definition widget_checkbox.h:17
checkbox::true_theme
SDL_Surface * true_theme
Definition widget_checkbox.h:18
checkbox::state
bool state
Definition widget_checkbox.h:20
checkbox::false_theme
SDL_Surface * false_theme
Definition widget_checkbox.h:19
gui_layer
Definition graphics.h:227
gui_layer::surface
SDL_Surface * surface
Definition graphics.h:229
utf8_str
Definition gui_string.h:52
widget
Definition widget.h:112
widget::cbox
struct checkbox * cbox
Definition widget.h:137
widget::dst
struct gui_layer * dst
Definition widget.h:116
widget::redraw
int(* redraw)(struct widget *pwidget)
Definition widget.h:163
widget::cont
struct container * cont
Definition widget.h:127
widget::mod
Uint16 mod
Definition widget.h:154
widget::private_data
union widget::@215 private_data
widget::theme
SDL_Surface * theme
Definition widget.h:118
widget::area
SDL_Rect area
Definition widget.h:149
widget::size
SDL_Rect size
Definition widget.h:145
TRUE
#define TRUE
Definition support.h:46
FALSE
#define FALSE
Definition support.h:47
themespec.h
widget.h
FC_WS_DISABLED
@ FC_WS_DISABLED
Definition widget.h:99
FREEWIDGET
#define FREEWIDGET(pwidget)
Definition widget.h:305
set_wstate
void set_wstate(struct widget *pwidget, enum widget_state state)
Definition widget_core.c:36
get_wstate
enum widget_state get_wstate(const struct widget *pwidget)
Definition widget_core.c:70
WF_FREE_GFX
@ WF_FREE_GFX
Definition widget.h:70
WF_FREE_PRIVATE_DATA
@ WF_FREE_PRIVATE_DATA
Definition widget.h:80
WF_FREE_STRING
@ WF_FREE_STRING
Definition widget.h:76
widget_redraw
static int widget_redraw(struct widget *pwidget)
Definition widget.h:276
set_wtype
void set_wtype(struct widget *pwidget, enum widget_type type)
Definition widget_core.c:45
get_wtype
enum widget_type get_wtype(const struct widget *pwidget)
Definition widget_core.c:78
set_wflag
void set_wflag(struct widget *pwidget, enum widget_flag flag)
Definition widget_core.c:54
WT_TCHECKBOX
@ WT_TCHECKBOX
Definition widget.h:59
WT_CHECKBOX
@ WT_CHECKBOX
Definition widget.h:58
toggle_checkbox
void toggle_checkbox(struct widget *cbox)
Definition widget_checkbox.c:225
create_textcheckbox
struct widget * create_textcheckbox(struct gui_layer *pdest, bool state, utf8_str *pstr, Uint32 flags)
Definition widget_checkbox.c:149
redraw_checkbox
static int redraw_checkbox(struct widget *icon)
Definition widget_checkbox.c:43
textcheckbox_baseclass_redraw
static int(* textcheckbox_baseclass_redraw)(struct widget *pwidget)
Definition widget_checkbox.c:38
get_checkbox_state
bool get_checkbox_state(struct widget *cbox)
Definition widget_checkbox.c:239
set_new_checkbox_theme
int set_new_checkbox_theme(struct widget *cbox, SDL_Surface *true_surf, SDL_Surface *false_surf)
Definition widget_checkbox.c:195
checkbox_baseclass_redraw
static int(* checkbox_baseclass_redraw)(struct widget *pwidget)
Definition widget_checkbox.c:37
create_checkbox
struct widget * create_checkbox(struct gui_layer *pdest, bool state, Uint32 flags)
Definition widget_checkbox.c:115
redraw_textcheckbox
static int redraw_textcheckbox(struct widget *cbox)
Definition widget_checkbox.c:78
widget_new
struct widget * widget_new(void)
Definition widget_core.c:252
create_icon_from_theme
SDL_Surface * create_icon_from_theme(SDL_Surface *icon_theme, Uint8 state)
Definition widget_icon.c:294
redraw_iconlabel
int redraw_iconlabel(struct widget *label)
Definition widget_label.c:436
create_iconlabel
struct widget * create_iconlabel(SDL_Surface *icon, struct gui_layer *pdest, utf8_str *pstr, Uint32 flags)
Definition widget_label.c:215
widget_p.h