Freeciv-3.3
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
37static int (*checkbox_baseclass_redraw)(struct widget *pwidget);
38static int (*textcheckbox_baseclass_redraw)(struct widget *pwidget);
39
40/**********************************************************************/
43static 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/**********************************************************************/
78static int redraw_textcheckbox(struct widget *cbox)
79{
80 int ret;
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;
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 */
105
106 FREESURFACE(icon);
107 cbox->theme = theme_surface;
108
109 return ret;
110}
111
112/**********************************************************************/
115struct 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
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
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/**********************************************************************/
149struct 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);
171
172 pstr->style &= ~SF_CENTER;
173
174 cbox->theme = surf;
175 FREESURFACE(icon);
176
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
184 /* We can't use cbox->redraw here, because it is of type iconlabel */
187 cbox->redraw = redraw_textcheckbox;
188
189 return cbox;
190}
191
192/**********************************************************************/
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));
207 cbox->private_data.cbox->state = FALSE;
208 }
209
210 ptmp = cbox->private_data.cbox;
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/**********************************************************************/
225void 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/**********************************************************************/
239bool get_checkbox_state(struct widget *cbox)
240{
241 return cbox->private_data.cbox->state;
242}
char * incite_cost
Definition comments.c:76
QString current_theme
Definition fc_client.cpp:64
int alphablit(SDL_Surface *src, SDL_Rect *srcrect, SDL_Surface *dst, SDL_Rect *dstrect, unsigned char alpha_mod)
Definition graphics.c:199
#define FREESURFACE(ptr)
Definition graphics.h:322
@ FC_WS_DISABLED
Definition widget.h:99
#define FREEWIDGET(pwidget)
Definition widget.h:305
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_PRIVATE_DATA
Definition widget.h:80
@ WF_FREE_STRING
Definition widget.h:76
static int widget_redraw(struct widget *pwidget)
Definition widget.h:276
void set_wtype(struct widget *pwidget, enum widget_type type)
Definition widget_core.c:45
enum widget_type get_wtype(const struct widget *pwidget)
Definition widget_core.c:78
void set_wflag(struct widget *pwidget, enum widget_flag flag)
Definition widget_core.c:54
@ WT_TCHECKBOX
Definition widget.h:59
@ WT_CHECKBOX
Definition widget.h:58
void toggle_checkbox(struct widget *cbox)
struct widget * create_textcheckbox(struct gui_layer *pdest, bool state, utf8_str *pstr, Uint32 flags)
static int redraw_checkbox(struct widget *icon)
static int(* textcheckbox_baseclass_redraw)(struct widget *pwidget)
bool get_checkbox_state(struct widget *cbox)
int set_new_checkbox_theme(struct widget *cbox, SDL_Surface *true_surf, SDL_Surface *false_surf)
static int(* checkbox_baseclass_redraw)(struct widget *pwidget)
struct widget * create_checkbox(struct gui_layer *pdest, bool state, Uint32 flags)
static int redraw_textcheckbox(struct widget *cbox)
struct widget * widget_new(void)
SDL_Surface * create_icon_from_theme(SDL_Surface *icon_theme, Uint8 state)
int redraw_iconlabel(struct widget *label)
struct widget * create_iconlabel(SDL_Surface *icon, struct gui_layer *pdest, utf8_str *pstr, Uint32 flags)
#define fc_calloc(n, esz)
Definition mem.h:38
SDL_Surface * false_theme
SDL_Surface * true_theme
SDL_Surface * surface
Definition graphics.h:229
struct checkbox * cbox
Definition widget.h:137
SDL_Surface * theme
Definition widget.h:118
union widget::@224 private_data
int(* redraw)(struct widget *pwidget)
Definition widget.h:163
struct gui_layer * dst
Definition widget.h:116
Uint16 mod
Definition widget.h:154
SDL_Rect area
Definition widget.h:149
SDL_Rect size
Definition widget.h:145
#define TRUE
Definition support.h:46
#define FALSE
Definition support.h:47