gtkcssmatcher.c 14.9 KB
Newer Older
Benjamin Otte's avatar
Benjamin Otte committed
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) 2012 Benjamin Otte <otte@gnome.org>
 *
 * 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 "gtkcssmatcherprivate.h"

22
#include "gtkcssnodedeclarationprivate.h"
23
#include "gtkcssnodeprivate.h"
Benjamin Otte's avatar
Benjamin Otte committed
24 25
#include "gtkwidgetpath.h"

26 27
/* GTK_CSS_MATCHER_WIDGET_PATH */

Benjamin Otte's avatar
Benjamin Otte committed
28 29 30
static gboolean
gtk_css_matcher_widget_path_get_parent (GtkCssMatcher       *matcher,
                                        const GtkCssMatcher *child)
Benjamin Otte's avatar
Benjamin Otte committed
31
{
32
  if (child->path.index == 0)
Benjamin Otte's avatar
Benjamin Otte committed
33 34
    return FALSE;

35
  matcher->path.klass = child->path.klass;
36
  matcher->path.decl = NULL;
37 38 39
  matcher->path.path = child->path.path;
  matcher->path.index = child->path.index - 1;
  matcher->path.sibling_index = gtk_widget_path_iter_get_sibling_index (matcher->path.path, matcher->path.index);
Benjamin Otte's avatar
Benjamin Otte committed
40 41 42 43

  return TRUE;
}

Benjamin Otte's avatar
Benjamin Otte committed
44 45 46
static gboolean
gtk_css_matcher_widget_path_get_previous (GtkCssMatcher       *matcher,
                                          const GtkCssMatcher *next)
Benjamin Otte's avatar
Benjamin Otte committed
47
{
48
  if (next->path.sibling_index == 0)
Benjamin Otte's avatar
Benjamin Otte committed
49 50
    return FALSE;

51
  matcher->path.klass = next->path.klass;
52
  matcher->path.decl = NULL;
53 54 55
  matcher->path.path = next->path.path;
  matcher->path.index = next->path.index;
  matcher->path.sibling_index = next->path.sibling_index - 1;
Benjamin Otte's avatar
Benjamin Otte committed
56 57 58 59

  return TRUE;
}

Benjamin Otte's avatar
Benjamin Otte committed
60 61
static GtkStateFlags
gtk_css_matcher_widget_path_get_state (const GtkCssMatcher *matcher)
Benjamin Otte's avatar
Benjamin Otte committed
62
{
63 64
  const GtkWidgetPath *siblings;
  
65 66 67
  if (matcher->path.decl)
    return gtk_css_node_declaration_get_state (matcher->path.decl);

68 69 70 71 72
  siblings = gtk_widget_path_iter_get_siblings (matcher->path.path, matcher->path.index);
  if (siblings && matcher->path.sibling_index != gtk_widget_path_iter_get_sibling_index (matcher->path.path, matcher->path.index))
    return gtk_widget_path_iter_get_state (siblings, matcher->path.sibling_index);
  else
    return gtk_widget_path_iter_get_state (matcher->path.path, matcher->path.index);
Benjamin Otte's avatar
Benjamin Otte committed
73 74
}

Benjamin Otte's avatar
Benjamin Otte committed
75
static gboolean
76
gtk_css_matcher_widget_path_has_name (const GtkCssMatcher     *matcher,
77
                                      /*interned*/ const char *name)
Benjamin Otte's avatar
Benjamin Otte committed
78 79
{
  const GtkWidgetPath *siblings;
80

81 82
  siblings = gtk_widget_path_iter_get_siblings (matcher->path.path, matcher->path.index);
  if (siblings && matcher->path.sibling_index != gtk_widget_path_iter_get_sibling_index (matcher->path.path, matcher->path.index))
83 84 85 86
    {
      const char *path_name = gtk_widget_path_iter_get_object_name (siblings, matcher->path.sibling_index);

      if (path_name == NULL)
87
        path_name = g_type_name (gtk_widget_path_iter_get_object_type (siblings, matcher->path.sibling_index));
88 89 90

      return path_name == name;
    }
Benjamin Otte's avatar
Benjamin Otte committed
91
  else
92 93 94 95
    {
      const char *path_name = gtk_widget_path_iter_get_object_name (matcher->path.path, matcher->path.index);

      if (path_name == NULL)
96
        path_name = g_type_name (gtk_widget_path_iter_get_object_type (matcher->path.path, matcher->path.index));
97 98 99

      return path_name == name;
    }
Benjamin Otte's avatar
Benjamin Otte committed
100 101
}

