1 /**********************************************************************
2 Freeciv - Copyright (C) 1996-2004 - The Freeciv Team
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 <X11/Intrinsic.h>
19 #include <X11/StringDefs.h>
20 #include <X11/Xaw/Command.h>
21 #include <X11/Xaw/Form.h>
22 #include <X11/Xaw/Label.h>
23 #include <X11/Xaw/List.h>
24 #include <X11/Xaw/SimpleMenu.h>
25 #include <X11/Xaw/Viewport.h>
26
27 /* utility */
28 #include "fcintl.h"
29 #include "log.h"
30 #include "support.h"
31
32 /* common */
33 #include "game.h"
34
35 /* client */
36 #include "client_main.h"
37 #include "connectdlg_g.h"
38 #include "dialogs_g.h"
39
40 #include "chatline.h" /* for send_chat() */
41 #include "gui_main.h"
42 #include "gui_stuff.h" /* for xaw_set_relative_position() */
43
44 #include "pages.h"
45
46 static enum client_pages old_page;
47
48 static Widget start_page_shell;
49 static Widget start_page_form;
50 static Widget start_page_label;
51 static Widget start_page_viewport;
52 static Widget start_page_players_list;
53 static Widget start_page_cancel_command;
54 static Widget start_page_nation_command;
55 static Widget start_page_take_command;
56 static Widget start_page_start_command;
57 void start_page_cancel_callback(Widget w, XtPointer client_data,
58 XtPointer call_data);
59 void start_page_nation_callback(Widget w, XtPointer client_data,
60 XtPointer call_data);
61 void start_page_take_callback(Widget w, XtPointer client_data,
62 XtPointer call_data);
63 void start_page_start_callback(Widget w, XtPointer client_data,
64 XtPointer call_data);
65
66
67 /***************************************************************************
68 Returns current client page
69 ***************************************************************************/
get_current_client_page(void)70 enum client_pages get_current_client_page(void)
71 {
72 return old_page;
73 }
74
75 /**************************************************************************
76 Sets the "page" that the client should show. See documentation in
77 pages_g.h.
78 **************************************************************************/
real_set_client_page(enum client_pages page)79 void real_set_client_page(enum client_pages page)
80 {
81 /* PORTME, PORTME, PORTME */
82 switch (page) {
83 case PAGE_MAIN:
84 /* FIXME: call main/intro page rather than falling to network page */
85 server_connect();
86 break;
87 case PAGE_GAME:
88 if (old_page == PAGE_START) {
89 popdown_start_page();
90 }
91 break;
92 case PAGE_START:
93 popup_start_page();
94 conn_list_dialog_update();
95 break;
96 case PAGE_SCENARIO:
97 case PAGE_LOAD:
98 case PAGE_NETWORK:
99 break;
100 }
101
102 old_page = page;
103 }
104
105 /****************************************************************************
106 Set the list of available rulesets. The default ruleset should be
107 "default", and if the user changes this then set_ruleset() should be
108 called.
109 ****************************************************************************/
set_rulesets(int num_rulesets,char ** rulesets)110 void set_rulesets(int num_rulesets, char **rulesets)
111 {
112 /* PORTME */
113 }
114
115 /**************************************************************************
116 START PAGE
117 **************************************************************************/
118
119 /**************************************************************************
120 Popup start page.
121 **************************************************************************/
popup_start_page(void)122 void popup_start_page(void)
123 {
124 if (!start_page_shell) {
125 create_start_page();
126 }
127
128 xaw_set_relative_position(toplevel, start_page_shell, 5, 25);
129 XtPopup(start_page_shell, XtGrabNone);
130 }
131
132 /**************************************************************************
133 Close start page.
134 **************************************************************************/
popdown_start_page(void)135 void popdown_start_page(void)
136 {
137 if (start_page_shell) {
138 XtDestroyWidget(start_page_shell);
139 start_page_shell = 0;
140 }
141 }
142
143 /**************************************************************************
144 Create start page.
145 **************************************************************************/
create_start_page(void)146 void create_start_page(void)
147 {
148 start_page_shell =
149 I_IN(I_T(XtCreatePopupShell("startpage",
150 topLevelShellWidgetClass,
151 toplevel, NULL, 0)));
152
153 start_page_form = XtVaCreateManagedWidget("startpageform",
154 formWidgetClass,
155 start_page_shell, NULL);
156
157 start_page_label = I_L(XtVaCreateManagedWidget("startpagelabel",
158 labelWidgetClass,
159 start_page_form, NULL));
160
161 start_page_viewport =
162 XtVaCreateManagedWidget("startpageviewport", viewportWidgetClass,
163 start_page_form, NULL);
164
165 start_page_players_list =
166 XtVaCreateManagedWidget("startpageplayerslist",
167 listWidgetClass,
168 start_page_viewport,
169 NULL);
170
171 start_page_cancel_command =
172 I_L(XtVaCreateManagedWidget("startpagecancelcommand",
173 commandWidgetClass,
174 start_page_form, NULL));
175
176 start_page_nation_command =
177 I_L(XtVaCreateManagedWidget("startpagenationcommand",
178 commandWidgetClass,
179 start_page_form,
180 NULL));
181
182 start_page_take_command =
183 I_L(XtVaCreateManagedWidget("startpagetakecommand",
184 commandWidgetClass,
185 start_page_form,
186 NULL));
187
188 start_page_start_command =
189 I_L(XtVaCreateManagedWidget("startpagestartcommand",
190 commandWidgetClass,
191 start_page_form,
192 NULL));
193
194 /*
195 XtAddCallback(start_page_players_list, XtNcallback,
196 start_page_players_list_callback,
197 NULL);
198 */
199 XtAddCallback(start_page_cancel_command, XtNcallback,
200 start_page_cancel_callback,
201 NULL);
202
203 XtAddCallback(start_page_nation_command, XtNcallback,
204 start_page_nation_callback,
205 NULL);
206
207 XtAddCallback(start_page_take_command, XtNcallback,
208 start_page_take_callback,
209 NULL);
210
211 XtAddCallback(start_page_start_command, XtNcallback,
212 start_page_start_callback,
213 NULL);
214
215 update_start_page();
216
217 XtRealizeWidget(start_page_shell);
218
219 XSetWMProtocols(display, XtWindow(start_page_shell),
220 &wm_delete_window, 1);
221 XtOverrideTranslations(start_page_shell,
222 XtParseTranslationTable("<Message>WM_PROTOCOLS: msg-close-start-page()"));
223 }
224
225 /**************************************************************************
226 Update start page players list.
227 (Separate from update_start_page() to avoid recursion.)
228 **************************************************************************/
real_update_start_page(void)229 void real_update_start_page(void)
230 {
231 if (!start_page_shell) {
232 return;
233 }
234 if ( C_S_PREPARING == client_state()) {
235 bool is_ready;
236 const char *nation, *leader;
237 char name[MAX_LEN_NAME + 8];
238 static CONST_FOR_XAW_LIST_CHANGE char *namelist_ptrs[MAX_NUM_PLAYERS];
239 static char namelist_text[MAX_NUM_PLAYERS][256];
240 int j;
241 Dimension width, height;
242
243 j = 0;
244
245 players_iterate(pplayer) {
246 if (pplayer->ai_controlled && !pplayer->was_created
247 && !pplayer->is_connected) {
248 fc_snprintf(name, sizeof(name), _("<%s AI>"),
249 ai_level_translated_name(pplayer->ai_common.skill_level));
250 } else {
251 sz_strlcpy(name, pplayer->username);
252 }
253 is_ready = pplayer->ai_controlled ? TRUE: pplayer->is_ready;
254 if (pplayer->nation == NO_NATION_SELECTED) {
255 nation = _("Random");
256 leader = "";
257 } else {
258 nation = nation_adjective_for_player(pplayer);
259 leader = player_name(pplayer);
260 }
261
262 fc_snprintf(namelist_text[j], sizeof(namelist_text[j]),
263 "%-16s %-5s %-16s %-16s %-4d ",
264 name,
265 is_ready ? " Yes " : " No ",
266 leader,
267 nation,
268 player_number(pplayer));
269
270 namelist_ptrs[j]=namelist_text[j];
271 j++;
272 } players_iterate_end;
273 conn_list_iterate(game.est_connections, pconn) {
274 if (NULL != pconn->playing && !pconn->observer) {
275 continue; /* Already listed above. */
276 }
277 sz_strlcpy(name, pconn->username);
278 nation = "";
279 leader = "";
280
281 fc_snprintf(namelist_text[j], sizeof(namelist_text[j]),
282 "%-16s %-5s %-16s %-16s %-4d ",
283 name,
284 " No ",
285 leader,
286 nation,
287 -1);
288
289 namelist_ptrs[j]=namelist_text[j];
290 j++;
291 } conn_list_iterate_end;
292 XawFormDoLayout(start_page_form, False);
293 XawListChange(start_page_players_list, namelist_ptrs, j, 0, True);
294
295 XtVaGetValues(start_page_players_list, XtNlongest, &width, NULL);
296 XtVaGetValues(start_page_players_list, XtNheight, &height, NULL);
297 XtVaSetValues(start_page_viewport, XtNwidth, width + 15, NULL);
298 XtVaSetValues(start_page_label, XtNwidth, width + 15, NULL);
299 XawFormDoLayout(start_page_form, True);
300 if (height > 120) {
301 height = 120;
302 }
303 XtVaSetValues(start_page_viewport, XtNheight, height, NULL);
304 }
305 }
306
307 /**************************************************************************
308 Entry point to update start page.
309 **************************************************************************/
update_start_page(void)310 void update_start_page(void)
311 {
312 real_update_start_page();
313 }
314
315 /**************************************************************************
316 Callback for start page "Cancel" button
317 **************************************************************************/
start_page_cancel_callback(Widget w,XtPointer client_data,XtPointer call_data)318 void start_page_cancel_callback(Widget w, XtPointer client_data,
319 XtPointer call_data)
320 {
321 popdown_start_page();
322 }
323
324 /**************************************************************************
325 Called when "Pick Nation" is clicked.
326 **************************************************************************/
start_page_nation_callback(Widget w,XtPointer client_data,XtPointer call_data)327 void start_page_nation_callback(Widget w, XtPointer client_data,
328 XtPointer call_data)
329 {
330 if (NULL != client.conn.playing) {
331 popup_races_dialog(client.conn.playing);
332 }
333 }
334
335 /**************************************************************************
336 Callback for start page "Take" button
337 **************************************************************************/
start_page_take_callback(Widget w,XtPointer client_data,XtPointer call_data)338 void start_page_take_callback(Widget w, XtPointer client_data,
339 XtPointer call_data)
340 {
341 XawListReturnStruct *ret;
342 struct player *selected_plr;
343 ret=XawListShowCurrent(start_page_players_list);
344 if(ret->list_index!=XAW_LIST_NONE) {
345 selected_plr=player_by_number(ret->list_index);
346 if(NULL != selected_plr ) {
347 send_chat_printf("/take \"%s\"", player_name(selected_plr));
348 if(selected_plr != client_player() && selected_plr->ai_controlled) {
349 send_chat("/away");
350 send_chat_printf("/%s", ai_level_cmd(selected_plr->ai_common.skill_level));
351 }
352 }
353 }
354 }
355
356 /**************************************************************************
357 Callback for start page "Start" button
358 **************************************************************************/
start_page_start_callback(Widget w,XtPointer client_data,XtPointer call_data)359 void start_page_start_callback(Widget w, XtPointer client_data,
360 XtPointer call_data)
361 {
362 /* popdown_start_page(); */
363 send_chat("/start");
364 }
365
366 /**************************************************************************
367 Callback for start page window (x) button
368 **************************************************************************/
start_page_msg_close(Widget w)369 void start_page_msg_close(Widget w)
370 {
371 popdown_start_page();
372 }
373
374