gtkstylecascade.c 12.5 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
/* GTK - The GIMP Toolkit
 * Copyright (C) 2012 Benjamin Otte <otte@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 "gtkstylecascadeprivate.h"

#include "gtkstyleprovider.h"
#include "gtkstyleproviderprivate.h"
24
#include "gtkprivate.h"
25 26 27 28 29

typedef struct _GtkStyleCascadeIter GtkStyleCascadeIter;
typedef struct _GtkStyleProviderData GtkStyleProviderData;

struct _GtkStyleCascadeIter {
30 31 32
  int  n_cascades;
  int *cascade_index;  /* each one points at last index that was returned, */
                       /* not next one that should be returned */
33 34 35 36 37 38
};

struct _GtkStyleProviderData
{
  GtkStyleProvider *provider;
  guint priority;
39
  guint changed_signal_id;
40 41 42 43 44 45
};

static GtkStyleProvider *
gtk_style_cascade_iter_next (GtkStyleCascade     *cascade,
                             GtkStyleCascadeIter *iter)
{
46 47 48
  GtkStyleCascade *cas;
  int ix, highest_priority_index = 0;
  GtkStyleProviderData *highest_priority_data = NULL;
49

50 51
  for (cas = cascade, ix = 0; ix < iter->n_cascades; cas = cas->parent, ix++)
    {
52
      GtkStyleProviderData *data;
53 54
      if (iter->cascade_index[ix] <= 0)
        continue;
55

56
      data = &g_array_index (cas->providers,
57 58 59
                                                   GtkStyleProviderData,
                                                   iter->cascade_index[ix] - 1);
      if (highest_priority_data == NULL || data->priority > highest_priority_data->priority)
60
        {
61 62
          highest_priority_index = ix;
          highest_priority_data = data;
63 64
        }
    }
65 66

  if (highest_priority_data != NULL)
67
    {
68 69
      iter->cascade_index[highest_priority_index]--;
      return highest_priority_data->provider;
70
    }
71
  return NULL;
72 73 74 75 76 77
}

static GtkStyleProvider *
gtk_style_cascade_iter_init (GtkStyleCascade     *cascade,
                             GtkStyleCascadeIter *iter)
{
78 79 80 81 82 83 84 85 86 87
  GtkStyleCascade *cas = cascade;
  int ix;

  iter->n_cascades = 1;
  while ((cas = cas->parent) != NULL)
    iter->n_cascades++;

  iter->cascade_index = g_new (int, iter->n_cascades);
  for (cas = cascade, ix = 0; ix < iter->n_cascades; cas = cas->parent, ix++)
    iter->cascade_index[ix] = cas->providers->len;
88 89 90 91

  return gtk_style_cascade_iter_next (cascade, iter);
}

92 93 94 95 96 97
static void
gtk_style_cascade_iter_clear (GtkStyleCascadeIter *iter)
{
  g_free (iter->cascade_index);
}

98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
static gboolean
gtk_style_cascade_get_style_property (GtkStyleProvider *provider,
                                      GtkWidgetPath    *path,
                                      GtkStateFlags     state,
                                      GParamSpec       *pspec,
                                      GValue           *value)
{
  GtkStyleCascade *cascade = GTK_STYLE_CASCADE (provider);
  GtkStyleCascadeIter iter;
  GtkStyleProvider *item;

  for (item = gtk_style_cascade_iter_init (cascade, &iter);
       item;
       item = gtk_style_cascade_iter_next (cascade, &iter))
    {
      if (gtk_style_provider_get_style_property (item,
                                                 path,
                                                 state,
                                                 pspec,
                                                 value))
118 119 120 121
        {
          gtk_style_cascade_iter_clear (&iter);
          return TRUE;
        }
122 123
    }

124
  gtk_style_cascade_iter_clear (&iter);
125 126 127 128 129 130 131 132 133
  return FALSE;
}

static void
gtk_style_cascade_provider_iface_init (GtkStyleProviderIface *iface)
{
  iface->get_style_property = gtk_style_cascade_get_style_property;
}

134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
static GtkSettings *
gtk_style_cascade_get_settings (GtkStyleProviderPrivate *provider)
{
  GtkStyleCascade *cascade = GTK_STYLE_CASCADE (provider);
  GtkStyleCascadeIter iter;
  GtkSettings *settings;
  GtkStyleProvider *item;

  for (item = gtk_style_cascade_iter_init (cascade, &iter);
       item;
       item = gtk_style_cascade_iter_next (cascade, &iter))
    {
      if (!GTK_IS_STYLE_PROVIDER_PRIVATE (item))
        continue;
          
      settings = _gtk_style_provider_private_get_settings (GTK_STYLE_PROVIDER_PRIVATE (item));
      if (settings)
151 152 153 154
        {
          gtk_style_cascade_iter_clear (&iter);
          return settings;
        }
155 156
    }

157
  gtk_style_cascade_iter_clear (&iter);
158 159 160
  return NULL;
}

