gtkcssnodedeclaration.c 16.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/*
 * Copyright © 2014 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 "gtkcssnodedeclarationprivate.h"
21
#include "gtkwidgetpathprivate.h"
22 23 24 25 26 27 28 29 30 31 32 33 34 35

#include <string.h>

typedef struct _GtkRegion GtkRegion;

struct _GtkRegion
{
  GQuark class_quark;
  GtkRegionFlags flags;
};

struct _GtkCssNodeDeclaration {
  guint refcount;
  GtkJunctionSides junction_sides;
36
  GType type;
37
  const /* interned */ char *name;
38
  const /* interned */ char *id;
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
  GtkStateFlags state;
  guint n_classes;
  guint n_regions;
  /* GQuark classes[n_classes]; */
  /* GtkRegion region[n_regions]; */
};

static inline GQuark *
get_classes (const GtkCssNodeDeclaration *decl)
{
  return (GQuark *) (decl + 1);
}

static inline GtkRegion *
get_regions (const GtkCssNodeDeclaration *decl)
{
  return (GtkRegion *) (get_classes (decl) + decl->n_classes);
}

static inline gsize
sizeof_node (guint n_classes,
             guint n_regions)
{
  return sizeof (GtkCssNodeDeclaration)
       + sizeof (GQuark) * n_classes
       + sizeof (GtkRegion) * n_regions;
}

static inline gsize
sizeof_this_node (GtkCssNodeDeclaration *decl)
{
  return sizeof_node (decl->n_classes, decl->n_regions);
}

static void
gtk_css_node_declaration_make_writable (GtkCssNodeDeclaration **decl)
{
  if ((*decl)->refcount == 1)
    return;

  (*decl)->refcount--;

  *decl = g_memdup (*decl, sizeof_this_node (*decl));
  (*decl)->refcount = 1;
}

static void
gtk_css_node_declaration_make_writable_resize (GtkCssNodeDeclaration **decl,
                                               gsize                   offset,
                                               gsize                   bytes_added,
                                               gsize                   bytes_removed)
{
  gsize old_size = sizeof_this_node (*decl);
  gsize new_size = old_size + bytes_added - bytes_removed;

  if ((*decl)->refcount == 1)
    {
      if (bytes_removed > 0 && old_size - offset - bytes_removed > 0)
97
        memmove (((char *) *decl) + offset, ((char *) *decl) + offset + bytes_removed, old_size - offset - bytes_removed);
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
      *decl = g_realloc (*decl, new_size);
      if (bytes_added > 0 && old_size - offset > 0)
        memmove (((char *) *decl) + offset + bytes_added, ((char *) *decl) + offset, old_size - offset);
    }
  else
    {
      GtkCssNodeDeclaration *old = *decl;

      old->refcount--;
  
      *decl = g_malloc (new_size);
      memcpy (*decl, old, offset);
      if (old_size - offset - bytes_removed > 0)
        memcpy (((char *) *decl) + offset + bytes_added, ((char *) old) + offset + bytes_removed, old_size - offset - bytes_removed);
      (*decl)->refcount = 1;
    }
}

GtkCssNodeDeclaration *
gtk_css_node_declaration_new (void)
{
  static GtkCssNodeDeclaration empty = {
    1, /* need to own a ref ourselves so the copy-on-write path kicks in when people change things */
    0,
    0,
123 124 125
    NULL,
    NULL,
    0,
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
    0,
    0
  };

  return gtk_css_node_declaration_ref (&empty);
}

GtkCssNodeDeclaration *
gtk_css_node_declaration_ref (GtkCssNodeDeclaration *decl)
{
  decl->refcount++;

  return decl;
}

void
gtk_css_node_declaration_unref (GtkCssNodeDeclaration *decl)
{
  decl->refcount--;
  if (decl->refcount > 0)
    return;

  g_free (decl);
}

gboolean
gtk_css_node_declaration_set_junction_sides (GtkCssNodeDeclaration **decl,
                                             GtkJunctionSides        junction_sides)
{
  if ((*decl)->junction_sides == junction_sides)
    return FALSE;
  
  gtk_css_node_declaration_make_writable (decl);
  (*decl)->junction_sides = junction_sides;

  return TRUE;
}

GtkJunctionSides
gtk_css_node_declaration_get_junction_sides (const GtkCssNodeDeclaration *decl)
{
  return decl->junction_sides;
}

