Removed silencing of gtk warning logs from gtk3.22-client.
[freeciv.git] / client / gui-xaw / gotodlg.c
blob1fc50741a4266b0ebc90db8046f8b01d4f579c3b
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.
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 ***********************************************************************/
14 #ifdef HAVE_CONFIG_H
15 #include <fc_config.h>
16 #endif
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <string.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>
32 /* utility */
33 #include "log.h"
34 #include "mem.h"
35 #include "support.h"
37 /* common */
38 #include "game.h"
39 #include "map.h"
40 #include "packets.h"
41 #include "player.h"
42 #include "unit.h"
43 #include "unitlist.h"
45 /* client */
46 #include "client_main.h"
47 #include "control.h"
48 #include "goto.h"
50 /* gui-xaw */
51 #include "gui_main.h"
52 #include "gui_stuff.h"
53 #include "mapctrl.h"
54 #include "mapview.h"
56 #include "gotodlg.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,
71 XtPointer call_data);
72 void goto_goto_command_callback(Widget w, XtPointer client_data,
73 XtPointer call_data);
74 void goto_airlift_command_callback(Widget w, XtPointer client_data,
75 XtPointer call_data);
76 void goto_all_toggle_callback(Widget w, XtPointer client_data,
77 XtPointer call_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[]={
83 " ",
84 " ",
85 " ",
86 " ",
87 " ",
88 " ",
89 " ",
90 " ",
91 " ",
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)
105 Position x, y;
106 Dimension width, height;
107 Boolean no_player_cities;
109 if (!can_client_issue_orders() || get_num_units_in_focus() == 0) {
110 return;
113 no_player_cities = !(city_list_size(client.conn.playing->cities));
115 original_tile = get_center_tile_mapcanvas();
117 XtSetSensitive(main_form, FALSE);
119 goto_dialog_shell =
120 I_T(XtCreatePopupShell("gotodialog", transientShellWidgetClass,
121 toplevel, NULL, 0));
123 goto_form = XtVaCreateManagedWidget("gotoform",
124 formWidgetClass,
125 goto_dialog_shell, NULL);
127 goto_label =
128 I_L(XtVaCreateManagedWidget("gotolabel", labelWidgetClass,
129 goto_form, NULL));
131 goto_viewport = XtVaCreateManagedWidget("gotoviewport",
132 viewportWidgetClass,
133 goto_form,
134 NULL);
136 goto_list = XtVaCreateManagedWidget("gotolist",
137 listWidgetClass,
138 goto_viewport,
139 XtNlist,
140 (XtArgVal)dummy_city_list,
141 NULL);
143 goto_center_command =
144 I_L(XtVaCreateManagedWidget("gotocentercommand", commandWidgetClass,
145 goto_form, NULL));
147 goto_airlift_command =
148 I_L(XtVaCreateManagedWidget("gotoairliftcommand", commandWidgetClass,
149 goto_form, NULL));
151 goto_all_toggle =
152 I_L(XtVaCreateManagedWidget("gotoalltoggle", toggleWidgetClass,
153 goto_form,
154 XtNstate, no_player_cities,
155 XtNsensitive, !no_player_cities,
156 NULL));
158 goto_cancel_command =
159 I_L(XtVaCreateManagedWidget("gotocancelcommand", commandWidgetClass,
160 goto_form, NULL));
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,
179 &x, &y);
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;
192 int len;
194 ret=XawListShowCurrent(goto_list);
195 if(ret->list_index==XAW_LIST_NONE)
196 return 0;
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)
212 int j = 0;
213 Boolean all_cities;
215 if (!can_client_issue_orders()) {
216 return;
219 XtVaGetValues(goto_all_toggle, XtNstate, &all_cities, NULL);
221 cleanup_goto_list();
223 if (all_cities) {
224 ncities_total = 0;
225 players_iterate(pplayer) {
226 ncities_total += city_list_size(pplayer->cities);
227 } players_iterate_end;
228 } else {
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) {
236 continue;
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;
250 if (ncities_total) {
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)
261 cleanup_goto_list();
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)) {
284 can_airlift = TRUE;
285 break;
287 } unit_list_iterate_end;
289 center_tile_mapcanvas(pdestcity->tile);
290 if (can_airlift) {
291 XtSetSensitive(goto_airlift_command, True);
292 return;
296 XtSetSensitive(goto_airlift_command, False);
299 /**************************************************************************
301 **************************************************************************/
302 void goto_airlift_command_callback(Widget w, XtPointer client_data,
303 XtPointer call_data)
305 struct city *pdestcity=get_selected_city();
306 if (pdestcity) {
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,
318 XtPointer call_data)
320 update_goto_dialog(goto_list);
323 /**************************************************************************
325 **************************************************************************/
326 void goto_goto_command_callback(Widget w, XtPointer client_data,
327 XtPointer call_data)
329 struct city *pdestcity = get_selected_city();
330 if (pdestcity) {
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,
342 XtPointer call_data)
344 center_tile_mapcanvas(original_tile);
345 popdown_goto_dialog();
348 /**************************************************************************
350 **************************************************************************/
351 static void cleanup_goto_list(void)
353 int i;
355 XawListChange(goto_list, dummy_city_list, 0, 0, FALSE);
357 XtSetSensitive(goto_airlift_command, False);
359 if(city_name_ptrs) {
360 for(i=0; i<ncities_total; i++) {
361 free(city_name_ptrs[i]);
363 free(city_name_ptrs);
365 ncities_total = 0;
366 city_name_ptrs = NULL;