← Back to team overview

elementaryart team mailing list archive

[Merge] lp:~victored/granite/utils into lp:granite

 

Victor Eduardo has proposed merging lp:~victored/granite/utils into lp:granite.

Requested reviews:
  elementary Pantheon team (elementary-pantheon)
Related bugs:
  Bug #1017231 in Granite: "modebutton returning a wrong index"
  https://bugs.launchpad.net/granite/+bug/1017231
  Bug #1021578 in Granite: "use header fonts in Welcome"
  https://bugs.launchpad.net/granite/+bug/1021578

For more details, see:
https://code.launchpad.net/~victored/granite/utils/+merge/121113

Move common code to Granite.Widgets.Utils, fix mode-button scrolling and add support for elementary's fonts
-- 
https://code.launchpad.net/~victored/granite/utils/+merge/121113
Your team elementaryart (old) is subscribed to branch lp:granite.
=== modified file 'lib/CMakeLists.txt'
--- lib/CMakeLists.txt	2012-08-22 22:21:28 +0000
+++ lib/CMakeLists.txt	2012-08-24 01:58:19 +0000
@@ -61,6 +61,7 @@
     Services/System.vala
     Services/Contractor.vala
     Services/IconFactory.vala
+    Widgets/Utils.vala
     Widgets/WrapLabel.vala
     Widgets/AboutDialog.vala
     Widgets/ModeButton.vala

=== modified file 'lib/GtkPatch/AboutDialog.vala'
--- lib/GtkPatch/AboutDialog.vala	2012-07-29 21:12:08 +0000
+++ lib/GtkPatch/AboutDialog.vala	2012-08-24 01:58:19 +0000
@@ -235,11 +235,6 @@
     private Label website_url_label;
     private Button close_button;
 
