From 92806042fbacaadd1a9216d01c7b2503ede459af Mon Sep 17 00:00:00 2001 From: Guilhem Bonnefille Date: Sat, 13 Dec 2008 21:31:37 +0000 Subject: [PATCH] News feature: external tools Come with some concrete tools: - Google maps - OpenStreetMap - Edit OpenStreetMap - Render Osmarender --- ChangeLog | 5 + src/Makefile.am | 4 + src/google.c | 7 ++ src/menu.xml.h | 1 + src/osm.c | 18 +++- src/vikexttool.c | 209 ++++++++++++++++++++++++++++++++++++++++++ src/vikexttool.h | 55 +++++++++++ src/vikexttools.c | 86 +++++++++++++++++ src/vikexttools.h | 34 +++++++ src/vikwebtool.c | 108 ++++++++++++++++++++++ src/vikwebtool.h | 55 +++++++++++ src/vikwebtoolcenter.c | 244 +++++++++++++++++++++++++++++++++++++++++++++++++ src/vikwebtoolcenter.h | 56 ++++++++++++ src/vikwindow.c | 3 + 14 files changed, 884 insertions(+), 1 deletion(-) create mode 100644 src/vikexttool.c create mode 100644 src/vikexttool.h create mode 100644 src/vikexttools.c create mode 100644 src/vikexttools.h create mode 100644 src/vikwebtool.c create mode 100644 src/vikwebtool.h create mode 100644 src/vikwebtoolcenter.c create mode 100644 src/vikwebtoolcenter.h diff --git a/ChangeLog b/ChangeLog index b821907c..8652e8f7 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,8 @@ +2008-12-13 +Guilhem Bonnefille : + * Add external tools framework + * Add OpenStreetMap and Google web tools + 2008-12-07 Quy Tonthat : * Clicking on "vivisble" tick no longer makes the layer selected. diff --git a/src/Makefile.am b/src/Makefile.am index 2ce9396a..81b12805 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -69,6 +69,10 @@ libviking_a_SOURCES = \ dem.c dem.h \ vikdemlayer.h vikdemlayer.c \ vikfilelist.c vikfilelist.h \ + vikexttool.c vikexttool.h \ + vikexttools.c vikexttools.h \ + vikwebtool.c vikwebtool.h \ + vikwebtoolcenter.c vikwebtoolcenter.h \ dems.c dems.h \ srtm_continent.c \ uibuilder.c uibuilder.h \ diff --git a/src/google.c b/src/google.c index 7ea0c7dc..08a0af6e 100644 --- a/src/google.c +++ b/src/google.c @@ -43,6 +43,8 @@ #include "globals.h" #include "google.h" #include "vikmapslayer.h" +#include "vikexttools.h" +#include "vikwebtoolcenter.h" static int google_download ( MapCoord *src, const gchar *dest_fn ); @@ -61,6 +63,11 @@ void google_init () { maps_layer_register_type(_("Google Street Maps"), 7, &google_1); maps_layer_register_type(_("Transparent Google Maps"), 10, &google_2); maps_layer_register_type(_("Google Terrain Maps"), 16, &google_4); + + // Webtools + VikWebtoolCenter *webtool = vik_webtool_center_new_with_members ( _("Google"), "http://maps.google.com/maps?f=q&hl=fr&geocode=&ie=UTF8&ll=%s,%s&z=%d&iwloc=addr" ); + vik_ext_tools_register ( VIK_EXT_TOOL ( webtool ) ); + g_object_unref ( webtool ); } /* 1 << (x) is like a 2**(x) */ diff --git a/src/menu.xml.h b/src/menu.xml.h index 5342f3f7..035632c5 100644 --- a/src/menu.xml.h +++ b/src/menu.xml.h @@ -87,6 +87,7 @@ static const char *menu_xml = " " " " " " + " " " " " " " " diff --git a/src/osm.c b/src/osm.c index 4b8d66e7..2152b197 100644 --- a/src/osm.c +++ b/src/osm.c @@ -23,15 +23,17 @@ #endif #include +#include #ifdef HAVE_MATH_H #include #endif - #include "viking.h" #include "coords.h" #include "vikcoord.h" #include "mapcoord.h" #include "vikmapslayer.h" +#include "vikwebtoolcenter.h" +#include "vikexttools.h" #include "osm.h" @@ -65,6 +67,20 @@ void osm_init () { maps_layer_register_type("BlueMarble", 15, &bluemarble_type); maps_layer_register_type("OpenAerialMap", 20, &openaerialmap_type); + + // Webtools + VikWebtoolCenter *webtool = NULL; + webtool = vik_webtool_center_new_with_members ( _("OSM (view)"), "http://openstreetmap.org/?lat=%s&lon=%s&zoom=%d&layers=B000FTF" ); + vik_ext_tools_register ( VIK_EXT_TOOL ( webtool ) ); + g_object_unref ( webtool ); + + webtool = vik_webtool_center_new_with_members ( _("OSM (edit)"), "http://www.openstreetmap.org/edit?lat=%s&lon=%s&zoom=%d" ); + vik_ext_tools_register ( VIK_EXT_TOOL ( webtool ) ); + g_object_unref ( webtool ); + + webtool = vik_webtool_center_new_with_members ( _("OSM (render)"), "http://www.informationfreeway.org/?lat=%s&lon=%s&zoom=%d&layers=B0000F000F" ); + vik_ext_tools_register ( VIK_EXT_TOOL ( webtool ) ); + g_object_unref ( webtool ); } /* 1 << (x) is like a 2**(x) */ diff --git a/src/vikexttool.c b/src/vikexttool.c new file mode 100644 index 00000000..66fb3566 --- /dev/null +++ b/src/vikexttool.c @@ -0,0 +1,209 @@ +/* + * viking -- GPS Data and Topo Analyzer, Explorer, and Manager + * + * Copyright (C) 2008, Guilhem Bonnefille + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include "vikexttool.h" + +#include + +#include + +static void ext_tool_class_init ( VikExtToolClass *klass ); +static void ext_tool_init ( VikExtTool *vlp ); + +static GObjectClass *parent_class; + +static void ext_tool_finalize ( GObject *gob ); +static gchar *ext_tool_get_label ( VikExtTool *vw ); + +typedef struct _VikExtToolPrivate VikExtToolPrivate; + +struct _VikExtToolPrivate +{ + gint id; + gchar *label; +}; + +#define EXT_TOOL_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), \ + VIK_EXT_TOOL_TYPE, \ + VikExtToolPrivate)) + +GType vik_ext_tool_get_type() +{ + static GType w_type = 0; + + if (!w_type) + { + static const GTypeInfo w_info = + { + sizeof (VikExtToolClass), + NULL, /* base_init */ + NULL, /* base_finalize */ + (GClassInitFunc) ext_tool_class_init, + NULL, /* class_finalize */ + NULL, /* class_data */ + sizeof (VikExtTool), + 0, + (GInstanceInitFunc) ext_tool_init, + }; + w_type = g_type_register_static ( G_TYPE_OBJECT, "VikExtTool", &w_info, G_TYPE_FLAG_ABSTRACT ); + } + + return w_type; +} + +enum +{ + PROP_0, + + PROP_ID, + PROP_LABEL, +}; + +static void +ext_tool_set_property (GObject *object, + guint property_id, + const GValue *value, + GParamSpec *pspec) +{ + VikExtTool *self = VIK_EXT_TOOL (object); + VikExtToolPrivate *priv = EXT_TOOL_GET_PRIVATE (self); + + switch (property_id) + { + case PROP_ID: + priv->id = g_value_get_uint (value); + g_debug ("VikExtTool.id: %d", priv->id); + break; + + case PROP_LABEL: + g_free (priv->label); + priv->label = g_value_dup_string (value); + g_debug ("VikExtTool.label: %s", priv->label); + break; + + default: + /* We don't have any other property... */ + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec); + break; + } +} + +static void +ext_tool_get_property (GObject *object, + guint property_id, + GValue *value, + GParamSpec *pspec) +{ + VikExtTool *self = VIK_EXT_TOOL (object); + VikExtToolPrivate *priv = EXT_TOOL_GET_PRIVATE (self); + + switch (property_id) + { + case PROP_ID: + g_value_set_uint (value, priv->id); + break; + + case PROP_LABEL: + g_value_set_string (value, priv->label); + break; + + default: + /* We don't have any other property... */ + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec); + break; + } +} + +static void ext_tool_class_init ( VikExtToolClass *klass ) +{ + GObjectClass *gobject_class; + GParamSpec *pspec; + + gobject_class = G_OBJECT_CLASS (klass); + gobject_class->finalize = ext_tool_finalize; + gobject_class->set_property = ext_tool_set_property; + gobject_class->get_property = ext_tool_get_property; + + pspec = g_param_spec_string ("label", + "Label", + "Set the label", + "" /* default value */, + G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE); + g_object_class_install_property (gobject_class, + PROP_LABEL, + pspec); + + pspec = g_param_spec_uint ("id", + "Id of the tool", + "Set the id", + 0 /* minimum value */, + G_MAXUINT16 /* maximum value */, + 0 /* default value */, + G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE); + g_object_class_install_property (gobject_class, + PROP_ID, + pspec); + + klass->get_label = ext_tool_get_label; + + parent_class = g_type_class_peek_parent (klass); + + g_type_class_add_private (klass, sizeof (VikExtToolPrivate)); +} + +VikExtTool *vik_ext_tool_new () +{ + return VIK_EXT_TOOL ( g_object_new ( VIK_EXT_TOOL_TYPE, NULL ) ); +} + +static void ext_tool_init ( VikExtTool *self ) +{ + VikExtToolPrivate *priv = EXT_TOOL_GET_PRIVATE (self); + priv->label = NULL; +} + +static void ext_tool_finalize ( GObject *gob ) +{ + VikExtToolPrivate *priv = EXT_TOOL_GET_PRIVATE ( gob ); + g_free ( priv->label ); priv->label = NULL; + G_OBJECT_CLASS(parent_class)->finalize(gob); +} + +static gchar *ext_tool_get_label ( VikExtTool *self ) +{ + VikExtToolPrivate *priv = NULL; + priv = EXT_TOOL_GET_PRIVATE (self); + return g_strdup ( priv->label ); +} + +gchar *vik_ext_tool_get_label ( VikExtTool *w ) +{ + return VIK_EXT_TOOL_GET_CLASS( w )->get_label( w ); +} + +void vik_ext_tool_open ( VikExtTool *self, VikWindow *vwindow ) +{ + VIK_EXT_TOOL_GET_CLASS( self )->open( self, vwindow ); +} diff --git a/src/vikexttool.h b/src/vikexttool.h new file mode 100644 index 00000000..7055bc77 --- /dev/null +++ b/src/vikexttool.h @@ -0,0 +1,55 @@ +/* + * viking -- GPS Data and Topo Analyzer, Explorer, and Manager + * + * Copyright (C) 2008, Guilhem Bonnefille + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + */ +#ifndef _VIKING_EXT_TOOL_H +#define _VIKING_EXT_TOOL_H + +#include + +#include "vikwindow.h" + +#define VIK_EXT_TOOL_TYPE (vik_ext_tool_get_type ()) +#define VIK_EXT_TOOL(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), VIK_EXT_TOOL_TYPE, VikExtTool)) +#define VIK_EXT_TOOL_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), VIK_EXT_TOOL_TYPE, VikExtToolClass)) +#define IS_VIK_EXT_TOOL(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), VIK_EXT_TOOL_TYPE)) +#define IS_VIK_EXT_TOOL_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), VIK_EXT_TOOL_TYPE)) +#define VIK_EXT_TOOL_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), VIK_EXT_TOOL_TYPE, VikExtToolClass)) + + +typedef struct _VikExtTool VikExtTool; +typedef struct _VikExtToolClass VikExtToolClass; + +struct _VikExtToolClass +{ + GObjectClass object_class; + gchar *(* get_label) (VikExtTool *self); + void (* open) (VikExtTool *self, VikWindow *vwindow); +}; + +GType vik_ext_tool_get_type (); + +struct _VikExtTool { + GObject obj; +}; + +gchar *vik_ext_tool_get_label ( VikExtTool *self ); +void vik_ext_tool_open ( VikExtTool *self, VikWindow *vwindow ); + +#endif diff --git a/src/vikexttools.c b/src/vikexttools.c new file mode 100644 index 00000000..3bc7d929 --- /dev/null +++ b/src/vikexttools.c @@ -0,0 +1,86 @@ +/* + * viking -- GPS Data and Topo Analyzer, Explorer, and Manager + * + * Copyright (C) 2008, Guilhem Bonnefille + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include "vikexttools.h" + +#include + +#include + +#define VIK_TOOL_DATA_KEY "vik-tool-data" + +static GList *ext_tools_list = NULL; + +void vik_ext_tools_register ( VikExtTool *tool ) +{ + IS_VIK_EXT_TOOL( tool ); + + ext_tools_list = g_list_append ( ext_tools_list, g_object_ref ( tool ) ); +} + +void vik_ext_tools_unregister_all () +{ + g_list_foreach ( ext_tools_list, (GFunc) g_object_unref, NULL ); +} + +static void ext_tools_open_cb ( GtkWidget *widget, VikWindow *vwindow ) +{ + gpointer ptr = g_object_get_data ( G_OBJECT(widget), VIK_TOOL_DATA_KEY ); + VikExtTool *ext_tool = VIK_EXT_TOOL ( ptr ); + vik_ext_tool_open ( ext_tool, vwindow ); +} + +void vik_ext_tools_add_menu_items_to_menu ( VikWindow *vwindow, GtkMenu *menu ) +{ + GList *iter; + for (iter = ext_tools_list; iter; iter = iter->next) + { + VikExtTool *ext_tool = NULL; + gchar *label = NULL; + ext_tool = VIK_EXT_TOOL ( iter->data ); + label = vik_ext_tool_get_label ( ext_tool ); + if ( label ) + { + GtkWidget *item = NULL; + item = gtk_menu_item_new_with_label ( _(label) ); + g_free ( label ); label = NULL; + // Store tool's ref into the menu entry + g_object_set_data ( G_OBJECT(item), VIK_TOOL_DATA_KEY, ext_tool ); + g_signal_connect ( G_OBJECT(item), "activate", G_CALLBACK(ext_tools_open_cb), vwindow ); + gtk_menu_shell_append (GTK_MENU_SHELL (menu), item); + gtk_widget_show ( item ); + } + } +} + +void vik_ext_tools_add_menu_items ( VikWindow *vwindow, GtkUIManager *uim ) +{ + GtkWidget *widget = NULL; + GtkMenu *menu = NULL; + widget = gtk_ui_manager_get_widget (uim, "/MainMenu/Tools/Exttools"); + menu = GTK_MENU ( gtk_menu_item_get_submenu ( GTK_MENU_ITEM ( widget ) ) ); + vik_ext_tools_add_menu_items_to_menu ( vwindow, menu ); + gtk_widget_show ( widget ); +} diff --git a/src/vikexttools.h b/src/vikexttools.h new file mode 100644 index 00000000..d85d2956 --- /dev/null +++ b/src/vikexttools.h @@ -0,0 +1,34 @@ +/* + * viking -- GPS Data and Topo Analyzer, Explorer, and Manager + * + * Copyright (C) 2008, Guilhem Bonnefille + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + */ +#ifndef _VIKING_EXT_TOOLS_H +#define _VIKING_EXT_TOOLS_H + +#include + +#include "vikwindow.h" + +#include "vikexttool.h" + +void vik_ext_tools_register ( VikExtTool *tool ); +void vik_ext_tools_unregister_all (); +void vik_ext_tools_add_menu_items ( VikWindow *vwindow, GtkUIManager *uim ); + +#endif diff --git a/src/vikwebtool.c b/src/vikwebtool.c new file mode 100644 index 00000000..706313a5 --- /dev/null +++ b/src/vikwebtool.c @@ -0,0 +1,108 @@ +/* + * viking -- GPS Data and Topo Analyzer, Explorer, and Manager + * + * Copyright (C) 2008, Guilhem Bonnefille + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include "vikwebtool.h" + +#include + +#include + +#include "util.h" + +static void webtool_class_init ( VikWebtoolClass *klass ); +static void webtool_init ( VikWebtool *vwd ); + +static GObjectClass *parent_class; + +static void webtool_finalize ( GObject *gob ); + +static void webtool_open ( VikExtTool *self, VikWindow *vwindow ); + +GType vik_webtool_get_type() +{ + static GType w_type = 0; + + if (!w_type) + { + static const GTypeInfo w_info = + { + sizeof (VikWebtoolClass), + NULL, /* base_init */ + NULL, /* base_finalize */ + (GClassInitFunc) webtool_class_init, + NULL, /* class_finalize */ + NULL, /* class_data */ + sizeof (VikWebtool), + 0, + (GInstanceInitFunc) webtool_init, + }; + w_type = g_type_register_static ( VIK_EXT_TOOL_TYPE, "VikWebtool", &w_info, G_TYPE_FLAG_ABSTRACT ); + } + + return w_type; +} + +static void webtool_class_init ( VikWebtoolClass *klass ) +{ + GObjectClass *object_class; + VikExtToolClass *ext_tool_class; + + object_class = G_OBJECT_CLASS (klass); + + object_class->finalize = webtool_finalize; + + parent_class = g_type_class_peek_parent (klass); + + ext_tool_class = VIK_EXT_TOOL_CLASS ( klass ); + ext_tool_class->open = webtool_open; +} + +VikWebtool *vik_webtool_new () +{ + return VIK_WEBTOOL ( g_object_new ( VIK_WEBTOOL_TYPE, NULL ) ); +} + +static void webtool_init ( VikWebtool *vlp ) +{ +} + +static void webtool_finalize ( GObject *gob ) +{ + // VikWebtool *w = VIK_WEBTOOL ( gob ); + G_OBJECT_CLASS(parent_class)->finalize(gob); +} + +static void webtool_open ( VikExtTool *self, VikWindow *vwindow ) +{ + VikWebtool *vwd = VIK_WEBTOOL ( self ); + gchar *url = vik_webtool_get_url ( vwd, vwindow ); + open_url ( NULL, url ); + g_free ( url ); +} + +gchar *vik_webtool_get_url ( VikWebtool *self, VikWindow *vwindow ) +{ + return VIK_WEBTOOL_GET_CLASS( self )->get_url( self, vwindow ); +} diff --git a/src/vikwebtool.h b/src/vikwebtool.h new file mode 100644 index 00000000..60cb9928 --- /dev/null +++ b/src/vikwebtool.h @@ -0,0 +1,55 @@ +/* + * viking -- GPS Data and Topo Analyzer, Explorer, and Manager + * + * Copyright (C) 2008, Guilhem Bonnefille + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + */ +#ifndef _VIKING_WEBTOOL_H +#define _VIKING_WEBTOOL_H + +#include + +#include "vikwindow.h" + +#include "vikexttool.h" + +#define VIK_WEBTOOL_TYPE (vik_webtool_get_type ()) +#define VIK_WEBTOOL(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), VIK_WEBTOOL_TYPE, VikWebtool)) +#define VIK_WEBTOOL_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), VIK_WEBTOOL_TYPE, VikWebtoolClass)) +#define IS_VIK_WEBTOOL(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), VIK_WEBTOOL_TYPE)) +#define IS_VIK_WEBTOOL_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), VIK_WEBTOOL_TYPE)) +#define VIK_WEBTOOL_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), VIK_WEBTOOL_TYPE, VikWebtoolClass)) + + +typedef struct _VikWebtool VikWebtool; +typedef struct _VikWebtoolClass VikWebtoolClass; + +struct _VikWebtoolClass +{ + VikExtToolClass object_class; + gchar *(* get_url) (VikWebtool *self, VikWindow *vwindow); +}; + +GType vik_webtool_get_type (); + +struct _VikWebtool { + VikExtTool obj; +}; + +gchar *vik_webtool_get_url ( VikWebtool *self, VikWindow *vwindow ); + +#endif diff --git a/src/vikwebtoolcenter.c b/src/vikwebtoolcenter.c new file mode 100644 index 00000000..eed97c1c --- /dev/null +++ b/src/vikwebtoolcenter.c @@ -0,0 +1,244 @@ +/* + * viking -- GPS Data and Topo Analyzer, Explorer, and Manager + * + * Copyright (C) 2008, Guilhem Bonnefille + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include "vikwebtoolcenter.h" + +#include + +#include +#include + +#include "util.h" +#include "globals.h" + +static void webtool_center_class_init ( VikWebtoolCenterClass *klass ); +static void webtool_center_init ( VikWebtoolCenter *vwd ); + +static GObjectClass *parent_class; + +static void webtool_center_finalize ( GObject *gob ); + +static guint8 webtool_center_mpp_to_zoom ( VikWebtool *self, gdouble mpp ); +static gchar *webtool_center_get_url ( VikWebtool *vw, VikWindow *vwindow ); + +typedef struct _VikWebtoolCenterPrivate VikWebtoolCenterPrivate; + +struct _VikWebtoolCenterPrivate +{ + gchar *url; +}; + +#define WEBTOOL_CENTER_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), \ + VIK_WEBTOOL_CENTER_TYPE, \ + VikWebtoolCenterPrivate)) + +GType vik_webtool_center_get_type() +{ + static GType w_type = 0; + + if (!w_type) + { + static const GTypeInfo w_info = + { + sizeof (VikWebtoolCenterClass), + NULL, /* base_init */ + NULL, /* base_finalize */ + (GClassInitFunc) webtool_center_class_init, + NULL, /* class_finalize */ + NULL, /* class_data */ + sizeof (VikWebtoolCenter), + 0, + (GInstanceInitFunc) webtool_center_init, + }; + w_type = g_type_register_static ( VIK_WEBTOOL_TYPE, "VikWebtoolCenter", &w_info, 0 ); + } + + return w_type; +} + +enum +{ + PROP_0, + + PROP_URL, +}; + +static void +webtool_center_set_property (GObject *object, + guint property_id, + const GValue *value, + GParamSpec *pspec) +{ + VikWebtoolCenter *self = VIK_WEBTOOL_CENTER (object); + VikWebtoolCenterPrivate *priv = WEBTOOL_CENTER_GET_PRIVATE (self); + + switch (property_id) + { + case PROP_URL: + g_free (priv->url); + priv->url = g_value_dup_string (value); + g_debug ("VikWebtoolCenter.url: %s", priv->url); + break; + + default: + /* We don't have any other property... */ + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec); + break; + } +} + +static void +webtool_center_get_property (GObject *object, + guint property_id, + GValue *value, + GParamSpec *pspec) +{ + VikWebtoolCenter *self = VIK_WEBTOOL_CENTER (object); + VikWebtoolCenterPrivate *priv = WEBTOOL_CENTER_GET_PRIVATE (self); + + switch (property_id) + { + case PROP_URL: + g_value_set_string (value, priv->url); + break; + + default: + /* We don't have any other property... */ + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec); + break; + } +} + +static void webtool_center_class_init ( VikWebtoolCenterClass *klass ) +{ + GObjectClass *gobject_class; + VikWebtoolClass *base_class; + GParamSpec *pspec; + + gobject_class = G_OBJECT_CLASS (klass); + + gobject_class->finalize = webtool_center_finalize; + gobject_class->set_property = webtool_center_set_property; + gobject_class->get_property = webtool_center_get_property; + + pspec = g_param_spec_string ("url", + "Template Url", + "Set the template url", + VIKING_URL /* default value */, + G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE); + g_object_class_install_property (gobject_class, + PROP_URL, + pspec); + + parent_class = g_type_class_peek_parent (klass); + + base_class = VIK_WEBTOOL_CLASS ( klass ); + base_class->get_url = webtool_center_get_url; + + klass->mpp_to_zoom = webtool_center_mpp_to_zoom; + + g_type_class_add_private (klass, sizeof (VikWebtoolCenterPrivate)); +} + +VikWebtoolCenter *vik_webtool_center_new () +{ + return VIK_WEBTOOL_CENTER ( g_object_new ( VIK_WEBTOOL_CENTER_TYPE, NULL ) ); +} + +VikWebtoolCenter *vik_webtool_center_new_with_members ( const gchar *label, const gchar *url ) +{ + VikWebtoolCenter *result = VIK_WEBTOOL_CENTER ( g_object_new ( VIK_WEBTOOL_CENTER_TYPE, + "label", label, + "url", url, + NULL ) ); + + return result; +} + +static void webtool_center_init ( VikWebtoolCenter *self ) +{ + VikWebtoolCenterPrivate *priv = WEBTOOL_CENTER_GET_PRIVATE (self); + priv->url = NULL; +} + +static void webtool_center_finalize ( GObject *gob ) +{ + VikWebtoolCenterPrivate *priv = WEBTOOL_CENTER_GET_PRIVATE ( gob ); + g_free ( priv->url ); priv->url = NULL; + G_OBJECT_CLASS(parent_class)->finalize(gob); +} + +/* 1 << (x) is like a 2**(x) */ +#define GZ(x) (1<<(x)) + +static const gdouble scale_mpps[] = { GZ(0), GZ(1), GZ(2), GZ(3), GZ(4), GZ(5), GZ(6), GZ(7), GZ(8), GZ(9), + GZ(10), GZ(11), GZ(12), GZ(13), GZ(14), GZ(15), GZ(16), GZ(17) }; + +static const gint num_scales = (sizeof(scale_mpps) / sizeof(scale_mpps[0])); + +#define ERROR_MARGIN 0.01 +static guint8 webtool_center_mpp_to_zoom ( VikWebtool *self, gdouble mpp ) { + gint i; + for ( i = 0; i < num_scales; i++ ) { + if ( ABS(scale_mpps[i] - mpp) < ERROR_MARGIN ) { + g_debug ( "webtool_center_mpp_to_zoom: %f -> %d", mpp, i ); + return i; + } + } + return 255; +} + +static gchar *webtool_center_get_url ( VikWebtool *self, VikWindow *vwindow ) +{ + VikWebtoolCenterPrivate *priv = NULL; + VikViewport *viewport = NULL; + const VikCoord *coord = NULL; + guint8 zoom = 0; + struct LatLon ll; + gchar strlat[G_ASCII_DTOSTR_BUF_SIZE], strlon[G_ASCII_DTOSTR_BUF_SIZE]; + + priv = WEBTOOL_CENTER_GET_PRIVATE (self); + viewport = vik_window_viewport ( vwindow ); + + // Coords + coord = vik_viewport_get_center ( viewport ); + vik_coord_to_latlon ( coord, &ll ); + + // zoom + g_assert ( vik_viewport_get_xmpp ( viewport ) == vik_viewport_get_ympp ( viewport ) ); + zoom = vik_webtool_center_mpp_to_zoom ( self, vik_viewport_get_zoom ( viewport ) ); + + // Cannot simply use g_strdup_printf and gdouble due to locale. + // As we compute an URL, we have to think in C locale. + g_ascii_dtostr (strlat, G_ASCII_DTOSTR_BUF_SIZE, ll.lat); + g_ascii_dtostr (strlon, G_ASCII_DTOSTR_BUF_SIZE, ll.lon); + + return g_strdup_printf ( priv->url, strlat, strlon, 17-zoom ); +} + +guint8 vik_webtool_center_mpp_to_zoom (VikWebtool *self, gdouble mpp) +{ + return VIK_WEBTOOL_CENTER_GET_CLASS( self )->mpp_to_zoom( self, mpp ); +} diff --git a/src/vikwebtoolcenter.h b/src/vikwebtoolcenter.h new file mode 100644 index 00000000..da8d40b2 --- /dev/null +++ b/src/vikwebtoolcenter.h @@ -0,0 +1,56 @@ +/* + * viking -- GPS Data and Topo Analyzer, Explorer, and Manager + * + * Copyright (C) 2008, Guilhem Bonnefille + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + */ +#ifndef _VIKING_WEBTOOL_CENTER_H +#define _VIKING_WEBTOOL_CENTER_H + +#include + +#include "vikwebtool.h" + +#define VIK_WEBTOOL_CENTER_TYPE (vik_webtool_center_get_type ()) +#define VIK_WEBTOOL_CENTER(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), VIK_WEBTOOL_CENTER_TYPE, VikWebtoolCenter)) +#define VIK_WEBTOOL_CENTER_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), VIK_WEBTOOL_CENTER_TYPE, VikWebtoolCenterClass)) +#define IS_VIK_WEBTOOL_CENTER(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), VIK_WEBTOOL_CENTER_TYPE)) +#define IS_VIK_WEBTOOL_CENTER_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), VIK_WEBTOOL_CENTER_TYPE)) +#define VIK_WEBTOOL_CENTER_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), VIK_WEBTOOL_CENTER_TYPE, VikWebtoolCenterClass)) + + +typedef struct _VikWebtoolCenter VikWebtoolCenter; +typedef struct _VikWebtoolCenterClass VikWebtoolCenterClass; + +struct _VikWebtoolCenterClass +{ + VikWebtoolClass object_class; + guint8 (* mpp_to_zoom) (VikWebtool *self, gdouble mpp); +}; + +GType vik_webtool_center_get_type (); + +struct _VikWebtoolCenter { + VikWebtool obj; +}; + +guint8 vik_webtool_center_mpp_to_zoom (VikWebtool *self, gdouble mpp); + +VikWebtoolCenter* vik_webtool_center_new ( ); +VikWebtoolCenter* vik_webtool_center_new_with_members ( const gchar *label, const gchar *url ); + +#endif diff --git a/src/vikwindow.c b/src/vikwindow.c index 4644b98e..34b57ad7 100644 --- a/src/vikwindow.c +++ b/src/vikwindow.c @@ -32,6 +32,7 @@ #include "print.h" #include "preferences.h" #include "icons/icons.h" +#include "vikexttools.h" #ifdef HAVE_STDLIB_H #include @@ -294,6 +295,7 @@ static void window_init ( VikWindow *vw ) gtk_toolbar_set_icon_size(GTK_TOOLBAR(gtk_ui_manager_get_widget (vw->uim, "/MainToolbar")), GTK_ICON_SIZE_SMALL_TOOLBAR); gtk_toolbar_set_style (GTK_TOOLBAR(gtk_ui_manager_get_widget (vw->uim, "/MainToolbar")), GTK_TOOLBAR_ICONS); + vik_ext_tools_add_menu_items ( vw, vw->uim ); g_signal_connect (G_OBJECT (vw), "delete_event", G_CALLBACK (delete_event), NULL); @@ -1942,6 +1944,7 @@ static GtkActionEntry entries[] = { { "SetPan", NULL, N_("_Pan"), 0, 0, 0 }, { "Layers", NULL, N_("_Layers"), 0, 0, 0 }, { "Tools", NULL, N_("_Tools"), 0, 0, 0 }, + { "Exttools", NULL, N_("_Webtools"), 0, 0, 0 }, { "Help", NULL, N_("_Help"), 0, 0, 0 }, { "New", GTK_STOCK_NEW, N_("_New"), "N", N_("New file"), (GCallback)newwindow_cb }, -- 2.11.4.GIT