gtkcssrepeatvalue.c 10.2 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
/* 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 "gtkcssrepeatvalueprivate.h"

#include "gtkcssnumbervalueprivate.h"

struct _GtkCssValue {
  GTK_CSS_VALUE_BASE
  GtkCssRepeatStyle x;
  GtkCssRepeatStyle y;
};

static void
gtk_css_value_repeat_free (GtkCssValue *value)
{
  g_slice_free (GtkCssValue, value);
}

36
static GtkCssValue *
37 38 39
gtk_css_value_repeat_compute (GtkCssValue             *value,
                              guint                    property_id,
                              GtkStyleProviderPrivate *provider,
40
                              GtkCssStyle             *style,
41
                              GtkCssStyle             *parent_style)
42 43 44 45
{
  return _gtk_css_value_ref (value);
}

46 47 48 49 50 51 52 53 54 55 56
static gboolean
gtk_css_value_repeat_equal (const GtkCssValue *repeat1,
                            const GtkCssValue *repeat2)
{
  return repeat1->x == repeat2->x
      && repeat1->y == repeat2->y;
}

static GtkCssValue *
gtk_css_value_repeat_transition (GtkCssValue *start,
                                 GtkCssValue *end,
57
                                 guint        property_id,
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
                                 double       progress)
{
  return NULL;
}

static void
gtk_css_value_background_repeat_print (const GtkCssValue *repeat,
                                       GString           *string)
{
  static const char *names[] = {
    "no-repeat",
    "repeat",
    "round",
    "space"
  };

  if (repeat->x == repeat->y)
    {
      g_string_append (string, names[repeat->x]);
    }
  else if (repeat->x == GTK_CSS_REPEAT_STYLE_REPEAT &&
           repeat->y == GTK_CSS_REPEAT_STYLE_NO_REPEAT)
    {
      g_string_append (string, "repeat-x");
    }
  else if (repeat->x == GTK_CSS_REPEAT_STYLE_NO_REPEAT &&
           repeat->y == GTK_CSS_REPEAT_STYLE_REPEAT)
    {
      g_string_append (string, "repeat-y");
    }
  else
    {
      g_string_append (string, names[repeat->x]);
      g_string_append_c (string, ' ');
      g_string_append (string, names[repeat->y]);
    }
}

static void
gtk_css_value_border_repeat_print (const GtkCssValue *repeat,
                                   GString           *string)
{
  static const char *names[] = {
    "stretch",
    "repeat",
    "round",
    "space"
  };

  g_string_append (string, names[repeat->x]);
  if (repeat->x != repeat->y)
    {
      g_string_append_c (string, ' ');
      g_string_append (string, names[repeat->y]);
    }
}

static const GtkCssValueClass GTK_CSS_VALUE_BACKGROUND_REPEAT = {
  gtk_css_value_repeat_free,
117
  gtk_css_value_repeat_compute,
118 119 120 121 122 123 124
  gtk_css_value_repeat_equal,
  gtk_css_value_repeat_transition,
  gtk_css_value_background_repeat_print
};

static const GtkCssValueClass GTK_CSS_VALUE_BORDER_REPEAT = {
  gtk_css_value_repeat_free,
125
  gtk_css_value_repeat_compute,
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 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312
  gtk_css_value_repeat_equal,
  gtk_css_value_repeat_transition,
  gtk_css_value_border_repeat_print
};
/* BACKGROUND REPEAT */

