gtkrevealer.c 33.4 KB
Newer Older
Matthias Clasen's avatar
Matthias Clasen committed
1 2
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/*
3
 * Copyright 2013, 2015 Red Hat, Inc.
Matthias Clasen's avatar
Matthias Clasen committed
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
 *
 * This program 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 program 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 program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * Author: Alexander Larsson <alexl@redhat.com>
20
 *         Carlos Soriano <csoriano@gnome.org>
Matthias Clasen's avatar
Matthias Clasen committed
21 22 23 24 25 26 27 28
 */

#include "config.h"
#include "gtkrevealer.h"
#include <gdk/gdk.h>
#include "gtktypebuiltins.h"
#include "gtkprivate.h"
#include "gtkintl.h"
29 30

#include "fallback-c89.c"
Matthias Clasen's avatar
Matthias Clasen committed
31

32 33 34 35 36 37 38 39 40 41 42 43
/**
 * SECTION:gtkrevealer
 * @Short_description: Hide and show with animation
 * @Title: GtkRevealer
 * @See_also: #GtkExpander
 *
 * The GtkRevealer widget is a container which animates
 * the transition of its child from invisible to visible.
 *
 * The style of transition can be controlled with
 * gtk_revealer_set_transition_type().
 *
44
 * These animations respect the #GtkSettings:gtk-enable-animations
45 46
 * setting.
 *
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
 * The GtkRevealer widget was added in GTK+ 3.10.
 */

/**
 * GtkRevealerTransitionType:
 * @GTK_REVEALER_TRANSITION_TYPE_NONE: No transition
 * @GTK_REVEALER_TRANSITION_TYPE_CROSSFADE: Fade in
 * @GTK_REVEALER_TRANSITION_TYPE_SLIDE_RIGHT: Slide in from the left
 * @GTK_REVEALER_TRANSITION_TYPE_SLIDE_LEFT: Slide in from the right
 * @GTK_REVEALER_TRANSITION_TYPE_SLIDE_UP: Slide in from the bottom
 * @GTK_REVEALER_TRANSITION_TYPE_SLIDE_DOWN: Slide in from the top
 *
 * These enumeration values describe the possible transitions
 * when the child of a #GtkRevealer widget is shown or hidden.
 */

Matthias Clasen's avatar
Matthias Clasen committed
63 64 65 66 67
enum  {
  PROP_0,
  PROP_TRANSITION_TYPE,
  PROP_TRANSITION_DURATION,
  PROP_REVEAL_CHILD,
68 69
  PROP_CHILD_REVEALED,
  LAST_PROP
Matthias Clasen's avatar
Matthias Clasen committed
70 71
};

72
typedef struct {
Matthias Clasen's avatar
Matthias Clasen committed
73 74 75 76 77 78 79 80 81 82 83 84 85
  GtkRevealerTransitionType transition_type;
  guint transition_duration;

  GdkWindow* bin_window;
  GdkWindow* view_window;

  gdouble current_pos;
  gdouble source_pos;
  gdouble target_pos;

  guint tick_id;
  gint64 start_time;
  gint64 end_time;
86
} GtkRevealerPrivate;
Matthias Clasen's avatar
Matthias Clasen committed
87

88
static GParamSpec *props[LAST_PROP] = { NULL, };
Matthias Clasen's avatar
Matthias Clasen committed
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

static void     gtk_revealer_real_realize                        (GtkWidget     *widget);
static void     gtk_revealer_real_unrealize                      (GtkWidget     *widget);
static void     gtk_revealer_real_add                            (GtkContainer  *widget,
                                                                  GtkWidget     *child);
static void     gtk_revealer_real_size_allocate                  (GtkWidget     *widget,
                                                                  GtkAllocation *allocation);
static void     gtk_revealer_real_map                            (GtkWidget     *widget);
static void     gtk_revealer_real_unmap                          (GtkWidget     *widget);
static gboolean gtk_revealer_real_draw                           (GtkWidget     *widget,
                                                                  cairo_t       *cr);
static void     gtk_revealer_real_get_preferred_height           (GtkWidget     *widget,
                                                                  gint          *minimum_height,
                                                                  gint          *natural_height);
static void     gtk_revealer_real_get_preferred_height_for_width (GtkWidget     *widget,
                                                                  gint           width,
                                                                  gint          *minimum_height,
                                                                  gint          *natural_height);
static void     gtk_revealer_real_get_preferred_width            (GtkWidget     *widget,
                                                                  gint          *minimum_width,
                                                                  gint          *natural_width);
static void     gtk_revealer_real_get_preferred_width_for_height (GtkWidget     *widget,
                                                                  gint           height,
                                                                  gint          *minimum_width,
                                                                  gint          *natural_width);

115
G_DEFINE_TYPE_WITH_PRIVATE (GtkRevealer, gtk_revealer, GTK_TYPE_BIN)
Matthias Clasen's avatar
Matthias Clasen committed
116

117 118 119 120 121 122 123 124 125 126 127 128 129 130
static void
gtk_revealer_get_padding (GtkRevealer *revealer,
                          GtkBorder   *padding)
{
  GtkWidget *widget = GTK_WIDGET (revealer);
  GtkStyleContext *context;
  GtkStateFlags state;

  context = gtk_widget_get_style_context (widget);
  state = gtk_style_context_get_state (context);

  gtk_style_context_get_padding (context, state, padding);
}

