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-4.0 */
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, *grid;
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 int tuw = tileset_unit_width(tileset);
129 int tuh = tileset_unit_height(tileset);
130 int tew = tileset_tile_width(tileset);
131 int teh = tileset_tile_height(tileset);
132
133 dlg = gtk_dialog_new_with_buttons(dlg_title, NULL, 0,
134 _("Close"), GTK_RESPONSE_NO,
135 do_label, GTK_RESPONSE_YES,
136 NULL);
138 gtk_dialog_set_default_response(GTK_DIALOG(dlg), GTK_RESPONSE_NO);
139 gtk_window_set_destroy_with_parent(GTK_WINDOW(dlg), TRUE);
140
141 main_box = gtk_box_new(GTK_ORIENTATION_VERTICAL, 2);
142 box = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 2);
143
144 lbl = gtk_label_new(actor_label);
145 gtk_box_append(GTK_BOX(box), lbl);
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_widget_set_size_request(icon, tuw, tuh);
154 gtk_box_append(GTK_BOX(box), icon);
155
156 lbl = gtk_label_new(utype_name_translation(actor_type));
157 gtk_box_append(GTK_BOX(box), lbl);
158
159 gtk_box_append(GTK_BOX(main_box), box);
160
161 sep = gtk_separator_new(GTK_ORIENTATION_HORIZONTAL);
162 gtk_box_append(GTK_BOX(main_box), sep);
163
164 lbl = gtk_label_new(tgt_label);
165 gtk_box_append(GTK_BOX(main_box), lbl);
166
167 grid = gtk_grid_new();
168
169 tcount = 0;
171 GdkPixbuf *tubuf;
172
173 if (!BV_ISSET(potential_tgt_extras, extra_number(ptgt))) {
174 continue;
175 }
176
177 struct unit_sel_extra_cb_data *cbdata
178 = fc_malloc(sizeof(struct unit_sel_extra_cb_data));
179
180 cbdata->tp_id = ptgt->id;
181 cbdata->dlg = dlg;
182
183 radio = gtk_check_button_new();
184 gtk_check_button_set_group(GTK_CHECK_BUTTON(radio),
185 GTK_CHECK_BUTTON(first_option));
186 if (first_option == NULL) {
187 first_option = radio;
188 default_option = first_option;
189 default_extra = ptgt;
190 }
191 g_signal_connect(radio, "toggled",
192 G_CALLBACK(unit_sel_extra_toggled), cbdata);
193 g_signal_connect(radio, "destroy",
194 G_CALLBACK(unit_sel_extra_destroyed), cbdata);
195 if (ptgt == suggested_tgt_extra) {
196 default_option = radio;
197 default_extra = suggested_tgt_extra;
198 }
199 gtk_grid_attach(GTK_GRID(grid), radio, 0, tcount, 1, 1);
200
201 tubuf = create_extra_pixbuf(ptgt);
202 if (tubuf != NULL) {
203 icon = gtk_image_new_from_pixbuf(tubuf);
204 g_object_unref(tubuf);
205 } else {
206 icon = gtk_image_new();
207 }
208 gtk_widget_set_size_request(icon, tew, teh);
209 gtk_grid_attach(GTK_GRID(grid), icon, 1, tcount, 1, 1);
210
211 lbl = gtk_label_new(tgt_extra_descr(ptgt, ptile));
212 gtk_grid_attach(GTK_GRID(grid), lbl, 2, tcount, 1, 1);
213
214 tcount++;
216 gtk_box_append(GTK_BOX(main_box), grid);
217
218 fc_assert_ret_val(default_option, FALSE);
219 gtk_check_button_set_active(GTK_CHECK_BUTTON(default_option), TRUE);
220
221 gtk_box_append(GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dlg))),
222 main_box);
223
224 g_object_set_data(G_OBJECT(dlg), "actor", GINT_TO_POINTER(actor->id));
225 g_object_set_data(G_OBJECT(dlg), "tile", ptile);
226
227 /* This function should never be called so that there would be no extra to select,
228 * and where there is extra to select, one of them gets selected as the default. */
229 fc_assert(default_extra != NULL);
230 if (default_extra != NULL) { /* Compiler still wants this */
231 g_object_set_data(G_OBJECT(dlg), "target", GINT_TO_POINTER(default_extra->id));
232 }
233
234 g_signal_connect(dlg, "response", do_callback, actor);
235
236 gtk_widget_show(gtk_dialog_get_content_area(GTK_DIALOG(dlg)));
237 gtk_widget_show(dlg);
238
239 return TRUE;
240}
#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_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(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
int tileset_unit_width(const struct tileset *t)
Definition tilespec.c:760
int tileset_unit_height(const struct tileset *t)
Definition tilespec.c:768
struct sprite * get_unittype_sprite(const struct tileset *t, const struct unit_type *punittype, enum direction8 facing)
Definition tilespec.c:6516
int tileset_tile_height(const struct tileset *t)
Definition tilespec.c:728
int tileset_tile_width(const struct tileset *t)
Definition tilespec.c:716
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