Freeciv-3.3: /fast/freeciv/nightly/main/client/gui-gtk-5.0/citizensinfo.c Source File

Freeciv-3.3

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#include <stdio.h>
19#include <stdlib.h>
20#include <string.h>
21
22#include <gtk/gtk.h>
23#include <gdk/gdkkeysyms.h>
24
25/* common */
26#include "citizens.h"
27#include "city.h"
28#include "nation.h"
29#include "player.h"
30
31/* client/gui-gtk-5.0 */
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);
42static int citizens_dialog_default_sort_column(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;
57 enum player_dlg_column_type type;
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};
67 static const int num_citizens_cols = ARRAY_SIZE(citizens_cols);
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
91 static struct dialog_list *dialog_list;
92
93/*************************************************************************/
96 static const char *col_nation(const struct city *pcity,
97 const struct player_slot *pslot)
98{
99 return nation_adjective_for_player(player_slot_get_player(pslot));
100}
101
102/*************************************************************************/
105 static 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/*************************************************************************/
126 static 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/*************************************************************************/
162 static int citizens_dialog_default_sort_column(void)
163{
164 return 0;
165}
166
167/*************************************************************************/
170 static struct citizens_dialog *citizens_dialog_create(const struct city *pcity)
171{
172 GtkWidget *frame, *sw;
173 struct citizens_dialog *pdialog = fc_malloc(sizeof(struct citizens_dialog));
174 int i;
175
176 pdialog->pcity = pcity;
177 pdialog->store = citizens_dialog_store_new();
178 pdialog->sort
179 = gtk_tree_model_sort_new_with_model(GTK_TREE_MODEL(pdialog->store));
180 g_object_unref(pdialog->store);
181
182 gtk_tree_sortable_set_sort_column_id(GTK_TREE_SORTABLE(pdialog->sort),
183 citizens_dialog_default_sort_column(),
184 GTK_SORT_DESCENDING);
185
186 pdialog->list
187 = gtk_tree_view_new_with_model(GTK_TREE_MODEL(pdialog->sort));
188 gtk_widget_set_halign(pdialog->list, GTK_ALIGN_CENTER);
189 g_object_unref(pdialog->sort);
190
191 for (i = 0; i < num_citizens_cols; i++) {
192 struct citizens_column *pcol;
193 GtkCellRenderer *renderer;
194 GtkTreeViewColumn *col;
195
196 pcol = &citizens_cols[i];
197 col = NULL;
198
199 switch (pcol->type) {
200 case COL_FLAG:
201 renderer = gtk_cell_renderer_pixbuf_new();
202 col = gtk_tree_view_column_new_with_attributes(_(pcol->title), renderer,
203 "pixbuf", i, NULL);
204 break;
205 case COL_TEXT:
206 renderer = gtk_cell_renderer_text_new();
207 g_object_set(renderer, "style-set", TRUE, "weight-set", TRUE, NULL);
208
209 col = gtk_tree_view_column_new_with_attributes(_(pcol->title), renderer,
210 "text", i,
211 "style", CITIZENS_DLG_COL_STYLE,
212 "weight", CITIZENS_DLG_COL_WEIGHT,
213 NULL);
214 gtk_tree_view_column_set_sort_column_id(col, i);
215 break;
216 case COL_RIGHT_TEXT:
217 renderer = gtk_cell_renderer_text_new();
218 g_object_set(renderer, "style-set", TRUE, "weight-set", TRUE, NULL);
219
220 col = gtk_tree_view_column_new_with_attributes(_(pcol->title), renderer,
221 "text", i,
222 "style", CITIZENS_DLG_COL_STYLE,
223 "weight", CITIZENS_DLG_COL_WEIGHT,
224 NULL);
225 gtk_tree_view_column_set_sort_column_id(col, i);
226 g_object_set(renderer, "xalign", 1.0, NULL);
227 gtk_tree_view_column_set_alignment(col, 1.0);
228 break;
229 case COL_COLOR:
230 case COL_BOOLEAN:
231 /* These are not used. */
232 fc_assert(pcol->type != COL_COLOR && pcol->type != COL_BOOLEAN);
233 continue;
234 }
235
236 if (col) {
237 gtk_tree_view_append_column(GTK_TREE_VIEW(pdialog->list), col);
238 }
239 }
240
241 gtk_widget_set_hexpand(GTK_WIDGET(pdialog->list), TRUE);
242 gtk_widget_set_vexpand(GTK_WIDGET(pdialog->list), TRUE);
243
244 sw = gtk_scrolled_window_new();
245 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(sw),
246 GTK_POLICY_AUTOMATIC,
247 GTK_POLICY_AUTOMATIC);
248 gtk_scrolled_window_set_has_frame(GTK_SCROLLED_WINDOW(sw), FALSE);
249 gtk_scrolled_window_set_child(GTK_SCROLLED_WINDOW(sw), pdialog->list);
250
251 frame = gtk_frame_new(_("Citizens"));
252 gtk_frame_set_child(GTK_FRAME(frame), sw);
253
254 pdialog->shell = frame;
255
256 dialog_list_prepend(dialog_list, pdialog);
257
258 citizens_dialog_refresh(pcity);
259
260 return pdialog;
261}
262
263/*************************************************************************/
266 void citizens_dialog_init(void)
267{
268 dialog_list = dialog_list_new();
269}
270
271/*************************************************************************/
274 void citizens_dialog_done(void)
275{
276 dialog_list_destroy(dialog_list);
277}
278
279/*************************************************************************/
282 static struct citizens_dialog *citizens_dialog_get(const struct city *pcity)
283{
284 dialog_list_iterate(dialog_list, pdialog) {
285 if (pdialog->pcity == pcity) {
286 return pdialog;
287 }
288 } dialog_list_iterate_end;
289
290 return NULL;
291}
292
293/*************************************************************************/
296 void citizens_dialog_refresh(const struct city *pcity)
297{
298 struct citizens_dialog *pdialog = citizens_dialog_get(pcity);
299
300 if (pdialog == NULL) {
301 return;
302 }
303
304 gtk_tree_store_clear(pdialog->store);
305 citizens_iterate(pcity, pslot, nationality) {
306 GtkTreeIter iter;
307
308 gtk_tree_store_append(pdialog->store, &iter, NULL);
309 citizens_dialog_row(pdialog->store, &iter, pcity, pslot);
310 } citizens_iterate_end;
311}
312
313/*************************************************************************/
316 static void citizens_dialog_row(GtkTreeStore *store, GtkTreeIter *it,
317 const struct city *pcity,
318 const struct player_slot *pslot)
319{
320 GdkPixbuf *pixbuf;
321 int style = PANGO_STYLE_NORMAL, weight = PANGO_WEIGHT_NORMAL;
322 int k;
323
324 for (k = 0; k < num_citizens_cols; k++) {
325 struct citizens_column *pcol = &citizens_cols[k];
326 switch (pcol->type) {
327 case COL_TEXT:
328 case COL_RIGHT_TEXT:
329 gtk_tree_store_set(store, it, k, pcol->func(pcity, pslot), -1);
330 break;
331 case COL_FLAG:
332 pixbuf = get_flag(nation_of_player(player_slot_get_player(pslot)));
333 if (pixbuf != NULL) {
334 gtk_tree_store_set(store, it, k, pixbuf, -1);
335 g_object_unref(pixbuf);
336 }
337 break;
338 case COL_COLOR:
339 case COL_BOOLEAN:
340 /* These are not used. */
341 fc_assert(pcol->type != COL_COLOR && pcol->type != COL_BOOLEAN);
342 continue;
343 break;
344 }
345 }
346
347 if (city_owner(pcity)->slot == pslot) {
348 weight = PANGO_WEIGHT_BOLD;
349 style = PANGO_STYLE_NORMAL;
350 }
351
352 gtk_tree_store_set(store, it,
353 CITIZENS_DLG_COL_STYLE, style,
354 CITIZENS_DLG_COL_WEIGHT, weight,
355 CITIZENS_DLG_COL_ID, player_slot_index(pslot),
356 -1);
357}
358
359/*************************************************************************/
362 void citizens_dialog_close(const struct city *pcity)
363{
364 struct citizens_dialog *pdialog = citizens_dialog_get(pcity);
365
366 if (pdialog == nullptr) {
367 return;
368 }
369
370 gtk_widget_set_visible(pdialog->shell, FALSE);
371
372 dialog_list_remove(dialog_list, pdialog);
373
374 gtk_box_remove(GTK_BOX(gtk_widget_get_parent(pdialog->shell)),
375 pdialog->shell);
376 free(pdialog);
377}
378
379/*************************************************************************/
382 GtkWidget *citizens_dialog_display(const struct city *pcity)
383{
384 struct citizens_dialog *pdialog = citizens_dialog_get(pcity);
385
386 if (pdialog == nullptr) {
387 pdialog = citizens_dialog_create(pcity);
388 }
389
390 gtk_widget_set_visible(pdialog->shell, TRUE);
391 citizens_dialog_refresh(pcity);
392
393 return pdialog->shell;
394}
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:392
_
#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:960
TRUE
#define TRUE
Definition support.h:46
FALSE
#define FALSE
Definition support.h:47