summaryrefslogtreecommitdiff
path: root/gtk/gtkcssnumbervalue.c
blob: a9f44dbc5ebf3c2108ac6fd88425fd8c83de58da (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
/* GTK - The GIMP Toolkit
 * Copyright (C) 2016 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 "gtkcssnumbervalueprivate.h"

#include "gtkcsscalcvalueprivate.h"
#include "gtkcssdimensionvalueprivate.h"
#include "gtkcsswin32sizevalueprivate.h"

struct _GtkCssValue {
  GTK_CSS_VALUE_BASE
};

GtkCssDimension
gtk_css_number_value_get_dimension (const GtkCssValue *value)
{
  GtkCssNumberValueClass *number_value_class = (GtkCssNumberValueClass *) value->class;

  return number_value_class->get_dimension (value);
}

gboolean
gtk_css_number_value_has_percent (const GtkCssValue *value)
{
  GtkCssNumberValueClass *number_value_class = (GtkCssNumberValueClass *) value->class;

  return number_value_class->has_percent (value);
}

GtkCssValue *
gtk_css_number_value_multiply (const GtkCssValue *value,
                               double             factor)
{
  GtkCssNumberValueClass *number_value_class = (GtkCssNumberValueClass *) value->class;

  return number_value_class->multiply (value, factor);
}

GtkCssValue *
gtk_css_number_value_add (GtkCssValue *value1,
                          GtkCssValue *value2)
{
  GtkCssValue *sum;

  sum = gtk_css_number_value_try_add (value1, value2);
  if (sum == NULL)
    sum = gtk_css_calc_value_new_sum (value1, value2);

  return sum;
}

GtkCssValue *
gtk_css_number_value_try_add (const GtkCssValue *value1,
                              const GtkCssValue *value2)
{
  GtkCssNumberValueClass *number_value_class;
  
  if (value1->class != value2->class)
    return NULL;

  number_value_class = (GtkCssNumberValueClass *) value1->class;

  return number_value_class->try_add (value1, value2);
}

/*
 * gtk_css_number_value_get_calc_term_order:
 * @value: Value to compute order for
 *
 * Determines the position of @value when printed as part of a calc()
 * expression. Values with lower numbers are printed first. Note that
 * these numbers are arbitrary, so when adding new types of values to
 * print, feel free to change them in implementations so that they
 * match.
 *
 * Returns: Magic value determining placement when printing calc()
 *     expression.
 */
gint
gtk_css_number_value_get_calc_term_order (const GtkCssValue *value)
{
  GtkCssNumberValueClass *number_value_class = (GtkCssNumberValueClass *) value->class;

  return number_value_class->get_calc_term_order (value);
}

GtkCssValue *
_gtk_css_number_value_new (double     value,
                           GtkCssUnit unit)
{
  return gtk_css_dimension_value_new (value, unit);
}

GtkCssValue *
gtk_css_number_value_transition (GtkCssValue *start,
                                 GtkCssValue *end,
                                 guint        property_id,
                                 double       progress)
{
  GtkCssValue *result, *mul_start, *mul_end;

  mul_start = gtk_css_number_value_multiply (start, 1 - progress);
  mul_end = gtk_css_number_value_multiply (end, progress);

  result = gtk_css_number_value_add (mul_start, mul_end);

  _gtk_css_value_unref (mul_start);
  _gtk_css_value_unref (mul_end);

  return result;
}

gboolean
gtk_css_number_value_can_parse (GtkCssParser *parser)
{
  return _gtk_css_parser_has_number (parser)
      || _gtk_css_parser_has_prefix (parser, "calc")
      || _gtk_css_parser_has_prefix (parser, "-gtk-win32-size")
      || _gtk_css_parser_has_prefix (parser, "-gtk-win32-part-width")
      || _gtk_css_parser_has_prefix (parser, "-gtk-win32-part-height")
      || _gtk_css_parser_has_prefix (parser, "-gtk-win32-part-border-top")
      || _gtk_css_parser_has_prefix (parser, "-gtk-win32-part-border-left")
      || _gtk_css_parser_has_prefix (parser, "-gtk-win32-part-border-bottom")
      || _gtk_css_parser_has_prefix (parser, "-gtk-win32-part-border-right");
}

GtkCssValue *
_gtk_css_number_value_parse (GtkCssParser           *parser,
                             GtkCssNumberParseFlags  flags)
{
  if (_gtk_css_parser_has_prefix (parser, "calc"))
    return gtk_css_calc_value_parse (parser, flags);
  if (_gtk_css_parser_has_prefix (parser, "-gtk-win32-size") ||
      _gtk_css_parser_has_prefix (parser, "-gtk-win32-part-width") ||
      _gtk_css_parser_has_prefix (parser, "-gtk-win32-part-height") ||
      _gtk_css_parser_has_prefix (parser, "-gtk-win32-part-border-top") ||
      _gtk_css_parser_has_prefix (parser, "-gtk-win32-part-border-left") ||
      _gtk_css_parser_has_prefix (parser, "-gtk-win32-part-border-bottom") ||
      _gtk_css_parser_has_prefix (parser, "-gtk-win32-part-border-right"))
    return gtk_css_win32_size_value_parse (parser, flags);

  return gtk_css_dimension_value_parse (parser, flags);
}

gboolean
gtk_css_number_value_check_token (const GtkCssToken *token)
{
  return gtk_css_token_is (token, GTK_CSS_TOKEN_SIGNED_INTEGER)
      || gtk_css_token_is (token, GTK_CSS_TOKEN_SIGNLESS_INTEGER)
      || gtk_css_token_is (token, GTK_CSS_TOKEN_SIGNED_NUMBER)
      || gtk_css_token_is (token, GTK_CSS_TOKEN_SIGNLESS_NUMBER)
      || gtk_css_token_is (token, GTK_CSS_TOKEN_DIMENSION)
      || gtk_css_token_is (token, GTK_CSS_TOKEN_SIGNED_INTEGER_DIMENSION)
      || gtk_css_token_is (token, GTK_CSS_TOKEN_SIGNLESS_INTEGER_DIMENSION)
      || gtk_css_token_is (token, GTK_CSS_TOKEN_PERCENTAGE)
      || gtk_css_token_is_function (token, "calc")
      || gtk_css_token_is_function (token, "-gtk-win32-size")
      || gtk_css_token_is_function (token, "-gtk-win32-part-width")
      || gtk_css_token_is_function (token, "-gtk-win32-part-height")
      || gtk_css_token_is_function (token, "-gtk-win32-part-border-top")
      || gtk_css_token_is_function (token, "-gtk-win32-part-border-left")
      || gtk_css_token_is_function (token, "-gtk-win32-part-border-bottom")
      || gtk_css_token_is_function (token, "-gtk-win32-part-border-right");
}

GtkCssValue *
gtk_css_number_value_token_parse (GtkCssTokenSource      *source,
                                  GtkCssNumberParseFlags  flags)
{
  const GtkCssToken *token = gtk_css_token_source_get_token (source);

  if (gtk_css_token_is_function (token, "calc"))
    return gtk_css_calc_value_token_parse (source, flags);
  else if (gtk_css_token_is_function (token, "-gtk-win32-size") ||
           gtk_css_token_is_function (token, "-gtk-win32-part-width") ||
           gtk_css_token_is_function (token, "-gtk-win32-part-height") ||
           gtk_css_token_is_function (token, "-gtk-win32-part-border-top") ||
           gtk_css_token_is_function (token, "-gtk-win32-part-border-left") ||
           gtk_css_token_is_function (token, "-gtk-win32-part-border-bottom") ||
           gtk_css_token_is_function (token, "-gtk-win32-part-border-right"))
    return gtk_css_win32_size_value_token_parse (source, flags);
  else
    return gtk_css_dimension_value_token_parse (source, flags);
}

double
_gtk_css_number_value_get (const GtkCssValue *number,
                           double             one_hundred_percent)
{
  GtkCssNumberValueClass *number_value_class;

  g_return_val_if_fail (number != NULL, 0.0);

  number_value_class = (GtkCssNumberValueClass *) number->class;

  return number_value_class->get (number, one_hundred_percent);
}