Matthias Clasen's avatar
Matthias Clasen committed
131 132 133
static void
gtk_revealer_init (GtkRevealer *revealer)
{
134
  GtkRevealerPrivate *priv = gtk_revealer_get_instance_private (revealer);
Matthias Clasen's avatar
Matthias Clasen committed
135 136 137 138 139 140 141 142 143 144 145 146 147 148

  priv->transition_type = GTK_REVEALER_TRANSITION_TYPE_SLIDE_DOWN;
  priv->transition_duration = 250;
  priv->current_pos = 0.0;
  priv->target_pos = 0.0;

  gtk_widget_set_has_window ((GtkWidget*) revealer, TRUE);
  gtk_widget_set_redraw_on_allocate ((GtkWidget*) revealer, FALSE);
}

static void
gtk_revealer_finalize (GObject *obj)
{
  GtkRevealer *revealer = GTK_REVEALER (obj);
149
  GtkRevealerPrivate *priv = gtk_revealer_get_instance_private (revealer);
Matthias Clasen's avatar
Matthias Clasen committed
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 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234

  if (priv->tick_id != 0)
    gtk_widget_remove_tick_callback (GTK_WIDGET (revealer), priv->tick_id);
  priv->tick_id = 0;

  G_OBJECT_CLASS (gtk_revealer_parent_class)->finalize (obj);
}

static void
gtk_revealer_get_property (GObject    *object,
                           guint       property_id,
                           GValue     *value,
                           GParamSpec *pspec)
{
  GtkRevealer *revealer = GTK_REVEALER (object);

  switch (property_id)
   {
    case PROP_TRANSITION_TYPE:
      g_value_set_enum (value, gtk_revealer_get_transition_type (revealer));
      break;
    case PROP_TRANSITION_DURATION:
      g_value_set_uint (value, gtk_revealer_get_transition_duration (revealer));
      break;
    case PROP_REVEAL_CHILD:
      g_value_set_boolean (value, gtk_revealer_get_reveal_child (revealer));
      break;
    case PROP_CHILD_REVEALED:
      g_value_set_boolean (value, gtk_revealer_get_child_revealed (revealer));
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
      break;
    }
}

static void
gtk_revealer_set_property (GObject      *object,
                           guint         property_id,
                           const GValue *value,
                           GParamSpec   *pspec)
{
  GtkRevealer *revealer = GTK_REVEALER (object);

  switch (property_id)
    {
    case PROP_TRANSITION_TYPE:
      gtk_revealer_set_transition_type (revealer, g_value_get_enum (value));
      break;
    case PROP_TRANSITION_DURATION:
      gtk_revealer_set_transition_duration (revealer, g_value_get_uint (value));
      break;
    case PROP_REVEAL_CHILD:
      gtk_revealer_set_reveal_child (revealer, g_value_get_boolean (value));
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
      break;
    }
}

static void
gtk_revealer_class_init (GtkRevealerClass *klass)
{
  GObjectClass *object_class = G_OBJECT_CLASS (klass);
  GtkWidgetClass *widget_class = GTK_WIDGET_CLASS(klass);
  GtkContainerClass *container_class = GTK_CONTAINER_CLASS (klass);

  object_class->get_property = gtk_revealer_get_property;
  object_class->set_property = gtk_revealer_set_property;
  object_class->finalize = gtk_revealer_finalize;

  widget_class->realize = gtk_revealer_real_realize;
  widget_class->unrealize = gtk_revealer_real_unrealize;
  widget_class->size_allocate = gtk_revealer_real_size_allocate;
  widget_class->map = gtk_revealer_real_map;
  widget_class->unmap = gtk_revealer_real_unmap;
  widget_class->draw = gtk_revealer_real_draw;
  widget_class->get_preferred_height = gtk_revealer_real_get_preferred_height;
  widget_class->get_preferred_height_for_width = gtk_revealer_real_get_preferred_height_for_width;
  widget_class->get_preferred_width = gtk_revealer_real_get_preferred_width;
  widget_class->get_preferred_width_for_height = gtk_revealer_real_get_preferred_width_for_height;

  container_class->add = gtk_revealer_real_add;

235 236 237 238 239 240
  props[PROP_TRANSITION_TYPE] =
    g_param_spec_enum ("transition-type",
                       P_("Transition type"),
                       P_("The type of animation used to transition"),
                       GTK_TYPE_REVEALER_TRANSITION_TYPE,
                       GTK_REVEALER_TRANSITION_TYPE_SLIDE_DOWN,
241
                       GTK_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_EXPLICIT_NOTIFY);
242 243 244 245 246 247

  props[PROP_TRANSITION_DURATION] =
    g_param_spec_uint ("transition-duration",
                       P_("Transition duration"),
                       P_("The animation duration, in milliseconds"),
                       0, G_MAXUINT, 250,
248
                       GTK_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_EXPLICIT_NOTIFY);
249 250 251 252 253 254

  props[PROP_REVEAL_CHILD] =
    g_param_spec_boolean ("reveal-child",
                          P_("Reveal Child"),
                          P_("Whether the container should reveal the child"),
                          FALSE,
255
                          GTK_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_EXPLICIT_NOTIFY);
256 257 258 259 260 261 262 263 264

  props[PROP_CHILD_REVEALED] =
    g_param_spec_boolean ("child-revealed",
                          P_("Child Revealed"),
                          P_("Whether the child is revealed and the animation target reached"),
                          FALSE,
                          G_PARAM_READABLE);

  g_object_class_install_properties (object_class, LAST_PROP, props);
Matthias Clasen's avatar
Matthias Clasen committed
265 266
}

