[glibmm] Gio::Action: Add convenience methods for bool-state (toggle) actions.



commit fca2daa8a76def22f49290b0c4e76c3faa086149
Author: Murray Cumming <murrayc murrayc com>
Date:   Fri Jul 26 22:40:08 2013 +0200

    Gio::Action: Add convenience methods for bool-state (toggle) actions.
    
    * gio/src/simpleaction.[hg|ccg]: Add constructors and
      create_bool() methods that take a bool instead of a VariantBase
      for the state.
    * gio/src/action.[hg|ccg]: Add get_state_bool() and
      change_state(bool).
    
      It might be better to put these in a derived class, but I still
      hope that the developers of the C API decide not to make this
      common case so awkward:
      https://bugzilla.gnome.org/show_bug.cgi?id=704392#c4

 gio/src/action.ccg       |   19 +++++++++++++++++++
 gio/src/action.hg        |   20 ++++++++++++++++++++
 gio/src/simpleaction.ccg |   10 ++++++++++
 gio/src/simpleaction.hg  |   30 ++++++++++++++++++++++++++++++
 4 files changed, 79 insertions(+), 0 deletions(-)
---
diff --git a/gio/src/action.ccg b/gio/src/action.ccg
index d3dd74a..2ca354b 100644
--- a/gio/src/action.ccg
+++ b/gio/src/action.ccg
@@ -24,4 +24,23 @@
 namespace Gio
 {
 
+bool Action::get_state_bool() const
+{
+  g_return_val_if_fail(
+    g_action_get_state_type(const_cast<GAction*>(gobj())) == G_VARIANT_TYPE_BOOLEAN,
+    false);
+
+  GVariant* state = g_action_get_state(const_cast<GAction*>(gobj()));
+  g_return_val_if_fail(state, false);
+  return g_variant_get_boolean(state);
+}
+
+void Action::change_state(bool value)
+{
+  g_return_if_fail(
+    g_action_get_state_type(const_cast<GAction*>(gobj())) == G_VARIANT_TYPE_BOOLEAN);
+
+  change_state(Glib::Variant<bool>::create(value));
+}
+
 } // namespace Gio
diff --git a/gio/src/action.hg b/gio/src/action.hg
index c14fa84..5b7152b 100644
--- a/gio/src/action.hg
+++ b/gio/src/action.hg
@@ -117,6 +117,8 @@ public:
 
   //TODO: Change the return type of get_state() when we can break ABI.
   // See https://bugzilla.gnome.org/show_bug.cgi?id=690134
+  // TODO: Surely it wouldn't really be an ABI break to change the return type,
+  // because nobody could possibly be using this method yet, because it is so utterly useless. murrayc
   /** Queries the current state of @a action.
    *
    * @note
@@ -144,7 +146,25 @@ public:
    */
   _WRAP_METHOD(void get_state() const, g_action_get_state)
 
+  //TODO: Docs
+  bool get_state_bool() const;
+
   _WRAP_METHOD(void change_state(const Glib::VariantBase& value), g_action_change_state)
+
+  /** Request for the state of @a action to be changed to @a value,
+   * assuming that the state is boolean.
+   * 
+   * See g_action_get_state_type().
+   * 
+   * This call merely requests a change.  The action may refuse to change
+   * its state or may change its state to something other than @a value.
+   * See g_action_get_state_hint().
+   * 
+   * @newin{2,38}
+   * @param value The new state.
+   */
+  void change_state(bool value);
+
   _WRAP_METHOD(void activate(const Glib::VariantBase& parameter), g_action_activate)
 
   _WRAP_PROPERTY("enabled", bool)
diff --git a/gio/src/simpleaction.ccg b/gio/src/simpleaction.ccg
index 286c964..c10a4d7 100644
--- a/gio/src/simpleaction.ccg
+++ b/gio/src/simpleaction.ccg
@@ -27,4 +27,14 @@ SimpleAction::SimpleAction(const Glib::ustring& name)
   _CONSTRUCT("name", name.c_str())
 {}
 
+SimpleAction::SimpleAction(const Glib::ustring& name, const Glib::VariantBase& state)
+:
+  _CONSTRUCT("name", name.c_str(), "state", const_cast<GVariant*>((state).gobj()))
+{}
+
+Glib::RefPtr<SimpleAction> SimpleAction::create_bool(const Glib::ustring&name, bool state)
+{
+  return create(name, Glib::Variant<bool>::create(state));
+}
+
 } // namespace Gio
diff --git a/gio/src/simpleaction.hg b/gio/src/simpleaction.hg
index a487b7c..3a5fb6e 100644
--- a/gio/src/simpleaction.hg
+++ b/gio/src/simpleaction.hg
@@ -45,8 +45,12 @@ class SimpleAction : public Glib::Object, public Action
   _STRUCT_NOT_HIDDEN
 
 protected:
+  //TODO: Docs
   explicit SimpleAction(const Glib::ustring& name);
 
+  //TODO: Docs
+  SimpleAction(const Glib::ustring& name, const Glib::VariantBase& state);
+
 #m4 _CONVERSION(`const Glib::VariantType&',`const GVariantType*',`$3.gobj()')
   _WRAP_CTOR(SimpleAction(const Glib::ustring& name, const Glib::VariantType& parameter_type), 
g_simple_action_new)
 
@@ -62,6 +66,32 @@ public:
   _WRAP_METHOD_DOCS_ONLY(g_simple_action_new_stateful)
   _WRAP_CREATE(const Glib::ustring& name, const Glib::VariantType& parameter_type, const Glib::VariantBase& 
state)
 
+  /** Creates a new stateful action.
+   * 
+   * @a state is the initial state of the action.  All future state values
+   * must have the same VariantType as the initial state.
+   * 
+   * @newin{2,38}
+   * @param name The name of the action.
+   * @param state The initial state of the action.
+   * @return A new SimpleAction.
+   */
+  _WRAP_CREATE(const Glib::ustring& name, const Glib::VariantBase& state)
+
+  //TODO: Use similar C API if they ever add it. Doing this manually is tedious.
+  //TODO: Create a derived SimpleToggleAction class for this?
+  /** Creates a new boolean stateful action.
+   * 
+   * @a state is the initial state of the action.  All future state values
+   * must also be bool.
+   * 
+   * @newin{2,38}
+   * @param name The name of the action.
+   * @param state The initial state of the action.
+   * @return A new SimpleAction.
+   */
+  static Glib::RefPtr<SimpleAction> create_bool(const Glib::ustring&name, bool state);
+
   _WRAP_METHOD(void set_enabled(bool enabled = true), g_simple_action_set_enabled)
   _WRAP_METHOD(void set_state(const Glib::VariantBase& value), g_simple_action_set_state)
 


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