Freeciv-3.2: /fast/freeciv/nightly/S3_2/client/gui-gtk-3.22/citizensinfo.c Source File

Freeciv-3.2

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