[baobab/wip/valacharts] Charts: implement context menu



commit a55fae987fcf1c40c0363f875e389dac13b91c31
Author: Stefano Facchini <stefano facchini gmail com>
Date:   Tue Jun 11 13:16:55 2013 +0200

    Charts: implement context menu
    
    Still not working

 src/baobab-chart.vala |   54 ++++++++++++++++++++++++++++++++++++++++++------
 src/baobab-menu.ui    |   18 ++++++++++++++++
 2 files changed, 65 insertions(+), 7 deletions(-)
---
diff --git a/src/baobab-chart.vala b/src/baobab-chart.vala
index 798ecf2..733ef93 100644
--- a/src/baobab-chart.vala
+++ b/src/baobab-chart.vala
@@ -38,6 +38,8 @@ namespace Baobab {
 
         bool model_changed;
 
+        Gtk.Menu context_menu = null;
+
         List<ChartItem> items;
 
         uint max_depth_ = MAX_DEPTH;
@@ -140,8 +142,6 @@ namespace Baobab {
             }
         }
 
-        Gtk.Widget popup_menu;
-
         public virtual signal void item_activated (Gtk.TreeIter iter) {
             root = model.get_path (iter);
         }
@@ -161,6 +161,19 @@ namespace Baobab {
 
         protected abstract ChartItem create_new_chartitem ();
 
+        SimpleActionGroup action_group;
+
+        const ActionEntry[] action_entries = {
+            { "move-up", move_up_root },
+            { "zoom-in", zoom_in },
+            { "zoom-out", zoom_out }
+        };
+
+        public Chart () {
+            action_group = new SimpleActionGroup ();
+            action_group.add_action_entries (action_entries, this);
+        }
+
         public override void realize () {
             Gtk.Allocation allocation;
             get_allocation (out allocation);
@@ -184,11 +197,6 @@ namespace Baobab {
             get_style_context ().set_background (window);
         }
 
-        public override void unrealize () {
-            // destroy popup menu...
-            base.unrealize ();
-        }
-
         public override void size_allocate (Gtk.Allocation allocation) {
             set_allocation (allocation);
             if (get_realized ()) {
@@ -456,6 +464,11 @@ namespace Baobab {
 
         protected override bool button_press_event (Gdk.EventButton event) {
             if (event.type == Gdk.EventType.BUTTON_PRESS) {
+                if (((Gdk.Event) (&event)).triggers_context_menu ()) {
+                    show_popup_menu (event);
+                    return true;
+                }
+
                 switch (event.button) {
                 case 1:
                     if (highlighted_item != null) {
@@ -517,5 +530,32 @@ namespace Baobab {
                 max_depth++;
             }
         }
+
+        void ensure_context_menu () {
+            if (context_menu != null) {
+                return;
+            }
+
+            var builder = new Gtk.Builder ();
+            try {
+                builder.add_from_resource ("/org/gnome/baobab/ui/baobab-menu.ui");
+            } catch (Error e) {
+                error ("loading context menu from resources: %s", e.message);
+            }
+
+            var menu_model = builder.get_object ("chartmenu") as MenuModel;
+            context_menu = new Gtk.Menu.from_model (menu_model);
+            context_menu.attach_to_widget (this, null);
+        }
+
+        void show_popup_menu (Gdk.EventButton? event) {
+            ensure_context_menu ();
+
+            if (event != null) {
+                context_menu.popup (null, null, null, event.button, event.time);
+            } else {
+                context_menu.popup (null, null, null, 0, Gtk.get_current_event_time ());
+            }
+        }
     }
 }
\ No newline at end of file
diff --git a/src/baobab-menu.ui b/src/baobab-menu.ui
index 75566d1..6b44a72 100644
--- a/src/baobab-menu.ui
+++ b/src/baobab-menu.ui
@@ -83,4 +83,22 @@
       </section>
     </submenu>
   </menu>
+  <menu id="chartmenu">
+    <section>
+      <item>
+        <attribute name="label" translatable="yes">_Move to parent folder</attribute>
+        <attribute name="action">move-up</attribute>
+      </item>
+    </section>
+    <section>
+      <item>
+        <attribute name="label" translatable="yes">Zoom _in</attribute>
+        <attribute name="action">zoom-in</attribute>
+      </item>
+      <item>
+        <attribute name="label" translatable="yes">Zoom _out</attribute>
+        <attribute name="action">zoom-out</attribute>
+      </item>
+    </section>
+  </menu>
 </interface>


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