[473884]: src / icon_source_emblem.c Maximize Restore History

Download this file

icon_source_emblem.c    196 lines (168 with data), 5.6 kB

  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
/*
rox-media (Media) - DeviceKit-disks utility for ROX
Copyright (C) 2009 Tony Houghton <h@realh.co.uk>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "i18n.h"
#include "icon_source.h"
#include "icon_source_emblem.h"
#include "icon_source_emblem_priv.h"
#define H_ICON_SOURCE_EMBLEM_GET_PRIVATE(obj) \
(G_TYPE_INSTANCE_GET_PRIVATE((obj), \
H_TYPE_ICON_SOURCE_EMBLEM, HIconSourceEmblemPrivate))
static void
base_changed(HIconSourceEmblem *self, HIconSource *base)
{
g_signal_emit(self, h_icon_source_signals[H_ICON_SOURCE_SIGNAL_CHANGED], 0);
}
static GdkPixbuf *
get_pixbuf(HIconSource *iself, int width, int height)
{
HIconSourceEmblem *self = H_ICON_SOURCE_EMBLEM(iself);
HIconSourceEmblemPrivate *priv = self->priv;
GdkPixbuf *base = h_icon_source_get_pixbuf(self->base, width, height);
double scale;
if (!base || !self->emblem || !self->show_emblem)
return base;
if (priv->width <= 0 || priv->height <= 0)
{
priv->width = gdk_pixbuf_get_width(self->emblem);
priv->height = gdk_pixbuf_get_height(self->emblem);
}
if (height == -1)
height = width;
height = (int) ((double) height / 2.5);
if (MAX(priv->width, priv->height) > height)
{
if (priv->width > priv->height)
scale = (double) height / priv->width;
else
scale = (double) height / priv->height;
}
else
{
scale = 1;
}
gdk_pixbuf_composite(self->emblem, base, 0, 0,
(int) ((double) priv->width * scale),
(int) ((double) priv->height * scale),
0, 0, scale, scale, GDK_INTERP_HYPER, 255);
/* FIXME: interpolation method should be configurable */
return base;
}
static void
h_icon_source_emblem_init_h_icon_source(HIconSourceInterface *iface)
{
iface->get_pixbuf = get_pixbuf;
}
G_DEFINE_TYPE_WITH_CODE(HIconSourceEmblem, h_icon_source_emblem, G_TYPE_OBJECT,
G_IMPLEMENT_INTERFACE(H_TYPE_ICON_SOURCE,
h_icon_source_emblem_init_h_icon_source));
static void
h_icon_source_emblem_init(HIconSourceEmblem *self)
{
HIconSourceEmblemPrivate *priv = self->priv =
H_ICON_SOURCE_EMBLEM_GET_PRIVATE(self);
self->base = NULL;
self->emblem = NULL;
self->show_emblem = FALSE;
priv->width = priv->height = 0;
}
static void
h_icon_source_emblem_dispose(GObject *gobj)
{
HIconSourceEmblem *self = H_ICON_SOURCE_EMBLEM(gobj);
HIconSourceEmblemPrivate *priv = self->priv;
if (self->base)
{
g_object_unref(self->base);
self->base = NULL;
}
if (self->emblem)
{
g_object_unref(self->emblem);
self->emblem = NULL;
}
priv->width = priv->height = 0;
self->show_emblem = FALSE;
G_OBJECT_CLASS(h_icon_source_emblem_parent_class)->dispose(gobj);
}
static void
h_icon_source_emblem_finalize(GObject *gobj)
{
G_OBJECT_CLASS(h_icon_source_emblem_parent_class)->finalize(gobj);
}
static void
h_icon_source_emblem_class_init(HIconSourceEmblemClass *klass)
{
GObjectClass *gclass;
g_type_class_add_private(klass, sizeof(HIconSourceEmblemPrivate));
gclass = G_OBJECT_CLASS(klass);
gclass->dispose = h_icon_source_emblem_dispose;
gclass->finalize = h_icon_source_emblem_finalize;
}
HIconSourceEmblem *
h_icon_source_emblem_new(HIconSource *base, GdkPixbuf *emblem)
{
HIconSourceEmblem * self =
H_ICON_SOURCE_EMBLEM(g_object_new(H_TYPE_ICON_SOURCE_EMBLEM, NULL));
if (base)
{
self->base = base;
g_object_ref(base);
}
if (emblem)
{
self->emblem = emblem;
g_object_ref(emblem);
}
return self;
}
void
h_icon_source_emblem_set_base(HIconSourceEmblem *self, HIconSource *base)
{
g_return_if_fail(H_IS_ICON_SOURCE_EMBLEM(self));
if (self->base)
g_object_unref(self->base);
self->base = base;
if (base)
{
g_object_ref(base);
g_signal_connect_swapped(base, "changed",
G_CALLBACK(base_changed), self);
}
g_signal_emit(self, h_icon_source_signals[H_ICON_SOURCE_SIGNAL_CHANGED], 0);
}
void
h_icon_source_emblem_set_emblem(HIconSourceEmblem *self, GdkPixbuf *emblem)
{
HIconSourceEmblemPrivate *priv = self->priv;
g_return_if_fail(H_IS_ICON_SOURCE_EMBLEM(self));
if (self->emblem)
g_object_unref(self->emblem);
self->emblem = emblem;
if (emblem)
{
g_object_ref(emblem);
priv->width = gdk_pixbuf_get_width(emblem);
priv->height = gdk_pixbuf_get_height(emblem);
}
g_signal_emit(self, h_icon_source_signals[H_ICON_SOURCE_SIGNAL_CHANGED], 0);
}
void
h_icon_source_emblem_set_show_emblem(HIconSourceEmblem *self, gboolean show)
{
g_return_if_fail(H_IS_ICON_SOURCE_EMBLEM(self));
self->show_emblem = show;
g_signal_emit(self, h_icon_source_signals[H_ICON_SOURCE_SIGNAL_CHANGED], 0);
}