gtk-demo: Rewrite the zoom demo slightly
authorMatthias Clasen <mclasen@redhat.com>
Wed, 8 Feb 2023 00:40:47 +0000 (19:40 -0500)
committerMatthias Clasen <mclasen@redhat.com>
Sat, 11 Feb 2023 20:09:38 +0000 (15:09 -0500)
Use a texture directly instead of a paintable.
This will be used in the following commit to
introduce filters.

demos/gtk-demo/demo3widget.c

index 9f8d943d14f005437906803e462a6f8dcbf03a8b..abf2a09b631e34a586a958c09f211995c7deb876 100644 (file)
@@ -3,7 +3,7 @@
 
 enum
 {
-  PROP_PAINTABLE = 1,
+  PROP_TEXTURE = 1,
   PROP_SCALE
 };
 
@@ -11,7 +11,7 @@ struct _Demo3Widget
 {
   GtkWidget parent_instance;
 
-  GdkPaintable *paintable;
+  GdkTexture *texture;
   float scale;
 
   GtkWidget *menu;
@@ -36,7 +36,7 @@ demo3_widget_dispose (GObject *object)
 {
   Demo3Widget *self = DEMO3_WIDGET (object);
 
-  g_clear_object (&self->paintable);
+  g_clear_object (&self->texture);
 
   gtk_widget_dispose_template (GTK_WIDGET (self), DEMO3_TYPE_WIDGET);
 
@@ -54,8 +54,8 @@ demo3_widget_snapshot (GtkWidget   *widget,
   width = gtk_widget_get_width (widget);
   height = gtk_widget_get_height (widget);
 
-  w = self->scale * gdk_paintable_get_intrinsic_width (self->paintable);
-  h = self->scale * gdk_paintable_get_intrinsic_height (self->paintable);
+  w = self->scale * gdk_texture_get_width (self->texture);
+  h = self->scale * gdk_texture_get_height (self->texture);
 
   x = MAX (0, (width - ceil (w)) / 2);
   y = MAX (0, (height - ceil (h)) / 2);
@@ -63,7 +63,7 @@ demo3_widget_snapshot (GtkWidget   *widget,
   gtk_snapshot_push_clip (snapshot, &GRAPHENE_RECT_INIT (0, 0, width, height));
   gtk_snapshot_save (snapshot);
   gtk_snapshot_translate (snapshot, &GRAPHENE_POINT_INIT (x, y));
-  gdk_paintable_snapshot (self->paintable, snapshot, w, h);
+  gtk_snapshot_append_texture (snapshot, self->texture, &GRAPHENE_RECT_INIT (0, 0, w, h));
   gtk_snapshot_restore (snapshot);
   gtk_snapshot_pop (snapshot);
 }
@@ -81,9 +81,9 @@ demo3_widget_measure (GtkWidget      *widget,
   int size;
 
   if (orientation == GTK_ORIENTATION_HORIZONTAL)
-    size = gdk_paintable_get_intrinsic_width (self->paintable);
+    size = gdk_texture_get_width (self->texture);
   else
-    size = gdk_paintable_get_intrinsic_height (self->paintable);
+    size = gdk_texture_get_height (self->texture);
 
   *minimum = *natural = self->scale * size;
 }
@@ -113,9 +113,9 @@ demo3_widget_set_property (GObject      *object,
 
   switch (prop_id)
     {
-    case PROP_PAINTABLE:
-      g_clear_object (&self->paintable);
-      self->paintable = g_value_dup_object (value);
+    case PROP_TEXTURE:
+      g_clear_object (&self->texture);
+      self->texture = g_value_dup_object (value);
       gtk_widget_queue_resize (GTK_WIDGET (object));
       break;
 
@@ -140,8 +140,8 @@ demo3_widget_get_property (GObject     *object,
 
   switch (prop_id)
     {
-    case PROP_PAINTABLE:
-      g_value_set_object (value, self->paintable);
+    case PROP_TEXTURE:
+      g_value_set_object (value, self->texture);
       break;
 
     case PROP_SCALE:
@@ -205,13 +205,13 @@ demo3_widget_class_init (Demo3WidgetClass *class)
   widget_class->measure = demo3_widget_measure;
   widget_class->size_allocate = demo3_widget_size_allocate;
 
-  g_object_class_install_property (object_class, PROP_PAINTABLE,
-      g_param_spec_object ("paintable", "Paintable", "Paintable",
-                           GDK_TYPE_PAINTABLE,
+  g_object_class_install_property (object_class, PROP_TEXTURE,
+      g_param_spec_object ("texture", NULL, NULL,
+                           GDK_TYPE_TEXTURE,
                            G_PARAM_READWRITE));
 
   g_object_class_install_property (object_class, PROP_SCALE,
-      g_param_spec_float ("scale", "Scale", "Scale",
+      g_param_spec_float ("scale", NULL, NULL,
                           0.0, 10.0, 1.0,
                           G_PARAM_READWRITE));
 
@@ -229,16 +229,13 @@ GtkWidget *
 demo3_widget_new (const char *resource)
 {
   Demo3Widget *self;
-  GdkPixbuf *pixbuf;
-  GdkPaintable *paintable;
+  GdkTexture *texture;
 
-  pixbuf = gdk_pixbuf_new_from_resource (resource, NULL);
-  paintable = GDK_PAINTABLE (gdk_texture_new_for_pixbuf (pixbuf));
+  texture = gdk_texture_new_from_resource (resource);
 
-  self = g_object_new (DEMO3_TYPE_WIDGET, "paintable", paintable, NULL);
+  self = g_object_new (DEMO3_TYPE_WIDGET, "texture", texture, NULL);
 
-  g_object_unref (pixbuf);
-  g_object_unref (paintable);
+  g_object_unref (texture);
 
   return GTK_WIDGET (self);
 }