PageRenderTime 47ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/src/vikgoto.c

https://bitbucket.org/Spencerx/viking
C | 190 lines | 134 code | 33 blank | 23 comment | 25 complexity | 84f36ca4d6ec838490a55d5fe6d89014 MD5 | raw file
  1. /*
  2. * viking -- GPS Data and Topo Analyzer, Explorer, and Manager
  3. *
  4. * Copyright (C) 2003-2005, Evan Battaglia <gtoevan@gmx.net>
  5. * Copyright (C) 2009, Guilhem Bonnefille <guilhem.bonnefille@gmail.com>
  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. *
  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. #include "viking.h"
  33. #include "util.h"
  34. #include "vikgototool.h"
  35. #include "vikgoto.h"
  36. static gchar *last_goto_str = NULL;
  37. static VikCoord *last_coord = NULL;
  38. static gchar *last_successful_goto_str = NULL;
  39. static GList *goto_tools_list = NULL;
  40. int last_goto_tool = 0;
  41. void vik_goto_register ( VikGotoTool *tool )
  42. {
  43. if ( IS_VIK_GOTO_TOOL( tool ) )
  44. goto_tools_list = g_list_append ( goto_tools_list, g_object_ref ( tool ) );
  45. }
  46. void vik_goto_unregister_all ()
  47. {
  48. g_list_foreach ( goto_tools_list, (GFunc) g_object_unref, NULL );
  49. }
  50. gchar * a_vik_goto_get_search_string_for_this_place(VikWindow *vw)
  51. {
  52. if (!last_coord)
  53. return NULL;
  54. VikViewport *vvp = vik_window_viewport(vw);
  55. const VikCoord *cur_center = vik_viewport_get_center(vvp);
  56. if (vik_coord_equals(cur_center, last_coord)) {
  57. return(last_successful_goto_str);
  58. }
  59. else
  60. return NULL;
  61. }
  62. static void display_no_tool(VikWindow *vw)
  63. {
  64. GtkWidget *dialog = NULL;
  65. dialog = gtk_message_dialog_new ( GTK_WINDOW(vw), GTK_DIALOG_MODAL, GTK_MESSAGE_WARNING, GTK_BUTTONS_OK, _("No goto tool available.") );
  66. gtk_dialog_run ( GTK_DIALOG(dialog) );
  67. gtk_widget_destroy(dialog);
  68. }
  69. static gboolean prompt_try_again(VikWindow *vw)
  70. {
  71. GtkWidget *dialog = NULL;
  72. gboolean ret = TRUE;
  73. dialog = gtk_dialog_new_with_buttons ( "", GTK_WINDOW(vw), 0, GTK_STOCK_OK, GTK_RESPONSE_ACCEPT, GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT, NULL );
  74. gtk_window_set_title(GTK_WINDOW(dialog), _("goto"));
  75. GtkWidget *goto_label = gtk_label_new(_("I don't know that place. Do you want another goto?"));
  76. gtk_box_pack_start ( GTK_BOX(GTK_DIALOG(dialog)->vbox), goto_label, FALSE, FALSE, 5 );
  77. gtk_dialog_set_default_response ( GTK_DIALOG(dialog), GTK_RESPONSE_ACCEPT );
  78. gtk_widget_show_all(dialog);
  79. if ( gtk_dialog_run ( GTK_DIALOG(dialog) ) != GTK_RESPONSE_ACCEPT )
  80. ret = FALSE;
  81. gtk_widget_destroy(dialog);
  82. return ret;
  83. }
  84. static gchar * a_prompt_for_goto_string(VikWindow *vw)
  85. {
  86. GtkWidget *dialog = NULL;
  87. dialog = gtk_dialog_new_with_buttons ( "", GTK_WINDOW(vw), 0, GTK_STOCK_OK, GTK_RESPONSE_ACCEPT, GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT, NULL );
  88. gtk_window_set_title(GTK_WINDOW(dialog), _("goto"));
  89. GtkWidget *tool_label = gtk_label_new(_("goto provider:"));
  90. GtkWidget *tool_list = gtk_combo_box_new_text ();
  91. GList *current = g_list_first (goto_tools_list);
  92. while (current != NULL)
  93. {
  94. char *label = NULL;
  95. VikGotoTool *tool = current->data;
  96. label = vik_goto_tool_get_label (tool);
  97. gtk_combo_box_append_text ( GTK_COMBO_BOX( tool_list ), label);
  98. current = g_list_next (current);
  99. }
  100. /* Set the previously selected provider as default */
  101. gtk_combo_box_set_active ( GTK_COMBO_BOX( tool_list ), last_goto_tool);
  102. GtkWidget *goto_label = gtk_label_new(_("Enter address or place name:"));
  103. GtkWidget *goto_entry = gtk_entry_new();
  104. if (last_goto_str)
  105. gtk_entry_set_text(GTK_ENTRY(goto_entry), last_goto_str);
  106. gtk_box_pack_start ( GTK_BOX(GTK_DIALOG(dialog)->vbox), tool_label, FALSE, FALSE, 5 );
  107. gtk_box_pack_start ( GTK_BOX(GTK_DIALOG(dialog)->vbox), tool_list, FALSE, FALSE, 5 );
  108. gtk_box_pack_start ( GTK_BOX(GTK_DIALOG(dialog)->vbox), goto_label, FALSE, FALSE, 5 );
  109. gtk_box_pack_start ( GTK_BOX(GTK_DIALOG(dialog)->vbox), goto_entry, FALSE, FALSE, 5 );
  110. gtk_dialog_set_default_response ( GTK_DIALOG(dialog), GTK_RESPONSE_ACCEPT );
  111. gtk_widget_show_all(dialog);
  112. if ( gtk_dialog_run ( GTK_DIALOG(dialog) ) != GTK_RESPONSE_ACCEPT ) {
  113. gtk_widget_destroy(dialog);
  114. return NULL;
  115. }
  116. last_goto_tool = gtk_combo_box_get_active ( GTK_COMBO_BOX (tool_list) );
  117. gchar *goto_str = g_strdup ( gtk_entry_get_text ( GTK_ENTRY(goto_entry) ) );
  118. gtk_widget_destroy(dialog);
  119. if (goto_str[0] != '\0') {
  120. if (last_goto_str)
  121. g_free(last_goto_str);
  122. last_goto_str = g_strdup(goto_str);
  123. }
  124. return(goto_str); /* goto_str needs to be freed by caller */
  125. }
  126. void a_vik_goto(VikWindow *vw, VikViewport *vvp)
  127. {
  128. VikCoord new_center;
  129. gchar *s_str;
  130. gboolean more = TRUE;
  131. if (goto_tools_list == NULL)
  132. {
  133. /* Empty list */
  134. display_no_tool(vw);
  135. return;
  136. }
  137. do {
  138. s_str = a_prompt_for_goto_string(vw);
  139. if ((!s_str) || (s_str[0] == 0)) {
  140. more = FALSE;
  141. }
  142. else if (!vik_goto_tool_get_coord(g_list_nth_data (goto_tools_list, last_goto_tool), vw, vvp, s_str, &new_center)) {
  143. if (last_coord)
  144. g_free(last_coord);
  145. last_coord = g_malloc(sizeof(VikCoord));
  146. *last_coord = new_center;
  147. if (last_successful_goto_str)
  148. g_free(last_successful_goto_str);
  149. last_successful_goto_str = g_strdup(last_goto_str);
  150. vik_viewport_set_center_coord(vvp, &new_center);
  151. more = FALSE;
  152. }
  153. else if (!prompt_try_again(vw))
  154. more = FALSE;
  155. g_free(s_str);
  156. } while (more);
  157. }