gtkcssgadget.c 36.3 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 23 24 25 26 27 28 29 30 31 32
/*
 * Copyright © 2015 Red Hat Inc.
 *
 * 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.1 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/>.
 *
 * Authors: Benjamin Otte <otte@gnome.org>
 */

#include "config.h"

#include "gtkcssgadgetprivate.h"

#include <math.h>

#include "gtkcssnumbervalueprivate.h"
#include "gtkcssshadowsvalueprivate.h"
#include "gtkcssstyleprivate.h"
#include "gtkcssstylepropertyprivate.h"
#include "gtkcsswidgetnodeprivate.h"
#include "gtkrenderbackgroundprivate.h"
#include "gtkrenderborderprivate.h"
33
#include "gtkdebug.h"
34
#include "gtkprivate.h"
35

36 37 38 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
/*
 * Gadgets are 'next-generation widgets' - they combine a CSS node
 * for style matching with geometry management and drawing. Each gadget
 * corresponds to 'CSS box'. Compared to traditional widgets, they are more
 * like building blocks - a typical GTK+ widget will have multiple gadgets,
 * for example a check button has its main gadget, and sub-gadgets for
 * the checkmark and the text.
 *
 * Gadgets are not themselves hierarchically organized, but it is common
 * to have a 'main' gadget, which gets used by the widgets size_allocate,
 * get_preferred_width, etc. and draw callbacks, and which in turn calls out
 * to the sub-gadgets. This call tree might extend further if there are
 * sub-sub-gadgets that a allocated relative to sub-gadgets. In typical
 * situations, the callback chain will reflect the tree structure of the
 * gadgets CSS nodes.
 *
 * Geometry management - Gadgets implement much of the CSS box model for you:
 * margins, border, padding, shadows, min-width/height are all applied automatically.
 *
 * Drawing - Gadgets implement standardized CSS drawing for you: background,
 * shadows and border are drawn before any custom drawing, and the focus outline
 * is (optionally) drawn afterwards.
 *
 * Invalidation - Gadgets sit 'between' widgets and CSS nodes, and connect
 * to the nodes ::style-changed signal and trigger appropriate invalidations
 * on the widget side.
 */

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 97 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 123 124 125 126
typedef struct _GtkCssGadgetPrivate GtkCssGadgetPrivate;
struct _GtkCssGadgetPrivate {
  GtkCssNode    *node;
  GtkWidget     *owner;
  GtkAllocation  allocated_size;
  gint           allocated_baseline;
};

enum {
  PROP_0,
  PROP_NODE,
  PROP_OWNER,
  /* add more */
  NUM_PROPERTIES
};

static GParamSpec *properties[NUM_PROPERTIES];

G_DEFINE_ABSTRACT_TYPE_WITH_CODE (GtkCssGadget, gtk_css_gadget, G_TYPE_OBJECT,
                                  G_ADD_PRIVATE (GtkCssGadget))

static void
gtk_css_gadget_real_get_preferred_size (GtkCssGadget   *gadget,
                                        GtkOrientation  orientation,
                                        gint            for_size,
                                        gint           *minimum,
                                        gint           *natural,
                                        gint           *minimum_baseline,
                                        gint           *natural_baseline)
{
  *minimum = 0;
  *natural = 0;

  if (minimum_baseline)
    *minimum_baseline = 0;
  if (natural_baseline)
    *natural_baseline = 0;
}

static void
gtk_css_gadget_real_allocate (GtkCssGadget        *gadget,
                              const GtkAllocation *allocation,
                              int                  baseline,
                              GtkAllocation       *out_clip)
{
  *out_clip = *allocation;
}

static gboolean
gtk_css_gadget_real_draw (GtkCssGadget *gadget,
                          cairo_t      *cr,
                          int           x,
                          int           y,
                          int           width,
                          int           height)
{
  return FALSE;
}

static void
gtk_css_gadget_real_style_changed (GtkCssGadget      *gadget,
                                   GtkCssStyleChange *change)
{
127 128 129 130 131 132
  if (gtk_css_style_change_affects (change, GTK_CSS_AFFECTS_SIZE))
    gtk_css_gadget_queue_resize (gadget);
  else if (gtk_css_style_change_affects (change, GTK_CSS_AFFECTS_CLIP))
    gtk_css_gadget_queue_allocate (gadget);
  else if (gtk_css_style_change_affects (change, GTK_CSS_AFFECTS_REDRAW))
    gtk_css_gadget_queue_draw (gadget);
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 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
}

static void
gtk_css_gadget_get_property (GObject    *object,
                             guint       property_id,
                             GValue     *value,
                             GParamSpec *pspec)
{
  GtkCssGadgetPrivate *priv = gtk_css_gadget_get_instance_private (GTK_CSS_GADGET (object));

  switch (property_id)
  {
    case PROP_NODE:
      g_value_set_object (value, priv->node);
      break;

    case PROP_OWNER:
      g_value_set_object (value, priv->owner);
      break;

    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
      break;
  }
}

static void
gtk_css_gadget_node_style_changed_cb (GtkCssNode        *node,
                                      GtkCssStyleChange *change,
                                      GtkCssGadget      *gadget)
{
  GtkCssGadgetClass *klass = GTK_CSS_GADGET_GET_CLASS (gadget);

  klass->style_changed (gadget, change);
}

