gtkcsswin32sizevalue.c 10.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/* 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 "gtkcsswin32sizevalueprivate.h"

22
#include "gtkwin32drawprivate.h"
23 24
#include "gtkwin32themeprivate.h"

25 26 27
typedef enum {
  GTK_WIN32_SIZE,
  GTK_WIN32_PART_WIDTH,
28 29 30 31 32
  GTK_WIN32_PART_HEIGHT,
  GTK_WIN32_PART_BORDER_TOP,
  GTK_WIN32_PART_BORDER_RIGHT,
  GTK_WIN32_PART_BORDER_BOTTOM,
  GTK_WIN32_PART_BORDER_LEFT
33 34 35 36 37
} GtkWin32SizeType;

static const char *css_value_names[] = {
  "-gtk-win32-size(",
  "-gtk-win32-part-width(",
38 39 40 41 42
  "-gtk-win32-part-height(",
  "-gtk-win32-part-border-top(",
  "-gtk-win32-part-border-right(",
  "-gtk-win32-part-border-bottom(",
  "-gtk-win32-part-border-left("
43 44
};

45 46
struct _GtkCssValue {
  GTK_CSS_VALUE_BASE
47 48 49 50 51 52 53 54 55 56 57 58
  double                 scale;         /* needed for calc() math */
  GtkWin32Theme         *theme;
  GtkWin32SizeType       type;
  union {
    struct {
      gint               id;
    } size;
    struct {
      gint               part;
      gint               state;
    } part;
  }                      val;
59 60
};

61 62 63
static GtkCssValue *    gtk_css_win32_size_value_new (double            scale,
                                                      GtkWin32Theme    *theme,
                                                      GtkWin32SizeType  type);
64 65 66 67 68 69 70 71

static void
gtk_css_value_win32_size_free (GtkCssValue *value)
{
  gtk_win32_theme_unref (value->theme);
  g_slice_free (GtkCssValue, value);
}

72 73 74
static int
gtk_css_value_win32_compute_size (const GtkCssValue *value)
{
75
  GtkBorder border;
76 77 78 79 80 81
  int size;

  switch (value->type)
    {
    case GTK_WIN32_SIZE:
      size = gtk_win32_theme_get_size (value->theme, value->val.size.id);
82
      break;
83 84 85 86 87 88 89 90 91

    case GTK_WIN32_PART_WIDTH:
      gtk_win32_theme_get_part_size (value->theme, value->val.part.part, value->val.part.state, &size, NULL);
      break;

    case GTK_WIN32_PART_HEIGHT:
      gtk_win32_theme_get_part_size (value->theme, value->val.part.part, value->val.part.state, NULL, &size);
      break;

92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
    case GTK_WIN32_PART_BORDER_TOP:
      gtk_win32_theme_get_part_border (value->theme, value->val.part.part, value->val.part.state, &border);
      size = border.top;
      break;

    case GTK_WIN32_PART_BORDER_RIGHT:
      gtk_win32_theme_get_part_border (value->theme, value->val.part.part, value->val.part.state, &border);
      size = border.right;
      break;

    case GTK_WIN32_PART_BORDER_BOTTOM:
      gtk_win32_theme_get_part_border (value->theme, value->val.part.part, value->val.part.state, &border);
      size = border.bottom;
      break;

    case GTK_WIN32_PART_BORDER_LEFT:
      gtk_win32_theme_get_part_border (value->theme, value->val.part.part, value->val.part.state, &border);
      size = border.left;
      break;

112 113 114 115 116 117 118 119
    default:
      g_assert_not_reached ();
      return 0;
    }

  return size;
}

120 121 122 123 124 125 126
static GtkCssValue *
gtk_css_value_win32_size_compute (GtkCssValue             *value,
                                  guint                    property_id,
                                  GtkStyleProviderPrivate *provider,
                                  GtkCssStyle             *style,
                                  GtkCssStyle             *parent_style)
{
127
  return _gtk_css_number_value_new (value->scale * gtk_css_value_win32_compute_size (value), GTK_CSS_PX);
128 129 130 131 132 133
}

