Adding support for gobject-introspection

Carlos Garnacho <[email protected]>
Newsgroups gmane.editors.abiword.devel
Message-ID <1343147376.680.36.camel@sacarino>
Hey,

Lately I've been working on adding gobject-introspection support for
libabiword (patch attached), although it's been a mostly smooth
experience I found some miscellaneous issues:

* I noticed that abi_widget_get_selection() and abi_widget_get_content()
advertize that the text must be freed through g_free(), although it's
allocated through c++ memory allocation, this plays odd with the
introspection support as it expects strings to be malloc()'ed. I've
attached a patch to make it work as advertized.

* In xap_UnixTableWidget.cpp, the restart_widget() function crashes on
GTK+3, as the event handler really expects an meaningful event there. My
understanding is that this code tried to cope with returning the widget
to a regular state as it disappeared while being clicked on. since >=
2.8, GTK+ widgets handle GdkEventGrabBroken, which get triggered on
situations like this, so I think this code can be safely removed, or
cased to older versions.

* I can also see graphical oddities (like the AbiWidget disappearing
from the window briefly after being shown), but this is doesn't seem
to be related to python nor gobject-introspection, as I also reproduced
on a simple C client, I'm still investigating what could cause this.

Comments welcome,
  Carlos
add-gobject-introspection-support.patch (text/x-patch, 5.7 KB)
diff --git a/configure.in b/configure.in
index aed409b..217a778 100644
--- a/configure.in
+++ b/configure.in
@@ -1164,6 +1164,7 @@ AC_OUTPUT(
   src/af/xap/gtk/t/Makefile
   src/af/xap/win/Makefile
   src/af/xap/xp/Makefile
+  src/gi-overrides/Makefile
   src/plugins/Makefile
   src/text/Makefile
   src/text/fmt/Makefile
diff --git a/src/Makefile.am b/src/Makefile.am
index 4c425c4..02ab678 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -1,5 +1,5 @@
 
-SUBDIRS = af plugins text wp
+SUBDIRS = af plugins text wp 
 
 libabiword_cpp = 
 opt_libs = 
@@ -9,11 +9,16 @@ resources =
 -include $(INTROSPECTION_MAKEFILE)
 INTROSPECTION_GIRS =
 INTROSPECTION_SCANNER_ARGS = \
-	-v --warn-all --accept-unprefixed
+	-v --warn-all --accept-unprefixed \
+	--symbol-prefix=abi --symbol-prefix=libabiword
 INTROSPECTION_COMPILER_ARGS = \
 	--includedir=$(srcdir) \
     --includedir=.
 
+if HAVE_INTROSPECTION
+SUBDIRS += gi-overrides
+endif
+
 if TOOLKIT_COCOA
 main_cpp = $(top_srcdir)/src/wp/main/cocoa/CocoaMain.cpp
 include $(top_srcdir)/src/wp/main/cocoa/bundle.mk
@@ -171,7 +176,9 @@ Abi_@ABI_SUB_SERIES@_gir_FILES = \
 	af/xap/gtk/xap_UnixTableWidget.h \
 	af/xap/gtk/xap_UnixTableWidget.cpp \
 	wp/ap/gtk/abiwidget.h \
-	wp/ap/gtk/abiwidget.cpp
+	wp/ap/gtk/abiwidget.cpp \
+	wp/main/gtk/libabiword.h \
+	wp/main/gtk/libabiword.cpp
 
 girdir = $(GIRDIR)
 gir_DATA = $(INTROSPECTION_GIRS)
diff --git a/src/gi-overrides/Abi.py b/src/gi-overrides/Abi.py
new file mode 100644
index 0000000..666fd61
--- /dev/null
+++ b/src/gi-overrides/Abi.py
@@ -0,0 +1,9 @@
+import sys
+from gi.repository import Gtk
+from ..overrides import override
+from ..importer import modules
+
+Abi = modules['Abi']._introspection_module
+
+# Initialize libabiword
+Abi.init(sys.argv)
diff --git a/src/gi-overrides/Makefile.am b/src/gi-overrides/Makefile.am
new file mode 100644
index 0000000..5792009
--- /dev/null
+++ b/src/gi-overrides/Makefile.am
@@ -0,0 +1,7 @@
+overrides_dir = `python -c "import gi; print gi._overridesdir"`
+
+install-data-local:
+	$(INSTALL_DATA) $(top_srcdir)/src/gi-overrides/Abi.py $(overrides_dir)/Abi.py
+
+uninstall-local:
+	rm -f $(overrides_dir)/Abi.py
diff --git a/src/wp/ap/gtk/abiwidget.cpp b/src/wp/ap/gtk/abiwidget.cpp
index d77ef12..f3c1638 100644
--- a/src/wp/ap/gtk/abiwidget.cpp
+++ b/src/wp/ap/gtk/abiwidget.cpp
@@ -1310,10 +1310,17 @@ s_abi_widget_get_file_type(const char * extension_or_mimetype, const char * cont
 	return ieft;
 }
 
-/*!
- * Extract all the content in the document.
- * Caller must g_free the memory.
- * Number of bytes is returned in iLength
+/**
+ * abi_widget_get_content:
+ * @w: an #AbiWidget
+ * @extension_or_mimetype: content type for the returned selection
+ * @exp_props: (allow-none): export properties
+ * @iLength: (out) (allow-none): length of returned selection, in bytes
+ *
+ * Gets all document contents
+ *
+ * Returns: (transfer full) (array length=iLength): the selection,
+ *          the caller must free the memory through g_free()
  */
 extern "C" gchar *
 abi_widget_get_content(AbiWidget * w, const char * extension_or_mimetype, const char * exp_props, gint * iLength)
@@ -1342,10 +1349,16 @@ abi_widget_get_content(AbiWidget * w, const char * extension_or_mimetype, const
 	return szOut;
 }
 
-/*!
- * Extract all the content in the selection
- * Caller must g_free the memory.
- * Number of bytes is returned in iLength
+/**
+ * abi_widget_get_selection:
+ * @w: an #AbiWidget
+ * @extension_or_mimetype: content type for the returned selection
+ * @iLength: (out) (allow-none): length of returned selection, in bytes
+ *
+ * Gets the current selection
+ *
+ * Returns: (transfer full) (array length=iLength): the selection,
+ *          the caller must free the memory through g_free()
  */
 extern "C" gchar *
 abi_widget_get_selection(AbiWidget * w, const gchar * extension_or_mimetype, gint * iLength)
@@ -1395,7 +1408,16 @@ abi_widget_get_selection(AbiWidget * w, const gchar * extension_or_mimetype, gin
 	return szOut;
 }
 
-
+/**
+ * abi_widget_get_mouse_pos:
+ * @w: an #AbiWidget
+ * @x: (out): return value for the mouse position in the X axis
+ * @y: (out): return value for the mouse position in the Y axis
+ *
+ * Returns the mouse position relative to @w
+ *
+ * Return value: #TRUE if the mouse position could be retrieved
+ */
 extern "C" gboolean
 abi_widget_get_mouse_pos(AbiWidget * w, gint32 * x, gint32 * y)
 {
@@ -2476,7 +2498,7 @@ abi_widget_get_type (void)
 }
 
 /**
- * abi_widget_new
+ * abi_widget_new:
  *
  * Creates a new AbiWord widget using an internal Abiword App
  */
@@ -2492,7 +2514,7 @@ abi_widget_new (void)
 }
 
 /**
- * abi_widget_new_with_file
+ * abi_widget_new_with_file:
  *
  * Creates a new AbiWord widget and tries to load the file
  * This uses an internal Abiword App
@@ -2520,7 +2542,7 @@ abi_widget_get_frame ( AbiWidget * w )
 }
 
 /**
- * abi_widget_invoke()
+ * abi_widget_invoke:
  *
  * Invoke any of abiword's edit methods by name
  *
@@ -2545,7 +2567,7 @@ abi_widget_invoke (AbiWidget * w, const char * mthdName)
 }
 
 /**
- * abi_widget_invoke_ex()
+ * abi_widget_invoke_ex:
  *
  * Invoke any of abiword's edit methods by name
  *
diff --git a/src/wp/main/gtk/libabiword.cpp b/src/wp/main/gtk/libabiword.cpp
index fa673c7..006a556 100644
--- a/src/wp/main/gtk/libabiword.cpp
+++ b/src/wp/main/gtk/libabiword.cpp
@@ -28,6 +28,13 @@
 
 static AP_UnixApp *_abiword_app = NULL;
 
+/**
+ * libabiword_init:
+ * @argc: (inout): argument count
+ * @argv: (array length=argc) (inout) (allow-none): Commandline arguments
+ *
+ * Initializes libabiword
+ */
 void libabiword_init (int argc, char **argv)
 {
 	if (!_abiword_app) {
@@ -41,6 +48,11 @@ void libabiword_init (int argc, char **argv)
 	}
 }
 
