[libgda] GtkSourceView can now be used in the text plugin



commit d97c92dba287cf9a1b33653aa30684520bd6ed23
Author: Vivien Malerba <malerba gnome-db org>
Date:   Wed Apr 28 22:29:02 2010 +0200

    GtkSourceView can now be used in the text plugin
    
    
    if it is detected, as a PROG_LANG option

 libgda-ui/data-entries/plugins/.gitignore          |    1 +
 libgda-ui/data-entries/plugins/Makefile.am         |    7 +-
 .../data-entries/plugins/gdaui-entry-filesel.c     |    1 +
 .../plugins/gdaui-entry-text-spec.xml.in           |   13 +++
 libgda-ui/data-entries/plugins/gdaui-entry-text.c  |  108 +++++++++++++++++++-
 libgda-ui/data-entries/plugins/gdaui-entry-text.h  |    4 +-
 libgda-ui/data-entries/plugins/libmain.c           |   83 +++++++++++++++-
 7 files changed, 209 insertions(+), 8 deletions(-)
---
diff --git a/libgda-ui/data-entries/plugins/.gitignore b/libgda-ui/data-entries/plugins/.gitignore
index 35b7cf9..9994c59 100644
--- a/libgda-ui/data-entries/plugins/.gitignore
+++ b/libgda-ui/data-entries/plugins/.gitignore
@@ -2,3 +2,4 @@ gdaui-entry-filesel-spec.xml
 gdaui-entry-password.xml
 gdaui-entry-pict-spec.xml
 gdaui-entry-pict-spec_string.xml
+gdaui-entry-text-spec.xml
diff --git a/libgda-ui/data-entries/plugins/Makefile.am b/libgda-ui/data-entries/plugins/Makefile.am
index e730ffd..5897cd3 100644
--- a/libgda-ui/data-entries/plugins/Makefile.am
+++ b/libgda-ui/data-entries/plugins/Makefile.am
@@ -7,6 +7,7 @@ AM_CPPFLAGS = \
         $(LIBGDA_CFLAGS) \
         $(GTK_CFLAGS) \
 	$(LIBGCRYPT_CFLAGS) \
+	$(GTKSOURCEVIEW_CFLAGS) \
         -DPREFIX=\""$(prefix)"\" \
         -DSYSCONFDIR=\""$(sysconfdir)"\" \
         -DDATADIR=\""$(datadir)"\" \
@@ -54,14 +55,16 @@ libgda_ui_plugins_la_LIBADD = \
         $(top_builddir)/libgda-ui/libgda-ui-4.0.la \
 	$(LIBGCRYPT_LIBS) \
 	$(LIBGDA_LIBS) \
-        $(GTK_LIBS)
+        $(GTK_LIBS) \
+	$(GTKSOURCEVIEW_LIBS)
 
 xmldir   = $(libdir)/libgda-$(GDA_ABI_MAJOR_VERSION).$(GDA_ABI_MINOR_VERSION)/plugins
 xml_in_files = \
 	gdaui-entry-pict-spec.xml.in \
 	gdaui-entry-pict-spec_string.xml.in \
 	gdaui-entry-filesel-spec.xml.in \
-	gdaui-entry-password.xml.in
+	gdaui-entry-password.xml.in \
+	gdaui-entry-text-spec.xml.in
 
 xml_DATA = $(xml_in_files:.xml.in=.xml)
 
diff --git a/libgda-ui/data-entries/plugins/gdaui-entry-filesel.c b/libgda-ui/data-entries/plugins/gdaui-entry-filesel.c
index 915dd3d..d5ca8b9 100644
--- a/libgda-ui/data-entries/plugins/gdaui-entry-filesel.c
+++ b/libgda-ui/data-entries/plugins/gdaui-entry-filesel.c
@@ -103,6 +103,7 @@ gdaui_entry_filesel_init (GdauiEntryFilesel * gdaui_entry_filesel)
  * gdaui_entry_filesel_new
  * @dh: the data handler to be used by the new widget
  * @type: the requested data type (compatible with @dh)
+ * @options: the options
  *
  * Creates a new widget which is mainly a GtkEntry
  *