161
static GtkCssValue *
162 163 164 165 166
gtk_style_cascade_get_color (GtkStyleProviderPrivate *provider,
                             const char              *name)
{
  GtkStyleCascade *cascade = GTK_STYLE_CASCADE (provider);
  GtkStyleCascadeIter iter;
167
  GtkCssValue *color;
168 169 170 171 172 173 174 175
  GtkStyleProvider *item;

  for (item = gtk_style_cascade_iter_init (cascade, &iter);
       item;
       item = gtk_style_cascade_iter_next (cascade, &iter))
    {
      if (GTK_IS_STYLE_PROVIDER_PRIVATE (item))
        {
176 177
          color = _gtk_style_provider_private_get_color (GTK_STYLE_PROVIDER_PRIVATE (item), name);
          if (color)
178 179 180 181
            {
              gtk_style_cascade_iter_clear (&iter);
              return color;
            }
182 183 184 185 186 187 188
        }
      else
        {
          /* If somebody hits this code path, shout at them */
        }
    }

189
  gtk_style_cascade_iter_clear (&iter);
190 191 192
  return NULL;
}

193 194 195 196 197 198 199 200
static int
gtk_style_cascade_get_scale (GtkStyleProviderPrivate *provider)
{
  GtkStyleCascade *cascade = GTK_STYLE_CASCADE (provider);

  return cascade->scale;
}

Matthias Clasen's avatar
Matthias Clasen committed
201
static GtkCssKeyframes *
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
gtk_style_cascade_get_keyframes (GtkStyleProviderPrivate *provider,
                                 const char              *name)
{
  GtkStyleCascade *cascade = GTK_STYLE_CASCADE (provider);
  GtkStyleCascadeIter iter;
  GtkCssKeyframes *keyframes;
  GtkStyleProvider *item;

  for (item = gtk_style_cascade_iter_init (cascade, &iter);
       item;
       item = gtk_style_cascade_iter_next (cascade, &iter))
    {
      if (!GTK_IS_STYLE_PROVIDER_PRIVATE (item))
        continue;
          
      keyframes = _gtk_style_provider_private_get_keyframes (GTK_STYLE_PROVIDER_PRIVATE (item), name);
      if (keyframes)
219 220 221 222
        {
          gtk_style_cascade_iter_clear (&iter);
          return keyframes;
        }
223 224
    }

225
  gtk_style_cascade_iter_clear (&iter);
226 227 228
  return NULL;
}

229 230
static void
gtk_style_cascade_lookup (GtkStyleProviderPrivate *provider,
231
                          const GtkCssMatcher     *matcher,
232 233
                          GtkCssLookup            *lookup,
                          GtkCssChange            *change)
234 235 236 237
{
  GtkStyleCascade *cascade = GTK_STYLE_CASCADE (provider);
  GtkStyleCascadeIter iter;
  GtkStyleProvider *item;
238
  GtkCssChange iter_change;
239 240 241 242 243

  for (item = gtk_style_cascade_iter_init (cascade, &iter);
       item;
       item = gtk_style_cascade_iter_next (cascade, &iter))
    {
244 245
      GtkStyleProviderPrivate *sp = (GtkStyleProviderPrivate*)item;
      if (GTK_IS_STYLE_PROVIDER_PRIVATE (sp))
246
        {
247
          _gtk_style_provider_private_lookup (sp, matcher, lookup,
248 249 250
                                              change ? &iter_change : NULL);
          if (change)
            *change |= iter_change;
251 252 253
        }
      else
        {
254 255
          /* you lose */
          g_warn_if_reached ();
256 257
        }
    }
258
  gtk_style_cascade_iter_clear (&iter);
259 260 261 262 263 264
}

static void
gtk_style_cascade_provider_private_iface_init (GtkStyleProviderPrivateInterface *iface)
{
  iface->get_color = gtk_style_cascade_get_color;
265
  iface->get_settings = gtk_style_cascade_get_settings;
266
  iface->get_scale = gtk_style_cascade_get_scale;
267
  iface->get_keyframes = gtk_style_cascade_get_keyframes;
268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300
  iface->lookup = gtk_style_cascade_lookup;
}

G_DEFINE_TYPE_EXTENDED (GtkStyleCascade, _gtk_style_cascade, G_TYPE_OBJECT, 0,
                        G_IMPLEMENT_INTERFACE (GTK_TYPE_STYLE_PROVIDER,
                                               gtk_style_cascade_provider_iface_init)
                        G_IMPLEMENT_INTERFACE (GTK_TYPE_STYLE_PROVIDER_PRIVATE,
                                               gtk_style_cascade_provider_private_iface_init));

static void
gtk_style_cascade_dispose (GObject *object)
{
  GtkStyleCascade *cascade = GTK_STYLE_CASCADE (object);

  _gtk_style_cascade_set_parent (cascade, NULL);
  g_array_unref (cascade->providers);

  G_OBJECT_CLASS (_gtk_style_cascade_parent_class)->dispose (object);
}

