[hyena] ListView: Refactor row selection rendering to use StyleContext.Render*



commit 3de0d69d378a0656c1d910d97326eadfb49faf43
Author: Bertrand Lorentz <bertrand lorentz gmail com>
Date:   Sat Jun 14 18:59:24 2014 +0200

    ListView: Refactor row selection rendering to use StyleContext.Render*
    
    Use StyleContext.RenderBackground and RenderFrame with the proper class
    and state to render selected rows.
    
    This allows us to follow the GTK theme for selected rows, in particular
    in the "backdrop" state, while getting rid of complicated Cairo code.
    
    This removes the workaround for buggy themes, as we're not choosing the
    color ourselves. But it should be OK, as the main culprit (Ambiance) was
    fixed, and any similar problem that would now affect us would also
    affect any custom treeview-like widget. So fix your themes !

 .../Hyena.Data.Gui/ListView/ListView_Rendering.cs  |   60 +++++++------------
 Hyena.Gui/Hyena.Gui.Theming/GtkTheme.cs            |   59 -------------------
 Hyena.Gui/Hyena.Gui.Theming/Theme.cs               |   20 -------
 3 files changed, 22 insertions(+), 117 deletions(-)
---
diff --git a/Hyena.Gui/Hyena.Data.Gui/ListView/ListView_Rendering.cs 
b/Hyena.Gui/Hyena.Data.Gui/ListView/ListView_Rendering.cs
index c31c2bb..d6e67bd 100644
--- a/Hyena.Gui/Hyena.Data.Gui/ListView/ListView_Rendering.cs
+++ b/Hyena.Gui/Hyena.Data.Gui/ListView/ListView_Rendering.cs
@@ -352,25 +352,7 @@ namespace Hyena.Data.Gui
                     }
 
                     if (selection_height > 0) {
-
-                        StyleContext.AddClass ("cell");
-                        var bg_selected_color = StyleContext.GetBackgroundColor (StateFlags.Selected);
-
-                        if (bg_selected_color.Equals (StyleContext.GetBackgroundColor (StateFlags.Normal))) {
-                            // see https://bugs.launchpad.net/bugs/1211831
-                            Hyena.Log.Warning ("Buggy CSS theme: same background-color for .cell:selected 
and .cell");
-                            StyleContext.RemoveClass ("cell");
-                            bg_selected_color = StyleContext.GetBackgroundColor (StateFlags.Selected);
-                            StyleContext.AddClass ("cell");
-                        }
-                        Cairo.Color selection_color = CairoExtensions.GdkRGBAToCairoColor 
(bg_selected_color);
-
-                        if (!HasFocus || HeaderFocused)
-                            selection_color = CairoExtensions.ColorShade (selection_color, 1.1);
-
-                        Theme.DrawRowSelection (cr, list_rendering_alloc.X, selection_y, 
list_rendering_alloc.Width, selection_height,
-                                                true, true, selection_color, CairoCorners.All);
-                        StyleContext.RemoveClass ("cell");
+                        PaintRowSelection (cr, list_rendering_alloc.X, selection_y, 
list_rendering_alloc.Width, selection_height);
                         selection_height = 0;
                     }
 
@@ -384,8 +366,7 @@ namespace Hyena.Data.Gui
             PaintReorderLine (cr, last_row, single_list_alloc);
 
             if (selection_height > 0) {
-                Theme.DrawRowSelection (cr, list_rendering_alloc.X, selection_y,
-                    list_rendering_alloc.Width, selection_height);
+                PaintRowSelection (cr, list_rendering_alloc.X, selection_y, list_rendering_alloc.Width, 
selection_height);
             }
 
             if (Selection != null && Selection.Count > 1 &&
@@ -409,6 +390,21 @@ namespace Hyena.Data.Gui
             cr.ResetClip ();
         }
 
+        private void PaintRowSelection (Cairo.Context cr, int x, int y, int width, int height)
+        {
+            StyleContext.Save ();
+            StyleContext.AddClass ("cell");
+            StyleContext.State |= StateFlags.Selected;
+
+            if (HasFocus && !HeaderFocused) {
+                StyleContext.State |= StateFlags.Focused;
+            }
+
+            StyleContext.RenderBackground (cr, x, y, width, height);
+            StyleContext.RenderFrame (cr, x, y, width, height);
+            StyleContext.Restore ();
+        }
+
         private void PaintReorderLine (Cairo.Context cr, int row_index, Rectangle single_list_alloc)
         {
             if (row_index == drag_reorder_row_index && IsReorderable) {
@@ -492,7 +488,7 @@ namespace Hyena.Data.Gui
             cr.Translate (area.X, area.Y);
             cell_context.Area = area;
             cell_context.Opaque = opaque;
-            cell_context.State = dragging ? StateFlags.Normal : state;
+            cell_context.State |= dragging ? StateFlags.Normal : state;
             cell.Render (cell_context, area.Width, area.Height);
             cr.Restore ();
 
@@ -559,27 +555,15 @@ namespace Hyena.Data.Gui
                 if (Selection != null && Selection.Contains (ViewLayout.GetModelIndex (layout_child))) {
                     selected_rows.Add (ViewLayout.GetModelIndex (layout_child));
 
-                    var selection_color = CairoExtensions.GdkRGBAToCairoColor 
(StyleContext.GetBackgroundColor (StateFlags.Selected));
-                    if (!HasFocus || HeaderFocused) {
-                        selection_color = CairoExtensions.ColorShade (selection_color, 1.1);
-                    }
-
-                    Theme.DrawRowSelection (cr,
-                        (int)child_allocation.X, (int)child_allocation.Y,
-                        (int)child_allocation.Width, (int)child_allocation.Height,
-                        true, true, selection_color, CairoCorners.All);
+                    PaintRowSelection (cr, (int)child_allocation.X, (int)child_allocation.Y,
+                        (int)child_allocation.Width, (int)child_allocation.Height);
 
-                    cell_context.State = StateFlags.Selected;
+                    cell_context.State = StyleContext.State | StateFlags.Selected;
                 } else {
-                    cell_context.State = StateFlags.Normal;
+                    cell_context.State = StyleContext.State;
                 }
 
-                //cr.Save ();
-                //cr.Translate (child_allocation.X, child_allocation.Y);
-                //cr.Rectangle (0, 0, child_allocation.Width, child_allocation.Height);
-                //cr.Clip ();
                 layout_child.Render (cell_context);
-                //cr.Restore ();
             }
 
             cr.ResetClip ();
diff --git a/Hyena.Gui/Hyena.Gui.Theming/GtkTheme.cs b/Hyena.Gui/Hyena.Gui.Theming/GtkTheme.cs
index 2dd5938..fefff3d 100644
--- a/Hyena.Gui/Hyena.Gui.Theming/GtkTheme.cs
+++ b/Hyena.Gui/Hyena.Gui.Theming/GtkTheme.cs
@@ -202,65 +202,6 @@ namespace Hyena.Gui.Theming
             cr.Stroke ();
         }
 
-        public override void DrawRowSelection (Cairo.Context cr, int x, int y, int width, int height,
-            bool filled, bool stroked, Cairo.Color color, CairoCorners corners)
-        {
-            DrawRowSelection (cr, x, y, width, height, filled, stroked, color, corners, false);
-        }
-
-        public void DrawRowSelection (Cairo.Context cr, int x, int y, int width, int height,
-            bool filled, bool stroked, Cairo.Color color, CairoCorners corners, bool flat_fill)
-        {
-            Cairo.Color selection_color = color;
-            Cairo.Color selection_highlight = CairoExtensions.ColorShade (selection_color, 1.24);
-            Cairo.Color selection_stroke = CairoExtensions.ColorShade (selection_color, 0.85);
-            selection_highlight.A = 0.5;
-            selection_stroke.A = color.A;
-            LinearGradient grad = null;
-
-            if (filled) {
-                if (flat_fill) {
-                    cr.SetSourceColor (selection_color);
-                } else {
-                    Cairo.Color selection_fill_light = CairoExtensions.ColorShade (selection_color, 1.12);
-                    Cairo.Color selection_fill_dark = selection_color;
-
-                    selection_fill_light.A = color.A;
-                    selection_fill_dark.A = color.A;
-
-                    grad = new LinearGradient (x, y, x, y + height);
-                    grad.AddColorStop (0, selection_fill_light);
-                    grad.AddColorStop (0.4, selection_fill_dark);
-                    grad.AddColorStop (1, selection_fill_light);
-
-                    cr.SetSource (grad);
-                }
-
-                CairoExtensions.RoundedRectangle (cr, x, y, width, height, Context.Radius, corners, true);
-                cr.Fill ();
-
-                if (grad != null) {
-                    grad.Dispose ();
-                }
-            }
-
-            if (filled && stroked) {
-                cr.LineWidth = 1.0;
-                cr.SetSourceColor (selection_highlight);
-                CairoExtensions.RoundedRectangle (cr, x + 1.5, y + 1.5, width - 3, height - 3,
-                    Context.Radius - 1, corners, true);
-                cr.Stroke ();
-            }
-
-            if (stroked) {
-                cr.LineWidth = 1.0;
-                cr.SetSourceColor (selection_stroke);
-                CairoExtensions.RoundedRectangle (cr, x + 0.5, y + 0.5, width - 1, height - 1,
-                    Context.Radius, corners, true);
-                cr.Stroke ();
-            }
-        }
-
         public override void DrawRowRule (Cairo.Context cr, int x, int y, int width, int height)
         {
             cr.SetSourceColor (new Cairo.Color (rule_color.R, rule_color.G, rule_color.B, 
Context.FillAlpha));
diff --git a/Hyena.Gui/Hyena.Gui.Theming/Theme.cs b/Hyena.Gui/Hyena.Gui.Theming/Theme.cs
index 7848d96..fab8a70 100644
--- a/Hyena.Gui/Hyena.Gui.Theming/Theme.cs
+++ b/Hyena.Gui/Hyena.Gui.Theming/Theme.cs
@@ -166,23 +166,6 @@ namespace Hyena.Gui.Theming
 
         public abstract void DrawColumnHighlight (Cairo.Context cr, Gdk.Rectangle alloc, Cairo.Color color);
 
-        public void DrawRowSelection (Cairo.Context cr, int x, int y, int width, int height)
-        {
-            DrawRowSelection (cr, x, y, width, height, true);
-        }
-
-        public void DrawRowSelection (Cairo.Context cr, int x, int y, int width, int height, bool filled)
-        {
-            Cairo.Color color = CairoExtensions.GdkRGBAToCairoColor (Widget.StyleContext.GetBackgroundColor 
(StateFlags.Selected));
-            DrawRowSelection (cr, x, y, width, height, filled, true, color, CairoCorners.All);
-        }
-
-        public void DrawRowSelection (Cairo.Context cr, int x, int y, int width, int height,
-            bool filled, bool stroked, Cairo.Color color)
-        {
-            DrawRowSelection (cr, x, y, width, height, filled, stroked, color, CairoCorners.All);
-        }
-
         public void DrawRowCursor (Cairo.Context cr, int x, int y, int width, int height)
         {
             Cairo.Color color = CairoExtensions.GdkRGBAToCairoColor (Widget.StyleContext.GetBackgroundColor 
(StateFlags.Selected));
@@ -196,9 +179,6 @@ namespace Hyena.Gui.Theming
 
         public abstract void DrawRowCursor (Cairo.Context cr, int x, int y, int width, int height, 
Cairo.Color color, CairoCorners corners);
 
-        public abstract void DrawRowSelection (Cairo.Context cr, int x, int y, int width, int height,
-            bool filled, bool stroked, Cairo.Color color, CairoCorners corners);
-
         public abstract void DrawRowRule (Cairo.Context cr, int x, int y, int width, int height);
 
         public Cairo.Color ViewFill {


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