Freeciv-3.3: /fast/freeciv/nightly/main/client/gui-sdl2/widget_combo.c Source File

Freeciv-3.3

Loading...
Searching...
No Matches
widget_combo.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#include "string_vector.h"
28
29/* client/gui-sdl2 */
30#include "colors.h"
31#include "gui_id.h"
32#include "gui_string.h"
33#include "gui_tilespec.h"
34#include "mapview.h"
35#include "widget.h"
36#include "widget_p.h"
37
38 struct combo_menu {
39 struct widget *begin_widget_list;
40 struct widget *end_widget_list;
41};
42
43 static int (*baseclass_redraw) (struct widget *widget) = NULL;
44
45
46/************************************************************************/
49 static int combo_redraw(struct widget *combo)
50{
51 SDL_Rect dest = { combo->size.x, combo->size.y, 0, 0 };
52 SDL_Surface *text, *surface;
53 struct combo_menu *menu;
54 int ret;
55
56 ret = baseclass_redraw(combo);
57 if (0 != ret) {
58 return ret;
59 }
60
61 surface = create_bcgnd_surf(combo->theme, get_wstate(combo),
62 combo->size.w, combo->size.h);
63
64 if (NULL == surface) {
65 return -1;
66 }
67
68 /* Blit theme. */
69 alphablit(surface, NULL, combo->dst->surface, &dest, 255);
70
71 /* Set position and blit text. */
72 text = create_text_surf_from_utf8(combo->string_utf8);
73 if (NULL != text) {
74 dest.y += (surface->h - surface->h) / 2;
75 /* Blit centred text to botton. */
76 if (combo->string_utf8->style & SF_CENTER) {
77 dest.x += (surface->w - text->w) / 2;
78 } else {
79 if (combo->string_utf8->style & SF_CENTER_RIGHT) {
80 dest.x += surface->w - text->w - adj_size(5);
81 } else {
82 dest.x += adj_size(5); /* center left */
83 }
84 }
85
86 alphablit(text, NULL, combo->dst->surface, &dest, 255);
87 }
88 /* text. */
89 ret = surface->h;
90
91 /* Free memory */
92 FREESURFACE(text);
93 FREESURFACE(surface);
94
95 menu = (struct combo_menu *) combo->private_data.ptr;
96 if (NULL != menu) {
97 ret = redraw_group(menu->begin_widget_list, menu->end_widget_list, 0);
98 }
99
100 return ret;
101}
102
103/************************************************************************/
106 static int combo_menu_callback(struct widget *window)
107{
108 if (PRESSED_EVENT(main_data.event)) {
109 struct combo_menu *menu =
110 (struct combo_menu *)window->data.widget->private_data.ptr;
111
112 move_window_group(menu->begin_widget_list, menu->end_widget_list);
113 }
114
115 return -1;
116}
117
118/************************************************************************/
121 static int combo_menu_item_callback(struct widget *label)
122{
123 struct widget *combo = label->data.widget;
124
125 if (PRESSED_EVENT(main_data.event)) {
126 copy_chars_to_utf8_str(combo->string_utf8, label->string_utf8->text);
127 widget_redraw(combo);
128 widget_mark_dirty(combo);
129 }
130 combo_popdown(combo);
131
132 return -1;
133}
134
135/************************************************************************/
138 void combo_popup(struct widget *combo)
139{
140 struct combo_menu *menu;
141 struct widget *window, *label = NULL;
142 int longest = 0, h = 0, x, y;
143
144 fc_assert_ret(NULL != combo);
145 fc_assert_ret(WT_COMBO == get_wtype(combo));
146
147 if (NULL != combo->private_data.ptr) {
148 return;
149 }
150
151 if (0 >= strvec_size(combo->data.vector)) {
152 return;
153 }
154
155 /* Menu. */
156 window = create_window_skeleton(NULL, NULL, 0);
157 window->action = combo_menu_callback;
158 window->data.widget = combo;
159 set_wstate(window, FC_WS_NORMAL);
160 add_to_gui_list(ID_COMBO_MENU, window);
161
162 /* Labels. */
163 strvec_iterate(combo->data.vector, string) {
164 label = create_iconlabel_from_chars(NULL, window->dst, string, 0,
165 WF_RESTORE_BACKGROUND);
166 label->action = combo_menu_item_callback;
167 label->data.widget = combo;
168 set_wstate(label, FC_WS_NORMAL);
169 add_to_gui_list(ID_LABEL, label);
170
171 longest = MAX(longest, label->size.w);
172 widget_set_position(label, adj_size(10), h);
173 h += adj_size(15);
174 } strvec_iterate_end;
175
176 /* Resize and relocate the window. */
177 resize_window(window, NULL, NULL, longest + 2 * adj_size(10), h);
178
179 x = combo->size.x + combo->dst->dest_rect.x;
180 if (x + window->size.w > main_window_width()) {
181 x = main_window_width() - window->size.w;
182 }
183 if (x < 0) {
184 x = 0;
185 }
186
187 y = combo->size.y - h + combo->dst->dest_rect.y;
188 if (y + window->size.h > main_window_height()) {
189 y = main_window_height() - window->size.h;
190 }
191 if (y < 0) {
192 y = 0;
193 }
194
195 widget_set_position(window, x, y);
196
197 /* Make data. */
198 menu = fc_malloc(sizeof(*menu));
199 menu->begin_widget_list = label;
200 menu->end_widget_list = window;
201 combo->private_data.ptr = menu;
202
203 /* Redraw. */
204 redraw_group(menu->begin_widget_list, menu->end_widget_list, 0);
205 widget_mark_dirty(window);
206 flush_dirty();
207}
208
209/************************************************************************/
212 void combo_popdown(struct widget *combo)
213{
214 struct combo_menu *menu;
215
216 fc_assert_ret(NULL != combo);
217 fc_assert_ret(WT_COMBO == get_wtype(combo));
218
219 menu = (struct combo_menu *) combo->private_data.ptr;
220 if (NULL == menu) {
221 return;
222 }
223
224 widget_mark_dirty(menu->end_widget_list);
225 popdown_window_group_dialog(menu->begin_widget_list,
226 menu->end_widget_list);
227 free(menu);
228 combo->private_data.ptr = NULL;
229 flush_dirty();
230}
231
232/************************************************************************/
235 struct widget *combo_new(SDL_Surface *background, struct gui_layer *dest,
236 utf8_str *pstr, const struct strvec *vector,
237 int length, Uint32 flags)
238{
239 SDL_Rect buf = {0, 0, 0, 0};
240 struct widget *combo = widget_new();
241
242 combo->theme = current_theme->edit;
243 combo->theme2 = background;
244 combo->string_utf8 = pstr;
245 set_wflag(combo, WF_FREE_STRING | WF_FREE_GFX | flags);
246 set_wstate(combo, FC_WS_DISABLED);
247 set_wtype(combo, WT_COMBO);
248 combo->mod = KMOD_NONE;
249
250 baseclass_redraw = combo->redraw;
251 combo->redraw = combo_redraw;
252 combo->destroy = combo_popdown;
253
254 if (NULL != pstr) {
255 combo->string_utf8->style |= SF_CENTER;
256 utf8_str_size(pstr, &buf);
257 buf.h += adj_size(4);
258 }
259
260 length = MAX(length, buf.w + adj_size(10));
261 correct_size_bcgnd_surf(current_theme->edit, &length, &buf.h);
262 combo->size.w = buf.w + adj_size(10);
263 combo->size.h = buf.h;
264
265 if (dest) {
266 combo->dst = dest;
267 } else {
268 combo->dst = add_gui_layer(combo->size.w, combo->size.h);
269 }
270 combo->data.vector = vector;
271 combo->private_data.ptr = NULL;
272
273 return combo;
274}
incite_cost
char * incite_cost
Definition comments.c:74
current_theme
QString current_theme
Definition fc_client.cpp:64
flush_dirty
void flush_dirty(void)
Definition mapview.c:468
main_window_width
int main_window_width(void)
Definition graphics.c:685
alphablit
int alphablit(SDL_Surface *src, SDL_Rect *srcrect, SDL_Surface *dst, SDL_Rect *dstrect, unsigned char alpha_mod)
Definition graphics.c:199
main_data
struct sdl2_data main_data
Definition graphics.c:57
add_gui_layer
struct gui_layer * add_gui_layer(int width, int height)
Definition graphics.c:112
main_window_height
int main_window_height(void)
Definition graphics.c:693
FREESURFACE
#define FREESURFACE(ptr)
Definition graphics.h:322
ID_LABEL
@ ID_LABEL
Definition gui_id.h:27
ID_COMBO_MENU
@ ID_COMBO_MENU
Definition gui_id.h:37
adj_size
#define adj_size(size)
Definition gui_main.h:141
PRESSED_EVENT
#define PRESSED_EVENT(event)
Definition gui_main.h:71
utf8_str_size
void utf8_str_size(utf8_str *pstr, SDL_Rect *fill)
Definition gui_string.c:106
copy_chars_to_utf8_str
utf8_str * copy_chars_to_utf8_str(utf8_str *pstr, const char *pchars)
Definition gui_string.c:251
create_text_surf_from_utf8
SDL_Surface * create_text_surf_from_utf8(utf8_str *pstr)
Definition gui_string.c:425
SF_CENTER
#define SF_CENTER
Definition gui_string.h:40
SF_CENTER_RIGHT
#define SF_CENTER_RIGHT
Definition gui_string.h:41
correct_size_bcgnd_surf
void correct_size_bcgnd_surf(SDL_Surface *ptheme, int *width, int *height)
Definition widget.c:63
add_to_gui_list
void add_to_gui_list(Uint16 id, struct widget *gui)
Definition widget.c:586
create_bcgnd_surf
SDL_Surface * create_bcgnd_surf(SDL_Surface *ptheme, enum widget_state state, Uint16 width, Uint16 height)
Definition widget.c:78
redraw_group
Uint16 redraw_group(const struct widget *begin_group_widget_list, const struct widget *end_group_widget_list, int add_to_update)
Definition widget.c:720
popdown_window_group_dialog
void popdown_window_group_dialog(struct widget *begin_group_widget_list, struct widget *end_group_widget_list)
Definition widget.c:983
move_window_group
void move_window_group(struct widget *begin_widget_list, struct widget *pwindow)
Definition widget.c:1039
widget_set_position
static void widget_set_position(struct widget *pwidget, int x, int y)
Definition widget.h:266
FC_WS_DISABLED
@ FC_WS_DISABLED
Definition widget.h:99
FC_WS_NORMAL
@ FC_WS_NORMAL
Definition widget.h:96
widget_mark_dirty
static void widget_mark_dirty(struct widget *pwidget)
Definition widget.h:286
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_STRING
@ WF_FREE_STRING
Definition widget.h:76
WF_RESTORE_BACKGROUND
@ WF_RESTORE_BACKGROUND
Definition widget.h:85
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_COMBO
@ WT_COMBO
Definition widget.h:62
combo_menu_item_callback
static int combo_menu_item_callback(struct widget *label)
Definition widget_combo.c:121
combo_popup
void combo_popup(struct widget *combo)
Definition widget_combo.c:138
baseclass_redraw
static int(* baseclass_redraw)(struct widget *widget)
Definition widget_combo.c:43
combo_new
struct widget * combo_new(SDL_Surface *background, struct gui_layer *dest, utf8_str *pstr, const struct strvec *vector, int length, Uint32 flags)
Definition widget_combo.c:235
combo_popdown
void combo_popdown(struct widget *combo)
Definition widget_combo.c:212
combo_menu_callback
static int combo_menu_callback(struct widget *window)
Definition widget_combo.c:106
combo_redraw
static int combo_redraw(struct widget *combo)
Definition widget_combo.c:49
widget_new
struct widget * widget_new(void)
Definition widget_core.c:252
create_iconlabel_from_chars
#define create_iconlabel_from_chars(picon, pdest, chars, ptsize, flags)
Definition widget_label.h:17
resize_window
bool resize_window(struct widget *pwindow, SDL_Surface *bcgd, SDL_Color *pcolor, Uint16 new_w, Uint16 new_h)
Definition widget_window.c:255
create_window_skeleton
struct widget * create_window_skeleton(struct gui_layer *pdest, utf8_str *title, Uint32 flags)
Definition widget_window.c:180
gui_id.h
gui_string.h
gui_tilespec.h
widget.h
widget_p.h
colors.h
mapview.h
log.h
fc_assert_ret
#define fc_assert_ret(condition)
Definition log.h:191
fc_malloc
#define fc_malloc(sz)
Definition mem.h:34
MAX
#define MAX(x, y)
Definition shared.h:54
y
struct sprite int int y
Definition sprite_g.h:31
x
struct sprite int x
Definition sprite_g.h:31
strvec_size
size_t strvec_size(const struct strvec *psv)
Definition string_vector.c:344
string_vector.h
strvec_iterate
#define strvec_iterate(psv, str)
Definition string_vector.h:61
strvec_iterate_end
#define strvec_iterate_end
Definition string_vector.h:68
gui_layer
Definition graphics.h:227
gui_layer::surface
SDL_Surface * surface
Definition graphics.h:229
gui_layer::dest_rect
SDL_Rect dest_rect
Definition graphics.h:228
sdl2_data::event
SDL_Event event
Definition graphics.h:217
strvec
Definition string_vector.c:29
utf8_str
Definition gui_string.h:52
utf8_str::style
Uint8 style
Definition gui_string.h:53
utf8_str::text
char * text
Definition gui_string.h:60
widget
Definition widget.h:112
widget::data
union widget::@216 data
widget::ptr
void * ptr
Definition widget.h:133
widget::theme
SDL_Surface * theme
Definition widget.h:118
widget::widget
struct widget * widget
Definition widget.h:132
widget::theme2
SDL_Surface * theme2
Definition widget.h:119
widget::private_data
union widget::@217 private_data
widget::redraw
int(* redraw)(struct widget *pwidget)
Definition widget.h:163
widget::dst
struct gui_layer * dst
Definition widget.h:116
widget::mod
Uint16 mod
Definition widget.h:154
widget::string_utf8
utf8_str * string_utf8
Definition widget.h:121
widget::vector
const struct strvec * vector
Definition widget.h:126
widget::action
int(* action)(struct widget *)
Definition widget.h:157
widget::destroy
void(* destroy)(struct widget *pwidget)
Definition widget.h:170
widget::size
SDL_Rect size
Definition widget.h:145