Freeciv-3.3
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-4.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
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) {
95 break;
96
97 case CMD_AIRLIFT:
98 {
100
101 if (pdestcity) {
105 }
107 }
108 }
109 break;
110
111 case CMD_GOTO:
112 {
113 struct city *pdestcity = get_selected_city();
114
115 if (pdestcity) {
119 }
120 }
121 break;
122
123 default:
124 break;
125 }
126
128 dshell = NULL;
129}
130
131/**********************************************************************/
134static void create_goto_dialog(void)
135{
136 GtkWidget *sw, *label, *frame, *vbox;
139
140 dshell = gtk_dialog_new_with_buttons(_("Goto/Airlift Unit"),
141 NULL,
142 0,
143 _("_Cancel"),
145 _("Air_lift"),
147 _("_Goto"),
148 CMD_GOTO,
149 NULL);
152 g_signal_connect(dshell, "destroy",
154 g_signal_connect(dshell, "response",
156
157 source = gtk_label_new("" /* Filled in later */);
161 source, NULL);
162
164 "use-underline", TRUE,
165 "label", _("Select destination ci_ty"),
166 "xalign", 0.0,
167 "yalign", 0.5,
168 NULL);
169 frame = gtk_frame_new("");
172 frame, NULL);
173
175 gtk_frame_set_child(GTK_FRAME(frame), vbox);
176
182
191
192 /* Set the mnemonic in the frame label to focus the city list */
194
195#ifdef FREECIV_DEBUG
198 "text", GD_COL_CITY_ID, NULL);
201#endif /* FREECIV_DEBUG */
202
205 "text", GD_COL_CITY_NAME, NULL);
208
211 "pixbuf", GD_COL_FLAG, NULL);
213
216 "text", GD_COL_NATION,
217 NULL);
220
223 "text", GD_COL_AIRLIFT,
224 NULL);
227
233
234 gtk_box_append(GTK_BOX(vbox), sw);
235
236 all_toggle = gtk_check_button_new_with_mnemonic(_("Show _All Cities"));
238
239 g_signal_connect(all_toggle, "toggled",
241
244
246
248
252}
253
254/**********************************************************************/
258{
260 return;
261 }
262
263 if (!dshell) {
265 }
266
268}
269
270/**********************************************************************/
273static struct city *get_selected_city(void)
274{
275 GtkTreeModel *model;
276 GtkTreeIter it;
277 int city_id;
278
280 return NULL;
281 }
282
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/**********************************************************************/
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);
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,
313 GD_COL_NATION, nation,
314 /* GD_COL_AIRLIFT is populated later */
315 -1);
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;
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 */
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) {
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();
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)"),
387 } else {
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);
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/**********************************************************************/
432{
433 bool nonempty = FALSE;
434
435 if (!client_has_player()) {
436 /* Case global observer. */
437 return;
438 }
439
441
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{
469 bool valid;
470
472 while (valid) {
473 int city_id;
474 const struct city *pcity;
475 const char *air_text;
476
478 GD_COL_CITY_ID, &city_id, -1);
479 pcity = game_city_by_number(city_id);
483 GD_COL_AIRLIFT, air_text ? air_text : "-", -1);
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. */
503 break;
504 }
506
507 if (can_airlift) {
510 return;
511 }
512 }
514}
515
516/**********************************************************************/
521 gpointer data)
522{
523 struct city *pdestcity;
524
525 if (gotodlg_updating) {
526 return;
527 }
528
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:148
void astr_set(struct astring *astr, const char *format,...)
Definition astring.c:251
const char * astr_build_and_list(struct astring *astr, const char *const *items, size_t number)
Definition astring.c:351
void astr_init(struct astring *astr)
Definition astring.c:139
void astr_add(struct astring *astr, const char *format,...)
Definition astring.c:271
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:1137
#define city_list_iterate(citylist, pcity)
Definition city.h:505
#define city_tile(_pcity_)
Definition city.h:561
#define city_list_iterate_end
Definition city.h:507
bool can_client_issue_orders(void)
bool client_has_player(void)
#define client_player()
static struct fc_sockaddr_list * list
Definition clinet.c:102
char * incite_cost
Definition comments.c:76
void request_unit_airlift(struct unit *punit, struct city *pcity)
Definition control.c:1543
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:74
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 int const struct action *paction struct unit struct city * pcity
Definition dialogs_g.h:78
#define Q_(String)
Definition fcintl.h:70
#define _(String)
Definition fcintl.h:67
struct world wld
Definition game.c:62
struct city * game_city_by_number(int id)
Definition game.c:106
bool send_goto_tile(struct unit *punit, struct tile *ptile)
Definition goto.c:1569
static GtkListStore * goto_list_store
Definition gotodlg.c:60
static void update_source_label(void)
Definition gotodlg.c:327
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:467
void goto_dialog_focus_units_changed(void)
Definition gotodlg.c:541
struct tile * original_tile
Definition gotodlg.c:62
void popup_goto_dialog(void)
Definition gotodlg.c:258
static GtkWidget * dshell
Definition gotodlg.c:56
static bool list_store_append_player_cities(GtkListStore *store, const struct player *pplayer)
Definition gotodlg.c:294
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:274
@ 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
@ CMD_AIRLIFT
Definition gotodlg.c:74
@ CMD_GOTO
Definition gotodlg.c:74
static void refresh_airlift_button(void)
Definition gotodlg.c:493
static GtkWidget * source
Definition gotodlg.c:58
static GtkWidget * view
Definition gotodlg.c:57
static void create_goto_dialog(void)
Definition gotodlg.c:135
static void goto_selection_callback(GtkTreeSelection *selection, gpointer data)
Definition gotodlg.c:521
GtkWidget * toplevel
Definition gui_main.c:126
void gtk_tree_view_focus(GtkTreeView *view)
Definition gui_stuff.c:236
void setup_dialog(GtkWidget *shell, GtkWidget *parent)
Definition gui_stuff.c:287
GdkPixbuf * get_flag(const struct nation_type *nation)
Definition plrdlg.c:607
void widget_destroyed(GtkWidget *wdg, void *data)
Definition gui_stuff.c:1155
static void update_goto_dialog(void)
Definition gotodlg.c:135
#define fc_assert_ret(condition)
Definition log.h:192
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:149
struct nation_type * nation_of_player(const struct player *pplayer)
Definition nation.c:444
struct city_list * cities
Definition packhand.c:119
#define players_iterate_end
Definition player.h:542
#define players_iterate(_pplayer)
Definition player.h:537
Definition city.h:317
struct city_list * cities
Definition player.h:281
Definition tile.h:50
int id
Definition unit.h:147
struct tile * tile
Definition unit.h:142
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:592
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:186
#define unit_tile(_pu)
Definition unit.h:404
#define unit_list_iterate(unitlist, punit)
Definition unitlist.h:31
#define unit_list_iterate_end
Definition unitlist.h:33