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)
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 ***********************************************************************/
15 #include <fc_config.h>
19 #ifdef SDL2_PLAIN_INCLUDE
21 #else /* SDL2_PLAIN_INCLUDE */
23 #endif /* SDL2_PLAIN_INCLUDE */
31 #include "gui_tilespec.h"
32 #include "themespec.h"
37 static int (*checkbox_baseclass_redraw
)(struct widget
*pwidget
);
38 static int (*textcheckbox_baseclass_redraw
)(struct widget
*pwidget
);
40 /**************************************************************************
41 Blit checkbox gfx to surface its on.
42 **************************************************************************/
43 static int redraw_checkbox(struct widget
*pIcon
)
46 SDL_Rect src
, area
= pIcon
->size
;
48 ret
= (*checkbox_baseclass_redraw
)(pIcon
);
57 src
.x
= (pIcon
->theme
->w
/ 4) * (Uint8
) (get_wstate(pIcon
));
59 src
.w
= (pIcon
->theme
->w
/ 4);
60 src
.h
= pIcon
->theme
->h
;
62 if (pIcon
->size
.w
!= src
.w
) {
63 area
.x
+= (pIcon
->size
.w
- src
.w
) / 2;
66 if (pIcon
->size
.h
!= src
.h
) {
67 area
.y
+= (pIcon
->size
.h
- src
.h
) / 2;
70 return alphablit(pIcon
->theme
, &src
, pIcon
->dst
->surface
, &area
, 255);
73 /**************************************************************************
74 Blit checkbox-with-text gfx to surface its on.
75 **************************************************************************/
76 static int redraw_textcheckbox(struct widget
*pCBox
)
79 SDL_Surface
*pTheme_Surface
, *pIcon
;
81 ret
= (*textcheckbox_baseclass_redraw
)(pCBox
);
86 if (pCBox
->string_utf8
== NULL
) {
87 return widget_redraw(pCBox
);
90 pTheme_Surface
= pCBox
->theme
;
91 pIcon
= create_icon_from_theme(pTheme_Surface
, get_wstate(pCBox
));
99 /* redraw icon label */
100 ret
= redraw_iconlabel(pCBox
);
103 pCBox
->theme
= pTheme_Surface
;
108 /**************************************************************************
109 Create a new checkbox widget.
110 **************************************************************************/
111 struct widget
*create_checkbox(struct gui_layer
*pDest
, bool state
,
114 struct widget
*pCBox
= widget_new();
115 struct CHECKBOX
*pTmp
= fc_calloc(1, sizeof(struct CHECKBOX
));
118 pCBox
->theme
= current_theme
->CBOX_Sell_Icon
;
120 pCBox
->theme
= current_theme
->CBOX_Unsell_Icon
;
123 set_wflag(pCBox
, (WF_FREE_STRING
| WF_FREE_GFX
| WF_FREE_PRIVATE_DATA
| flags
));
124 set_wstate(pCBox
, FC_WS_DISABLED
);
125 set_wtype(pCBox
, WT_CHECKBOX
);
126 pCBox
->mod
= KMOD_NONE
;
129 pTmp
->pTRUE_Theme
= current_theme
->CBOX_Sell_Icon
;
130 pTmp
->pFALSE_Theme
= current_theme
->CBOX_Unsell_Icon
;
131 pCBox
->private_data
.cbox
= pTmp
;
133 checkbox_baseclass_redraw
= pCBox
->redraw
;
134 pCBox
->redraw
= redraw_checkbox
;
136 pCBox
->size
.w
= pCBox
->theme
->w
/ 4;
137 pCBox
->size
.h
= pCBox
->theme
->h
;
142 /**************************************************************************
143 Create a new checkbox-with-text widget.
144 **************************************************************************/
145 struct widget
*create_textcheckbox(struct gui_layer
*pDest
, bool state
,
146 utf8_str
*pstr
, Uint32 flags
)
148 struct widget
*pCBox
;
149 struct CHECKBOX
*pTmp
;
150 SDL_Surface
*pSurf
, *pIcon
;
151 struct widget
*pTmpWidget
;
154 return create_checkbox(pDest
, state
, flags
);
157 pTmp
= fc_calloc(1, sizeof(struct CHECKBOX
));
160 pSurf
= current_theme
->CBOX_Sell_Icon
;
162 pSurf
= current_theme
->CBOX_Unsell_Icon
;
165 pIcon
= create_icon_from_theme(pSurf
, 0);
166 pCBox
= create_iconlabel(pIcon
, pDest
, pstr
, (flags
| WF_FREE_PRIVATE_DATA
));
168 pstr
->style
&= ~SF_CENTER
;
170 pCBox
->theme
= pSurf
;
173 set_wtype(pCBox
, WT_TCHECKBOX
);
175 pTmp
->pTRUE_Theme
= current_theme
->CBOX_Sell_Icon
;
176 pTmp
->pFALSE_Theme
= current_theme
->CBOX_Unsell_Icon
;
177 pCBox
->private_data
.cbox
= pTmp
;
179 pTmpWidget
= widget_new();
180 /* we can't use pCBox->redraw here, because it is of type iconlabel */
181 textcheckbox_baseclass_redraw
= pTmpWidget
->redraw
;
182 FREEWIDGET(pTmpWidget
);
183 pCBox
->redraw
= redraw_textcheckbox
;
188 /**************************************************************************
189 Set theme surfaces for a checkbox.
190 **************************************************************************/
191 int set_new_checkbox_theme(struct widget
*pCBox
,
192 SDL_Surface
*pTrue
, SDL_Surface
*pFalse
)
194 struct CHECKBOX
*pTmp
;
196 if (!pCBox
|| (get_wtype(pCBox
) != WT_CHECKBOX
)) {
200 if (!pCBox
->private_data
.cbox
) {
201 pCBox
->private_data
.cbox
= fc_calloc(1, sizeof(struct CHECKBOX
));
202 set_wflag(pCBox
, WF_FREE_PRIVATE_DATA
);
203 pCBox
->private_data
.cbox
->state
= FALSE
;
206 pTmp
= pCBox
->private_data
.cbox
;
207 pTmp
->pTRUE_Theme
= pTrue
;
208 pTmp
->pFALSE_Theme
= pFalse
;
210 pCBox
->theme
= pTrue
;
212 pCBox
->theme
= pFalse
;
218 /**************************************************************************
219 Change the state of the checkbox.
220 **************************************************************************/
221 void toggle_checkbox(struct widget
*pCBox
)
223 if (pCBox
->private_data
.cbox
->state
) {
224 pCBox
->theme
= pCBox
->private_data
.cbox
->pFALSE_Theme
;
225 pCBox
->private_data
.cbox
->state
= FALSE
;
227 pCBox
->theme
= pCBox
->private_data
.cbox
->pTRUE_Theme
;
228 pCBox
->private_data
.cbox
->state
= TRUE
;
232 /**************************************************************************
233 Check state of the checkbox.
234 **************************************************************************/
235 bool get_checkbox_state(struct widget
*pCBox
)
237 return pCBox
->private_data
.cbox
->state
;