[cluttermm] Added get_object convienience method.



commit acc08253bb554ccc74b859cb31aa7a266c9d8c44
Author: Chris Kühl <chrisk openismus com>
Date:   Mon Mar 28 15:45:09 2011 +0200

    Added get_object convienience method.
    
    * clutter/src/script.[h|cc]g: Added get_object and helper functions.
    These changes were inspired by get_widget in Gtkmm's Builder class.

 ChangeLog              |    7 +++++++
 clutter/src/script.ccg |   37 +++++++++++++++++++++++++++++++++++++
 clutter/src/script.hg  |   27 ++++++++++++++++++++++++++-
 3 files changed, 70 insertions(+), 1 deletions(-)
---
diff --git a/ChangeLog b/ChangeLog
index f483163..964d4b7 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,10 @@
+2011-03-28  Chris Kühl  <chrisk openismus com>
+
+	Added get_object convienience method.
+
+	* clutter/src/script.[h|cc]g: Added get_object and helper functions.
+	These changes were inspired by get_widget in Gtkmm's Builder class.
+
 2011-02-28  Chris Kühl  <chrisk openismus com>
 
  	Temporarily remove methods with std::map containing a Glib::ValueBase.
diff --git a/clutter/src/script.ccg b/clutter/src/script.ccg
index 08e0a6b..9bc8b2b 100644
--- a/clutter/src/script.ccg
+++ b/clutter/src/script.ccg
@@ -84,6 +84,43 @@ std::vector<Glib::RefPtr<const Glib::Object> > Script::list_objects() const
   return objectVec;
 }
 
+GObject* Script::get_cobject(const Glib::ustring& name)
+{
+  GObject *cobject = clutter_script_get_object (gobj(), name.c_str());
+  if(!cobject)
+  {
+    g_critical("cluttermm: object `%s' not found in ClutterScript file.", name.c_str());
+    return 0;
+  }
+
+  return cobject;
+}
+
+Glib::RefPtr<Glib::Object> Script::get_object_checked(const Glib::ustring& name, GType type)
+{
+  // Get the widget from the ClutterScript JSON file.
+  GObject *cobject = get_cobject(name);
+  Glib::RefPtr<Glib::Object> object;
+
+  if(!cobject)
+  {
+    g_critical("cluttermm: Glib::Object: object `%s' was not found in the ClutterScript file, or the specified part of it.",
+      name.c_str());
+    return object;
+  }
+
+  // Check if it has the correct type.
+  if(!g_type_is_a(G_OBJECT_TYPE(cobject), type))
+  {
+    g_critical("cluttermm: object `%s' (in ClutterScript file) is of type `%s' but `%s' was expected",
+               name.c_str(), G_OBJECT_TYPE_NAME(cobject), g_type_name(type));
+    return object;
+  }
+
+  object = Glib::wrap (cobject, true /* take ref */);
+  return object;
+}
+
 #ifdef GLIBMM_VFUNCS_ENABLED
 // Custom coded so that we can custom-code the vfunc in the ClutterScript class.
 // This is marginally better than modifying gtkmmproc to allow this.
diff --git a/clutter/src/script.hg b/clutter/src/script.hg
index 57e7780..9a6b557 100644
--- a/clutter/src/script.hg
+++ b/clutter/src/script.hg
@@ -21,7 +21,6 @@
 _DEFS(cluttermm,clutter)
 _PINCLUDE(glibmm/private/object_p.h)
 
-
 namespace Clutter
 {
 
@@ -53,6 +52,29 @@ public:
   _WRAP_METHOD(Glib::RefPtr<Glib::Object> get_object(const Glib::ustring& name), clutter_script_get_object, refreturn)
   _WRAP_METHOD(Glib::RefPtr<const Glib::Object> get_object(const Glib::ustring& name) const, clutter_script_get_object, refreturn, constversion)
 
+  /** Get a RefPtr to an object from the ClutterScript JSON file.
+   * For instance:
+   * @code
+   * Glib::RefPtr<Clutter::Rectangle> aRectangleRef;
+   * scriptObject->get_object("myRectangle", aRectangleRef);
+   * @endcode
+   * This method prints a warning message to the console if the object
+   * doesn't exist or has the wrong type, so you don't need to check that
+   * manually.
+   *
+   * @param name The name of the object.
+   * @retval derived_object A RefPtr to the object, or <tt>0</tt> on failure.
+   */
+  template <class T_Object> inline
+  void get_object(const Glib::ustring& name, Glib::RefPtr<T_Object>& derived_object)
+  {
+    // The dynamic_cast<> should never fail if geT_Object_checked() succeeded.
+    derived_object = Glib::RefPtr<T_Object>::cast_dynamic(this->get_object_checked(name, T_Object::get_base_type()));
+
+    if(!derived_object)
+      g_critical("Clutter:Script::get_object(): dynamic_cast<> failed.");
+  }
+
   //clutter_script_get_objects() is a varargs convenience function for C.
   _IGNORE(clutter_script_get_objects)
 
@@ -73,6 +95,9 @@ public:
   _WRAP_PROPERTY("filename-set", bool)
 
 protected:
+  Glib::RefPtr<Glib::Object> get_object_checked(const Glib::ustring& name, GType type);
+  GObject* get_cobject(const Glib::ustring& name);
+
 #m4begin
 dnl Custom-coded vfunc:
 dnl



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