]> Pileus Git - ~andy/gtk/blobdiff - gtk/gtkimage.c
GtkTextView: don't popdown a bubble if we don't have one
[~andy/gtk] / gtk / gtkimage.c
index 5f392deebbaddee10e2a70e8a2ca5eb2c75b2cc5..d373b51b709d81586f61af595b9504d836775b3e 100644 (file)
@@ -12,9 +12,7 @@
  * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public
- * License along with this library; if not, write to the
- * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
- * Boston, MA 02111-1307, USA.
+ * License along with this library. If not, see <http://www.gnu.org/licenses/>.
  */
 
 /*
  *
  * Sometimes an application will want to avoid depending on external data
  * files, such as image files. GTK+ comes with a program to avoid this,
- * called <application>gdk-pixbuf-csource</application>. This program
+ * called <application>gdk-pixbuf-csource</application>. This library
  * allows you to convert an image into a C variable declaration, which
  * can then be loaded into a #GdkPixbuf using
  * gdk_pixbuf_new_from_inline().
@@ -141,6 +139,7 @@ struct _GtkImagePrivate
   GdkPixbufAnimationIter *animation_iter;
 
   gchar                *filename;       /* Only used with GTK_IMAGE_ANIMATION, GTK_IMAGE_PIXBUF */
+  gchar                *resource_path;  /* Only used with GTK_IMAGE_PIXBUF */
 };
 
 
@@ -159,7 +158,7 @@ static void gtk_image_get_preferred_height (GtkWidget    *widget,
 static void gtk_image_style_updated        (GtkWidget    *widget);
 static void gtk_image_screen_changed       (GtkWidget    *widget,
                                             GdkScreen    *prev_screen);
-static void gtk_image_destroy              (GtkWidget    *widget);
+static void gtk_image_finalize             (GObject      *object);
 static void gtk_image_reset                (GtkImage     *image);
 
 static void gtk_image_set_property         (GObject      *object,
@@ -186,6 +185,7 @@ enum
   PROP_ICON_NAME,
   PROP_STORAGE_TYPE,
   PROP_GICON,
+  PROP_RESOURCE,
   PROP_USE_FALLBACK
 };
 
@@ -201,10 +201,10 @@ gtk_image_class_init (GtkImageClass *class)
   
   gobject_class->set_property = gtk_image_set_property;
   gobject_class->get_property = gtk_image_get_property;
+  gobject_class->finalize = gtk_image_finalize;
 
   widget_class = GTK_WIDGET_CLASS (class);
   widget_class->draw = gtk_image_draw;
-  widget_class->destroy = gtk_image_destroy;
   widget_class->get_preferred_width = gtk_image_get_preferred_width;
   widget_class->get_preferred_height = gtk_image_get_preferred_height;
   widget_class->unmap = gtk_image_unmap;
@@ -311,7 +311,22 @@ gtk_image_class_init (GtkImageClass *class)
                                                         P_("The GIcon being displayed"),
                                                         G_TYPE_ICON,
                                                         GTK_PARAM_READWRITE));