267 268 269 270 271 272 273 274 275
/**
 * gtk_revealer_new:
 *
 * Creates a new #GtkRevealer.
 *
 * Returns: a newly created #GtkRevealer
 *
 * Since: 3.10
 */
Matthias Clasen's avatar
Matthias Clasen committed
276 277 278 279 280 281
GtkWidget *
gtk_revealer_new (void)
{
  return g_object_new (GTK_TYPE_REVEALER, NULL);
}

282 283 284
static GtkRevealerTransitionType
effective_transition (GtkRevealer *revealer)
{
285
  GtkRevealerPrivate *priv = gtk_revealer_get_instance_private (revealer);
286 287 288 289 290 291 292
  gboolean animations_enabled;

  g_object_get (gtk_widget_get_settings (GTK_WIDGET (revealer)),
                "gtk-enable-animations", &animations_enabled,
                NULL);
  if (!animations_enabled)
    return GTK_REVEALER_TRANSITION_TYPE_NONE;
293 294 295 296 297 298 299 300 301 302 303 304

  if (gtk_widget_get_direction (GTK_WIDGET (revealer)) == GTK_TEXT_DIR_RTL)
    {
      if (priv->transition_type == GTK_REVEALER_TRANSITION_TYPE_SLIDE_LEFT)
        return GTK_REVEALER_TRANSITION_TYPE_SLIDE_RIGHT;
      else if (priv->transition_type == GTK_REVEALER_TRANSITION_TYPE_SLIDE_RIGHT)
        return GTK_REVEALER_TRANSITION_TYPE_SLIDE_LEFT;
    }

  return priv->transition_type;
}

Matthias Clasen's avatar
Matthias Clasen committed
305 306 307 308 309 310
static void
gtk_revealer_get_child_allocation (GtkRevealer   *revealer,
                                   GtkAllocation *allocation,
                                   GtkAllocation *child_allocation)
{
  GtkWidget *child;
311
  GtkRevealerTransitionType transition;
312 313
  GtkBorder padding;
  gint vertical_padding, horizontal_padding;
Matthias Clasen's avatar
Matthias Clasen committed
314 315 316 317

  g_return_if_fail (revealer != NULL);
  g_return_if_fail (allocation != NULL);

318 319 320 321 322
  /* See explanation on gtk_revealer_real_size_allocate */
  gtk_revealer_get_padding (revealer, &padding);
  vertical_padding = padding.top + padding.bottom;
  horizontal_padding = padding.left + padding.right;

Matthias Clasen's avatar
Matthias Clasen committed
323 324
  child_allocation->x = 0;
  child_allocation->y = 0;
325 326
  child_allocation->width = 0;
  child_allocation->height = 0;
Matthias Clasen's avatar
Matthias Clasen committed
327 328 329 330

  child = gtk_bin_get_child (GTK_BIN (revealer));
  if (child != NULL && gtk_widget_get_visible (child))
    {
331 332 333
      transition = effective_transition (revealer);
      if (transition == GTK_REVEALER_TRANSITION_TYPE_SLIDE_LEFT ||
          transition == GTK_REVEALER_TRANSITION_TYPE_SLIDE_RIGHT)
334
        gtk_widget_get_preferred_width_for_height (child, MAX (0, allocation->height - vertical_padding), NULL,
Matthias Clasen's avatar
Matthias Clasen committed
335 336
                                                   &child_allocation->width);
      else
337
        gtk_widget_get_preferred_height_for_width (child, MAX (0, allocation->width - horizontal_padding), NULL,
Matthias Clasen's avatar
Matthias Clasen committed
338 339
                                                   &child_allocation->height);
    }
340

341 342
  child_allocation->width = MAX (child_allocation->width, allocation->width - horizontal_padding);
  child_allocation->height = MAX (child_allocation->height, allocation->height - vertical_padding);
Matthias Clasen's avatar
Matthias Clasen committed
343 344 345 346 347 348
}

static void
gtk_revealer_real_realize (GtkWidget *widget)
{
  GtkRevealer *revealer = GTK_REVEALER (widget);
349
  GtkRevealerPrivate *priv = gtk_revealer_get_instance_private (revealer);
Matthias Clasen's avatar
Matthias Clasen committed
350 351 352 353 354
  GtkAllocation allocation;
  GdkWindowAttr attributes = { 0 };
  GdkWindowAttributesType attributes_mask;
  GtkAllocation child_allocation;
  GtkWidget *child;
355
  GtkRevealerTransitionType transition;
356
  GtkBorder padding;
Matthias Clasen's avatar
Matthias Clasen committed
357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380

  gtk_widget_set_realized (widget, TRUE);

  gtk_widget_get_allocation (widget, &allocation);

  attributes.x = allocation.x;
  attributes.y = allocation.y;
  attributes.width = allocation.width;
  attributes.height = allocation.height;
  attributes.window_type = GDK_WINDOW_CHILD;
  attributes.wclass = GDK_INPUT_OUTPUT;
  attributes.visual = gtk_widget_get_visual (widget);
  attributes.event_mask =
    gtk_widget_get_events (widget) | GDK_EXPOSURE_MASK;
  attributes_mask = (GDK_WA_X | GDK_WA_Y) | GDK_WA_VISUAL;

  priv->view_window =
    gdk_window_new (gtk_widget_get_parent_window ((GtkWidget*) revealer),
                    &attributes, attributes_mask);
  gtk_widget_set_window (widget, priv->view_window);
  gtk_widget_register_window (widget, priv->view_window);

  gtk_revealer_get_child_allocation (revealer, &allocation, &child_allocation);

381
  gtk_revealer_get_padding (revealer, &padding);
Matthias Clasen's avatar
Matthias Clasen committed
382 383 384 385 386
  attributes.x = 0;
  attributes.y = 0;
  attributes.width = child_allocation.width;
  attributes.height = child_allocation.height;

387
  /* See explanation on gtk_revealer_real_size_allocate */
388 389
  transition = effective_transition (revealer);
  if (transition == GTK_REVEALER_TRANSITION_TYPE_SLIDE_DOWN)
390 391 392 393
    {
      attributes.y = allocation.height - child_allocation.height - padding.bottom;
      attributes.x = padding.left;
    }
394
  else if (transition == GTK_REVEALER_TRANSITION_TYPE_SLIDE_RIGHT)
395 396 397 398 399 400 401 402 403
    {
      attributes.y = padding.top;
      attributes.x = allocation.width - child_allocation.width - padding.right;
    }
 else
   {
     attributes.y = padding.top;
     attributes.x = padding.left;
   }
Matthias Clasen's avatar
Matthias Clasen committed
404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419

  priv->bin_window =
    gdk_window_new (priv->view_window, &attributes, attributes_mask);
  gtk_widget_register_window (widget, priv->bin_window);

  child = gtk_bin_get_child (GTK_BIN (revealer));
  if (child != NULL)
    gtk_widget_set_parent_window (child, priv->bin_window);

  gdk_window_show (priv->bin_window);
}

