gtksnapshot.c 30.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
/* GTK - The GIMP Toolkit
 * Copyright (C) 2016 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 "gtksnapshot.h"
#include "gtksnapshotprivate.h"

23 24
#include "gtkcssrgbavalueprivate.h"
#include "gtkcssshadowsvalueprivate.h"
25 26
#include "gtkrenderbackgroundprivate.h"
#include "gtkrenderborderprivate.h"
27
#include "gtkrendericonprivate.h"
28 29
#include "gtkstylecontextprivate.h"

30 31
#include "gsk/gskrendernodeprivate.h"

Matthias Clasen's avatar
Matthias Clasen committed
32 33 34 35 36 37 38 39 40 41 42 43

/**
 * SECTION:gtksnapshot
 * @Short_description: Auxiliary object for snapshots
 * @Title: GtkSnapshot
 *
 * GtkSnapshot is an auxiliary object that assists in creating #GskRenderNodes
 * in the #GtkWidget::snapshot vfunc. It functions in a similar way to
 * a cairo context, and maintains a stack of render nodes and their associated
 * transformations.
 *
 * The node at the top of the stack is the the one that gtk_snapshot_append_node()
44
 * operates on. Use the gtk_snapshot_push() and gtk_snapshot_pop() functions to
Matthias Clasen's avatar
Matthias Clasen committed
45 46 47 48 49 50
 * change the current node.
 *
 * The only way to obtain a #GtkSnapshot object is as an argument to
 * the #GtkWidget::snapshot vfunc.
 */

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
static GskRenderNode *
gtk_snapshot_collect_default (GskRenderNode **nodes,
                              guint           n_nodes,
                              const char     *name,
                              gpointer        unused)
{
  GskRenderNode *node;

  if (n_nodes == 0)
    {
      node = NULL;
    }
  else if (n_nodes == 1)
    {
      node = gsk_render_node_ref (nodes[0]);
    }
  else
    {
      node = gsk_container_node_new (nodes, n_nodes);
      gsk_render_node_set_name (node, name);
    }

  return node;
}

76
static GtkSnapshotState *
77 78 79 80 81 82 83
gtk_snapshot_state_new (GtkSnapshotState       *parent,
                        char                   *name,
                        cairo_region_t         *clip,
                        double                  translate_x,
                        double                  translate_y,
                        GtkSnapshotCollectFunc  collect_func,
                        gpointer                collect_data)
84 85 86 87 88
{
  GtkSnapshotState *state;

  state = g_slice_new0 (GtkSnapshotState);

89 90
  state->nodes = g_ptr_array_new_with_free_func ((GDestroyNotify) gsk_render_node_unref);

91
  state->parent = parent;
92
  state->name = name;
93 94
  if (clip)
    state->clip_region = cairo_region_reference (clip);
95 96 97 98
  state->translate_x = translate_x;
  state->translate_y = translate_y;
  state->collect_func = collect_func;
  state->collect_data = collect_data;
99 100 101 102 103 104 105

  return state;
}

static void
gtk_snapshot_state_free (GtkSnapshotState *state)
{
106 107
  g_ptr_array_unref (state->nodes);

108 109 110
  if (state->clip_region)
    cairo_region_destroy (state->clip_region);

111 112
  g_free (state->name);

113 114 115
  g_slice_free (GtkSnapshotState, state);
}

116
void
117 118
gtk_snapshot_init (GtkSnapshot          *snapshot,
                   GskRenderer          *renderer,
119 120 121
                   const cairo_region_t *clip,
                   const char           *name,
                   ...)
122
{
123
  char *str;
124

125 126
  snapshot->state = NULL;
  snapshot->renderer = renderer;
127 128 129 130 131 132 133 134 135

  if (name)
    {
      va_list args;

      va_start (args, name);
      str = g_strdup_vprintf (name, args);
      va_end (args);
    }
136 137
  else
    str = NULL;
138

139 140 141
  snapshot->state = gtk_snapshot_state_new (NULL,
                                            str,
                                            (cairo_region_t *) clip,
142 143 144
                                            0, 0,
                                            gtk_snapshot_collect_default,
                                            NULL);
145 146
}

