aboutsummaryrefslogtreecommitdiffstats
path: root/matchbox-panel/mb-panel-scaling-image2.c
blob: 258f1e41a74169c3637923f0116924aadaf96b2e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*- */
/*
 * (C) 2013 Intel Corp
 *
 * Author: Ross Burton <ross.burton@intel.com>
 *
 * Licensed under the GPL v2 or greater.
 */

#include <config.h>
#include <string.h>
#include <gtk/gtk.h>

#include "mb-panel-scaling-image2.h"

struct _MBPanelScalingImage2Private {
        GtkOrientation orientation;
        char *icon;
        int size;
        GtkIconTheme *icon_theme;
        guint icon_theme_changed_id;
        GdkPixbuf *pixbuf;
};

enum {
        PROP_0,
        PROP_ORIENTATION,
        PROP_ICON,
};

G_DEFINE_TYPE (MBPanelScalingImage2,
               mb_panel_scaling_image2,
               GTK_TYPE_DRAWING_AREA);

static void
mb_panel_scaling_image2_init (MBPanelScalingImage2 *image)
{
        image->priv = G_TYPE_INSTANCE_GET_PRIVATE (image,
                                                   MB_PANEL_TYPE_SCALING_IMAGE2,
                                                   MBPanelScalingImage2Private);

        image->priv->orientation = GTK_ORIENTATION_HORIZONTAL;

        image->priv->icon = NULL;

        image->priv->pixbuf = NULL;
}

static void
mb_panel_scaling_image2_set_property (GObject      *object,
                                     guint         property_id,
                                     const GValue *value,
                                     GParamSpec   *pspec)
{
        MBPanelScalingImage2 *image;

        image = MB_PANEL_SCALING_IMAGE2 (object);

        switch (property_id) {
        case PROP_ORIENTATION:
                image->priv->orientation = g_value_get_enum (value);
                break;
        case PROP_ICON:
                mb_panel_scaling_image2_set_icon (image,
                                                 g_value_get_string (value));
                break;
        default:
                G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
                break;
        }
}

static void
mb_panel_scaling_image2_get_property (GObject    *object,
                                     guint       property_id,
                                     GValue     *value,
                                     GParamSpec *pspec)
{
        MBPanelScalingImage2 *image;

        image = MB_PANEL_SCALING_IMAGE2 (object);

        switch (property_id) {
        case PROP_ORIENTATION:
                g_value_set_enum (value, image->priv->orientation);
                break;
        case PROP_ICON:
                g_value_set_string (value, image->priv->icon);
                break;
        default:
                G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
                break;
        }
}

static void
mb_panel_scaling_image2_dispose (GObject *object)
{
        MBPanelScalingImage2 *image;
        GObjectClass *object_class;

        object_class = G_OBJECT_CLASS (mb_panel_scaling_image2_parent_class);
        image = MB_PANEL_SCALING_IMAGE2 (object);

        if (image->priv->icon_theme_changed_id) {
                g_signal_handler_disconnect
                        (image->priv->icon_theme,
                         image->priv->icon_theme_changed_id);
                image->priv->icon_theme_changed_id = 0;
        }

        object_class->dispose (object);
}

static void
mb_panel_scaling_image2_finalize (GObject *object)
{
        MBPanelScalingImage2 *image;
        GObjectClass *object_class;

        object_class = G_OBJECT_CLASS (mb_panel_scaling_image2_parent_class);
        image = MB_PANEL_SCALING_IMAGE2 (object);

        g_free (image->priv->icon);

        object_class->finalize (object);
}

/* Strips extension off filename */
static char *
strip_extension (const char *file)
{
        char *stripped, *p;

        stripped = g_strdup (file);

        p = strrchr (stripped, '.');
        if (p &&
            (!strcmp (p, ".png") ||
             !strcmp (p, ".svg") ||
             !strcmp (p, ".xpm")))
	        *p = 0;

        return stripped;
}

/* Find icon filename */
/* This follows the same logic as gnome-panel. This should hopefully
 * ensure correct behaviour. */
