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

Freeciv-3.1

Loading...
Searching...
No Matches
spaceshipdlg.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 <stdio.h>
19#include <stdlib.h>
20
21#include <gtk/gtk.h>
22
23/* utility */
24#include "fcintl.h"
25#include "log.h"
26#include "mem.h"
27#include "shared.h"
28#include "support.h"
29
30/* common */
31#include "game.h"
32#include "map.h"
33#include "packets.h"
34#include "player.h"
35#include "spaceship.h"
36#include "victory.h"
37
38/* client */
39#include "client_main.h"
40#include "climisc.h"
41#include "colors.h"
42#include "options.h"
43#include "text.h"
44#include "tilespec.h"
45
46/* client/gui-gtk-4.0 */
47#include "dialogs.h"
48#include "graphics.h"
49#include "gui_main.h"
50#include "gui_stuff.h"
51#include "helpdlg.h"
52#include "inputdlg.h"
53#include "mapctrl.h"
54#include "mapview.h"
55#include "repodlgs.h"
56
57#include "spaceshipdlg.h"
58
59struct spaceship_dialog {
60 struct player *pplayer;
61 struct gui_dialog *shell;
62 GtkWidget *main_form;
63 GtkWidget *info_label;
64 GtkWidget *image_canvas;
65};
66
67#define SPECLIST_TAG dialog
68#define SPECLIST_TYPE struct spaceship_dialog
69#include "speclist.h"
70
71#define dialog_list_iterate(dialoglist, pdialog) \
72 TYPED_LIST_ITERATE(struct spaceship_dialog, dialoglist, pdialog)
73#define dialog_list_iterate_end LIST_ITERATE_END
74
75 static struct dialog_list *dialog_list;
76
77static struct spaceship_dialog *get_spaceship_dialog(struct player *pplayer);
78static struct spaceship_dialog *create_spaceship_dialog(struct player
79 *pplayer);
80
81static void spaceship_dialog_update_image(struct spaceship_dialog *pdialog);
82static void spaceship_dialog_update_info(struct spaceship_dialog *pdialog);
83
84/************************************************************************/
87 void spaceship_dialog_init(void)
88{
89 dialog_list = dialog_list_new();
90}
91
92/************************************************************************/
95 void spaceship_dialog_done(void)
96{
97 dialog_list_destroy(dialog_list);
98}
99
100/************************************************************************/
103 struct spaceship_dialog *get_spaceship_dialog(struct player *pplayer)
104{
105 dialog_list_iterate(dialog_list, pdialog) {
106 if (pdialog->pplayer == pplayer) {
107 return pdialog;
108 }
109 } dialog_list_iterate_end;
110
111 return NULL;
112}
113
114/************************************************************************/
117 void refresh_spaceship_dialog(struct player *pplayer)
118{
119 struct spaceship_dialog *pdialog;
120 struct player_spaceship *pship;
121
122 if (!(pdialog = get_spaceship_dialog(pplayer))) {
123 return;
124 }
125
126 pship = &(pdialog->pplayer->spaceship);
127
128 if (victory_enabled(VC_SPACERACE)
129 && pplayer == client.conn.playing
130 && pship->state == SSHIP_STARTED
131 && pship->success_rate > 0.0) {
132 gui_dialog_set_response_sensitive(pdialog->shell,
133 GTK_RESPONSE_ACCEPT, TRUE);
134 } else {
135 gui_dialog_set_response_sensitive(pdialog->shell,
136 GTK_RESPONSE_ACCEPT, FALSE);
137 }
138
139 spaceship_dialog_update_info(pdialog);
140 spaceship_dialog_update_image(pdialog);
141}
142
143/************************************************************************/
146 void popup_spaceship_dialog(struct player *pplayer)
147{
148 struct spaceship_dialog *pdialog;
149
150 if (!(pdialog = get_spaceship_dialog(pplayer))) {
151 pdialog = create_spaceship_dialog(pplayer);
152 }
153
154 gui_dialog_raise(pdialog->shell);
155}
156
157/************************************************************************/
160 void popdown_spaceship_dialog(struct player *pplayer)
161{
162 struct spaceship_dialog *pdialog;
163
164 if ((pdialog = get_spaceship_dialog(pplayer))) {
165 gui_dialog_destroy(pdialog->shell);
166 }
167}
168
169/************************************************************************/
172 static void spaceship_image_canvas_draw(GtkDrawingArea *w, cairo_t *cr,
173 int width, int height,
174 gpointer data)
175{
176 struct spaceship_dialog *pdialog = (struct spaceship_dialog *)data;
177 struct canvas store = FC_STATIC_CANVAS_INIT;
178
179 store.drawable = cr;
180
181 put_spaceship(&store, 0, 0, pdialog->pplayer);
182}
183
184/************************************************************************/
187 static void spaceship_destroy_callback(GtkWidget *w, gpointer data)
188{
189 struct spaceship_dialog *pdialog = (struct spaceship_dialog *)data;
190
191 dialog_list_remove(dialog_list, pdialog);
192
193 free(pdialog);
194}
195
196/************************************************************************/
199 static void spaceship_response(struct gui_dialog *dlg, int response,
200 gpointer data)
201{
202 switch (response) {
203 case GTK_RESPONSE_ACCEPT:
204 send_packet_spaceship_launch(&client.conn);
205 break;
206
207 default:
208 gui_dialog_destroy(dlg);
209 break;
210 }
211}
212
213/************************************************************************/
216 struct spaceship_dialog *create_spaceship_dialog(struct player *pplayer)
217{
218 struct spaceship_dialog *pdialog;
219 GtkWidget *hgrid, *frame;
220 int w, h;
221 int grid_col = 0;
222
223 pdialog = fc_malloc(sizeof(struct spaceship_dialog));
224 pdialog->pplayer = pplayer;
225
226 gui_dialog_new(&pdialog->shell, GTK_NOTEBOOK(top_notebook), NULL, TRUE);
227 gui_dialog_set_title(pdialog->shell, player_name(pplayer));
228
229 gui_dialog_add_button(pdialog->shell, "window-close", _("_Close"),
230 GTK_RESPONSE_CLOSE);
231 gui_dialog_add_button(pdialog->shell, NULL, _("_Launch"),
232 GTK_RESPONSE_ACCEPT);
233
234 g_signal_connect(pdialog->shell->grid, "destroy",
235 G_CALLBACK(spaceship_destroy_callback), pdialog);
236 gui_dialog_response_set_callback(pdialog->shell, spaceship_response);
237
238 hgrid = gtk_grid_new();
239 gtk_grid_set_column_spacing(GTK_GRID(hgrid), 5);
240 gui_dialog_add_content_widget(pdialog->shell, hgrid);
241
242 frame = gtk_frame_new(NULL);
243 gtk_grid_attach(GTK_GRID(hgrid), frame, grid_col++, 0, 1, 1);
244
245 pdialog->image_canvas = gtk_drawing_area_new();
246 gtk_widget_set_can_focus(pdialog->image_canvas, TRUE);
247 get_spaceship_dimensions(&w, &h);
248 gtk_widget_set_size_request(pdialog->image_canvas, w, h);
249
250 gtk_frame_set_child(GTK_FRAME(frame), pdialog->image_canvas);
251 gtk_widget_realize(pdialog->image_canvas);
252
253 gtk_drawing_area_set_draw_func(GTK_DRAWING_AREA(pdialog->image_canvas),
254 spaceship_image_canvas_draw, pdialog, NULL);
255
256 pdialog->info_label = gtk_label_new(get_spaceship_descr(NULL));
257
258 gtk_label_set_justify(GTK_LABEL(pdialog->info_label), GTK_JUSTIFY_LEFT);
259 gtk_widget_set_halign(pdialog->info_label, GTK_ALIGN_START);
260 gtk_widget_set_valign(pdialog->info_label, GTK_ALIGN_START);
261
262 gtk_grid_attach(GTK_GRID(hgrid), pdialog->info_label, grid_col++, 0, 1, 1);
263 gtk_widget_set_name(pdialog->info_label, "spaceship_label");
264
265 dialog_list_prepend(dialog_list, pdialog);
266
267 gtk_widget_grab_focus(pdialog->image_canvas);
268
269 gui_dialog_show_all(pdialog->shell);
270
271 refresh_spaceship_dialog(pdialog->pplayer);
272
273 return pdialog;
274}
275
276/************************************************************************/
279 void spaceship_dialog_update_info(struct spaceship_dialog *pdialog)
280{
281 gtk_label_set_text(GTK_LABEL(pdialog->info_label),
282 get_spaceship_descr(&pdialog->pplayer->spaceship));
283}
284
285/************************************************************************/
289 void spaceship_dialog_update_image(struct spaceship_dialog *pdialog)
290{
291 gtk_widget_queue_draw(pdialog->image_canvas);
292}
height
struct canvas int int struct sprite int int int int height
Definition canvas_g.h:44
width
struct canvas int int struct sprite int int int width
Definition canvas_g.h:44
client
struct civclient client
Definition client_main.c:149
client_main.h
climisc.h
VC_SPACERACE
@ VC_SPACERACE
Definition fc_types.h:1123
fcintl.h
_
#define _(String)
Definition fcintl.h:67
game.h
FC_STATIC_CANVAS_INIT
#define FC_STATIC_CANVAS_INIT
Definition canvas.h:27
top_notebook
GtkWidget * top_notebook
Definition gui_main.c:128
gui_dialog_destroy
void gui_dialog_destroy(struct gui_dialog *dlg)
Definition gui_stuff.c:951
gui_dialog_raise
void gui_dialog_raise(struct gui_dialog *dlg)
Definition gui_stuff.c:864
gui_dialog_new
void gui_dialog_new(struct gui_dialog **pdlg, GtkNotebook *notebook, gpointer user_data, bool check_top)
Definition gui_stuff.c:504
gui_dialog_response_set_callback
void gui_dialog_response_set_callback(struct gui_dialog *dlg, GUI_DIALOG_RESPONSE_FUN fun)
Definition gui_stuff.c:985
gui_dialog_add_button
GtkWidget * gui_dialog_add_button(struct gui_dialog *dlg, const char *text, int response)
Definition gui_stuff.c:706
gui_dialog_show_all
void gui_dialog_show_all(struct gui_dialog *dlg)
Definition gui_stuff.c:794
gui_dialog_set_title
void gui_dialog_set_title(struct gui_dialog *dlg, const char *title)
Definition gui_stuff.c:932
gui_dialog_set_response_sensitive
void gui_dialog_set_response_sensitive(struct gui_dialog *dlg, int response, bool setting)
Definition gui_stuff.c:759
spaceship_dialog_update_info
static void spaceship_dialog_update_info(struct spaceship_dialog *pdialog)
Definition spaceshipdlg.c:281
get_spaceship_dialog
static struct spaceship_dialog * get_spaceship_dialog(struct player *pplayer)
Definition spaceshipdlg.c:103
popdown_spaceship_dialog
void popdown_spaceship_dialog(struct player *pplayer)
Definition spaceshipdlg.c:160
spaceship_destroy_callback
static void spaceship_destroy_callback(GtkWidget *w, gpointer data)
Definition spaceshipdlg.c:189
dialog_list_iterate_end
#define dialog_list_iterate_end
Definition spaceshipdlg.c:73
spaceship_dialog_init
void spaceship_dialog_init(void)
Definition spaceshipdlg.c:87
dialog_list_iterate
#define dialog_list_iterate(dialoglist, pdialog)
Definition spaceshipdlg.c:71
dialog_list
static struct dialog_list * dialog_list
Definition spaceshipdlg.c:75
spaceship_dialog_update_image
static void spaceship_dialog_update_image(struct spaceship_dialog *pdialog)
Definition spaceshipdlg.c:291
spaceship_dialog_done
void spaceship_dialog_done(void)
Definition spaceshipdlg.c:95
popup_spaceship_dialog
void popup_spaceship_dialog(struct player *pplayer)
Definition spaceshipdlg.c:146
refresh_spaceship_dialog
void refresh_spaceship_dialog(struct player *pplayer)
Definition spaceshipdlg.c:117
create_spaceship_dialog
static struct spaceship_dialog * create_spaceship_dialog(struct player *pplayer)
Definition spaceshipdlg.c:218
spaceship_response
static void spaceship_response(struct gui_dialog *dlg, int response, gpointer data)
Definition spaceshipdlg.c:201
gui_dialog_add_content_widget
void gui_dialog_add_content_widget(struct gui_dialog *dlg, GtkWidget *wdg)
Definition gui_stuff.c:1095
gui_stuff.h
inputdlg.h
spaceship_image_canvas_draw
static void spaceship_image_canvas_draw(GtkDrawingArea *w, cairo_t *cr, int width, int height, gpointer data)
Definition spaceshipdlg.c:172
colors.h
dialogs.h
graphics.h
gui_main.h
helpdlg.h
mapctrl.h
mapview.h
repodlgs.h
spaceshipdlg.h
log.h
map.h
get_spaceship_dimensions
void get_spaceship_dimensions(int *width, int *height)
Definition mapview_common.c:3701
put_spaceship
void put_spaceship(struct canvas *pcanvas, int canvas_x, int canvas_y, const struct player *pplayer)
Definition mapview_common.c:3714
mem.h
fc_malloc
#define fc_malloc(sz)
Definition mem.h:34
options.h
packets.h
send_packet_spaceship_launch
int send_packet_spaceship_launch(struct connection *pc)
Definition packets_gen.c:52959
player_name
const char * player_name(const struct player *pplayer)
Definition player.c:886
player.h
shared.h
spaceship.h
SSHIP_STARTED
@ SSHIP_STARTED
Definition spaceship.h:84
speclist.h
canvas
Definition canvas.h:21
canvas::drawable
cairo_t * drawable
Definition canvas.h:23
civclient::conn
struct connection conn
Definition client_main.h:96
connection::playing
struct player * playing
Definition connection.h:156
gui_dialog
Definition gui_stuff.h:69
gui_dialog::grid
GtkWidget * grid
Definition gui_stuff.h:71
player_spaceship
Definition spaceship.h:94
player_spaceship::success_rate
double success_rate
Definition spaceship.h:115
player_spaceship::state
enum spaceship_state state
Definition spaceship.h:108
player
Definition player.h:249
player::spaceship
struct player_spaceship spaceship
Definition player.h:286
spaceship_dialog
Definition spaceshipdlg.c:59
spaceship_dialog::image_canvas
GtkWidget * image_canvas
Definition spaceshipdlg.c:64
spaceship_dialog::shell
struct gui_dialog * shell
Definition spaceshipdlg.c:61
spaceship_dialog::pplayer
struct player * pplayer
Definition spaceshipdlg.c:60
spaceship_dialog::info_label
GtkWidget * info_label
Definition spaceshipdlg.c:63
spaceship_dialog::main_form
GtkWidget * main_form
Definition spaceshipdlg.c:62
support.h
TRUE
#define TRUE
Definition support.h:46
FALSE
#define FALSE
Definition support.h:47
get_spaceship_descr
const char * get_spaceship_descr(struct player_spaceship *pship)
Definition text.c:1533
text.h
tilespec.h
victory_enabled
bool victory_enabled(enum victory_condition_type victory)
Definition victory.c:26
victory.h