147
GskRenderNode *
148
gtk_snapshot_finish (GtkSnapshot *snapshot)
149
{
150 151 152
  GskRenderNode *result;
  
  result = gtk_snapshot_pop (snapshot);
153

154
  if (snapshot->state != NULL)
155 156 157
    {
      g_warning ("Too many gtk_snapshot_push() calls.");
    }
158

159
  return result;
160 161
}

Matthias Clasen's avatar
Matthias Clasen committed
162 163 164
/**
 * gtk_snapshot_push:
 * @snapshot: a #GtkSnapshot
165 166 167
 * @keep_coordinates: If %TRUE, the current offset and clip will be kept.
 *     Otherwise, the clip will be unset and the offset will be reset to
 *     (0, 0).
Matthias Clasen's avatar
Matthias Clasen committed
168
 * @bounds: the bounds for the new node
169
 * @name: (transfer none): a printf() style format string for the name for the new node
Matthias Clasen's avatar
Matthias Clasen committed
170 171 172 173 174 175 176
 * @...: arguments to insert into the format string
 *
 * Creates a new render node, appends it to the current render
 * node of @snapshot, and makes it the new current render node.
 *
 * Since: 3.90
 */
177
void
178
gtk_snapshot_push (GtkSnapshot           *snapshot,
179
                   gboolean               keep_coordinates,
180 181 182
                   const char            *name,
                   ...)
{
183
  char *str;
184 185 186 187 188 189 190 191 192

  if (name)
    {
      va_list args;

      va_start (args, name);
      str = g_strdup_vprintf (name, args);
      va_end (args);
    }
193 194
  else
    str = NULL;
195

196 197 198 199 200 201
  if (keep_coordinates)
    {
      snapshot->state = gtk_snapshot_state_new (snapshot->state,
                                                str,
                                                snapshot->state->clip_region,
                                                snapshot->state->translate_x,
202 203 204
                                                snapshot->state->translate_y,
                                                gtk_snapshot_collect_default,
                                                NULL);
205 206 207 208 209 210
    }
  else
    {
      snapshot->state = gtk_snapshot_state_new (snapshot->state,
                                                str,
                                                NULL,
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
                                                0, 0,
                                                gtk_snapshot_collect_default,
                                                NULL);
    }
}

static GskRenderNode *
gtk_snapshot_collect_transform (GskRenderNode **nodes,
                                guint           n_nodes,
                                const char     *name,
                                gpointer        transform)
{
  GskRenderNode *node, *transform_node;

  node = gtk_snapshot_collect_default (nodes, n_nodes, name, NULL);
  if (node == NULL)
    return NULL;

  transform_node = gsk_transform_node_new (node, transform);
  gsk_render_node_set_name (transform_node, name);

  gsk_render_node_unref (node);
  g_slice_free (graphene_matrix_t, transform);

  return transform_node;
}

void
gtk_snapshot_push_transform (GtkSnapshot             *snapshot,
                             const graphene_matrix_t *transform,
                             const char              *name,
                             ...)
{
  graphene_matrix_t offset;
  graphene_matrix_t* real_transform;

  graphene_matrix_init_translate (&offset,
                                  &GRAPHENE_POINT3D_INIT(
                                      snapshot->state->translate_x,
                                      snapshot->state->translate_y,
                                      0
                                  ));

  real_transform = g_slice_new (graphene_matrix_t);
  graphene_matrix_multiply (transform, &offset, real_transform);

  char *str;

  if (name)
    {
      va_list args;

      va_start (args, name);
      str = g_strdup_vprintf (name, args);
      va_end (args);
    }
  else
    str = NULL;

  snapshot->state = gtk_snapshot_state_new (snapshot->state,
                                            str,
                                            NULL,
                                            0, 0,
                                            gtk_snapshot_collect_transform,
                                            real_transform);
}

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
static GskRenderNode *
gtk_snapshot_collect_opacity (GskRenderNode **nodes,
                              guint           n_nodes,
                              const char     *name,
                              gpointer        opacity)
{
  GskRenderNode *node, *opacity_node;

  node = gtk_snapshot_collect_default (nodes, n_nodes, name, NULL);
  if (node == NULL)
    return NULL;

  opacity_node = gsk_opacity_node_new (node, *(double *) opacity);
  gsk_render_node_set_name (opacity_node, name);

  gsk_render_node_unref (node);
  g_free (opacity);

  return opacity_node;
}

