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