Benjamin Otte's avatar
Benjamin Otte committed
102 103
static gboolean
gtk_css_matcher_widget_path_has_class (const GtkCssMatcher *matcher,
104
                                       GQuark               class_name)
Benjamin Otte's avatar
Benjamin Otte committed
105 106 107
{
  const GtkWidgetPath *siblings;
  
108 109 110 111
  if (matcher->path.decl &&
      gtk_css_node_declaration_has_class (matcher->path.decl, class_name))
    return TRUE;

112 113
  siblings = gtk_widget_path_iter_get_siblings (matcher->path.path, matcher->path.index);
  if (siblings && matcher->path.sibling_index != gtk_widget_path_iter_get_sibling_index (matcher->path.path, matcher->path.index))
114
    return gtk_widget_path_iter_has_qclass (siblings, matcher->path.sibling_index, class_name);
Benjamin Otte's avatar
Benjamin Otte committed
115
  else
116
    return gtk_widget_path_iter_has_qclass (matcher->path.path, matcher->path.index, class_name);
Benjamin Otte's avatar
Benjamin Otte committed
117 118
}

Benjamin Otte's avatar
Benjamin Otte committed
119 120 121
static gboolean
gtk_css_matcher_widget_path_has_id (const GtkCssMatcher *matcher,
                                    const char          *id)
Benjamin Otte's avatar
Benjamin Otte committed
122 123 124
{
  const GtkWidgetPath *siblings;
  
125 126 127
  siblings = gtk_widget_path_iter_get_siblings (matcher->path.path, matcher->path.index);
  if (siblings && matcher->path.sibling_index != gtk_widget_path_iter_get_sibling_index (matcher->path.path, matcher->path.index))
    return gtk_widget_path_iter_has_name (siblings, matcher->path.sibling_index, id);
Benjamin Otte's avatar
Benjamin Otte committed
128
  else
129
    return gtk_widget_path_iter_has_name (matcher->path.path, matcher->path.index, id);
Benjamin Otte's avatar
Benjamin Otte committed
130 131
}

132 133 134 135 136
static gboolean
gtk_css_matcher_widget_path_has_position (const GtkCssMatcher *matcher,
                                          gboolean             forward,
                                          int                  a,
                                          int                  b)
Benjamin Otte's avatar
Benjamin Otte committed
137 138
{
  const GtkWidgetPath *siblings;
139
  int x;
Benjamin Otte's avatar
Benjamin Otte committed
140

141
  siblings = gtk_widget_path_iter_get_siblings (matcher->path.path, matcher->path.index);
Benjamin Otte's avatar
Benjamin Otte committed
142
  if (!siblings)
143 144 145 146 147 148 149 150 151 152 153 154 155 156
    return FALSE;

  if (forward)
    x = matcher->path.sibling_index + 1;
  else
    x = gtk_widget_path_length (siblings) - matcher->path.sibling_index;

  x -= b;

  if (a == 0)
    return x == 0;

  if (x % a)
    return FALSE;
Benjamin Otte's avatar
Benjamin Otte committed
157

158
  return x / a >= 0;
Benjamin Otte's avatar
Benjamin Otte committed
159 160
}

Benjamin Otte's avatar
Benjamin Otte committed
161 162 163 164
static const GtkCssMatcherClass GTK_CSS_MATCHER_WIDGET_PATH = {
  gtk_css_matcher_widget_path_get_parent,
  gtk_css_matcher_widget_path_get_previous,
  gtk_css_matcher_widget_path_get_state,
165
  gtk_css_matcher_widget_path_has_name,
Benjamin Otte's avatar
Benjamin Otte committed
166 167
  gtk_css_matcher_widget_path_has_class,
  gtk_css_matcher_widget_path_has_id,
168
  gtk_css_matcher_widget_path_has_position,
169
  FALSE
Benjamin Otte's avatar
Benjamin Otte committed
170 171
};

