SF.net SVN: geany-plugins:[1957] trunk/geany-plugins/devhelp/src

codebrainz at users.sourceforge.net codebrainz at xxxxx
Mon Mar 7 05:13:28 UTC 2011


Revision: 1957
          http://geany-plugins.svn.sourceforge.net/geany-plugins/?rev=1957&view=rev
Author:   codebrainz
Date:     2011-03-07 05:13:27 +0000 (Mon, 07 Mar 2011)

Log Message:
-----------
Convert DevhelpPlugin to a GObject.

Modified Paths:
--------------
    trunk/geany-plugins/devhelp/src/Makefile.am
    trunk/geany-plugins/devhelp/src/plugin.c
    trunk/geany-plugins/devhelp/src/plugin.h

Added Paths:
-----------
    trunk/geany-plugins/devhelp/src/devhelpplugin.c
    trunk/geany-plugins/devhelp/src/devhelpplugin.h

Removed Paths:
-------------
    trunk/geany-plugins/devhelp/src/dh-plugin.c
    trunk/geany-plugins/devhelp/src/dh-plugin.h

Modified: trunk/geany-plugins/devhelp/src/Makefile.am
===================================================================
--- trunk/geany-plugins/devhelp/src/Makefile.am	2011-03-07 00:55:38 UTC (rev 1956)
+++ trunk/geany-plugins/devhelp/src/Makefile.am	2011-03-07 05:13:27 UTC (rev 1957)
@@ -11,7 +11,7 @@
 
 devhelp_la_SOURCES = \
 	plugin.c \
-	dh-plugin.c \
+	devhelpplugin.c \
 	main-notebook.c
 devhelp_la_CFLAGS = \
 	$(AM_CFLAGS) \

