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
/* CTK - 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, see <http://www.gnu.org/licenses/>.
 */

#include "config.h"
#include "ctkmodifierstyle.h"
#include "ctkstyleproviderprivate.h"
#include "ctkintl.h"

typedef struct StylePropertyValue StylePropertyValue;

struct _CtkModifierStylePrivate
{
  CtkStyleProperties *style;
  GHashTable *color_properties;
};

static void ctk_modifier_style_provider_init         (CtkStyleProviderIface            *iface);
static void ctk_modifier_style_provider_private_init (CtkStyleProviderPrivateInterface *iface);
static void ctk_modifier_style_finalize              (GObject                          *object);

G_DEFINE_TYPE_EXTENDED (CtkModifierStyle, _ctk_modifier_style, G_TYPE_OBJECT, 0,
                        G_ADD_PRIVATE (CtkModifierStyle)<--- There is an unknown macro here somewhere. Configuration is required. If G_ADD_PRIVATE is a macro then please configure it.
                        G_IMPLEMENT_INTERFACE (CTK_TYPE_STYLE_PROVIDER,
                                               ctk_modifier_style_provider_init)
                        G_IMPLEMENT_INTERFACE (CTK_TYPE_STYLE_PROVIDER_PRIVATE,
                                               ctk_modifier_style_provider_private_init));

static void
_ctk_modifier_style_class_init (CtkModifierStyleClass *klass)
{
  GObjectClass *object_class;

  object_class = G_OBJECT_CLASS (klass);

  object_class->finalize = ctk_modifier_style_finalize;
}

static void
_ctk_modifier_style_init (CtkModifierStyle *modifier_style)
{
  CtkModifierStylePrivate *priv;

  priv = modifier_style->priv = _ctk_modifier_style_get_instance_private (modifier_style);

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

static gboolean
ctk_modifier_style_get_style_property (CtkStyleProvider *provider,
                                       CtkWidgetPath    *path G_GNUC_UNUSED,
                                       CtkStateFlags     state G_GNUC_UNUSED,
                                       GParamSpec       *pspec,
                                       GValue           *value)
{
  CtkModifierStylePrivate *priv;
  CdkRGBA *rgba;
  CdkColor color;
  gchar *str;

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

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

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

  if (!rgba)
    return FALSE;

  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);
  return TRUE;
}

static void
ctk_modifier_style_provider_init (CtkStyleProviderIface *iface)
{
  iface->get_style_property = ctk_modifier_style_get_style_property;
}

static CtkCssValue *
ctk_modifier_style_provider_get_color (CtkStyleProviderPrivate *provider,
                                       const char              *name)
{
  CtkModifierStyle *style = CTK_MODIFIER_STYLE (provider);

  return _ctk_style_provider_private_get_color (CTK_STYLE_PROVIDER_PRIVATE (style->priv->style), name);
}

static void
ctk_modifier_style_provider_lookup (CtkStyleProviderPrivate *provider,
                                    const CtkCssMatcher     *matcher,
                                    CtkCssLookup            *lookup,
                                    CtkCssChange            *change)
{
  CtkModifierStyle *style = CTK_MODIFIER_STYLE (provider);

  _ctk_style_provider_private_lookup (CTK_STYLE_PROVIDER_PRIVATE (style->priv->style),
                                      matcher,
                                      lookup,
                                      change);
}

static void
ctk_modifier_style_provider_private_init (CtkStyleProviderPrivateInterface *iface)
{
  iface->get_color = ctk_modifier_style_provider_get_color;
  iface->lookup = ctk_modifier_style_provider_lookup;
}

static void
ctk_modifier_style_finalize (GObject *object)
{
  CtkModifierStylePrivate *priv;

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

  G_OBJECT_CLASS (_ctk_modifier_style_parent_class)->finalize (object);
}

CtkModifierStyle *
_ctk_modifier_style_new (void)
{
  return g_object_new (CTK_TYPE_MODIFIER_STYLE, NULL);
}

static void
modifier_style_set_color (CtkModifierStyle *style,
                          const gchar      *prop,
                          CtkStateFlags     state,
                          const CdkRGBA    *color)
{
  CtkModifierStylePrivate *priv;

  g_return_if_fail (CTK_IS_MODIFIER_STYLE (style));

  priv = style->priv;

  if (color)
    ctk_style_properties_set (priv->style, state,
                              prop, color,
                              NULL);
  else
    ctk_style_properties_unset_property (priv->style, prop, state);

  _ctk_style_provider_private_changed (CTK_STYLE_PROVIDER_PRIVATE (style));
}

void
_ctk_modifier_style_set_background_color (CtkModifierStyle *style,
                                          CtkStateFlags     state,
                                          const CdkRGBA    *color)
{
  g_return_if_fail (CTK_IS_MODIFIER_STYLE (style));

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

void
_ctk_modifier_style_set_color (CtkModifierStyle *style,
                               CtkStateFlags     state,
                               const CdkRGBA    *color)
{
  g_return_if_fail (CTK_IS_MODIFIER_STYLE (style));

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

void
_ctk_modifier_style_set_font (CtkModifierStyle           *style,
                              const PangoFontDescription *font_desc)
{
  CtkModifierStylePrivate *priv;

  g_return_if_fail (CTK_IS_MODIFIER_STYLE (style));

  priv = style->priv;

  if (font_desc)
    ctk_style_properties_set (priv->style, 0,
                              "font", font_desc,
                              NULL);
  else
    ctk_style_properties_unset_property (priv->style, "font", 0);

  _ctk_style_provider_private_changed (CTK_STYLE_PROVIDER_PRIVATE (style));
}

void
_ctk_modifier_style_map_color (CtkModifierStyle *style,
                               const gchar      *name,
                               const CdkRGBA    *color)
{
  CtkModifierStylePrivate *priv;
  CtkSymbolicColor *symbolic_color = NULL;

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

  priv = style->priv;

  if (color)
    symbolic_color = ctk_symbolic_color_new_literal (color);

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

  _ctk_style_provider_private_changed (CTK_STYLE_PROVIDER_PRIVATE (style));
}

void
_ctk_modifier_style_set_color_property (CtkModifierStyle *style,
                                        GType             widget_type,
                                        const gchar      *prop_name,
                                        const CdkRGBA    *color)
{
  CtkModifierStylePrivate *priv;
  const CdkRGBA *old_color;
  gchar *str;

  g_return_if_fail (CTK_IS_MODIFIER_STYLE (style));
  g_return_if_fail (g_type_is_a (widget_type, CTK_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) ||
      (color && old_color && cdk_rgba_equal (color, old_color)))
    {
      g_free (str);
      return;
    }

  if (color)
    {
      g_hash_table_insert (priv->color_properties, str,
                           cdk_rgba_copy (color));
    }
  else
    {
      g_hash_table_remove (priv->color_properties, str);
      g_free (str);
    }

  _ctk_style_provider_private_changed (CTK_STYLE_PROVIDER_PRIVATE (style));
}