summaryrefslogtreecommitdiff
path: root/gtk/gtkhidingbox.c
blob: 0a4940237948c8134ee29c2e868ff03231e66a19 (plain)
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
33
34
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
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
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
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
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
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
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
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
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
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
837
838
839
840
841
842
843
844
845
846
847
848
849
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
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
/*
 * Copyright (C) 2015 Rafał Lużyński <digitalfreak@lingonborough.com>
 * Copyright (C) 2015 Red Hat
 *
 * Licensed under the GNU General Public License Version 2
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Authors: Rafał Lużyński <digitalfreak@lingonborough.com>
 *          Carlos Soriano <csoriano@gnome.org>
 */

#include "config.h"

#include "gtkhidingbox.h"
#include "gtkwidgetprivate.h"
#include "gtkintl.h"
#include "gtksizerequest.h"
#include "gtkbuildable.h"
#include "gtkrevealer.h"
#include "gtkadjustment.h"
#include "gtkscrolledwindow.h"
#include "gtkbox.h"

//TODO remove
#include "gtkbutton.h"

#include "glib.h"

#define INVERT_ANIMATION_TIME 500 //ms

typedef enum {
  ANIMATION_PHASE_NONE,
  ANIMATION_PHASE_OUT,
  ANIMATION_PHASE_MOVE,
  ANIMATION_PHASE_IN
} HidingBoxAnimationPhase;

struct _GtkHidingBoxPrivate
{
  GList *children;
  gint16 spacing;
  gint inverted :1;
  GList *widgets_to_hide;
  GList *widgets_to_show;
  GList *widgets_to_remove;
  GList *widgets_shown;
  HidingBoxAnimationPhase animation_phase;
  gint current_width;
  gint current_height;
  guint needs_update :1;

  gboolean invert_animation;

  GtkWidget *scrolled_window;
  GtkWidget *box;
  GtkAdjustment *hadjustment;

  guint tick_id;
  guint64 initial_time;
};

static void
gtk_hiding_box_buildable_init (GtkBuildableIface *iface);

G_DEFINE_TYPE_WITH_CODE (GtkHidingBox, gtk_hiding_box, GTK_TYPE_CONTAINER,
                         G_ADD_PRIVATE (GtkHidingBox)
                         G_IMPLEMENT_INTERFACE (GTK_TYPE_BUILDABLE, gtk_hiding_box_buildable_init))


static void
gtk_hiding_box_buildable_add_child (GtkBuildable *buildable,
                                    GtkBuilder   *builder,
                                    GObject      *child,
                                    const gchar  *type)
{
  GtkHidingBoxPrivate *priv = gtk_hiding_box_get_instance_private (buildable);

  if (!type)
    {
      gtk_container_add (GTK_CONTAINER (buildable), GTK_WIDGET (child));
      priv->needs_update = TRUE;
    }
  else
    {
      GTK_BUILDER_WARN_INVALID_CHILD_TYPE (GTK_HIDING_BOX (buildable), type);
    }
}

static void
gtk_hiding_box_buildable_init (GtkBuildableIface *iface)
{
  iface->add_child = gtk_hiding_box_buildable_add_child;
}

enum {
  PROP_0,
  PROP_SPACING,
  PROP_INVERTED,
  LAST_PROP
};

static GParamSpec *hiding_box_properties[LAST_PROP] = { NULL, };

static void
gtk_hiding_box_set_property (GObject      *object,
                             guint         prop_id,
                             const GValue *value,
                             GParamSpec   *pspec)
{
  GtkHidingBox *box = GTK_HIDING_BOX (object);

  switch (prop_id)
    {
    case PROP_SPACING:
      gtk_hiding_box_set_spacing (box, g_value_get_int (value));
      break;
    case PROP_INVERTED:
      gtk_hiding_box_set_inverted (box, g_value_get_boolean (value));
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
    }
}