Copied: trunk/geany-plugins/devhelp/src/devhelpplugin.c (from rev 1956, trunk/geany-plugins/devhelp/src/dh-plugin.c)
===================================================================
--- trunk/geany-plugins/devhelp/src/devhelpplugin.c	                        (rev 0)
+++ trunk/geany-plugins/devhelp/src/devhelpplugin.c	2011-03-07 05:13:27 UTC (rev 1957)
@@ -0,0 +1,438 @@
+//      devhelpplugin.c
+//      
+//      Copyright 2011 Matthew Brush <mbrush at desktop>
+//      
+//      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., 51 Franklin Street, Fifth Floor, Boston,
+//      MA 02110-1301, USA.
+//      
+//      
+
+#include <gtk/gtk.h>
+#include <geanyplugin.h>
+
+#include <devhelp/dh-base.h>
+#include <devhelp/dh-book-tree.h>
+#include <devhelp/dh-search.h>
+#include <devhelp/dh-link.h>
+
+#ifdef HAVE_BOOK_MANAGER /* for newer api */
+#include <devhelp/dh-book-manager.h>
+#endif
+
+#include <webkit/webkitwebview.h>
+
+#include "plugin.h"
+#include "devhelpplugin.h"
+#include "main-notebook.h"
+
+
+/* Devhelp base object */
+static DhBase *dhbase = NULL; 
+
+struct _DevhelpPluginPrivate
+{
+	/* add your private declarations here */
+	gint tmp;
+};
+
+static void devhelp_plugin_finalize			(GObject *object);
+
+G_DEFINE_TYPE(DevhelpPlugin, devhelp_plugin, G_TYPE_OBJECT)
+
+
+static void devhelp_plugin_class_init(DevhelpPluginClass *klass)
+{
+	GObjectClass *g_object_class;
+
+	g_object_class = G_OBJECT_CLASS(klass);
+
+	g_object_class->finalize = devhelp_plugin_finalize;
+
+	g_type_class_add_private((gpointer)klass, sizeof(DevhelpPluginPrivate));
+}
+
+
+static void devhelp_plugin_finalize(GObject *object)
+{
+	DevhelpPlugin *self;
+
+	g_return_if_fail(object != NULL);
+	g_return_if_fail(DEVHELP_IS_PLUGIN(object));
+
+	self = DEVHELP_PLUGIN(object);
+
+	gtk_widget_destroy(self->sb_notebook);
+	
+	gtk_notebook_remove_page(GTK_NOTEBOOK(self->main_notebook),
+								self->webview_tab);
+								
+	if (!self->in_message_window)
+		main_notebook_destroy();
+   
+	gtk_widget_destroy(self->editor_menu_sep);
+	gtk_widget_destroy(self->editor_menu_item);
+   
+	gtk_notebook_set_tab_pos(GTK_NOTEBOOK(
+								geany->main_widgets->sidebar_notebook), 
+								self->orig_sb_tab_pos);
+
+	G_OBJECT_CLASS(devhelp_plugin_parent_class)->finalize(object);
+}
+
+
+static void devhelp_plugin_init(DevhelpPlugin *self)
+{
+	self->priv = G_TYPE_INSTANCE_GET_PRIVATE(self,
+		DEVHELP_TYPE_PLUGIN, DevhelpPluginPrivate);
+	
+}
+
+/* Called when the editor menu item is selected */
+static void on_search_help_activate(GtkMenuItem *menuitem, gpointer user_data)
+{
+	DevhelpPlugin *dhplug = user_data;
+	gchar *current_tag = devhelp_plugin_get_current_tag();
+	
+	if (current_tag == NULL)
+		return;
+	
+	dh_search_set_search_string (DH_SEARCH(dhplug->search), current_tag, NULL);
+	
+	/* activate devhelp tabs with search tab active */
+	devhelp_plugin_activate_tabs(dhplug, FALSE);
+	
+	g_free(current_tag);
+}
+
+/* 
+ * Called when the editor context menu is shown so that the devhelp
+ * search item can be disabled if there isn't a selected tag.
+ */
+static void on_editor_menu_popup(GtkWidget *widget, gpointer user_data)
+{
+	gchar *curword = NULL;
+	gchar *new_label = NULL;
+	DevhelpPlugin *dhplug = user_data;
+	
+	curword = devhelp_plugin_get_current_tag();
+	if (curword == NULL)
+		gtk_widget_set_sensitive(dhplug->editor_menu_item, FALSE);
+	else {
+		gtk_widget_set_sensitive(dhplug->editor_menu_item, TRUE);
+		new_label = g_strdup_printf("Search Devhelp for '%s'", curword);
+		gtk_menu_item_set_label(GTK_MENU_ITEM(dhplug->editor_menu_item),
+								new_label);
+		g_free(new_label);
+	}
+	
+	g_free(curword);	
+}
+
+/**
+ * on_link_clicked:
+ * @param ignored		Not used
+ * @param link	  		The devhelp link object describing what was clicked.
+ * @param user_data 	The current DevhelpPlugin struct.
+ * 
+ * Called when a link in either the contents or search areas on the sidebar 
+ * have a link clicked on, meaning to load that file into the webview.
+ */
+static void on_link_clicked(GObject *ignored, DhLink *link, gpointer user_data)
+{
+	gchar *uri = dh_link_get_uri(link);
+	DevhelpPlugin *plug = user_data;
+	webkit_web_view_open(WEBKIT_WEB_VIEW(plug->webview), uri);
+	g_free(uri);
+	gtk_notebook_set_current_page(GTK_NOTEBOOK(plug->main_notebook), 
+									plug->webview_tab);
+}
+
+
+/**
+ * devhelp_plugin_new:
+ * 
+ * Creates a new DevhelpPlugin.  The returned structure is allocated dyamically
+ * and must be freed with the devhelp_plugin_destroy() function.  This function
+ * gets called from Geany's plugin_init() function.
+ * 
+ * @return A newly allocated DevhelpPlugin struct or null on error.
+ */
+DevhelpPlugin *devhelp_plugin_new(gboolean sb_tabs_bottom, gboolean show_in_msgwin)
+{
+	gchar *homepage_uri;
+	GtkWidget *book_tree_sw, *webview_sw, *contents_label;
+	GtkWidget *search_label, *dh_sidebar_label, *doc_label;
+	DevhelpPlugin *dhplug;
+
+	dhplug = g_object_new(DEVHELP_TYPE_PLUGIN, NULL);
+	
+	if (dhplug == NULL) {
+		g_printerr(_("Cannot create a new Devhelp plugin, out of memory.\n"));
+		return NULL;
+	}
+	
+#ifdef HAVE_BOOK_MANAGER /* for newer api */
+	DhBookManager *book_manager;
+#else
+	GNode *books;
+	GList *keywords;
+#endif
+	
+	if (dhbase == NULL)
+		dhbase = dh_base_new();	
+		
+#ifdef HAVE_BOOK_MANAGER /* for newer api */
+	book_manager = dh_base_get_book_manager(dhbase);
+	dhplug->book_tree = dh_book_tree_new(book_manager);
+	dhplug->search = dh_search_new(book_manager);
+#else	
+	books = dh_base_get_book_tree(dhbase);
+	keywords = dh_base_get_keywords(dhbase);
+	dhplug->book_tree = dh_book_tree_new(books);
+	dhplug->search = dh_search_new(keywords);
+#endif
+
+	dhplug->in_message_window = show_in_msgwin;
+	
+	/* create/grab notebooks */
+	dhplug->sb_notebook = gtk_notebook_new();
+	dhplug->doc_notebook = geany->main_widgets->notebook;
+	
+	if (dhplug->in_message_window)
+		dhplug->main_notebook = geany->main_widgets->message_window_notebook;
+	else
+		dhplug->main_notebook = main_notebook_get();
+	
+	/* editor menu items */
+	dhplug->editor_menu_sep = gtk_separator_menu_item_new();
+	dhplug->editor_menu_item = gtk_menu_item_new_with_label(
+									_("Search Documentation for Tag"));
+	   
+	/* tab labels */
+	contents_label = gtk_label_new(_("Contents"));
+	search_label = gtk_label_new(_("Search"));
+	dh_sidebar_label = gtk_label_new(_("Devhelp"));
+	doc_label = gtk_label_new(_("Documentation"));	
+	
+	dhplug->orig_sb_tab_pos = gtk_notebook_get_tab_pos(GTK_NOTEBOOK(
+									geany->main_widgets->sidebar_notebook));
+	devhelp_plugin_sidebar_tabs_bottom(dhplug, sb_tabs_bottom);
+
+	/* sidebar contents/book tree */
+	book_tree_sw = gtk_scrolled_window_new(NULL, NULL);
+	gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(book_tree_sw),
+		GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
+	gtk_container_set_border_width(GTK_CONTAINER(book_tree_sw), 6);
+	gtk_container_add(GTK_CONTAINER(book_tree_sw), dhplug->book_tree); 
+	gtk_widget_show(dhplug->book_tree);
+	
+	/* sidebar search */
+	gtk_widget_show(dhplug->search);
+	
+	/* webview to display documentation */
+	dhplug->webview = webkit_web_view_new();
+	webview_sw = gtk_scrolled_window_new(NULL, NULL);
+	gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(webview_sw),
+		GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
+	/*gtk_container_set_border_width(GTK_CONTAINER(webview_sw), 6);*/
+	gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(webview_sw), 
+		GTK_SHADOW_ETCHED_IN);
+	gtk_container_add(GTK_CONTAINER(webview_sw), dhplug->webview);
+	gtk_widget_show_all(webview_sw);
+	
+	/* setup the sidebar notebook */
+	gtk_notebook_append_page(GTK_NOTEBOOK(dhplug->sb_notebook),
+		book_tree_sw, contents_label);
+	gtk_notebook_append_page(GTK_NOTEBOOK(dhplug->sb_notebook),
+		dhplug->search, search_label);
+	gtk_notebook_set_current_page(GTK_NOTEBOOK(dhplug->sb_notebook), 0);
+		
+	gtk_widget_show_all(dhplug->sb_notebook);
+	gtk_notebook_append_page(
+		GTK_NOTEBOOK(geany->main_widgets->sidebar_notebook),
+		dhplug->sb_notebook, dh_sidebar_label);
+	dhplug->sb_notebook_tab = gtk_notebook_page_num(
+		GTK_NOTEBOOK(geany->main_widgets->sidebar_notebook),
+		dhplug->sb_notebook);
+
+	/* put the webview stuff into the main notebook */
+	gtk_notebook_append_page(GTK_NOTEBOOK(dhplug->main_notebook),
+		webview_sw, doc_label);
+	dhplug->webview_tab = gtk_notebook_page_num(
+							GTK_NOTEBOOK(dhplug->main_notebook), webview_sw);
+	
+	/* add menu item to editor popup menu */
+	/* todo: make this an image menu item with devhelp icon */
+	gtk_menu_shell_append(GTK_MENU_SHELL(geany->main_widgets->editor_menu),
+		dhplug->editor_menu_sep);
+	gtk_menu_shell_append(GTK_MENU_SHELL(geany->main_widgets->editor_menu),
+		dhplug->editor_menu_item);
+	gtk_widget_show(dhplug->editor_menu_sep);
+	gtk_widget_show(dhplug->editor_menu_item);
+
+	/* connect signals */
+	g_signal_connect(
+			geany->main_widgets->editor_menu, 
+			"show",
+			G_CALLBACK(on_editor_menu_popup), 
+			dhplug);
+										
+	g_signal_connect(
+			dhplug->editor_menu_item, 
+			"activate",
+			G_CALLBACK(on_search_help_activate), 
+			dhplug);
+										
+	g_signal_connect(
+			dhplug->book_tree, 
+			"link-selected", 
+			G_CALLBACK(on_link_clicked), 
+			dhplug);
+										
+	g_signal_connect(
+			dhplug->search, 
+			"link-selected",
+			G_CALLBACK(on_link_clicked), 
+			dhplug);	
+
+	/* toggle state tracking */
+	dhplug->last_main_tab_id = gtk_notebook_get_current_page(
+									GTK_NOTEBOOK(dhplug->main_notebook));
+	dhplug->last_sb_tab_id = gtk_notebook_get_current_page(GTK_NOTEBOOK(
+									geany->main_widgets->sidebar_notebook));
+	dhplug->tabs_toggled = FALSE;
+	
+	/* load the default homepage for the webview */
+	homepage_uri = g_filename_to_uri(DHPLUG_WEBVIEW_HOME_FILE, NULL, NULL);
+	if (homepage_uri) {
+		webkit_web_view_load_uri(WEBKIT_WEB_VIEW(dhplug->webview), 
+								 homepage_uri);
+		g_free(homepage_uri);
+	}
+	
+	return dhplug;
+}
+
+/** 
+ * devhelp_plugin_clean_word:
+ * @param	str	String to clean
+ * 
+ * Replaces non GEANY_WORDCHARS in str with spaces and then trims whitespace.
+ * This function does not allocate a new string, it modifies str in place
+ * and returns a pointer to str. 
+ * TODO: make this only remove stuff from the start or end of string.
+ * 
+ * @return Pointer to (cleaned) @str.
+ */
+gchar *devhelp_plugin_clean_word(gchar *str)
+{
+	return g_strstrip(g_strcanon(str, GEANY_WORDCHARS, ' '));
+}
+
+/**
+ * devhelp_plugin_get_current_tag:
+ * 
+ * Gets either the current selection or the word at the current selection.
+ * 
+ * @return Newly allocated string with current tag or NULL no tag.
+ */
+gchar *devhelp_plugin_get_current_tag(void)
+{
+	gint pos;
+	gchar *tag = NULL;
+	GeanyDocument *doc = document_get_current();
+	
+	if (sci_has_selection(doc->editor->sci))
+		return devhelp_plugin_clean_word(sci_get_selection_contents(doc->editor->sci));
+	
+	pos = sci_get_current_position(doc->editor->sci);
+	tag = editor_get_word_at_pos(doc->editor, pos, GEANY_WORDCHARS);
+	
+	if (tag == NULL) 
+		return NULL;
+	
+	if (tag[0] == '\0') {
+		g_free(tag);
+		return NULL;
+	}
+	
+	return devhelp_plugin_clean_word(tag);
+}
+
+
+/**
+ * devhelp_plugin_activate_tabs:
+ * @param	dhplug		The current DevhelpPlugin struct.
+ * @param	contents	If TRUE then select the devhelp Contents tab
+ *						otherwise select the devhelp Search tab.
+ * 
+ * Toggles devhelp related tabs to be current tabs or back to the
+ * previously selected tabs.
+ */ 
+void devhelp_plugin_activate_tabs(DevhelpPlugin *dhplug, gboolean contents)
+{
+	if (!dhplug->tabs_toggled)
+	{
+		/* toggle state tracking */
+		dhplug->last_main_tab_id = gtk_notebook_get_current_page(
+										GTK_NOTEBOOK(dhplug->main_notebook));
+		dhplug->last_sb_tab_id = gtk_notebook_get_current_page(GTK_NOTEBOOK(
+										geany->main_widgets->sidebar_notebook));
+		dhplug->tabs_toggled = TRUE;
+		
+		gtk_notebook_set_current_page(GTK_NOTEBOOK(
+										geany->main_widgets->sidebar_notebook), 
+			dhplug->sb_notebook_tab);
+		gtk_notebook_set_current_page(
+			GTK_NOTEBOOK(dhplug->main_notebook), dhplug->webview_tab);
+		if (contents)
+			gtk_notebook_set_current_page(GTK_NOTEBOOK(dhplug->sb_notebook), 0);
+		else
+			gtk_notebook_set_current_page(GTK_NOTEBOOK(dhplug->sb_notebook), 1);
+	}
+	else
+	{
+		gtk_notebook_set_current_page(GTK_NOTEBOOK(dhplug->main_notebook), 
+										dhplug->last_main_tab_id);
+		gtk_notebook_set_current_page(GTK_NOTEBOOK(
+										geany->main_widgets->sidebar_notebook),
+										dhplug->last_sb_tab_id);
+		dhplug->tabs_toggled = FALSE;
+	}
+}
+
+/**
+ * devhelp_plugin_sidebar_tabs_bottom:
+ * @param dhplug	The current DevhelpPlugin struct.
+ * @param bottom	Whether to move the sidebar tabs to the bottom or not.
+ * 
+ * Changes the sidebar tab position from its default position to the bottom or
+ * vice versa.
+ */
+void devhelp_plugin_sidebar_tabs_bottom(DevhelpPlugin *dhplug, gboolean bottom)
+{
+	if (bottom)
+		gtk_notebook_set_tab_pos(GTK_NOTEBOOK(geany->main_widgets->sidebar_notebook), 
+								 GTK_POS_BOTTOM);
+	else
+		gtk_notebook_set_tab_pos(GTK_NOTEBOOK(geany->main_widgets->sidebar_notebook),
+								 dhplug->orig_sb_tab_pos);
+}
+
+
+
+
+
+

