Qt client - remove right click menu from end turn sidebar
[freeciv.git] / client / gui-gtk-2.0 / inteldlg.c
blobca3b8772c5ecc86d598aee85e3311b4795d3e570
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-2.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, *alignment;
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(shell)->vbox), notebook);
200 /* overview tab. */
201 table = gtk_table_new(ARRAY_SIZE(table_text), 2, FALSE);
203 gtk_table_set_row_spacings(GTK_TABLE(table), 2);
204 gtk_table_set_col_spacings(GTK_TABLE(table), 12);
206 alignment = gtk_alignment_new(0.0, 0.0, 0.0, 0.0);
207 gtk_container_set_border_width(GTK_CONTAINER(alignment), 6);
208 gtk_container_add(GTK_CONTAINER(alignment), table);
210 /* TRANS: Overview tab of foreign intelligence report dialog */
211 label = gtk_label_new_with_mnemonic(_("_Overview"));
212 gtk_notebook_append_page(GTK_NOTEBOOK(notebook), alignment, label);
214 for (i = 0; i < ARRAY_SIZE(table_text); i++) {
215 if (table_text[i]) {
216 label = gtk_label_new(_(table_text[i]));
217 gtk_misc_set_alignment(GTK_MISC(label), 0.0, 0.5);
218 gtk_table_attach(GTK_TABLE(table), label,
219 0, 1, i, i+1, GTK_FILL, GTK_FILL|GTK_EXPAND, 0, 0);
221 label = gtk_label_new(NULL);
222 pdialog->table_labels[i] = label;
223 gtk_misc_set_alignment(GTK_MISC(label), 0.0, 0.5);
224 gtk_table_attach(GTK_TABLE(table), label,
225 1, 2, i, i+1, GTK_FILL, GTK_FILL|GTK_EXPAND, 0, 0);
226 } else {
227 pdialog->table_labels[i] = NULL;
228 gtk_table_set_row_spacing(GTK_TABLE(table), i, 12);
232 /* diplomacy tab. */
233 pdialog->diplstates = gtk_tree_store_new(1, G_TYPE_STRING);
235 view = gtk_tree_view_new_with_model(GTK_TREE_MODEL(pdialog->diplstates));
236 g_object_unref(pdialog->diplstates);
237 gtk_container_set_border_width(GTK_CONTAINER(view), 6);
238 gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(view), FALSE);
240 rend = gtk_cell_renderer_text_new();
241 col = gtk_tree_view_column_new_with_attributes(NULL, rend,
242 "text", 0, NULL);
243 gtk_tree_view_append_column(GTK_TREE_VIEW(view), col);
245 gtk_tree_view_expand_all(GTK_TREE_VIEW(view));
247 sw = gtk_scrolled_window_new(NULL,NULL);
248 gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(sw),
249 GTK_SHADOW_ETCHED_IN);
250 gtk_container_add(GTK_CONTAINER(sw), view);
252 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(sw),
253 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
255 alignment = gtk_alignment_new(0.0, 0.0, 1.0, 1.0);
256 gtk_container_set_border_width(GTK_CONTAINER(alignment), 6);
257 gtk_container_add(GTK_CONTAINER(alignment), sw);
259 label = gtk_label_new_with_mnemonic(_("_Diplomacy"));
260 gtk_notebook_append_page(GTK_NOTEBOOK(notebook), alignment, label);
262 /* techs tab. */
263 pdialog->techs = gtk_list_store_new(2, G_TYPE_BOOLEAN, G_TYPE_STRING);
264 gtk_tree_sortable_set_sort_column_id(GTK_TREE_SORTABLE(pdialog->techs),
265 1, GTK_SORT_ASCENDING);
267 view = gtk_tree_view_new_with_model(GTK_TREE_MODEL(pdialog->techs));
268 g_object_unref(pdialog->techs);
269 gtk_container_set_border_width(GTK_CONTAINER(view), 6);
270 gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(view), FALSE);
272 rend = gtk_cell_renderer_toggle_new();
273 col = gtk_tree_view_column_new_with_attributes(NULL, rend,
274 "active", 0, NULL);
275 gtk_tree_view_append_column(GTK_TREE_VIEW(view), col);
277 rend = gtk_cell_renderer_text_new();
278 col = gtk_tree_view_column_new_with_attributes(NULL, rend,
279 "text", 1, NULL);
280 gtk_tree_view_append_column(GTK_TREE_VIEW(view), col);
282 sw = gtk_scrolled_window_new(NULL,NULL);
283 gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(sw),
284 GTK_SHADOW_ETCHED_IN);
285 gtk_container_add(GTK_CONTAINER(sw), view);
287 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(sw),
288 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
290 alignment = gtk_alignment_new(0.0, 0.0, 1.0, 1.0);
291 gtk_container_set_border_width(GTK_CONTAINER(alignment), 6);
292 gtk_container_add(GTK_CONTAINER(alignment), sw);
294 label = gtk_label_new_with_mnemonic(_("_Techs"));
295 gtk_notebook_append_page(GTK_NOTEBOOK(notebook), alignment, label);
297 gtk_widget_show_all(GTK_DIALOG(shell)->vbox);
299 dialog_list_prepend(dialog_list, pdialog);
301 return pdialog;
304 /****************************************************************************
305 Update the intelligence dialog for the given player. This is called by
306 the core client code when that player's information changes.
307 ****************************************************************************/
308 void update_intel_dialog(struct player *p)
310 struct intel_dialog *pdialog = get_intel_dialog(p);
312 if (pdialog) {
313 const struct research *mresearch, *presearch;
314 GtkTreeIter diplstates[DS_LAST];
315 int i;
317 /* window title. */
318 gchar *title = g_strdup_printf(_("Foreign Intelligence: %s Empire"),
319 nation_adjective_for_player(p));
320 gtk_window_set_title(GTK_WINDOW(pdialog->shell), title);
321 g_free(title);
323 /* diplomacy tab. */
324 gtk_tree_store_clear(pdialog->diplstates);
326 for (i = 0; i < ARRAY_SIZE(diplstates); i++) {
327 GtkTreeIter it;
328 GValue v = { 0, };
330 gtk_tree_store_append(pdialog->diplstates, &it, NULL);
331 g_value_init(&v, G_TYPE_STRING);
332 g_value_set_static_string(&v, diplstate_type_translated_name(i));
333 gtk_tree_store_set_value(pdialog->diplstates, &it, 0, &v);
334 g_value_unset(&v);
335 diplstates[i] = it;
338 players_iterate_alive(other) {
339 const struct player_diplstate *state;
340 GtkTreeIter it;
341 GValue v = { 0, };
343 if (other == p) {
344 continue;
346 state = player_diplstate_get(p, other);
347 gtk_tree_store_append(pdialog->diplstates, &it,
348 &diplstates[state->type]);
349 g_value_init(&v, G_TYPE_STRING);
350 g_value_set_static_string(&v, player_name(other));
351 gtk_tree_store_set_value(pdialog->diplstates, &it, 0, &v);
352 g_value_unset(&v);
353 } players_iterate_alive_end;
355 /* techs tab. */
356 gtk_list_store_clear(pdialog->techs);
358 mresearch = research_get(client_player());
359 presearch = research_get(p);
360 advance_index_iterate(A_FIRST, advi) {
361 if (research_invention_state(presearch, advi) == TECH_KNOWN) {
362 GtkTreeIter it;
364 gtk_list_store_append(pdialog->techs, &it);
366 gtk_list_store_set(pdialog->techs, &it,
367 0, research_invention_state(mresearch, advi)
368 != TECH_KNOWN,
369 1, research_advance_name_translation(presearch,
370 advi),
371 -1);
373 } advance_index_iterate_end;
375 /* table labels. */
376 for (i = 0; i < ARRAY_SIZE(pdialog->table_labels); i++) {
377 if (pdialog->table_labels[i]) {
378 struct city *pcity;
379 gchar *buf = NULL;
380 char tbuf[256];
382 switch (i) {
383 case LABEL_RULER:
384 ruler_title_for_player(p, tbuf, sizeof(tbuf));
385 buf = g_strdup(tbuf);
386 break;
387 case LABEL_GOVERNMENT:
388 buf = g_strdup(government_name_for_player(p));
389 break;
390 case LABEL_CAPITAL:
391 pcity = player_capital(p);
392 /* TRANS: "unknown" location */
393 buf = g_strdup((!pcity) ? _("(unknown)") : city_name_get(pcity));
394 break;
395 case LABEL_GOLD:
396 buf = g_strdup_printf("%d", p->economic.gold);
397 break;
398 case LABEL_TAX:
399 buf = g_strdup_printf("%d%%", p->economic.tax);
400 break;
401 case LABEL_SCIENCE:
402 buf = g_strdup_printf("%d%%", p->economic.science);
403 break;
404 case LABEL_LUXURY:
405 buf = g_strdup_printf("%d%%", p->economic.luxury);
406 break;
407 case LABEL_RESEARCHING:
409 struct research *research = research_get(p);
411 switch (research->researching) {
412 case A_UNKNOWN:
413 /* TRANS: "Unknown" advance/technology */
414 buf = g_strdup(_("(Unknown)"));
415 break;
416 case A_UNSET:
417 /* TRANS: missing value */
418 buf = g_strdup(_("(none)"));
419 break;
420 default:
421 buf = g_strdup_printf("%s(%d/%d)",
422 research_advance_name_translation
423 (research, research->researching),
424 research->bulbs_researched,
425 research->client.researching_cost);
426 break;
428 break;
430 default:
431 break;
434 if (buf) {
435 gtk_label_set_text(GTK_LABEL(pdialog->table_labels[i]), buf);
436 g_free(buf);