gtkcsspositionvalue.c 8.66 KB
Newer Older
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
/* 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 "gtkcsspositionvalueprivate.h"

#include "gtkcssnumbervalueprivate.h"

struct _GtkCssValue {
  GTK_CSS_VALUE_BASE
  GtkCssValue *x;
  GtkCssValue *y;
};

static void
gtk_css_value_position_free (GtkCssValue *value)
{
  _gtk_css_value_unref (value->x);
  _gtk_css_value_unref (value->y);

  g_slice_free (GtkCssValue, value);
}

39
static GtkCssValue *
40 41 42
gtk_css_value_position_compute (GtkCssValue             *position,
                                guint                    property_id,
                                GtkStyleProviderPrivate *provider,
43
                                GtkCssStyle             *style,
44
                                GtkCssStyle             *parent_style)
45 46 47
{
  GtkCssValue *x, *y;

48 49
  x = _gtk_css_value_compute (position->x, property_id, provider, style, parent_style);
  y = _gtk_css_value_compute (position->y, property_id, provider, style, parent_style);
50 51 52 53 54 55 56 57 58 59
  if (x == position->x && y == position->y)
    {
      _gtk_css_value_unref (x);
      _gtk_css_value_unref (y);
      return _gtk_css_value_ref (position);
    }

  return _gtk_css_position_value_new (x, y);
}

60 61 62 63 64 65 66 67 68 69 70
static gboolean
gtk_css_value_position_equal (const GtkCssValue *position1,
                              const GtkCssValue *position2)
{
  return _gtk_css_value_equal (position1->x, position2->x)
      && _gtk_css_value_equal (position1->y, position2->y);
}

static GtkCssValue *
gtk_css_value_position_transition (GtkCssValue *start,
                                   GtkCssValue *end,
71
                                   guint        property_id,
72 73 74 75
                                   double       progress)
{
  GtkCssValue *x, *y;

76
  x = _gtk_css_value_transition (start->x, end->x, property_id, progress);
77 78
  if (x == NULL)
    return NULL;
79
  y = _gtk_css_value_transition (start->y, end->y, property_id, progress);
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
  if (y == NULL)
    {
      _gtk_css_value_unref (x);
      return NULL;
    }

  return _gtk_css_position_value_new (x, y);
}

static void
gtk_css_value_position_print (const GtkCssValue *position,
                              GString           *string)
{
  struct {
    const char *x_name;
    const char *y_name;
    GtkCssValue *number;
  } values[] = { 
    { "left",   "top",    _gtk_css_number_value_new (0, GTK_CSS_PERCENT) },
    { "right",  "bottom", _gtk_css_number_value_new (100, GTK_CSS_PERCENT) }
  };
  GtkCssValue *center = _gtk_css_number_value_new (50, GTK_CSS_PERCENT);
  guint i;

  if (_gtk_css_value_equal (position->x, center))
    {
      if (_gtk_css_value_equal (position->y, center))
        {
          g_string_append (string, "center");
          goto done;
        }
    }
  else
    {
      for (i = 0; i < G_N_ELEMENTS (values); i++)
        {
          if (_gtk_css_value_equal (position->x, values[i].number))
            {
              g_string_append (string, values[i].x_name);
              break;
            }
        }
      if (i == G_N_ELEMENTS (values))
        _gtk_css_value_print (position->x, string);

      if (_gtk_css_value_equal (position->y, center))
        goto done;

      g_string_append_c (string, ' ');
    }

  for (i = 0; i < G_N_ELEMENTS (values); i++)
    {
      if (_gtk_css_value_equal (position->y, values[i].number))
        {
          g_string_append (string, values[i].y_name);
          goto done;
        }
    }
  if (i == G_N_ELEMENTS (values))
    {
      if (_gtk_css_value_equal (position->x, center))
        g_string_append (string, "center ");
      _gtk_css_value_print (position->y, string);
    }

done:
  for (i = 0; i < G_N_ELEMENTS (values); i++)
    _gtk_css_value_unref (values[i].number);
  _gtk_css_value_unref (center);
}

static const GtkCssValueClass GTK_CSS_VALUE_POSITION = {
  gtk_css_value_position_free,
154
  gtk_css_value_position_compute,
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
  gtk_css_value_position_equal,
  gtk_css_value_position_transition,
  gtk_css_value_position_print
};

GtkCssValue *
_gtk_css_position_value_new (GtkCssValue *x,
                             GtkCssValue *y)
{
  GtkCssValue *result;

  result = _gtk_css_value_new (GtkCssValue, &GTK_CSS_VALUE_POSITION);
  result->x = x;
  result->y = y;

  return result;
}

Matthias Clasen's avatar
Matthias Clasen committed
173 174
static GtkCssValue *
position_value_parse (GtkCssParser *parser, gboolean try)
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
{
  static const struct {
    const char *name;
    guint       percentage;
    gboolean    horizontal;
    gboolean    vertical;
  } names[] = {
    { "left",     0, TRUE,  FALSE },
    { "right",  100, TRUE,  FALSE },
    { "center",  50, TRUE,  TRUE  },
    { "top",      0, FALSE, TRUE  },
    { "bottom", 100, FALSE, TRUE  },
    { NULL    ,   0, TRUE,  FALSE }, /* used for numbers */
    { NULL    ,  50, TRUE,  TRUE  }  /* used for no value */
  };
  GtkCssValue *x, *y;
  GtkCssValue **missing;
  guint first, second;

  for (first = 0; names[first].name != NULL; first++)
    {
      if (_gtk_css_parser_try (parser, names[first].name, TRUE))
        {
          if (names[first].horizontal)
            {
	      x = _gtk_css_number_value_new (names[first].percentage, GTK_CSS_PERCENT);
              missing = &y;
            }
          else
            {
	      y = _gtk_css_number_value_new (names[first].percentage, GTK_CSS_PERCENT);
              missing = &x;
            }
          break;
        }
    }
  if (names[first].name == NULL)
    {
213
      if (gtk_css_number_value_can_parse (parser))
214 215 216 217 218 219 220 221 222 223
        {
          missing = &y;
          x = _gtk_css_number_value_parse (parser,
                                           GTK_CSS_PARSE_PERCENT
                                           | GTK_CSS_PARSE_LENGTH);

          if (x == NULL)
            return NULL;
        }
      else
224
        {
Matthias Clasen's avatar
Matthias Clasen committed
225 226
          if (!try)
            _gtk_css_parser_error (parser, "Unrecognized position value");
227 228
          return NULL;
        }
229 230 231 232 233 234 235 236 237 238 239 240 241
    }

  for (second = 0; names[second].name != NULL; second++)
    {
      if (_gtk_css_parser_try (parser, names[second].name, TRUE))
        {
	  *missing = _gtk_css_number_value_new (names[second].percentage, GTK_CSS_PERCENT);
          break;
        }
    }

  if (names[second].name == NULL)
    {
242
      if (gtk_css_number_value_can_parse (parser))
243 244 245
        {
          if (missing != &y)
            {
Matthias Clasen's avatar
Matthias Clasen committed
246 247
              if (!try)
                _gtk_css_parser_error (parser, "Invalid combination of values");
248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270
              _gtk_css_value_unref (y);
              return NULL;
            }
          y = _gtk_css_number_value_parse (parser,
                                           GTK_CSS_PARSE_PERCENT
                                           | GTK_CSS_PARSE_LENGTH);
          if (y == NULL)
            {
              _gtk_css_value_unref (x);
	      return NULL;
            }
        }
      else
        {
          second++;
          *missing = _gtk_css_number_value_new (50, GTK_CSS_PERCENT);
        }
    }
  else
    {
      if ((names[first].horizontal && !names[second].vertical) ||
          (!names[first].horizontal && !names[second].horizontal))
        {
Matthias Clasen's avatar
Matthias Clasen committed
271 272
          if (!try)
            _gtk_css_parser_error (parser, "Invalid combination of values");
273 274 275 276 277 278 279 280 281
          _gtk_css_value_unref (x);
          _gtk_css_value_unref (y);
          return NULL;
        }
    }

  return _gtk_css_position_value_new (x, y);
}

Matthias Clasen's avatar
Matthias Clasen committed
282 283 284 285 286 287 288 289 290 291 292 293
GtkCssValue *
_gtk_css_position_value_parse (GtkCssParser *parser)
{
  return position_value_parse (parser, FALSE);
}

GtkCssValue *
_gtk_css_position_value_try_parse (GtkCssParser *parser)
{
  return position_value_parse (parser, TRUE);
}

294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313
double
_gtk_css_position_value_get_x (const GtkCssValue *position,
                               double             one_hundred_percent)
{
  g_return_val_if_fail (position != NULL, 0.0);
  g_return_val_if_fail (position->class == &GTK_CSS_VALUE_POSITION, 0.0);

  return _gtk_css_number_value_get (position->x, one_hundred_percent);
}

double
_gtk_css_position_value_get_y (const GtkCssValue *position,
                               double             one_hundred_percent)
{
  g_return_val_if_fail (position != NULL, 0.0);
  g_return_val_if_fail (position->class == &GTK_CSS_VALUE_POSITION, 0.0);

  return _gtk_css_number_value_get (position->y, one_hundred_percent);
}