[glibmm] Add small Glib::Property example



commit 706ba417277b9c31c03fadcd706759ca06ce5e6b
Author: Jonathon Jongsma <jonathon quotidian org>
Date:   Mon Apr 27 21:32:01 2009 -0500

    Add small Glib::Property example
    
    	* examples/properties/Makefile.am
    	* examples/properties/properties_example.cc
    	* .gitignore
    	* configure.ac
    	* examples/Makefile.am: add a brief example of using properties with
    	a Glib::Object-derived class
---
 .gitignore                                |    1 +
 ChangeLog                                 |    9 +++
 configure.ac                              |    1 +
 examples/Makefile.am                      |    2 +-
 examples/properties/Makefile.am           |    6 ++
 examples/properties/properties_example.cc |   81 +++++++++++++++++++++++++++++
 6 files changed, 99 insertions(+), 1 deletions(-)

diff --git a/.gitignore b/.gitignore
index 55c8d1f..77a5eb3 100644
--- a/.gitignore
+++ b/.gitignore
@@ -46,6 +46,7 @@ examples/iochannel_stream/example
 examples/keyfile/example
 examples/markup/parser
 examples/options/example
+examples/properties/properties_example
 examples/regex/example
 examples/thread/dispatcher
 examples/thread/dispatcher2
diff --git a/ChangeLog b/ChangeLog
index 3fbb01d..25d8315 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -15,6 +15,15 @@
 	conversion of ustring::iterator to ustring::const_iterator.  This
 	makes the operators work with mixed argument types. (bgo #580773)
 
+2009-04-27  Jonathon Jongsma  <jonathon quotidian org>
+
+	* examples/properties/Makefile.am
+	* examples/properties/properties_example.cc
+	* .gitignore
+	* configure.ac
+	* examples/Makefile.am: add a brief example of using properties with
+	a Glib::Object-derived class
+
 2009-04-23  Johannes Schmid <jschmid openismus com>
 
 	* tools/pm/DocParser.pm:
diff --git a/configure.ac b/configure.ac
index 1e23854..4dd4073 100644
--- a/configure.ac
+++ b/configure.ac
@@ -318,6 +318,7 @@ AC_CONFIG_FILES([
     examples/keyfile/Makefile
     examples/markup/Makefile
     examples/options/Makefile
+    examples/properties/Makefile
     examples/thread/Makefile
     examples/iochannel_stream/Makefile
     examples/child_watch/Makefile
diff --git a/examples/Makefile.am b/examples/Makefile.am
index bcf3d84..ccc9014 100644
--- a/examples/Makefile.am
+++ b/examples/Makefile.am
@@ -1,4 +1,4 @@
-example_dirs = child_watch compose iochannel_stream markup options regex thread keyfile
+example_dirs = child_watch compose iochannel_stream markup options properties regex thread keyfile
 
 # These use gtkmm stuff:
 # thread
diff --git a/examples/properties/Makefile.am b/examples/properties/Makefile.am
new file mode 100644
index 0000000..2a97360
--- /dev/null
+++ b/examples/properties/Makefile.am
@@ -0,0 +1,6 @@
+include $(top_srcdir)/examples/Makefile.am_fragment
+
+#Build the executable, but don't install it.
+noinst_PROGRAMS = properties_example
+properties_example_SOURCES = properties_example.cc
+
diff --git a/examples/properties/properties_example.cc b/examples/properties/properties_example.cc
new file mode 100644
index 0000000..b4c0327
--- /dev/null
+++ b/examples/properties/properties_example.cc
@@ -0,0 +1,81 @@
+/* Copyright (C) 2008 jonathon jongsma
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library 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
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free
+ * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ */
+
+#include <glibmm.h>
+#include <iostream>
+
+// A class that contains properties must inherit from Glib::Object (or a class
+// that inherits from Glib::Object)
+class Person : public Glib::Object
+{
+public:
+    Person () :
+        // to register custom properties, you must register a custom GType.  If
+        // you don't know what that means, don't worry, just remember to add
+        // this Glib::ObjectBase constructor call to your class' constructor
+        Glib::ObjectBase (typeid (Person)),
+        // register the properties with the object and give them names
+        prop_firstname (*this, "firstname"),
+        prop_lastname (*this, "lastname"),
+        // this one has a default value
+        prop_age (*this, "age", 10)
+    {}
+
+    // provide proxies for the properties.  The proxy allows you to connect to
+    // the 'changed' signal, etc.
+    Glib::PropertyProxy<Glib::ustring> property_firstname ()
+    { return prop_firstname.get_proxy (); }
+    Glib::PropertyProxy<Glib::ustring> property_lastname ()
+    { return prop_lastname.get_proxy (); }
+    Glib::PropertyProxy<int> property_age ()
+    { return prop_age.get_proxy (); }
+
+private:
+    Glib::Property<Glib::ustring> prop_firstname;
+    Glib::Property<Glib::ustring> prop_lastname;
+    Glib::Property<int> prop_age;
+};
+
+void on_firstname_changed ()
+{ std::cout << "- firstname changed!" << std::endl; }
+void on_lastname_changed ()
+{ std::cout << "- lastname changed!" << std::endl; }
+void on_age_changed ()
+{ std::cout << "- age changed!" << std::endl; }
+
+int main (int argc, char** argv)
+{
+    Glib::init ();
+    Person p;
+    // Register some handlers that will be called when the values of the
+    // specified parameters are changed
+    p.property_firstname ().signal_changed ()
+        .connect (sigc::ptr_fun (&on_firstname_changed));
+    p.property_lastname ().signal_changed ()
+        .connect (sigc::ptr_fun (&on_lastname_changed));
+    p.property_age ().signal_changed ()
+        .connect (sigc::ptr_fun (&on_age_changed));
+
+    // now change the properties and see that the handlers get called
+    std::cout << "Changing the properties of 'p'" << std::endl;
+    p.property_firstname () = "John";
+    p.property_lastname () = "Doe";
+    p.property_age () = 43;
+    std::cout << "Done changing the properties of 'p'" << std::endl;
+
+    return 0;
+}



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