]>
Commit | Line | Data |
---|---|---|
6571a7de GB |
1 | /* |
2 | * viking -- GPS Data and Topo Analyzer, Explorer, and Manager | |
3 | * | |
4 | * Copyright (C) 2003-2005, Evan Battaglia <gtoevan@gmx.net> | |
5 | * | |
6 | * This program is free software; you can redistribute it and/or modify | |
7 | * it under the terms of the GNU General Public License as published by | |
8 | * the Free Software Foundation; either version 2 of the License, or | |
9 | * (at your option) any later version. | |
10 | * | |
11 | * This program is distributed in the hope that it will be useful, | |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | * GNU General Public License for more details. | |
15 | * | |
16 | * You should have received a copy of the GNU General Public License | |
17 | * along with this program; if not, write to the Free Software | |
18 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
19 | * | |
20 | * Created by Quy Tonthat <qtonthat@gmail.com> | |
21 | */ | |
22 | #ifdef HAVE_CONFIG_H | |
23 | #include "config.h" | |
24 | #endif | |
25 | #include <stdlib.h> | |
26 | #include <stdio.h> | |
27 | #include <string.h> | |
28 | #include <glib.h> | |
29 | #include <glib/gstdio.h> | |
30 | #include <glib/gprintf.h> | |
31 | #include <glib/gi18n.h> | |
32 | ||
33 | #include "viking.h" | |
34 | #include "util.h" | |
35 | #include "curl_download.h" | |
36 | ||
34e71b99 | 37 | #include "vikgototool.h" |
6571a7de | 38 | |
34e71b99 | 39 | static gchar *last_goto_str = NULL; |
6571a7de | 40 | static VikCoord *last_coord = NULL; |
34e71b99 | 41 | static gchar *last_successful_goto_str = NULL; |
6571a7de | 42 | |
34e71b99 | 43 | static GList *goto_tools_list = NULL; |
6571a7de | 44 | |
a29f8468 | 45 | int last_goto_tool = 0; |
6571a7de | 46 | |
34e71b99 | 47 | void vik_goto_register ( VikGotoTool *tool ) |
6571a7de | 48 | { |
34e71b99 | 49 | IS_VIK_GOTO_TOOL( tool ); |
6571a7de | 50 | |
34e71b99 | 51 | goto_tools_list = g_list_append ( goto_tools_list, g_object_ref ( tool ) ); |
6571a7de GB |
52 | } |
53 | ||
34e71b99 | 54 | void vik_goto_unregister_all () |
6571a7de | 55 | { |
34e71b99 | 56 | g_list_foreach ( goto_tools_list, (GFunc) g_object_unref, NULL ); |
6571a7de GB |
57 | } |
58 | ||
34e71b99 | 59 | gchar * a_vik_goto_get_search_string_for_this_place(VikWindow *vw) |
6571a7de GB |
60 | { |
61 | if (!last_coord) | |
62 | return NULL; | |
63 | ||
64 | VikViewport *vvp = vik_window_viewport(vw); | |
65 | const VikCoord *cur_center = vik_viewport_get_center(vvp); | |
66 | if (vik_coord_equals(cur_center, last_coord)) { | |
34e71b99 | 67 | return(last_successful_goto_str); |
6571a7de GB |
68 | } |
69 | else | |
70 | return NULL; | |
71 | } | |
72 | ||
73 | static void display_no_tool(VikWindow *vw) | |
74 | { | |
75 | GtkWidget *dialog = NULL; | |
76 | ||
34e71b99 | 77 | dialog = gtk_message_dialog_new ( GTK_WINDOW(vw), GTK_DIALOG_MODAL, GTK_MESSAGE_WARNING, GTK_BUTTONS_OK, _("No goto tool available.") ); |
6571a7de GB |
78 | |
79 | gtk_dialog_run ( GTK_DIALOG(dialog) ); | |
80 | ||
81 | gtk_widget_destroy(dialog); | |
82 | } | |
83 | ||
84 | static gboolean prompt_try_again(VikWindow *vw) | |
85 | { | |
86 | GtkWidget *dialog = NULL; | |
87 | gboolean ret = TRUE; | |
88 | ||
89 | dialog = gtk_dialog_new_with_buttons ( "", GTK_WINDOW(vw), 0, GTK_STOCK_OK, GTK_RESPONSE_ACCEPT, GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT, NULL ); | |
34e71b99 | 90 | gtk_window_set_title(GTK_WINDOW(dialog), _("goto")); |
6571a7de | 91 | |
34e71b99 GB |
92 | GtkWidget *goto_label = gtk_label_new(_("I don't know that place. Do you want another goto?")); |
93 | gtk_box_pack_start ( GTK_BOX(GTK_DIALOG(dialog)->vbox), goto_label, FALSE, FALSE, 5 ); | |
6571a7de GB |
94 | gtk_widget_show_all(dialog); |
95 | ||
96 | if ( gtk_dialog_run ( GTK_DIALOG(dialog) ) != GTK_RESPONSE_ACCEPT ) | |
97 | ret = FALSE; | |
98 | ||
99 | gtk_widget_destroy(dialog); | |
100 | return ret; | |
101 | } | |
102 | ||
34e71b99 | 103 | static gchar * a_prompt_for_goto_string(VikWindow *vw) |
6571a7de GB |
104 | { |
105 | GtkWidget *dialog = NULL; | |
106 | ||
107 | dialog = gtk_dialog_new_with_buttons ( "", GTK_WINDOW(vw), 0, GTK_STOCK_OK, GTK_RESPONSE_ACCEPT, GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT, NULL ); | |
34e71b99 | 108 | gtk_window_set_title(GTK_WINDOW(dialog), _("goto")); |
6571a7de | 109 | |
34e71b99 | 110 | GtkWidget *tool_label = gtk_label_new(_("goto provider:")); |
6571a7de GB |
111 | GtkWidget *tool_list = gtk_combo_box_new_text (); |
112 | ||
34e71b99 | 113 | GList *current = g_list_first (goto_tools_list); |
6571a7de GB |
114 | while (current != NULL) |
115 | { | |
116 | char *label = NULL; | |
34e71b99 GB |
117 | VikGotoTool *tool = current->data; |
118 | label = vik_goto_tool_get_label (tool); | |
6571a7de GB |
119 | gtk_combo_box_append_text ( GTK_COMBO_BOX( tool_list ), label); |
120 | current = g_list_next (current); | |
121 | } | |
a29f8468 GB |
122 | /* Set the previously selected provider as default */ |
123 | gtk_combo_box_set_active ( GTK_COMBO_BOX( tool_list ), last_goto_tool); | |
6571a7de | 124 | |
34e71b99 GB |
125 | GtkWidget *goto_label = gtk_label_new(_("Enter address or place name:")); |
126 | GtkWidget *goto_entry = gtk_entry_new(); | |
127 | if (last_goto_str) | |
128 | gtk_entry_set_text(GTK_ENTRY(goto_entry), last_goto_str); | |
6571a7de GB |
129 | |
130 | gtk_box_pack_start ( GTK_BOX(GTK_DIALOG(dialog)->vbox), tool_label, FALSE, FALSE, 5 ); | |
131 | gtk_box_pack_start ( GTK_BOX(GTK_DIALOG(dialog)->vbox), tool_list, FALSE, FALSE, 5 ); | |
34e71b99 GB |
132 | gtk_box_pack_start ( GTK_BOX(GTK_DIALOG(dialog)->vbox), goto_label, FALSE, FALSE, 5 ); |
133 | gtk_box_pack_start ( GTK_BOX(GTK_DIALOG(dialog)->vbox), goto_entry, FALSE, FALSE, 5 ); | |
6571a7de GB |
134 | gtk_widget_show_all(dialog); |
135 | ||
136 | if ( gtk_dialog_run ( GTK_DIALOG(dialog) ) != GTK_RESPONSE_ACCEPT ) { | |
137 | gtk_widget_destroy(dialog); | |
138 | return NULL; | |
139 | } | |
140 | ||
34e71b99 | 141 | last_goto_tool = gtk_combo_box_get_active ( GTK_COMBO_BOX (tool_list) ); |
6571a7de | 142 | |
34e71b99 | 143 | gchar *goto_str = g_strdup ( gtk_entry_get_text ( GTK_ENTRY(goto_entry) ) ); |
6571a7de GB |
144 | |
145 | gtk_widget_destroy(dialog); | |
146 | ||
34e71b99 GB |
147 | if (goto_str[0] != '\0') { |
148 | if (last_goto_str) | |
149 | g_free(last_goto_str); | |
150 | last_goto_str = g_strdup(goto_str); | |
6571a7de GB |
151 | } |
152 | ||
34e71b99 | 153 | return(goto_str); /* goto_str needs to be freed by caller */ |
6571a7de GB |
154 | } |
155 | ||
34e71b99 | 156 | void a_vik_goto(VikWindow *vw, VikLayersPanel *vlp, VikViewport *vvp) |
6571a7de GB |
157 | { |
158 | VikCoord new_center; | |
159 | gchar *s_str; | |
160 | gboolean more = TRUE; | |
161 | ||
34e71b99 | 162 | if (goto_tools_list == NULL) |
6571a7de GB |
163 | { |
164 | /* Empty list */ | |
165 | display_no_tool(vw); | |
166 | return; | |
167 | } | |
168 | ||
169 | do { | |
34e71b99 | 170 | s_str = a_prompt_for_goto_string(vw); |
6571a7de GB |
171 | if ((!s_str) || (s_str[0] == 0)) { |
172 | more = FALSE; | |
173 | } | |
174 | ||
34e71b99 | 175 | else if (!vik_goto_tool_get_coord(g_list_nth_data (goto_tools_list, last_goto_tool), vw, vvp, s_str, &new_center)) { |
0810881f GB |
176 | if (last_coord) |
177 | g_free(last_coord); | |
178 | last_coord = g_malloc(sizeof(VikCoord)); | |
179 | *last_coord = new_center; | |
34e71b99 GB |
180 | if (last_successful_goto_str) |
181 | g_free(last_successful_goto_str); | |
182 | last_successful_goto_str = g_strdup(last_goto_str); | |
6571a7de GB |
183 | vik_viewport_set_center_coord(vvp, &new_center); |
184 | vik_layers_panel_emit_update(vlp); | |
185 | more = FALSE; | |
186 | } | |
187 | else if (!prompt_try_again(vw)) | |
188 | more = FALSE; | |
189 | g_free(s_str); | |
190 | } while (more); | |
191 | } |