summaryrefslogtreecommitdiff
path: root/gtk/gtkcssimagecrossfade.c
blob: 6953606c8edf6943c419b437c199fcca2d0df0ec (plain)
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
/*
 * 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 <math.h>
#include <string.h>

#include "gtkcssimagecrossfadeprivate.h"

#include "gtkcssnumbervalueprivate.h"
#include "gtkcssimagefallbackprivate.h"
#include "gtkcsscolorvalueprivate.h"


typedef struct _CrossFadeEntry CrossFadeEntry;

struct _CrossFadeEntry
{
  double progress;
  gboolean has_progress;
  GtkCssImage *image;
};

G_DEFINE_TYPE (GtkCssImageCrossFade, gtk_css_image_cross_fade, GTK_TYPE_CSS_IMAGE)

static void
cross_fade_entry_clear (gpointer data)
{
  CrossFadeEntry *entry = data;

  g_clear_object (&entry->image);
}

static void
gtk_css_image_cross_fade_recalculate_progress (GtkCssImageCrossFade *self)
{
  double total_progress;
  guint n_no_progress;
  guint i;

  total_progress = 0.0;
  n_no_progress = 0;

  for (i = 0; i < self->images->len; i++)
    {
      CrossFadeEntry *entry = &g_array_index (self->images, CrossFadeEntry, i);

      if (entry->has_progress)
        total_progress += entry->progress;
      else
        n_no_progress++;
    }

  if (n_no_progress)
    {
      double progress;
      if (total_progress >= 1.0)
        {
          progress = 0.0;
        }
      else
        {
          progress = (1.0 - total_progress) / n_no_progress;
          total_progress = 1.0;
        }
      for (i = 0; i < self->images->len; i++)
        {
          CrossFadeEntry *entry = &g_array_index (self->images, CrossFadeEntry, i);

          if (!entry->has_progress)
            entry->progress = progress;
        }
    }

  self->total_progress = total_progress;
}

static void
gtk_css_image_cross_fade_add (GtkCssImageCrossFade *self,
                              gboolean              has_progress,
                              double                progress,
                              GtkCssImage          *image)
{
  CrossFadeEntry entry;

  entry.has_progress = has_progress;
  entry.progress = progress;
  entry.image = image;
  g_array_append_val (self->images, entry);

  gtk_css_image_cross_fade_recalculate_progress (self);
}

static GtkCssImageCrossFade *
gtk_css_image_cross_fade_new_empty (void)
{
  return g_object_new (GTK_TYPE_CSS_IMAGE_CROSS_FADE, NULL);
}

/* XXX: The following is not correct, it should actually run the
 * CSS sizing algorithm for every child, not just query height and
 * width independently.
 */
static int
gtk_css_image_cross_fade_get_width (GtkCssImage *image)
{
  GtkCssImageCrossFade *self = GTK_CSS_IMAGE_CROSS_FADE (image);
  double sum_width, sum_progress;
  guint i;

  sum_width = 0.0;
  sum_progress = 0.0;

  for (i = 0; i < self->images->len; i++)
    {
      CrossFadeEntry *entry = &g_array_index (self->images, CrossFadeEntry, i);
      int image_width;

      image_width = _gtk_css_image_get_width (entry->image);
      if (image_width == 0)
        continue;
      sum_width += image_width * entry->progress;
      sum_progress += entry->progress;
    }

  if (sum_progress <= 0.0)
    return 0;

  return ceil (sum_width / sum_progress);
}

static int
gtk_css_image_cross_fade_get_height (GtkCssImage *image)
{
  GtkCssImageCrossFade *self = GTK_CSS_IMAGE_CROSS_FADE (image);
  double sum_height, sum_progress;
  guint i;

  sum_height = 0.0;
  sum_progress = 0.0;

  for (i = 0; i < self->images->len; i++)
    {
      CrossFadeEntry *entry = &g_array_index (self->images, CrossFadeEntry, i);
      int image_height;

      image_height = _gtk_css_image_get_height (entry->image);
      if (image_height == 0)
        continue;
      sum_height += image_height * entry->progress;
      sum_progress += entry->progress;
    }

  if (sum_progress <= 0.0)
    return 0;

  return ceil (sum_height / sum_progress);
}

static gboolean
gtk_css_image_cross_fade_equal (GtkCssImage *image1,
                                GtkCssImage *image2)
{
  GtkCssImageCrossFade *cross_fade1 = GTK_CSS_IMAGE_CROSS_FADE (image1);
  GtkCssImageCrossFade *cross_fade2 = GTK_CSS_IMAGE_CROSS_FADE (image2);
  guint i;

  if (cross_fade1->images->len != cross_fade2->images->len)
    return FALSE;

  for (i = 0; i < cross_fade1->images->len; i++)
    {
      CrossFadeEntry *entry1 = &g_array_index (cross_fade1->images, CrossFadeEntry, i);
      CrossFadeEntry *entry2 = &g_array_index (cross_fade2->images, CrossFadeEntry, i);

      if (entry1->progress != entry2->progress ||
          !_gtk_css_image_equal (entry1->image, entry2->image))
        return FALSE;
    }
  
  return TRUE;
}

