Freeciv-3.1
Loading...
Searching...
No Matches
citizensinfo.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
19#include <stdio.h>
20#include <stdlib.h>
21#include <string.h>
22
23#include <gtk/gtk.h>
24#include <gdk/gdkkeysyms.h>
25
26/* common */
27#include "citizens.h"
28#include "city.h"
29#include "player.h"
30
31/* client/gui-gtk-3.22 */
32#include "gui_stuff.h"
33#include "plrdlg.h"
34
35#include "citizensinfo.h"
36
37
38static struct citizens_dialog *citizens_dialog_get(const struct city *pcity);
39static struct citizens_dialog
40 *citizens_dialog_create(const struct city *pcity);
41static GtkTreeStore *citizens_dialog_store_new(void);
43static void citizens_dialog_row(GtkTreeStore *store, GtkTreeIter *it,
44 const struct city *pcity,
45 const struct player_slot *pslot);
46
47static const char *col_nation(const struct city *pcity,
48 const struct player_slot *pslot);
49static const char *col_citizens(const struct city *pcity,
50 const struct player_slot *pslot);
51
52/*****************************************************************************
53 The layout of the citizens display.
54*****************************************************************************/
55static struct citizens_column {
56 bool show;
58 const char *title;
59 const char *(*func)(const struct city *, const struct player_slot *);
60 /* if type = COL_*TEXT */
61 const char *tagname; /* for save_options */
62} citizens_cols[] = {
63 {TRUE, COL_RIGHT_TEXT, N_("#"), col_citizens, "citizens"},
64 {TRUE, COL_FLAG, N_("Flag"), NULL, "flag"},
65 {TRUE, COL_TEXT, N_("Nation"), col_nation, "nation"}
66};
68#define CITIZENS_DLG_COL_STYLE (0 + num_citizens_cols)
69#define CITIZENS_DLG_COL_WEIGHT (1 + num_citizens_cols)
70#define CITIZENS_DLG_COL_ID (2 + num_citizens_cols)
71
72/*****************************************************************************
73 The citizens dialog.
74*****************************************************************************/
75struct citizens_dialog {
76 const struct city *pcity;
77 GtkWidget *shell;
78 GtkTreeStore *store;
79 GtkWidget *list;
80 GtkTreeModel *sort;
81};
82
83#define SPECLIST_TAG dialog
84#define SPECLIST_TYPE struct citizens_dialog
85#include "speclist.h"
86
87#define dialog_list_iterate(dialoglist, pdialog) \
88 TYPED_LIST_ITERATE(struct citizens_dialog, dialoglist, pdialog)
89#define dialog_list_iterate_end LIST_ITERATE_END
90
91static struct dialog_list *dialog_list;
92
93/*************************************************************************/
96static const char *col_nation(const struct city *pcity,
97 const struct player_slot *pslot)
98{
100}
101
102/*************************************************************************/
105static const char *col_citizens(const struct city *pcity,
106 const struct player_slot *pslot)
107{
108 citizens nationality = citizens_nation_get(pcity, pslot);
109
110 if (nationality == 0) {
111 return "-";
112 } else {
113 static char buf[8];
114
115 fc_snprintf(buf, sizeof(buf), "%d", nationality);
116
117 return buf;
118 }
119}
120
121/*************************************************************************/
126static GtkTreeStore *citizens_dialog_store_new(void)
127{
128 GtkTreeStore *store;
129 GType model_types[num_citizens_cols + 3];
130 int i;
131
132 for (i = 0; i < num_citizens_cols; i++) {
133 switch (citizens_cols[i].type) {
134 case COL_FLAG:
135 model_types[i] = GDK_TYPE_PIXBUF;
136 break;
137 case COL_COLOR:
138 model_types[i] = GDK_TYPE_PIXBUF;
139 break;
140 case COL_BOOLEAN:
141 model_types[i] = G_TYPE_BOOLEAN;
142 break;
143 case COL_TEXT:
144 case COL_RIGHT_TEXT:
145 model_types[i] = G_TYPE_STRING;
146 break;
147 }
148 }
149 /* special (invisible rows) - Text style, weight and player id */
150 model_types[i++] = G_TYPE_INT; /* CITIZENS_DLG_COL_STYLE. */
151 model_types[i++] = G_TYPE_INT; /* CITIZENS_DLG_COL_WEIGHT. */
152 model_types[i++] = G_TYPE_INT; /* CITIZENS_DLG_COL_ID. */
153
154 store = gtk_tree_store_newv(i, model_types);
155
156 return store;
157}
158
159/*************************************************************************/
163{
164 return 0;
165}
166
167/*************************************************************************/
170static struct citizens_dialog
172{
173 GtkWidget *frame, *sw;
174 struct citizens_dialog *pdialog = fc_malloc(sizeof(struct citizens_dialog));
175 int i;
176
177 pdialog->pcity = pcity;
178 pdialog->store = citizens_dialog_store_new();
179 pdialog->sort
180 = gtk_tree_model_sort_new_with_model(GTK_TREE_MODEL(pdialog->store));
181 g_object_unref(pdialog->store);
182
183 gtk_tree_sortable_set_sort_column_id(GTK_TREE_SORTABLE(pdialog->sort),
185 GTK_SORT_DESCENDING);
186
187 pdialog->list
188 = gtk_tree_view_new_with_model(GTK_TREE_MODEL(pdialog->sort));
189 gtk_widget_set_halign(pdialog->list, GTK_ALIGN_CENTER);
190 g_object_unref(pdialog->sort);
191
192 for (i = 0; i < num_citizens_cols; i++) {
193 struct citizens_column *pcol;
194 GtkCellRenderer *renderer;
195 GtkTreeViewColumn *col;
196
197 pcol = &citizens_cols[i];
198 col = NULL;
199
200 switch (pcol->type) {
201 case COL_FLAG:
202 renderer = gtk_cell_renderer_pixbuf_new();
203 col = gtk_tree_view_column_new_with_attributes(_(pcol->title), renderer,
204 "pixbuf", i, NULL);
205 break;
206 case COL_TEXT:
207 renderer = gtk_cell_renderer_text_new();
208 g_object_set(renderer, "style-set", TRUE, "weight-set", TRUE, NULL);
209
210 col = gtk_tree_view_column_new_with_attributes(_(pcol->title), renderer,
211 "text", i,
212 "style", CITIZENS_DLG_COL_STYLE,
213 "weight", CITIZENS_DLG_COL_WEIGHT,
214 NULL);
215 gtk_tree_view_column_set_sort_column_id(col, i);
216 break;
217 case COL_RIGHT_TEXT:
218 renderer = gtk_cell_renderer_text_new();
219 g_object_set(renderer, "style-set", TRUE, "weight-set", TRUE, NULL);
220
221 col = gtk_tree_view_column_new_with_attributes(_(pcol->title), renderer,
222 "text", i,
223 "style", CITIZENS_DLG_COL_STYLE,
224 "weight", CITIZENS_DLG_COL_WEIGHT,
225 NULL);
226 gtk_tree_view_column_set_sort_column_id(col, i);
227 g_object_set(renderer, "xalign", 1.0, NULL);
228 gtk_tree_view_column_set_alignment(col, 1.0);
229 break;
230 case COL_COLOR:
231 case COL_BOOLEAN:
232 /* These are not used. */
233 fc_assert(pcol->type != COL_COLOR && pcol->type != COL_BOOLEAN);
234 continue;
235 }
236
237 if (col) {
238 gtk_tree_view_append_column(GTK_TREE_VIEW(pdialog->list), col);
239 }
240 }
241
242 gtk_widget_set_hexpand(GTK_WIDGET(pdialog->list), TRUE);
243 gtk_widget_set_vexpand(GTK_WIDGET(pdialog->list), TRUE);
244
245 sw = gtk_scrolled_window_new(NULL, NULL);
246 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(sw),
247 GTK_POLICY_AUTOMATIC,
248 GTK_POLICY_AUTOMATIC);
249 gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(sw),
250 GTK_SHADOW_NONE);
251 gtk_container_add(GTK_CONTAINER(sw), pdialog->list);
252
253 frame = gtk_frame_new(_("Citizens"));
254 gtk_container_add(GTK_CONTAINER(frame), sw);
255
256 pdialog->shell = frame;
257
258 dialog_list_prepend(dialog_list, pdialog);
259
261
262 return pdialog;
263}
264
265/*************************************************************************/
269{
270 dialog_list = dialog_list_new();
271}
272
273/*************************************************************************/
277{
278 dialog_list_destroy(dialog_list);
279}
280
281/*************************************************************************/
284static struct citizens_dialog *citizens_dialog_get(const struct city *pcity)
285{
287 if (pdialog->pcity == pcity) {
288 return pdialog;
289 }
291
292 return NULL;
293}
294
295/*************************************************************************/
299{
300 struct citizens_dialog *pdialog = citizens_dialog_get(pcity);
301
302 if (pdialog == NULL) {
303 return;
304 }
305
306 gtk_tree_store_clear(pdialog->store);
307 citizens_iterate(pcity, pslot, nationality) {
308 GtkTreeIter iter;
309
310 gtk_tree_store_append(pdialog->store, &iter, NULL);
311 citizens_dialog_row(pdialog->store, &iter, pcity, pslot);
313}
314
315/*************************************************************************/
318static void citizens_dialog_row(GtkTreeStore *store, GtkTreeIter *it,
319 const struct city *pcity,
320 const struct player_slot *pslot)
321{
322 GdkPixbuf *pixbuf;
323 int style = PANGO_STYLE_NORMAL, weight = PANGO_WEIGHT_NORMAL;
324 int k;
325
326 for (k = 0; k < num_citizens_cols; k++) {
327 struct citizens_column *pcol = &citizens_cols[k];
328
329 switch (pcol->type) {
330 case COL_TEXT:
331 case COL_RIGHT_TEXT:
332 gtk_tree_store_set(store, it, k, pcol->func(pcity, pslot), -1);
333 break;
334 case COL_FLAG:
336 if (pixbuf != NULL) {
337 gtk_tree_store_set(store, it, k, pixbuf, -1);
338 g_object_unref(pixbuf);
339 }
340 break;
341 case COL_COLOR:
342 case COL_BOOLEAN:
343 /* These are not used. */
344 fc_assert(pcol->type != COL_COLOR && pcol->type != COL_BOOLEAN);
345 continue;
346 break;
347 }
348 }
349
350 if (city_owner(pcity)->slot == pslot) {
351 weight = PANGO_WEIGHT_BOLD;
352 style = PANGO_STYLE_NORMAL;
353 }
354
355 gtk_tree_store_set(store, it,
359 -1);
360}
361
362/*************************************************************************/
365void citizens_dialog_close(const struct city *pcity)
366{
367 struct citizens_dialog *pdialog = citizens_dialog_get(pcity);
368
369 if (pdialog == NULL) {
370 return;
371 }
372
373 gtk_widget_hide(pdialog->shell);
374
375 dialog_list_remove(dialog_list, pdialog);
376
377 gtk_widget_destroy(pdialog->shell);
378 free(pdialog);
379}
380
381/*************************************************************************/
384GtkWidget *citizens_dialog_display(const struct city *pcity)
385{
386 struct citizens_dialog *pdialog = citizens_dialog_get(pcity);
387
388 if (!pdialog) {
389 pdialog = citizens_dialog_create(pcity);
390 }
391
392 gtk_widget_show_all(pdialog->shell);
394
395 return pdialog->shell;
396}
citizens citizens_nation_get(const struct city *pcity, const struct player_slot *pslot)
Definition citizens.c:74
#define citizens_iterate_end
Definition citizens.h:54
#define citizens_iterate(_pcity, _pslot, _nationality)
Definition citizens.h:48
#define city_owner(_pcity_)
Definition city.h:543
unsigned char citizens
Definition fc_types.h:358
#define _(String)
Definition fcintl.h:67
#define N_(String)
Definition fcintl.h:69
#define CITIZENS_DLG_COL_STYLE
void citizens_dialog_init(void)
static const char * col_citizens(const struct city *pcity, const struct player_slot *pslot)
GtkWidget * citizens_dialog_display(const struct city *pcity)
static struct citizens_column citizens_cols[]
void citizens_dialog_refresh(const struct city *pcity)
#define dialog_list_iterate_end
static struct citizens_dialog * citizens_dialog_create(const struct city *pcity)
#define CITIZENS_DLG_COL_ID
static const int num_citizens_cols
#define CITIZENS_DLG_COL_WEIGHT
#define dialog_list_iterate(dialoglist, pdialog)
void citizens_dialog_close(const struct city *pcity)
static struct dialog_list * dialog_list
static GtkTreeStore * citizens_dialog_store_new(void)
static int citizens_dialog_default_sort_column(void)
static const char * col_nation(const struct city *pcity, const struct player_slot *pslot)
static struct citizens_dialog * citizens_dialog_get(const struct city *pcity)
static void citizens_dialog_row(GtkTreeStore *store, GtkTreeIter *it, const struct city *pcity, const struct player_slot *pslot)
void citizens_dialog_done(void)
GdkPixbuf * get_flag(const struct nation_type *nation)
Definition plrdlg.c:609
GType type
Definition repodlgs.c:1312
@ COL_TEXT
Definition unitselect.c:50
#define fc_assert(condition)
Definition log.h:176
#define fc_malloc(sz)
Definition mem.h:34
const char * nation_adjective_for_player(const struct player *pplayer)
Definition nation.c:168
struct nation_type * nation_of_player(const struct player *pplayer)
Definition nation.c:443
int player_slot_index(const struct player_slot *pslot)
Definition player.c:419
struct player * player_slot_get_player(const struct player_slot *pslot)
Definition player.c:430
player_dlg_column_type
@ COL_BOOLEAN
@ COL_FLAG
@ COL_RIGHT_TEXT
@ COL_COLOR
#define ARRAY_SIZE(x)
Definition shared.h:85
const char * tagname
const char *(* func)(const struct city *, const struct player_slot *)
enum player_dlg_column_type type
const char * title
GtkTreeStore * store
const struct city * pcity
GtkTreeModel * sort
GtkWidget * list
GtkWidget * shell
Definition city.h:309
int fc_snprintf(char *str, size_t n, const char *format,...)
Definition support.c:969
#define TRUE
Definition support.h:46