[hyena/canvas] [Hyena.Gui] Improve canvas rendering/layout



commit 8f46915a2a84ab3daf06474fdb65965beab69912
Author: Gabriel Burt <gabriel burt gmail com>
Date:   Fri Oct 22 13:02:28 2010 -0500

    [Hyena.Gui] Improve canvas rendering/layout

 Hyena.Gui/Hyena.Data.Gui/ColumnCell.cs   |   14 ++++++++-
 Hyena.Gui/Hyena.Gui.Canvas/CanvasItem.cs |   27 +++++++++++++++--
 Hyena.Gui/Hyena.Gui.Canvas/StackPanel.cs |   10 +++++-
 Hyena.Gui/Hyena.Gui.Canvas/TextBlock.cs  |   45 +++++++++++++++++++----------
 4 files changed, 72 insertions(+), 24 deletions(-)
---
diff --git a/Hyena.Gui/Hyena.Data.Gui/ColumnCell.cs b/Hyena.Gui/Hyena.Data.Gui/ColumnCell.cs
index 6c95092..7a491ff 100644
--- a/Hyena.Gui/Hyena.Data.Gui/ColumnCell.cs
+++ b/Hyena.Gui/Hyena.Data.Gui/ColumnCell.cs
@@ -60,7 +60,10 @@ namespace Hyena.Data.Gui
             get { return ObjectBinder.BoundObjectParent; }
         }
 
-        //public string Property { get; private set; }
+        public string Property {
+            get { return ObjectBinder.Property; }
+            set { ObjectBinder.Property = value; }
+        }
 
         public void BindListItem (object item)
         {
@@ -81,7 +84,14 @@ namespace Hyena.Data.Gui
             Render (context, ContentAllocation.Width, ContentAllocation.Height);
         }
 
-        public abstract void Render (CellContext context, double cellWidth, double cellHeight);
+        public virtual void Render (CellContext context, double cellWidth, double cellHeight)
+        {
+            Render (context, context.State, cellWidth, cellHeight);
+        }
+
+        public virtual void Render (CellContext context, Gtk.StateType state, double cellWidth, double cellHeight)
+        {
+        }
 
         public bool Expand { get; set; }
 
