[gtk+/wip/baedert/gtkimageview: 101/113] GtkImageView: Add _set_surface



commit 014e02d015adfff8264fad60c81a24a3469176a9
Author: Timm Bäder <mail baedert org>
Date:   Sat Jul 18 18:49:06 2015 +0200

    GtkImageView: Add _set_surface
    
    and another horribly boring demo for it

 demos/gtk-demo/image_view.c  |   18 +++++++++++
 demos/gtk-demo/image_view.ui |   32 +++++++++++++++------
 gtk/gtkimageview.c           |   65 ++++++++++++++++++++++++++++++++++--------
 gtk/gtkimageview.h           |    8 ++---
 4 files changed, 97 insertions(+), 26 deletions(-)
---
diff --git a/demos/gtk-demo/image_view.c b/demos/gtk-demo/image_view.c
index 332e13e..3fbb18b 100644
--- a/demos/gtk-demo/image_view.c
+++ b/demos/gtk-demo/image_view.c
@@ -131,6 +131,24 @@ load_pixbuf_button_clicked_cb ()
 }
 
 
+void
+load_surface_button_clicked_cb ()
+{
+  GdkPixbuf *pixbuf;
+  cairo_surface_t *surface;
+
+  /* I really hope you have this. */
+  pixbuf = gdk_pixbuf_new_from_file ("/usr/share/backgrounds/gnome/adwaita-day.jpg",
+                                     NULL);
+
+  g_assert (pixbuf != NULL);
+
+  surface = gdk_cairo_surface_create_from_pixbuf (pixbuf, 1, NULL);
+
+  gtk_image_view_set_surface (GTK_IMAGE_VIEW (image_view), surface);
+}
+
+
 
 GtkWidget *
 do_image_view (GtkWidget *do_widget)
diff --git a/demos/gtk-demo/image_view.ui b/demos/gtk-demo/image_view.ui
index 8945697..fbbc87f 100644
--- a/demos/gtk-demo/image_view.ui
+++ b/demos/gtk-demo/image_view.ui
@@ -77,6 +77,20 @@
           </packing>
         </child>
 
+        <child>
+          <object class="GtkButton">
+            <property name="visible">true</property>
+            <property name="label" translatable="yes">Load Surface</property>
+            <signal name="clicked" handler="load_surface_button_clicked_cb" />
+          </object>
+          <packing>
+            <property name="left_attach">0</property>
+            <property name="top_attach">3</property>
+            <property name="width">2</property>
+          </packing>
+        </child>
+
+
 
 
         <child>
@@ -86,7 +100,7 @@
           </object>
           <packing>
             <property name="left_attach">0</property>
-            <property name="top_attach">3</property>
+            <property name="top_attach">4</property>
             <property name="width">2</property>
           </packing>
         </child>
@@ -100,7 +114,7 @@
           </object>
           <packing>
             <property name="left_attach">0</property>
-            <property name="top_attach">4</property>
+            <property name="top_attach">5</property>
             <property name="width">2</property>
           </packing>
         </child>
@@ -115,7 +129,7 @@
           </object>
           <packing>
             <property name="left_attach">0</property>
-            <property name="top_attach">5</property>
+            <property name="top_attach">6</property>
             <property name="width">2</property>
           </packing>
         </child>
@@ -130,7 +144,7 @@
           </object>
           <packing>
             <property name="left_attach">0</property>
-            <property name="top_attach">6</property>
+            <property name="top_attach">7</property>
             <property name="width">2</property>
           </packing>
         </child>
@@ -143,7 +157,7 @@
           </object>
           <packing>
             <property name="left_attach">0</property>
-            <property name="top_attach">7</property>
+            <property name="top_attach">8</property>
             <property name="width">2</property>
           </packing>
         </child>
@@ -157,7 +171,7 @@
           </object>
           <packing>
             <property name="left_attach">0</property>
-            <property name="top_attach">8</property>
+            <property name="top_attach">9</property>
             <property name="width">2</property>
           </packing>
         </child>
@@ -200,7 +214,7 @@
           </object>
           <packing>
             <property name="left_attach">0</property>
-            <property name="top_attach">9</property>
+            <property name="top_attach">10</property>
             <property name="width">2</property>
           </packing>
         </child>
@@ -215,7 +229,7 @@
           </object>
           <packing>
             <property name="left_attach">0</property>
-            <property name="top_attach">10</property>
+            <property name="top_attach">11</property>
             <property name="width">2</property>
           </packing>
         </child>
@@ -229,7 +243,7 @@
           </object>
           <packing>
             <property name="left_attach">0</property>
-            <property name="top_attach">11</property>
+            <property name="top_attach">12</property>
             <property name="width">2</property>
           </packing>
         </child>
diff --git a/gtk/gtkimageview.c b/gtk/gtkimageview.c
index cf4bd8e..6b9a533 100644
--- a/gtk/gtkimageview.c
+++ b/gtk/gtkimageview.c
@@ -837,6 +837,9 @@ gtk_image_view_map (GtkWidget *widget)
 {
   GtkImageViewPrivate *priv = gtk_image_view_get_instance_private ((GtkImageView *)widget);
 
+  if (priv->is_animation)
+    gtk_image_view_start_animation ((GtkImageView *)widget);
+
   GTK_WIDGET_CLASS (gtk_image_view_parent_class)->map (widget);
 }
 