-  
+
+  /**
+   * GtkImage:resource:
+   *
+   * A path to a resource file to display.
+   *
+   * Since: 3.8
+   */
+  g_object_class_install_property (gobject_class,
+                                   PROP_RESOURCE,
+                                   g_param_spec_string ("resource",
+                                                        P_("Resource"),
+                                                        P_("The resource path being displayed"),
+                                                        NULL,
+                                                        GTK_PARAM_READWRITE));
+
   g_object_class_install_property (gobject_class,
                                    PROP_STORAGE_TYPE,
                                    g_param_spec_enum ("storage-type",
@@ -361,14 +376,16 @@ gtk_image_init (GtkImage *image)
 }
 
 static void
-gtk_image_destroy (GtkWidget *widget)
+gtk_image_finalize (GObject *object)
 {
-  GtkImage *image = GTK_IMAGE (widget);
+  GtkImage *image = GTK_IMAGE (object);
 
   g_clear_object (&image->priv->icon_helper);
+  
+  g_free (image->priv->filename);
 
-  GTK_WIDGET_CLASS (gtk_image_parent_class)->destroy (widget);
-}
+  G_OBJECT_CLASS (gtk_image_parent_class)->finalize (object);
+};
 
 static void 
 gtk_image_set_property (GObject      *object,
@@ -380,6 +397,9 @@ gtk_image_set_property (GObject      *object,
   GtkImagePrivate *priv = image->priv;
   GtkIconSize icon_size = _gtk_icon_helper_get_icon_size (priv->icon_helper);
 
+  if (icon_size == GTK_ICON_SIZE_INVALID)
+    icon_size = DEFAULT_ICON_SIZE;
+
   switch (prop_id)
     {
     case PROP_PIXBUF:
@@ -415,6 +435,9 @@ gtk_image_set_property (GObject      *object,
       gtk_image_set_from_gicon (image, g_value_get_object (value),
                                icon_size);
       break;
+    case PROP_RESOURCE:
+      gtk_image_set_from_resource (image, g_value_get_string (value));
+      break;
 
     case PROP_USE_FALLBACK:
       _gtk_icon_helper_set_use_fallback (priv->icon_helper, g_value_get_boolean (value));
@@ -444,7 +467,7 @@ gtk_image_get_property (GObject     *object,
       g_value_set_string (value, priv->filename);
       break;
     case PROP_STOCK:
-      g_value_set_string (value, _gtk_icon_helper_get_icon_name (priv->icon_helper));
+      g_value_set_string (value, _gtk_icon_helper_get_stock_id (priv->icon_helper));
       break;
     case PROP_ICON_SET:
       g_value_set_boxed (value, _gtk_icon_helper_peek_icon_set (priv->icon_helper));
@@ -464,9 +487,15 @@ gtk_image_get_property (GObject     *object,
     case PROP_GICON:
       g_value_set_object (value, _gtk_icon_helper_peek_gicon (priv->icon_helper));
       break;
+    case PROP_RESOURCE:
+      g_value_set_string (value, priv->resource_path);
+      break;
     case PROP_USE_FALLBACK:
       g_value_set_boolean (value, _gtk_icon_helper_get_use_fallback (priv->icon_helper));
       break;
+    case PROP_STORAGE_TYPE:
+      g_value_set_enum (value, _gtk_icon_helper_get_storage_type (priv->icon_helper));
+      break;
     default:
       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
       break;
@@ -509,6 +538,43 @@ gtk_image_new_from_file   (const gchar *filename)
   return GTK_WIDGET (image);
 }
 
+/**
+ * gtk_image_new_from_resource:
+ * @resource_path: a resource path
+ *
+ * Creates a new #GtkImage displaying the resource file @resource_path. If the file
+ * isn't found or can't be loaded, the resulting #GtkImage will
+ * display a "broken image" icon. This function never returns %NULL,
+ * it always returns a valid #GtkImage widget.
+ *
+ * If the file contains an animation, the image will contain an
+ * animation.
+ *
+ * If you need to detect failures to load the file, use
+ * gdk_pixbuf_new_from_file() to load the file yourself, then create
+ * the #GtkImage from the pixbuf. (Or for animations, use
+ * gdk_pixbuf_animation_new_from_file()).
+ *
+ * The storage type (gtk_image_get_storage_type()) of the returned
+ * image is not defined, it will be whatever is appropriate for
+ * displaying the file.
+ *
+ * Return value: a new #GtkImage
+ *
+ * Since: 3.4
+ **/
+GtkWidget*
+gtk_image_new_from_resource (const gchar *resource_path)
+{
+  GtkImage *image;
+
+  image = g_object_new (GTK_TYPE_IMAGE, NULL);
+
+  gtk_image_set_from_resource (image, resource_path);
+
+  return GTK_WIDGET (image);
+}
+
 /**
  * gtk_image_new_from_pixbuf:
  * @pixbuf: (allow-none): a #GdkPixbuf, or %NULL
@@ -737,6 +803,60 @@ gtk_image_set_from_file   (GtkImage    *image,
   g_object_thaw_notify (G_OBJECT (image));
 }
 
+/**
+ * gtk_image_set_from_resource:
+ * @image: a #GtkImage
+ * @resource_path: (allow-none): a resource path or %NULL
+ *
+ * See gtk_image_new_from_resource() for details.
+ **/
+void
+gtk_image_set_from_resource (GtkImage    *image,
+                            const gchar *resource_path)
+{
+  GtkImagePrivate *priv;
+  GdkPixbufAnimation *animation;
+
+  g_return_if_fail (GTK_IS_IMAGE (image));
+
+  priv = image->priv;
+
+  g_object_freeze_notify (G_OBJECT (image));
+
+  gtk_image_clear (image);
+
+  if (resource_path == NULL)
+    {
+      g_object_thaw_notify (G_OBJECT (image));
+      return;
+    }
+
+  animation = gdk_pixbuf_animation_new_from_resource (resource_path, NULL);
+
+  if (animation == NULL)
+    {
+      gtk_image_set_from_stock (image,
+                                GTK_STOCK_MISSING_IMAGE,
+                                DEFAULT_ICON_SIZE);
+      g_object_thaw_notify (G_OBJECT (image));
+      return;
+    }
+
+  priv->resource_path = g_strdup (resource_path);
+
+  if (gdk_pixbuf_animation_is_static_image (animation))
+    gtk_image_set_from_pixbuf (image, gdk_pixbuf_animation_get_static_image (animation));
+  else
+    gtk_image_set_from_animation (image, animation);
+
+  g_object_notify (G_OBJECT (image), "resource");
+
+  g_object_unref (animation);
+
+  g_object_thaw_notify (G_OBJECT (image));
+}
+
+
 /**
  * gtk_image_set_from_pixbuf:
  * @image: a #GtkImage
@@ -1221,9 +1341,6 @@ animation_timeout (gpointer data)
         gdk_threads_add_timeout (delay, animation_timeout, image);
 
       gtk_widget_queue_draw (widget);
-
-      if (gtk_widget_is_drawable (widget))
-        gdk_window_process_updates (gtk_widget_get_window (widget), TRUE);
     }
 
   return FALSE;
@@ -1253,6 +1370,29 @@ get_animation_frame (GtkImage *image)
   return g_object_ref (gdk_pixbuf_animation_iter_get_pixbuf (priv->animation_iter));
 }
 
+static void
+gtk_image_get_preferred_size (GtkImage *image,
+                              gint     *width_out,
+                              gint     *height_out)
+{
+  GtkImagePrivate *priv = image->priv;
+  gint width, height;
+  GtkBorder border;
+  GtkStyleContext *context;
+
+  context = gtk_widget_get_style_context (GTK_WIDGET (image));
+  _gtk_icon_helper_get_size (priv->icon_helper, context, &width, &height);
+  _gtk_misc_get_padding_and_border (GTK_MISC (image), &border);
+
+  width += border.left + border.right;
+  height += border.top + border.bottom;
+
+  if (width_out)
+    *width_out = width;
+  if (height_out)
+    *height_out = height;
+}
+
 static gint
 gtk_image_draw (GtkWidget *widget,
                 cairo_t   *cr)
@@ -1260,11 +1400,10 @@ gtk_image_draw (GtkWidget *widget,
   GtkImage *image;
   GtkImagePrivate *priv;
   GtkMisc *misc;
-  GtkStateFlags state;
   GtkStyleContext *context;
   gint x, y, width, height;
-  gint xpad, ypad;
   gfloat xalign, yalign;
+  GtkBorder border;
 
   g_return_val_if_fail (GTK_IS_IMAGE (widget), FALSE);
 
@@ -1273,20 +1412,23 @@ gtk_image_draw (GtkWidget *widget,
   priv = image->priv;
 
   context = gtk_widget_get_style_context (widget);
-  state = gtk_widget_get_state_flags (widget);
 
-  gtk_style_context_save (context);
-  gtk_style_context_set_state (context, state);
+  gtk_render_background (context, cr,
+                         0, 0,
+                         gtk_widget_get_allocated_width (widget), gtk_widget_get_allocated_height (widget));
+  gtk_render_frame (context, cr,
+                    0, 0,
+                    gtk_widget_get_allocated_width (widget), gtk_widget_get_allocated_height (widget));
 
   gtk_misc_get_alignment (misc, &xalign, &yalign);
-  gtk_misc_get_padding (misc, &xpad, &ypad);
-  _gtk_icon_helper_get_size (priv->icon_helper, context, &width, &height);
+  gtk_image_get_preferred_size (image, &width, &height);
+  _gtk_misc_get_padding_and_border (misc, &border);
 
   if (gtk_widget_get_direction (widget) != GTK_TEXT_DIR_LTR)
     xalign = 1.0 - xalign;
 
-  x = floor (xpad + ((gtk_widget_get_allocated_width (widget) - width) * xalign));
-  y = floor (ypad + ((gtk_widget_get_allocated_height (widget) - height) * yalign));
+  x = floor ((gtk_widget_get_allocated_width (widget) - width) * xalign) + border.left;
+  y = floor ((gtk_widget_get_allocated_height (widget) - height) * yalign) + border.top;
 
   if (gtk_image_get_storage_type (image) == GTK_IMAGE_ANIMATION)
     {
@@ -1303,8 +1445,6 @@ gtk_image_draw (GtkWidget *widget,
                              x, y);
     }
 
-  gtk_style_context_restore (context);
-
   return FALSE;
 }
 
@@ -1355,6 +1495,13 @@ gtk_image_reset (GtkImage *image)
       g_object_notify (G_OBJECT (image), "file");
     }
 
+  if (priv->resource_path)
+    {
+      g_free (priv->resource_path);
+      priv->resource_path = NULL;
+      g_object_notify (G_OBJECT (image), "resource");
+    }
+
   _gtk_icon_helper_clear (priv->icon_helper);
 
   g_object_thaw_notify (G_OBJECT (image));
@@ -1382,33 +1529,21 @@ gtk_image_get_preferred_width (GtkWidget *widget,
                                gint      *minimum,
                                gint      *natural)
 {
-  GtkImage *image = GTK_IMAGE (widget);
-  GtkImagePrivate *priv = image->priv;
-  gint xpad, width;
-  GtkStyleContext *context;
+  gint width;
 
-  context = gtk_widget_get_style_context (widget);
-  gtk_misc_get_padding (GTK_MISC (image), &xpad, NULL);
-  _gtk_icon_helper_get_size (priv->icon_helper, context, &width, NULL);
-
-  *minimum = *natural = width + 2 * xpad;
-}
+  gtk_image_get_preferred_size (GTK_IMAGE (widget), &width, NULL);
+  *minimum = *natural = width;
+} 
 
 static void
 gtk_image_get_preferred_height (GtkWidget *widget,
                                 gint      *minimum,
                                 gint      *natural)
 {
-  GtkImage *image = GTK_IMAGE (widget);
-  GtkImagePrivate *priv = image->priv;
-  gint ypad, height;
-  GtkStyleContext *context;
-
-  context = gtk_widget_get_style_context (widget);
-  gtk_misc_get_padding (GTK_MISC (image), NULL, &ypad);
-  _gtk_icon_helper_get_size (priv->icon_helper, context, NULL, &height);
+  gint height;
 
-  *minimum = *natural = height + 2 * ypad;
+  gtk_image_get_preferred_size (GTK_IMAGE (widget), NULL, &height);
+  *minimum = *natural = height;
 }
 
 static void