image: Add gtk_image_set_can_shrink()
authorBenjamin Otte <otte@redhat.com>
Thu, 15 Mar 2018 17:48:44 +0000 (18:48 +0100)
committerBenjamin Otte <otte@redhat.com>
Fri, 16 Mar 2018 05:04:45 +0000 (06:04 +0100)
Images with that value set will request a 0x0 minimum size and scale
down their contents.

docs/reference/gtk/gtk4-sections.txt
gtk/gtkimage.c
gtk/gtkimage.h

index 9bfaf9bad547285978d4f66780c4428368bebdee..6c84a75e8a9293b0a39cd3368a6bb7a6968720f6 100644 (file)
@@ -1507,6 +1507,8 @@ gtk_image_set_icon_size
 gtk_image_get_icon_size
 gtk_image_set_keep_aspect_ratio
 gtk_image_get_keep_aspect_ratio
+gtk_image_set_can_shrink
+gtk_image_get_can_shrink
 <SUBSECTION Standard>
 GTK_IMAGE
 GTK_IS_IMAGE
index 3d31eb3242e7ef3b0af76e8fbd21fa6f4537bf2d..8a8a8235f565657d08aefba9cd0422c6c51dad13 100644 (file)
@@ -88,6 +88,7 @@ struct _GtkImagePrivate
   gchar                *resource_path;  /* Only used with GTK_IMAGE_SURFACE */
 
   guint keep_aspect_ratio : 1;
+  guint can_shrink : 1;
 };
 
 
@@ -132,6 +133,7 @@ enum
   PROP_RESOURCE,
   PROP_USE_FALLBACK,
   PROP_KEEP_ASPECT_RATIO,
+  PROP_CAN_SHRINK,
   NUM_PROPERTIES
 };
 
@@ -277,6 +279,18 @@ gtk_image_class_init (GtkImageClass *class)
                             TRUE,
                             GTK_PARAM_READWRITE|G_PARAM_EXPLICIT_NOTIFY);
 
+  /**
+   * GtkImage:can-shrink
+   *
+   * If the #GtkImage can be made smaller than the image it contains.
+   */
+  image_props[PROP_CAN_SHRINK] =
+      g_param_spec_boolean ("can-shrink",
+                            P_("Can shrink"),
+                            P_("Allow image to be smaller than contents"),
+                            FALSE,
+                            GTK_PARAM_READWRITE|G_PARAM_EXPLICIT_NOTIFY);
+
   g_object_class_install_properties (gobject_class, NUM_PROPERTIES, image_props);
 
   gtk_widget_class_set_accessible_type (widget_class, GTK_TYPE_IMAGE_ACCESSIBLE);
@@ -357,6 +371,10 @@ gtk_image_set_property (GObject      *object,
       gtk_image_set_keep_aspect_ratio (image, g_value_get_boolean (value));
       break;
 
+    case PROP_CAN_SHRINK:
+      gtk_image_set_can_shrink (image, g_value_get_boolean (value));
+      break;
+
     default:
       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
       break;
@@ -404,6 +422,9 @@ gtk_image_get_property (GObject     *object,
     case PROP_KEEP_ASPECT_RATIO:
       g_value_set_boolean (value, priv->keep_aspect_ratio);
       break;
+    case PROP_CAN_SHRINK:
+      g_value_set_boolean (value, priv->can_shrink);
+      break;
     case PROP_STORAGE_TYPE:
       g_value_set_enum (value, _gtk_icon_helper_get_storage_type (priv->icon_helper));
       break;
@@ -1391,6 +1412,9 @@ gtk_image_measure (GtkWidget      *widget,
                            for_size,
                            minimum, natural);
 
+  if (priv->can_shrink)
+    *minimum = 0;
+
   if (orientation == GTK_ORIENTATION_VERTICAL)
     {
       baseline_align = gtk_image_get_baseline_align (GTK_IMAGE (widget));
@@ -1545,6 +1569,54 @@ gtk_image_get_keep_aspect_ratio (GtkImage *image)
   return priv->keep_aspect_ratio;
 }
 
+/**
+ * gtk_image_set_can_shrink:
+ * @image: a #GtkImage
+ * @can_shrink: if the @image can be made smaller than its contents
+ *
+ * If set to %TRUE, the @image can be made smaller than its contents.
+ * The contents will be scaled down when rendering.
+ *
+ * If you want to still force a minimum size manually, consider using
+ * gtk_widget_set_size_request().
+ *
+ * Also of note is that a similar function for growing does not exist
+ * because the grow behavior can be controlled via
+ * gtk_widget_set_halign() and gtk_widget_set_valign().
+ */
+void
+gtk_image_set_can_shrink (GtkImage *image,
+                          gboolean  can_shrink)
+{
+  GtkImagePrivate *priv = gtk_image_get_instance_private (image);
+
+  g_return_if_fail (GTK_IS_IMAGE (image));
+
+  if (priv->can_shrink == can_shrink)
+    return;
+
+  priv->can_shrink = can_shrink;
+  g_object_notify_by_pspec (G_OBJECT (image), image_props[PROP_CAN_SHRINK]);
+}
+
+/**
+ * gtk_image_get_can_shrink:
+ * @image: a #GtkImage
+ *
+ * Gets the value set via gtk_image_set_can_shrink().
+ *
+ * Returns: %TRUE if the image can be made smaller than its contents
+ **/
+gboolean
+gtk_image_get_can_shrink (GtkImage *image)
+{
+  GtkImagePrivate *priv = gtk_image_get_instance_private (image);
+
+  g_return_val_if_fail (GTK_IS_IMAGE (image), FALSE);
+
+  return priv->can_shrink;
+}
+
 void
 gtk_image_get_image_size (GtkImage *image,
                           int      *width,
index d9fa53313644ecf7420e8fdb66afc0178f87ac1c..6d5a8309b0db3c1364a8e21fbf0981f0f9edca41 100644 (file)
@@ -151,6 +151,9 @@ void gtk_image_set_icon_size      (GtkImage        *image,
 GDK_AVAILABLE_IN_ALL
 void gtk_image_set_keep_aspect_ratio (GtkImage     *image,
                                       gboolean      keep_aspect_ratio);
+GDK_AVAILABLE_IN_ALL
+void gtk_image_set_can_shrink     (GtkImage        *image,
+                                   gboolean         can_shrink);
 
 GDK_AVAILABLE_IN_ALL
 GtkImageType gtk_image_get_storage_type (GtkImage   *image);
@@ -170,6 +173,8 @@ GDK_AVAILABLE_IN_ALL
 GtkIconSize gtk_image_get_icon_size (GtkImage             *image);
 GDK_AVAILABLE_IN_ALL
 gboolean   gtk_image_get_keep_aspect_ratio (GtkImage      *image);
+GDK_AVAILABLE_IN_ALL
+gboolean   gtk_image_get_can_shrink (GtkImage             *image);
 
 G_END_DECLS