static void
gtk_hiding_box_get_property (GObject    *object,
                             guint       prop_id,
                             GValue     *value,
                             GParamSpec *pspec)
{
  GtkHidingBox *box = GTK_HIDING_BOX (object);
  GtkHidingBoxPrivate *priv = gtk_hiding_box_get_instance_private (box);

  switch (prop_id)
    {
    case PROP_SPACING:
      g_value_set_int (value, priv->spacing);
      break;
    case PROP_INVERTED:
      g_value_set_boolean (value, priv->inverted);
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
    }
}

static void
gtk_hiding_box_add (GtkContainer *container,
                    GtkWidget    *widget)
{
  GtkHidingBox *box = GTK_HIDING_BOX (container);
  GtkWidget *revealer;
  GtkHidingBoxPrivate *priv = gtk_hiding_box_get_instance_private (box);
  GtkStyleContext *style_context;

  revealer = gtk_revealer_new ();
  style_context = gtk_widget_get_style_context (revealer);
  gtk_style_context_add_class (style_context, "pathbar-initial-opacity");
  gtk_revealer_set_transition_type (GTK_REVEALER (revealer),
                                    GTK_REVEALER_TRANSITION_TYPE_SLIDE_RIGHT);
  gtk_container_add (GTK_CONTAINER (revealer), widget);
  g_print ("box fine? %s \n", G_OBJECT_TYPE_NAME (priv->box));
  gtk_container_add (GTK_CONTAINER (priv->box), revealer);
  priv->children = g_list_append (priv->children, widget);
  gtk_widget_show (revealer);

  g_print ("add\n");
}

static void
really_remove_child (GtkHidingBox *self,
                     GtkWidget    *widget)
{
  GList *child;
  GtkHidingBoxPrivate *priv = gtk_hiding_box_get_instance_private (self);

  g_print ("really remove child %p %s\n", widget, gtk_button_get_label (GTK_BUTTON (widget)));
  for (child = priv->widgets_to_remove; child != NULL; child = child->next)
    {
      GtkWidget *revealer;

      revealer = gtk_widget_get_parent (child->data);
      g_print ("aver %p\n", child->data);
      if (child->data == widget && !gtk_revealer_get_child_revealed (GTK_REVEALER (revealer)))
        {
          g_print ("############################## INSIDE\n");
          gboolean was_visible = gtk_widget_get_visible (widget);

          priv->widgets_to_remove = g_list_remove (priv->widgets_to_remove,
                                                   child->data);
          gtk_container_remove (GTK_CONTAINER (priv->box), revealer);

          if (was_visible)
            gtk_widget_queue_resize (GTK_WIDGET (self));

          break;
        }
    }
}

static void
unrevealed_really_remove_child (GObject    *widget,
                                GParamSpec *pspec,
                                gpointer    user_data)
{
  GtkHidingBox *self = GTK_HIDING_BOX (user_data);

  g_print ("unrevelaed really remove child %p %s\n", widget, G_OBJECT_TYPE_NAME (widget));
  really_remove_child (self, gtk_bin_get_child (GTK_BIN (widget)));
}

static void
gtk_hiding_box_remove (GtkContainer *container,
                       GtkWidget    *widget)
{
  GList *child;
  GtkHidingBox *box = GTK_HIDING_BOX (container);
  GtkHidingBoxPrivate *priv = gtk_hiding_box_get_instance_private (box);
  GtkWidget *to_remove;

  g_print ("remove %p %s\n", widget, G_OBJECT_TYPE_NAME (widget));
  if (GTK_IS_REVEALER (widget) && gtk_widget_get_parent (widget) == priv->box)
    to_remove = gtk_bin_get_child (widget);
  else
    to_remove = widget;

  priv->widgets_to_remove = g_list_append (priv->widgets_to_remove, to_remove);
  priv->children = g_list_remove (priv->children, to_remove);
  priv->needs_update = TRUE;
  gtk_widget_queue_resize (GTK_WIDGET (container));
}