-    // Set the markup used for big text (program name and version)
-    private const string BIG_TEXT_CSS = """
-    .h2 { font: open sans light 18; }
-    """;
-
     private const string STYLESHEET = """
         * {
             -GtkDialog-action-area-border: 12px;
@@ -259,20 +254,9 @@
         deletable = false; // Hide the window's close button when possible
         set_default_response(ResponseType.CANCEL);
 
-        var style_provider = new CssProvider ();
-
-        try {
-            style_provider.load_from_data (STYLESHEET, -1);
-        } catch (Error e) {
-            warning ("GraniteAboutDialog: %s. The widget will not look as intended.", e.message);
-        }
-
-        get_style_context().add_provider(style_provider, STYLE_PROVIDER_PRIORITY_APPLICATION);
-
-        var title_css = new Gtk.CssProvider ();
-        try {
-            title_css.load_from_data (BIG_TEXT_CSS, -1);
-        } catch (Error e) { warning (e.message); }
+
+        Granite.Widgets.Utils.set_theming (this, STYLESHEET, null,
+                                           Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION);
 
         // Set the default containers
         Box content_area = (Box)get_content_area();
@@ -304,8 +288,8 @@
         name_label.halign = Gtk.Align.START;
         name_label.set_line_wrap(true);
         name_label.set_selectable(true);
-        name_label.get_style_context ().add_provider (title_css, Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION);
-        name_label.get_style_context ().add_class ("h2");
+
+        Granite.Widgets.Utils.apply_text_style_to_label (TextStyle.H2, name_label);
 
         copyright_label = new Label("");
         copyright_label.set_selectable(true);

=== modified file 'lib/Widgets/AboutDialog.vala'
--- lib/Widgets/AboutDialog.vala	2012-07-12 16:04:33 +0000
+++ lib/Widgets/AboutDialog.vala	2012-08-24 01:58:19 +0000
@@ -86,15 +86,6 @@
         {
             Box action_area = (Box) get_action_area ();
             
-            /* help button style */
-            var help_button_style_provider = new CssProvider();
-            try {
-                help_button_style_provider.load_from_data(HELP_BUTTON_STYLESHEET, -1);
-            }
-            catch (Error e) {
-                warning ("%s. Some widgets will not look as intended", e.message);
-            }
-
             var draw_ref = new Gtk.Window ();
             draw_ref.get_style_context ().add_class (STYLE_CLASS_CONTENT_VIEW_WINDOW);
 
@@ -114,11 +105,13 @@
             this.get_content_area ().margin_bottom = 3;
 
             /* help button */
-            help_button = new Button.with_label("?");
-            help_button.get_style_context ().add_class ("help_button");
-            help_button.get_style_context ().add_provider (help_button_style_provider, STYLE_PROVIDER_PRIORITY_APPLICATION);
+            help_button = new Button.with_label ("?");
+
+            Utils.set_theming (help_button, HELP_BUTTON_STYLESHEET, "help_button",
+                               Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION);
+
             help_button.halign = Gtk.Align.CENTER;
-            help_button.pressed.connect(() => { activate_link(help); });
+            help_button.clicked.connect (() => { activate_link(help); });
 
             /* Circular help button */
             help_button.size_allocate.connect ( (alloc) => {
@@ -131,12 +124,12 @@
 
             /* translate button */
             translate_button = new Button.with_label(_("Translate This App"));
-            translate_button.pressed.connect ( () => { activate_link(translate); });
+            translate_button.clicked.connect ( () => { activate_link(translate); });
             action_area.pack_start (translate_button, false, false, 0);
 
             /* bug button */
             bug_button = new Button.with_label (_("Report a Problem"));
-            bug_button.pressed.connect (() => {             
+            bug_button.clicked.connect (() => {             
                 try {
                     GLib.Process.spawn_command_line_async ("apport-bug %i".printf (Posix.getpid ()));
                 } catch (Error e) {

=== modified file 'lib/Widgets/CompositedWindow.vala'
--- lib/Widgets/CompositedWindow.vala	2012-01-25 02:39:54 +0000
+++ lib/Widgets/CompositedWindow.vala	2012-08-24 01:58:19 +0000
@@ -22,31 +22,20 @@
 
     public class CompositedWindow : Gtk.Window, Gtk.Buildable {
 
-        private CssProvider style_provider;
-
-        private const string COMPOSITED_WINDOW_STYLESHEET = """
-            .composited {
-                background-color: rgba (0, 0, 0, 0.0);
-            }
-        """;
+        private const string STYLESHEET =
+            ".composited { background-color: rgba (0,0,0,0); }";
 
         construct {
-
-            // Set up css provider
-            style_provider = new CssProvider ();
-            try {
-                style_provider.load_from_data (COMPOSITED_WINDOW_STYLESHEET, -1);
-            } catch (Error e) {
-                warning ("GraniteWidgetsCompositedWindow: %s", e.message);
-            }
-
             // Window properties
-            set_visual (get_screen ().get_rgba_visual());
-            get_style_context().add_class("composited");
-            get_style_context ().add_provider (style_provider, 600);
             app_paintable = true;
             decorated = false;
             resizable = false;
+
+            set_visual (get_screen ().get_rgba_visual());
+
+            // Set up css provider
+            Utils.set_theming (this, STYLESHEET, "composited",
+                               Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION);
         }
     }
 }

=== modified file 'lib/Widgets/DecoratedWindow.vala'
--- lib/Widgets/DecoratedWindow.vala	2012-07-07 06:28:43 +0000
+++ lib/Widgets/DecoratedWindow.vala	2012-08-24 01:58:19 +0000
@@ -22,12 +22,9 @@
 
 namespace Granite.Widgets {
 
-    [CCode (cname="get_close_pixbuf")]
-    public extern Gdk.Pixbuf get_close_pixbuf ();
-
     public class DecoratedWindow : CompositedWindow {
 
-        static const string DECORATED_WINDOW_FALLBACK_STYLESHEET = """
+        const string DECORATED_WINDOW_FALLBACK_STYLESHEET = """
             .decorated-window {
                 border-style:solid;
                 border-color:alpha (#000, 0.35);
@@ -38,27 +35,18 @@
         """;
 
         // Currently not overridable