static gboolean
gtk_css_gadget_should_connect_style_changed (GtkCssNode *node)
{
  /* Delegate to WidgetClass->style_changed */
  if (GTK_IS_CSS_WIDGET_NODE (node))
    return FALSE;

  return TRUE;
}

static void
gtk_css_gadget_unset_node (GtkCssGadget *gadget)
{
  GtkCssGadgetPrivate *priv = gtk_css_gadget_get_instance_private (gadget);

  if (priv->node)
    {
      if (gtk_css_gadget_should_connect_style_changed (priv->node))
        {
        if (g_signal_handlers_disconnect_by_func (priv->node, gtk_css_gadget_node_style_changed_cb, gadget) != 1)
          {
            g_assert_not_reached ();
          }
        }
      g_object_unref (priv->node);
      priv->node = NULL;
    }
}

198
void
199 200 201 202 203
gtk_css_gadget_set_node (GtkCssGadget *gadget,
                         GtkCssNode   *node)
{
  GtkCssGadgetPrivate *priv = gtk_css_gadget_get_instance_private (gadget);

204 205
  gtk_css_gadget_unset_node (gadget);

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 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 278 279 280 281 282 283 284 285 286 287
  if (node != NULL)
    priv->node = g_object_ref (node);
  else
    priv->node = gtk_css_node_new ();

  if (gtk_css_gadget_should_connect_style_changed (priv->node))
    {
      g_signal_connect_after (priv->node,
                              "style-changed",
                              G_CALLBACK (gtk_css_gadget_node_style_changed_cb),
                              gadget);
    }
}

static void
gtk_css_gadget_set_property (GObject      *object,
                             guint         property_id,
                             const GValue *value,
                             GParamSpec   *pspec)
{
  GtkCssGadget *gadget = GTK_CSS_GADGET (object);
  GtkCssGadgetPrivate *priv = gtk_css_gadget_get_instance_private (gadget);

  switch (property_id)
  {
    case PROP_NODE:
      gtk_css_gadget_set_node (gadget, g_value_get_object (value));
      break;

    case PROP_OWNER:
      priv->owner = g_value_get_object (value);
      break;

    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
      break;
  }
}


static void
gtk_css_gadget_finalize (GObject *object)
{
  GtkCssGadget *gadget = GTK_CSS_GADGET (object);

  gtk_css_gadget_unset_node (gadget);

  G_OBJECT_CLASS (gtk_css_gadget_parent_class)->finalize (object);
}

static void
gtk_css_gadget_class_init (GtkCssGadgetClass *klass)
{
  GObjectClass *object_class = G_OBJECT_CLASS (klass);

  object_class->get_property = gtk_css_gadget_get_property;
  object_class->set_property = gtk_css_gadget_set_property;
  object_class->finalize = gtk_css_gadget_finalize;

  klass->get_preferred_size = gtk_css_gadget_real_get_preferred_size;
  klass->allocate = gtk_css_gadget_real_allocate;
  klass->draw = gtk_css_gadget_real_draw;
  klass->style_changed = gtk_css_gadget_real_style_changed;

  properties[PROP_NODE] = g_param_spec_object ("node", "Node",
                                               "CSS node",
                                               GTK_TYPE_CSS_NODE,
                                               G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY |
                                               G_PARAM_EXPLICIT_NOTIFY | G_PARAM_STATIC_STRINGS);
  properties[PROP_OWNER] = g_param_spec_object ("owner", "Owner",
                                                "Widget that created and owns this gadget",
                                                GTK_TYPE_WIDGET,
                                                G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY |
                                                G_PARAM_EXPLICIT_NOTIFY | G_PARAM_STATIC_STRINGS);


  g_object_class_install_properties (object_class, NUM_PROPERTIES, properties);
}

static void
gtk_css_gadget_init (GtkCssGadget *gadget)
{
288
  GtkCssGadgetPrivate *priv = gtk_css_gadget_get_instance_private (gadget);
289

290 291 292
  priv->allocated_size.width = -1;
  priv->allocated_size.height = -1;
  priv->allocated_baseline = -1;
293 294
}

295 296 297 298 299 300 301 302
/**
 * gtk_css_gadget_get_node:
 * @gadget: a #GtkCssGadget
 *
 * Get the CSS node for this gadget.
 *
  * Returns: (transfer none):  the CSS node
 */
303 304 305 306 307 308 309 310
GtkCssNode *
gtk_css_gadget_get_node (GtkCssGadget *gadget)
{
  GtkCssGadgetPrivate *priv = gtk_css_gadget_get_instance_private (gadget);

  return priv->node;
}

311 312 313 314 315 316 317 318
/**
 * gtk_css_gadget_get_style:
 * @gadget: a #GtkCssGadget
 *
 * Get the CSS style for this gadget.
 *
 * Returns: (transfer none):  the CSS style
 */
319 320 321 322 323 324 325 326
GtkCssStyle *
gtk_css_gadget_get_style (GtkCssGadget *gadget)
{
  GtkCssGadgetPrivate *priv = gtk_css_gadget_get_instance_private (gadget);

  return gtk_css_node_get_style (priv->node);
}