@@ -846,6 +849,9 @@ gtk_image_view_unmap (GtkWidget *widget)
   GtkImageViewPrivate *priv = gtk_image_view_get_instance_private ((GtkImageView *)widget);
 
 
+  if (priv->is_animation)
+    gtk_image_view_stop_animation ((GtkImageView *)widget);
+
   GTK_WIDGET_CLASS (gtk_image_view_parent_class)->unmap (widget);
 }
 
@@ -1108,6 +1114,23 @@ gtk_image_view_new ()
 
 
 static void
+gtk_image_view_replace_surface (GtkImageView    *image_view,
+                                cairo_surface_t *surface,
+                                int              scale_factor)
+{
+  GtkImageViewPrivate *priv = gtk_image_view_get_instance_private (image_view);
+
+  if (priv->image_surface)
+    cairo_surface_destroy (priv->image_surface);
+
+  priv->scale_factor = scale_factor;
+  priv->image_surface = surface;
+  cairo_surface_reference (priv->image_surface);
+  priv->surface_width = cairo_image_surface_get_width (priv->image_surface);
+  priv->surface_height = cairo_image_surface_get_height (priv->image_surface);
+}
+
+static void
 gtk_image_view_update_surface (GtkImageView    *image_view,
                                const GdkPixbuf *frame,
                                int              scale_factor)
@@ -1126,19 +1149,13 @@ gtk_image_view_update_surface (GtkImageView    *image_view,
     {
       GdkWindow *window = gtk_widget_get_window ((GtkWidget *)image_view);
       int surface_scale = gtk_widget_get_scale_factor ((GtkWidget *)image_view);
+      cairo_surface_t *new_surface = gdk_cairo_surface_create_from_pixbuf (frame,
+                                                                           surface_scale,
+                                                                           window);
 
-      if (priv->image_surface)
-        cairo_surface_destroy (priv->image_surface);
-
-      priv->scale_factor  = scale_factor;
-      priv->image_surface = gdk_cairo_surface_create_from_pixbuf (frame,
-                                                                  surface_scale,
-                                                                  window);
-
-
-      cairo_surface_reference (priv->image_surface);
-      priv->surface_width  = real_width;
-      priv->surface_height = real_height;
+      gtk_image_view_replace_surface (image_view,
+                                      new_surface,
+                                      surface_scale);
     }
   else
     {
@@ -1350,3 +1367,27 @@ gtk_image_view_set_pixbuf (GtkImageView    *image_view,
   gtk_image_view_update_surface (image_view, pixbuf, scale_factor);
 }
 
+void
+gtk_image_view_set_surface (GtkImageView    *image_view,
+                            cairo_surface_t *surface)
+{
+  GtkImageViewPrivate *priv = gtk_image_view_get_instance_private (image_view);
+  double scale_x;
+  double scale_y;
+  g_return_if_fail (GTK_IS_IMAGE_VIEW (image_view));
+  g_return_if_fail (surface != NULL);
+  g_return_if_fail (cairo_surface_get_type (surface) == CAIRO_SURFACE_TYPE_IMAGE);
+
+
+  cairo_surface_get_device_scale (surface, &scale_x, &scale_y);
+  g_assert (scale_x == scale_y); /* XXX Legal? */
+
+  gtk_image_view_replace_surface (image_view,
+                                  surface,
+                                  scale_x);
+
+  if (priv->fit_allocation)
+    gtk_widget_queue_draw ((GtkWidget *)image_view);
+  else
+    gtk_widget_queue_resize ((GtkWidget *)image_view);
+}
diff --git a/gtk/gtkimageview.h b/gtk/gtkimageview.h
index 9d8888e..973f2c4 100644
--- a/gtk/gtkimageview.h
+++ b/gtk/gtkimageview.h
@@ -42,12 +42,14 @@ GType         gtk_image_view_get_type (void) G_GNUC_CONST;
 GDK_AVAILABLE_IN_3_18
 GtkWidget *   gtk_image_view_new      ();
 
-
 GDK_AVAILABLE_IN_3_18
 void gtk_image_view_set_pixbuf (GtkImageView    *image_view,
                                 const GdkPixbuf *pixbuf,
                                 int              scale_factor);
 
+GDK_AVAILABLE_IN_3_18
+void gtk_image_view_set_surface (GtkImageView    *image_view,
+                                 cairo_surface_t *surface);
 
 /* Loading {{{ */
 
@@ -130,10 +132,6 @@ void gtk_image_view_set_zoom_gesture_enabled (GtkImageView *image_view,
 GDK_AVAILABLE_IN_3_18
 gboolean gtk_image_view_get_zoom_gesture_enabled (GtkImageView *image_view);
 
-// XXX Adding a gtk_image_view_set_pixbuf would work, but we are working with animations internally...
-
-
-
 /* }}} */
 
 G_END_DECLS


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