[ease/bindings] [bindings] Added a library to bind GObject properties



commit 858c8ccab9bc2d98427d38afc7a0bc04f321ca08
Author: Nate Stedman <natesm gmail com>
Date:   Fri Aug 20 13:33:41 2010 -0400

    [bindings] Added a library to bind GObject properties

 .gitignore                        |    1 +
 Makefile.am                       |    5 +-
 configure.ac                      |    7 ++-
 ease-bindings/Makefile.am         |   73 ++++++++++++++++++++++
 ease-bindings/bindings-test.vala  |   51 ++++++++++++++++
 ease-bindings/bindings.vala       |  120 +++++++++++++++++++++++++++++++++++++
 pkgconfig/ease-bindings-0.1.pc.in |   11 ++++
 vapi/ease-bindings-0.1.deps       |    1 +
 8 files changed, 266 insertions(+), 3 deletions(-)
---
diff --git a/.gitignore b/.gitignore
index ae5a2a0..4e85caa 100644
--- a/.gitignore
+++ b/.gitignore
@@ -17,6 +17,7 @@ libease/tmp-introspect*
 
 # build files
 ease/ease
+ease-bindings/bindings-test
 *.h
 *.o
 *.lo
diff --git a/Makefile.am b/Makefile.am
index 344904d..131f438 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -1,6 +1,6 @@
 NULL =
 
-SUBDIRS = po data flutter ease-core ease
+SUBDIRS = po data ease-bindings flutter ease-core ease
 
 # distribution
 EXTRA_DIST = data/ease.desktop.in.in  \
@@ -13,8 +13,9 @@ ACLOCAL_AMFLAGS = -I m4
 
 pkgconfigdir = $(libdir)/pkgconfig
 pkgconfig_DATA = \
+	pkgconfig/ease-bindings- BINDINGS_VERSION@.pc \
     pkgconfig/ease-core- EASE_CORE_VERSION@.pc \
-    pkgconfig/flutter- FLUTTER_VERSION@.pc
+    pkgconfig/flutter- FLUTTER_VERSION@.pc 
     
 run: ease/ease
 	ease/ease
diff --git a/configure.ac b/configure.ac
index c365005..0639c26 100644
--- a/configure.ac
+++ b/configure.ac
@@ -3,6 +3,7 @@ AC_PREREQ([2.59])
 m4_define(ease_version, 0.4)
 m4_define(ease_core_version, 0.4)
 m4_define(flutter_version, 0.3)
+m4_define(bindings_version, 0.1)
 
 AC_INIT([Ease],
         [ease_version],
@@ -15,10 +16,12 @@ AC_CONFIG_FILES([
 	po/Makefile.in
 	data/Makefile
 	ease-core/Makefile
+	ease-bindings/Makefile
 	flutter/Makefile
 	ease/Makefile
 	pkgconfig/ease-core-0.4.pc
-	pkgconfig/flutter-0.3.pc])
+	pkgconfig/flutter-0.3.pc
+	pkgconfig/ease-bindings-0.1.pc])
 
 AC_CONFIG_SRCDIR([Makefile.am])
 AC_CONFIG_HEADERS([config.h])
@@ -84,6 +87,8 @@ AC_DEFINE(EASE_CORE_VERSION, ease_core_version, [Ease Core Version])
 AC_SUBST(EASE_CORE_VERSION, ease_core_version)
 AC_DEFINE(FLUTTER_VERSION, flutter_version, [Flutter Version])
 AC_SUBST(FLUTTER_VERSION, flutter_version)
+AC_DEFINE(BINDINGS_VERSION, bindings_version, [Ease Bindings Version])
+AC_SUBST(BINDINGS_VERSION, bindings_version)
 
 dnl internationalization
 GETTEXT_PACKAGE=ease