170 171 172 173
gboolean
gtk_css_node_declaration_set_type (GtkCssNodeDeclaration **decl,
                                   GType                   type)
{
174
  if ((*decl)->type == type)
175 176 177 178 179 180 181 182 183 184 185 186 187 188
    return FALSE;

  gtk_css_node_declaration_make_writable (decl);
  (*decl)->type = type;

  return TRUE;
}

GType
gtk_css_node_declaration_get_type (const GtkCssNodeDeclaration *decl)
{
  return decl->type;
}

189 190 191 192
gboolean
gtk_css_node_declaration_set_name (GtkCssNodeDeclaration   **decl,
                                   /*interned*/ const char  *name)
{
193
  if ((*decl)->name == name)
194 195 196 197 198 199 200 201 202 203 204 205 206 207
    return FALSE;

  gtk_css_node_declaration_make_writable (decl);
  (*decl)->name = name;

  return TRUE;
}

/*interned*/ const char *
gtk_css_node_declaration_get_name (const GtkCssNodeDeclaration *decl)
{
  return decl->name;
}

208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
gboolean
gtk_css_node_declaration_set_id (GtkCssNodeDeclaration **decl,
                                 const char             *id)
{
  id = g_intern_string (id);

  if ((*decl)->id == id)
    return FALSE;

  gtk_css_node_declaration_make_writable (decl);
  (*decl)->id = id;

  return TRUE;
}

const char *
gtk_css_node_declaration_get_id (const GtkCssNodeDeclaration *decl)
{
  return decl->id;
}

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
gboolean
gtk_css_node_declaration_set_state (GtkCssNodeDeclaration **decl,
                                    GtkStateFlags           state)
{
  if ((*decl)->state == state)
    return FALSE;
  
  gtk_css_node_declaration_make_writable (decl);
  (*decl)->state = state;

  return TRUE;
}

GtkStateFlags
gtk_css_node_declaration_get_state (const GtkCssNodeDeclaration *decl)
{
  return decl->state;
}

static gboolean
find_class (const GtkCssNodeDeclaration *decl,
            GQuark                       class_quark,
            guint                       *position)
{
  gint min, max, mid;
  gboolean found = FALSE;
  GQuark *classes;
  guint pos;

258
  *position = 0;
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

  if (decl->n_classes == 0)
    return FALSE;

  min = 0;
  max = decl->n_classes - 1;
  classes = get_classes (decl);

  do
    {
      GQuark item;

      mid = (min + max) / 2;
      item = classes[mid];

      if (class_quark == item)
        {
          found = TRUE;
          pos = mid;
          break;
        }
      else if (class_quark > item)
        min = pos = mid + 1;
      else
        {
          max = mid - 1;
          pos = mid;
        }
    }
  while (min <= max);

290
  *position = pos;
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

  return found;
}

gboolean
gtk_css_node_declaration_add_class (GtkCssNodeDeclaration **decl,
                                    GQuark                  class_quark)
{
  guint pos;

  if (find_class (*decl, class_quark, &pos))
    return FALSE;

  gtk_css_node_declaration_make_writable_resize (decl,
                                                 (char *) &get_classes (*decl)[pos] - (char *) *decl,
                                                 sizeof (GQuark),
                                                 0);
  (*decl)->n_classes++;
  get_classes(*decl)[pos] = class_quark;

  return TRUE;
}

gboolean
gtk_css_node_declaration_remove_class (GtkCssNodeDeclaration **decl,
                                       GQuark                  class_quark)
{
  guint pos;

  if (!find_class (*decl, class_quark, &pos))
    return FALSE;

  gtk_css_node_declaration_make_writable_resize (decl,
                                                 (char *) &get_classes (*decl)[pos] - (char *) *decl,
                                                 0,
                                                 sizeof (GQuark));
  (*decl)->n_classes--;

  return TRUE;
}

332 333 334 335 336 337 338 339 340 341 342 343 344 345 346
gboolean
gtk_css_node_declaration_clear_classes (GtkCssNodeDeclaration **decl)
{
  if ((*decl)->n_classes == 0)
    return FALSE;

  gtk_css_node_declaration_make_writable_resize (decl,
                                                 (char *) get_classes (*decl) - (char *) *decl,
                                                 0,
                                                 sizeof (GQuark) * (*decl)->n_classes);
  (*decl)->n_classes = 0;

  return TRUE;
}

