gtkmodifierstyle.c 8.71 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 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
/* GTK - The GIMP Toolkit
 * Copyright (C) 2010 Carlos Garnacho <carlosg@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 "gtkmodifierstyle.h"
#include "gtkintl.h"

typedef struct GtkModifierStylePrivate GtkModifierStylePrivate;
typedef struct StylePropertyValue StylePropertyValue;

struct GtkModifierStylePrivate
{
  GtkStyleProperties *style;
  GHashTable *color_properties;
};

enum {
  CHANGED,
  LAST_SIGNAL
};

static guint signals [LAST_SIGNAL] = { 0 };

static void gtk_modifier_style_provider_init (GtkStyleProviderIface *iface);
static void gtk_modifier_style_finalize      (GObject      *object);

43
G_DEFINE_TYPE_EXTENDED (GtkModifierStyle, _gtk_modifier_style, G_TYPE_OBJECT, 0,
44 45 46 47
                        G_IMPLEMENT_INTERFACE (GTK_TYPE_STYLE_PROVIDER,
                                               gtk_modifier_style_provider_init));

static void
48
_gtk_modifier_style_class_init (GtkModifierStyleClass *klass)
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
{
  GObjectClass *object_class;

  object_class = G_OBJECT_CLASS (klass);

  object_class->finalize = gtk_modifier_style_finalize;

  signals[CHANGED] =
    g_signal_new (I_("changed"),
                  G_TYPE_FROM_CLASS (object_class),
                  G_SIGNAL_RUN_LAST,
                  0, NULL, NULL,
                  g_cclosure_marshal_VOID__VOID,
                  G_TYPE_NONE, 0);

  g_type_class_add_private (object_class, sizeof (GtkModifierStylePrivate));
}

static void
68
_gtk_modifier_style_init (GtkModifierStyle *modifier_style)
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
{
  GtkModifierStylePrivate *priv;

  priv = modifier_style->priv = G_TYPE_INSTANCE_GET_PRIVATE (modifier_style,
                                                             GTK_TYPE_MODIFIER_STYLE,
                                                             GtkModifierStylePrivate);

  priv->color_properties = g_hash_table_new_full (g_str_hash,
                                                  g_str_equal,
                                                  (GDestroyNotify) g_free,
                                                  (GDestroyNotify) gdk_rgba_free);
  priv->style = gtk_style_properties_new ();
}

static GtkStyleProperties *
gtk_modifier_style_get_style (GtkStyleProvider *provider,
                              GtkWidgetPath    *path)
{
  GtkModifierStylePrivate *priv;

  priv = GTK_MODIFIER_STYLE (provider)->priv;
  return g_object_ref (priv->style);
}

static gboolean
gtk_modifier_style_get_style_property (GtkStyleProvider *provider,
                                       GtkWidgetPath    *path,
                                       GtkStateFlags     state,
                                       GParamSpec       *pspec,
                                       GValue           *value)
{
  GtkModifierStylePrivate *priv;
101 102
  GdkRGBA *rgba;
  GdkColor color;
103 104 105 106 107 108 109 110 111 112 113
  gchar *str;

  /* Reject non-color types for now */
  if (pspec->value_type != GDK_TYPE_COLOR)
    return FALSE;

  priv = GTK_MODIFIER_STYLE (provider)->priv;
  str = g_strdup_printf ("-%s-%s",
                         g_type_name (pspec->owner_type),
                         pspec->name);

114
  rgba = g_hash_table_lookup (priv->color_properties, str);
115 116
  g_free (str);

117
  if (!rgba)
118 119
    return FALSE;

120 121 122 123 124
  color.red = (guint) (rgba->red * 65535.) + 0.5;
  color.green = (guint) (rgba->green * 65535.) + 0.5;
  color.blue = (guint) (rgba->blue * 65535.) + 0.5;

  g_value_set_boxed (value, &color);
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
  return TRUE;
}

static void
gtk_modifier_style_provider_init (GtkStyleProviderIface *iface)
{
  iface->get_style = gtk_modifier_style_get_style;
  iface->get_style_property = gtk_modifier_style_get_style_property;
}

static void
gtk_modifier_style_finalize (GObject *object)
{
  GtkModifierStylePrivate *priv;

  priv = GTK_MODIFIER_STYLE (object)->priv;
  g_hash_table_destroy (priv->color_properties);
  g_object_unref (priv->style);

144
  G_OBJECT_CLASS (_gtk_modifier_style_parent_class)->finalize (object);
145 146 147
}

GtkModifierStyle *
148
_gtk_modifier_style_new (void)
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
{
  return g_object_new (GTK_TYPE_MODIFIER_STYLE, NULL);
}

