Freeciv-3.2: /fast/freeciv/nightly/S3_2/client/gui-sdl2/finddlg.c Source File

Freeciv-3.2

Loading...
Searching...
No Matches
finddlg.c
Go to the documentation of this file.
1/***********************************************************************
2 Freeciv - Copyright (C) 1996 - A Kjeldberg, L Gregersen, P Unold
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#include <stdlib.h>
19
20/* SDL2 */
21#ifdef SDL2_PLAIN_INCLUDE
22#include <SDL.h>
23#else /* SDL2_PLAIN_INCLUDE */
24#include <SDL2/SDL.h>
25#endif /* SDL2_PLAIN_INCLUDE */
26
27/* utility */
28#include "fcintl.h"
29#include "log.h"
30
31/* common */
32#include "game.h"
33#include "nation.h"
34
35/* client */
36#include "zoom.h"
37
38/* gui-sdl2 */
39#include "colors.h"
40#include "graphics.h"
41#include "gui_id.h"
42#include "gui_main.h"
43#include "gui_tilespec.h"
44#include "mapctrl.h"
45#include "mapview.h"
46#include "sprite.h"
47#include "widget.h"
48
49#include "finddlg.h"
50
51/* ====================================================================== */
52/* ============================= FIND CITY MENU ========================= */
53/* ====================================================================== */
54 static struct advanced_dialog *find_city_dlg = NULL;
55
56/**********************************************************************/
59 static int find_city_window_dlg_callback(struct widget *pwindow)
60{
61 if (PRESSED_EVENT(main_data.event)) {
62 move_window_group(find_city_dlg->begin_widget_list, pwindow);
63 }
64
65 return -1;
66}
67
68/**********************************************************************/
71 static int exit_find_city_dlg_callback(struct widget *pwidget)
72{
73 if (PRESSED_EVENT(main_data.event)) {
74 int orginal_x = pwidget->data.cont->id0;
75 int orginal_y = pwidget->data.cont->id1;
76
77 popdown_find_dialog();
78
79 center_tile_mapcanvas(map_pos_to_tile(&(wld.map), orginal_x, orginal_y));
80
81 flush_dirty();
82 }
83
84 return -1;
85}
86
87/**********************************************************************/
90 static int find_city_callback(struct widget *pwidget)
91{
92 if (PRESSED_EVENT(main_data.event)) {
93 struct city *pcity = pwidget->data.city;
94
95 if (pcity) {
96 center_tile_mapcanvas(pcity->tile);
97 if (main_data.event.button.button == SDL_BUTTON_RIGHT) {
98 popdown_find_dialog();
99 }
100 flush_dirty();
101 }
102 }
103
104 return -1;
105}
106
107/**********************************************************************/
110 void popdown_find_dialog(void)
111{
112 if (find_city_dlg) {
113 popdown_window_group_dialog(find_city_dlg->begin_widget_list,
114 find_city_dlg->end_widget_list);
115 FC_FREE(find_city_dlg->scroll);
116 FC_FREE(find_city_dlg);
117 enable_and_redraw_find_city_button();
118 }
119}
120
121/**********************************************************************/
124 void popup_find_dialog(void)
125{
126 struct widget *pwindow = NULL, *buf = NULL;
127 SDL_Surface *logo = NULL;
128 utf8_str *pstr;
129 char cbuf[128];
130 int h = 0, n = 0, w = 0, units_h = 0;
131 struct player *owner = NULL;
132 struct tile *original;
133 int window_x = 0, window_y = 0;
134 bool mouse = (main_data.event.type == SDL_MOUSEBUTTONDOWN);
135 SDL_Rect area;
136
137 /* check that there are any cities to find */
138 players_iterate(pplayer) {
139 h = city_list_size(pplayer->cities);
140 if (h > 0) {
141 break;
142 }
143 } players_iterate_end;
144
145 if (find_city_dlg && !h) {
146 return;
147 }
148
149 original = canvas_pos_to_tile(main_data.map->w / 2, main_data.map->h / 2, map_zoom);
150
151 find_city_dlg = fc_calloc(1, sizeof(struct advanced_dialog));
152
153 pstr = create_utf8_from_char_fonto(_("Find City"), FONTO_ATTENTION);
154 pstr->style |= TTF_STYLE_BOLD;
155
156 pwindow = create_window_skeleton(NULL, pstr, 0);
157
158 pwindow->action = find_city_window_dlg_callback;
159 set_wstate(pwindow , FC_WS_NORMAL);
160
161 add_to_gui_list(ID_TERRAIN_ADV_DLG_WINDOW, pwindow);
162 find_city_dlg->end_widget_list = pwindow;
163
164 area = pwindow->area;
165
166 /* ---------- */
167 /* Exit button */
168 buf = create_themeicon(current_theme->small_cancel_icon, pwindow->dst,
169 WF_WIDGET_HAS_INFO_LABEL | WF_RESTORE_BACKGROUND
170 | WF_FREE_DATA);
171 buf->info_label = create_utf8_from_char_fonto(_("Close Dialog (Esc)"),
172 FONTO_ATTENTION);
173 area.w = MAX(area.w, buf->size.w + adj_size(10));
174 buf->action = exit_find_city_dlg_callback;
175 set_wstate(buf, FC_WS_NORMAL);
176 buf->key = SDLK_ESCAPE;
177 buf->data.cont = fc_calloc(1, sizeof(struct container));
178 buf->data.cont->id0 = index_to_map_pos_x(tile_index(original));
179 buf->data.cont->id1 = index_to_map_pos_y(tile_index(original));
180
181 add_to_gui_list(ID_TERRAIN_ADV_DLG_EXIT_BUTTON, buf);
182 /* ---------- */
183
184 players_iterate(pplayer) {
185 city_list_iterate(pplayer->cities, pcity) {
186 fc_snprintf(cbuf , sizeof(cbuf), "%s (%d)", city_name_get(pcity),
187 city_size_get(pcity));
188
189 pstr = create_utf8_from_char_fonto(cbuf, FONTO_DEFAULT);
190 pstr->style |= (TTF_STYLE_BOLD|SF_CENTER);
191
192 if (!player_owns_city(owner, pcity)) {
193 logo = get_nation_flag_surface(nation_of_player(city_owner(pcity)));
194 logo = crop_visible_part_from_surface(logo);
195 }
196
197 buf = create_iconlabel(logo, pwindow->dst, pstr,
198 (WF_RESTORE_BACKGROUND|WF_DRAW_TEXT_LABEL_WITH_SPACE));
199
200 if (!player_owns_city(owner, pcity)) {
201 set_wflag(buf, WF_FREE_THEME);
202 owner = city_owner(pcity);
203 }
204
205 buf->string_utf8->style &= ~SF_CENTER;
206 buf->string_utf8->fgcol = *(get_player_color(tileset, city_owner(pcity))->color);
207 buf->string_utf8->bgcol = (SDL_Color) {0, 0, 0, 0};
208
209 buf->data.city = pcity;
210
211 buf->action = find_city_callback;
212 set_wstate(buf, FC_WS_NORMAL);
213
214 add_to_gui_list(ID_LABEL , buf);
215
216 area.w = MAX(area.w, buf->size.w);
217 area.h += buf->size.h;
218
219 if (n > 19) {
220 set_wflag(buf , WF_HIDDEN);
221 }
222
223 n++;
224 } city_list_iterate_end;
225 } players_iterate_end;
226
227 find_city_dlg->begin_widget_list = buf;
228 find_city_dlg->begin_active_widget_list = find_city_dlg->begin_widget_list;
229 find_city_dlg->end_active_widget_list = pwindow->prev->prev;
230 find_city_dlg->active_widget_list = find_city_dlg->end_active_widget_list;
231
232
233 /* ---------- */
234 if (n > 20) {
235 units_h = create_vertical_scrollbar(find_city_dlg, 1, 20, TRUE, TRUE);
236 find_city_dlg->scroll->count = n;
237
238 n = units_h;
239 area.w += n;
240
241 units_h = 20 * buf->size.h + adj_size(2);
242
243 } else {
244 units_h = area.h;
245 }
246
247 /* ---------- */
248
249 area.h = units_h;
250
251 resize_window(pwindow , NULL, NULL,
252 (pwindow->size.w - pwindow->area.w) + area.w,
253 (pwindow->size.h - pwindow->area.h) + area.h);
254
255 area = pwindow->area;
256
257 if (!mouse) {
258 window_x = adj_size(10);
259 window_y = (main_window_height() - pwindow->size.h) / 2;
260 } else {
261 window_x = (main_data.event.motion.x + pwindow->size.w + adj_size(10) < main_window_width()) ?
262 (main_data.event.motion.x + adj_size(10)) :
263 (main_window_width() - pwindow->size.w - adj_size(10));
264 window_y = (main_data.event.motion.y - adj_size(2) + pwindow->size.h < main_window_height()) ?
265 (main_data.event.motion.y - adj_size(2)) :
266 (main_window_height() - pwindow->size.h - adj_size(10));
267 }
268
269 widget_set_position(pwindow, window_x, window_y);
270
271 w = area.w;
272
273 if (find_city_dlg->scroll) {
274 w -= n;
275 }
276
277 /* exit button */
278 buf = pwindow->prev;
279
280 buf->size.x = area.x + area.w - buf->size.w - 1;
281 buf->size.y = pwindow->size.y + adj_size(2);
282
283 /* cities */
284 buf = buf->prev;
285 setup_vertical_widgets_position(1, area.x, area.y, w, 0,
286 find_city_dlg->begin_active_widget_list,
287 buf);
288
289 if (find_city_dlg->scroll) {
290 setup_vertical_scrollbar_area(find_city_dlg->scroll,
291 area.x + area.w, area.y,
292 area.h, TRUE);
293 }
294
295 /* -------------------- */
296 /* redraw */
297 redraw_group(find_city_dlg->begin_widget_list, pwindow, 0);
298 widget_mark_dirty(pwindow);
299
300 flush_dirty();
301}
n
#define n
Definition astring.c:77
city_name_get
const char * city_name_get(const struct city *pcity)
Definition city.c:1128
city_list_iterate
#define city_list_iterate(citylist, pcity)
Definition city.h:508
city_size_get
static citizens city_size_get(const struct city *pcity)
Definition city.h:569
city_owner
#define city_owner(_pcity_)
Definition city.h:563
city_list_iterate_end
#define city_list_iterate_end
Definition city.h:510
get_player_color
struct color * get_player_color(const struct tileset *t, const struct player *pplayer)
Definition colors_common.c:125
incite_cost
char * incite_cost
Definition comments.c:74
current_theme
QString current_theme
Definition fc_client.cpp:65
fcintl.h
_
#define _(String)
Definition fcintl.h:67
wld
struct world wld
Definition game.c:63
game.h
owner
struct city * owner
Definition citydlg.c:220
popup_find_dialog
void popup_find_dialog(void)
Definition finddlg.c:58
flush_dirty
void flush_dirty(void)
Definition mapview.c:468
find_city_window_dlg_callback
static int find_city_window_dlg_callback(struct widget *pwindow)
Definition finddlg.c:59
find_city_dlg
static struct advanced_dialog * find_city_dlg
Definition finddlg.c:54
popdown_find_dialog
void popdown_find_dialog(void)
Definition finddlg.c:110
find_city_callback
static int find_city_callback(struct widget *pwidget)
Definition finddlg.c:90
exit_find_city_dlg_callback
static int exit_find_city_dlg_callback(struct widget *pwidget)
Definition finddlg.c:71
main_window_width
int main_window_width(void)
Definition graphics.c:685
crop_visible_part_from_surface
SDL_Surface * crop_visible_part_from_surface(SDL_Surface *psrc)
Definition graphics.c:1225
main_data
struct sdl2_data main_data
Definition graphics.c:57
main_window_height
int main_window_height(void)
Definition graphics.c:693
ID_TERRAIN_ADV_DLG_WINDOW
@ ID_TERRAIN_ADV_DLG_WINDOW
Definition gui_id.h:201
ID_TERRAIN_ADV_DLG_EXIT_BUTTON
@ ID_TERRAIN_ADV_DLG_EXIT_BUTTON
Definition gui_id.h:202
ID_LABEL
@ ID_LABEL
Definition gui_id.h:27
adj_size
#define adj_size(size)
Definition gui_main.h:141
PRESSED_EVENT
#define PRESSED_EVENT(event)
Definition gui_main.h:71
SF_CENTER
#define SF_CENTER
Definition gui_string.h:40
FONTO_DEFAULT
@ FONTO_DEFAULT
Definition gui_string.h:65
FONTO_ATTENTION
@ FONTO_ATTENTION
Definition gui_string.h:67
create_utf8_from_char_fonto
#define create_utf8_from_char_fonto(string_in, fonto)
Definition gui_string.h:108
get_nation_flag_surface
static SDL_Surface * get_nation_flag_surface(const struct nation_type *pnation)
Definition gui_tilespec.h:227
enable_and_redraw_find_city_button
void enable_and_redraw_find_city_button(void)
Definition mapctrl.c:2235
add_to_gui_list
void add_to_gui_list(Uint16 id, struct widget *gui)
Definition widget.c:586
setup_vertical_widgets_position
int setup_vertical_widgets_position(int step, Sint16 start_x, Sint16 start_y, Uint16 w, Uint16 h, struct widget *begin, struct widget *end)
Definition widget.c:1051
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_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
WF_WIDGET_HAS_INFO_LABEL
@ WF_WIDGET_HAS_INFO_LABEL
Definition widget.h:88
WF_FREE_DATA
@ WF_FREE_DATA
Definition widget.h:78
WF_RESTORE_BACKGROUND
@ WF_RESTORE_BACKGROUND
Definition widget.h:85
WF_HIDDEN
@ WF_HIDDEN
Definition widget.h:68
WF_FREE_THEME
@ WF_FREE_THEME
Definition widget.h:72
WF_DRAW_TEXT_LABEL_WITH_SPACE
@ WF_DRAW_TEXT_LABEL_WITH_SPACE
Definition widget.h:87
set_wflag
void set_wflag(struct widget *pwidget, enum widget_flag flag)
Definition widget_core.c:54
create_themeicon
struct widget * create_themeicon(SDL_Surface *icon_theme, struct gui_layer *pdest, Uint32 flags)
Definition widget_icon.c:212
create_iconlabel
struct widget * create_iconlabel(SDL_Surface *icon, struct gui_layer *pdest, utf8_str *pstr, Uint32 flags)
Definition widget_label.c:215
setup_vertical_scrollbar_area
void setup_vertical_scrollbar_area(struct scroll_bar *scroll, Sint16 start_x, Sint16 start_y, Uint16 height, bool swap_start_x)
Definition widget_scrollbar.c:567
create_vertical_scrollbar
Uint32 create_vertical_scrollbar(struct advanced_dialog *dlg, Uint8 step, Uint8 active, bool create_scrollbar, bool create_buttons)
Definition widget_scrollbar.c:486
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_tilespec.h
widget.h
colors.h
finddlg.h
graphics.h
gui_main.h
mapctrl.h
mapview.h
sprite.h
log.h
map_pos_to_tile
struct tile * map_pos_to_tile(const struct civ_map *nmap, int map_x, int map_y)
Definition map.c:419
index_to_map_pos_y
static int index_to_map_pos_y(int mindex)
Definition map.h:724
index_to_map_pos_x
static int index_to_map_pos_x(int mindex)
Definition map.h:715
canvas_pos_to_tile
struct tile * canvas_pos_to_tile(float canvas_x, float canvas_y, float zoom)
Definition mapview_common.c:763
center_tile_mapcanvas
void center_tile_mapcanvas(const struct tile *ptile)
Definition mapview_common.c:1237
fc_calloc
#define fc_calloc(n, esz)
Definition mem.h:38
FC_FREE
#define FC_FREE(ptr)
Definition mem.h:41
nation_of_player
struct nation_type * nation_of_player(const struct player *pplayer)
Definition nation.c:444
nation.h
player_owns_city
bool player_owns_city(const struct player *pplayer, const struct city *pcity)
Definition player.c:261
players_iterate_end
#define players_iterate_end
Definition player.h:537
players_iterate
#define players_iterate(_pplayer)
Definition player.h:532
MAX
#define MAX(x, y)
Definition shared.h:54
advanced_dialog
Definition widget.h:180
advanced_dialog::begin_active_widget_list
struct widget * begin_active_widget_list
Definition widget.h:184
advanced_dialog::end_widget_list
struct widget * end_widget_list
Definition widget.h:182
advanced_dialog::end_active_widget_list
struct widget * end_active_widget_list
Definition widget.h:185
advanced_dialog::active_widget_list
struct widget * active_widget_list
Definition widget.h:186
advanced_dialog::scroll
struct scroll_bar * scroll
Definition widget.h:187
advanced_dialog::begin_widget_list
struct widget * begin_widget_list
Definition widget.h:181
city
Definition city.h:320
city::tile
struct tile * tile
Definition city.h:322
color::color
GdkRGBA color
Definition colors.h:22
container
Definition widget.h:102
container::id1
int id1
Definition widget.h:104
container::id0
int id0
Definition widget.h:103
player
Definition player.h:247
scroll_bar::count
Uint16 count
Definition widget_scrollbar.h:27
sdl2_data::event
SDL_Event event
Definition graphics.h:217
sdl2_data::map
SDL_Surface * map
Definition graphics.h:210
tile
Definition tile.h:50
tileset
Definition tilespec.c:481
utf8_str
Definition gui_string.h:52
widget
Definition widget.h:112
widget::data
union widget::@192 data
widget::prev
struct widget * prev
Definition widget.h:114
widget::dst
struct gui_layer * dst
Definition widget.h:116
widget::city
struct city * city
Definition widget.h:128
widget::cont
struct container * cont
Definition widget.h:127
widget::action
int(* action)(struct widget *)
Definition widget.h:157
widget::area
SDL_Rect area
Definition widget.h:149
widget::size
SDL_Rect size
Definition widget.h:145
world::map
struct civ_map map
Definition world_object.h:38
fc_snprintf
int fc_snprintf(char *str, size_t n, const char *format,...)
Definition support.c:974
TRUE
#define TRUE
Definition support.h:46
tile_index
#define tile_index(_pt_)
Definition tile.h:88
map_zoom
float map_zoom
Definition zoom.c:25
zoom.h