static void
gtk_revealer_real_unrealize (GtkWidget *widget)
{
  GtkRevealer *revealer = GTK_REVEALER (widget);
420
  GtkRevealerPrivate *priv = gtk_revealer_get_instance_private (revealer);
Matthias Clasen's avatar
Matthias Clasen committed
421 422 423 424 425 426 427 428 429 430 431 432 433

  gtk_widget_unregister_window (widget, priv->bin_window);
  gdk_window_destroy (priv->bin_window);
  priv->view_window = NULL;

  GTK_WIDGET_CLASS (gtk_revealer_parent_class)->unrealize (widget);
}

static void
gtk_revealer_real_add (GtkContainer *container,
                       GtkWidget    *child)
{
  GtkRevealer *revealer = GTK_REVEALER (container);
434
  GtkRevealerPrivate *priv = gtk_revealer_get_instance_private (revealer);
Matthias Clasen's avatar
Matthias Clasen committed
435 436 437 438 439 440 441 442 443 444 445 446 447 448

  g_return_if_fail (child != NULL);

  gtk_widget_set_parent_window (child, priv->bin_window);
  gtk_widget_set_child_visible (child, priv->current_pos != 0.0);

  GTK_CONTAINER_CLASS (gtk_revealer_parent_class)->add (container, child);
}

static void
gtk_revealer_real_size_allocate (GtkWidget     *widget,
                                 GtkAllocation *allocation)
{
  GtkRevealer *revealer = GTK_REVEALER (widget);
449
  GtkRevealerPrivate *priv = gtk_revealer_get_instance_private (revealer);
Matthias Clasen's avatar
Matthias Clasen committed
450 451 452 453
  GtkAllocation child_allocation;
  GtkWidget *child;
  gboolean window_visible;
  int bin_x, bin_y;
454
  GtkRevealerTransitionType transition;
455
  GtkBorder padding;
Matthias Clasen's avatar
Matthias Clasen committed
456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478

  g_return_if_fail (allocation != NULL);

  gtk_widget_set_allocation (widget, allocation);
  gtk_revealer_get_child_allocation (revealer, allocation, &child_allocation);

  child = gtk_bin_get_child (GTK_BIN (revealer));
  if (child != NULL && gtk_widget_get_visible (child))
    gtk_widget_size_allocate (child, &child_allocation);

  if (gtk_widget_get_realized (widget))
    {
      if (gtk_widget_get_mapped (widget))
        {
          window_visible = allocation->width > 0 && allocation->height > 0;

          if (!window_visible && gdk_window_is_visible (priv->view_window))
            gdk_window_hide (priv->view_window);

          if (window_visible && !gdk_window_is_visible (priv->view_window))
            gdk_window_show (priv->view_window);
        }

479 480
      /* The view window will follow the revealer allocation, which is modified
       * along the animation */
Matthias Clasen's avatar
Matthias Clasen committed
481 482 483 484
      gdk_window_move_resize (priv->view_window,
                              allocation->x, allocation->y,
                              allocation->width, allocation->height);

485
      gtk_revealer_get_padding (revealer, &padding);
Matthias Clasen's avatar
Matthias Clasen committed
486 487
      bin_x = 0;
      bin_y = 0;
488

489
      transition = effective_transition (revealer);
490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511
      /* The child allocation is fixed (it is not modified by the animation),
       * and it's origin is relative to the bin_window.
       * The bin_window has the same allocation as the child, and then the bin_window
       * deals with the relative positioning with respect to the revealer taking
       * into account the paddings of the revealer.
       *
       * For most of transitions, the bin_window moves along with the revealer,
       * as its allocation changes.
       * However for GTK_REVEALER_TRANSITION_TYPE_SLIDE_DOWN
       * we need to first move the bin_window upwards and then slide it down in
       * the revealer.
       * Otherwise the child would appear as static and the revealer will allocate
       * following the animation, clipping the child.
       * To calculate the correct y position for this case:
       * allocation->height - child_allocation.height is the relative position
       * towards the revealer taking into account the animation progress with
       * both vertical paddings added, therefore we need to substract the part
       * that we don't want to take into account for the y position, which
       * in this case is the bottom padding.
       *
       * The same special treatment is needed for GTK_REVEALER_TRANSITION_TYPE_SLIDE_RIGHT.
       */
512
      if (transition == GTK_REVEALER_TRANSITION_TYPE_SLIDE_DOWN)
513 514 515 516
        {
          bin_y = allocation->height - child_allocation.height - padding.bottom;
          bin_x = padding.left;
        }
517
      else if (transition == GTK_REVEALER_TRANSITION_TYPE_SLIDE_RIGHT)
518 519 520 521 522 523 524 525 526
        {
          bin_y = padding.top;
          bin_x = allocation->width - child_allocation.width - padding.right;
        }
     else
       {
         bin_x = padding.left;
         bin_y = padding.top;
       }
Matthias Clasen's avatar
Matthias Clasen committed
527 528 529 530 531 532 533 534 535 536 537

      gdk_window_move_resize (priv->bin_window,
                              bin_x, bin_y,
                              child_allocation.width, child_allocation.height);
    }
}