static void
gtk_hiding_box_forall (GtkContainer *container,
                       gboolean      include_internals,
                       GtkCallback   callback,
                       gpointer      callback_data)
{
  GtkHidingBox *box = GTK_HIDING_BOX (container);
  GtkHidingBoxPrivate *priv = gtk_hiding_box_get_instance_private (box);
  GList *child;

  for (child = priv->children; child != NULL; child = child->next)
    (* callback) (child->data, callback_data);

  if (include_internals)
  {
    (* callback) (priv->scrolled_window, callback_data);

    for (child = priv->widgets_to_remove; child != NULL; child = child->next)
      (* callback) (child->data, callback_data);
  }
}

static void
clear_animation_state (GtkHidingBox *self)
{
  GtkHidingBoxPrivate *priv = gtk_hiding_box_get_instance_private (self);

  priv->animation_phase = ANIMATION_PHASE_NONE;
  g_list_free (priv->widgets_to_show);
  priv->widgets_to_show = NULL;
  g_list_free (priv->widgets_to_hide);
  priv->widgets_to_hide = NULL;
}

static gboolean
update_children_visibility (GtkHidingBox     *box,
                            GtkAllocation    *allocation,
                            GtkRequestedSize *sizes,
                            gboolean          update,
                            gint             *children_size,
                            gint             *n_visible_children_expanding)
{
  GtkHidingBoxPrivate *priv = gtk_hiding_box_get_instance_private (box);
  GtkWidget *child_widget;
  GList *child;
  GtkRequestedSize *sizes_temp;
  gint i;
  GList *children;
  GList *temp_widgets_to_show = NULL;
  gboolean allocate_more_children = TRUE;
  gint n_visible_children = 0;

  g_list_free (priv->widgets_to_show);
  priv->widgets_to_show = NULL;
  g_list_free (priv->widgets_to_hide);
  priv->widgets_to_hide = NULL;

  *n_visible_children_expanding = 0;
  *children_size = -priv->spacing;
  children = g_list_copy (priv->children);
  sizes_temp = g_newa (GtkRequestedSize, g_list_length (priv->children));
  if (priv->inverted)
    children = g_list_reverse (children);

  /* Retrieve desired size for visible children. */
  for (i = 0, child = children; child != NULL; i++, child = child->next)
    {
      child_widget = GTK_WIDGET (child->data);

      gtk_widget_get_preferred_width_for_height (child_widget,
                                                 allocation->height,
                                                 &sizes_temp[i].minimum_size,
                                                 &sizes_temp[i].natural_size);
      /* Assert the api is working properly */
      if (sizes_temp[i].minimum_size < 0)
        g_error ("GtkHidingBox child %s minimum width: %d < 0 for height %d",
                 gtk_widget_get_name (child_widget),
                 sizes_temp[i].minimum_size, allocation->height);

      if (sizes_temp[i].natural_size < sizes_temp[i].minimum_size)
        g_error ("GtkHidingBox child %s natural width: %d < minimum %d for height %d",
                 gtk_widget_get_name (child_widget),
                 sizes_temp[i].natural_size, sizes_temp[i].minimum_size,
                 allocation->height);

      *children_size += sizes_temp[i].minimum_size + priv->spacing;
      sizes_temp[i].data = child_widget;

      if (!allocate_more_children || *children_size > allocation->width)
        {
          allocate_more_children = FALSE;
          if (gtk_revealer_get_child_revealed (GTK_REVEALER (gtk_widget_get_parent (child_widget))))
            priv->widgets_to_hide = g_list_append (priv->widgets_to_hide, child_widget);

          continue;
        }

      if (gtk_widget_get_hexpand (child_widget))
        (n_visible_children_expanding)++;
      (n_visible_children)++;

      if (!g_list_find (priv->widgets_to_remove, child_widget))
        priv->widgets_to_show = g_list_append (priv->widgets_to_show, child_widget);
    }

  for (i = 0; i < n_visible_children; i++)
    {
      if (priv->inverted)
        {
          sizes[n_visible_children - i - 1].minimum_size = sizes_temp[i].minimum_size;
          sizes[n_visible_children - i - 1].natural_size = sizes_temp[i].natural_size;
        }
      else
        {
          sizes[i].minimum_size = sizes_temp[i].minimum_size;
          sizes[i].natural_size = sizes_temp[i].natural_size;
        }
    }

  g_list_free (children);
}

