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);
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*****************************************************************************/
56static struct citizens_column {
57 bool show;
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};
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*****************************************************************************/
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
92static struct dialog_list *dialog_list;
93
94/*************************************************************************/
97static const char *col_nation(const struct city *pcity,
98 const struct player_slot *pslot)
99{
101}
102
103/*************************************************************************/
106static 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/*************************************************************************/
128{
129 GtkTreeStore *store;
131 int i;
132
133 for (i = 0; i < num_citizens_cols; i++) {
134 switch (citizens_cols[i].type) {
135 case COL_FLAG:
137 break;
138 case COL_COLOR:
140 break;
141 case COL_BOOLEAN:
143 break;
144 case COL_TEXT:
145 case COL_RIGHT_TEXT:
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
156
157 return store;
158}
159
160/*************************************************************************/
164{
165 return 0;
166}
167
168/*************************************************************************/
171static struct citizens_dialog
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
182 g_object_unref(pdialog->store);
183
187
188 pdialog->list
191 g_object_unref(pdialog->sort);
192
193 for (i = 0; i < num_citizens_cols; i++) {
194 struct citizens_column *pcol;
195 GtkCellRenderer *renderer;
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();
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
212 "text", i,
213 "style", CITIZENS_DLG_COL_STYLE,
214 "weight", CITIZENS_DLG_COL_WEIGHT,
215 NULL);
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
223 "text", i,
224 "style", CITIZENS_DLG_COL_STYLE,
225 "weight", CITIZENS_DLG_COL_WEIGHT,
226 NULL);
228 g_object_set(renderer, "xalign", 1.0, NULL);
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) {
240 }
241 }
242
245
252 gtk_container_add(GTK_CONTAINER(sw), pdialog->list);
253
254 frame = gtk_frame_new(_("Citizens"));
256
257 pdialog->shell = frame;
258
260
262
263 return pdialog;
264}
265
266/*************************************************************************/
273
274/*************************************************************************/
281
282/*************************************************************************/
285static struct citizens_dialog *citizens_dialog_get(const struct city *pcity)
286{
288 if (pdialog->pcity == pcity) {
289 return pdialog;
290 }
292
293 return NULL;
294}
295
296/*************************************************************************/
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) {
310
312 citizens_dialog_row(pdialog->store, &iter, pcity, pslot);
314}
315
316/*************************************************************************/
320 const struct city *pcity,
321 const struct player_slot *pslot)
322{
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:
337 if (pixbuf != NULL) {
338 gtk_tree_store_set(store, it, k, pixbuf, -1);
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,
360 -1);
361}
362
363/*************************************************************************/
366void 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
377
378 gtk_widget_destroy(pdialog->shell);
379 free(pdialog);
380}
381
382/*************************************************************************/
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);
395
396 return pdialog->shell;
397}
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:563
char * incite_cost
Definition comments.c:75
unsigned char citizens
Definition fc_types.h:388
#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:607
GType type
Definition repodlgs.c:1313
@ 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:169
struct nation_type * nation_of_player(const struct player *pplayer)
Definition nation.c:444
int player_slot_index(const struct player_slot *pslot)
Definition player.c:426
struct player * player_slot_get_player(const struct player_slot *pslot)
Definition player.c:437
player_dlg_column_type
@ COL_BOOLEAN
@ COL_FLAG
@ COL_RIGHT_TEXT
@ COL_COLOR
#define ARRAY_SIZE(x)
Definition shared.h:85
const char * tagname
enum player_dlg_column_type type
const char * title
GtkTreeStore * store
const struct city * pcity
GtkTreeModel * sort
GtkWidget * list
GtkWidget * shell
Definition city.h:320
int fc_snprintf(char *str, size_t n, const char *format,...)
Definition support.c:974
#define TRUE
Definition support.h:46