327
/**
328
 * gtk_css_gadget_get_owner:
329 330 331 332 333 334
 * @gadget: a #GtkCssGadget
 *
 * Get the widget to which this gadget belongs.
 *
 * Returns: (transfer none):  the widget to which @gadget belongs
 */
335 336 337 338 339 340 341 342
GtkWidget *
gtk_css_gadget_get_owner (GtkCssGadget *gadget)
{
  GtkCssGadgetPrivate *priv = gtk_css_gadget_get_instance_private (gadget);

  return priv->owner;
}

343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359
void
gtk_css_gadget_set_visible (GtkCssGadget *gadget,
                            gboolean      visible)
{
  GtkCssGadgetPrivate *priv = gtk_css_gadget_get_instance_private (gadget);

  gtk_css_node_set_visible (priv->node, visible);
}

gboolean
gtk_css_gadget_get_visible (GtkCssGadget *gadget)
{
  GtkCssGadgetPrivate *priv = gtk_css_gadget_get_instance_private (gadget);

  return gtk_css_node_get_visible (priv->node);
}

360 361 362 363 364 365 366
/**
 * gtk_css_gadget_add_class:
 * @gadget: a #GtkCssGadget
 * @name: class name to use in CSS matching
 *
 * Adds a style class to the gadgets CSS node.
 */
367 368 369 370 371 372 373 374 375 376 377 378
void
gtk_css_gadget_add_class (GtkCssGadget *gadget,
                          const char   *name)
{
  GtkCssGadgetPrivate *priv = gtk_css_gadget_get_instance_private (gadget);
  GQuark quark;

  quark = g_quark_from_string (name);

  gtk_css_node_add_class (priv->node, quark);
}

379 380 381 382 383 384 385
/**
 * gtk_css_gadget_remove_class:
 * @gadget: a #GtkCssGadget
 * @name: class name
 *
 * Removes a style class from the gadgets CSS node.
 */
386 387 388 389 390 391 392 393 394 395 396 397 398 399
void
gtk_css_gadget_remove_class (GtkCssGadget *gadget,
                             const char   *name)
{
  GtkCssGadgetPrivate *priv = gtk_css_gadget_get_instance_private (gadget);
  GQuark quark;

  quark = g_quark_try_string (name);
  if (quark == 0)
    return;

  gtk_css_node_remove_class (priv->node, quark);
}

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
/**
 * gtk_css_gadget_set_state:
 * @gadget: a #GtkCssGadget
 * @state: The new state
 *
 * Sets the state of the gadget's CSS node.
 */
void
gtk_css_gadget_set_state (GtkCssGadget  *gadget,
                          GtkStateFlags  state)
{
  GtkCssGadgetPrivate *priv = gtk_css_gadget_get_instance_private (gadget);

  gtk_css_node_set_state (priv->node, state);
}

/**
 * gtk_css_gadget_add_state:
 * @gadget: a #GtkCssGadget
 * @state: The state to add
 *
 * Adds the given states to the states of gadget's CSS node. Other states
 * will be kept as they are.
 */
void
gtk_css_gadget_add_state (GtkCssGadget  *gadget,
                          GtkStateFlags  state)
{
  GtkCssGadgetPrivate *priv = gtk_css_gadget_get_instance_private (gadget);

  gtk_css_node_set_state (priv->node, gtk_css_node_get_state (priv->node) | state);
}

/**
 * gtk_css_gadget_remove_state:
 * @gadget: a #GtkCssGadget
 * @state: The state to remove
 *
 * Adds the given states to the states of gadget's CSS node. Other states
 * will be kept as they are.
 */
void
gtk_css_gadget_remove_state (GtkCssGadget  *gadget,
                             GtkStateFlags  state)
{
  GtkCssGadgetPrivate *priv = gtk_css_gadget_get_instance_private (gadget);

  gtk_css_node_set_state (priv->node, gtk_css_node_get_state (priv->node) & ~state);
}

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 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491
static gint
get_number (GtkCssStyle *style,
            guint        property)
{
  double d = _gtk_css_number_value_get (gtk_css_style_get_value (style, property), 100);

  if (d < 1)
    return ceil (d);
  else
    return floor (d);
}

static void
get_box_margin (GtkCssStyle *style,
                GtkBorder   *margin)
{
  margin->top = get_number (style, GTK_CSS_PROPERTY_MARGIN_TOP);
  margin->left = get_number (style, GTK_CSS_PROPERTY_MARGIN_LEFT);
  margin->bottom = get_number (style, GTK_CSS_PROPERTY_MARGIN_BOTTOM);
  margin->right = get_number (style, GTK_CSS_PROPERTY_MARGIN_RIGHT);
}

static void
get_box_border (GtkCssStyle *style,
                GtkBorder   *border)
{
  border->top = get_number (style, GTK_CSS_PROPERTY_BORDER_TOP_WIDTH);
  border->left = get_number (style, GTK_CSS_PROPERTY_BORDER_LEFT_WIDTH);
  border->bottom = get_number (style, GTK_CSS_PROPERTY_BORDER_BOTTOM_WIDTH);
  border->right = get_number (style, GTK_CSS_PROPERTY_BORDER_RIGHT_WIDTH);
}