void
gtk_snapshot_push_opacity (GtkSnapshot *snapshot,
                           double       opacity,
                           const char  *name,
                           ...)
{
  double *real_opacity;
  char *str;

  if (name)
    {
      va_list args;

      va_start (args, name);
      str = g_strdup_vprintf (name, args);
      va_end (args);
    }
  else
    str = NULL;

  real_opacity = g_memdup (&opacity, sizeof (gdouble));

  snapshot->state = gtk_snapshot_state_new (snapshot->state,
                                            str,
                                            snapshot->state->clip_region,
                                            snapshot->state->translate_x,
                                            snapshot->state->translate_y,
                                            gtk_snapshot_collect_opacity,
                                            real_opacity);
}

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
static void
rectangle_init_from_graphene (cairo_rectangle_int_t *cairo,
                              const graphene_rect_t *graphene)
{
  cairo->x = floorf (graphene->origin.x);
  cairo->y = floorf (graphene->origin.y);
  cairo->width = ceilf (graphene->origin.x + graphene->size.width) - cairo->x;
  cairo->height = ceilf (graphene->origin.y + graphene->size.height) - cairo->y;
}

static GskRenderNode *
gtk_snapshot_collect_clip (GskRenderNode **nodes,
                           guint           n_nodes,
                           const char     *name,
                           gpointer        bounds)
{
  GskRenderNode *node, *clip_node;

  node = gtk_snapshot_collect_default (nodes, n_nodes, name, NULL);
  if (node == NULL)
    return NULL;

  clip_node = gsk_clip_node_new (node, bounds);
  gsk_render_node_set_name (clip_node, name);

  gsk_render_node_unref (node);
  g_slice_free (graphene_rect_t, bounds);

  return clip_node;
}

