[gedit] Introduce GeditViewActivatable



commit 210ed41e48988646203fc94eba19ce94657821cd
Author: Steve Frécinaux <code istique net>
Date:   Fri Jun 25 11:58:11 2010 +0200

    Introduce GeditViewActivatable
    
    This interface is for extensions that should be activated against a
    gedit view (or the gedit document it contains)
    
    https://bugzilla.gnome.org/show_bug.cgi?id=606363

 gedit/Makefile.am              |    2 +
 gedit/gedit-view-activatable.c |   90 ++++++++++++++++++++++++++++++++++++++++
 gedit/gedit-view-activatable.h |   66 +++++++++++++++++++++++++++++
 gedit/gedit-view.c             |   45 ++++++++++++++++++++
 4 files changed, 203 insertions(+), 0 deletions(-)
---
diff --git a/gedit/Makefile.am b/gedit/Makefile.am
index cb8ec57..9baf31e 100644
--- a/gedit/Makefile.am
+++ b/gedit/Makefile.am
@@ -144,6 +144,7 @@ INST_H_FILES =				\
 	gedit-tab.h 			\
 	gedit-utils.h 			\
 	gedit-view.h 			\
+	gedit-view-activatable.h	\
 	gedit-window.h			\
 	gedit-window-activatable.h
 
@@ -157,6 +158,7 @@ header_DATA = 				\
 	$(INST_H_FILES)
 
 libgedit_private_la_SOURCES =		\
+	gedit-view-activatable.c	\
 	gedit-window-activatable.c
 
 libgedit_c_files =			\
diff --git a/gedit/gedit-view-activatable.c b/gedit/gedit-view-activatable.c
new file mode 100644
index 0000000..846b7c8
--- /dev/null
+++ b/gedit/gedit-view-activatable.c
@@ -0,0 +1,90 @@
+/*
+ * gedit-view-activatable.h
+ * This file is part of gedit
+ *
+ * Copyright (C) 2010 Steve Frécinaux
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU Library 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 Library General Public License for more details.
+ *
+ *  You should have received a copy of the GNU Library 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 "gedit-view-activatable.h"
+
+/**
+ * SECTION:gedit-view-activatable
+ * @short_description: Interface for extensions activatable on views
+ * @see_also: #PeasExtensionSet
+ *
+ * #GeditViewActivatable is an interface which should be implemented by
+ * extensions that should be activated on a gedit view (or the document it
+ * contains).
+ **/
+G_DEFINE_INTERFACE(GeditViewActivatable, gedit_view_activatable, G_TYPE_OBJECT)
+
+void
+gedit_view_activatable_default_init (GeditViewActivatableInterface *iface)
+{
+}
+
+/**
+ * gedit_view_activatable_activate:
+ * @activatable: A #GeditViewActivatable.
+ * @view: The #GeditView on which the plugin should be activated.
+ *
+ * Activates the extension on the given view.
+ */
+void
+gedit_view_activatable_activate (GeditViewActivatable *activatable,
+				   GeditView            *view)
+{
+	GeditViewActivatableInterface *iface;
+
+	g_return_if_fail (GEDIT_IS_VIEW_ACTIVATABLE (activatable));
+	g_return_if_fail (GEDIT_IS_VIEW (view));
+
+	iface = GEDIT_VIEW_ACTIVATABLE_GET_IFACE (activatable);
+
+	if (iface->activate != NULL)
+	{
+		iface->activate (activatable, view);
+	}
+}
+
+/**
+ * gedit_view_activatable_deactivate:
+ * @activatable: A #GeditViewActivatable.
+ * @view: A #GeditView.
+ *
+ * Deactivates the plugin on the given view.
+ */
+void
+gedit_view_activatable_deactivate (GeditViewActivatable *activatable,
+				     GeditView            *view)
+{
+	GeditViewActivatableInterface *iface;
+
+	g_return_if_fail (GEDIT_IS_VIEW_ACTIVATABLE (activatable));
+	g_return_if_fail (GEDIT_IS_VIEW (view));
+
+	iface = GEDIT_VIEW_ACTIVATABLE_GET_IFACE (activatable);
+
+	if (iface->deactivate != NULL)
+	{
+		iface->deactivate (activatable, view);
+	}
+}
diff --git a/gedit/gedit-view-activatable.h b/gedit/gedit-view-activatable.h
new file mode 100644
index 0000000..0e0dbf7
--- /dev/null
+++ b/gedit/gedit-view-activatable.h
@@ -0,0 +1,66 @@
+/*
+ * gedit-view-activatable.h
+ * This file is part of gedit
+ *
+ * Copyright (C) 2010 - Steve Frécinaux
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU Library 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 Library General Public License for more details.
+ *
+ *  You should have received a copy of the GNU Library 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 __GEDIT_VIEW_ACTIVATABLE_H__
+#define __GEDIT_VIEW_ACTIVATABLE_H__
+
+#include <glib-object.h>
+
+#include "gedit-view.h"
+
+G_BEGIN_DECLS
+
+/*
+ * Type checking and casting macros
+ */
+#define GEDIT_TYPE_VIEW_ACTIVATABLE		(gedit_view_activatable_get_type ())
+#define GEDIT_VIEW_ACTIVATABLE(obj)		(G_TYPE_CHECK_INSTANCE_CAST ((obj), GEDIT_TYPE_VIEW_ACTIVATABLE, GeditViewActivatable))
+#define GEDIT_VIEW_ACTIVATABLE_IFACE(obj)	(G_TYPE_CHECK_CLASS_CAST ((obj), GEDIT_TYPE_VIEW_ACTIVATABLE, GeditViewActivatableInterface))
+#define GEDIT_IS_VIEW_ACTIVATABLE(obj)		(G_TYPE_CHECK_INSTANCE_TYPE ((obj), GEDIT_TYPE_VIEW_ACTIVATABLE))
+#define GEDIT_VIEW_ACTIVATABLE_GET_IFACE(obj)	(G_TYPE_INSTANCE_GET_INTERFACE ((obj), GEDIT_TYPE_VIEW_ACTIVATABLE, GeditViewActivatableInterface))
+
+typedef struct _GeditViewActivatable           GeditViewActivatable; /* dummy typedef */
+typedef struct _GeditViewActivatableInterface  GeditViewActivatableInterface;
+
+struct _GeditViewActivatableInterface
+{
+	GTypeInterface g_iface;
+
+	/* Virtual public methods */
+	void	(*activate)		(GeditViewActivatable *activatable,
+					 GeditView            *view);
+	void	(*deactivate)		(GeditViewActivatable	*activatable,
+					 GeditView            *view);
+};
+
+/*
+ * Public methods
+ */
+GType	 gedit_view_activatable_get_type	(void)  G_GNUC_CONST;
+
+void	 gedit_view_activatable_activate	(GeditViewActivatable *activatable,
+						 GeditView            *view);
+void	 gedit_view_activatable_deactivate	(GeditViewActivatable *activatable,
+						 GeditView            *view);
+
+G_END_DECLS
+
+#endif /* __GEDIT_VIEW_ACTIVATABLE_H__ */
diff --git a/gedit/gedit-view.c b/gedit/gedit-view.c
index 200fd6d..1f9dd43 100644
--- a/gedit/gedit-view.c
+++ b/gedit/gedit-view.c
@@ -38,10 +38,13 @@
 #include <stdlib.h>
 
 #include <gdk/gdkkeysyms.h>