-        static const string DECORATED_WINDOW_STYLESHEET = """
+        const string DECORATED_WINDOW_STYLESHEET = """
             .decorated-window { border-width:1px; }
         """;
 
         public static void set_default_theming (Gtk.Window ref_window) {
-            var normal_style = new Gtk.CssProvider ();
-            var fallback_style = new Gtk.CssProvider ();
-
-            try {
-                normal_style.load_from_data (DECORATED_WINDOW_STYLESHEET, -1);
-                fallback_style.load_from_data (DECORATED_WINDOW_FALLBACK_STYLESHEET, -1);
-            } catch (Error e) {
-                warning (e.message);
-            }
-
-            ref_window.get_style_context ().add_class (STYLE_CLASS_DECORATED_WINDOW);
-
-            ref_window.get_style_context ().add_provider (normal_style,
-                                            Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION);
-            ref_window.get_style_context ().add_provider (fallback_style,
-                                            Gtk.STYLE_PROVIDER_PRIORITY_FALLBACK);
+            Utils.set_theming (ref_window, DECORATED_WINDOW_STYLESHEET,
+                               STYLE_CLASS_DECORATED_WINDOW,
+                               Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION);
+
+            Utils.set_theming (ref_window, DECORATED_WINDOW_FALLBACK_STYLESHEET,
+                               STYLE_CLASS_DECORATED_WINDOW,
+                               Gtk.STYLE_PROVIDER_PRIORITY_FALLBACK);
         }
 
         public bool show_title { get; set; default = true; }
@@ -88,7 +76,7 @@
             this.has_resize_grip = false;
             this.window_position = Gtk.WindowPosition.CENTER_ON_PARENT;
 
-            this.close_img = get_close_pixbuf ();
+            this.close_img = Utils.get_close_pixbuf ();
 
             this._title = new Gtk.Label (null);
             this._title.halign = Gtk.Align.CENTER;

=== modified file 'lib/Widgets/ModeButton.vala'
--- lib/Widgets/ModeButton.vala	2012-02-13 03:41:28 +0000
+++ lib/Widgets/ModeButton.vala	2012-08-24 01:58:19 +0000
@@ -23,18 +23,22 @@
 
     public class ModeButton : Gtk.Box {
 
+        private class Item : Gtk.ToggleButton {
+            public Item () {
+                can_focus = false;
+                Utils.set_theming (this, ModeButton.STYLESHEET, "raised",
+                                   ModeButton.STYLE_PRIORITY);
+            }
+        }
+
         public signal void mode_added (int index, Gtk.Widget widget);
         public signal void mode_removed (int index, Gtk.Widget widget);
         public signal void mode_changed (Gtk.Widget widget);
 
-        // Style properties. Please note that style class names are for internal
-        // use only. Theme developers should use GraniteWidgetsModeButton instead.
-        internal static CssProvider style_provider;
-        internal static StyleContext widget_style;
-        private const int style_priority = Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION;
+        private const int STYLE_PRIORITY = Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION;
 
         private const string STYLESHEET = """
