~rcryderman/awn/awn-cairo-effects

« back to all changes in this revision

Viewing changes to libawn/awn-icons.c

Merged trunk

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * This program is free software; you can redistribute it and/or modify
 
3
 * it under the terms of the GNU General Public License as published by
 
4
 * the Free Software Foundation; either version 2 of the License, or
 
5
 * (at your option) any later version.
 
6
 * 
 
7
 * This program is distributed in the hope that it will be useful,
 
8
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
9
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
10
 * GNU Library General Public License for more details.
 
11
 * 
 
12
 * You should have received a copy of the GNU General Public License
 
13
 * along with this program; if not, write to the Free Software
 
14
 * Foundation, Inc., 51 Franklin Street, Fifth Floor Boston, MA 02110-1301,  USA
 
15
 */
 
16
 
 
17
/* awn-icons.c */
 
18
 
 
19
#include "awn-icons.h"
 
20
 
 
21
#include <string.h>
 
22
 
 
23
#include <glib.h>
 
24
#include <glib/gstdio.h>
 
25
 
 
26
 
 
27
G_DEFINE_TYPE (AwnIcons, awn_icons, G_TYPE_OBJECT)
 
28
 
 
29
#define AWN_ICONS_THEME_NAME "awn-theme"
 
30
 
 
31
#define GET_PRIVATE(o) \
 
32
  (G_TYPE_INSTANCE_GET_PRIVATE ((o), LIBAWN_TYPE_AWN_ICONS, AwnIconsPrivate))
 
33
 
 
34
typedef struct _AwnIconsPrivate AwnIconsPrivate;
 
35
 
 
36
struct _AwnIconsPrivate {
 
37
  GtkWidget *   icon_widget;    //used if Non-NULL for drag and drop support
 
38
 
 
39
  GtkIconTheme *  awn_theme;
 
40
  
 
41
  gchar **  states;
 
42
  gchar **  icon_names;
 
43
  gchar *   applet_name;
 
44
  gchar *   uid;
 
45
  
 
46
  gint  height;
 
47
  gint  cur_icon;
 
48
  gint  count;
 
49
};
 
50
 
 
51
void awn_icons_set_icons_info(AwnIcons * icons,GtkWidget * applet,
 
52
                             gchar * applet_name,gchar * uid,gint height,
 
53
                             gchar **states,gchar **icon_names)
 
54
{
 
55
  g_return_if_fail(icons);
 
56
  g_return_if_fail(applet_name);
 
57
  g_return_if_fail(uid);
 
58
  g_return_if_fail(states);
 
59
  g_return_if_fail(icon_names);
 
60
 
 
61
  int count;
 
62
  AwnIconsPrivate *priv=GET_PRIVATE(icons); 
 
63
 
 
64
  if (applet)
 
65
  {
 
66
    priv->icon_widget = GTK_WIDGET(applet);
 
67
  }
 
68
  for (count=0;states[count];count++);
 
69
  priv->count = count;
 
70
  
 
71
  for (count=0;icon_names[count];count++);
 
72
  g_return_if_fail(count == priv->count);
 
73
 
 
74
  if (priv->states)
 
75
  {
 
76
    for(count=0;icon_names[count];count++)
 
77
    {
 
78
      g_free(icon_names[count]);
 
79
      g_free(states[count]);
 
80
    }
 
81
    g_free(priv->states);
 
82
    g_free(priv->icon_names);
 
83
  }
 
84
  
 
85
  priv->states = g_malloc( sizeof(gchar *) * count);
 
86
  priv->icon_names = g_malloc( sizeof(gchar *) * count);
 
87
  
 
88
  for (count=0; count < priv->count; count ++)
 
89
  {
 
90
    priv->states[count] = g_strdup(states[count]);
 
91
    priv->icon_names[count] = g_strdup(icon_names[count]);
 
92
  }
 
93
  priv->states[count] = NULL;
 
94
  priv->icon_names[count] = NULL;
 
95
 
 
96
  if (priv->applet_name)
 
97
  {
 
98
    g_free(priv->applet_name);
 
99
  }
 
100
  priv->applet_name = g_strdup(applet_name);
 
101
  
 
102
  if (priv->uid)
 
103
  {
 
104
    g_free(priv->uid);
 
105
  }
 
106
  priv->uid = g_strdup(uid);
 
107
  priv->height = height;
 
108
  
 
109
  gtk_icon_theme_rescan_if_needed(priv->awn_theme);
 
110
}
 
