summaryrefslogtreecommitdiff
path: root/gtk/gtkcssbordervalue.c
blob: 7161470063f98041975b6a37d16a77ad376b17ef (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
/* GTK - The GIMP Toolkit
 * Copyright (C) 2011 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 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 "gtkcssbordervalueprivate.h"

#include "gtkcssnumbervalueprivate.h"

struct _GtkCssValue {
  GTK_CSS_VALUE_BASE
  guint fill :1;
  GtkCssValue *values[4];
};

static void
gtk_css_value_border_free (GtkCssValue *value)
{
  guint i;

  for (i = 0; i < 4; i++)
    {
      if (value->values[i])
        _gtk_css_value_unref (value->values[i]);
    }

  g_slice_free (GtkCssValue, value);
}

static GtkCssValue *
gtk_css_value_border_compute (GtkCssValue             *value,
                              guint                    property_id,
                              GtkStyleProviderPrivate *provider,
                              GtkCssStyle             *style,
                              GtkCssStyle             *parent_style)
{
  GtkCssValue *computed;
  gboolean changed = FALSE;
  guint i;

  computed = _gtk_css_border_value_new (NULL, NULL, NULL, NULL);
  computed->fill = value->fill;

  for (i = 0; i < 4; i++)
    {
      if (value->values[i])
        {
          computed->values[i] = _gtk_css_value_compute (value->values[i], property_id, provider, style, parent_style);
          changed |= (computed->values[i] != value->values[i]);
        }
    }

  if (!changed)
    {
      _gtk_css_value_unref (computed);
      return _gtk_css_value_ref (value);
    }

  return computed;
}

static gboolean
gtk_css_value_border_equal (const GtkCssValue *value1,
                            const GtkCssValue *value2)
{
  guint i;

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

  for (i = 0; i < 4; i++)
    {
      if (!_gtk_css_value_equal0 (value1->values[i], value2->values[i]))
        return FALSE;
    }

  return TRUE;
}

static GtkCssValue *
gtk_css_value_border_transition (GtkCssValue *start,
                                 GtkCssValue *end,
                                 guint        property_id,
                                 double       progress)
{
  return NULL;
}

static void
gtk_css_value_border_print (const GtkCssValue *value,
                            GString           *string)
{
  guint i, n;

  if (!_gtk_css_value_equal0 (value->values[GTK_CSS_RIGHT], value->values[GTK_CSS_LEFT]))
    n = 4;
  else if (!_gtk_css_value_equal0 (value->values[GTK_CSS_TOP], value->values[GTK_CSS_BOTTOM]))
    n = 3;
  else if (!_gtk_css_value_equal0 (value->values[GTK_CSS_TOP], value->values[GTK_CSS_RIGHT]))
    n = 2;
  else
    n = 1;

  for (i = 0; i < n; i++)
    {
      if (i > 0)
        g_string_append_c (string, ' ');

      if (value->values[i] == NULL)
        g_string_append (string, "auto");
      else
        _gtk_css_value_print (value->values[i], string);
    }

  if (value->fill)
    g_string_append (string, " fill");
}

static const GtkCssValueClass GTK_CSS_VALUE_BORDER = {
  gtk_css_value_border_free,
  gtk_css_value_border_compute,
  gtk_css_value_border_equal,
  gtk_css_value_border_transition,
  gtk_css_value_border_print
};

GtkCssValue *
_gtk_css_border_value_new (GtkCssValue *top,
                           GtkCssValue *right,
                           GtkCssValue *bottom,
                           GtkCssValue *left)
{
  GtkCssValue *result;

  result = _gtk_css_value_new (GtkCssValue, &GTK_CSS_VALUE_BORDER);
  result->values[GTK_CSS_TOP] = top;
  result->values[GTK_CSS_RIGHT] = right;
  result->values[GTK_CSS_BOTTOM] = bottom;
  result->values[GTK_CSS_LEFT] = left;

  return result;
}

GtkCssValue *
_gtk_css_border_value_parse (GtkCssParser           *parser,
                             GtkCssNumberParseFlags  flags,
                             gboolean                allow_auto,
                             gboolean                allow_fill)
{
  GtkCssValue *result;
  guint i;

  result = _gtk_css_border_value_new (NULL, NULL, NULL, NULL);

  if (allow_fill)
    result->fill = _gtk_css_parser_try (parser, "fill", TRUE);

  for (i = 0; i < 4; i++)
    {
      if (allow_auto && _gtk_css_parser_try (parser, "auto", TRUE))
        continue;

      if (!_gtk_css_parser_has_number (parser))
        break;

      result->values[i] = _gtk_css_number_value_parse (parser, flags);
      if (result->values[i] == NULL)
        {
          _gtk_css_value_unref (result);
          return NULL;
        }
    }

  if (i == 0)
    {
      _gtk_css_parser_error (parser, "Expected a number");
      _gtk_css_value_unref (result);
      return NULL;
    }

  if (allow_fill && !result->fill)
    result->fill = _gtk_css_parser_try (parser, "fill", TRUE);

  for (; i < 4; i++)
    {
      if (result->values[(i - 1) >> 1])
        result->values[i] = _gtk_css_value_ref (result->values[(i - 1) >> 1]);
    }

  return result;
}

GtkCssValue *
_gtk_css_border_value_get_top (const GtkCssValue *value)
{
  g_return_val_if_fail (value->class == &GTK_CSS_VALUE_BORDER, NULL);

  return value->values[GTK_CSS_TOP];
}

GtkCssValue *
_gtk_css_border_value_get_right (const GtkCssValue *value)
{
  g_return_val_if_fail (value->class == &GTK_CSS_VALUE_BORDER, NULL);

  return value->values[GTK_CSS_RIGHT];
}

GtkCssValue *
_gtk_css_border_value_get_bottom (const GtkCssValue *value)
{
  g_return_val_if_fail (value->class == &GTK_CSS_VALUE_BORDER, NULL);

  return value->values[GTK_CSS_BOTTOM];
}

GtkCssValue *
_gtk_css_border_value_get_left (const GtkCssValue *value)
{
  g_return_val_if_fail (value->class == &GTK_CSS_VALUE_BORDER, NULL);

  return value->values[GTK_CSS_LEFT];
}