Freeciv-3.2
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/* SDL3 */
19#include <SDL3/SDL.h>
20
21/* utility */
22#include "log.h"
23
24/* gui-sdl3 */
25#include "colors.h"
26#include "graphics.h"
27#include "gui_tilespec.h"
28#include "themespec.h"
29
30#include "widget.h"
31#include "widget_p.h"
32
33static int (*checkbox_baseclass_redraw)(struct widget *pwidget);
34static int (*textcheckbox_baseclass_redraw)(struct widget *pwidget);
35
36/**********************************************************************/
39static int redraw_checkbox(struct widget *icon)
40{
41 int ret;
42 SDL_Rect src, area = icon->size;
43
44 ret = (*checkbox_baseclass_redraw)(icon);
45 if (ret != 0) {
46 return ret;
47 }
48
49 if (!icon->theme) {
50 return -3;
51 }
52
53 src.x = (icon->theme->w / 4) * (Uint8) (get_wstate(icon));
54 src.y = 0;
55 src.w = (icon->theme->w / 4);
56 src.h = icon->theme->h;
57
58 if (icon->size.w != src.w) {
59 area.x += (icon->size.w - src.w) / 2;
60 }
61
62 if (icon->size.h != src.h) {
63 area.y += (icon->size.h - src.h) / 2;
64 }
65
66 return alphablit(icon->theme, &src, icon->dst->surface, &area, 255);
67}
68
69/**********************************************************************/
74static int redraw_textcheckbox(struct widget *cbox)
75{
76 int ret;
78
79 ret = (*textcheckbox_baseclass_redraw)(cbox);
80 if (ret != 0) {
81 return ret;
82 }
83
84 if (cbox->string_utf8 == NULL) {
85 return widget_redraw(cbox);
86 }
87
88 theme_surface = cbox->theme;
90
91 if (!icon) {
92 return -3; /* FIXME: What SDL_Error this maps to, and should it map to none?
93 * Maybe it should be a positive value (SDL_Errors have
94 * negative values space) */
95 }
96
97 cbox->theme = icon;
98
99 /* redraw icon label */
101
102 FREESURFACE(icon);
103 cbox->theme = theme_surface;
104
105 return ret;
106}
107
108/**********************************************************************/
111struct widget *create_checkbox(struct gui_layer *pdest, bool state,
112 Uint32 flags)
113{
114 struct widget *cbox = widget_new();
115 struct checkbox *ptmp = fc_calloc(1, sizeof(struct checkbox));
116
117 if (state) {
118 cbox->theme = current_theme->cbox_sell_icon;
119 } else {
120 cbox->theme = current_theme->cbox_unsell_icon;
121 }
122
125 set_wtype(cbox, WT_CHECKBOX);
126 cbox->mod = SDL_KMOD_NONE;
127 cbox->dst = pdest;
128 ptmp->state = state;
129 ptmp->true_theme = current_theme->cbox_sell_icon;
130 ptmp->false_theme = current_theme->cbox_unsell_icon;
131 cbox->private_data.cbox = ptmp;
132
134 cbox->redraw = redraw_checkbox;
135
136 cbox->size.w = cbox->theme->w / 4;
137 cbox->size.h = cbox->theme->h;
138
139 return cbox;
140}
141
142/**********************************************************************/
145struct widget *create_textcheckbox(struct gui_layer *pdest, bool state,
146 utf8_str *pstr, Uint32 flags)
147{
148 struct widget *cbox;
149 struct checkbox *ptmp;
150 SDL_Surface *surf, *icon;
151 struct widget *tmp_widget;
152
153 if (pstr == NULL) {
154 return create_checkbox(pdest, state, flags);
155 }
156
157 ptmp = fc_calloc(1, sizeof(struct checkbox));
158
159 if (state) {
160 surf = current_theme->cbox_sell_icon;
161 } else {
162 surf = current_theme->cbox_unsell_icon;
163 }
164
165 icon = create_icon_from_theme(surf, 0);
167
168 pstr->style &= ~SF_CENTER;
169
170 cbox->theme = surf;
171 FREESURFACE(icon);
172
174 ptmp->state = state;
175 ptmp->true_theme = current_theme->cbox_sell_icon;
176 ptmp->false_theme = current_theme->cbox_unsell_icon;
177 cbox->private_data.cbox = ptmp;
178
180 /* We can't use cbox->redraw here, because it is of type iconlabel */
183 cbox->redraw = redraw_textcheckbox;
184
185 return cbox;
186}
187
188/**********************************************************************/
193{
194 struct checkbox *ptmp;
195
196 if (!cbox || (get_wtype(cbox) != WT_CHECKBOX)) {
197 return -1;
198 }
199
200 if (!cbox->private_data.cbox) {
201 cbox->private_data.cbox = fc_calloc(1, sizeof(struct checkbox));
203 cbox->private_data.cbox->state = FALSE;
204 }
205
206 ptmp = cbox->private_data.cbox;
208 ptmp->false_theme = false_surf;
209 if (ptmp->state) {
210 cbox->theme = true_surf;
211 } else {
212 cbox->theme = false_surf;
213 }
214
215 return 0;
216}
217
218/**********************************************************************/
221void toggle_checkbox(struct widget *cbox)
222{
223 if (cbox->private_data.cbox->state) {
224 cbox->theme = cbox->private_data.cbox->false_theme;
225 cbox->private_data.cbox->state = FALSE;
226 } else {
227 cbox->theme = cbox->private_data.cbox->true_theme;
228 cbox->private_data.cbox->state = TRUE;
229 }
230}
231
232/**********************************************************************/
235bool get_checkbox_state(struct widget *cbox)
236{
237 return cbox->private_data.cbox->state;
238}
char * incite_cost
Definition comments.c:75
QString current_theme
Definition fc_client.cpp:65
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
int(* redraw)(struct widget *pwidget)
Definition widget.h:163
struct gui_layer * dst
Definition widget.h:116
Uint16 mod
Definition widget.h:154
union widget::@195 private_data
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