diff --git a/ease-bindings/Makefile.am b/ease-bindings/Makefile.am
new file mode 100644
index 0000000..7694b61
--- /dev/null
+++ b/ease-bindings/Makefile.am
@@ -0,0 +1,73 @@
+# libease-bind
+lib_LTLIBRARIES = libease-bindings- BINDINGS_VERSION@.la
+
+INCLUDES = \
+	$(EASE_CFLAGS)
+
+EASE_CFLAGS += -w
+
+AM_CPPFLAGS = \
+	$(EASE_CFLAGS) \
+	-include $(CONFIG_HEADER) \
+	$(NULL)
+
+# source files
+libease_bindings_ BINDINGS_VERSION@_la_SOURCES = bindings.vala
+
+# compiler flags
+libease_bindings_ BINDINGS_VERSION@_la_VALAFLAGS = \
+	--pkg gee-1.0 \
+	--pkg glib-2.0 \
+	--vapi=$(top_srcdir)/vapi/ease-bindings- BINDINGS_VERSION@.vapi \
+	--header=ease-bindings.h \
+	--thread \
+	-g \
+	$(NULL)
+
+libease_bindings_ BINDINGS_VERSION@_la_CFLAGS = $(EASE_CFLAGS)
+libease_bindings_ BINDINGS_VERSION@_la_LDFLAGS = $(EASE_LDFLAGS)
+
+# header installation
+ease_bindings_includedir=$(includedir)/ease-bindings/
+ease_bindings_include_HEADERS = ease-bindings.h
+
+# vapi installation
+vapidir = $(datadir)/vala/vapi
+vapi_DATA = \
+	$(top_srcdir)/vapi/ease-bindings- BINDINGS_VERSION@.vapi \
+	$(top_srcdir)/vapi/ease-bindings- BINDINGS_VERSION@.deps
+
+# remove generated files on `make clean`
+CLEANFILES = \
+	bindings.c \
+	$(top_srcdir)/vapi/ease-bindings- BINDINGS_VERSION@.vapi \
+	ease-bindings.h
+
+EXTRA_DIST = \
+	$(top_srcdir)/vapi/ease-bindings- BINDINGS_VERSION@.vapi \
+	$(top_srcdir)/vapi/ease-bindings- BINDINGS_VERSION@.deps
+
+
+# bindings test
+noinst_PROGRAMS = bindings-test
+
+bindings_test_SOURCES = bindings-test.vala
+
+bindings_test_VALAFLAGS = \
+	--pkg ease-bindings- BINDINGS_VERSION@ \
+	--pkg gtk+-2.0 \
+	--vapidir $(top_srcdir)/vapi \
+	-g
+
+bindings_test_CFLAGS = \
+	$(EASE_CFLAGS) \
+	-w \
+	-I$(top_srcdir)/ease-bindings \
+	-include $(top_srcdir)/config.h
+
+bindings_test_LDFLAGS = $(EASE_LIBS) --export-dynamic
+
+bindings_test_LDADD = \
+	$(top_builddir)/ease-bindings/libease-bindings- BINDINGS_VERSION@.la \
+	-lm \
+	$(NULL)
diff --git a/ease-bindings/bindings-test.vala b/ease-bindings/bindings-test.vala
new file mode 100644
index 0000000..cc0a602
--- /dev/null
+++ b/ease-bindings/bindings-test.vala
@@ -0,0 +1,51 @@
+/*  Ease, a GTK presentation application
+    Copyright (C) 2010 Nate Stedman
+
+    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, see <http://www.gnu.org/licenses/>.
+*/
+
+public int main(string[] args)
+{
+	Gtk.init(ref args);
+	
+	// create bound GTK widgets
+	var scale = new Gtk.HScale.with_range(0, 10, 0.1);
+	var spin = new Gtk.SpinButton.with_range(0, 10, 0.1);
+	
+	// bind the scale and spin buttons together
+	Bindings.connect(spin, "value", scale.adjustment, "value");
+	
+	// create a button to drop the binding
+	var button = new Gtk.Button.with_label("Drop Binding");
+	button.clicked.connect(() => {
+		Bindings.drop(spin, "value", scale.adjustment, "value");
+	});
+	
+	// place in a window
+	var window = new Gtk.Window(Gtk.WindowType.TOPLEVEL);
+	window.width_request = 400;
+	var hbox = new Gtk.HBox(false, 5);
+	hbox.pack_start(spin, false, false, 0);
+	hbox.pack_start(scale, true, true, 0);
+	hbox.pack_start(button, false, false, 0);
+	window.add(hbox);
+	
+	// show the window
+	window.show_all();
+	window.hide.connect(() => Gtk.main_quit());
+	
+	Gtk.main();
+	
+	return 0;
+}
diff --git a/ease-bindings/bindings.vala b/ease-bindings/bindings.vala
new file mode 100644
index 0000000..1103ec1
--- /dev/null
+++ b/ease-bindings/bindings.vala
@@ -0,0 +1,120 @@
+/*  Ease, a GTK presentation application
+    Copyright (C) 2010 Nate Stedman
+
+    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, see <http://www.gnu.org/licenses/>.
+*/
+
+public static class Bindings
+{
+	private static Gee.LinkedList<Binding> bindings
+	{
+		get
+		{
+			if (bindings_lazy != null) return bindings_lazy;
+			return bindings_lazy = new Gee.LinkedList<Binding>();
+		}
+	}
+	private static Gee.LinkedList<Binding> bindings_lazy;
+	
+	public static void connect(GLib.Object object1, string property1,
+	                           GLib.Object object2, string property2)
+	{
+		// connect signal handlers
+		object1.notify[property1].connect(on_notify);
+		object2.notify[property1].connect(on_notify);
+		
+		// keep track of the binding
+		bindings.add(new Binding(object1, property1, object2, property2));
+	}
+	
+	public static void drop(GLib.Object object1, string property1,
+	                        GLib.Object object2, string property2)
+	{
+		if (bindings.size < 1) return;
+		
+		var itr = bindings.iterator();
+		for (itr.first();; itr.next())
+		{
+			var binding = itr.get() as Binding;
+			if ((binding.obj1 == object1 && binding.prop1 == property1 &&
+			     binding.obj2 == object2 && binding.prop2 == property2) ||
+			    (binding.obj2 == object1 && binding.prop2 == property1 &&
+			     binding.obj1 == object2 && binding.prop1 == property2))
+			{
+				itr.remove();
+			}
+			if (!itr.has_next()) break;
+		}
+	}
+	
+	private static void on_notify(GLib.Object object, GLib.ParamSpec pspec)
+	{
+		foreach (var binding in bindings)
+		{
+			if (binding.silence) continue;
+			if (object == binding.obj1 && pspec.name == binding.prop1)
+			{
+				// don't loop on this binding
+				binding.silence = true;
+				
+				// perform the set
+				set(object, pspec.name, binding.obj2, binding.prop2);
+				
+				// start acting on this binding again
+				binding.silence = false;
+			}
+			else if (object == binding.obj2 && pspec.name == binding.prop2)
+			{
+				// don't loop on this binding
+				binding.silence = true;
+				
+				// perform the set
+				set(object, pspec.name, binding.obj1, binding.prop1);
+				
+				// start acting on this binding again
+				binding.silence = false;
+			}
+		}
+	}
+	
+	private static void set(GLib.Object from, string from_prop,
+	                        GLib.Object to, string to_prop)
+	{
+		// get the value from the sender
+		var type = from.get_class().find_property(from_prop).value_type;
+		var storage = GLib.Value(type);
+		from.get_property(from_prop, ref storage);
+		
+		// set the value on the bound object
+		to.set_property(to_prop, storage);
+	}
+	
+	private class Binding : GLib.Object
+	{
+		public GLib.Object obj1;
+		public GLib.Object obj2;
+		public string prop1;
+		public string prop2;
+		public bool silence = false;
+		
+		public Binding(GLib.Object o1, string p1, GLib.Object o2, string p2)
+		{
+			obj1 = o1;
+			obj2 = o2;
+			prop1 = p1;
+			prop2 = p2;
+		}
+	}
+}
+
diff --git a/pkgconfig/ease-bindings-0.1.pc.in b/pkgconfig/ease-bindings-0.1.pc.in
new file mode 100644
index 0000000..d91a5b9
--- /dev/null
+++ b/pkgconfig/ease-bindings-0.1.pc.in
@@ -0,0 +1,11 @@
+prefix= prefix@
+exec_prefix= exec_prefix@
+libdir= libdir@
+includedir= includedir@
+
+Name: Ease Bindings
+Description: Simple GObject Property Bindings
+Requires: gee-1.0 gobject-2.0
+Version: @PACKAGE_VERSION@
+Libs: -L${libdir} -lease-bindings- BINDINGS_VERSION@
+Cflags: -I${includedir}/ease-bindings- BINDINGS_VERSION@ -I${libdir}/ease-bindings- BINDINGS_VERSION@/include
diff --git a/vapi/ease-bindings-0.1.deps b/vapi/ease-bindings-0.1.deps
new file mode 100644
index 0000000..8871971
--- /dev/null
+++ b/vapi/ease-bindings-0.1.deps
@@ -0,0 +1 @@
+gee-1.0



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