gtkcssanimatedstyle.c 17.2 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
/*
 * Copyright © 2012 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.1 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/>.
 *
 * Authors: Benjamin Otte <otte@gnome.org>
 */

#include "config.h"

#include "gtkcssanimatedstyleprivate.h"

#include "gtkcssanimationprivate.h"
#include "gtkcssarrayvalueprivate.h"
#include "gtkcssenumvalueprivate.h"
#include "gtkcssinheritvalueprivate.h"
#include "gtkcssinitialvalueprivate.h"
#include "gtkcssnumbervalueprivate.h"
#include "gtkcsssectionprivate.h"
#include "gtkcssshorthandpropertyprivate.h"
32
#include "gtkcssstaticstyleprivate.h"
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
#include "gtkcssstringvalueprivate.h"
#include "gtkcssstylepropertyprivate.h"
#include "gtkcsstransitionprivate.h"
#include "gtkprivate.h"
#include "gtkstyleanimationprivate.h"
#include "gtkstylepropertyprivate.h"
#include "gtkstyleproviderprivate.h"

G_DEFINE_TYPE (GtkCssAnimatedStyle, gtk_css_animated_style, GTK_TYPE_CSS_STYLE)

static GtkCssValue *
gtk_css_animated_style_get_value (GtkCssStyle *style,
                                  guint        id)
{
  GtkCssAnimatedStyle *animated = GTK_CSS_ANIMATED_STYLE (style);

  if (animated->animated_values &&
      id < animated->animated_values->len &&
      g_ptr_array_index (animated->animated_values, id))
    return g_ptr_array_index (animated->animated_values, id);

  return gtk_css_animated_style_get_intrinsic_value (animated, id);
}

static GtkCssSection *
gtk_css_animated_style_get_section (GtkCssStyle *style,
                                    guint        id)
{
  GtkCssAnimatedStyle *animated = GTK_CSS_ANIMATED_STYLE (style);

63
  return gtk_css_style_get_section (animated->style, id);
64 65
}

66 67 68 69 70 71 72 73
static gboolean
gtk_css_animated_style_is_static (GtkCssStyle *style)
{
  GtkCssAnimatedStyle *animated = GTK_CSS_ANIMATED_STYLE (style);
  GSList *list;

  for (list = animated->animations; list; list = list->next)
    {
74
      if (!_gtk_style_animation_is_static (list->data))
75 76 77 78 79 80
        return FALSE;
    }

  return TRUE;
}