static void
get_box_padding (GtkCssStyle *style,
                 GtkBorder   *border)
{
  border->top = get_number (style, GTK_CSS_PROPERTY_PADDING_TOP);
  border->left = get_number (style, GTK_CSS_PROPERTY_PADDING_LEFT);
  border->bottom = get_number (style, GTK_CSS_PROPERTY_PADDING_BOTTOM);
  border->right = get_number (style, GTK_CSS_PROPERTY_PADDING_RIGHT);
}

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
static gboolean
allocation_contains_point (GtkAllocation *allocation,
                           gint           x,
                           gint           y)
{
  return (x >= allocation->x) &&
    (x < allocation->x + allocation->width) &&
    (y >= allocation->y) &&
    (y < allocation->y + allocation->height);
}

static void
shift_allocation (GtkCssGadget  *gadget,
                  GtkAllocation *allocation)
{
  GtkCssGadgetPrivate *priv = gtk_css_gadget_get_instance_private (gadget);

  if (priv->owner && !gtk_widget_get_has_window (priv->owner))
    {
      GtkAllocation widget_alloc;
      gtk_widget_get_allocation (priv->owner, &widget_alloc);
      allocation->x -= widget_alloc.x;
      allocation->y -= widget_alloc.y;
    }
}

/**
 * gtk_css_gadget_margin_box_contains_point:
 * @gadget: the #GtkCssGadget being tested
 * @x: X coordinate of the testing point
 * @y: Y coordinate of the testing point
 *
 * Checks whether the point at the provided coordinates is contained within the
 *   margin box of the gadget. The (X, Y) are relative to the gadget
 *   origin.
 *
 * Returns: %TRUE if the point at (X, Y) is contained within the margin
 *   box of the @gadget.
 */
gboolean
gtk_css_gadget_margin_box_contains_point (GtkCssGadget *gadget,
                                          int           x,
                                          int           y)
{
Cosimo Cecchi's avatar
Cosimo Cecchi committed
536 537 538
  GtkAllocation margin_box;
  gtk_css_gadget_get_margin_box (gadget, &margin_box);
  return allocation_contains_point (&margin_box, x, y);
539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558
}

/**
 * gtk_css_gadget_border_box_contains_point:
 * @gadget: the #GtkCssGadget being tested
 * @x: X coordinate of the testing point
 * @y: Y coordinate of the testing point
 *
 * Checks whether the point at the provided coordinates is contained within the
 *   border box of the gadget. The (X, Y) are relative to the gadget
 *   origin.
 *
 * Returns: %TRUE if the point at (X, Y) is contained within the border
 *   box of the @gadget.
 */
gboolean
gtk_css_gadget_border_box_contains_point (GtkCssGadget *gadget,
                                          int           x,
                                          int           y)
{
Cosimo Cecchi's avatar
Cosimo Cecchi committed
559 560 561
  GtkAllocation border_box;
  gtk_css_gadget_get_border_box (gadget, &border_box);
  return allocation_contains_point (&border_box, x, y);
562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581
}

/**
 * gtk_css_gadget_content_box_contains_point:
 * @gadget: the #GtkCssGadget being tested
 * @x: X coordinate of the testing point
 * @y: Y coordinate of the testing point
 *
 * Checks whether the point at the provided coordinates is contained within the
 *   content box of the gadget. The (X, Y) are relative to the gadget
 *   origin.
 *
 * Returns: %TRUE if the point at (X, Y) is contained within the content
 *   box of the @gadget.
 */
gboolean
gtk_css_gadget_content_box_contains_point (GtkCssGadget *gadget,
                                           int           x,
                                           int           y)
{
Cosimo Cecchi's avatar
Cosimo Cecchi committed
582 583 584
  GtkAllocation content_box;
  gtk_css_gadget_get_content_box (gadget, &content_box);
  return allocation_contains_point (&content_box, x, y);
585 586
}

587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604
/**
 * gtk_css_gadget_get_preferred_size:
 * @gadget: the #GtkCssGadget whose size is requested
 * @orientation: whether a width (ie horizontal) or height (ie vertical) size is requested
 * @for_size: the available size in the opposite direction, or -1
 * @minimum: (nullable): return location for the minimum size
 * @natural: (nullable): return location for the natural size
 * @minimum_baseline: (nullable): return location for the baseline at minimum size
 * @natural_baseline: (nullable): return location for the baseline at natural size
 *
 * Gets the gadgets minimum and natural size (and, optionally, baseline)
 * in the given orientation for the specified size in the opposite direction.
 *
 * The returned values include CSS padding, border and margin in addition to the
 * gadgets content size, and respect the CSS min-with or min-height properties.
 *
 * The @for_size is assumed to include CSS padding, border and margins as well.
 */