Added: trunk/geany-plugins/devhelp/src/devhelpplugin.h
===================================================================
--- trunk/geany-plugins/devhelp/src/devhelpplugin.h	                        (rev 0)
+++ trunk/geany-plugins/devhelp/src/devhelpplugin.h	2011-03-07 05:13:27 UTC (rev 1957)
@@ -0,0 +1,98 @@
+//      devhelpplugin.h
+//      
+//      Copyright 2011 Matthew Brush <mbrush at desktop>
+//      
+//      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., 51 Franklin Street, Fifth Floor, Boston,
+//      MA 02110-1301, USA.
+//      
+//      
+
+
+#ifndef __DEVHELPPLUGIN_H__
+#define __DEVHELPPLUGIN_H__
+
+#include <gtk/gtk.h>
+
+G_BEGIN_DECLS
+
+#ifndef DHPLUG_DATA_DIR
+#define DHPLUG_DATA_DIR "/usr/local/share/geany-devhelp"
+#endif
+
+#define DHPLUG_WEBVIEW_HOME_FILE DHPLUG_DATA_DIR"/home.html"
+
+#define DEVHELP_TYPE_PLUGIN				(devhelp_plugin_get_type())
+#define DEVHELP_PLUGIN(obj)				(G_TYPE_CHECK_INSTANCE_CAST((obj),\
+			DEVHELP_TYPE_PLUGIN, DevhelpPlugin))
+#define DEVHELP_PLUGIN_CLASS(klass)		(G_TYPE_CHECK_CLASS_CAST((klass),\
+			DEVHELP_TYPE_PLUGIN, DevhelpPluginClass))
+#define DEVHELP_IS_PLUGIN(obj)			(G_TYPE_CHECK_INSTANCE_TYPE((obj),\
+			DEVHELP_TYPE_PLUGIN))
+#define DEVHELP_IS_PLUGIN_CLASS(klass)	(G_TYPE_CHECK_CLASS_TYPE((klass),\
+			DEVHELP_TYPE_PLUGIN))
+#define DEVHELP_PLUGIN_GET_CLASS(obj)	(G_TYPE_INSTANCE_GET_CLASS((obj),\
+			DEVHELP_TYPE_PLUGIN, DevhelpPluginClass))
+
+typedef struct _DevhelpPlugin			DevhelpPlugin;
+typedef struct _DevhelpPluginClass		DevhelpPluginClass;
+typedef struct _DevhelpPluginPrivate		DevhelpPluginPrivate;
+
+struct _DevhelpPlugin
+{
+	GObject parent;
+
+	GtkWidget *book_tree;			/// "Contents" in the sidebar
+	GtkWidget *search;				/// "Search" in the sidebar
+	GtkWidget *sb_notebook;			/// Notebook that holds contents/search
+	gint sb_notebook_tab;			/// Index of tab where devhelp sidebar is
+	GtkWidget *webview;				/// Webkit that shows documentation
+	gint webview_tab;				/// Index of tab that contains the webview
+	GtkWidget *main_notebook;		/// Notebook that holds Geany doc notebook and
+									/// and webkit view
+	GtkWidget *doc_notebook;		/// Geany's document notebook  
+	GtkWidget *editor_menu_item;	/// Item in the editor's context menu 
+	GtkWidget *editor_menu_sep;		/// Separator item above menu item
+	gboolean *webview_active;		/// Tracks whether webview stuff is shown
+	
+	gboolean last_main_tab_id;		/// These track the last id of the tabs
+	gboolean last_sb_tab_id;		///   before toggling
+	gboolean tabs_toggled;			/// Tracks state of whether to toggle to
+									/// Devhelp or back to code
+	gboolean created_main_nb;		/// Track whether we created the main notebook
+	
+	GtkPositionType orig_sb_tab_pos;
+	gboolean sidebar_tab_bottom;
+	gboolean in_message_window;
+	
+	DevhelpPluginPrivate *priv;
+};
+
+struct _DevhelpPluginClass
+{
+	GObjectClass parent_class;
+};
+
+
+GType devhelp_plugin_get_type (void);
+DevhelpPlugin* devhelp_plugin_new (gboolean sb_tabs_bottom, gboolean show_in_msgwin);
+
+gchar *devhelp_plugin_clean_word(gchar *str);
+gchar *devhelp_plugin_get_current_tag(void);
+void devhelp_plugin_activate_tabs(DevhelpPlugin *dhplug, gboolean contents);
+void devhelp_plugin_sidebar_tabs_bottom(DevhelpPlugin *dhplug, gboolean bottom);
+
+G_END_DECLS
+
+#endif /* __DEVHELPPLUGIN_H__ */