static char *
find_icon (GtkIconTheme *icon_theme,
           const char   *icon,
           int           size)
{
        GtkIconInfo *info;
        char *new_icon, *stripped, *prefixed;

        if (g_path_is_absolute (icon)) {
                if (g_file_test (icon, G_FILE_TEST_EXISTS))
                        return g_strdup (icon);
                else
                        new_icon = g_path_get_basename (icon);
        } else
                new_icon = (char *) icon;

        stripped = strip_extension (new_icon);

        if (new_icon != icon)
                g_free (new_icon);

        prefixed = g_strconcat ("panel-", stripped, NULL);

        info = gtk_icon_theme_lookup_icon (icon_theme,
                                           prefixed,
                                           size,
                                           0);

        if (info == NULL) {
          info = gtk_icon_theme_lookup_icon (icon_theme,
                                             stripped,
                                             size,
                                             0);
        }

        g_free (stripped);
        g_free (prefixed);

        if (info) {
                char *file;

                file = g_strdup (gtk_icon_info_get_filename (info));

                gtk_icon_info_free (info);

                return file;
        } else
                return NULL;
}

/* Reload the specified icon */
static void
reload_icon (MBPanelScalingImage2 *image, gboolean force)
{
        GtkAllocation alloc;
        int size;
        char *file;
        GError *error;
        GdkPixbuf *pixbuf;

        /* Determine the required icon size */
        gtk_widget_get_allocation (GTK_WIDGET (image), &alloc);
        size = image->priv->orientation == GTK_ORIENTATION_HORIZONTAL
                ? alloc.height : alloc.width;

        if (!force && size == image->priv->size) {
                return;
        }

        image->priv->size = size;

        if (!image->priv->icon) {
                g_clear_object (&image->priv->pixbuf);
                gtk_widget_queue_resize (GTK_WIDGET (image));
                return;
        }

        file = find_icon (image->priv->icon_theme,
                          image->priv->icon,
                          size);
	if (!file) {
                g_warning ("Icon \"%s\" not found", image->priv->icon);

                return;
        }

        error = NULL;
        pixbuf = gdk_pixbuf_new_from_file_at_scale (file, size, size, TRUE, &error);
        g_free (file);

        g_clear_object (&image->priv->pixbuf);
        if (pixbuf) {
                image->priv->pixbuf = pixbuf;
        } else {
                g_warning ("No pixbuf found: %s", error->message);
                g_error_free (error);
        }

        gtk_widget_queue_resize (GTK_WIDGET (image));
}

/* Icon theme changed */
static void
icon_theme_changed_cb (GtkIconTheme   *icon_theme,
                       MBPanelScalingImage2 *image)
{
        GtkWidget *widget = GTK_WIDGET (image);

        if (gtk_widget_get_realized (widget)) {
                reload_icon (image, TRUE);
        }
}

static void
mb_panel_scaling_image2_size_allocate (GtkWidget *widget,
                                      GtkAllocation *allocation)
{
        MBPanelScalingImage2 *image = MB_PANEL_SCALING_IMAGE2 (widget);
        GtkWidgetClass *widget_class;

        widget_class = GTK_WIDGET_CLASS (mb_panel_scaling_image2_parent_class);
        widget_class->size_allocate (widget, allocation);

        if (gtk_widget_get_realized (widget)) {
                reload_icon (image, FALSE);
        }
}

static void
mb_panel_scaling_image2_realize (GtkWidget *widget)
{
        GtkWidgetClass *widget_class;

        reload_icon (MB_PANEL_SCALING_IMAGE2 (widget), TRUE);

        widget_class = GTK_WIDGET_CLASS (mb_panel_scaling_image2_parent_class);
        widget_class->realize (widget);
}

static void
mb_panel_scaling_image2_screen_changed (GtkWidget *widget,
                                        GdkScreen *old_screen)
{
        MBPanelScalingImage2 *image;
        GdkScreen *screen;
        GtkIconTheme *new_icon_theme;

        if (GTK_WIDGET_CLASS (mb_panel_scaling_image2_parent_class)->screen_changed)
                GTK_WIDGET_CLASS (mb_panel_scaling_image2_parent_class)->screen_changed (widget, old_screen);

        image = MB_PANEL_SCALING_IMAGE2 (widget);
        screen = gtk_widget_get_screen (widget);
        new_icon_theme = gtk_icon_theme_get_for_screen (screen);
        if (image->priv->icon_theme == new_icon_theme)
                return;

        if (image->priv->icon_theme_changed_id) {
                g_signal_handler_disconnect
                        (image->priv->icon_theme,
                         image->priv->icon_theme_changed_id);
        }

        image->priv->icon_theme = new_icon_theme;

        image->priv->icon_theme_changed_id =
                g_signal_connect (image->priv->icon_theme,
                                  "changed",
                                  G_CALLBACK (icon_theme_changed_cb),
                                  image);

        if (gtk_widget_get_realized (widget)) {
                reload_icon (MB_PANEL_SCALING_IMAGE2 (widget), TRUE);
        }
}
static gboolean
mb_panel_scaling_image2_draw (GtkWidget *widget, cairo_t *cr)
{
        MBPanelScalingImage2 *image = MB_PANEL_SCALING_IMAGE2 (widget);

        if (image->priv->pixbuf) {
                gdk_cairo_set_source_pixbuf (cr, image->priv->pixbuf, 0.0, 0.0);
                cairo_paint (cr);
        }
        return TRUE;
}