static gboolean
gtk_css_value_win32_size_equal (const GtkCssValue *value1,
                                const GtkCssValue *value2)
{
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
  if (value1->type != value2->type ||
      !gtk_win32_theme_equal (value1->theme, value2->theme) )
    return FALSE;

  switch (value1->type)
    {
    case GTK_WIN32_SIZE:
      return value1->val.size.id == value2->val.size.id;

    case GTK_WIN32_PART_WIDTH:
    case GTK_WIN32_PART_HEIGHT:
      return value1->val.part.part == value2->val.part.part
          && value1->val.part.state == value2->val.part.state;

    default:
      g_assert_not_reached ();
      return FALSE;
    }
152 153 154 155 156 157 158 159 160 161
}

static void
gtk_css_value_win32_size_print (const GtkCssValue *value,
                                GString           *string)
{
  if (value->scale != 1.0)
    {
      g_string_append_printf (string, "%g * ", value->scale);
    }
162
  g_string_append (string, css_value_names[value->type]);
163
  gtk_win32_theme_print (value->theme, string);
164 165 166 167

  switch (value->type)
    {
    case GTK_WIN32_SIZE:
168 169 170 171 172 173 174
      {
        const char *name = gtk_win32_get_sys_metric_name_for_id (value->val.size.id);
        if (name)
          g_string_append (string, name);
        else
          g_string_append_printf (string, ", %d", value->val.size.id);
      }
175 176 177 178
      break;

    case GTK_WIN32_PART_WIDTH:
    case GTK_WIN32_PART_HEIGHT:
179 180 181 182
    case GTK_WIN32_PART_BORDER_TOP:
    case GTK_WIN32_PART_BORDER_RIGHT:
    case GTK_WIN32_PART_BORDER_BOTTOM:
    case GTK_WIN32_PART_BORDER_LEFT:
183 184 185 186 187 188 189 190 191
      g_string_append_printf (string, ", %d, %d", value->val.part.part, value->val.part.state);
      break;

    default:
      g_assert_not_reached ();
      break;
    }

  g_string_append (string, ")");
192 193 194 195 196 197
}

static double
gtk_css_value_win32_size_get (const GtkCssValue *value,
                              double             one_hundred_percent)
{
198
  return value->scale * gtk_css_value_win32_compute_size (value);
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
}

static GtkCssDimension
gtk_css_value_win32_size_get_dimension (const GtkCssValue *value)
{
  return GTK_CSS_DIMENSION_LENGTH;
}

static gboolean
gtk_css_value_win32_size_has_percent (const GtkCssValue *value)
{
  return FALSE;
}

static GtkCssValue *
gtk_css_value_win32_size_multiply (const GtkCssValue *value,
                                   double             factor)
{
217 218 219 220 221 222
  GtkCssValue *result;

  result = gtk_css_win32_size_value_new (value->scale * factor, value->theme, value->type);
  result->val = value->val;

  return result;
223 224 225 226 227 228
}

static GtkCssValue *
gtk_css_value_win32_size_try_add (const GtkCssValue *value1,
                                  const GtkCssValue *value2)
{
229 230
  GtkCssValue *result;

231 232 233
  if (!gtk_css_value_win32_size_equal (value1, value2))
    return NULL;

234 235 236 237
  result = gtk_css_win32_size_value_new (value1->scale + value2->scale, value1->theme, value1->type);
  result->val = value1->val;

  return result;
238 239 240 241 242
}

static gint
gtk_css_value_win32_size_get_calc_term_order (const GtkCssValue *value)
{
243
  return 2000 + 100 * value->type;
244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262
}

static const GtkCssNumberValueClass GTK_CSS_VALUE_WIN32_SIZE = {
  {
    gtk_css_value_win32_size_free,
    gtk_css_value_win32_size_compute,
    gtk_css_value_win32_size_equal,
    gtk_css_number_value_transition,
    gtk_css_value_win32_size_print
  },
  gtk_css_value_win32_size_get,
  gtk_css_value_win32_size_get_dimension,
  gtk_css_value_win32_size_has_percent,
  gtk_css_value_win32_size_multiply,
  gtk_css_value_win32_size_try_add,
  gtk_css_value_win32_size_get_calc_term_order
};

static GtkCssValue *
263 264 265
gtk_css_win32_size_value_new (double            scale,
                              GtkWin32Theme    *theme,
                              GtkWin32SizeType  type)
