[gtk+/wip/baedert/gtkimageview: 2068/2075] Use GtkPlayable to start/stop pixbuf animations



commit 3fa2d2d430d250fae2c8becdd984752205ccc15d
Author: Timm Bäder <mail baedert org>
Date:   Sat Feb 27 15:40:28 2016 +0100

    Use GtkPlayable to start/stop pixbuf animations

 gtk/gtkabstractimage.c |   61 ++++++++++++++++++++++++++++++++++++++++++-----
 gtk/gtkabstractimage.h |   40 +++++++++++++++++++++++++++++-
 gtk/gtkimageview.c     |    9 +++++++
 3 files changed, 101 insertions(+), 9 deletions(-)
---
diff --git a/gtk/gtkabstractimage.c b/gtk/gtkabstractimage.c
index 1cf43e9..1256df5 100644
--- a/gtk/gtkabstractimage.c
+++ b/gtk/gtkabstractimage.c
@@ -83,8 +83,40 @@ gtk_abstract_image_get_scale_factor (GtkAbstractImage *image)
 
 /* }}} */
 
+/* GtkPlayable {{{ */
+G_DEFINE_TYPE (GtkPlayable, gtk_playable, GTK_TYPE_ABSTRACT_IMAGE)
+
+void
+gtk_playable_start (GtkPlayable *p)
+{
+  g_return_if_fail (GTK_IS_PLAYABLE (p));
+
+  GTK_PLAYABLE_GET_CLASS (p)->start (p);
+}
+
+void
+gtk_playable_stop (GtkPlayable *p)
+{
+  g_return_if_fail (GTK_IS_PLAYABLE (p));
+
+  GTK_PLAYABLE_GET_CLASS (p)->stop (p);
+}
+
+static void
+gtk_playable_init (GtkPlayable *p)
+{
+
+}
+
+static void
+gtk_playable_class_init (GtkPlayableClass *p_class)
+{
+
+}
+/* }}} */
+
 /* GtkPixbufAnimationImage {{{ */
-G_DEFINE_TYPE (GtkPixbufAnimationImage, gtk_pixbuf_animation_image, GTK_TYPE_ABSTRACT_IMAGE)
+G_DEFINE_TYPE (GtkPixbufAnimationImage, gtk_pixbuf_animation_image, GTK_TYPE_PLAYABLE)
 
 GtkPixbufAnimationImage *
 gtk_pixbuf_animation_image_new (GdkPixbufAnimation *animation, int scale_factor)
@@ -140,16 +172,27 @@ gtk_pixbuf_animation_image_draw (GtkAbstractImage *_image, cairo_t *ct)
 {
   GtkPixbufAnimationImage *image = GTK_PIXBUF_ANIMATION_IMAGE (_image);
 
-  /* We start the animation at the first draw() call... */
-  if (G_UNLIKELY (image->timeout_id == 0))
-    {
-      image->timeout_id = g_timeout_add (image->delay_ms, gtk_pixbuf_animation_image_advance, image);
-    }
-
   cairo_set_source_surface (ct, image->frame, 0, 0);
 }
 
 static void
