[hyena/gtk3] AnimatedWidget: Port drawing code to cairo



commit 32cd2c02b85a8a3ad5a07ee37a27868e2a3ca192
Author: Bertrand Lorentz <bertrand lorentz gmail com>
Date:   Mon Jul 4 21:57:12 2011 +0200

    AnimatedWidget: Port drawing code to cairo
    
    Also improve the associated AnimatedVBox test module to be more
    realistic and to check that the animation is also done when the widget
    is destroyed.

 Hyena.Gui/Hyena.Widgets/AnimatedVBox.cs   |   77 ++++++++++++++++++++++++-----
 Hyena.Gui/Hyena.Widgets/AnimatedWidget.cs |   30 ++++++++----
 2 files changed, 85 insertions(+), 22 deletions(-)
---
diff --git a/Hyena.Gui/Hyena.Widgets/AnimatedVBox.cs b/Hyena.Gui/Hyena.Widgets/AnimatedVBox.cs
index 4d27c00..10ae0d0 100644
--- a/Hyena.Gui/Hyena.Widgets/AnimatedVBox.cs
+++ b/Hyena.Gui/Hyena.Widgets/AnimatedVBox.cs
@@ -46,28 +46,81 @@ namespace Hyena.Widgets
     [Hyena.Gui.TestModule ("Animated VBox")]
     internal class AnimatedVBoxTestModule : Gtk.Window
     {
-        Label label1, label2, label3;
+        Table tile, tile2;
+        uint timeout_id;
 
         public AnimatedVBoxTestModule () : base ("Animated VBox")
         {
             AnimatedVBox vbox = new AnimatedVBox ();
             Add (vbox);
-            ShowAll ();
 
-            label1 = new Label ("First Label");
-            label2 = new Label ("Second Label");
-            label3 = new Label ("Third Label with a longer text");
+            tile = BuildWidget ("Example destroyed");
+            tile2 = BuildWidget ("Example removed");
 
-            vbox.PackEnd (label1, Hyena.Gui.Theatrics.Easing.Linear, Blocking.Downstage);
-            vbox.PackEnd (label2, 2000, Hyena.Gui.Theatrics.Easing.ExponentialIn, Blocking.Upstage);
-            vbox.PackEnd (label3, 5000, Hyena.Gui.Theatrics.Easing.QuadraticInOut);
+            vbox.PackEnd (tile, 3000, Hyena.Gui.Theatrics.Easing.QuadraticOut);
+            vbox.PackEnd (tile2, 3000, Hyena.Gui.Theatrics.Easing.QuadraticOut);
+            ShowAll ();
 
-            GLib.Timeout.AddSeconds (10, delegate {
-                vbox.Remove (label2);
-                vbox.Remove (label1);
-                vbox.Remove (label3);
+            timeout_id = GLib.Timeout.AddSeconds (5, delegate {
+                tile.Destroy ();
+                vbox.Remove (tile2);
                 return false;
             });
         }
+
+        protected override bool OnDeleteEvent (Gdk.Event evnt)
+        {
+            if (timeout_id > 0) {
+                GLib.Timeout.Remove (timeout_id);
+            }
+            return base.OnDeleteEvent (evnt);
+        }
+
+
+        private Table BuildWidget (string title)
+        {
+            var tile = new Table (3, 2, false);
+
+            tile.ColumnSpacing = 5;
+            tile.RowSpacing = 2;
+
+            var title_label = new Label ();
+            title_label.Xalign = 0.0f;
+            title_label.Ellipsize = Pango.EllipsizeMode.End;
+            title_label.Markup = String.Format ("<small><b>{0}</b></small>", GLib.Markup.EscapeText (title));
+
+            var status_label = new Label ();
+            status_label.Xalign = 0.0f;
+            status_label.Ellipsize = Pango.EllipsizeMode.End;
+            status_label.Markup = "<small>Testing...</small>";
+
+            var progress_bar = new ProgressBar ();
+            progress_bar.SetSizeRequest (0, -1);
+            progress_bar.Fraction = 0.5;
+            progress_bar.Text = "Doing nothing...";
+
+            var cancel_button = new Button (new Image (Stock.Stop, IconSize.Menu));
+            cancel_button.Relief = ReliefStyle.None;
+
+            tile.Attach (title_label, 0, 3, 0, 1,
+                AttachOptions.Expand | AttachOptions.Fill,
+                AttachOptions.Expand | AttachOptions.Fill, 0, 0);
+
+            tile.Attach (status_label, 0, 3, 1, 2,
+                AttachOptions.Expand | AttachOptions.Fill,
+                AttachOptions.Expand | AttachOptions.Fill, 0, 0);
+
+            tile.Attach (progress_bar, 1, 2, 2, 3,
+                AttachOptions.Expand | AttachOptions.Fill,
+                AttachOptions.Shrink, 0, 0);
+
+            tile.Attach (cancel_button, 2, 3, 2, 3,
+                AttachOptions.Shrink | AttachOptions.Fill,
+                AttachOptions.Shrink | AttachOptions.Fill, 0, 0);
+
+            tile.ShowAll ();
+
+            return tile;
+        }
     }
 }
\ No newline at end of file
diff --git a/Hyena.Gui/Hyena.Widgets/AnimatedWidget.cs b/Hyena.Gui/Hyena.Widgets/AnimatedWidget.cs
index 7eccf59..7fb99c0 100644
--- a/Hyena.Gui/Hyena.Widgets/AnimatedWidget.cs
+++ b/Hyena.Gui/Hyena.Widgets/AnimatedWidget.cs
@@ -31,6 +31,7 @@ using System.Collections.Generic;
 using Gdk;
 using Gtk;
 
+using Hyena.Gui;
 using Hyena.Gui.Theatrics;
 
 namespace Hyena.Widgets
@@ -62,7 +63,7 @@ namespace Hyena.Widgets
         private readonly bool horizontal;
         private double percent;
         private Rectangle widget_alloc;
-        //private Pixmap canvas;
+        private Cairo.Surface surface;
 
         public AnimatedWidget (Widget widget, uint duration, Easing easing, Blocking blocking, bool horizontal)
         {
@@ -92,17 +93,22 @@ namespace Hyena.Widgets
 
         private void OnWidgetDestroyed (object sender, EventArgs args)
         {
-/*            if (!IsRealized) {
+            if (!IsRealized) {
                 return;
             }
 
-            canvas = new Pixmap (Window, widget_alloc.Width, widget_alloc.Height);
-            canvas.DrawDrawable (Style.BackgroundGC (State), Window,
-                widget_alloc.X, widget_alloc.Y, 0, 0, widget_alloc.Width, widget_alloc.Height);
+            // Copy the widget's pixels to surface, we'll use it to draw the animation
+            surface = Window.CreateSimilarSurface (Cairo.Content.ColorAlpha, widget_alloc.Width, widget_alloc.Height);
+            var cr = new Cairo.Context (surface);
+            Gdk.CairoHelper.SetSourceWindow (cr, Window, widget_alloc.X, widget_alloc.Y);
+            cr.Rectangle (0, 0, widget_alloc.Width, widget_alloc.Height);
+            cr.Fill ();
 
             if (AnimationState != AnimationState.Going) {
                 WidgetDestroyed (this, args);
-            }*/
+            }
+
+            ((IDisposable)cr).Dispose ();
         }
 
 #region Overrides
@@ -190,11 +196,15 @@ namespace Hyena.Widgets
 
         protected override bool OnDrawn (Cairo.Context cr)
         {
-/*            if (canvas != null) {
-                Window.DrawDrawable (Style.BackgroundGC (State), canvas,
-                    0, 0, widget_alloc.X, widget_alloc.Y, widget_alloc.Width, widget_alloc.Height);
+            if (surface != null) {
+                cr.Save ();
+                Gtk.CairoHelper.TransformToWindow (cr, this, Window);
+                cr.SetSource (surface);
+                cr.Rectangle (widget_alloc.X, widget_alloc.Y, widget_alloc.Width, widget_alloc.Height);
+                cr.Fill ();
+                cr.Restore ();
                 return true;
-            } else*/ {
+            } else {
                 return base.OnDrawn (cr);
             }
         }



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