static gboolean
gtk_css_image_cross_fade_is_dynamic (GtkCssImage *image)
{
  GtkCssImageCrossFade *self = GTK_CSS_IMAGE_CROSS_FADE (image);
  guint i;

  for (i = 0; i < self->images->len; i++)
    {
      CrossFadeEntry *entry = &g_array_index (self->images, CrossFadeEntry, i);

      if (gtk_css_image_is_dynamic (entry->image))
        return TRUE;
    }

  return FALSE;
}

static GtkCssImage *
gtk_css_image_cross_fade_get_dynamic_image (GtkCssImage *image,
                                            gint64       monotonic_time)
{
  GtkCssImageCrossFade *self = GTK_CSS_IMAGE_CROSS_FADE (image);
  GtkCssImageCrossFade *result;
  guint i;

  result = gtk_css_image_cross_fade_new_empty ();

  for (i = 0; i < self->images->len; i++)
    {
      CrossFadeEntry *entry = &g_array_index (self->images, CrossFadeEntry, i);

      gtk_css_image_cross_fade_add (result,
                                    entry->has_progress,
                                    entry->progress,
                                    gtk_css_image_get_dynamic_image (entry->image, monotonic_time));
    }

  return GTK_CSS_IMAGE (result);
}

static void
gtk_css_image_cross_fade_snapshot (GtkCssImage *image,
                                   GtkSnapshot *snapshot,
                                   double       width,
                                   double       height)
{
  GtkCssImageCrossFade *self = GTK_CSS_IMAGE_CROSS_FADE (image);
  double remaining;
  guint i, n_cross_fades;

  if (self->total_progress < 1.0)
    {
      n_cross_fades = self->images->len;
      remaining = 1.0;
    }
  else
    {
      n_cross_fades = self->images->len - 1;
      remaining = self->total_progress;
    }

  for (i = 0; i < n_cross_fades; i++)
    {
      CrossFadeEntry *entry = &g_array_index (self->images, CrossFadeEntry, i);

      gtk_snapshot_push_cross_fade (snapshot, 1.0 - entry->progress / remaining);
      remaining -= entry->progress;
      gtk_css_image_snapshot (entry->image, snapshot, width, height);
      gtk_snapshot_pop (snapshot);
    }

  if (n_cross_fades < self->images->len)
    {
      CrossFadeEntry *entry = &g_array_index (self->images, CrossFadeEntry, self->images->len - 1);
      gtk_css_image_snapshot (entry->image, snapshot, width, height);
    }

  for (i = 0; i < n_cross_fades; i++)
    {
      gtk_snapshot_pop (snapshot);
    }
}

static gboolean
parse_progress (GtkCssParser *parser,
                gpointer      option_data,
                gpointer      user_data)
{
  double *progress = option_data;
  GtkCssValue *number;
  
  number = _gtk_css_number_value_parse (parser, GTK_CSS_PARSE_PERCENT | GTK_CSS_POSITIVE_ONLY);
  if (number == NULL)
    return FALSE;
  *progress = _gtk_css_number_value_get (number, 1);
  _gtk_css_value_unref (number);

  if (*progress > 1.0)
    {
      gtk_css_parser_error_value (parser, "Percentages over 100%% are not allowed. Given value: %f", *progress);
      return FALSE;
    }

  return TRUE;
}

static gboolean
parse_image (GtkCssParser *parser,
             gpointer      option_data,
             gpointer      user_data)
{
  GtkCssImage **image = option_data;

  if (_gtk_css_image_can_parse (parser))
    {
      *image = _gtk_css_image_new_parse (parser);
      if (*image == NULL)
        return FALSE;

      return TRUE;
    }
  else if (gtk_css_color_value_can_parse (parser))
    {
      GtkCssValue *color;

      color = _gtk_css_color_value_parse (parser);
      if (color == NULL)
        return FALSE;

      *image = _gtk_css_image_fallback_new_for_color (color);

      return TRUE;
    }
  
  return FALSE;
}

static guint
gtk_css_image_cross_fade_parse_arg (GtkCssParser *parser,
                                    guint         arg,
                                    gpointer      data)
{
  GtkCssImageCrossFade *self = data;
  double progress = -1.0;
  GtkCssImage *image = NULL;
  GtkCssParseOption options[] =
    {
      { (void *)gtk_css_number_value_can_parse, parse_progress, &progress },
      { NULL, parse_image, &image },
    };

  if (!gtk_css_parser_consume_any (parser, options, G_N_ELEMENTS (options), self))
    return 0;

  g_assert (image != NULL);

  if (progress < 0.0)
    gtk_css_image_cross_fade_add (self, FALSE, 0.0, image);
  else
    gtk_css_image_cross_fade_add (self, TRUE, progress, image);

  return 1;
}