605 606 607 608 609 610 611 612 613 614 615 616 617
void
gtk_css_gadget_get_preferred_size (GtkCssGadget   *gadget,
                                   GtkOrientation  orientation,
                                   gint            for_size,
                                   gint           *minimum,
                                   gint           *natural,
                                   gint           *minimum_baseline,
                                   gint           *natural_baseline)
{
  GtkCssStyle *style;
  GtkBorder margin, border, padding;
  int min_size, extra_size, extra_opposite, extra_baseline;
  int unused_minimum, unused_natural;
618
  int forced_minimum, forced_natural;
619
  int min_for_size;
620 621 622 623 624 625

  if (minimum == NULL)
    minimum = &unused_minimum;
  if (natural == NULL)
    natural = &unused_natural;

626 627 628 629 630 631 632 633 634 635 636
  if (!gtk_css_gadget_get_visible (gadget))
    {
      *minimum = 0;
      *natural = 0;
      if (minimum_baseline)
        *minimum_baseline = -1;
      if (natural_baseline)
        *natural_baseline = -1;
      return;
    }

637 638 639 640 641 642 643 644 645 646
  style = gtk_css_gadget_get_style (gadget);
  get_box_margin (style, &margin);
  get_box_border (style, &border);
  get_box_padding (style, &padding);
  if (orientation == GTK_ORIENTATION_HORIZONTAL)
    {
      extra_size = margin.left + margin.right + border.left + border.right + padding.left + padding.right;
      extra_opposite = margin.top + margin.bottom + border.top + border.bottom + padding.top + padding.bottom;
      extra_baseline = margin.left + border.left + padding.left;
      min_size = get_number (style, GTK_CSS_PROPERTY_MIN_WIDTH);
647
      min_for_size = get_number (style, GTK_CSS_PROPERTY_MIN_HEIGHT);
648 649 650 651 652 653 654
    }
  else
    {
      extra_size = margin.top + margin.bottom + border.top + border.bottom + padding.top + padding.bottom;
      extra_opposite = margin.left + margin.right + border.left + border.right + padding.left + padding.right;
      extra_baseline = margin.top + border.top + padding.top;
      min_size = get_number (style, GTK_CSS_PROPERTY_MIN_HEIGHT);
655
      min_for_size = get_number (style, GTK_CSS_PROPERTY_MIN_WIDTH);
656 657 658
    }

  if (for_size > -1)
659 660
    {
      if (for_size < min_for_size)
661 662 663 664 665
        g_warning ("for_size smaller than min-size (%d < %d) "
                   "while measuring gadget (node %s, owner %s)",
                   for_size, min_for_size,
                   gtk_css_node_get_name (gtk_css_gadget_get_node (gadget)),
                   G_OBJECT_TYPE_NAME (gtk_css_gadget_get_owner (gadget)));
666 667 668

      for_size = MAX (0, for_size - extra_opposite);
    }
669 670 671 672 673 674 675 676 677 678 679 680 681 682

  if (minimum_baseline)
    *minimum_baseline = -1;
  if (natural_baseline)
    *natural_baseline = -1;

  GTK_CSS_GADGET_GET_CLASS (gadget)->get_preferred_size (gadget,
                                                         orientation,
                                                         for_size,
                                                         minimum, natural,
                                                         minimum_baseline, natural_baseline);

  g_warn_if_fail (*minimum <= *natural);

683 684
  forced_minimum = MAX (*minimum, min_size);
  forced_natural = MAX (*natural, min_size);
685 686

  if (minimum_baseline && *minimum_baseline > -1)
687
    {
688
      *minimum_baseline += 0.5 * (forced_minimum - *minimum);
689 690
      *minimum_baseline = MAX (0, *minimum_baseline + extra_baseline);
    }
691
  if (natural_baseline && *natural_baseline > -1)
692
    {
693
      *natural_baseline += 0.5 * (forced_natural - *natural);
694 695 696 697 698 699
      *natural_baseline = MAX (0, *natural_baseline + extra_baseline);
    }

  *minimum = MAX (0, forced_minimum + extra_size);
  *natural = MAX (0, forced_natural + extra_size);

700 701
}

702 703 704 705 706 707 708 709 710 711 712 713 714 715
/**
 * gtk_css_gadget_allocate:
 * @gadget: the #GtkCssGadget to allocate
 * @allocation: the allocation
 * @baseline: the baseline for the allocation
 * @out_clip: (out): return location for the gadgets clip region
 *
 * Allocates the gadget.
 *
 * The @allocation is assumed to include CSS padding, border and margin.
 * The gadget content will be allocated a smaller area that excludes these.
 * The @out_clip includes the shadow extents of the gadget in addition to
 * any content clip.
 */