static void
modifier_style_set_color (GtkModifierStyle *style,
                          const gchar      *prop,
                          GtkStateFlags     state,
                          const GdkRGBA    *color)
{
  GtkModifierStylePrivate *priv;
  GdkRGBA *old_color;

  g_return_if_fail (GTK_IS_MODIFIER_STYLE (style));

  priv = style->priv;
  gtk_style_properties_get (priv->style, state,
                            prop, &old_color,
                            NULL);

  if ((!color && !old_color) ||
      (color && old_color && gdk_rgba_equal (color, old_color)))
    {
      gdk_rgba_free (old_color);
      return;
    }

  if (color)
    gtk_style_properties_set (priv->style, state,
                              prop, color,
                              NULL);
  else
    gtk_style_properties_unset_property (priv->style, prop, state);

  g_signal_emit (style, signals[CHANGED], 0);
  gdk_rgba_free (old_color);
}

void
188 189 190
_gtk_modifier_style_set_background_color (GtkModifierStyle *style,
                                          GtkStateFlags     state,
                                          const GdkRGBA    *color)
191 192 193 194 195 196 197
{
  g_return_if_fail (GTK_IS_MODIFIER_STYLE (style));

  modifier_style_set_color (style, "background-color", state, color);
}

void
198 199 200
_gtk_modifier_style_set_color (GtkModifierStyle *style,
                               GtkStateFlags     state,
                               const GdkRGBA    *color)
201 202 203 204 205 206 207
{
  g_return_if_fail (GTK_IS_MODIFIER_STYLE (style));

  modifier_style_set_color (style, "color", state, color);
}

void
208 209
_gtk_modifier_style_set_font (GtkModifierStyle           *style,
                              const PangoFontDescription *font_desc)
210 211 212 213 214 215 216 217 218 219 220 221 222 223
{
  GtkModifierStylePrivate *priv;
  PangoFontDescription *old_font;

  g_return_if_fail (GTK_IS_MODIFIER_STYLE (style));

  priv = style->priv;
  gtk_style_properties_get (priv->style, 0,
                            "font", &old_font,
                            NULL);

  if ((!old_font && !font_desc) ||
      (old_font && font_desc &&
       pango_font_description_equal (old_font, font_desc)))
224 225
    {
      if (old_font)
226
        pango_font_description_free (old_font);
227 228 229

      return;
    }
230 231 232 233 234 235 236 237

  if (font_desc)
    gtk_style_properties_set (priv->style, 0,
                              "font", font_desc,
                              NULL);
  else
    gtk_style_properties_unset_property (priv->style, "font", 0);

238 239 240
  if (old_font)
    pango_font_description_free (old_font);

241 242 243 244
  g_signal_emit (style, signals[CHANGED], 0);
}

void
245 246 247
_gtk_modifier_style_map_color (GtkModifierStyle *style,
                               const gchar      *name,
                               const GdkRGBA    *color)
248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266
{
  GtkModifierStylePrivate *priv;
  GtkSymbolicColor *symbolic_color = NULL;

  g_return_if_fail (GTK_IS_MODIFIER_STYLE (style));
  g_return_if_fail (name != NULL);

  priv = style->priv;

  if (color)
    symbolic_color = gtk_symbolic_color_new_literal (color);

  gtk_style_properties_map_color (priv->style,
                                  name, symbolic_color);

  g_signal_emit (style, signals[CHANGED], 0);
}

void
267 268 269 270
_gtk_modifier_style_set_color_property (GtkModifierStyle *style,
                                        GType             widget_type,
                                        const gchar      *prop_name,
                                        const GdkRGBA    *color)
271 272
{
  GtkModifierStylePrivate *priv;
273
  const GdkRGBA *old_color;
274 275 276 277 278 279 280 281 282 283 284 285
  gchar *str;

  g_return_if_fail (GTK_IS_MODIFIER_STYLE (style));
  g_return_if_fail (g_type_is_a (widget_type, GTK_TYPE_WIDGET));
  g_return_if_fail (prop_name != NULL);

  priv = style->priv;
  str = g_strdup_printf ("-%s-%s", g_type_name (widget_type), prop_name);

  old_color = g_hash_table_lookup (priv->color_properties, str);

  if ((!color && !old_color) ||
286
      (color && old_color && gdk_rgba_equal (color, old_color)))
287 288 289 290 291 292
    {
      g_free (str);
      return;
    }

  if (color)
293 294 295 296
    {
      g_hash_table_insert (priv->color_properties, str,
                           gdk_rgba_copy (color));
    }
297
  else
298 299 300 301
    {
      g_hash_table_remove (priv->color_properties, str);
      g_free (str);
    }
302 303 304

  g_signal_emit (style, signals[CHANGED], 0);
}