gtkcssmatcher.c 13.1 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 22 23
/* 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"

#include "gtkwidgetpath.h"

24 25
/* GTK_CSS_MATCHER_WIDGET_PATH */

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

33 34 35 36 37
  matcher->path.klass = child->path.klass;
  matcher->path.path = child->path.path;
  matcher->path.state_flags = 0;
  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
38 39 40 41

  return TRUE;
}

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

49 50 51 52 53
  matcher->path.klass = next->path.klass;
  matcher->path.path = next->path.path;
  matcher->path.state_flags = 0;
  matcher->path.index = next->path.index;
  matcher->path.sibling_index = next->path.sibling_index - 1;
Benjamin Otte's avatar
Benjamin Otte committed
54 55 56 57

  return TRUE;
}

Benjamin Otte's avatar
Benjamin Otte committed
58 59
static GtkStateFlags
gtk_css_matcher_widget_path_get_state (const GtkCssMatcher *matcher)
Benjamin Otte's avatar
Benjamin Otte committed
60
{
61
  return matcher->path.state_flags;
Benjamin Otte's avatar
Benjamin Otte committed
62 63
}

Benjamin Otte's avatar
Benjamin Otte committed
64
static gboolean
65 66
gtk_css_matcher_widget_path_has_type (const GtkCssMatcher *matcher,
                                      GType                type)
Benjamin Otte's avatar
Benjamin Otte committed
67 68
{
  const GtkWidgetPath *siblings;
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 g_type_is_a (gtk_widget_path_iter_get_object_type (siblings, matcher->path.sibling_index), type);
Benjamin Otte's avatar
Benjamin Otte committed
73
  else
74
    return g_type_is_a (gtk_widget_path_iter_get_object_type (matcher->path.path, matcher->path.index), type);
Benjamin Otte's avatar
Benjamin Otte committed
75 76
}

Benjamin Otte's avatar
Benjamin Otte committed
77 78
static gboolean
gtk_css_matcher_widget_path_has_class (const GtkCssMatcher *matcher,
79
                                       GQuark               class_name)
Benjamin Otte's avatar
Benjamin Otte committed
80 81 82
{
  const GtkWidgetPath *siblings;
  
83 84
  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))
85
    return gtk_widget_path_iter_has_qclass (siblings, matcher->path.sibling_index, class_name);
Benjamin Otte's avatar
Benjamin Otte committed
86
  else
87
    return gtk_widget_path_iter_has_qclass (matcher->path.path, matcher->path.index, class_name);
Benjamin Otte's avatar
Benjamin Otte committed
88 89
}

Benjamin Otte's avatar
Benjamin Otte committed
90 91 92
static gboolean
gtk_css_matcher_widget_path_has_id (const GtkCssMatcher *matcher,
                                    const char          *id)
Benjamin Otte's avatar
Benjamin Otte committed
93 94 95
{
  const GtkWidgetPath *siblings;
  
96 97 98
  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
99
  else
100
    return gtk_widget_path_iter_has_name (matcher->path.path, matcher->path.index, id);
Benjamin Otte's avatar
Benjamin Otte committed
101 102
}

Benjamin Otte's avatar
Benjamin Otte committed
103 104
static gboolean
gtk_css_matcher_widget_path_has_regions (const GtkCssMatcher *matcher)
Benjamin Otte's avatar
Benjamin Otte committed
105 106 107 108 109
{
  const GtkWidgetPath *siblings;
  GSList *regions;
  gboolean result;
  
110 111 112
  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))
    regions = gtk_widget_path_iter_list_regions (siblings, matcher->path.sibling_index);
Benjamin Otte's avatar
Benjamin Otte committed
113
  else
114
    regions = gtk_widget_path_iter_list_regions (matcher->path.path, matcher->path.index);
Benjamin Otte's avatar
Benjamin Otte committed
115 116 117 118 119 120
  result = regions != NULL;
  g_slist_free (regions);

  return result;
}

Benjamin Otte's avatar
Benjamin Otte committed
121 122 123 124
static gboolean
gtk_css_matcher_widget_path_has_region (const GtkCssMatcher *matcher,
                                        const char          *region,
                                        GtkRegionFlags       flags)
Benjamin Otte's avatar
Benjamin Otte committed
125 126 127 128
{
  const GtkWidgetPath *siblings;
  GtkRegionFlags region_flags;
  
129 130
  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))
Benjamin Otte's avatar
Benjamin Otte committed
131
    {
132
      if (!gtk_widget_path_iter_has_region (siblings, matcher->path.sibling_index, region, &region_flags))
Benjamin Otte's avatar
Benjamin Otte committed
133 134 135 136
        return FALSE;
    }
  else
    {
137
      if (!gtk_widget_path_iter_has_region (matcher->path.path, matcher->path.index, region, &region_flags))
Benjamin Otte's avatar
Benjamin Otte committed
138 139 140 141 142 143 144 145 146
        return FALSE;
    }

  if ((flags & region_flags) != flags)
    return FALSE;

  return TRUE;
}

