File: gtkcssvariablevalue.c

package info (click to toggle)
gtk4 4.20.3%2Bds-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 187,060 kB
  • sloc: ansic: 779,084; xml: 3,093; javascript: 3,054; python: 1,911; java: 752; sh: 682; makefile: 315; perl: 162; cpp: 21
file content (158 lines) | stat: -rw-r--r-- 4,137 bytes parent folder | download | duplicates (4)
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
/*
 * Copyright (C) 2023 GNOME Foundation 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: Alice Mikhaylenko <alicem@gnome.org>
 */

#include "gtkcssvariablevalueprivate.h"

GtkCssVariableValue *
gtk_css_variable_value_new (GBytes                       *bytes,
                            gsize                         offset,
                            gsize                         end_offset,
                            gsize                         length,
                            GtkCssVariableValueReference *references,
                            gsize                         n_references)
{
  GtkCssVariableValue *self = g_new0 (GtkCssVariableValue, 1);

  self->ref_count = 1;

  self->bytes = g_bytes_ref (bytes);
  self->offset = offset;
  self->end_offset = end_offset;
  self->length = length;

  self->references = references;
  self->n_references = n_references;

  return self;
}

GtkCssVariableValue *
gtk_css_variable_value_new_initial (GBytes *bytes,
                                    gsize   offset,
                                    gsize   end_offset)
{
  GtkCssVariableValue *self = gtk_css_variable_value_new (bytes, offset, end_offset, 1, NULL, 0);

  self->is_invalid = TRUE;

  return self;
}

GtkCssVariableValue *
gtk_css_variable_value_ref (GtkCssVariableValue *self)
{
  self->ref_count++;

  return self;
}

void
gtk_css_variable_value_unref (GtkCssVariableValue *self)
{
  gsize i;

  self->ref_count--;
  if (self->ref_count > 0)
    return;

  g_bytes_unref (self->bytes);

  for (i = 0; i < self->n_references; i++)
    {
      GtkCssVariableValueReference *ref = &self->references[i];

      g_free (ref->name);
      if (ref->fallback)
        gtk_css_variable_value_unref (ref->fallback);
    }

  if (self->section)
    gtk_css_section_unref (self->section);

  g_free (self->references);
  g_free (self);
}

void
gtk_css_variable_value_print (GtkCssVariableValue *self,
                              GString             *string)
{
  gsize len = self->end_offset - self->offset;
  gconstpointer data = g_bytes_get_region (self->bytes, 1, self->offset, len);

  g_assert (data != NULL);

  g_string_append_len (string, (const char *) data, len);
}

char *
gtk_css_variable_value_to_string (GtkCssVariableValue *self)
{
  GString *string = g_string_new (NULL);
  gtk_css_variable_value_print (self, string);
  return g_string_free (string, FALSE);
}

gboolean
gtk_css_variable_value_equal (const GtkCssVariableValue *value1,
                              const GtkCssVariableValue *value2)
{
  if (value1 == value2)
    return TRUE;

  if (value1 == NULL || value2 == NULL)
    return FALSE;

  if (value1->bytes != value2->bytes)
    return FALSE;

  if (value1->offset != value2->offset)
    return FALSE;

  if (value1->end_offset != value2->end_offset)
    return FALSE;

  return TRUE;
}

GtkCssVariableValue *
gtk_css_variable_value_transition  (GtkCssVariableValue *start,
                                    GtkCssVariableValue *end,
                                    double               progress)
{
  GtkCssVariableValue *ret = progress < 0.5 ? start : end;

  if (ret == NULL)
    return NULL;

  return gtk_css_variable_value_ref (ret);
}

void
gtk_css_variable_value_set_section (GtkCssVariableValue *self,
                                    GtkCssSection       *section)
{
  self->section = gtk_css_section_ref (section);
}

void
gtk_css_variable_value_taint (GtkCssVariableValue *self)
{
  self->is_animation_tainted = TRUE;
}