266 267 268 269 270 271
{
  GtkCssValue *result;

  result = _gtk_css_value_new (GtkCssValue, &GTK_CSS_VALUE_WIN32_SIZE.value_class);
  result->scale = scale;
  result->theme = gtk_win32_theme_ref (theme);
272
  result->type = type;
273 274 275 276

  return result;
}

277 278 279 280
static GtkCssValue *
gtk_css_win32_size_value_parse_size (GtkCssValue *value,
                                     GtkCssParser *parser)
{
281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296
  char *name;

  name = _gtk_css_parser_try_ident (parser, TRUE);
  if (name)
    {
      value->val.size.id = gtk_win32_get_sys_metric_id_for_name (name);
      if (value->val.size.id == -1)
        {
          _gtk_css_parser_error (parser, "'%s' is not a name for a win32 metric.", name);
          _gtk_css_value_unref (value);
          g_free (name);
          return NULL;
        }
      g_free (name);
    }
  else if (!_gtk_css_parser_try_int (parser, &value->val.size.id))
297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333
    {
      _gtk_css_value_unref (value);
      _gtk_css_parser_error (parser, "Expected an integer ID");
      return NULL;
    }

  return value;
}

static GtkCssValue *
gtk_css_win32_size_value_parse_part_size (GtkCssValue *value,
                                          GtkCssParser *parser)
{
  if (!_gtk_css_parser_try_int (parser, &value->val.part.part))
    {
      _gtk_css_value_unref (value);
      _gtk_css_parser_error (parser, "Expected an integer part ID");
      return NULL;
    }

  if (! _gtk_css_parser_try (parser, ",", TRUE))
    {
      _gtk_css_value_unref (value);
      _gtk_css_parser_error (parser, "Expected ','");
      return NULL;
    }

  if (!_gtk_css_parser_try_int (parser, &value->val.part.state))
    {
      _gtk_css_value_unref (value);
      _gtk_css_parser_error (parser, "Expected an integer state ID");
      return NULL;
    }

  return value;
}

334 335 336 337 338 339
GtkCssValue *
gtk_css_win32_size_value_parse (GtkCssParser           *parser,
                                GtkCssNumberParseFlags  flags)
{
  GtkWin32Theme *theme;
  GtkCssValue *result;
340
  guint type;
341

342
  for (type = 0; type < G_N_ELEMENTS(css_value_names); type++)
343
    {
344 345 346 347 348 349 350
      if (_gtk_css_parser_try (parser, css_value_names[type], TRUE))
        break;
    }

  if (type >= G_N_ELEMENTS(css_value_names))
    {
      _gtk_css_parser_error (parser, "Not a win32 size value");
351 352 353
      return NULL;
    }

354 355 356
  theme = gtk_win32_theme_parse (parser);
  if (theme == NULL)
    return NULL;
357

358 359 360
  result = gtk_css_win32_size_value_new (1.0, theme, type);
  gtk_win32_theme_unref (theme);

361 362
  if (! _gtk_css_parser_try (parser, ",", TRUE))
    {
363
      _gtk_css_value_unref (result);
364 365 366 367
      _gtk_css_parser_error (parser, "Expected ','");
      return NULL;
    }

368
  switch (result->type)
369
    {
370 371 372 373 374 375
    case GTK_WIN32_SIZE:
      result = gtk_css_win32_size_value_parse_size (result, parser);
      break;

    case GTK_WIN32_PART_WIDTH:
    case GTK_WIN32_PART_HEIGHT:
376 377 378 379
    case GTK_WIN32_PART_BORDER_TOP:
    case GTK_WIN32_PART_BORDER_RIGHT:
    case GTK_WIN32_PART_BORDER_BOTTOM:
    case GTK_WIN32_PART_BORDER_LEFT:
380 381 382 383 384 385 386 387
      result = gtk_css_win32_size_value_parse_part_size (result, parser);
      break;

    default:
      g_assert_not_reached ();
      _gtk_css_value_unref (result);
      result = NULL;
      break;
388 389
    }

390 391 392
  if (result == NULL)
    return NULL;

393 394
  if (!_gtk_css_parser_try (parser, ")", TRUE))
    {
395
      _gtk_css_value_unref (result);
396 397 398 399 400 401
      _gtk_css_parser_error (parser, "Expected ')'");
      return NULL;
    }

  return result;
}