172
gboolean
173 174 175
_gtk_css_matcher_init (GtkCssMatcher               *matcher,
                       const GtkWidgetPath         *path,
                       const GtkCssNodeDeclaration *decl)
Benjamin Otte's avatar
Benjamin Otte committed
176
{
177 178 179
  if (gtk_widget_path_length (path) == 0)
    return FALSE;

180
  matcher->path.klass = &GTK_CSS_MATCHER_WIDGET_PATH;
181
  matcher->path.decl = decl;
182 183 184
  matcher->path.path = path;
  matcher->path.index = gtk_widget_path_length (path) - 1;
  matcher->path.sibling_index = gtk_widget_path_iter_get_sibling_index (path, matcher->path.index);
185 186

  return TRUE;
Benjamin Otte's avatar
Benjamin Otte committed
187 188
}

189 190 191 192 193 194 195 196 197 198 199 200
/* GTK_CSS_MATCHER_NODE */

static gboolean
gtk_css_matcher_node_get_parent (GtkCssMatcher       *matcher,
                                 const GtkCssMatcher *child)
{
  GtkCssNode *node;
  
  node = gtk_css_node_get_parent (child->node.node);
  if (node == NULL)
    return FALSE;

201
  return gtk_css_node_init_matcher (node, matcher);
202 203
}

204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
static GtkCssNode *
get_previous_visible_sibling (GtkCssNode *node)
{
  do {
    node = gtk_css_node_get_previous_sibling (node);
  } while (node && !gtk_css_node_get_visible (node));

  return node;
}

static GtkCssNode *
get_next_visible_sibling (GtkCssNode *node)
{
  do {
    node = gtk_css_node_get_next_sibling (node);
  } while (node && !gtk_css_node_get_visible (node));

  return node;
}

224 225 226 227 228 229
static gboolean
gtk_css_matcher_node_get_previous (GtkCssMatcher       *matcher,
                                   const GtkCssMatcher *next)
{
  GtkCssNode *node;
  
230
  node = get_previous_visible_sibling (next->node.node);
231 232 233
  if (node == NULL)
    return FALSE;

234
  return gtk_css_node_init_matcher (node, matcher);
235 236 237 238 239 240 241 242 243
}

static GtkStateFlags
gtk_css_matcher_node_get_state (const GtkCssMatcher *matcher)
{
  return gtk_css_node_get_state (matcher->node.node);
}

static gboolean
244
gtk_css_matcher_node_has_name (const GtkCssMatcher     *matcher,
245
                               /*interned*/ const char *name)
246
{
247
  return gtk_css_node_get_name (matcher->node.node) == name;
248 249 250 251 252 253 254 255 256 257 258 259 260
}

static gboolean
gtk_css_matcher_node_has_class (const GtkCssMatcher *matcher,
                                GQuark               class_name)
{
  return gtk_css_node_has_class (matcher->node.node, class_name);
}

static gboolean
gtk_css_matcher_node_has_id (const GtkCssMatcher *matcher,
                             const char          *id)
{
261 262
  /* assume all callers pass an interned string */
  return gtk_css_node_get_id (matcher->node.node) == id;
263 264 265 266
}

static gboolean
gtk_css_matcher_node_nth_child (GtkCssNode *node,
267
                                GtkCssNode *(* prev_node_func) (GtkCssNode *),
268 269 270
                                int         a,
                                int         b)
{
271
  int pos, x;
272

273 274 275 276 277 278 279 280 281 282 283 284
  /* special-case the common "first-child" and "last-child" */
  if (a == 0)
    {
      while (b > 0 && node != NULL)
        {
          b--;
          node = prev_node_func (node);
        }

      return b == 0 && node == NULL;
    }

285 286 287 288 289 290 291
  /* count nodes */
  for (pos = 0; node != NULL; pos++)
    node = prev_node_func (node);

  /* solve pos = a * X + b
   * and return TRUE if X is integer >= 0 */
  x = pos - b;
292

293 294
  if (x % a)
    return FALSE;
295

296
  return x / a >= 0;
297 298 299 300 301 302 303 304
}