Deleted: trunk/geany-plugins/devhelp/src/dh-plugin.c
===================================================================
--- trunk/geany-plugins/devhelp/src/dh-plugin.c	2011-03-07 00:55:38 UTC (rev 1956)
+++ trunk/geany-plugins/devhelp/src/dh-plugin.c	2011-03-07 05:13:27 UTC (rev 1957)
@@ -1,389 +0,0 @@
-/*
- * dhplug.c - Part of the Geany Devhelp Plugin
- * 
- * Copyright 2010 Matthew Brush <mbrush at leftclick.ca>
- * 
- * 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 3 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., 51 Franklin Street, Fifth Floor, Boston,
- * MA 02110-1301, USA.
- */
-
-#include <gtk/gtk.h>
-#include <geanyplugin.h>
-
-#include <devhelp/dh-base.h>
-#include <devhelp/dh-book-tree.h>
-#include <devhelp/dh-search.h>
-#include <devhelp/dh-link.h>
-
-#ifdef HAVE_BOOK_MANAGER /* for newer api */
-#include <devhelp/dh-book-manager.h>
-#endif
-
-#include <webkit/webkitwebview.h>
-
-#include "plugin.h"
-#include "dh-plugin.h"
-#include "main-notebook.h"
-
-/* Devhelp base object */
-static DhBase *dhbase = NULL;  
-
-/** 
- * devhelp_clean_word:
- * @param	str	String to clean
- * 
- * Replaces non GEANY_WORDCHARS in str with spaces and then trims whitespace.
- * This function does not allocate a new string, it modifies str in place
- * and returns a pointer to str. 
- * TODO: make this only remove stuff from the start or end of string.
- * 
- * @return Pointer to (cleaned) @str.
- */
-gchar *devhelp_clean_word(gchar *str)
-{
-	return g_strstrip(g_strcanon(str, GEANY_WORDCHARS, ' '));
-}
-
-/**
- * devhelp_get_current_tag:
- * 
- * Gets either the current selection or the word at the current selection.
- * 
- * @return Newly allocated string with current tag or NULL no tag.
- */
-gchar *devhelp_get_current_tag(void)
-{
-	gint pos;
-	gchar *tag = NULL;
-	GeanyDocument *doc = document_get_current();
-	
-	if (sci_has_selection(doc->editor->sci))
-		return devhelp_clean_word(sci_get_selection_contents(doc->editor->sci));
-	
-	pos = sci_get_current_position(doc->editor->sci);
-	tag = editor_get_word_at_pos(doc->editor, pos, GEANY_WORDCHARS);
-	
-	if (tag == NULL) 
-		return NULL;
-	
-	if (tag[0] == '\0') {
-		g_free(tag);
-		return NULL;
-	}
-	
-	return devhelp_clean_word(tag);
-}
-
-/**
- * devhelp_activate_tabs:
- * @param	dhplug		The current DevhelpPlugin struct.
- * @param	contents	If TRUE then select the devhelp Contents tab
- *						otherwise select the devhelp Search tab.
- * 
- * Toggles devhelp related tabs to be current tabs or back to the
- * previously selected tabs.
- */ 
-void devhelp_activate_tabs(DevhelpPlugin *dhplug, gboolean contents)
-{
-	if (!dhplug->tabs_toggled)
-	{
-		/* toggle state tracking */
-		dhplug->last_main_tab_id = gtk_notebook_get_current_page(
-										GTK_NOTEBOOK(dhplug->main_notebook));
-		dhplug->last_sb_tab_id = gtk_notebook_get_current_page(GTK_NOTEBOOK(
-										geany->main_widgets->sidebar_notebook));
-		dhplug->tabs_toggled = TRUE;
-		
-		gtk_notebook_set_current_page(GTK_NOTEBOOK(
-										geany->main_widgets->sidebar_notebook), 
-			dhplug->sb_notebook_tab);
-		gtk_notebook_set_current_page(
-			GTK_NOTEBOOK(dhplug->main_notebook), dhplug->webview_tab);
-		if (contents)
-			gtk_notebook_set_current_page(GTK_NOTEBOOK(dhplug->sb_notebook), 0);
-		else
-			gtk_notebook_set_current_page(GTK_NOTEBOOK(dhplug->sb_notebook), 1);
-	}
-	else
-	{
-		gtk_notebook_set_current_page(GTK_NOTEBOOK(dhplug->main_notebook), 
-										dhplug->last_main_tab_id);
-		gtk_notebook_set_current_page(GTK_NOTEBOOK(
-										geany->main_widgets->sidebar_notebook),
-										dhplug->last_sb_tab_id);
-		dhplug->tabs_toggled = FALSE;
-	}
-}
-
-void devhelp_plugin_sidebar_tabs_bottom(DevhelpPlugin *dhplug, gboolean bottom)
-{
-	if (bottom)
-		gtk_notebook_set_tab_pos(GTK_NOTEBOOK(geany->main_widgets->sidebar_notebook), 
-								 GTK_POS_BOTTOM);
-	else
-		gtk_notebook_set_tab_pos(GTK_NOTEBOOK(geany->main_widgets->sidebar_notebook),
-								 dhplug->orig_sb_tab_pos);
-}
-
-/* Called when the editor menu item is selected */
-static void on_search_help_activate(GtkMenuItem *menuitem, gpointer user_data)
-{
-	DevhelpPlugin *dhplug = user_data;
-	gchar *current_tag = devhelp_get_current_tag();
-	
-	if (current_tag == NULL)
-		return;
-	
-	dh_search_set_search_string (DH_SEARCH(dhplug->search), current_tag, NULL);
-	
-	/* activate devhelp tabs with search tab active */
-	devhelp_activate_tabs(dhplug, FALSE);
-	
-	g_free(current_tag);
-}
-
-/* 
- * Called when the editor context menu is shown so that the devhelp
- * search item can be disabled if there isn't a selected tag.
- */
-static void on_editor_menu_popup(GtkWidget *widget, gpointer user_data)
-{
-	gchar *curword = NULL;
-	gchar *new_label = NULL;
-	DevhelpPlugin *dhplug = user_data;
-	
-	curword = devhelp_get_current_tag();
-	if (curword == NULL)
-		gtk_widget_set_sensitive(dhplug->editor_menu_item, FALSE);
-	else {
-		gtk_widget_set_sensitive(dhplug->editor_menu_item, TRUE);
-		new_label = g_strdup_printf("Search Devhelp for '%s'", curword);
-		gtk_menu_item_set_label(GTK_MENU_ITEM(dhplug->editor_menu_item),
-								new_label);
-		g_free(new_label);
-	}
-	
-	g_free(curword);	
-}
-
-/**
- * on_link_clicked:
- * @param ignored		Not used
- * @param link	  		The devhelp link object describing what was clicked.
- * @param user_data 	The current DevhelpPlugin struct.
- * 
- * Called when a link in either the contents or search areas on the sidebar 
- * have a link clicked on, meaning to load that file into the webview.
- */
-static void on_link_clicked(GObject *ignored, DhLink *link, gpointer user_data)
-{
-	gchar *uri = dh_link_get_uri(link);
-	DevhelpPlugin *plug = user_data;
-	webkit_web_view_open(WEBKIT_WEB_VIEW(plug->webview), uri);
-	g_free(uri);
-	gtk_notebook_set_current_page(GTK_NOTEBOOK(plug->main_notebook), 
-									plug->webview_tab);
-}
-
-/**
- * devhelp_plugin_new:
- * 
- * Creates a new DevhelpPlugin.  The returned structure is allocated dyamically
- * and must be freed with the devhelp_plugin_destroy() function.  This function
- * gets called from Geany's plugin_init() function.
- * 
- * @return A newly allocated DevhelpPlugin struct or null on error.
- */
-DevhelpPlugin *devhelp_plugin_new(gboolean sb_tabs_bottom, gboolean show_in_msgwin)
-{	
-	gchar *homepage_uri;
-	GtkWidget *book_tree_sw, *webview_sw, *contents_label;
-	GtkWidget *search_label, *dh_sidebar_label, *doc_label;
-
-#ifdef HAVE_BOOK_MANAGER /* for newer api */
-	DhBookManager *book_manager;
-#else
-	GNode *books;
-	GList *keywords;
-#endif
-
-	DevhelpPlugin *dhplug = g_malloc0(sizeof(DevhelpPlugin));
-	
-	if (dhplug == NULL) {
-		g_printerr(_("Cannot create a new Devhelp plugin, out of memory.\n"));
-		return NULL;
-	}
-	
-	if (dhbase == NULL)
-		dhbase = dh_base_new();	
-		
-#ifdef HAVE_BOOK_MANAGER /* for newer api */
-	book_manager = dh_base_get_book_manager(dhbase);
-	dhplug->book_tree = dh_book_tree_new(book_manager);
-	dhplug->search = dh_search_new(book_manager);
-#else	
-	books = dh_base_get_book_tree(dhbase);
-	keywords = dh_base_get_keywords(dhbase);
-	dhplug->book_tree = dh_book_tree_new(books);
-	dhplug->search = dh_search_new(keywords);
-#endif
-
-	dhplug->in_message_window = show_in_msgwin;
-	
-	/* create/grab notebooks */
-	dhplug->sb_notebook = gtk_notebook_new();
-	dhplug->doc_notebook = geany->main_widgets->notebook;
-	
-	if (dhplug->in_message_window)
-		dhplug->main_notebook = geany->main_widgets->message_window_notebook;
-	else
-		dhplug->main_notebook = main_notebook_get();
-	
-	/* editor menu items */
-	dhplug->editor_menu_sep = gtk_separator_menu_item_new();
-	dhplug->editor_menu_item = gtk_menu_item_new_with_label(
-									_("Search Documentation for Tag"));
-	   
-	/* tab labels */
-	contents_label = gtk_label_new(_("Contents"));
-	search_label = gtk_label_new(_("Search"));
-	dh_sidebar_label = gtk_label_new(_("Devhelp"));
-	doc_label = gtk_label_new(_("Documentation"));	
-	
-	dhplug->orig_sb_tab_pos = gtk_notebook_get_tab_pos(GTK_NOTEBOOK(
-									geany->main_widgets->sidebar_notebook));
-	devhelp_plugin_sidebar_tabs_bottom(dhplug, sb_tabs_bottom);
-
-	/* sidebar contents/book tree */
-	book_tree_sw = gtk_scrolled_window_new(NULL, NULL);
-	gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(book_tree_sw),
-		GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
-	gtk_container_set_border_width(GTK_CONTAINER(book_tree_sw), 6);
-	gtk_container_add(GTK_CONTAINER(book_tree_sw), dhplug->book_tree); 
-	gtk_widget_show(dhplug->book_tree);
-	
-	/* sidebar search */
-	gtk_widget_show(dhplug->search);
-	
-	/* webview to display documentation */
-	dhplug->webview = webkit_web_view_new();
-	webview_sw = gtk_scrolled_window_new(NULL, NULL);
-	gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(webview_sw),
-		GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
-	/*gtk_container_set_border_width(GTK_CONTAINER(webview_sw), 6);*/
-	gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(webview_sw), 
-		GTK_SHADOW_ETCHED_IN);
-	gtk_container_add(GTK_CONTAINER(webview_sw), dhplug->webview);
-	gtk_widget_show_all(webview_sw);
-	
-	/* setup the sidebar notebook */
-	gtk_notebook_append_page(GTK_NOTEBOOK(dhplug->sb_notebook),
-		book_tree_sw, contents_label);
-	gtk_notebook_append_page(GTK_NOTEBOOK(dhplug->sb_notebook),
-		dhplug->search, search_label);
-	gtk_notebook_set_current_page(GTK_NOTEBOOK(dhplug->sb_notebook), 0);
-		
-	gtk_widget_show_all(dhplug->sb_notebook);
-	gtk_notebook_append_page(
-		GTK_NOTEBOOK(geany->main_widgets->sidebar_notebook),
-		dhplug->sb_notebook, dh_sidebar_label);
-	dhplug->sb_notebook_tab = gtk_notebook_page_num(
-		GTK_NOTEBOOK(geany->main_widgets->sidebar_notebook),
-		dhplug->sb_notebook);
-
-	/* put the webview stuff into the main notebook */
-	gtk_notebook_append_page(GTK_NOTEBOOK(dhplug->main_notebook),
-		webview_sw, doc_label);
-	dhplug->webview_tab = gtk_notebook_page_num(
-							GTK_NOTEBOOK(dhplug->main_notebook), webview_sw);
-	
-	/* add menu item to editor popup menu */
-	/* todo: make this an image menu item with devhelp icon */
-	gtk_menu_shell_append(GTK_MENU_SHELL(geany->main_widgets->editor_menu),
-		dhplug->editor_menu_sep);
-	gtk_menu_shell_append(GTK_MENU_SHELL(geany->main_widgets->editor_menu),
-		dhplug->editor_menu_item);
-	gtk_widget_show(dhplug->editor_menu_sep);
-	gtk_widget_show(dhplug->editor_menu_item);
-
-	/* connect signals */
-	g_signal_connect(
-			geany->main_widgets->editor_menu, 
-			"show",
-			G_CALLBACK(on_editor_menu_popup), 
-			dhplug);
-										
-	g_signal_connect(
-			dhplug->editor_menu_item, 
-			"activate",
-			G_CALLBACK(on_search_help_activate), 
-			dhplug);
-										
-	g_signal_connect(
-			dhplug->book_tree, 
-			"link-selected", 
-			G_CALLBACK(on_link_clicked), 
-			dhplug);
-										
-	g_signal_connect(
-			dhplug->search, 
-			"link-selected",
-			G_CALLBACK(on_link_clicked), 
-			dhplug);	
-
-	/* toggle state tracking */
-	dhplug->last_main_tab_id = gtk_notebook_get_current_page(
-									GTK_NOTEBOOK(dhplug->main_notebook));
-	dhplug->last_sb_tab_id = gtk_notebook_get_current_page(GTK_NOTEBOOK(
-									geany->main_widgets->sidebar_notebook));
-	dhplug->tabs_toggled = FALSE;
-	
-	/* load the default homepage for the webview */
-	homepage_uri = g_filename_to_uri(DHPLUG_WEBVIEW_HOME_FILE, NULL, NULL);
-	if (homepage_uri) {
-		webkit_web_view_load_uri(WEBKIT_WEB_VIEW(dhplug->webview), 
-								 homepage_uri);
-		g_free(homepage_uri);
-	}
-	
-	return dhplug;
-}
-
-/**
- * devhelp_plugin_destroy:
- * @param dhplug	The DevhelpPlugin to destroy/free.
- * 
- * Destroys the associated widgets and frees memory for a DevhelpPlugin.  This
- * should be called from Geany's plugin_cleanup() function.
- */
-void devhelp_plugin_destroy(DevhelpPlugin *dhplug)
-{	
-	gtk_widget_destroy(dhplug->sb_notebook);
-	
-	gtk_notebook_remove_page(GTK_NOTEBOOK(dhplug->main_notebook),
-								dhplug->webview_tab);
-								
-	if (!dhplug->in_message_window)
-		main_notebook_destroy();
-   
-	gtk_widget_destroy(dhplug->editor_menu_sep);
-	gtk_widget_destroy(dhplug->editor_menu_item);
-   
-	gtk_notebook_set_tab_pos(GTK_NOTEBOOK(
-								geany->main_widgets->sidebar_notebook), 
-								dhplug->orig_sb_tab_pos);
-
-	g_free(dhplug);
-}