111
 
 
112
void awn_icons_set_icon_info(AwnIcons * icons,
 
113
                             GtkWidget * applet,
 
114
                             gchar * applet_name,
 
115
                             gchar * uid, 
 
116
                             gint height,
 
117
                             gchar *icon_name)
 
118
{
 
119
  g_return_if_fail(icons);  
 
120
  gchar *states[] = {"__SINGULAR__",NULL};
 
121
  gchar *icon_names[] = {NULL,NULL};
 
122
  icon_names[0] = icon_name;
 
123
  awn_icons_set_icons_info(icons,applet,applet_name,
 
124
                           uid,height,states,icon_names);
 
125
  
 
126
}
 
127
 
 
128
GdkPixbuf * awn_icons_get_icon(AwnIcons * icons, gchar * state)
 
129
{
 
130
  g_return_val_if_fail(icons,NULL);
 
131
  g_return_val_if_fail(state,NULL);
 
132
  
 
133
  gint count;
 
134
  GdkPixbuf * pixbuf = NULL;  
 
135
  AwnIconsPrivate *priv=GET_PRIVATE(icons);
 
136
  
 
137
  for (count = 0; priv->states[count]; count++)
 
138
  {
 
139
    if ( strcmp(state,priv->states[count]) == 0 )
 
140
    {
 
141
      int i;
 
142
      gchar * name=NULL;
 
143
      priv->count = count;
 
144
      
 
145
      for (i=0;i<6 ;i++)
 
146
      {
 
147
        switch (i)
 
148
        {
 
149
          case  0:                
 
150
            name = g_strdup_printf("%s-%s-%s",
 
151
                                  priv->icon_names[count],
 
152
                                  priv->applet_name,
 
153
                                  priv->uid);
 
154
            pixbuf = gtk_icon_theme_load_icon(priv->awn_theme, name , 
 
155
                                              priv->height, 0, NULL);
 
156
            break;
 
157
          case  1:
 
158
            name = g_strdup_printf("%s-%s",
 
159
                                   priv->icon_names[count],
 
160
                                   priv->applet_name);
 
161
            pixbuf = gtk_icon_theme_load_icon(priv->awn_theme, name , 
 
162
                                        priv->height, 0, NULL);
 
163
            break;
 
164
          case  2:
 
165
            pixbuf = gtk_icon_theme_load_icon(priv->awn_theme, 
 
166
                                        priv->icon_names[count],priv->height, 
 
167
                                        0, NULL);
 
168
            break;
 
169
          case  3:
 
170
            pixbuf = gtk_icon_theme_load_icon(gtk_icon_theme_get_default(), 
 
171
                                    priv->icon_names[count],priv->height, 
 
172
                                    0, NULL);
 
173
            break;
 
174
          case  4:
 
175
            pixbuf = gtk_icon_theme_load_icon(gtk_icon_theme_get_default(), 
 
176
                                  "stock_stop",priv->height, 0, NULL);
 
177
            break;
 
178
          case  5:
 
179
            pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB, TRUE, 8, 
 
180
                                    priv->height,priv->height);
 
181
            gdk_pixbuf_fill(pixbuf, 0xee221155);
 
182
            break;
 
183
        }            
 
184
        g_free(name);
 
185
        name = NULL;
 
186
        if (pixbuf)
 
187
        {
 
188
          if (gdk_pixbuf_get_height(pixbuf) != priv->height)
 
189
          {
 
190
            GdkPixbuf * new_pixbuf = gdk_pixbuf_scale_simple(pixbuf,                                                             
 
191
                                     gdk_pixbuf_get_width(pixbuf)*
 
192
                                     priv->height/gdk_pixbuf_get_height(pixbuf),
 
193
                                     priv->height,                                                             
 
194
                                     GDK_INTERP_HYPER
 
195
                                     );
 
196
            g_object_unref(pixbuf);
 
197
            pixbuf = new_pixbuf;
 
198
          }
 
199
          break;
 
200
        }
 
201
      }
 
202
    }
 
203
  }
 
204
  g_assert(pixbuf);  
 
205
  return pixbuf;
 