static gboolean
needs_update (GtkHidingBox  *box,
              GtkAllocation *allocation)
{
  GtkHidingBoxPrivate *priv = gtk_hiding_box_get_instance_private (box);
  GtkWidget *child_widget;
  GList *child;
  GtkRequestedSize *sizes_temp;
  gint i;
  GList *children;
  GList *widget_shown;
  GList *widgets_to_show = NULL;
  gboolean needs_update = FALSE;
  gboolean allocate_more_children = TRUE;
  gint children_size = -priv->spacing;

  if (priv->needs_update)
    return TRUE;

  if (allocation->width != priv->current_width ||
      allocation->height != priv->current_height)
    return TRUE;

  children = g_list_copy (priv->children);
  sizes_temp = g_newa (GtkRequestedSize, g_list_length (priv->children));
  if (priv->inverted)
    children = g_list_reverse (children);

  widget_shown = priv->widgets_shown;
  /* Retrieve desired size for visible children. */
  for (i = 0, child = children; child != NULL; i++, child = child->next)
    {
      child_widget = GTK_WIDGET (child->data);
      if (!gtk_widget_get_visible (child_widget) || !allocate_more_children)
        {
          needs_update = child->data != widget_shown->data;
          widgets_to_show = g_list_prepend (widgets_to_show, child->data);
          widget_shown = widget_shown->next;
          if (needs_update)
            {
              break;
            }
          continue;
        }
      else
        {
          needs_update = child->data == widget_shown->data;
          if (needs_update)
            {
              break;
            }
        }

      gtk_widget_get_preferred_width_for_height (child_widget,
                                                 allocation->height,
                                                 &sizes_temp[i].minimum_size,
                                                 &sizes_temp[i].natural_size);
      /* Assert the api is working properly */
      if (sizes_temp[i].minimum_size < 0)
        g_error ("GtkHidingBox child %s minimum width: %d < 0 for height %d",
                 gtk_widget_get_name (child_widget),
                 sizes_temp[i].minimum_size, allocation->height);

      if (sizes_temp[i].natural_size < sizes_temp[i].minimum_size)
        g_error ("GtkHidingBox child %s natural width: %d < minimum %d for height %d",
                 gtk_widget_get_name (child_widget),
                 sizes_temp[i].natural_size, sizes_temp[i].minimum_size,
                 allocation->height);

      children_size += sizes_temp[i].minimum_size + priv->spacing;
      sizes_temp[i].data = child_widget;

      if (children_size > allocation->width)
        {
          allocate_more_children = FALSE;
        }
    }

  needs_update = needs_update || widget_shown != NULL;

  g_list_free (children);

  return needs_update;
}

static void
opacity_on (GObject    *widget,
            GParamSpec *pspec,
            gpointer    user_data)
{
  GtkHidingBoxPrivate *priv = gtk_hiding_box_get_instance_private (GTK_HIDING_BOX (user_data));

  g_print ("############opacity on!!!!!\n");
  g_signal_handlers_disconnect_by_func (widget, opacity_on, user_data);
  priv->widgets_to_show = g_list_remove (priv->widgets_to_show,
                                         gtk_bin_get_child (GTK_BIN (widget)));
}

static void
opacity_off (GObject    *widget,
             GParamSpec *pspec,
             gpointer    user_data)
{
  GtkHidingBoxPrivate *priv = gtk_hiding_box_get_instance_private (GTK_HIDING_BOX (user_data));

  g_print ("############opacity off!!!!!\n");
  g_signal_handlers_disconnect_by_func (widget, opacity_off, user_data);
  priv->widgets_to_hide = g_list_remove (priv->widgets_to_hide,
                                         gtk_bin_get_child (GTK_BIN (widget)));
}

