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 #include <string.h>
21
22 #include <gtk/gtk.h>
23
24 /* utility */
25 #include "fcintl.h"
26 #include "log.h"
27
28 /* common */
29 #include "game.h"
30 #include "player.h"
31
32 /* client */
33 #include "options.h"
34
35 /* client/gui-gtk-2.0 */
36 #include "dialogs.h"
37 #include "gui_main.h"
38 #include "gui_stuff.h"
39 #include "mapview.h"
40
41 #include "finddlg.h"
42
43 static struct gui_dialog *find_dialog_shell;
44 static GtkWidget *find_view;
45
46 static void update_find_dialog(GtkListStore *store);
47
48 static void find_response(struct gui_dialog *dlg, int response, gpointer data);
49 static void find_destroy_callback(GtkWidget *w, gpointer data);
50 static void find_selection_callback(GtkTreeSelection *selection,
51 GtkTreeModel *model);
52
53 static struct tile *pos;
54
55 /****************************************************************
56 popup the dialog 10% inside the main-window
57 *****************************************************************/
popup_find_dialog(void)58 void popup_find_dialog(void)
59 {
60 if (!find_dialog_shell) {
61 GtkWidget *label;
62 GtkWidget *sw;
63 GtkListStore *store;
64 GtkTreeSelection *selection;
65 GtkCellRenderer *renderer;
66 GtkTreeViewColumn *column;
67
68 pos = get_center_tile_mapcanvas();
69
70 gui_dialog_new(&find_dialog_shell, GTK_NOTEBOOK(bottom_notebook), NULL,
71 TRUE);
72 gui_dialog_set_title(find_dialog_shell, _("Find City"));
73 gui_dialog_set_default_size(find_dialog_shell, -1, 240);
74
75 gui_dialog_add_button(find_dialog_shell,
76 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL);
77 gui_dialog_add_button(find_dialog_shell,
78 GTK_STOCK_FIND, GTK_RESPONSE_ACCEPT);
79
80 gui_dialog_set_default_response(find_dialog_shell, GTK_RESPONSE_ACCEPT);
81
82 gui_dialog_response_set_callback(find_dialog_shell, find_response);
83
84 g_signal_connect(find_dialog_shell->vbox, "destroy",
85 G_CALLBACK(find_destroy_callback), NULL);
86
87 store = gtk_list_store_new(2, G_TYPE_STRING, G_TYPE_POINTER);
88 gtk_tree_sortable_set_sort_column_id(GTK_TREE_SORTABLE(store),
89 0, GTK_SORT_ASCENDING);
90
91 find_view = gtk_tree_view_new_with_model(GTK_TREE_MODEL(store));
92 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(find_view));
93 g_object_unref(store);
94 gtk_tree_view_columns_autosize(GTK_TREE_VIEW(find_view));
95 gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(find_view), FALSE);
96
97 renderer = gtk_cell_renderer_text_new();
98 column = gtk_tree_view_column_new_with_attributes(NULL, renderer,
99 "text", 0, NULL);
100 gtk_tree_view_column_set_sort_order(column, GTK_SORT_ASCENDING);
101 gtk_tree_view_append_column(GTK_TREE_VIEW(find_view), column);
102
103 sw = gtk_scrolled_window_new(NULL, NULL);
104 gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(sw),
105 GTK_SHADOW_ETCHED_IN);
106 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(sw),
107 GTK_POLICY_AUTOMATIC, GTK_POLICY_ALWAYS);
108 gtk_container_add(GTK_CONTAINER(sw), find_view);
109
110 label = g_object_new(GTK_TYPE_LABEL,
111 "use-underline", TRUE,
112 "mnemonic-widget", find_view,
113 "label", _("Ci_ties:"),
114 "xalign", 0.0, "yalign", 0.5, NULL);
115 gtk_box_pack_start(GTK_BOX(find_dialog_shell->vbox), label,
116 FALSE, FALSE, 0);
117 gtk_box_pack_start(GTK_BOX(find_dialog_shell->vbox), sw,
118 TRUE, TRUE, 2);
119
120 g_signal_connect(selection, "changed",
121 G_CALLBACK(find_selection_callback), store);
122
123 update_find_dialog(store);
124 gtk_tree_view_focus(GTK_TREE_VIEW(find_view));
125
126 gui_dialog_show_all(find_dialog_shell);
127 }
128
129 gui_dialog_raise(find_dialog_shell);
130 }
131
132
133
134 /**************************************************************************
135 Update find dialog with current cities
136 **************************************************************************/
update_find_dialog(GtkListStore * store)137 static void update_find_dialog(GtkListStore *store)
138 {
139 GtkTreeIter it;
140
141 gtk_list_store_clear(store);
142
143 players_iterate(pplayer) {
144 city_list_iterate(pplayer->cities, pcity) {
145 GValue value = { 0, };
146
147 gtk_list_store_append(store, &it);
148
149 g_value_init(&value, G_TYPE_STRING);
150 g_value_set_static_string(&value, city_name_get(pcity));
151 gtk_list_store_set_value(store, &it, 0, &value);
152 g_value_unset(&value);
153
154 gtk_list_store_set(store, &it, 1, pcity, -1);
155 } city_list_iterate_end;
156 } players_iterate_end;
157 }
158
159 /**************************************************************************
160 User responded to find dialog
161 **************************************************************************/
find_response(struct gui_dialog * dlg,int response,gpointer data)162 static void find_response(struct gui_dialog *dlg, int response, gpointer data)
163 {
164 if (response == GTK_RESPONSE_ACCEPT) {
165 GtkTreeSelection *selection;
166 GtkTreeModel *model;
167 GtkTreeIter it;
168
169 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(find_view));
170
171 if (gtk_tree_selection_get_selected(selection, &model, &it)) {
172 struct city *pcity;
173
174 gtk_tree_model_get(model, &it, 1, &pcity, -1);
175
176 if (pcity) {
177 pos = pcity->tile;
178 }
179 }
180 }
181 gui_dialog_destroy(dlg);
182 }
183
184 /**************************************************************************
185 Find dialog destroyed
186 **************************************************************************/
find_destroy_callback(GtkWidget * w,gpointer data)187 static void find_destroy_callback(GtkWidget *w, gpointer data)
188 {
189 can_slide = FALSE;
190 center_tile_mapcanvas(pos);
191 can_slide = TRUE;
192 }
193
194 /**************************************************************************
195 User selected city from find dialog
196 **************************************************************************/
find_selection_callback(GtkTreeSelection * selection,GtkTreeModel * model)197 static void find_selection_callback(GtkTreeSelection *selection,
198 GtkTreeModel *model)
199 {
200 GtkTreeIter it;
201 struct city *pcity;
202
203 if (!gtk_tree_selection_get_selected(selection, NULL, &it))
204 return;
205
206 gtk_tree_model_get(model, &it, 1, &pcity, -1);
207
208 if (pcity) {
209 can_slide = FALSE;
210 center_tile_mapcanvas(pcity->tile);
211 can_slide = TRUE;
212 }
213 }
214