-            .GraniteModeButton .button {
+            .mode-button .button {
                 -GtkToolbar-button-relief: normal;
                 border-radius: 0 0 0 0;
                 border-style: solid;
@@ -44,19 +48,19 @@
                 -unico-outer-stroke-radius: 0 0 0 0;
             }
 
-            .GraniteModeButton .button:active,
-            .GraniteModeButton .button:insensitive {
+            .mode-button .button:active,
+            .mode-button .button:insensitive {
                 -unico-outer-stroke-width: 1px 0 1px 0;
             }
 
-            .GraniteModeButton .button:first-child {
+            .mode-button .button:first-child {
                 border-radius: 3px 0 0 3px;
                 border-width: 1px 0 1px 1px;
 
                 -unico-outer-stroke-width: 1px 0 1px 1px;
             }
 
-            .GraniteModeButton .button:last-child {
+            .mode-button .button:last-child {
                 border-radius: 0 3px 3px 0;
                 border-width: 1px;
 
@@ -82,44 +86,22 @@
         }
 
         public ModeButton () {
-
-            if (style_provider == null)
-            {
-                style_provider = new CssProvider ();
-                try {
-                    style_provider.load_from_data (STYLESHEET, -1);
-                } catch (Error e) {
-                    warning ("GraniteModeButton: %s. The widget will not look as intended", e.message);
-                }
-            }
-
-            widget_style = get_style_context ();
-            widget_style.add_class ("GraniteModeButton");
+            Utils.set_theming (this, STYLESHEET, "mode-button", STYLE_PRIORITY);
+
+            set_visual (get_screen ().get_rgba_visual ());
 
             homogeneous = true;
             spacing = 0;
             app_paintable = true;
-            set_visual (get_screen ().get_rgba_visual ());
-
             can_focus = true;
         }
 
-        public int append_pixbuf (Gdk.Pixbuf? pixbuf) {
-            if (pixbuf == null) {
-                warning ("GraniteWidgetsModeButton: Attempt to add null pixbuf failed.");
-                return -1;
-            }
-
+        public int append_pixbuf (Gdk.Pixbuf pixbuf) {
             var image = new Image.from_pixbuf (pixbuf);
             return append (image);
         }
 
-        public int append_text (string? text) {
-            if (text == null) {
-                warning ("GraniteWidgetsModeButton: Attempt to add null text string failed.");
-                return -1;
-            }
-
+        public int append_text (string text) {
             return append (new Gtk.Label(text));
         }
 
@@ -134,12 +116,9 @@
         }
 
         public int append (Gtk.Widget w) {
-            if (w == null) {
-                warning ("GraniteWidgetsModeButton: Attempt to add null widget failed.");
-                return -1;
-            }
-
-            var button = new ModeButtonItem ();
+            var button = new Item ();
+            button.add_events (Gdk.EventMask.SCROLL_MASK);
+            button.scroll_event.connect (on_scroll_event);
 
             button.add (w);
 
@@ -152,7 +131,7 @@
             add (button);
             button.show_all ();
 
-            int item_index = (int)get_children ().length ();
+            int item_index = (int)get_children ().length () - 1;
             mode_added (item_index, w); // Emit the added signal
             return item_index;
         }
@@ -194,27 +173,20 @@
             _selected = -1;
         }
 
-        protected override bool scroll_event (EventScroll ev) {
-            if (ev.direction == Gdk.ScrollDirection.DOWN) {
-                selected ++;
-            }
-            else if (ev.direction == Gdk.ScrollDirection.UP) {
-                selected --;
+        private bool on_scroll_event (Gtk.Widget widget, Gdk.EventScroll ev) {
+            switch (ev.direction) {
+                case Gdk.ScrollDirection.DOWN:
+                case Gdk.ScrollDirection.RIGHT:
+                    selected ++;
+                    break;
+                case Gdk.ScrollDirection.UP:
+                case Gdk.ScrollDirection.LEFT:
+                    selected --;
+                    break;
             }
 
             return false;
         }
     }
-
-    private class ModeButtonItem : Gtk.ToggleButton {
-        public ModeButtonItem () {
-            can_focus = false;
-
-            const int style_priority = Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION;
-
-            get_style_context ().add_class ("raised");
-            get_style_context ().add_provider (ModeButton.style_provider, style_priority);
-        }
-    }
 }
 

=== modified file 'lib/Widgets/PopOver.vala'
--- lib/Widgets/PopOver.vala	2012-08-17 21:18:22 +0000
+++ lib/Widgets/PopOver.vala	2012-08-24 01:58:19 +0000
@@ -47,7 +47,6 @@
     double offset = 15.0;
     const int MARGIN = 12;
     Gtk.Widget menu;
-    static Gtk.CssProvider style_provider;
     Gtk.Box hbox;
     Gtk.Box abox;
 
@@ -103,20 +102,14 @@
     }
 
     construct {
-        // Set up css provider
-        style_provider = new Gtk.CssProvider ();
-        try {
-            style_provider.load_from_data (POPOVER_STYLESHEET, -1);
-        } catch (Error e) {
-            warning ("GranitePopOver: %s. The widget will not look as intended.", e.message);
-        }
-
         // Window properties
         set_visual (get_screen ().get_rgba_visual());
 
         get_style_context ().add_class ("popover");
         get_style_context ().add_class ("composited");
-        get_style_context ().add_provider_for_screen (get_screen(), style_provider, 600);
+
+        Utils.set_theming_for_screen (get_screen (), POPOVER_STYLESHEET,
+                                      Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION);
 
         app_paintable = true;
         decorated = false;

=== modified file 'lib/Widgets/SidebarPaned.vala'
--- lib/Widgets/SidebarPaned.vala	2012-08-16 00:20:46 +0000
+++ lib/Widgets/SidebarPaned.vala	2012-08-24 01:58:19 +0000
@@ -20,12 +20,12 @@
  * Authored by: Victor Eduardo <victoreduardm@xxxxxxxxx>
  */
 
-public class Granite.Widgets.SidebarPaned : Gtk.Overlay, Gtk.Orientable {
+public class Granite.Widgets.SidebarPaned : Gtk.EventBox, Gtk.Orientable {
 
     protected Gtk.Paned paned { get; private set; }
-    private Gtk.EventBox? handle = null;
+    private Gtk.Overlay overlay;
+    private Gtk.EventBox handle;
     private bool on_resize_mode = false;
-    private Gdk.Cursor? arrow_cursor = null;
 
     static const string STYLE_PROP_HANDLE_SIZE = "handle-size";
 
@@ -80,8 +80,17 @@
         this.paned.add2 (child);
     }
 
-    public new void remove (Gtk.Widget child) {
-        this.paned.remove (child);
+    public new void remove (Gtk.Widget widget) {
+        this.paned.remove (widget);
+    }
+
+    public new void add (Gtk.Widget widget) {
+        if (get_child1 () == null)
+            add1 (widget);
+        else if (get_child2 () == null)
+            add2 (widget);
+        else
+            critical ("Container supports a maximum of two children");
     }
 
     public unowned Gtk.Widget? get_child1 () {
@@ -96,6 +105,14 @@
         return this.handle.get_window ();
     }
 
+    public new void foreach (Gtk.Callback callback) {
+        this.paned.foreach (callback);
+    }
+
+    public new void forall (Gtk.Callback callback) {
+        this.paned.forall (callback);
+    }
+
     public new void set_direction (Gtk.TextDirection dir) {
         this.paned.set_direction (dir);
         base.set_direction (dir);
@@ -106,15 +123,6 @@
         return this.paned.get_direction ();
     }
 
-    public new void set_default_direction (Gtk.TextDirection dir) {
-        base.set_default_direction (dir);
-        this.paned.set_default_direction (dir);
-    }
-
-    public new Gtk.TextDirection get_default_direction () {
-        return this.paned.get_default_direction ();
-    }
-
     public SidebarPaned () {
         this.paned.get_style_context ().add_class ("sidebar-pane-separator");
 
@@ -131,8 +139,10 @@
             }
         """;
 
-        set_theming (this.paned, DEFAULT_STYLESHEET, Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION);
-        set_theming (this.paned, FALLBACK_STYLESHEET, Gtk.STYLE_PROVIDER_PRIORITY_THEME);
+        Utils.set_theming (this.paned, DEFAULT_STYLESHEET, "",
+                           Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION);
+        Utils.set_theming (this.paned, FALLBACK_STYLESHEET, "",
+                           Gtk.STYLE_PROVIDER_PRIORITY_THEME);
     }
 
 
@@ -142,19 +152,25 @@
 
     construct {
         push_composite_child ();
+        this.overlay = new Gtk.Overlay ();
+        this.overlay.set_composite_name ("overlay");
+        pop_composite_child ();
+
+        push_composite_child ();
         this.paned = new Gtk.Paned (Gtk.Orientation.HORIZONTAL);
         this.paned.set_composite_name ("paned");
         pop_composite_child ();
 
         this.paned.expand = true;
 
-        this.add (this.paned);
-
         Gdk.RGBA transparent = { 0.0, 0.0, 0.0, 0.0 };
-        this.override_background_color (0, transparent);
+        overlay.override_background_color (0, transparent);
 
         setup_handle ();
 
+        this.overlay.add (this.paned);
+        base.add (this.overlay);
+
         this.paned.size_allocate.connect_after (on_paned_size_allocate);
 
         // The virtual handle will always follow the paned's position
@@ -181,7 +197,7 @@
         Gdk.RGBA transparent = { 0.0, 0.0, 0.0, 0.0 };
         this.handle.override_background_color (0, transparent);
 
-        this.add_overlay (handle);
+        overlay.add_overlay (handle);
 
         this.handle.add_events (Gdk.EventMask.BUTTON_PRESS_MASK
                                | Gdk.EventMask.BUTTON_RELEASE_MASK);
@@ -189,29 +205,21 @@
         this.handle.button_press_event.connect (on_handle_button_press);
         this.handle.button_release_event.connect (on_handle_button_release);
         this.handle.grab_broken_event.connect (on_handle_grab_broken);
-        this.handle.realize.connect (set_arrow_cursor);
-    }
-
-    protected static void set_theming (Gtk.Widget widget, string stylesheet, int priority) {
-        var css_provider = new Gtk.CssProvider ();
-
-        try {
-            css_provider.load_from_data (stylesheet, -1);
-        }
-        catch (Error e) {
-            warning (e.message);
-            return_if_reached ();
-        }
-
-        widget.get_style_context ().add_provider (css_provider, priority);
+        this.handle.realize.connect (set_handle_cursor);
     }
 
     public override bool motion_notify_event (Gdk.EventMotion e) {
         var device = e.device ?? Gtk.get_current_event_device ();
 
         if (device == null) {
-            var dev_manager = this.paned.get_display ().get_device_manager ();
-            device = dev_manager.list_devices (Gdk.DeviceType.MASTER).nth_data (0);
+            var display = this.paned.get_display ();
+
+            if (display != null) {
+                var dev_manager = display.get_device_manager ();
+
+                if (dev_manager != null)
+                    device = dev_manager.list_devices (Gdk.DeviceType.MASTER).nth_data (0);
+            }
         }
 
         if (this.on_resize_mode && device != null) {
@@ -222,7 +230,7 @@
                 window.get_device_position (device, out x, out y, null);
 
                 if (this.orientation == Gtk.Orientation.HORIZONTAL)
-                    pos = is_ltr() ? x : this.paned.get_allocated_width() - x;
+                    pos = is_ltr () ? x : this.paned.get_allocated_width () - x;
                 else
                     pos = y;
 
@@ -239,8 +247,10 @@
 
     private bool is_ltr () {
         var dir = get_direction ();
+
         if (dir == Gtk.TextDirection.NONE)
             dir = get_default_direction ();
+
         return dir == Gtk.TextDirection.LTR;
     }
 
@@ -253,12 +263,10 @@
         this.handle.set_size_request (0, 0);
 
         if (horizontal) {
-            this.arrow_cursor = new Gdk.Cursor (Gdk.CursorType.SB_H_DOUBLE_ARROW);
             this.handle.margin_top = this.handle.margin_bottom = 0;
             this.handle.halign = Gtk.Align.START;
             this.handle.valign = Gtk.Align.FILL;
         } else {
-            this.arrow_cursor = new Gdk.Cursor (Gdk.CursorType.SB_V_DOUBLE_ARROW);
             this.handle.margin_left = this.handle.margin_right = 0;
             this.handle.halign = Gtk.Align.FILL;
             this.handle.valign = Gtk.Align.START;
@@ -268,7 +276,7 @@
         update_virtual_handle_position ();
 
         // Update cursor.
-        set_arrow_cursor ();
+        set_handle_cursor ();
     }
 
     private void on_paned_size_allocate () {
@@ -303,10 +311,16 @@
         }
     }
 
-    private void set_arrow_cursor () {
-        var window = this.handle.get_window ();
-        if (window != null && window.get_cursor () != this.arrow_cursor)
-            window.set_cursor (this.arrow_cursor);
+    private void set_handle_cursor () {
+        Gdk.Cursor? arrow_cursor = null;
+
+        var paned_handle_window = this.paned.get_handle_window ();
+        if (paned_handle_window != null)
+            arrow_cursor = paned_handle_window.get_cursor ();
+
+        var handle_window = this.handle.get_window ();
+        if (handle_window != null && handle_window.get_cursor () != arrow_cursor)
+            handle_window.set_cursor (arrow_cursor);
     }
 
     /**

=== modified file 'lib/Widgets/StatusBar.vala'
--- lib/Widgets/StatusBar.vala	2012-05-07 21:00:33 +0000
+++ lib/Widgets/StatusBar.vala	2012-08-24 01:58:19 +0000
@@ -28,11 +28,8 @@
 
     protected const int ITEM_SPACING = 3;
 
-    protected Gtk.CssProvider style_provider;
-    protected Gtk.StyleContext context;
-
     /* This prevents the huge vertical padding */
-    private const string STATUSBAR_STYLESHEET = """
+    private const string STYLESHEET = """
         GraniteWidgetsStatusBar {
             border-bottom-width: 0;
             border-right-width: 0;
@@ -45,23 +42,14 @@
     """;
 
     public StatusBar () {
-
-        style_provider = new Gtk.CssProvider ();
-
-        try {
-            style_provider.load_from_data (STATUSBAR_STYLESHEET, -1);
-        }
-        catch (Error err) {
-            warning (err.message);
-        }
-
         /* Get rid of the "toolbar" class to avoid inheriting its style.
          * We want the widget to look more like a normal statusbar.
          */
         get_style_context ().remove_class (Gtk.STYLE_CLASS_TOOLBAR);
 
-        context = new Gtk.StyleContext ();
-        context.add_provider_for_screen (get_screen (), style_provider, Gtk.STYLE_PROVIDER_PRIORITY_THEME);
+        Utils.set_theming_for_screen (this.get_screen (), STYLESHEET,
+                                      Gtk.STYLE_PROVIDER_PRIORITY_THEME);
+
 
         status_label = new Gtk.Label ("");
         status_label.set_justify (Gtk.Justification.CENTER);
@@ -95,11 +83,6 @@
     }
 
     public void set_text (string text) {
-        if (text == null) {
-            status_label.set_text ("");
-            return;
-        }
-
         status_label.set_text (text);
     }
 }

=== modified file 'lib/Widgets/ToolButtonWithMenu.vala'
--- lib/Widgets/ToolButtonWithMenu.vala	2012-01-25 08:19:48 +0000
+++ lib/Widgets/ToolButtonWithMenu.vala	2012-08-24 01:58:19 +0000
@@ -286,20 +286,9 @@
 
             if (menu_position == MenuPosition.INSIDE_WINDOW) {
                 /* Get window geometry */
-                Gtk.Widget? parent_widget = get_parent ();
-                Gtk.Widget? next_parent = parent_widget.get_parent ();
-
-                while (true) {
-                    if (parent_widget != null && next_parent != null) {
-                        parent_widget = parent_widget.get_parent();
-                        next_parent = parent_widget.get_parent();
-                    }
-
-                    if (parent_widget == null || next_parent == null)
-                        break;
-                }
-
-                Allocation window_allocation;
+                var parent_widget = get_toplevel ();
+
+                Gtk.Allocation window_allocation;
                 parent_widget.get_allocation (out window_allocation);
 
                 parent_widget.get_window ().get_origin (out x, out y);

=== added file 'lib/Widgets/Utils.vala'
--- lib/Widgets/Utils.vala	1970-01-01 00:00:00 +0000
+++ lib/Widgets/Utils.vala	2012-08-24 01:58:19 +0000
@@ -0,0 +1,121 @@
+// -*- Mode: vala; indent-tabs-mode: nil; tab-width: 4 -*-
+/*
+ * Copyright (c) 2012 Granite Developers
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2 of the
+ * License, or (at your option) any later version.
+ *
+ * This is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this program; see the file COPYING.  If not,
+ * write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ * Boston, MA 02111-1307, USA.
+ */
+
+public enum Granite.TextStyle {
+    TITLE,
+    H1,
+    H2,
+    H3;
+
+    public string get_stylesheet (out string style_class = null) {
+        switch (this) {
+            case TITLE:
+                style_class = "title";
+                return @".$style_class { font: raleway 36; }";
+            case H1:
+                style_class = "h1";
+                return @".$style_class { font: open sans bold 24; }";
+            case H2:
+                style_class = "h2";
+                return @".$style_class { font: open sans light 18; }";
+            case H3:
+                style_class = "h3";
+                return @".$style_class { font: open sans bold 12; }";
+            default:
+                assert_not_reached ();
+        }
+    }
+}
+
+namespace Granite.Widgets.Utils {
+
+    [CCode (cname="get_close_pixbuf")]
+    public extern Gdk.Pixbuf get_close_pixbuf ();
+
+
+    /**
+     * Applies the stylesheet to the widget
+     */
+    public void set_theming (Gtk.Widget widget, string stylesheet,
+                              string? class_name, int priority) {
+        var css_provider = get_css_provider (stylesheet);
+
+        var context = widget.get_style_context ();
+
+        if (css_provider != null)
+            context.add_provider (css_provider, priority);
+
+        if (class_name != null && class_name.strip () != "")
+            context.add_class (class_name);
+    }
+
+    /**
+     * Applies an stylesheet to the given screen. This will affects all the
+     * widgets which are part of that screen.
+     */
+    public void set_theming_for_screen (Gdk.Screen screen, string stylesheet, int priority) {
+        var provider = get_css_provider (stylesheet);
+
+        if (provider != null)
+            Gtk.StyleContext.add_provider_for_screen (screen, provider, priority);
+    }
+
+    /**
+     * @return a new {@link Gtk.CssProvider}, or null in case the parsing of
+     *         @stylesheet failed.
+     */
+    public Gtk.CssProvider? get_css_provider (string stylesheet) {
+        Gtk.CssProvider provider = new Gtk.CssProvider ();
+
+        try {
+            provider.load_from_data (stylesheet, -1);
+        }
+        catch (Error e) {
+            warning ("Could not create CSS Provider: %s\nStylesheet:\n%s",
+                     e.message, stylesheet);
+            return null;
+        }
+
+        return provider;
+    }
+
+
+    /**
+     * Text Style
+     */
+
+    public void apply_text_style_to_label (TextStyle text_style, Gtk.Label label) {
+        var style_provider = new Gtk.CssProvider ();
+        var style_context = label.get_style_context ();
+
+        string style_class, stylesheet;
+        stylesheet = text_style.get_stylesheet (out style_class);
+        style_context.add_class (style_class);
+
+        try {
+            style_provider.load_from_data (stylesheet, -1);
+        } catch (Error err) {
+            warning ("Couldn't apply style to label: %s", err.message);
+            return;
+        }
+
+        style_context.add_provider (style_provider, Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION);
+    }
+}

=== modified file 'lib/Widgets/Welcome.vala'
--- lib/Widgets/Welcome.vala	2012-03-18 23:07:11 +0000
+++ lib/Widgets/Welcome.vala	2012-08-24 01:58:19 +0000
@@ -30,10 +30,6 @@
     protected Gtk.Box options;
 
     public Welcome (string title_text, string subtitle_text) {
-        string _title_text = title_text;
-        string _subtitle_text = subtitle_text;
-        _title_text = _title_text.replace("&", "&amp;");
-        _subtitle_text = _subtitle_text.replace("&", "&amp;");
 
         Gtk.Box content = new Gtk.Box (Gtk.Orientation.VERTICAL, 0);
 
@@ -47,22 +43,20 @@
         content.pack_start (new Gtk.Box (Gtk.Orientation.HORIZONTAL, 0), true, true, 0);
 
         // Labels
-        var title = new Gtk.Label ("<span weight='medium' size='14700'>" + _title_text + "</span>");
-
-        title.get_style_context().add_class ("title");
-
-        title.use_markup = true;
+        var title = new Gtk.Label (title_text);
+        Utils.apply_text_style_to_label (TextStyle.H1, title);
+
         title.set_justify (Gtk.Justification.CENTER);
         content.pack_start (title, false, true, 0);
 
-        var subtitle = new Gtk.Label ("<span weight='medium' size='11500'>" + _subtitle_text + "</span>");
-        subtitle.use_markup = true;
+        var subtitle = new Gtk.Label (subtitle_text);
+        Utils.apply_text_style_to_label (TextStyle.H2, subtitle);
+
         subtitle.sensitive = false;
         subtitle.set_justify (Gtk.Justification.CENTER);
+
         content.pack_start (subtitle, false, true, 2);
 
-        subtitle.get_style_context().add_class("subtitle");
-
         // Options wrapper
         this.options = new Gtk.Box (Gtk.Orientation.VERTICAL, 8);
         var options_wrapper = new Gtk.Box (Gtk.Orientation.HORIZONTAL, 0);
@@ -110,25 +104,18 @@
     }
 
     public int append_with_image (Gtk.Image? image, string option_text, string description_text) {
-        string _option_text = option_text;
-        string _description_text = description_text;
-        _option_text = _option_text.replace ("&", "&amp;");
-        _description_text = _description_text.replace ("&", "&amp;");
-
         // Option label
-        var label = new Gtk.Label ("<span weight='medium' size='11700'>" + _option_text + "</span>");
+        var label = new Gtk.Label (Markup.printf_escaped ("<span weight='medium' size='11700'>%s</span>", option_text));
         label.use_markup = true;
         label.halign = Gtk.Align.START;
         label.valign = Gtk.Align.CENTER;
-        label.get_style_context().add_class ("option-title");
 
         // Description label
-        var description = new Gtk.Label ("<span weight='medium' size='11400'>" + _description_text + "</span>");
+        var description = new Gtk.Label (Markup.printf_escaped ("<span weight='medium' size='11400'>%s</span>", description_text));
         description.use_markup = true;
         description.halign = Gtk.Align.START;
         description.valign = Gtk.Align.CENTER;
         description.sensitive = false;
-        description.get_style_context().add_class ("option-description");
 
         // Button
         var button = new Gtk.Button ();


Follow ups