gthemedicon.c 4.52 KB
Newer Older
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
/* GIO - GLib Input, Output and Streaming Library
 * 
 * Copyright (C) 2006-2007 Red Hat, Inc.
 *
 * 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.
 *
 * Author: Alexander Larsson <alexl@redhat.com>
 */

#include <config.h>

#include "gthemedicon.h"

27 28
#include "gioalias.h"

29 30
/**
 * SECTION:gthemedicon
Matthias Clasen's avatar
Matthias Clasen committed
31
 * @short_description: Icon theming support
32
 * @see_also: #GIcon, #GLoadableIcon
33
 *
34 35 36 37
 * #GThemedIcon is an implementation of #GIcon that supports icon themes.
 * #GThemedIcon contains a list of all of the icons present in an icon
 * theme, so that icons can be looked up quickly. #GThemedIcon does
 * not provide actual pixmaps for icons, just the icon names.
38 39
 **/

40
static void g_themed_icon_icon_iface_init (GIconIface *iface);
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

struct _GThemedIcon
{
  GObject parent_instance;
  
  char **names;
};

struct _GThemedIconClass
{
  GObjectClass parent_class;
};

G_DEFINE_TYPE_WITH_CODE (GThemedIcon, g_themed_icon, G_TYPE_OBJECT,
			 G_IMPLEMENT_INTERFACE (G_TYPE_ICON,
						g_themed_icon_icon_iface_init))
  
static void
g_themed_icon_finalize (GObject *object)
{
  GThemedIcon *themed;

  themed = G_THEMED_ICON (object);

  g_strfreev (themed->names);
  
  if (G_OBJECT_CLASS (g_themed_icon_parent_class)->finalize)
    (*G_OBJECT_CLASS (g_themed_icon_parent_class)->finalize) (object);
}

static void
g_themed_icon_class_init (GThemedIconClass *klass)
{
  GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
  
  gobject_class->finalize = g_themed_icon_finalize;
}

static void
g_themed_icon_init (GThemedIcon *themed)
{
}

/**
 * g_themed_icon_new:
86
 * @iconname: a string containing an icon name.
87
 * 
88 89 90
 * Creates a new themed icon for @iconname.
 * 
 * Returns: a new #GThemedIcon.
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
 **/
GIcon *
g_themed_icon_new (const char *iconname)
{
  GThemedIcon *themed;

  g_return_val_if_fail (iconname != NULL, NULL);

  themed = g_object_new (G_TYPE_THEMED_ICON, NULL);
  themed->names = g_new (char *, 2);
  themed->names[0] = g_strdup (iconname);
  themed->names[1] = NULL;
  
  return G_ICON (themed);
}

/**
 * g_themed_icon_new_from_names:
109 110 111 112
 * @iconnames: an array of strings containing icon names.
 * @len: the number of elements in the @iconnames array.
 * 
 * Creates a new themed icon for @iconnames.
113
 * 
114
 * Returns: a new #GThemedIcon.
115 116
 **/
GIcon *
117 118
g_themed_icon_new_from_names (char **iconnames, 
                              int    len)
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
{
  GThemedIcon *themed;
  int i;

  g_return_val_if_fail (iconnames != NULL, NULL);
  
  themed = g_object_new (G_TYPE_THEMED_ICON, NULL);
  if (len == -1)
    themed->names = g_strdupv (iconnames);
  else
    {
      themed->names = g_new (char *, len + 1);
      for (i = 0; i < len; i++)
	themed->names[i] = g_strdup (iconnames[i]);
      themed->names[i] = NULL;
    }
  
  
  return G_ICON (themed);
}

/**
 * g_themed_icon_get_names:
142 143 144
 * @icon: a #GThemedIcon.
 * 
 * Gets the names of icons from within @icon.
145
 * 
146
 * Returns: a list of icon names.
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
 **/
const char * const *
g_themed_icon_get_names (GThemedIcon *icon)
{
  g_return_val_if_fail (G_IS_THEMED_ICON (icon), NULL);
  return (const char * const *)icon->names;
}

static guint
g_themed_icon_hash (GIcon *icon)
{
  GThemedIcon *themed = G_THEMED_ICON (icon);
  guint hash;
  int i;

  hash = 0;

  for (i = 0; themed->names[i] != NULL; i++)
    hash ^= g_str_hash (themed->names[i]);
  
  return hash;
}

static gboolean
g_themed_icon_equal (GIcon *icon1,
172
                     GIcon *icon2)
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
{
  GThemedIcon *themed1 = G_THEMED_ICON (icon1);
  GThemedIcon *themed2 = G_THEMED_ICON (icon2);
  int i;

  for (i = 0; themed1->names[i] != NULL && themed2->names[i] != NULL; i++)
    {
      if (!g_str_equal (themed1->names[i], themed2->names[i]))
	return FALSE;
    }

  return themed1->names[i] == NULL && themed2->names[i] == NULL;
}

static void
g_themed_icon_icon_iface_init (GIconIface *iface)
{
  iface->hash = g_themed_icon_hash;
  iface->equal = g_themed_icon_equal;
}
193 194 195

#define __G_THEMED_ICON_C__
#include "gioaliasdef.c"