+/**
+ * libabiword_init_noargs:
+ *
+ * Initializes libabiword
+ */
 void libabiword_init_noargs ()
 {
 	if (!_abiword_app) {
use-g-new-to-allocate-text.patch (text/x-patch, 944 B)
diff --git a/src/wp/ap/gtk/abiwidget.cpp b/src/wp/ap/gtk/abiwidget.cpp
index ebd3aab..d77ef12 100644
--- a/src/wp/ap/gtk/abiwidget.cpp
+++ b/src/wp/ap/gtk/abiwidget.cpp
@@ -1333,7 +1333,7 @@ abi_widget_get_content(AbiWidget * w, const char * extension_or_mimetype, const
 	gsf_output_close(GSF_OUTPUT(sink));
 	guint32 size = gsf_output_size (GSF_OUTPUT(sink));
 	const guint8* ibytes = gsf_output_memory_get_bytes (sink);
-	gchar * szOut = new gchar[size+1];
+	gchar * szOut = g_new (gchar, size+1);
 	memcpy(szOut,ibytes,size);
 	szOut[size] = 0;
 	g_object_unref(G_OBJECT(sink));
@@ -1386,7 +1386,7 @@ abi_widget_get_selection(AbiWidget * w, const gchar * extension_or_mimetype, gin
 		return NULL;
 	pie->copyToBuffer(pDocRange,&buf);
 	guint32 size = buf.getLength();
-	gchar * szOut = new gchar[size+1];
+	gchar * szOut = g_new (gchar, size+1);
 	memcpy(szOut,buf.getPointer(0),size);
 	szOut[size] = 0;
 	g_object_unref(G_OBJECT(sink));
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.