void
gtk_snapshot_push_clip (GtkSnapshot           *snapshot,
                        const graphene_rect_t *bounds,
                        const char            *name,
                        ...)
{
  graphene_rect_t *real_bounds;
  cairo_region_t *clip;
  cairo_rectangle_int_t rect;
  char *str;

  real_bounds = g_slice_new (graphene_rect_t);
  graphene_rect_offset_r (bounds, snapshot->state->translate_x, snapshot->state->translate_y, real_bounds);

  if (name)
    {
      va_list args;

      va_start (args, name);
      str = g_strdup_vprintf (name, args);
      va_end (args);
    }
  else
    str = NULL;
  
  rectangle_init_from_graphene (&rect, real_bounds);
  if (snapshot->state->clip_region)
    {
      clip = cairo_region_copy (snapshot->state->clip_region);
      cairo_region_intersect_rectangle (clip, &rect);
391
    }
392 393 394 395 396 397 398 399 400 401 402 403 404
  else
    {
      clip = cairo_region_create_rectangle (&rect);
    }
  snapshot->state = gtk_snapshot_state_new (snapshot->state,
                                            str,
                                            clip,
                                            snapshot->state->translate_x,
                                            snapshot->state->translate_y,
                                            gtk_snapshot_collect_clip,
                                            real_bounds);

  cairo_region_destroy (clip);
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 GskRenderNode *
gtk_snapshot_collect_rounded_clip (GskRenderNode **nodes,
                                   guint           n_nodes,
                                   const char     *name,
                                   gpointer        bounds)
{
  GskRenderNode *node, *clip_node;

  node = gtk_snapshot_collect_default (nodes, n_nodes, name, NULL);
  if (node == NULL)
    return NULL;

  clip_node = gsk_rounded_clip_node_new (node, bounds);
  gsk_render_node_set_name (clip_node, name);

  gsk_render_node_unref (node);
  g_slice_free (GskRoundedRect, bounds);

  return clip_node;
}

void
gtk_snapshot_push_rounded_clip (GtkSnapshot          *snapshot,
                                const GskRoundedRect *bounds,
                                const char           *name,
                                ...)
{
  GskRoundedRect *real_bounds;
  cairo_region_t *clip;
  cairo_rectangle_int_t rect;
  char *str;

  real_bounds = g_slice_new (GskRoundedRect);
  gsk_rounded_rect_init_copy (real_bounds, bounds);
  gsk_rounded_rect_offset (real_bounds, snapshot->state->translate_x, snapshot->state->translate_y);

  if (name)
    {
      va_list args;

      va_start (args, name);
      str = g_strdup_vprintf (name, args);
      va_end (args);
    }
  else
    str = NULL;
  
  rectangle_init_from_graphene (&rect, &real_bounds->bounds);
  if (snapshot->state->clip_region)
    {
      clip = cairo_region_copy (snapshot->state->clip_region);
      cairo_region_intersect_rectangle (clip, &rect);
    }
  else
    {
      clip = cairo_region_create_rectangle (&rect);
    }
  snapshot->state = gtk_snapshot_state_new (snapshot->state,
                                            str,
                                            clip,
                                            snapshot->state->translate_x,
                                            snapshot->state->translate_y,
                                            gtk_snapshot_collect_rounded_clip,
                                            real_bounds);

  cairo_region_destroy (clip);
}

Benjamin Otte's avatar
Benjamin Otte committed
475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 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
typedef struct {
  gsize n_shadows;
  GskShadow shadows[0];
} Shadow;

static GskRenderNode *
gtk_snapshot_collect_shadow (GskRenderNode **nodes,
                             guint           n_nodes,
                             const char     *name,
                             gpointer        data)
{
  Shadow *shadow = data;
  GskRenderNode *node, *shadow_node;

  node = gtk_snapshot_collect_default (nodes, n_nodes, name, NULL);
  if (node == NULL)
    return NULL;

  shadow_node = gsk_shadow_node_new (node, shadow->shadows, shadow->n_shadows);
  gsk_render_node_set_name (shadow_node, name);

  gsk_render_node_unref (node);
  g_free (shadow);

  return shadow_node;
}

void
gtk_snapshot_push_shadow (GtkSnapshot            *snapshot,
                          const GskShadow        *shadow,
                          gsize                   n_shadows,
                          const char             *name,
                          ...)
{
  Shadow *real_shadow;
  char *str;

  real_shadow = g_malloc (sizeof (GskShadow) * n_shadows + sizeof (GskShadow));
  real_shadow->n_shadows = n_shadows;
  memcpy (real_shadow->shadows, shadow, sizeof (GskShadow) * n_shadows);

  if (name)
    {
      va_list args;

      va_start (args, name);
      str = g_strdup_vprintf (name, args);
      va_end (args);
    }
  else
    str = NULL;
  
  snapshot->state = gtk_snapshot_state_new (snapshot->state,
                                            str,
                                            snapshot->state->clip_region,
                                            snapshot->state->translate_x,
                                            snapshot->state->translate_y,
                                            gtk_snapshot_collect_shadow,
                                            real_shadow);
}

Matthias Clasen's avatar
Matthias Clasen committed
536 537 538 539 540 541 542
/**
 * gtk_snapshot_pop:
 * @snapshot: a #GtkSnapshot
 *
 * Removes the top element from the stack of render nodes,
 * making the node underneath the current node again.
 *
543 544 545 546
 * Returns: (transfer full) (allow none): A #GskRenderNode for
 *     the contents that were rendered to @snapshot since
 *     the corresponding gtk_snapshot_push() call
 *
Matthias Clasen's avatar
Matthias Clasen committed
547 548
 * Since: 3.90
 */
549
GskRenderNode *
550
gtk_snapshot_pop (GtkSnapshot *snapshot)
551
{
552
  GtkSnapshotState *state;
553
  GskRenderNode *node;
554 555

  if (snapshot->state == NULL)
556 557
    {
      g_warning ("Too many gtk_snapshot_pop() calls.");
558
      return NULL;
559 560
    }

561 562
  state = snapshot->state;
  snapshot->state = state->parent;
563

564 565 566 567
  node = state->collect_func ((GskRenderNode **) state->nodes->pdata,
                              state->nodes->len,
                              state->name,
                              state->collect_data);
568

569
  gtk_snapshot_state_free (state);
570 571

  return node;
572 573
}

574 575 576 577 578 579 580 581 582 583 584 585 586 587 588
/**
 * gtk_snapshot_pop_and_append:
 * @snapshot: a #GtkSnapshot
 *
 * Removes the top element from the stack of render nodes,
 * and appends it to the node underneath it.
 *
 * Since: 3.90
 */
void
gtk_snapshot_pop_and_append (GtkSnapshot *snapshot)
{
  GskRenderNode *node;

  node = gtk_snapshot_pop (snapshot);
589 590 591 592 593
  if (node)
    {
      gtk_snapshot_append_node (snapshot, node);
      gsk_render_node_unref (node);
    }
594 595
}

Matthias Clasen's avatar
Matthias Clasen committed
596 597 598 599 600 601 602
/**
 * gtk_snapshot_get_renderer:
 * @snapshot: a #GtkSnapshot
 *
 * Obtains the #GskRenderer that this snapshot will be
 * rendered with.
 *
603
 * Returns: (transfer none): the #GskRenderer
Matthias Clasen's avatar
Matthias Clasen committed
604 605 606
 *
 * Since: 3.90
 */
607
GskRenderer *
608
gtk_snapshot_get_renderer (const GtkSnapshot *snapshot)
609
{
610
  return snapshot->renderer;
611 612
}

Matthias Clasen's avatar
Matthias Clasen committed
613
/**
614 615 616 617
 * gtk_snapshot_translate_2d:
 * @snapshot: a $GtkSnapshot
 * @x: horizontal translation
 * @y: vertical translation
Matthias Clasen's avatar
Matthias Clasen committed
618
 *
619
 * Appends a translation by (@x, @y) to the current transformation.
Matthias Clasen's avatar
Matthias Clasen committed
620 621 622
 *
 * Since: 3.90
 */
623
void
624 625 626
gtk_snapshot_translate_2d (GtkSnapshot *snapshot,
                           int          x,
                           int          y)
627
{
628 629
  snapshot->state->translate_x += x;
  snapshot->state->translate_y += y;
630 631
}

Matthias Clasen's avatar
Matthias Clasen committed
632
/**
633
 * gtk_snapshot_get_offset:
Matthias Clasen's avatar
Matthias Clasen committed
634
 * @snapshot: a #GtkSnapshot
635 636
 * @x: (out allow-none): return location for x offset
 * @y: (out allow-none): return location for y offset
Matthias Clasen's avatar
Matthias Clasen committed
637
 *
638 639
 * Queries the offset managed by @snapshot. This offset is the
 * accumulated sum of calls to gtk_snapshot_translate_2d().
Matthias Clasen's avatar
Matthias Clasen committed
640
 *
641 642 643
 * Use this offset to determine how to offset nodes that you
 * manually add to the snapshot using
 * gtk_snapshot_append_node().
Matthias Clasen's avatar
Matthias Clasen committed
644
 *
645 646 647 648
 * Note that other functions that add nodes for you, such as
 * gtk_snapshot_append_cairo_node() will add this offset for
 * you.
 **/
649
void
650 651 652
gtk_snapshot_get_offset (GtkSnapshot *snapshot,
                         double      *x,
                         double      *y)
653
{
654 655
  if (x)
    *x = snapshot->state->translate_x;
656

657 658
  if (y)
    *y = snapshot->state->translate_y;
659 660
}

Matthias Clasen's avatar
Matthias Clasen committed
661 662 663 664 665 666 667 668 669 670
/**
 * gtk_snapshot_append_node:
 * @snapshot: a #GtkSnapshot
 * @node: a #GskRenderNode
 *
 * Appends @node to the current render node of @snapshot,
 * without changing the current node. If @snapshot does
 * not have a current node yet, @node will become the
 * initial node.
 */
671
void
672
gtk_snapshot_append_node (GtkSnapshot   *snapshot,
673 674
                          GskRenderNode *node)
{
675
  g_return_if_fail (snapshot != NULL);
676 677
  g_return_if_fail (GSK_IS_RENDER_NODE (node));

678 679
  if (snapshot->state)
    {
680
      g_ptr_array_add (snapshot->state->nodes, gsk_render_node_ref (node));
681
    }
682 683
  else
    {
684
      g_critical ("Tried appending a node to an already finished snapshot.");
685 686 687
    }
}

Matthias Clasen's avatar
Matthias Clasen committed
688 689
/**
 * gtk_snapshot_append_cairo_node:
690
 * @snapshot: a #GtkSnapshot
Matthias Clasen's avatar
Matthias Clasen committed
691
 * @bounds: the bounds for the new node
692
 * @name: (transfer none): a printf() style format string for the name for the new node
Matthias Clasen's avatar
Matthias Clasen committed
693 694 695 696 697 698 699 700 701 702
 * @...: arguments to insert into the format string
 *
 * Creates a new render node and appends it to the current render
 * node of @snapshot, without changing the current node.
 *
 * Returns: a cairo_t suitable for drawing the contents of the newly
 *     created render node
 *
 * Since: 3.90
 */
703
cairo_t *
704
gtk_snapshot_append_cairo_node (GtkSnapshot           *snapshot,
705 706 707 708 709
                                const graphene_rect_t *bounds,
                                const char            *name,
                                ...)
{
  GskRenderNode *node;
710 711
  graphene_rect_t real_bounds;
  cairo_t *cr;
712

713
  g_return_val_if_fail (snapshot != NULL, NULL);
714 715
  g_return_val_if_fail (bounds != NULL, NULL);

716 717
  graphene_rect_offset_r (bounds, snapshot->state->translate_x, snapshot->state->translate_y, &real_bounds);
  node = gsk_cairo_node_new (&real_bounds);
718 719 720 721 722 723 724 725 726 727 728 729 730 731 732

  if (name)
    {
      va_list args;
      char *str;

      va_start (args, name);
      str = g_strdup_vprintf (name, args);
      va_end (args);

      gsk_render_node_set_name (node, str);

      g_free (str);
    }

733
  gtk_snapshot_append_node (snapshot, node);
734 735
  gsk_render_node_unref (node);

736 737 738 739 740
  cr = gsk_cairo_node_get_draw_context (node, snapshot->renderer);

  cairo_translate (cr, snapshot->state->translate_x, snapshot->state->translate_y);

  return cr;
741 742
}

743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836
/**
 * gtk_snapshot_append_texture_node:
 * @snapshot: a #GtkSnapshot
 * @texture: the #GskTexture to render
 * @bounds: the bounds for the new node
 * @name: (transfer none): a printf() style format string for the name for the new node
 * @...: arguments to insert into the format string
 *
 * Creates a new render node drawing the @texture into the given @bounds and appends it
 * to the current render node of @snapshot.
 **/
void
gtk_snapshot_append_texture_node (GtkSnapshot            *snapshot,
                                  GskTexture             *texture,
                                  const graphene_rect_t  *bounds,
                                  const char             *name,
                                  ...)
{
  GskRenderNode *node;
  graphene_rect_t real_bounds;

  g_return_if_fail (snapshot != NULL);
  g_return_if_fail (GSK_IS_TEXTURE (texture));
  g_return_if_fail (bounds != NULL);

  graphene_rect_offset_r (bounds, snapshot->state->translate_x, snapshot->state->translate_y, &real_bounds);
  node = gsk_texture_node_new (texture, &real_bounds);

  if (name)
    {
      va_list args;
      char *str;

      va_start (args, name);
      str = g_strdup_vprintf (name, args);
      va_end (args);

      gsk_render_node_set_name (node, str);

      g_free (str);
    }

  gtk_snapshot_append_node (snapshot, node);
  gsk_render_node_unref (node);
}

/**
 * gtk_snapshot_append_color_node:
 * @snapshot: a #GtkSnapshot
 * @color: the #GdkRGBA to draw
 * @bounds: the bounds for the new node
 * @name: (transfer none): a printf() style format string for the name for the new node
 * @...: arguments to insert into the format string
 *
 * Creates a new render node drawing the @color into the given @bounds and appends it
 * to the current render node of @snapshot.
 *
 * You should try to avoid calling this function if @color is transparent.
 **/
void
gtk_snapshot_append_color_node (GtkSnapshot           *snapshot,
                                const GdkRGBA         *color,
                                const graphene_rect_t *bounds,
                                const char            *name,
                                ...)
{
  GskRenderNode *node;
  graphene_rect_t real_bounds;

  g_return_if_fail (snapshot != NULL);
  g_return_if_fail (color != NULL);
  g_return_if_fail (bounds != NULL);

  graphene_rect_offset_r (bounds, snapshot->state->translate_x, snapshot->state->translate_y, &real_bounds);
  node = gsk_color_node_new (color, &real_bounds);

  if (name)
    {
      va_list args;
      char *str;

      va_start (args, name);
      str = g_strdup_vprintf (name, args);
      va_end (args);

      gsk_render_node_set_name (node, str);

      g_free (str);
    }

  gtk_snapshot_append_node (snapshot, node);
  gsk_render_node_unref (node);
}

Matthias Clasen's avatar
Matthias Clasen committed
837 838 839 840 841 842 843 844 845 846 847
/**
 * gtk_snapshot_clips_rect:
 * @snapshot: a #GtkSnapshot
 * @bounds: a rectangle
 *
 * Tests whether the rectangle is entirely outside the clip region of @snapshot.
 *
 * Returns: %TRUE is @bounds is entirely outside the clip region
 *
 * Since: 3.90
 */
848 849 850 851
gboolean
gtk_snapshot_clips_rect (GtkSnapshot           *snapshot,
                         const graphene_rect_t *bounds)
{
852
  graphene_rect_t offset_bounds;
853 854
  cairo_rectangle_int_t rect;

855 856 857
  if (snapshot->state->clip_region == NULL)
    return FALSE;

858 859
  graphene_rect_offset_r (bounds, snapshot->state->translate_x, snapshot->state->translate_y, &offset_bounds);
  rectangle_init_from_graphene (&rect, &offset_bounds);
860

861
  return cairo_region_contains_rectangle (snapshot->state->clip_region, &rect) == CAIRO_REGION_OVERLAP_OUT;
862 863
}

Matthias Clasen's avatar
Matthias Clasen committed
864 865
/**
 * gtk_snapshot_render_background:
866
 * @snapshot: a #GtkSnapshot
Matthias Clasen's avatar
Matthias Clasen committed
867 868 869 870 871 872 873 874 875 876 877 878
 * @context: the #GtkStyleContext to use
 * @x: X origin of the rectangle
 * @y: Y origin of the rectangle
 * @width: rectangle width
 * @height: rectangle height
 *
 * Creates a render node for the CSS background according to @context,
 * and appends it to the current node of @snapshot, without changing
 * the current node.
 *
 * Since: 3.90
 */
879
void
880
gtk_snapshot_render_background (GtkSnapshot     *snapshot,
881 882 883 884 885 886
                                GtkStyleContext *context,
                                gdouble          x,
                                gdouble          y,
                                gdouble          width,
                                gdouble          height)
{
887
  g_return_if_fail (snapshot != NULL);
888 889
  g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));

