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-4.0 */
33#include "gui_main.h"
34#include "gui_stuff.h"
35#include "sprite.h"
36#include "unitselect.h"
37
38#include "unitselunitdlg.h"
39
41 GtkWidget *dlg;
42 int tp_id;
43};
44
45/************************************************************************/
48static 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/************************************************************************/
62static void unit_sel_unit_destroyed(GtkWidget *radio, gpointer userdata)
63{
64 free(userdata);
65}
66
67/************************************************************************/
70bool 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, *grid;
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 int tuw = tileset_unit_width(tileset);
94
95 dlg = gtk_dialog_new_with_buttons(dlg_title, NULL, 0,
96 _("Close"), GTK_RESPONSE_NO,
97 do_label, GTK_RESPONSE_YES,
98 NULL);
100 gtk_dialog_set_default_response(GTK_DIALOG(dlg), GTK_RESPONSE_NO);
101 gtk_window_set_destroy_with_parent(GTK_WINDOW(dlg), TRUE);
102
103 main_box = gtk_box_new(GTK_ORIENTATION_VERTICAL, 2);
104 box = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 2);
105
106 lbl = gtk_label_new(actor_label);
107 gtk_box_append(GTK_BOX(box), lbl);
108
109 spr = get_unittype_sprite(tileset, actor_type, direction8_invalid());
110 if (spr != NULL) {
111 icon = gtk_image_new_from_pixbuf(sprite_get_pixbuf(spr));
112 } else {
113 icon = gtk_image_new();
114 }
115 gtk_widget_set_size_request(icon, tuw, tuh);
116 gtk_box_append(GTK_BOX(box), icon);
117
118 lbl = gtk_label_new(utype_name_translation(actor_type));
119 gtk_box_append(GTK_BOX(box), lbl);
120
121 gtk_box_append(GTK_BOX(main_box), box);
122
123 sep = gtk_separator_new(GTK_ORIENTATION_HORIZONTAL);
124 gtk_box_append(GTK_BOX(main_box), sep);
125
126 lbl = gtk_label_new(tgt_label);
127 gtk_box_append(GTK_BOX(main_box), lbl);
128
129 grid = gtk_grid_new();
130
131 tcount = 0;
132 unit_list_iterate(potential_tgt_units, ptgt) {
133 GdkPixbuf *tubuf;
134
135 struct unit_sel_unit_cb_data *cbdata
136 = fc_malloc(sizeof(struct unit_sel_unit_cb_data));
137
138 cbdata->tp_id = ptgt->id;
139 cbdata->dlg = dlg;
140
141 radio = gtk_check_button_new();
142 gtk_check_button_set_group(GTK_CHECK_BUTTON(radio),
143 GTK_CHECK_BUTTON(first_option));
144 if (first_option == NULL) {
145 first_option = radio;
146 default_option = first_option;
147 default_unit = ptgt;
148 }
149 g_signal_connect(radio, "toggled",
150 G_CALLBACK(unit_sel_unit_toggled), cbdata);
151 g_signal_connect(radio, "destroy",
152 G_CALLBACK(unit_sel_unit_destroyed), cbdata);
153 if (ptgt == suggested_tgt_unit) {
154 default_option = radio;
155 default_unit = suggested_tgt_unit;
156 }
157 gtk_grid_attach(GTK_GRID(grid), radio, 0, tcount, 1, 1);
158
159 tubuf = usdlg_get_unit_image(ptgt);
160 if (tubuf != NULL) {
161 icon = gtk_image_new_from_pixbuf(tubuf);
162 g_object_unref(tubuf);
163 } else {
164 icon = gtk_image_new();
165 }
166 gtk_widget_set_size_request(icon, tuw, tuh);
167 gtk_grid_attach(GTK_GRID(grid), icon, 1, tcount, 1, 1);
168
169 lbl = gtk_label_new(usdlg_get_unit_descr(ptgt));
170 gtk_grid_attach(GTK_GRID(grid), lbl, 2, tcount, 1, 1);
171
172 tcount++;
174 gtk_box_append(GTK_BOX(main_box), grid);
175
176 fc_assert_ret_val(default_option, FALSE);
177 gtk_check_button_set_active(GTK_CHECK_BUTTON(default_option), TRUE);
178
179 gtk_box_append(GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dlg))),
180 main_box);
181
182 g_object_set_data(G_OBJECT(dlg), "actor", GINT_TO_POINTER(actor->id));
183 g_object_set_data(G_OBJECT(dlg), "tile", ptile);
184
185 /* This function should never be called so that there would be no unit to select,
186 * and where there is unit to select, one of them gets selected as the default. */
187 fc_assert(default_unit != NULL);
188 if (default_unit != NULL) { /* Compiler still wants this */
189 g_object_set_data(G_OBJECT(dlg), "target", GINT_TO_POINTER(default_unit->id));
190 }
191
192 g_signal_connect(dlg, "response", do_callback, actor);
193
194 gtk_widget_show(gtk_dialog_get_content_area(GTK_DIALOG(dlg)));
195 gtk_widget_show(dlg);
196
197 return TRUE;
198}
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
#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 * usdlg_get_unit_image(const struct unit *punit)
Definition unitselect.c:765
const char * usdlg_get_unit_descr(const struct unit *punit)
Definition unitselect.c:786
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)
static void unit_sel_unit_toggled(GtkToggleButton *tb, gpointer userdata)
static void unit_sel_unit_destroyed(GtkWidget *radio, gpointer userdata)
#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
Definition tile.h:49
Definition unit.h:138
int id
Definition unit.h:145
#define TRUE
Definition support.h:46
#define FALSE
Definition support.h:47
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
#define unit_list_iterate(unitlist, punit)
Definition unitlist.h:31
#define unit_list_iterate_end
Definition unitlist.h:33
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