static void
idle_update_revealers (GtkHidingBox *box)
{
  GtkHidingBoxPrivate *priv = gtk_hiding_box_get_instance_private (box);
  GList *l;

  for (l = priv->widgets_to_hide; l != NULL; l = l->next)
    {
      GtkRevealer *revealer;

      g_print ("update revealer hide %s\n", gtk_button_get_label (GTK_BUTTON (l->data)));
      revealer = GTK_REVEALER (gtk_widget_get_parent (l->data));
      if (gtk_revealer_get_reveal_child (revealer))
        {
          GtkStyleContext *style_context;

          style_context = gtk_widget_get_style_context (GTK_WIDGET (revealer));
          gtk_style_context_remove_class (style_context, "pathbar-initial-opacity");
          gtk_style_context_remove_class (style_context, "pathbar-opacity-on");
          gtk_style_context_add_class (style_context, "pathbar-opacity-off");
          g_signal_connect (revealer, "notify::child-revealed", (GCallback) opacity_off, box);
          gtk_revealer_set_reveal_child (revealer, FALSE);
        }
    }

  for (l = priv->widgets_to_remove; l != NULL; l = l->next)
    {
      GtkRevealer *revealer;

      g_print ("update revealer remove %s\n", gtk_button_get_label (GTK_BUTTON (l->data)));
      revealer = GTK_REVEALER (gtk_widget_get_parent (l->data));
      if (gtk_revealer_get_child_revealed (revealer))
        {
          GtkStyleContext *style_context;

          style_context = gtk_widget_get_style_context (GTK_WIDGET (revealer));
          gtk_style_context_remove_class (style_context, "pathbar-initial-opacity");
          gtk_style_context_remove_class (style_context, "pathbar-opacity-on");
          gtk_style_context_add_class (style_context, "pathbar-opacity-off");
          g_signal_connect (revealer, "notify::child-revealed",
                            (GCallback) unrevealed_really_remove_child, box);
          gtk_revealer_set_reveal_child (revealer, FALSE);
        }
      else
        {
          g_print ("widget to remove NOT revealed %p\n", l->data);
          really_remove_child (box, l->data);
        }
    }

  if (priv->widgets_to_remove || priv->widgets_to_hide)
    return;

  for (l = priv->widgets_to_show; l != NULL; l = l->next)
    {
      GtkRevealer *revealer;

      revealer = GTK_REVEALER (gtk_widget_get_parent (l->data));
      if (!gtk_revealer_get_reveal_child (revealer))
        {
          GtkStyleContext *style_context;

          style_context = gtk_widget_get_style_context (GTK_WIDGET (revealer));
          gtk_style_context_remove_class (style_context, "pathbar-opacity-off");
          gtk_style_context_remove_class (style_context, "pathbar-initial-opacity");
          gtk_style_context_add_class (style_context, "pathbar-opacity-on");
          gtk_revealer_set_reveal_child (revealer, TRUE);
          g_signal_connect (revealer, "notify::child-revealed", (GCallback) opacity_on, box);
        }
    }

}

static void
gtk_hiding_box_size_allocate (GtkWidget     *widget,
                              GtkAllocation *allocation)
{
  GtkHidingBox *box = GTK_HIDING_BOX (widget);
  GtkHidingBoxPrivate *priv = gtk_hiding_box_get_instance_private (box);
  GtkAllocation child_allocation;
  GtkRequestedSize *sizes;
  gint n_visible_children_expanding = 0;
  gint children_size = 0;

  gtk_widget_set_allocation (widget, allocation);

  sizes = g_newa (GtkRequestedSize, g_list_length (priv->children));
  update_children_visibility (box, allocation, sizes, FALSE, &children_size,
                              &n_visible_children_expanding);

  idle_update_revealers (box);

  child_allocation.x = allocation->x;
  child_allocation.y = allocation->y;
  child_allocation.width = allocation->width;
  child_allocation.height = allocation->height;
  gtk_widget_size_allocate (priv->scrolled_window, &child_allocation);

  _gtk_widget_set_simple_clip (widget, NULL);
}

