Freeciv-3.1: /fast/freeciv/nightly/S3_1/client/gui-gtk-4.0/infradlg.c Source File

Freeciv-3.1

Loading...
Searching...
No Matches
infradlg.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 <gtk/gtk.h>
19
20/* utility */
21#include "fcintl.h"
22
23/* common */
24#include "extras.h"
25#include "game.h"
26
27/* client */
28#include "client_main.h"
29#include "dialogs_g.h"
30#include "mapview_common.h"
31
32/* client/gui-gtk-4.0 */
33#include "gui_main.h"
34#include "gui_stuff.h"
35
36#include "infradlg.h"
37
38 static GtkWidget *infra_list_box = NULL;
39 static GtkWidget *instruction_label = NULL;
40 static GtkWidget *points_label = NULL;
41 static int infra_rows = 0;
42
43struct infra_cb_data {
44 struct tile *ptile;
45 struct extra_type *pextra;
46};
47
48/************************************************************************/
51 static bool infra_dialog_open(void)
52{
53 return infra_list_box != NULL;
54}
55
56/************************************************************************/
59 static void infra_response_callback(GtkWidget *dlg, gint arg)
60{
61 infra_list_box = NULL;
62 instruction_label = NULL;
63 points_label = NULL;
64 infra_rows = 0;
65
66 client_infratile_set(NULL);
67
68 gtk_window_destroy(GTK_WINDOW(dlg));
69}
70
71/************************************************************************/
74 static void infra_selected_callback(GtkButton *but, gpointer userdata)
75{
76 struct infra_cb_data *cbdata = (struct infra_cb_data *)userdata;
77
78 dsend_packet_player_place_infra(&client.conn, cbdata->ptile->index,
79 cbdata->pextra->id);
80}
81
82/************************************************************************/
85 void infra_dialog_popup(void)
86{
87 GtkWidget *dlg;
88 GtkWidget *main_box;
89 GtkWidget *sep;
90 int grid_row = 0;
91
92 if (infra_dialog_open()) {
93 /* One infra dialog already open. */
94 return;
95 }
96
97 dlg = gtk_dialog_new_with_buttons(_("Place infrastructure"), NULL, 0,
98 _("Close"), GTK_RESPONSE_NO,
99 NULL);
100
101 setup_dialog(dlg, toplevel);
102 gtk_dialog_set_default_response(GTK_DIALOG(dlg), GTK_RESPONSE_NO);
103 gtk_window_set_destroy_with_parent(GTK_WINDOW(dlg), TRUE);
104
105 main_box = gtk_grid_new();
106 gtk_orientable_set_orientation(GTK_ORIENTABLE(main_box),
107 GTK_ORIENTATION_VERTICAL);
108
109 instruction_label = gtk_label_new(_("First click a tile."));
110 gtk_grid_attach(GTK_GRID(main_box), instruction_label, 0, grid_row++, 1, 1);
111
112 sep = gtk_separator_new(GTK_ORIENTATION_HORIZONTAL);
113 gtk_grid_attach(GTK_GRID(main_box), sep, 0, grid_row++, 1, 1);
114
115 points_label = gtk_label_new(_("- infrapoints"));
116 gtk_grid_attach(GTK_GRID(main_box), points_label, 0, grid_row++, 1, 1);
117
118 sep = gtk_separator_new(GTK_ORIENTATION_HORIZONTAL);
119 gtk_grid_attach(GTK_GRID(main_box), sep, 0, grid_row++, 1, 1);
120
121 infra_list_box = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
122 gtk_grid_attach(GTK_GRID(main_box), infra_list_box, 0, grid_row++, 1, 1);
123
124 gtk_box_append(GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dlg))),
125 main_box);
126
127 g_signal_connect(dlg, "destroy", G_CALLBACK(infra_response_callback), NULL);
128 g_signal_connect(dlg, "response", G_CALLBACK(infra_response_callback), NULL);
129
130 gtk_widget_show(gtk_dialog_get_content_area(GTK_DIALOG(dlg)));
131 gtk_widget_show(dlg);
132
133 update_infra_dialog();
134}
135
136/************************************************************************/
139 void update_infra_dialog(void)
140{
141 if (infra_dialog_open()) {
142 char buffer[100];
143
144 fc_snprintf(buffer, sizeof(buffer),
145 PL_("%d infrapoint", "%d infrapoints",
146 client.conn.playing->economic.infra_points),
147 client.conn.playing->economic.infra_points);
148
149 gtk_label_set_text(GTK_LABEL(points_label), buffer);
150 }
151}
152
153/************************************************************************/
156 bool infra_placement_mode(void)
157{
158 return infra_list_box != NULL;
159}
160
161/************************************************************************/
164 void infra_placement_set_tile(struct tile *ptile)
165{
166 if (infra_list_box != NULL) {
167 GtkWidget *child = gtk_widget_get_first_child(infra_list_box);
168
169 while (child != NULL) {
170 gtk_box_remove(GTK_BOX(infra_list_box), child);
171 child = gtk_widget_get_first_child(infra_list_box);
172 }
173 }
174 infra_rows = 0;
175
176 if (!client_map_is_known_and_seen(ptile, client.conn.playing, V_MAIN)) {
177 return;
178 }
179
180 client_infratile_set(ptile);
181
182 extra_type_iterate(pextra) {
183 if (player_can_place_extra(pextra, client.conn.playing, ptile)) {
184 GtkWidget *but = gtk_button_new_with_label(extra_name_translation(pextra));
185 struct infra_cb_data *cbdata = fc_malloc(sizeof(struct infra_cb_data));
186
187 cbdata->ptile = ptile;
188 cbdata->pextra = pextra;
189
190 g_signal_connect(but, "clicked",
191 G_CALLBACK(infra_selected_callback), cbdata);
192 gtk_box_append(GTK_BOX(infra_list_box), but);
193 infra_rows++;
194 }
195 } extra_type_iterate_end;
196
197 if (infra_rows <= 0) {
198 gtk_label_set_text(GTK_LABEL(instruction_label),
199 _("No infra possible. Select another tile."));
200 } else {
201 gtk_label_set_text(GTK_LABEL(instruction_label),
202 _("Select infra for the tile, or another tile."));
203 }
204
205 gtk_widget_show(infra_list_box);
206}
client_map_is_known_and_seen
bool client_map_is_known_and_seen(const struct tile *ptile, const struct player *pplayer, enum vision_layer vlayer)
Definition client_main.c:1374
client
struct civclient client
Definition client_main.c:149
client_main.h
dialogs_g.h
player_can_place_extra
bool player_can_place_extra(const struct extra_type *pextra, const struct player *pplayer, const struct tile *ptile)
Definition extras.c:462
extra_name_translation
const char * extra_name_translation(const struct extra_type *pextra)
Definition extras.c:186
extras.h
extra_type_iterate
#define extra_type_iterate(_p)
Definition extras.h:291
extra_type_iterate_end
#define extra_type_iterate_end
Definition extras.h:297
fcintl.h
PL_
#define PL_(String1, String2, n)
Definition fcintl.h:71
_
#define _(String)
Definition fcintl.h:67
game.h
toplevel
GtkWidget * toplevel
Definition gui_main.c:124
setup_dialog
void setup_dialog(GtkWidget *shell, GtkWidget *parent)
Definition gui_stuff.c:281
update_infra_dialog
void update_infra_dialog(void)
Definition infradlg.c:24
instruction_label
static GtkWidget * instruction_label
Definition infradlg.c:39
infra_response_callback
static void infra_response_callback(GtkWidget *dlg, gint arg)
Definition infradlg.c:59
infra_placement_mode
bool infra_placement_mode(void)
Definition infradlg.c:155
infra_dialog_open
static bool infra_dialog_open(void)
Definition infradlg.c:51
infra_selected_callback
static void infra_selected_callback(GtkButton *but, gpointer userdata)
Definition infradlg.c:74
infra_placement_set_tile
void infra_placement_set_tile(struct tile *ptile)
Definition infradlg.c:163
points_label
static GtkWidget * points_label
Definition infradlg.c:40
infra_rows
static int infra_rows
Definition infradlg.c:41
infra_dialog_popup
void infra_dialog_popup(void)
Definition infradlg.c:85
gui_stuff.h
infra_list_box
static GtkWidget * infra_list_box
Definition infradlg.c:38
infradlg.h
gui_main.h
client_infratile_set
void client_infratile_set(struct tile *ptile)
Definition mapview_common.c:4098
mapview_common.h
fc_malloc
#define fc_malloc(sz)
Definition mem.h:34
dsend_packet_player_place_infra
int dsend_packet_player_place_infra(struct connection *pc, int tile, int extra)
Definition packets_gen.c:35770
civclient::conn
struct connection conn
Definition client_main.h:96
connection::playing
struct player * playing
Definition connection.h:156
extra_type
Definition extras.h:85
extra_type::id
int id
Definition extras.h:86
infra_cb_data
Definition infradlg.c:43
infra_cb_data::pextra
struct extra_type * pextra
Definition infradlg.c:45
infra_cb_data::ptile
struct tile * ptile
Definition infradlg.c:44
player_economic::infra_points
int infra_points
Definition player.h:74
player::economic
struct player_economic economic
Definition player.h:284
tile
Definition tile.h:49
tile::index
int index
Definition tile.h:50
fc_snprintf
int fc_snprintf(char *str, size_t n, const char *format,...)
Definition support.c:969
TRUE
#define TRUE
Definition support.h:46