static void
gtk_revealer_set_position (GtkRevealer *revealer,
                           gdouble      pos)
{
538
  GtkRevealerPrivate *priv = gtk_revealer_get_instance_private (revealer);
Matthias Clasen's avatar
Matthias Clasen committed
539 540
  gboolean new_visible;
  GtkWidget *child;
541
  GtkRevealerTransitionType transition;
Matthias Clasen's avatar
Matthias Clasen committed
542 543 544 545 546 547 548 549 550 551 552 553 554 555 556

  priv->current_pos = pos;

  /* We check target_pos here too, because we want to ensure we set
   * child_visible immediately when starting a reveal operation
   * otherwise the child widgets will not be properly realized
   * after the reveal returns.
   */
  new_visible = priv->current_pos != 0.0 || priv->target_pos != 0.0;

  child = gtk_bin_get_child (GTK_BIN (revealer));
  if (child != NULL &&
      new_visible != gtk_widget_get_child_visible (child))
    gtk_widget_set_child_visible (child, new_visible);

557 558
  transition = effective_transition (revealer);
  if (transition == GTK_REVEALER_TRANSITION_TYPE_CROSSFADE)
559 560 561 562 563 564 565 566
    {
      gtk_widget_set_opacity (GTK_WIDGET (revealer), priv->current_pos);
      gtk_widget_queue_draw (GTK_WIDGET (revealer));
    }
  else
    {
      gtk_widget_queue_resize (GTK_WIDGET (revealer));
    }
Matthias Clasen's avatar
Matthias Clasen committed
567 568

  if (priv->current_pos == priv->target_pos)
569
    g_object_notify_by_pspec (G_OBJECT (revealer), props[PROP_CHILD_REVEALED]);
Matthias Clasen's avatar
Matthias Clasen committed
570 571
}

572 573 574 575 576
/* From clutter-easing.c, based on Robert Penner's
 * infamous easing equations, MIT license.
 */
static double
ease_out_cubic (double t)
Matthias Clasen's avatar
Matthias Clasen committed
577
{
578 579
  double p = t - 1;
  return p * p * p + 1;
Matthias Clasen's avatar
Matthias Clasen committed
580 581 582 583 584 585
}

static void
gtk_revealer_animate_step (GtkRevealer *revealer,
                           gint64       now)
{
586
  GtkRevealerPrivate *priv = gtk_revealer_get_instance_private (revealer);
Matthias Clasen's avatar
Matthias Clasen committed
587 588 589
  gdouble t;

  if (now < priv->end_time)
590 591 592 593
    t = (now - priv->start_time) / (gdouble) (priv->end_time - priv->start_time);
  else
    t = 1.0;
  t = ease_out_cubic (t);
Matthias Clasen's avatar
Matthias Clasen committed
594 595 596 597 598 599

  gtk_revealer_set_position (revealer,
                            priv->source_pos + (t * (priv->target_pos - priv->source_pos)));
}

static gboolean
600
gtk_revealer_animate_cb (GtkWidget     *widget,
Matthias Clasen's avatar
Matthias Clasen committed
601 602 603
                         GdkFrameClock *frame_clock,
                         gpointer       user_data)
{
604
  GtkRevealer *revealer = GTK_REVEALER (widget);
605
  GtkRevealerPrivate *priv = gtk_revealer_get_instance_private (revealer);
Matthias Clasen's avatar
Matthias Clasen committed
606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622
  gint64 now;

  now = gdk_frame_clock_get_frame_time (frame_clock);
  gtk_revealer_animate_step (revealer, now);
  if (priv->current_pos == priv->target_pos)
    {
      priv->tick_id = 0;
      return FALSE;
    }

  return TRUE;
}