static void
finish_invert_animation (GtkHidingBox *self)
{
  GtkHidingBoxPrivate *priv = gtk_hiding_box_get_instance_private (self);

  priv->invert_animation = FALSE;
}


static void
invert_animation_on_tick (GtkWidget     *widget,
                          GdkFrameClock *frame_clock,
                          gpointer       user_data)
{
  GtkHidingBox *self = GTK_HIDING_BOX (user_data);
  GtkHidingBoxPrivate *priv = gtk_hiding_box_get_instance_private (self);
  guint64 elapsed;
  gfloat progress;
  gdouble adjustment_value;

  if (!priv->initial_time)
    priv->initial_time = gdk_frame_clock_get_frame_time (frame_clock);

  elapsed = gdk_frame_clock_get_frame_time (frame_clock) - priv->initial_time;
  progress = elapsed / INVERT_ANIMATION_TIME;

  if (progress >= 1)
    {
      finish_invert_animation (self);

      return;
    }

  if (priv->inverted)
    adjustment_value = 1 / (progress * (gtk_adjustment_get_lower (priv->hadjustment) - gtk_adjustment_get_upper (priv->hadjustment)));
  else
    adjustment_value = progress * (gtk_adjustment_get_lower (priv->hadjustment) - gtk_adjustment_get_upper (priv->hadjustment));

  gtk_adjustment_set_value (priv->hadjustment, adjustment_value);
}

static void
start_invert_animation (GtkHidingBox *self)
{
  GtkHidingBoxPrivate *priv = gtk_hiding_box_get_instance_private (self);
  GList *child;

  priv->invert_animation = TRUE;

  for (child = priv->children; child != NULL; child = child->next)
    {
      GtkWidget *revealer;

      revealer = gtk_widget_get_parent (GTK_WIDGET (child->data));
      gtk_revealer_set_transition_duration (GTK_REVEALER (revealer), 0);
      gtk_revealer_set_reveal_child (GTK_REVEALER (revealer), TRUE);
    }

  priv->tick_id = gtk_widget_add_tick_callback (priv->scrolled_window,
                                                (GtkTickCallback) invert_animation_on_tick,
                                                self, NULL);
}

static void
hadjustment_on_changed (GtkAdjustment *hadjustment,
                        gpointer       user_data)
{
  GtkHidingBox *box = GTK_HIDING_BOX (user_data);
  GtkHidingBoxPrivate *priv = gtk_hiding_box_get_instance_private (box);

  if (priv->invert_animation)
    return;

  if (priv->inverted)
    gtk_adjustment_set_value (hadjustment, gtk_adjustment_get_upper (hadjustment));
  else
    gtk_adjustment_set_value (hadjustment, gtk_adjustment_get_lower (hadjustment));
}

static void
gtk_hiding_box_get_preferred_width (GtkWidget *widget,
                                    gint      *minimum_width,
                                    gint      *natural_width)
{
  GtkHidingBox *box = GTK_HIDING_BOX (widget);
  GtkHidingBoxPrivate *priv = gtk_hiding_box_get_instance_private (box);
  gint child_minimum_width;
  gint child_natural_width;
  GList *child;
  gint n_visible_children;
  gboolean have_min = FALSE;
  GList *children;

  *minimum_width = 0;
  *natural_width = 0;

  children = g_list_copy (priv->children);
  if (priv->inverted)
    children = g_list_reverse (children);

  n_visible_children = 0;
  for (child = children; child != NULL; child = child->next)
    {
      if (!gtk_widget_is_visible (child->data))
        continue;

      ++n_visible_children;
      gtk_widget_get_preferred_width (child->data, &child_minimum_width, &child_natural_width);
      /* Minimum is a minimum of the first visible child */
      if (!have_min)
        {
          *minimum_width = child_minimum_width;
          have_min = TRUE;
        }
      /* Natural is a sum of all visible children */
      *natural_width += child_natural_width;
    }

  /* Natural must also include the spacing */
  if (priv->spacing && n_visible_children > 1)
    *natural_width += priv->spacing * (n_visible_children - 1);

  g_list_free (children);
}

