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)
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 ***********************************************************************/
15 #include <fc_config.h>
22 #include <X11/Intrinsic.h>
23 #include <X11/StringDefs.h>
24 #include <X11/Xaw/Form.h>
25 #include <X11/Xaw/Label.h>
26 #include <X11/Xaw/SimpleMenu.h>
27 #include <X11/Xaw/Command.h>
28 #include <X11/Xaw/List.h>
29 #include <X11/Xaw/Viewport.h>
30 #include <X11/Xaw/Toggle.h>
46 #include "client_main.h"
52 #include "gui_stuff.h"
58 static Widget goto_dialog_shell
;
59 static Widget goto_form
;
60 static Widget goto_label
;
61 static Widget goto_viewport
;
62 static Widget goto_list
;
63 static Widget goto_center_command
;
64 static Widget goto_airlift_command
;
65 static Widget goto_all_toggle
;
66 static Widget goto_cancel_command
;
68 void update_goto_dialog(Widget goto_list
);
70 void goto_cancel_command_callback(Widget w
, XtPointer client_data
,
72 void goto_goto_command_callback(Widget w
, XtPointer client_data
,
74 void goto_airlift_command_callback(Widget w
, XtPointer client_data
,
76 void goto_all_toggle_callback(Widget w
, XtPointer client_data
,
78 void goto_list_callback(Widget w
, XtPointer client_data
, XtPointer call_data
);
80 static void cleanup_goto_list(void);
82 static char *dummy_city_list
[]={
95 static int ncities_total
= 0;
96 static char **city_name_ptrs
= NULL
;
97 static struct tile
*original_tile
;
100 /****************************************************************
101 popup the dialog 10% inside the main-window
102 *****************************************************************/
103 void popup_goto_dialog(void)
106 Dimension width
, height
;
107 Boolean no_player_cities
;
109 if (!can_client_issue_orders() || get_num_units_in_focus() == 0) {
113 no_player_cities
= !(city_list_size(client
.conn
.playing
->cities
));
115 original_tile
= get_center_tile_mapcanvas();
117 XtSetSensitive(main_form
, FALSE
);
120 I_T(XtCreatePopupShell("gotodialog", transientShellWidgetClass
,
123 goto_form
= XtVaCreateManagedWidget("gotoform",
125 goto_dialog_shell
, NULL
);
128 I_L(XtVaCreateManagedWidget("gotolabel", labelWidgetClass
,
131 goto_viewport
= XtVaCreateManagedWidget("gotoviewport",
136 goto_list
= XtVaCreateManagedWidget("gotolist",
140 (XtArgVal
)dummy_city_list
,
143 goto_center_command
=
144 I_L(XtVaCreateManagedWidget("gotocentercommand", commandWidgetClass
,
147 goto_airlift_command
=
148 I_L(XtVaCreateManagedWidget("gotoairliftcommand", commandWidgetClass
,
152 I_L(XtVaCreateManagedWidget("gotoalltoggle", toggleWidgetClass
,
154 XtNstate
, no_player_cities
,
155 XtNsensitive
, !no_player_cities
,
158 goto_cancel_command
=
159 I_L(XtVaCreateManagedWidget("gotocancelcommand", commandWidgetClass
,
162 XtAddCallback(goto_list
, XtNcallback
, goto_list_callback
, NULL
);
163 XtAddCallback(goto_center_command
, XtNcallback
,
164 goto_goto_command_callback
, NULL
);
165 XtAddCallback(goto_airlift_command
, XtNcallback
,
166 goto_airlift_command_callback
, NULL
);
167 XtAddCallback(goto_all_toggle
, XtNcallback
,
168 goto_all_toggle_callback
, NULL
);
169 XtAddCallback(goto_cancel_command
, XtNcallback
,
170 goto_cancel_command_callback
, NULL
);
172 XtRealizeWidget(goto_dialog_shell
);
174 update_goto_dialog(goto_list
);
176 XtVaGetValues(toplevel
, XtNwidth
, &width
, XtNheight
, &height
, NULL
);
178 XtTranslateCoords(toplevel
, (Position
) width
/10, (Position
) height
/10,
180 XtVaSetValues(goto_dialog_shell
, XtNx
, x
, XtNy
, y
, NULL
);
182 XtPopup(goto_dialog_shell
, XtGrabNone
);
184 /* force refresh of viewport so the scrollbar is added.
185 * Buggy sun athena requires this */
186 XtVaSetValues(goto_viewport
, XtNforceBars
, True
, NULL
);
189 static struct city
*get_selected_city(void)
191 XawListReturnStruct
*ret
;
194 ret
=XawListShowCurrent(goto_list
);
195 if(ret
->list_index
==XAW_LIST_NONE
)
198 len
= strlen(ret
->string
);
199 if(len
>3 && strcmp(ret
->string
+len
-3, "(A)")==0) {
200 char name
[MAX_LEN_NAME
];
201 fc_strlcpy(name
, ret
->string
, MIN(sizeof(name
),len
-2));
202 return game_city_by_name(name
);
204 return game_city_by_name(ret
->string
);
207 /**************************************************************************
209 **************************************************************************/
210 void update_goto_dialog(Widget goto_cities
)
215 if (!can_client_issue_orders()) {
219 XtVaGetValues(goto_all_toggle
, XtNstate
, &all_cities
, NULL
);
225 players_iterate(pplayer
) {
226 ncities_total
+= city_list_size(pplayer
->cities
);
227 } players_iterate_end
;
229 ncities_total
= city_list_size(client
.conn
.playing
->cities
);
232 city_name_ptrs
= fc_malloc(ncities_total
*sizeof(char*));
234 players_iterate(pplayer
) {
235 if (!all_cities
&& pplayer
!= client
.conn
.playing
) {
238 city_list_iterate(pplayer
->cities
, pcity
) {
239 char name
[MAX_LEN_NAME
+3];
241 sz_strlcpy(name
, city_name_get(pcity
));
242 /* FIXME: should use unit_can_airlift_to(). */
243 if (pcity
->airlift
) {
244 sz_strlcat(name
, "(A)");
246 city_name_ptrs
[j
++] = fc_strdup(name
);
247 } city_list_iterate_end
;
248 } players_iterate_end
;
251 qsort(city_name_ptrs
, ncities_total
, sizeof(char *), compare_strings_ptrs
);
252 XawListChange(goto_cities
, city_name_ptrs
, ncities_total
, 0, True
);
256 /**************************************************************************
258 **************************************************************************/
259 static void popdown_goto_dialog(void)
263 XtDestroyWidget(goto_dialog_shell
);
264 XtSetSensitive(main_form
, TRUE
);
267 /**************************************************************************
269 **************************************************************************/
270 void goto_list_callback(Widget w
, XtPointer client_data
, XtPointer call_data
)
272 XawListReturnStruct
*ret
;
274 ret
= XawListShowCurrent(goto_list
);
276 if (ret
->list_index
!= XAW_LIST_NONE
) {
277 struct city
*pdestcity
;
279 if ((pdestcity
= get_selected_city())) {
280 bool can_airlift
= FALSE
;
282 unit_list_iterate(get_units_in_focus(), punit
) {
283 if (unit_can_airlift_to(punit
, pdestcity
)) {
287 } unit_list_iterate_end
;
289 center_tile_mapcanvas(pdestcity
->tile
);
291 XtSetSensitive(goto_airlift_command
, True
);
296 XtSetSensitive(goto_airlift_command
, False
);
299 /**************************************************************************
301 **************************************************************************/
302 void goto_airlift_command_callback(Widget w
, XtPointer client_data
,
305 struct city
*pdestcity
=get_selected_city();
307 unit_list_iterate(get_units_in_focus(), punit
) {
308 request_unit_airlift(punit
, pdestcity
);
309 } unit_list_iterate_end
;
311 popdown_goto_dialog();
314 /**************************************************************************
316 **************************************************************************/
317 void goto_all_toggle_callback(Widget w
, XtPointer client_data
,
320 update_goto_dialog(goto_list
);
323 /**************************************************************************
325 **************************************************************************/
326 void goto_goto_command_callback(Widget w
, XtPointer client_data
,
329 struct city
*pdestcity
= get_selected_city();
331 unit_list_iterate(get_units_in_focus(), punit
) {
332 send_goto_tile(punit
, pdestcity
->tile
);
333 } unit_list_iterate_end
;
335 popdown_goto_dialog();
338 /**************************************************************************
340 **************************************************************************/
341 void goto_cancel_command_callback(Widget w
, XtPointer client_data
,
344 center_tile_mapcanvas(original_tile
);
345 popdown_goto_dialog();
348 /**************************************************************************
350 **************************************************************************/
351 static void cleanup_goto_list(void)
355 XawListChange(goto_list
, dummy_city_list
, 0, 0, FALSE
);
357 XtSetSensitive(goto_airlift_command
, False
);
360 for(i
=0; i
<ncities_total
; i
++) {
361 free(city_name_ptrs
[i
]);
363 free(city_name_ptrs
);
366 city_name_ptrs
= NULL
;