static void
gtk_revealer_start_animation (GtkRevealer *revealer,
                              gdouble      target)
{
623
  GtkRevealerPrivate *priv = gtk_revealer_get_instance_private (revealer);
Matthias Clasen's avatar
Matthias Clasen committed
624
  GtkWidget *widget = GTK_WIDGET (revealer);
625
  GtkRevealerTransitionType transition;
Matthias Clasen's avatar
Matthias Clasen committed
626 627 628 629 630

  if (priv->target_pos == target)
    return;

  priv->target_pos = target;
631
  g_object_notify_by_pspec (G_OBJECT (revealer), props[PROP_REVEAL_CHILD]);
Matthias Clasen's avatar
Matthias Clasen committed
632

633
  transition = effective_transition (revealer);
Matthias Clasen's avatar
Matthias Clasen committed
634 635
  if (gtk_widget_get_mapped (widget) &&
      priv->transition_duration != 0 &&
636
      transition != GTK_REVEALER_TRANSITION_TYPE_NONE)
Matthias Clasen's avatar
Matthias Clasen committed
637 638 639 640 641 642
    {
      priv->source_pos = priv->current_pos;
      priv->start_time = gdk_frame_clock_get_frame_time (gtk_widget_get_frame_clock (widget));
      priv->end_time = priv->start_time + (priv->transition_duration * 1000);
      if (priv->tick_id == 0)
        priv->tick_id =
643
          gtk_widget_add_tick_callback (widget, gtk_revealer_animate_cb, revealer, NULL);
Matthias Clasen's avatar
Matthias Clasen committed
644 645 646 647 648 649 650 651 652 653 654
      gtk_revealer_animate_step (revealer, priv->start_time);
    }
  else
    {
      gtk_revealer_set_position (revealer, target);
    }
}

static void
gtk_revealer_stop_animation (GtkRevealer *revealer)
{
655
  GtkRevealerPrivate *priv = gtk_revealer_get_instance_private (revealer);
Matthias Clasen's avatar
Matthias Clasen committed
656 657 658 659 660 661 662 663 664 665 666 667 668

  priv->current_pos = priv->target_pos;
  if (priv->tick_id != 0)
    {
      gtk_widget_remove_tick_callback (GTK_WIDGET (revealer), priv->tick_id);
      priv->tick_id = 0;
    }
}

static void
gtk_revealer_real_map (GtkWidget *widget)
{
  GtkRevealer *revealer = GTK_REVEALER (widget);
669
  GtkRevealerPrivate *priv = gtk_revealer_get_instance_private (revealer);
Matthias Clasen's avatar
Matthias Clasen committed
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
  GtkAllocation allocation;

  if (!gtk_widget_get_mapped (widget))
    {
      gtk_widget_get_allocation (widget, &allocation);

      if (allocation.width > 0 && allocation.height > 0)
        gdk_window_show (priv->view_window);

      gtk_revealer_start_animation (revealer, priv->target_pos);
    }

  GTK_WIDGET_CLASS (gtk_revealer_parent_class)->map (widget);
}

static void
gtk_revealer_real_unmap (GtkWidget *widget)
{
  GtkRevealer *revealer = GTK_REVEALER (widget);

  GTK_WIDGET_CLASS (gtk_revealer_parent_class)->unmap (widget);

  gtk_revealer_stop_animation (revealer);
}

static gboolean
gtk_revealer_real_draw (GtkWidget *widget,
                        cairo_t   *cr)
{
  GtkRevealer *revealer = GTK_REVEALER (widget);
700
  GtkRevealerPrivate *priv = gtk_revealer_get_instance_private (revealer);
Matthias Clasen's avatar
Matthias Clasen committed
701 702 703 704 705 706 707

  if (gtk_cairo_should_draw_window (cr, priv->bin_window))
    GTK_WIDGET_CLASS (gtk_revealer_parent_class)->draw (widget, cr);

  return TRUE;
}

708 709 710 711 712 713 714 715 716 717 718 719
/**
 * gtk_revealer_set_reveal_child:
 * @revealer: a #GtkRevealer
 * @reveal_child: %TRUE to reveal the child
 *
 * Tells the #GtkRevealer to reveal or conceal its child.
 *
 * The transition will be animated with the current
 * transition type of @revealer.
 *
 * Since: 3.10
 */
Matthias Clasen's avatar
Matthias Clasen committed
720 721 722 723 724 725 726 727 728 729 730 731
void
gtk_revealer_set_reveal_child (GtkRevealer *revealer,
                               gboolean     reveal_child)
{
  g_return_if_fail (GTK_IS_REVEALER (revealer));

  if (reveal_child)
    gtk_revealer_start_animation (revealer, 1.0);
  else
    gtk_revealer_start_animation (revealer, 0.0);
}

732 733
/**
 * gtk_revealer_get_reveal_child:
Matthias Clasen's avatar
Matthias Clasen committed
734
 * @revealer: a #GtkRevealer
735 736 737 738 739 740 741 742 743
 *
 * Returns whether the child is currently
 * revealed. See gtk_revealer_set_reveal_child().
 *
 * This function returns %TRUE as soon as the transition
 * is to the revealed state is started. To learn whether
 * the child is fully revealed (ie the transition is completed),
 * use gtk_revealer_get_child_revealed().
 *
744
 * Returns: %TRUE if the child is revealed.
745 746 747
 *
 * Since: 3.10
 */
Matthias Clasen's avatar
Matthias Clasen committed
748 749 750
gboolean
gtk_revealer_get_reveal_child (GtkRevealer *revealer)
{
751 752
  GtkRevealerPrivate *priv = gtk_revealer_get_instance_private (revealer);

Matthias Clasen's avatar
Matthias Clasen committed
753 754
  g_return_val_if_fail (GTK_IS_REVEALER (revealer), FALSE);

755
  return priv->target_pos != 0.0;
Matthias Clasen's avatar
Matthias Clasen committed
756 757
}

758 759 760 761 762 763 764
/**
 * gtk_revealer_get_child_revealed:
 * @revealer: a #GtkRevealer
 *
 * Returns whether the child is fully revealed, ie wether
 * the transition to the revealed state is completed.
 *
765
 * Returns: %TRUE if the child is fully revealed
766 767 768
 *
 * Since: 3.10
 */