347 348 349 350
gboolean
gtk_css_node_declaration_has_class (const GtkCssNodeDeclaration *decl,
                                    GQuark                       class_quark)
{
351
  guint pos;
352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371
  GQuark *classes = get_classes (decl);

  switch (decl->n_classes)
    {
    case 3:
      if (classes[2] == class_quark)
        return TRUE;

    case 2:
      if (classes[1] == class_quark)
        return TRUE;

    case 1:
      if (classes[0] == class_quark)
        return TRUE;

    case 0:
      return FALSE;

    default:
372
      return find_class (decl, class_quark, &pos);
373
    }
374 375
}

376 377 378
const GQuark *
gtk_css_node_declaration_get_classes (const GtkCssNodeDeclaration *decl,
                                      guint                       *n_classes)
379
{
380
  *n_classes = decl->n_classes;
381

382
  return get_classes (decl);
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 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474
}

static gboolean
find_region (const GtkCssNodeDeclaration *decl,
             GQuark                       region_quark,
             guint                       *position)
{
  gint min, max, mid;
  gboolean found = FALSE;
  GtkRegion *regions;
  guint pos;

  if (position)
    *position = 0;

  if (decl->n_regions == 0)
    return FALSE;

  min = 0;
  max = decl->n_regions - 1;
  regions = get_regions (decl);

  do
    {
      GQuark item;

      mid = (min + max) / 2;
      item = regions[mid].class_quark;

      if (region_quark == item)
        {
          found = TRUE;
          pos = mid;
          break;
        }
      else if (region_quark > item)
        min = pos = mid + 1;
      else
        {
          max = mid - 1;
          pos = mid;
        }
    }
  while (min <= max);

  if (position)
    *position = pos;

  return found;
}

gboolean
gtk_css_node_declaration_add_region (GtkCssNodeDeclaration **decl,
                                     GQuark                  region_quark,
                                     GtkRegionFlags          flags)
{
  GtkRegion *regions;
  guint pos;

  if (find_region (*decl, region_quark, &pos))
    return FALSE;

  gtk_css_node_declaration_make_writable_resize (decl,
                                                 (char *) &get_regions (*decl)[pos] - (char *) *decl,
                                                 sizeof (GtkRegion),
                                                 0);
  (*decl)->n_regions++;
  regions = get_regions(*decl);
  regions[pos].class_quark = region_quark;
  regions[pos].flags = flags;

  return TRUE;
}

gboolean
gtk_css_node_declaration_remove_region (GtkCssNodeDeclaration **decl,
                                        GQuark                  region_quark)
{
  guint pos;

  if (!find_region (*decl, region_quark, &pos))
    return FALSE;

  gtk_css_node_declaration_make_writable_resize (decl,
                                                 (char *) &get_regions (*decl)[pos] - (char *) *decl,
                                                 0,
                                                 sizeof (GtkRegion));
  (*decl)->n_regions--;

  return TRUE;
}

475 476 477 478 479 480 481 482 483 484 485 486 487 488 489
gboolean
gtk_css_node_declaration_clear_regions (GtkCssNodeDeclaration **decl)
{
  if ((*decl)->n_regions == 0)
    return FALSE;

  gtk_css_node_declaration_make_writable_resize (decl,
                                                 (char *) get_regions (*decl) - (char *) *decl,
                                                 0,
                                                 sizeof (GtkRegion) * (*decl)->n_regions);
  (*decl)->n_regions = 0;

  return TRUE;
}

490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535
gboolean
gtk_css_node_declaration_has_region (const GtkCssNodeDeclaration  *decl,
                                     GQuark                        region_quark,
                                     GtkRegionFlags               *flags_return)
{
  guint pos;

  if (!find_region (decl, region_quark, &pos))
    {
      if (flags_return)
        *flags_return = 0;
      return FALSE;
    }

  if (flags_return)
    *flags_return = get_regions (decl)[pos].flags;

  return TRUE;
}

GList *
gtk_css_node_declaration_list_regions (const GtkCssNodeDeclaration *decl)
{
  GtkRegion *regions;
  GList *result;
  guint i;

  regions = get_regions (decl);
  result = NULL;

  for (i = 0; i < decl->n_regions; i++)
    {
      result = g_list_prepend (result, GUINT_TO_POINTER (regions[i].class_quark));
    }

  return result;
}

