2 * viking -- GPS Data and Topo Analyzer, Explorer, and Manager
4 * Copyright (C) 2008, Guilhem Bonnefille <guilhem.bonnefille@gmail.com>
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.
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.
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
26 #include "vikexttools.h"
30 #include <glib/gi18n.h>
32 #define VIK_TOOL_DATA_KEY "vik-tool-data"
33 #define VIK_TOOL_WIN_KEY "vik-tool-win"
35 static GList *ext_tools_list = NULL;
37 void vik_ext_tools_register ( VikExtTool *tool )
39 if ( IS_VIK_EXT_TOOL( tool ) )
40 ext_tools_list = g_list_append ( ext_tools_list, g_object_ref ( tool ) );
43 void vik_ext_tools_unregister_all ()
45 g_list_foreach ( ext_tools_list, (GFunc) g_object_unref, NULL );
48 static void ext_tools_open_cb ( GtkWidget *widget, VikWindow *vwindow )
50 gpointer ptr = g_object_get_data ( G_OBJECT(widget), VIK_TOOL_DATA_KEY );
51 VikExtTool *ext_tool = VIK_EXT_TOOL ( ptr );
52 vik_ext_tool_open ( ext_tool, vwindow );
55 void vik_ext_tools_add_action_items ( VikWindow *vwindow, GtkUIManager *uim, GtkActionGroup *action_group, guint mid )
58 for (iter = ext_tools_list; iter; iter = iter->next)
60 VikExtTool *ext_tool = NULL;
62 ext_tool = VIK_EXT_TOOL ( iter->data );
63 label = vik_ext_tool_get_label ( ext_tool );
66 gtk_ui_manager_add_ui ( uim, mid, "/ui/MainMenu/Tools/Exttools",
69 GTK_UI_MANAGER_MENUITEM, FALSE );
71 GtkAction *action = gtk_action_new ( label, label, NULL, NULL );
72 g_object_set_data ( G_OBJECT(action), VIK_TOOL_DATA_KEY, ext_tool );
73 g_signal_connect ( G_OBJECT(action), "activate", G_CALLBACK(ext_tools_open_cb), vwindow );
75 gtk_action_group_add_action ( action_group, action );
77 g_object_unref ( action );
79 g_free ( label ); label = NULL;
84 static void ext_tool_open_at_position_cb ( GtkWidget *widget, VikCoord *vc )
86 gpointer ptr = g_object_get_data ( G_OBJECT(widget), VIK_TOOL_DATA_KEY );
87 VikExtTool *ext_tool = VIK_EXT_TOOL ( ptr );
88 gpointer wptr = g_object_get_data ( G_OBJECT(widget), VIK_TOOL_WIN_KEY );
89 VikWindow *vw = VIK_WINDOW ( wptr );
90 vik_ext_tool_open_at_position ( ext_tool, vw, vc );
95 * mostly for allowing to assign for TrackWaypoint layer menus
97 void vik_ext_tools_add_menu_items_to_menu ( VikWindow *vw, GtkMenu *menu, VikCoord *vc )
99 for (GList *iter = ext_tools_list; iter; iter = iter->next) {
100 VikExtTool *ext_tool = NULL;
102 ext_tool = VIK_EXT_TOOL ( iter->data );
103 label = vik_ext_tool_get_label ( ext_tool );
105 GtkWidget *item = NULL;
106 item = gtk_menu_item_new_with_label ( _(label) );
107 g_free ( label ); label = NULL;
108 // Store some data into the menu entry
109 g_object_set_data ( G_OBJECT(item), VIK_TOOL_DATA_KEY, ext_tool );
110 g_object_set_data ( G_OBJECT(item), VIK_TOOL_WIN_KEY, vw );
112 g_signal_connect ( G_OBJECT(item), "activate", G_CALLBACK(ext_tool_open_at_position_cb), vc );
114 g_signal_connect ( G_OBJECT(item), "activate", G_CALLBACK(ext_tools_open_cb), vw );
115 gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
116 gtk_widget_show ( item );