716 717 718 719 720 721 722
void
gtk_css_gadget_allocate (GtkCssGadget        *gadget,
                         const GtkAllocation *allocation,
                         int                  baseline,
                         GtkAllocation       *out_clip)
{
  GtkCssGadgetPrivate *priv = gtk_css_gadget_get_instance_private (gadget);
723
  GtkAllocation content_allocation;
724
  GtkAllocation tmp_clip;
725
  GtkAllocation content_clip = { 0, 0, 0, 0 };
726 727 728 729 730
  GtkBorder margin, border, padding, shadow, extents;
  GtkCssStyle *style;

  g_return_if_fail (out_clip != NULL);

731
  if (!gtk_css_gadget_get_visible (gadget))
732 733 734 735 736 737 738
    {
      out_clip->x = 0;
      out_clip->y = 0;
      out_clip->width = 0;
      out_clip->height = 0;
      return;
    }
739

740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755
  priv->allocated_size = *allocation;
  priv->allocated_baseline = baseline;

  style = gtk_css_gadget_get_style (gadget);
  get_box_margin (style, &margin);
  get_box_border (style, &border);
  get_box_padding (style, &padding);
  extents.top = margin.top + border.top + padding.top;
  extents.right = margin.right + border.right + padding.right;
  extents.bottom = margin.bottom + border.bottom + padding.bottom;
  extents.left = margin.left + border.left + padding.left;

  content_allocation.x = allocation->x + extents.left;
  content_allocation.y = allocation->y + extents.top;
  content_allocation.width = allocation->width - extents.left - extents.right;
  content_allocation.height = allocation->height - extents.top - extents.bottom;
756

757
  if (baseline >= 0)
758
    baseline -= extents.top;
759

760 761
  if (content_allocation.width < 0)
    {
762
      g_warning ("Negative content width %d (allocation %d, extents %dx%d) "
763
                 "while allocating gadget (node %s, owner %s)",
764 765
                 content_allocation.width, allocation->width,
                 extents.left, extents.right,
766 767
                 gtk_css_node_get_name (gtk_css_gadget_get_node (gadget)),
                 G_OBJECT_TYPE_NAME (gtk_css_gadget_get_owner (gadget)));
768 769 770 771 772
      content_allocation.width = 0;
    }

  if (content_allocation.height < 0)
    {
773
      g_warning ("Negative content height %d (allocation %d, extents %dx%d) "
774
                 "while allocating gadget (node %s, owner %s)",
775 776
                 content_allocation.height, allocation->height,
                 extents.top, extents.bottom,
777 778
                 gtk_css_node_get_name (gtk_css_gadget_get_node (gadget)),
                 G_OBJECT_TYPE_NAME (gtk_css_gadget_get_owner (gadget)));
779
      content_allocation.height = 0;
780
    }
781 782 783 784

  GTK_CSS_GADGET_GET_CLASS (gadget)->allocate (gadget, &content_allocation, baseline, &content_clip);

  _gtk_css_shadows_value_get_extents (gtk_css_style_get_value (style, GTK_CSS_PROPERTY_BOX_SHADOW), &shadow);
785

786 787
  out_clip->x = allocation->x + margin.left - shadow.left;
  out_clip->y = allocation->y + margin.top - shadow.top;
788 789
  out_clip->width = MAX (0, allocation->width - margin.left - margin.right + shadow.left + shadow.right);
  out_clip->height = MAX (0, allocation->height - margin.top - margin.bottom + shadow.top + shadow.bottom);
790

791 792
  if (content_clip.width > 0 && content_clip.height > 0)
    gdk_rectangle_union (&content_clip, out_clip, out_clip);
793 794 795 796 797 798 799 800

  if (gtk_css_style_render_outline_get_clip (style,
                                             allocation->x + margin.left,
                                             allocation->y + margin.top,
                                             allocation->width - margin.left - margin.right,
                                             allocation->height - margin.top - margin.bottom,
                                             &tmp_clip))
    gdk_rectangle_union (&tmp_clip, out_clip, out_clip);
801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821
}

/**
 * gtk_css_gadget_draw:
 * @gadget: The gadget to draw
 * @cr: The cairo context to draw to
 *
 * Will draw the gadget at the position allocated via
 * gtk_css_gadget_allocate(). It is your responsibility to make
 * sure that those 2 coordinate systems match.
 *
 * The drawing virtual function will be passed an untransformed @cr.
 * This is important because functions like
 * gtk_container_propagate_draw() depend on that.
 */