Deleted: trunk/geany-plugins/devhelp/src/dh-plugin.h
===================================================================
--- trunk/geany-plugins/devhelp/src/dh-plugin.h	2011-03-07 00:55:38 UTC (rev 1956)
+++ trunk/geany-plugins/devhelp/src/dh-plugin.h	2011-03-07 05:13:27 UTC (rev 1957)
@@ -1,68 +0,0 @@
-/*
- * dhplug.h - Part of the Geany Devhelp Plugin
- * 
- * Copyright 2010 Matthew Brush <mbrush at leftclick.ca>
- * 
- * 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 3 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., 51 Franklin Street, Fifth Floor, Boston,
- * MA 02110-1301, USA.
- */
- 
-#ifndef DH_PLUGIN_H
-#define DH_PLUGIN_H
-
-#include <gtk/gtk.h>
-
-#ifndef DHPLUG_DATA_DIR
-#define DHPLUG_DATA_DIR "/usr/local/share/geany-devhelp"
-#endif
-
-#define DHPLUG_WEBVIEW_HOME_FILE DHPLUG_DATA_DIR"/home.html"
-
-/* main plugin struct */
-typedef struct
-{
-	GtkWidget *book_tree;			/// "Contents" in the sidebar
-	GtkWidget *search;				/// "Search" in the sidebar
-	GtkWidget *sb_notebook;			/// Notebook that holds contents/search
-	gint sb_notebook_tab;			/// Index of tab where devhelp sidebar is
-	GtkWidget *webview;				/// Webkit that shows documentation
-	gint webview_tab;				/// Index of tab that contains the webview
-	GtkWidget *main_notebook;		/// Notebook that holds Geany doc notebook and
-									/// and webkit view
-	GtkWidget *doc_notebook;		/// Geany's document notebook  
-	GtkWidget *editor_menu_item;	/// Item in the editor's context menu 
-	GtkWidget *editor_menu_sep;		/// Separator item above menu item
-	gboolean *webview_active;		/// Tracks whether webview stuff is shown
-	
-	gboolean last_main_tab_id;		/// These track the last id of the tabs
-	gboolean last_sb_tab_id;		///   before toggling
-	gboolean tabs_toggled;			/// Tracks state of whether to toggle to
-									/// Devhelp or back to code
-	gboolean created_main_nb;		/// Track whether we created the main notebook
-	
-	GtkPositionType orig_sb_tab_pos;
-	gboolean sidebar_tab_bottom;
-	gboolean in_message_window;
-
-} DevhelpPlugin;
-
-gchar *devhelp_clean_word(gchar *str);
-gchar *devhelp_get_current_tag(void);
-void devhelp_activate_tabs(DevhelpPlugin *dhplug, gboolean contents);
-DevhelpPlugin *devhelp_plugin_new(gboolean sb_tabs_bottom, gboolean show_in_msgwin);
-void devhelp_plugin_destroy(DevhelpPlugin *dhplug);
-void devhelp_plugin_sidebar_tabs_bottom(DevhelpPlugin *dhplug, gboolean bottom);
-
-#endif