147 148 149 150 151
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
152 153
{
  const GtkWidgetPath *siblings;
154
  int x;
Benjamin Otte's avatar
Benjamin Otte committed
155

156
  siblings = gtk_widget_path_iter_get_siblings (matcher->path.path, matcher->path.index);
Benjamin Otte's avatar
Benjamin Otte committed
157
  if (!siblings)
158 159 160 161 162 163 164 165 166 167 168 169 170 171
    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
172

173
  return x / a > 0;
Benjamin Otte's avatar
Benjamin Otte committed
174 175
}

Benjamin Otte's avatar
Benjamin Otte committed
176 177 178 179
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,
180
  gtk_css_matcher_widget_path_has_type,
Benjamin Otte's avatar
Benjamin Otte committed
181 182 183 184
  gtk_css_matcher_widget_path_has_class,
  gtk_css_matcher_widget_path_has_id,
  gtk_css_matcher_widget_path_has_regions,
  gtk_css_matcher_widget_path_has_region,
185
  gtk_css_matcher_widget_path_has_position,
186
  FALSE
Benjamin Otte's avatar
Benjamin Otte committed
187 188
};

189
gboolean
Benjamin Otte's avatar
Benjamin Otte committed
190 191 192 193
_gtk_css_matcher_init (GtkCssMatcher       *matcher,
                       const GtkWidgetPath *path,
                       GtkStateFlags        state)
{
194 195 196
  if (gtk_widget_path_length (path) == 0)
    return FALSE;

197 198 199 200 201
  matcher->path.klass = &GTK_CSS_MATCHER_WIDGET_PATH;
  matcher->path.path = path;
  matcher->path.state_flags = state;
  matcher->path.index = gtk_widget_path_length (path) - 1;
  matcher->path.sibling_index = gtk_widget_path_iter_get_sibling_index (path, matcher->path.index);
202 203

  return TRUE;
Benjamin Otte's avatar
Benjamin Otte committed
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
/* 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
233 234
    | GTK_STATE_FLAG_FOCUSED | GTK_STATE_FLAG_BACKDROP | GTK_STATE_FLAG_LINK
    | GTK_STATE_FLAG_VISITED;
235 236 237
}

static gboolean
238 239
gtk_css_matcher_any_has_type (const GtkCssMatcher *matcher,
                              GType                type)
240 241 242 243 244 245
{
  return TRUE;
}

static gboolean
gtk_css_matcher_any_has_class (const GtkCssMatcher *matcher,
246
                               GQuark               class_name)
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
{
  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_regions (const GtkCssMatcher *matcher)
{
  return TRUE;
}

static gboolean
gtk_css_matcher_any_has_region (const GtkCssMatcher *matcher,
                                const char          *region,
                                GtkRegionFlags       flags)
{
  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,
285
  gtk_css_matcher_any_has_type,
286 287 288 289 290
  gtk_css_matcher_any_has_class,
  gtk_css_matcher_any_has_id,
  gtk_css_matcher_any_has_regions,
  gtk_css_matcher_any_has_region,
  gtk_css_matcher_any_has_position,
291
  TRUE
292 293 294 295 296 297 298 299
};

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

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
/* 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
330 331
      | GTK_STATE_FLAG_FOCUSED | GTK_STATE_FLAG_BACKDROP | GTK_STATE_FLAG_LINK
      | GTK_STATE_FLAG_VISITED;
332 333 334
}

static gboolean
335 336
gtk_css_matcher_superset_has_type (const GtkCssMatcher *matcher,
                                   GType                type)
337 338
{
  if (matcher->superset.relevant & GTK_CSS_CHANGE_NAME)
339
    return _gtk_css_matcher_has_type (matcher->superset.subset, type);
340 341 342 343 344 345
  else
    return TRUE;
}

static gboolean
gtk_css_matcher_superset_has_class (const GtkCssMatcher *matcher,
346
                                    GQuark               class_name)
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 402 403 404
{
  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_regions (const GtkCssMatcher *matcher)
{
  if (matcher->superset.relevant & GTK_CSS_CHANGE_NAME)
    return _gtk_css_matcher_has_regions (matcher->superset.subset);
  else
    return TRUE;
}

static gboolean
gtk_css_matcher_superset_has_region (const GtkCssMatcher *matcher,
                                     const char          *region,
                                     GtkRegionFlags       flags)
{
  if (matcher->superset.relevant & GTK_CSS_CHANGE_NAME)
    {
      if (matcher->superset.relevant & GTK_CSS_CHANGE_POSITION)
        return _gtk_css_matcher_has_region (matcher->superset.subset, region, flags);
      else
        return _gtk_css_matcher_has_region (matcher->superset.subset, region, 0);
    }
  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,
405
  gtk_css_matcher_superset_has_type,
406 407 408 409 410
  gtk_css_matcher_superset_has_class,
  gtk_css_matcher_superset_has_id,
  gtk_css_matcher_superset_has_regions,
  gtk_css_matcher_superset_has_region,
  gtk_css_matcher_superset_has_position,
411
  FALSE
412 413 414 415 416 417 418 419 420 421 422 423 424 425 426
};

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;
}