+gtk_pixbuf_animation_image_start (GtkPlayable *p)
+{
+  GtkPixbufAnimationImage *image = GTK_PIXBUF_ANIMATION_IMAGE (p);
+
+  image->timeout_id = g_timeout_add (image->delay_ms, gtk_pixbuf_animation_image_advance, image);
+}
+
+static void
+gtk_pixbuf_animation_image_stop (GtkPlayable *p)
+{
+  GtkPixbufAnimationImage *image = GTK_PIXBUF_ANIMATION_IMAGE (p);
+
+  g_source_remove (image->timeout_id);
+  image->timeout_id = 0;
+}
+
+static void
 gtk_pixbuf_animation_image_init (GtkPixbufAnimationImage *image)
 {
   image->timeout_id = 0;
@@ -159,11 +202,15 @@ static void
 gtk_pixbuf_animation_image_class_init (GtkPixbufAnimationImageClass *klass)
 {
   GtkAbstractImageClass *image_class = GTK_ABSTRACT_IMAGE_CLASS (klass);
+  GtkPlayableClass *p_class = GTK_PLAYABLE_CLASS (klass);
 
   image_class->get_width = gtk_pixbuf_animation_image_get_width;
   image_class->get_height = gtk_pixbuf_animation_image_get_height;
   image_class->get_scale_factor = gtk_pixbuf_animation_image_get_scale_factor;
   image_class->draw = gtk_pixbuf_animation_image_draw;
+
+  p_class->start = gtk_pixbuf_animation_image_start;
+  p_class->stop = gtk_pixbuf_animation_image_stop;
 }
 /* }}} */
 
diff --git a/gtk/gtkabstractimage.h b/gtk/gtkabstractimage.h
index 89bc5cf..45ac95c 100644
--- a/gtk/gtkabstractimage.h
+++ b/gtk/gtkabstractimage.h
@@ -67,6 +67,42 @@ int gtk_abstract_image_get_scale_factor (GtkAbstractImage *image);
 
 /* ------------------------------------------------------------------------------------ */
 
+typedef struct _GtkPlayable GtkPlayable;
+typedef struct _GtkPlayableClass GtkPlayableClass;
+
+#define GTK_TYPE_PLAYABLE           (gtk_playable_get_type ())
+#define GTK_PLAYABLE(obj)           (G_TYPE_CHECK_INSTANCE_CAST (obj, GTK_TYPE_PLAYABLE, GtkPlayable))
+#define GTK_PLAYABLE_CLASS(cls)     (G_TYPE_CHECK_CLASS_CAST (cls, GTK_TYPE_PLAYABLE, GtkPlayableClass))
+#define GTK_IS_PLAYABLE(obj)        (G_TYPE_CHECK_INSTANCE_TYPE (obj, GTK_TYPE_PLAYABLE))
+#define GTK_IS_PLAYABLE_CLASS(obj)  (G_TYPE_CHECK_CLASS_TYPE (obj, GTK_TYPE_PLAYABLE))
+#define GTK_PLAYABLE_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GTK_TYPE_PLAYABLE, GtkPlayableClass))
+
+
+struct _GtkPlayable
+{
+  GtkAbstractImage parent;
+};
+
+
+struct _GtkPlayableClass
+{
+  GtkAbstractImageClass parent_class;
+
+  void (*start) (GtkPlayable *playable);
+  void (*stop)  (GtkPlayable *playable);
+};
+
+GDK_AVAILABLE_IN_3_20
+GType gtk_playable_get_type (void) G_GNUC_CONST;
+
+GDK_AVAILABLE_IN_3_20
+void gtk_playable_start (GtkPlayable *p);
+
+GDK_AVAILABLE_IN_3_20
+void gtk_playable_stop (GtkPlayable *p);
+
+/* ------------------------------------------------------------------------------------ */
+
 
 typedef struct _GtkPixbufAnimationImage GtkPixbufAnimationImage;
 typedef struct _GtkPixbufAnimationImageClass GtkPixbufAnimationImageClass;
@@ -80,7 +116,7 @@ typedef struct _GtkPixbufAnimationImageClass GtkPixbufAnimationImageClass;
 
 struct _GtkPixbufAnimationImage
 {
-  GtkAbstractImage parent;
+  GtkPlayable parent;
   GdkPixbufAnimation *animation;
   GdkPixbufAnimationIter *iter;
   cairo_surface_t *frame;
@@ -91,7 +127,7 @@ struct _GtkPixbufAnimationImage
 
 struct _GtkPixbufAnimationImageClass
 {
-  GtkAbstractImageClass parent_class;
+  GtkPlayableClass parent_class;
 };
 
 GDK_AVAILABLE_IN_3_20
diff --git a/gtk/gtkimageview.c b/gtk/gtkimageview.c
index 1686568..280b535 100644
--- a/gtk/gtkimageview.c
+++ b/gtk/gtkimageview.c
@@ -1720,6 +1720,9 @@ gtk_image_view_map (GtkWidget *widget)
   if (priv->event_window)
     gdk_window_show (priv->event_window);
 
+  if (GTK_IS_PLAYABLE (priv->image))
+    gtk_playable_start (GTK_PLAYABLE (priv->image));
+
   GTK_WIDGET_CLASS (gtk_image_view_parent_class)->map (widget);
 }
 
@@ -1731,6 +1734,9 @@ gtk_image_view_unmap (GtkWidget *widget)
   if (priv->event_window)
     gdk_window_hide (priv->event_window);
 
+  if (GTK_IS_PLAYABLE (priv->image))
+    gtk_playable_stop (GTK_PLAYABLE (priv->image));
+
   GTK_WIDGET_CLASS (gtk_image_view_parent_class)->unmap (widget);
 }
 
@@ -2126,6 +2132,9 @@ gtk_image_view_replace_image (GtkImageView     *image_view,
       g_signal_connect (priv->image, "changed", G_CALLBACK (image_changed_cb), image_view);
     }
 
+  if (GTK_IS_PLAYABLE (priv->image) && gtk_widget_get_mapped (GTK_WIDGET (image_view)))
+    gtk_playable_start (GTK_PLAYABLE (priv->image));
+
   gtk_image_view_update_adjustments (image_view);
 
   if (priv->fit_allocation)


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