Matthias Clasen's avatar
Matthias Clasen committed
769 770 771
gboolean
gtk_revealer_get_child_revealed (GtkRevealer *revealer)
{
772 773
  GtkRevealerPrivate *priv = gtk_revealer_get_instance_private (revealer);
  gboolean animation_finished = (priv->target_pos == priv->current_pos);
Matthias Clasen's avatar
Matthias Clasen committed
774 775 776 777 778 779 780 781 782 783 784 785 786 787 788
  gboolean reveal_child = gtk_revealer_get_reveal_child (revealer);

  if (animation_finished)
    return reveal_child;
  else
    return !reveal_child;
}

/* These all report only the natural size, ignoring the minimal size,
 * because its not really possible to allocate the right size during
 * animation if the child size can change (without the child
 * re-arranging itself during the animation).
 */

static void
789 790 791 792 793
set_height_with_paddings (GtkRevealer *revealer,
                          gint         preferred_minimum_height,
                          gint         preferred_natural_height,
                          gint        *minimum_height_out,
                          gint        *natural_height_out)
Matthias Clasen's avatar
Matthias Clasen committed
794
{
795
  GtkRevealerPrivate *priv = gtk_revealer_get_instance_private (revealer);
Matthias Clasen's avatar
Matthias Clasen committed
796 797
  gint minimum_height;
  gint natural_height;
798
  GtkRevealerTransitionType transition;
799 800
  GtkBorder padding;
  gint vertical_padding;
Matthias Clasen's avatar
Matthias Clasen committed
801

802 803 804 805
  gtk_revealer_get_padding (revealer, &padding);
  vertical_padding = padding.top + padding.bottom;
  minimum_height = preferred_minimum_height + vertical_padding;
  natural_height = preferred_natural_height + vertical_padding;
Matthias Clasen's avatar
Matthias Clasen committed
806

807
  transition = effective_transition (revealer);
808 809
  if (transition == GTK_REVEALER_TRANSITION_TYPE_NONE ||
      transition == GTK_REVEALER_TRANSITION_TYPE_SLIDE_UP ||
810
      transition == GTK_REVEALER_TRANSITION_TYPE_SLIDE_DOWN)
811 812 813 814 815
    {
      /* Padding are included in the animation */
      minimum_height = round (minimum_height * priv->current_pos);
      natural_height = round (natural_height * priv->current_pos);
    }
Matthias Clasen's avatar
Matthias Clasen committed
816

817
  *minimum_height_out = MIN (minimum_height, natural_height);
818
  *natural_height_out = natural_height;
Matthias Clasen's avatar
Matthias Clasen committed
819 820
}

821 822 823 824 825 826 827 828 829 830 831 832 833 834 835
static void
gtk_revealer_real_get_preferred_height (GtkWidget *widget,
                                        gint      *minimum_height_out,
                                        gint      *natural_height_out)
{
  GtkRevealer *revealer = GTK_REVEALER (widget);
  gint minimum_height;
  gint natural_height;

  GTK_WIDGET_CLASS (gtk_revealer_parent_class)->get_preferred_height (widget, &minimum_height, &natural_height);

  set_height_with_paddings (revealer, minimum_height, natural_height,
                            minimum_height_out, natural_height_out);
}

Matthias Clasen's avatar
Matthias Clasen committed
836 837 838 839 840 841 842 843 844 845 846 847
static void
gtk_revealer_real_get_preferred_height_for_width (GtkWidget *widget,
                                                  gint       width,
                                                  gint      *minimum_height_out,
                                                  gint      *natural_height_out)
{
  GtkRevealer *revealer = GTK_REVEALER (widget);
  gint minimum_height;
  gint natural_height;

  GTK_WIDGET_CLASS (gtk_revealer_parent_class)->get_preferred_height_for_width (widget, width, &minimum_height, &natural_height);

848 849
  set_height_with_paddings (revealer, minimum_height, natural_height,
                            minimum_height_out, natural_height_out);
Matthias Clasen's avatar
Matthias Clasen committed
850 851 852
}

static void
853 854 855 856 857
set_width_with_paddings (GtkRevealer *revealer,
                         gint         preferred_minimum_width,
                         gint         preferred_natural_width,
                         gint        *minimum_width_out,
                         gint        *natural_width_out)
Matthias Clasen's avatar
Matthias Clasen committed
858
{
859
  GtkRevealerPrivate *priv = gtk_revealer_get_instance_private (revealer);
Matthias Clasen's avatar
Matthias Clasen committed
860 861
  gint minimum_width;
  gint natural_width;
862
  GtkRevealerTransitionType transition;
863 864
  GtkBorder padding;
  gint horizontal_padding;
Matthias Clasen's avatar
Matthias Clasen committed
865

866 867
  gtk_revealer_get_padding (revealer, &padding);
  horizontal_padding = padding.left + padding.right;
868 869
  minimum_width = preferred_minimum_width + horizontal_padding;
  natural_width = preferred_natural_width + horizontal_padding;
Matthias Clasen's avatar
Matthias Clasen committed
870

871
  transition = effective_transition (revealer);
872
  if (transition == GTK_REVEALER_TRANSITION_TYPE_SLIDE_LEFT ||
873
      transition == GTK_REVEALER_TRANSITION_TYPE_SLIDE_RIGHT)
874 875 876 877 878
    {
      /* Paddings are included in the animation */
      minimum_width = round (minimum_width * priv->current_pos);
      natural_width = round (natural_width * priv->current_pos);
    }
Matthias Clasen's avatar
Matthias Clasen committed
879

880
  *minimum_width_out = MIN (minimum_width, natural_width);
881
  *natural_width_out = natural_width;
Matthias Clasen's avatar
Matthias Clasen committed
882 883
}

