summaryrefslogtreecommitdiff
path: root/gtk/gtkcsswin32sizevalue.c
blob: 134078b3a51a6b8077aa04a75acea50641197d9a (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
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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
/* 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"

#include "gtkwin32drawprivate.h"
#include "gtkwin32themeprivate.h"

typedef enum {
  GTK_WIN32_SIZE,
  GTK_WIN32_PART_WIDTH,
  GTK_WIN32_PART_HEIGHT,
  GTK_WIN32_PART_BORDER_TOP,
  GTK_WIN32_PART_BORDER_RIGHT,
  GTK_WIN32_PART_BORDER_BOTTOM,
  GTK_WIN32_PART_BORDER_LEFT
} GtkWin32SizeType;

static const char *css_value_names[] = {
  "-gtk-win32-size(",
  "-gtk-win32-part-width(",
  "-gtk-win32-part-height(",
  "-gtk-win32-part-border-top(",
  "-gtk-win32-part-border-right(",
  "-gtk-win32-part-border-bottom(",
  "-gtk-win32-part-border-left("
};

struct _GtkCssValue {
  GTK_CSS_VALUE_BASE
  double                 scale;         /* needed for calc() math */
  GtkWin32Theme         *theme;
  GtkWin32SizeType       type;
  union {
    struct {
      gint               id;
    } size;
    struct {
      gint               part;
      gint               state;
    } part;
  }                      val;
};

static GtkCssValue *    gtk_css_win32_size_value_new (double            scale,
                                                      GtkWin32Theme    *theme,
                                                      GtkWin32SizeType  type);

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

static int
gtk_css_value_win32_compute_size (const GtkCssValue *value)
{
  GtkBorder border;
  int size;

  switch (value->type)
    {
    case GTK_WIN32_SIZE:
      size = gtk_win32_theme_get_size (value->theme, value->val.size.id);
      break;

    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;

    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;

    default:
      g_assert_not_reached ();
      return 0;
    }

  return size;
}

static GtkCssValue *
gtk_css_value_win32_size_compute (GtkCssValue             *value,
                                  guint                    property_id,
                                  GtkStyleProviderPrivate *provider,
                                  GtkCssStyle             *style,
                                  GtkCssStyle             *parent_style)
{
  return _gtk_css_number_value_new (value->scale * gtk_css_value_win32_compute_size (value), GTK_CSS_PX);
}

static gboolean
gtk_css_value_win32_size_equal (const GtkCssValue *value1,
                                const GtkCssValue *value2)
{
  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;
    }
}

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);
    }
  g_string_append (string, css_value_names[value->type]);
  gtk_win32_theme_print (value->theme, string);

  switch (value->type)
    {
    case GTK_WIN32_SIZE:
      {
        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);
      }
      break;

    case GTK_WIN32_PART_WIDTH:
    case GTK_WIN32_PART_HEIGHT:
    case GTK_WIN32_PART_BORDER_TOP:
    case GTK_WIN32_PART_BORDER_RIGHT:
    case GTK_WIN32_PART_BORDER_BOTTOM:
    case GTK_WIN32_PART_BORDER_LEFT:
      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, ")");
}

static double
gtk_css_value_win32_size_get (const GtkCssValue *value,
                              double             one_hundred_percent)
{
  return value->scale * gtk_css_value_win32_compute_size (value);
}

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)
{
  GtkCssValue *result;

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

  return result;
}

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

  if (!gtk_css_value_win32_size_equal (value1, value2))
    return NULL;

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

  return result;
}

static gint
gtk_css_value_win32_size_get_calc_term_order (const GtkCssValue *value)
{
  return 2000 + 100 * value->type;
}

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 *
gtk_css_win32_size_value_new (double            scale,
                              GtkWin32Theme    *theme,
                              GtkWin32SizeType  type)
{
  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);
  result->type = type;

  return result;
}

static GtkCssValue *
gtk_css_win32_size_value_parse_size (GtkCssValue *value,
                                     GtkCssParser *parser)
{
  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))
    {
      _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;
}

GtkCssValue *
gtk_css_win32_size_value_parse (GtkCssParser           *parser,
                                GtkCssNumberParseFlags  flags)
{
  GtkWin32Theme *theme;
  GtkCssValue *result;
  guint type;

  for (type = 0; type < G_N_ELEMENTS(css_value_names); type++)
    {
      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");
      return NULL;
    }

  theme = gtk_win32_theme_parse (parser);
  if (theme == NULL)
    return NULL;

  result = gtk_css_win32_size_value_new (1.0, theme, type);
  gtk_win32_theme_unref (theme);

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

  switch (result->type)
    {
    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:
    case GTK_WIN32_PART_BORDER_TOP:
    case GTK_WIN32_PART_BORDER_RIGHT:
    case GTK_WIN32_PART_BORDER_BOTTOM:
    case GTK_WIN32_PART_BORDER_LEFT:
      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;
    }

  if (result == NULL)
    return NULL;

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

  return result;
}