static void
gtk_hiding_box_get_preferred_height (GtkWidget *widget,
                                     gint      *minimum_height,
                                     gint      *natural_height)
{
  GtkHidingBox *box = GTK_HIDING_BOX (widget);
  GtkHidingBoxPrivate *priv = gtk_hiding_box_get_instance_private (box);
  gint child_minimum_height;
  gint child_natural_height;
  GList *child;

  *minimum_height = 0;
  *natural_height = 0;
  for (child = priv->children; child != NULL; child = child->next)
    {
      if (!gtk_widget_is_visible (child->data))
        continue;

      gtk_widget_get_preferred_height (child->data, &child_minimum_height, &child_natural_height);
      *minimum_height = MAX (*minimum_height, child_minimum_height);
      *natural_height = MAX (*natural_height, child_natural_height);
    }
}

static GtkSizeRequestMode
gtk_hiding_box_get_request_mode (GtkWidget *self)
{
  return GTK_SIZE_REQUEST_WIDTH_FOR_HEIGHT;
}

static void
on_what (gpointer  data,
         GObject  *where_the_object_was)
{
  G_BREAKPOINT ();
}

static void
gtk_hiding_box_init (GtkHidingBox *box)
{
  GtkHidingBoxPrivate *priv = gtk_hiding_box_get_instance_private (box);
  GtkAdjustment *hadjustment;
  GtkWidget *hscrollbar;

  gtk_widget_set_has_window (GTK_WIDGET (box), FALSE);
  priv->scrolled_window = gtk_scrolled_window_new (NULL, NULL);
  priv->hadjustment = gtk_scrolled_window_get_hadjustment (GTK_SCROLLED_WINDOW (priv->scrolled_window));
  g_signal_connect (priv->hadjustment, "changed", (GCallback) hadjustment_on_changed, box);
  hscrollbar = gtk_scrolled_window_get_hscrollbar (GTK_SCROLLED_WINDOW (priv->scrolled_window));
  gtk_widget_hide (hscrollbar);
  priv->box = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
  g_object_weak_ref (G_OBJECT (priv->box), on_what, NULL);
  gtk_container_add (GTK_CONTAINER (priv->scrolled_window), priv->box);
  gtk_widget_set_parent (priv->scrolled_window, GTK_WIDGET (box));

  priv->invert_animation = FALSE;
  priv->spacing = 0;
  priv->inverted = FALSE;
  priv->widgets_to_hide = NULL;
  priv->widgets_to_show = NULL;
  priv->widgets_to_remove = NULL;
  priv->widgets_shown = NULL;
  priv->animation_phase = ANIMATION_PHASE_NONE;

  gtk_widget_show_all (priv->scrolled_window);
}