diff --git a/libgda-ui/data-entries/plugins/gdaui-entry-text-spec.xml.in b/libgda-ui/data-entries/plugins/gdaui-entry-text-spec.xml.in
new file mode 100644
index 0000000..69b87a3
--- /dev/null
+++ b/libgda-ui/data-entries/plugins/gdaui-entry-text-spec.xml.in
@@ -0,0 +1,13 @@
+<?xml version="1.0"?>
+<data-set-spec>
+  <parameters>
+    <parameter id="PROG_LANG" _name="Programming language" _descr="Define to enable syntactical colourisation" gdatype="string" source="langs:0"/>
+  </parameters>
+  <sources>
+    <gda_array name="langs">
+      <gda_array_field name="id" gdatype="gchararray"/>
+      <gda_array_field name="name" gdatype="gchararray"/>
+      <gda_array_data/>
+    </gda_array>
+  </sources>
+</data-set-spec>
diff --git a/libgda-ui/data-entries/plugins/gdaui-entry-text.c b/libgda-ui/data-entries/plugins/gdaui-entry-text.c
index 7fdb678..3fef9c6 100644
--- a/libgda-ui/data-entries/plugins/gdaui-entry-text.c
+++ b/libgda-ui/data-entries/plugins/gdaui-entry-text.c
@@ -1,6 +1,6 @@
 /* gdaui-entry-text.c
  *
- * Copyright (C) 2003 - 2005 Vivien Malerba
+ * Copyright (C) 2003 - 2010 Vivien Malerba
  *
  * This Library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Library General Public License as
@@ -18,8 +18,24 @@
  * USA
  */
 
+#include <string.h>
+#include <binreloc/gda-binreloc.h>
 #include "gdaui-entry-text.h"
 #include <libgda/gda-data-handler.h>
+#include <libgda/gda-blob-op.h>
+
+#ifdef HAVE_GTKSOURCEVIEW
+  #ifdef GTK_DISABLE_SINGLE_INCLUDES
+    #undef GTK_DISABLE_SINGLE_INCLUDES
+  #endif
+
+  #include <gtksourceview/gtksourceview.h>
+  #include <gtksourceview/gtksourcelanguagemanager.h>
+  #include <gtksourceview/gtksourcebuffer.h>
+  #include <gtksourceview/gtksourcestyleschememanager.h>
+  #include <gtksourceview/gtksourcestylescheme.h>
+#endif
+#define LANGUAGE_SQL "gda-sql"
 
 /* 
  * Main static functions 
@@ -45,6 +61,7 @@ struct _GdauiEntryTextPrivate
 {
 	GtkTextBuffer *buffer;
 	GtkWidget     *view;
+	gchar         *lang; /* for code colourisation */
 };
 
 
@@ -101,13 +118,14 @@ gdaui_entry_text_init (GdauiEntryText * gdaui_entry_text)
  * gdaui_entry_text_new
  * @dh: the data handler to be used by the new widget
  * @type: the requested data type (compatible with @dh)
+ * @options: the options
  *
  * Creates a new widget which is mainly a GtkEntry
  *
  * Returns: the new widget
  */
 GtkWidget *
