summaryrefslogtreecommitdiff
path: root/gtk/gtkiconsetpicture.c
blob: 0193cfdca8b1c14d00a3049f72f72df11b85025b (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
/* GTK - The GIMP Drawing Kit
 * Copyright (C) 2010 Benjamin Otte <otte@gnome.org>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * 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.
 */

#include "config.h"

#include "gtkiconsetpicture.h"

#include <cairo-gobject.h>

#include "gtkintl.h"
#include "gtkprivate.h"
#include "gtkstyledpicture.h"

struct _GtkIconSetPicturePrivate {
  GdkPicture *picture;
  GtkIconSet *set;
  GtkIconSize size;
};

enum {
  PROP_0,
  PROP_ICON_SET,
  PROP_SIZE
};

static void
gtk_icon_set_picture_update_picture (GtkIconSetPicture *picture)
{
  GtkIconSetPicturePrivate *priv = picture->priv;
  GtkIconSet *icon_set;
  GtkStyleContext *style;
  GdkPixbuf *pixbuf;
  GtkWidgetPath *path;
  
  path = gtk_widget_path_new ();
  style = gtk_style_context_new ();
  gtk_style_context_set_path (style, path);
  gtk_widget_path_free (path);

  if (priv->set == NULL)
    {
      icon_set = gtk_style_context_lookup_icon_set (style, GTK_STOCK_MISSING_IMAGE);
      g_assert (icon_set);
    }
  else
    icon_set = priv->set;

  pixbuf = gtk_icon_set_render_icon_pixbuf (icon_set, style, priv->size);
  gdk_pixbuf_picture_set_pixbuf (GDK_PIXBUF_PICTURE (priv->picture), pixbuf);
  g_object_unref (pixbuf);

  g_object_unref (style);
}

static GdkPicture *
gtk_icon_set_picture_update_styled (GtkStyledPicture *styled,
                                    GdkPicture *pixbuf_picture)
{
  GdkPicture *icon = gtk_picture_get_unstyled (GDK_PICTURE (styled));
  GtkWidget *widget = gtk_styled_picture_get_widget (styled);
  GtkIconSetPicturePrivate *priv = GTK_ICON_SET_PICTURE (icon)->priv;
  GtkIconSet *icon_set;
  GtkStyleContext *style;
  GdkPixbuf *pixbuf;

  style = gtk_widget_get_style_context (widget);
  gtk_style_context_save (style);
  gtk_style_context_set_state (style, gtk_widget_get_state_flags (widget));

  if (priv->set == NULL)
    {
      icon_set = gtk_style_context_lookup_icon_set (style, GTK_STOCK_MISSING_IMAGE);
      g_assert (icon_set);
    }
  else
    icon_set = priv->set;

  pixbuf = gtk_icon_set_render_icon_pixbuf (icon_set, style, priv->size);
  gdk_pixbuf_picture_set_pixbuf (GDK_PIXBUF_PICTURE (pixbuf_picture), pixbuf);
  g_object_unref (pixbuf);

  gtk_style_context_restore (style);

  g_object_ref (pixbuf_picture);
  return pixbuf_picture;
}

static GdkPicture *
gtk_icon_set_picture_attach (GdkPicture *picture,
                             GtkWidget  *widget)
{
  GdkPicture *styled;
  GdkPicture *pixbuf_picture;

  styled = gtk_styled_picture_new (picture, widget);
  pixbuf_picture = gdk_pixbuf_picture_new (NULL);
  g_signal_connect (styled, "update", G_CALLBACK (gtk_icon_set_picture_update_styled), pixbuf_picture);
  gtk_styled_picture_update (GTK_STYLED_PICTURE (styled));
  /* We can get rid of it here. styled will have a reference to it
   * and keep that reference until it goes away. */
  g_object_unref (pixbuf_picture);

  return styled;
}

static void
gtk_icon_set_picture_stylable_picture_init (GtkStylablePictureInterface *iface)
{
  iface->attach = gtk_icon_set_picture_attach;
}

G_DEFINE_TYPE_WITH_CODE (GtkIconSetPicture, gtk_icon_set_picture, GDK_TYPE_PICTURE,
			 G_IMPLEMENT_INTERFACE (GTK_TYPE_STYLABLE_PICTURE,
						gtk_icon_set_picture_stylable_picture_init))

/**
 * SECTION:gtkiconsetpicture
 * @Short_description: Pictures for #GtkIconSets
 * @Title: GtkIconSetPicture
 * @See_also: #GtkIconSet
 *
 * A #GtkIconSetPicture is an implementation of #GdkPicture for icon sets.
 */

static void
gtk_icon_set_picture_get_property (GObject    *object,
                                   guint       prop_id,
                                   GValue     *value,
                                   GParamSpec *pspec)
{
  GtkIconSetPicture *picture = GTK_ICON_SET_PICTURE (object);
  GtkIconSetPicturePrivate *priv = picture->priv;

  switch (prop_id)
    {
    case PROP_ICON_SET:
      g_value_set_boxed (value, priv->set);
      break;
    case PROP_SIZE:
      g_value_set_int (value, priv->size);
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
    }
}

static void
gtk_icon_set_picture_set_property (GObject      *object,
                                   guint         prop_id,
                                   const GValue *value,
                                   GParamSpec   *pspec)
{
  GtkIconSetPicture *picture = GTK_ICON_SET_PICTURE (object);

  switch (prop_id)
    {
    case PROP_ICON_SET:
      gtk_icon_set_picture_set_icon_set (picture, g_value_get_boxed (value));
      break;
    case PROP_SIZE:
      gtk_icon_set_picture_set_size (picture, g_value_get_int (value));
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
    }
}

static void
gtk_icon_set_picture_dispose (GObject *object)
{
  GtkIconSetPicture *icon_set = GTK_ICON_SET_PICTURE (object);
  GtkIconSetPicturePrivate *priv = icon_set->priv;

  if (priv->set) {
    gtk_icon_set_unref (priv->set);
    priv->set = NULL;
  }

  G_OBJECT_CLASS (gtk_icon_set_picture_parent_class)->dispose (object);
}

static cairo_surface_t *
gtk_icon_set_picture_ref_surface (GdkPicture *picture)
{
  GtkIconSetPicture *icon_set = GTK_ICON_SET_PICTURE (picture);

  return gdk_picture_ref_surface (icon_set->priv->picture);
}

static void
gtk_icon_set_picture_draw (GdkPicture *picture,
                           cairo_t    *cr)
{
  GtkIconSetPicture *icon_set = GTK_ICON_SET_PICTURE (picture);

  gdk_picture_draw (icon_set->priv->picture, cr);
}

static void
gtk_icon_set_picture_class_init (GtkIconSetPictureClass *klass)
{
  GObjectClass *object_class = G_OBJECT_CLASS (klass);
  GdkPictureClass *picture_class = GDK_PICTURE_CLASS (klass);

  object_class->get_property = gtk_icon_set_picture_get_property;
  object_class->set_property = gtk_icon_set_picture_set_property;
  object_class->dispose = gtk_icon_set_picture_dispose;

  picture_class->ref_surface = gtk_icon_set_picture_ref_surface;
  picture_class->draw = gtk_icon_set_picture_draw;

  /**
   * GtkIconSetPicture:size:
   *
   * The #GtkIconSize to use to determine the actual image size.
   *
   * Since: 3.2
   */
  g_object_class_install_property (object_class,
                                   PROP_SIZE,
                                   g_param_spec_int ("size",
                                                     P_("Size"),
                                                     P_("Symbolic size to use"),
                                                     G_MININT, G_MAXINT,
                                                     GTK_ICON_SIZE_BUTTON,
                                                     GTK_PARAM_READWRITE));

  /**
   * GtkIconSetPicture:icon-set:
   *
   * The icon set to display.
   *
   * Since: 3.2
   */
  g_object_class_install_property (object_class,
                                   PROP_ICON_SET,
                                   g_param_spec_boxed ("icon-set",
                                                       P_("Icon set"),
                                                       P_("Icon set to display"),
                                                       GTK_TYPE_ICON_SET,
                                                       GTK_PARAM_READWRITE));

  g_type_class_add_private (klass, sizeof (GtkIconSetPicturePrivate));
}

static void
gtk_icon_set_picture_resized_callback (GdkPicture *    pixbuf,
                                   GtkIconSetPicture *icon_set)
{
  gdk_picture_resized (GDK_PICTURE (icon_set),
                       gdk_picture_get_width (pixbuf),
                       gdk_picture_get_height (pixbuf));
}

static void
gtk_icon_set_picture_init (GtkIconSetPicture *picture)
{
  GtkIconSetPicturePrivate *priv;

  picture->priv = G_TYPE_INSTANCE_GET_PRIVATE (picture,
                                               GTK_TYPE_ICON_SET_PICTURE,
                                               GtkIconSetPicturePrivate);

  priv = picture->priv;
  priv->size = GTK_ICON_SIZE_BUTTON;
  priv->picture = gdk_pixbuf_picture_new (NULL);
  g_signal_connect_swapped (priv->picture,
                            "changed",
                            G_CALLBACK (gdk_picture_changed_region),
                            picture);
  g_signal_connect (priv->picture,
                    "resized",
                    G_CALLBACK (gtk_icon_set_picture_resized_callback),
                    picture);

  gtk_icon_set_picture_update_picture (picture);
}

/**
 * gtk_icon_set_picture_new:
 * @icon_set_name: the name of the icon_set to display
 * @size: The icon_set size to use or -1 for default
 *
 * Creates a new #GtkIconSetPicture displaying the given @icon_set.
 *
 * Returns: a new picture
 **/
GdkPicture *
gtk_icon_set_picture_new (GtkIconSet *icon_set,
                          GtkIconSize size)
{
  return g_object_new (GTK_TYPE_ICON_SET_PICTURE,
                       "icon-set", icon_set,
                       "size", size,
                       NULL);
}

GtkIconSize
gtk_icon_set_picture_get_size (GtkIconSetPicture *picture)
{
  g_return_val_if_fail (GTK_IS_ICON_SET_PICTURE (picture), -1);

  return picture->priv->size;
}

void
gtk_icon_set_picture_set_size (GtkIconSetPicture *picture,
                           GtkIconSize     size)
{
  GtkIconSetPicturePrivate *priv;

  g_return_if_fail (GTK_IS_ICON_SET_PICTURE (picture));

  priv = picture->priv;

  if (priv->size == size)
    return;

  priv->size = size;
  
  gtk_icon_set_picture_update_picture (picture);
  g_object_notify (G_OBJECT (picture), "size");
}

GtkIconSet *
gtk_icon_set_picture_get_icon_set (GtkIconSetPicture * picture)
{
  g_return_val_if_fail (GTK_IS_ICON_SET_PICTURE (picture), NULL);

  return picture->priv->set;
}

void
gtk_icon_set_picture_set_icon_set (GtkIconSetPicture *picture,
                                   GtkIconSet *       icon_set)
{
  GtkIconSetPicturePrivate *priv;

  g_return_if_fail (GTK_IS_ICON_SET_PICTURE (picture));

  priv = picture->priv;

  if (icon_set)
    gtk_icon_set_ref (icon_set);

  if (priv->set)
    gtk_icon_set_ref (priv->set);
  
  priv->set = icon_set;

  gtk_icon_set_picture_update_picture (picture);
  g_object_notify (G_OBJECT (picture), "icon-set");
}