Freeciv-3.1: /fast/freeciv/nightly/S3_1/client/gui-gtk-3.22/unitselunitdlg.c Source File

Freeciv-3.1

Loading...
Searching...
No Matches
unitselunitdlg.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 "game.h"
25#include "movement.h"
26#include "unit.h"
27
28/* client */
29#include "control.h"
30#include "tilespec.h"
31
32/* client/gui-gtk-3.22 */
33#include "gui_main.h"
34#include "gui_stuff.h"
35#include "sprite.h"
36#include "unitselect.h"
37
38#include "unitselunitdlg.h"
39
40struct unit_sel_unit_cb_data {
41 GtkWidget *dlg;
42 int tp_id;
43};
44
45/************************************************************************/
48 static void unit_sel_unit_toggled(GtkToggleButton *tb, gpointer userdata)
49{
50 struct unit_sel_unit_cb_data *cbdata
51 = (struct unit_sel_unit_cb_data *)userdata;
52
53 if (gtk_toggle_button_get_active(tb)) {
54 g_object_set_data(G_OBJECT(cbdata->dlg), "target",
55 GINT_TO_POINTER(cbdata->tp_id));
56 }
57}
58
59/************************************************************************/
62 static void unit_sel_unit_destroyed(GtkWidget *radio, gpointer userdata)
63{
64 free(userdata);
65}
66
67/************************************************************************/
70 bool select_tgt_unit(struct unit *actor, struct tile *ptile,
71 struct unit_list *potential_tgt_units,
72 struct unit *suggested_tgt_unit,
73 const gchar *dlg_title,
74 const gchar *actor_label,
75 const gchar *tgt_label,
76 const gchar *do_label,
77 GCallback do_callback)
78{
79 GtkWidget *dlg;
80 GtkWidget *main_box;
81 GtkWidget *box;
82 GtkWidget *icon;
83 GtkWidget *lbl;
84 GtkWidget *sep;
85 GtkWidget *radio;
86 GtkWidget *default_option = NULL;
87 GtkWidget *first_option = NULL;
88 struct sprite *spr;
89 const struct unit_type *actor_type = unit_type_get(actor);
90 int tcount;
91 const struct unit *default_unit = NULL;
92
93 dlg = gtk_dialog_new_with_buttons(dlg_title, NULL, 0,
94 _("Close"), GTK_RESPONSE_NO,
95 do_label, GTK_RESPONSE_YES,
96 NULL);
97 setup_dialog(dlg, toplevel);
98 gtk_dialog_set_default_response(GTK_DIALOG(dlg), GTK_RESPONSE_NO);
99 gtk_window_set_destroy_with_parent(GTK_WINDOW(dlg), TRUE);
100
101 main_box = gtk_grid_new();
102 gtk_orientable_set_orientation(GTK_ORIENTABLE(main_box),
103 GTK_ORIENTATION_VERTICAL);
104 box = gtk_grid_new();
105 gtk_orientable_set_orientation(GTK_ORIENTABLE(box),
106 GTK_ORIENTATION_HORIZONTAL);
107
108 lbl = gtk_label_new(actor_label);
109 gtk_grid_attach(GTK_GRID(box), lbl, 0, 0, 1, 1);
110
111 spr = get_unittype_sprite(tileset, actor_type, direction8_invalid());
112 if (spr != NULL) {
113 icon = gtk_image_new_from_pixbuf(sprite_get_pixbuf(spr));
114 } else {
115 icon = gtk_image_new();
116 }
117 gtk_grid_attach(GTK_GRID(box), icon, 1, 0, 1, 1);
118
119 lbl = gtk_label_new(utype_name_translation(actor_type));
120 gtk_grid_attach(GTK_GRID(box), lbl, 2, 0, 1, 1);
121
122 gtk_container_add(GTK_CONTAINER(main_box), box);
123
124 sep = gtk_separator_new(GTK_ORIENTATION_HORIZONTAL);
125 gtk_container_add(GTK_CONTAINER(main_box), sep);
126
127 lbl = gtk_label_new(tgt_label);
128 gtk_container_add(GTK_CONTAINER(main_box), lbl);
129
130 box = gtk_grid_new();
131
132 tcount = 0;
133 unit_list_iterate(potential_tgt_units, ptgt) {
134 GdkPixbuf *tubuf;
135
136 struct unit_sel_unit_cb_data *cbdata
137 = fc_malloc(sizeof(struct unit_sel_unit_cb_data));
138
139 cbdata->tp_id = ptgt->id;
140 cbdata->dlg = dlg;
141
142 radio = gtk_radio_button_new_from_widget(
143 GTK_RADIO_BUTTON(first_option));
144 if (first_option == NULL) {
145 first_option = radio;
146 default_option = first_option;
147 default_unit = ptgt;
148 }
149 /* The lists must be the same length if they contain the same
150 * elements. */
151 fc_assert_msg(g_slist_length(gtk_radio_button_get_group(
152 GTK_RADIO_BUTTON(radio)))
153 == g_slist_length(gtk_radio_button_get_group(
154 GTK_RADIO_BUTTON(first_option))),
155 "The radio button for '%s' is broken.",
156 unit_rule_name(ptgt));
157 g_signal_connect(radio, "toggled",
158 G_CALLBACK(unit_sel_unit_toggled), cbdata);
159 g_signal_connect(radio, "destroy",
160 G_CALLBACK(unit_sel_unit_destroyed), cbdata);
161 if (ptgt == suggested_tgt_unit) {
162 default_option = radio;
163 default_unit = suggested_tgt_unit;
164 }
165 gtk_grid_attach(GTK_GRID(box), radio, 0, tcount, 1, 1);
166
167 tubuf = usdlg_get_unit_image(ptgt);
168 if (tubuf != NULL) {
169 icon = gtk_image_new_from_pixbuf(tubuf);
170 g_object_unref(tubuf);
171 } else {
172 icon = gtk_image_new();
173 }
174 gtk_grid_attach(GTK_GRID(box), icon, 1, tcount, 1, 1);
175
176 lbl = gtk_label_new(usdlg_get_unit_descr(ptgt));
177 gtk_grid_attach(GTK_GRID(box), lbl, 2, tcount, 1, 1);
178
179 tcount++;
180 } unit_list_iterate_end;
181 gtk_container_add(GTK_CONTAINER(main_box), box);
182
183 fc_assert_ret_val(default_option, FALSE);
184 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(default_option), TRUE);
185
186 gtk_container_add(
187 GTK_CONTAINER(gtk_dialog_get_content_area(GTK_DIALOG(dlg))),
188 main_box);
189
190 g_object_set_data(G_OBJECT(dlg), "actor", GINT_TO_POINTER(actor->id));
191 g_object_set_data(G_OBJECT(dlg), "tile", ptile);
192
193 /* This function should never be called so that there would be no unit to select,
194 * and where there is unit to select, one of them gets selected as the default. */
195 fc_assert(default_unit != NULL);
196 if (default_unit != NULL) { /* Compiler still wants this */
197 g_object_set_data(G_OBJECT(dlg), "target", GINT_TO_POINTER(default_unit->id));
198 }
199
200 g_signal_connect(dlg, "response", do_callback, actor);
201
202 gtk_widget_show_all(gtk_dialog_get_content_area(GTK_DIALOG(dlg)));
203 gtk_widget_show(dlg);
204
205 return TRUE;
206}
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:72
fcintl.h
_
#define _(String)
Definition fcintl.h:67
game.h
toplevel
GtkWidget * toplevel
Definition gui_main.c:124
setup_dialog
void setup_dialog(GtkWidget *shell, GtkWidget *parent)
Definition gui_stuff.c:281
sprite_get_pixbuf
GdkPixbuf * sprite_get_pixbuf(struct sprite *sprite)
Definition sprite.c:402
usdlg_get_unit_image
GdkPixbuf * usdlg_get_unit_image(const struct unit *punit)
Definition unitselect.c:765
usdlg_get_unit_descr
const char * usdlg_get_unit_descr(const struct unit *punit)
Definition unitselect.c:786
select_tgt_unit
bool select_tgt_unit(struct unit *actor, struct tile *ptile, struct unit_list *potential_tgt_units, struct unit *suggested_tgt_unit, const gchar *dlg_title, const gchar *actor_label, const gchar *tgt_label, const gchar *do_label, GCallback do_callback)
Definition unitselunitdlg.c:70
unit_sel_unit_toggled
static void unit_sel_unit_toggled(GtkToggleButton *tb, gpointer userdata)
Definition unitselunitdlg.c:48
unit_sel_unit_destroyed
static void unit_sel_unit_destroyed(GtkWidget *radio, gpointer userdata)
Definition unitselunitdlg.c:62
gui_stuff.h
unitselect.h
unitselunitdlg.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
sprite
Definition sprite.h:22
tile
Definition tile.h:49
tileset
Definition tilespec.c:459
unit_sel_unit_cb_data
Definition unitselunitdlg.c:40
unit_sel_unit_cb_data::tp_id
int tp_id
Definition unitselunitdlg.c:42
unit_sel_unit_cb_data::dlg
GtkWidget * dlg
Definition unitselunitdlg.c:41
unit_type
Definition unittype.h:481
unit
Definition unit.h:138
unit::id
int id
Definition unit.h:145
TRUE
#define TRUE
Definition support.h:46
FALSE
#define FALSE
Definition support.h:47
get_unittype_sprite
struct sprite * get_unittype_sprite(const struct tileset *t, const struct unit_type *punittype, enum direction8 facing)
Definition tilespec.c:6520
tilespec.h
unit.h
unit_list_iterate
#define unit_list_iterate(unitlist, punit)
Definition unitlist.h:31
unit_list_iterate_end
#define unit_list_iterate_end
Definition unitlist.h:33
unit_type_get
const struct unit_type * unit_type_get(const struct unit *punit)
Definition unittype.c:123
unit_rule_name
const char * unit_rule_name(const struct unit *punit)
Definition unittype.c:1639
utype_name_translation
const char * utype_name_translation(const struct unit_type *punittype)
Definition unittype.c:1612