890
  gtk_snapshot_translate_2d (snapshot, x, y);
891
  gtk_css_style_snapshot_background (gtk_style_context_lookup_style (context),
892
                                     snapshot,
893
                                     width, height);
894
  gtk_snapshot_translate_2d (snapshot, -x, -y);
895 896
}

897 898 899 900 901 902 903 904 905 906 907 908 909 910 911
/**
 * gtk_snapshot_render_frame:
 * @snapshot: a #GtkSnapshot
 * @context: the #GtkStyleContext to use
 * @x: X origin of the rectangle
 * @y: Y origin of the rectangle
 * @width: rectangle width
 * @height: rectangle height
 *
 * Creates a render node for the CSS border according to @context,
 * and appends it to the current node of @snapshot, without changing
 * the current node.
 *
 * Since: 3.90
 */
912
void
913
gtk_snapshot_render_frame (GtkSnapshot     *snapshot,
914 915 916 917 918 919
                           GtkStyleContext *context,
                           gdouble          x,
                           gdouble          y,
                           gdouble          width,
                           gdouble          height)
{
920
  g_return_if_fail (snapshot != NULL);
921 922
  g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));

923
  gtk_snapshot_translate_2d (snapshot, x, y);
924
  gtk_css_style_snapshot_border (gtk_style_context_lookup_style (context),
925
                                 snapshot,
926
                                 width, height);