static void
mb_panel_scaling_image2_get_preferred_width (GtkWidget *widget, int *minimum_width, int *natural_width)
{
        MBPanelScalingImage2 *image = MB_PANEL_SCALING_IMAGE2 (widget);

        if (image->priv->pixbuf) {
                *minimum_width = *natural_width = gdk_pixbuf_get_width (image->priv->pixbuf);
        } else {
                *minimum_width = *natural_width = 1;
        }
}

static void
mb_panel_scaling_image2_class_init (MBPanelScalingImage2Class *klass)
{
        GObjectClass *object_class;
        GtkWidgetClass *widget_class;

        object_class = G_OBJECT_CLASS (klass);

        object_class->set_property = mb_panel_scaling_image2_set_property;
        object_class->get_property = mb_panel_scaling_image2_get_property;
        object_class->dispose      = mb_panel_scaling_image2_dispose;
        object_class->finalize     = mb_panel_scaling_image2_finalize;

        widget_class = GTK_WIDGET_CLASS (klass);
        widget_class->size_allocate  = mb_panel_scaling_image2_size_allocate;
        widget_class->realize        = mb_panel_scaling_image2_realize;
        widget_class->screen_changed = mb_panel_scaling_image2_screen_changed;
        /* TODO: respect orientation and switch to height-for-width in vertical mode */
        widget_class->get_preferred_width = mb_panel_scaling_image2_get_preferred_width;
        widget_class->draw = mb_panel_scaling_image2_draw;

        g_type_class_add_private (klass, sizeof (MBPanelScalingImage2Private));

        g_object_class_install_property
                (object_class,
                 PROP_ORIENTATION,
                 g_param_spec_enum
                         ("orientation",
                          "orientation",
                          "The containing panels orientation.",
                          GTK_TYPE_ORIENTATION,
                          GTK_ORIENTATION_HORIZONTAL,
                          G_PARAM_READWRITE |
                          G_PARAM_STATIC_NAME | G_PARAM_STATIC_NICK |
                          G_PARAM_STATIC_BLURB));

        g_object_class_install_property
                (object_class,
                 PROP_ICON,
                 g_param_spec_string
                         ("icon",
                          "icon",
                          "The loaded icon.",
                          NULL,
                          G_PARAM_READWRITE |
                          G_PARAM_STATIC_NAME | G_PARAM_STATIC_NICK |
                          G_PARAM_STATIC_BLURB));
}

/**
 * mb_panel_scaling_image2_new
 * @orientation: The orientation of the containing panel.
 * @icon: The icon to display. This can be an absolute path, or a name
 * of an icon theme icon.
 *
 * Return value: A new #MBPanelScalingImage2 object displaying @icon.
 **/
GtkWidget *
mb_panel_scaling_image2_new (GtkOrientation orientation,
                            const char    *icon)
{
        return g_object_new (MB_PANEL_TYPE_SCALING_IMAGE2,
                             "orientation", orientation,
                             "icon", icon,
                             NULL);
}

/**
 * mb_panel_scaling_image2_set_icon
 * @image: A #MBPanelScalingImage2
 * @icon: The icon to display. This can be an absolute path, or a name
 * of an icon theme icon.
 *
 * Displays @icon in @image.
 **/
void
mb_panel_scaling_image2_set_icon (MBPanelScalingImage2 *image,
                                 const char          *icon)
{
        g_return_if_fail (MB_PANEL_IS_SCALING_IMAGE2 (image));

        g_free (image->priv->icon);
        image->priv->icon = g_strdup (icon);

        if (!gtk_widget_get_realized (GTK_WIDGET (image)))
                return;

        reload_icon (image, TRUE);
}

/**
 * mb_panel_scaling_image2_get_icon
 * @image: A #MBPanelScalingImage2
 *
 * Return value: The displayed icon. This string should not be freed.
 **/
const char *
mb_panel_scaling_image2_get_icon (MBPanelScalingImage2 *image)
{
        g_return_val_if_fail (MB_PANEL_IS_SCALING_IMAGE2 (image), NULL);

        return image->priv->icon;
}