81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
static void
gtk_css_animated_style_dispose (GObject *object)
{
  GtkCssAnimatedStyle *style = GTK_CSS_ANIMATED_STYLE (object);

  if (style->animated_values)
    {
      g_ptr_array_unref (style->animated_values);
      style->animated_values = NULL;
    }

  g_slist_free_full (style->animations, g_object_unref);
  style->animations = NULL;

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

static void
gtk_css_animated_style_finalize (GObject *object)
{
  GtkCssAnimatedStyle *style = GTK_CSS_ANIMATED_STYLE (object);

103
  g_object_unref (style->style);
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118

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

static void
gtk_css_animated_style_class_init (GtkCssAnimatedStyleClass *klass)
{
  GObjectClass *object_class = G_OBJECT_CLASS (klass);
  GtkCssStyleClass *style_class = GTK_CSS_STYLE_CLASS (klass);

  object_class->dispose = gtk_css_animated_style_dispose;
  object_class->finalize = gtk_css_animated_style_finalize;

  style_class->get_value = gtk_css_animated_style_get_value;
  style_class->get_section = gtk_css_animated_style_get_section;
119
  style_class->is_static = gtk_css_animated_style_is_static;
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
}

static void
gtk_css_animated_style_init (GtkCssAnimatedStyle *style)
{
}

void
gtk_css_animated_style_set_animated_value (GtkCssAnimatedStyle *style,
                                           guint                id,
                                           GtkCssValue         *value)
{
  gtk_internal_return_if_fail (GTK_IS_CSS_ANIMATED_STYLE (style));
  gtk_internal_return_if_fail (value != NULL);

  if (style->animated_values == NULL)
    style->animated_values = g_ptr_array_new_with_free_func ((GDestroyNotify)_gtk_css_value_unref);
  if (id >= style->animated_values->len)
   g_ptr_array_set_size (style->animated_values, id + 1);

  if (g_ptr_array_index (style->animated_values, id))
    _gtk_css_value_unref (g_ptr_array_index (style->animated_values, id));
  g_ptr_array_index (style->animated_values, id) = _gtk_css_value_ref (value);

}

GtkCssValue *
gtk_css_animated_style_get_intrinsic_value (GtkCssAnimatedStyle *style,
                                            guint                id)
{
  gtk_internal_return_val_if_fail (GTK_IS_CSS_ANIMATED_STYLE (style), NULL);

152
  return gtk_css_style_get_value (style->style, id);
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
}

/* TRANSITIONS */

typedef struct _TransitionInfo TransitionInfo;
struct _TransitionInfo {
  guint index;                  /* index into value arrays */
  gboolean pending;             /* TRUE if we still need to handle it */
};

static void
transition_info_add (TransitionInfo    infos[GTK_CSS_PROPERTY_N_PROPERTIES],
                     GtkStyleProperty *property,
                     guint             index)
{
  if (property == NULL)
    {
      guint i;

      for (i = 0; i < _gtk_css_style_property_get_n_properties (); i++)
        {
          GtkCssStyleProperty *prop = _gtk_css_style_property_lookup_by_id (i);

          transition_info_add (infos, GTK_STYLE_PROPERTY (prop), index);
        }
    }
  else if (GTK_IS_CSS_SHORTHAND_PROPERTY (property))
    {
      GtkCssShorthandProperty *shorthand = GTK_CSS_SHORTHAND_PROPERTY (property);
      guint i;

      for (i = 0; i < _gtk_css_shorthand_property_get_n_subproperties (shorthand); i++)
        {
          GtkCssStyleProperty *prop = _gtk_css_shorthand_property_get_subproperty (shorthand, i);

          transition_info_add (infos, GTK_STYLE_PROPERTY (prop), index);
        }
    }
  else if (GTK_IS_CSS_STYLE_PROPERTY (property))
    {
      guint id;
      
      if (!_gtk_css_style_property_is_animated (GTK_CSS_STYLE_PROPERTY (property)))
        return;

      id = _gtk_css_style_property_get_id (GTK_CSS_STYLE_PROPERTY (property));
      g_assert (id < GTK_CSS_PROPERTY_N_PROPERTIES);
      infos[id].index = index;
      infos[id].pending = TRUE;
    }
  else
    {
      g_assert_not_reached ();
    }
}

static void
transition_infos_set (TransitionInfo  infos[GTK_CSS_PROPERTY_N_PROPERTIES],
                      GtkCssValue    *transitions)
{
  guint i;

  for (i = 0; i < _gtk_css_array_value_get_n_values (transitions); i++)
    {
      GtkStyleProperty *property;
      GtkCssValue *prop_value;

      prop_value = _gtk_css_array_value_get_nth (transitions, i);
      if (g_ascii_strcasecmp (_gtk_css_ident_value_get (prop_value), "all") == 0)
        property = NULL;
      else
        {
          property = _gtk_style_property_lookup (_gtk_css_ident_value_get (prop_value));
          if (property == NULL)
            continue;
        }
      
      transition_info_add (infos, property, i);
    }
}

static GtkStyleAnimation *
gtk_css_animated_style_find_transition (GtkCssAnimatedStyle *style,
                                        guint                property_id)
{
  GSList *list;

  for (list = style->animations; list; list = list->next)
    {
      if (!GTK_IS_CSS_TRANSITION (list->data))
        continue;

      if (_gtk_css_transition_get_property (list->data) == property_id)
        return list->data;
    }

  return NULL;
}

252 253 254
static GSList *
gtk_css_animated_style_create_css_transitions (GSList              *animations,
                                               GtkCssStyle         *base_style,
255
                                               gint64               timestamp,
256
                                               GtkCssStyle         *source)
257 258 259 260 261
{
  TransitionInfo transitions[GTK_CSS_PROPERTY_N_PROPERTIES] = { { 0, } };
  GtkCssValue *durations, *delays, *timing_functions;
  guint i;

262
  transition_infos_set (transitions, gtk_css_style_get_value (base_style, GTK_CSS_PROPERTY_TRANSITION_PROPERTY));
263

264 265 266
  durations = gtk_css_style_get_value (base_style, GTK_CSS_PROPERTY_TRANSITION_DURATION);
  delays = gtk_css_style_get_value (base_style, GTK_CSS_PROPERTY_TRANSITION_DELAY);
  timing_functions = gtk_css_style_get_value (base_style, GTK_CSS_PROPERTY_TRANSITION_TIMING_FUNCTION);
267 268 269 270 271 272 273 274 275 276 277 278 279 280 281

  for (i = 0; i < GTK_CSS_PROPERTY_N_PROPERTIES; i++)
    {
      GtkStyleAnimation *animation;
      GtkCssValue *start, *end;
      double duration, delay;

      if (!transitions[i].pending)
        continue;

      duration = _gtk_css_number_value_get (_gtk_css_array_value_get_nth (durations, transitions[i].index), 100);
      delay = _gtk_css_number_value_get (_gtk_css_array_value_get_nth (delays, transitions[i].index), 100);
      if (duration + delay == 0.0)
        continue;

282
      if (GTK_IS_CSS_ANIMATED_STYLE (source))
283
        {
284
          start = gtk_css_animated_style_get_intrinsic_value (GTK_CSS_ANIMATED_STYLE (source), i);
285 286
          end = gtk_css_style_get_value (base_style, i);

287 288 289 290
          if (_gtk_css_value_equal (start, end))
            {
              animation = gtk_css_animated_style_find_transition (GTK_CSS_ANIMATED_STYLE (source), i);
              if (animation)
291 292 293 294
                {
                  animation = _gtk_style_animation_advance (animation, timestamp);
                  animations = g_slist_prepend (animations, animation);
                }
295 296 297

              continue;
            }
298
        }
299

300 301 302 303
      if (_gtk_css_value_equal (gtk_css_style_get_value (source, i),
                                gtk_css_style_get_value (base_style, i)))
        continue;

304 305 306
      animation = _gtk_css_transition_new (i,
                                           gtk_css_style_get_value (source, i),
                                           _gtk_css_array_value_get_nth (timing_functions, i),
307 308 309
                                           timestamp,
                                           duration * G_USEC_PER_SEC,
                                           delay * G_USEC_PER_SEC);
310
      animations = g_slist_prepend (animations, animation);
311
    }
312 313

  return animations;
314 315 316
}

static GtkStyleAnimation *
317 318
gtk_css_animated_style_find_animation (GSList     *animations,
                                       const char *name)
319 320 321
{
  GSList *list;

322
  for (list = animations; list; list = list->next)
323 324 325 326 327 328 329 330 331 332 333
    {
      if (!GTK_IS_CSS_ANIMATION (list->data))
        continue;

      if (g_str_equal (_gtk_css_animation_get_name (list->data), name))
        return list->data;
    }

  return NULL;
}

334 335 336
static GSList *
gtk_css_animated_style_create_css_animations (GSList                  *animations,
                                              GtkCssStyle             *base_style,
337
                                              GtkCssStyle             *parent_style,
338 339
                                              gint64                   timestamp,
                                              GtkStyleProviderPrivate *provider,
340
                                              GtkCssStyle             *source)
341
{
342
  GtkCssValue *durations, *delays, *timing_functions, *animation_names;
343 344 345
  GtkCssValue *iteration_counts, *directions, *play_states, *fill_modes;
  guint i;

346 347 348 349 350 351 352 353
  animation_names = gtk_css_style_get_value (base_style, GTK_CSS_PROPERTY_ANIMATION_NAME);
  durations = gtk_css_style_get_value (base_style, GTK_CSS_PROPERTY_ANIMATION_DURATION);
  delays = gtk_css_style_get_value (base_style, GTK_CSS_PROPERTY_ANIMATION_DELAY);
  timing_functions = gtk_css_style_get_value (base_style, GTK_CSS_PROPERTY_ANIMATION_TIMING_FUNCTION);
  iteration_counts = gtk_css_style_get_value (base_style, GTK_CSS_PROPERTY_ANIMATION_ITERATION_COUNT);
  directions = gtk_css_style_get_value (base_style, GTK_CSS_PROPERTY_ANIMATION_DIRECTION);
  play_states = gtk_css_style_get_value (base_style, GTK_CSS_PROPERTY_ANIMATION_PLAY_STATE);
  fill_modes = gtk_css_style_get_value (base_style, GTK_CSS_PROPERTY_ANIMATION_FILL_MODE);
354

355
  for (i = 0; i < _gtk_css_array_value_get_n_values (animation_names); i++)
356 357 358 359 360
    {
      GtkStyleAnimation *animation;
      GtkCssKeyframes *keyframes;
      const char *name;
      
361
      name = _gtk_css_ident_value_get (_gtk_css_array_value_get_nth (animation_names, i));
362 363 364
      if (g_ascii_strcasecmp (name, "none") == 0)
        continue;

365
      animation = gtk_css_animated_style_find_animation (animations, name);
366 367 368
      if (animation)
        continue;

369
      if (GTK_IS_CSS_ANIMATED_STYLE (source))
370
        animation = gtk_css_animated_style_find_animation (GTK_CSS_ANIMATED_STYLE (source)->animations, name);
371 372 373

      if (animation)
        {
374 375 376
          animation = _gtk_css_animation_advance_with_play_state (GTK_CSS_ANIMATION (animation),
                                                                  timestamp,
                                                                  _gtk_css_play_state_value_get (_gtk_css_array_value_get_nth (play_states, i)));
377 378 379 380 381 382 383
        }
      else
        {
          keyframes = _gtk_style_provider_private_get_keyframes (provider, name);
          if (keyframes == NULL)
            continue;

384
          keyframes = _gtk_css_keyframes_compute (keyframes, provider, base_style, parent_style);
385 386 387 388 389 390 391 392 393 394 395 396 397

          animation = _gtk_css_animation_new (name,
                                              keyframes,
                                              timestamp,
                                              _gtk_css_number_value_get (_gtk_css_array_value_get_nth (delays, i), 100) * G_USEC_PER_SEC,
                                              _gtk_css_number_value_get (_gtk_css_array_value_get_nth (durations, i), 100) * G_USEC_PER_SEC,
                                              _gtk_css_array_value_get_nth (timing_functions, i),
                                              _gtk_css_direction_value_get (_gtk_css_array_value_get_nth (directions, i)),
                                              _gtk_css_play_state_value_get (_gtk_css_array_value_get_nth (play_states, i)),
                                              _gtk_css_fill_mode_value_get (_gtk_css_array_value_get_nth (fill_modes, i)),
                                              _gtk_css_number_value_get (_gtk_css_array_value_get_nth (iteration_counts, i), 100));
          _gtk_css_keyframes_unref (keyframes);
        }
398
      animations = g_slist_prepend (animations, animation);
399
    }
400 401

  return animations;
402 403 404 405
}

/* PUBLIC API */

406
static void
407
gtk_css_animated_style_apply_animations (GtkCssAnimatedStyle *style)
408 409 410 411 412 413 414
{
  GSList *l;

  for (l = style->animations; l; l = l->next)
    {
      GtkStyleAnimation *animation = l->data;
      
415 416
      _gtk_style_animation_apply_values (animation,
                                         GTK_CSS_ANIMATED_STYLE (style));
417 418 419
    }
}

420 421 422 423 424 425
GtkCssStyle *
gtk_css_animated_style_new (GtkCssStyle             *base_style,
                            GtkCssStyle             *parent_style,
                            gint64                   timestamp,
                            GtkStyleProviderPrivate *provider,
                            GtkCssStyle             *previous_style)
426
{
427
  GtkCssAnimatedStyle *result;
428
  GSList *animations;
429 430 431 432 433 434
  
  gtk_internal_return_val_if_fail (GTK_IS_CSS_STYLE (base_style), NULL);
  gtk_internal_return_val_if_fail (parent_style == NULL || GTK_IS_CSS_STYLE (parent_style), NULL);
  gtk_internal_return_val_if_fail (GTK_IS_STYLE_PROVIDER (provider), NULL);
  gtk_internal_return_val_if_fail (previous_style == NULL || GTK_IS_CSS_STYLE (previous_style), NULL);

435 436 437
  if (timestamp == 0)
    return g_object_ref (base_style);

438 439 440 441
  animations = NULL;

  if (previous_style != NULL)
    animations = gtk_css_animated_style_create_css_transitions (animations, base_style, timestamp, previous_style);
442
  animations = gtk_css_animated_style_create_css_animations (animations, base_style, parent_style, timestamp, provider, previous_style);
443 444 445 446

  if (animations == NULL)
    return g_object_ref (base_style);

447 448 449
  result = g_object_new (GTK_TYPE_CSS_ANIMATED_STYLE, NULL);

  result->style = g_object_ref (base_style);
450
  result->current_time = timestamp;
451
  result->animations = animations;
452

453
  gtk_css_animated_style_apply_animations (result);
454

455
  return GTK_CSS_STYLE (result);
456 457
}

458 459
GtkCssStyle *
gtk_css_animated_style_new_advance (GtkCssAnimatedStyle *source,
460
                                    GtkCssStyle         *base,
461
                                    gint64               timestamp)
462
{
463 464
  GtkCssAnimatedStyle *result;
  GSList *l, *animations;
465

466
  gtk_internal_return_val_if_fail (GTK_IS_CSS_ANIMATED_STYLE (source), NULL);
467
  gtk_internal_return_val_if_fail (GTK_IS_CSS_STYLE (base), NULL);
468
  
469
  if (timestamp == 0 || timestamp == source->current_time)
470 471
    return g_object_ref (source->style);

472 473
  gtk_internal_return_val_if_fail (timestamp > source->current_time, NULL);

474 475
  animations = NULL;
  for (l = source->animations; l; l = l->next)
476
    {
477
      GtkStyleAnimation *animation = l->data;
478
      
479
      if (_gtk_style_animation_is_finished (animation))
480
        continue;
481

482 483
      animation = _gtk_style_animation_advance (animation, timestamp);
      animations = g_slist_prepend (animations, animation);
484 485
    }
  animations = g_slist_reverse (animations);
486

487 488
  if (animations == NULL)
    return g_object_ref (source->style);
489

490
  result = g_object_new (GTK_TYPE_CSS_ANIMATED_STYLE, NULL);
491

492
  result->style = g_object_ref (base);
493 494
  result->current_time = timestamp;
  result->animations = animations;
495

496
  gtk_css_animated_style_apply_animations (result);
497

498
  return GTK_CSS_STYLE (result);
499
}