]> git.street.me.uk Git - andy/viking.git/blame - src/vikgoto.c
[DOC] Add documentation on new public routing function
[andy/viking.git] / src / vikgoto.c
CommitLineData
6571a7de
GB
1/*
2 * viking -- GPS Data and Topo Analyzer, Explorer, and Manager
3 *
4 * Copyright (C) 2003-2005, Evan Battaglia <gtoevan@gmx.net>
bfecd26e 5 * Copyright (C) 2009, Guilhem Bonnefille <guilhem.bonnefille@gmail.com>
6571a7de
GB
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 *
6571a7de
GB
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"
34e71b99 35#include "vikgototool.h"
3d2c0c26 36#include "vikgoto.h"
6571a7de 37
34e71b99 38static gchar *last_goto_str = NULL;
6571a7de 39static VikCoord *last_coord = NULL;
34e71b99 40static gchar *last_successful_goto_str = NULL;
6571a7de 41
34e71b99 42static GList *goto_tools_list = NULL;
6571a7de 43
a29f8468 44int last_goto_tool = 0;
6571a7de 45
34e71b99 46void vik_goto_register ( VikGotoTool *tool )
6571a7de 47{
619a68f3
RN
48 if ( IS_VIK_GOTO_TOOL( tool ) )
49 goto_tools_list = g_list_append ( goto_tools_list, g_object_ref ( tool ) );
6571a7de
GB
50}
51
34e71b99 52void vik_goto_unregister_all ()
6571a7de 53{
34e71b99 54 g_list_foreach ( goto_tools_list, (GFunc) g_object_unref, NULL );
6571a7de
GB
55}
56
34e71b99 57gchar * a_vik_goto_get_search_string_for_this_place(VikWindow *vw)
6571a7de
GB
58{
59 if (!last_coord)
60 return NULL;
61
62 VikViewport *vvp = vik_window_viewport(vw);
63 const VikCoord *cur_center = vik_viewport_get_center(vvp);
64 if (vik_coord_equals(cur_center, last_coord)) {
34e71b99 65 return(last_successful_goto_str);
6571a7de
GB
66 }
67 else
68 return NULL;
69}
70
71static void display_no_tool(VikWindow *vw)
72{
73 GtkWidget *dialog = NULL;
74
34e71b99 75 dialog = gtk_message_dialog_new ( GTK_WINDOW(vw), GTK_DIALOG_MODAL, GTK_MESSAGE_WARNING, GTK_BUTTONS_OK, _("No goto tool available.") );
6571a7de
GB
76
77 gtk_dialog_run ( GTK_DIALOG(dialog) );
78
79 gtk_widget_destroy(dialog);
80}
81
82static gboolean prompt_try_again(VikWindow *vw)
83{
84 GtkWidget *dialog = NULL;
85 gboolean ret = TRUE;
86
87 dialog = gtk_dialog_new_with_buttons ( "", GTK_WINDOW(vw), 0, GTK_STOCK_OK, GTK_RESPONSE_ACCEPT, GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT, NULL );
34e71b99 88 gtk_window_set_title(GTK_WINDOW(dialog), _("goto"));
6571a7de 89
34e71b99 90 GtkWidget *goto_label = gtk_label_new(_("I don't know that place. Do you want another goto?"));
9b082b39 91 gtk_box_pack_start ( GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dialog))), goto_label, FALSE, FALSE, 5 );
5b7d7928 92 gtk_dialog_set_default_response ( GTK_DIALOG(dialog), GTK_RESPONSE_ACCEPT );
6571a7de
GB
93 gtk_widget_show_all(dialog);
94
95 if ( gtk_dialog_run ( GTK_DIALOG(dialog) ) != GTK_RESPONSE_ACCEPT )
96 ret = FALSE;
97
98 gtk_widget_destroy(dialog);
99 return ret;
100}
101
34e71b99 102static gchar * a_prompt_for_goto_string(VikWindow *vw)
6571a7de
GB
103{
104 GtkWidget *dialog = NULL;
105
106 dialog = gtk_dialog_new_with_buttons ( "", GTK_WINDOW(vw), 0, GTK_STOCK_OK, GTK_RESPONSE_ACCEPT, GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT, NULL );
34e71b99 107 gtk_window_set_title(GTK_WINDOW(dialog), _("goto"));
6571a7de 108
34e71b99 109 GtkWidget *tool_label = gtk_label_new(_("goto provider:"));
1bc1c05b 110 GtkWidget *tool_list = vik_combo_box_text_new ();
34e71b99 111 GList *current = g_list_first (goto_tools_list);
6571a7de
GB
112 while (current != NULL)
113 {
114 char *label = NULL;
34e71b99
GB
115 VikGotoTool *tool = current->data;
116 label = vik_goto_tool_get_label (tool);
1bc1c05b 117 vik_combo_box_text_append ( tool_list, label );
6571a7de
GB
118 current = g_list_next (current);
119 }
a29f8468
GB
120 /* Set the previously selected provider as default */
121 gtk_combo_box_set_active ( GTK_COMBO_BOX( tool_list ), last_goto_tool);
6571a7de 122
34e71b99
GB
123 GtkWidget *goto_label = gtk_label_new(_("Enter address or place name:"));
124 GtkWidget *goto_entry = gtk_entry_new();
125 if (last_goto_str)
126 gtk_entry_set_text(GTK_ENTRY(goto_entry), last_goto_str);
6571a7de 127
ae1d3fd1
RN
128 // 'ok' when press return in the entry
129 g_signal_connect_swapped (goto_entry, "activate", G_CALLBACK(a_dialog_response_accept), dialog);
130
9b082b39
RN
131 gtk_box_pack_start ( GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dialog))), tool_label, FALSE, FALSE, 5 );
132 gtk_box_pack_start ( GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dialog))), tool_list, FALSE, FALSE, 5 );
133 gtk_box_pack_start ( GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dialog))), goto_label, FALSE, FALSE, 5 );
134 gtk_box_pack_start ( GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dialog))), goto_entry, FALSE, FALSE, 5 );
5b7d7928 135 gtk_dialog_set_default_response ( GTK_DIALOG(dialog), GTK_RESPONSE_ACCEPT );
6571a7de
GB
136 gtk_widget_show_all(dialog);
137
ae1d3fd1
RN
138 // Ensure the text field has focus so we can start typing straight away
139 gtk_widget_grab_focus ( goto_entry );
140
6571a7de
GB
141 if ( gtk_dialog_run ( GTK_DIALOG(dialog) ) != GTK_RESPONSE_ACCEPT ) {
142 gtk_widget_destroy(dialog);
143 return NULL;
144 }
145
34e71b99 146 last_goto_tool = gtk_combo_box_get_active ( GTK_COMBO_BOX (tool_list) );
6571a7de 147
34e71b99 148 gchar *goto_str = g_strdup ( gtk_entry_get_text ( GTK_ENTRY(goto_entry) ) );
6571a7de
GB
149
150 gtk_widget_destroy(dialog);
151
34e71b99
GB
152 if (goto_str[0] != '\0') {
153 if (last_goto_str)
154 g_free(last_goto_str);
155 last_goto_str = g_strdup(goto_str);
6571a7de
GB
156 }
157
34e71b99 158 return(goto_str); /* goto_str needs to be freed by caller */
6571a7de
GB
159}
160
c36a079a 161void a_vik_goto(VikWindow *vw, VikViewport *vvp)
6571a7de
GB
162{
163 VikCoord new_center;
164 gchar *s_str;
165 gboolean more = TRUE;
166
34e71b99 167 if (goto_tools_list == NULL)
6571a7de
GB
168 {
169 /* Empty list */
170 display_no_tool(vw);
171 return;
172 }
173
174 do {
34e71b99 175 s_str = a_prompt_for_goto_string(vw);
6571a7de
GB
176 if ((!s_str) || (s_str[0] == 0)) {
177 more = FALSE;
178 }
179
34e71b99 180 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
181 if (last_coord)
182 g_free(last_coord);
183 last_coord = g_malloc(sizeof(VikCoord));
184 *last_coord = new_center;
34e71b99
GB
185 if (last_successful_goto_str)
186 g_free(last_successful_goto_str);
187 last_successful_goto_str = g_strdup(last_goto_str);
6571a7de 188 vik_viewport_set_center_coord(vvp, &new_center);
6571a7de
GB
189 more = FALSE;
190 }
191 else if (!prompt_try_again(vw))
192 more = FALSE;
193 g_free(s_str);
194 } while (more);
195}