summaryrefslogtreecommitdiff
path: root/endless/eospagemanager.c
blob: 98f6108abf98e7808f629fb0f72147fe83c4a161 (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
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
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
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
/* Copyright 2013 Endless Mobile, Inc. */

#include "config.h"
#include "eospagemanager.h"

#include <gtk/gtk.h>

#include <string.h>

/**
 * SECTION:page-manager
 * @short_description: Controlling the flow of your application
 * @title: Page Manager
 *
 * Your users experience your application as a series of
 * <emphasis>pages</emphasis> &mdash; screens that present a small amount
 * of information or show one feature before moving on to the next page
 * or a previous page.
 *
 * The <emphasis>page manager</emphasis> controls how these pages relate to
 * each other.
 * There are several different page managers available, each representing a
 * different user interaction.
 * The default page manager, described in this section of the manual, lets you
 * add any number of pages and switch between them however you like, but there
 * are also other, more specialized ones:
 * for example, the #EosSplashScreenPageManager displays a splash screen and
 * later turns control over to a different page or page manager when you signal
 * it to;
 * and the #EosTabbedPageManager creates a tabbed interface in your window, much
 * like the one in your browser.
 *
 * Each window has a page manager; one is created by default when you create
 * the window, but you can replace it by a different one.
 * You can also nest page managers, one inside the other, in order to create
 * more complex application flows.
 *
 * A page can be any widget, most likely a container widget with other widgets
 * inside it.
 * To add a page to a page manager, call
 * |[
 * gtk_container_add (GTK_CONTAINER (page_manager), page);
 * ]|
 * If the added page is the only page, then the page manager will display it
 * immediately.
 * If the page manager was already displaying another page, then adding a new
 * page will not change which page is displayed.
 *
 * To get information about how to display the pages, for example the background
 * image to use, the page manager reads the page's <emphasis>child
 * properties</emphasis>.
 * These are like regular properties, but instead of modifying the page, they
 * modify the relationship between the page and the page manager that contains
 * it.
 * Most pages have at least a name and a background image as child properties.
 * You can add a page with child properties as follows:
 * |[
 * gtk_container_add_with_properties (GTK_CONTAINER (page_manager), page,
 *                                    "name", "front-page",
 *                                    "background", "image.jpg",
 *                                    NULL);
 * ]|
 * In Javascript, this has been simplified to use JSON:
 * |[
 * page_manager.add(page, {
 *     name: 'front-page',
 *     background: 'image.jpg'
 * });
 * ]|
 * To remove a page, use gtk_container_remove() or
 * eos_page_manager_remove_page_by_name().
 * If the removed page was the only page, then the page manager will display
 * nothing.
 * If that page was currently displaying but was not the only page, then the
 * page manager will display another page; which page is undefined.
 *
 * <warning>
 *   <para>Removing pages with gtk_container_remove() is currently broken due to
 *   a bug in GTK. Use eos_page_manager_remove_page_by_name() for the time
 *   being.</para>
 * </warning>
 *
 * In general, it is convenient to refer to a page by its name when dealing with
 * the page manager, so you should make a point of giving all your pages names.
 */

G_DEFINE_TYPE (EosPageManager, eos_page_manager, GTK_TYPE_CONTAINER)

#define PAGE_MANAGER_PRIVATE(o) \
  (G_TYPE_INSTANCE_GET_PRIVATE ((o), EOS_TYPE_PAGE_MANAGER, EosPageManagerPrivate))

typedef struct _EosPageManagerPageInfo EosPageManagerPageInfo;
struct _EosPageManagerPageInfo
{
  GtkWidget *page;
  gchar *name;
  gboolean fake_page_actions_visible;
  GtkWidget *custom_toolbox_widget;
};

struct _EosPageManagerPrivate
{
  GtkWidget *stack;
  GList *page_info; /* GList<EosPageManagerPageInfo> */
  GHashTable *pages_by_name; /* GHashTable<gchar *, EosPageManagerPageInfo *> */
  GHashTable *pages_by_widget; /* GHashTable<GtkWidget *, EosPageManagerPageInfo *> */
  EosPageManagerPageInfo *visible_page_info;
};

enum
{
  PROP_0,
  PROP_VISIBLE_PAGE,
  PROP_VISIBLE_PAGE_NAME,
  NPROPS
};

enum
{
  CHILD_PROP_0,
  CHILD_PROP_NAME,
  CHILD_PROP_PAGE_ACTIONS,
  CHILD_PROP_CUSTOM_TOOLBOX_WIDGET,
  NCHILDPROPS
};

static GParamSpec *eos_page_manager_props[NPROPS] = { NULL, };
static GParamSpec *eos_page_manager_child_props[NCHILDPROPS] = { NULL, };

static void
page_info_free (EosPageManagerPageInfo *info)
{
  g_free (info->name);
  g_slice_free (EosPageManagerPageInfo, info);
}

/*
 * find_page_info_by_widget:
 * @self: the page manager
 * @page: the page to look for
 *
 * Searches for the page info corresponding to the child @page.
 *
 * Returns: the #EosPageManagerPageInfo for @page, or %NULL if @page is not a
 * child of @self.
 */
static EosPageManagerPageInfo *
find_page_info_by_widget (EosPageManager *self,
                          GtkWidget      *page)
{
  return g_hash_table_lookup (self->priv->pages_by_widget, page);
}

/*
 * find_page_info_by_name:
 * @self: the page manager
 * @name: the name to look for
 *
 * Searches for the page info corresponding to the child with name @name.
 *
 * Returns: the #EosPageManagerPageInfo for @name, or %NULL if @name is not the
 * name of a child of @self.
 */
static EosPageManagerPageInfo *
find_page_info_by_name (EosPageManager *self,
                        const gchar    *name)
{
  return g_hash_table_lookup (self->priv->pages_by_name, name);
}

/* Convenience function, since this warning occurs at several places */
static void
warn_page_widget_not_found (EosPageManager *self,
                            GtkWidget      *page)
{
  g_critical ("Page at %p (type %s) is not a child of EosPageManager %p",
              page,
              g_type_name (G_OBJECT_TYPE (page)),
              self);
}

/* Convenience function, since this warning occurs at several places */
static void
warn_page_name_not_found (EosPageManager *self,
                          const gchar    *name)
{
  g_critical ("EosPageManager %p has no page named %s",
              self,
              name);
}

/* Invariants: number of pages in list and number of pages in pages_by_widget
hash table must be equal; and number of pages in pages_by_name hash table must
be equal or less. This check is expensive, should only be enabled for debugging.
*/
static void
assert_internal_state (EosPageManager *self)
{
#ifdef DEBUG
  guint list_length = g_list_length (self->priv->page_info);
  g_assert_cmpuint (list_length,
                    ==,
                    g_hash_table_size (self->priv->pages_by_widget));
  g_assert_cmpuint (list_length,
                    >=,
                    g_hash_table_size (self->priv->pages_by_name));
#endif
}

static void
set_visible_page_from_info (EosPageManager         *self,
                            EosPageManagerPageInfo *info)
{
  /* FIXME when porting to GtkStack */
  GtkNotebook *stack_notebook = GTK_NOTEBOOK (self->priv->stack);
  int page_pos = gtk_notebook_page_num (stack_notebook, info->page);
  /* Invariant: if info is not NULL, then page must be in stack */
  g_assert (page_pos != -1);
  gtk_notebook_set_current_page (stack_notebook, page_pos);

  self->priv->visible_page_info = info;

  GObject *self_object = G_OBJECT (self);
  g_object_notify(self_object, "visible-page");
  g_object_notify(self_object, "visible-page-name");
}

static void
eos_page_manager_get_property (GObject    *object,
                               guint       property_id,
                               GValue     *value,
                               GParamSpec *pspec)
{
  EosPageManager *self = EOS_PAGE_MANAGER (object);

  switch (property_id)
    {
    case PROP_VISIBLE_PAGE:
      g_value_set_object (value, eos_page_manager_get_visible_page (self));
      break;

    case PROP_VISIBLE_PAGE_NAME:
      g_value_set_string (value, eos_page_manager_get_visible_page_name (self));
      break;

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

static void
eos_page_manager_set_property (GObject      *object,
                               guint         property_id,
                               const GValue *value,
                               GParamSpec   *pspec)
{
  EosPageManager *self = EOS_PAGE_MANAGER (object);

  switch (property_id)
    {
    case PROP_VISIBLE_PAGE:
      eos_page_manager_set_visible_page (self, g_value_get_object (value));
      break;

    case PROP_VISIBLE_PAGE_NAME:
      eos_page_manager_set_visible_page_name (self, g_value_get_string (value));
      break;

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

static void
eos_page_manager_finalize (GObject *object)
{
  EosPageManager *self = EOS_PAGE_MANAGER (object);

  g_list_foreach (self->priv->page_info, (GFunc)page_info_free, NULL);
  g_hash_table_destroy(self->priv->pages_by_widget);
  g_hash_table_destroy(self->priv->pages_by_name);

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

static GtkSizeRequestMode
eos_page_manager_get_request_mode (GtkWidget *widget)
{
  EosPageManager *self = EOS_PAGE_MANAGER (widget);

  return gtk_widget_get_request_mode (self->priv->stack);
}

static void
eos_page_manager_get_preferred_height (GtkWidget       *widget,
                                       gint            *minimum,
                                       gint            *natural)
{
  EosPageManager *self = EOS_PAGE_MANAGER (widget);

  gtk_widget_get_preferred_height (self->priv->stack, minimum, natural);
}

static void
eos_page_manager_get_preferred_width_for_height (GtkWidget       *widget,
                                                 gint             height,
                                                 gint            *minimum,
                                                 gint            *natural)
{
  EosPageManager *self = EOS_PAGE_MANAGER (widget);

  gtk_widget_get_preferred_height_for_width (self->priv->stack, height,
                                             minimum, natural);
}

static void
eos_page_manager_get_preferred_width (GtkWidget       *widget,
                                      gint            *minimum,
                                      gint            *natural)
{
  EosPageManager *self = EOS_PAGE_MANAGER (widget);

  gtk_widget_get_preferred_width (self->priv->stack, minimum, natural);
}

static void
eos_page_manager_get_preferred_height_for_width (GtkWidget       *widget,
                                                 gint             width,
                                                 gint            *minimum,
                                                 gint            *natural)
{
  EosPageManager *self = EOS_PAGE_MANAGER (widget);

  gtk_widget_get_preferred_height_for_width (self->priv->stack, width,
                                             minimum, natural);
}

static void
eos_page_manager_size_allocate (GtkWidget *widget,
                                GtkAllocation *allocation)
{
  EosPageManager *self = EOS_PAGE_MANAGER (widget);

  gtk_widget_set_allocation (widget, allocation);
  gtk_widget_size_allocate (self->priv->stack, allocation);
}

static void
eos_page_manager_show_all (GtkWidget *widget)
{
  EosPageManager *self = EOS_PAGE_MANAGER (widget);

  GTK_WIDGET_CLASS (eos_page_manager_parent_class)->show (widget);
  if (self->priv->stack != NULL)
    gtk_widget_show_all (self->priv->stack);
}

static void
eos_page_manager_map (GtkWidget *widget)
{
  EosPageManager *self = EOS_PAGE_MANAGER (widget);

  if (self->priv->stack != NULL && gtk_widget_get_visible (self->priv->stack))
    gtk_widget_map (self->priv->stack);
  GTK_WIDGET_CLASS (eos_page_manager_parent_class)->map (widget);
}

static void
eos_page_manager_unmap (GtkWidget *widget)
{
  EosPageManager *self = EOS_PAGE_MANAGER (widget);

  if (self->priv->stack != NULL)
    gtk_widget_unmap (self->priv->stack);
  GTK_WIDGET_CLASS (eos_page_manager_parent_class)->unmap (widget);
}

static gboolean
eos_page_manager_draw (GtkWidget *widget,
                       cairo_t   *cr)
{
  EosPageManager *self = EOS_PAGE_MANAGER (widget);

  if (self->priv->stack != NULL)
    gtk_widget_draw (self->priv->stack, cr);

  return FALSE;
}

static void
eos_page_manager_add (GtkContainer *container,
                      GtkWidget    *new_page)
{
  EosPageManager *self = EOS_PAGE_MANAGER (container);

  gtk_container_add (GTK_CONTAINER (self->priv->stack), new_page);
  EosPageManagerPageInfo *info = g_slice_new0 (EosPageManagerPageInfo);
  info->page = new_page;
  self->priv->page_info = g_list_prepend (self->priv->page_info, info);
  g_hash_table_insert (self->priv->pages_by_widget, new_page, info);

  /* If there were no pages yet, then this one must become the visible one */
  if (self->priv->visible_page_info == NULL)
    self->priv->visible_page_info = info;

  assert_internal_state (self);
}

static void
eos_page_manager_remove (GtkContainer *container,
                         GtkWidget    *page)
{
  EosPageManager *self = EOS_PAGE_MANAGER (container);

  gtk_container_remove (GTK_CONTAINER (self->priv->stack), page);
  EosPageManagerPageInfo *info = find_page_info_by_widget (self, page);
  if (info == NULL)
    {
      warn_page_widget_not_found (self, page);
      return;
    }
  self->priv->page_info = g_list_remove (self->priv->page_info, info);
  g_hash_table_remove (self->priv->pages_by_widget, page);
  if (info->name != NULL)
    g_hash_table_remove (self->priv->pages_by_name, info->name);

  /* If this was the only page */
  if (self->priv->visible_page_info == info)
    self->priv->visible_page_info = NULL;

  page_info_free (info);

  assert_internal_state (self);
}

static void
eos_page_manager_forall (GtkContainer *container,
                         gboolean      include_internals,
                         GtkCallback   callback,
                         gpointer      callback_data)
{
  EosPageManager *self = EOS_PAGE_MANAGER (container);

  if (self->priv->stack == NULL)
    return;

  GtkContainerClass *stack_class = GTK_CONTAINER_GET_CLASS (self->priv->stack);
  stack_class->forall (GTK_CONTAINER (self->priv->stack),
                       include_internals,
                       callback,
                       callback_data);
  if (include_internals)
    callback (self->priv->stack, callback_data);
}

static void
eos_page_manager_get_child_property (GtkContainer *container,
                                     GtkWidget    *child,
                                     guint         property_id,
                                     GValue       *value,
                                     GParamSpec   *pspec)
{
  EosPageManager *self = EOS_PAGE_MANAGER (container);

  switch (property_id)
    {
    case CHILD_PROP_NAME:
      g_value_set_string (value, eos_page_manager_get_page_name (self, child));
      break;

    case CHILD_PROP_PAGE_ACTIONS:
      g_value_set_boolean (value,
                           eos_page_manager_get_page_actions (self, child));
      break;

    case CHILD_PROP_CUSTOM_TOOLBOX_WIDGET:
      g_value_set_object (value,
                          eos_page_manager_get_page_custom_toolbox_widget (self,
                                                                           child));
      break;

    default:
      GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container,
                                                    property_id, pspec);
    }
}

static void
eos_page_manager_set_child_property (GtkContainer *container,
                                     GtkWidget    *child,
                                     guint         property_id,
                                     const GValue *value,
                                     GParamSpec   *pspec)
{
  EosPageManager *self = EOS_PAGE_MANAGER (container);

  switch (property_id)
    {
    case CHILD_PROP_NAME:
      eos_page_manager_set_page_name (self, child, g_value_get_string (value));
      break;

    case CHILD_PROP_PAGE_ACTIONS:
      eos_page_manager_set_page_actions (self, child,
                                         g_value_get_boolean (value));
      break;

    case CHILD_PROP_CUSTOM_TOOLBOX_WIDGET:
      eos_page_manager_set_page_custom_toolbox_widget (self, child,
                                                       g_value_get_object (value));
      break;

    default:
      GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container,
                                                    property_id, pspec);
    }
}

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

  g_type_class_add_private (klass, sizeof (EosPageManagerPrivate));

  object_class->get_property = eos_page_manager_get_property;
  object_class->set_property = eos_page_manager_set_property;
  object_class->finalize = eos_page_manager_finalize;

  /* Pass all size requesting and allocation on to the stack */
  widget_class->get_request_mode = eos_page_manager_get_request_mode;
  widget_class->get_preferred_height = eos_page_manager_get_preferred_height;
  widget_class->get_preferred_height_for_width =
    eos_page_manager_get_preferred_height_for_width;
  widget_class->get_preferred_width = eos_page_manager_get_preferred_width;
  widget_class->get_preferred_width_for_height =
    eos_page_manager_get_preferred_width_for_height;
  widget_class->size_allocate = eos_page_manager_size_allocate;
  widget_class->show_all = eos_page_manager_show_all;
  widget_class->map = eos_page_manager_map;
  widget_class->unmap = eos_page_manager_unmap;
  widget_class->draw = eos_page_manager_draw;

  container_class->add = eos_page_manager_add;
  container_class->remove = eos_page_manager_remove;
  container_class->forall = eos_page_manager_forall;
  container_class->get_child_property = eos_page_manager_get_child_property;
  container_class->set_child_property = eos_page_manager_set_child_property;

  /**
   * EosPageManager:visible-page:
   *
   * A reference to the page widget that is currently being displayed by the
   * page manager.
   * If the page manager has no pages, then this is %NULL.
   */
  eos_page_manager_props[PROP_VISIBLE_PAGE] =
    g_param_spec_object ("visible-page", "Visible page",
                         "Page widget currently displaying in the page manager",
                         GTK_TYPE_WIDGET,
                         G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
  /**
   * EosPageManager:visible-page-name:
   *
   * The name of the page that is currently being displayed by the page manager.
   * If the page manager has no pages, or if there is a page currently being
   * displayed but it has no name, then this is %NULL.
   */
  eos_page_manager_props[PROP_VISIBLE_PAGE_NAME] =
    g_param_spec_string ("visible-page-name", "Visible page name",
                         "Name of page currently displaying in the page "
                         "manager",
                         "",
                         G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);

  g_object_class_install_properties (object_class, NPROPS,
                                     eos_page_manager_props);

  /**
   * EosPageManager:name:
   *
   * The name of this page. Make sure to choose a unique name.
   */
  eos_page_manager_child_props[CHILD_PROP_NAME] =
    g_param_spec_string ("name", "Name", "Unique ID for the page",
                         NULL,
                         G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);

  /**
   * EosPageManager:page-actions:
   *
   * The actions exported by this page, to be displayed in the action area on
   * the right of the window.
   *
   * <warning><para>Currently, this property is a boolean value. %TRUE means
   *   to display a fake action area, and %FALSE means don't display.
   * </para></warning>
   */
  eos_page_manager_child_props[CHILD_PROP_PAGE_ACTIONS] =
    g_param_spec_boolean ("page-actions", "Page Actions",
                          "Actions the page exports into the action area",
                          FALSE,
                          G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);

  /**
   * EosPageManager:custom-toolbox-widget:
   *
   * The custom toolbox widget belonging to this page, to be displayed on the
   * left of the window when the page is displaying. Setting this to %NULL
   * indicates that there should be no toolbox widget.
   *
   * <warning><para>Currently, there is no such thing as a
   *   <emphasis>non-</emphasis>custom toolbox widget.
   * </para></warning>
   */
  eos_page_manager_child_props[CHILD_PROP_CUSTOM_TOOLBOX_WIDGET] =
    g_param_spec_object ("custom-toolbox-widget", "Custom toolbox widget",
                         "Custom toolbox widget displayed left of the page",
                         GTK_TYPE_WIDGET,
                         G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);

  /* Install child properties all at once, because there is no
  gtk_container_class_install_child_properties() function */
  int count;
  for (count = PROP_0 + 1; count < NCHILDPROPS; count++)
    gtk_container_class_install_child_property (container_class, count,
                                                eos_page_manager_child_props[count]);
}

static void
eos_page_manager_init (EosPageManager *self)
{
  GtkWidget *self_widget = GTK_WIDGET (self);
  self->priv = PAGE_MANAGER_PRIVATE (self);
  self->priv->pages_by_widget = g_hash_table_new (g_direct_hash,
                                                  g_direct_equal);
  self->priv->pages_by_name = g_hash_table_new_full (g_str_hash,
                                                     g_str_equal,
                                                     g_free,
                                                     NULL);

  gtk_widget_set_has_window (self_widget, FALSE);

  /* TODO replace with GtkStack */
  self->priv->stack = gtk_notebook_new ();
  g_object_set (self->priv->stack,
                "show-tabs", FALSE,
                "show-border", FALSE,
                NULL);
  gtk_widget_set_parent (self->priv->stack, self_widget);
}

/* Public API */

/**
 * eos_page_manager_new:
 *
 * Creates a new default page manager.
 *
 * Returns: the new page manager.
 */
GtkWidget *
eos_page_manager_new (void)
{
  return g_object_new (EOS_TYPE_PAGE_MANAGER, NULL);
}

/**
 * eos_page_manager_get_visible_page:
 * @self: the page manager
 *
 * Gets the page widget that @self is currently displaying.
 * See #EosPageManager:visible-page for more information.
 *
 * Returns: (transfer none): the page #GtkWidget, or %NULL if @self does not
 * have any pages.
 */
GtkWidget *
eos_page_manager_get_visible_page (EosPageManager *self)
{
  g_return_val_if_fail (EOS_IS_PAGE_MANAGER (self), NULL);

  if(self->priv->visible_page_info == NULL)
    return NULL;

  return self->priv->visible_page_info->page;
}

/**
 * eos_page_manager_set_visible_page:
 * @self: the page manager
 * @page: the page to switch to
 *
 * Switches the page manager @self to display @page.
 * The @page widget must previously have been added to the page manager.
 * See #EosPageManager:visible-page for more information.
 */
void
eos_page_manager_set_visible_page (EosPageManager *self,
                                   GtkWidget      *page)
{
  g_return_if_fail (EOS_IS_PAGE_MANAGER (self));

  EosPageManagerPageInfo *info = find_page_info_by_widget (self, page);
  if (info == NULL)
    {
      warn_page_widget_not_found (self, page);
      return;
    }

  set_visible_page_from_info (self, info);
}

/**
 * eos_page_manager_get_visible_page_name:
 * @self: the page manager
 *
 * Gets the name of the page widget that @self is currently displaying.
 * See #EosPageManager:visible-page for more information.
 *
 * Returns: (allow-none): the name of the page, or %NULL if @self does not have
 * any pages or if the visible page does not have a name.
 */
const gchar *
eos_page_manager_get_visible_page_name (EosPageManager *self)
{
  g_return_val_if_fail (EOS_IS_PAGE_MANAGER (self), NULL);

  if(self->priv->visible_page_info == NULL)
    return NULL;

  return self->priv->visible_page_info->name;
}

/**
 * eos_page_manager_set_visible_page_name:
 * @self: the page manager
 * @page_name: the name of the page to switch to
 *
 * Switches the page manager @self to display the page called @page_name.
 * This page must previously have been added to the page manager.
 * See #EosPageManager:visible-page for more information.
 */
void
eos_page_manager_set_visible_page_name (EosPageManager *self,
                                        const gchar    *page_name)
{
  g_return_if_fail (EOS_IS_PAGE_MANAGER (self));
  g_return_if_fail (page_name != NULL);

  EosPageManagerPageInfo *info = find_page_info_by_name (self, page_name);
  if (info == NULL)
    {
      warn_page_name_not_found (self, page_name);
      return;
    }

  set_visible_page_from_info (self, info);
}

/**
 * eos_page_manager_get_page_name:
 * @self: the page manager
 * @page: the page to be queried
 *
 * Gets the name of @page, which must previously have been added to the
 * page manager.
 * See #EosPageManager:name for more information.
 *
 * Returns: (allow-none): the name of @page, or %NULL if @page does not have a
 * name.
 */
const gchar *
eos_page_manager_get_page_name (EosPageManager *self,
                                GtkWidget      *page)
{
  g_return_val_if_fail (EOS_IS_PAGE_MANAGER (self), NULL);
  g_return_val_if_fail (GTK_IS_WIDGET (page), NULL);

  EosPageManagerPageInfo *info = find_page_info_by_widget (self, page);
  if (info == NULL)
    {
      warn_page_widget_not_found (self, page);
      return NULL;
    }

  return info->name;
}

/**
 * eos_page_manager_set_page_name:
 * @self: the page manager
 * @page: the page to be renamed
 * @name: (allow-none): the new name for @page
 *
 * Changes the name of @page, which must previously have been added to the
 * page manager.
 * To remove @page's name, pass %NULL for @name.
 * See #EosPageManager:name for more information.
 */
void
eos_page_manager_set_page_name (EosPageManager *self,
                                GtkWidget      *page,
                                const gchar    *name)
{
  EosPageManagerPageInfo *info;

  g_return_if_fail (EOS_IS_PAGE_MANAGER (self));
  g_return_if_fail (GTK_IS_WIDGET (page));

  /* Two pages with the same name are not allowed */
  if (name != NULL)
    {
      info = find_page_info_by_name (self, name);
      if (info != NULL && info->page != page)
        {
          g_critical ("Not setting page name to \"%s\", because page manager "
                      "already contains a page by that name",
                      name);
          return;
        }
    }

  info = find_page_info_by_widget (self, page);
  if (info == NULL)
    {
      warn_page_widget_not_found (self, page);
      return;
    }

  if (g_strcmp0(info->name, name) == 0)
    return;

  if (info->name != NULL)
    g_hash_table_remove (self->priv->pages_by_name, info->name);
  g_free (info->name);
  info->name = g_strdup (name);
  if (name != NULL)
      g_hash_table_insert (self->priv->pages_by_name, g_strdup (name), info);

  gtk_container_child_notify (GTK_CONTAINER (self), page, "name");

  assert_internal_state (self);
}

/**
 * eos_page_manager_get_page_actions:
 * @self: the page manager
 * @page: the page to be queried
 *
 * Gets whether to display a fake actions area when displaying @page.
 * See #EosPageManager:page-actions for more information.
 *
 * <warning><para>This function is a temporary implementation, do not expect
 *   this API to remain stable.
 * </para></warning>
 *
 * Returns: %TRUE if the fake actions area should be visible when displaying
 * @page, or %FALSE if it should not.
 */
gboolean
eos_page_manager_get_page_actions (EosPageManager *self,
                                   GtkWidget      *page)
{
  g_return_val_if_fail (self != NULL && EOS_IS_PAGE_MANAGER (self), FALSE);
  g_return_val_if_fail (page != NULL && GTK_IS_WIDGET (page), FALSE);

  EosPageManagerPageInfo *info = find_page_info_by_widget (self, page);
  g_return_val_if_fail (info != NULL, FALSE);

  return info->fake_page_actions_visible;
}

/**
 * eos_page_manager_set_page_actions:
 * @self: the page manager
 * @page: the page
 * @actions_visible: whether to display an action area beside @page
 *
 * Sets whether to display a fake actions area when displaying @page.
 * See #EosPageManager:page-actions for more information.
 *
 * <warning><para>This function is a temporary implementation, do not expect
 *   this API to remain stable.
 * </para></warning>
 */
void
eos_page_manager_set_page_actions (EosPageManager *self,
                                   GtkWidget      *page,
                                   gboolean        actions_visible)
{
  g_return_if_fail (self != NULL && EOS_IS_PAGE_MANAGER (self));
  g_return_if_fail (page != NULL && GTK_IS_WIDGET (page));

  EosPageManagerPageInfo *info = find_page_info_by_widget (self, page);
  g_return_if_fail (info != NULL);

  if (info->fake_page_actions_visible == actions_visible)
    return;

  info->fake_page_actions_visible = actions_visible;

  gtk_container_child_notify (GTK_CONTAINER (self), page, "page-actions");
}

/**
 * eos_page_manager_get_page_custom_toolbox_widget:
 * @self: the page manager
 * @page: the page to be queried
 *
 * Retrieves @page's custom toolbox widget, if it has one.
 * See #EosPageManager:custom-toolbox-widget for more information.
 *
 * <note><para>
 *   Currently, there is no possible way to have a non-custom toolbox widget.
 * </para></note>
 *
 * Returns: (transfer none): the custom toolbox #GtkWidget of @page, or %NULL if
 * there is none.
 */
GtkWidget *
eos_page_manager_get_page_custom_toolbox_widget (EosPageManager *self,
                                                 GtkWidget      *page)
{
  g_return_val_if_fail (self != NULL && EOS_IS_PAGE_MANAGER (self), NULL);
  g_return_val_if_fail (page != NULL && GTK_IS_WIDGET (page), NULL);

  EosPageManagerPageInfo *info = find_page_info_by_widget (self, page);
  g_return_val_if_fail (info != NULL, NULL);

  return info->custom_toolbox_widget;
}

/**
 * eos_page_manager_set_page_custom_toolbox_widget:
 * @self: the page manager
 * @page: the page
 * @custom_toolbox_widget: (allow-none): custom toolbox widget for @page
 *
 * Sets the custom toolbox widget to display to the left of @page.
 * See #EosPageManager:custom-toolbox-widget for more information.
 *
 * <note><para>
 *   Currently, there is no possible way to have a non-custom toolbox widget.
 * </para></note>
 */
void
eos_page_manager_set_page_custom_toolbox_widget (EosPageManager *self,
                                                 GtkWidget      *page,
                                                 GtkWidget      *custom_toolbox_widget)
{
  g_return_if_fail (self != NULL && EOS_IS_PAGE_MANAGER (self));
  g_return_if_fail (page != NULL && GTK_IS_WIDGET (page));
  g_return_if_fail (custom_toolbox_widget == NULL ||
                    GTK_IS_WIDGET (custom_toolbox_widget));

  EosPageManagerPageInfo *info = find_page_info_by_widget (self, page);
  g_return_if_fail (info != NULL);

  if (info->custom_toolbox_widget == custom_toolbox_widget)
    return;

  if (info->custom_toolbox_widget)
    g_object_unref (info->custom_toolbox_widget);

  g_object_ref (custom_toolbox_widget);
  info->custom_toolbox_widget = custom_toolbox_widget;

  gtk_container_child_notify (GTK_CONTAINER (self), page,
                              "custom-toolbox-widget");
}

/**
 * eos_page_manager_remove_page_by_name:
 * @self: the page manager
 * @name: the name of the page to remove
 *
 * Removes the page called @name from the page manager.
 * If that page was the only page, then the page manager will display nothing.
 * If that page was currently displaying but was not the only page, then the
 * page manager will display another page; which page is undefined.
 *
 * To remove a page without looking it up by name, use gtk_container_remove().
 */
void
eos_page_manager_remove_page_by_name (EosPageManager *self,
                                      const gchar    *name)
{
  g_return_if_fail (EOS_IS_PAGE_MANAGER (self));
  g_return_if_fail (name != NULL);

  EosPageManagerPageInfo *info = find_page_info_by_name (self, name);
  if (info == NULL)
    {
      warn_page_name_not_found (self, name);
      return;
    }

  /* FIXME: Can't use gtk_container_remove() directly because that asserts
  gtk_widget_get_parent(child) == self || GTK_IS_ASSISTANT(self)
  See https://bugzilla.gnome.org/show_bug.cgi?id=699756 [endlessm/eos-sdk#67] */
  g_signal_emit_by_name (self, "remove", info->page);

  assert_internal_state (self);
}