gtkcssmatcher.c 13 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 65 66
static gboolean
gtk_css_matcher_widget_path_has_name (const GtkCssMatcher *matcher,
                                      const char          *name)
Benjamin Otte's avatar
Benjamin Otte committed
67 68 69 70 71
{
  const GtkWidgetPath *siblings;
  GType type;
  
  type = g_type_from_name (name);
72 73 74
  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
75
  else
76
    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
77 78
}

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

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

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

  return result;
}

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

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

  return TRUE;
}

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

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

175
  return x / a > 0;
Benjamin Otte's avatar
Benjamin Otte committed
176 177
}

Benjamin Otte's avatar
Benjamin Otte committed
178 179 180 181 182 183 184 185 186
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,
  gtk_css_matcher_widget_path_has_name,
  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,
187
  gtk_css_matcher_widget_path_has_position,
Benjamin Otte's avatar
Benjamin Otte committed
188 189
};

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

198 199 200 201 202
  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);
203 204

  return TRUE;
Benjamin Otte's avatar
Benjamin Otte committed
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
/* 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
    | GTK_STATE_FLAG_FOCUSED | GTK_STATE_FLAG_BACKDROP;
}

static gboolean
gtk_css_matcher_any_has_name (const GtkCssMatcher *matcher,
                              const char          *name)
{
  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 285 286 287 288 289 290 291 292 293 294 295 296 297 298
{
  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,
  gtk_css_matcher_any_has_name,
  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,
};

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

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
/* 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
      | GTK_STATE_FLAG_FOCUSED | GTK_STATE_FLAG_BACKDROP;
}

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

static gboolean
gtk_css_matcher_superset_has_class (const GtkCssMatcher *matcher,
344
                                    GQuark               class_name)
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 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423
{
  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,
  gtk_css_matcher_superset_has_name,
  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,
};

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