[ekiga/ds-gtk-application] ActorMenu: Changed API to be able to build a GMenuModel.



commit 5aabbb23748936781af42d8532398cbbb63f79fb
Author: Damien Sandras <dsandras beip be>
Date:   Sat Mar 29 15:21:20 2014 +0100

    ActorMenu: Changed API to be able to build a GMenuModel.
    
    This is better than playing with the XML code in the frontend.

 lib/engine/gui/gtk-core/actor-menu.cpp |   83 ++++++++++++++++++++++----------
 lib/engine/gui/gtk-core/actor-menu.h   |   50 +++++++++++++++++--
 2 files changed, 102 insertions(+), 31 deletions(-)
---
diff --git a/lib/engine/gui/gtk-core/actor-menu.cpp b/lib/engine/gui/gtk-core/actor-menu.cpp
index efe3fe5..90097c5 100644
--- a/lib/engine/gui/gtk-core/actor-menu.cpp
+++ b/lib/engine/gui/gtk-core/actor-menu.cpp
@@ -60,6 +60,9 @@ action_activated (GSimpleAction *a,
 
 Ekiga::ActorMenu::ActorMenu (Ekiga::Actor & _obj) : obj (_obj)
 {
+  n = 0;
+  builder = gtk_builder_new ();
+
   obj.action_enabled.connect (boost::bind (static_cast<void (Ekiga::ActorMenu::*)(const 
std::string&)>(&Ekiga::ActorMenu::add_gio_action), this, _1));
   obj.action_disabled.connect (boost::bind (&Ekiga::ActorMenu::remove_gio_action, this, _1));
 
@@ -74,6 +77,45 @@ Ekiga::ActorMenu::~ActorMenu ()
   for (it = obj.actions.begin(); it != obj.actions.end(); ++it)
     g_action_map_remove_action (G_ACTION_MAP (g_application_get_default ()),
                                 it->first.c_str ());
+
+  g_object_unref (builder);
+}
+
+
+void
+Ekiga::ActorMenu::activate (const std::string & name)
+{
+  ActionMap::const_iterator it;
+  if (!name.empty ()) {
+
+    it = obj.actions.find (name);
+    if (it != obj.actions.end ())
+      it->second->activate ();
+  }
+  else {
+
+    for (it = obj.actions.begin(); it != obj.actions.end(); ++it) {
+      if (it->second->is_enabled ()) {
+        it->second->activate ();
+        return;
+      }
+    }
+  }
+}
+
+
+GMenuModel *
+Ekiga::ActorMenu::get ()
+{
+  gtk_builder_add_from_string (builder, build ().c_str (), -1, NULL);
+  return (n > 0 ? G_MENU_MODEL (gtk_builder_get_object (builder, "menu")) : NULL);
+}
+
+
+unsigned
+Ekiga::ActorMenu::size ()
+{
+  return n;
 }
 
 
@@ -135,34 +177,11 @@ Ekiga::ActorMenu::remove_gio_action (const std::string & name)
 
 
 const std::string
-Ekiga::ActorMenu::get_xml_menu (const std::string & id,
-                                const std::string & content,
-                                bool full)
-{
-  std::string xml_content = "<menu id=\"" + id + "\">" + content + "</menu>";
-
-  if (full)
-    return "<?xml_content version=\"1.0\"?><interface>" + xml_content + "</interface>";
-
-  return xml_content;
-}
-
-
-void
-Ekiga::ActorMenu::activate (const std::string & name)
-{
-  ActionMap::const_iterator it = obj.actions.find (name);
-
-  if (it != obj.actions.end ())
-    it->second->activate ();
-}
-
-
-const std::string
 Ekiga::ActorMenu::as_xml (const std::string & id)
 {
   ActionMap::const_iterator it;
   std::string xml_content;
+  n = 0;
 
   if (!id.empty ())
    xml_content += "    <section id=\"" + id + "\">";
@@ -171,21 +190,33 @@ Ekiga::ActorMenu::as_xml (const std::string & id)
 
   for (it = obj.actions.begin(); it != obj.actions.end(); ++it) {
 
-    if (it->second->is_enabled ())
+    if (it->second->is_enabled ()) {
       xml_content +=
         "      <item>"
         "        <attribute name=\"label\" translatable=\"yes\">"+it->second->get_description 
()+"</attribute>"
         "        <attribute name=\"action\">win."+it->second->get_name ()+"</attribute>"
         "      </item>";
+      n++;
+    }
   }
 
   xml_content +=
     "    </section>";
 
-  return xml_content;
+  return (n > 0 ? xml_content : "");
+}
+
+
+const std::string
+Ekiga::ActorMenu::build ()
+{
+  std::string xml_content = "<menu id=\"menu\">" + as_xml () + "</menu>";
+
+  return "<?xml_content version=\"1.0\"?><interface>" + xml_content + "</interface>";
 }
 
 
+
 Ekiga::ContactActorMenu::ContactActorMenu (Ekiga::Actor & _obj) : ActorMenu (_obj)
 {
 }
diff --git a/lib/engine/gui/gtk-core/actor-menu.h b/lib/engine/gui/gtk-core/actor-menu.h
index 6ac815f..1f71b1d 100644
--- a/lib/engine/gui/gtk-core/actor-menu.h
+++ b/lib/engine/gui/gtk-core/actor-menu.h
@@ -41,6 +41,8 @@
 #include "action.h"
 #include "contact-action.h"
 
+#include <gtk/gtk.h>
+
 namespace Ekiga {
 
   /* ActorMenu
@@ -63,27 +65,65 @@ namespace Ekiga {
     ActorMenu (Actor & obj);
     virtual ~ActorMenu ();
 
-    virtual void activate (const std::string & name);
-    virtual const std::string as_xml (const std::string & id = "");
 
-    static const std::string get_xml_menu (const std::string & id,
-                                           const std::string & content,
-                                           bool full);
+    /** Activate the action of the given name. The first enabled action
+     *  (if any) is activated when no action name is given.
+     * @param name is the action name, or can be left empty.
+     */
+    virtual void activate (const std::string & name = "");
+
+
+    /** Return a pointer to the GMenuModel corresponding to the ActorMenu (if any).
+     * @return a pointer to the GMenuModel, NULL if none (no action, or all actions
+     *         are disabled).
+     */
+    GMenuModel *get ();
+
+
+    /** Return the number of actions of the ActorMenu. The counter is only
+     *  updated after get () has been called.
+     */
+    unsigned size ();
 
   protected:
+    /** Sync GIO actions with Actions */
     virtual void sync_gio_actions ();
+
+    /** Add GIO action with given name */
     virtual void add_gio_action (const std::string & name);
+
+    /** Add GIO action corresponding to the given Action */
     virtual void add_gio_action (ActionPtr a);
+
+    /** Remove GIO action with the given name */
     virtual void remove_gio_action (const std::string & name);
 
+    /** Return the XML representation of the enabled Actions */
+    virtual const std::string as_xml (const std::string & id = "");
+
+    /** Remove the XML representation of the full menu with
+     * enabled Actions.
+     */
+    virtual const std::string build ();
+
     Actor & obj;
+
+  private:
+    unsigned n;
+    GtkBuilder *builder;
   };
 
   class ContactActorMenu : public ActorMenu
   {
   public:
+
     ContactActorMenu (Actor & obj);
 
+    /** Set the data usable by the ContactActorMenu.
+     *  Available actions will depend on the data being set.
+     * @param a ContactPtr (if any).
+     * @param an uri (if any).
+     */
     void set_data (ContactPtr _contact = ContactPtr (),
                    const std::string & _uri = "");
   };


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