diff --git a/Hyena.Gui/Hyena.Gui.Canvas/CanvasItem.cs b/Hyena.Gui/Hyena.Gui.Canvas/CanvasItem.cs
index 30f09f9..b2dbd80 100644
--- a/Hyena.Gui/Hyena.Gui.Canvas/CanvasItem.cs
+++ b/Hyena.Gui/Hyena.Gui.Canvas/CanvasItem.cs
@@ -137,8 +137,8 @@ namespace Hyena.Gui.Canvas
 
         public virtual Size Measure (Size available)
         {
-            double m_x = Margin.Left + Margin.Right;
-            double m_y = Margin.Top + Margin.Bottom;
+            double m_x = Margin.X;
+            double m_y = Margin.Y;
 
             double a_w = available.Width - m_x;
             double a_h = available.Height - m_y;
@@ -151,10 +151,11 @@ namespace Hyena.Gui.Canvas
 
         public void Render (Hyena.Data.Gui.CellContext context)
         {
+            var alloc = ContentAllocation;
             var cr = context.Context;
             double opacity = Opacity;
 
-            if (ContentAllocation.Width <= 0 || ContentAllocation.Height <= 0 || opacity <= 0) {
+            if (alloc.Width <= 0 || alloc.Height <= 0 || opacity <= 0) {
                 return;
             }
 
@@ -172,13 +173,18 @@ namespace Hyena.Gui.Canvas
                 cr.Restore ();
                 cr.Translate (Math.Round (Margin.Left), Math.Round (Margin.Top));
             } else {
-                cr.Translate (Math.Round (ContentAllocation.X), Math.Round (ContentAllocation.Y));
+                cr.Translate (Math.Round (alloc.X), Math.Round (alloc.Y));
             }
 
             cr.Antialias = Cairo.Antialias.Default;
 
+            cr.Rectangle (0, 0, alloc.Width, alloc.Height);
+            cr.Clip ();
+
             ClippedRender (context);
 
+            cr.ResetClip ();
+
             if (opacity < 1.0) {
                 cr.PopGroupToSource ();
                 cr.PaintWithAlpha (Opacity);
@@ -187,6 +193,19 @@ namespace Hyena.Gui.Canvas
             cr.Restore ();
         }
 
+        /*protected Rect TopLevelAllocation {
+            get {
+                var alloc = ContentAllocation;
+                var top = this;
+                while (top.Parent != null) {
+                    alloc.Offset (top.Parent.Allocation);
+                    top = top.Parent;
+                }
+
+                return alloc;
+            }
+        }*/
+
         protected virtual void ClippedRender (Cairo.Context cr)
         {
         }
diff --git a/Hyena.Gui/Hyena.Gui.Canvas/StackPanel.cs b/Hyena.Gui/Hyena.Gui.Canvas/StackPanel.cs
index e02fbec..d98c0e1 100644
--- a/Hyena.Gui/Hyena.Gui.Canvas/StackPanel.cs
+++ b/Hyena.Gui/Hyena.Gui.Canvas/StackPanel.cs
@@ -41,6 +41,7 @@ namespace Hyena.Gui.Canvas
         {
             Size result = new Size (0, 0);
 
+            int visible_children = 0;
             foreach (var child in Children) {
                 if (!child.Visible) {
                     continue;
@@ -55,6 +56,8 @@ namespace Hyena.Gui.Canvas
                     result.Width += size.Width;
                     result.Height = Math.Max (result.Height, size.Height);
                 }
+
+                visible_children++;
             }
 
             if (!Double.IsNaN (Width)) {
@@ -65,15 +68,18 @@ namespace Hyena.Gui.Canvas
                 result.Height = Height;
             }
 
+            result.Width += Margin.X;
+            result.Height += Margin.Y;
+
             if (!available.IsEmpty) {
                 result.Width = Math.Min (result.Width, available.Width);
                 result.Height = Math.Min (result.Height, available.Height);
             }
 
             if (Orientation == Orientation.Vertical) {
-                result.Height += Spacing * (Children.Count - 1);
+                result.Height += Spacing * (visible_children - 1);
             } else {
-                result.Width += Spacing * (Children.Count - 1);
+                result.Width += Spacing * (visible_children - 1);
             }
 
             return DesiredSize = result;
diff --git a/Hyena.Gui/Hyena.Gui.Canvas/TextBlock.cs b/Hyena.Gui/Hyena.Gui.Canvas/TextBlock.cs
index 7988ead..c5ddcad 100644
--- a/Hyena.Gui/Hyena.Gui.Canvas/TextBlock.cs
+++ b/Hyena.Gui/Hyena.Gui.Canvas/TextBlock.cs
@@ -25,8 +25,10 @@
 // THE SOFTWARE.
 
 using System;
+using System.Linq;
 
 using Cairo;
+using Hyena.Gui;
 
 namespace Hyena.Gui.Canvas
 {
@@ -39,11 +41,14 @@ namespace Hyena.Gui.Canvas
         public TextBlock ()
         {
             InstallProperty<string> ("Text", String.Empty);
+            InstallProperty<string> ("TextFormat", "");
+            InstallProperty<bool> ("UseMarkup", false);
             InstallProperty<double> ("HorizontalAlignment", 0.0);
             InstallProperty<double> ("VerticalAlignment", 0.0);
             InstallProperty<FontWeight> ("FontWeight", FontWeight.Normal);
             InstallProperty<TextWrap> ("TextWrap", TextWrap.None);
             InstallProperty<bool> ("ForceSize", false);
+            EllipsizeMode = Pango.EllipsizeMode.End;
         }
 
         private bool EnsureLayout ()
@@ -79,7 +84,8 @@ namespace Hyena.Gui.Canvas
             layout.Wrap = GetPangoWrapMode (wrap);
             layout.FontDescription.Weight = GetPangoFontWeight (FontWeight);
             layout.SingleParagraphMode = wrap == TextWrap.None;
-            UpdateLayout (layout, Text);
+            layout.Ellipsize = EllipsizeMode;
+            UpdateLayout (layout, GetText ());
             layout.GetPixelSize (out text_w, out text_h);
 
             double width = text_w;
@@ -87,19 +93,28 @@ namespace Hyena.Gui.Canvas
                 width = available.Width;
             }
 
-            DesiredSize = new Size (
-                width + Margin.Left + Margin.Right,
-                text_h + Margin.Top + Margin.Bottom);
+            //DesiredSize = new Size (width, text_h);
+            var size = new Size (width, text_h);
 
             // Hack, as this prevents the TextBlock from
             // being flexible in a Vertical StackPanel
-            Height = DesiredSize.Height;
+            Height = size.Height;
 
             if (ForceSize) {
                 Width = DesiredSize.Width;
             }
 
-            return DesiredSize;
+            return size;
+        }
+
+        private string GetText ()
+        {
+            if (TextGenerator != null) {
+                return TextGenerator (BoundObject);
+            } else {
+                var so = BoundObject;
+                return so == null ? Text : so.ToString ();
+            }
         }
 
         private static char[] lfcr = new char[] {'\n', '\r'};
@@ -119,7 +134,7 @@ namespace Hyena.Gui.Canvas
 
         private string GetFormattedText (string text)
         {
-            if (TextFormat == null) {
+            if (String.IsNullOrEmpty (TextFormat)) {
                 return text;
             }
             return String.Format (TextFormat, UseMarkup ? GLib.Markup.EscapeText (text) : text);
@@ -139,6 +154,7 @@ namespace Hyena.Gui.Canvas
             }
 
             int text_width, text_height;
+            layout.SetHeight ((int)(Pango.Scale.PangoScale * RenderSize.Height));
             layout.GetPixelSize (out text_width, out text_height);
 
             Rect new_alloc = new Rect (
@@ -180,7 +196,7 @@ namespace Hyena.Gui.Canvas
             cr.Rectangle (0, 0, RenderSize.Width, RenderSize.Height);
             cr.Clip ();
 
-            bool fade = text_alloc.Width > RenderSize.Width;
+            bool fade = Fade && text_alloc.Width > RenderSize.Width;
 
             if (fade) {
                 cr.PushGroup ();
@@ -230,6 +246,8 @@ namespace Hyena.Gui.Canvas
                 case "FontWeight":
                 case "TextWrap":
                 case "Text":
+                case "TextFormat":
+                case "UseMarkup":
                 case "ForceSize":
                     if (layout != null) {
                         InvalidateMeasure ();
@@ -251,13 +269,6 @@ namespace Hyena.Gui.Canvas
             get { return invalidation_rect; }
         }
 
-        public override void Bind (object o)
-        {
-            base.Bind (o);
-            var so = BoundObject;
-            Text = so == null ? "" : so.ToString ();
-        }
-
         public string Text {
             get { return GetValue<string> ("Text"); }
             set { SetValue<string> ("Text", value); }
@@ -278,6 +289,8 @@ namespace Hyena.Gui.Canvas
             set { SetValue<TextWrap> ("TextWrap", value); }
         }
 
+        public bool Fade { get; set; }
+
         public bool ForceSize {
             get { return GetValue<bool> ("ForceSize"); }
             set { SetValue<bool> ("ForceSize", value); }
@@ -293,7 +306,7 @@ namespace Hyena.Gui.Canvas
         }
 
         public double HorizontalAlignment {
-            get { return GetValue<double> ("HorizontalAlignment", 0.5); }
+            get { return GetValue<double> ("HorizontalAlignment", 0.0); }
             set { SetValue<double> ("HorizontalAlignment", value); }
         }
 



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