void
gtk_css_gadget_draw (GtkCssGadget *gadget,
                     cairo_t      *cr)
{
  GtkCssGadgetPrivate *priv = gtk_css_gadget_get_instance_private (gadget);
  GtkBorder margin, border, padding;
822
  gboolean draw_focus = FALSE;
823 824 825
  GtkCssStyle *style;
  int x, y, width, height;
  int contents_x, contents_y, contents_width, contents_height;
Cosimo Cecchi's avatar
Cosimo Cecchi committed
826
  GtkAllocation margin_box;
827

828 829 830
  if (!gtk_css_gadget_get_visible (gadget))
    return;

Cosimo Cecchi's avatar
Cosimo Cecchi committed
831
  gtk_css_gadget_get_margin_box (gadget, &margin_box);
832

Cosimo Cecchi's avatar
Cosimo Cecchi committed
833 834 835 836
  x = margin_box.x;
  y = margin_box.y;
  width = margin_box.width;
  height = margin_box.height;
837

838 839 840
  if (width < 0 || height < 0)
    {
      g_warning ("Drawing a gadget with negative dimensions. "
841 842 843
                 "Did you forget to allocate a size? (node %s owner %s)",
                 gtk_css_node_get_name (gtk_css_gadget_get_node (gadget)),
                 G_OBJECT_TYPE_NAME (gtk_css_gadget_get_owner (gadget)));
844 845 846 847 848 849
      x = 0;
      y = 0;
      width = gtk_widget_get_allocated_width (priv->owner);
      height = gtk_widget_get_allocated_height (priv->owner);
    }

850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875
  style = gtk_css_gadget_get_style (gadget);
  get_box_margin (style, &margin);
  get_box_border (style, &border);
  get_box_padding (style, &padding);

  gtk_css_style_render_background (style,
                                   cr,
                                   x + margin.left,
                                   y + margin.top,
                                   width - margin.left - margin.right,
                                   height - margin.top - margin.bottom,
                                   gtk_css_node_get_junction_sides (priv->node));
  gtk_css_style_render_border (style,
                               cr,
                               x + margin.left,
                               y + margin.top,
                               width - margin.left - margin.right,
                               height - margin.top - margin.bottom,
                               0,
                               gtk_css_node_get_junction_sides (priv->node));

  contents_x = x + margin.left + border.left + padding.left;
  contents_y = y + margin.top + border.top + padding.top;
  contents_width = width - margin.left - margin.right - border.left - border.right - padding.left - padding.right;
  contents_height = height - margin.top - margin.bottom - border.top - border.bottom - padding.top - padding.bottom;

876 877 878 879 880
  if (contents_width > 0 && contents_height > 0)
    draw_focus = GTK_CSS_GADGET_GET_CLASS (gadget)->draw (gadget,
                                                          cr,
                                                          contents_x, contents_y,
                                                          contents_width, contents_height);
881 882 883 884 885 886 887 888

  if (draw_focus)
    gtk_css_style_render_outline (style,
                                  cr,
                                  x + margin.left,
                                  y + margin.top,
                                  width - margin.left - margin.right,
                                  height - margin.top - margin.bottom);
889 890

#if G_ENABLE_DEBUG
891 892
  {
    GdkDisplay *display = gtk_widget_get_display (gtk_css_gadget_get_owner (gadget));
893 894
    GtkDebugFlag flags = gtk_get_display_debug_flags (display);
    if G_UNLIKELY (flags & GTK_DEBUG_LAYOUT)
895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915
      {
        cairo_save (cr);
        cairo_new_path (cr);
        cairo_rectangle (cr,
                         x + margin.left,
                         y + margin.top,
                         width - margin.left - margin.right,
                         height - margin.top - margin.bottom);
        cairo_set_line_width (cr, 1.0);
        cairo_set_source_rgba (cr, 0, 0, 1.0, 0.33);
        cairo_stroke (cr);
        cairo_rectangle (cr,
                         contents_x,
                         contents_y,
                         contents_width,
                         contents_height);
        cairo_set_line_width (cr, 1.0);
        cairo_set_source_rgba (cr, 1.0, 0, 1.0, 0.33);
        cairo_stroke (cr);
        cairo_restore (cr);
      }
916
    if G_UNLIKELY (flags & GTK_DEBUG_BASELINES)
917 918 919 920 921 922 923 924 925 926 927 928 929
      {
        int baseline = priv->allocated_baseline;

        if (baseline != -1)
          {
            if (priv->owner && !gtk_widget_get_has_window (priv->owner))
              {
                GtkAllocation widget_alloc;
                gtk_widget_get_allocation (priv->owner, &widget_alloc);
                baseline -= widget_alloc.y;
              }
            cairo_save (cr);
            cairo_new_path (cr);
930
            cairo_move_to (cr, x + margin.left, baseline + 0.5);
931 932 933 934 935 936 937 938
            cairo_rel_line_to (cr, width - margin.left - margin.right, 0);
            cairo_set_line_width (cr, 1.0);
            cairo_set_source_rgba (cr, 1.0, 0, 0.25, 0.25);
            cairo_stroke (cr);
            cairo_restore (cr);
          }
      }
  }
939
#endif
940 941
}

942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966
void
gtk_css_gadget_queue_resize (GtkCssGadget *gadget)
{
  g_return_if_fail (GTK_IS_CSS_GADGET (gadget));

  gtk_widget_queue_resize (gtk_css_gadget_get_owner (gadget));
}

void
gtk_css_gadget_queue_allocate (GtkCssGadget *gadget)
{
  g_return_if_fail (GTK_IS_CSS_GADGET (gadget));

  gtk_widget_queue_allocate (gtk_css_gadget_get_owner (gadget));
}

void
gtk_css_gadget_queue_draw (GtkCssGadget *gadget)
{
  g_return_if_fail (GTK_IS_CSS_GADGET (gadget));
  
  /* XXX: Only invalidate clip here */
  gtk_widget_queue_draw (gtk_css_gadget_get_owner (gadget));
}

967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017
/**
 * gtk_css_gadget_get_margin_box:
 * @gadget: a #GtkCssGadget
 * @box: (out): Return location for gadget's the margin box
 *
 * Returns the margin box of the gadget. The box coordinates are relative to
 *   the gadget origin. Compare with gtk_css_gadget_get_margin_allocation(),
 *   which returns the margin box in the widget allocation coordinates.
 */
void
gtk_css_gadget_get_margin_box (GtkCssGadget  *gadget,
                               GtkAllocation *box)
{
  gtk_css_gadget_get_margin_allocation (gadget, box, NULL);
  shift_allocation (gadget, box);
}