guint
gtk_css_node_declaration_hash (gconstpointer elem)
{
  const GtkCssNodeDeclaration *decl = elem;
  GQuark *classes;
  GtkRegion *regions;
  guint hash, i;
  
536
  hash = (guint) decl->type;
537
  hash ^= GPOINTER_TO_UINT (decl->name);
538 539
  hash <<= 5;
  hash ^= GPOINTER_TO_UINT (decl->id);
540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574

  classes = get_classes (decl);
  for (i = 0; i < decl->n_classes; i++)
    {
      hash <<= 5;
      hash += classes[i];
    }

  regions = get_regions (decl);
  for (i = 0; i < decl->n_regions; i++)
    {
      hash <<= 5;
      hash += regions[i].class_quark;
      hash += regions[i].flags;
    }

  hash ^= ((guint) decl->junction_sides) << (sizeof (guint) * 8 - 5);
  hash ^= decl->state;

  return hash;
}

gboolean
gtk_css_node_declaration_equal (gconstpointer elem1,
                                gconstpointer elem2)
{
  const GtkCssNodeDeclaration *decl1 = elem1;
  const GtkCssNodeDeclaration *decl2 = elem2;
  GQuark *classes1, *classes2;
  GtkRegion *regions1, *regions2;
  guint i;

  if (decl1 == decl2)
    return TRUE;

575 576 577
  if (decl1->type != decl2->type)
    return FALSE;

578 579 580
  if (decl1->name != decl2->name)
    return FALSE;

581 582 583
  if (decl1->state != decl2->state)
    return FALSE;

584 585 586
  if (decl1->id != decl2->id)
    return FALSE;

587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624
  if (decl1->n_classes != decl2->n_classes)
    return FALSE;

  classes1 = get_classes (decl1);
  classes2 = get_classes (decl2);
  for (i = 0; i < decl1->n_classes; i++)
    {
      if (classes1[i] != classes2[i])
        return FALSE;
    }

  if (decl1->n_regions != decl2->n_regions)
    return FALSE;

  regions1 = get_regions (decl1);
  regions2 = get_regions (decl2);
  for (i = 0; i < decl1->n_regions; i++)
    {
      if (regions1[i].class_quark != regions2[i].class_quark ||
          regions1[i].flags != regions2[i].flags)
        return FALSE;
    }

  if (decl1->junction_sides != decl2->junction_sides)
    return FALSE;

  return TRUE;
}

void
gtk_css_node_declaration_add_to_widget_path (const GtkCssNodeDeclaration *decl,
                                             GtkWidgetPath               *path,
                                             guint                        pos)
{
  GQuark *classes;
  GtkRegion *regions;
  guint i;

625 626
  /* Set name and id */
  gtk_widget_path_iter_set_object_name (path, pos, decl->name);
627 628
  if (decl->id)
    gtk_widget_path_iter_set_name (path, pos, decl->id);
629

630 631 632 633 634 635 636 637 638 639 640 641 642 643 644
  /* Set widget regions */
  regions = get_regions (decl);
  for (i = 0; i < decl->n_regions; i++)
    {
G_GNUC_BEGIN_IGNORE_DEPRECATIONS
      gtk_widget_path_iter_add_region (path, pos,
                                       g_quark_to_string (regions[i].class_quark),
                                       regions[i].flags);
G_GNUC_END_IGNORE_DEPRECATIONS
    }

  /* Set widget classes */
  classes = get_classes (decl);
  for (i = 0; i < decl->n_classes; i++)
    {
645
      gtk_widget_path_iter_add_qclass (path, pos, classes[i]);
646 647 648 649 650 651
    }

  /* Set widget state */
  gtk_widget_path_iter_set_state (path, pos, decl->state);
}

652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701
/* Append the declaration to the string, in selector format */
void
gtk_css_node_declaration_print (const GtkCssNodeDeclaration *decl,
                                GString                     *string)
{
  static const char *state_names[] = {
    "active",
    "hover",
    "selected",
    "disabled",
    "indeterminate",
    "focus",
    "backdrop",
    "dir(ltr)",
    "dir(rtl)",
    "link",
    "visited",
    "checked",
    "drop(active)"
  };
  const GQuark *classes;
  guint i;

  if (decl->name)
    g_string_append (string, decl->name);
  else
    g_string_append (string, g_type_name (decl->type));

  if (decl->id)
    {
      g_string_append_c (string, '#');
      g_string_append (string, decl->id);
    }

  classes = get_classes (decl);
  for (i = 0; i < decl->n_classes; i++)
    {
      g_string_append_c (string, '.');
      g_string_append (string, g_quark_to_string (classes[i]));
    }

  for (i = 0; i < G_N_ELEMENTS (state_names); i++)
    {
      if (decl->state & (1 << i))
        {
          g_string_append_c (string, ':');
          g_string_append (string, state_names[i]);
        }
    }
}