[glibmm] ActionMap, SimpleAction: Add API for creating radio actions.



commit b2e671ed523446a6d45f390362a39e7f544b85a5
Author: Murray Cumming <murrayc murrayc com>
Date:   Mon Jul 29 21:49:39 2013 +0200

    ActionMap, SimpleAction: Add API for creating radio actions.
    
    * gio/src/simpleaction.[hg|ccg]:
      Add create_radio_string() and create_radio_integer().
    * gio/src/actionmap.[hg|ccg]:
      Add add_action_radio_string() and add_action_radio_integer().
    
      With GAction, radio items seem to be one action with multiple
      possible parameter/state values, with the parameter and state
      values always being equal. This C++ API assumes that string
      and integer will be the most commonly-used types for those values.
      These values are specified in the GtkBuilder <menu> as target
      values for <item>s each having the same action name.
    
      I am really not sure that this is the best API we can have.

 gio/src/actionmap.ccg    |   29 +++++++++++++++++++++++++++
 gio/src/actionmap.hg     |   49 ++++++++++++++++++++++++++++++++++++++++++++++
 gio/src/simpleaction.ccg |   15 +++++++++++++-
 gio/src/simpleaction.hg  |   22 ++++++++++++++++++++
 4 files changed, 114 insertions(+), 1 deletions(-)
---
diff --git a/gio/src/actionmap.ccg b/gio/src/actionmap.ccg
index 3c87802..c05b009 100644
--- a/gio/src/actionmap.ccg
+++ b/gio/src/actionmap.ccg
@@ -51,4 +51,33 @@ Glib::RefPtr<SimpleAction> ActionMap::add_action_bool(const Glib::ustring& name,
   return action;
 }
 
+Glib::RefPtr<SimpleAction> ActionMap::add_action_radio_string(const Glib::ustring& name)
+{
+  Glib::RefPtr<SimpleAction> action = SimpleAction::create_radio_string(name);
+  add_action(action);
+  return action;
+}
+
+Glib::RefPtr<SimpleAction> ActionMap::add_action_radio_string(const Glib::ustring& name, const ActivateSlot& 
slot)
+{
+  Glib::RefPtr<SimpleAction> action = add_action_radio_string(name);
+  action->signal_activate().connect(slot);
+  return action;
+}
+
+Glib::RefPtr<SimpleAction> ActionMap::add_action_radio_integer(const Glib::ustring& name)
+{
+  Glib::RefPtr<SimpleAction> action = SimpleAction::create_radio_integer(name);
+  add_action(action);
+  return action;
+}
+
+Glib::RefPtr<SimpleAction> ActionMap::add_action_radio_integer(const Glib::ustring& name, const 
ActivateSlot& slot)
+{
+  Glib::RefPtr<SimpleAction> action = add_action_radio_integer(name);
+  action->signal_activate().connect(slot);
+  return action;
+}
+
+
 } // namespace Gio
diff --git a/gio/src/actionmap.hg b/gio/src/actionmap.hg
index 3e6d71a..54926dc 100644
--- a/gio/src/actionmap.hg
+++ b/gio/src/actionmap.hg
@@ -84,10 +84,59 @@ public:
   Glib::RefPtr<SimpleAction> add_action(const Glib::ustring& name, const ActivateSlot& slot);
   _IGNORE(g_action_map_add_action_entries)
 
+  /** A convenience method for creating a boolean-stateful SimpleAction instance
+   * and adding it to the ActionMap.
+   *
+   * @param name The name of the Action.
+   * @param slot The callback method to be called when the action is activated.
+   * @return The Action.
+   */
   Glib::RefPtr<SimpleAction> add_action_bool(const Glib::ustring& name, bool state);
 
+  /** A convenience method for creating a boolean-stateful SimpleAction instance
+   * and adding it to the ActionMap.
+   *
+   * @param name The name of the Action.
+   * @param slot The callback method to be called when the action is activated.
+   * @return The Action.
+   */
   Glib::RefPtr<SimpleAction> add_action_bool(const Glib::ustring& name, bool state, const ActivateSlot& 
slot);
 