927
  gtk_snapshot_translate_2d (snapshot, -x, -y);
928
}
929

Matthias Clasen's avatar
Matthias Clasen committed
930 931
/**
 * gtk_snapshot_render_focus:
932
 * @snapshot: a #GtkSnapshot
Matthias Clasen's avatar
Matthias Clasen committed
933 934 935 936 937 938 939 940 941 942 943 944
 * @context: the #GtkStyleContext to use
 * @x: X origin of the rectangle
 * @y: Y origin of the rectangle
 * @width: rectangle width
 * @height: rectangle height
 *
 * Creates a render node for the focus outline according to @context,
 * and appends it to the current node of @snapshot, without changing
 * the current node.
 *
 * Since: 3.90
 */
945
void
946
gtk_snapshot_render_focus (GtkSnapshot     *snapshot,
947 948 949 950 951 952
                           GtkStyleContext *context,
                           gdouble          x,
                           gdouble          y,
                           gdouble          width,
                           gdouble          height)
{
953
  g_return_if_fail (snapshot != NULL);
954 955
  g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));

956
  gtk_snapshot_translate_2d (snapshot, x, y);
957
  gtk_css_style_snapshot_outline (gtk_style_context_lookup_style (context),
958
                                  snapshot,
959
                                  width, height);
960
  gtk_snapshot_translate_2d (snapshot, -x, -y);