static struct {
  const char *name;
  GtkCssValue values[4];
} background_repeat_values[4] = {
  { "no-repeat",
  { { &GTK_CSS_VALUE_BACKGROUND_REPEAT, 1, GTK_CSS_REPEAT_STYLE_NO_REPEAT, GTK_CSS_REPEAT_STYLE_NO_REPEAT },
    { &GTK_CSS_VALUE_BACKGROUND_REPEAT, 1, GTK_CSS_REPEAT_STYLE_NO_REPEAT, GTK_CSS_REPEAT_STYLE_REPEAT    },
    { &GTK_CSS_VALUE_BACKGROUND_REPEAT, 1, GTK_CSS_REPEAT_STYLE_NO_REPEAT, GTK_CSS_REPEAT_STYLE_ROUND     },
    { &GTK_CSS_VALUE_BACKGROUND_REPEAT, 1, GTK_CSS_REPEAT_STYLE_NO_REPEAT, GTK_CSS_REPEAT_STYLE_SPACE     }
  } },
  { "repeat",
  { { &GTK_CSS_VALUE_BACKGROUND_REPEAT, 1, GTK_CSS_REPEAT_STYLE_REPEAT,    GTK_CSS_REPEAT_STYLE_NO_REPEAT },
    { &GTK_CSS_VALUE_BACKGROUND_REPEAT, 1, GTK_CSS_REPEAT_STYLE_REPEAT,    GTK_CSS_REPEAT_STYLE_REPEAT    },
    { &GTK_CSS_VALUE_BACKGROUND_REPEAT, 1, GTK_CSS_REPEAT_STYLE_REPEAT,    GTK_CSS_REPEAT_STYLE_ROUND     },
    { &GTK_CSS_VALUE_BACKGROUND_REPEAT, 1, GTK_CSS_REPEAT_STYLE_REPEAT,    GTK_CSS_REPEAT_STYLE_SPACE     }
  } }, 
  { "round",
  { { &GTK_CSS_VALUE_BACKGROUND_REPEAT, 1, GTK_CSS_REPEAT_STYLE_ROUND,     GTK_CSS_REPEAT_STYLE_NO_REPEAT },
    { &GTK_CSS_VALUE_BACKGROUND_REPEAT, 1, GTK_CSS_REPEAT_STYLE_ROUND,     GTK_CSS_REPEAT_STYLE_REPEAT    },
    { &GTK_CSS_VALUE_BACKGROUND_REPEAT, 1, GTK_CSS_REPEAT_STYLE_ROUND,     GTK_CSS_REPEAT_STYLE_ROUND     },
    { &GTK_CSS_VALUE_BACKGROUND_REPEAT, 1, GTK_CSS_REPEAT_STYLE_ROUND,     GTK_CSS_REPEAT_STYLE_SPACE     }
  } }, 
  { "space",
  { { &GTK_CSS_VALUE_BACKGROUND_REPEAT, 1, GTK_CSS_REPEAT_STYLE_SPACE,     GTK_CSS_REPEAT_STYLE_NO_REPEAT },
    { &GTK_CSS_VALUE_BACKGROUND_REPEAT, 1, GTK_CSS_REPEAT_STYLE_SPACE,     GTK_CSS_REPEAT_STYLE_REPEAT    },
    { &GTK_CSS_VALUE_BACKGROUND_REPEAT, 1, GTK_CSS_REPEAT_STYLE_SPACE,     GTK_CSS_REPEAT_STYLE_ROUND     },
    { &GTK_CSS_VALUE_BACKGROUND_REPEAT, 1, GTK_CSS_REPEAT_STYLE_SPACE,     GTK_CSS_REPEAT_STYLE_SPACE     }
  } }
};

GtkCssValue *
_gtk_css_background_repeat_value_new (GtkCssRepeatStyle x,
                                      GtkCssRepeatStyle y)
{
  return _gtk_css_value_ref (&background_repeat_values[x].values[y]);
}

static gboolean
_gtk_css_background_repeat_style_try (GtkCssParser      *parser,
                                      GtkCssRepeatStyle *result)
{
  guint i;

  for (i = 0; i < G_N_ELEMENTS (background_repeat_values); i++)
    {
      if (_gtk_css_parser_try (parser, background_repeat_values[i].name, TRUE))
        {
          *result = i;
          return TRUE;
        }
    }

  return FALSE;
}

GtkCssValue *
_gtk_css_background_repeat_value_try_parse (GtkCssParser *parser)
{
  GtkCssRepeatStyle x, y;

  g_return_val_if_fail (parser != NULL, NULL);

  if (_gtk_css_parser_try (parser, "repeat-x", TRUE))
    return _gtk_css_background_repeat_value_new (GTK_CSS_REPEAT_STYLE_REPEAT, GTK_CSS_REPEAT_STYLE_NO_REPEAT);
  if (_gtk_css_parser_try (parser, "repeat-y", TRUE))
    return _gtk_css_background_repeat_value_new (GTK_CSS_REPEAT_STYLE_NO_REPEAT, GTK_CSS_REPEAT_STYLE_REPEAT);

  if (!_gtk_css_background_repeat_style_try (parser, &x))
    return NULL;

  if (!_gtk_css_background_repeat_style_try (parser, &y))
    y = x;

  return _gtk_css_background_repeat_value_new (x, y);
}

GtkCssRepeatStyle
_gtk_css_background_repeat_value_get_x (const GtkCssValue *repeat)
{
  g_return_val_if_fail (repeat->class == &GTK_CSS_VALUE_BACKGROUND_REPEAT, GTK_CSS_REPEAT_STYLE_NO_REPEAT);

  return repeat->x;
}

GtkCssRepeatStyle
_gtk_css_background_repeat_value_get_y (const GtkCssValue *repeat)
{
  g_return_val_if_fail (repeat->class == &GTK_CSS_VALUE_BACKGROUND_REPEAT, GTK_CSS_REPEAT_STYLE_NO_REPEAT);

  return repeat->y;
}

/* BORDER IMAGE REPEAT */

