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
42
43static int (*baseclass_redraw) (struct widget *widget) = NULL;
44
45
46/************************************************************************/
49static 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. */
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) {
98 }
99
100 return ret;
101}
102
103/************************************************************************/
106static int combo_menu_callback(struct widget *window)
107{
109 struct combo_menu *menu =
110 (struct combo_menu *)window->data.widget->private_data.ptr;
111
113 }
114
115 return -1;
116}
117
118/************************************************************************/
121static int combo_menu_item_callback(struct widget *label)
122{
123 struct widget *combo = label->data.widget;
124
127 widget_redraw(combo);
128 widget_mark_dirty(combo);
129 }
130 combo_popdown(combo);
131
132 return -1;
133}
134
135/************************************************************************/
138void 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);
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);
161
162 /* Labels. */
163 strvec_iterate(combo->data.vector, string) {
164 label = create_iconlabel_from_chars(NULL, window->dst, string, 0,
167 label->data.widget = combo;
168 set_wstate(label, FC_WS_NORMAL);
170
171 longest = MAX(longest, label->size.w);
172 widget_set_position(label, adj_size(10), h);
173 h += adj_size(15);
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. */
205 widget_mark_dirty(window);
206 flush_dirty();
207}
208
209/************************************************************************/
212void combo_popdown(struct widget *combo)
213{
214 struct combo_menu *menu;
215
216 fc_assert_ret(NULL != combo);
218
219 menu = (struct combo_menu *) combo->private_data.ptr;
220 if (NULL == menu) {
221 return;
222 }
223
226 menu->end_widget_list);
227 free(menu);
228 combo->private_data.ptr = NULL;
229 flush_dirty();
230}
231
232/************************************************************************/
235struct 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);
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;
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}
char * incite_cost
Definition comments.c:76
QString current_theme
Definition fc_client.cpp:64
void flush_dirty(void)
Definition mapview.c:468
int main_window_width(void)
Definition graphics.c:685
int alphablit(SDL_Surface *src, SDL_Rect *srcrect, SDL_Surface *dst, SDL_Rect *dstrect, unsigned char alpha_mod)
Definition graphics.c:199
struct sdl2_data main_data
Definition graphics.c:57
struct gui_layer * add_gui_layer(int width, int height)
Definition graphics.c:112
int main_window_height(void)
Definition graphics.c:693
#define FREESURFACE(ptr)
Definition graphics.h:322
@ ID_LABEL
Definition gui_id.h:27
@ ID_COMBO_MENU
Definition gui_id.h:37
#define adj_size(size)
Definition gui_main.h:141
#define PRESSED_EVENT(event)
Definition gui_main.h:71
void utf8_str_size(utf8_str *pstr, SDL_Rect *fill)
Definition gui_string.c:106
utf8_str * copy_chars_to_utf8_str(utf8_str *pstr, const char *pchars)
Definition gui_string.c:251
SDL_Surface * create_text_surf_from_utf8(utf8_str *pstr)
Definition gui_string.c:425
#define SF_CENTER
Definition gui_string.h:40
#define SF_CENTER_RIGHT
Definition gui_string.h:41
void correct_size_bcgnd_surf(SDL_Surface *ptheme, int *width, int *height)
Definition widget.c:63
void add_to_gui_list(Uint16 id, struct widget *gui)
Definition widget.c:586
SDL_Surface * create_bcgnd_surf(SDL_Surface *ptheme, enum widget_state state, Uint16 width, Uint16 height)
Definition widget.c:78
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
void popdown_window_group_dialog(struct widget *begin_group_widget_list, struct widget *end_group_widget_list)
Definition widget.c:983
void move_window_group(struct widget *begin_widget_list, struct widget *pwindow)
Definition widget.c:1039
static void widget_set_position(struct widget *pwidget, int x, int y)
Definition widget.h:266
@ FC_WS_DISABLED
Definition widget.h:99
@ FC_WS_NORMAL
Definition widget.h:96
static void widget_mark_dirty(struct widget *pwidget)
Definition widget.h:286
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_STRING
Definition widget.h:76
@ WF_RESTORE_BACKGROUND
Definition widget.h:85
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_COMBO
Definition widget.h:62
static int combo_menu_item_callback(struct widget *label)
void combo_popup(struct widget *combo)
static int(* baseclass_redraw)(struct widget *widget)
struct widget * combo_new(SDL_Surface *background, struct gui_layer *dest, utf8_str *pstr, const struct strvec *vector, int length, Uint32 flags)
void combo_popdown(struct widget *combo)
static int combo_menu_callback(struct widget *window)
static int combo_redraw(struct widget *combo)
struct widget * widget_new(void)
#define create_iconlabel_from_chars(picon, pdest, chars, ptsize, flags)
bool resize_window(struct widget *pwindow, SDL_Surface *bcgd, SDL_Color *pcolor, Uint16 new_w, Uint16 new_h)
struct widget * create_window_skeleton(struct gui_layer *pdest, utf8_str *title, Uint32 flags)
#define fc_assert_ret(condition)
Definition log.h:192
#define fc_malloc(sz)
Definition mem.h:34
#define MAX(x, y)
Definition shared.h:54
struct sprite int int y
Definition sprite_g.h:31
struct sprite int x
Definition sprite_g.h:31
size_t strvec_size(const struct strvec *psv)
#define strvec_iterate(psv, str)
#define strvec_iterate_end
struct widget * end_widget_list
struct widget * begin_widget_list
SDL_Surface * surface
Definition graphics.h:229
SDL_Rect dest_rect
Definition graphics.h:228
SDL_Event event
Definition graphics.h:217
Uint8 style
Definition gui_string.h:53
char * text
Definition gui_string.h:60
void * ptr
Definition widget.h:133
SDL_Surface * theme
Definition widget.h:118
struct widget * widget
Definition widget.h:132
union widget::@223 data
SDL_Surface * theme2
Definition widget.h:119
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
utf8_str * string_utf8
Definition widget.h:121
const struct strvec * vector
Definition widget.h:126
int(* action)(struct widget *)
Definition widget.h:157
void(* destroy)(struct widget *pwidget)
Definition widget.h:170
SDL_Rect size
Definition widget.h:145