static gboolean
gtk_css_image_cross_fade_parse (GtkCssImage  *image,
                                GtkCssParser *parser)
{
  if (!gtk_css_parser_has_function (parser, "cross-fade"))
    {
      gtk_css_parser_error_syntax (parser, "Expected 'cross-fade('");
      return FALSE;
    }

  return gtk_css_parser_consume_function (parser, 1, G_MAXUINT, gtk_css_image_cross_fade_parse_arg, image);
}

static void
gtk_css_image_cross_fade_print (GtkCssImage *image,
                                GString     *string)
{
  GtkCssImageCrossFade *self = GTK_CSS_IMAGE_CROSS_FADE (image);
  guint i;

  g_string_append (string, "cross-fade(");

  for (i = 0; i < self->images->len; i++)
    {
      CrossFadeEntry *entry = &g_array_index (self->images, CrossFadeEntry, i);

      if (i > 0)
        g_string_append_printf (string, ", ");
      if (entry->has_progress)
        g_string_append_printf (string, "%g%% ", entry->progress * 100.0);
      _gtk_css_image_print (entry->image, string);
    }

  g_string_append (string, ")");
}

static GtkCssImage *
gtk_css_image_cross_fade_compute (GtkCssImage      *image,
                                  guint             property_id,
                                  GtkStyleProvider *provider,
                                  GtkCssStyle      *style,
                                  GtkCssStyle      *parent_style)
{
  GtkCssImageCrossFade *self = GTK_CSS_IMAGE_CROSS_FADE (image);
  GtkCssImageCrossFade *result;
  guint i;

  result = gtk_css_image_cross_fade_new_empty ();

  for (i = 0; i < self->images->len; i++)
    {
      CrossFadeEntry *entry = &g_array_index (self->images, CrossFadeEntry, i);

      gtk_css_image_cross_fade_add (result,
                                    entry->has_progress,
                                    entry->progress,
                                    _gtk_css_image_compute (entry->image, property_id, provider, style, parent_style));
    }

  return GTK_CSS_IMAGE (result);
}

static void
gtk_css_image_cross_fade_dispose (GObject *object)
{
  GtkCssImageCrossFade *cross_fade = GTK_CSS_IMAGE_CROSS_FADE (object);

  g_clear_pointer (&cross_fade->images, g_array_unref);

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

static gboolean
gtk_css_image_cross_fade_is_computed (GtkCssImage *image)
{
  GtkCssImageCrossFade *cross_fade = GTK_CSS_IMAGE_CROSS_FADE (image);
  guint i;

  for (i = 0; i < cross_fade->images->len; i++)
    {
      const CrossFadeEntry *entry = &g_array_index (cross_fade->images, CrossFadeEntry, i);
      if (!gtk_css_image_is_computed (entry->image))
        return FALSE;
    }

  return TRUE;
}

static void
gtk_css_image_cross_fade_class_init (GtkCssImageCrossFadeClass *klass)
{
  GtkCssImageClass *image_class = GTK_CSS_IMAGE_CLASS (klass);
  GObjectClass *object_class = G_OBJECT_CLASS (klass);

  image_class->get_width = gtk_css_image_cross_fade_get_width;
  image_class->get_height = gtk_css_image_cross_fade_get_height;
  image_class->compute = gtk_css_image_cross_fade_compute;
  image_class->equal = gtk_css_image_cross_fade_equal;
  image_class->snapshot = gtk_css_image_cross_fade_snapshot;
  image_class->is_dynamic = gtk_css_image_cross_fade_is_dynamic;
  image_class->get_dynamic_image = gtk_css_image_cross_fade_get_dynamic_image;
  image_class->parse = gtk_css_image_cross_fade_parse;
  image_class->print = gtk_css_image_cross_fade_print;
  image_class->is_computed = gtk_css_image_cross_fade_is_computed;

  object_class->dispose = gtk_css_image_cross_fade_dispose;
}

static void
gtk_css_image_cross_fade_init (GtkCssImageCrossFade *self)
{
  self->images = g_array_new (FALSE, FALSE, sizeof (CrossFadeEntry));
  g_array_set_clear_func (self->images, cross_fade_entry_clear);
}

GtkCssImage *
_gtk_css_image_cross_fade_new (GtkCssImage *start,
                               GtkCssImage *end,
                               double       progress)
{
  GtkCssImageCrossFade *self;

  g_return_val_if_fail (start == NULL || GTK_IS_CSS_IMAGE (start), NULL);
  g_return_val_if_fail (end == NULL || GTK_IS_CSS_IMAGE (end), NULL);

  self = gtk_css_image_cross_fade_new_empty ();

  if (start)
    gtk_css_image_cross_fade_add (self, TRUE, 1.0 - progress, g_object_ref (start));
  if (end)
    gtk_css_image_cross_fade_add (self, TRUE, progress, g_object_ref (end));

  return GTK_CSS_IMAGE (self);
}