static struct {
  const char *name;
  GtkCssValue values[4];
} border_repeat_values[4] = {
  { "stretch",
  { { &GTK_CSS_VALUE_BORDER_REPEAT, 1, GTK_CSS_REPEAT_STYLE_STRETCH, GTK_CSS_REPEAT_STYLE_STRETCH },
    { &GTK_CSS_VALUE_BORDER_REPEAT, 1, GTK_CSS_REPEAT_STYLE_STRETCH, GTK_CSS_REPEAT_STYLE_REPEAT  },
    { &GTK_CSS_VALUE_BORDER_REPEAT, 1, GTK_CSS_REPEAT_STYLE_STRETCH, GTK_CSS_REPEAT_STYLE_ROUND   },
    { &GTK_CSS_VALUE_BORDER_REPEAT, 1, GTK_CSS_REPEAT_STYLE_STRETCH, GTK_CSS_REPEAT_STYLE_SPACE   }
  } },
  { "repeat",
  { { &GTK_CSS_VALUE_BORDER_REPEAT, 1, GTK_CSS_REPEAT_STYLE_REPEAT,  GTK_CSS_REPEAT_STYLE_STRETCH },
    { &GTK_CSS_VALUE_BORDER_REPEAT, 1, GTK_CSS_REPEAT_STYLE_REPEAT,  GTK_CSS_REPEAT_STYLE_REPEAT  },
    { &GTK_CSS_VALUE_BORDER_REPEAT, 1, GTK_CSS_REPEAT_STYLE_REPEAT,  GTK_CSS_REPEAT_STYLE_ROUND   },
    { &GTK_CSS_VALUE_BORDER_REPEAT, 1, GTK_CSS_REPEAT_STYLE_REPEAT,  GTK_CSS_REPEAT_STYLE_SPACE   }
  } }, 
  { "round",
  { { &GTK_CSS_VALUE_BORDER_REPEAT, 1, GTK_CSS_REPEAT_STYLE_ROUND,   GTK_CSS_REPEAT_STYLE_STRETCH },
    { &GTK_CSS_VALUE_BORDER_REPEAT, 1, GTK_CSS_REPEAT_STYLE_ROUND,   GTK_CSS_REPEAT_STYLE_REPEAT  },
    { &GTK_CSS_VALUE_BORDER_REPEAT, 1, GTK_CSS_REPEAT_STYLE_ROUND,   GTK_CSS_REPEAT_STYLE_ROUND   },
    { &GTK_CSS_VALUE_BORDER_REPEAT, 1, GTK_CSS_REPEAT_STYLE_ROUND,   GTK_CSS_REPEAT_STYLE_SPACE   }
  } }, 
  { "space",
  { { &GTK_CSS_VALUE_BORDER_REPEAT, 1, GTK_CSS_REPEAT_STYLE_SPACE,   GTK_CSS_REPEAT_STYLE_STRETCH },
    { &GTK_CSS_VALUE_BORDER_REPEAT, 1, GTK_CSS_REPEAT_STYLE_SPACE,   GTK_CSS_REPEAT_STYLE_REPEAT  },
    { &GTK_CSS_VALUE_BORDER_REPEAT, 1, GTK_CSS_REPEAT_STYLE_SPACE,   GTK_CSS_REPEAT_STYLE_ROUND   },
    { &GTK_CSS_VALUE_BORDER_REPEAT, 1, GTK_CSS_REPEAT_STYLE_SPACE,   GTK_CSS_REPEAT_STYLE_SPACE   }
  } }
};

GtkCssValue *
_gtk_css_border_repeat_value_new (GtkCssRepeatStyle x,
                                  GtkCssRepeatStyle y)
{
  return _gtk_css_value_ref (&border_repeat_values[x].values[y]);
}

static gboolean
_gtk_css_border_repeat_style_try (GtkCssParser      *parser,
                                  GtkCssRepeatStyle *result)
{
  guint i;

  for (i = 0; i < G_N_ELEMENTS (border_repeat_values); i++)
    {
      if (_gtk_css_parser_try (parser, border_repeat_values[i].name, TRUE))
        {
          *result = i;
          return TRUE;
        }
    }

  return FALSE;
}

GtkCssValue *
_gtk_css_border_repeat_value_try_parse (GtkCssParser *parser)
{
  GtkCssRepeatStyle x, y;

  g_return_val_if_fail (parser != NULL, NULL);

  if (!_gtk_css_border_repeat_style_try (parser, &x))
    return NULL;

  if (!_gtk_css_border_repeat_style_try (parser, &y))
    y = x;

  return _gtk_css_border_repeat_value_new (x, y);
}

GtkCssRepeatStyle
_gtk_css_border_repeat_value_get_x (const GtkCssValue *repeat)
{
  g_return_val_if_fail (repeat->class == &GTK_CSS_VALUE_BORDER_REPEAT, GTK_CSS_REPEAT_STYLE_STRETCH);

  return repeat->x;
}

GtkCssRepeatStyle
_gtk_css_border_repeat_value_get_y (const GtkCssValue *repeat)
{
  g_return_val_if_fail (repeat->class == &GTK_CSS_VALUE_BORDER_REPEAT, GTK_CSS_REPEAT_STYLE_STRETCH);

  return repeat->y;
}