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

Freeciv-3.3

Loading...
Searching...
No Matches
unitselextradlg.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 <gtk/gtk.h>
19
20/* utility */
21#include "fcintl.h"
22
23/* common */
24#include "extras.h"
25#include "game.h"
26#include "movement.h"
27#include "nation.h"
28#include "unit.h"
29
30/* client */
31#include "control.h"
32#include "tilespec.h"
33
34/* client/gui-gtk-3.22 */
35#include "gui_main.h"
36#include "gui_stuff.h"
37#include "sprite.h"
38
39#include "unitselextradlg.h"
40
41 struct unit_sel_extra_cb_data {
42 GtkWidget *dlg;
43 int tp_id;
44};
45
46/***********************************************************************/
50 static const char *tgt_extra_descr(const struct extra_type *tgt_extra,
51 const struct tile *tgt_tile)
52{
53 static char buf[248] = "";
54 static char buf2[248] = "";
55
56 if (tile_has_extra(tgt_tile, tgt_extra)) {
57 if (extra_owner(tgt_tile)) {
58 /* TRANS: nation adjective for extra owner used below if the target
59 * tile has the target extra and it has an owner. */
60 fc_snprintf(buf2, sizeof(buf2), Q_("?eowner:%s"),
61 nation_adjective_for_player(extra_owner(tgt_tile)));
62 } else {
63 /* TRANS: used below if the target tile has the target extra but it
64 * doesn't have an owner. */
65 sz_strlcpy(buf2, _("target"));
66 }
67 } else {
68 /* TRANS: used below if the target tile doesn't have the target
69 * extra (so it is assumed that it will be created). */
70 sz_strlcpy(buf2, _("create"));
71 }
72
73 /* TRANS: extra name ... one of the above strings depending on if the
74 * target extra currently exists at the target tile and if it has an
75 * owner. */
76 fc_snprintf(buf, sizeof(buf), _("%s\n(%s)"),
77 extra_name_translation(tgt_extra), buf2);
78
79 return buf;
80}
81
82/************************************************************************/
85 static void unit_sel_extra_toggled(GtkToggleButton *tb, gpointer userdata)
86{
87 struct unit_sel_extra_cb_data *cbdata
88 = (struct unit_sel_extra_cb_data *)userdata;
89
90 if (gtk_toggle_button_get_active(tb)) {
91 g_object_set_data(G_OBJECT(cbdata->dlg), "target",
92 GINT_TO_POINTER(cbdata->tp_id));
93 }
94}
95
96/************************************************************************/
99 static void unit_sel_extra_destroyed(GtkWidget *radio, gpointer userdata)
100{
101 free(userdata);
102}
103
104/************************************************************************/
107 bool select_tgt_extra(struct unit *actor, struct tile *ptile,
108 bv_extras potential_tgt_extras,
109 struct extra_type *suggested_tgt_extra,
110 const gchar *dlg_title,
111 const gchar *actor_label,
112 const gchar *tgt_label,
113 const gchar *do_label,
114 GCallback do_callback)
115{
116 GtkWidget *dlg;
117 GtkWidget *main_box;
118 GtkWidget *box;
119 GtkWidget *icon;
120 GtkWidget *lbl;
121 GtkWidget *sep;
122 GtkWidget *radio;
123 GtkWidget *default_option = NULL;
124 GtkWidget *first_option = NULL;
125 struct sprite *spr;
126 const struct unit_type *actor_type = unit_type_get(actor);
127 int tcount;
128 const struct extra_type *default_extra = NULL;
129
130 dlg = gtk_dialog_new_with_buttons(dlg_title, NULL, 0,
131 _("Close"), GTK_RESPONSE_NO,
132 do_label, GTK_RESPONSE_YES,
133 NULL);
134 setup_dialog(dlg, toplevel);
135 gtk_dialog_set_default_response(GTK_DIALOG(dlg), GTK_RESPONSE_NO);
136 gtk_window_set_destroy_with_parent(GTK_WINDOW(dlg), TRUE);
137
138 main_box = gtk_grid_new();
139 gtk_orientable_set_orientation(GTK_ORIENTABLE(main_box),
140 GTK_ORIENTATION_VERTICAL);
141 box = gtk_grid_new();
142 gtk_orientable_set_orientation(GTK_ORIENTABLE(box),
143 GTK_ORIENTATION_HORIZONTAL);
144
145 lbl = gtk_label_new(actor_label);
146 gtk_grid_attach(GTK_GRID(box), lbl, 0, 0, 1, 1);
147
148 spr = get_unittype_sprite(tileset, actor_type,
149 actor->activity, direction8_invalid());
150 if (spr != NULL) {
151 icon = gtk_image_new_from_pixbuf(sprite_get_pixbuf(spr));
152 } else {
153 icon = gtk_image_new();
154 }
155 gtk_grid_attach(GTK_GRID(box), icon, 1, 0, 1, 1);
156
157 lbl = gtk_label_new(utype_name_translation(actor_type));
158 gtk_grid_attach(GTK_GRID(box), lbl, 2, 0, 1, 1);
159
160 gtk_container_add(GTK_CONTAINER(main_box), box);
161
162 sep = gtk_separator_new(GTK_ORIENTATION_HORIZONTAL);
163 gtk_container_add(GTK_CONTAINER(main_box), sep);
164
165 lbl = gtk_label_new(tgt_label);
166 gtk_container_add(GTK_CONTAINER(main_box), lbl);
167
168 box = gtk_grid_new();
169
170 tcount = 0;
171 extra_type_re_active_iterate(ptgt) {
172 GdkPixbuf *tubuf;
173
174 if (!BV_ISSET(potential_tgt_extras, extra_number(ptgt))) {
175 continue;
176 }
177
178 struct unit_sel_extra_cb_data *cbdata
179 = fc_malloc(sizeof(struct unit_sel_extra_cb_data));
180
181 cbdata->tp_id = ptgt->id;
182 cbdata->dlg = dlg;
183
184 radio = gtk_radio_button_new_from_widget(
185 GTK_RADIO_BUTTON(first_option));
186 if (first_option == NULL) {
187 first_option = radio;
188 default_option = first_option;
189 default_extra = ptgt;
190 }
191 /* The lists must be the same length if they contain the same
192 * elements. */
193 fc_assert_msg(g_slist_length(gtk_radio_button_get_group(
194 GTK_RADIO_BUTTON(radio)))
195 == g_slist_length(gtk_radio_button_get_group(
196 GTK_RADIO_BUTTON(first_option))),
197 "The radio button for '%s' is broken.",
198 extra_rule_name(ptgt));
199 g_signal_connect(radio, "toggled",
200 G_CALLBACK(unit_sel_extra_toggled), cbdata);
201 g_signal_connect(radio, "destroy",
202 G_CALLBACK(unit_sel_extra_destroyed), cbdata);
203 if (ptgt == suggested_tgt_extra) {
204 default_option = radio;
205 default_extra = suggested_tgt_extra;
206 }
207 gtk_grid_attach(GTK_GRID(box), radio, 0, tcount, 1, 1);
208
209 tubuf = create_extra_pixbuf(ptgt);
210 if (tubuf != NULL) {
211 icon = gtk_image_new_from_pixbuf(tubuf);
212 g_object_unref(tubuf);
213 } else {
214 icon = gtk_image_new();
215 }
216 gtk_grid_attach(GTK_GRID(box), icon, 1, tcount, 1, 1);
217
218 lbl = gtk_label_new(tgt_extra_descr(ptgt, ptile));
219 gtk_grid_attach(GTK_GRID(box), lbl, 2, tcount, 1, 1);
220
221 tcount++;
222 } extra_type_re_active_iterate_end;
223 gtk_container_add(GTK_CONTAINER(main_box), box);
224
225 fc_assert_ret_val(default_option, FALSE);
226 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(default_option), TRUE);
227
228 gtk_container_add(
229 GTK_CONTAINER(gtk_dialog_get_content_area(GTK_DIALOG(dlg))),
230 main_box);
231
232 g_object_set_data(G_OBJECT(dlg), "actor", GINT_TO_POINTER(actor->id));
233 g_object_set_data(G_OBJECT(dlg), "tile", ptile);
234
235 /* This function should never be called so that there would be no extra to select,
236 * and where there is extra to select, one of them gets selected as the default. */
237 fc_assert(default_extra != NULL);
238 if (default_extra != NULL) { /* Compiler still wants this */
239 g_object_set_data(G_OBJECT(dlg), "target", GINT_TO_POINTER(default_extra->id));
240 }
241
242 g_signal_connect(dlg, "response", do_callback, actor);
243
244 gtk_widget_show_all(gtk_dialog_get_content_area(GTK_DIALOG(dlg)));
245 gtk_widget_show(dlg);
246
247 return TRUE;
248}
BV_ISSET
#define BV_ISSET(bv, bit)
Definition bitvector.h:78
incite_cost
char * incite_cost
Definition comments.c:74
control.h
actor
struct unit struct city struct unit struct tile struct extra_type const struct act_prob *act_probs int actor_unit_id struct unit * actor
Definition dialogs_g.h:73
extra_owner
struct player * extra_owner(const struct tile *ptile)
Definition extras.c:1128
extra_number
int extra_number(const struct extra_type *pextra)
Definition extras.c:161
extra_rule_name
const char * extra_rule_name(const struct extra_type *pextra)
Definition extras.c:203
extra_name_translation
const char * extra_name_translation(const struct extra_type *pextra)
Definition extras.c:194
extras.h
extra_type_re_active_iterate_end
#define extra_type_re_active_iterate_end
Definition extras.h:329
extra_type_re_active_iterate
#define extra_type_re_active_iterate(_p)
Definition extras.h:325
fcintl.h
Q_
#define Q_(String)
Definition fcintl.h:70
_
#define _(String)
Definition fcintl.h:67
game.h
toplevel
GtkWidget * toplevel
Definition gui_main.c:125
setup_dialog
void setup_dialog(GtkWidget *shell, GtkWidget *parent)
Definition gui_stuff.c:287
sprite_get_pixbuf
GdkPixbuf * sprite_get_pixbuf(struct sprite *sprite)
Definition sprite.c:402
create_extra_pixbuf
GdkPixbuf * create_extra_pixbuf(const struct extra_type *pextra)
Definition sprite.c:494
unit_sel_extra_toggled
static void unit_sel_extra_toggled(GtkToggleButton *tb, gpointer userdata)
Definition unitselextradlg.c:85
select_tgt_extra
bool select_tgt_extra(struct unit *actor, struct tile *ptile, bv_extras potential_tgt_extras, struct extra_type *suggested_tgt_extra, const gchar *dlg_title, const gchar *actor_label, const gchar *tgt_label, const gchar *do_label, GCallback do_callback)
Definition unitselextradlg.c:107
unit_sel_extra_destroyed
static void unit_sel_extra_destroyed(GtkWidget *radio, gpointer userdata)
Definition unitselextradlg.c:99
tgt_extra_descr
static const char * tgt_extra_descr(const struct extra_type *tgt_extra, const struct tile *tgt_tile)
Definition unitselextradlg.c:50
gui_stuff.h
unitselextradlg.h
gui_main.h
sprite.h
fc_assert_msg
#define fc_assert_msg(condition, message,...)
Definition log.h:181
fc_assert
#define fc_assert(condition)
Definition log.h:176
fc_assert_ret_val
#define fc_assert_ret_val(condition, val)
Definition log.h:194
fc_malloc
#define fc_malloc(sz)
Definition mem.h:34
movement.h
nation_adjective_for_player
const char * nation_adjective_for_player(const struct player *pplayer)
Definition nation.c:169
nation.h
extra_type
Definition extras.h:88
sprite
Definition sprite.h:22
tile
Definition tile.h:50
tileset
Definition tilespec.c:482
unit_sel_extra_cb_data
Definition unitselextradlg.c:41
unit_sel_extra_cb_data::dlg
GtkWidget * dlg
Definition unitselextradlg.c:42
unit_sel_extra_cb_data::tp_id
int tp_id
Definition unitselextradlg.c:43
unit_type
Definition unittype.h:507
unit
Definition unit.h:140
unit::activity
enum unit_activity activity
Definition unit.h:159
unit::id
int id
Definition unit.h:147
fc_snprintf
int fc_snprintf(char *str, size_t n, const char *format,...)
Definition support.c:960
sz_strlcpy
#define sz_strlcpy(dest, src)
Definition support.h:189
TRUE
#define TRUE
Definition support.h:46
FALSE
#define FALSE
Definition support.h:47
tile_has_extra
#define tile_has_extra(ptile, pextra)
Definition tile.h:148
get_unittype_sprite
struct sprite * get_unittype_sprite(const struct tileset *t, const struct unit_type *punittype, enum unit_activity activity, enum direction8 facing)
Definition tilespec.c:6844
tilespec.h
unit.h
unit_type_get
const struct unit_type * unit_type_get(const struct unit *punit)
Definition unittype.c:123
utype_name_translation
const char * utype_name_translation(const struct unit_type *punittype)
Definition unittype.c:1560