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

Freeciv-3.3

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-5.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
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, *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);
93 int tuh = tileset_unit_height(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);
99 setup_dialog(dlg, toplevel);
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,
110 actor->activity, direction8_invalid());
111 if (spr != NULL) {
112 icon = gtk_image_new_from_pixbuf(sprite_get_pixbuf(spr));
113 } else {
114 icon = gtk_image_new();
115 }
116 gtk_widget_set_size_request(icon, tuw, tuh);
117 gtk_box_append(GTK_BOX(box), icon);
118
119 lbl = gtk_label_new(utype_name_translation(actor_type));
120 gtk_box_append(GTK_BOX(box), lbl);
121
122 gtk_box_append(GTK_BOX(main_box), box);
123
124 sep = gtk_separator_new(GTK_ORIENTATION_HORIZONTAL);
125 gtk_box_append(GTK_BOX(main_box), sep);
126
127 lbl = gtk_label_new(tgt_label);
128 gtk_box_append(GTK_BOX(main_box), lbl);
129
130 grid = 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_check_button_new();
143 gtk_check_button_set_group(GTK_CHECK_BUTTON(radio),
144 GTK_CHECK_BUTTON(first_option));
145 if (first_option == NULL) {
146 first_option = radio;
147 default_option = first_option;
148 default_unit = ptgt;
149 }
150 g_signal_connect(radio, "toggled",
151 G_CALLBACK(unit_sel_unit_toggled), cbdata);
152 g_signal_connect(radio, "destroy",
153 G_CALLBACK(unit_sel_unit_destroyed), cbdata);
154 if (ptgt == suggested_tgt_unit) {
155 default_option = radio;
156 default_unit = suggested_tgt_unit;
157 }
158 gtk_grid_attach(GTK_GRID(grid), radio, 0, tcount, 1, 1);
159
160 tubuf = usdlg_get_unit_image(ptgt);
161 if (tubuf != NULL) {
162 icon = gtk_image_new_from_pixbuf(tubuf);
163 g_object_unref(tubuf);
164 } else {
165 icon = gtk_image_new();
166 }
167 gtk_widget_set_size_request(icon, tuw, tuh);
168 gtk_grid_attach(GTK_GRID(grid), icon, 1, tcount, 1, 1);
169
170 lbl = gtk_label_new(usdlg_get_unit_descr(ptgt));
171 gtk_grid_attach(GTK_GRID(grid), lbl, 2, tcount, 1, 1);
172
173 tcount++;
174 } unit_list_iterate_end;
175 gtk_box_append(GTK_BOX(main_box), grid);
176
177 fc_assert_ret_val(default_option, FALSE);
178 gtk_check_button_set_active(GTK_CHECK_BUTTON(default_option), TRUE);
179
180 gtk_box_append(GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dlg))),
181 main_box);
182
183 g_object_set_data(G_OBJECT(dlg), "actor", GINT_TO_POINTER(actor->id));
184 g_object_set_data(G_OBJECT(dlg), "tile", ptile);
185
186 /* This function should never be called so that there would be no unit
187 * to select, and where there is unit to select, one of them gets
188 * selected as the default. */
189 fc_assert(default_unit != nullptr);
190 if (default_unit != nullptr) { /* Compiler still wants this */
191 g_object_set_data(G_OBJECT(dlg), "target",
192 GINT_TO_POINTER(default_unit->id));
193 }
194
195 g_signal_connect(dlg, "response", do_callback, actor);
196
197 gtk_widget_set_visible(gtk_dialog_get_content_area(GTK_DIALOG(dlg)), TRUE);
198 gtk_widget_set_visible(dlg, TRUE);
199
200 return TRUE;
201}
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
fcintl.h
_
#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
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
#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:50
tileset
Definition tilespec.c:482
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:507
unit
Definition unit.h:140
unit::activity
enum unit_activity activity
Definition unit.h:159
unit::id
int id
Definition unit.h:147
TRUE
#define TRUE
Definition support.h:46
FALSE
#define FALSE
Definition support.h:47
tileset_unit_width
int tileset_unit_width(const struct tileset *t)
Definition tilespec.c:798
tileset_unit_height
int tileset_unit_height(const struct tileset *t)
Definition tilespec.c:806
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_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
utype_name_translation
const char * utype_name_translation(const struct unit_type *punittype)
Definition unittype.c:1560