static gboolean
gtk_css_matcher_node_has_position (const GtkCssMatcher *matcher,
                                   gboolean             forward,
                                   int                  a,
                                   int                  b)
{
305 306 307 308
  return gtk_css_matcher_node_nth_child (matcher->node.node,
                                         forward ? get_previous_visible_sibling 
                                                 : get_next_visible_sibling,
                                         a, b);
309 310 311 312 313 314
}

static const GtkCssMatcherClass GTK_CSS_MATCHER_NODE = {
  gtk_css_matcher_node_get_parent,
  gtk_css_matcher_node_get_previous,
  gtk_css_matcher_node_get_state,
315
  gtk_css_matcher_node_has_name,
316 317 318 319 320 321 322 323 324 325 326 327 328 329
  gtk_css_matcher_node_has_class,
  gtk_css_matcher_node_has_id,
  gtk_css_matcher_node_has_position,
  FALSE
};

void
_gtk_css_matcher_node_init (GtkCssMatcher *matcher,
                            GtkCssNode    *node)
{
  matcher->node.klass = &GTK_CSS_MATCHER_NODE;
  matcher->node.node = node;
}

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
/* GTK_CSS_MATCHER_WIDGET_ANY */

static gboolean
gtk_css_matcher_any_get_parent (GtkCssMatcher       *matcher,
                                const GtkCssMatcher *child)
{
  _gtk_css_matcher_any_init (matcher);

  return TRUE;
}

static gboolean
gtk_css_matcher_any_get_previous (GtkCssMatcher       *matcher,
                                  const GtkCssMatcher *next)
{
  _gtk_css_matcher_any_init (matcher);

  return TRUE;
}

static GtkStateFlags
gtk_css_matcher_any_get_state (const GtkCssMatcher *matcher)
{
  /* XXX: This gets tricky when we implement :not() */

  return GTK_STATE_FLAG_ACTIVE | GTK_STATE_FLAG_PRELIGHT | GTK_STATE_FLAG_SELECTED
    | GTK_STATE_FLAG_INSENSITIVE | GTK_STATE_FLAG_INCONSISTENT
357 358
    | GTK_STATE_FLAG_FOCUSED | GTK_STATE_FLAG_BACKDROP | GTK_STATE_FLAG_LINK
    | GTK_STATE_FLAG_VISITED;
359 360 361
}

static gboolean
362
gtk_css_matcher_any_has_name (const GtkCssMatcher     *matcher,
363
                              /*interned*/ const char *name)
364 365 366 367 368 369
{
  return TRUE;
}

static gboolean
gtk_css_matcher_any_has_class (const GtkCssMatcher *matcher,
370
                               GQuark               class_name)
371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394
{
  return TRUE;
}

static gboolean
gtk_css_matcher_any_has_id (const GtkCssMatcher *matcher,
                                    const char          *id)
{
  return TRUE;
}

static gboolean
gtk_css_matcher_any_has_position (const GtkCssMatcher *matcher,
                                  gboolean             forward,
                                  int                  a,
                                  int                  b)
{
  return TRUE;
}

static const GtkCssMatcherClass GTK_CSS_MATCHER_ANY = {
  gtk_css_matcher_any_get_parent,
  gtk_css_matcher_any_get_previous,
  gtk_css_matcher_any_get_state,
395
  gtk_css_matcher_any_has_name,
396 397 398
  gtk_css_matcher_any_has_class,
  gtk_css_matcher_any_has_id,
  gtk_css_matcher_any_has_position,
399
  TRUE
400 401 402 403 404 405 406 407
};

void
_gtk_css_matcher_any_init (GtkCssMatcher *matcher)
{
  matcher->klass = &GTK_CSS_MATCHER_ANY;
}

