Replaced deprecated gtk_menu_popup() calls with modern constructs in gtk3.22-client
[freeciv.git] / client / gui-gtk-3.0 / inteldlg.c
blob4966673fc90040a2e3e21b1027ab71406775f526
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.
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 ***********************************************************************/
14 #ifdef HAVE_CONFIG_H
15 #include <fc_config.h>
16 #endif
18 #include <stdio.h>
19 #include <stdlib.h>
21 #include <gtk/gtk.h>
23 /* utility */
24 #include "fcintl.h"
25 #include "log.h"
26 #include "shared.h"
27 #include "support.h"
29 /* common */
30 #include "government.h"
31 #include "packets.h"
32 #include "player.h"
33 #include "research.h"
35 /* client */
36 #include "client_main.h"
37 #include "options.h"
39 /* client/gui-gtk-3.0 */
40 #include "gui_main.h"
41 #include "gui_stuff.h"
42 #include "mapview.h"
44 #include "inteldlg.h"
46 /******************************************************************/
47 static const char *table_text[] = {
48 N_("Ruler:"),
49 N_("Government:"),
50 N_("Capital:"),
51 N_("Gold:"),
52 NULL,
53 N_("Tax:"),
54 N_("Science:"),
55 N_("Luxury:"),
56 NULL,
57 N_("Researching:")
60 enum table_label {
61 LABEL_RULER,
62 LABEL_GOVERNMENT,
63 LABEL_CAPITAL,
64 LABEL_GOLD,
65 LABEL_SEP1,
66 LABEL_TAX,
67 LABEL_SCIENCE,
68 LABEL_LUXURY,
69 LABEL_SEP2,
70 LABEL_RESEARCHING,
71 LABEL_LAST
74 /******************************************************************/
75 struct intel_dialog {
76 struct player *pplayer;
77 GtkWidget *shell;
79 GtkTreeStore *diplstates;
80 GtkListStore *techs;
81 GtkWidget *table_labels[LABEL_LAST];
84 #define SPECLIST_TAG dialog
85 #define SPECLIST_TYPE struct intel_dialog
86 #include "speclist.h"
88 #define dialog_list_iterate(dialoglist, pdialog) \
89 TYPED_LIST_ITERATE(struct intel_dialog, dialoglist, pdialog)
90 #define dialog_list_iterate_end LIST_ITERATE_END
92 static struct dialog_list *dialog_list;
93 static struct intel_dialog *create_intel_dialog(struct player *p);
95 /****************************************************************
96 Initialize intelligenze dialogs
97 *****************************************************************/
98 void intel_dialog_init()
100 dialog_list = dialog_list_new();
103 /****************************************************************
104 Free resources allocated for intelligenze dialogs
105 *****************************************************************/
106 void intel_dialog_done()
108 dialog_list_destroy(dialog_list);
111 /****************************************************************
112 Get intelligenze dialog between client user and other player
113 passed as parameter.
114 *****************************************************************/
115 static struct intel_dialog *get_intel_dialog(struct player *pplayer)
117 dialog_list_iterate(dialog_list, pdialog) {
118 if (pdialog->pplayer == pplayer) {
119 return pdialog;
121 } dialog_list_iterate_end;
123 return NULL;
126 /****************************************************************
127 Open intelligenze dialog
128 *****************************************************************/
129 void popup_intel_dialog(struct player *p)
131 struct intel_dialog *pdialog;
133 if (!(pdialog = get_intel_dialog(p))) {
134 pdialog = create_intel_dialog(p);
137 update_intel_dialog(p);
139 gtk_window_present(GTK_WINDOW(pdialog->shell));
142 /****************************************************************
143 Intelligenze dialog destruction requested
144 *****************************************************************/
145 static void intel_destroy_callback(GtkWidget *w, gpointer data)
147 struct intel_dialog *pdialog = (struct intel_dialog *)data;
149 dialog_list_remove(dialog_list, pdialog);
151 free(pdialog);
154 /**************************************************************************
155 Close an intelligence dialog for the given player.
156 **************************************************************************/
157 void close_intel_dialog(struct player *p)
159 struct intel_dialog *pdialog = get_intel_dialog(p);
160 intel_destroy_callback(NULL, pdialog);
163 /****************************************************************
164 Create new intelligenze dialog between client user and player
165 given as parameter.
166 *****************************************************************/
167 static struct intel_dialog *create_intel_dialog(struct player *p)
169 struct intel_dialog *pdialog;
171 GtkWidget *shell, *notebook, *label, *sw, *view, *table;
172 GtkCellRenderer *rend;
173 GtkTreeViewColumn *col;
175 int i;
177 pdialog = fc_malloc(sizeof(*pdialog));
178 pdialog->pplayer = p;
180 shell = gtk_dialog_new_with_buttons(NULL,
181 NULL,
183 GTK_STOCK_CLOSE,
184 GTK_RESPONSE_CLOSE,
185 NULL);
186 pdialog->shell = shell;
187 gtk_window_set_default_size(GTK_WINDOW(shell), 350, 350);
188 setup_dialog(shell, toplevel);
189 gtk_dialog_set_default_response(GTK_DIALOG(shell), GTK_RESPONSE_CLOSE);
191 g_signal_connect(shell, "destroy",
192 G_CALLBACK(intel_destroy_callback), pdialog);
193 g_signal_connect(shell, "response",
194 G_CALLBACK(gtk_widget_destroy), NULL);
196 notebook = gtk_notebook_new();
197 gtk_notebook_set_tab_pos(GTK_NOTEBOOK(notebook), GTK_POS_BOTTOM);
198 gtk_container_add(GTK_CONTAINER(gtk_dialog_get_content_area(GTK_DIALOG(shell))), notebook);
200 /* overview tab. */
201 table = gtk_grid_new();
202 g_object_set(table, "margin", 6, NULL);
204 gtk_grid_set_row_spacing(GTK_GRID(table), 2);
205 gtk_grid_set_column_spacing(GTK_GRID(table), 12);
207 /* TRANS: Overview tab of foreign intelligence report dialog */
208 label = gtk_label_new_with_mnemonic(_("_Overview"));
209 gtk_notebook_append_page(GTK_NOTEBOOK(notebook), table, label);
211 for (i = 0; i < ARRAY_SIZE(table_text); i++) {
212 if (table_text[i]) {
213 label = gtk_label_new(_(table_text[i]));
214 gtk_widget_set_halign(label, GTK_ALIGN_START);
215 gtk_widget_set_valign(label, GTK_ALIGN_CENTER);
216 gtk_grid_attach(GTK_GRID(table), label, 0, i, 1, 1);
218 label = gtk_label_new(NULL);
219 pdialog->table_labels[i] = label;
220 gtk_widget_set_halign(label, GTK_ALIGN_START);
221 gtk_widget_set_valign(label, GTK_ALIGN_CENTER);
222 gtk_grid_attach(GTK_GRID(table), label, 1, i, 1, 1);
223 } else {
224 pdialog->table_labels[i] = NULL;
228 /* diplomacy tab. */
229 pdialog->diplstates = gtk_tree_store_new(1, G_TYPE_STRING);
231 view = gtk_tree_view_new_with_model(GTK_TREE_MODEL(pdialog->diplstates));
232 g_object_set(view, "margin", 6, NULL);
233 gtk_widget_set_hexpand(view, TRUE);
234 gtk_widget_set_vexpand(view, TRUE);
235 g_object_unref(pdialog->diplstates);
236 gtk_container_set_border_width(GTK_CONTAINER(view), 6);
237 gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(view), FALSE);
239 rend = gtk_cell_renderer_text_new();
240 col = gtk_tree_view_column_new_with_attributes(NULL, rend,
241 "text", 0, NULL);
242 gtk_tree_view_append_column(GTK_TREE_VIEW(view), col);
244 gtk_tree_view_expand_all(GTK_TREE_VIEW(view));
246 sw = gtk_scrolled_window_new(NULL,NULL);
247 gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(sw),
248 GTK_SHADOW_ETCHED_IN);
249 gtk_container_add(GTK_CONTAINER(sw), view);
251 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(sw),
252 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
254 label = gtk_label_new_with_mnemonic(_("_Diplomacy"));
255 gtk_notebook_append_page(GTK_NOTEBOOK(notebook), sw, label);
257 /* techs tab. */
258 pdialog->techs = gtk_list_store_new(2, G_TYPE_BOOLEAN, G_TYPE_STRING);
259 gtk_tree_sortable_set_sort_column_id(GTK_TREE_SORTABLE(pdialog->techs),
260 1, GTK_SORT_ASCENDING);
262 view = gtk_tree_view_new_with_model(GTK_TREE_MODEL(pdialog->techs));
263 g_object_set(view, "margin", 6, NULL);
264 gtk_widget_set_hexpand(view, TRUE);
265 gtk_widget_set_vexpand(view, TRUE);
266 g_object_unref(pdialog->techs);
267 gtk_container_set_border_width(GTK_CONTAINER(view), 6);
268 gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(view), FALSE);
270 rend = gtk_cell_renderer_toggle_new();
271 col = gtk_tree_view_column_new_with_attributes(NULL, rend,
272 "active", 0, NULL);
273 gtk_tree_view_append_column(GTK_TREE_VIEW(view), col);
275 rend = gtk_cell_renderer_text_new();
276 col = gtk_tree_view_column_new_with_attributes(NULL, rend,
277 "text", 1, NULL);
278 gtk_tree_view_append_column(GTK_TREE_VIEW(view), col);
280 sw = gtk_scrolled_window_new(NULL,NULL);
281 gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(sw),
282 GTK_SHADOW_ETCHED_IN);
283 gtk_container_add(GTK_CONTAINER(sw), view);
285 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(sw),
286 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
288 label = gtk_label_new_with_mnemonic(_("_Techs"));
289 gtk_notebook_append_page(GTK_NOTEBOOK(notebook), sw, label);
291 gtk_widget_show_all(gtk_dialog_get_content_area(GTK_DIALOG(shell)));
293 dialog_list_prepend(dialog_list, pdialog);
295 return pdialog;
298 /****************************************************************************
299 Update the intelligence dialog for the given player. This is called by
300 the core client code when that player's information changes.
301 ****************************************************************************/
302 void update_intel_dialog(struct player *p)
304 struct intel_dialog *pdialog = get_intel_dialog(p);
306 if (pdialog) {
307 const struct research *mresearch, *presearch;
308 GtkTreeIter diplstates[DS_LAST];
309 int i;
311 /* window title. */
312 gchar *title = g_strdup_printf(_("Foreign Intelligence: %s Empire"),
313 nation_adjective_for_player(p));
314 gtk_window_set_title(GTK_WINDOW(pdialog->shell), title);
315 g_free(title);
317 /* diplomacy tab. */
318 gtk_tree_store_clear(pdialog->diplstates);
320 for (i = 0; i < ARRAY_SIZE(diplstates); i++) {
321 GtkTreeIter it;
322 GValue v = { 0, };
324 gtk_tree_store_append(pdialog->diplstates, &it, NULL);
325 g_value_init(&v, G_TYPE_STRING);
326 g_value_set_static_string(&v, diplstate_type_translated_name(i));
327 gtk_tree_store_set_value(pdialog->diplstates, &it, 0, &v);
328 g_value_unset(&v);
329 diplstates[i] = it;
332 players_iterate(other) {
333 const struct player_diplstate *state;
334 GtkTreeIter it;
335 GValue v = { 0, };
337 if (other == p || !other->is_alive) {
338 continue;
340 state = player_diplstate_get(p, other);
341 gtk_tree_store_append(pdialog->diplstates, &it,
342 &diplstates[state->type]);
343 g_value_init(&v, G_TYPE_STRING);
344 g_value_set_static_string(&v, player_name(other));
345 gtk_tree_store_set_value(pdialog->diplstates, &it, 0, &v);
346 g_value_unset(&v);
347 } players_iterate_end;
349 /* techs tab. */
350 gtk_list_store_clear(pdialog->techs);
352 mresearch = research_get(client_player());
353 presearch = research_get(p);
354 advance_index_iterate(A_FIRST, advi) {
355 if (research_invention_state(presearch, advi) == TECH_KNOWN) {
356 GtkTreeIter it;
358 gtk_list_store_append(pdialog->techs, &it);
360 gtk_list_store_set(pdialog->techs, &it,
361 0, research_invention_state(mresearch, advi)
362 != TECH_KNOWN,
363 1, research_advance_name_translation(presearch,
364 advi),
365 -1);
367 } advance_index_iterate_end;
369 /* table labels. */
370 for (i = 0; i < ARRAY_SIZE(pdialog->table_labels); i++) {
371 if (pdialog->table_labels[i]) {
372 struct city *pcity;
373 gchar *buf = NULL;
374 char tbuf[256];
376 switch (i) {
377 case LABEL_RULER:
378 ruler_title_for_player(p, tbuf, sizeof(tbuf));
379 buf = g_strdup(tbuf);
380 break;
381 case LABEL_GOVERNMENT:
382 buf = g_strdup(government_name_for_player(p));
383 break;
384 case LABEL_CAPITAL:
385 pcity = player_capital(p);
386 /* TRANS: "unknown" location */
387 buf = g_strdup((!pcity) ? _("(unknown)") : city_name_get(pcity));
388 break;
389 case LABEL_GOLD:
390 buf = g_strdup_printf("%d", p->economic.gold);
391 break;
392 case LABEL_TAX:
393 buf = g_strdup_printf("%d%%", p->economic.tax);
394 break;
395 case LABEL_SCIENCE:
396 buf = g_strdup_printf("%d%%", p->economic.science);
397 break;
398 case LABEL_LUXURY:
399 buf = g_strdup_printf("%d%%", p->economic.luxury);
400 break;
401 case LABEL_RESEARCHING:
403 struct research *research = research_get(p);
405 switch (research->researching) {
406 case A_UNKNOWN:
407 /* TRANS: "Unknown" advance/technology */
408 buf = g_strdup(_("(Unknown)"));
409 break;
410 case A_UNSET:
411 /* TRANS: missing value */
412 buf = g_strdup(_("(none)"));
413 break;
414 default:
415 buf = g_strdup_printf("%s(%d/%d)",
416 research_advance_name_translation
417 (research, research->researching),
418 research->bulbs_researched,
419 research->client.researching_cost);
420 break;
422 break;
424 default:
425 break;
428 if (buf) {
429 gtk_label_set_text(GTK_LABEL(pdialog->table_labels[i]), buf);
430 g_free(buf);