static void
gtk_hiding_box_class_init (GtkHidingBoxClass *class)
{
  GObjectClass *object_class = G_OBJECT_CLASS (class);
  GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
  GtkContainerClass *container_class = GTK_CONTAINER_CLASS (class);

  object_class->set_property = gtk_hiding_box_set_property;
  object_class->get_property = gtk_hiding_box_get_property;

  widget_class->size_allocate = gtk_hiding_box_size_allocate;
  widget_class->get_preferred_width = gtk_hiding_box_get_preferred_width;
  widget_class->get_preferred_height = gtk_hiding_box_get_preferred_height;
  widget_class->get_request_mode = gtk_hiding_box_get_request_mode;

  container_class->add = gtk_hiding_box_add;
  container_class->remove = gtk_hiding_box_remove;
  container_class->forall = gtk_hiding_box_forall;

  hiding_box_properties[PROP_SPACING] =
           g_param_spec_int ("spacing",
                             _("Spacing"),
                             _("The amount of space between children"),
                             0, G_MAXINT, 0,
                             G_PARAM_READWRITE|G_PARAM_EXPLICIT_NOTIFY);

  hiding_box_properties[PROP_INVERTED] =
           g_param_spec_int ("inverted",
                             _("Direction of hiding children inverted"),
                             P_("If false the container will start hiding widgets from the end when there is not enough space, and the oposite in case inverted is true."),
                             0, G_MAXINT, 0,
                             G_PARAM_READWRITE);

  g_object_class_install_properties (object_class, LAST_PROP, hiding_box_properties);
}

/**
 * gtk_hiding_box_new:
 *
 * Creates a new #GtkHidingBox.
 *
 * Returns: a new #GtkHidingBox.
 **/
GtkWidget *
gtk_hiding_box_new (void)
{
  return g_object_new (GTK_TYPE_HIDING_BOX, NULL);
}

/**
 * gtk_hiding_box_set_spacing:
 * @box: a #GtkHidingBox
 * @spacing: the number of pixels to put between children
 *
 * Sets the #GtkHidingBox:spacing property of @box, which is the
 * number of pixels to place between children of @box.
 */
void
gtk_hiding_box_set_spacing (GtkHidingBox *box,
                            gint          spacing)
{
  GtkHidingBoxPrivate *priv ;

  g_return_if_fail (GTK_IS_HIDING_BOX (box));

  priv = gtk_hiding_box_get_instance_private (box);

  if (priv->spacing != spacing)
    {
      priv->spacing = spacing;

      g_object_notify (G_OBJECT (box), "spacing");

      gtk_widget_queue_resize (GTK_WIDGET (box));
    }
}

/**
 * gtk_hiding_box_get_spacing:
 * @box: a #GtkHidingBox
 *
 * Gets the value set by gtk_hiding_box_set_spacing().
 *
 * Returns: spacing between children
 **/
gint
gtk_hiding_box_get_spacing (GtkHidingBox *box)
{
  GtkHidingBoxPrivate *priv ;

  g_return_val_if_fail (GTK_IS_HIDING_BOX (box), 0);

  priv = gtk_hiding_box_get_instance_private (box);

  return priv->spacing;
}


void
gtk_hiding_box_set_inverted (GtkHidingBox *box,
                             gboolean      inverted)
{
  GtkHidingBoxPrivate *priv ;

  g_return_if_fail (GTK_IS_HIDING_BOX (box));

  priv = gtk_hiding_box_get_instance_private (box);

  if (priv->inverted != inverted)
    {
      priv->inverted = inverted != FALSE;

      g_object_notify (G_OBJECT (box), "inverted");

      gtk_widget_queue_resize (GTK_WIDGET (box));
    }
}

gboolean
gtk_hiding_box_get_inverted (GtkHidingBox *box)
{
  GtkHidingBoxPrivate *priv ;

  g_return_val_if_fail (GTK_IS_HIDING_BOX (box), 0);

  priv = gtk_hiding_box_get_instance_private (box);

  return priv->inverted;
}

GList *
gtk_hiding_box_get_overflow_children (GtkHidingBox *box)
{
  GtkHidingBoxPrivate *priv ;
  GList *result = NULL;
  GList *l;

  g_return_val_if_fail (GTK_IS_HIDING_BOX (box), 0);

  priv = gtk_hiding_box_get_instance_private (box);

  for (l = priv->children; l != NULL; l = l->next)
    if (gtk_widget_is_visible (l->data) && !gtk_widget_get_child_visible (l->data))
      result = g_list_append (result, l->data);

  return result;
}