Modified: trunk/geany-plugins/devhelp/src/plugin.c
===================================================================
--- trunk/geany-plugins/devhelp/src/plugin.c	2011-03-07 00:55:38 UTC (rev 1956)
+++ trunk/geany-plugins/devhelp/src/plugin.c	2011-03-07 05:13:27 UTC (rev 1957)
@@ -27,7 +27,7 @@
 #include <devhelp/dh-search.h>
 
 #include "plugin.h"
-#include "dh-plugin.h"
+#include "devhelpplugin.h"
 
 PLUGIN_VERSION_CHECK(200)
 
@@ -62,18 +62,18 @@
 	switch (key_id)
 	{
 		case KB_DEVHELP_TOGGLE_CONTENTS:
-			devhelp_activate_tabs(dev_help_plugin, TRUE);
+			devhelp_plugin_activate_tabs(dev_help_plugin, TRUE);
 			break;
 		case KB_DEVHELP_TOGGLE_SEARCH:
-			devhelp_activate_tabs(dev_help_plugin, FALSE);
+			devhelp_plugin_activate_tabs(dev_help_plugin, FALSE);
 			break;
 		case KB_DEVHELP_SEARCH_SYMBOL:
 		{
-			gchar *current_tag = devhelp_get_current_tag();
+			gchar *current_tag = devhelp_plugin_get_current_tag();
 			if (current_tag == NULL) return;
 			dh_search_set_search_string(
 				DH_SEARCH(dev_help_plugin->search), current_tag, NULL);
-			devhelp_activate_tabs(dev_help_plugin, FALSE);
+			devhelp_plugin_activate_tabs(dev_help_plugin, FALSE);
 			g_free(current_tag);
 			break;
 		}
@@ -100,7 +100,7 @@
 {
 	static gboolean message_shown = FALSE;
 	if (response_id == GTK_RESPONSE_OK || response_id == GTK_RESPONSE_APPLY) {
-		store_preferences();
+		plugin_store_preferences();
 		if (!message_shown) 
 		{
 			dialogs_show_msgbox(GTK_MESSAGE_INFO, "Settings for whether to use "
@@ -277,8 +277,8 @@
 
 	plugin_module_make_resident(geany_plugin);
 
-	config_init();				   
-	load_preferences();
+	plugin_config_init();				   
+	plugin_load_preferences();
 	
 	dev_help_plugin = devhelp_plugin_new(move_sidebar_tabs_bottom,
 										 show_in_msg_window);
@@ -297,10 +297,9 @@
 
 void plugin_cleanup(void)
 {	
-	store_preferences();
+	plugin_store_preferences();
 	
-	if (dev_help_plugin != NULL)
-		devhelp_plugin_destroy(dev_help_plugin);
+	g_object_unref(dev_help_plugin);
 	
 	g_free(default_config);
 	g_free(user_config);

Modified: trunk/geany-plugins/devhelp/src/plugin.h
===================================================================
--- trunk/geany-plugins/devhelp/src/plugin.h	2011-03-07 00:55:38 UTC (rev 1956)
+++ trunk/geany-plugins/devhelp/src/plugin.h	2011-03-07 05:13:27 UTC (rev 1957)
@@ -24,7 +24,7 @@
 
 #include <gtk/gtk.h>
 #include <geanyplugin.h>
-#include "dh-plugin.h"
+#include "devhelpplugin.h"
 
 extern GeanyPlugin	 	*geany_plugin;
 extern GeanyData	   	*geany_data;


This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.



More information about the Plugins-Commits mailing list