884 885 886 887 888 889 890 891 892 893 894 895 896 897
static void
gtk_revealer_real_get_preferred_width (GtkWidget *widget,
                                       gint      *minimum_width_out,
                                       gint      *natural_width_out)
{
  GtkRevealer *revealer = GTK_REVEALER (widget);
  gint minimum_width;
  gint natural_width;

  GTK_WIDGET_CLASS (gtk_revealer_parent_class)->get_preferred_width (widget, &minimum_width, &natural_width);
  set_width_with_paddings (revealer, minimum_width, natural_width,
                           minimum_width_out, natural_width_out);
}

Matthias Clasen's avatar
Matthias Clasen committed
898 899 900 901 902 903 904 905 906 907 908 909
static void
gtk_revealer_real_get_preferred_width_for_height (GtkWidget *widget,
                                                  gint       height,
                                                  gint      *minimum_width_out,
                                                  gint      *natural_width_out)
{
  GtkRevealer *revealer = GTK_REVEALER (widget);
  gint minimum_width;
  gint natural_width;

  GTK_WIDGET_CLASS (gtk_revealer_parent_class)->get_preferred_width_for_height (widget, height, &minimum_width, &natural_width);

910 911
  set_width_with_paddings (revealer, minimum_width, natural_width,
                           minimum_width_out, natural_width_out);
Matthias Clasen's avatar
Matthias Clasen committed
912 913
}

914 915 916 917 918 919 920 921 922 923 924
/**
 * gtk_revealer_get_transition_duration:
 * @revealer: a #GtkRevealer
 *
 * Returns the amount of time (in milliseconds) that
 * transitions will take.
 *
 * Returns: the transition duration
 *
 * Since: 3.10
 */
Matthias Clasen's avatar
Matthias Clasen committed
925 926 927
guint
gtk_revealer_get_transition_duration (GtkRevealer *revealer)
{
928 929
  GtkRevealerPrivate *priv = gtk_revealer_get_instance_private (revealer);

Matthias Clasen's avatar
Matthias Clasen committed
930 931
  g_return_val_if_fail (GTK_IS_REVEALER (revealer), 0);

932
  return priv->transition_duration;
Matthias Clasen's avatar
Matthias Clasen committed
933 934
}

935 936 937 938 939 940 941 942 943
/**
 * gtk_revealer_set_transition_duration:
 * @revealer: a #GtkRevealer
 * @duration: the new duration, in milliseconds
 *
 * Sets the duration that transitions will take.
 *
 * Since: 3.10
 */
Matthias Clasen's avatar
Matthias Clasen committed
944 945 946 947
void
gtk_revealer_set_transition_duration (GtkRevealer *revealer,
                                      guint        value)
{
948 949
  GtkRevealerPrivate *priv = gtk_revealer_get_instance_private (revealer);

Matthias Clasen's avatar
Matthias Clasen committed
950 951
  g_return_if_fail (GTK_IS_REVEALER (revealer));

952 953 954
  if (priv->transition_duration == value)
    return;

955
  priv->transition_duration = value;
956
  g_object_notify_by_pspec (G_OBJECT (revealer), props[PROP_TRANSITION_DURATION]);
Matthias Clasen's avatar
Matthias Clasen committed
957 958
}

959 960 961 962 963 964 965
/**
 * gtk_revealer_get_transition_type:
 * @revealer: a #GtkRevealer
 *
 * Gets the type of animation that will be used
 * for transitions in @revealer.
 *
966
 * Returns: the current transition type of @revealer
967 968 969
 *
 * Since: 3.10
 */
Matthias Clasen's avatar
Matthias Clasen committed
970 971 972
GtkRevealerTransitionType
gtk_revealer_get_transition_type (GtkRevealer *revealer)
{
973 974
  GtkRevealerPrivate *priv = gtk_revealer_get_instance_private (revealer);

Matthias Clasen's avatar
Matthias Clasen committed
975 976
  g_return_val_if_fail (GTK_IS_REVEALER (revealer), GTK_REVEALER_TRANSITION_TYPE_NONE);

977
  return priv->transition_type;
Matthias Clasen's avatar
Matthias Clasen committed
978 979
}

980
/**
981 982
 * gtk_revealer_set_transition_type:
 * @revealer: a #GtkRevealer
983 984 985 986 987 988 989 990
 * @transition: the new transition type
 *
 * Sets the type of animation that will be used for
 * transitions in @revealer. Available types include
 * various kinds of fades and slides.
 *
 * Since: 3.10
 */
Matthias Clasen's avatar
Matthias Clasen committed
991 992 993 994
void
gtk_revealer_set_transition_type (GtkRevealer               *revealer,
                                  GtkRevealerTransitionType  transition)
{
995 996
  GtkRevealerPrivate *priv = gtk_revealer_get_instance_private (revealer);

Matthias Clasen's avatar
Matthias Clasen committed
997 998
  g_return_if_fail (GTK_IS_REVEALER (revealer));

999 1000 1001
  if (priv->transition_type == transition)
    return;

1002
  priv->transition_type = transition;
Matthias Clasen's avatar
Matthias Clasen committed
1003
  gtk_widget_queue_resize (GTK_WIDGET (revealer));
1004
  g_object_notify_by_pspec (G_OBJECT (revealer), props[PROP_TRANSITION_TYPE]);
Matthias Clasen's avatar
Matthias Clasen committed
1005
}