961 962
}

Matthias Clasen's avatar
Matthias Clasen committed
963 964
/**
 * gtk_snapshot_render_layout:
965
 * @snapshot: a #GtkSnapshot
Matthias Clasen's avatar
Matthias Clasen committed
966 967 968 969 970 971 972 973 974 975 976
 * @context: the #GtkStyleContext to use
 * @x: X origin of the rectangle
 * @y: Y origin of the rectangle
 * @layout: the #PangoLayout to render
 *
 * Creates a render node for rendering @layout according to the style
 * information in @context, and appends it to the current node of @snapshot,
 * without changing the current node.
 *
 * Since: 3.90
 */
977
void
978
gtk_snapshot_render_layout (GtkSnapshot     *snapshot,
979 980 981 982 983 984 985 986 987 988 989 990
                            GtkStyleContext *context,
                            gdouble          x,
                            gdouble          y,
                            PangoLayout     *layout)
{
  const GdkRGBA *fg_color;
  graphene_rect_t bounds;
  GtkBorder shadow_extents;
  PangoRectangle ink_rect;
  GtkCssValue *shadow;
  cairo_t *cr;

991
  g_return_if_fail (snapshot != NULL);
992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004
  g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
  g_return_if_fail (PANGO_IS_LAYOUT (layout));

  fg_color = _gtk_css_rgba_value_get_rgba (_gtk_style_context_peek_property (context, GTK_CSS_PROPERTY_COLOR));
  shadow = _gtk_style_context_peek_property (context, GTK_CSS_PROPERTY_TEXT_SHADOW);
  pango_layout_get_pixel_extents (layout, &ink_rect, NULL);
  _gtk_css_shadows_value_get_extents (shadow, &shadow_extents);
  graphene_rect_init (&bounds,
                      ink_rect.x - shadow_extents.left,
                      ink_rect.y - shadow_extents.top,
                      ink_rect.width + shadow_extents.left + shadow_extents.right,
                      ink_rect.height + shadow_extents.top + shadow_extents.bottom);

1005
  gtk_snapshot_translate_2d (snapshot, x, y);
1006

1007
  cr = gtk_snapshot_append_cairo_node (snapshot, &bounds, "Text<%dchars>", pango_layout_get_character_count (layout));
1008 1009 1010 1011 1012 1013 1014

  _gtk_css_shadows_value_paint_layout (shadow, cr, layout);

  gdk_cairo_set_source_rgba (cr, fg_color);
  pango_cairo_show_layout (cr, layout);

  cairo_destroy (cr);
1015
  gtk_snapshot_translate_2d (snapshot, -x, -y);
1016 1017
}

