Freeciv-3.1
Loading...
Searching...
No Matches
gotodlg.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 <stdio.h>
19#include <stdlib.h>
20#include <string.h>
21
22#include <gtk/gtk.h>
23#include <gdk/gdkkeysyms.h>
24
25/* utility */
26#include "astring.h"
27#include "fcintl.h"
28#include "log.h"
29#include "support.h"
30
31/* common */
32#include "game.h"
33#include "map.h"
34#include "packets.h"
35#include "player.h"
36#include "unit.h"
37#include "unitlist.h"
38
39/* client */
40#include "client_main.h"
41#include "control.h"
42#include "goto.h"
43#include "options.h"
44#include "text.h"
45
46/* clien/gui-gtk-3.0 */
47#include "plrdlg.h"
48#include "dialogs.h"
49#include "gui_main.h"
50#include "gui_stuff.h"
51#include "mapview.h"
52
53#include "gotodlg.h"
54
55
56static GtkWidget *dshell = NULL;
57static GtkWidget *view;
58static GtkWidget *source;
59static GtkWidget *all_toggle;
60static GtkListStore *goto_list_store;
61static GtkTreeSelection *goto_list_selection;
63static bool gotodlg_updating = FALSE;
64
65static void update_goto_dialog(GtkToggleButton *button);
66static void update_source_label(void);
67static void refresh_airlift_column(void);
68static void refresh_airlift_button(void);
69static void goto_selection_callback(GtkTreeSelection *selection, gpointer data);
70
71static struct city *get_selected_city(void);
72
73enum {
75};
76
77enum {
78 GD_COL_CITY_ID = 0, /* Not shown if not compiled with --enable-debug. */
83
85};
86
87/**********************************************************************/
90static void goto_cmd_callback(GtkWidget *dlg, gint arg)
91{
92 switch (arg) {
93 case GTK_RESPONSE_CANCEL:
95 break;
96
97 case CMD_AIRLIFT:
98 {
99 struct city *pdestcity = get_selected_city();
100
101 if (pdestcity) {
103 if (unit_can_airlift_to(&(wld.map), punit, pdestcity)) {
104 request_unit_airlift(punit, pdestcity);
105 }
107 }
108 }
109 break;
110
111 case CMD_GOTO:
112 {
113 struct city *pdestcity = get_selected_city();
114
115 if (pdestcity) {
117 send_goto_tile(punit, pdestcity->tile);
119 }
120 }
121 break;
122
123 default:
124 break;
125 }
126
127 gtk_widget_destroy(dlg);
128 dshell = NULL;
129}
130
131/**********************************************************************/
134static void create_goto_dialog(void)
135{
136 GtkWidget *sw, *label, *frame, *vbox;
137 GtkCellRenderer *rend;
138 GtkTreeViewColumn *col;
139
140 dshell = gtk_dialog_new_with_buttons(_("Goto/Airlift Unit"),
141 NULL,
142 0,
143 GTK_STOCK_CANCEL,
144 GTK_RESPONSE_CANCEL,
145 _("Air_lift"),
147 _("_Goto"),
148 CMD_GOTO,
149 NULL);
151 gtk_window_set_position(GTK_WINDOW(dshell), GTK_WIN_POS_MOUSE);
152 gtk_dialog_set_default_response(GTK_DIALOG(dshell), CMD_GOTO);
153 g_signal_connect(dshell, "destroy",
154 G_CALLBACK(gtk_widget_destroyed), &dshell);
155 g_signal_connect(dshell, "response",
156 G_CALLBACK(goto_cmd_callback), NULL);
157
158 source = gtk_label_new("" /* filled in later */);
159 gtk_label_set_line_wrap(GTK_LABEL(source), TRUE);
160 gtk_label_set_justify(GTK_LABEL(source), GTK_JUSTIFY_CENTER);
161 gtk_box_pack_start(GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dshell))),
162 source, FALSE, FALSE, 0);
163
164 label = g_object_new(GTK_TYPE_LABEL,
165 "use-underline", TRUE,
166 "label", _("Select destination ci_ty"),
167 "xalign", 0.0,
168 "yalign", 0.5,
169 NULL);
170 frame = gtk_frame_new("");
171 gtk_frame_set_label_widget(GTK_FRAME(frame), label);
172 gtk_box_pack_start(GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dshell))),
173 frame, TRUE, TRUE, 0);
174
175 vbox = gtk_grid_new();
176 gtk_orientable_set_orientation(GTK_ORIENTABLE(vbox),
177 GTK_ORIENTATION_VERTICAL);
178 gtk_grid_set_row_spacing(GTK_GRID(vbox), 6);
179 gtk_container_add(GTK_CONTAINER(frame), vbox);
180
181 goto_list_store = gtk_list_store_new(GD_COL_NUM, G_TYPE_INT, G_TYPE_STRING,
182 GDK_TYPE_PIXBUF, G_TYPE_STRING, G_TYPE_STRING);
183 gtk_tree_sortable_set_sort_column_id(GTK_TREE_SORTABLE(goto_list_store),
184 GD_COL_CITY_NAME, GTK_SORT_ASCENDING);
185
186 view = gtk_tree_view_new_with_model(GTK_TREE_MODEL(goto_list_store));
187 gtk_widget_set_hexpand(view, TRUE);
188 gtk_widget_set_vexpand(view, TRUE);
189 g_object_unref(goto_list_store);
190 goto_list_selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(view));
191 gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(view), TRUE);
192 gtk_tree_view_set_search_column(GTK_TREE_VIEW(view), GD_COL_CITY_NAME);
193 gtk_tree_view_set_enable_search(GTK_TREE_VIEW(view), TRUE);
194
195 /* Set the mnemonic in the frame label to focus the city list */
196 gtk_label_set_mnemonic_widget(GTK_LABEL(label), view);
197
198#ifdef FREECIV_DEBUG
199 rend = gtk_cell_renderer_text_new();
200 col = gtk_tree_view_column_new_with_attributes(_("Id"), rend,
201 "text", GD_COL_CITY_ID, NULL);
202 gtk_tree_view_append_column(GTK_TREE_VIEW(view), col);
203 gtk_tree_view_column_set_sort_column_id(col, GD_COL_CITY_ID);
204#endif /* FREECIV_DEBUG */
205
206 rend = gtk_cell_renderer_text_new();
207 col = gtk_tree_view_column_new_with_attributes(_("City"), rend,
208 "text", GD_COL_CITY_NAME, NULL);
209 gtk_tree_view_append_column(GTK_TREE_VIEW(view), col);
210 gtk_tree_view_column_set_sort_column_id(col, GD_COL_CITY_NAME);
211
212 rend = gtk_cell_renderer_pixbuf_new();
213 col = gtk_tree_view_column_new_with_attributes(NULL, rend,
214 "pixbuf", GD_COL_FLAG, NULL);
215 gtk_tree_view_append_column(GTK_TREE_VIEW(view), col);
216
217 rend = gtk_cell_renderer_text_new();
218 col = gtk_tree_view_column_new_with_attributes(_("Nation"), rend,
219 "text", GD_COL_NATION, NULL);
220 gtk_tree_view_append_column(GTK_TREE_VIEW(view), col);
221 gtk_tree_view_column_set_sort_column_id(col, GD_COL_NATION);
222
223 rend = gtk_cell_renderer_text_new();
224 col = gtk_tree_view_column_new_with_attributes(_("Airlift"), rend,
225 "text", GD_COL_AIRLIFT, NULL);
226 gtk_tree_view_append_column(GTK_TREE_VIEW(view), col);
227 gtk_tree_view_column_set_sort_column_id(col, GD_COL_AIRLIFT);
228
229 sw = gtk_scrolled_window_new(NULL, NULL);
230 gtk_container_add(GTK_CONTAINER(sw), view);
231 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(sw),
232 GTK_POLICY_NEVER, GTK_POLICY_ALWAYS);
233 gtk_scrolled_window_set_min_content_height(GTK_SCROLLED_WINDOW(sw), 200);
234
235 gtk_container_add(GTK_CONTAINER(vbox), sw);
236
237 all_toggle = gtk_check_button_new_with_mnemonic(_("Show _All Cities"));
238 gtk_container_add(GTK_CONTAINER(vbox), all_toggle);
239
240 g_signal_connect(all_toggle, "toggled", G_CALLBACK(update_goto_dialog), NULL);
241
242 g_signal_connect(goto_list_selection, "changed",
243 G_CALLBACK(goto_selection_callback), NULL);
244
245 gtk_widget_show_all(dshell);
246
248
250 update_goto_dialog(GTK_TOGGLE_BUTTON(all_toggle));
251 gtk_tree_view_focus(GTK_TREE_VIEW(view));
252}
253
254/**********************************************************************/
258{
260 return;
261 }
262
263 if (!dshell) {
265 }
266
267 gtk_window_present(GTK_WINDOW(dshell));
268}
269
270/**********************************************************************/
273static struct city *get_selected_city(void)
274{
275 GtkTreeModel *model;
276 GtkTreeIter it;
277 int city_id;
278
279 if (!gtk_tree_selection_get_selected(goto_list_selection, NULL, &it)) {
280 return NULL;
281 }
282
283 model = gtk_tree_view_get_model(GTK_TREE_VIEW(view));
284
285 gtk_tree_model_get(model, &it, GD_COL_CITY_ID, &city_id, -1);
286
287 return game_city_by_number(city_id);
288}
289
290/**********************************************************************/
293static bool list_store_append_player_cities(GtkListStore *store,
294 const struct player *pplayer)
295{
296 GtkTreeIter it;
297 struct nation_type *pnation = nation_of_player(pplayer);
298 const char *nation = nation_adjective_translation(pnation);
299 GdkPixbuf *pixbuf;
300
301 if (city_list_size(pplayer->cities) == 0) {
302 return FALSE;
303 }
304
305 pixbuf = get_flag(pnation);
306
307 city_list_iterate(pplayer->cities, pcity) {
308 gtk_list_store_append(store, &it);
309 gtk_list_store_set(store, &it,
310 GD_COL_CITY_ID, pcity->id,
312 GD_COL_FLAG, pixbuf,
313 GD_COL_NATION, nation,
314 /* GD_COL_AIRLIFT is populated later */
315 -1);
317 g_object_unref(pixbuf);
318
319 return TRUE;
320}
321
322/**********************************************************************/
326static void update_source_label(void)
327{
328 /* Arbitrary limit to stop the label getting ridiculously long */
329 static const int max_cities = 10;
330 struct {
331 const struct city *city;
332 struct unit_list *units;
333 } cities[max_cities];
334 int ncities = 0;
335 bool too_many = FALSE;
336 bool no_city = FALSE; /* any units not in a city? */
337 struct astring strs[max_cities];
338 int nstrs;
339 char *last_str;
340 const char *descriptions[max_cities+1];
341 int i;
342
343 /* Sanity check: if no units selected, give up */
344 if (unit_list_size(get_units_in_focus()) == 0) {
345 gtk_label_set_text(GTK_LABEL(source), _("No units selected."));
346 return;
347 }
348
349 /* Divide selected units up into a list of unique cities */
351 const struct city *pcity = tile_city(unit_tile(punit));
352 if (pcity) {
353 /* Inefficient, but it's not a long list */
354 for (i = 0; i < ncities; i++) {
355 if (cities[i].city == pcity) {
356 unit_list_append(cities[i].units, punit);
357 break;
358 }
359 }
360 if (i == ncities) {
361 if (ncities < max_cities) {
362 cities[ncities].city = pcity;
363 cities[ncities].units = unit_list_new();
364 unit_list_append(cities[ncities].units, punit);
365 ncities++;
366 } else {
367 too_many = TRUE;
368 break;
369 }
370 }
371 } else {
372 no_city = TRUE;
373 }
375
376 /* Describe the individual cities. */
377 for (i = 0; i < ncities; i++) {
378 const char *air_text = get_airlift_text(cities[i].units, NULL);
379
380 astr_init(&strs[i]);
381 if (air_text != NULL) {
382 astr_add(&strs[i],
383 /* TRANS: goto/airlift dialog. "Paris (airlift: 2/4)".
384 * A set of these appear in an "and"-separated list. */
385 _("%s (airlift: %s)"),
386 city_name_get(cities[i].city), air_text);
387 } else {
388 astr_add(&strs[i], "%s", city_name_get(cities[i].city));
389 }
390 descriptions[i] = astr_str(&strs[i]);
391 unit_list_destroy(cities[i].units);
392 }
393 if (too_many) {
394 /* TRANS: goto/airlift dialog. Too many cities to list, some omitted.
395 * Appears at the end of an "and"-separated list. */
396 descriptions[ncities] = last_str = fc_strdup(Q_("?gotodlg:more"));
397 nstrs = ncities+1;
398 } else if (no_city) {
399 /* TRANS: goto/airlift dialog. For units not currently in a city.
400 * Appears at the end of an "and"-separated list. */
401 descriptions[ncities] = last_str = fc_strdup(Q_("?gotodlg:no city"));
402 nstrs = ncities+1;
403 } else {
404 last_str = NULL;
405 nstrs = ncities;
406 }
407
408 /* Finally, update the label. */
409 {
410 struct astring label = ASTRING_INIT, list = ASTRING_INIT;
411 astr_set(&label,
412 /* TRANS: goto/airlift dialog. Current location of units; %s is an
413 * "and"-separated list of cities and associated info */
414 _("Currently in: %s"),
415 astr_build_and_list(&list, descriptions, nstrs));
416 astr_free(&list);
417 gtk_label_set_text(GTK_LABEL(source), astr_str(&label));
418 astr_free(&label);
419 }
420
421 /* Clear up. */
422 for (i = 0; i < ncities; i++) {
423 astr_free(&strs[i]);
424 }
425 free(last_str); /* might have been NULL */
426}
427
428/**********************************************************************/
431static void update_goto_dialog(GtkToggleButton *button)
432{
433 bool nonempty = FALSE;
434
435 if (!client_has_player()) {
436 /* Case global observer. */
437 return;
438 }
439
441
442 gtk_list_store_clear(goto_list_store);
443
444 if (gtk_toggle_button_get_active(button)) {
445 players_iterate(pplayer) {
448 } else {
450 }
451
453
455
456 if (!nonempty) {
457 /* No selection causes callbacks to fire, causing also Airlift button
458 * to update. Do it here. */
460 }
461}
462
463/**********************************************************************/
466static void refresh_airlift_column(void)
467{
468 GtkTreeIter iter;
469 bool valid;
470
471 valid = gtk_tree_model_get_iter_first(GTK_TREE_MODEL(goto_list_store), &iter);
472 while (valid) {
473 int city_id;
474 const struct city *pcity;
475 const char *air_text;
476
477 gtk_tree_model_get(GTK_TREE_MODEL(goto_list_store), &iter,
478 GD_COL_CITY_ID, &city_id, -1);
479 pcity = game_city_by_number(city_id);
480 fc_assert_ret(pcity != NULL);
481 air_text = get_airlift_text(get_units_in_focus(), pcity);
482 gtk_list_store_set(GTK_LIST_STORE(goto_list_store), &iter,
483 GD_COL_AIRLIFT, air_text ? air_text : "-", -1);
484 valid = gtk_tree_model_iter_next(GTK_TREE_MODEL(goto_list_store), &iter);
485 }
486}
487
488/**********************************************************************/
492static void refresh_airlift_button(void)
493{
494 struct city *pdestcity = get_selected_city();
495
496 if (NULL != pdestcity) {
497 bool can_airlift = FALSE;
498
499 /* Allow action if any of the selected units can airlift. */
501 if (unit_can_airlift_to(&(wld.map), punit, pdestcity)) {
502 can_airlift = TRUE;
503 break;
504 }
506
507 if (can_airlift) {
508 gtk_dialog_set_response_sensitive(GTK_DIALOG(dshell),
510 return;
511 }
512 }
513 gtk_dialog_set_response_sensitive(GTK_DIALOG(dshell), CMD_AIRLIFT, FALSE);
514}
515
516/**********************************************************************/
520static void goto_selection_callback(GtkTreeSelection *selection,
521 gpointer data)
522{
523 struct city *pdestcity;
524
525 if (gotodlg_updating) {
526 return;
527 }
528
529 pdestcity = get_selected_city();
530
531 if (NULL != pdestcity) {
533 }
535}
536
537/**********************************************************************/
541{
542 /* Is the dialog currently being displayed? */
543 if (dshell) {
544 /* Location of current units and ability to airlift may have changed */
548 }
549}
void astr_free(struct astring *astr)
Definition astring.c:153
void astr_set(struct astring *astr, const char *format,...)
Definition astring.c:267
const char * astr_build_and_list(struct astring *astr, const char *const *items, size_t number)
Definition astring.c:367
void astr_init(struct astring *astr)
Definition astring.c:144
void astr_add(struct astring *astr, const char *format,...)
Definition astring.c:287
static const char * astr_str(const struct astring *astr) fc__attribute((nonnull(1)))
Definition astring.h:93
#define ASTRING_INIT
Definition astring.h:44
const char * city_name_get(const struct city *pcity)
Definition city.c:1115
#define city_list_iterate(citylist, pcity)
Definition city.h:488
#define city_tile(_pcity_)
Definition city.h:544
#define city_list_iterate_end
Definition city.h:490
bool can_client_issue_orders(void)
bool client_has_player(void)
#define client_player()
static struct fc_sockaddr_list * list
Definition clinet.c:102
void request_unit_airlift(struct unit *punit, struct city *pcity)
Definition control.c:1538
struct unit_list * get_units_in_focus(void)
Definition control.c:177
int get_num_units_in_focus(void)
Definition control.c:185
struct unit struct city struct unit struct tile struct extra_type const struct act_prob *act_probs int actor_unit_id struct unit struct unit * punit
Definition dialogs_g.h:73
#define Q_(String)
Definition fcintl.h:70
#define _(String)
Definition fcintl.h:67
struct world wld
Definition game.c:58
struct city * game_city_by_number(int id)
Definition game.c:102
bool send_goto_tile(struct unit *punit, struct tile *ptile)
Definition goto.c:1551
static GtkListStore * goto_list_store
Definition gotodlg.c:60
static void update_source_label(void)
Definition gotodlg.c:326
static void goto_cmd_callback(GtkWidget *dlg, gint arg)
Definition gotodlg.c:90
static GtkTreeSelection * goto_list_selection
Definition gotodlg.c:61
static void refresh_airlift_column(void)
Definition gotodlg.c:466
void goto_dialog_focus_units_changed(void)
Definition gotodlg.c:540
struct tile * original_tile
Definition gotodlg.c:62
void popup_goto_dialog(void)
Definition gotodlg.c:257
static GtkWidget * dshell
Definition gotodlg.c:56
static bool list_store_append_player_cities(GtkListStore *store, const struct player *pplayer)
Definition gotodlg.c:293
static GtkWidget * all_toggle
Definition gotodlg.c:59
static bool gotodlg_updating
Definition gotodlg.c:63
static struct city * get_selected_city(void)
Definition gotodlg.c:273
@ CMD_AIRLIFT
Definition gotodlg.c:74
@ CMD_GOTO
Definition gotodlg.c:74
@ GD_COL_CITY_ID
Definition gotodlg.c:78
@ GD_COL_FLAG
Definition gotodlg.c:80
@ GD_COL_AIRLIFT
Definition gotodlg.c:82
@ GD_COL_NATION
Definition gotodlg.c:81
@ GD_COL_CITY_NAME
Definition gotodlg.c:79
@ GD_COL_NUM
Definition gotodlg.c:84
static void refresh_airlift_button(void)
Definition gotodlg.c:492
static GtkWidget * source
Definition gotodlg.c:58
static GtkWidget * view
Definition gotodlg.c:57
static void create_goto_dialog(void)
Definition gotodlg.c:134
static void goto_selection_callback(GtkTreeSelection *selection, gpointer data)
Definition gotodlg.c:520
GtkWidget * toplevel
Definition gui_main.c:124
void gtk_tree_view_focus(GtkTreeView *view)
Definition gui_stuff.c:230
void setup_dialog(GtkWidget *shell, GtkWidget *parent)
Definition gui_stuff.c:281
GdkPixbuf * get_flag(const struct nation_type *nation)
Definition plrdlg.c:609
static void update_goto_dialog(void)
Definition gotodlg.c:134
#define fc_assert_ret(condition)
Definition log.h:191
struct tile * get_center_tile_mapcanvas(void)
void center_tile_mapcanvas(const struct tile *ptile)
#define fc_strdup(str)
Definition mem.h:43
const char * nation_adjective_translation(const struct nation_type *pnation)
Definition nation.c:148
struct nation_type * nation_of_player(const struct player *pplayer)
Definition nation.c:443
struct city_list * cities
Definition packhand.c:117
#define players_iterate_end
Definition player.h:535
#define players_iterate(_pplayer)
Definition player.h:530
Definition city.h:309
struct tile * tile
Definition city.h:311
struct city_list * cities
Definition player.h:281
Definition tile.h:49
struct civ_map map
#define TRUE
Definition support.h:46
#define FALSE
Definition support.h:47
const char * get_airlift_text(const struct unit_list *punits, const struct city *pdest)
Definition text.c:578
struct city * tile_city(const struct tile *ptile)
Definition tile.c:83
bool unit_can_airlift_to(const struct civ_map *nmap, const struct unit *punit, const struct city *pdest_city)
Definition unit.c:188
#define unit_tile(_pu)
Definition unit.h:395
#define unit_list_iterate(unitlist, punit)
Definition unitlist.h:31
#define unit_list_iterate_end
Definition unitlist.h:33