summaryrefslogtreecommitdiff
path: root/gtk/gtkcsslineheightvalue.c
blob: c9104cee24bbf6211e06ca6aea361800faef508f (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
/* GTK - The GIMP Toolkit
 * Copyright (C) 2021 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 "gtkcsslineheightvalueprivate.h"
#include "gtkcssnumbervalueprivate.h"
#include "gtkcssstyleprivate.h"

struct _GtkCssValue {
  GTK_CSS_VALUE_BASE
  GtkCssValue *height;
};

static GtkCssValue *default_line_height;

static GtkCssValue *gtk_css_value_line_height_new_empty (void);
static GtkCssValue *gtk_css_value_line_height_new       (GtkCssValue *height);

static void
gtk_css_value_line_height_free (GtkCssValue *value)
{
  _gtk_css_value_unref (value->height);
  g_slice_free (GtkCssValue, value);
}

static GtkCssValue *
gtk_css_value_line_height_compute (GtkCssValue      *value,
                                   guint             property_id,
                                   GtkStyleProvider *provider,
                                   GtkCssStyle      *style,
                                   GtkCssStyle      *parent_style)
{
  GtkCssValue *height;

  height = _gtk_css_value_compute (value->height, property_id, provider, style, parent_style);

  if (gtk_css_number_value_get_dimension (height) == GTK_CSS_DIMENSION_PERCENTAGE)
    {
      double factor;
      GtkCssValue *val;
      GtkCssValue *computed;

      factor =  _gtk_css_number_value_get (height, 1);
      val = gtk_css_dimension_value_new (factor, GTK_CSS_EM);

      computed = _gtk_css_value_compute (val,
                                         GTK_CSS_PROPERTY_FONT_SIZE,
                                         provider,
                                         style,
                                         parent_style);

      _gtk_css_value_unref (val);
      _gtk_css_value_unref (height);

      return computed;
    }
  else
    {
      return height;
    }
}

static gboolean
gtk_css_value_line_height_equal (const GtkCssValue *value1,
                                 const GtkCssValue *value2)
{
  if (value1->height == NULL || value2->height == NULL)
    return FALSE;

  return _gtk_css_value_equal (value1->height, value2->height);
}

static GtkCssValue *
gtk_css_value_line_height_transition (GtkCssValue *start,
                                      GtkCssValue *end,
                                      guint        property_id,
                                      double       progress)
{
  GtkCssValue *height;

  if (start->height == NULL || end->height == NULL)
    return NULL;

  height = _gtk_css_value_transition (start->height, end->height, property_id, progress);
  if (height == NULL)
    return NULL;

  return gtk_css_value_line_height_new (height);
}

static void
gtk_css_value_line_height_print (const GtkCssValue *value,
                                 GString           *string)
{
  if (value->height == NULL)
    g_string_append (string, "normal");
  else
    _gtk_css_value_print (value->height, string);
}

static const GtkCssValueClass GTK_CSS_VALUE_LINE_HEIGHT = {
  "GtkCssLineHeightValue",
  gtk_css_value_line_height_free,
  gtk_css_value_line_height_compute,
  gtk_css_value_line_height_equal,
  gtk_css_value_line_height_transition,
  NULL,
  NULL,
  gtk_css_value_line_height_print
};

static GtkCssValue *
gtk_css_value_line_height_new_empty (void)
{
  GtkCssValue *result;

  result = _gtk_css_value_new (GtkCssValue, &GTK_CSS_VALUE_LINE_HEIGHT);
  result->height = NULL;
  result->is_computed = TRUE;

  return result;
}

static GtkCssValue *
gtk_css_value_line_height_new (GtkCssValue *height)
{
  GtkCssValue *result;

  result = _gtk_css_value_new (GtkCssValue, &GTK_CSS_VALUE_LINE_HEIGHT);
  result->height = height;

  return result;
}

GtkCssValue *
gtk_css_line_height_value_get_default (void)
{
  if (default_line_height == NULL)
    default_line_height = gtk_css_value_line_height_new_empty ();

  return default_line_height;
}

GtkCssValue *
gtk_css_line_height_value_parse (GtkCssParser *parser)
{
  GtkCssValue *height;

  if (gtk_css_parser_try_ident (parser, "normal"))
    return _gtk_css_value_ref (gtk_css_line_height_value_get_default ());

  height = _gtk_css_number_value_parse (parser, GTK_CSS_PARSE_NUMBER |
                                                GTK_CSS_PARSE_PERCENT |
                                                GTK_CSS_PARSE_LENGTH |
                                                GTK_CSS_POSITIVE_ONLY);
  if (!height)
    return NULL;

  return gtk_css_value_line_height_new (height);
}

double
gtk_css_line_height_value_get (const GtkCssValue *value)
{
  if (value->class == &GTK_CSS_VALUE_LINE_HEIGHT)
    return 0.0;

  return _gtk_css_number_value_get (value, 1);
}