408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437
/* GTK_CSS_MATCHER_WIDGET_SUPERSET */

static gboolean
gtk_css_matcher_superset_get_parent (GtkCssMatcher       *matcher,
                                     const GtkCssMatcher *child)
{
  _gtk_css_matcher_any_init (matcher);

  return TRUE;
}

static gboolean
gtk_css_matcher_superset_get_previous (GtkCssMatcher       *matcher,
                                       const GtkCssMatcher *next)
{
  _gtk_css_matcher_any_init (matcher);

  return TRUE;
}

static GtkStateFlags
gtk_css_matcher_superset_get_state (const GtkCssMatcher *matcher)
{
  /* XXX: This gets tricky when we implement :not() */

  if (matcher->superset.relevant & GTK_CSS_CHANGE_STATE)
    return _gtk_css_matcher_get_state (matcher->superset.subset);
  else
    return GTK_STATE_FLAG_ACTIVE | GTK_STATE_FLAG_PRELIGHT | GTK_STATE_FLAG_SELECTED
      | GTK_STATE_FLAG_INSENSITIVE | GTK_STATE_FLAG_INCONSISTENT
438 439
      | GTK_STATE_FLAG_FOCUSED | GTK_STATE_FLAG_BACKDROP | GTK_STATE_FLAG_LINK
      | GTK_STATE_FLAG_VISITED;
440 441 442
}

static gboolean
443
gtk_css_matcher_superset_has_name (const GtkCssMatcher     *matcher,
444
                                   /*interned*/ const char *name)
445 446
{
  if (matcher->superset.relevant & GTK_CSS_CHANGE_NAME)
447
    return _gtk_css_matcher_has_name (matcher->superset.subset, name);
448 449 450 451 452 453
  else
    return TRUE;
}

static gboolean
gtk_css_matcher_superset_has_class (const GtkCssMatcher *matcher,
454
                                    GQuark               class_name)
455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487
{
  if (matcher->superset.relevant & GTK_CSS_CHANGE_CLASS)
    return _gtk_css_matcher_has_class (matcher->superset.subset, class_name);
  else
    return TRUE;
}

static gboolean
gtk_css_matcher_superset_has_id (const GtkCssMatcher *matcher,
                                 const char          *id)
{
  if (matcher->superset.relevant & GTK_CSS_CHANGE_NAME)
    return _gtk_css_matcher_has_id (matcher->superset.subset, id);
  else
    return TRUE;
}

static gboolean
gtk_css_matcher_superset_has_position (const GtkCssMatcher *matcher,
                                       gboolean             forward,
                                       int                  a,
                                       int                  b)
{
  if (matcher->superset.relevant & GTK_CSS_CHANGE_POSITION)
    return _gtk_css_matcher_has_position (matcher->superset.subset, forward, a, b);
  else
    return TRUE;
}

static const GtkCssMatcherClass GTK_CSS_MATCHER_SUPERSET = {
  gtk_css_matcher_superset_get_parent,
  gtk_css_matcher_superset_get_previous,
  gtk_css_matcher_superset_get_state,
488
  gtk_css_matcher_superset_has_name,
489 490 491
  gtk_css_matcher_superset_has_class,
  gtk_css_matcher_superset_has_id,
  gtk_css_matcher_superset_has_position,
492
  FALSE
493 494 495 496 497 498 499 500 501 502 503 504 505 506 507
};

void
_gtk_css_matcher_superset_init (GtkCssMatcher       *matcher,
                                const GtkCssMatcher *subset,
                                GtkCssChange         relevant)
{
  g_return_if_fail (subset != NULL);
  g_return_if_fail ((relevant & ~(GTK_CSS_CHANGE_CLASS | GTK_CSS_CHANGE_NAME | GTK_CSS_CHANGE_POSITION | GTK_CSS_CHANGE_STATE)) == 0);

  matcher->superset.klass = &GTK_CSS_MATCHER_SUPERSET;
  matcher->superset.subset = subset;
  matcher->superset.relevant = relevant;
}