Matthias Clasen's avatar
Matthias Clasen committed
1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031
/**
 * gtk_snapshot_render_icon:
 * @snapshot: a #GtkSnapshot
 * @context: the #GtkStyleContext to use
 * @pixbuf: the #GdkPixbuf to render
 * @x: X origin of the rectangle
 * @y: Y origin of the rectangle
 *
 * Creates a render node for rendering @pixbuf according to the style
 * information in @context, and appends it to the current node of @snapshot,
 * without changing the current node.
 *
 * Since: 3.90
 */
1032 1033 1034 1035 1036 1037 1038 1039 1040
void
gtk_snapshot_render_icon (GtkSnapshot     *snapshot,
                          GtkStyleContext *context,
                          GdkPixbuf       *pixbuf,
                          gdouble          x,
                          gdouble          y)
{
  GskTexture *texture;

Benjamin Otte's avatar
Benjamin Otte committed
1041
  texture = gsk_texture_new_for_pixbuf (pixbuf);
1042
  gtk_snapshot_translate_2d (snapshot, x, y);
1043 1044
  gtk_css_style_snapshot_icon_texture (gtk_style_context_lookup_style (context),
                                       snapshot,
1045 1046
                                       texture,
                                       1);
1047 1048 1049 1050
  gtk_snapshot_translate_2d (snapshot, -x, -y);
  gsk_texture_unref (texture);
}