-gdaui_entry_text_new (GdaDataHandler *dh, GType type)
+gdaui_entry_text_new (GdaDataHandler *dh, GType type, const gchar *options)
 {
 	GObject *obj;
 	GdauiEntryText *mgtxt;
@@ -118,6 +136,19 @@ gdaui_entry_text_new (GdaDataHandler *dh, GType type)
 
 	obj = g_object_new (GDAUI_TYPE_ENTRY_TEXT, "handler", dh, NULL);
 	mgtxt = GDAUI_ENTRY_TEXT (obj);
+#ifdef HAVE_GTKSOURCEVIEW
+	if (options && *options) {
+                GdaQuarkList *params;
+                const gchar *str;
+
+                params = gda_quark_list_new_from_string (options);
+                str = gda_quark_list_find (params, "PROG_LANG");
+                if (str)
+			mgtxt->priv->lang = g_strdup (str);
+                gda_quark_list_free (params);
+        }
+#endif
+
 	gdaui_data_entry_set_value_type (GDAUI_DATA_ENTRY (mgtxt), type);
 
 	return GTK_WIDGET (obj);
@@ -134,7 +165,6 @@ gdaui_entry_text_dispose (GObject   * object)
 
 	gdaui_entry_text = GDAUI_ENTRY_TEXT (object);
 	if (gdaui_entry_text->priv) {
-
 	}
 
 	/* parent class */
@@ -151,6 +181,7 @@ gdaui_entry_text_finalize (GObject   * object)
 
 	gdaui_entry_text = GDAUI_ENTRY_TEXT (object);
 	if (gdaui_entry_text->priv) {
+		g_free (gdaui_entry_text->priv->lang);
 
 		g_free (gdaui_entry_text->priv);
 		gdaui_entry_text->priv = NULL;
@@ -160,6 +191,57 @@ gdaui_entry_text_finalize (GObject   * object)
 	parent_class->finalize (object);
 }
 
+#ifdef HAVE_GTKSOURCEVIEW
+static void
+create_tags_for_sql (GtkTextBuffer *buffer, const gchar *language)
+{
+	GtkSourceLanguageManager *mgr;
+	GtkSourceLanguage *lang;
+	gchar ** current_search_path;
+	gint len;
+	gchar ** search_path;
+
+	GtkSourceStyleSchemeManager* sch_mgr;
+	GtkSourceStyleScheme *sch;
+
+	g_return_if_fail (language != NULL);
+	g_return_if_fail (!strcmp (language, LANGUAGE_SQL));
+	mgr = gtk_source_language_manager_new ();
+
+	/* alter search path */
+	current_search_path = (gchar **) gtk_source_language_manager_get_search_path (mgr);
+	len = g_strv_length (current_search_path);
+	search_path = g_new0 (gchar*, len + 2);
+	memcpy (search_path, current_search_path, sizeof (gchar*) * len);
+	search_path [len] = gda_gbr_get_file_path (GDA_DATA_DIR, LIBGDA_ABI_NAME, "language-specs", NULL);
+	gtk_source_language_manager_set_search_path (mgr, search_path);
+	g_free (search_path [len]);
+	g_free (search_path);
+
+	lang = gtk_source_language_manager_get_language (mgr, "gda-sql");
+
+	if (!lang) {
+		gchar *tmp;
+		tmp = gda_gbr_get_file_path (GDA_DATA_DIR, LIBGDA_ABI_NAME, "language-spec", NULL);
+		g_print ("Could not find the gda-sql.lang file in %s,\nusing the default SQL highlighting rules.\n",
+			 tmp);
+		g_free (tmp);
+		lang = gtk_source_language_manager_get_language (mgr, "sql");
+	}
+	if (lang)
+		gtk_source_buffer_set_language (GTK_SOURCE_BUFFER (buffer), lang);
+
+	g_object_unref (mgr);
+
+	sch_mgr = gtk_source_style_scheme_manager_get_default ();
+	sch = gtk_source_style_scheme_manager_get_scheme (sch_mgr, "tango");
+	if (sch) 
+		gtk_source_buffer_set_style_scheme (GTK_SOURCE_BUFFER (buffer), sch);
+}	
+#endif
+
+
+
 static GtkWidget *
 create_entry (GdauiEntryWrapper *mgwrap)
 {
@@ -170,7 +252,27 @@ create_entry (GdauiEntryWrapper *mgwrap)
 	mgtxt = GDAUI_ENTRY_TEXT (mgwrap);
 	g_return_val_if_fail (mgtxt->priv, NULL);
 
+#ifdef HAVE_GTKSOURCEVIEW
+	if (mgtxt->priv->lang) {
+		GtkSourceBuffer *sbuf;
+		mgtxt->priv->view = gtk_source_view_new ();
+		sbuf = GTK_SOURCE_BUFFER (gtk_text_view_get_buffer (GTK_TEXT_VIEW (mgtxt->priv->view)));
+
+		GtkSourceLanguageManager *lm;
+		GtkSourceLanguage *sl;
+		lm = gtk_source_language_manager_get_default ();
+		sl = gtk_source_language_manager_get_language (lm, mgtxt->priv->lang);
+		
+		gtk_source_buffer_set_language (sbuf, sl);
+		gtk_source_buffer_set_highlight_syntax (sbuf, TRUE);
+		if (! strcmp (mgtxt->priv->lang, LANGUAGE_SQL))
+			create_tags_for_sql (GTK_TEXT_BUFFER (sbuf), LANGUAGE_SQL);
+	}
+	else
+		mgtxt->priv->view = gtk_text_view_new ();
+#else
 	mgtxt->priv->view = gtk_text_view_new ();
+#endif
 	mgtxt->priv->buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (mgtxt->priv->view));
 	sw = gtk_scrolled_window_new (NULL, NULL);
 	gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (sw), GTK_SHADOW_IN);