+  /** A convenience method for creating a string-based radio SimpleAction instance
+   * and adding it to the ActionMap.
+   *
+   * @param name The name of the Action.
+   * @return The Action.
+   */
+  Glib::RefPtr<SimpleAction> add_action_radio_string(const Glib::ustring& name);
+
+  /** A convenience method for creating a string-based radio SimpleAction instance
+   * and adding it to the ActionMap.
+   *
+   * @param name The name of the Action.
+   * @param slot The callback method to be called when the action is activated.
+   * @return The Action.
+   */
+  Glib::RefPtr<SimpleAction> add_action_radio_string(const Glib::ustring& name, const ActivateSlot& slot);
+
+  /** A convenience method for creating an integer-based radio SimpleAction instance
+   * and adding it to the ActionMap.
+   *
+   * @param name The name of the Action.
+   * @return The Action.
+   */
+  Glib::RefPtr<SimpleAction> add_action_radio_integer(const Glib::ustring& name);
+
+  /** A convenience method for creating an integer-based radio SimpleAction instance
+   * and adding it to the ActionMap.
+   *
+   * @param name The name of the Action.
+   * @param slot The callback method to be called when the action is activated.
+   * @return The Action.
+   */
+  Glib::RefPtr<SimpleAction> add_action_radio_integer(const Glib::ustring& name, const ActivateSlot& slot);
+
+
   _WRAP_METHOD(void add_action(const Glib::RefPtr<Action>& action), g_action_map_add_action)
   _WRAP_METHOD(void remove_action(const Glib::ustring& action_name), g_action_map_remove_action)
 
diff --git a/gio/src/simpleaction.ccg b/gio/src/simpleaction.ccg
index c10a4d7..ae90453 100644
--- a/gio/src/simpleaction.ccg
+++ b/gio/src/simpleaction.ccg
@@ -32,9 +32,22 @@ SimpleAction::SimpleAction(const Glib::ustring& name, const Glib::VariantBase& s
   _CONSTRUCT("name", name.c_str(), "state", const_cast<GVariant*>((state).gobj()))
 {}
 
-Glib::RefPtr<SimpleAction> SimpleAction::create_bool(const Glib::ustring&name, bool state)
+Glib::RefPtr<SimpleAction> SimpleAction::create_bool(const Glib::ustring& name, bool state)
 {
   return create(name, Glib::Variant<bool>::create(state));
 }
 
+
+Glib::RefPtr<SimpleAction> SimpleAction::create_radio_string(const Glib::ustring& name)
+{
+  //See https://developer.gnome.org/glib/stable/gvariant-format-strings.html#gvariant-format-strings-strings
+  return create(name, Glib::VariantType("s"));
+}
+
+Glib::RefPtr<SimpleAction> SimpleAction::create_radio_integer(const Glib::ustring& name)
+{
+  //See 
https://developer.gnome.org/glib/stable/gvariant-format-strings.html#gvariant-format-strings-numeric-types
+  return create(name, Glib::VariantType("i"));
+}
+
 } // namespace Gio
diff --git a/gio/src/simpleaction.hg b/gio/src/simpleaction.hg
index 3a5fb6e..744d8dd 100644
--- a/gio/src/simpleaction.hg
+++ b/gio/src/simpleaction.hg
@@ -92,6 +92,28 @@ public:
    */
   static Glib::RefPtr<SimpleAction> create_bool(const Glib::ustring&name, bool state);
 
+  //TODO: Use similar C API if they ever add it. Doing this manually is tedious.
+  //TODO: Create a derived SimpleToggleAction class for this?
+  //TODO: Suggest other API that could be used to activate or discover the currently-select radio value.
+  /** Creates a new radio action with a string-based target value.
+   * 
+   * @newin{2,38}
+   * @param name The name of the action.
+   * @return A new SimpleAction.
+   */
+  static Glib::RefPtr<SimpleAction> create_radio_string(const Glib::ustring& name);
+
+  //TODO: Use similar C API if they ever add it. Doing this manually is tedious.
+  //TODO: Create a derived SimpleToggleAction class for this?
+  //TODO: Suggest other API that could be used to activate or discover the currently-select radio value.
+  /** Creates a new radio action with an integer-based target value.
+   * 
+   * @newin{2,38}
+   * @param name The name of the action.
+   * @return A new SimpleAction.
+   */
+  static Glib::RefPtr<SimpleAction> create_radio_integer(const Glib::ustring& name);
+
   _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]