static void
_gtk_style_cascade_class_init (GtkStyleCascadeClass *klass)
{
  GObjectClass *object_class = G_OBJECT_CLASS (klass);

  object_class->dispose = gtk_style_cascade_dispose;
}

static void
style_provider_data_clear (gpointer data_)
{
  GtkStyleProviderData *data = data_;

301
  g_signal_handler_disconnect (data->provider, data->changed_signal_id);
302 303 304 305 306 307
  g_object_unref (data->provider);
}

static void
_gtk_style_cascade_init (GtkStyleCascade *cascade)
{
308 309
  cascade->scale = 1;

310 311 312 313 314 315 316 317 318 319 320 321 322 323
  cascade->providers = g_array_new (FALSE, FALSE, sizeof (GtkStyleProviderData));
  g_array_set_clear_func (cascade->providers, style_provider_data_clear);
}

GtkStyleCascade *
_gtk_style_cascade_new (void)
{
  return g_object_new (GTK_TYPE_STYLE_CASCADE, NULL);
}

void
_gtk_style_cascade_set_parent (GtkStyleCascade *cascade,
                               GtkStyleCascade *parent)
{
324 325
  gtk_internal_return_if_fail (GTK_IS_STYLE_CASCADE (cascade));
  gtk_internal_return_if_fail (parent == NULL || GTK_IS_STYLE_CASCADE (parent));
326 327 328 329 330

  if (cascade->parent == parent)
    return;

  if (parent)
331 332 333 334 335 336 337
    {
      g_object_ref (parent);
      g_signal_connect_swapped (parent,
                                "-gtk-private-changed",
                                G_CALLBACK (_gtk_style_provider_private_changed),
                                cascade);
    }
338 339

  if (cascade->parent)
340 341 342 343 344 345
    {
      g_signal_handlers_disconnect_by_func (cascade->parent, 
                                            _gtk_style_provider_private_changed,
                                            cascade);
      g_object_unref (cascade->parent);
    }
346 347 348 349 350 351 352 353 354 355 356 357

  cascade->parent = parent;
}

void
_gtk_style_cascade_add_provider (GtkStyleCascade  *cascade,
                                 GtkStyleProvider *provider,
                                 guint             priority)
{
  GtkStyleProviderData data;
  guint i;

358 359 360
  gtk_internal_return_if_fail (GTK_IS_STYLE_CASCADE (cascade));
  gtk_internal_return_if_fail (GTK_IS_STYLE_PROVIDER (provider));
  gtk_internal_return_if_fail (GTK_STYLE_PROVIDER (cascade) != provider);
361 362 363

  data.provider = g_object_ref (provider);
  data.priority = priority;
364 365 366 367
  data.changed_signal_id = g_signal_connect_swapped (provider,
                                                     "-gtk-private-changed",
                                                     G_CALLBACK (_gtk_style_provider_private_changed),
                                                     cascade);
368 369 370 371 372 373 374 375 376 377

  /* ensure it gets removed first */
  _gtk_style_cascade_remove_provider (cascade, provider);

  for (i = 0; i < cascade->providers->len; i++)
    {
      if (g_array_index (cascade->providers, GtkStyleProviderData, i).priority > priority)
        break;
    }
  g_array_insert_val (cascade->providers, i, data);
378 379

  _gtk_style_provider_private_changed (GTK_STYLE_PROVIDER_PRIVATE (cascade));
380 381 382 383 384 385 386 387
}

void
_gtk_style_cascade_remove_provider (GtkStyleCascade  *cascade,
                                    GtkStyleProvider *provider)
{
  guint i;

388 389
  gtk_internal_return_if_fail (GTK_IS_STYLE_CASCADE (cascade));
  gtk_internal_return_if_fail (GTK_IS_STYLE_PROVIDER (provider));
390 391 392 393 394 395 396 397

  for (i = 0; i < cascade->providers->len; i++)
    {
      GtkStyleProviderData *data = &g_array_index (cascade->providers, GtkStyleProviderData, i);

      if (data->provider == provider)
        {
          g_array_remove_index (cascade->providers, i);
398 399
  
          _gtk_style_provider_private_changed (GTK_STYLE_PROVIDER_PRIVATE (cascade));
400 401 402 403 404
          break;
        }
    }
}

405 406 407 408
void
_gtk_style_cascade_set_scale (GtkStyleCascade *cascade,
                              int              scale)
{
409
  gtk_internal_return_if_fail (GTK_IS_STYLE_CASCADE (cascade));
410 411 412 413 414 415 416 417 418 419 420 421

  if (cascade->scale == scale)
    return;

  cascade->scale = scale;

  _gtk_style_provider_private_changed (GTK_STYLE_PROVIDER_PRIVATE (cascade));
}

int
_gtk_style_cascade_get_scale (GtkStyleCascade *cascade)
{
422
  gtk_internal_return_val_if_fail (GTK_IS_STYLE_CASCADE (cascade), 1);
423 424 425

  return cascade->scale;
}