diff --git a/libgda-ui/data-entries/plugins/gdaui-entry-text.h b/libgda-ui/data-entries/plugins/gdaui-entry-text.h
index 60c8a14..f9c0924 100644
--- a/libgda-ui/data-entries/plugins/gdaui-entry-text.h
+++ b/libgda-ui/data-entries/plugins/gdaui-entry-text.h
@@ -1,6 +1,6 @@
 /* gdaui-entry-text.h
  *
- * Copyright (C) 2003 - 2006 Vivien Malerba
+ * Copyright (C) 2003 - 2010 Vivien Malerba
  *
  * This Library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Library General Public License as
@@ -51,7 +51,7 @@ struct _GdauiEntryTextClass
 };
 
 GType        gdaui_entry_text_get_type        (void) G_GNUC_CONST;
-GtkWidget   *gdaui_entry_text_new             (GdaDataHandler *dh, GType type);
+GtkWidget   *gdaui_entry_text_new             (GdaDataHandler *dh, GType type, const gchar *options);
 
 
 G_END_DECLS
diff --git a/libgda-ui/data-entries/plugins/libmain.c b/libgda-ui/data-entries/plugins/libmain.c
index 9ad95ca..c8d009d 100644
--- a/libgda-ui/data-entries/plugins/libmain.c
+++ b/libgda-ui/data-entries/plugins/libmain.c
@@ -39,6 +39,18 @@
 #include "gdaui-data-cell-renderer-password.h"
 #endif
 
+#ifdef HAVE_GTKSOURCEVIEW
+  #ifdef GTK_DISABLE_SINGLE_INCLUDES
+    #undef GTK_DISABLE_SINGLE_INCLUDES
+  #endif
+
+  #include <gtksourceview/gtksourceview.h>
+  #include <gtksourceview/gtksourcelanguagemanager.h>
+  #include <gtksourceview/gtksourcebuffer.h>
+  #include <gtksourceview/gtksourcestyleschememanager.h>
+  #include <gtksourceview/gtksourcestylescheme.h>
+#endif
+
 static GdauiDataEntry *plugin_entry_filesel_create_func (GdaDataHandler *handler, GType type, const gchar *options);
 static GdauiDataEntry *plugin_entry_cidr_create_func (GdaDataHandler *handler, GType type, const gchar *options);
 static GdauiDataEntry *plugin_entry_text_create_func (GdaDataHandler *handler, GType type, const gchar *options);
@@ -136,6 +148,75 @@ plugin_init (GError **error)
 	plugin->cell_create_func = NULL;
 	retlist = g_slist_append (retlist, plugin);
 
+#ifdef HAVE_GTKSOURCEVIEW
+	file = gda_gbr_get_file_path (GDA_LIB_DIR, LIBGDA_ABI_NAME, "plugins", "gdaui-entry-text-spec.xml", NULL);
+	if (! g_file_test (file, G_FILE_TEST_EXISTS)) {
+		if (error && !*error)
+			g_set_error (error, 0, 0, _("Missing spec. file '%s'"), file);
+        }
+	else {
+		xmlDocPtr doc;
+		doc = xmlParseFile (file);
+		if (!doc) {
+			if (error && !*error)
+				g_set_error (error, 0, 0, _("Missing spec. file '%s'"), file);
+		}
+		else {
+			xmlNodePtr node;
+			node = xmlDocGetRootElement (doc);
+			for (node = node->children; node; node = node->next) {
+				if (!strcmp ((gchar*) node->name, "sources")) {
+					for (node = node->children; node; node = node->next) {
+						if (!strcmp ((gchar*) node->name, "gda_array")) {
+							for (node = node->children; node; node = node->next) {
+								if (!strcmp ((gchar*) node->name,
+									     "gda_array_data")) {
+									break;
+								}
+							}
+
+							break;
+						}
+					}
+					break;
+				}
+			}
+			GtkSourceLanguageManager *lm;
+			const gchar * const * langs;
+			gint i;
+			lm = gtk_source_language_manager_get_default ();
+			langs = gtk_source_language_manager_get_language_ids (lm);
+			for (i = 0; ; i++) {
+				const gchar *tmp;
+				tmp = langs [i];
+				if (!tmp)
+					break;
+				if (node) {
+					xmlNodePtr row;
+					row = xmlNewChild (node, NULL, BAD_CAST "gda_array_row", NULL);
+					xmlNewChild (row, NULL, BAD_CAST "gda_value", BAD_CAST tmp);
+
+					GtkSourceLanguage *sl;
+					sl = gtk_source_language_manager_get_language (lm, tmp);
+					if (sl)
+						xmlNewChild (row, NULL, BAD_CAST "gda_value",
+							     BAD_CAST gtk_source_language_get_name (sl));
+					else
+						xmlNewChild (row, NULL, BAD_CAST "gda_value", BAD_CAST tmp);
+				}
+			}
+
+			xmlChar *out;
+			int size;
+			xmlDocDumpFormatMemory (doc, &out, &size, 0);
+			xmlFreeDoc (doc);
+			plugin->options_xml_spec = g_strdup ((gchar*) out);
+			xmlFree (out);
+		}
+	}
+	g_free (file);
+#endif
+
 	/* Picture - binary */
 	plugin = g_new0 (GdauiPlugin, 1);
 	plugin->plugin_name = "picture";
@@ -203,7 +284,7 @@ plugin_entry_cidr_create_func (GdaDataHandler *handler, GType type, const gchar
 static GdauiDataEntry *
 plugin_entry_text_create_func (GdaDataHandler *handler, GType type, const gchar *options)
 {
-	return (GdauiDataEntry *) gdaui_entry_text_new (handler, type);
+	return (GdauiDataEntry *) gdaui_entry_text_new (handler, type, options);
 }
 
 static GdauiDataEntry *



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