[gnome-boxes] Use MenuBox in WizardSource



commit a43f9df665f0428b4acd19de9cc90ee2a7b00d53
Author: Marc-Andrà Lureau <marcandre lureau gmail com>
Date:   Tue Nov 15 15:10:03 2011 +0100

    Use MenuBox in WizardSource

 data/gtk-style.css     |   17 ++++--
 src/menu-box.vala      |  121 +++++++++++++++++++++++++-----------------------
 src/wizard-source.vala |   57 +++++++++++++----------
 3 files changed, 106 insertions(+), 89 deletions(-)
---
diff --git a/data/gtk-style.css b/data/gtk-style.css
index c59308c..6569723 100644
--- a/data/gtk-style.css
+++ b/data/gtk-style.css
@@ -1,5 +1,5 @@
 @define-color boxes_bg_color shade (@theme_bg_color, 0.5);
- define-color boxes_sidebar_bg_color mix (@theme_bg_color, @boxes_bg_color, 0.5);
+ define-color boxes_bg2_color mix (@theme_bg_color, @boxes_bg_color, 0.5);
 @define-color boxes_selected_color #5f8dd3;
 
 #TopbarLabel {
@@ -12,6 +12,11 @@
     font-weight: bold;
 }
 
+.separator {
+    -GtkWidget-wide-separators: true;
+    -GtkWidget-separator-height: 1;
+}
+
 .view {
     background-color: @boxes_bg_color;
 /*
@@ -28,7 +33,7 @@
 }
 
 .boxes-sidebar-bg {
-    background-color: @boxes_sidebar_bg_color;
+    background-color: @boxes_bg2_color;
     background-image: url("/opt/gnome/share/gnome-boxes/pixmaps/boxes-gray.png");
     background-repeat: repeat;
 }
@@ -55,7 +60,7 @@
 }
 
 .boxes-source-nb {
-    border-radius: 15;
+    background-color: alpha(#fff, 0.0);
 }
 
 .boxes-selected {
@@ -134,8 +139,8 @@
 }
 
 BoxesMenuBox .menuitem {
-    background-color: @theme_base_color;
-    border-radius: 10;
+    background-color: @boxes_bg2_color;
+    border-radius: 15;
 }
 
 BoxesMenuBox .menuitem:focused {
@@ -143,5 +148,5 @@ BoxesMenuBox .menuitem:focused {
 }
 
 BoxesMenuBox .menuitem:prelight {
-    background-color: shade (@theme_base_color, 1.1);
+    background-color: shade (@boxes_bg2_color, 1.1);
 }
diff --git a/src/menu-box.vala b/src/menu-box.vala
index d4d0280..0b00cc6 100644
--- a/src/menu-box.vala
+++ b/src/menu-box.vala
@@ -3,83 +3,88 @@
 private class Boxes.MenuBox: Gtk.Box {
     public signal void selected (Gtk.Widget widget);
 
-    private class Child {
-        public Gtk.Widget widget;
-        public bool in_item;
+    public class Item: Gtk.EventBox {
+        private bool _in_item;
+        private bool _selectable;
+        private Gtk.Alignment alignment;
 
-        public Child (Gtk.Widget widget) {
-            this.widget = widget;
-        }
+        private bool in_item {
+            get { return _in_item; }
+            set {
+                _in_item = value;
 
-        public void update_state () {
-            var flags = widget.get_state_flags ();
+                var flags = this.get_state_flags ();
 
-            flags = flags & ~(Gtk.StateFlags.PRELIGHT | Gtk.StateFlags.ACTIVE);
-            if (in_item)
-                flags |= Gtk.StateFlags.PRELIGHT;
+                flags = flags & ~(Gtk.StateFlags.PRELIGHT | Gtk.StateFlags.ACTIVE);
+                if (in_item && selectable)
+                    flags |= Gtk.StateFlags.PRELIGHT;
 
-            widget.set_state_flags (flags, true);
+                set_state_flags (flags, true);
+            }
         }
-    }
 
-    private new int spacing;
-    private HashTable<Gtk.Widget, Child> children;
-    construct {
-        children = new HashTable<Gtk.Widget, Child> (GLib.direct_hash, GLib.direct_equal);
-    }
+        public bool selectable {
+            get { return _selectable; }
+            set {
+                _selectable = value;
+                can_focus = value;
+            }
+        }
+
+        construct {
+            get_style_context ().add_class ("menuitem");
+            visible_window = false;
 
-    public MenuBox (Gtk.Orientation orientation, int spacing = 10) {
-        GLib.Object (orientation: orientation);
-        this.spacing = spacing;
+            alignment = new Gtk.Alignment (0.0f, 0.0f, 1.0f, 1.0f);
+            child = alignment;
+
+            enter_notify_event.connect (() => {
+                in_item = true;
+                return true;
+            });
+
+            leave_notify_event.connect (() => {
+                in_item = false;
+                return true;
+            });
+        }
+
+        public Item (Gtk.Widget child, bool selectable = false) {
+            alignment.add (child);
+            this.selectable = selectable;
+        }
     }
 
-    public override void remove (Gtk.Widget widget) {
-        children.remove (widget);
-        base.remove (widget);
+    public MenuBox (Gtk.Orientation orientation, int spacing = 0) {
+        GLib.Object (orientation: orientation, spacing: spacing);
     }
 
     public override void add (Gtk.Widget widget) {
-        var eventbox = new Gtk.EventBox ();
-        eventbox.get_style_context ().add_class ("menuitem");
-        eventbox.visible_window = false;
-        eventbox.can_focus = true;
-
-        var child = new Child (eventbox);
-        children.insert (eventbox, child);
-
-        eventbox.enter_notify_event.connect (() => {
-            child.in_item = true;
-            child.update_state ();
-            return true;
-        });
-
-        eventbox.leave_notify_event.connect (() => {
-            child.in_item = false;
-            child.update_state ();
-            return true;
-        });
-        eventbox.button_press_event.connect (() => {
-            eventbox.grab_focus ();
-            selected (widget);
-            return true;
-        });
-
-        var alignment = new Gtk.Alignment (0.0f, 0.0f, 1.0f, 1.0f);
-        alignment.add (widget);
-        alignment.margin_top = 10;
-        alignment.margin_bottom = 10;
-        eventbox.add (alignment);
-        base.add (eventbox);
+        var item  = widget as Item;
+
+        if (item != null) {
+            item.button_press_event.connect (() => {
+                item.grab_focus ();
+                selected (item);
+                return true;
+            });
+        }
+
+        base.add (widget);
     }
 
     public override bool draw (Cairo.Context cr) {
         foreach (var child in get_children ()) {
             int position = 0;
+            Gtk.Allocation child_allocation;
             Gtk.Allocation allocation;
             child_get (child, "position", ref position);
             bool last = get_children ().length () == (position + 1);
 
-            child.get_allocation (out allocation);
+            child.get_allocation (out child_allocation);
+            get_allocation (out allocation);
+            child_allocation.x -= allocation.x;
+            child_allocation.y -= allocation.y;
 
             var ctxt = child.get_style_context ();
             Gtk.JunctionSides junction = 0;
@@ -98,8 +103,8 @@ private class Boxes.MenuBox: Gtk.Box {
 
             ctxt.set_state (child.get_state_flags ());
             ctxt.set_junction_sides (junction);
-            ctxt.render_background (cr, allocation.x, allocation.y,
-                                    allocation.width, allocation.height);
+            ctxt.render_background (cr, child_allocation.x, child_allocation.y,
+                                    child_allocation.width, child_allocation.height);
         }
 
         return base.draw (cr);
diff --git a/src/wizard-source.vala b/src/wizard-source.vala
index 18e5aae..f5dae9a 100644
--- a/src/wizard-source.vala
+++ b/src/wizard-source.vala
@@ -18,8 +18,15 @@ private class Boxes.WizardSource: GLib.Object {
         set {
             _page = value;
             notebook.set_current_page (page);
-            if (page == SourcePage.URL)
+            switch (page) {
+            case SourcePage.MAIN:
+                menubox.grab_focus ();
+                break;
+            case SourcePage.URL:
                 url_entry.changed ();
+                url_entry.grab_focus ();
+                break;
+            }
         }
     }
     public string uri {
@@ -27,6 +34,7 @@ private class Boxes.WizardSource: GLib.Object {
         set { url_entry.set_text (value); }
     }
 
+    private Boxes.MenuBox menubox;
     private Gtk.Notebook notebook;
     public Gtk.Entry url_entry;
 
@@ -36,11 +44,12 @@ private class Boxes.WizardSource: GLib.Object {
         notebook.show_tabs = false;
 
         /* main page */
-        var vbox = new Gtk.VBox (false, 10);
-        vbox.margin_top = vbox.margin_bottom = 15;
-        notebook.append_page (vbox, null);
+        menubox = new Boxes.MenuBox (Gtk.Orientation.VERTICAL);
+        menubox.margin_top = menubox.margin_bottom = 15;
+        menubox.grab_focus ();
+        notebook.append_page (menubox, null);
 
-        var hbox = add_entry (vbox, () => { page = SourcePage.URL; });
+        var hbox = add_entry (menubox, () => { page = SourcePage.URL; });
         var label = new Gtk.Label (_("Enter URL"));
         label.xalign = 0.0f;
         hbox.pack_start (label, true, true);
@@ -48,10 +57,9 @@ private class Boxes.WizardSource: GLib.Object {
         hbox.pack_start (next, false, false);
 
         var separator = new Gtk.Separator (Gtk.Orientation.HORIZONTAL);
-        separator.height_request = 5;
-        vbox.pack_start (separator, false, false);
+        menubox.pack_start (separator, false, false);
 
-        hbox = add_entry (vbox, () => { page = SourcePage.FILE; });
+        hbox = add_entry (menubox, () => { page = SourcePage.FILE; });
         label = new Gtk.Label (_("Select a file"));
         label.xalign = 0.0f;
         hbox.pack_start (label, true, true);
@@ -59,23 +67,22 @@ private class Boxes.WizardSource: GLib.Object {
         hbox.pack_start (next, false, false);
 
         /* URL page */
-        vbox = new Gtk.VBox (false, 10);
-        vbox.margin_top = vbox.margin_bottom = 15;
-        notebook.append_page (vbox, null);
+        menubox = new Boxes.MenuBox (Gtk.Orientation.VERTICAL);
+        menubox.margin_top = menubox.margin_bottom = 15;
+        notebook.append_page (menubox, null);
 
-        hbox = add_entry (vbox, () => { page = SourcePage.MAIN; });
+        hbox = add_entry (menubox, () => { page = SourcePage.MAIN; });
         var prev = new Gtk.Label ("â");
         hbox.pack_start (prev, false, false);
         label = new Gtk.Label (_("Enter URL"));
         label.get_style_context ().add_class ("boxes-source-label");
         hbox.pack_start (label, true, true);
         separator = new Gtk.Separator (Gtk.Orientation.HORIZONTAL);
-        separator.height_request = 5;
-        vbox.pack_start (separator, false, false);
-        hbox = add_entry (vbox);
+        menubox.add (separator);
+        hbox = add_entry (menubox);
         url_entry = new Gtk.Entry ();
         hbox.add (url_entry);
-        hbox = add_entry (vbox);
+        hbox = add_entry (menubox);
         var image = new Gtk.Image.from_icon_name ("network-workgroup", 0);
         // var image = new Gtk.Image.from_icon_name ("krfb", 0);
         image.pixel_size = 96;
@@ -90,18 +97,18 @@ private class Boxes.WizardSource: GLib.Object {
         notebook.show_all ();
     }
 
-    private Gtk.HBox add_entry (Gtk.VBox vbox, ClickedFunc? clicked = null) {
-        var ebox = new Gtk.EventBox ();
-        ebox.visible_window = false;
+    private Gtk.HBox add_entry (MenuBox box, ClickedFunc? clicked = null) {
         var hbox = new Gtk.HBox (false, 20);
-        ebox.add (hbox);
-        vbox.pack_start (ebox, false, false);
+        var item = new MenuBox.Item (hbox);
+        box.add (item);
         hbox.margin_left = hbox.margin_right = 20;
+        hbox.margin_top = hbox.margin_bottom = 10;
+
         if (clicked != null) {
-            ebox.add_events (Gdk.EventMask.BUTTON_PRESS_MASK);
-            ebox.button_press_event.connect (() => {
-                clicked ();
-                return true;
+            item.selectable = true;
+            box.selected.connect ((widget) => {
+                if (widget == item)
+                    clicked ();
             });
         }
 



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