2
* Copyright (C) 2008-2010 Nick Schermer <nick@xfce.org>
4
* This library is free software; you can redistribute it and/or modify it
5
* under the terms of the GNU General Public License as published by the Free
6
* Software Foundation; either version 2 of the License, or (at your option)
9
* This library is distributed in the hope that it will be useful, but WITHOUT
10
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14
* You should have received a copy of the GNU Library General Public License
15
* along with this library; if not, write to the Free Software Foundation,
16
* Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
31
#include <libxfce4util/libxfce4util.h>
33
#include <common/panel-private.h>
34
#include <libxfce4panel/xfce-panel-macros.h>
35
#include <libxfce4panel/xfce-panel-image.h>
36
#include <libxfce4panel/xfce-panel-convenience.h>
37
#include <libxfce4panel/libxfce4panel-alias.h>
42
* SECTION: xfce-panel-image
43
* @title: XfcePanelImage
44
* @short_description: Scalable image suitable for panel plugins
45
* @include: libxfce4panel/libxfce4panel.h
47
* The #XfcePanelImage is a widgets suitable for for example panel
48
* buttons where the developer does not exacly know the size of the
49
* image (due to theming and user setting).
51
* The #XfcePanelImage widget automatically scales to the allocated
52
* size of the widget. Because of that nature it never requests a size,
53
* so this will only work if you pack the image in another widget
54
* that will expand it.
55
* If you want to force an image size you can use xfce_panel_image_set_size()
56
* to set a pixel size, in that case the widget will request an fixed size
57
* which makes it usefull for usage in dialogs.
62
/* design limit for the panel, to reduce the uncached pixbuf size */
63
#define MAX_PIXBUF_SIZE (128)
65
#define xfce_panel_image_unref_null(obj) G_STMT_START { if ((obj) != NULL) \
67
g_object_unref (G_OBJECT (obj)); \
73
struct _XfcePanelImagePrivate
75
/* pixbuf set by the user */
78
/* internal cached pixbuf (resized) */
87
/* whether we round to fixed icon sizes */
88
guint force_icon_sizes : 1;
90
/* cached width and height */
105
static void xfce_panel_image_get_property (GObject *object,
109
static void xfce_panel_image_set_property (GObject *object,
113
static void xfce_panel_image_finalize (GObject *object);
114
static void xfce_panel_image_size_request (GtkWidget *widget,
115
GtkRequisition *requisition);
116
static void xfce_panel_image_size_allocate (GtkWidget *widget,
117
GtkAllocation *allocation);
118
static gboolean xfce_panel_image_expose_event (GtkWidget *widget,
119
GdkEventExpose *event);
120
static void xfce_panel_image_style_set (GtkWidget *widget,
121
GtkStyle *previous_style);
122
static GdkPixbuf *xfce_panel_image_scale_pixbuf (GdkPixbuf *source,
128
G_DEFINE_TYPE (XfcePanelImage, xfce_panel_image, GTK_TYPE_WIDGET)
133
xfce_panel_image_class_init (XfcePanelImageClass *klass)
135
GObjectClass *gobject_class;
136
GtkWidgetClass *gtkwidget_class;
138
g_type_class_add_private (klass, sizeof (XfcePanelImagePrivate));
140
gobject_class = G_OBJECT_CLASS (klass);
141
gobject_class->get_property = xfce_panel_image_get_property;
142
gobject_class->set_property = xfce_panel_image_set_property;
143
gobject_class->finalize = xfce_panel_image_finalize;
145
gtkwidget_class = GTK_WIDGET_CLASS (klass);
146
gtkwidget_class->size_request = xfce_panel_image_size_request;
147
gtkwidget_class->size_allocate = xfce_panel_image_size_allocate;
148
gtkwidget_class->expose_event = xfce_panel_image_expose_event;
149
gtkwidget_class->style_set = xfce_panel_image_style_set;
151
g_object_class_install_property (gobject_class,
153
g_param_spec_string ("source",
158
| G_PARAM_STATIC_STRINGS));
160
g_object_class_install_property (gobject_class,
162
g_param_spec_object ("pixbuf",
167
| G_PARAM_STATIC_STRINGS));
169
g_object_class_install_property (gobject_class,
171
g_param_spec_int ("size",
173
"Pixel size of the image",
174
-1, MAX_PIXBUF_SIZE, -1,
176
| G_PARAM_STATIC_STRINGS));
178
gtk_widget_class_install_style_property (gtkwidget_class,
179
g_param_spec_boolean ("force-gtk-icon-sizes",
181
"Force the image to fix to GtkIconSizes",
184
| G_PARAM_STATIC_STRINGS));
190
xfce_panel_image_init (XfcePanelImage *image)
192
GTK_WIDGET_SET_FLAGS (image, GTK_NO_WINDOW);
194
image->priv = G_TYPE_INSTANCE_GET_PRIVATE (image, XFCE_TYPE_PANEL_IMAGE, XfcePanelImagePrivate);
196
image->priv->pixbuf = NULL;
197
image->priv->cache = NULL;
198
image->priv->source = NULL;
199
image->priv->size = -1;
200
image->priv->width = -1;
201
image->priv->height = -1;
202
image->priv->force_icon_sizes = FALSE;
208
xfce_panel_image_get_property (GObject *object,
213
XfcePanelImagePrivate *priv = XFCE_PANEL_IMAGE (object)->priv;
218
g_value_set_string (value, priv->source);
222
g_value_set_object (value, priv->pixbuf);
226
g_value_set_int (value, priv->size);
230
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
238
xfce_panel_image_set_property (GObject *object,
246
xfce_panel_image_set_from_source (XFCE_PANEL_IMAGE (object),
247
g_value_get_string (value));
251
xfce_panel_image_set_from_pixbuf (XFCE_PANEL_IMAGE (object),
252
g_value_get_object (value));
256
xfce_panel_image_set_size (XFCE_PANEL_IMAGE (object),
257
g_value_get_int (value));
261
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
269
xfce_panel_image_finalize (GObject *object)
271
xfce_panel_image_clear (XFCE_PANEL_IMAGE (object));
273
(*G_OBJECT_CLASS (xfce_panel_image_parent_class)->finalize) (object);
279
xfce_panel_image_size_request (GtkWidget *widget,
280
GtkRequisition *requisition)
282
XfcePanelImagePrivate *priv = XFCE_PANEL_IMAGE (widget)->priv;
286
requisition->width = priv->size;
287
requisition->height = priv->size;
289
else if (priv->pixbuf != NULL)
291
requisition->width = gdk_pixbuf_get_width (priv->pixbuf);
292
requisition->height = gdk_pixbuf_get_height (priv->pixbuf);
296
requisition->width = widget->allocation.width;
297
requisition->height = widget->allocation.height;
304
xfce_panel_image_size_allocate (GtkWidget *widget,
305
GtkAllocation *allocation)
307
XfcePanelImagePrivate *priv = XFCE_PANEL_IMAGE (widget)->priv;
310
GtkIconTheme *icon_theme = NULL;
313
widget->allocation = *allocation;
315
/* check if the available size changed */
316
if ((priv->pixbuf != NULL || priv->source != NULL)
317
&& allocation->width > 0
318
&& allocation->height > 0
319
&& (allocation->width != priv->width
320
|| allocation->height != priv->height))
322
/* store the new size */
323
priv->width = allocation->width;
324
priv->height = allocation->height;
327
xfce_panel_image_unref_null (priv->cache);
329
size = MIN (priv->width, priv->height);
330
if (G_UNLIKELY (priv->force_icon_sizes && size < 32))
332
/* we use some hardcoded values here for convienence,
333
* above 32 pixels svg icons will kick in */
334
if (size > 16 && size < 22)
336
else if (size > 22 && size < 24)
338
else if (size > 24 && size < 32)
342
if (priv->pixbuf != NULL)
344
/* use the pixbuf set by the user */
345
pixbuf = g_object_ref (G_OBJECT (priv->pixbuf));
347
if (G_LIKELY (pixbuf != NULL))
349
/* scale the icon to the correct size */
350
priv->cache = xfce_panel_image_scale_pixbuf (pixbuf, size, size);
351
g_object_unref (G_OBJECT (pixbuf));
356
screen = gtk_widget_get_screen (widget);
357
if (G_LIKELY (screen != NULL))
358
icon_theme = gtk_icon_theme_get_for_screen (screen);
360
priv->cache = xfce_panel_pixbuf_from_source (priv->source, icon_theme, size);
368
xfce_panel_image_expose_event (GtkWidget *widget,
369
GdkEventExpose *event)
371
XfcePanelImagePrivate *priv = XFCE_PANEL_IMAGE (widget)->priv;
372
gint source_width, source_height;
374
GtkIconSource *source;
375
GdkPixbuf *rendered = NULL;
376
GdkPixbuf *pixbuf = priv->cache;
378
if (G_LIKELY (pixbuf != NULL))
380
/* get the size of the cache pixbuf */
381
source_width = gdk_pixbuf_get_width (priv->cache);
382
source_height = gdk_pixbuf_get_height (priv->cache);
385
dest_x = widget->allocation.x + (priv->width - source_width) / 2;
386
dest_y = widget->allocation.y + (priv->height - source_height) / 2;
388
if (GTK_WIDGET_STATE (widget) == GTK_STATE_INSENSITIVE)
390
source = gtk_icon_source_new ();
391
gtk_icon_source_set_pixbuf (source, pixbuf);
393
rendered = gtk_style_render_icon (widget->style,
395
gtk_widget_get_direction (widget),
396
GTK_WIDGET_STATE (widget),
397
-1, widget, "xfce-panel-image");
398
gtk_icon_source_free (source);
400
if (G_LIKELY (rendered != NULL))
404
/* draw the pixbuf */
405
gdk_draw_pixbuf (widget->window,
406
widget->style->black_gc,
409
source_width, source_height,
410
GDK_RGB_DITHER_NORMAL, 0, 0);
412
if (rendered != NULL)
413
g_object_unref (G_OBJECT (rendered));
422
xfce_panel_image_style_set (GtkWidget *widget,
423
GtkStyle *previous_style)
425
XfcePanelImagePrivate *priv = XFCE_PANEL_IMAGE (widget)->priv;
428
/* let gtk update the widget style */
429
(*GTK_WIDGET_CLASS (xfce_panel_image_parent_class)->style_set) (widget, previous_style);
431
/* get style property */
432
gtk_widget_style_get (widget, "force-gtk-icon-sizes", &force, NULL);
434
/* update if needed */
435
if (priv->force_icon_sizes != force)
437
priv->force_icon_sizes = force;
439
gtk_widget_queue_resize (widget);
442
/* update the icon if we have an icon-name source */
443
if (previous_style != NULL && priv->source != NULL
444
&& !g_path_is_absolute (priv->source))
446
/* unset the size to force an update */
447
priv->width = priv->height = -1;
448
gtk_widget_queue_resize (widget);
455
xfce_panel_image_scale_pixbuf (GdkPixbuf *source,
464
panel_return_val_if_fail (GDK_IS_PIXBUF (source), NULL);
466
/* we fail on invalid sizes */
467
if (G_UNLIKELY (dest_width <= 0 || dest_height <= 0))
470
source_width = gdk_pixbuf_get_width (source);
471
source_height = gdk_pixbuf_get_height (source);
473
/* check if we need to scale */
474
if (source_width <= dest_width && source_height <= dest_height)
475
return g_object_ref (G_OBJECT (source));
477
/* calculate the new dimensions */
478
wratio = (gdouble) source_width / (gdouble) dest_width;
479
hratio = (gdouble) source_height / (gdouble) dest_height;
482
dest_width = rint (source_width / hratio);
484
dest_height = rint (source_height / wratio);
486
return gdk_pixbuf_scale_simple (source, MAX (dest_width, 1),
487
MAX (dest_height, 1),
488
GDK_INTERP_BILINEAR);
494
* xfce_panel_image_new:
496
* Creates a new empty #XfcePanelImage widget.
498
* returns: a newly created XfcePanelImage widget.
503
xfce_panel_image_new (void)
505
return g_object_new (XFCE_TYPE_PANEL_IMAGE, NULL);
511
* xfce_panel_image_new_from_pixbuf:
512
* @pixbuf : a #GdkPixbuf, or %NULL.
514
* Creates a new #XfcePanelImage displaying @pixbuf. #XfcePanelImage
515
* will add its own reference rather than adopting yours. You don't
516
* need to scale the pixbuf to the correct size, the #XfcePanelImage
517
* will take care of that based on the allocation of the widget or
518
* the size set with xfce_panel_image_set_size().
520
* returns: a newly created XfcePanelImage widget.
525
xfce_panel_image_new_from_pixbuf (GdkPixbuf *pixbuf)
527
g_return_val_if_fail (pixbuf == NULL || GDK_IS_PIXBUF (pixbuf), NULL);
529
return g_object_new (XFCE_TYPE_PANEL_IMAGE,
530
"pixbuf", pixbuf, NULL);
536
* xfce_panel_image_new_from_source:
537
* @source : source of the image. This can be an absolute path or
538
* an icon-name or %NULL.
540
* Creates a new #XfcePanelImage displaying @source. #XfcePanelImage
541
* will detect if @source points to an absolute file or it and icon-name.
542
* For icon-names it will also look for files in the pixbuf folder or
543
* strip the extensions, which makes it suitable for usage with icon
544
* keys in .desktop files.
546
* returns: a newly created XfcePanelImage widget.
551
xfce_panel_image_new_from_source (const gchar *source)
553
g_return_val_if_fail (source == NULL || *source != '\0', NULL);
555
return g_object_new (XFCE_TYPE_PANEL_IMAGE,
556
"source", source, NULL);
562
* xfce_panel_image_set_from_pixbuf:
563
* @image : an #XfcePanelImage.
564
* @pixbuf : a #GdkPixbuf, or %NULL.
566
* See xfce_panel_image_new_from_pixbuf() for details.
571
xfce_panel_image_set_from_pixbuf (XfcePanelImage *image,
574
g_return_if_fail (XFCE_IS_PANEL_IMAGE (image));
575
g_return_if_fail (pixbuf == NULL || GDK_IS_PIXBUF (pixbuf));
577
xfce_panel_image_clear (image);
579
/* set the new pixbuf, scale it to the maximum size if needed */
580
image->priv->pixbuf = xfce_panel_image_scale_pixbuf (pixbuf,
581
MAX_PIXBUF_SIZE, MAX_PIXBUF_SIZE);
583
gtk_widget_queue_resize (GTK_WIDGET (image));
589
* xfce_panel_image_set_from_source:
590
* @image : an #XfcePanelImage.
591
* @source : source of the image. This can be an absolute path or
592
* an icon-name or %NULL.
594
* See xfce_panel_image_new_from_source() for details.
599
xfce_panel_image_set_from_source (XfcePanelImage *image,
602
g_return_if_fail (XFCE_IS_PANEL_IMAGE (image));
603
g_return_if_fail (source == NULL || *source != '\0');
605
xfce_panel_image_clear (image);
607
image->priv->source = g_strdup (source);
609
gtk_widget_queue_resize (GTK_WIDGET (image));
615
* xfce_panel_image_set_size:
616
* @image : an #XfcePanelImage.
617
* @size : a new size in pixels.
619
* This will force an image size, instead of looking at the allocation
620
* size, see introduction for more details. You can set a @size of
621
* -1 to turn this off.
626
xfce_panel_image_set_size (XfcePanelImage *image,
630
g_return_if_fail (XFCE_IS_PANEL_IMAGE (image));
632
if (G_LIKELY (image->priv->size != size))
634
image->priv->size = size;
635
gtk_widget_queue_resize (GTK_WIDGET (image));
642
* xfce_panel_image_get_size:
643
* @image : an #XfcePanelImage.
645
* The size of the image, set by xfce_panel_image_set_size() or -1
646
* if no size is forced and the image is scaled to the allocation size.
648
* Returns: icon size in pixels of the image or -1.
653
xfce_panel_image_get_size (XfcePanelImage *image)
655
g_return_val_if_fail (XFCE_IS_PANEL_IMAGE (image), -1);
656
return image->priv->size;
662
* xfce_panel_image_clear:
663
* @image : an #XfcePanelImage.
665
* Resets the image to be empty.
670
xfce_panel_image_clear (XfcePanelImage *image)
672
XfcePanelImagePrivate *priv = XFCE_PANEL_IMAGE (image)->priv;
674
g_return_if_fail (XFCE_IS_PANEL_IMAGE (image));
676
if (priv->source != NULL)
678
g_free (priv->source);
682
xfce_panel_image_unref_null (priv->pixbuf);
683
xfce_panel_image_unref_null (priv->cache);
692
#define __XFCE_PANEL_IMAGE_C__
693
#include <libxfce4panel/libxfce4panel-aliasdef.c>