206
}
 
207
 
 
208
 
 
209
GdkPixbuf * awn_icons_get_icon_simple(AwnIcons * icons)
 
210
{
 
211
  g_return_val_if_fail(icons,NULL);
 
212
 
 
213
  return awn_icons_get_icon(icons,"__SINGULAR__");
 
214
}
 
215
 
 
216
static void
 
217
awn_icons_get_property (GObject *object, guint property_id,
 
218
                              GValue *value, GParamSpec *pspec)
 
219
{
 
220
  switch (property_id) {
 
221
  default:
 
222
    G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
 
223
  }
 
224
}
 
225
 
 
226
static void
 
227
awn_icons_set_property (GObject *object, guint property_id,
 
228
                              const GValue *value, GParamSpec *pspec)
 
229
{
 
230
  switch (property_id) {
 
231
  default:
 
232
    G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
 
233
  }
 
234
}
 
235
 
 
236
static void
 
237
awn_icons_dispose (GObject *object)
 
238
{
 
239
  if (G_OBJECT_CLASS (awn_icons_parent_class)->dispose)
 
240
    G_OBJECT_CLASS (awn_icons_parent_class)->dispose (object);
 
241
}
 
242
 
 
243
static void
 
244
awn_icons_finalize (GObject *object)
 
245
{
 
246
  if (G_OBJECT_CLASS (awn_icons_parent_class)->finalize)
 
247
    G_OBJECT_CLASS (awn_icons_parent_class)->finalize (object);
 
248
}
 
249
 
 
250
static void
 
251
awn_icons_class_init (AwnIconsClass *klass)
 
252
{
 
253
  GObjectClass *object_class = G_OBJECT_CLASS (klass);
 
254
 
 
255
  g_type_class_add_private (klass, sizeof (AwnIconsPrivate));
 
256
 
 
257
  object_class->get_property = awn_icons_get_property;
 
258
  object_class->set_property = awn_icons_set_property;
 
259
  object_class->dispose = awn_icons_dispose;
 
260
  object_class->finalize = awn_icons_finalize;
 
261
}
 
262
 
 
263
static void
 
264
awn_icons_init (AwnIcons *self)
 
265
{
 
266
  AwnIconsPrivate *priv=GET_PRIVATE(self);
 
267
  priv->icon_widget = NULL;
 
268
  priv->states = NULL;
 
269
  priv->icon_names = NULL;
 
270
  priv->uid = NULL;
 
271
  priv->cur_icon = 0;
 
272
  priv->count = 0;  
 
273
  priv->height = 0;
 
274
 
 
275
  //do we have our dirs....
 
276
  const gchar * home = g_getenv("HOME");
 
277
  
 
278
  gchar * icon_dir = g_strdup_printf("%s/.icons",home);
 
279
  if ( !g_file_test(icon_dir,G_FILE_TEST_IS_DIR) )
 
280
  {
 
281
    g_mkdir(icon_dir,0775);
 
282
  }
 
283
  
 
284
  gchar * awn_theme_dir = g_strdup_printf("%s/%s",icon_dir,AWN_ICONS_THEME_NAME);
 
285
  g_free(icon_dir);
 
286
  if ( !g_file_test(awn_theme_dir,G_FILE_TEST_IS_DIR) )
 
287
  {
 
288
    g_mkdir(awn_theme_dir,0775);
 
289
  }
 
290
  
 
291
  gchar * awn_scalable_dir = g_strdup_printf("%s/scalable",awn_theme_dir);  
 
292
  g_free(awn_theme_dir);
 
293
  if ( !g_file_test(awn_scalable_dir,G_FILE_TEST_IS_DIR) )
 
294
  {
 
295
    g_mkdir(awn_scalable_dir,0775);
 
296
  }
 
297
  
 
298
  g_free(awn_scalable_dir);
 
299
  
 
300
  
 
301
  priv->awn_theme = gtk_icon_theme_new();
 
302
  gtk_icon_theme_set_custom_theme(priv->awn_theme,AWN_ICONS_THEME_NAME);
 
303
}
 
304
 
 
305
AwnIcons*
 
306
awn_icons_new (void)
 
307
{
 
308
  return g_object_new (LIBAWN_TYPE_AWN_ICONS, NULL);
 
309
}
 
310
 
 
311