]> git.street.me.uk Git - andy/viking.git/blobdiff - src/vikrouting.c
SF Bugs#133: Remove the auto added map when opening the first .vik file from the...
[andy/viking.git] / src / vikrouting.c
index 74a8e71354da2bf543e8e83ebd54897a10df7fc2..1192cdab5e4ef4c6d7d0e868f9d79f6b7f9967e7 100644 (file)
@@ -51,7 +51,7 @@
 static GList *routing_engine_list = NULL;
 
 static VikLayerParam prefs[] = {
-  { VIK_LAYER_NUM_TYPES, VIKING_ROUTING_PARAMS_NAMESPACE "default", VIK_LAYER_PARAM_STRING, VIK_LAYER_GROUP_NONE, N_("Default engine:"), VIK_LAYER_WIDGET_COMBOBOX, NULL, NULL, NULL },
+  { VIK_LAYER_NUM_TYPES, VIKING_ROUTING_PARAMS_NAMESPACE "default", VIK_LAYER_PARAM_STRING, VIK_LAYER_GROUP_NONE, N_("Default engine:"), VIK_LAYER_WIDGET_COMBOBOX, NULL, NULL, NULL, NULL, NULL, NULL },
 };
 
 gchar **routing_engine_labels = NULL;
@@ -65,13 +65,14 @@ gchar **routing_engine_ids = NULL;
 void
 vik_routing_prefs_init()
 {
-  a_preferences_register_group ( VIKING_ROUTING_PARAMS_GROUP_KEY, "Routing" );
+  a_preferences_register_group ( VIKING_ROUTING_PARAMS_GROUP_KEY, _("Routing") );
 
   VikLayerParamData tmp;
   tmp.s = NULL;
   a_preferences_register(prefs, tmp, VIKING_ROUTING_PARAMS_GROUP_KEY);
 }
 
+/* @see g_list_find_custom */
 static gint
 search_by_id (gconstpointer a,
               gconstpointer b)
@@ -108,7 +109,7 @@ vik_routing_find_engine ( const gchar *id )
  * 
  * Returns: the default engine
  */
-static VikRoutingEngine *
+VikRoutingEngine *
 vik_routing_default_engine ( void )
 {
   const gchar *id = a_preferences_get ( VIKING_ROUTING_PARAMS_NAMESPACE "default")->s;
@@ -124,14 +125,16 @@ vik_routing_default_engine ( void )
  * vik_routing_default_find:
  * 
  * Route computation with default engine.
+ *
+ * Return indicates success or not
  */
-void
+gboolean
 vik_routing_default_find(VikTrwLayer *vt, struct LatLon start, struct LatLon end)
 {
   /* The engine */
   VikRoutingEngine *engine = vik_routing_default_engine ( );
   /* The route computation */
-  vik_routing_engine_find ( engine, vt, start, end );
+  return vik_routing_engine_find ( engine, vt, start, end );
 }
 
 /**
@@ -221,3 +224,84 @@ vik_routing_foreach_engine (GFunc func, gpointer user_data)
 {
   g_list_foreach ( routing_engine_list, func, user_data );
 }
+
+/*
+ * This function is called for all routing engine registered.
+ * Following result of the predicate function, the current engine
+ * is added to the combobox. In order to retrieve the VikRoutingEngine
+ * object, we store a list of added engine in a GObject's data "engines".
+ *
+ * @see g_list_foreach()
+ */
+static void
+fill_engine_box (gpointer data, gpointer user_data)
+{
+  VikRoutingEngine *engine = (VikRoutingEngine*) data;
+  /* Retrieve combo */
+  GtkWidget *widget = (GtkWidget*) user_data;
+
+  /* Only register engine fulliling expected behavior */
+  Predicate predicate = g_object_get_data ( G_OBJECT ( widget ), "func" );
+  gpointer predicate_data = g_object_get_data ( G_OBJECT ( widget ), "user_data" );
+  /* No predicate means to register all engines */
+  gboolean ok = predicate == NULL || predicate (engine, predicate_data);
+
+  if (ok)
+  {
+    /* Add item in widget */
+    const gchar *label = vik_routing_engine_get_label (engine);
+    vik_combo_box_text_append (widget, label);
+    /* Save engine in internal list */
+    GList *engines = (GList*) g_object_get_data ( G_OBJECT ( widget ) , "engines" );
+    engines = g_list_append ( engines, engine );
+    g_object_set_data ( G_OBJECT ( widget ), "engines", engines );
+  }
+}
+
+/**
+ * vik_routing_ui_selector_new:
+ * @func: user function to decide if an engine has to be added or not
+ * @user_data: user data for previous function
+ *
+ * Creates a combo box to allow selection of a routing engine.
+ *
+ * We use GObject data hastable to store and retrieve the VikRoutingEngine
+ * associated to the selection.
+ *
+ * Returns: the combo box
+ */
+GtkWidget *
+vik_routing_ui_selector_new (Predicate func, gpointer user_data)
+{
+  /* Create the combo */
+  GtkWidget * combo = vik_combo_box_text_new ();
+
+  /* Save data for foreach function */
+  g_object_set_data ( G_OBJECT ( combo ), "func", func );
+  g_object_set_data ( G_OBJECT ( combo ), "user_data", user_data );
+
+  /* Filter all engines with given user function */
+  vik_routing_foreach_engine (fill_engine_box, combo);
+
+  return combo;
+}
+
+/**
+ * vik_routing_ui_selector_get_nth:
+ * @combo: the GtkWidget combobox
+ * @pos: the selected position
+ *
+ * Retrieve the VikRoutingEngine stored in a list attached to @combo
+ * via the "engines" property.
+ *
+ * Returns: the VikRoutingEngine object associated to @pos
+ */
+VikRoutingEngine *
+vik_routing_ui_selector_get_nth (GtkWidget *combo, int pos)
+{
+  /* Retrieve engine */
+  GList *engines = (GList*) g_object_get_data ( G_OBJECT ( combo ) , "engines" );
+  VikRoutingEngine *engine = g_list_nth_data ( engines, pos );
+
+  return engine;
+}