/**
 * gtk_css_gadget_get_border_box:
 * @gadget: a #GtkCssGadget
 * @box: (out): Return location for gadget's the border box
 *
 * Returns the border box of the gadget. The box coordinates are relative to
 *   the gadget origin. Compare with gtk_css_gadget_get_border_allocation(),
 *   which returns the border box in the widget allocation coordinates.
 */
void
gtk_css_gadget_get_border_box (GtkCssGadget  *gadget,
                               GtkAllocation *box)
{
  gtk_css_gadget_get_border_allocation (gadget, box, NULL);
  shift_allocation (gadget, box);
}

/**
 * gtk_css_gadget_get_content_box:
 * @gadget: a #GtkCssGadget
 * @box: (out): Return location for gadget's the content box
 *
 * Returns the content box of the gadget. The box coordinates are relative to
 *   the gadget origin. Compare with gtk_css_gadget_get_content_allocation(),
 *   which returns the content box in the widget allocation coordinates.
 */
void
gtk_css_gadget_get_content_box (GtkCssGadget  *gadget,
                                GtkAllocation *box)
{
  gtk_css_gadget_get_content_allocation (gadget, box, NULL);
  shift_allocation (gadget, box);
}

1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041
void
gtk_css_gadget_get_margin_allocation (GtkCssGadget  *gadget,
                                      GtkAllocation *allocation,
                                      int           *baseline)
{
  GtkCssGadgetPrivate *priv = gtk_css_gadget_get_instance_private (gadget);

  g_return_if_fail (GTK_IS_CSS_GADGET (gadget));

  if (!gtk_css_gadget_get_visible (gadget))
    {
      if (allocation)
        allocation->x = allocation->y = allocation->width = allocation->height = 0;
      if (baseline)
        *baseline = -1;
      return;
    }

  if (allocation)
    *allocation = priv->allocated_size;
  if (baseline)
    *baseline = priv->allocated_baseline;
}

1042 1043 1044 1045 1046 1047 1048 1049 1050 1051
void
gtk_css_gadget_get_border_allocation (GtkCssGadget  *gadget,
                                      GtkAllocation *allocation,
                                      int           *baseline)
{
  GtkCssGadgetPrivate *priv = gtk_css_gadget_get_instance_private (gadget);
  GtkBorder margin;

  g_return_if_fail (GTK_IS_CSS_GADGET (gadget));

1052 1053 1054 1055 1056 1057 1058 1059 1060
  if (!gtk_css_gadget_get_visible (gadget))
    {
      if (allocation)
        allocation->x = allocation->y = allocation->width = allocation->height = 0;
      if (baseline)
        *baseline = -1;
      return;
    }

1061 1062 1063 1064 1065 1066
  get_box_margin (gtk_css_gadget_get_style (gadget), &margin);

  if (allocation)
    {
      allocation->x = priv->allocated_size.x + margin.left;
      allocation->y = priv->allocated_size.y + margin.top;
1067 1068
      allocation->width = MAX (0, priv->allocated_size.width - margin.left - margin.right);
      allocation->height = MAX (0, priv->allocated_size.height - margin.top - margin.bottom);
1069 1070 1071 1072 1073 1074 1075 1076 1077 1078
    }
  if (baseline)
    {
      if (priv->allocated_baseline >= 0)
        *baseline = priv->allocated_baseline - margin.top;
      else
        *baseline = -1;
    }
}

1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089
void
gtk_css_gadget_get_content_allocation (GtkCssGadget  *gadget,
                                       GtkAllocation *allocation,
                                       int           *baseline)
{
  GtkCssGadgetPrivate *priv = gtk_css_gadget_get_instance_private (gadget);
  GtkBorder margin, border, padding, extents;
  GtkCssStyle *style;

  g_return_if_fail (GTK_IS_CSS_GADGET (gadget));

1090 1091 1092 1093 1094 1095 1096 1097 1098
  if (!gtk_css_gadget_get_visible (gadget))
    {
      if (allocation)
        allocation->x = allocation->y = allocation->width = allocation->height = 0;
      if (baseline)
        *baseline = -1;
      return;
    }

1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111
  style = gtk_css_gadget_get_style (gadget);
  get_box_margin (style, &margin);
  get_box_border (style, &border);
  get_box_padding (style, &padding);
  extents.top = margin.top + border.top + padding.top;
  extents.right = margin.right + border.right + padding.right;
  extents.bottom = margin.bottom + border.bottom + padding.bottom;
  extents.left = margin.left + border.left + padding.left;

  if (allocation)
    {
      allocation->x = priv->allocated_size.x + extents.left;
      allocation->y = priv->allocated_size.y + extents.top;
1112 1113
      allocation->width = MAX (0, priv->allocated_size.width - extents.left - extents.right);
      allocation->height = MAX (0, priv->allocated_size.height - extents.top - extents.bottom);
1114
    }
1115

1116 1117 1118 1119 1120 1121 1122 1123
  if (baseline)
    {
      if (priv->allocated_baseline >= 0)
        *baseline = priv->allocated_baseline - extents.top;
      else
        *baseline = -1;
    }
}