+#include <libpeas/peas-extension-set.h>
 
 #include <glib/gi18n.h>
 
 #include "gedit-view.h"
+#include "gedit-view-activatable.h"
+#include "gedit-plugins-engine.h"
 #include "gedit-debug.h"
 #include "gedit-marshal.h"
 #include "gedit-utils.h"
@@ -96,6 +99,8 @@ struct _GeditViewPrivate
 	gboolean     disable_popdown;
 	
 	GtkTextBuffer *current_buffer;
+
+	PeasExtensionSet *extensions;
 };
 
 /* The search entry completion is shared among all the views */
@@ -305,6 +310,24 @@ current_buffer_removed (GeditView *view)
 }
 
 static void
+extension_added (PeasExtensionSet *extensions,
+		 PeasPluginInfo   *info,
+		 PeasExtension    *exten,
+		 GeditView        *view)
+{
+	peas_extension_call (exten, "activate", view);
+}
+
+static void
+extension_removed (PeasExtensionSet *extensions,
+		   PeasPluginInfo   *info,
+		   PeasExtension    *exten,
+		   GeditView        *view)
+{
+	peas_extension_call (exten, "deactivate", view);
+}
+
+static void
 on_notify_buffer_cb (GeditView  *view,
 		     GParamSpec *arg1,
 		     gpointer    userdata)
@@ -330,6 +353,11 @@ on_notify_buffer_cb (GeditView  *view,
 			  "search_highlight_updated",
 			  G_CALLBACK (search_highlight_updated_cb),
 			  view);
+
+	/* We only activate the extensions when the right buffer is set,
+	 * because most plugins will expect this behaviour, and we won't
+	 * change the buffer later anyway. */
+	peas_extension_set_call (view->priv->extensions, "activate", view);
 }
 
 static void 
@@ -422,11 +450,23 @@ gedit_view_init (GeditView *view)
 	if (tl != NULL)
 		gtk_target_list_add_uri_targets (tl, TARGET_URI_LIST);
 		
+	view->priv->extensions = peas_extension_set_new (PEAS_ENGINE (gedit_plugins_engine_get_default ()),
+							 GEDIT_TYPE_VIEW_ACTIVATABLE);
+	g_signal_connect (view->priv->extensions,
+			  "extension-added",
+			  G_CALLBACK (extension_added),
+			  view);
+	g_signal_connect (view->priv->extensions,
+			  "extension-removed",
+			  G_CALLBACK (extension_removed),
+			  view);
+
 	/* Act on buffer change */
 	g_signal_connect (view, 
 			  "notify::buffer", 
 			  G_CALLBACK (on_notify_buffer_cb),
 			  NULL);
+
 }
 
 static void
@@ -436,6 +476,11 @@ gedit_view_destroy (GtkObject *object)
 
 	view = GEDIT_VIEW (object);
 
+	peas_extension_set_call (view->priv->extensions,
+				 "deactivate",
+				 view);
+	g_object_unref (view->priv->extensions);
+
 	if (view->priv->search_window != NULL)
 	{
 		gtk_widget_destroy (view->priv->search_window);



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]